QMCPACK
SimpleFixedNodeBranch.cpp
Go to the documentation of this file.
1 //////////////////////////////////////////////////////////////////////////////////////
2 // This file is distributed under the University of Illinois/NCSA Open Source License.
3 // See LICENSE file in top directory for details.
4 //
5 // Copyright (c) 2019 QMCPACK developers.
6 //
7 // File developed by: Peter Doak, doakpw@ornl.gov, Oak Ridge National Laboratory
8 // Ken Esler, kpesler@gmail.com, University of Illinois at Urbana-Champaign
9 // Jeremy McMinnis, jmcminis@gmail.com, University of Illinois at Urbana-Champaign
10 // Luke Shulenburger, lshulen@sandia.gov, Sandia National Laboratories
11 // Jeongnim Kim, jeongnim.kim@gmail.com, University of Illinois at Urbana-Champaign
12 // Raymond Clay III, j.k.rofling@gmail.com, Lawrence Livermore National Laboratory
13 // Ye Luo, yeluo@anl.gov, Argonne National Laboratory
14 // Mark A. Berrill, berrillma@ornl.gov, Oak Ridge National Laboratory
15 //
16 // File created by: Jeongnim Kim, jeongnim.kim@gmail.com, University of Illinois at Urbana-Champaign
17 //////////////////////////////////////////////////////////////////////////////////////
18 
19 
20 #include "SimpleFixedNodeBranch.h"
22 #include <numeric>
23 #include "OhmmsData/FileUtility.h"
27 #include "QMCDrivers/BranchIO.h"
28 #include "Particle/Reptile.h"
31 
32 namespace qmcplusplus
33 {
35 
36 ///enum to yes/no options saved in sParam
37 enum
38 {
39  COMBOPT,
40  USETAUOPT,
41  MIXDMCOPT,
42  DUMMYOPT
43 };
44 
45 SimpleFixedNodeBranch::SimpleFixedNodeBranch(RealType tau, int nideal) //, PopHist(5), DMCEnergyHist(5)
46 {
47  BranchMode.set(B_DMCSTAGE, 0); //warmup stage
48  BranchMode.set(B_POPCONTROL, 1); //use standard DMC
49  BranchMode.set(B_USETAUEFF, 1); //use taueff
50  BranchMode.set(B_CLEARHISTORY, 0); //clear history and start with the current average
51  BranchMode.set(B_KILLNODES, 0); //when killing walkers at nodes etrial is updated differently
52  vParam.fill(1.0);
53  vParam[SBVP::TAU] = tau;
54  vParam[SBVP::TAUEFF] = tau;
55  vParam[SBVP::FEEDBACK] = 1.0;
57  R2Accepted(1.0e-10);
58  R2Proposed(1.0e-10);
59  //set the default values for integer parameters
60  iParam[B_WARMUPSTEPS] = 200;
64  iParam[B_MAXWALKERS] = nideal;
65  iParam[B_MINWALKERS] = nideal;
66  iParam[B_COUNTER] = -1;
67  //default is no
68  sParam.resize(DUMMYOPT, "no");
69  //default is classic
70  branching_cutoff_scheme = "classic";
72  reset();
73 }
74 
75 /** copy constructor
76  *
77  * Copy only selected data members and WalkerController is never copied.
78  */
80  : BranchMode(abranch.BranchMode),
81  iParam(abranch.iParam),
82  vParam(abranch.vParam),
83  branching_cutoff_scheme(abranch.branching_cutoff_scheme),
84  sParam(abranch.sParam)
85 {
87  reset();
88 }
89 
91 
93 {
94  m_param.add(iParam[B_WARMUPSTEPS], "warmupSteps");
95  m_param.add(iParam[B_WARMUPSTEPS], "warmupsteps");
96  m_param.add(iParam[B_ENERGYUPDATEINTERVAL], "energyUpdateInterval");
97  m_param.add(iParam[B_BRANCHINTERVAL], "branchInterval");
98  m_param.add(iParam[B_TARGETWALKERS], "targetWalkers");
99  m_param.add(iParam[B_TARGETWALKERS], "targetwalkers");
100  m_param.add(iParam[B_TARGETWALKERS], "target_walkers");
101  //trial energy
102  m_param.add(vParam[SBVP::EREF], "refEnergy");
103  m_param.add(vParam[SBVP::EREF], "ref_energy");
104  m_param.add(vParam[SBVP::EREF], "en_ref");
105  m_param.add(vParam[SBVP::TAU], "tau");
106  m_param.add(vParam[SBVP::TAU], "timestep");
107  m_param.add(vParam[SBVP::TAU], "timeStep");
108  m_param.add(vParam[SBVP::TAU], "TimeStep");
109  //filterscale: sets the filtercutoff to sigma*filterscale
110  m_param.add(vParam[SBVP::FILTERSCALE], "filterscale");
111  //feed back parameter for population control
112  m_param.add(vParam[SBVP::FEEDBACK], "feedback");
113  //turn on/off effective tau onl for time-step error comparisons
114  m_param.add(sParam[USETAUOPT], "useBareTau");
115  m_param.add(sParam[MIXDMCOPT], "warmupByReconfiguration");
116  m_param.add(branching_cutoff_scheme, "branching_cutoff_scheme");
117  m_param.add(debug_disable_branching_, "debug_disable_branching", {"no", "yes"});
118 }
119 
120 void SimpleFixedNodeBranch::start(const std::string& froot, bool append)
121 {
122  RootName = froot;
123  MyEstimator->reset();
124 }
125 
127 {
128  BranchMode.set(B_DMC, 1); //set DMC
129  BranchMode.set(B_DMCSTAGE, iParam[B_WARMUPSTEPS] == 0); //use warmup
130  //this is not necessary
131  //check if tau is different and set the initial values
132  //vParam[SBVP::TAU]=tau;
133  bool fromscratch = false;
135 
136  int nwtot_now = walkers.getGlobalNumWalkers();
137 
138  //this is the first time DMC is used
139  if (WalkerController == nullptr)
140  {
141  if (iParam[B_TARGETWALKERS] == 0)
142  {
143  Communicate* acomm = MyEstimator->getCommunicator();
144  int ncontexts = acomm->size();
145  std::vector<int> nw(ncontexts, 0), nwoff(ncontexts + 1, 0);
146  nw[acomm->rank()] = walkers.getActiveWalkers();
147  acomm->allreduce(nw);
148  for (int ip = 0; ip < ncontexts; ++ip)
149  nwoff[ip + 1] = nwoff[ip] + nw[ip];
150  walkers.setWalkerOffsets(nwoff);
151  iParam[B_TARGETWALKERS] = nwoff[ncontexts];
152  }
153  // \todo reparsing XML nodes is an antipattern, remove.
155  if (!BranchMode[B_RESTART])
156  {
157  fromscratch = true;
158  app_log() << " START ALL OVER " << std::endl;
159  vParam[SBVP::TAUEFF] = tau;
160  BranchMode.set(B_POPCONTROL, !fixW); //fixW -> 0
161  BranchMode.set(B_KILLNODES, killwalker);
162  iParam[B_MAXWALKERS] = WalkerController->get_n_max();
163  iParam[B_MINWALKERS] = WalkerController->get_n_min();
164  if (!fixW && sParam[MIXDMCOPT] == "yes")
165  {
166  app_log() << "Warmup DMC is done with a fixed population " << iParam[B_TARGETWALKERS] << std::endl;
167  BackupWalkerController = std::move(WalkerController); //save the main controller
168  // \todo: Not likely to be ok to call reset on a moved from WalkerController!
169  WalkerController.reset(
170  createWalkerController(iParam[B_TARGETWALKERS], MyEstimator->getCommunicator(), myNode, true));
171  BranchMode.set(B_POPCONTROL, 0);
172  }
173  //PopHist.clear();
174  //PopHist.reserve(std::max(iParam[B_ENERGYUPDATEINTERVAL],5));
175  }
176  WalkerController->setWalkerID(walkers);
177  }
178  //else
179  //{
180  // BranchMode.set(B_DMCSTAGE,0);//always reset warmup
181  //}
182  MyEstimator->reset();
183  //update the simulation parameters
184  WalkerController->put(myNode);
185  //assign current Eref and a large number for variance
186  WalkerController->setTrialEnergy(vParam[SBVP::ETRIAL]);
187  this->reset();
188  if (fromscratch)
189  {
190  //determine the branch cutoff to limit wild weights based on the sigma and sigmaBound
191  setBranchCutoff(vParam[SBVP::SIGMA2], WalkerController->get_target_sigma(), 50, walkers.R.size());
193  }
194  //reset controller
195  WalkerController->reset();
197  BackupWalkerController->reset();
198  app_log() << " QMC counter = " << iParam[B_COUNTER] << std::endl;
199  app_log() << " time step = " << vParam[SBVP::TAU] << std::endl;
200  app_log() << " effective time step = " << vParam[SBVP::TAUEFF] << std::endl;
201  app_log() << " trial energy = " << vParam[SBVP::ETRIAL] << std::endl;
202  app_log() << " reference energy = " << vParam[SBVP::EREF] << std::endl;
203  app_log() << " Feedback = " << vParam[SBVP::FEEDBACK] << std::endl;
204  app_log() << " reference variance = " << vParam[SBVP::SIGMA2] << std::endl;
205  app_log() << " target walkers = " << iParam[B_TARGETWALKERS] << std::endl;
206  app_log() << " branching cutoff scheme " << branching_cutoff_scheme << std::endl;
207  app_log() << " branch cutoff = " << vParam[SBVP::BRANCHCUTOFF] << " " << vParam[SBVP::BRANCHMAX] << std::endl;
208  app_log() << " Max and minimum walkers per node= " << iParam[B_MAXWALKERS] << " " << iParam[B_MINWALKERS]
209  << std::endl;
210  app_log() << " QMC Status (BranchMode) = " << BranchMode << std::endl;
211  if (debug_disable_branching_ == "yes")
212  app_log() << " Disable branching for debugging as the user input request." << std::endl;
213 
214  return int(round(double(iParam[B_TARGETWALKERS]) / double(nwtot_now)));
215 }
216 
218 {
219  RealType allowedFlux = 50.0;
220  BranchMode.set(B_RMC, 1); //set RMC
221  BranchMode.set(B_RMCSTAGE, iParam[B_WARMUPSTEPS] == 0); //use warmup
222  //this is not necessary
223  //check if tau is different and set the initial values
224  //vParam[SBVP::TAU]=tau;
225  bool fromscratch = false;
227  //this is the first time DMC is used
228  if (WalkerController == 0)
229  {
230  // if(iParam[B_TARGETWALKERS]==0)
231  // {
232  // Communicate* acomm=MyEstimator->getCommunicator();
233  // int ncontexts=acomm->size();
234  // std::vector<int> nw(ncontexts,0),nwoff(ncontexts+1,0);
235  // nw[acomm->rank()]=W.getActiveWalkers();
236  // acomm->allreduce(nw);
237  // for(int ip=0; ip<ncontexts; ++ip)
238  // nwoff[ip+1]=nwoff[ip]+nw[ip];
239  // W.setWalkerOffsets(nwoff);
240  // iParam[B_TARGETWALKERS]=nwoff[ncontexts];
241  // }
242  if (!BranchMode[B_RESTART])
243  {
244  fromscratch = true;
245  app_log() << " START ALL OVER " << std::endl;
246  vParam[SBVP::TAUEFF] = tau;
247  //PopHist.clear();
248  //PopHist.reserve(std::max(iParam[B_ENERGYUPDATEINTERVAL],5));
249  }
250  }
251  //else
252  //{
253  // BranchMode.set(B_DMCSTAGE,0);//always reset warmup
254  //}
255  MyEstimator->reset();
256  this->reset();
257  if (fromscratch)
258  {
259  //determine the branch cutoff to limit wild weights based on the sigma and sigmaBound
260  setBranchCutoff(vParam[SBVP::SIGMA2], allowedFlux, 50, W.R.size());
262  }
263  //reset controller
264  app_log() << " QMC counter = " << iParam[B_COUNTER] << std::endl;
265  app_log() << " time step = " << vParam[SBVP::TAU] << std::endl;
266  app_log() << " effective time step = " << vParam[SBVP::TAUEFF] << std::endl;
267  app_log() << " reference energy = " << vParam[SBVP::EREF] << std::endl;
268  app_log() << " Feedback = " << vParam[SBVP::FEEDBACK] << std::endl;
269  app_log() << " reference variance = " << vParam[SBVP::SIGMA2] << std::endl;
270  app_log() << " branching cutoff scheme " << branching_cutoff_scheme << std::endl;
271  app_log() << " branch cutoff = " << vParam[SBVP::BRANCHCUTOFF] << " " << vParam[SBVP::BRANCHMAX] << std::endl;
272  app_log() << " QMC Status (BranchMode) = " << BranchMode << std::endl;
273 }
274 
276 {
277  if (counter == 0 && WalkerController)
278  WalkerController->reset();
279 }
280 
282 {
283  //collect the total weights and redistribute the walkers accordingly, using a fixed tolerance
284 
285  FullPrecRealType pop_now;
286  if (debug_disable_branching_ == "no" && (BranchMode[B_DMCSTAGE] || iter))
287  pop_now = WalkerController->branch(iter, walkers, 0.1);
288  else
289  pop_now = WalkerController->doNotBranch(iter, walkers); //do not branch for the first step of a warmup
290 
291  //population for trial energy modification should not include any released node walkers.
292  pop_now -= WalkerController->get_ensemble_property().RNSamples;
293  //current energy
294  vParam[SBVP::ENOW] = WalkerController->get_ensemble_property().Energy;
295  VarianceHist(WalkerController->get_ensemble_property().Variance);
296  R2Accepted(WalkerController->get_ensemble_property().R2Accepted);
297  R2Proposed(WalkerController->get_ensemble_property().R2Proposed);
298  //PopHist(pop_now);
299  vParam[SBVP::EREF] = EnergyHist.mean(); //current mean
300  if (BranchMode[B_USETAUEFF])
302 
303  if (BranchMode[B_KILLNODES])
305  std::log(WalkerController->get_ensemble_property().LivingFraction) / vParam[SBVP::TAUEFF]);
306  else
308 
309  if (BranchMode[B_DMCSTAGE]) // main stage
310  {
312  {
313  if (ToDoSteps > 0)
314  --ToDoSteps;
315  else
316  {
319  }
320  }
321  else
323  }
324  else //warmup
325  {
326  if (BranchMode[B_USETAUEFF])
329  {
330  //RealType emix=((iParam[B_WARMUPSTEPS]-ToDoSteps)<100)?(0.25*vParam[SBVP::EREF]+0.75*vParam[SBVP::ENOW]):vParam[SBVP::EREF];
331  //vParam[SBVP::ETRIAL]=emix+Feedback*(logN-std::log(pop_now));
332  //vParam[SBVP::ETRIAL]=vParam[SBVP::EREF]+Feedback*(logN-std::log(pop_now));
333  if (BranchMode[B_KILLNODES])
334  vParam[SBVP::ETRIAL] = (0.00 * vParam[SBVP::EREF] + 1.0 * vParam[SBVP::ENOW]) +
335  vParam[SBVP::FEEDBACK] * (logN - std::log(pop_now)) -
336  std::log(WalkerController->get_ensemble_property().LivingFraction) / vParam[SBVP::TAU];
337  else
339  }
340  else
342  --ToDoSteps;
343  if (ToDoSteps == 0) //warmup is done
344  {
346  setBranchCutoff(vParam[SBVP::SIGMA2], WalkerController->get_target_sigma(), 10, walkers.R.size());
347  app_log() << "\n Warmup is completed after " << iParam[B_WARMUPSTEPS] << std::endl;
348  if (BranchMode[B_USETAUEFF])
349  app_log() << "\n TauEff = " << vParam[SBVP::TAUEFF]
350  << "\n TauEff/Tau = " << vParam[SBVP::TAUEFF] / vParam[SBVP::TAU];
351  else
352  app_log() << "\n TauEff proposed = " << vParam[SBVP::TAUEFF] * R2Accepted.result() / R2Proposed.result();
353  app_log() << "\n Etrial = " << vParam[SBVP::ETRIAL] << std::endl;
354  app_log() << " Running average of energy = " << EnergyHist.mean() << std::endl;
355  app_log() << " Variance = " << vParam[SBVP::SIGMA2] << std::endl;
356  app_log() << "branch cutoff = " << vParam[SBVP::BRANCHCUTOFF] << " " << vParam[SBVP::BRANCHMAX] << std::endl;
358  iParam[B_WARMUPSTEPS] = 0;
359  BranchMode.set(B_DMCSTAGE, 1); //set BranchModex to main stage
360  //reset the histogram
361  EnergyHist.clear();
363  if (sParam[MIXDMCOPT] == "yes")
364  {
365  app_log() << "Switching to DMC with fluctuating populations" << std::endl;
366  BranchMode.set(B_POPCONTROL, 1); //use standard DMC
370  app_log() << " Etrial = " << vParam[SBVP::ETRIAL] << std::endl;
371  WalkerController->start();
372  }
373  //This is not necessary
374  //EnergyHist(DMCEnergyHist.mean());
375  }
376  }
377  WalkerController->setTrialEnergy(vParam[SBVP::ETRIAL]);
378  //accumulate collectables and energies for scalar.dat
379  FullPrecRealType wgt_inv = WalkerController->get_num_contexts() / WalkerController->get_ensemble_property().Weight;
380  walkers.Collectables *= wgt_inv;
381  MyEstimator->accumulate(walkers);
382 }
383 
384 /**
385  *
386  */
388 {
389  //Update the current energy and accumulate.
392  vParam[SBVP::ENOW] = 0.5 * (head.Properties(WP::LOCALENERGY) + tail.Properties(WP::LOCALENERGY));
393  // app_log()<<"IN SimpleFixedNodeBranch::collect\n";
394  // app_log()<<"\tvParam[SBVP::ENOW]="<<vParam[SBVP::ENOW]<< std::endl;
397  // app_log()<<"\tvParam[SBVP::EREF]="<<vParam[SBVP::EREF]<< std::endl;
398  //Update the energy variance and R2 for effective timestep and filtering.
400  R2Accepted(head.Properties(WP::R2ACCEPTED));
401  R2Proposed(head.Properties(WP::R2PROPOSED));
402  // app_log()<<"\thead.Properties(R2ACCEPTED)="<<head.Properties(R2ACCEPTED)<< std::endl;
403  // app_log()<<"\thead.Properties(R2PROPOSED)="<<head.Properties(R2PROPOSED)<< std::endl;
404  // app_log()<<"\tR2Accepted="<<R2Accepted.result()<< std::endl;
405  // app_log()<<"\tR2Proposed="<<R2Proposed.result()<< std::endl;
406  // app_log()<<"\tR2Accept/R2Prop="<<R2Accepted.result()/R2Proposed.result()<< std::endl;
407  // app_log()<<"\t <E^2> = "<<VarianceHist.mean()<< std::endl;
408  // app_log()<<"\t <E> = "<<EnergyHist.mean()<< std::endl;
409  // app_log()<<"\t <E>^2 = "<<std::pow(EnergyHist.mean(),2)<< std::endl;
410  // app_log()<<"\t var = "<<VarianceHist.mean()-pow(EnergyHist.mean(),2)<< std::endl;
411  // app_log()<<"--------------\n";
412  //current mean
413  if (BranchMode[B_USETAUEFF])
414  {
415  //app_log()<<" BRANCHMODE = "<<BranchMode[B_USETAUEFF]<< std::endl;
417  // app_log()<<"\tvParam[SBVP::TAU]="<<vParam[SBVP::TAU]<<" "<<vParam[SBVP::TAUEFF]<< std::endl;
418  }
419  /*
420  if(BranchMode[B_RMCSTAGE]) // main stage
421  {
422  if(BranchMode[B_POPCONTROL])
423  {
424  if(ToDoSteps>0)
425  --ToDoSteps;
426  else
427  {
428  vParam[SBVP::ETRIAL]=vParam[SBVP::EREF]+vParam[SBVP::FEEDBACK]*(logN-std::log(pop_now));
429  ToDoSteps=iParam[B_ENERGYUPDATEINTERVAL]-1;
430  }
431  }
432  else
433  vParam[SBVP::ETRIAL]=vParam[SBVP::EREF];
434  }*/
435  //app_log()<<"BranchMode[B_RMCSTAGE]="<<BranchMode[B_RMCSTAGE]<< std::endl;
436  if (!BranchMode[B_RMCSTAGE]) //warmup
437  {
438  if (BranchMode[B_USETAUEFF])
440  // app_log()<<"\t <E^2> = "<<VarianceHist.mean()<< std::endl;
441  // app_log()<<"\t <E> = "<<EnergyHist.mean()<< std::endl;
442  // app_log()<<"\t <E>^2 = "<<std::pow(EnergyHist.mean(),2)<< std::endl;
443  //app_log()<<"\t var = "<<VarianceHist.mean()-std::pow(EnergyHist.mean(),2)<< std::endl;
444  // app_log()<<"\t var = "<<VarianceHist.mean()<< std::endl;
445  // app_log()<<"----\n";
446  //app_log()<<"ToDoSteps="<<ToDoSteps<< std::endl;
448  --ToDoSteps;
449  if (ToDoSteps == 0) //warmup is done
450  {
454  app_log() << "\n Warmup is completed after " << iParam[B_WARMUPSTEPS] << " steps." << std::endl;
455  if (BranchMode[B_USETAUEFF])
456  app_log() << "\n TauEff = " << vParam[SBVP::TAUEFF]
457  << "\n TauEff/Tau = " << vParam[SBVP::TAUEFF] / vParam[SBVP::TAU];
458  else
459  app_log() << "\n TauEff proposed = " << vParam[SBVP::TAUEFF] * R2Accepted.result() / R2Proposed.result();
460  app_log() << "\n Running average of energy = " << EnergyHist.mean() << std::endl;
461  app_log() << "\n Variance = " << vParam[SBVP::SIGMA2] << std::endl;
462  app_log() << "\nbranch cutoff = " << vParam[SBVP::BRANCHCUTOFF] << " " << vParam[SBVP::BRANCHMAX] << std::endl;
464  iParam[B_WARMUPSTEPS] = 0;
465  BranchMode.set(B_RMCSTAGE, 1); //set BranchModex to main stage
466  //reset the histogram
467  EnergyHist.clear();
469  }
470  }
471  //accumulate collectables and energies for scalar.dat
472  MyEstimator->accumulate(W);
473 }
474 
475 /** Calculates and saves various action components, also does necessary updates for running averages.
476  *
477  */
479 {
480  //use effective time step of BranchInterval*Tau
481  //Feed = 1.0/(static_cast<RealType>(NumGeneration*BranchInterval)*Tau);
482  //logN = Feed*std::log(static_cast<RealType>(Nideal));
483  //JNKIM passive
484  //BranchMode.set(B_DMC,1);//set DMC
485  //BranchMode.set(B_DMCSTAGE,0);//set warmup
486  if (WalkerController)
487  {
488  //this is to compare the time step errors
489  BranchMode.set(B_USETAUEFF, sParam[USETAUOPT] == "no");
490  if (BranchMode[B_DMCSTAGE]) //
492  else
495  {
496  //logN = Feedback*std::log(static_cast<RealType>(iParam[B_TARGETWALKERS]));
497  logN = std::log(static_cast<FullPrecRealType>(iParam[B_TARGETWALKERS]));
498  if (vParam[SBVP::FEEDBACK] == 0.0)
499  vParam[SBVP::FEEDBACK] = 1.0;
500  }
501  else
502  {
503  //may set Eref to a safe value
504  //if(EnergyHistory.count()<5) Eref -= vParam[EnergyWindowIndex];
506  vParam[SBVP::FEEDBACK] = 0.0;
507  logN = 0.0;
508  }
509  // vParam(abranch.vParam)
510  WalkerController->start();
511  }
512  if (BranchMode[B_RMC])
513  {
514  //this is to compare the time step errors
515  // BranchMode.set(B_USETAUEFF,sParam[USETAUOPT]=="no");
516  if (BranchMode[B_RMCSTAGE]) //
518  else
520  }
521 }
522 
524 {
525  app_log() << "BRANCH resetRun" << std::endl;
526  //estimator is always reset
527  MyEstimator->reset();
528  MyEstimator->setCollectionMode(true);
529  std::bitset<B_MODE_MAX> bmode(BranchMode);
530  IParamType iparam_old(iParam);
531  VParamType vparam_old(vParam);
532  myNode = cur;
533  //store old target
534  int nw_target = iParam[B_TARGETWALKERS];
535  m_param.put(cur);
536 
537  int target_min = -1;
538  ParameterSet p;
539  p.add(target_min, "minimumtargetwalkers"); //p.add(target_min,"minimumTargetWalkers");
540  p.add(target_min, "minimumsamples"); //p.add(target_min,"minimumSamples");
541  p.put(cur);
542 
543  if (iParam[B_TARGETWALKERS] < target_min)
544  {
545  iParam[B_TARGETWALKERS] = target_min;
546  }
547 
548  bool same_wc = true;
550  {
551  std::string reconfig("no");
552  // method is actually IndexType so conceivably indicates much more that reconfig="yes" or "no"
553  if (WalkerController->get_method())
554  reconfig = "runwhileincorrect"; // forces SR during warmup?
555  std::string reconfig_prev(reconfig);
556  ParameterSet p;
557  p.add(reconfig, "reconfiguration");
558  p.put(cur);
559  if (reconfig != "no" && reconfig != "runwhileincorrect")
560  {
561  // remove this once bug is fixed
562  throw std::runtime_error("Reconfiguration is currently broken and gives incorrect results. Use dynamic "
563  "population control by setting reconfiguration=\"no\" or removing the reconfiguration "
564  "option from the DMC input section. If accessing the broken reconfiguration code path "
565  "is still desired, set reconfiguration to \"runwhileincorrect\" instead of \"yes\".");
566  }
567  same_wc = (reconfig == reconfig_prev);
568  }
569 
570  //everything is the same, do nothing
571  if (same_wc && bmode == BranchMode && std::equal(iParam.begin(), iParam.end(), iparam_old.begin()) &&
572  std::equal(vParam.begin(), vParam.end(), vparam_old.begin()))
573  {
574  app_log() << " Continue with the same input as the previous block." << std::endl;
575  app_log().flush();
576  //return 1;
577  }
578  app_log() << " SimpleFixedNodeBranch::resetRun detected changes in <parameter>'s " << std::endl;
579  app_log() << " BranchMode : " << bmode << " " << BranchMode << std::endl;
580 
581  //vmc does not need to do anything with WalkerController
582  if (!BranchMode[B_DMC])
583  {
584  app_log() << " iParam (old): " << iparam_old << std::endl;
585  app_log() << " iParam (new): " << iParam << std::endl;
586  app_log() << " vParam (old): " << vparam_old << std::endl;
587  app_log() << " vParam (new): " << vParam << std::endl;
588  app_log().flush();
589  return 1;
590  }
591 
592  if (WalkerController == nullptr)
593  {
594  APP_ABORT("SimpleFixedNodeBranch::resetRun cannot initialize WalkerController");
595  }
596 
597  if (!same_wc)
598  {
599  app_log() << "Destroy WalkerController. Existing method " << WalkerController->get_method() << std::endl;
600  ;
602  app_log().flush();
603 
604  BranchMode[B_POPCONTROL] = (WalkerController->get_method() == 0);
606  {
608  if (vParam[SBVP::FEEDBACK] == 0.0)
609  vParam[SBVP::FEEDBACK] = 1.0;
610  }
611  }
612 
613  //always add a warmup step using default 10 steps
614  R2Accepted.clear();
615  R2Proposed.clear();
616  //R2Accepted(1.0e-12);
617  //R2Proposed(1.0e-12);
618  BranchMode[B_DMCSTAGE] = 0;
619  WalkerController->put(myNode);
621  setBranchCutoff(vParam[SBVP::SIGMA2], WalkerController->get_target_sigma(), 10);
622  WalkerController->reset();
624  BackupWalkerController->reset();
625 
626  iParam[B_MAXWALKERS] = WalkerController->get_n_max();
627  iParam[B_MINWALKERS] = WalkerController->get_n_min();
628 
629  app_log() << " iParam (old): " << iparam_old << std::endl;
630  app_log() << " iParam (new): " << iParam << std::endl;
631  app_log() << " vParam (old): " << vparam_old << std::endl;
632  app_log() << " vParam (new): " << vParam << std::endl;
633 
634  app_log() << std::endl << " Using branching cutoff scheme " << branching_cutoff_scheme << std::endl;
635 
636  app_log().flush();
637 
638  // return static_cast<int>(iParam[B_TARGETWALKERS]*1.01/static_cast<double>(nw_target));
639  return static_cast<int>(round(static_cast<double>(iParam[B_TARGETWALKERS] / static_cast<double>(nw_target))));
640 }
641 
643 {
644  std::ostringstream o;
645  if (!BranchMode[B_DMCSTAGE])
646  {
647  FullPrecRealType e, sigma2;
648  MyEstimator->getApproximateEnergyVariance(e, sigma2);
650  vParam[SBVP::SIGMA2] = sigma2;
651  EnergyHist.clear();
653  //DMCEnergyHist.clear();
656  //DMCEnergyHist(vParam[SBVP::EREF]);
657  o << "SimpleFixedNodeBranch::checkParameters " << std::endl;
658  o << " Average Energy of a population = " << e << std::endl;
659  o << " Energy Variance = " << vParam[SBVP::SIGMA2] << std::endl;
660  }
661  app_log() << o.str() << std::endl;
662  app_log().flush();
663 }
664 
666 {
667  std::ostringstream o;
668  if (WalkerController)
669  {
670  o << "====================================================";
671  o << "\n SimpleFixedNodeBranch::finalize after a DMC block";
672  o << "\n QMC counter = " << iParam[B_COUNTER];
673  o << "\n time step = " << vParam[SBVP::TAU];
674  o << "\n effective time step = " << vParam[SBVP::TAUEFF];
675  o << "\n trial energy = " << vParam[SBVP::ETRIAL];
676  o << "\n reference energy = " << vParam[SBVP::EREF];
677  o << "\n reference variance = " << vParam[SBVP::SIGMA2];
678  o << "\n target walkers = " << iParam[B_TARGETWALKERS];
679  o << "\n branch cutoff = " << vParam[SBVP::BRANCHCUTOFF] << " " << vParam[SBVP::BRANCHMAX];
680  o << "\n Max and minimum walkers per node= " << iParam[B_MAXWALKERS] << " " << iParam[B_MINWALKERS];
681  o << "\n Feedback = " << vParam[SBVP::FEEDBACK];
682  o << "\n QMC Status (BranchMode) = " << BranchMode;
683  o << "\n====================================================";
684  }
685  //running RMC
686  else if (BranchMode[B_RMC])
687  {
688  o << "====================================================";
689  o << "\n SimpleFixedNodeBranch::finalize after a RMC block";
690  o << "\n QMC counter = " << iParam[B_COUNTER];
691  o << "\n time step = " << vParam[SBVP::TAU];
692  o << "\n effective time step = " << vParam[SBVP::TAUEFF];
693  o << "\n reference energy = " << vParam[SBVP::EREF];
694  o << "\n reference variance = " << vParam[SBVP::SIGMA2];
695  o << "\n cutoff energy = " << vParam[SBVP::BRANCHCUTOFF] << " " << vParam[SBVP::BRANCHMAX];
696  o << "\n QMC Status (BranchMode) = " << BranchMode;
697  o << "\n====================================================";
698  }
699  else
700  {
701  //running VMC
702  FullPrecRealType e, sigma2;
703  MyEstimator->getApproximateEnergyVariance(e, sigma2);
705  vParam[SBVP::SIGMA2] = sigma2;
706  //this is just to avoid diving by n-1 == 0
708  //add Eref to the DMCEnergyHistory
709  //DMCEnergyHist(vParam[SBVP::EREF]);
710  o << "====================================================";
711  o << "\n SimpleFixedNodeBranch::finalize after a VMC block";
712  o << "\n QMC counter = " << iParam[B_COUNTER];
713  o << "\n time step = " << vParam[SBVP::TAU];
714  o << "\n reference energy = " << vParam[SBVP::EREF];
715  o << "\n reference variance = " << vParam[SBVP::SIGMA2];
716  o << "\n====================================================";
717  }
718  app_log() << o.str() << std::endl;
719  write(RootName, true);
720 }
721 
722 /** Parse the xml file for parameters
723  *@param cur current xmlNode
724  *
725  * Few important parameters are:
726  * <ul>
727  * <li> en_ref: a reference energy
728  * <li> num_gen: number of generations \f$N_G\f$ to reach equilibrium, used in the feedback parameter
729  * \f$ feed = \frac{1}{N_G \tau} \f$
730  * </ul>
731  */
732 bool SimpleFixedNodeBranch::put(xmlNodePtr cur)
733 {
734  //save it
735  myNode = cur;
736  //check dmc/vmc and decide to create WalkerControllerBase
737  m_param.put(cur);
738  reset();
739  MyEstimator->setCollectionMode(true); //always collect
740  return true;
741 }
742 
743 void SimpleFixedNodeBranch::write(const std::string& fname, bool overwrite)
744 {
745  RootName = fname;
746  if (MyEstimator->is_manager())
747  {
748  //\since 2008-06-24
751  BranchIO<SimpleFixedNodeBranch> hh(*this, MyEstimator->getCommunicator());
752  bool success = hh.write(fname);
753  }
754 }
755 
756 void SimpleFixedNodeBranch::read(const std::string& fname)
757 {
758  BranchMode.set(B_RESTART, 0);
759  if (fname.empty())
760  return;
763  BranchIO<SimpleFixedNodeBranch> hh(*this, MyEstimator->getCommunicator());
764  BranchModeType bmode(BranchMode);
765  bool success = hh.read(fname);
766  if (success && R2Proposed.good() && bmode[B_POPCONTROL] == BranchMode[B_POPCONTROL])
767  {
768  BranchMode.set(B_RESTART, 1);
769  app_log() << " Restarting, cummulative properties:"
770  << "\n energy = " << EnergyHist.mean() << "\n variance = " << VarianceHist.mean()
771  << "\n r2accepted = " << R2Accepted.mean() << "\n r2proposed = " << R2Proposed.mean()
772  << std::endl;
773  }
774  else
775  {
776  if (BranchMode[B_POPCONTROL] != bmode[B_POPCONTROL])
777  {
778  app_log() << " Population control method has changed from " << BranchMode[B_POPCONTROL] << " to "
779  << bmode[B_POPCONTROL] << std::endl;
781  }
782  }
783 
784  app_log().flush();
785 }
786 
788  FullPrecRealType targetSigma,
789  FullPrecRealType maxSigma,
790  int Nelec)
791 {
792  if (branching_cutoff_scheme == "DRV")
793  {
794  // eq.(3), J. Chem. Phys. 89, 3629 (1988).
795  // eq.(9), J. Chem. Phys. 99, 2865 (1993).
797  }
798  else if (branching_cutoff_scheme == "ZSGMA")
799  {
800  // eq.(6), Phys. Rev. B 93, 241118(R) (2016)
801  // do nothing if Nelec is not passed in.
802  if (Nelec > 0)
804  }
805  else if (branching_cutoff_scheme == "YL")
806  {
807  // a scheme from Ye Luo.
808  vParam[SBVP::BRANCHCUTOFF] = std::sqrt(variance) * std::min(targetSigma, std::sqrt(1.0 / vParam[SBVP::TAU]));
809  }
810  else if (branching_cutoff_scheme == "classic")
811  {
812  // default QMCPACK choice which is the same as v3.0.0 and before.
813  vParam[SBVP::BRANCHCUTOFF] = std::min(std::max(variance * targetSigma, maxSigma), 2.5 / vParam[SBVP::TAU]);
814  }
815  else
816  APP_ABORT("SimpleFixedNodeBranch::setBranchCutoff unknown branching cutoff scheme " + branching_cutoff_scheme);
817 
820 }
821 
822 std::ostream& operator<<(std::ostream& os, SimpleFixedNodeBranch::VParamType& rhs)
823 {
824  for (auto value : rhs)
825  os << std::setw(18) << std::setprecision(10) << value;
826  return os;
827 }
828 
829 } // namespace qmcplusplus
void collect(int iter, MCWalkerConfiguration &w)
update RMC counters and running averages.
HamiltonianRef::FullPrecRealType FullPrecRealType
PropertyContainer_t Properties
scalar properties of a walker
Definition: Walker.h:125
std::bitset< B_MODE_MAX > BranchModeType
booleans to set the branch modes
A set of walkers that are to be advanced by Metropolis Monte Carlo.
helper functions for EinsplineSetBuilder
Definition: Configuration.h:43
int rank() const
return the rank
Definition: Communicate.h:116
Walker_t & getTail()
Definition: Reptile.h:98
void reset()
reset the internal parameters
void checkParameters(MCWalkerConfiguration &w)
determine trial and reference energies
std::ostream & app_log()
Definition: OutputManager.h:65
std::string branching_cutoff_scheme
scheme of branching cutoff
accumulator_set< RealType > R2Proposed
a simple accumulator for energy
void branch(int iter, MCWalkerConfiguration &w)
perform branching
void start(const std::string &froot, bool append=false)
start a run
const char walkers[]
Definition: HDFVersion.h:36
xmlNodePtr myNode
save xml element
SimpleFixedNodeBranch(RealType tau, int nideal)
Constructor.
int size() const
return the number of tasks
Definition: Communicate.h:118
bool good() const
return true if Weight!= 0
Definition: accumulators.h:103
void write(const std::string &fname, bool overwrite=true)
write the state
T min(T a, T b)
bool put(std::istream &is) override
read from std::istream
Definition: ParameterSet.h:42
accumulator_set< FullPrecRealType > EnergyHist
a simple accumulator for energy
void setBranchCutoff(FullPrecRealType variance, FullPrecRealType targetSigma, FullPrecRealType maxSigma, int Nelec=0)
set branch cutoff, max, filter
void flush(int counter)
restart averaging
ParameterSet m_param
set of parameters
Wrapping information on parallelism.
Definition: Communicate.h:68
void allreduce(T &)
MakeReturn< BinaryNode< FnPow, typename CreateLeaf< Vector< T1, C1 > >::Leaf_t, typename CreateLeaf< Vector< T2, C2 > >::Leaf_t > >::Expression_t pow(const Vector< T1, C1 > &l, const Vector< T2, C2 > &r)
FullPrecRealType logN
Feed*log(N)
std::unique_ptr< WalkerControlBase > BackupWalkerController
Backup WalkerController for mixed DMC.
Manages the state of QMC sections and handles population control for DMCs.
1 to use taueff accordning to JCP 93, 0 to use tau
frequency of the trial energy updates, default 1
class to handle a set of parameters
Definition: ParameterSet.h:27
WalkerProperties::Indexes WP
Definition: ParticleSet.cpp:34
int initWalkerController(MCWalkerConfiguration &mcwc, bool fixW, bool killwalker)
initialize the WalkerController
1 to kill walkers when a node crossing is detected
1 for the standard dmc, 0 for the comb method
std::ostream & operator<<(std::ostream &out, const AntiSymTensor< T, D > &rhs)
ParticlePos R
Position.
Definition: ParticleSet.h:79
#define APP_ABORT(msg)
Widely used but deprecated fatal error macros from legacy code.
Definition: AppAbort.h:27
Manager class of scalar estimators.
warmup steps, valid when BranchMode[D_DMCSTAGE] == 0
void registerParameters()
create map between the parameter name and variables
void read(const std::string &fname)
MakeReturn< UnaryNode< FnLog, typename CreateLeaf< Vector< T1, C1 > >::Leaf_t > >::Expression_t log(const Vector< T1, C1 > &l)
void add(PDT &aparam, const std::string &aname_in, std::vector< PDT > candidate_values={}, TagStatus status=TagStatus::OPTIONAL)
add a new parameter corresponding to an xmlNode <parameter>
return_type mean() const
return the mean
Definition: accumulators.h:126
MakeReturn< UnaryNode< FnSqrt, typename CreateLeaf< Vector< T1, C1 > >::Leaf_t > >::Expression_t sqrt(const Vector< T1, C1 > &l)
return_type result() const
return the sum
Definition: accumulators.h:108
std::vector< std::string > sParam
string parameters
Indexes
an enum denoting index of physical properties
bool write(const std::string &fname)
Definition: BranchIO.cpp:109
void initReptile(MCWalkerConfiguration &w)
initialize reptile stats
bool put(xmlNodePtr cur)
Parse the xml file for parameters.
Walker_t & getHead()
Definition: Reptile.h:97
return_type count() const
return the count
Definition: accumulators.h:116
declare a handler of DMC branching
interval between branch, see population control
std::unique_ptr< EstimatorManagerBase > MyEstimator
std::unique_ptr< WalkerControlBase > WalkerController
WalkerController.
accumulator_set< RealType > R2Accepted
a simple accumulator for energy
int resetRun(xmlNodePtr cur)
reset the internal parameters
target total number of walkers per mpi group
Declare a global Random Number Generator.
A container class to represent a walker.
Definition: Walker.h:49
WalkerControlBase * createWalkerController(int nwtot, Communicate *comm, xmlNodePtr cur, bool reconfig)
function to create WalkerControlBase or its derived class
int ToDoSteps
number of remaning steps for a specific tasks
void finalize(MCWalkerConfiguration &w)
finalize the simulation
accumulator_set< FullPrecRealType > VarianceHist
a simple accumulator for variance
std::string debug_disable_branching_
disable branching for debugging