1
0
mirror of https://github.com/paboyle/Grid.git synced 2024-09-21 01:25:48 +01:00
Grid/lib/qcd/hmc/HMC.h

125 lines
3.3 KiB
C
Raw Normal View History

2015-07-03 08:51:41 +01:00
//--------------------------------------------------------------------
/*! @file HMC.h
2015-07-07 06:59:37 +01:00
* @brief Classes for Hybrid Monte Carlo update
2015-07-03 08:51:41 +01:00
*
* @author Guido Cossu
* Time-stamp: <2015-07-30 16:58:26 neo>
2015-07-03 08:51:41 +01:00
*/
//--------------------------------------------------------------------
#ifndef HMC_INCLUDED
#define HMC_INCLUDED
#include <string>
2015-07-07 06:59:37 +01:00
2015-07-03 08:51:41 +01:00
namespace Grid{
namespace QCD{
struct HMCparameters{
Integer Nsweeps; /* @brief Number of sweeps in this run */
Integer TotalSweeps; /* @brief If provided, the total number of sweeps */
Integer ThermalizationSteps;
Integer StartingConfig;
Integer SaveInterval; //Setting to 0 does not save configurations
2015-07-07 06:59:37 +01:00
std::string Filename_prefix; // To save configurations and rng seed
2015-07-03 08:51:41 +01:00
HMCparameters();
};
2015-07-06 08:46:43 +01:00
template <class Algorithm>
2015-07-03 08:51:41 +01:00
class HybridMonteCarlo{
2015-07-03 08:51:41 +01:00
const HMCparameters Params;
GridSerialRNG sRNG; // Fixme: need a RNG management strategy.
2015-07-06 08:46:43 +01:00
Integrator<Algorithm>& MD;
/////////////////////////////////////////////////////////
// Metropolis step
/////////////////////////////////////////////////////////
2015-07-03 18:43:14 +01:00
bool metropolis_test(const RealD DeltaH){
2015-07-03 18:43:14 +01:00
RealD rn_test;
2015-07-03 18:43:14 +01:00
RealD prob = std::exp(-DeltaH);
2015-07-03 18:43:14 +01:00
random(sRNG,rn_test);
std::cout<<GridLogMessage<< "--------------------------------------------\n";
std::cout<<GridLogMessage<< "dH = "<<DeltaH << " Random = "<< rn_test <<"\n";
std::cout<<GridLogMessage<< "Acc. Probability = " << ((prob<1.0)? prob: 1.0)<< " ";
2015-07-03 18:43:14 +01:00
if((prob >1.0) || (rn_test <= prob)){ // accepted
std::cout<<GridLogMessage <<"-- ACCEPTED\n";
2015-07-03 18:43:14 +01:00
return true;
} else { // rejected
std::cout<<GridLogMessage <<"-- REJECTED\n";
2015-07-03 18:43:14 +01:00
return false;
}
2015-07-03 18:43:14 +01:00
}
/////////////////////////////////////////////////////////
// Evolution
/////////////////////////////////////////////////////////
2015-07-07 06:59:37 +01:00
RealD evolve_step(LatticeGaugeField& U){
2015-07-06 08:46:43 +01:00
MD.init(U); // set U and initialize P and phi's
2015-07-06 08:46:43 +01:00
RealD H0 = MD.S(U); // initial state action
std::cout<<GridLogMessage<<"Total H before = "<< H0 << "\n";
2015-07-06 08:46:43 +01:00
MD.integrate(U);
2015-07-03 18:43:14 +01:00
2015-07-06 08:46:43 +01:00
RealD H1 = MD.S(U); // updated state action
std::cout<<GridLogMessage<<"Total H after = "<< H1 << "\n";
2015-07-03 18:43:14 +01:00
return (H1-H0);
}
2015-07-03 08:51:41 +01:00
public:
/////////////////////////////////////////
// Constructor
/////////////////////////////////////////
HybridMonteCarlo(HMCparameters Pms, Integrator<Algorithm>& MolDyn): Params(Pms),MD(MolDyn) {
//FIXME... initialize RNGs also with seed ; RNG management strategy
2015-07-03 18:43:14 +01:00
sRNG.SeedRandomDevice();
2015-07-03 18:43:14 +01:00
}
2015-07-03 08:51:41 +01:00
~HybridMonteCarlo(){};
2015-07-03 18:43:14 +01:00
2015-07-07 06:59:37 +01:00
void evolve(LatticeGaugeField& Uin){
2015-07-03 18:43:14 +01:00
Real DeltaH;
2015-07-03 18:43:14 +01:00
// Thermalizations
for(int iter=1; iter <= Params.ThermalizationSteps; ++iter){
std::cout<<GridLogMessage << "-- # Thermalization step = "<< iter << "\n";
2015-07-03 18:43:14 +01:00
DeltaH = evolve_step(Uin);
std::cout<<GridLogMessage<< "dH = "<< DeltaH << "\n";
2015-07-03 18:43:14 +01:00
}
2015-07-04 09:47:50 +01:00
// Actual updates (evolve a copy Ucopy then copy back eventually)
2015-07-07 06:59:37 +01:00
LatticeGaugeField Ucopy(Uin._grid);
2015-07-03 18:43:14 +01:00
for(int iter=Params.StartingConfig;
iter < Params.Nsweeps+Params.StartingConfig; ++iter){
std::cout<<GridLogMessage << "-- # Sweep = "<< iter << "\n";
2015-07-04 09:47:50 +01:00
Ucopy = Uin;
2015-07-04 09:47:50 +01:00
DeltaH = evolve_step(Ucopy);
2015-07-03 18:43:14 +01:00
if(metropolis_test(DeltaH)) Uin = Ucopy;
2015-07-03 18:43:14 +01:00
}
}
2015-07-03 08:51:41 +01:00
};
}// QCD
}// Grid
#endif