QMCPACK
TWFFastDerivWrapper.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) 2021 QMCPACK developers.
6 //
7 // File developed by: Raymond Clay III, rclay@sandia.gov, Sandia National Laboratories
8 //
9 // File created by: Raymond Clay III, rclay@sandia.gov, Sandia National Laboratories
10 //////////////////////////////////////////////////////////////////////////////////////
11 
12 
13 #ifndef QMCPLUSPLUS_TWFFASTDERIVWRAPPER_H
14 #define QMCPLUSPLUS_TWFFASTDERIVWRAPPER_H
15 
18 #include "Configuration.h"
19 #include "Particle/ParticleSet.h"
20 namespace qmcplusplus
21 {
22 /**
23  * TWFFastDerivWrapper is a wrapper class for TrialWavefunction that provides separate and low level access to the Jastrow and
24  * SPOSet objects. This is so that observables can be recast in matrix form and their derivatives taken efficiently.
25  * Currently this is hard coded for ground state slater jastrow wave functions, but generalization to
26  * arbitrary occupations and multideterminants are straightforward and will come online as the code is tested and validated.
27  *
28  * Please see : J. Chem. Phys. 144, 194105 (2016) https://doi.org/10.1063/1.4948778 for implementation details and formalism.
29  */
31 {
32 public:
42 
43  TWFFastDerivWrapper() = default;
44  /** @brief Add a particle group.
45  *
46  * Here, a "group" corresponds to a subset of particles which are antisymmetric with
47  * respect to eachother. TWFFastDerivWrapper ensures that there is a binding between the groupid
48  * in ParticleSet and the SPOSet associated with that particle group. This function stores
49  * the ParticleSet groupid and SPOSet in a vector for lookup and communication with QMCPACK conventions,
50  * but is agnostic to the order of group registration or evaluation.
51  *
52  * @param[in] P. ParticleSet
53  * @param[in] groupid. ParticleSet groupid to which SPOSet corresponds.
54  * @param[in] spo. Pointer to SPOSet.
55  * @return void.
56  */
57  void addGroup(const ParticleSet& P, const IndexType groupid, SPOSet* spo);
58  inline void addJastrow(WaveFunctionComponent* j) { jastrow_list_.push_back(j); };
59 
60  /** @brief Takes particle set groupID and returns the TWF internal index for it.
61  *
62  * ParticleSet groups can be registered in whichever order. However, the internal indexing
63  * of TWFFastDerivWrapper keeps track on a first-come, first serve basis. That is, if I register
64  * particle groups 3, 1, and 0 in that order, then the internal indexing goes like
65  * 0->3, 1->1, 2->0. Thus, this function looks up where in the internal indexing scheme
66  * ParticleSet gid is located. This is necessary, since the matrix list data structures follow
67  * the internal TWF indexing.
68  *
69  * @param[in] gid. ParticleSet group ID to look up.
70  * @return The corresponding internal groupID.
71  */
72  IndexType getTWFGroupIndex(const IndexType gid) const;
73 
74  /** @ingroup Query functions
75  * @{
76  * @brief These are query functions to the internal state of various groups and SPOSet info.
77  * All group indices will refer to the internal group indices. Convert from ParticleSet to
78  * TWF group first!
79  *
80  * Source of truth for orbital sizes will be the individual SPOSets. Particle group sizes
81  * will be ParticleSet in conjunction with groupID maps.
82  */
83  inline IndexType numGroups() const { return spos_.size(); };
84  SPOSet* getSPOSet(const IndexType sid) const { return spos_[sid]; };
85  inline IndexType numOrbitals(const IndexType sid) const { return spos_[sid]->size(); };
86  /** @} */
87 
88  //////////////////////////////////////////////////////////////////////////////////////////////////////////
89  //These are convenience functions/wrappers to SPOSet calls. Idea being that observables just need //
90  //to make calls to this object to build the auxiliary matrices required for fast derivative computation.//
91  //On the other hand, it wouldn't be unreasonable to make the observables do the SPOSet calls themselves.//
92  //////////////////////////////////////////////////////////////////////////////////////////////////////////
93 
94  /** @brief Returns the non-rectangular slater matrices M_ij=phi_j(r_i) (N_particle x N_orbs) for each species group.
95  *
96  * @param[in] P particle set.
97  * @param[in,out] mmat Output vector of slater matrices. Each vector entry corresponds to a different particle group.
98  * @return Void
99  */
100  void getM(const ParticleSet& P, std::vector<ValueMatrix>& mmat) const;
101 
102  /** @brief Returns value of all orbitals (relevant to given species/group) at a particular particle coordinate.
103  *
104  * @param[in] P particle set.
105  * @param[in] iel particle ID.
106  * @param[in,out] val Vector of phi_i(r_iel) for all i=0,Norbs.
107  * @return Void
108  */
109  IndexType getRowM(const ParticleSet& P, const IndexType iel, ValueVector& val) const;
110 
111  /** @brief Returns value, gradient, and laplacian matrices for all orbitals and all particles, species by species.
112  *
113  * @param[in] P particle set.
114  * @param[in,out] mvec Slater matrix M_ij=phi_j(r_i) for each species group.
115  * @param[in,out] gmat electron gradient of slater matrix [G_ij]_a = d/dr_a,i phi_j(r_i). a=x,y,z
116  * @param[in,out] lmat electron laplacian of slater matrix [L_ij] = \nabla^2_i phi_j(r_i).
117  * @return Void
118  */
119  void getEGradELaplM(const ParticleSet& P,
120  std::vector<ValueMatrix>& mvec,
121  std::vector<GradMatrix>& gmat,
122  std::vector<ValueMatrix>& lmat) const;
123 
124  /** @brief Returns x,y,z components of ion gradient of slater matrices.
125  *
126  * @param[in] P particle set.
127  * @param[in] source ion particle set.
128  * @param[in]*** iat ion ID w.r.t. which to take derivative.
129  * @param[in,out] dmvec Slater matrix d/dR_{iat,a} M_ij=d/dR_{iat,a} phi_j(r_i) for each species group. First index is a=x,y,z.
130  * @return Void
131  */
132  void getIonGradM(const ParticleSet& P,
133  const ParticleSet& source,
134  const int iat,
135  std::vector<std::vector<ValueMatrix>>& dmvec) const;
136 
137  /** @brief Returns x,y,z components of ion gradient of slater matrices and their laplacians..
138  *
139  * @param[in] P particle set.
140  * @param[in] source ion particle set.
141  * @param[in] iat ion ID w.r.t. which to take derivative.
142  * @param[in,out] dmvec Slater matrix d/dR_{iat,a} M_ij=d/dR_{iat,a} phi_j(r_i) for each species group.
143  * First index is a=x,y,z.
144  * @param[in,out] dlmat Slater matrix d/dR_{iat,a} L_ij=d/dR_{iat,a} \nabla^2_i phi_j(r_i) for each species group.
145  * First index is a=x,y,z.
146  * @return Void
147  */
148  void getIonGradIonGradELaplM(const ParticleSet& P,
149  const ParticleSet& source,
150  int iat,
151  std::vector<std::vector<ValueMatrix>>& dmvec,
152  std::vector<std::vector<GradMatrix>>& dgmat,
153  std::vector<std::vector<ValueMatrix>>& dlmat) const;
154 
155 
156  /** @brief Evaluates value, gradient, and laplacian of the total jastrow. So of J in exp(J).
157  *
158  * @param[in] Particle set.
159  * @param[in,out] Electron gradients.
160  * @param[in,out] Electron laplacians.
161  * @return Jastrow value
162  */
166 
167  /** @brief Given a proposed move of electron iel from r->r', computes exp(J')/exp(J)
168  *
169  * @param[in] P electron particle set.
170  * @param[in] iel electron being moved.
171  * @return Jastrow ratio.
172  */
173  RealType evaluateJastrowRatio(ParticleSet& P, const int iel) const;
174 
175  /** @brief Given a proposed move of electron iel from r->r', computes exp(J(r'))/exp(J(r))
176  * and grad_iel(J(r')).)
177  *
178  * @param[in] P electron particle set.
179  * @param[in] iel electron being moved.
180  * @param[in,out] grad grad_iel(J(r')). So iel electron gradient at new position.
181  * @return Jastrow ratio.
182  */
183  RealType calcJastrowRatioGrad(ParticleSet& P, const int iel, GradType& grad) const;
184 
185  /** @brief Return ionic gradient of J(r).
186  *
187  * @param[in] P electron particle set.
188  * @param[in] source ion particle set.
189  * @param[in] iat Ion to take derivative w.r.t.
190  * @return Gradient of J(r) w.r.t. ion iat.
191  */
192  GradType evaluateJastrowGradSource(ParticleSet& P, ParticleSet& source, const int iat) const;
193 
194  /** @brief Return ionic gradients of J(r), grad_iel(J(r)), and lapl_iel(J(r)).
195  *
196  * @param[in] P electron particle set.
197  * @param[in] source ion particle set.
198  * @param[in] iat Ion to take derivative w.r.t.
199  * @param[in,out] grad_grad iat ion gradient of electron gradients of J(r).
200  * @param[in,out] grad_lapl iat ion gradient of electron laplacians of J(r).
201  * @return Gradient of J(r) w.r.t. ion iat.
202  */
204  ParticleSet& source,
205  const int iat,
208 
209  /** @brief Takes sub matrices of full SPOSet quantities (evaluated on all particles and all orbitals), consistent with ground
210  * state occupations.
211  *
212  * @param[in] A non-rectangular matrices of SPOSet derived quantities, evaluated on all orbitals and all particles.
213  * @param[in,out] Aslice square matrices consistent with a ground state occupation.
214  * @return Void
215  */
216  void getGSMatrices(const std::vector<ValueMatrix>& A, std::vector<ValueMatrix>& Aslice) const;
217 
218  /** @brief Calculates derivative of observable via Tr[M^{-1} dB - X * dM ]. Consistent with ground state occupation.
219  *
220  * @param[in] Minv. inverse of slater matrices for ground state occupations.
221  * @param[in] X. X=M^-1 B M^-1. B observable matrix, and is consistent with ground state occupation.
222  * @param[in] dM. Target derivative of M, and is consistent with ground state occupation.
223  * @param[in] dB. Target derivative of B, and is consistent with ground state occupation.
224  * @return Derivative of O psi/psi = Tr[M^{-1} dB - X * dM ]
225  */
226  ValueType computeGSDerivative(const std::vector<ValueMatrix>& Minv,
227  const std::vector<ValueMatrix>& X,
228  const std::vector<ValueMatrix>& dM,
229  const std::vector<ValueMatrix>& dB) const;
230 
231  //////////////////////////////////////////////////////////////////////////////////////////////
232  //And now we just have some helper functions for doing useful math on our lists of matrices.//
233  //////////////////////////////////////////////////////////////////////////////////////////////
234 
235  /** @brief Helper function that inverts all slater matrices in our species list.
236  *
237  * @param[in] M. List of slater matrices for each species. These are square and consistent with some occupation.
238  * @param[in,out] Minv. The species by species list of inverted matrices from M.
239  * @return Void.
240  */
241  void invertMatrices(const std::vector<ValueMatrix>& M, std::vector<ValueMatrix>& Minv);
242 
243  /** @brief Build the auxiliary X=M^-1 B M^-1 matrix.
244  *
245  * @param[in] Minv. List of slater matrix inverses M^-1 for a given occupation.
246  * @param[in] B. Observable auxiliary matrix for a given occupation.
247  * @param[in,out] X. M^-1*B*M^-1 is stored in this list of matrices.
248  * @return Void.
249  */
250  void buildX(const std::vector<ValueMatrix>& Minv, const std::vector<ValueMatrix>& B, std::vector<ValueMatrix>& X);
251 
252  /** @brief Goes through a list of matrices and zeros them out. Does this in place.
253  *
254  * @param[in,out] A. The list of matrices to be zeroed out. After call, A is all zeros.
255  * @return Void.
256  */
257  void wipeMatrices(std::vector<ValueMatrix>& A);
258 
259  /** @brief Returns Tr(A*B). Actually, we assume a block diagonal structure, so this is
260  * really Sum_i Tr(A_i*B_i), where i is the species index.
261  *
262  * @param[in] A. Vector of matrices A.
263  * @param[in] B. Vector of matrices B.
264  * @return Value of Sum_i Tr(A_i*B_i).
265  */
266  ValueType trAB(const std::vector<ValueMatrix>& A, const std::vector<ValueMatrix>& B);
267 
268 
269 private:
270  std::vector<SPOSet*> spos_;
271  std::vector<IndexType> groups_;
272  std::vector<ValueMatrix> psi_M_;
273  std::vector<ValueMatrix> psi_M_inv_;
274  std::vector<WaveFunctionComponent*> jastrow_list_;
275 };
276 
277 /**@}*/
278 } // namespace qmcplusplus
279 #endif
base class for Single-particle orbital sets
Definition: SPOSet.h:46
Fixed-size array.
Definition: OhmmsTinyMeta.h:30
std::vector< WaveFunctionComponent * > jastrow_list_
IndexType numGroups() const
These are query functions to the internal state of various groups and SPOSet info.
helper functions for EinsplineSetBuilder
Definition: Configuration.h:43
QTBase::GradType GradType
Definition: Configuration.h:62
std::vector< ValueMatrix > psi_M_
QTBase::RealType RealType
Definition: Configuration.h:58
ValueType trAB(const std::vector< ValueMatrix > &A, const std::vector< ValueMatrix > &B)
Returns Tr(A*B).
RealType evaluateJastrowRatio(ParticleSet &P, const int iel) const
Given a proposed move of electron iel from r->r&#39;, computes exp(J&#39;)/exp(J)
GradType evaluateJastrowGradSource(ParticleSet &P, ParticleSet &source, const int iat) const
Return ionic gradient of J(r).
OrbitalSetTraits< ValueType >::ValueMatrix ValueMatrix
Definition: SPOSet.h:50
LatticeGaussianProduct::GradType GradType
void getEGradELaplM(const ParticleSet &P, std::vector< ValueMatrix > &mvec, std::vector< GradMatrix > &gmat, std::vector< ValueMatrix > &lmat) const
Returns value, gradient, and laplacian matrices for all orbitals and all particles, species by species.
IndexType numOrbitals(const IndexType sid) const
Attaches a unit to a Vector for IO.
TWFFastDerivWrapper is a wrapper class for TrialWavefunction that provides separate and low level acc...
void getIonGradM(const ParticleSet &P, const ParticleSet &source, const int iat, std::vector< std::vector< ValueMatrix >> &dmvec) const
Returns x,y,z components of ion gradient of slater matrices.
OrbitalSetTraits< ValueType >::ValueVector ValueVector
ValueType computeGSDerivative(const std::vector< ValueMatrix > &Minv, const std::vector< ValueMatrix > &X, const std::vector< ValueMatrix > &dM, const std::vector< ValueMatrix > &dB) const
Calculates derivative of observable via Tr[M^{-1} dB - X * dM ].
void getGSMatrices(const std::vector< ValueMatrix > &A, std::vector< ValueMatrix > &Aslice) const
Takes sub matrices of full SPOSet quantities (evaluated on all particles and all orbitals), consistent with ground state occupations.
QMCTraits::IndexType IndexType
Definition: FSUtilities.h:11
OrbitalSetTraits< ValueType >::GradMatrix GradMatrix
Definition: SPOSet.h:52
An abstract class for a component of a many-body trial wave function.
Specialized paritlce class for atomistic simulations.
Definition: ParticleSet.h:55
RealType calcJastrowRatioGrad(ParticleSet &P, const int iel, GradType &grad) const
Given a proposed move of electron iel from r->r&#39;, computes exp(J(r&#39;))/exp(J(r)) and grad_iel(J(r&#39;))...
QTBase::ValueType ValueType
Definition: Configuration.h:60
OrbitalSetTraits< ValueType >::ValueVector ValueVector
Definition: SPOSet.h:49
void getM(const ParticleSet &P, std::vector< ValueMatrix > &mmat) const
Returns the non-rectangular slater matrices M_ij=phi_j(r_i) (N_particle x N_orbs) for each species gr...
OHMMS_INDEXTYPE IndexType
define other types
Definition: Configuration.h:65
std::vector< IndexType > groups_
OrbitalSetTraits< ValueType >::GradVector GradVector
Definition: SPOSet.h:51
IndexType getRowM(const ParticleSet &P, const IndexType iel, ValueVector &val) const
Returns value of all orbitals (relevant to given species/group) at a particular particle coordinate...
void addJastrow(WaveFunctionComponent *j)
void getIonGradIonGradELaplM(const ParticleSet &P, const ParticleSet &source, int iat, std::vector< std::vector< ValueMatrix >> &dmvec, std::vector< std::vector< GradMatrix >> &dgmat, std::vector< std::vector< ValueMatrix >> &dlmat) const
Returns x,y,z components of ion gradient of slater matrices and their laplacians. ...
void wipeMatrices(std::vector< ValueMatrix > &A)
Goes through a list of matrices and zeros them out.
LatticeGaussianProduct::ValueType ValueType
IndexType getTWFGroupIndex(const IndexType gid) const
Takes particle set groupID and returns the TWF internal index for it.
Declaration of WaveFunctionComponent.
double B(double x, int k, int i, const std::vector< double > &t)
RealType evaluateJastrowVGL(const ParticleSet &P, ParticleSet::ParticleGradient &G, ParticleSet::ParticleLaplacian &L) const
Evaluates value, gradient, and laplacian of the total jastrow.
void buildX(const std::vector< ValueMatrix > &Minv, const std::vector< ValueMatrix > &B, std::vector< ValueMatrix > &X)
Build the auxiliary X=M^-1 B M^-1 matrix.
SPOSet * getSPOSet(const IndexType sid) const
std::vector< ValueMatrix > psi_M_inv_
void invertMatrices(const std::vector< ValueMatrix > &M, std::vector< ValueMatrix > &Minv)
Helper function that inverts all slater matrices in our species list.
void addGroup(const ParticleSet &P, const IndexType groupid, SPOSet *spo)
Add a particle group.
OrbitalSetTraits< ValueType >::HessMatrix HessMatrix
Definition: SPOSet.h:54