QMCPACK
test_user_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: 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 "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("UserJastrowFunctor", "[wavefunction]")
23 {
25 
27 
28  UserFunctor<RealType> uf("test_functor");
29 
30  // This may or may not need to be present
31  uf.setCusp(1.0);
32 
33  // Adjust this based on the example XML block
34  const char* xmltext = R"(<tmp>
35  <var name="B" id="j_B">2.0</var>
36  </tmp>)";
37 
39  bool okay = doc.parseFromString(xmltext);
40  REQUIRE(okay);
41 
42  xmlNodePtr root = doc.getRoot();
43  uf.put(root);
44 
45  // Adjust these
46  CHECK(uf.A == Approx(1.0));
47  CHECK(uf.B == Approx(2.0));
48 
49  RealType r = 1.0;
50  RealType val = uf.evaluate(r);
51 
52  RealType dudr;
53  RealType d2udr2;
54  RealType val_2 = uf.evaluate(r, dudr, d2udr2);
55 
56  CHECK(val == Approx(val_2));
57 
58  // Finite difference to verify the spatial derivatives
59 
60  RealType h = 0.01;
61  RealType r_plus_h = r + h;
62  RealType r_minus_h = r - h;
63  RealType val_plus_h = uf.evaluate(r_plus_h);
64  RealType val_minus_h = uf.evaluate(r_minus_h);
65 
66  RealType approx_dudr = (val_plus_h - val_minus_h) / (2 * h);
67  CHECK(dudr == Approx(approx_dudr).epsilon(h));
68 
69  RealType approx_d2udr2 = (val_plus_h + val_minus_h - 2 * val) / (h * h);
70  CHECK(d2udr2 == Approx(approx_d2udr2).epsilon(h));
71 
72 
73  RealType dudr_3;
74  RealType d2udr2_3;
75  RealType d3udr3_3;
76  RealType val_3 = uf.evaluate(r, dudr_3, d2udr2_3, d3udr3_3);
77 
78  CHECK(val == Approx(val_3));
79  CHECK(dudr == Approx(dudr_3));
80  CHECK(d2udr2 == Approx(d2udr2_3));
81 
82 
83  // Adjust this based on the number of variational parameters
84  const int nparam = 1;
85 
86  // Outer vector is over parameters
87  // Inner (TinyVector) is the parameter derivative of the value, first, and second derivatives.
88  std::vector<TinyVector<RealType, 3>> param_derivs(nparam);
89 
90  uf.evaluateDerivatives(r, param_derivs);
91 
92  optimize::VariableSet var_param;
93  uf.checkInVariablesExclusive(var_param);
94  var_param.resetIndex();
95  REQUIRE(var_param.size_of_active() == nparam);
96 
97 
98  for (int i = 0; i < nparam; i++)
99  {
100  std::string var_name = var_param.name(i);
101  RealType old_param = std::real(var_param[var_name]);
102  var_param[var_name] = old_param + h;
103 
104  uf.resetParametersExclusive(var_param);
105 
106  RealType dudr_h;
107  RealType d2udr2_h;
108  RealType val_h = uf.evaluate(r, dudr_h, d2udr2_h);
109 
110  RealType val_dp = (val_h - val) / h;
111  CHECK(val_dp == Approx(param_derivs[i][0]).epsilon(h));
112 
113  RealType dudr_dp = (dudr_h - dudr) / h;
114  CHECK(dudr_dp == Approx(param_derivs[i][1]).epsilon(h));
115 
116  RealType d2udr2_dp = (d2udr2_h - d2udr2) / h;
117  CHECK(d2udr2_dp == Approx(param_derivs[i][2]).epsilon(h));
118 
119  var_param[var_name] = old_param;
120  uf.resetParametersExclusive(var_param);
121  }
122 
123  // Could do finite differences to verify the parameter derivatives
124 }
125 } // namespace qmcplusplus
class that handles xmlDoc
Definition: Libxml2Doc.h:76
QMCTraits::RealType real
helper functions for EinsplineSetBuilder
Definition: Configuration.h:43
bool put(xmlNodePtr cur) override
process xmlnode and registers variables to optimize
Definition: UserFunctor.h:263
void checkInVariablesExclusive(opt_variables_type &active) override
check in variational parameters to the global list of parameters used by the optimizer.
Definition: UserFunctor.h:307
TEST_CASE("complex_helper", "[type_traits]")
void setCusp(real_type cusp) override
empty virtual function to help builder classes
Definition: UserFunctor.h:70
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
Implements the function f = A*r/(B*r + 1) - A/B.
Definition: UserFunctor.h:47
REQUIRE(std::filesystem::exists(filename))
void resetParametersExclusive(const opt_variables_type &active) override
reset the parameters during optimizations.
Definition: UserFunctor.h:317
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
real_type A
Value.
Definition: UserFunctor.h:52
User-defined functor.
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
bool evaluateDerivatives(real_type r, std::vector< TinyVector< real_type, 3 >> &derivs) override
Definition: UserFunctor.h:213
optimize::VariableSet::real_type real_type
typedef for real values
real_type evaluate(real_type r) const
Definition: UserFunctor.h:85
real_type B
Value.
Definition: UserFunctor.h:60