Bridge++  Version 1.5.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 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  std::vector<Director *> director,
48  Integrator *integrator,
50  : m_vl(CommonParameters::Vlevel())
51 {
52  ActionSet action_set = action_list.get_actions();
53 
54  m_action.resize(action_set.size());
55  for (int i = 0; i < action_set.size(); ++i) {
56  m_action[i] = action_set[i];
57  }
58  m_director.resize(director.size());
59  for (int i = 0; i < director.size(); ++i) {
60  m_director[i] = director[i];
61  }
62  m_integrator = integrator;
63  m_rand = rand.get();
64  m_staple = new Staple_lex;
65  m_Metropolis_test = false;
67  m_trajectory_length = 0.0;
68 }
69 
70 
71 //====================================================================
74  Integrator *integrator,
75  RandomNumbers *rand)
76  : m_vl(CommonParameters::Vlevel())
77 {
78  ActionSet action_set = action_list.get_actions();
79 
80  m_action.resize(action_set.size());
81  for (int i = 0; i < action_set.size(); ++i) {
82  m_action[i] = action_set[i];
83  }
84  m_integrator = integrator;
85  m_rand = rand;
86  m_staple = new Staple_lex;
87  m_Metropolis_test = false;
89  m_trajectory_length = 0.0;
90 }
91 
92 
93 //====================================================================
95  Integrator *integrator,
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.get();
107  m_staple = new Staple_lex;
108  m_Metropolis_test = false;
110  m_trajectory_length = 0.0;
111 }
112 
113 
114 //====================================================================
117 {
118  delete m_Langevin_P;
119  delete m_staple;
120 }
121 
122 
123 //====================================================================
125 {
126  const string str_vlevel = params.get_string("verbose_level");
127 
128  m_vl = vout.set_verbose_level(str_vlevel);
129 
130  //- fetch and check input parameters
131  double traj_length;
132  bool Metropolis_test;
133 
134  int err = 0;
135  err += params.fetch_double("trajectory_length", traj_length);
136  err += params.fetch_bool("Metropolis_test", Metropolis_test);
137 
138  if (err) {
139  vout.crucial(m_vl, "Error at %s: input parameter not found.\n", class_name.c_str());
140  exit(EXIT_FAILURE);
141  }
142 
143  set_parameters(traj_length, Metropolis_test);
144 }
145 
146 
147 //====================================================================
148 void HMC_General::set_parameters(const double trajectory_length, const int Metropolis_test)
149 {
150  vout.crucial(m_vl, "%s: warning: integer variable for Metroplis_test is obsolete. use boolean parameter.\n", class_name.c_str());
151 
152  return set_parameters(trajectory_length, (Metropolis_test == 0) ? false : true);
153 }
154 
155 
156 //====================================================================
157 void HMC_General::set_parameters(const double trajectory_length, const bool Metropolis_test)
158 {
159  //- print input parameters
160  vout.general(m_vl, "%s:\n", class_name.c_str());
161  vout.general(m_vl, " Number of actions: %4d\n", m_action.size());
162  vout.general(m_vl, " traj_length = %8.6f\n", trajectory_length);
163  vout.general(m_vl, " Metropolis_test = %s\n", Metropolis_test ? "true" : "false");
164 
165  //- range check
166  // NB. Metropolis_test == 0 is allowed.
167 
168  //- store values
169  m_trajectory_length = trajectory_length;
170  m_Metropolis_test = Metropolis_test;
171 }
172 
173 
174 //====================================================================
176 {
177  const int Nc = CommonParameters::Nc();
178 
179  Field_G U(Uorg);
180 
181  for (int i = 0; i < m_action.size(); ++i) {
182  m_action[i]->set_config(&U);
183  }
184 
185  const int Nin = U.nin();
186  const int Nvol = U.nvol();
187  const int Nex = U.nex();
188 
189  Field_G iP(Nvol, Nex);
190 
191 
192  vout.general(m_vl, "HMC (general) start.\n");
193 
194  // Langevin step
195  const double H_total0 = langevin(iP, U);
196  vout.general(m_vl, " H_total(init) = %20.10f\n", H_total0);
197 
198  const double plaq0 = m_staple->plaquette(U);
199  vout.general(m_vl, " plaq(init) = %20.12f\n", plaq0);
200 
201  // initial Hamiltonian
202  // H_total0 = calc_Hamiltonian(iP,U);
203 
204  // molecular dynamical integration
206 
207  // trial Hamiltonian
208  const double H_total1 = calc_Hamiltonian(iP, U);
209  vout.general(m_vl, " H_total(trial) = %20.10f\n", H_total1);
210 
211  const double plaq1 = m_staple->plaquette(U);
212  vout.general(m_vl, " plaq(trial) = %20.12f\n", plaq1);
213 
214 
215  // Metropolis test
216  vout.general(m_vl, "Metropolis test.\n");
217 
218  const double diff_H = H_total1 - H_total0;
219  const double exp_minus_diff_H = exp(-diff_H);
220 
221  double rand = -1.0;
222  if (m_Metropolis_test) {
223  rand = m_rand->get();
224  }
225  vout.general(m_vl, " H(diff) = %14.8f\n", diff_H);
226  vout.general(m_vl, " exp(-diff_H) = %14.8f\n", exp_minus_diff_H);
227  vout.general(m_vl, " Random number = %14.8f\n", rand);
228 
229  if (rand <= exp_minus_diff_H) { // accepted
230  vout.general(m_vl, " Accepted\n");
231  Uorg = U;
232  } else { // rejected
233  vout.general(m_vl, " Rejected\n");
234  }
235 
236 
237  const double plaq_final = m_staple->plaquette(Uorg);
238  vout.general(m_vl, " plaq(final) = %20.12f\n", plaq_final);
239 
240  return plaq_final;
241 }
242 
243 
244 //====================================================================
245 double HMC_General::langevin(Field_G& iP, const Field_G& U)
246 {
247  const int Nc = CommonParameters::Nc();
248  const int Ndim = CommonParameters::Ndim();
249  const int NcA = Nc * Nc - 1;
250 
251  const int Nvol = CommonParameters::Nvol();
252  const int NPE = CommonParameters::NPE();
253 
254  vout.general(m_vl, "Langevin step.\n");
255 
256  // discard caches
258 
259  for (int i = 0; i < m_director.size(); ++i) {
260  m_director[i]->notify_linkv();
261  }
262 
263  // kinetic term
264  double H_iP = m_Langevin_P->set_iP(iP);
265  vout.general(m_vl, " Kinetic term:\n");
266  vout.general(m_vl, " H_kin = %18.8f\n", H_iP);
267  vout.general(m_vl, " H_kin/dof = %18.8f\n", H_iP / NcA / Nvol / NPE / Ndim);
268 
269 
270  double H_actions = 0.0;
271  for (int i = 0; i < m_action.size(); ++i) {
272  H_actions += m_action[i]->langevin(m_rand);
273  }
274 
275  double H_total = H_iP + H_actions;
276  return H_total;
277 }
278 
279 
280 //====================================================================
281 double HMC_General::calc_Hamiltonian(const Field_G& iP, const Field_G& U)
282 {
283  const int Nin = U.nin();
284  const int Nvol = U.nvol();
285  const int Nex = U.nex();
286 
287  const int Nc = CommonParameters::Nc();
288  const int Nd = CommonParameters::Nd();
289  const int NcA = Nc * Nc - 1;
290 
291  const int NPE = CommonParameters::NPE();
292 
293  vout.general(m_vl, "Hamiltonian calculation.\n");
294 
295  // kinetic term
296  double H_iP = calcH_P(iP);
297  vout.general(m_vl, " Kinetic term:\n");
298  vout.general(m_vl, " H_kin = %18.8f\n", H_iP);
299  vout.general(m_vl, " H_kin/dof = %18.8f\n", H_iP / NcA / Nvol / NPE / Nex);
300 
301  double H_actions = 0.0;
302  for (int i = 0; i < m_action.size(); ++i) {
303  H_actions += m_action[i]->calcH();
304  }
305 
306  double H_total = H_iP + H_actions;
307  return H_total;
308 }
309 
310 
311 //====================================================================
312 double HMC_General::calcH_P(const Field_G& iP)
313 {
314  const double hn = iP.norm();
315  const double H_iP = 0.5 * hn * hn;
316 
317  return H_iP;
318 }
319 
320 
321 //====================================================================
322 //============================================================END=====
BridgeIO vout
Definition: bridgeIO.cpp:503
int fetch_bool(const string &key, bool &value) const
Definition: parameters.cpp:391
virtual double get()=0
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:197
static const std::string class_name
Definition: hmc_General.h:48
int fetch_double(const string &key, double &value) const
Definition: parameters.cpp:327
double plaquette(const Field_G &)
calculates plaquette value.
Definition: staple_lex.cpp:38
int nvol() const
Definition: field.h:127
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
double calc_Hamiltonian(const Field_G &iP, const Field_G &U)
std::vector< Action * > m_action
actions
Definition: hmc_General.h:53
Class for parameters.
Definition: parameters.h:46
virtual void invalidate_cache()=0
Staple_lex * m_staple
Definition: hmc_General.h:57
Base class of Integrator class family.
Definition: integrator.h:29
int nin() const
Definition: field.h:126
ActionSet get_actions() const
Definition: action_list.cpp:89
Staple construction.
Definition: staple_lex.h:39
SU(N) gauge field.
Definition: field_G.h:38
double langevin(Field_G &iP, const Field_G &U)
~HMC_General()
destructor
double norm() const
Definition: field.h:222
pointer get() const
int nex() const
Definition: field.h:128
Common parameter class: provides parameters as singleton.
virtual void evolve(const double step_size, Field_G &iP, Field_G &U)=0
double calcH_P(const Field_G &iP)
bool m_Metropolis_test
Metropolis test: enabled if true.
Definition: hmc_General.h:52
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:43
Integrator * m_integrator
MD integrator.
Definition: hmc_General.h:55
double update(Field_G &)
string get_string(const string &key) const
Definition: parameters.cpp:221
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:20
Langevin part of HMC for conjugate momentum to link variable.