QMCPACK
TWFFastDerivWrapper.cpp
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 
15 #include <iostream>
16 namespace qmcplusplus
17 {
19 {
20  IndexType return_group_index(-1);
21  for (IndexType i = 0; i < groups_.size(); i++)
22  if (gid == groups_[i])
23  return_group_index = i;
24 
25  assert(return_group_index != -1);
26 
27  return return_group_index;
28 }
29 
31 {
32  if (std::find(groups_.begin(), groups_.end(), gid) == groups_.end())
33  {
34  groups_.push_back(gid);
35  spos_.push_back(spo);
36  }
37 }
38 
39 void TWFFastDerivWrapper::getM(const ParticleSet& P, std::vector<ValueMatrix>& mvec) const
40 {
41  IndexType ngroups = spos_.size();
42  for (IndexType i = 0; i < ngroups; i++)
43  {
44  const IndexType gid = groups_[i];
45  const IndexType first = P.first(i);
46  const IndexType last = P.last(i);
47  const IndexType nptcls = last - first;
48  const IndexType norbs = spos_[i]->getOrbitalSetSize();
49  GradMatrix tmpgmat;
50  ValueMatrix tmplmat;
51  tmpgmat.resize(nptcls, norbs);
52  tmplmat.resize(nptcls, norbs);
53  spos_[i]->evaluate_notranspose(P, first, last, mvec[i], tmpgmat, tmplmat);
54  }
55 }
56 
60 {
62  G = 0.0;
63  L = 0.0;
64  for (int i = 0; i < jastrow_list_.size(); ++i)
65  {
66  logpsi += jastrow_list_[i]->evaluateLog(P, G, L);
67  }
68  RealType rval = std::real(logpsi);
69  return rval;
70 }
71 
73 {
74  //legacy calls are hit and miss with const. Remove const for index.
75  int iel_(iel);
77  for (int i = 0; i < jastrow_list_.size(); ++i)
78  {
79  r *= jastrow_list_[i]->ratio(P, iel_);
80  }
81 
82  RealType ratio_return(1.0);
83  convertToReal(r, ratio_return);
84  return ratio_return;
85 }
86 
88  const int iel,
89  GradType& grad) const
90 {
91  int iel_(iel);
93  for (int i = 0; i < jastrow_list_.size(); ++i)
94  {
95  r *= jastrow_list_[i]->ratioGrad(P, iel_, grad);
96  }
97  RealType ratio_return(1.0);
98  convertToReal(r, ratio_return);
99  return ratio_return;
100 }
101 
103  ParticleSet& source,
104  const int iat) const
105 {
106  GradType grad_iat = GradType();
107  for (int i = 0; i < jastrow_list_.size(); ++i)
108  grad_iat += jastrow_list_[i]->evalGradSource(P, source, iat);
109  return grad_iat;
110 }
111 
113  ParticleSet& P,
114  ParticleSet& source,
115  const int iat,
118 {
119  GradType grad_iat = GradType();
120  for (int dim = 0; dim < OHMMS_DIM; dim++)
121  for (int i = 0; i < grad_grad[0].size(); i++)
122  {
123  grad_grad[dim][i] = GradType();
124  lapl_grad[dim][i] = 0.0;
125  }
126  for (int i = 0; i < jastrow_list_.size(); ++i)
127  grad_iat += jastrow_list_[i]->evalGradSource(P, source, iat, grad_grad, lapl_grad);
128  return grad_iat;
129 }
130 
132  std::vector<ValueMatrix>& mvec,
133  std::vector<GradMatrix>& gmat,
134  std::vector<ValueMatrix>& lmat) const
135 {
136  IndexType ngroups = mvec.size();
137  for (IndexType i = 0; i < ngroups; i++)
138  {
139  const IndexType gid = groups_[i];
140  const IndexType first = P.first(i);
141  const IndexType last = P.last(i);
142  const IndexType nptcls = last - first;
143  const IndexType norbs = spos_[i]->getOrbitalSetSize();
144  spos_[i]->evaluate_notranspose(P, first, last, mvec[i], gmat[i], lmat[i]);
145  }
146 }
147 
149  const ParticleSet& source,
150  const int iat,
151  std::vector<std::vector<ValueMatrix>>& dmvec) const
152 {
153  IndexType ngroups = dmvec[0].size();
154  for (IndexType i = 0; i < ngroups; i++)
155  {
156  const IndexType gid = groups_[i];
157  const IndexType first = P.first(i);
158  const IndexType last = P.last(i);
159  const IndexType nptcls = last - first;
160  const IndexType norbs = spos_[i]->getOrbitalSetSize();
161 
162  GradMatrix grad_phi;
163 
164  grad_phi.resize(nptcls, norbs);
165 
166  spos_[i]->evaluateGradSource(P, first, last, source, iat, grad_phi);
167 
168  for (IndexType idim = 0; idim < OHMMS_DIM; idim++)
169  for (IndexType iptcl = 0; iptcl < nptcls; iptcl++)
170  for (IndexType iorb = 0; iorb < norbs; iorb++)
171  {
172  dmvec[idim][i][iptcl][iorb] += grad_phi[iptcl][iorb][idim];
173  }
174  }
175 }
176 
178  const ParticleSet& source,
179  int iat,
180  std::vector<std::vector<ValueMatrix>>& dmvec,
181  std::vector<std::vector<GradMatrix>>& dgmat,
182  std::vector<std::vector<ValueMatrix>>& dlmat) const
183 {
184  IndexType ngroups = dmvec[0].size();
185  for (IndexType i = 0; i < ngroups; i++)
186  {
187  const IndexType gid = groups_[i];
188  const IndexType first = P.first(i);
189  const IndexType last = P.last(i);
190  const IndexType nptcls = last - first;
191  const IndexType norbs = spos_[i]->getOrbitalSetSize();
192 
193  GradMatrix grad_phi;
194  HessMatrix grad_grad_phi;
195  GradMatrix grad_lapl_phi;
196 
197  grad_phi.resize(nptcls, norbs);
198  grad_grad_phi.resize(nptcls, norbs);
199  grad_lapl_phi.resize(nptcls, norbs);
200 
201  spos_[i]->evaluateGradSource(P, first, last, source, iat, grad_phi, grad_grad_phi, grad_lapl_phi);
202 
203  for (IndexType idim = 0; idim < OHMMS_DIM; idim++)
204  for (IndexType iptcl = 0; iptcl < nptcls; iptcl++)
205  for (IndexType iorb = 0; iorb < norbs; iorb++)
206  {
207  dmvec[idim][i][iptcl][iorb] += grad_phi[iptcl][iorb][idim];
208  dlmat[idim][i][iptcl][iorb] += grad_lapl_phi[iptcl][iorb][idim];
209  for (IndexType ielec = 0; ielec < OHMMS_DIM; ielec++)
210  dgmat[idim][i][iptcl][iorb][ielec] += grad_grad_phi[iptcl][iorb](idim, ielec);
211  }
212  }
213 }
214 
216  const std::vector<ValueMatrix>& X,
217  const std::vector<ValueMatrix>& dM,
218  const std::vector<ValueMatrix>& dB) const
219 {
220  IndexType nspecies = Minv.size();
221  ValueType dval = 0.0;
222  for (int id = 0; id < nspecies; id++)
223  {
224  int ptclnum = Minv[id].rows();
225  ValueType dval_id = 0.0;
226  for (int i = 0; i < ptclnum; i++)
227  for (int j = 0; j < ptclnum; j++)
228  {
229  //Tr[M^{-1} dB - X * dM ]
230  dval_id += Minv[id][i][j] * dB[id][j][i] - X[id][i][j] * dM[id][j][i];
231  }
232  dval += dval_id;
233  }
234  return dval;
235 }
236 
237 void TWFFastDerivWrapper::invertMatrices(const std::vector<ValueMatrix>& M, std::vector<ValueMatrix>& Minv)
238 {
239  IndexType nspecies = M.size();
240  for (IndexType id = 0; id < nspecies; id++)
241  {
242  assert(M[id].cols() == M[id].rows());
243  Minv[id] = M[id];
244  invert_matrix(Minv[id]);
245  }
246 }
247 
248 void TWFFastDerivWrapper::buildX(const std::vector<ValueMatrix>& Minv,
249  const std::vector<ValueMatrix>& B,
250  std::vector<ValueMatrix>& X)
251 {
252  IndexType nspecies = Minv.size();
253 
254  for (IndexType id = 0; id < nspecies; id++)
255  {
256  int ptclnum = Minv[id].rows();
257  assert(Minv[id].rows() == Minv[id].cols());
258  ValueMatrix tmpmat;
259  X[id].resize(ptclnum, ptclnum);
260  tmpmat.resize(ptclnum, ptclnum);
261  //(B*A^-1)
262  for (int i = 0; i < ptclnum; i++)
263  for (int j = 0; j < ptclnum; j++)
264  for (int k = 0; k < ptclnum; k++)
265  {
266  tmpmat[i][j] += B[id][i][k] * Minv[id][k][j];
267  }
268  //A^{-1}*B*A^{-1}
269  for (int i = 0; i < ptclnum; i++)
270  for (int j = 0; j < ptclnum; j++)
271  for (int k = 0; k < ptclnum; k++)
272  {
273  X[id][i][j] += Minv[id][i][k] * tmpmat[k][j];
274  }
275  }
276 }
277 
278 void TWFFastDerivWrapper::wipeMatrices(std::vector<ValueMatrix>& A)
279 {
280  for (IndexType id = 0; id < A.size(); id++)
281  {
282  A[id] = 0.0;
283  }
284 }
285 
287  const std::vector<ValueMatrix>& B)
288 {
289  IndexType nspecies = A.size();
290  assert(A.size() == B.size());
291  ValueType val = 0.0;
292  //Now to compute the kinetic energy
293  for (IndexType id = 0; id < nspecies; id++)
294  {
295  int ptclnum = A[id].rows();
296  ValueType val_id = 0.0;
297  assert(A[id].cols() == B[id].rows() && A[id].rows() == B[id].cols());
298  for (int i = 0; i < A[id].rows(); i++)
299  for (int j = 0; j < A[id].cols(); j++)
300  {
301  val_id += A[id][i][j] * B[id][j][i];
302  }
303  val += val_id;
304  }
305 
306  return val;
307 }
308 
309 void TWFFastDerivWrapper::getGSMatrices(const std::vector<ValueMatrix>& A, std::vector<ValueMatrix>& Aslice) const
310 {
311  IndexType nspecies = A.size();
312  Aslice.resize(nspecies);
313  for (IndexType id = 0; id < nspecies; id++)
314  {
315  IndexType ptclnum = A[id].rows();
316  Aslice[id].resize(ptclnum, ptclnum);
317  for (IndexType i = 0; i < ptclnum; i++)
318  for (IndexType j = 0; j < ptclnum; j++)
319  Aslice[id][i][j] = A[id][i][j];
320  }
321 }
322 
323 
325  const IndexType iel,
326  ValueVector& val) const
327 {
328  IndexType gid = P.getGroupID(iel);
329  IndexType sid = getTWFGroupIndex(gid);
330 
331  GradVector tempg;
332  ValueVector templ;
333 
334  IndexType norbs = spos_[sid]->getOrbitalSetSize();
335 
336  tempg.resize(norbs);
337  templ.resize(norbs);
338 
339  spos_[sid]->evaluateVGL(P, iel, val, tempg, templ);
340 
341  return sid;
342 }
343 
344 } // namespace qmcplusplus
base class for Single-particle orbital sets
Definition: SPOSet.h:46
MatrixA::value_type invert_matrix(MatrixA &M, bool getdet=true)
invert a matrix
Fixed-size array.
Definition: OhmmsTinyMeta.h:30
std::vector< WaveFunctionComponent * > jastrow_list_
QMCTraits::RealType real
helper functions for EinsplineSetBuilder
Definition: Configuration.h:43
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)
void convertToReal(const T1 &in, T2 &out)
generic conversion from type T1 to type T2 using implicit conversion
Definition: ConvertToReal.h:32
GradType evaluateJastrowGradSource(ParticleSet &P, ParticleSet &source, const int iat) const
Return ionic gradient of J(r).
int first(int igroup) const
return the first index of a group i
Definition: ParticleSet.h:514
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.
Attaches a unit to a Vector for IO.
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.
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.
#define OHMMS_DIM
Definition: config.h:64
std::complex< QTFull::RealType > LogValue
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;))...
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...
int last(int igroup) const
return the last index of a group i
Definition: ParticleSet.h:517
std::vector< IndexType > groups_
Define determinant operators.
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 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.
IndexType getTWFGroupIndex(const IndexType gid) const
Takes particle set groupID and returns the TWF internal index for it.
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.
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.
int getGroupID(int iat) const
return the group id of a given particle in the particle set.
Definition: ParticleSet.h:520