QMCPACK
test_short_range_cusp_jastrow.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: Eric Neuscamman, eneuscamman@berkeley.edu, University of California, Berkeley
8 //
9 // File created by: Eric Neuscamman, eneuscamman@berkeley.edu, University of California, Berkeley
10 //////////////////////////////////////////////////////////////////////////////////////
11 
12 
13 #include "catch.hpp"
14 
15 #include "Configuration.h"
16 #include "Message/Communicate.h"
17 #include "OhmmsData/Libxml2Doc.h"
19 
20 namespace qmcplusplus
21 {
22 TEST_CASE("ShortRangeCuspJastrowFunctor", "[wavefunction]")
23 {
25 
27 
28  // prepare xml input to set up the functor
29  const std::string xmltext("<tmp> "
30  " <correlation rcut=\"6\" cusp=\"3\" elementType=\"Li\"> "
31  " <var id=\"LiCuspR0\" name=\"R0\" optimize=\"yes\"> 0.0624 </var>"
32  " <coefficients id=\"LiCuspB\" type=\"Array\" optimize=\"yes\"> "
33  " 0.3 0.2 0.4 "
34  " </coefficients> "
35  " </correlation> "
36  "</tmp> ");
37 
38  // parse the xml input
40  const bool xml_parsed_okay = doc.parseFromString(xmltext);
41  REQUIRE(xml_parsed_okay == true);
42 
43  // get the xml root node pointer
44  xmlNodePtr root = doc.getRoot();
45  REQUIRE(root != NULL);
46 
47  // get the xml pointer to the functor node
48  xmlNodePtr child = root->xmlChildrenNode;
49  while (child != NULL)
50  {
51  if (xmlIsBlankNode(child))
52  child = child->next;
53  else
54  break;
55  }
56  REQUIRE(child != NULL);
57 
58  // prepare the Jastrow factor using the xml input
59  ShortRangeCuspFunctor<RealType> f("test_functor");
60  f.put(child);
61  CHECK(f.A == Approx(3.0));
62  CHECK(f.R0 == Approx(0.0624));
63  CHECK(f.B.at(0) == Approx(0.3));
64  CHECK(f.B.at(1) == Approx(0.2));
65  CHECK(f.B.at(2) == Approx(0.4));
66  REQUIRE(f.Opt_A == false);
67  REQUIRE(f.Opt_R0 == true);
68  REQUIRE(f.Opt_B == true);
69  REQUIRE(f.ID_A == "string_not_set");
70  REQUIRE(f.ID_R0 == "LiCuspR0");
71  REQUIRE(f.ID_B == "LiCuspB");
72  CHECK(f.cutoff_radius == Approx(6.0));
73 
74  // set finite difference displacement parameter
75  const RealType h = 0.0001;
76 
77  // accuracy tolerance depends on floating point precision
78  const RealType eps = (sizeof(RealType) < 6 ? 0.002 : 0.0001);
79 
80  // evaluate a couple ways at a given radius
81  RealType r = 0.04;
82  RealType val = f.evaluate(r);
83  RealType dudr = 10000000.0; // initialize to a wrong value
84  RealType d2udr2 = 15000000.0; // initialize to a wrong value
85  RealType val_2 = f.evaluate(r, dudr, d2udr2);
86 
87  CHECK(val == Approx(-0.1970331287).epsilon(eps));
88  CHECK(val_2 == Approx(-0.1970331287).epsilon(eps));
89  CHECK(val == Approx(val_2));
90 
91  // Finite difference to verify the spatial derivatives
92  RealType r_ph = r + h;
93  RealType r_mh = r - h;
94  RealType dudr_ph = 20000000.0; // initialize to a wrong value
95  RealType dudr_mh = 30000000.0; // initialize to a wrong value
96  RealType d2udr2_ph = 40000000.0; // initialize to a wrong value
97  RealType d2udr2_mh = 50000000.0; // initialize to a wrong value
98  RealType val_ph = f.evaluate(r_ph, dudr_ph, d2udr2_ph);
99  RealType val_mh = f.evaluate(r_mh, dudr_mh, d2udr2_mh);
100  RealType approx_dudr = (val_ph - val_mh) / (2 * h);
101  RealType approx_d2udr2 = (dudr_ph - dudr_mh) / (2 * h);
102  CHECK(dudr == Approx(approx_dudr).epsilon(eps));
103  CHECK(d2udr2 == Approx(approx_d2udr2).epsilon(eps));
104 
105  // currently the d3udr3_3 function is not implemented
106  //RealType dudr_3;
107  //RealType d2udr2_3;
108  //RealType d3udr3_3;
109  //RealType val_3 = f.evaluate(r, dudr_3, d2udr2_3, d3udr3_3);
110  //CHECK(val == Approx(val_3));
111  //CHECK(dudr == Approx(dudr_3));
112  //CHECK(d2udr2 == Approx(d2udr2_3));
113 
114  // Now let's do finite difference checks for the parameter derivatives
115 
116  // Adjust this based on the number of variational parameters
117  const int nparam = 4;
118 
119  // Outer vector is over parameters
120  // Inner (TinyVector) is the parameter derivative of the value, first, and second derivatives.
121  std::vector<TinyVector<RealType, 3>> param_derivs(nparam);
122 
123  f.evaluateDerivatives(r, param_derivs);
124 
125  optimize::VariableSet var_param;
126  f.checkInVariablesExclusive(var_param);
127  var_param.resetIndex();
128  REQUIRE(var_param.size_of_active() == nparam);
129 
130  for (int i = 0; i < nparam; i++)
131  {
132  const std::string var_name = var_param.name(i);
133  const RealType old_param = std::real(var_param[var_name]);
134  //std::cout << "checking parameter " << var_name << std::endl;
135 
136  RealType dudr_h = 10000000.0; // initialize to a wrong value
137  RealType d2udr2_h = 10000000.0; // initialize to a wrong value
138  var_param[var_name] = old_param + h;
139  f.resetParametersExclusive(var_param);
140  RealType val_h = f.evaluate(r, dudr_h, d2udr2_h);
141 
142  RealType dudr_m = 20000000.0; // initialize to a wrong value
143  RealType d2udr2_m = 20000000.0; // initialize to a wrong value
144  var_param[var_name] = old_param - h;
145  f.resetParametersExclusive(var_param);
146  RealType val_m = f.evaluate(r, dudr_m, d2udr2_m);
147 
148  const RealType val_dp = (val_h - val_m) / (2.0 * h);
149  CHECK(val_dp == Approx(param_derivs[i][0]).epsilon(eps));
150 
151  const RealType dudr_dp = (dudr_h - dudr_m) / (2.0 * h);
152  CHECK(dudr_dp == Approx(param_derivs[i][1]).epsilon(eps));
153 
154  const RealType d2udr2_dp = (d2udr2_h - d2udr2_m) / (2.0 * h);
155  CHECK(d2udr2_dp == Approx(param_derivs[i][2]).epsilon(eps));
156 
157  var_param[var_name] = old_param;
158  f.resetParametersExclusive(var_param);
159  }
160 
161  // Could do finite differences to verify the parameter derivatives
162 }
163 } // namespace qmcplusplus
class that handles xmlDoc
Definition: Libxml2Doc.h:76
bool evaluateDerivatives(real_type r, std::vector< TinyVector< real_type, 3 >> &derivs) override
compute derivatives of U(r), dU/dr, and d^2U/dr^2 with respect to the variational parameters ...
QMCTraits::RealType real
helper functions for EinsplineSetBuilder
Definition: Configuration.h:43
bool Opt_R0
true, if R0 is optimizable
TEST_CASE("complex_helper", "[type_traits]")
Functor designed to encode short-ranged structure near a nuclear cusp.
xmlNodePtr getRoot()
Definition: Libxml2Doc.h:88
void resetIndex()
reset Index
Communicate * Controller
Global Communicator for a process.
Definition: Communicate.cpp:35
Wrapping information on parallelism.
Definition: Communicate.h:68
real_type A
variable that controls the cusp
bool put(xmlNodePtr cur) override
read in information about the functor from an xml node
REQUIRE(std::filesystem::exists(filename))
class to handle a set of variables that can be modified during optimizations
Definition: VariableSet.h:49
int size_of_active() const
return the number of active variables
Definition: VariableSet.h:78
bool Opt_A
true, if A is optimizable
real_type evaluate(real_type r) const
compute U(r) at a particular value of r
void resetParametersExclusive(const opt_variables_type &active) override
reset the parameters during optimizations.
bool Opt_B
true, if B variables are optimizable
void checkInVariablesExclusive(opt_variables_type &active) override
check in variational parameters to the global list of parameters used by the optimizer.
bool parseFromString(const std::string_view data)
Definition: Libxml2Doc.cpp:204
CHECK(log_values[0]==ComplexApprox(std::complex< double >{ 5.603777579195571, -6.1586603331188225 }))
const std::string & name(int i) const
return the name of i-th variable
Definition: VariableSet.h:189
std::vector< real_type > B
variables that add detail through an expansion in sigmoidal functions
optimize::VariableSet::real_type real_type
typedef for real values
real_type R0
the soft cutoff distance that controls how short ranged the functor is