QMCPACK
SlaterDet.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: Ken Esler, kpesler@gmail.com, University of Illinois at Urbana-Champaign
8 // Miguel Morales, moralessilva2@llnl.gov, 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 // Raymond Clay III, j.k.rofling@gmail.com, Lawrence Livermore 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 #ifndef QMCPLUSPLUS_SLATERDETERMINANT_WITHBASE_H
19 #define QMCPLUSPLUS_SLATERDETERMINANT_WITHBASE_H
21 
22 #include <map>
23 
24 namespace qmcplusplus
25 {
26 class TWFFastDerivWrapper;
27 
29 {
30 public:
32  ///container for the DiracDeterminants
33  const std::vector<std::unique_ptr<Determinant_t>> Dets;
34 
35  /** constructor
36  * @param targetPtcl target Particleset
37  */
38  SlaterDet(ParticleSet& targetPtcl,
39  std::vector<std::unique_ptr<Determinant_t>> dets,
40  const std::string& class_name = "SlaterDet");
41 
42  ///destructor
43  ~SlaterDet() override;
44 
45  std::string getClassName() const override { return "SlaterDet"; }
46 
47  bool isFermionic() const final { return true; }
48  bool isOptimizable() const override;
49 
50  void extractOptimizableObjectRefs(UniqueOptObjRefs& opt_obj_refs) override;
51 
52  void checkOutVariables(const opt_variables_type& active) override;
53 
54  void registerTWFFastDerivWrapper(const ParticleSet& P, TWFFastDerivWrapper& twf) const override;
55 
58  ParticleSet::ParticleLaplacian& L) override;
59 
63  const RefVector<ParticleSet::ParticleLaplacian>& L_list) const override;
64 
68  bool fromscratch) override;
69 
74  bool fromscratch) const override;
75 
76  void recompute(const ParticleSet& P) override;
77 
80  const std::vector<bool>& recompute) const override;
81 
82  void evaluateHessian(ParticleSet& P, HessVector& grad_grad_psi) override;
83 
84  ///return the total number of Dirac determinants
85  inline int size() const { return Dets.size(); }
86 
87  void registerData(ParticleSet& P, WFBufferType& buf) override;
88 
89  LogValue updateBuffer(ParticleSet& P, WFBufferType& buf, bool fromscratch = false) override;
90 
91  void copyFromBuffer(ParticleSet& P, WFBufferType& buf) override;
92 
93  void createResource(ResourceCollection& collection) const override;
94 
95  void acquireResource(ResourceCollection& collection,
96  const RefVectorWithLeader<WaveFunctionComponent>& wfc_list) const override;
97 
98  void releaseResource(ResourceCollection& collection,
99  const RefVectorWithLeader<WaveFunctionComponent>& wfc_list) const override;
100 
101  inline void evaluateRatios(const VirtualParticleSet& VP, std::vector<ValueType>& ratios) override
102  {
103  return Dets[getDetID(VP.refPtcl)]->evaluateRatios(VP, ratios);
104  }
105 
106  inline void evaluateSpinorRatios(const VirtualParticleSet& VP, const std::pair<ValueVector, ValueVector>& spinor_multiplier, std::vector<ValueType>& ratios) override
107  {
108  return Dets[getDetID(VP.refPtcl)]->evaluateSpinorRatios(VP, spinor_multiplier, ratios);
109  }
110 
112  const opt_variables_type& optvars,
113  std::vector<ValueType>& ratios,
114  Matrix<ValueType>& dratios) override;
115 
118  std::vector<std::vector<ValueType>>& ratios) const override
119  {
120  if (wfc_list.size())
121  {
122  // assuming all the VP.refPtcl are identical
123  const int det_id = getDetID(vp_list[0].refPtcl);
124  Dets[det_id]->mw_evaluateRatios(extract_DetRef_list(wfc_list, det_id), vp_list, ratios);
125  }
126  }
127 
128  PsiValue ratioGrad(ParticleSet& P, int iat, GradType& grad_iat) override;
129 
130  PsiValue ratioGradWithSpin(ParticleSet& P, int iat, GradType& grad_iat, ComplexType& spingrad_iat) override;
131 
133  const RefVectorWithLeader<ParticleSet>& p_list,
134  int iat,
135  std::vector<PsiValue>& ratios,
136  std::vector<GradType>& grad_now) const override;
137 
139  const RefVectorWithLeader<ParticleSet>& p_list,
140  int iat,
141  std::vector<PsiValue>& ratios,
142  std::vector<GradType>& grad_now,
143  std::vector<ComplexType>& spingrad_now) const override;
144 
145  GradType evalGrad(ParticleSet& P, int iat) override { return Dets[getDetID(iat)]->evalGrad(P, iat); }
146 
147  GradType evalGradWithSpin(ParticleSet& P, int iat, ComplexType& spingrad) override
148  {
149  return Dets[getDetID(iat)]->evalGradWithSpin(P, iat, spingrad);
150  }
151 
153  const RefVectorWithLeader<ParticleSet>& p_list,
154  int iat,
155  std::vector<GradType>& grad_now) const override
156  {
157  const int det_id = getDetID(iat);
158  Dets[det_id]->mw_evalGrad(extract_DetRef_list(wfc_list, det_id), p_list, iat, grad_now);
159  }
160 
162  const RefVectorWithLeader<ParticleSet>& p_list,
163  int iat,
164  std::vector<GradType>& grad_now,
165  std::vector<ComplexType>& spingrad_now) const override;
166 
167  GradType evalGradSource(ParticleSet& P, ParticleSet& src, int iat) override
168  {
169  GradType G = GradType();
170  for (int iz = 0; iz < size(); iz++)
171  G += Dets[iz]->evalGradSource(P, src, iat);
172  return G;
173  }
174 
176  ParticleSet& src,
177  int iat,
180  {
181  GradType G = GradType();
182  for (int iz = 0; iz < size(); iz++)
183  G += Dets[iz]->evalGradSource(P, src, iat, grad_grad, lapl_grad);
184  return G;
185  }
186 
187  inline void restore(int iat) override { return Dets[getDetID(iat)]->restore(iat); }
188 
189  inline void acceptMove(ParticleSet& P, int iat, bool safe_to_delay = false) override
190  {
191  Dets[getDetID(iat)]->acceptMove(P, iat, safe_to_delay);
192 
193  log_value_ = 0.0;
194  for (int i = 0; i < Dets.size(); ++i)
195  log_value_ += Dets[i]->get_log_value();
196  }
197 
199  const RefVectorWithLeader<ParticleSet>& p_list,
200  int iat,
201  const std::vector<bool>& isAccepted,
202  bool safe_to_delay = false) const override
203  {
204  constexpr LogValue czero(0);
205 
206  // This log_value_ is in the slater determinant, it's still around but not consistent anymore with the
207  // sum of the log_values in its determinants. Caching the state seems like a bad call, but the wfc base class
208  // having log_value_ as a data member asks for this sort of consistency issue when wfc can contain wfc.
209  for (int iw = 0; iw < wfc_list.size(); iw++)
210  if (isAccepted[iw])
211  wfc_list.getCastedElement<SlaterDet>(iw).log_value_ = czero;
212 
213  for (int i = 0; i < Dets.size(); ++i)
214  {
215  const auto Det_list(extract_DetRef_list(wfc_list, i));
216 
217  if (i == getDetID(iat))
218  Dets[i]->mw_accept_rejectMove(Det_list, p_list, iat, isAccepted, safe_to_delay);
219 
220  for (int iw = 0; iw < wfc_list.size(); iw++)
221  if (isAccepted[iw])
222  wfc_list.getCastedElement<SlaterDet>(iw).log_value_ += Det_list[iw].get_log_value();
223  }
224  }
225 
226  void completeUpdates() override
227  {
228  for (int i = 0; i < Dets.size(); i++)
229  Dets[i]->completeUpdates();
230  }
231 
233  {
234  for (int i = 0; i < Dets.size(); i++)
235  Dets[i]->mw_completeUpdates(extract_DetRef_list(wfc_list, i));
236  }
237 
238  inline PsiValue ratio(ParticleSet& P, int iat) override { return Dets[getDetID(iat)]->ratio(P, iat); }
239 
241  const RefVectorWithLeader<ParticleSet>& p_list,
242  int iat,
243  std::vector<PsiValue>& ratios) const override
244  {
245  const int det_id = getDetID(iat);
246  Dets[det_id]->mw_calcRatio(extract_DetRef_list(wfc_list, det_id), p_list, iat, ratios);
247  }
248 
249  std::unique_ptr<WaveFunctionComponent> makeClone(ParticleSet& tqp) const override;
250 
251  SPOSetPtr getPhi(int i = 0) { return Dets[i]->getPhi(); }
252 
253  void evaluateRatiosAlltoOne(ParticleSet& P, std::vector<ValueType>& ratios) override;
254 
256  const opt_variables_type& active,
257  Vector<ValueType>& dlogpsi,
258  Vector<ValueType>& dhpsioverpsi) override
259  {
260  // First zero out values, since each determinant only adds on
261  // its contribution (i.e. +=) , rather than setting the value
262  // (i.e. =)
263  for (int k = 0; k < myVars.size(); ++k)
264  {
265  int kk = myVars.where(k);
266  if (kk >= 0)
267  dlogpsi[kk] = dhpsioverpsi[kk] = 0.0;
268  }
269  // Now add on contribution from each determinant to the derivatives
270  for (int i = 0; i < Dets.size(); i++)
271  Dets[i]->evaluateDerivatives(P, active, dlogpsi, dhpsioverpsi);
272  }
273 
274  void evaluateDerivativesWF(ParticleSet& P, const opt_variables_type& active, Vector<ValueType>& dlogpsi) override
275  {
276  // First zero out values, since each determinant only adds on
277  // its contribution (i.e. +=) , rather than setting the value
278  // (i.e. =)
279  for (int k = 0; k < myVars.size(); ++k)
280  {
281  int kk = myVars.where(k);
282  if (kk >= 0)
283  dlogpsi[kk] = 0.0;
284  }
285  // Now add on contribution from each determinant to the derivatives
286  for (int i = 0; i < Dets.size(); i++)
287  Dets[i]->evaluateDerivativesWF(P, active, dlogpsi);
288  }
289 
290  void evaluateGradDerivatives(const ParticleSet::ParticleGradient& G_in, std::vector<ValueType>& dgradlogpsi) override
291  {
292  for (int i = 0; i < Dets.size(); i++)
293  Dets[i]->evaluateGradDerivatives(G_in, dgradlogpsi);
294  }
295 
296 private:
297  //get Det ID
298  inline int getDetID(const int iat) const
299  {
300  int id = 0;
301  while (iat > Last[id])
302  id++;
303  return id;
304  }
305 
306  // helper function for extracting a list of WaveFunctionComponent from a list of TrialWaveFunction
309  int det_id) const
310  {
312  Det_list.reserve(wfc_list.size());
313  for (WaveFunctionComponent& wfc : wfc_list)
314  Det_list.push_back(*static_cast<SlaterDet&>(wfc).Dets[det_id]);
315  return Det_list;
316  }
317 
318  ///the last particle of each group
319  std::vector<int> Last;
320 };
321 } // namespace qmcplusplus
322 #endif
base class for Single-particle orbital sets
Definition: SPOSet.h:46
void evaluateRatios(const VirtualParticleSet &VP, std::vector< ValueType > &ratios) override
evaluate ratios to evaluate the non-local PP
Definition: SlaterDet.h:101
Declaration of DiracDeterminantBase with a S(ingle)P(article)O(rbital)Set.
void evaluateHessian(ParticleSet &P, HessVector &grad_grad_psi) override
Definition: SlaterDet.cpp:199
void checkOutVariables(const opt_variables_type &active) override
check out variational optimizable variables
Definition: SlaterDet.cpp:53
LogValue evaluateGL(const ParticleSet &P, ParticleSet::ParticleGradient &G, ParticleSet::ParticleLaplacian &L, bool fromscratch) override
compute gradients and laplacian of the TWF with respect to each particle.
Definition: SlaterDet.cpp:151
void evaluateGradDerivatives(const ParticleSet::ParticleGradient &G_in, std::vector< ValueType > &dgradlogpsi) override
Calculates the derivatives of with respect to the optimizable parameters, and the dot product of thi...
Definition: SlaterDet.h:290
Fixed-size array.
Definition: OhmmsTinyMeta.h:30
void evaluateDerivRatios(const VirtualParticleSet &VP, const opt_variables_type &optvars, std::vector< ValueType > &ratios, Matrix< ValueType > &dratios) override
evaluate ratios to evaluate the non-local PP
Definition: SlaterDet.cpp:114
std::string getClassName() const override
return class name
Definition: SlaterDet.h:45
void mw_evaluateRatios(const RefVectorWithLeader< WaveFunctionComponent > &wfc_list, const RefVectorWithLeader< const VirtualParticleSet > &vp_list, std::vector< std::vector< ValueType >> &ratios) const override
evaluate ratios to evaluate the non-local PP multiple walkers
Definition: SlaterDet.h:116
WaveFunctionComponent::PsiValue PsiValue
Definition: SlaterDet.cpp:25
helper functions for EinsplineSetBuilder
Definition: Configuration.h:43
QTBase::GradType GradType
Definition: Configuration.h:62
std::unique_ptr< WaveFunctionComponent > makeClone(ParticleSet &tqp) const override
make clone
Definition: SlaterDet.cpp:267
int size() const
return the total number of Dirac determinants
Definition: SlaterDet.h:85
void extractOptimizableObjectRefs(UniqueOptObjRefs &opt_obj_refs) override
extract underlying OptimizableObject references
Definition: SlaterDet.cpp:47
void registerData(ParticleSet &P, WFBufferType &buf) override
For particle-by-particle move.
Definition: SlaterDet.cpp:241
PsiValue ratio(ParticleSet &P, int iat) override
evaluate the ratio of the new to old WaveFunctionComponent value
Definition: SlaterDet.h:238
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 override
moves of the iat-th particle on some walkers in a batch is accepted.
Definition: SlaterDet.h:198
constexpr std::complex< float > czero
Definition: BLAS.hpp:51
void mw_ratioGradWithSpin(const RefVectorWithLeader< WaveFunctionComponent > &wfc_list, const RefVectorWithLeader< ParticleSet > &p_list, int iat, std::vector< PsiValue > &ratios, std::vector< GradType > &grad_now, std::vector< ComplexType > &spingrad_now) const override
compute the ratio of the new to old WaveFunctionComponent value and the new gradient/spingradient of ...
Definition: SlaterDet.cpp:85
A ParticleSet that handles virtual moves of a selected particle of a given physical ParticleSet Virtu...
GradType evalGradSource(ParticleSet &P, ParticleSet &src, int iat, TinyVector< ParticleSet::ParticleGradient, OHMMS_DIM > &grad_grad, TinyVector< ParticleSet::ParticleLaplacian, OHMMS_DIM > &lapl_grad) override
Adds the gradient w.r.t.
Definition: SlaterDet.h:175
int getDetID(const int iat) const
Definition: SlaterDet.h:298
int refPtcl
Reference particle.
LatticeGaussianProduct::GradType GradType
QTBase::ComplexType ComplexType
Definition: Configuration.h:59
const std::vector< std::unique_ptr< Determinant_t > > Dets
container for the DiracDeterminants
Definition: SlaterDet.h:33
GradType evalGradSource(ParticleSet &P, ParticleSet &src, int iat) override
return the logarithmic gradient for the iat-th particle of the source particleset ...
Definition: SlaterDet.h:167
void createResource(ResourceCollection &collection) const override
initialize a shared resource and hand it to a collection
Definition: SlaterDet.cpp:215
Attaches a unit to a Vector for IO.
TWFFastDerivWrapper is a wrapper class for TrialWavefunction that provides separate and low level acc...
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 override
evaluate from scratch the same type WaveFunctionComponent of multiple walkers
Definition: SlaterDet.cpp:132
void restore(int iat) override
If a move for iat-th particle is rejected, restore to the content.
Definition: SlaterDet.h:187
opt_variables_type myVars
list of variables this WaveFunctionComponent handles
std::complex< QTFull::RealType > LogValue
void evaluateRatiosAlltoOne(ParticleSet &P, std::vector< ValueType > &ratios) override
evaluate the ratios of one virtual move with respect to all the particles
Definition: SlaterDet.cpp:108
An abstract class for a component of a many-body trial wave function.
Specialized paritlce class for atomistic simulations.
Definition: ParticleSet.h:55
LogValue updateBuffer(ParticleSet &P, WFBufferType &buf, bool fromscratch=false) override
For particle-by-particle move.
Definition: SlaterDet.cpp:249
void releaseResource(ResourceCollection &collection, const RefVectorWithLeader< WaveFunctionComponent > &wfc_list) const override
return a shared resource to a collection
Definition: SlaterDet.cpp:231
PsiValue ratioGradWithSpin(ParticleSet &P, int iat, GradType &grad_iat, ComplexType &spingrad_iat) override
evaluate the ratio of the new to old WaveFunctionComponent value and the new spin gradient Default im...
Definition: SlaterDet.cpp:70
RefVectorWithLeader< WaveFunctionComponent > extract_DetRef_list(const RefVectorWithLeader< WaveFunctionComponent > &wfc_list, int det_id) const
Definition: SlaterDet.h:307
SPOSetPtr getPhi(int i=0)
Definition: SlaterDet.h:251
void evaluateDerivativesWF(ParticleSet &P, const opt_variables_type &active, Vector< ValueType > &dlogpsi) override
Compute the derivatives of the log of the wavefunction with respect to optimizable parameters...
Definition: SlaterDet.h:274
void recompute(const ParticleSet &P) override
recompute the value of the WaveFunctionComponents which require critical accuracy.
Definition: SlaterDet.cpp:182
void registerTWFFastDerivWrapper(const ParticleSet &P, TWFFastDerivWrapper &twf) const override
Register the component with the TWFFastDerivWrapper wrapper.
Definition: SlaterDet.cpp:277
void mw_ratioGrad(const RefVectorWithLeader< WaveFunctionComponent > &wfc_list, const RefVectorWithLeader< ParticleSet > &p_list, int iat, std::vector< PsiValue > &ratios, std::vector< GradType > &grad_now) const override
compute the ratio of the new to old WaveFunctionComponent value and the new gradient of multiple walk...
Definition: SlaterDet.cpp:75
CASTTYPE & getCastedElement(size_t i) const
class to handle a set of variables that can be modified during optimizations
Definition: VariableSet.h:49
PsiValue ratioGrad(ParticleSet &P, int iat, GradType &grad_iat) override
evaluate the ratio of the new to old WaveFunctionComponent value and the new gradient ...
Definition: SlaterDet.cpp:65
int where(int i) const
return the locator of the i-th Index
Definition: VariableSet.h:90
bool isFermionic() const final
true, if this component is fermionic
Definition: SlaterDet.h:47
bool isOptimizable() const override
if true, this contains optimizable components
Definition: SlaterDet.cpp:42
void acquireResource(ResourceCollection &collection, const RefVectorWithLeader< WaveFunctionComponent > &wfc_list) const override
acquire a shared resource from a collection
Definition: SlaterDet.cpp:221
void completeUpdates() override
complete all the delayed or asynchronous operations before leaving the p-by-p move region...
Definition: SlaterDet.h:226
size_type size() const
return the size
Definition: VariableSet.h:88
~SlaterDet() override
destructor
GradType evalGradWithSpin(ParticleSet &P, int iat, ComplexType &spingrad) override
return the current spin gradient for the iat-th particle Default implementation assumes that WaveFunc...
Definition: SlaterDet.h:147
std::vector< std::reference_wrapper< T > > RefVector
void evaluateSpinorRatios(const VirtualParticleSet &VP, const std::pair< ValueVector, ValueVector > &spinor_multiplier, std::vector< ValueType > &ratios) override
Used by SOECPComponent for faster SOC evaluation.
Definition: SlaterDet.h:106
std::vector< int > Last
the last particle of each group
Definition: SlaterDet.h:319
void evaluateDerivatives(ParticleSet &P, const opt_variables_type &active, Vector< ValueType > &dlogpsi, Vector< ValueType > &dhpsioverpsi) override
Compute the derivatives of both the log of the wavefunction and kinetic energy with respect to optimi...
Definition: SlaterDet.h:255
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 override
compute the current gradients and spin gradients for the iat-th particle of multiple walkers ...
Definition: SlaterDet.cpp:98
void mw_completeUpdates(const RefVectorWithLeader< WaveFunctionComponent > &wfc_list) const override
complete all the delayed or asynchronous operations for all the walkers in a batch before leaving the...
Definition: SlaterDet.h:232
QMCTraits::ComplexType ComplexType
OrbitalSetTraits< ValueType >::HessVector HessVector
void mw_recompute(const RefVectorWithLeader< WaveFunctionComponent > &wfc_list, const RefVectorWithLeader< ParticleSet > &p_list, const std::vector< bool > &recompute) const override
Definition: SlaterDet.cpp:188
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 override
evaluate gradients and laplacian of the same type WaveFunctionComponent of multiple walkers ...
Definition: SlaterDet.cpp:162
std::complex< double > LogValue
SlaterDet(ParticleSet &targetPtcl, std::vector< std::unique_ptr< Determinant_t >> dets, const std::string &class_name="SlaterDet")
constructor
Definition: SlaterDet.cpp:27
LogValue evaluateLog(const ParticleSet &P, ParticleSet::ParticleGradient &G, ParticleSet::ParticleLaplacian &L) override
evaluate the value of the WaveFunctionComponent from scratch
Definition: SlaterDet.cpp:122
void copyFromBuffer(ParticleSet &P, WFBufferType &buf) override
For particle-by-particle move.
Definition: SlaterDet.cpp:259
GradType evalGrad(ParticleSet &P, int iat) override
return the current gradient for the iat-th particle
Definition: SlaterDet.h:145
void acceptMove(ParticleSet &P, int iat, bool safe_to_delay=false) override
a move for iat-th particle is accepted.
Definition: SlaterDet.h:189
void mw_evalGrad(const RefVectorWithLeader< WaveFunctionComponent > &wfc_list, const RefVectorWithLeader< ParticleSet > &p_list, int iat, std::vector< GradType > &grad_now) const override
compute the current gradients for the iat-th particle of multiple walkers
Definition: SlaterDet.h:152
void mw_calcRatio(const RefVectorWithLeader< WaveFunctionComponent > &wfc_list, const RefVectorWithLeader< ParticleSet > &p_list, int iat, std::vector< PsiValue > &ratios) const override
compute the ratio of the new to old WaveFunctionComponent value of multiple walkers ...
Definition: SlaterDet.h:240