1
0
mirror of https://github.com/paboyle/Grid.git synced 2025-06-14 22:07:05 +01:00

More progress in the HMC construction

This commit is contained in:
neo
2015-07-04 02:43:14 +09:00
parent 9655d43017
commit 30c9dc473d
12 changed files with 234 additions and 139 deletions

View File

@ -18,81 +18,7 @@ namespace Grid{
}
/////////////////////////////////////////////////////////////////
HybridMonteCarlo::HybridMonteCarlo(GridSerialRNG& R):RNG(R){
//FIXME
}
void HybridMonteCarlo::evolve(LatticeColourMatrix& Uin)const{
Real DeltaH;
Real timer;
// Thermalizations
for(int iter=1; iter <= Params.ThermalizationSteps; ++iter){
std::cout << "-- # Thermalization step = "<< iter << "\n";
DeltaH = evolve_step(Uin);
std::cout<< "[Timing] Trajectory time (s) : "<< timer/1000.0 << "\n";
std::cout<< "dH = "<< DeltaH << "\n";
// Update matrix
//Uin = md_->get_U(); //accept every time
}
// Actual updates
for(int iter=Params.StartingConfig;
iter < Params.Nsweeps+Params.StartingConfig; ++iter){
std::cout << "-- # Sweep = "<< iter << "\n";
DeltaH = evolve_step(Uin);
if(metropolis_test(DeltaH)) {};//Uin = md_->get_U();
// need sync?
}
}
RealD HybridMonteCarlo::evolve_step(LatticeColourMatrix& Uin)const{
/*
md_->init(Uin,RNG); // set U and initialize P and phi's
RealD H0 = md_->S(); // current state
std::cout<<"Total H_before = "<< H0 << "\n";
md_->integrator();
RealD H1 = md_->calc_H(); // updated state
std::cout<<"Total H_after = "<< H1 << "\n";
return (H1-H0);
*/
return 0;
}
bool HybridMonteCarlo::metropolis_test(const RealD DeltaH)const{
RealD rn_test;
RealD prob = std::exp(-DeltaH);
random(RNG,rn_test);
std::cout<< "--------------------------------------------\n";
std::cout<< "dH = "<<DeltaH << " Random = "<< rn_test
<< "\nAcc. Probability = " << ((prob<1.0)? prob: 1.0)<< " ";
if((prob >1.0) || (rn_test <= prob)){ // accepted
std::cout <<"-- ACCEPTED\n";
return true;
} else { // rejected
std::cout <<"-- REJECTED\n";
return false;
}
}
}
}

View File

@ -25,21 +25,91 @@ namespace Grid{
HMCparameters();
};
template <class IntegType>
class HybridMonteCarlo{
const HMCparameters Params;
GridSerialRNG& RNG;
// FIXME need the integrator
GridSerialRNG sRNG;
GridParallelRNG pRNG;
std::unique_ptr< Integrator<IntegType> > MD;
bool metropolis_test(const RealD DeltaH)const;
RealD evolve_step(LatticeColourMatrix&)const;
bool metropolis_test(const RealD DeltaH){
RealD rn_test;
RealD prob = std::exp(-DeltaH);
random(sRNG,rn_test);
std::cout<< "--------------------------------------------\n";
std::cout<< "dH = "<<DeltaH << " Random = "<< rn_test
<< "\nAcc. Probability = " << ((prob<1.0)? prob: 1.0)<< " ";
if((prob >1.0) || (rn_test <= prob)){ // accepted
std::cout <<"-- ACCEPTED\n";
return true;
} else { // rejected
std::cout <<"-- REJECTED\n";
return false;
}
}
RealD evolve_step(LatticeColourMatrix& Uin){
MD->init(Uin,pRNG); // set U and initialize P and phi's
RealD H0 = MD->S(); // current state
std::cout<<"Total H_before = "<< H0 << "\n";
MD->integrate(0);
RealD H1 = MD->S(); // updated state
std::cout<<"Total H_after = "<< H1 << "\n";
return (H1-H0);
}
public:
HybridMonteCarlo(GridSerialRNG&);
HybridMonteCarlo(Integrator<IntegType>& MolDyn, GridBase* grid):MD(&MolDyn),pRNG(grid){
//FIXME
// initialize RNGs
sRNG.SeedRandomDevice();
pRNG.SeedRandomDevice();
}
~HybridMonteCarlo(){};
void evolve(LatticeColourMatrix& Uin)const;
void evolve(LatticeColourMatrix& Uin){
Real DeltaH;
Real timer;
// Thermalizations
for(int iter=1; iter <= Params.ThermalizationSteps; ++iter){
std::cout << "-- # Thermalization step = "<< iter << "\n";
DeltaH = evolve_step(Uin);
std::cout<< "[Timing] Trajectory time (s) : "<< timer/1000.0 << "\n";
std::cout<< "dH = "<< DeltaH << "\n";
// Update matrix
Uin = MD->get_U(); //accept every time
}
// Actual updates
for(int iter=Params.StartingConfig;
iter < Params.Nsweeps+Params.StartingConfig; ++iter){
std::cout << "-- # Sweep = "<< iter << "\n";
DeltaH = evolve_step(Uin);
if(metropolis_test(DeltaH)) Uin = MD->get_U();
// need sync?
}
}
};

