QMCPACK
SODMCUpdatePbyPFast.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) 2020 QMCPACK developers.
6 //
7 // File developed by: Cody A. Melton, cmelton@sandia.gov, Sandia National Laboratories
8 //
9 // File created by: Cody A. Melton, cmelton@sandia.gov, Sandia National Laboratories
10 //////////////////////////////////////////////////////////////////////////////////////
11 
12 
14 #include "Particle/HDFWalkerIO.h"
19 #if !defined(REMOVE_TRACEMANAGER)
21 #else
22 using TraceManager = int;
23 #endif
24 //#define TEST_INNERBRANCH
25 
26 
27 namespace qmcplusplus
28 {
30 
31 const TimerNameList_t<SODMCTimers> SODMCTimerNames = {{SODMC_buffer, "SODMCUpdatePbyP::Buffer"},
32  {SODMC_movePbyP, "SODMCUpdatePbyP::movePbyP"},
33  {SODMC_hamiltonian, "SODMCUpdatePbyP::Hamiltonian"},
34  {SODMC_collectables, "SODMCUpdatePbyP::Collectables"},
35  {SODMC_tmoves, "SODMCUpdatePbyP::Tmoves"}};
36 
37 
38 /// Constructor.
40  TrialWaveFunction& psi,
41  QMCHamiltonian& h,
44 
45 {}
46 
47 /// destructor
49 
51 {
52  Walker_t::WFBuffer_t& w_buffer(thisWalker.DataSet);
53  {
54  ScopedTimer local_timer(myTimers[SODMC_buffer]);
55  W.loadWalker(thisWalker, true);
56  Psi.copyFromBuffer(W, w_buffer);
57  }
58  //create a 3N-Dimensional Gaussian with variance=1
61  int nAcceptTemp(0);
62  int nRejectTemp(0);
63  //copy the old energy and scale factor of drift
64  FullPrecRealType eold(thisWalker.Properties(WP::LOCALENERGY));
65  FullPrecRealType enew(eold);
66  RealType rr_proposed = 0.0;
67  RealType rr_accepted = 0.0;
68  {
69  ScopedTimer local_timer(myTimers[SODMC_movePbyP]);
70  for (int ig = 0; ig < W.groups(); ++ig) //loop over species
71  {
72  RealType tauovermass = Tau * MassInvS[ig];
73  RealType oneover2tau = 0.5 / (tauovermass);
74  RealType sqrttau = std::sqrt(tauovermass);
75  Psi.prepareGroup(W, ig);
76  for (int iat = W.first(ig); iat < W.last(ig); ++iat)
77  {
78  //get the displacement
79  ComplexType spingrad_iat;
80  GradType grad_iat = Psi.evalGradWithSpin(W, iat, spingrad_iat);
81  PosType dr;
83  DriftModifier->getDrift(tauovermass, grad_iat, dr);
84  DriftModifier->getDrift(tauovermass / spinMass, spingrad_iat, ds);
85  dr += sqrttau * deltaR[iat];
86  ds += std::sqrt(tauovermass / spinMass) * deltaS[iat];
87  bool is_valid = W.makeMoveAndCheckWithSpin(iat, dr, ds);
88  RealType rr = tauovermass * dot(deltaR[iat], deltaR[iat]);
89  rr_proposed += rr;
90  if (!is_valid || rr > m_r2max)
91  {
92  ++nRejectTemp;
93  W.accept_rejectMove(iat, false);
94  continue;
95  }
96  ValueType ratio = Psi.calcRatioGradWithSpin(W, iat, grad_iat, spingrad_iat);
97  //node is crossed reject the move
99  {
100  ++nRejectTemp;
101  ++nNodeCrossing;
102  W.accept_rejectMove(iat, false);
103  Psi.rejectMove(iat);
104  }
105  else
106  {
107  FullPrecRealType logGf = -0.5 * dot(deltaR[iat], deltaR[iat]);
108  logGf += -0.5 * deltaS[iat] * deltaS[iat];
109  //Use the force of the particle iat
110  DriftModifier->getDrift(tauovermass, grad_iat, dr);
111  DriftModifier->getDrift(tauovermass / spinMass, spingrad_iat, ds);
112  dr = W.R[iat] - W.getActivePos() - dr;
113  ds = W.spins[iat] - W.getActiveSpinVal() - ds;
114  FullPrecRealType logGb = -oneover2tau * dot(dr, dr);
115  logGb += -spinMass * oneover2tau * ds * ds;
116  RealType prob = std::norm(ratio) * std::exp(logGb - logGf);
117  bool is_accepted = false;
118 
119  if (RandomGen() < prob)
120  {
121  is_accepted = true;
122 
123  ++nAcceptTemp;
124  Psi.acceptMove(W, iat, true);
125  rr_accepted += rr;
126  }
127  else
128  {
129  ++nRejectTemp;
130  Psi.rejectMove(iat);
131  }
132  W.accept_rejectMove(iat, is_accepted);
133  }
134  }
135  }
137  W.donePbyP();
138  }
139 
140  if (nAcceptTemp > 0)
141  {
142  //need to overwrite the walker properties
143  RealType logpsi(0);
144  {
145  ScopedTimer local_timer(myTimers[SODMC_buffer]);
146  thisWalker.Age = 0;
147  logpsi = Psi.updateBuffer(W, w_buffer, recompute);
149  checkLogAndGL(W, Psi, "checkGL_after_moves");
150  W.saveWalker(thisWalker);
151  }
152  {
153  ScopedTimer local_timer(myTimers[SODMC_hamiltonian]);
154  enew = H.evaluateWithToperator(W);
155  }
156  thisWalker.resetProperty(logpsi, Psi.getPhase(), enew, rr_accepted, rr_proposed, 1.0);
157  thisWalker.Weight *= branchEngine->branchWeight(enew, eold);
158  {
160  H.auxHevaluate(W, thisWalker);
161  H.saveProperty(thisWalker.getPropertyBase());
162  }
163  }
164  else
165  {
166  //all moves are rejected: does not happen normally with reasonable wavefunctions
167  thisWalker.Age++;
168  thisWalker.Properties(WP::R2ACCEPTED) = 0.0;
169  //weight is set to 0 for traces
170  // consistent w/ no evaluate/auxHevaluate
171  RealType wtmp = thisWalker.Weight;
172  thisWalker.Weight = 0.0;
173  H.rejectedMove(W, thisWalker);
174  thisWalker.Weight = wtmp;
175  ++nAllRejected;
176  enew = eold; //copy back old energy
177  thisWalker.Weight *= branchEngine->branchWeight(enew, eold);
178  }
179 #if !defined(REMOVE_TRACEMANAGER)
181 #endif
182  {
183  ScopedTimer local_timer(myTimers[SODMC_tmoves]);
184  const int NonLocalMoveAcceptedTemp = H.makeNonLocalMoves(W);
185  if (NonLocalMoveAcceptedTemp > 0)
186  {
187  RealType logpsi = Psi.updateBuffer(W, w_buffer, false);
188  W.saveWalker(thisWalker);
189  NonLocalMoveAccepted += NonLocalMoveAcceptedTemp;
190  }
191  }
192  nAccept += nAcceptTemp;
193  nReject += nRejectTemp;
194 
195  setMultiplicity(thisWalker);
196 }
197 
198 } // namespace qmcplusplus
TraceManager * Traces
traces
const BranchEngineType * branchEngine
branch engine, stateless reference to the one in QMCDriver
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.
const TimerNameList_t< SODMCTimers > SODMCTimerNames
helper functions for EinsplineSetBuilder
Definition: Configuration.h:43
Scalar_t getActiveSpinVal() const
Definition: ParticleSet.h:262
ParticleScalar spins
internal spin variables for dynamical spin calculations
Definition: ParticleSet.h:81
QTBase::RealType RealType
Definition: Configuration.h:58
TrialWaveFunction & Psi
trial function
std::vector< TimerIDName_t< T > > TimerNameList_t
Definition: TimerManager.h:156
RealType spinMass
spin mass
Definition: QMCUpdateBase.h:75
void acceptMove(ParticleSet &P, int iat, bool safe_to_delay=false)
update the state with the new data
ParticleSet::ParticleScalar deltaS
temporart storage for spin displacement
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)
QTBase::ComplexType ComplexType
Definition: Configuration.h:59
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
const PosType & getActivePos() const
Definition: ParticleSet.h:261
GradType evalGradWithSpin(ParticleSet &P, int iat, ComplexType &spingrad)
compute d/ds ln(psi) spin gradient at current particle position for iat electron
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.
void advanceWalker(Walker_t &thisWalker, bool recompute) override
move a walker
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
bool makeMoveAndCheckWithSpin(Index_t iat, const SingleParticlePos &displ, const Scalar_t &sdispl)
makeMoveAndCheck, but now includes an update to the spin variable
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
SODMCUpdatePbyPWithRejectionFast(MCWalkerConfiguration &w, TrialWaveFunction &psi, QMCHamiltonian &h, RandomBase< FullPrecRealType > &rg)
Constructor.
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
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)
ValueType calcRatioGradWithSpin(ParticleSet &P, int iat, GradType &grad_iat, ComplexType &spingrad_iat)
compute psi(R_new) / psi(R_current) ratio and d/ds ln(psi(R_new)) spin gradient It returns a complex ...
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
A container class to represent a walker.
Definition: Walker.h:49
QMCHamiltonian & H
Hamiltonian.
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 makeGaussRandomWithEngine(ParticleAttrib< TinyVector< T, D >> &a, RG &rng)