QMCPACK
test_xml_io.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 
16 #include "OhmmsData/Libxml2Doc.h"
17 #include "OhmmsPETE/Tensor.h"
18 #include "Particle/ParticleSet.h"
20 
21 #include <stdio.h>
22 #include <string>
23 
24 using std::string;
25 
26 namespace qmcplusplus
27 {
28 TEST_CASE("read_particleset_xml", "[particle_io][xml]")
29 {
30  const char* particles = R"(<tmp>
31 <particleset name="ion0" size="1">
32  <group name="He">
33  <parameter name="charge">2</parameter>
34  </group>
35  <attrib name="position" datatype="posArray">
36  0.1 0.2 0.3
37  </attrib>
38 </particleset>
39 <particleset name="e">
40  <group name="u" size="1">
41  <parameter name="charge">-1</parameter>
42  <attrib name="position" datatype="posArray">
43  -0.28 0.0225 -2.709
44  </attrib>
45  </group>
46  <group name="d" size="1">
47  <parameter name="charge">-1</parameter>
48  <attrib name="position" datatype="posArray">
49  -1.08389 1.9679 -0.0128914
50  </attrib>
51  </group>
52 </particleset>
53 </tmp>
54 )";
56  bool okay = doc.parseFromString(particles);
57  REQUIRE(okay);
58 
59  xmlNodePtr root = doc.getRoot();
60 
61  const SimulationCell simulation_cell;
62  ParticleSet ions(simulation_cell), electrons(simulation_cell);
63 
64  XMLParticleParser parse_ions(ions);
65  xmlNodePtr part1 = xmlFirstElementChild(root);
66  parse_ions.readXML(part1);
67 
68  REQUIRE(ions.groups() == 1);
69  REQUIRE(ions.R.size() == 1);
70  CHECK(ions.R[0][0] == Approx(0.1));
71  CHECK(ions.R[0][1] == Approx(0.2));
72  CHECK(ions.R[0][2] == Approx(0.3));
73  REQUIRE(ions.getName() == "ion0");
74 
75  XMLParticleParser parse_electrons(electrons);
76  xmlNodePtr part2 = xmlNextElementSibling(part1);
77  parse_electrons.readXML(part2);
78 
79  REQUIRE(electrons.groups() == 2);
80  REQUIRE(electrons.R.size() == 2);
81  CHECK(electrons.R[0][0] == Approx(-0.28));
82  CHECK(electrons.R[0][1] == Approx(0.0225));
83  CHECK(electrons.R[0][2] == Approx(-2.709));
84 
85  CHECK(electrons.R[1][0] == Approx(-1.08389));
86  CHECK(electrons.R[1][1] == Approx(1.9679));
87  CHECK(electrons.R[1][2] == Approx(-0.0128914));
88  REQUIRE(electrons.getName() == "e");
89 }
90 
91 TEST_CASE("read_particleset_recorder_xml", "[particle_io][xml]")
92 {
93  const char* particles = R"(<tmp>
94 <particleset name="ion0" size="3">
95  <group name="He">
96  <parameter name="charge">2</parameter>
97  </group>
98  <group name="H">
99  <parameter name="charge">1</parameter>
100  </group>
101  <attrib name="position" datatype="posArray">
102  0.1 0.2 0.3
103  0.3 0.1 0.2
104  0.2 0.3 0.1
105  </attrib>
106  <attrib name="ionid" datatype="stringArray">
107  H He He
108  </attrib>
109 </particleset>
110 </tmp>
111 )";
113  bool okay = doc.parseFromString(particles);
114  REQUIRE(okay);
115 
116  xmlNodePtr root = doc.getRoot();
117 
118  const SimulationCell simulation_cell;
119  ParticleSet ions(simulation_cell), electrons(simulation_cell);
120 
121  XMLParticleParser parse_ions(ions);
122  xmlNodePtr part1 = xmlFirstElementChild(root);
123  parse_ions.readXML(part1);
124 
125  CHECK(ions.groups() == 2);
126  CHECK(ions.R.size() == 3);
127  CHECK(ions.GroupID[0] == 0); // He
128  CHECK(ions.GroupID[1] == 0); // He
129  CHECK(ions.GroupID[2] == 1); // H
130  CHECK(ions.R[0][0] == Approx(0.3));
131  CHECK(ions.R[0][1] == Approx(0.1));
132  CHECK(ions.R[0][2] == Approx(0.2));
133  CHECK(ions.R[1][0] == Approx(0.2));
134  CHECK(ions.R[1][1] == Approx(0.3));
135  CHECK(ions.R[1][2] == Approx(0.1));
136  CHECK(ions.R[2][0] == Approx(0.1));
137  CHECK(ions.R[2][1] == Approx(0.2));
138  CHECK(ions.R[2][2] == Approx(0.3));
139  CHECK(ions.getName() == "ion0");
140 }
141 
142 TEST_CASE("read_dynamic_spin_eset_xml", "[particle_io][xml]")
143 {
144  const char* particles = R"(<tmp>
145 <particleset name="e">
146  <group name="e" size="3">
147  <parameter name="charge">-1</parameter>
148  <attrib name="position" datatype="posArray">
149  -0.28 0.0225 -2.709
150  -1.28 1.0225 -1.709
151  -2.28 2.0225 -0.709
152  </attrib>
153  <attrib name="spins" datatype="scalarArray">
154  1.0
155  0.2
156  3.0
157  </attrib>
158  </group>
159 </particleset>
160 </tmp>
161 )";
163  bool okay = doc.parseFromString(particles);
164  REQUIRE(okay);
165 
166  xmlNodePtr root = doc.getRoot();
167 
168  xmlNodePtr part1 = xmlFirstElementChild(root);
169 
170  const SimulationCell simulation_cell;
171  ParticleSet electrons(simulation_cell);
172 
173  XMLParticleParser parse_electrons(electrons);
174  parse_electrons.readXML(part1);
175 
176  REQUIRE(electrons.groups() == 1);
177 
178  REQUIRE(electrons.R.size() == 3);
179 
180  CHECK(electrons.R[0][0] == Approx(-0.28));
181  CHECK(electrons.R[0][1] == Approx(0.0225));
182  CHECK(electrons.R[0][2] == Approx(-2.709));
183 
184  CHECK(electrons.R[1][0] == Approx(-1.28));
185  CHECK(electrons.R[1][1] == Approx(1.0225));
186  CHECK(electrons.R[1][2] == Approx(-1.709));
187 
188  CHECK(electrons.R[2][0] == Approx(-2.28));
189  CHECK(electrons.R[2][1] == Approx(2.0225));
190  CHECK(electrons.R[2][2] == Approx(-0.709));
191 
192  CHECK(electrons.spins[0] == Approx(1.0));
193  CHECK(electrons.spins[1] == Approx(0.2));
194  CHECK(electrons.spins[2] == Approx(3.0));
195 
196  REQUIRE(electrons.getName() == "e");
197 }
198 } // namespace qmcplusplus
class that handles xmlDoc
Definition: Libxml2Doc.h:76
const std::string & getName() const
return the name
helper functions for EinsplineSetBuilder
Definition: Configuration.h:43
ParticleScalar spins
internal spin variables for dynamical spin calculations
Definition: ParticleSet.h:81
TEST_CASE("complex_helper", "[type_traits]")
xmlNodePtr getRoot()
Definition: Libxml2Doc.h:88
int groups() const
return the number of groups
Definition: ParticleSet.h:511
Specialized paritlce class for atomistic simulations.
Definition: ParticleSet.h:55
size_type size() const
return the current size
Definition: OhmmsVector.h:162
REQUIRE(std::filesystem::exists(filename))
bool readXML(xmlNodePtr cur)
process xmlnode <particleset/> which contains everything about the particle set to initialize ...
ParticlePos R
Position.
Definition: ParticleSet.h:79
bool parseFromString(const std::string_view data)
Definition: Libxml2Doc.cpp:204
CHECK(log_values[0]==ComplexApprox(std::complex< double >{ 5.603777579195571, -6.1586603331188225 }))