QMCPACK
TwoBodyJastrow.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: Jeongnim Kim, jeongnim.kim@intel.com, Intel Corp.
8 // Amrita Mathuriya, amrita.mathuriya@intel.com, Intel Corp.
9 // Ye Luo, yeluo@anl.gov, Argonne National Laboratory
10 //
11 // File created by: Ye Luo, yeluo@anl.gov, Argonne National Laboratory
12 //////////////////////////////////////////////////////////////////////////////////////
13 // -*- C++ -*-
14 #ifndef QMCPLUSPLUS_TWOBODYJASTROW_OMPTARGET_H
15 #define QMCPLUSPLUS_TWOBODYJASTROW_OMPTARGET_H
16 
17 #include <map>
18 #include <numeric>
19 #include "Configuration.h"
20 #if !defined(QMC_BUILD_SANDBOX_ONLY)
22 #endif
23 #include <ResourceHandle.h>
24 #include "Particle/DistanceTable.h"
25 #include "LongRange/StructFact.h"
27 #include "J2KECorrection.h"
28 
29 #include "BsplineFunctor.h"
30 #include "PadeFunctors.h"
31 #include "UserFunctor.h"
32 #include "FakeFunctor.h"
33 
34 namespace qmcplusplus
35 {
36 
37 template<typename T>
38 struct TwoBodyJastrowMultiWalkerMem;
39 
40 /** @ingroup WaveFunctionComponent
41  * @brief Specialization for two-body Jastrow function using multiple functors
42  *
43  * Each pair-type can have distinct function \f$u(r_{ij})\f$.
44  * For electrons, distinct pair correlation functions are used
45  * for spins up-up/down-down and up-down/down-up.
46  *
47  * Based on TwoBodyJastrow.h with these considerations
48  * - DistanceTable using SoA containers
49  * - support mixed precision: FT::real_type != OHMMS_PRECISION
50  * - loops over the groups: elminated PairID
51  * - support simd function
52  * - double the loop counts
53  * - Memory use is O(N).
54  */
55 template<class FT>
57 {
58 public:
59  ///alias FuncType
60  using FuncType = FT;
61  ///type of each component U, dU, d2U;
62  using valT = typename FT::real_type;
63  ///element position type
65  ///use the same container
68 
71 
72 protected:
73  ///number of particles
74  const size_t N;
75  ///number of groups of the target particleset
76  const size_t NumGroups;
77  ///number of spatial dimensions
78  const size_t ndim;
79  ///laplacian prefactor
80  const valT lapfac;
81 
82 private:
83  /// if true use offload
84  const bool use_offload_;
85 
86  /** initialize storage Uat,dUat, d2Uat */
87  void resizeInternalStorage();
88 
89  ///number of particles + padded
90  const size_t N_padded;
91  /// the group_id of each particle
93  ///diff value
95  ///Correction
97  ///\f$Uat[i] = sum_(j) u_{i,j}\f$
99  ///\f$dUat[i] = sum_(j) du_{i,j}\f$
101  ///\f$d2Uat[i] = sum_(j) d2u_{i,j}\f$
108  ///Uniquue J2 set for cleanup
109  std::map<std::string, std::unique_ptr<FT>> J2Unique;
110  ///Container for \f$F[ig*NumGroups+jg]\f$. treat every pointer as a reference.
111  std::vector<FT*> F;
112  /// e-e table ID
113  const int my_table_ID_;
114  // helper for compute J2 Chiesa KE correction
116 
117  /// Map indices from subcomponent variables to component variables
118  std::vector<std::pair<int, int>> OffSet;
120 
121  std::vector<GradDerivVec> gradLogPsi;
122  std::vector<ValueDerivVec> lapLogPsi;
123 
125 
127  {
129  gradLogPsi.resize(myVars.size(), GradDerivVec(N));
130  lapLogPsi.resize(myVars.size(), ValueDerivVec(N));
131  }
132 
133  /// compute G and L from internally stored data
135 
136  /*@{ internal compute engines*/
137  valT computeU(const ParticleSet& P, int iat, const DistRow& dist);
138 
139  void computeU3(const ParticleSet& P,
140  int iat,
141  const DistRow& dist,
142  RealType* restrict u,
143  RealType* restrict du,
144  RealType* restrict d2u,
145  bool triangle = false);
146 
147  /** compute gradient
148  */
149  posT accumulateG(const valT* restrict du, const DisplRow& displ) const;
150  /**@} */
151 
152 public:
153  TwoBodyJastrow(const std::string& obj_name, ParticleSet& p, bool use_offload);
154  TwoBodyJastrow(const TwoBodyJastrow& rhs) = delete;
155  ~TwoBodyJastrow() override;
156 
157  /** add functor for (ia,ib) pair */
158  void addFunc(int ia, int ib, std::unique_ptr<FT> j);
159 
160  void checkSanity() const override;
161 
162  void createResource(ResourceCollection& collection) const override;
163 
164  void acquireResource(ResourceCollection& collection,
165  const RefVectorWithLeader<WaveFunctionComponent>& wfc_list) const override;
166 
167  void releaseResource(ResourceCollection& collection,
168  const RefVectorWithLeader<WaveFunctionComponent>& wfc_list) const override;
169 
170  std::string getClassName() const override { return "TwoBodyJastrow"; }
171  bool isOptimizable() const override { return true; }
172  void extractOptimizableObjectRefs(UniqueOptObjRefs& opt_obj_refs) override;
173  /** check out optimizable variables
174  */
175  void checkOutVariables(const opt_variables_type& active) override;
176 
178 
179  std::unique_ptr<WaveFunctionComponent> makeClone(ParticleSet& tqp) const override;
180 
183  ParticleSet::ParticleLaplacian& L) override;
185  const RefVectorWithLeader<ParticleSet>& p_list,
187  const RefVector<ParticleSet::ParticleLaplacian>& L_list) const override;
188 
189  void evaluateHessian(ParticleSet& P, HessVector& grad_grad_psi) override;
190 
191  /** recompute internal data assuming distance table is fully ready */
192  void recompute(const ParticleSet& P) override;
194  const RefVectorWithLeader<ParticleSet>& p_list,
195  const std::vector<bool>& recompute) const override;
196 
197  PsiValue ratio(ParticleSet& P, int iat) override;
199  const RefVectorWithLeader<ParticleSet>& p_list,
200  int iat,
201  std::vector<PsiValue>& ratios) const override;
202 
203  void evaluateRatios(const VirtualParticleSet& VP, std::vector<ValueType>& ratios) override;
204  void evaluateRatiosAlltoOne(ParticleSet& P, std::vector<ValueType>& ratios) override;
205 
208  std::vector<std::vector<ValueType>>& ratios) const override;
209 
210  GradType evalGrad(ParticleSet& P, int iat) override;
211 
212  PsiValue ratioGrad(ParticleSet& P, int iat, GradType& grad_iat) override;
214  const RefVectorWithLeader<ParticleSet>& p_list,
215  int iat,
216  std::vector<PsiValue>& ratios,
217  std::vector<GradType>& grad_new) const override;
218 
219  void acceptMove(ParticleSet& P, int iat, bool safe_to_delay = false) override;
221  const RefVectorWithLeader<ParticleSet>& p_list,
222  int iat,
223  const std::vector<bool>& isAccepted,
224  bool safe_to_delay = false) const override;
225 
226  inline void restore(int iat) override {}
227 
228  /** compute G and L after the sweep
229  */
233  bool fromscratch = false) override;
235  const RefVectorWithLeader<ParticleSet>& p_list,
238  bool fromscratch) const override;
239 
240  void registerData(ParticleSet& P, WFBufferType& buf) override;
241 
242  void copyFromBuffer(ParticleSet& P, WFBufferType& buf) override;
243 
244  LogValue updateBuffer(ParticleSet& P, WFBufferType& buf, bool fromscratch = false) override;
245 
247 
248  inline RealType KECorrection() override { return KEcorr; }
249 
250  const std::vector<FT*>& getPairFunctions() const { return F; }
251 
252  // Accessors for unit testing
253  std::pair<int, int> getComponentOffset(int index) { return OffSet.at(index); }
254 
256 
258  const opt_variables_type& active,
259  Vector<ValueType>& dlogpsi,
260  Vector<ValueType>& dhpsioverpsi) override;
261 
262  void evaluateDerivativesWF(ParticleSet& P, const opt_variables_type& active, Vector<ValueType>& dlogpsi) override;
263 
265  const opt_variables_type& optvars,
266  std::vector<ValueType>& ratios,
267  Matrix<ValueType>& dratios) override;
268 };
269 
270 extern template class TwoBodyJastrow<BsplineFunctor<QMCTraits::RealType>>;
271 extern template class TwoBodyJastrow<PadeFunctor<QMCTraits::RealType>>;
272 extern template class TwoBodyJastrow<UserFunctor<QMCTraits::RealType>>;
273 extern template class TwoBodyJastrow<FakeFunctor<QMCTraits::RealType>>;
274 } // namespace qmcplusplus
275 #endif
void resize(size_type n, Type_t val=Type_t())
Resize the container.
Definition: OhmmsVector.h:166
void checkSanity() const override
Validate the internal consistency of the object.
Fixed-size array.
Definition: OhmmsTinyMeta.h:30
std::vector< T, aligned_allocator< T > > aligned_vector
WaveFunctionComponent::PsiValue PsiValue
Definition: SlaterDet.cpp:25
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
helper functions for EinsplineSetBuilder
Definition: Configuration.h:43
void registerData(ParticleSet &P, WFBufferType &buf) override
For particle-by-particle move.
FakeFunctor for testing purpose.
void finalizeOptimization() override
void addFunc(int ia, int ib, std::unique_ptr< FT > j)
add functor for (ia,ib) pair
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...
const std::vector< FT * > & getPairFunctions() const
std::vector< std::pair< int, int > > OffSet
Map indices from subcomponent variables to component variables.
GradType evalGrad(ParticleSet &P, int iat) override
return the current gradient for the iat-th particle
VectorSoaContainer< RealType, DIM > DisplRow
Definition: DistanceTable.h:47
LatticeGaussianProduct::GradType GradType
Vector< valT, aligned_allocator< valT > > d2Uat
SoA adaptor class for Vector<TinyVector<T,D> >
void extractOptimizableObjectRefs(UniqueOptObjRefs &opt_obj_refs) override
extract underlying OptimizableObject references
DistanceTable::DisplRow DisplRow
std::unique_ptr< WaveFunctionComponent > makeClone(ParticleSet &tqp) const override
make clone
Attaches a unit to a Vector for IO.
PsiValue ratioGrad(ParticleSet &P, int iat, GradType &grad_iat) override
evaluate the ratio of the new to old WaveFunctionComponent value and the new gradient ...
const bool use_offload_
if true use offload
void resizeInternalStorage()
initialize storage Uat,dUat, d2Uat
aligned_vector< valT > DistCompressed
std::map< std::string, std::unique_ptr< FT > > J2Unique
Uniquue J2 set for cleanup.
void releaseResource(ResourceCollection &collection, const RefVectorWithLeader< WaveFunctionComponent > &wfc_list) const override
return a shared resource to a collection
aligned_vector< valT > cur_u
std::vector< ValueDerivVec > lapLogPsi
opt_variables_type myVars
list of variables this WaveFunctionComponent handles
std::vector< FT * > F
Container for . treat every pointer as a reference.
void evaluateDerivativesWF(ParticleSet &P, const opt_variables_type &active, Vector< ValueType > &dlogpsi) override
QTFull::RealType computeGL(ParticleSet::ParticleGradient &G, ParticleSet::ParticleLaplacian &L) const
compute G and L from internally stored data
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.
const size_t ndim
number of spatial dimensions
posT accumulateG(const valT *restrict du, const DisplRow &displ) const
compute gradient
Specialized paritlce class for atomistic simulations.
Definition: ParticleSet.h:55
opt_variables_type & getComponentVars()
TwoBodyJastrow(const std::string &obj_name, ParticleSet &p, bool use_offload)
ParticleAttrib< QTFull::GradType > GradDerivVec
void recompute(const ParticleSet &P) override
recompute internal data assuming distance table is fully ready
RealType KECorrection() override
Return the Chiesa kinetic energy correction.
void mw_evaluateRatios(const RefVectorWithLeader< WaveFunctionComponent > &wfc_list, const RefVectorWithLeader< const VirtualParticleSet > &vp_list, std::vector< std::vector< ValueType >> &ratios) const override
aligned_vector< valT > cur_du
aligned_vector< valT > old_du
aligned_vector< valT > cur_d2u
LogValue updateBuffer(ParticleSet &P, WFBufferType &buf, bool fromscratch=false) override
For particle-by-particle move.
DistanceTable::DistRow DistRow
use the same container
void createResource(ResourceCollection &collection) const override
initialize a shared resource and hand it to a collection
ResourceHandle< TwoBodyJastrowMultiWalkerMem< RealType > > mw_mem_handle_
valT computeU(const ParticleSet &P, int iat, const DistRow &dist)
class to handle a set of variables that can be modified during optimizations
Definition: VariableSet.h:49
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 ...
Functors which implement Pade functions.
const int my_table_ID_
e-e table ID
const size_t N_padded
number of particles + padded
bool isOptimizable() const override
if true, this contains optimizable components
typename FT::real_type valT
type of each component U, dU, d2U;
PsiValue ratio(ParticleSet &P, int iat) override
evaluate the ratio of the new to old WaveFunctionComponent value
void evaluateDerivRatios(const VirtualParticleSet &VP, const opt_variables_type &optvars, std::vector< ValueType > &ratios, Matrix< ValueType > &dratios) override
size_type size() const
return the size
Definition: VariableSet.h:88
Specialization for two-body Jastrow function using multiple functors.
aligned_vector< int > DistIndice
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
void evaluateHessian(ParticleSet &P, HessVector &grad_grad_psi) override
void computeU3(const ParticleSet &P, int iat, const DistRow &dist, RealType *restrict u, RealType *restrict du, RealType *restrict d2u, bool triangle=false)
intenal function to compute
const valT lapfac
laplacian prefactor
std::vector< std::reference_wrapper< T > > RefVector
OHMMS_PRECISION real_type
Vector< int, OffloadPinnedAllocator< int > > grp_ids
the group_id of each particle
Vector< RealType, aligned_allocator< RealType > > DistRow
Definition: DistanceTable.h:46
User-defined functor.
aligned_vector< valT > old_u
void acquireResource(ResourceCollection &collection, const RefVectorWithLeader< WaveFunctionComponent > &wfc_list) const override
acquire a shared resource from a collection
const size_t NumGroups
number of groups of the target particleset
VectorSoaContainer< valT, DIM, aligned_allocator< valT > > dUat
LogValue evaluateGL(const ParticleSet &P, ParticleSet::ParticleGradient &G, ParticleSet::ParticleLaplacian &L, bool fromscratch=false) override
compute G and L after the sweep
void evaluateRatiosAlltoOne(ParticleSet &P, std::vector< ValueType > &ratios) override
Vector< RealType > dLogPsi
J2KECorrection< RealType, FT > j2_ke_corr_helper
Precision RealType
Definition: QMCTypes.h:37
std::pair< int, int > getComponentOffset(int index)
void evaluateDerivatives(ParticleSet &P, const opt_variables_type &active, Vector< ValueType > &dlogpsi, Vector< ValueType > &dhpsioverpsi) override
void evaluateRatios(const VirtualParticleSet &VP, std::vector< ValueType > &ratios) override
OrbitalSetTraits< ValueType >::HessVector HessVector
void checkOutVariables(const opt_variables_type &active) override
check out optimizable variables
std::vector< GradDerivVec > gradLogPsi
void mw_recompute(const RefVectorWithLeader< WaveFunctionComponent > &wfc_list, const RefVectorWithLeader< ParticleSet > &p_list, const std::vector< bool > &recompute) const override
Declaration of WaveFunctionComponent.
std::complex< double > LogValue
RealType DiffVal
diff value
TinyVector< valT, DIM > posT
element position type
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.
RealType KEcorr
Correction.
FT FuncType
alias FuncType
ParticleAttrib< QTFull::ValueType > ValueDerivVec
const size_t N
number of particles
void mw_calcRatio(const RefVectorWithLeader< WaveFunctionComponent > &wfc_list, const RefVectorWithLeader< ParticleSet > &p_list, int iat, std::vector< PsiValue > &ratios) const override
void copyFromBuffer(ParticleSet &P, WFBufferType &buf) override
For particle-by-particle move.
aligned_vector< valT > old_d2u
void acceptMove(ParticleSet &P, int iat, bool safe_to_delay=false) override
a move for iat-th particle is accepted.
std::string getClassName() const override
return class name
LogValue evaluateLog(const ParticleSet &P, ParticleSet::ParticleGradient &G, ParticleSet::ParticleLaplacian &L) override
evaluate the value of the WaveFunctionComponent from scratch
Vector< valT, aligned_allocator< valT > > Uat