QMCPACK
SFNBranch.h
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) 2020 QMCPACK developers.
6 //
7 // File developed by: Peter Doak, doakpw@ornl.gov, Oak Ridge National Laboratory
8 //
9 // Refactored from: SimpleFixedNodeBranch.cpp
10 //////////////////////////////////////////////////////////////////////////////////////
11 
12 
13 #ifndef QMCPLUSPLUS_SIMPLE_FIXEDNODE_BRANCH_H
14 #define QMCPLUSPLUS_SIMPLE_FIXEDNODE_BRANCH_H
15 
16 #include <array>
17 #include <Configuration.h>
18 #include "OhmmsData/ParameterSet.h"
22 #include "Particle/Walker.h"
23 #include "QMCDrivers/Crowd.h"
24 #include "DMC/DMCRefEnergy.h"
25 #include <bitset>
26 
27 namespace qmcplusplus
28 {
29 /** Manages the state of QMC sections and handles population control for DMCs
30  *
31  * \todo: Remove duplicate reading of Driver XML section with own copies of input
32  * parameters.
33  * \todo: Rename, it is the only branching class so its name is too much
34  * \todo: Use normal types for data members, don't be clever,
35  * the parameter enums violate KISS and make debugging annoying
36  * \todo: Remove as much state as possible.
37  *
38  * QMCDriver object owns a SFNBranch to keep track of the
39  * progress of a qmc section. It implements several methods to control the
40  * population and trial energy during a DMC and evaluate the properties of
41  * a population, e.g., energy, variance, population etc.
42  * \see {http://qmcpack.cmscc.org/qmc-basics}
43  *
44  * Steps in SFNB states machine
45  * 1. Construction (gets global walker number (rank or section wide?)
46  * 3. put(reads driver XML node yet again)
47  * 4. InitParam
48  * a. If TargetWalkers isn't known
49  * aa. allreduce and updates MCMW globalWalkers.
50  * bb. sets target walkers to whatever current total active walkers is.
51  * b. If not a restart
52  * aa. saves fixW and killWalker to internal params, otherwise just discards.
53  * bb. updates SFNB copy of MAX/MINWALKRS from walker controller,
54  * these were set in constructer but I guess thats ony if this is a restart
55  * 7. updateParam after each walker control branch (outside SFNBranch)
56  * a. Not first iter during warmup then call WalkerController branch.
57  * else call WC doNotBranch, returns pop_now
58  * b. copy a bunch a state from WC to SFNB (should be with respect to pop_now - number of released nodes)
59  * c. If using taueff update that based on acceptance ration and current tau.
60  * d. If not warmup calculate ETRIAL based on EREF and feedback * log(TargetWalkers) - log(pop_now)
61  * e. set WC's TrialEnergy
62  */
63 class SFNBranch : public QMCTraits
64 {
65 public:
67 
68  /*! enum for booleans
69  * \since 2008-05-05
70  */
71  enum
72  {
73  B_DMC = 0 /**< 1 for dmc, 0 for anything else */
74  ,
75  B_DMCSTAGE = 1 /**< 1 for main, 0 for wamrup */
76  ,
77  B_POPCONTROL = 2 /**< 1 for the standard dmc, 0 for the comb method */
78  ,
79  B_USETAUEFF = 3 /**< 1 to use taueff accordning to JCP 93, 0 to use tau */
80  ,
81  B_CLEARHISTORY = 4 /**< 1 to clear the history */
82  ,
83  B_KILLNODES = 5 /**< 1 to kill walkers when a node crossing is detected */
84  ,
85  B_RESTART = 6 /**< 1 if restarting */
86  ,
87  B_RMC = 7 /**< 1 for rmc, 0 for anything else */
88  ,
89  B_RMCSTAGE = 8 /**< 1 for main, 0 for warmup */
90  ,
91  B_MODE_MAX = 10 /**< size of BranchMode */
92  };
93 
94  using BranchModeType = std::bitset<B_MODE_MAX>;
95 
96  /*! enum for iParam std::bitset<B_IPARAM_MAX>
97  * When introducing a new iParam, check if B_IPARAM_MAX is sufficiently large. Use multiples of 8
98  * Why? Much easier to use bool flags. Are these ever serialized?
99  */
100  enum
101  {
102  B_WARMUPSTEPS = 0, /**< warmup steps, valid when BranchMode[D_DMCSTAGE] == 0 */
103  B_ENERGYUPDATEINTERVAL, /**< frequency of the trial energy updates, default 1 */
104  B_COUNTER, /**< counter for tracking object state */
105  B_TARGETWALKERS, /**< target total number of walkers per mpi group */
106  B_BRANCHINTERVAL, /**< interval between branch, see population control */
107  B_IPARAM_MAX /**< size of iParam */
108  };
109 
110  /** input parameters of integer types
111  * \since 2008-05-05
112  */
114 
115  /** enum for vParam
116  *
117  * Easy serialization is a relatively minor concern compared to the
118  * annoyance this causes elsewhere.
119  */
121  {
122  TAU = 0,
123  TAUEFF, // effective time step
124  ETRIAL, // Trial energy
125  EREF, // Estimate of the ground state energy based on the single step population average during warmup
126  // or the historical average of population average post warmup.
127  // Center of the branching cutoff energy window.
128  ENOW, // weighted average energy of the population in the current step
129  BRANCHMAX,
130  BRANCHCUTOFF,
131  BRANCHFILTER,
132  SIGMA2,
133  SIGMA_BOUND,
134  FEEDBACK,
135  FILTERSCALE,
136  VPARAM_MAX = 17 // four extra, why? Sloppy or undocumented hack?
137  };
139 
140  /** controlling parameters of full precision real type
141  *
142  * Mostly internal
143  */
144  template<typename PAR_ENUM>
145  struct VParams : public std::array<FullPrecRealType, static_cast<size_t>(PAR_ENUM::VPARAM_MAX)>
146  {
147  using Base = std::array<FullPrecRealType, static_cast<size_t>(PAR_ENUM::VPARAM_MAX)>;
148  FullPrecRealType& operator[](PAR_ENUM sbvp) { return Base::operator[](static_cast<size_t>(sbvp)); }
149  const FullPrecRealType& operator[](PAR_ENUM sbvp) const { return Base::operator[](static_cast<size_t>(sbvp)); }
150  };
152 
153  ///Constructor
155 
156  ///copy constructor
157  SFNBranch(const SFNBranch& abranch) = delete;
158 
159  ~SFNBranch();
160 
161  inline bool phaseChanged(RealType psi0) const
162  {
163 // TODO: remove ifdef
164 #if defined(QMC_COMPLEX)
165  return false;
166 #else
167  return std::cos(psi0) < std::numeric_limits<RealType>::epsilon();
168 #endif
169  }
170 
171  /** increment QMCCounter
172  *
173  * QMCCounter is the number of times any QMC section is processed.
174  */
175  inline void advanceQMCCounter() { iParam[B_COUNTER]++; }
176  inline void regressQMCCounter() { iParam[B_COUNTER]--; }
177 
178  /** initialize branching parameters
179  * @param pop Population of Walkers
180  * @param fixW true, if reconfiguration with the fixed number of walkers is used
181  * @param killwalker
182  * @return number of copies to make in case targetwalkers changed
183  */
184  int initParam(const MCPopulation& population, FullPrecRealType ene, FullPrecRealType var, bool fixW, bool killwalker);
185 
186  /** return the bare branch weight
187  *
188  * This is equivalent to calling branchWeight(enew,eold,1.0,1.0)
189  */
190  inline RealType branchWeightBare(RealType enew, RealType eold) const
191  {
192  return std::exp(vParam[SBVP::TAUEFF] * (vParam[SBVP::ETRIAL] - 0.5 * (enew + eold)));
193  }
194 
195  /** return the bare branch weight with a filtering using an energy window
196  *
197  * Cutoff values are set by the variance
198  */
200  {
201  FullPrecRealType taueff_ = vParam[SBVP::TAUEFF] * 0.5;
202  FullPrecRealType x = std::max(vParam[SBVP::EREF] - enew, vParam[SBVP::EREF] - eold);
203  if (x > vParam[SBVP::BRANCHMAX])
204  taueff_ = 0.0;
205  else if (x > vParam[SBVP::BRANCHCUTOFF])
206  taueff_ *= (1.0 - (x - vParam[SBVP::BRANCHCUTOFF]) * vParam[SBVP::BRANCHFILTER]);
207  return std::exp(taueff_ * (vParam[SBVP::ETRIAL] * 2.0 - enew - eold));
208  }
209 
210  inline RealType symLinkAction(RealType logGf, RealType logGb, RealType enew, RealType eold) const
211  {
212  RealType driftaction = -0.5 * (logGf + logGb);
213  //RealType energyaction =
214  RealType taueff_ = vParam[SBVP::TAUEFF] * 0.5;
215  RealType x = std::max(vParam[SBVP::EREF] - enew, vParam[SBVP::EREF] - eold);
216  if (x > vParam[SBVP::BRANCHMAX])
217  taueff_ = 0.0;
218  else if (x > vParam[SBVP::BRANCHCUTOFF])
219  taueff_ *= (1.0 - (x - vParam[SBVP::BRANCHCUTOFF]) * vParam[SBVP::BRANCHFILTER]);
220  RealType energyaction = taueff_ * (enew + eold);
221  return driftaction + energyaction;
222  }
223 
224  inline RealType symLinkActionBare(RealType logGf, RealType logGb, RealType enew, RealType eold) const
225  {
226  RealType driftaction = -0.5 * (logGf + logGb);
227  RealType taueff_ = vParam[SBVP::TAUEFF] * 0.5;
228  RealType energyaction = taueff_ * (enew + eold);
229  // RealType wavefunctionaction= -psinew + psiold;
230  return driftaction + energyaction;
231  }
232 
233  inline RealType DMCLinkAction(RealType enew, RealType eold) const
234  {
235  RealType taueff_ = vParam[SBVP::TAUEFF] * 0.5;
236  RealType x = std::max(vParam[SBVP::EREF] - enew, vParam[SBVP::EREF] - eold);
237  if (x > vParam[SBVP::BRANCHMAX])
238  taueff_ = 0.0;
239  else if (x > vParam[SBVP::BRANCHCUTOFF])
240  taueff_ *= (1.0 - (x - vParam[SBVP::BRANCHCUTOFF]) * vParam[SBVP::BRANCHFILTER]);
241  return taueff_ * (enew + eold);
242  }
243  /** return the branch weight according to JCP1993 Umrigar et al. Appendix A p=1, q=0
244  * @param enew new energy
245  * @param eold old energy
246  * @param scnew \f$ V_{sc}(R_{new})/V(R_{new}) \f$
247  * @param scold \f$ V_{sc}(R_{old})/V(R_{old}) \f$
248  */
249  inline RealType branchWeight(RealType enew, RealType eold, RealType scnew, RealType scold) const
250  {
251  FullPrecRealType s1 = (vParam[SBVP::ETRIAL] - vParam[SBVP::EREF]) + (vParam[SBVP::EREF] - enew) * scnew;
252  FullPrecRealType s0 = (vParam[SBVP::ETRIAL] - vParam[SBVP::EREF]) + (vParam[SBVP::EREF] - eold) * scold;
253  return std::exp(vParam[SBVP::TAUEFF] * 0.5 * (s1 + s0));
254  }
255 
256  /** return the branch weight according to JCP1993 Umrigar et al. Appendix A
257  * @param enew new energy
258  * @param eold old energy
259  * @param scnew \f$ V_{sc}(R_{new})/V(R_{new}) \f$
260  * @param scold \f$ V_{sc}(R_{old})/V(R_{old}) \f$
261  * @param p acceptance ratio
262  */
263  inline RealType branchWeight(RealType enew, RealType eold, RealType scnew, RealType scold, RealType p) const
264  {
265  FullPrecRealType s1 = (vParam[SBVP::ETRIAL] - vParam[SBVP::EREF]) + (vParam[SBVP::EREF] - enew) * scnew;
266  FullPrecRealType s0 = (vParam[SBVP::ETRIAL] - vParam[SBVP::EREF]) + (vParam[SBVP::EREF] - eold) * scold;
267  return std::exp(vParam[SBVP::TAUEFF] * (p * 0.5 * (s1 - s0) + s0));
268  //return std::exp(TauEff*(p*0.5*(sp-sq)+sq));
269  }
270 
271  inline RealType getEref() const { return vParam[SBVP::EREF]; }
272  inline RealType getEtrial() const { return vParam[SBVP::ETRIAL]; }
273  inline RealType getTau() const { return vParam[SBVP::TAU]; }
274  inline RealType getTauEff() const { return vParam[SBVP::TAUEFF]; }
275 
276  int getWarmupToDoSteps() const { return WarmUpToDoSteps; }
277  /** perform branching
278  * @param iter current step
279  * @param w Walker configuration
280  */
281  void updateParamAfterPopControl(const MCDataType<FullPrecRealType>& wc_ensemble_prop, int Nelec);
282 
283  bool put(xmlNodePtr cur);
284 
285  /** create map between the parameter name and variables */
286  void registerParameters();
287 
288  ///finalize the simulation
289  void printStatus() const;
290 
291  friend std::ostream& operator<<(std::ostream& os, SFNBranch::VParamType& rhs);
292 
293 private:
294  ///set branch cutoff, max, filter
295  void setBranchCutoff(FullPrecRealType variance,
296  FullPrecRealType targetSigma,
297  FullPrecRealType maxSigma,
298  int Nelec = 0);
299 
301 
303 
305  /// number of remaning steps in warmup, [0, iParam[B_WARMUPSTEPS]]
307  /// number of remaning steps in before adjusting ETRIAL, [0, iParam[B_ENERGYUPDATEINTERVAL]]
309  ///save xml element
310  xmlNodePtr myNode;
311  /// collect energy and variance history
313  ///a simple accumulator for energy
315  ///a simple accumulator for energy
317  ///a simple accumulator for reptation's center slice
319  /////histogram of populations
320  //BlockHistogram<RealType> DMCEnergyHist;
321  ///scheme of branching cutoff
323  ///set of parameters
325  ///string parameters
326  std::vector<std::string> sParam;
327 };
328 
329 std::ostream& operator<<(std::ostream& os, SFNBranch::VParamType& rhs);
330 
331 } // namespace qmcplusplus
332 #endif
int initParam(const MCPopulation &population, FullPrecRealType ene, FullPrecRealType var, bool fixW, bool killwalker)
initialize branching parameters
Definition: SFNBranch.cpp:94
RealType getTau() const
Definition: SFNBranch.h:273
HamiltonianRef::FullPrecRealType FullPrecRealType
helper functions for EinsplineSetBuilder
Definition: Configuration.h:43
Manages the state of QMC sections and handles population control for DMCs.
Definition: SFNBranch.h:63
1 for main, 0 for warmup
Definition: SFNBranch.h:89
FullPrecRealType & operator[](PAR_ENUM sbvp)
Definition: SFNBranch.h:148
RealType branchWeight(RealType enew, RealType eold, RealType scnew, RealType scold, RealType p) const
return the branch weight according to JCP1993 Umrigar et al.
Definition: SFNBranch.h:263
void printStatus() const
finalize the simulation
Definition: SFNBranch.cpp:231
void updateParamAfterPopControl(const MCDataType< FullPrecRealType > &wc_ensemble_prop, int Nelec)
perform branching
Definition: SFNBranch.cpp:133
RealType getTauEff() const
Definition: SFNBranch.h:274
std::bitset< B_MODE_MAX > BranchModeType
Definition: SFNBranch.h:94
1 for main, 0 for wamrup
Definition: SFNBranch.h:75
Handle updating Eref used for calculating the trial energy.
Definition: DMCRefEnergy.h:25
int WarmUpToDoSteps
number of remaning steps in warmup, [0, iParam[B_WARMUPSTEPS]]
Definition: SFNBranch.h:306
int getWarmupToDoSteps() const
Definition: SFNBranch.h:276
BranchModeType BranchMode
Definition: SFNBranch.h:300
RealType branchWeightBare(RealType enew, RealType eold) const
return the bare branch weight
Definition: SFNBranch.h:190
MakeReturn< UnaryNode< FnCos, typename CreateLeaf< Vector< T1, C1 > >::Leaf_t > >::Expression_t cos(const Vector< T1, C1 > &l)
DMCRefEnergy ref_energy_collector
collect energy and variance history
Definition: SFNBranch.h:312
class to handle a set of parameters
Definition: ParameterSet.h:27
RealType getEref() const
Definition: SFNBranch.h:271
SimpleBranchVectorParameter
enum for vParam
Definition: SFNBranch.h:120
bool put(xmlNodePtr cur)
Parse the xml file for parameters.
Definition: SFNBranch.cpp:277
RealType DMCLinkAction(RealType enew, RealType eold) const
Definition: SFNBranch.h:233
RealType symLinkActionBare(RealType logGf, RealType logGb, RealType enew, RealType eold) const
Definition: SFNBranch.h:224
void advanceQMCCounter()
increment QMCCounter
Definition: SFNBranch.h:175
std::ostream & operator<<(std::ostream &out, const AntiSymTensor< T, D > &rhs)
controlling parameters of full precision real type
Definition: SFNBranch.h:145
xmlNodePtr myNode
save xml element
Definition: SFNBranch.h:310
RealType symLinkAction(RealType logGf, RealType logGb, RealType enew, RealType eold) const
Definition: SFNBranch.h:210
MakeReturn< UnaryNode< FnExp, typename CreateLeaf< Vector< T1, C1 > >::Leaf_t > >::Expression_t exp(const Vector< T1, C1 > &l)
accumulator_set< RealType > R2Accepted
a simple accumulator for energy
Definition: SFNBranch.h:314
Define a accumulator whose average is evaluated for a moving block of a fixed steps.
SFNBranch(RealType tau, RealType feedback, DMCRefEnergyScheme)
Constructor.
Definition: SFNBranch.cpp:34
1 to clear the history
Definition: SFNBranch.h:81
friend std::ostream & operator<<(std::ostream &os, SFNBranch::VParamType &rhs)
Definition: SFNBranch.cpp:322
void setBranchCutoff(FullPrecRealType variance, FullPrecRealType targetSigma, FullPrecRealType maxSigma, int Nelec=0)
set branch cutoff, max, filter
Definition: SFNBranch.cpp:287
1 for dmc, 0 for anything else
Definition: SFNBranch.h:73
ParameterSet m_param
set of parameters
Definition: SFNBranch.h:324
interval between branch, see population control
Definition: SFNBranch.h:106
std::array< FullPrecRealType, static_cast< size_t >(SBVP ::VPARAM_MAX)> Base
Definition: SFNBranch.h:147
DMCRefEnergyScheme
DMCRefEnergy schemes.
int EtrialUpdateToDoSteps
number of remaning steps in before adjusting ETRIAL, [0, iParam[B_ENERGYUPDATEINTERVAL]] ...
Definition: SFNBranch.h:308
1 for rmc, 0 for anything else
Definition: SFNBranch.h:87
RealType branchWeight(FullPrecRealType enew, FullPrecRealType eold) const
return the bare branch weight with a filtering using an energy window
Definition: SFNBranch.h:199
counter for tracking object state
Definition: SFNBranch.h:104
accumulator_set< RealType > R2Center
a simple accumulator for reptation&#39;s center slice
Definition: SFNBranch.h:318
QTFull::RealType FullPrecRealType
Definition: Configuration.h:66
1 to use taueff accordning to JCP 93, 0 to use tau
Definition: SFNBranch.h:79
traits for QMC variables
Definition: Configuration.h:49
warmup steps, valid when BranchMode[D_DMCSTAGE] == 0
Definition: SFNBranch.h:102
target total number of walkers per mpi group
Definition: SFNBranch.h:105
std::string branching_cutoff_scheme
scheme of branching cutoff
Definition: SFNBranch.h:322
void registerParameters()
create map between the parameter name and variables
Definition: SFNBranch.cpp:68
A container class to represent a walker.
Definition: Walker.h:49
bool phaseChanged(RealType psi0) const
Definition: SFNBranch.h:161
RealType getEtrial() const
Definition: SFNBranch.h:272
frequency of the trial energy updates, default 1
Definition: SFNBranch.h:103
accumulator_set< RealType > R2Proposed
a simple accumulator for energy
Definition: SFNBranch.h:316
RealType branchWeight(RealType enew, RealType eold, RealType scnew, RealType scold) const
return the branch weight according to JCP1993 Umrigar et al.
Definition: SFNBranch.h:249
Define and declare accumulator_set.
1 to kill walkers when a node crossing is detected
Definition: SFNBranch.h:83
1 for the standard dmc, 0 for the comb method
Definition: SFNBranch.h:77
const FullPrecRealType & operator[](PAR_ENUM sbvp) const
Definition: SFNBranch.h:149
std::vector< std::string > sParam
string parameters
Definition: SFNBranch.h:326