QMCPACK
BsplineSet.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) 2019 QMCPACK developers.
6 //
7 // File developed by: Jeremy McMinnis, jmcminis@gmail.com, University of Illinois at Urbana-Champaign
8 // Jaron T. Krogel, krogeljt@ornl.gov, Oak Ridge National Laboratory
9 // Jeongnim Kim, jeongnim.kim@gmail.com, University of Illinois at Urbana-Champaign
10 // Mark A. Berrill, berrillma@ornl.gov, Oak Ridge National Laboratory
11 // Ye Luo, yeluo@anl.gov, Argonne National Laboratory
12 //
13 // File created by: Jeongnim Kim, jeongnim.kim@gmail.com, University of Illinois at Urbana-Champaign
14 //////////////////////////////////////////////////////////////////////////////////////
15 
16 
17 /** @file BsplineSet.h
18  *
19  * BsplineSet is a SPOSet derived class and serves as a base class for B-spline SPO C2C/C2R/R2R implementation
20  */
21 #ifndef QMCPLUSPLUS_BSPLINESET_H
22 #define QMCPLUSPLUS_BSPLINESET_H
23 
25 #include "spline/einspline_engine.hpp"
26 #include "spline/einspline_util.hpp"
27 
28 namespace qmcplusplus
29 {
30 /** BsplineSet is the base class for SplineC2C, SplineC2R, SplineR2R.
31  * Its derived template classes manage the storage and evaluation at given precision.
32  * BsplineSet also implements a few fallback routines in case optimized implementation is not necessary in the derived class.
33  */
34 class BsplineSet : public SPOSet
35 {
36 protected:
37  static const int D = DIM;
38  ///Index of this adoptor, when multiple adoptors are used for NUMA or distributed cases
39  size_t MyIndex;
40  ///first index of the SPOs this Spline handles
41  size_t first_spo;
42  ///last index of the SPOs this Spline handles
43  size_t last_spo;
44  ///sign bits at the G/2 boundaries
46  ///flags to unpack sin/cos
47  std::vector<bool> MakeTwoCopies;
48  /** kpoints for each unique orbitals.
49  * Note: for historic reason, this sign is opposite to what was used in DFT when orbitals were generated.
50  * Changing the sign requires updating all the evaluation code.
51  */
52  std::vector<SPOSet::PosType> kPoints;
53  ///remap splines to orbitals
55  ///band offsets used for communication
56  std::vector<int> offset;
57 
58 public:
59  BsplineSet(const std::string& my_name) : SPOSet(my_name), MyIndex(0), first_spo(0), last_spo(0) {}
60 
61  virtual bool isComplex() const = 0;
62  virtual std::string getKeyword() const = 0;
63 
64  auto& getHalfG() const { return HalfG; }
65 
66  inline void init_base(int n)
67  {
68  kPoints.resize(n);
69  MakeTwoCopies.resize(n);
70  BandIndexMap.resize(n);
71  for (int i = 0; i < n; i++)
72  BandIndexMap[i] = i;
73  }
74 
75  ///remap kpoints to group general kpoints & special kpoints
77  {
78  std::vector<SPOSet::PosType> k_copy(kPoints);
79  const int nk = kPoints.size();
80  int nCB = 0;
81  //two pass
82  for (int i = 0; i < nk; ++i)
83  {
84  if (MakeTwoCopies[i])
85  {
86  kPoints[nCB] = k_copy[i];
87  BandIndexMap[nCB++] = i;
88  }
89  }
90  int nRealBands = nCB;
91  for (int i = 0; i < nk; ++i)
92  {
93  if (!MakeTwoCopies[i])
94  {
95  kPoints[nRealBands] = k_copy[i];
96  BandIndexMap[nRealBands++] = i;
97  }
98  }
99  return nCB; //return the number of complex bands
100  }
101 
102  // propagate SPOSet virtual functions
104  using SPOSet::evaluateValue;
105  using SPOSet::evaluateVGH;
106  using SPOSet::evaluateVGHGH;
107  using SPOSet::evaluateVGL;
112 
116 
117  std::unique_ptr<SPOSet> makeClone() const override = 0;
118 
119  void setOrbitalSetSize(int norbs) override { OrbitalSetSize = norbs; }
120 
122  int first,
123  int last,
124  ValueMatrix& logdet,
125  GradMatrix& dlogdet,
126  ValueMatrix& d2logdet) override
127  {
129  using grad_type = GradMatrix::value_type;
130  for (int iat = first, i = 0; iat < last; ++iat, ++i)
131  {
132  ValueVector v(logdet[i], logdet.cols());
133  GradVector g(dlogdet[i], dlogdet.cols());
134  ValueVector l(d2logdet[i], d2logdet.cols());
135  evaluateVGL(P, iat, v, g, l);
136  }
137  }
138 
140  const RefVectorWithLeader<ParticleSet>& P_list,
141  int first,
142  int last,
143  const RefVector<ValueMatrix>& logdet_list,
144  const RefVector<GradMatrix>& dlogdet_list,
145  const RefVector<ValueMatrix>& d2logdet_list) const override
146  {
147  assert(this == &spo_list.getLeader());
149  using grad_type = GradMatrix::value_type;
150 
151  const size_t nw = spo_list.size();
152  std::vector<ValueVector> mw_psi_v;
153  std::vector<GradVector> mw_dpsi_v;
154  std::vector<ValueVector> mw_d2psi_v;
155  RefVector<ValueVector> psi_v_list;
156  RefVector<GradVector> dpsi_v_list;
157  RefVector<ValueVector> d2psi_v_list;
158  mw_psi_v.reserve(nw);
159  mw_dpsi_v.reserve(nw);
160  mw_d2psi_v.reserve(nw);
161  psi_v_list.reserve(nw);
162  dpsi_v_list.reserve(nw);
163  d2psi_v_list.reserve(nw);
164 
165  for (int iat = first, i = 0; iat < last; ++iat, ++i)
166  {
167  mw_psi_v.clear();
168  mw_dpsi_v.clear();
169  mw_d2psi_v.clear();
170  psi_v_list.clear();
171  dpsi_v_list.clear();
172  d2psi_v_list.clear();
173 
174  for (int iw = 0; iw < nw; iw++)
175  {
176  mw_psi_v.emplace_back(logdet_list[iw].get()[i], logdet_list[iw].get().cols());
177  mw_dpsi_v.emplace_back(dlogdet_list[iw].get()[i], dlogdet_list[iw].get().cols());
178  mw_d2psi_v.emplace_back(d2logdet_list[iw].get()[i], d2logdet_list[iw].get().cols());
179  psi_v_list.push_back(mw_psi_v.back());
180  dpsi_v_list.push_back(mw_dpsi_v.back());
181  d2psi_v_list.push_back(mw_d2psi_v.back());
182  }
183 
184  mw_evaluateVGL(spo_list, P_list, iat, psi_v_list, dpsi_v_list, d2psi_v_list);
185  }
186  }
187 
189  int first,
190  int last,
191  ValueMatrix& logdet,
192  GradMatrix& dlogdet,
193  HessMatrix& grad_grad_logdet) override
194  {
195  for (int iat = first, i = 0; iat < last; ++iat, ++i)
196  {
197  ValueVector v(logdet[i], logdet.cols());
198  GradVector g(dlogdet[i], dlogdet.cols());
199  HessVector h(grad_grad_logdet[i], grad_grad_logdet.cols());
200  evaluateVGH(P, iat, v, g, h);
201  }
202  }
203 
205  int first,
206  int last,
207  ValueMatrix& logdet,
208  GradMatrix& dlogdet,
209  HessMatrix& grad_grad_logdet,
210  GGGMatrix& grad_grad_grad_logdet) override
211  {
212  for (int iat = first, i = 0; iat < last; ++iat, ++i)
213  {
214  ValueVector v(logdet[i], logdet.cols());
215  GradVector g(dlogdet[i], dlogdet.cols());
216  HessVector h(grad_grad_logdet[i], grad_grad_logdet.cols());
217  GGGVector gh(grad_grad_grad_logdet[i], grad_grad_grad_logdet.cols());
218  evaluateVGHGH(P, iat, v, g, h, gh);
219  }
220  }
221 
223  int first,
224  int last,
225  const ParticleSet& source,
226  int iat_src,
227  GradMatrix& gradphi) override
228  {
229  //Do nothing, since Einsplines don't explicitly depend on ion positions.
230  }
231 
233  int first,
234  int last,
235  const ParticleSet& source,
236  int iat_src,
237  GradMatrix& grad_phi,
238  HessMatrix& grad_grad_phi,
239  GradMatrix& grad_lapl_phi) override
240  {
241  //Do nothing, since Einsplines don't explicitly depend on ion positions.
242  }
243 
244  template<class BSPLINESPO>
245  friend class SplineSetReader;
246  friend struct BsplineReader;
247 };
248 
249 } // namespace qmcplusplus
250 #endif
base class for Single-particle orbital sets
Definition: SPOSet.h:46
OrbitalSetTraits< ValueType >::HessVector HessVector
Definition: SPOSet.h:53
std::unique_ptr< SPOSet > makeClone() const override=0
make a clone of itself every derived class must implement this to have threading working correctly...
static const int D
Definition: BsplineSet.h:37
std::vector< T, aligned_allocator< T > > aligned_vector
helper functions for EinsplineSetBuilder
Definition: Configuration.h:43
aligned_vector< int > BandIndexMap
remap splines to orbitals
Definition: BsplineSet.h:54
BsplineSet is the base class for SplineC2C, SplineC2R, SplineR2R.
Definition: BsplineSet.h:34
size_t MyIndex
Index of this adoptor, when multiple adoptors are used for NUMA or distributed cases.
Definition: BsplineSet.h:39
virtual void evaluateVGH(const ParticleSet &P, int iat, ValueVector &psi, GradVector &dpsi, HessVector &grad_grad_psi)
evaluate the values, gradients and hessians of this single-particle orbital set
Definition: SPOSet.cpp:238
OrbitalSetTraits< ValueType >::ValueMatrix ValueMatrix
Definition: SPOSet.h:50
BsplineSet(const std::string &my_name)
Definition: BsplineSet.h:59
void evaluateGradSource(const ParticleSet &P, int first, int last, const ParticleSet &source, int iat_src, GradMatrix &grad_phi, HessMatrix &grad_grad_phi, GradMatrix &grad_lapl_phi) override
evaluate the gradients of values, gradients, laplacians of this single-particle orbital for [first...
Definition: BsplineSet.h:232
virtual bool isComplex() const =0
OrbitalSetTraits< ValueType >::GradMatrix GradMatrix
Definition: SPOSet.h:52
virtual void evaluateValue(const ParticleSet &P, int iat, ValueVector &psi)=0
evaluate the values of this single-particle orbital set
Each SplineC2X needs a reader derived from BsplineReader.
Definition: BsplineReader.h:39
Specialized paritlce class for atomistic simulations.
Definition: ParticleSet.h:55
General SplineSetReader to handle any unitcell.
std::vector< int > offset
band offsets used for communication
Definition: BsplineSet.h:56
virtual void mw_evaluateVGL(const RefVectorWithLeader< SPOSet > &spo_list, const RefVectorWithLeader< ParticleSet > &P_list, int iat, const RefVector< ValueVector > &psi_v_list, const RefVector< GradVector > &dpsi_v_list, const RefVector< ValueVector > &d2psi_v_list) const
evaluate the values, gradients and laplacians of this single-particle orbital sets of multiple walker...
Definition: SPOSet.cpp:93
virtual void mw_evaluateVGLandDetRatioGrads(const RefVectorWithLeader< SPOSet > &spo_list, const RefVectorWithLeader< ParticleSet > &P_list, int iat, const std::vector< const ValueType *> &invRow_ptr_list, OffloadMWVGLArray &phi_vgl_v, std::vector< ValueType > &ratios, std::vector< GradType > &grads) const
evaluate the values, gradients and laplacians of this single-particle orbital sets and determinant ra...
Definition: SPOSet.cpp:126
OrbitalSetTraits< ValueType >::ValueVector ValueVector
Definition: SPOSet.h:49
virtual void evaluateVGL(const ParticleSet &P, int iat, ValueVector &psi, GradVector &dpsi, ValueVector &d2psi)=0
evaluate the values, gradients and laplacians of this single-particle orbital set ...
virtual void finalizeConstruction()
finalize the construction of SPOSet
Definition: SPOSet.h:554
void evaluate_notranspose(const ParticleSet &P, int first, int last, ValueMatrix &logdet, GradMatrix &dlogdet, HessMatrix &grad_grad_logdet) override
evaluate the values, gradients and hessians of this single-particle orbital for [first,last) particles
Definition: BsplineSet.h:188
virtual void acquireResource(ResourceCollection &collection, const RefVectorWithLeader< SPOSet > &spo_list) const
acquire a shared resource from collection
Definition: SPOSet.h:532
IndexType OrbitalSetSize
number of Single-particle orbitals
Definition: SPOSet.h:566
void evaluateGradSource(const ParticleSet &P, int first, int last, const ParticleSet &source, int iat_src, GradMatrix &gradphi) override
evaluate the gradients of this single-particle orbital for [first,last) target particles with respect...
Definition: BsplineSet.h:222
virtual void evaluateDetRatios(const VirtualParticleSet &VP, ValueVector &psi, const ValueVector &psiinv, std::vector< ValueType > &ratios)
evaluate determinant ratios for virtual moves, e.g., sphere move for nonlocalPP
Definition: SPOSet.cpp:47
virtual void releaseResource(ResourceCollection &collection, const RefVectorWithLeader< SPOSet > &spo_list) const
return a shared resource to collection
Definition: SPOSet.h:536
auto & getHalfG() const
Definition: BsplineSet.h:64
std::vector< std::reference_wrapper< T > > RefVector
void setOrbitalSetSize(int norbs) override
set the OrbitalSetSize
Definition: BsplineSet.h:119
void mw_evaluate_notranspose(const RefVectorWithLeader< SPOSet > &spo_list, const RefVectorWithLeader< ParticleSet > &P_list, int first, int last, const RefVector< ValueMatrix > &logdet_list, const RefVector< GradMatrix > &dlogdet_list, const RefVector< ValueMatrix > &d2logdet_list) const override
Definition: BsplineSet.h:139
OrbitalSetTraits< ValueType >::GradHessVector GGGVector
Definition: SPOSet.h:55
OrbitalSetTraits< ValueType >::GradVector GradVector
Definition: SPOSet.h:51
virtual void mw_evaluateDetRatios(const RefVectorWithLeader< SPOSet > &spo_list, const RefVectorWithLeader< const VirtualParticleSet > &vp_list, const RefVector< ValueVector > &psi_list, const std::vector< const ValueType *> &invRow_ptr_list, std::vector< std::vector< ValueType >> &ratios_list) const
evaluate determinant ratios for virtual moves, e.g., sphere move for nonlocalPP, of multiple walkers ...
Definition: SPOSet.cpp:69
void evaluate_notranspose(const ParticleSet &P, int first, int last, ValueMatrix &logdet, GradMatrix &dlogdet, ValueMatrix &d2logdet) override
evaluate the values, gradients and laplacians of this single-particle orbital for [first...
Definition: BsplineSet.h:121
void evaluate_notranspose(const ParticleSet &P, int first, int last, ValueMatrix &logdet, GradMatrix &dlogdet, HessMatrix &grad_grad_logdet, GGGMatrix &grad_grad_grad_logdet) override
evaluate the values, gradients, hessians and third derivatives of this single-particle orbital for [f...
Definition: BsplineSet.h:204
std::vector< bool > MakeTwoCopies
flags to unpack sin/cos
Definition: BsplineSet.h:47
void init_base(int n)
Definition: BsplineSet.h:66
TinyVector< int, D > HalfG
sign bits at the G/2 boundaries
Definition: BsplineSet.h:45
OrbitalSetTraits< ValueType >::GradHessMatrix GGGMatrix
Definition: SPOSet.h:56
size_t first_spo
first index of the SPOs this Spline handles
Definition: BsplineSet.h:41
virtual void createResource(ResourceCollection &collection) const
initialize a shared resource and hand it to collection
Definition: SPOSet.h:528
int remap_kpoints()
remap kpoints to group general kpoints & special kpoints
Definition: BsplineSet.h:76
QMCTraits::FullPrecRealType value_type
std::vector< SPOSet::PosType > kPoints
kpoints for each unique orbitals.
Definition: BsplineSet.h:52
virtual void evaluateVGHGH(const ParticleSet &P, int iat, ValueVector &psi, GradVector &dpsi, HessVector &grad_grad_psi, GGGVector &grad_grad_grad_psi)
evaluate the values, gradients, hessians, and grad hessians of this single-particle orbital set ...
Definition: SPOSet.cpp:244
size_t last_spo
last index of the SPOs this Spline handles
Definition: BsplineSet.h:43
OrbitalSetTraits< ValueType >::HessMatrix HessMatrix
Definition: SPOSet.h:54
virtual std::string getKeyword() const =0