1
0
mirror of https://github.com/paboyle/Grid.git synced 2025-04-04 11:15:55 +01:00

Added minimum norm integrator

Little rearrangement of HMC and integrator classes
This commit is contained in:
neo 2015-07-06 16:17:32 +09:00
parent 12e1682a87
commit f95db88d19
3 changed files with 94 additions and 39 deletions

View File

@ -10,7 +10,6 @@
#include <string> #include <string>
#include <memory> #include <memory>
#include <unistd.h>
namespace Grid{ namespace Grid{
namespace QCD{ namespace QCD{
@ -30,10 +29,8 @@ namespace Grid{
class HybridMonteCarlo{ class HybridMonteCarlo{
const HMCparameters Params; const HMCparameters Params;
GridSerialRNG sRNG; GridSerialRNG sRNG;
GridParallelRNG pRNG;
Integrator<IntegType>& MD; Integrator<IntegType>& MD;
bool metropolis_test(const RealD DeltaH){ bool metropolis_test(const RealD DeltaH){
RealD rn_test; RealD rn_test;
RealD prob = std::exp(-DeltaH); RealD prob = std::exp(-DeltaH);
@ -54,7 +51,7 @@ namespace Grid{
RealD evolve_step(LatticeLorentzColourMatrix& U){ RealD evolve_step(LatticeLorentzColourMatrix& U){
MD.init(U,pRNG); // set U and initialize P and phi's MD.init(U); // set U and initialize P and phi's
RealD H0 = MD.S(U); // current state RealD H0 = MD.S(U); // current state
std::cout<<"Total H before = "<< H0 << "\n"; std::cout<<"Total H before = "<< H0 << "\n";
@ -70,14 +67,12 @@ namespace Grid{
public: public:
HybridMonteCarlo(HMCparameters Pms, HybridMonteCarlo(HMCparameters Pms,
Integrator<IntegType>& MolDyn, Integrator<IntegType>& MolDyn):
GridBase* grid): Params(Pms),MD(MolDyn){
Params(Pms),MD(MolDyn),pRNG(grid){
//FIXME //FIXME
// initialize RNGs // initialize RNGs also with seed
sRNG.SeedRandomDevice(); sRNG.SeedRandomDevice();
pRNG.SeedRandomDevice();
} }
~HybridMonteCarlo(){}; ~HybridMonteCarlo(){};
@ -85,7 +80,7 @@ namespace Grid{
void evolve(LatticeLorentzColourMatrix& Uin){ void evolve(LatticeLorentzColourMatrix& Uin){
Real DeltaH; Real DeltaH;
// Thermalizations // Thermalizations
for(int iter=1; iter <= Params.ThermalizationSteps; ++iter){ for(int iter=1; iter <= Params.ThermalizationSteps; ++iter){
std::cout << "-- # Thermalization step = "<< iter << "\n"; std::cout << "-- # Thermalization step = "<< iter << "\n";

View File

@ -24,9 +24,6 @@ namespace Grid{
typedef std::vector<ActionLevel> ActionSet; typedef std::vector<ActionLevel> ActionSet;
typedef std::vector<Observer*> ObserverList; typedef std::vector<Observer*> ObserverList;
class LeapFrog;
struct IntegratorParameters{ struct IntegratorParameters{
int Nexp; int Nexp;
int MDsteps; // number of outer steps int MDsteps; // number of outer steps
@ -46,20 +43,20 @@ namespace Grid{
} }
template< class IntegratorPolicy > template< class IntegratorPolicy >
class Integrator{ class Integrator{
private: private:
IntegratorParameters Params; IntegratorParameters Params;
const ActionSet as; const ActionSet as;
const std::vector<int> Nrel; //relative step size per level const std::vector<int> Nrel; //relative step size per level
//ObserverList observers; // not yet
std::unique_ptr<LatticeLorentzColourMatrix> P; std::unique_ptr<LatticeLorentzColourMatrix> P;
GridParallelRNG pRNG;
IntegratorPolicy TheIntegrator;// contains parameters too //ObserverList observers; // not yet
IntegratorPolicy TheIntegrator;
void register_observers();
void notify_observers();
void update_P(LatticeLorentzColourMatrix&U, int level,double ep){ void update_P(LatticeLorentzColourMatrix&U, int level,double ep){
for(int a=0; a<as[level].size(); ++a){ for(int a=0; a<as[level].size(); ++a){
@ -77,36 +74,32 @@ namespace Grid{
for (int mu = 0; mu < Nd; mu++){ for (int mu = 0; mu < Nd; mu++){
Umu=peekLorentz(U, mu); Umu=peekLorentz(U, mu);
Pmu=peekLorentz(*P, mu); Pmu=peekLorentz(*P, mu);
Umu = expMat(Pmu, ep)*Umu; Umu = expMat(Pmu, ep, Params.Nexp)*Umu;
pokeLorentz(U, Umu, mu); pokeLorentz(U, Umu, mu);
} }
} }
void register_observers();
void notify_observers();
friend void IntegratorPolicy::step (LatticeLorentzColourMatrix& U, friend void IntegratorPolicy::step (LatticeLorentzColourMatrix& U,
int level, std::vector<int>& clock, int level, std::vector<int>& clock,
Integrator<LeapFrog>* Integ); Integrator<IntegratorPolicy>* Integ);
public: public:
Integrator(IntegratorParameters Par, Integrator(GridBase* grid, IntegratorParameters Par,
ActionSet& Aset, std::vector<int> Nrel_): ActionSet& Aset, std::vector<int> Nrel_):
Params(Par),as(Aset),Nrel(Nrel_){ Params(Par),as(Aset),Nrel(Nrel_),P(new LatticeLorentzColourMatrix(grid)),pRNG(grid){
assert(as.size() == Nrel.size()); assert(as.size() == Nrel.size());
pRNG.SeedRandomDevice();
}; };
~Integrator(){} ~Integrator(){}
//Initialization of momenta and actions //Initialization of momenta and actions
void init(LatticeLorentzColourMatrix& U, void init(LatticeLorentzColourMatrix& U){
GridParallelRNG& pRNG){
std::cout<< "Integrator init\n"; std::cout<< "Integrator init\n";
if (!P){
std::unique_ptr<LatticeLorentzColourMatrix> Pnew(new LatticeLorentzColourMatrix(U._grid));
P = std::move(Pnew);
}
MDutils::generate_momenta(*P,pRNG); MDutils::generate_momenta(*P,pRNG);
for(int level=0; level< as.size(); ++level){ for(int level=0; level< as.size(); ++level){
for(int actionID=0; actionID<as.at(level).size(); ++actionID){ for(int actionID=0; actionID<as.at(level).size(); ++actionID){
@ -116,7 +109,7 @@ namespace Grid{
} }
// Calculate action
RealD S(LatticeLorentzColourMatrix& U){ RealD S(LatticeLorentzColourMatrix& U){
LatticeComplex Hloc(U._grid); LatticeComplex Hloc(U._grid);
Hloc = zero; Hloc = zero;
@ -153,7 +146,74 @@ namespace Grid{
class MinimumNorm2{ class MinimumNorm2{
const double lambda = 0.1931833275037836; const double lambda = 0.1931833275037836;
public: public:
void step (LatticeLorentzColourMatrix& U, int level, std::vector<int>& clock); void step (LatticeLorentzColourMatrix& U,
int level, std::vector<int>& clock,
Integrator<MinimumNorm2>* Integ){
// level : current level
// fl : final level
// eps : current step size
int fl = Integ->as.size() -1;
double eps = Integ->Params.stepsize;
for(int l=0; l<=level; ++l) eps/= 2.0*Integ->Nrel[l];
int fin = Integ->Nrel[0];
for(int l=1; l<=level; ++l) fin*= 2.0*Integ->Nrel[l];
fin = 3*Integ->Params.MDsteps*fin -1;
for(int e=0; e<Integ->Nrel[level]; ++e){
if(clock[level] == 0){ // initial half step
Integ->update_P(U,level,lambda*eps);
++clock[level];
for(int l=0; l<level;++l) std::cout<<" ";
std::cout<<"P "<< clock[level] <<std::endl;
}
if(level == fl){ // lowest level
Integ->update_U(U,0.5*eps);
for(int l=0; l<level;++l) std::cout<<" ";
std::cout<<"U "<< (clock[level]+1) <<std::endl;
}else{ // recursive function call
step(U,level+1,clock, Integ);
}
Integ->update_P(U,level,(1.0-2.0*lambda)*eps);
++clock[level];
for(int l=0; l<level;++l) std::cout<<" ";
std::cout<<"P "<< (clock[level]) <<std::endl;
if(level == fl){ // lowest level
Integ->update_U(U,0.5*eps);
for(int l=0; l<level;++l) std::cout<<" ";
std::cout<<"U "<< (clock[level]+1) <<std::endl;
}else{ // recursive function call
step(U,level+1,clock, Integ);
}
if(clock[level] == fin){ // final half step
Integ->update_P(U,level,lambda*eps);
++clock[level];
for(int l=0; l<level;++l) std::cout<<" ";
std::cout<<"P "<< clock[level] <<std::endl;
}else{ // bulk step
Integ->update_P(U,level,lambda*2.0*eps);
clock[level]+=2;
for(int l=0; l<level;++l) std::cout<<" ";
std::cout<<"P "<< clock[level] <<std::endl;
}
}
}
}; };
@ -162,7 +222,6 @@ namespace Grid{
void step (LatticeLorentzColourMatrix& U, void step (LatticeLorentzColourMatrix& U,
int level, std::vector<int>& clock, int level, std::vector<int>& clock,
Integrator<LeapFrog>* Integ){ Integrator<LeapFrog>* Integ){
// cl : current level
// fl : final level // fl : final level
// eps : current step size // eps : current step size
@ -179,7 +238,7 @@ namespace Grid{
for(int e=0; e<Integ->Nrel[level]; ++e){ for(int e=0; e<Integ->Nrel[level]; ++e){
if(clock[level] == 0){ // initial half step if(clock[level] == 0){ // initial half step
Integ->update_P(U, level,eps/2); Integ->update_P(U, level,eps/2.0);
++clock[level]; ++clock[level];
for(int l=0; l<level;++l) std::cout<<" "; for(int l=0; l<level;++l) std::cout<<" ";
std::cout<<"P "<< 0.5*clock[level] <<std::endl; std::cout<<"P "<< 0.5*clock[level] <<std::endl;
@ -192,7 +251,7 @@ namespace Grid{
step(U, level+1,clock, Integ); step(U, level+1,clock, Integ);
} }
if(clock[level] == fin){ // final half step if(clock[level] == fin){ // final half step
Integ->update_P(U, level,eps/2); Integ->update_P(U, level,eps/2.0);
++clock[level]; ++clock[level];
for(int l=0; l<level;++l) std::cout<<" "; for(int l=0; l<level;++l) std::cout<<" ";

View File

@ -29,7 +29,7 @@ int main (int argc, char ** argv)
SU3::HotConfiguration(pRNG, U); SU3::HotConfiguration(pRNG, U);
// simplify template? // simplify template declaration? Strip the lorentz from the second template
WilsonGaugeAction<LatticeLorentzColourMatrix, LatticeColourMatrix> Waction(6.0); WilsonGaugeAction<LatticeLorentzColourMatrix, LatticeColourMatrix> Waction(6.0);
//Collect actions //Collect actions
@ -39,13 +39,14 @@ int main (int argc, char ** argv)
FullSet.push_back(Level1); FullSet.push_back(Level1);
// Create integrator // Create integrator
typedef MinimumNorm2 IntegratorAlgorithm;// change here to change the algorithm
IntegratorParameters MDpar(12,30,1.0); IntegratorParameters MDpar(12,30,1.0);
std::vector<int> rel ={1}; std::vector<int> rel ={1};
Integrator<LeapFrog> MDleapfrog(MDpar, FullSet,rel); Integrator<IntegratorAlgorithm> MDynamics(&Fine,MDpar, FullSet,rel);
// Create HMC // Create HMC
HMCparameters HMCpar; HMCparameters HMCpar;
HybridMonteCarlo<LeapFrog> HMC(HMCpar, MDleapfrog, &Fine); HybridMonteCarlo<IntegratorAlgorithm> HMC(HMCpar, MDynamics);
HMC.evolve(U); HMC.evolve(U);