2016-07-05 12:03:54 +01:00
|
|
|
/*************************************************************************************
|
2016-01-02 14:51:32 +00:00
|
|
|
|
2016-07-05 12:03:54 +01:00
|
|
|
Grid physics library, www.github.com/paboyle/Grid
|
2016-01-02 14:51:32 +00:00
|
|
|
|
2016-07-05 12:03:54 +01:00
|
|
|
Source file: ./lib/qcd/hmc/HMC.h
|
2016-01-02 14:51:32 +00:00
|
|
|
|
2016-07-05 12:03:54 +01:00
|
|
|
Copyright (C) 2015
|
2016-01-02 14:51:32 +00:00
|
|
|
|
|
|
|
Author: Azusa Yamaguchi <ayamaguc@staffmail.ed.ac.uk>
|
|
|
|
Author: Peter Boyle <paboyle@ph.ed.ac.uk>
|
|
|
|
Author: neo <cossu@post.kek.jp>
|
|
|
|
Author: paboyle <paboyle@ph.ed.ac.uk>
|
|
|
|
|
2016-07-05 12:03:54 +01:00
|
|
|
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.
|
2016-01-02 14:51:32 +00:00
|
|
|
|
2016-07-05 12:03:54 +01:00
|
|
|
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.
|
2016-01-02 14:51:32 +00:00
|
|
|
|
2016-07-05 12:03:54 +01:00
|
|
|
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-01-02 14:51:32 +00:00
|
|
|
|
2016-07-05 12:03:54 +01:00
|
|
|
See the full license in the file "LICENSE" in the top level distribution
|
|
|
|
directory
|
|
|
|
*************************************************************************************/
|
|
|
|
/* END LEGAL */
|
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
|
|
|
|
*/
|
|
|
|
//--------------------------------------------------------------------
|
|
|
|
#ifndef HMC_INCLUDED
|
|
|
|
#define HMC_INCLUDED
|
|
|
|
|
|
|
|
#include <string>
|
2017-02-09 15:18:38 +00:00
|
|
|
#include <list>
|
2015-07-07 06:59:37 +01:00
|
|
|
|
2017-03-17 02:40:17 +00:00
|
|
|
|
|
|
|
|
2017-02-22 18:09:33 +00:00
|
|
|
#include <Grid/qcd/hmc/integrators/Integrator.h>
|
|
|
|
#include <Grid/qcd/hmc/integrators/Integrator_algorithm.h>
|
|
|
|
|
2016-07-05 12:03:54 +01:00
|
|
|
namespace Grid {
|
|
|
|
namespace QCD {
|
|
|
|
|
2017-01-17 10:46:49 +00:00
|
|
|
struct HMCparameters: Serializable {
|
|
|
|
GRID_SERIALIZABLE_CLASS_MEMBERS(HMCparameters,
|
2017-01-27 11:23:51 +00:00
|
|
|
Integer, StartTrajectory,
|
|
|
|
Integer, Trajectories, /* @brief Number of sweeps in this run */
|
|
|
|
bool, MetropolisTest,
|
|
|
|
Integer, NoMetropolisUntil,
|
|
|
|
std::string, StartingType,
|
|
|
|
IntegratorParameters, MD)
|
2016-07-05 12:03:54 +01:00
|
|
|
|
|
|
|
HMCparameters() {
|
|
|
|
////////////////////////////// Default values
|
2017-01-05 13:09:32 +00:00
|
|
|
MetropolisTest = true;
|
2016-07-05 12:03:54 +01:00
|
|
|
NoMetropolisUntil = 10;
|
2017-01-05 13:09:32 +00:00
|
|
|
StartTrajectory = 0;
|
|
|
|
Trajectories = 10;
|
2017-01-17 13:22:18 +00:00
|
|
|
StartingType = "HotStart";
|
2016-07-05 12:03:54 +01:00
|
|
|
/////////////////////////////////
|
|
|
|
}
|
|
|
|
|
2017-01-17 13:22:18 +00:00
|
|
|
template <class ReaderClass >
|
|
|
|
HMCparameters(Reader<ReaderClass> & TheReader){
|
|
|
|
initialize(TheReader);
|
|
|
|
}
|
|
|
|
|
|
|
|
template < class ReaderClass >
|
|
|
|
void initialize(Reader<ReaderClass> &TheReader){
|
2017-06-24 23:04:05 +01:00
|
|
|
std::cout << GridLogMessage << "Reading HMC\n";
|
2017-01-17 13:22:18 +00:00
|
|
|
read(TheReader, "HMC", *this);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-10-03 15:28:00 +01:00
|
|
|
void print_parameters() const {
|
|
|
|
std::cout << GridLogMessage << "[HMC parameters] Trajectories : " << Trajectories << "\n";
|
|
|
|
std::cout << GridLogMessage << "[HMC parameters] Start trajectory : " << StartTrajectory << "\n";
|
2017-01-05 13:09:32 +00:00
|
|
|
std::cout << GridLogMessage << "[HMC parameters] Metropolis test (on/off): " << std::boolalpha << MetropolisTest << "\n";
|
2016-10-03 15:28:00 +01:00
|
|
|
std::cout << GridLogMessage << "[HMC parameters] Thermalization trajs : " << NoMetropolisUntil << "\n";
|
2017-01-17 13:22:18 +00:00
|
|
|
std::cout << GridLogMessage << "[HMC parameters] Starting type : " << StartingType << "\n";
|
|
|
|
MD.print_parameters();
|
2016-07-05 12:03:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
};
|
2017-03-17 02:42:55 +00:00
|
|
|
|
2016-10-03 15:28:00 +01:00
|
|
|
template <class IntegratorType>
|
2016-07-05 12:03:54 +01:00
|
|
|
class HybridMonteCarlo {
|
|
|
|
private:
|
|
|
|
const HMCparameters Params;
|
|
|
|
|
2016-10-03 15:28:00 +01:00
|
|
|
typedef typename IntegratorType::Field Field;
|
2017-01-18 16:31:51 +00:00
|
|
|
typedef std::vector< HmcObservable<Field> * > ObsListType;
|
2016-10-03 15:28:00 +01:00
|
|
|
|
2017-01-18 16:31:51 +00:00
|
|
|
//pass these from the resource manager
|
|
|
|
GridSerialRNG &sRNG;
|
|
|
|
GridParallelRNG &pRNG;
|
|
|
|
|
2016-10-03 15:28:00 +01:00
|
|
|
Field &Ucur;
|
2017-02-09 15:18:38 +00:00
|
|
|
|
2016-07-05 12:03:54 +01:00
|
|
|
IntegratorType &TheIntegrator;
|
2017-01-18 16:31:51 +00:00
|
|
|
ObsListType Observables;
|
2016-07-05 12:03:54 +01:00
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////
|
|
|
|
// Metropolis step
|
|
|
|
/////////////////////////////////////////////////////////
|
|
|
|
bool metropolis_test(const RealD DeltaH) {
|
|
|
|
RealD rn_test;
|
|
|
|
|
|
|
|
RealD prob = std::exp(-DeltaH);
|
|
|
|
|
|
|
|
random(sRNG, rn_test);
|
|
|
|
|
|
|
|
std::cout << GridLogMessage
|
|
|
|
<< "--------------------------------------------------\n";
|
|
|
|
std::cout << GridLogMessage << "exp(-dH) = " << prob
|
|
|
|
<< " Random = " << rn_test << "\n";
|
|
|
|
std::cout << GridLogMessage
|
|
|
|
<< "Acc. Probability = " << ((prob < 1.0) ? prob : 1.0) << "\n";
|
|
|
|
|
|
|
|
if ((prob > 1.0) || (rn_test <= prob)) { // accepted
|
|
|
|
std::cout << GridLogMessage << "Metropolis_test -- ACCEPTED\n";
|
|
|
|
std::cout << GridLogMessage
|
|
|
|
<< "--------------------------------------------------\n";
|
|
|
|
return true;
|
|
|
|
} else { // rejected
|
|
|
|
std::cout << GridLogMessage << "Metropolis_test -- REJECTED\n";
|
|
|
|
std::cout << GridLogMessage
|
|
|
|
<< "--------------------------------------------------\n";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////
|
|
|
|
// Evolution
|
|
|
|
/////////////////////////////////////////////////////////
|
2017-02-09 15:18:38 +00:00
|
|
|
RealD evolve_hmc_step(Field &U) {
|
2016-07-05 12:03:54 +01:00
|
|
|
TheIntegrator.refresh(U, pRNG); // set U and initialize P and phi's
|
|
|
|
|
|
|
|
RealD H0 = TheIntegrator.S(U); // initial state action
|
|
|
|
|
|
|
|
std::streamsize current_precision = std::cout.precision();
|
2017-02-24 17:03:42 +00:00
|
|
|
std::cout.precision(15);
|
2016-07-05 12:03:54 +01:00
|
|
|
std::cout << GridLogMessage << "Total H before trajectory = " << H0 << "\n";
|
|
|
|
std::cout.precision(current_precision);
|
|
|
|
|
|
|
|
TheIntegrator.integrate(U);
|
|
|
|
|
|
|
|
RealD H1 = TheIntegrator.S(U); // updated state action
|
|
|
|
|
2017-02-24 17:03:42 +00:00
|
|
|
///////////////////////////////////////////////////////////
|
|
|
|
if(0){
|
|
|
|
std::cout << "------------------------- Reversibility test" << std::endl;
|
|
|
|
TheIntegrator.reverse_momenta();
|
|
|
|
TheIntegrator.integrate(U);
|
|
|
|
|
|
|
|
H1 = TheIntegrator.S(U); // updated state action
|
|
|
|
std::cout << "--------------------------------------------" << std::endl;
|
|
|
|
}
|
|
|
|
///////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
|
|
std::cout.precision(15);
|
2016-07-05 12:03:54 +01:00
|
|
|
std::cout << GridLogMessage << "Total H after trajectory = " << H1
|
2017-02-09 15:18:38 +00:00
|
|
|
<< " dH = " << H1 - H0 << "\n";
|
2016-07-05 12:03:54 +01:00
|
|
|
std::cout.precision(current_precision);
|
2017-02-09 15:18:38 +00:00
|
|
|
|
2016-07-05 12:03:54 +01:00
|
|
|
return (H1 - H0);
|
|
|
|
}
|
2017-02-09 15:18:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2016-07-05 12:03:54 +01:00
|
|
|
|
|
|
|
public:
|
|
|
|
/////////////////////////////////////////
|
|
|
|
// Constructor
|
|
|
|
/////////////////////////////////////////
|
2017-01-18 16:31:51 +00:00
|
|
|
HybridMonteCarlo(HMCparameters _Pams, IntegratorType &_Int,
|
|
|
|
GridSerialRNG &_sRNG, GridParallelRNG &_pRNG,
|
|
|
|
ObsListType _Obs, Field &_U)
|
2017-02-09 15:18:38 +00:00
|
|
|
: Params(_Pams), TheIntegrator(_Int), sRNG(_sRNG), pRNG(_pRNG), Observables(_Obs), Ucur(_U) {}
|
2016-07-05 12:03:54 +01:00
|
|
|
~HybridMonteCarlo(){};
|
|
|
|
|
|
|
|
void evolve(void) {
|
|
|
|
Real DeltaH;
|
|
|
|
|
2016-10-03 15:28:00 +01:00
|
|
|
Field Ucopy(Ucur._grid);
|
2016-07-05 12:03:54 +01:00
|
|
|
|
2016-10-03 15:28:00 +01:00
|
|
|
Params.print_parameters();
|
2016-12-22 12:41:56 +00:00
|
|
|
TheIntegrator.print_actions();
|
2016-07-05 12:03:54 +01:00
|
|
|
|
|
|
|
// Actual updates (evolve a copy Ucopy then copy back eventually)
|
2017-01-05 13:09:32 +00:00
|
|
|
unsigned int FinalTrajectory = Params.Trajectories + Params.NoMetropolisUntil + Params.StartTrajectory;
|
|
|
|
for (int traj = Params.StartTrajectory; traj < FinalTrajectory; ++traj) {
|
2016-07-05 12:03:54 +01:00
|
|
|
std::cout << GridLogMessage << "-- # Trajectory = " << traj << "\n";
|
2017-01-05 13:09:32 +00:00
|
|
|
if (traj < Params.StartTrajectory + Params.NoMetropolisUntil) {
|
|
|
|
std::cout << GridLogMessage << "-- Thermalization" << std::endl;
|
2017-02-09 15:18:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
double t0=usecond();
|
2016-07-05 12:03:54 +01:00
|
|
|
Ucopy = Ucur;
|
|
|
|
|
2017-02-09 15:18:38 +00:00
|
|
|
DeltaH = evolve_hmc_step(Ucopy);
|
|
|
|
// Metropolis-Hastings test
|
2016-07-05 12:03:54 +01:00
|
|
|
bool accept = true;
|
2017-01-05 13:09:32 +00:00
|
|
|
if (traj >= Params.StartTrajectory + Params.NoMetropolisUntil) {
|
2016-07-05 12:03:54 +01:00
|
|
|
accept = metropolis_test(DeltaH);
|
2017-01-05 13:09:32 +00:00
|
|
|
} else {
|
|
|
|
std::cout << GridLogMessage << "Skipping Metropolis test" << std::endl;
|
2015-12-22 08:54:40 +00:00
|
|
|
}
|
2015-07-27 10:32:28 +01:00
|
|
|
|
2017-02-09 15:18:38 +00:00
|
|
|
if (accept)
|
|
|
|
Ucur = Ucopy;
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-01-05 13:09:32 +00:00
|
|
|
double t1=usecond();
|
|
|
|
std::cout << GridLogMessage << "Total time for trajectory (s): " << (t1-t0)/1e6 << std::endl;
|
|
|
|
|
2015-07-03 18:43:14 +01:00
|
|
|
|
2016-07-05 12:03:54 +01:00
|
|
|
for (int obs = 0; obs < Observables.size(); obs++) {
|
2017-01-05 13:09:32 +00:00
|
|
|
std::cout << GridLogDebug << "Observables # " << obs << std::endl;
|
|
|
|
std::cout << GridLogDebug << "Observables total " << Observables.size() << std::endl;
|
|
|
|
std::cout << GridLogDebug << "Observables pointer " << Observables[obs] << std::endl;
|
2016-07-05 12:03:54 +01:00
|
|
|
Observables[obs]->TrajectoryComplete(traj + 1, Ucur, sRNG, pRNG);
|
2015-07-03 18:43:14 +01:00
|
|
|
}
|
2017-01-05 13:09:32 +00:00
|
|
|
std::cout << GridLogMessage << ":::::::::::::::::::::::::::::::::::::::::::" << std::endl;
|
2016-07-05 12:03:54 +01:00
|
|
|
}
|
|
|
|
}
|
2017-01-18 16:31:51 +00:00
|
|
|
|
2016-07-05 12:03:54 +01:00
|
|
|
};
|
2015-07-03 08:51:41 +01:00
|
|
|
|
2017-02-22 18:09:33 +00:00
|
|
|
|
2016-07-05 12:03:54 +01:00
|
|
|
} // QCD
|
|
|
|
} // Grid
|
2015-07-03 08:51:41 +01:00
|
|
|
|
2017-04-05 14:41:04 +01:00
|
|
|
|
|
|
|
// april 11 2017 merge, Guido, commenting out
|
|
|
|
//#include <Grid/parallelIO/NerscIO.h>
|
|
|
|
//#include <Grid/qcd/hmc/NerscCheckpointer.h>
|
|
|
|
//#include <Grid/qcd/hmc/HmcRunner.h>
|
2017-02-22 18:09:33 +00:00
|
|
|
|
2016-07-05 12:03:54 +01:00
|
|
|
#endif
|