QMCPACK
test_hamiltonian_factory.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) 2018 Jeongnim Kim and 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"
21 
22 namespace qmcplusplus
23 {
24 std::unique_ptr<ParticleSet> createElectronParticleSet(const SimulationCell& simulation_cell)
25 {
26  auto qp = std::make_unique<ParticleSet>(simulation_cell);
27  qp->setName("e");
28  qp->create({2});
29  qp->R[0][0] = 1.0;
30  qp->R[0][1] = 2.0;
31  qp->R[0][2] = 3.0;
32  qp->R[1][0] = 0.0;
33  qp->R[1][1] = 1.1;
34  qp->R[1][2] = 2.2;
35 
36  SpeciesSet& tspecies = qp->getSpeciesSet();
37  int upIdx = tspecies.addSpecies("u");
38  int massIdx = tspecies.addAttribute("mass");
39  tspecies(massIdx, upIdx) = 1.0;
40 
41  return qp;
42 }
43 
44 TEST_CASE("HamiltonianFactory", "[hamiltonian]")
45 {
47 
48  const SimulationCell simulation_cell;
49  auto elec_ptr = createElectronParticleSet(simulation_cell);
50  auto ions_ptr = std::make_unique<ParticleSet>(simulation_cell);
51 
52  auto &ions(*ions_ptr), elec(*elec_ptr);
53 
54  ions.setName("ion0");
55  ions.create({1});
56 
57  HamiltonianFactory::PSetMap particle_set_map;
58  particle_set_map.emplace(ions_ptr->getName(), std::move(ions_ptr));
59  particle_set_map.emplace(elec_ptr->getName(), std::move(elec_ptr));
60 
61  RuntimeOptions runtime_options;
63  psi_map.emplace("psi0", WaveFunctionFactory::buildEmptyTWFForTesting(runtime_options, "psi0"));
64 
65  HamiltonianFactory hf("h0", elec, particle_set_map, psi_map, c);
66 
67  const char* hamiltonian_xml = R"(<hamiltonian name="h0" type="generic" target="e">
68  <pairpot type="coulomb" name="ElecElec" source="e" target="e"/>
69  <pairpot type="coulomb" name="IonIon" source="ion0" target="ion0"/>
70  <pairpot type="coulomb" name="ElecIon" source="ion0" target="e"/>
71 </hamiltonian>)";
72 
74  bool okay = doc.parseFromString(hamiltonian_xml);
75  REQUIRE(okay);
76 
77  xmlNodePtr root = doc.getRoot();
78  hf.put(root);
79 
80 
81  REQUIRE(hf.getH());
82  REQUIRE(hf.getH()->size() == 3);
83  REQUIRE(hf.getH()->total_size() == 3);
84 
85  REQUIRE(hf.getH()->getOperatorType("ElecElec") == "coulomb");
86  REQUIRE(hf.getH()->getOperatorType("ElecIon") == "coulomb");
87 }
88 
89 TEST_CASE("HamiltonianFactory pseudopotential", "[hamiltonian]")
90 {
92 
93  const SimulationCell simulation_cell;
94  auto elec_ptr = createElectronParticleSet(simulation_cell);
95  auto ions_ptr = std::make_unique<ParticleSet>(simulation_cell);
96 
97  auto &ions(*ions_ptr), elec(*elec_ptr);
98 
99  ions.setName("ion0");
100  std::vector<int> agroup({1});
101  ions.create(agroup);
102 
103  SpeciesSet& tspecies = ions.getSpeciesSet();
104  int idx = tspecies.addSpecies("C");
105  int chargeIdx = tspecies.addAttribute("charge");
106  int atomicNumberIdx = tspecies.addAttribute("atomicnumber");
107  tspecies(chargeIdx, idx) = 4;
108  tspecies(atomicNumberIdx, idx) = 6;
109 
110  HamiltonianFactory::PSetMap particle_set_map;
111  particle_set_map.emplace(ions_ptr->getName(), std::move(ions_ptr));
112  particle_set_map.emplace(elec_ptr->getName(), std::move(elec_ptr));
113 
114  RuntimeOptions runtime_options;
116  psi_map.emplace("psi0", WaveFunctionFactory::buildEmptyTWFForTesting(runtime_options, "psi0"));
117 
118  HamiltonianFactory hf("h0", elec, particle_set_map, psi_map, c);
119 
120  const char* hamilonian_xml = R"(<hamiltonian name="h0" type="generic" target="e">
121  <pairpot type="pseudo" name="PseudoPot" source="ion0" wavefunction="psi0" format="xml">
122  <pseudo elementType="C" href="C.BFD.xml"/>
123  </pairpot>
124 </hamiltonian>)";
125 
127  bool okay = doc.parseFromString(hamilonian_xml);
128  REQUIRE(okay);
129 
130  xmlNodePtr root = doc.getRoot();
131  hf.put(root);
132 }
133 
134 } // namespace qmcplusplus
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
helper functions for EinsplineSetBuilder
Definition: Configuration.h:43
TEST_CASE("complex_helper", "[type_traits]")
std::map< std::string, const std::unique_ptr< ParticleSet > > PSetMap
xmlNodePtr getRoot()
Definition: Libxml2Doc.h:88
QMCHamiltonian * getH() const
get targetH
Communicate * Controller
Global Communicator for a process.
Definition: Communicate.cpp:35
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
REQUIRE(std::filesystem::exists(filename))
int size() const
return the number of Hamiltonians
Factory class to build a many-body wavefunction.
bool put(xmlNodePtr cur)
read from xmlNode
Declaration of a HamiltonianFactory.
std::map< std::string, const std::unique_ptr< TrialWaveFunction > > PsiPoolType
std::unique_ptr< ParticleSet > createElectronParticleSet(const SimulationCell &simulation_cell)
static std::unique_ptr< TrialWaveFunction > buildEmptyTWFForTesting(const RuntimeOptions &runtime_options, const std::string_view name)
create an empty TrialWaveFunction for testing use.
const std::string & getOperatorType(const std::string &name)
return type of named H element or fail
bool parseFromString(const std::string_view data)
Definition: Libxml2Doc.cpp:204
Declaration of a WaveFunctionFactory.
Custom container for set of attributes for a set of species.
Definition: SpeciesSet.h:33
int total_size() const
return the total number of Hamiltonians (physical + aux)