QMCPACK
DMCUpdatePbyPL2.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: Jaron T. Krogel, krogeljt@ornl.gov, Oak Ridge National Laboratory
13 //////////////////////////////////////////////////////////////////////////////////////
14 
15 
18 // #include "Particle/DistanceTable.h"
19 #include "Particle/HDFWalkerIO.h"
23 #if !defined(REMOVE_TRACEMANAGER)
25 #else
26 using TraceManager = int;
27 #endif
28 //#define TEST_INNERBRANCH
30 
31 namespace qmcplusplus
32 {
34 
35 /// Constructor.
37  TrialWaveFunction& psi,
38  QMCHamiltonian& h,
41 {}
42 
43 /// destructor
45 
46 void DMCUpdatePbyPL2::advanceWalker(Walker_t& thisWalker, bool recompute)
47 {
48  Walker_t::WFBuffer_t& w_buffer(thisWalker.DataSet);
49  {
50  ScopedTimer local_timer(myTimers[DMC_buffer]);
51  W.loadWalker(thisWalker, true);
52  Psi.copyFromBuffer(W, w_buffer);
53  }
54  //create a 3N-Dimensional Gaussian with variance=1
56  int nAcceptTemp(0);
57  int nRejectTemp(0);
58  //copy the old energy and scale factor of drift
59  //EstimatorRealType eold(thisWalker.Properties(LOCALENERGY));
60  //EstimatorRealType enew(eold);
61  FullPrecRealType eold(thisWalker.Properties(WP::LOCALENERGY));
62  FullPrecRealType enew(eold);
63  RealType rr_proposed = 0.0;
64  RealType rr_accepted = 0.0;
65  mPosType K;
66  mTensorType D;
67  mTensorType Dchol;
68  PosType Ktmp, drtmp;
69  TensorType Dtmp;
70  bool L2_proj = H.has_L2();
71  if (L2_proj)
72  {
73  Ktmp = 0.0;
74  Dtmp = 0.0;
75  for (int d = 0; d < DIM; d++)
76  Dtmp(d, d) = 1.0;
77  }
78  {
79  ScopedTimer local_timer(myTimers[DMC_movePbyP]);
80  for (int ig = 0; ig < W.groups(); ++ig) //loop over species
81  {
82  RealType tauovermass = Tau * MassInvS[ig];
83  RealType oneover2tau = 0.5 / (tauovermass);
84  RealType sqrttau = std::sqrt(tauovermass);
85  RealType rr;
86  for (int iat = W.first(ig); iat < W.last(ig); ++iat)
87  {
88  //W.setActive(iat);
89  //get the displacement
90  GradType grad_iat = Psi.evalGrad(W, iat);
91  mPosType dr;
92  mPosType dr_diff = deltaR[iat];
93  if (!L2_proj) // normal projector
94  {
95  getScaledDrift(tauovermass, grad_iat, dr);
96  dr += sqrttau * dr_diff;
97  rr = tauovermass * dot(dr_diff, dr_diff);
98  rr_proposed += rr;
99  if (rr > m_r2max)
100  {
101  ++nRejectTemp;
102  W.accept_rejectMove(iat, false);
103  continue;
104  }
105  if (!W.makeMoveAndCheck(iat, dr))
106  {
107  W.accept_rejectMove(iat, false);
108  continue;
109  }
110  }
111  else // projector including L2 potential
112  {
113  // do a fake move (zero distance)
114  // this ensures the temporary distance data is correct
115  // will need to remove this later, but requires reimplementation of computeL2DK
116  dr = 0.0;
117  if (!W.makeMoveAndCheck(iat, dr))
118  {
119  W.accept_rejectMove(iat, false);
120  continue;
121  }
122 
123  H.computeL2DK(W, iat, Dtmp, Ktmp);
124  D = Dtmp; // upcast for mixed precision
125  K = Ktmp;
126  getScaledDriftL2(tauovermass, grad_iat, D, K, dr);
127 
128  W.accept_rejectMove(iat, false);
129  rr = tauovermass * dot(dr_diff, dr_diff);
130  rr_proposed += rr;
131  if (rr > m_r2max)
132  {
133  ++nRejectTemp;
134  W.accept_rejectMove(iat, false);
135  continue;
136  }
137 
138  // move with just drift to update distance tables
139  if (!W.makeMoveAndCheck(iat, dr))
140  {
141  W.accept_rejectMove(iat, false);
142  continue;
143  }
144 
145  // compute diffusion step
146  H.computeL2D(W, iat, Dtmp);
147  D = Dtmp; // upcast for mixed precision
148  Dchol = cholesky(D);
149  dr_diff = dot(Dchol, dr_diff);
150  dr += sqrttau * dr_diff;
151 
152  // reverse the intermediate drift move
153  W.accept_rejectMove(iat, false);
154  // move with drift and diffusion together
155  if (!W.makeMoveAndCheck(iat, dr))
156  {
157  W.accept_rejectMove(iat, false);
158  continue;
159  }
160  }
161  ValueType ratio = Psi.calcRatioGrad(W, iat, grad_iat);
162  //node is crossed reject the move
164  {
165  ++nRejectTemp;
166  ++nNodeCrossing;
167  W.accept_rejectMove(iat, false);
168  Psi.rejectMove(iat);
169  }
170  else
171  {
172  FullPrecRealType logGf = -0.5 * dot(deltaR[iat], deltaR[iat]);
173  //Use the force of the particle iat
174  DriftModifier->getDrift(tauovermass, grad_iat, drtmp);
175  dr = drtmp; // upcast for mixed precision
176  dr = W.R[iat] - W.getActivePos() - dr;
177  FullPrecRealType logGb = -oneover2tau * dot(dr, dr);
178  RealType prob = std::norm(ratio) * std::exp(logGb - logGf);
179  bool is_accepted = false;
180 
181  if (RandomGen() < prob)
182  {
183  is_accepted = true;
184 
185  ++nAcceptTemp;
186  Psi.acceptMove(W, iat, true);
187  rr_accepted += rr;
188  }
189  else
190  {
191  ++nRejectTemp;
192  Psi.rejectMove(iat);
193  }
194  W.accept_rejectMove(iat, is_accepted);
195  }
196  }
197  }
199  W.donePbyP();
200  }
201 
202  if (nAcceptTemp > 0)
203  {
204  //need to overwrite the walker properties
205  RealType logpsi(0);
206  {
207  ScopedTimer local_timer(myTimers[DMC_buffer]);
208  thisWalker.Age = 0;
209  logpsi = Psi.updateBuffer(W, w_buffer, recompute);
210  W.saveWalker(thisWalker);
211  }
212  {
213  ScopedTimer local_timer(myTimers[DMC_hamiltonian]);
214  enew = H.evaluateWithToperator(W);
215  }
216  thisWalker.resetProperty(logpsi, Psi.getPhase(), enew, rr_accepted, rr_proposed, 1.0);
217  thisWalker.Weight *= branchEngine->branchWeight(enew, eold);
218  {
219  ScopedTimer local_timer(myTimers[DMC_collectables]);
220  H.auxHevaluate(W, thisWalker);
221  H.saveProperty(thisWalker.getPropertyBase());
222  }
223  }
224  else
225  {
226  //all moves are rejected: does not happen normally with reasonable wavefunctions
227  thisWalker.Age++;
228  thisWalker.Properties(WP::R2ACCEPTED) = 0.0;
229  //weight is set to 0 for traces
230  // consistent w/ no evaluate/auxHevaluate
231  RealType wtmp = thisWalker.Weight;
232  thisWalker.Weight = 0.0;
233  H.rejectedMove(W, thisWalker);
234  thisWalker.Weight = wtmp;
235  ++nAllRejected;
236  enew = eold; //copy back old energy
237  thisWalker.Weight *= branchEngine->branchWeight(enew, eold);
238  }
239 #if !defined(REMOVE_TRACEMANAGER)
241 #endif
242  {
243  ScopedTimer local_timer(myTimers[DMC_tmoves]);
244  const int NonLocalMoveAcceptedTemp = H.makeNonLocalMoves(W);
245  if (NonLocalMoveAcceptedTemp > 0)
246  {
247  RealType logpsi = Psi.updateBuffer(W, w_buffer, false);
248  W.saveWalker(thisWalker);
249  NonLocalMoveAccepted += NonLocalMoveAcceptedTemp;
250  }
251  }
252  nAccept += nAcceptTemp;
253  nReject += nRejectTemp;
254 
255  setMultiplicity(thisWalker);
256 }
257 
258 } // namespace qmcplusplus
TraceManager * Traces
traces
const BranchEngineType * branchEngine
branch engine, stateless reference to the one in QMCDriver
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
void acceptMove(ParticleSet &P, int iat, bool safe_to_delay=false)
update the state with the new data
DMCUpdatePbyPL2(MCWalkerConfiguration &w, TrialWaveFunction &psi, QMCHamiltonian &h, RandomBase< FullPrecRealType > &rg)
Constructor.
void saveProperty(IT first)
save the values of Hamiltonian elements to the Properties
bool phaseChanged(RealType psi0) const
Tensor< T, 3 > cholesky(const Tensor< T, 3 > &a)
Definition: TensorOps.h:950
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
void computeL2DK(ParticleSet &P, int iel, TensorType &D, PosType &K)
compute D matrix and K vector for L2 potential propagator
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
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
~DMCUpdatePbyPL2() override
destructor
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...
void advanceWalker(Walker_t &thisWalker, bool recompute) override
move a walker
void getScaledDriftL2(Tt tau, const TinyVector< TG, D > &qf, const Tensor< T, D > &Dmat, TinyVector< T, D > &Kvec, TinyVector< T, D > &drift)
evaluate a drift with a real force with rescaling for an L2 potential
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
void loadWalker(Walker_t &awalker, bool pbyp)
load a Walker_t to the current ParticleSet
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...
bool has_L2()
determine if L2 potential is present
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
void getScaledDrift(Tt tau, const TinyVector< TG, D > &qf, TinyVector< T, D > &drift)
evaluate a drift with a real force
virtual void getDrift(RealType tau, const GradType &qf, PosType &drift) const =0
evaluate a drift with a real force
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 computeL2D(ParticleSet &P, int iel, TensorType &D)
compute D matrix for L2 potential propagator
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)