QMCPACK
ExampleHeComponent.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) 2019 QMCPACK developers.
6 //
7 // File developed by: Mark Dewing, mdewing@anl.gov, Argonne National Laboratory
8 //
9 // File created by: Mark Dewing, mdewing@anl.gov, Argonne National Laboratory
10 //////////////////////////////////////////////////////////////////////////////////////
11 
12 
13 #include "ExampleHeComponent.h"
14 #include "OhmmsData/AttributeSet.h"
15 #include "DistanceTable.h"
16 
17 /**@file ExampleHeComponent.cpp
18  */
19 namespace qmcplusplus
20 {
21 bool ExampleHeComponent::put(xmlNodePtr cur)
22 {
23  cur = cur->xmlChildrenNode;
24  while (cur != NULL)
25  {
26  std::string cname((const char*)(cur->name));
27 
28  if (cname == "var")
29  {
30  std::string id_in;
31  std::string p_name;
32  OhmmsAttributeSet rAttrib;
33  rAttrib.add(id_in, "id");
34  rAttrib.add(p_name, "name");
35  rAttrib.put(cur);
36 
37 
38  if (p_name == "B")
39  {
40  ID_B = id_in;
41  putContent(B, cur);
42  opt_B = true;
43  }
44  }
45  cur = cur->next;
46  }
47 
48  my_vars_.clear();
49 
50  if (opt_B)
52 
53 
54  // Electron-nucleus cusp
55  Z = 2.0;
56 
57  // Electron-electron cusp
58  A = -0.5;
59  //A = 0.0;
60 
61 
62  return true;
63 }
64 
68 {
69  const auto& ee_table = P.getDistTableAA(my_table_ee_idx_);
70  const auto& ee_dists = ee_table.getDistances();
71  const auto& ee_displs = ee_table.getDisplacements();
72  // Only the lower triangle is up-to-date after particle-by-particle moves
73  double r12 = ee_dists[1][0];
74  auto rhat12 = ee_displs[1][0] / r12;
75 
76  const auto& ei_table = P.getDistTableAB(my_table_ei_idx_);
77  const auto& ei_dists = ei_table.getDistances();
78  const auto& ei_displs = ei_table.getDisplacements();
79 
80  // First index is electron, second index is ion
81  double r1 = ei_dists[0][0];
82  double r2 = ei_dists[1][0];
83 
84  auto rhat1 = ei_displs[0][0] / r1;
85  auto rhat2 = ei_displs[1][0] / r2;
86 
87  // Normalization for STO is not strictly necessary in this example, but it
88  // makes the values directly comparable to existing code
89  double norm = 0.5 / std::sqrt(M_PI);
90 
91  double du = A / ((B * r12 + 1) * (B * r12 + 1));
92 
93  double df1 = -Z;
94  G[0] = -df1 * rhat1 - du * rhat12;
95  // Previous line copies all three components and is the same as
96  //G[0][0] = -df1*rhat1[0];
97  //G[0][1] = -df1*rhat1[1];
98  //G[0][2] = -df1*rhat1[2];
99 
100  double df2 = -Z;
101  G[1] = -df2 * rhat2 + du * rhat12;
102 
103  double del_u = 2 * A / (r12 * (B * r12 + 1) * (B * r12 + 1) * (B * r12 + 1));
104 
105  L[0] = -2 * Z / r1 - del_u;
106  L[1] = -2 * Z / r2 - del_u;
107 
108  double u = A * r12 / (B * r12 + 1) - A / B;
109 
110  log_value_ = -Z * (r1 + r2) + std::log(norm * norm) - u;
111  return log_value_;
112 }
113 
115 {
116  const auto& ee_table = P.getDistTableAA(my_table_ee_idx_);
117  const auto& ee_dists = ee_table.getDistances();
118  const auto& ee_temp_r = ee_table.getTempDists();
119 
120  // only the lower triangle of e-e Distances and Displacements can be used.
121  double r12_old = ee_dists[1][0];
122  double r12_new = ee_temp_r[iat == 0 ? 1 : 0];
123 
124  const auto& ei_table = P.getDistTableAB(my_table_ei_idx_);
125  const auto& ei_dists = ei_table.getDistances();
126  const auto& ei_temp_r = ei_table.getTempDists();
127 
128  double r_old = ei_dists[iat][0];
129  double r_new = ei_temp_r[0];
130 
131  double u_old = A * r12_old / (B * r12_old + 1);
132  double u_new = A * r12_new / (B * r12_new + 1);
133 
134  double log_v_old = -Z * (r_old)-u_old;
135  double log_v_new = -Z * (r_new)-u_new;
136 
137  return std::exp(static_cast<PsiValue>(log_v_new - log_v_old));
138 }
139 
141 {
142  const auto& ei_table = P.getDistTableAB(my_table_ei_idx_);
143  const auto& ei_dists = ei_table.getDistances();
144  const auto& ei_displs = ei_table.getDisplacements();
145 
146  double r = ei_dists[iat][0];
147  auto rhat = ei_displs[iat][0] / r;
148 
149  const auto& ee_table = P.getDistTableAA(my_table_ee_idx_);
150  const auto& ee_dists = ee_table.getDistances();
151  const auto& ee_displs = ee_table.getDisplacements();
152 
153  // only the lower triangle of e-e Distances and Displacements can be used.
154  double r12 = ee_dists[1][0];
155  auto rhat12 = (iat == 0 ? -ee_displs[1][0] : ee_displs[1][0]) / r12;
156 
157  double du = A / ((B * r12 + 1) * (B * r12 + 1));
158 
159  return Z * rhat + rhat12 * du;
160 }
161 
163 {
164  const auto& ee_table = P.getDistTableAA(my_table_ee_idx_);
165  const auto& ee_dists = ee_table.getDistances();
166  const auto& ee_displs = ee_table.getDisplacements();
167  const auto& ee_temp_r = ee_table.getTempDists();
168  const auto& ee_temp_dr = ee_table.getTempDispls();
169 
170  const int jat = (iat == 0 ? 1 : 0);
171  // only the lower triangle of e-e Distances and Displacements can be used.
172  double r12_old = ee_dists[1][0];
173  double r12_new = ee_temp_r[jat];
174 
175  auto rhat12 = ee_temp_dr[jat] / r12_new;
176 
177  const auto& ei_table = P.getDistTableAB(my_table_ei_idx_);
178  const auto& ei_dists = ei_table.getDistances();
179  const auto& ei_displs = ei_table.getDisplacements();
180  const auto& ei_temp_r = ei_table.getTempDists();
181  const auto& ei_temp_dr = ei_table.getTempDispls();
182 
183  double r_old = ei_dists[iat][0];
184  double r_new = ei_temp_r[0];
185 
186  auto rhat = ei_temp_dr[0] / r_new;
187 
188  double du = A / ((B * r12_new + 1) * (B * r12_new + 1));
189  double df = -Z;
190  grad_iat = -df * rhat + du * rhat12;
191 
192  double u_old = A * r12_old / (B * r12_old + 1);
193  double u_new = A * r12_new / (B * r12_new + 1);
194 
195  double log_v_old = -Z * (r_old)-u_old;
196  double log_v_new = -Z * (r_new)-u_new;
197 
198  return std::exp(static_cast<PsiValue>(log_v_new - log_v_old));
199 }
200 
201 
203 {
204  return evaluateLog(P, P.G, P.L);
205 }
206 
207 std::unique_ptr<WaveFunctionComponent> ExampleHeComponent::makeClone(ParticleSet& tpq) const
208 {
209  return std::make_unique<ExampleHeComponent>(*this);
210 }
211 
213 {
214  if (my_vars_.size())
215  {
216  int ia = my_vars_.where(0);
217  if (ia > -1)
218  {
219  int i = 0;
220  if (opt_B)
221  {
222  B = std::real(my_vars_[i++] = active[ia++]);
223  }
224  }
225  }
226 }
227 
229  const OptVariablesType& optvars,
230  Vector<ValueType>& dlogpsi,
231  Vector<ValueType>& dhpsioverpsi)
232 {
233  using RealGradType = TinyVector<RealType, 3>;
234 
235  double tmpB = std::real(optvars[0]);
236 
237  const auto& ee_table = P.getDistTableAA(my_table_ee_idx_);
238  const auto& ee_dists = ee_table.getDistances();
239  const auto& ee_displs = ee_table.getDisplacements();
240  const auto& ee_temp_r = ee_table.getTempDists();
241  const auto& ee_temp_dr = ee_table.getTempDispls();
242 
243  double r12 = ee_dists[1][0];
244  auto rhat12 = ee_displs[1][0] / r12;
245 
246  const auto& ei_table = P.getDistTableAB(my_table_ei_idx_);
247  const auto& ei_dists = ei_table.getDistances();
248  const auto& ei_displs = ei_table.getDisplacements();
249  const auto& ei_temp_r = ei_table.getTempDists();
250  const auto& ei_temp_dr = ei_table.getTempDispls();
251 
252  double r1 = ei_dists[0][0];
253  double r2 = ei_dists[1][0];
254 
255  auto rhat1 = ei_displs[0][0] / r1;
256  auto rhat2 = ei_displs[1][0] / r2;
257 
258  dlogpsi[0] = A * r12 * r12 / ((tmpB * r12 + 1) * (tmpB * r12 + 1)) - A / (tmpB * tmpB);
259 
260  double df = -Z;
261  double du = A / ((B * r12 + 1) * (B * r12 + 1));
262  RealGradType G1 = -df * rhat1 - rhat12 * du;
263  RealGradType G2 = -df * rhat2 + rhat12 * du;
264 
265  double dudb = -2 * A * r12 / std::pow(B * r12 + 1, 3);
266  RealGradType dG1 = rhat12 * dudb;
267  RealGradType dG2 = -1 * rhat12 * dudb;
268 
269  double dlap = -6 * A / std::pow((tmpB * r12 + 1), 4);
270  dhpsioverpsi[0] =
271  dlap + (G1[0] * dG1[0] + G1[1] * dG1[1] + G1[2] * dG1[2]) + (G2[0] * dG2[0] + G2[1] * dG2[1] + G2[2] * dG2[2]);
272 }
273 
274 
275 }; // namespace qmcplusplus
LogValue evaluateLog(const ParticleSet &P, ParticleSet::ParticleGradient &G, ParticleSet::ParticleLaplacian &L) override
evaluate the value of the WaveFunctionComponent from scratch
Fixed-size array.
Definition: OhmmsTinyMeta.h:30
const std::vector< DistRow > & getDistances() const
return full table distances
QMCTraits::RealType real
helper functions for EinsplineSetBuilder
Definition: Configuration.h:43
const DistanceTableAA & getDistTableAA(int table_ID) const
get a distance table by table_ID and dyanmic_cast to DistanceTableAA
An example wavefunction component for a simple wavefunction for a helium atom.
bool put(xmlNodePtr cur)
assign attributes to the set
Definition: AttributeSet.h:55
GradType evalGrad(ParticleSet &P, int iat) override
return the current gradient for the iat-th particle
void evaluateDerivatives(ParticleSet &P, const OptVariablesType &optvars, Vector< ValueType > &dlogpsi, Vector< ValueType > &dhpsioverpsi) override
Compute the derivatives of both the log of the wavefunction and kinetic energy with respect to optimi...
Attaches a unit to a Vector for IO.
ParticleLaplacian L
laplacians of the particles
Definition: ParticleSet.h:85
std::complex< QTFull::RealType > LogValue
const DistanceTableAB & getDistTableAB(int table_ID) const
get a distance table by table_ID and dyanmic_cast to DistanceTableAB
MakeReturn< BinaryNode< FnPow, typename CreateLeaf< Vector< T1, C1 > >::Leaf_t, typename CreateLeaf< Vector< T2, C2 > >::Leaf_t > >::Expression_t pow(const Vector< T1, C1 > &l, const Vector< T2, C2 > &r)
Specialized paritlce class for atomistic simulations.
Definition: ParticleSet.h:55
double norm(const zVec &c)
Definition: VectorOps.h:118
PsiValue ratio(ParticleSet &P, int iat) override
evaluate the ratio of the new to old WaveFunctionComponent value
void insert(const std::string &vname, real_type v, bool enable=true, int type=OTHER_P)
Definition: VariableSet.h:133
class to handle a set of attributes of an xmlNode
Definition: AttributeSet.h:24
void resetParametersExclusive(const OptVariablesType &active) override
reset the parameters during optimizations.
ParticleGradient G
gradients of the particles
Definition: ParticleSet.h:83
class to handle a set of variables that can be modified during optimizations
Definition: VariableSet.h:49
int where(int i) const
return the locator of the i-th Index
Definition: VariableSet.h:90
MakeReturn< UnaryNode< FnExp, typename CreateLeaf< Vector< T1, C1 > >::Leaf_t > >::Expression_t exp(const Vector< T1, C1 > &l)
void clear()
clear the variable set
Definition: VariableSet.cpp:28
size_type size() const
return the size
Definition: VariableSet.h:88
std::unique_ptr< WaveFunctionComponent > makeClone(ParticleSet &tpq) const override
make clone
bool putContent(T &a, xmlNodePtr cur)
replaces a&#39;s value with the first "element" in the "string" returned by XMLNodeString{cur}.
Definition: libxmldefs.h:88
MakeReturn< UnaryNode< FnLog, typename CreateLeaf< Vector< T1, C1 > >::Leaf_t > >::Expression_t log(const Vector< T1, C1 > &l)
MakeReturn< UnaryNode< FnSqrt, typename CreateLeaf< Vector< T1, C1 > >::Leaf_t > >::Expression_t sqrt(const Vector< T1, C1 > &l)
const std::vector< DistRow > & getDistances() const
return full table distances
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 add(PDT &aparam, const std::string &aname, std::vector< PDT > candidate_values={}, TagStatus status=TagStatus::OPTIONAL)
add a new attribute
Definition: AttributeSet.h:42
LogValue updateBuffer(ParticleSet &P, WFBufferType &buf, bool fromscratch=false) override
For particle-by-particle move.