QMCPACK
test_pade_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) 2016 Jeongnim Kim and QMCPACK developers.
6 //
7 // File developed by: Mark Dewing, markdewing@gmail.com, University of Illinois at Urbana-Champaign
8 //
9 // File created by: Mark Dewing, markdewing@gmail.com, University of Illinois at Urbana-Champaign
10 //////////////////////////////////////////////////////////////////////////////////////
11 
12 
13 #include "catch.hpp"
14 
15 #include "OhmmsData/Libxml2Doc.h"
16 #include "OhmmsPETE/OhmmsMatrix.h"
17 #include "Particle/ParticleSet.h"
24 
25 
26 #include <stdio.h>
27 #include <string>
28 
29 using std::string;
30 
31 namespace qmcplusplus
32 {
33 TEST_CASE("Pade functor", "[wavefunction]")
34 {
35  double A = -0.25;
36  double B = 0.1;
37  PadeFunctor<double> pf("test_functor");
38  pf.B0 = B;
39  pf.setCusp(A);
40 
41  double r = 1.2;
42  double u = pf.evaluate(r);
43  CHECK(u == Approx(2.232142857142857));
44 }
45 
46 TEST_CASE("Pade2 functor", "[wavefunction]")
47 {
48  double A = 0.8;
49  double B = 5.0;
50  double C = -0.1;
51  Pade2ndOrderFunctor<double> pf2("test_functor");
52  pf2.A = A;
53  pf2.B = B;
54  pf2.C = C;
55  pf2.reset();
56 
57  double r = 1.2;
58  double u = pf2.evaluate(r);
59  CHECK(u == Approx(0.11657142857142856));
60 }
61 
62 
63 TEST_CASE("Pade Jastrow", "[wavefunction]")
64 {
66 
67  const SimulationCell simulation_cell;
68  ParticleSet ions_(simulation_cell);
69  ParticleSet elec_(simulation_cell);
70 
71  // Need 1 electron and 1 proton, somehow
72  ions_.setName("ion");
73  ions_.create({1});
74  ions_.R[0] = {0.0, 0.0, 0.0};
75  elec_.setName("elec");
76  elec_.create({2, 0});
77  elec_.R[0] = {-0.28, 0.0225, -2.709};
78  elec_.R[1] = {-1.08389, 1.9679, -0.0128914};
79  SpeciesSet& tspecies = elec_.getSpeciesSet();
80  int upIdx = tspecies.addSpecies("u");
81  int downIdx = tspecies.addSpecies("d");
82  int chargeIdx = tspecies.addAttribute("charge");
83  int massIdx = tspecies.addAttribute("mass");
84  tspecies(chargeIdx, upIdx) = -1;
85  tspecies(chargeIdx, downIdx) = -1;
86  tspecies(massIdx, upIdx) = 1;
87  tspecies(massIdx, downIdx) = 1;
88 
89  const char* particles = R"(<tmp>
90 <jastrow name="Jee" type="Two-Body" function="pade">
91  <correlation speciesA="u" speciesB="u">
92  <var id="juu_b" name="B">0.1</var>
93  </correlation>
94 </jastrow>
95 </tmp>
96 )";
98  bool okay = doc.parseFromString(particles);
99  REQUIRE(okay);
100 
101  xmlNodePtr root = doc.getRoot();
102 
103  xmlNodePtr jas1 = xmlFirstElementChild(root);
104 
105  // cusp = -0.25
106  // r_ee = 3.42050023755
107  RadialJastrowBuilder jastrow(c, elec_);
108  std::unique_ptr<WaveFunctionComponent> jas(jastrow.buildComponent(jas1));
109 
110  // update all distance tables
111  elec_.update();
112 
113  double logpsi_real = std::real(jas->evaluateLog(elec_, elec_.G, elec_.L));
114  CHECK(logpsi_real == Approx(-1.862821769493147));
115 }
116 
117 TEST_CASE("Pade2 Jastrow", "[wavefunction]")
118 {
120 
122  auto& simulation_cell(ptcl.getSimulationCell());
123  auto ions_uptr = std::make_unique<ParticleSet>(simulation_cell);
124  auto elec_uptr = std::make_unique<ParticleSet>(simulation_cell);
125  ParticleSet& ions_(*ions_uptr);
126  ParticleSet& elec_(*elec_uptr);
127 
128  ions_.setName("ion0");
129  ptcl.addParticleSet(std::move(ions_uptr));
130  ions_.create({1});
131  ions_.R[0] = {0.0, 0.0, 0.0};
132  SpeciesSet& ispecies = ions_.getSpeciesSet();
133  int HIdx = ispecies.addSpecies("H");
134  int ichargeIdx = ispecies.addAttribute("charge");
135  ispecies(ichargeIdx, HIdx) = 1.0;
136 
137  elec_.setName("e");
138  ptcl.addParticleSet(std::move(elec_uptr));
139  elec_.create({1, 1});
140  elec_.R[0] = {0.5, 0.5, 0.5};
141  elec_.R[1] = {-0.5, -0.5, -0.5};
142 
143  SpeciesSet& tspecies = elec_.getSpeciesSet();
144  int upIdx = tspecies.addSpecies("u");
145  int downIdx = tspecies.addSpecies("d");
146  int massIdx = tspecies.addAttribute("mass");
147  int chargeIdx = tspecies.addAttribute("charge");
148  tspecies(massIdx, upIdx) = 1.0;
149  tspecies(massIdx, downIdx) = 1.0;
150  tspecies(chargeIdx, upIdx) = -1.0;
151  tspecies(massIdx, downIdx) = -1.0;
152  // Necessary to set mass
153  elec_.resetGroups();
154 
155  ions_.update();
156  elec_.addTable(elec_);
157  elec_.addTable(ions_);
158  elec_.update();
159 
160  const char* jasxml = R"(<wavefunction name="psi0" target="e">
161  <jastrow name="J1" type="One-Body" function="pade2" print="yes" source="ion0">
162  <correlation elementType="H">
163  <var id="J1H_A" name="A">0.8</var>
164  <var id="J1H_B" name="B">5.0</var>
165  <var id="J1H_C" name="C">-0.1</var>
166  </correlation>
167  </jastrow>
168 </wavefunction>
169 )";
171  bool okay = doc.parseFromString(jasxml);
172  REQUIRE(okay);
173 
174  xmlNodePtr jas1 = doc.getRoot();
175 
176  // update all distance tables
177  elec_.update();
178  WaveFunctionFactory wf_factory(elec_, ptcl.getPool(), c);
179  RuntimeOptions runtime_options;
180  auto twf_ptr = wf_factory.buildTWF(jas1, runtime_options);
181  auto& twf(*twf_ptr);
182  twf.setMassTerm(elec_);
183  twf.evaluateLog(elec_);
184  twf.prepareGroup(elec_, 0);
185 
186  auto& twf_component_list = twf.getOrbitals();
187 
188  opt_variables_type active;
189  twf.checkInVariables(active);
190  active.removeInactive();
191  int nparam = active.size_of_active();
192  REQUIRE(nparam == 3);
193 
195  Vector<ValueType> dlogpsi(nparam);
196  Vector<ValueType> dhpsioverpsi(nparam);
197  //twf.evaluateDerivatives(elec_, active, dlogpsi, dhpsioverpsi);
198  twf_component_list[0]->evaluateDerivatives(elec_, active, dlogpsi, dhpsioverpsi);
199 
200  // Numbers not validated
201  std::vector<ValueType> expected_dlogpsi = {-0.3249548841, 0.0376658437, -0.2814191847};
202  std::vector<ValueType> expected_dhpsioverpsi = {0.0146266746, 0.0031788682, 0.4554097531};
203  for (int i = 0; i < nparam; i++)
204  {
205  CHECK(dlogpsi[i] == ValueApprox(expected_dlogpsi[i]));
206  CHECK(dhpsioverpsi[i] == ValueApprox(expected_dhpsioverpsi[i]));
207  }
208 }
209 } // namespace qmcplusplus
real_type B0
input B
Definition: PadeFunctors.h:47
void setName(const std::string &aname)
Definition: ParticleSet.h:237
class that handles xmlDoc
Definition: Libxml2Doc.h:76
int addSpecies(const std::string &aname)
When a name species does not exist, add a new species.
Definition: SpeciesSet.cpp:33
QMCTraits::RealType real
helper functions for EinsplineSetBuilder
Definition: Configuration.h:43
real_type evaluate(real_type r) const
Definition: PadeFunctors.h:357
TEST_CASE("complex_helper", "[type_traits]")
std::unique_ptr< WaveFunctionComponent > buildComponent(xmlNodePtr cur) override
process a xml node at cur
real_type A
coefficients
Definition: PadeFunctors.h:327
void addParticleSet(std::unique_ptr< ParticleSet > &&p)
add a ParticleSet* to the pool with its ownership transferred ParticleSet built outside the ParticleS...
xmlNodePtr getRoot()
Definition: Libxml2Doc.h:88
JastrowBuilder using an analytic 1d functor Should be able to eventually handle all one and two body ...
void update(bool skipSK=false)
update the internal data
Communicate * Controller
Global Communicator for a process.
Definition: Communicate.cpp:35
ParticleLaplacian L
laplacians of the particles
Definition: ParticleSet.h:85
int addAttribute(const std::string &aname)
for a new attribute, allocate the data, !More often used to get the index of a species ...
Definition: SpeciesSet.cpp:45
Wrapping information on parallelism.
Definition: Communicate.h:68
int addTable(const ParticleSet &psrc, DTModes modes=DTModes::ALL_OFF)
add a distance table
Specialized paritlce class for atomistic simulations.
Definition: ParticleSet.h:55
QTBase::ValueType ValueType
Definition: Configuration.h:60
REQUIRE(std::filesystem::exists(filename))
Manage a collection of ParticleSet objects.
ParticleGradient G
gradients of the particles
Definition: ParticleSet.h:83
Factory class to build a many-body wavefunction.
class to handle a set of variables that can be modified during optimizations
Definition: VariableSet.h:49
Functors which implement Pade functions.
ParticlePos R
Position.
Definition: ParticleSet.h:79
int size_of_active() const
return the number of active variables
Definition: VariableSet.h:78
real_type evaluate(real_type r) const
Definition: PadeFunctors.h:89
Implements a Pade Function .
Definition: PadeFunctors.h:38
SpeciesSet & getSpeciesSet()
retrun the SpeciesSet of this particle set
Definition: ParticleSet.h:231
void create(const std::vector< int > &agroup)
create grouped particles
bool parseFromString(const std::string_view data)
Definition: Libxml2Doc.cpp:204
CHECK(log_values[0]==ComplexApprox(std::complex< double >{ 5.603777579195571, -6.1586603331188225 }))
LatticeGaussianProduct::ValueType ValueType
Declaration of a WaveFunctionFactory.
Declaration of WaveFunctionComponent.
Custom container for set of attributes for a set of species.
Definition: SpeciesSet.h:33
const PoolType & getPool() const
get the Pool object
double B(double x, int k, int i, const std::vector< double > &t)
void removeInactive()
remove inactive variables and trim the internal data
void setCusp(real_type cusp) override
empty virtual function to help builder classes
Definition: PadeFunctors.h:70
const auto & getSimulationCell() const
get simulation cell
Declaration of ParticleSetPool.
void reset() override
reset the internal variables.
Definition: PadeFunctors.h:347