QMCPACK
DMCUpdatePbyPFast.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) 2016 Jeongnim Kim and QMCPACK developers.
6 //
7 // File developed by: Jeremy McMinnis, jmcminis@gmail.com, University of Illinois at Urbana-Champaign
8 // Jeongnim Kim, jeongnim.kim@gmail.com, University of Illinois at Urbana-Champaign
9 // Jaron T. Krogel, krogeljt@ornl.gov, Oak Ridge National Laboratory
10 // Mark A. Berrill, berrillma@ornl.gov, Oak Ridge National Laboratory
11 //
12 // File created by: Jeremy McMinnis, jmcminis@gmail.com, University of Illinois at Urbana-Champaign
13 //////////////////////////////////////////////////////////////////////////////////////
14 
15 
18 #include "Particle/HDFWalkerIO.h"
22 #if !defined(REMOVE_TRACEMANAGER)
24 #else
25 using TraceManager = int;
26 #endif
27 #include "WalkerLogCollector.h"
28 //#define TEST_INNERBRANCH
29 
30 
31 namespace qmcplusplus
32 {
34 
35 TimerNameList_t<DMCTimers> DMCTimerNames = {{DMC_buffer, "DMCUpdatePbyP::Buffer"},
36  {DMC_movePbyP, "DMCUpdatePbyP::movePbyP"},
37  {DMC_hamiltonian, "DMCUpdatePbyP::Hamiltonian"},
38  {DMC_collectables, "DMCUpdatePbyP::Collectables"},
39  {DMC_tmoves, "DMCUpdatePbyP::Tmoves"}};
40 
41 
42 /// Constructor.
44  TrialWaveFunction& psi,
45  QMCHamiltonian& h,
48 {}
49 
50 /// destructor
52 
54 {
55  Walker_t::WFBuffer_t& w_buffer(thisWalker.DataSet);
56  {
57  ScopedTimer local_timer(myTimers[DMC_buffer]);
58  W.loadWalker(thisWalker, true);
59  Psi.copyFromBuffer(W, w_buffer);
60  }
61  //create a 3N-Dimensional Gaussian with variance=1
63  int nAcceptTemp(0);
64  int nRejectTemp(0);
65  //copy the old energy and scale factor of drift
66  FullPrecRealType eold(thisWalker.Properties(WP::LOCALENERGY));
67  FullPrecRealType enew(eold);
68  RealType rr_proposed = 0.0;
69  RealType rr_accepted = 0.0;
70  {
71  ScopedTimer local_timer(myTimers[DMC_movePbyP]);
72  for (int ig = 0; ig < W.groups(); ++ig) //loop over species
73  {
74  RealType tauovermass = Tau * MassInvS[ig];
75  RealType oneover2tau = 0.5 / (tauovermass);
76  RealType sqrttau = std::sqrt(tauovermass);
77  Psi.prepareGroup(W, ig);
78  for (int iat = W.first(ig); iat < W.last(ig); ++iat)
79  {
80  //get the displacement
81  GradType grad_iat = Psi.evalGrad(W, iat);
82  PosType dr;
83  DriftModifier->getDrift(tauovermass, grad_iat, dr);
84  dr += sqrttau * deltaR[iat];
85  bool is_valid = W.makeMoveAndCheck(iat, dr);
86  RealType rr = tauovermass * dot(deltaR[iat], deltaR[iat]);
87  rr_proposed += rr;
88  if (!is_valid || rr > m_r2max)
89  {
90  ++nRejectTemp;
91  W.accept_rejectMove(iat, false);
92  continue;
93  }
94  ValueType ratio = Psi.calcRatioGrad(W, iat, grad_iat);
95  //node is crossed reject the move
97  {
98  ++nRejectTemp;
99  ++nNodeCrossing;
100  W.accept_rejectMove(iat, false);
101  Psi.rejectMove(iat);
102  }
103  else
104  {
105  FullPrecRealType logGf = -0.5 * dot(deltaR[iat], deltaR[iat]);
106  //Use the force of the particle iat
107  DriftModifier->getDrift(tauovermass, grad_iat, dr);
108  dr = W.R[iat] - W.getActivePos() - dr;
109  FullPrecRealType logGb = -oneover2tau * dot(dr, dr);
110  RealType prob = std::norm(ratio) * std::exp(logGb - logGf);
111  bool is_accepted = false;
112  if (RandomGen() < prob)
113  {
114  is_accepted = true;
115  ++nAcceptTemp;
116  Psi.acceptMove(W, iat, true);
117  rr_accepted += rr;
118  }
119  else
120  {
121  ++nRejectTemp;
122  Psi.rejectMove(iat);
123  }
124  W.accept_rejectMove(iat, is_accepted);
125  }
126  }
127  }
129  W.donePbyP();
130  }
131 
132  if (nAcceptTemp > 0)
133  {
134  //need to overwrite the walker properties
135  RealType logpsi(0);
136  {
137  ScopedTimer local_timer(myTimers[DMC_buffer]);
138  thisWalker.Age = 0;
139  logpsi = Psi.updateBuffer(W, w_buffer, recompute);
141  checkLogAndGL(W, Psi, "checkGL_after_moves");
142  W.saveWalker(thisWalker);
143  }
144  {
145  ScopedTimer local_timer(myTimers[DMC_hamiltonian]);
146  enew = H.evaluateWithToperator(W);
147  }
148  thisWalker.resetProperty(logpsi, Psi.getPhase(), enew, rr_accepted, rr_proposed, 1.0);
149  thisWalker.Weight *= branchEngine->branchWeight(enew, eold);
150  {
151  ScopedTimer local_timer(myTimers[DMC_collectables]);
152  H.auxHevaluate(W, thisWalker);
153  H.saveProperty(thisWalker.getPropertyBase());
154  }
155  }
156  else
157  {
158  //all moves are rejected: does not happen normally with reasonable wavefunctions
159  thisWalker.Age++;
160  thisWalker.Properties(WP::R2ACCEPTED) = 0.0;
161  //weight is set to 0 for traces
162  // consistent w/ no evaluate/auxHevaluate
163  RealType wtmp = thisWalker.Weight;
164  thisWalker.Weight = 0.0;
165  H.rejectedMove(W, thisWalker);
166  thisWalker.Weight = wtmp;
167  ++nAllRejected;
168  enew = eold; //copy back old energy
169  thisWalker.Weight *= branchEngine->branchWeight(enew, eold);
170  }
171 #if !defined(REMOVE_TRACEMANAGER)
173 #endif
174  if(wlog_collector)
175  wlog_collector->collect(thisWalker,W,Psi,H);
176  {
177  ScopedTimer local_timer(myTimers[DMC_tmoves]);
178  const int NonLocalMoveAcceptedTemp = H.makeNonLocalMoves(W);
179  if (NonLocalMoveAcceptedTemp > 0)
180  {
181  RealType logpsi = Psi.updateBuffer(W, w_buffer, false);
182  // debugging lines
183  //W.update(true);
184  //RealType logpsi2 = Psi.evaluateLog(W);
185  //if(logpsi!=logpsi2) std::cout << " logpsi " << logpsi << " logps2i " << logpsi2 << " diff " << logpsi2-logpsi << std::endl;
186  W.saveWalker(thisWalker);
187  NonLocalMoveAccepted += NonLocalMoveAcceptedTemp;
188  }
189  }
190  nAccept += nAcceptTemp;
191  nReject += nRejectTemp;
192 
193  setMultiplicity(thisWalker);
194 }
195 
196 } // namespace qmcplusplus
TraceManager * Traces
traces
const BranchEngineType * branchEngine
branch engine, stateless reference to the one in QMCDriver
WalkerLogCollector * wlog_collector
Fixed-size array.
Definition: OhmmsTinyMeta.h:30
PropertyContainer_t Properties
scalar properties of a walker
Definition: Walker.h:125
Base class for update methods for each step.
Definition: QMCUpdateBase.h:41
void rejectMove(int iat)
restore to the original state
WFBuffer_t DataSet
buffer for the data for particle-by-particle update
Definition: Walker.h:135
A set of walkers that are to be advanced by Metropolis Monte Carlo.
void copyFromBuffer(ParticleSet &P, WFBufferType &buf)
copy all the wavefunction components from buffer.
helper functions for EinsplineSetBuilder
Definition: Configuration.h:43
QTBase::RealType RealType
Definition: Configuration.h:58
TrialWaveFunction & Psi
trial function
std::vector< TimerIDName_t< T > > TimerNameList_t
Definition: TimerManager.h:156
void acceptMove(ParticleSet &P, int iat, bool safe_to_delay=false)
update the state with the new data
void saveProperty(IT first)
save the values of Hamiltonian elements to the Properties
bool phaseChanged(RealType psi0) const
Collection of Local Energy Operators.
void buffer_sample(int current_step)
int first(int igroup) const
return the first index of a group i
Definition: ParticleSet.h:514
int current_step
current MC step
Definition: ParticleSet.h:134
ParticleSet::ParticlePos deltaR
temporary storage for random displacement
RealType updateBuffer(ParticleSet &P, WFBufferType &buf, bool fromscratch=false)
update all the wavefunction components in buffer.
int Age
Age of this walker age is incremented when a walker is not moved after a sweep.
Definition: Walker.h:100
DriverDebugChecks debug_checks_
determine additional checks for debugging purpose
Definition: QMCUpdateBase.h:58
IndexType nNodeCrossing
Total number of node crossings per block.
Definition: QMCUpdateBase.h:69
int groups() const
return the number of groups
Definition: ParticleSet.h:511
TimerNameList_t< DMCTimers > DMCTimerNames
const PosType & getActivePos() const
Definition: ParticleSet.h:261
DMCUpdatePbyPWithRejectionFast(MCWalkerConfiguration &w, TrialWaveFunction &psi, QMCHamiltonian &h, RandomBase< FullPrecRealType > &rg)
Constructor.
double norm(const zVec &c)
Definition: VectorOps.h:118
WalkerProperties::Indexes WP
Definition: ParticleSet.cpp:34
FullPrecRealType evaluateWithToperator(ParticleSet &P)
evaluate Local energy with Toperators updated.
QTBase::ValueType ValueType
Definition: Configuration.h:60
RealType branchWeight(FullPrecRealType enew, FullPrecRealType eold) const
return the bare branch weight with a filtering using an energy window
void saveWalker(Walker_t &awalker)
save this to awalker
RealType Tau
timestep
Definition: QMCUpdateBase.h:73
void accept_rejectMove(Index_t iat, bool accepted, bool forward_mode=true)
accept or reject a proposed move Two operation modes: The using and updating distance tables via Part...
ParticlePos R
Position.
Definition: ParticleSet.h:79
int last(int igroup) const
return the last index of a group i
Definition: ParticleSet.h:517
MakeReturn< UnaryNode< FnExp, typename CreateLeaf< Vector< T1, C1 > >::Leaf_t > >::Expression_t exp(const Vector< T1, C1 > &l)
MCWalkerConfiguration & W
walker ensemble
static void checkLogAndGL(ParticleSet &pset, TrialWaveFunction &twf, const std::string_view location)
check logpsi and grad and lap against values computed from scratch
void loadWalker(Walker_t &awalker, bool pbyp)
load a Walker_t to the current ParticleSet
void collect(const MCPWalker &walker, const ParticleSet &pset, const TrialWaveFunction &wfn, const QMCHamiltonian &ham, int step=-1)
collect all data for one walker into the data buffers
void auxHevaluate(ParticleSet &P)
MakeReturn< UnaryNode< FnSqrt, typename CreateLeaf< Vector< T1, C1 > >::Leaf_t > >::Expression_t sqrt(const Vector< T1, C1 > &l)
void rejectedMove(ParticleSet &P, Walker_t &ThisWalker)
Looks like a hack see DMCBatched.cpp and DMC.cpp weight is used like temporary flag from DMC...
Class to represent a many-body trial wave function.
RandomBase< FullPrecRealType > & RandomGen
random number generator
GradType evalGrad(ParticleSet &P, int iat)
Indexes
an enum denoting index of physical properties
FullPrecRealType Weight
Weight of the walker.
Definition: Walker.h:102
int makeNonLocalMoves(ParticleSet &P)
make non local moves
Tensor< typename BinaryReturn< T1, T2, OpMultiply >::Type_t, D > dot(const AntiSymTensor< T1, D > &lhs, const AntiSymTensor< T2, D > &rhs)
bool makeMoveAndCheck(Index_t iat, const SingleParticlePos &displ)
move the iat-th particle to active_pos_
QTFull::RealType FullPrecRealType
Definition: Configuration.h:66
TimerManager< NewTimer > & getGlobalTimerManager()
IndexType nAccept
counter for number of moves accepted
Definition: QMCUpdateBase.h:63
IndexType nReject
counter for number of moves rejected
Definition: QMCUpdateBase.h:65
FullPrecRealType * getPropertyBase()
Definition: Walker.h:277
virtual void getDrift(RealType tau, const GradType &qf, PosType &drift) const =0
evaluate a drift with a real force
void prepareGroup(ParticleSet &P, int ig)
Prepare internal data for updating WFC correspond to a particle group Particle groups usually corresp...
std::vector< RealType > MassInvS
1/Mass per species
void setMultiplicity(WalkerIter_t it, WalkerIter_t it_end)
set the multiplicity of the walkers to branch
Declaration of a MCWalkerConfiguration.
A container class to represent a walker.
Definition: Walker.h:49
QMCHamiltonian & H
Hamiltonian.
ValueType calcRatioGrad(ParticleSet &P, int iat, GradType &grad_iat)
compute psi(R_new) / psi(R_current) ratio and ln(psi(R_new)) gradients It returns a complex value if...
RealType m_r2max
maximum displacement^2
void donePbyP(bool skipSK=false)
update structure factor and unmark active_ptcl_
void resetProperty(FullPrecRealType logpsi, FullPrecRealType sigN, FullPrecRealType ene)
reset the property of a walker
Definition: Walker.h:296
void completeUpdates()
complete all the delayed or asynchronous operations before leaving the p-by-p move region...
IndexType NonLocalMoveAccepted
Total numer of non-local moves accepted.
Definition: QMCUpdateBase.h:71
IndexType nAllRejected
Total number of the steps when all the particle moves are rejected.
Definition: QMCUpdateBase.h:67
const DriftModifierBase * DriftModifier
drift modifer, stateless reference to the one in QMCDriver
void advanceWalker(Walker_t &thisWalker, bool recompute) override
move a walker
void makeGaussRandomWithEngine(ParticleAttrib< TinyVector< T, D >> &a, RG &rng)