View File

@ -3,18 +3,22 @@
@brief utilities for MD including funcs to generate initial HMC momentum
*/
#include <Grid.h>
static const double sq3i = 1.0/sqrt(3.0);
namespace Grid{
namespace QCD{
void MDutils::generate_momenta(LatticeLorentzColourMatrix& P,GridParallelRNG& pRNG){
// for future support of different groups
MDutils::generate_momenta_su3(P, pRNG);
}
void MDutils::generate_momenta_su3(LatticeColourMatrix& P,GridParallelRNG& pRNG){
SU3::GaussianLieAlgebraMatrix(pRNG, P);
void MDutils::generate_momenta_su3(LatticeLorentzColourMatrix& P,GridParallelRNG& pRNG){
LatticeColourMatrix Pmu(P._grid);
for(int mu=0;mu<Nd;mu++){
SU3::GaussianLieAlgebraMatrix(pRNG, Pmu);
pokeLorentz(P, Pmu, mu);
}
}

View File

@ -9,40 +9,71 @@
#ifndef INTEGRATOR_INCLUDED
#define INTEGRATOR_INCLUDED
class Action;
class RandNum;
class Observer;
typedef std::vector<Action*> ActionLevel;
typedef std::vector<ActionLevel> ActionSet;
typedef std::vector<Observer*> ObserverList;
/*! @brief Abstract base class for Molecular Dynamics management */
namespace Grid{
namespace QCD{
typedef Action<LatticeLorentzColourMatrix>* ActPtr; // now force the same size as the rest of the code
typedef std::vector<ActPtr> ActionLevel;
typedef std::vector<ActionLevel> ActionSet;
typedef std::vector<Observer*> ObserverList;
class Integrator2MN{
const double lambda = 0.1931833275037836;
void step (LatticeColourMatrix&, LatticeColourMatrix&,
int, std::vector<int>&);
};
class IntegratorLeapFrog{
void step (LatticeColourMatrix&, LatticeColourMatrix&,
int, std::vector<int>&);
};
template< class IntegratorPolicy >
class Integrator{
private:
virtual void update_P(int lv,double ep) = 0;
virtual void update_U(double ep) = 0;
int Nexp;
int MDsteps; // number of outer steps
RealD trajL; // trajectory length
RealD stepsize;
const std::vector<int> Nrel; // relative steps per level
const ActionSet as;
ObserverList observers;
// LatticeColourMatrix* const U; // is shared among all actions - or use a singleton...
LatticeColourMatrix P;
virtual void register_observers() = 0;
virtual void notify_observers() = 0;
IntegratorPolicy TheIntegrator;// contains parameters too
void update_P(int lv,double ep);
void update_U(double ep);
void register_observers();
void notify_observers();
void integrator_step(int level ,std::vector<Integer>& clock);
public:
virtual ~Integrator(){}
virtual void init(const LatticeColourMatrix&,
const GridParallelRNG& RNG)=0;
virtual double S()const =0;
virtual void integrate(int level) =0;
virtual const LatticeColourMatrix get_U() const =0;
void generate_momenta(LatticeColourMatrix& P,const RandNum& rand);
Integrator(int Nexp_, int MDsteps_, RealD trajL_,
ActionSet& Aset, ObserverList obs):as(Aset), observers(obs){};
~Integrator(){}
void init(LatticeLorentzColourMatrix&,
GridParallelRNG&);
double S();
void integrate(int level);
LatticeColourMatrix get_U();
};
namespace MDutils{
void generate_momenta_su3(LatticeColourMatrix& P,GridParallelRNG& RNG);
void generate_momenta(LatticeLorentzColourMatrix&,GridParallelRNG&);
void generate_momenta_su3(LatticeLorentzColourMatrix&,GridParallelRNG&);
}
}