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