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