QMCPACK
WaveFunctionComponent.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: Ken Esler, kpesler@gmail.com, University of Illinois at Urbana-Champaign
8 // Raymond Clay III, j.k.rofling@gmail.com, Lawrence Livermore National Laboratory
9 // Jeremy McMinnis, jmcminis@gmail.com, University of Illinois at Urbana-Champaign
10 // Jeongnim Kim, jeongnim.kim@gmail.com, University of Illinois at Urbana-Champaign
11 // Jaron T. Krogel, krogeljt@ornl.gov, Oak Ridge National Laboratory
12 // Mark A. Berrill, berrillma@ornl.gov, Oak Ridge National Laboratory
13 //
14 // File created by: Jeongnim Kim, jeongnim.kim@gmail.com, University of Illinois at Urbana-Champaign
15 //////////////////////////////////////////////////////////////////////////////////////
16 
17 
18 #include "WaveFunctionComponent.h"
19 
20 namespace qmcplusplus
21 {
22 // for return types
24 
26  : UpdateMode(ORB_WALKER), Bytes_in_WFBuffer(0), my_name_(obj_name), log_value_(0.0)
27 {}
28 
30 
35 {
36  assert(this == &wfc_list.getLeader());
37  for (int iw = 0; iw < wfc_list.size(); iw++)
38  wfc_list[iw].evaluateLog(p_list[iw], G_list[iw], L_list[iw]);
39 }
40 
42 {
45 
46  evaluateLog(P, temp_G, temp_L);
47 }
48 
51  const std::vector<bool>& recompute) const
52 {
53  assert(this == &wfc_list.getLeader());
54  for (int iw = 0; iw < wfc_list.size(); iw++)
55  if (recompute[iw])
56  wfc_list[iw].recompute(p_list[iw]);
57 }
58 
61  int ig) const
62 {
63  assert(this == &wfc_list.getLeader());
64  for (int iw = 0; iw < wfc_list.size(); iw++)
65  wfc_list[iw].prepareGroup(p_list[iw], ig);
66 }
67 
68 template<CoordsType CT>
71  const int iat,
72  TWFGrads<CT>& grad_now) const
73 {
74  if constexpr (CT == CoordsType::POS_SPIN)
75  mw_evalGradWithSpin(wfc_list, p_list, iat, grad_now.grads_positions, grad_now.grads_spins);
76  else
77  mw_evalGrad(wfc_list, p_list, iat, grad_now.grads_positions);
78 }
79 
82  int iat,
83  std::vector<GradType>& grad_now) const
84 {
85  assert(this == &wfc_list.getLeader());
86  for (int iw = 0; iw < wfc_list.size(); iw++)
87  grad_now[iw] = wfc_list[iw].evalGrad(p_list[iw], iat);
88 }
89 
92  int iat,
93  std::vector<GradType>& grad_now,
94  std::vector<ComplexType>& spingrad_now) const
95 {
96  assert(this == &wfc_list.getLeader());
97  for (int iw = 0; iw < wfc_list.size(); iw++)
98  {
99  spingrad_now[iw] = 0;
100  grad_now[iw] = wfc_list[iw].evalGradWithSpin(p_list[iw], iat, spingrad_now[iw]);
101  }
102 }
103 
105  const RefVectorWithLeader<ParticleSet>& p_list,
106  int iat,
107  std::vector<PsiValue>& ratios) const
108 {
109  assert(this == &wfc_list.getLeader());
110  for (int iw = 0; iw < wfc_list.size(); iw++)
111  ratios[iw] = wfc_list[iw].ratio(p_list[iw], iat);
112 }
113 
114 
116 {
117  APP_ABORT("WaveFunctionComponent::ratioGrad is not implemented in " + getClassName() + " class.");
118  return ValueType();
119 }
120 
121 template<CoordsType CT>
123  const RefVectorWithLeader<ParticleSet>& p_list,
124  int iat,
125  std::vector<PsiValue>& ratios,
126  TWFGrads<CT>& grad_new) const
127 {
128  if constexpr (CT == CoordsType::POS_SPIN)
129  mw_ratioGradWithSpin(wfc_list, p_list, iat, ratios, grad_new.grads_positions, grad_new.grads_spins);
130  else
131  mw_ratioGrad(wfc_list, p_list, iat, ratios, grad_new.grads_positions);
132 }
133 
135  const RefVectorWithLeader<ParticleSet>& p_list,
136  int iat,
137  std::vector<PsiValue>& ratios,
138  std::vector<GradType>& grad_new) const
139 {
140  assert(this == &wfc_list.getLeader());
141  for (int iw = 0; iw < wfc_list.size(); iw++)
142  ratios[iw] = wfc_list[iw].ratioGrad(p_list[iw], iat, grad_new[iw]);
143 }
144 
146  const RefVectorWithLeader<ParticleSet>& p_list,
147  int iat,
148  std::vector<PsiValue>& ratios,
149  std::vector<GradType>& grad_new,
150  std::vector<ComplexType>& spingrad_new) const
151 {
152  assert(this == &wfc_list.getLeader());
153  for (int iw = 0; iw < wfc_list.size(); iw++)
154  ratios[iw] = wfc_list[iw].ratioGradWithSpin(p_list[iw], iat, grad_new[iw], spingrad_new[iw]);
155 }
156 
158  const RefVectorWithLeader<ParticleSet>& p_list,
159  int iat,
160  const std::vector<bool>& isAccepted,
161  bool safe_to_delay) const
162 {
163  assert(this == &wfc_list.getLeader());
164  for (int iw = 0; iw < wfc_list.size(); iw++)
165  if (isAccepted[iw])
166  wfc_list[iw].acceptMove(p_list[iw], iat, safe_to_delay);
167  else
168  wfc_list[iw].restore(iat);
169 }
170 
172 {
173  assert(this == &wfc_list.getLeader());
174  for (int iw = 0; iw < wfc_list.size(); iw++)
175  wfc_list[iw].completeUpdates();
176 }
177 
181  bool fromscratch)
182 {
183  return evaluateLog(P, G, L);
184 }
185 
187  const RefVectorWithLeader<ParticleSet>& p_list,
190  bool fromscratch) const
191 {
192  assert(this == &wfc_list.getLeader());
193  for (int iw = 0; iw < wfc_list.size(); iw++)
194  wfc_list[iw].evaluateGL(p_list[iw], G_list[iw], L_list[iw], fromscratch);
195 }
196 
198 {
199  if (isOptimizable())
200  throw std::logic_error("Bug!! " + getClassName() +
201  "::extractOptimizableObjectRefs "
202  "must be overloaded when the WFC is optimizable.");
203 }
204 
206 {
207  if (isOptimizable())
208  throw std::logic_error("Bug!! " + getClassName() +
209  "::checkOutVariables "
210  "must be overloaded when the WFC is optimizable.");
211 }
212 
214  const opt_variables_type& active,
215  Vector<ValueType>& dlogpsi)
216 {
217  throw std::runtime_error("WaveFunctionComponent::evaluateDerivativesWF is not implemented by " + getClassName());
218 }
219 
220 
221 /*@todo makeClone should be a pure virtual function
222  */
223 std::unique_ptr<WaveFunctionComponent> WaveFunctionComponent::makeClone(ParticleSet& tpq) const
224 {
225  APP_ABORT("Implement WaveFunctionComponent::makeClone " + getClassName() + " class.");
226  return std::unique_ptr<WaveFunctionComponent>();
227 }
228 
230 
231 void WaveFunctionComponent::evaluateRatiosAlltoOne(ParticleSet& P, std::vector<ValueType>& ratios)
232 {
233  assert(P.getTotalNum() == ratios.size());
234  for (int i = 0; i < P.getTotalNum(); ++i)
235  ratios[i] = ratio(P, i);
236 }
237 
238 void WaveFunctionComponent::evaluateRatios(const VirtualParticleSet& P, std::vector<ValueType>& ratios)
239 {
240  std::ostringstream o;
241  o << "WaveFunctionComponent::evaluateRatios is not implemented by " << getClassName();
242  APP_ABORT(o.str());
243 }
244 
245 void WaveFunctionComponent::evaluateSpinorRatios(const VirtualParticleSet& P, const std::pair<ValueVector, ValueVector>& spinor_multiplier, std::vector<ValueType>& ratios)
246 {
247  evaluateRatios(P, ratios);
248 }
249 
252  std::vector<std::vector<ValueType>>& ratios) const
253 {
254  assert(this == &wfc_list.getLeader());
255  for (int iw = 0; iw < wfc_list.size(); iw++)
256  wfc_list[iw].evaluateRatios(vp_list[iw], ratios[iw]);
257 }
258 
260  const opt_variables_type& optvars,
261  std::vector<ValueType>& ratios,
262  Matrix<ValueType>& dratios)
263 {
264  //default is only ratios and zero derivatives
265  evaluateRatios(VP, ratios);
266 }
267 
269 {
270  std::ostringstream o;
271  o << "WaveFunctionComponent::registerTWFFastDerivWrapper is not implemented by " << getClassName();
272  APP_ABORT(o.str());
273 }
274 
275 template void WaveFunctionComponent::mw_evalGrad<CoordsType::POS>(
277  const RefVectorWithLeader<ParticleSet>& p_list,
278  int iat,
279  TWFGrads<CoordsType::POS>& grad_now) const;
280 template void WaveFunctionComponent::mw_evalGrad<CoordsType::POS_SPIN>(
282  const RefVectorWithLeader<ParticleSet>& p_list,
283  int iat,
284  TWFGrads<CoordsType::POS_SPIN>& grad_now) const;
285 template void WaveFunctionComponent::mw_ratioGrad<CoordsType::POS>(
287  const RefVectorWithLeader<ParticleSet>& p_list,
288  int iat,
289  std::vector<PsiValue>& ratios,
290  TWFGrads<CoordsType::POS>& grad_new) const;
291 template void WaveFunctionComponent::mw_ratioGrad<CoordsType::POS_SPIN>(
293  const RefVectorWithLeader<ParticleSet>& p_list,
294  int iat,
295  std::vector<PsiValue>& ratios,
296  TWFGrads<CoordsType::POS_SPIN>& grad_new) const;
297 
298 } // namespace qmcplusplus
Fixed-size array.
Definition: OhmmsTinyMeta.h:30
virtual bool isOptimizable() const
if true, this contains optimizable components
virtual LogValue evaluateLog(const ParticleSet &P, ParticleSet::ParticleGradient &G, ParticleSet::ParticleLaplacian &L)=0
evaluate the value of the WaveFunctionComponent from scratch
virtual void evaluateDerivRatios(const VirtualParticleSet &VP, const opt_variables_type &optvars, std::vector< ValueType > &ratios, Matrix< ValueType > &dratios)
evaluate ratios to evaluate the non-local PP
WaveFunctionComponent::PsiValue PsiValue
Definition: SlaterDet.cpp:25
helper functions for EinsplineSetBuilder
Definition: Configuration.h:43
virtual void mw_prepareGroup(const RefVectorWithLeader< WaveFunctionComponent > &wfc_list, const RefVectorWithLeader< ParticleSet > &p_list, int ig) const
virtual void mw_recompute(const RefVectorWithLeader< WaveFunctionComponent > &wfc_list, const RefVectorWithLeader< ParticleSet > &p_list, const std::vector< bool > &recompute) const
QTBase::RealType RealType
Definition: Configuration.h:58
virtual void registerTWFFastDerivWrapper(const ParticleSet &P, TWFFastDerivWrapper &twf) const
Register the component with the TWFFastDerivWrapper wrapper.
virtual void recompute(const ParticleSet &P)
recompute the value of the WaveFunctionComponents which require critical accuracy.
virtual std::string getClassName() const =0
return class name
size_t getTotalNum() const
Definition: ParticleSet.h:493
virtual LogValue evaluateGL(const ParticleSet &P, ParticleSet::ParticleGradient &G, ParticleSet::ParticleLaplacian &L, bool fromscratch)
compute gradients and laplacian of the TWF with respect to each particle.
A ParticleSet that handles virtual moves of a selected particle of a given physical ParticleSet Virtu...
Attaches a unit to a Vector for IO.
TWFFastDerivWrapper is a wrapper class for TrialWavefunction that provides separate and low level acc...
void mw_evalGrad(const RefVectorWithLeader< WaveFunctionComponent > &wfc_list, const RefVectorWithLeader< ParticleSet > &p_list, const int iat, TWFGrads< CT > &grads_now) const
compute the current gradients for the iat-th particle of multiple walkers
virtual void mw_evaluateGL(const RefVectorWithLeader< WaveFunctionComponent > &wfc_list, const RefVectorWithLeader< ParticleSet > &p_list, const RefVector< ParticleSet::ParticleGradient > &G_list, const RefVector< ParticleSet::ParticleLaplacian > &L_list, bool fromscratch) const
evaluate gradients and laplacian of the same type WaveFunctionComponent of multiple walkers ...
std::complex< QTFull::RealType > LogValue
virtual void mw_ratioGradWithSpin(const RefVectorWithLeader< WaveFunctionComponent > &wfc_list, const RefVectorWithLeader< ParticleSet > &p_list, int iat, std::vector< PsiValue > &ratios, std::vector< GradType > &grad_new, std::vector< ComplexType > &spingrad_new) const
compute the ratio of the new to old WaveFunctionComponent value and the new gradient/spingradient of ...
Specialized paritlce class for atomistic simulations.
Definition: ParticleSet.h:55
virtual void checkOutVariables(const opt_variables_type &active)
check out variational optimizable variables
QTBase::ValueType ValueType
Definition: Configuration.h:60
virtual void mw_completeUpdates(const RefVectorWithLeader< WaveFunctionComponent > &wfc_list) const
complete all the delayed or asynchronous operations for all the walkers in a batch before leaving the...
class to handle a set of variables that can be modified during optimizations
Definition: VariableSet.h:49
virtual void mw_evalGradWithSpin(const RefVectorWithLeader< WaveFunctionComponent > &wfc_list, const RefVectorWithLeader< ParticleSet > &p_list, int iat, std::vector< GradType > &grad_now, std::vector< ComplexType > &spingrad_now) const
compute the current gradients and spin gradients for the iat-th particle of multiple walkers ...
virtual GradType evalGrad(ParticleSet &P, int iat)
return the current gradient for the iat-th particle
#define APP_ABORT(msg)
Widely used but deprecated fatal error macros from legacy code.
Definition: AppAbort.h:27
virtual void evaluateSpinorRatios(const VirtualParticleSet &VP, const std::pair< ValueVector, ValueVector > &spinor_multiplier, std::vector< ValueType > &ratios)
Used by SOECPComponent for faster SOC evaluation.
virtual void evaluateDerivativesWF(ParticleSet &P, const opt_variables_type &optvars, Vector< ValueType > &dlogpsi)
Compute the derivatives of the log of the wavefunction with respect to optimizable parameters...
virtual PsiValue ratio(ParticleSet &P, int iat)=0
evaluate the ratio of the new to old WaveFunctionComponent value
virtual void evaluateRatios(const VirtualParticleSet &VP, std::vector< ValueType > &ratios)
evaluate ratios to evaluate the non-local PP
virtual void prepareGroup(ParticleSet &P, int ig)
Prepare internal data for updating WFC correspond to a particle group It should be called before movi...
virtual void mw_accept_rejectMove(const RefVectorWithLeader< WaveFunctionComponent > &wfc_list, const RefVectorWithLeader< ParticleSet > &p_list, int iat, const std::vector< bool > &isAccepted, bool safe_to_delay=false) const
moves of the iat-th particle on some walkers in a batch is accepted.
std::vector< std::reference_wrapper< T > > RefVector
virtual void extractOptimizableObjectRefs(UniqueOptObjRefs &opt_obj_refs)
extract underlying OptimizableObject references
virtual void mw_evaluateLog(const RefVectorWithLeader< WaveFunctionComponent > &wfc_list, const RefVectorWithLeader< ParticleSet > &p_list, const RefVector< ParticleSet::ParticleGradient > &G_list, const RefVector< ParticleSet::ParticleLaplacian > &L_list) const
evaluate from scratch the same type WaveFunctionComponent of multiple walkers
void mw_ratioGrad(const RefVectorWithLeader< WaveFunctionComponent > &wfc_list, const RefVectorWithLeader< ParticleSet > &p_list, int iat, std::vector< PsiValue > &ratios, TWFGrads< CT > &grad_new) const
virtual void mw_evaluateRatios(const RefVectorWithLeader< WaveFunctionComponent > &wfc_list, const RefVectorWithLeader< const VirtualParticleSet > &vp_list, std::vector< std::vector< ValueType >> &ratios) const
evaluate ratios to evaluate the non-local PP multiple walkers
virtual PsiValue ratioGradWithSpin(ParticleSet &P, int iat, GradType &grad_iat, ComplexType &spingrad_iat)
evaluate the ratio of the new to old WaveFunctionComponent value and the new spin gradient Default im...
virtual PsiValue ratioGrad(ParticleSet &P, int iat, GradType &grad_iat)
evaluate the ratio of the new to old WaveFunctionComponent value and the new gradient ...
virtual void completeUpdates()
complete all the delayed or asynchronous operations before leaving the p-by-p move region...
WaveFunctionComponent(const std::string &obj_name="")
default constructor
Declaration of WaveFunctionComponent.
virtual void mw_calcRatio(const RefVectorWithLeader< WaveFunctionComponent > &wfc_list, const RefVectorWithLeader< ParticleSet > &p_list, int iat, std::vector< PsiValue > &ratios) const
compute the ratio of the new to old WaveFunctionComponent value of multiple walkers ...
virtual void evaluateRatiosAlltoOne(ParticleSet &P, std::vector< ValueType > &ratios)
evaluate the ratios of one virtual move with respect to all the particles
virtual RealType KECorrection()
Return the Chiesa kinetic energy correction.
virtual std::unique_ptr< WaveFunctionComponent > makeClone(ParticleSet &tqp) const
make clone
virtual ~WaveFunctionComponent()
default destructor