QMCPACK
test_polynomial_eeI_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: Ye Luo, yeluo@anl.gov, Argonne National Laboratory
8 //
9 // File created by: Ye Luo, yeluo@anl.gov, Argonne National Laboratory
10 //////////////////////////////////////////////////////////////////////////////////////
11 
12 
13 #include "catch.hpp"
14 
15 #include "OhmmsData/Libxml2Doc.h"
16 #include "OhmmsPETE/OhmmsMatrix.h"
17 #include "Particle/ParticleSet.h"
23 #include <ResourceCollection.h>
25 
26 #include <stdio.h>
27 #include <string>
28 
29 using std::string;
30 
31 namespace qmcplusplus
32 {
36 
37 TEST_CASE("PolynomialFunctor3D functor zero", "[wavefunction]")
38 {
39  PolynomialFunctor3D functor("test_functor");
40 
41  double r = 1.2;
42  double u = functor.evaluate(r, r, r);
43  REQUIRE(u == 0.0);
44 }
45 
47 {
49 
50  const SimulationCell simulation_cell;
51  ParticleSet ions_(simulation_cell, kind_selected);
52  ParticleSet elec_(simulation_cell, kind_selected);
53 
54  ions_.setName("ion");
55  ions_.create({2});
56  ions_.R[0] = {2.0, 0.0, 0.0};
57  ions_.R[1] = {-2.0, 0.0, 0.0};
58  SpeciesSet& source_species(ions_.getSpeciesSet());
59  source_species.addSpecies("O");
60  ions_.update();
61 
62  elec_.setName("elec");
63  elec_.create({2, 2});
64  elec_.R[0] = {1.00, 0.0, 0.0};
65  elec_.R[1] = {0.0, 0.0, 0.0};
66  elec_.R[2] = {-1.00, 0.0, 0.0};
67  elec_.R[3] = {0.0, 0.0, 2.0};
68  SpeciesSet& target_species(elec_.getSpeciesSet());
69  int upIdx = target_species.addSpecies("u");
70  int downIdx = target_species.addSpecies("d");
71  int chargeIdx = target_species.addAttribute("charge");
72  target_species(chargeIdx, upIdx) = -1;
73  target_species(chargeIdx, downIdx) = -1;
74  //elec_.resetGroups();
75 
76  const char* particles = R"(<tmp>
77  <jastrow name="J3" type="eeI" function="polynomial" source="ion" print="yes">
78  <correlation ispecies="O" especies="u" isize="3" esize="3" rcut="10">
79  <coefficients id="uuO" type="Array" optimize="yes"> 8.227710241e-06 2.480817653e-06 -5.354068112e-06 -1.112644787e-05 -2.208006078e-06 5.213121933e-06 -1.537865869e-05 8.899030233e-06 6.257255156e-06 3.214580988e-06 -7.716743107e-06 -5.275682077e-06 -1.778457637e-06 7.926231121e-06 1.767406868e-06 5.451359059e-08 2.801423724e-06 4.577282736e-06 7.634608083e-06 -9.510673173e-07 -2.344131575e-06 -1.878777219e-06 3.937363358e-07 5.065353773e-07 5.086724869e-07 -1.358768154e-07</coefficients>
80  </correlation>
81  <correlation ispecies="O" especies1="u" especies2="d" isize="3" esize="3" rcut="10">
82  <coefficients id="udO" type="Array" optimize="yes"> -6.939530224e-06 2.634169299e-05 4.046077477e-05 -8.002682388e-06 -5.396795988e-06 6.697370507e-06 5.433953051e-05 -6.336849668e-06 3.680471431e-05 -2.996059772e-05 1.99365828e-06 -3.222705626e-05 -8.091669063e-06 4.15738535e-06 4.843939112e-06 3.563650208e-07 3.786332474e-05 -1.418336941e-05 2.282691374e-05 1.29239286e-06 -4.93580873e-06 -3.052539228e-06 9.870288001e-08 1.844286407e-06 2.970561871e-07 -4.364303677e-08</coefficients>
83  </correlation>
84  </jastrow>
85 </tmp>
86 )";
88  bool okay = doc.parseFromString(particles);
89  REQUIRE(okay);
90 
91  xmlNodePtr root = doc.getRoot();
92 
93  xmlNodePtr jas_eeI = xmlFirstElementChild(root);
94 
95  eeI_JastrowBuilder jastrow(c, elec_, ions_);
96  std::unique_ptr<WaveFunctionComponent> jas(jastrow.buildComponent(jas_eeI));
97 
99  auto j3_uptr = jastrow.buildComponent(jas_eeI);
100  WaveFunctionComponent* j3 = dynamic_cast<J3Type*>(j3_uptr.get());
101  REQUIRE(j3 != nullptr);
102 
103  // update all distance tables
104  elec_.update();
105 
106  double logpsi_real = std::real(j3->evaluateLog(elec_, elec_.G, elec_.L));
107  CHECK(logpsi_real == Approx(-1.193457749)); // note: number not validated
108 
109  double KE = -0.5 * (Dot(elec_.G, elec_.G) + Sum(elec_.L));
110  CHECK(KE == Approx(-0.058051245)); // note: number not validated
111 
113  using PosType = QMCTraits::PosType;
114 
115  // set virtutal particle position
116  PosType newpos(0.3, 0.2, 0.5);
117 
118  elec_.makeVirtualMoves(newpos);
119  std::vector<ValueType> ratios(elec_.getTotalNum());
120  j3->evaluateRatiosAlltoOne(elec_, ratios);
121 
122  CHECK(std::real(ratios[0]) == Approx(0.8744938582));
123  CHECK(std::real(ratios[1]) == Approx(1.0357541137));
124  CHECK(std::real(ratios[2]) == Approx(0.8302245609));
125  CHECK(std::real(ratios[3]) == Approx(0.7987703724));
126 
127  elec_.makeMove(0, newpos - elec_.R[0]);
128  PsiValue ratio_0 = j3->ratio(elec_, 0);
129  elec_.rejectMove(0);
130 
131  elec_.makeMove(1, newpos - elec_.R[1]);
132  PsiValue ratio_1 = j3->ratio(elec_, 1);
133  elec_.rejectMove(1);
134 
135  elec_.makeMove(2, newpos - elec_.R[2]);
136  PsiValue ratio_2 = j3->ratio(elec_, 2);
137  elec_.rejectMove(2);
138 
139  elec_.makeMove(3, newpos - elec_.R[3]);
140  PsiValue ratio_3 = j3->ratio(elec_, 3);
141  elec_.rejectMove(3);
142 
143  CHECK(std::real(ratio_0) == Approx(0.8744938582));
144  CHECK(std::real(ratio_1) == Approx(1.0357541137));
145  CHECK(std::real(ratio_2) == Approx(0.8302245609));
146  CHECK(std::real(ratio_3) == Approx(0.7987703724));
147 
148  UniqueOptObjRefs opt_obj_refs;
149  j3->extractOptimizableObjectRefs(opt_obj_refs);
150  REQUIRE(opt_obj_refs.size() == 2);
151 
152  opt_variables_type optvars;
155 
156  for (OptimizableObject& obj : opt_obj_refs)
157  obj.checkInVariablesExclusive(optvars);
158  optvars.resetIndex();
159  const int NumOptimizables(optvars.size());
160  j3->checkOutVariables(optvars);
161  dlogpsi.resize(NumOptimizables);
162  dhpsioverpsi.resize(NumOptimizables);
163  j3->evaluateDerivatives(elec_, optvars, dlogpsi, dhpsioverpsi);
164 
165  app_log() << std::endl << "reporting dlogpsi and dhpsioverpsi" << std::scientific << std::endl;
166  for (int iparam = 0; iparam < NumOptimizables; iparam++)
167  app_log() << "param=" << iparam << " : " << dlogpsi[iparam] << " " << dhpsioverpsi[iparam] << std::endl;
168  app_log() << std::endl;
169 
170  CHECK(std::real(dlogpsi[43]) == Approx(1.3358726814e+05));
171  CHECK(std::real(dhpsioverpsi[43]) == Approx(-2.3246270644e+05));
172 
174  dlogpsiWF.resize(NumOptimizables);
175  j3->evaluateDerivativesWF(elec_, optvars, dlogpsiWF);
176  for (int i = 0; i < NumOptimizables; i++)
177  CHECK(dlogpsi[i] == ValueApprox(dlogpsiWF[i]));
178 
179  VirtualParticleSet VP(elec_, 2);
180  std::vector<PosType> newpos2(2);
181  std::vector<ValueType> ratios2(2);
182  newpos2[0] = newpos - elec_.R[1];
183  newpos2[1] = PosType(0.2, 0.5, 0.3) - elec_.R[1];
184  VP.makeMoves(elec_, 1, newpos2);
185  j3->evaluateRatios(VP, ratios2);
186 
187  CHECK(std::real(ratios2[0]) == Approx(1.0357541137));
188  CHECK(std::real(ratios2[1]) == Approx(1.0257141422));
189 
190  std::fill(ratios2.begin(), ratios2.end(), 0);
191  Matrix<ValueType> dratio(2, NumOptimizables);
192  j3->evaluateDerivRatios(VP, optvars, ratios2, dratio);
193 
194  CHECK(std::real(ratios2[0]) == Approx(1.0357541137));
195  CHECK(std::real(ratios2[1]) == Approx(1.0257141422));
196  CHECK(std::real(dratio[0][43]) == Approx(-1.4282569e+03));
197 
198  // testing batched interfaces
199  ResourceCollection pset_res("test_pset_res");
200  ResourceCollection wfc_res("test_wfc_res");
201 
202  elec_.createResource(pset_res);
203  j3->createResource(wfc_res);
204 
205  // make a clones
206  ParticleSet elec_clone(elec_);
207  auto j3_clone = j3->makeClone(elec_clone);
208 
209  // testing batched interfaces
210  RefVectorWithLeader<ParticleSet> p_ref_list(elec_, {elec_, elec_clone});
211  RefVectorWithLeader<WaveFunctionComponent> j3_ref_list(*j3, {*j3, *j3_clone});
212 
213  ResourceCollectionTeamLock<ParticleSet> mw_pset_lock(pset_res, p_ref_list);
214  ResourceCollectionTeamLock<WaveFunctionComponent> mw_wfc_lock(wfc_res, j3_ref_list);
215 
216  std::vector<bool> isAccepted(2, true);
217  ParticleSet::mw_update(p_ref_list);
218  j3->mw_recompute(j3_ref_list, p_ref_list, isAccepted);
219 
220  // test NLPP related APIs
221  const int nknot = 3;
222  VirtualParticleSet vp(elec_, nknot), vp_clone(elec_clone, nknot);
223  RefVectorWithLeader<VirtualParticleSet> vp_list(vp, {vp, vp_clone});
224  ResourceCollection vp_res("test_vp_res");
225  vp.createResource(vp_res);
226  ResourceCollectionTeamLock<VirtualParticleSet> mw_vp_lock(vp_res, vp_list);
227 
228  const int ei_table_index = elec_.addTable(ions_);
229  const auto& ei_table1 = elec_.getDistTableAB(ei_table_index);
230  // make virtual move of elec 0, reference ion 1
231  NLPPJob<RealType> job1(1, 0, ei_table1.getDistances()[0][1], -ei_table1.getDisplacements()[0][1]);
232  const auto& ei_table2 = elec_clone.getDistTableAB(ei_table_index);
233  // make virtual move of elec 1, reference ion 3
234  NLPPJob<RealType> job2(3, 1, ei_table2.getDistances()[1][3], -ei_table2.getDisplacements()[1][3]);
235 
236  std::vector<PosType> deltaV1{{0.1, 0.2, 0.3}, {0.1, 0.3, 0.2}, {0.2, 0.1, 0.3}};
237  std::vector<PosType> deltaV2{{0.02, 0.01, 0.03}, {0.02, 0.03, 0.01}, {0.03, 0.01, 0.02}};
238 
239  VirtualParticleSet::mw_makeMoves(vp_list, p_ref_list, {deltaV1, deltaV2}, {job1, job2}, false);
240 
241  std::vector<std::vector<ValueType>> nlpp_ratios(2);
242  nlpp_ratios[0].resize(nknot);
243  nlpp_ratios[1].resize(nknot);
244  j3->mw_evaluateRatios(j3_ref_list, RefVectorWithLeader<const VirtualParticleSet>(vp, {vp, vp_clone}), nlpp_ratios);
245 
246  CHECK(ValueApprox(nlpp_ratios[0][0]) == ValueType(1.0273599625));
247  CHECK(ValueApprox(nlpp_ratios[0][1]) == ValueType(1.0227555037));
248  CHECK(ValueApprox(nlpp_ratios[0][2]) == ValueType(1.0473958254));
249  CHECK(ValueApprox(nlpp_ratios[1][0]) == ValueType(1.0013145208));
250  CHECK(ValueApprox(nlpp_ratios[1][1]) == ValueType(1.0011137724));
251  CHECK(ValueApprox(nlpp_ratios[1][2]) == ValueType(1.0017225742));
252 }
253 
254 TEST_CASE("PolynomialFunctor3D Jastrow", "[wavefunction]")
255 {
258 }
259 } // namespace qmcplusplus
void resize(size_type n, Type_t val=Type_t())
Resize the container.
Definition: OhmmsVector.h:166
DynamicCoordinateKind
enumerator for DynamicCoordinates kinds
void setName(const std::string &aname)
Definition: ParticleSet.h:237
T Sum(const ParticleAttrib< T > &pa)
class that handles xmlDoc
Definition: Libxml2Doc.h:76
virtual LogValue evaluateLog(const ParticleSet &P, ParticleSet::ParticleGradient &G, ParticleSet::ParticleLaplacian &L)=0
evaluate the value of the WaveFunctionComponent from scratch
virtual void evaluateDerivRatios(const VirtualParticleSet &VP, const opt_variables_type &optvars, std::vector< ValueType > &ratios, Matrix< ValueType > &dratios)
evaluate ratios to evaluate the non-local PP
WaveFunctionComponent::PsiValue PsiValue
Definition: SlaterDet.cpp:25
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
virtual void mw_recompute(const RefVectorWithLeader< WaveFunctionComponent > &wfc_list, const RefVectorWithLeader< ParticleSet > &p_list, const std::vector< bool > &recompute) const
QTBase::RealType RealType
Definition: Configuration.h:58
size_t getTotalNum() const
Definition: ParticleSet.h:493
void test_J3_polynomial3D(const DynamicCoordinateKind kind_selected)
std::ostream & app_log()
Definition: OutputManager.h:65
TEST_CASE("complex_helper", "[type_traits]")
A ParticleSet that handles virtual moves of a selected particle of a given physical ParticleSet Virtu...
void makeMoves(const ParticleSet &refp, int jel, const std::vector< PosType > &deltaV, bool sphere=false, int iat=-1)
move virtual particles to new postions and update distance tables
xmlNodePtr getRoot()
Definition: Libxml2Doc.h:88
void createResource(ResourceCollection &collection) const
initialize a shared resource and hand it to a collection
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
T Dot(const ParticleAttrib< TinyVector< T, D >> &pa, const ParticleAttrib< TinyVector< T, D >> &pb)
std::complex< QTFull::RealType > LogValue
const DistanceTableAB & getDistTableAB(int table_ID) const
get a distance table by table_ID and dyanmic_cast to DistanceTableAB
Wrapping information on parallelism.
Definition: Communicate.h:68
An abstract class for a component of a many-body trial wave function.
virtual void evaluateDerivatives(ParticleSet &P, const opt_variables_type &optvars, Vector< ValueType > &dlogpsi, Vector< ValueType > &dhpsioverpsi)=0
Compute the derivatives of both the log of the wavefunction and kinetic energy with respect to optimi...
int addTable(const ParticleSet &psrc, DTModes modes=DTModes::ALL_OFF)
add a distance table
Specialized paritlce class for atomistic simulations.
Definition: ParticleSet.h:55
real_type evaluate(real_type r_12, real_type r_1I, real_type r_2I) const
virtual void checkOutVariables(const opt_variables_type &active)
check out variational optimizable variables
QTBase::ValueType ValueType
Definition: Configuration.h:60
REQUIRE(std::filesystem::exists(filename))
void rejectMove(Index_t iat)
reject a proposed move in regular mode
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
virtual void createResource(ResourceCollection &collection) const
initialize a shared resource and hand it to a collection
ParticlePos R
Position.
Definition: ParticleSet.h:79
QTBase::PosType PosType
Definition: Configuration.h:61
virtual void evaluateDerivativesWF(ParticleSet &P, const opt_variables_type &optvars, Vector< ValueType > &dlogpsi)
Compute the derivatives of the log of the wavefunction with respect to optimizable parameters...
static void mw_makeMoves(const RefVectorWithLeader< VirtualParticleSet > &vp_list, const RefVectorWithLeader< ParticleSet > &p_list, const RefVector< const std::vector< PosType >> &deltaV_list, const RefVector< const NLPPJob< RealType >> &joblist, bool sphere)
virtual PsiValue ratio(ParticleSet &P, int iat)=0
evaluate the ratio of the new to old WaveFunctionComponent value
virtual void evaluateRatios(const VirtualParticleSet &VP, std::vector< ValueType > &ratios)
evaluate ratios to evaluate the non-local PP
SpeciesSet & getSpeciesSet()
retrun the SpeciesSet of this particle set
Definition: ParticleSet.h:231
void create(const std::vector< int > &agroup)
create grouped particles
virtual void extractOptimizableObjectRefs(UniqueOptObjRefs &opt_obj_refs)
extract underlying OptimizableObject references
QMCTraits::RealType RealType
void makeMove(Index_t iat, const SingleParticlePos &displ, bool maybe_accept=true)
move the iat-th particle to active_pos_
bool parseFromString(const std::string_view data)
Definition: Libxml2Doc.cpp:204
Declaraton of ParticleAttrib<T>
static void mw_update(const RefVectorWithLeader< ParticleSet > &p_list, bool skipSK=false)
batched version of update
CHECK(log_values[0]==ComplexApprox(std::complex< double >{ 5.603777579195571, -6.1586603331188225 }))
virtual void mw_evaluateRatios(const RefVectorWithLeader< WaveFunctionComponent > &wfc_list, const RefVectorWithLeader< const VirtualParticleSet > &vp_list, std::vector< std::vector< ValueType >> &ratios) const
evaluate ratios to evaluate the non-local PP multiple walkers
TinyVector< double, 3 > PosType
handles acquire/release resource by the consumer (RefVectorWithLeader type).
LatticeGaussianProduct::ValueType ValueType
meta data for NLPP calculation of a pair of ion and electron This is not just meta data...
std::unique_ptr< WaveFunctionComponent > buildComponent(xmlNodePtr cur) override
process a xml node at cur
Declaration of WaveFunctionComponent.
std::complex< double > LogValue
Custom container for set of attributes for a set of species.
Definition: SpeciesSet.h:33
virtual void evaluateRatiosAlltoOne(ParticleSet &P, std::vector< ValueType > &ratios)
evaluate the ratios of one virtual move with respect to all the particles
virtual std::unique_ptr< WaveFunctionComponent > makeClone(ParticleSet &tqp) const
make clone
Specialization for three-body Jastrow function using multiple functors.
void makeVirtualMoves(const SingleParticlePos &newpos)
Handles virtual moves for all the particles to a single newpos.