QMCPACK
MultiSlaterDetTableMethod.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) 2016 Jeongnim Kim and QMCPACK developers.
6 //
7 // File developed by: Miguel Morales, moralessilva2@llnl.gov, Lawrence Livermore National Laboratory
8 // Jeongnim Kim, jeongnim.kim@gmail.com, University of Illinois at Urbana-Champaign
9 // Jeremy McMinnis, jmcminis@gmail.com, University of Illinois at Urbana-Champaign
10 // Mark A. Berrill, berrillma@ornl.gov, Oak Ridge National Laboratory
11 //
12 // File created by: Miguel Morales, moralessilva2@llnl.gov, Lawrence Livermore National Laboratory
13 //////////////////////////////////////////////////////////////////////////////////////
14 
15 
16 #ifndef QMCPLUSPLUS_MULTISLATERDETERMINANTFAST_ORBITAL_H
17 #define QMCPLUSPLUS_MULTISLATERDETERMINANTFAST_ORBITAL_H
18 #include <Configuration.h>
21 #include "Utilities/TimerManager.h"
24 #include "ResourceCollection.h"
25 
26 namespace qmcplusplus
27 {
28 /// CSF related dataset
29 struct CSFData
30 {
33 
34  // coefficients of csfs, these are only used during optm
35  std::vector<ValueType> coeffs;
36  // number of dets per csf
37  std::vector<size_t> dets_per_csf;
38  // coefficient of csf expansion (smaller dimension)
39  std::vector<RealType> expansion;
40 };
41 
42 /** @ingroup WaveFunctionComponent
43  * @brief An AntiSymmetric WaveFunctionComponent composed of a linear combination of SlaterDeterminants.
44  *
45  *\f[
46  *MSD({\bf R}) = \sum_n c_n S_n({\bf R})
47  *\f].
48  *
49  *The (S)ingle(P)article(O)rbitalSet template parameter is an
50  *engine which fills in single-particle orbital terms.
51  *
52  \f[
53  \frac{\nabla_i\Psi}{\Psi} = \frac{\sum_{n=1}^M c_n \nabla_i D_n}
54  {\sum_{n=1}^M c_n D_n}
55  \f]
56  \f[
57  \frac{\sum_{n=1}^M c_n S_n(\sum_{j=1}^N(\nabla_i
58  S^{ij}_n({\bf r_i}))(S^{-1})^{ji}_n}{\sum_{n=1}^M c_n S_n}
59  \f]
60  The Laplacian
61  \f[
62  \frac{\nabla^2_i\Psi}{\Psi} = \frac{\sum_{n=1}^M c_n S_n(\sum_{j=1}^N
63  (\nabla_i^2S^{ij}_n({\bf r_i}))(S^{-1})^{ji}_n}{\sum_{n=1}^M c_n S_n}
64  \f]
65  */
67 {
68 public:
72 
73  template<typename DT>
75 
76  using SPOSetPtr = SPOSet*;
88 
89 
90  ///constructor
92  std::vector<std::unique_ptr<MultiDiracDeterminant>>&& dets,
93  bool use_pre_computing);
94 
95  ///destructor
96  ~MultiSlaterDetTableMethod() override;
97 
98  std::string getClassName() const override { return "MultiSlaterDetTableMethod"; }
99  bool isFermionic() const final { return true; }
100  bool isMultiDet() const final { return true; }
101  bool isOptimizable() const override { return true; }
102  void extractOptimizableObjectRefs(UniqueOptObjRefs& opt_obj_refs) override;
103  void checkOutVariables(const opt_variables_type& active) override;
104  void checkInVariablesExclusive(opt_variables_type& active) override;
105  void resetParametersExclusive(const opt_variables_type& active) override;
106 
107  //builds orbital rotation parameters using MultiSlater member variables
108  void buildOptVariables();
109 
113 
116  ParticleSet::ParticleLaplacian& L) override;
117 
118  /** Compute ratios of the individual Slater determinants and the total MSD value.
119  *\f[
120  *ratio_n = Det_n / \frac{\sum_n c_n Det_n}
121  *\f].
122  */
124 
125  const std::vector<ValueType>& getLinearExpansionCoefs() const;
126 
127  void prepareGroup(ParticleSet& P, int ig) override;
128 
130  const RefVectorWithLeader<ParticleSet>& p_list,
131  int ig) const override;
132 
133  GradType evalGrad(ParticleSet& P, int iat) override;
134  //evalGrad, but returns the spin gradient as well
135  GradType evalGradWithSpin(ParticleSet& P, int iat, ComplexType& spingrad) override;
136 
138  const RefVectorWithLeader<ParticleSet>& P_list,
139  int iat,
140  std::vector<GradType>& grad_now) const override;
141 
143  const RefVectorWithLeader<ParticleSet>& P_list,
144  int iat,
145  std::vector<PsiValue>& ratios,
146  std::vector<GradType>& grad_new) const override;
147 
149  const RefVectorWithLeader<ParticleSet>& P_list,
150  int iat,
151  std::vector<PsiValue>& ratios) const override;
152 
153  PsiValue ratio(ParticleSet& P, int iat) override;
154  PsiValue ratioGrad(ParticleSet& P, int iat, GradType& grad_iat) override;
155  //ratioGradWithSpin, but includes tthe spin gradient info
156  PsiValue ratioGradWithSpin(ParticleSet& P, int iat, GradType& grad_iat, ComplexType& spingrad_iat) override;
157 
158  void evaluateRatios(const VirtualParticleSet& VP, std::vector<ValueType>& ratios) override;
159 
160  void evaluateRatiosAlltoOne(ParticleSet& P, std::vector<ValueType>& ratios) override
161  {
162  // the base class routine may probably work, just never tested.
163  // it can also be highly optimized with a specialized implementation.
164  throw std::runtime_error(" Need to implement MultiSlaterDetTableMethod::evaluateRatiosAlltoOne. \n");
165  }
166 
167  void acceptMove(ParticleSet& P, int iat, bool safe_to_delay = false) override;
168  void restore(int iat) override;
169 
171  const RefVectorWithLeader<ParticleSet>& p_list,
172  int iat,
173  const std::vector<bool>& isAccepted,
174  bool safe_to_delay = false) const override;
175 
176  void registerData(ParticleSet& P, WFBufferType& buf) override;
177  LogValue updateBuffer(ParticleSet& P, WFBufferType& buf, bool fromscratch = false) override;
178  void copyFromBuffer(ParticleSet& P, WFBufferType& buf) override;
179 
180  void createResource(ResourceCollection& collection) const override;
181  void acquireResource(ResourceCollection& collection,
182  const RefVectorWithLeader<WaveFunctionComponent>& wfc_list) const override;
183  void releaseResource(ResourceCollection& collection,
184  const RefVectorWithLeader<WaveFunctionComponent>& wfc_list) const override;
185 
186  std::unique_ptr<WaveFunctionComponent> makeClone(ParticleSet& tqp) const override;
188  const opt_variables_type& optvars,
189  Vector<ValueType>& dlogpsi,
190  Vector<ValueType>& dhpsioverpsi) override;
191 
192  void evaluateDerivativesWF(ParticleSet& P, const opt_variables_type& optvars, Vector<ValueType>& dlogpsi) override;
193 
195  const opt_variables_type& optvars,
196  std::vector<ValueType>& ratios,
197  Matrix<ValueType>& dratios) override;
198 
199  /** initialize a few objects and states by the builder
200  * YL: it should be part of the constructor. It cannot be added to the constructor
201  * because the constructor is used by makeClone. The right way of fix needs:
202  * 1. implement a copy constructor and reroute makeClone to it.
203  * 2. merge initialize() into the constructor.
204  */
205  void initialize(std::unique_ptr<std::vector<std::vector<size_t>>> C2node_in,
206  std::unique_ptr<std::vector<ValueType>> C_in,
207  std::unique_ptr<opt_variables_type> myVars_in,
208  std::unique_ptr<CSFData> csf_data_in,
209  bool optimizable,
210  bool CI_optimizable);
211 
212 private:
213  //get Det ID. It should be consistent with particle group id within the particle set.
214  inline int getDetID(const int iat) const
215  {
216  int id = 0;
217  while (iat > Last[id])
218  id++;
219  return id;
220  }
221 
222  /** an implementation shared by evalGrad and ratioGrad. Use precomputed data
223  * @param newpos to distinguish evalGrad(false) ratioGrad(true)
224  */
225  PsiValue evalGrad_impl(ParticleSet& P, int iat, bool newpos, GradType& g_at);
226  /// multi walker version of evalGrad_impl
228  const RefVectorWithLeader<ParticleSet>& P_list,
229  int iat,
230  bool newpos,
231  std::vector<GradType>& grad_now,
232  std::vector<PsiValue>& psi_list);
233 
234  /** an implementation shared by evalGrad and ratioGrad. No use of precomputed data
235  * @param newpos to distinguish evalGrad(false) ratioGrad(true)
236  */
237  PsiValue evalGrad_impl_no_precompute(ParticleSet& P, int iat, bool newpos, GradType& g_at);
238 
239  //implemtation for evalGradWithSpin
240  PsiValue evalGradWithSpin_impl(ParticleSet& P, int iat, bool newpos, GradType& g_at, ComplexType& sg_at);
241  //implemtation for evalGradWithSpin with no precomputation
243  int iat,
244  bool newpos,
245  GradType& g_at,
246  ComplexType& sg_at);
247 
248  // compute the new multi determinant to reference determinant ratio based on temporarycoordinates.
250 
251  /** precompute C_otherDs for a given particle group
252  * @param P a particle set
253  * @param ig group id
254  */
255  void precomputeC_otherDs(const ParticleSet& P, int ig);
256 
258  const opt_variables_type& optvars,
259  Vector<ValueType>& dlogpsi,
260  Vector<ValueType>& dhpsioverpsi);
261 
263  const opt_variables_type& optvars,
264  Vector<ValueType>& dlogpsi);
265 
266  /** compute parameter derivatives of CI/CSF coefficients
267  * @param dlogpsi saved derivatives
268  * @param move when one electron virtual move was proposed, pass the determinant group of the moved electron and
269  * the ratio of multideterminant and the reference single determinant
270  */
272  std::optional<std::pair<unsigned, PsiValue>> move = std::nullopt) const;
273 
274  /// determinant collection
275  std::vector<std::unique_ptr<MultiDiracDeterminant>> Dets;
276 
277  /** map determinant in linear combination to unique det list
278  * map global det id to unique det id. [spin, global det id] = unique det id
279  */
280  std::shared_ptr<std::vector<std::vector<size_t>>> C2node;
281  /// CI coefficients
282  std::shared_ptr<std::vector<ValueType>> C;
283  /// if true, the CI coefficients are optimized
285  //optimizable variable is shared with the clones
286  std::shared_ptr<opt_variables_type> myVars;
287 
288  /// CSF data set. If nullptr, not using CSF
289  std::shared_ptr<CSFData> csf_data_;
290 
291  ///the last particle of each group
292  std::vector<int> Last;
293  ///use pre-compute (fast) algorithm
294  const bool use_pre_computing_;
295 
296  /// current psi over ref single det
298  /// new psi over new ref single det when one particle is moved
300 
301  size_t ActiveSpin;
303 
304  /// C_n x D^1_n x D^2_n ... D^3_n with one D removed. Summed by group. [spin, unique det id]
305  //std::vector<Vector<ValueType, OffloadPinnedAllocator<ValueType>>> C_otherDs;
306  std::vector<OffloadVector<ValueType>> C_otherDs;
307 
310  std::vector<ValueVector> laplSum;
311 
312  // temporary storage for evaluateDerivatives
314  std::vector<Matrix<RealType>> dpsia, dLa;
315  std::vector<Array<GradType, OHMMS_DIM>> dGa;
316 
319 
320  // helper function for extracting a list of WaveFunctionComponent from a list of TrialWaveFunction
323  int det_id) const
324  {
326  *wfc_list.getCastedLeader<MultiSlaterDetTableMethod>().Dets[det_id]);
327  det_list.reserve(wfc_list.size());
328  for (WaveFunctionComponent& wfc : wfc_list)
329  det_list.push_back(*static_cast<MultiSlaterDetTableMethod&>(wfc).Dets[det_id]);
330  return det_list;
331  }
332 };
333 
334 } // namespace qmcplusplus
335 #endif
const std::vector< ValueType > & getLinearExpansionCoefs() const
base class for Single-particle orbital sets
Definition: SPOSet.h:46
std::shared_ptr< CSFData > csf_data_
CSF data set. If nullptr, not using CSF.
std::vector< Matrix< RealType > > dpsia
Walker< QMCTraits, PtclOnLatticeTraits > Walker_t
walker type
Definition: ParticleSet.h:59
Fixed-size array.
Definition: OhmmsTinyMeta.h:30
OrbitalSetTraits< ValueType >::ValueVector ValueVector
WaveFunctionComponent::PsiValue PsiValue
Definition: SlaterDet.cpp:25
std::vector< RealType > expansion
helper functions for EinsplineSetBuilder
Definition: Configuration.h:43
void registerData(ParticleSet &P, WFBufferType &buf) override
For particle-by-particle move.
timer_manager class.
bool CI_Optimizable
if true, the CI coefficients are optimized
void evaluateMultiDiracDeterminantDerivatives(ParticleSet &P, const opt_variables_type &optvars, Vector< ValueType > &dlogpsi, Vector< ValueType > &dhpsioverpsi)
QTBase::RealType RealType
Definition: Configuration.h:58
MultiSlaterDetTableMethod(ParticleSet &targetPtcl, std::vector< std::unique_ptr< MultiDiracDeterminant >> &&dets, bool use_pre_computing)
constructor
void evaluateDerivatives(ParticleSet &P, const opt_variables_type &optvars, Vector< ValueType > &dlogpsi, Vector< ValueType > &dhpsioverpsi) override
std::vector< ValueType > coeffs
std::shared_ptr< opt_variables_type > myVars
void mw_calcRatio(const RefVectorWithLeader< WaveFunctionComponent > &WFC_list, const RefVectorWithLeader< ParticleSet > &P_list, int iat, std::vector< PsiValue > &ratios) const override
std::shared_ptr< std::vector< ValueType > > C
CI coefficients.
ResourceHandle manages the temporary resource referenced from a collection.
A ParticleSet that handles virtual moves of a selected particle of a given physical ParticleSet Virtu...
PsiValue evalGrad_impl_no_precompute(ParticleSet &P, int iat, bool newpos, GradType &g_at)
an implementation shared by evalGrad and ratioGrad.
LogValue evaluateLog(const ParticleSet &P, ParticleSet::ParticleGradient &G, ParticleSet::ParticleLaplacian &L) override
evaluate the value of the WaveFunctionComponent from scratch
LatticeGaussianProduct::GradType GradType
void extractOptimizableObjectRefs(UniqueOptObjRefs &opt_obj_refs) override
extract underlying OptimizableObject references
Timer accumulates time and call counts.
Definition: NewTimer.h:135
void precomputeC_otherDs(const ParticleSet &P, int ig)
precompute C_otherDs for a given particle group
WaveFunctionComponent::ValueType ValueType
void evaluateDerivativesMSD(Vector< ValueType > &dlogpsi, std::optional< std::pair< unsigned, PsiValue >> move=std::nullopt) const
compute parameter derivatives of CI/CSF coefficients
std::unique_ptr< WaveFunctionComponent > makeClone(ParticleSet &tqp) const override
make clone
Attaches a unit to a Vector for IO.
void evaluateMultiDiracDeterminantDerivativesWF(ParticleSet &P, const opt_variables_type &optvars, Vector< ValueType > &dlogpsi)
void resetParametersExclusive(const opt_variables_type &active) override
reset the parameters during optimizations.
OrbitalSetTraits< ValueType >::HessType HessType
void releaseResource(ResourceCollection &collection, const RefVectorWithLeader< WaveFunctionComponent > &wfc_list) const override
return a shared resource to a collection
RefVectorWithLeader< MultiDiracDeterminant > extract_DetRef_list(const RefVectorWithLeader< WaveFunctionComponent > &wfc_list, int det_id) const
void prepareGroup(ParticleSet &P, int ig) override
Prepare internal data for updating WFC correspond to a particle group It should be called before movi...
void restore(int iat) override
If a move for iat-th particle is rejected, restore to the content.
An abstract class for a component of a many-body trial wave function.
OrbitalSetTraits< ValueType >::ValueMatrix ValueMatrix
OrbitalSetTraits< ValueType >::HessMatrix HessMatrix
Specialized paritlce class for atomistic simulations.
Definition: ParticleSet.h:55
PsiValue evalGradWithSpin_impl_no_precompute(ParticleSet &P, int iat, bool newpos, GradType &g_at, ComplexType &sg_at)
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...
void calcIndividualDetRatios(Vector< ValueType > &ratios)
Compute ratios of the individual Slater determinants and the total MSD value.
QTBase::ValueType ValueType
Definition: Configuration.h:60
static void mw_evalGrad_impl(const RefVectorWithLeader< WaveFunctionComponent > &WFC_list, const RefVectorWithLeader< ParticleSet > &P_list, int iat, bool newpos, std::vector< GradType > &grad_now, std::vector< PsiValue > &psi_list)
multi walker version of evalGrad_impl
std::vector< OffloadVector< ValueType > > C_otherDs
C_n x D^1_n x D^2_n ... D^3_n with one D removed. Summed by group. [spin, unique det id]...
std::vector< std::unique_ptr< MultiDiracDeterminant > > Dets
determinant collection
void createResource(ResourceCollection &collection) const override
initialize a shared resource and hand it to a collection
PsiValue evalGradWithSpin_impl(ParticleSet &P, int iat, bool newpos, GradType &g_at, ComplexType &sg_at)
std::vector< int > Last
the last particle of each group
std::vector< Array< GradType, OHMMS_DIM > > dGa
class to handle a set of variables that can be modified during optimizations
Definition: VariableSet.h:49
std::shared_ptr< std::vector< std::vector< size_t > > > C2node
map determinant in linear combination to unique det list map global det id to unique det id...
void initialize(std::unique_ptr< std::vector< std::vector< size_t >>> C2node_in, std::unique_ptr< std::vector< ValueType >> C_in, std::unique_ptr< opt_variables_type > myVars_in, std::unique_ptr< CSFData > csf_data_in, bool optimizable, bool CI_optimizable)
initialize a few objects and states by the builder YL: it should be part of the constructor.
bool isOptimizable() const override
if true, this contains optimizable components
GradType evalGrad(ParticleSet &P, int iat) override
return the current gradient for the iat-th particle
void evaluateDerivRatios(const VirtualParticleSet &VP, const opt_variables_type &optvars, std::vector< ValueType > &ratios, Matrix< ValueType > &dratios) override
bool isMultiDet() const final
true, if this component is multi-determinant
const bool use_pre_computing_
use pre-compute (fast) algorithm
OrbitalSetTraits< ValueType >::GradVector GradVector
LogValue updateBuffer(ParticleSet &P, WFBufferType &buf, bool fromscratch=false) override
For particle-by-particle move.
PsiValue psi_ratio_to_ref_det_
current psi over ref single det
ResourceHandle< MultiSlaterDetTableMethodMultiWalkerResource > mw_res_handle_
void acquireResource(ResourceCollection &collection, const RefVectorWithLeader< WaveFunctionComponent > &wfc_list) const override
acquire a shared resource from a collection
void evaluateRatiosAlltoOne(ParticleSet &P, std::vector< ValueType > &ratios) override
void evaluateDerivativesWF(ParticleSet &P, const opt_variables_type &optvars, Vector< ValueType > &dlogpsi) override
PsiValue evalGrad_impl(ParticleSet &P, int iat, bool newpos, GradType &g_at)
an implementation shared by evalGrad and ratioGrad.
~MultiSlaterDetTableMethod() override
destructor
std::vector< size_t > dets_per_csf
GradType evalGradWithSpin(ParticleSet &P, int iat, ComplexType &spingrad) override
return the current spin gradient for the iat-th particle Default implementation assumes that WaveFunc...
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
void evaluateRatios(const VirtualParticleSet &VP, std::vector< ValueType > &ratios) override
WaveFunctionComponent::RealType RealType
QMCTraits::ComplexType ComplexType
PsiValue ratioGrad(ParticleSet &P, int iat, GradType &grad_iat) override
evaluate the ratio of the new to old WaveFunctionComponent value and the new gradient ...
void checkOutVariables(const opt_variables_type &active) override
check out variational optimizable variables
PsiValue ratio(ParticleSet &P, int iat) override
evaluate the ratio of the new to old WaveFunctionComponent value
Declaration of WaveFunctionComponent.
std::complex< double > LogValue
void mw_prepareGroup(const RefVectorWithLeader< WaveFunctionComponent > &wfc_list, const RefVectorWithLeader< ParticleSet > &p_list, int ig) const override
trait class to handel a set of Orbitals
void mw_ratioGrad(const RefVectorWithLeader< WaveFunctionComponent > &WFC_list, const RefVectorWithLeader< ParticleSet > &P_list, int iat, std::vector< PsiValue > &ratios, std::vector< GradType > &grad_new) const override
PsiValue computeRatio_NewMultiDet_to_NewRefDet(int det_id) const
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.
OrbitalSetTraits< ValueType >::IndexVector IndexVector
A container class to represent a walker.
Definition: Walker.h:49
LogValue evaluate_vgl_impl(const ParticleSet &P, ParticleSet::ParticleGradient &g_tmp, ParticleSet::ParticleLaplacian &l_tmp)
Compute VGL of this MultiSlaterDetTableMethod.
void copyFromBuffer(ParticleSet &P, WFBufferType &buf) override
For particle-by-particle move.
PsiValue new_psi_ratio_to_new_ref_det_
new psi over new ref single det when one particle is moved
An AntiSymmetric WaveFunctionComponent composed of a linear combination of SlaterDeterminants.
void acceptMove(ParticleSet &P, int iat, bool safe_to_delay=false) override
a move for iat-th particle is accepted.
void checkInVariablesExclusive(opt_variables_type &active) override
check in variational parameters to the global list of parameters used by the optimizer.
bool isFermionic() const final
true, if this component is fermionic
std::string getClassName() const override
return class name