QMCPACK
test_QMCCostFunctionBase.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) 2022 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 #include "catch.hpp"
15 
16 
17 namespace qmcplusplus
18 {
19 
21 {
22 public:
24  : QMCCostFunctionBase(w, psi, h, c)
25  {}
26 
27  void resetPsi(bool final_reset = false) override {}
28  Return_rt fillOverlapHamiltonianMatrices(Matrix<Return_rt>& Left, Matrix<Return_rt>& Right) override { return 0; }
29  void getConfigurations(const std::string& aroot) override {}
30  void checkConfigurations(EngineHandle& handle) override {}
31  EffectiveWeight correlatedSampling(bool needGrad = true) override { return 0; }
32 
34  {
35  do_override_output = true;
37  }
38 
39  // Print the updated XML nodes. Useful for debugging.
40  void printXml()
41  {
42  char* buf;
43  int out_buflen;
44  xmlDocDumpFormatMemory(m_doc_out, (xmlChar**)&buf, &out_buflen, 1);
45  std::cout << "XML buffer length = " << out_buflen << std::endl;
46  std::cout << "XML: " << std::endl << buf << std::endl;
47  xmlFree(buf);
48  }
49 
50  xmlDocPtr getDoc() { return m_doc_out; }
51 };
52 
53 
54 TEST_CASE("updateXmlNodes", "[drivers]")
55 {
56  const SimulationCell simulation_cell;
57  MCWalkerConfiguration w(simulation_cell);
59  RuntimeOptions runtime_options;
60  TrialWaveFunction psi(runtime_options);
61 
63 
64  QMCCostFunctionTest cost(w, psi, h, comm);
65 
66  cost.setRootName("tmp");
67 
68  const char* wf_xml = R"(
69 <wavefunction/>
70  )";
71 
73  bool okay = doc.parseFromString(wf_xml);
74  REQUIRE(okay);
76 
77  cost.callUpdateXmlNodes();
78  //cost.printXml();
79 
80  xmlXPathContextPtr acontext = xmlXPathNewContext(cost.getDoc());
81  OhmmsXPathObject check_elem("//override_variational_parameters", acontext);
82  REQUIRE(check_elem.size() == 1);
83 
84  std::string href = getXMLAttributeValue(check_elem[0], "href");
85  REQUIRE(href == "tmp.vp.h5");
86 
87  xmlXPathFreeContext(acontext);
88 }
89 
90 TEST_CASE("updateXmlNodes with existing element", "[drivers]")
91 {
92  const SimulationCell simulation_cell;
93  MCWalkerConfiguration w(simulation_cell);
95  RuntimeOptions runtime_options;
96  TrialWaveFunction psi(runtime_options);
97 
99 
100  QMCCostFunctionTest cost(w, psi, h, comm);
101  cost.setRootName("tmp2");
102 
103  const char* wf_xml = R"(
104 <wavefunction>
105  <override_variational_parameters href="vp.tmp.h5"/>
106 </wavefunction>
107  )";
108 
110  bool okay = doc.parseFromString(wf_xml);
111  REQUIRE(okay);
113 
114  cost.callUpdateXmlNodes();
115  //cost.printXml();
116 
117  xmlXPathContextPtr acontext = xmlXPathNewContext(cost.getDoc());
118  OhmmsXPathObject check_elem("//override_variational_parameters", acontext);
119  REQUIRE(check_elem.size() == 1);
120 
121  std::string href = getXMLAttributeValue(check_elem[0], "href");
122  REQUIRE(href == "tmp2.vp.h5");
123 
124  xmlXPathFreeContext(acontext);
125 }
126 
127 
128 } // namespace qmcplusplus
class that handles xmlDoc
Definition: Libxml2Doc.h:76
A set of walkers that are to be advanced by Metropolis Monte Carlo.
helper functions for EinsplineSetBuilder
Definition: Configuration.h:43
void setRootName(const std::string &aroot)
void checkConfigurations(EngineHandle &handle) override
TEST_CASE("complex_helper", "[type_traits]")
xmlNodePtr getRoot()
Definition: Libxml2Doc.h:88
Collection of Local Energy Operators.
QMCTraits::QTFull::RealType EffectiveWeight
Communicate * Controller
Global Communicator for a process.
Definition: Communicate.cpp:35
bool do_override_output
Flag on whether the variational parameter override is output to the new wavefunction.
class to handle xmlXPathObject
Definition: Libxml2Doc.h:26
void resetPsi(bool final_reset=false) override
reset the wavefunction
Wrapping information on parallelism.
Definition: Communicate.h:68
REQUIRE(std::filesystem::exists(filename))
QMCCostFunctionTest(MCWalkerConfiguration &w, TrialWaveFunction &psi, QMCHamiltonian &h, Communicate *c)
Implements wave-function optimization.
std::string getXMLAttributeValue(const xmlNodePtr cur, const std::string_view name)
get the value string for attribute name if name is unfound in cur you get an empty string back this i...
EffectiveWeight correlatedSampling(bool needGrad=true) override
run correlated sampling return effective walkers ( w_i)^2/(Nw * w^2_i)
xmlDocPtr m_doc_out
document node to be dumped
Class to represent a many-body trial wave function.
bool parseFromString(const std::string_view data)
Definition: Libxml2Doc.cpp:204
void setWaveFunctionNode(xmlNodePtr cur)
Return_rt fillOverlapHamiltonianMatrices(Matrix< Return_rt > &Left, Matrix< Return_rt > &Right) override
void getConfigurations(const std::string &aroot) override