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) 2020 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 
16 #include "Message/Communicate.h"
17 #include "OhmmsData/Libxml2Doc.h"
18 #include "Particle/ParticleSetPool.h"
19 
20 
21 #include <stdio.h>
22 #include <string>
23 #include <sstream>
24 
25 
26 namespace qmcplusplus
27 {
28 TEST_CASE("ParticleSetPool", "[qmcapp]")
29 {
30   Communicate* c;
31   c = OHMMS::Controller;
32 
33   ParticleSetPool pp(c);
34 
35   // See ParticleIO/tests/test_xml_io.cpp for particle parsing
36   const char* particles = " \
37 <particleset name=\"ion0\" size=\"1\"> \
38   <group name=\"He\"> \
39     <parameter name=\"charge\">2</parameter> \
40   </group> \
41   <attrib name=\"position\" datatype=\"posArray\"> \
42     0.1 0.2 0.3 \
43   </attrib> \
44 </particleset> \
45 ";
46   Libxml2Document doc;
47   bool okay = doc.parseFromString(particles);
48   REQUIRE(okay);
49 
50   xmlNodePtr root = doc.getRoot();
51 
52   pp.put(root);
53 
54   ParticleSet* ions = pp.getParticleSet("ion0");
55   REQUIRE(ions != NULL);
56 
57   ParticleSet* not_here = pp.getParticleSet("does_not_exist");
58   REQUIRE(not_here == NULL);
59 
60   ParticleSet* ws = pp.getWalkerSet("ion0");
61   REQUIRE(ws != NULL);
62 
63   auto ps2 = std::make_unique<ParticleSet>();
64   ps2->setName("particle_set_2");
65   pp.addParticleSet(std::move(ps2));
66 
67   // should do nothing, since no random particlesets were specified
68   pp.randomize();
69 
70   std::stringstream out;
71   pp.get(out);
72   //std::cout << "ParticleSetPool::get returns  " << std::endl;
73   //std::cout << out.str() << std::endl;
74 }
75 
76 TEST_CASE("ParticleSetPool random", "[qmcapp]")
77 {
78   Communicate* c;
79   c = OHMMS::Controller;
80 
81   ParticleSetPool pp(c);
82 
83   // See ParticleIO/tests/test_xml_io.cpp for particle parsing
84   const char* particles = " \
85 <tmp> \
86 <particleset name=\"ion0\" size=\"1\"> \
87   <group name=\"He\"> \
88     <parameter name=\"charge\">2</parameter> \
89   </group> \
90   <attrib name=\"position\" datatype=\"posArray\"> \
91     0.1 0.2 0.3 \
92   </attrib> \
93 </particleset> \
94 <particleset name=\"elec\" random=\"yes\" randomsrc=\"ion0\"> \
95    <group name=\"u\" size=\"4\"> \
96       <parameter name=\"charge\">-1</parameter> \
97    </group> \
98 </particleset> \
99 </tmp> \
100 ";
101   Libxml2Document doc;
102   bool okay = doc.parseFromString(particles);
103   REQUIRE(okay);
104 
105   xmlNodePtr root = doc.getRoot();
106 
107   xmlNodePtr part_ion = xmlFirstElementChild(root);
108   pp.put(part_ion);
109   pp.put(xmlNextElementSibling(part_ion));
110 
111   ParticleSet* ions = pp.getParticleSet("ion0");
112   REQUIRE(ions != NULL);
113 
114   ParticleSet* elec = pp.getParticleSet("elec");
115   REQUIRE(ions != NULL);
116   REQUIRE(elec->R.size() == 4);
117   REQUIRE(elec->spins.size() == 4);
118 
119   // should do something
120   pp.randomize();
121 
122   REQUIRE(elec->R[0][0] != 0.0);
123 #if !defined(QMC_CUDA)
124   REQUIRE(elec->spins[0] != 0.0);
125 #endif
126 }
127 
128 TEST_CASE("ParticleSetPool putTileMatrix", "[qmcapp]")
129 {
130   Communicate* c;
131   c = OHMMS::Controller;
132 
133   ParticleSetPool pp(c);
134 
135   const char* tile_matrix = "<tmp tilematrix='1 0 0 1 1 0 2 1 1'/>";
136 
137   Libxml2Document doc;
138   bool okay = doc.parseFromString(tile_matrix);
139   REQUIRE(okay);
140 
141   xmlNodePtr root = doc.getRoot();
142   pp.putTileMatrix(root);
143 
144   REQUIRE(pp.getTileMatrix()[0] == 1);
145   REQUIRE(pp.getTileMatrix()[1] == 0);
146   REQUIRE(pp.getTileMatrix()[2] == 0);
147 
148   REQUIRE(pp.getTileMatrix()[3] == 1);
149   REQUIRE(pp.getTileMatrix()[4] == 1);
150   REQUIRE(pp.getTileMatrix()[5] == 0);
151 
152   REQUIRE(pp.getTileMatrix()[6] == 2);
153   REQUIRE(pp.getTileMatrix()[7] == 1);
154   REQUIRE(pp.getTileMatrix()[8] == 1);
155 }
156 
157 TEST_CASE("ParticleSetPool putLattice", "[qmcapp]")
158 {
159   Communicate* c;
160   c = OHMMS::Controller;
161 
162   ParticleSetPool pp(c);
163 
164   const char* lattice = "<parameter name='lattice'> </parameter>";
165 
166   Libxml2Document doc;
167   bool okay = doc.parseFromString(lattice);
168   REQUIRE(okay);
169 
170   xmlNodePtr root = doc.getRoot();
171   pp.putLattice(root);
172 }
173 } // namespace qmcplusplus
174