1
0
mirror of https://github.com/paboyle/Grid.git synced 2024-11-16 02:35:36 +00:00
Grid/lib/qcd/hmc/GenericHMCrunner.h

220 lines
7.6 KiB
C
Raw Normal View History

/*************************************************************************************
Grid physics library, www.github.com/paboyle/Grid
Source file: ./lib/qcd/hmc/GenericHmcRunner.h
Copyright (C) 2015
Author: paboyle <paboyle@ph.ed.ac.uk>
Author: Guido Cossu <guido.cossu@ed.ac.uk>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
2016-10-21 14:22:25 +01:00
See the full license in the file "LICENSE" in the top level distribution
directory
*************************************************************************************/
/* END LEGAL */
#ifndef GRID_GENERIC_HMC_RUNNER
#define GRID_GENERIC_HMC_RUNNER
2016-12-22 12:41:56 +00:00
#include <unordered_map>
namespace Grid {
namespace QCD {
// very ugly here but possibly resolved if we had a base Reader class
template < class ReaderClass >
class HMCRunnerBase {
2017-01-17 10:46:49 +00:00
public:
virtual void Run() = 0;
virtual void initialize(ReaderClass& ) = 0;
2017-01-17 10:46:49 +00:00
};
2017-01-05 13:09:32 +00:00
template <class Implementation,
template <typename, typename, typename> class Integrator,
class RepresentationsPolicy = NoHirep, class ReaderClass = XmlReader>
class HMCWrapperTemplate: public HMCRunnerBase<ReaderClass> {
2017-01-05 13:09:32 +00:00
public:
INHERIT_FIELD_TYPES(Implementation);
typedef Implementation ImplPolicy; // visible from outside
template <typename S = NoSmearing<Implementation> >
using IntegratorType = Integrator<Implementation, S, RepresentationsPolicy>;
2017-01-17 13:22:18 +00:00
HMCparameters Parameters;
std::string ParameterFile;
2017-01-05 13:09:32 +00:00
HMCResourceManager<Implementation> Resources;
// The set of actions (keep here for lower level users, for now)
2017-01-05 13:09:32 +00:00
ActionSet<Field, RepresentationsPolicy> TheAction;
HMCWrapperTemplate() = default;
HMCWrapperTemplate(HMCparameters Par){
Parameters = Par;
}
void initialize(ReaderClass & TheReader){
std::cout << "Initialization of the HMC" << std::endl;
Resources.initialize(TheReader);
// eventually add smearing
Resources.GetActionSet(TheAction);
}
2017-01-05 13:09:32 +00:00
void ReadCommandLine(int argc, char **argv) {
std::string arg;
2017-01-17 13:22:18 +00:00
if (GridCmdOptionExists(argv, argv + argc, "--StartingType")) {
arg = GridCmdOptionPayload(argv, argv + argc, "--StartingType");
if (arg != "HotStart" && arg != "ColdStart" && arg != "TepidStart" &&
arg != "CheckpointStart") {
std::cout << GridLogError << "Unrecognized option in --StartingType\n";
2017-01-05 13:09:32 +00:00
std::cout
<< GridLogError
<< "Valid [HotStart, ColdStart, TepidStart, CheckpointStart]\n";
2017-01-17 13:22:18 +00:00
exit(1);
2017-01-05 13:09:32 +00:00
}
2017-01-17 13:22:18 +00:00
Parameters.StartingType = arg;
2017-01-05 13:09:32 +00:00
}
2017-02-09 15:18:38 +00:00
if (GridCmdOptionExists(argv, argv + argc, "--StartingTrajectory")) {
arg = GridCmdOptionPayload(argv, argv + argc, "--StartingTrajectory");
2017-01-05 13:09:32 +00:00
std::vector<int> ivec(0);
GridCmdOptionIntVector(arg, ivec);
2017-01-17 13:22:18 +00:00
Parameters.StartTrajectory = ivec[0];
2017-01-05 13:09:32 +00:00
}
if (GridCmdOptionExists(argv, argv + argc, "--Trajectories")) {
arg = GridCmdOptionPayload(argv, argv + argc, "--Trajectories");
std::vector<int> ivec(0);
GridCmdOptionIntVector(arg, ivec);
2017-01-17 13:22:18 +00:00
Parameters.Trajectories = ivec[0];
2017-01-05 13:09:32 +00:00
}
if (GridCmdOptionExists(argv, argv + argc, "--Thermalizations")) {
arg = GridCmdOptionPayload(argv, argv + argc, "--Thermalizations");
std::vector<int> ivec(0);
GridCmdOptionIntVector(arg, ivec);
2017-01-17 13:22:18 +00:00
Parameters.NoMetropolisUntil = ivec[0];
2017-01-05 13:09:32 +00:00
}
if (GridCmdOptionExists(argv, argv + argc, "--ParameterFile")) {
arg = GridCmdOptionPayload(argv, argv + argc, "--ParameterFile");
ParameterFile = arg;
}
2016-12-22 12:41:56 +00:00
}
2017-01-05 13:09:32 +00:00
template <class SmearingPolicy>
void Run(SmearingPolicy &S) {
Runner(S);
}
void Run(){
NoSmearing<Implementation> S;
Runner(S);
2016-12-22 12:41:56 +00:00
}
//////////////////////////////////////////////////////////////////
2017-01-05 13:09:32 +00:00
private:
template <class SmearingPolicy>
void Runner(SmearingPolicy &Smearing) {
auto UGrid = Resources.GetCartesian();
Resources.AddRNGs();
Field U(UGrid);
2017-01-17 10:46:49 +00:00
// Can move this outside?
2017-01-05 13:09:32 +00:00
typedef IntegratorType<SmearingPolicy> TheIntegrator;
2017-03-10 14:54:53 +00:00
TheIntegrator MDynamics(UGrid, Parameters.MD, TheAction, Smearing);
2017-01-05 13:09:32 +00:00
2017-01-17 13:22:18 +00:00
if (Parameters.StartingType == "HotStart") {
2017-01-05 13:09:32 +00:00
// Hot start
Resources.SeedFixedIntegers();
Implementation::HotConfiguration(Resources.GetParallelRNG(), U);
2017-01-17 13:22:18 +00:00
} else if (Parameters.StartingType == "ColdStart") {
2017-01-05 13:09:32 +00:00
// Cold start
Resources.SeedFixedIntegers();
Implementation::ColdConfiguration(Resources.GetParallelRNG(), U);
2017-01-17 13:22:18 +00:00
} else if (Parameters.StartingType == "TepidStart") {
2017-01-05 13:09:32 +00:00
// Tepid start
Resources.SeedFixedIntegers();
Implementation::TepidConfiguration(Resources.GetParallelRNG(), U);
2017-01-17 13:22:18 +00:00
} else if (Parameters.StartingType == "CheckpointStart") {
2017-01-05 13:09:32 +00:00
// CheckpointRestart
2017-01-17 13:22:18 +00:00
Resources.GetCheckPointer()->CheckpointRestore(Parameters.StartTrajectory, U,
2017-01-05 13:09:32 +00:00
Resources.GetSerialRNG(),
Resources.GetParallelRNG());
}
Smearing.set_Field(U);
2017-01-17 13:22:18 +00:00
HybridMonteCarlo<TheIntegrator> HMC(Parameters, MDynamics,
2017-01-05 13:09:32 +00:00
Resources.GetSerialRNG(),
Resources.GetParallelRNG(),
Resources.GetObservables(), U);
2017-01-05 13:09:32 +00:00
// Run it
HMC.evolve();
}
};
2016-12-22 12:41:56 +00:00
// These are for gauge fields, default integrator MinimumNorm2
2017-01-05 13:09:32 +00:00
template <template <typename, typename, typename> class Integrator>
using GenericHMCRunner = HMCWrapperTemplate<PeriodicGimplR, Integrator>;
template <template <typename, typename, typename> class Integrator>
using GenericHMCRunnerF = HMCWrapperTemplate<PeriodicGimplF, Integrator>;
template <template <typename, typename, typename> class Integrator>
using GenericHMCRunnerD = HMCWrapperTemplate<PeriodicGimplD, Integrator>;
2017-02-10 10:27:51 +00:00
// These are for gauge fields, default integrator MinimumNorm2
template <template <typename, typename, typename> class Integrator>
using ConjugateHMCRunner = HMCWrapperTemplate<ConjugateGimplR, Integrator>;
template <template <typename, typename, typename> class Integrator>
using ConjugateHMCRunnerF = HMCWrapperTemplate<ConjugateGimplF, Integrator>;
template <template <typename, typename, typename> class Integrator>
using ConjugateHMCRunnerD = HMCWrapperTemplate<ConjugateGimplD, Integrator>;
2017-01-05 13:09:32 +00:00
template <class RepresentationsPolicy,
template <typename, typename, typename> class Integrator>
using GenericHMCRunnerHirep =
HMCWrapperTemplate<PeriodicGimplR, Integrator, RepresentationsPolicy>;
template <class Implementation, class RepresentationsPolicy,
template <typename, typename, typename> class Integrator>
using GenericHMCRunnerTemplate = HMCWrapperTemplate<Implementation, Integrator, RepresentationsPolicy>;
2017-01-05 13:09:32 +00:00
typedef HMCWrapperTemplate<ScalarImplR, MinimumNorm2, ScalarFields>
ScalarGenericHMCRunner;
typedef HMCWrapperTemplate<ScalarAdjImplR, MinimumNorm2, ScalarMatrixFields>
ScalarAdjGenericHMCRunner;
template <int Colours>
using ScalarNxNAdjGenericHMCRunner = HMCWrapperTemplate < ScalarNxNAdjImplR<Colours>, ForceGradient, ScalarNxNMatrixFields<Colours> >;
} // namespace QCD
} // namespace Grid
2016-12-22 12:41:56 +00:00
#endif // GRID_GENERIC_HMC_RUNNER