Bridge++  Ver. 2.0.2
hmc_General.cpp
Go to the documentation of this file.
1 
14 #include "hmc_General.h"
15 
16 const std::string HMC_General::class_name = "HMC_General";
17 
18 //====================================================================
21  std::vector<Director *> director,
22  Integrator *integrator,
23  RandomNumbers *rand)
24  : m_vl(CommonParameters::Vlevel())
25 {
26  ActionSet action_set = action_list.get_actions();
27 
28  m_action.resize(action_set.size());
29  for (int i = 0; i < action_set.size(); ++i) {
30  m_action[i] = action_set[i];
31  }
32  m_director.resize(director.size());
33  for (int i = 0; i < director.size(); ++i) {
34  m_director[i] = director[i];
35  }
36  m_integrator = integrator;
37  m_rand = rand;
38  m_staple = new Staple_lex;
39  m_Metropolis_test = false;
41  m_trajectory_length = 0.0;
42 }
43 
44 
45 //====================================================================
47 // with params
49  std::vector<Director *> director,
50  Integrator *integrator,
51  RandomNumbers *rand,
52  const Parameters& params)
53  : m_vl(CommonParameters::Vlevel())
54 {
55  ActionSet action_set = action_list.get_actions();
56 
57  m_action.resize(action_set.size());
58  for (int i = 0; i < action_set.size(); ++i) {
59  m_action[i] = action_set[i];
60  }
61  m_director.resize(director.size());
62  for (int i = 0; i < director.size(); ++i) {
63  m_director[i] = director[i];
64  }
65  m_integrator = integrator;
66  m_rand = rand;
67  m_staple = new Staple_lex;
68  m_Metropolis_test = false;
70  m_trajectory_length = 0.0;
71 
72  set_parameters(params);
73 }
74 
75 
76 //====================================================================
79  Integrator *integrator,
80  RandomNumbers *rand)
81  : m_vl(CommonParameters::Vlevel())
82 {
83  ActionSet action_set = action_list.get_actions();
84 
85  m_action.resize(action_set.size());
86  for (int i = 0; i < action_set.size(); ++i) {
87  m_action[i] = action_set[i];
88  }
89  m_integrator = integrator;
90  m_rand = rand;
91  m_staple = new Staple_lex;
92  m_Metropolis_test = false;
94  m_trajectory_length = 0.0;
95 }
96 
97 
98 //====================================================================
100 // with params
102  Integrator *integrator,
103  RandomNumbers *rand,
104  const Parameters& params)
105  : m_vl(CommonParameters::Vlevel())
106 {
107  ActionSet action_set = action_list.get_actions();
108 
109  m_action.resize(action_set.size());
110  for (int i = 0; i < action_set.size(); ++i) {
111  m_action[i] = action_set[i];
112  }
113  m_integrator = integrator;
114  m_rand = rand;
115  m_staple = new Staple_lex;
116  m_Metropolis_test = false;
118  m_trajectory_length = 0.0;
119 
120  set_parameters(params);
121 }
122 
123 
124 //====================================================================
127 {
128  delete m_Langevin_P;
129  delete m_staple;
130 }
131 
132 
133 //====================================================================
135 {
136  std::string vlevel;
137  if (!params.fetch_string("verbose_level", vlevel)) {
138  m_vl = vout.set_verbose_level(vlevel);
139  }
140 
141  //- fetch and check input parameters
142  double traj_length;
143  bool Metropolis_test;
144 
145  int err = 0;
146  err += params.fetch_double("trajectory_length", traj_length);
147  err += params.fetch_bool("Metropolis_test", Metropolis_test);
148 
149  if (err) {
150  vout.crucial(m_vl, "Error at %s: input parameter not found.\n", class_name.c_str());
151  exit(EXIT_FAILURE);
152  }
153 
154  set_parameters(traj_length, Metropolis_test);
155 }
156 
157 
158 //====================================================================
160 {
161  params.set_double("trajectory_length", m_trajectory_length);
162  params.set_bool("Metropolis_test", m_Metropolis_test);
163 
164  params.set_string("verbose_level", vout.get_verbose_level(m_vl));
165 }
166 
167 
168 //====================================================================
169 void HMC_General::set_parameters(const double trajectory_length, const int Metropolis_test)
170 {
171  vout.crucial(m_vl, "%s: warning: integer variable for Metroplis_test is obsolete. use boolean parameter.\n", class_name.c_str());
172 
173  return set_parameters(trajectory_length, (Metropolis_test == 0) ? false : true);
174 }
175 
176 
177 //====================================================================
178 void HMC_General::set_parameters(const double trajectory_length, const bool Metropolis_test)
179 {
180  //- print input parameters
181  vout.general(m_vl, "%s:\n", class_name.c_str());
182  vout.general(m_vl, " Number of actions: %4d\n", m_action.size());
183  vout.general(m_vl, " traj_length = %8.6f\n", trajectory_length);
184  vout.general(m_vl, " Metropolis_test = %s\n", Metropolis_test ? "true" : "false");
185 
186  //- range check
187  // NB. Metropolis_test == 0 is allowed.
188 
189  //- store values
190  m_trajectory_length = trajectory_length;
191  m_Metropolis_test = Metropolis_test;
192 }
193 
194 
195 //====================================================================
197 {
198  const int Nc = CommonParameters::Nc();
199 
200  Field_G U(Uorg);
201 
202  for (int i = 0; i < m_action.size(); ++i) {
203  m_action[i]->set_config(&U);
204  }
205 
206  const int Nin = U.nin();
207  const int Nvol = U.nvol();
208  const int Nex = U.nex();
209 
210  Field_G iP(Nvol, Nex);
211 
212 
213  vout.general(m_vl, "HMC (general) start.\n");
214 
215  // Langevin step
216  const double H_total0 = langevin(iP, U);
217  vout.general(m_vl, " H_total(init) = %20.10f\n", H_total0);
218 
219  const double plaq0 = m_staple->plaquette(U);
220  vout.general(m_vl, " plaq(init) = %20.12f\n", plaq0);
221 
222  // initial Hamiltonian
223  // H_total0 = calc_Hamiltonian(iP,U);
224 
225  // molecular dynamical integration
227 
228  // trial Hamiltonian
229  const double H_total1 = calc_Hamiltonian(iP, U);
230  vout.general(m_vl, " H_total(trial) = %20.10f\n", H_total1);
231 
232  const double plaq1 = m_staple->plaquette(U);
233  vout.general(m_vl, " plaq(trial) = %20.12f\n", plaq1);
234 
235 
236  // Metropolis test
237  vout.general(m_vl, "Metropolis test.\n");
238 
239  const double diff_H = H_total1 - H_total0;
240  const double exp_minus_diff_H = exp(-diff_H);
241 
242  double rand = -1.0;
243  if (m_Metropolis_test) {
244  rand = m_rand->get();
245  }
246  vout.general(m_vl, " H(diff) = %14.8f\n", diff_H);
247  vout.general(m_vl, " exp(-diff_H) = %14.8f\n", exp_minus_diff_H);
248  vout.general(m_vl, " Random number = %14.8f\n", rand);
249 
250  if (rand <= exp_minus_diff_H) { // accepted
251  vout.general(m_vl, " Accepted\n");
252  Uorg = U;
253  } else { // rejected
254  vout.general(m_vl, " Rejected\n");
255  }
256 
257 
258  const double plaq_final = m_staple->plaquette(Uorg);
259  vout.general(m_vl, " plaq(final) = %20.12f\n", plaq_final);
260 
261  return plaq_final;
262 }
263 
264 
265 //====================================================================
266 double HMC_General::langevin(Field_G& iP, const Field_G& U)
267 {
268  const int Nc = CommonParameters::Nc();
269  const int Ndim = CommonParameters::Ndim();
270  const int NcA = Nc * Nc - 1;
271 
272  const int Nvol = CommonParameters::Nvol();
273  const int NPE = CommonParameters::NPE();
274 
275  vout.general(m_vl, "Langevin step.\n");
276 
277  // discard caches
279 
280  for (int i = 0; i < m_director.size(); ++i) {
281  m_director[i]->notify_linkv();
282  }
283 
284  // kinetic term
285  double H_iP = m_Langevin_P->set_iP(iP);
286  vout.general(m_vl, " Kinetic term:\n");
287  vout.general(m_vl, " H_kin = %18.8f\n", H_iP);
288  vout.general(m_vl, " H_kin/dof = %18.8f\n", H_iP / NcA / Nvol / NPE / Ndim);
289 
290 
291  double H_actions = 0.0;
292  for (int i = 0; i < m_action.size(); ++i) {
293  H_actions += m_action[i]->langevin(m_rand);
294  }
295 
296  double H_total = H_iP + H_actions;
297  return H_total;
298 }
299 
300 
301 //====================================================================
302 double HMC_General::calc_Hamiltonian(const Field_G& iP, const Field_G& U)
303 {
304  const int Nin = U.nin();
305  const int Nvol = U.nvol();
306  const int Nex = U.nex();
307 
308  const int Nc = CommonParameters::Nc();
309  const int Nd = CommonParameters::Nd();
310  const int NcA = Nc * Nc - 1;
311 
312  const int NPE = CommonParameters::NPE();
313 
314  vout.general(m_vl, "Hamiltonian calculation.\n");
315 
316  // kinetic term
317  double H_iP = calcH_P(iP);
318  vout.general(m_vl, " Kinetic term:\n");
319  vout.general(m_vl, " H_kin = %18.8f\n", H_iP);
320  vout.general(m_vl, " H_kin/dof = %18.8f\n", H_iP / NcA / Nvol / NPE / Nex);
321 
322  double H_actions = 0.0;
323  for (int i = 0; i < m_action.size(); ++i) {
324  H_actions += m_action[i]->calcH();
325  }
326 
327  double H_total = H_iP + H_actions;
328  return H_total;
329 }
330 
331 
332 //====================================================================
333 double HMC_General::calcH_P(const Field_G& iP)
334 {
335  const double hn = iP.norm();
336  const double H_iP = 0.5 * hn * hn;
337 
338  return H_iP;
339 }
340 
341 
342 //====================================================================
343 //============================================================END=====
Parameters::set_bool
void set_bool(const string &key, const bool value)
Definition: parameters.cpp:30
HMC_General::m_trajectory_length
double m_trajectory_length
Definition: hmc_General.h:59
HMC_General::HMC_General
HMC_General(const ActionList &action_list, std::vector< Director * > director, Integrator *integrator, RandomNumbers *rand)
constructor with action_list, directors, and random number generator
Definition: hmc_General.cpp:20
Langevin_Momentum::set_iP
double set_iP(Field_G &iP)
Setting conjugate momenta and returns kinetic part of Hamiltonian.
Definition: langevin_Momentum.cpp:17
Integrator::invalidate_cache
virtual void invalidate_cache()=0
Parameters::set_string
void set_string(const string &key, const string &value)
Definition: parameters.cpp:39
CommonParameters
Common parameter class: provides parameters as singleton.
Definition: commonParameters.h:42
HMC_General::m_integrator
Integrator * m_integrator
MD integrator.
Definition: hmc_General.h:55
HMC_General::update
double update(Field_G &)
Definition: hmc_General.cpp:196
CommonParameters::Ndim
static int Ndim()
Definition: commonParameters.h:117
Parameters
Class for parameters.
Definition: parameters.h:46
Staple_lex::plaquette
double plaquette(const Field_G &)
calculates plaquette value.
Definition: staple_lex.cpp:89
Parameters::set_double
void set_double(const string &key, const double value)
Definition: parameters.cpp:33
Field::nex
int nex() const
Definition: field.h:128
RandomNumbers
Base class of random number generators.
Definition: randomNumbers.h:43
HMC_General::m_Langevin_P
Langevin_Momentum * m_Langevin_P
Definition: hmc_General.h:58
CommonParameters::Nvol
static int Nvol()
Definition: commonParameters.h:109
HMC_General::m_rand
RandomNumbers * m_rand
random number generator
Definition: hmc_General.h:56
HMC_General::m_action
std::vector< Action * > m_action
actions
Definition: hmc_General.h:53
Field::nin
int nin() const
Definition: field.h:126
Parameters::fetch_bool
int fetch_bool(const string &key, bool &value) const
Definition: parameters.cpp:391
ActionSet
std::vector< Action * > ActionSet
Definition: action_list.h:38
ActionList
lists of actions at respective integrator levels.
Definition: action_list.h:40
HMC_General::get_parameters
void get_parameters(Parameters &params) const
Definition: hmc_General.cpp:159
CommonParameters::Nc
static int Nc()
Definition: commonParameters.h:115
hmc_General.h
HMC_General::langevin
double langevin(Field_G &iP, const Field_G &U)
Definition: hmc_General.cpp:266
RandomNumbers::get
virtual double get()=0
HMC_General::set_parameters
void set_parameters(const Parameters &params)
Definition: hmc_General.cpp:134
HMC_General::m_director
std::vector< Director * > m_director
directors
Definition: hmc_General.h:54
Field::norm
double norm() const
Definition: field.h:226
HMC_General::~HMC_General
~HMC_General()
destructor
Definition: hmc_General.cpp:126
HMC_General::class_name
static const std::string class_name
Definition: hmc_General.h:48
HMC_General::m_vl
Bridge::VerboseLevel m_vl
Definition: hmc_General.h:61
HMC_General::calcH_P
double calcH_P(const Field_G &iP)
Definition: hmc_General.cpp:333
Langevin_Momentum
Langevin part of HMC for conjugate momentum to link variable.
Definition: langevin_Momentum.h:38
Field::nvol
int nvol() const
Definition: field.h:127
HMC_General::calc_Hamiltonian
double calc_Hamiltonian(const Field_G &iP, const Field_G &U)
Definition: hmc_General.cpp:302
CommonParameters::NPE
static int NPE()
Definition: commonParameters.h:101
CommonParameters::Nd
static int Nd()
Definition: commonParameters.h:116
Staple_lex
Staple construction.
Definition: staple_lex.h:39
Bridge::BridgeIO::set_verbose_level
static VerboseLevel set_verbose_level(const std::string &str)
Definition: bridgeIO.cpp:133
HMC_General::m_staple
Staple_lex * m_staple
Definition: hmc_General.h:57
ActionList::get_actions
ActionSet get_actions() const
Definition: action_list.cpp:82
Parameters::fetch_string
int fetch_string(const string &key, string &value) const
Definition: parameters.cpp:378
Parameters::fetch_double
int fetch_double(const string &key, double &value) const
Definition: parameters.cpp:327
Bridge::BridgeIO::crucial
void crucial(const char *format,...)
Definition: bridgeIO.cpp:180
Field_G
SU(N) gauge field.
Definition: field_G.h:38
Integrator
Base class of Integrator class family.
Definition: integrator.h:29
Integrator::evolve
virtual void evolve(const double step_size, Field_G &iP, Field_G &U)=0
Bridge::BridgeIO::general
void general(const char *format,...)
Definition: bridgeIO.cpp:200
HMC_General::m_Metropolis_test
bool m_Metropolis_test
Metropolis test: enabled if true.
Definition: hmc_General.h:52
Bridge::vout
BridgeIO vout
Definition: bridgeIO.cpp:512
Bridge::BridgeIO::get_verbose_level
static std::string get_verbose_level(const VerboseLevel vl)
Definition: bridgeIO.cpp:154