1 | /*
|
---|
2 | * Project: MoleCuilder
|
---|
3 | * Description: creates and alters molecular systems
|
---|
4 | * Copyright (C) 2010 University of Bonn. All rights reserved.
|
---|
5 | * Please see the LICENSE file or "Copyright notice" in builder.cpp for details.
|
---|
6 | */
|
---|
7 |
|
---|
8 | /*
|
---|
9 | * ParserMpqcUnitTest.cpp
|
---|
10 | *
|
---|
11 | * Created on: Mar 3, 2010
|
---|
12 | * Author: metzler
|
---|
13 | */
|
---|
14 |
|
---|
15 | // include config.h
|
---|
16 | #ifdef HAVE_CONFIG_H
|
---|
17 | #include <config.h>
|
---|
18 | #endif
|
---|
19 |
|
---|
20 | #include "ParserMpqcUnitTest.hpp"
|
---|
21 |
|
---|
22 | #include <cppunit/CompilerOutputter.h>
|
---|
23 | #include <cppunit/extensions/TestFactoryRegistry.h>
|
---|
24 | #include <cppunit/ui/text/TestRunner.h>
|
---|
25 |
|
---|
26 | #include <boost/any.hpp>
|
---|
27 |
|
---|
28 | #include "World.hpp"
|
---|
29 | #include "atom.hpp"
|
---|
30 | #include "element.hpp"
|
---|
31 | #include "periodentafel.hpp"
|
---|
32 | #include "Descriptors/AtomTypeDescriptor.hpp"
|
---|
33 | #include "CodePatterns/Assert.hpp"
|
---|
34 |
|
---|
35 | #ifdef HAVE_TESTRUNNER
|
---|
36 | #include "UnitTestMain.hpp"
|
---|
37 | #endif /*HAVE_TESTRUNNER*/
|
---|
38 |
|
---|
39 | using namespace std;
|
---|
40 |
|
---|
41 | // Registers the fixture into the 'registry'
|
---|
42 | CPPUNIT_TEST_SUITE_REGISTRATION( ParserMpqcUnitTest );
|
---|
43 |
|
---|
44 | static string waterMpqc_CLHF ="% Created by MoleCuilder\n\
|
---|
45 | mpqc: (\n\
|
---|
46 | \tsavestate = no\n\
|
---|
47 | \tdo_gradient = yes\n\
|
---|
48 | \tmole<CLHF>: (\n\
|
---|
49 | \t\tmolecule = $:molecule\n\
|
---|
50 | \t\tbasis = $:basis\n\
|
---|
51 | \t\tmaxiter = 1000\n\
|
---|
52 | \t\tmemory = 16000000\n\
|
---|
53 | \t)\n\
|
---|
54 | )\n\
|
---|
55 | molecule<Molecule>: (\n\
|
---|
56 | \tunit = angstrom\n\
|
---|
57 | \t{ atoms geometry } = {\n\
|
---|
58 | \t\tO [ -0.505735\t0\t0 ]\n\
|
---|
59 | \t\tH [ 0.252867\t0\t0.504284 ]\n\
|
---|
60 | \t\tH [ 0.252867\t0\t-0.504284 ]\n\
|
---|
61 | \t}\n\
|
---|
62 | )\n\
|
---|
63 | basis<GaussianBasisSet>: (\n\
|
---|
64 | \tname = \"3-21G\"\n\
|
---|
65 | \tmolecule = $:molecule\n\
|
---|
66 | )\n"; // tested with mpqc 3.0.0-alpha
|
---|
67 | static string waterMpqc_CLKS ="% Created by MoleCuilder\n\
|
---|
68 | mpqc: (\n\
|
---|
69 | \tsavestate = no\n\
|
---|
70 | \tdo_gradient = yes\n\
|
---|
71 | \tmole<CLKS>: (\n\
|
---|
72 | \t\tfunctional<StdDenFunctional>:(name=B3LYP)\n\
|
---|
73 | \t\tmolecule = $:molecule\n\
|
---|
74 | \t\tbasis = $:basis\n\
|
---|
75 | \t\tmaxiter = 1000\n\
|
---|
76 | \t\tmemory = 16000000\n\
|
---|
77 | \t)\n\
|
---|
78 | )\n\
|
---|
79 | molecule<Molecule>: (\n\
|
---|
80 | \tunit = angstrom\n\
|
---|
81 | \t{ atoms geometry } = {\n\
|
---|
82 | \t\tO [ -0.505735\t0\t0 ]\n\
|
---|
83 | \t\tH [ 0.252867\t0\t0.504284 ]\n\
|
---|
84 | \t\tH [ 0.252867\t0\t-0.504284 ]\n\
|
---|
85 | \t}\n\
|
---|
86 | )\n\
|
---|
87 | basis<GaussianBasisSet>: (\n\
|
---|
88 | \tname = \"3-21G\"\n\
|
---|
89 | \tmolecule = $:molecule\n\
|
---|
90 | )\n"; // tested with mpqc 3.0.0-alpha
|
---|
91 | static string waterMpqc_MBPT2 ="% Created by MoleCuilder\n\
|
---|
92 | mpqc: (\n\
|
---|
93 | \tsavestate = no\n\
|
---|
94 | \tdo_gradient = yes\n\
|
---|
95 | \tmole<MBPT2>: (\n\
|
---|
96 | \t\tbasis = $:basis\n\
|
---|
97 | \t\tmolecule = $:molecule\n\
|
---|
98 | \t\tmemory = 16000000\n\
|
---|
99 | \t\treference<CLHF>: (\n\
|
---|
100 | \t\t\tmaxiter = 1000\n\
|
---|
101 | \t\t\tbasis = $:basis\n\
|
---|
102 | \t\t\tmolecule = $:molecule\n\
|
---|
103 | \t\t\tmemory = 16000000\n\
|
---|
104 | \t\t)\n\
|
---|
105 | \t)\n\
|
---|
106 | )\n\
|
---|
107 | molecule<Molecule>: (\n\
|
---|
108 | \tunit = angstrom\n\
|
---|
109 | \t{ atoms geometry } = {\n\
|
---|
110 | \t\tO [ -0.505735\t0\t0 ]\n\
|
---|
111 | \t\tH [ 0.252867\t0\t0.504284 ]\n\
|
---|
112 | \t\tH [ 0.252867\t0\t-0.504284 ]\n\
|
---|
113 | \t}\n\
|
---|
114 | )\n\
|
---|
115 | basis<GaussianBasisSet>: (\n\
|
---|
116 | \tname = \"3-21G\"\n\
|
---|
117 | \tmolecule = $:molecule\n\
|
---|
118 | )\n"; // tested with mpqc 3.0.0-alpha
|
---|
119 | static string waterMpqc_MBPT2_R12 ="% Created by MoleCuilder\n\
|
---|
120 | mpqc: (\n\
|
---|
121 | \tsavestate = no\n\
|
---|
122 | \tdo_gradient = yes\n\
|
---|
123 | \tmole<MBPT2_R12>: (\n\
|
---|
124 | \t\tmolecule = $:molecule\n\
|
---|
125 | \t\tbasis = $:basis\n\
|
---|
126 | \t\taux_basis = $:abasis\n\
|
---|
127 | \t\tstdapprox = \"A'\"\n\
|
---|
128 | \t\tnfzc = 1\n\
|
---|
129 | \t\tmemory = 16000000\n\
|
---|
130 | \t\tintegrals<IntegralCints>:()\n\
|
---|
131 | \t\treference<CLHF>: (\n\
|
---|
132 | \t\t\tmolecule = $:molecule\n\
|
---|
133 | \t\t\tbasis = $:basis\n\
|
---|
134 | \t\t\tmaxiter = 1000\n\
|
---|
135 | \t\t\tmemory = 16000000\n\
|
---|
136 | \t\t\tintegrals<IntegralCints>:()\n\
|
---|
137 | \t\t)\n\
|
---|
138 | \t)\n\
|
---|
139 | )\n\
|
---|
140 | molecule<Molecule>: (\n\
|
---|
141 | \tunit = angstrom\n\
|
---|
142 | \t{ atoms geometry } = {\n\
|
---|
143 | \t\tO [ -0.505735\t0\t0 ]\n\
|
---|
144 | \t\tH [ 0.252867\t0\t0.504284 ]\n\
|
---|
145 | \t\tH [ 0.252867\t0\t-0.504284 ]\n\
|
---|
146 | \t}\n\
|
---|
147 | )\n\
|
---|
148 | basis<GaussianBasisSet>: (\n\
|
---|
149 | \tname = \"3-21G\"\n\
|
---|
150 | \tmolecule = $:molecule\n\
|
---|
151 | )\n\
|
---|
152 | % auxiliary basis set specification\n\
|
---|
153 | \tabasis<GaussianBasisSet>: (\n\
|
---|
154 | \tname = \"aug-cc-pVDZ\"\n\
|
---|
155 | \tmolecule = $:molecule\n\
|
---|
156 | )\n"; // basically tested with mpqc 3.0.0-alpha (no parse errors but did not calculate due to missing code)
|
---|
157 |
|
---|
158 | void ParserMpqcUnitTest::setUp() {
|
---|
159 | mpqc = new MpqcParser();
|
---|
160 |
|
---|
161 | World::getInstance();
|
---|
162 |
|
---|
163 | setVerbosity(2);
|
---|
164 |
|
---|
165 | // we need hydrogens and oxygens in the following tests
|
---|
166 | CPPUNIT_ASSERT(World::getInstance().getPeriode()->FindElement(1) != NULL);
|
---|
167 | CPPUNIT_ASSERT(World::getInstance().getPeriode()->FindElement(8) != NULL);
|
---|
168 | }
|
---|
169 |
|
---|
170 | void ParserMpqcUnitTest::tearDown() {
|
---|
171 | delete mpqc;
|
---|
172 | ChangeTracker::purgeInstance();
|
---|
173 | World::purgeInstance();
|
---|
174 | }
|
---|
175 |
|
---|
176 | /************************************ tests ***********************************/
|
---|
177 |
|
---|
178 | void ParserMpqcUnitTest::ParameterTypeTest() {
|
---|
179 | // check types in boost::any map
|
---|
180 | CPPUNIT_ASSERT(mpqc->getParams().params[MpqcParser_Parameters::hessianParam].type() == typeid(bool));
|
---|
181 | CPPUNIT_ASSERT(mpqc->getParams().params[MpqcParser_Parameters::hessianParam].type() != typeid(int));
|
---|
182 | CPPUNIT_ASSERT(mpqc->getParams().params[MpqcParser_Parameters::savestateParam].type() == typeid(bool));
|
---|
183 | CPPUNIT_ASSERT(mpqc->getParams().params[MpqcParser_Parameters::do_gradientParam].type() == typeid(bool));
|
---|
184 | CPPUNIT_ASSERT(mpqc->getParams().params[MpqcParser_Parameters::maxiterParam].type() == typeid(int));
|
---|
185 | CPPUNIT_ASSERT(mpqc->getParams().params[MpqcParser_Parameters::memoryParam].type() == typeid(int));
|
---|
186 | CPPUNIT_ASSERT(mpqc->getParams().params[MpqcParser_Parameters::stdapproxParam].type() == typeid(std::string));
|
---|
187 | CPPUNIT_ASSERT(mpqc->getParams().params[MpqcParser_Parameters::nfzcParam].type() == typeid(int));
|
---|
188 | CPPUNIT_ASSERT(mpqc->getParams().params[MpqcParser_Parameters::basisParam].type() == typeid(std::string));
|
---|
189 | CPPUNIT_ASSERT(mpqc->getParams().params[MpqcParser_Parameters::aux_basisParam].type() == typeid(std::string));
|
---|
190 | CPPUNIT_ASSERT(mpqc->getParams().params[MpqcParser_Parameters::integrationParam].type() == typeid(MpqcParser_Parameters::IntegralCints));
|
---|
191 | CPPUNIT_ASSERT(mpqc->getParams().params[MpqcParser_Parameters::theoryParam].type() == typeid(MpqcParser_Parameters::MBPT2));
|
---|
192 | }
|
---|
193 |
|
---|
194 | void ParserMpqcUnitTest::ParameterDefaultTest() {
|
---|
195 | // check default values
|
---|
196 | CPPUNIT_ASSERT(mpqc->getParams().getString(MpqcParser_Parameters::hessianParam) == "no");
|
---|
197 | CPPUNIT_ASSERT(!mpqc->getParams().getBool(MpqcParser_Parameters::hessianParam));
|
---|
198 | CPPUNIT_ASSERT(mpqc->getParams().getString(MpqcParser_Parameters::savestateParam) == "no");
|
---|
199 | CPPUNIT_ASSERT(!mpqc->getParams().getBool(MpqcParser_Parameters::savestateParam));
|
---|
200 | CPPUNIT_ASSERT(mpqc->getParams().getString(MpqcParser_Parameters::do_gradientParam) == "yes");
|
---|
201 | CPPUNIT_ASSERT(mpqc->getParams().getBool(MpqcParser_Parameters::do_gradientParam));
|
---|
202 | CPPUNIT_ASSERT(mpqc->getParams().getInt(MpqcParser_Parameters::maxiterParam) == 1000);
|
---|
203 | CPPUNIT_ASSERT(mpqc->getParams().getInt(MpqcParser_Parameters::memoryParam) == 16000000);
|
---|
204 | CPPUNIT_ASSERT(mpqc->getParams().getString(MpqcParser_Parameters::stdapproxParam) == "A'");
|
---|
205 | CPPUNIT_ASSERT(mpqc->getParams().getInt(MpqcParser_Parameters::nfzcParam) == 1);
|
---|
206 | CPPUNIT_ASSERT(mpqc->getParams().getString(MpqcParser_Parameters::basisParam) == "3-21G");
|
---|
207 | CPPUNIT_ASSERT(mpqc->getParams().getString(MpqcParser_Parameters::aux_basisParam) == "aug-cc-pVDZ");
|
---|
208 | CPPUNIT_ASSERT(mpqc->getParams().getString(MpqcParser_Parameters::integrationParam) == "IntegralCints");
|
---|
209 | CPPUNIT_ASSERT(mpqc->getParams().getString(MpqcParser_Parameters::theoryParam) == "MBPT2");
|
---|
210 | CPPUNIT_ASSERT(mpqc->getParams().getTheory() == MpqcParser_Parameters::MBPT2);
|
---|
211 | CPPUNIT_ASSERT(mpqc->getParams().getIntegration() == MpqcParser_Parameters::IntegralCints);
|
---|
212 |
|
---|
213 | // check that values are not removed
|
---|
214 | CPPUNIT_ASSERT(!mpqc->getParams().params[MpqcParser_Parameters::theoryParam].empty());
|
---|
215 |
|
---|
216 | // check throw, for the moment aren't, are caught in getInt()
|
---|
217 | CPPUNIT_ASSERT_THROW(mpqc->getParams().getInt(MpqcParser_Parameters::integrationParam), boost::bad_any_cast);
|
---|
218 | CPPUNIT_ASSERT_THROW(mpqc->getParams().getInt(MpqcParser_Parameters::theoryParam), boost::bad_any_cast);
|
---|
219 |
|
---|
220 | }
|
---|
221 |
|
---|
222 | void ParserMpqcUnitTest::ParameterCloneTest() {
|
---|
223 | FormatParser_Parameters *clone = mpqc->getParams().clone();
|
---|
224 | CPPUNIT_ASSERT(mpqc->getParams().getString(MpqcParser_Parameters::theoryParam) == "MBPT2");
|
---|
225 | std::stringstream setvalue("theory = CLHF");
|
---|
226 | setvalue >> mpqc->getParams();
|
---|
227 | CPPUNIT_ASSERT(mpqc->getParams().getString(MpqcParser_Parameters::theoryParam) == "CLHF");
|
---|
228 | mpqc->getParams().makeClone(*clone);
|
---|
229 | CPPUNIT_ASSERT(mpqc->getParams().getString(MpqcParser_Parameters::theoryParam) == "MBPT2");
|
---|
230 | }
|
---|
231 |
|
---|
232 | void ParserMpqcUnitTest::ParameterSetterTest() {
|
---|
233 | // test a string
|
---|
234 | {
|
---|
235 | std::stringstream setvalue("theory = CLHF");
|
---|
236 | setvalue >> mpqc->getParams();
|
---|
237 | // std::cout << "integration method is "
|
---|
238 | // << mpqc->getParams().getString(MpqcParser_Parameters::theoryParam) << std::endl;
|
---|
239 | CPPUNIT_ASSERT(mpqc->getParams().getString(MpqcParser_Parameters::theoryParam) == "CLHF");
|
---|
240 | }
|
---|
241 | // test a bool
|
---|
242 | {
|
---|
243 | std::stringstream setvalue("Hessian = yes");
|
---|
244 | setvalue >> mpqc->getParams();
|
---|
245 | // std::cout << "Hessian is "
|
---|
246 | // << mpqc->getParams().getString(MpqcParser_Parameters::hessianParam) << std::endl;
|
---|
247 | CPPUNIT_ASSERT(mpqc->getParams().getString(MpqcParser_Parameters::hessianParam) == "yes");
|
---|
248 | }
|
---|
249 | // test int
|
---|
250 | {
|
---|
251 | std::stringstream setvalue("maxiter = 500");
|
---|
252 | setvalue >> mpqc->getParams();
|
---|
253 | // std::cout << "maxiter is "
|
---|
254 | // << mpqc->getParams().getString(MpqcParser_Parameters::maxiterParam) << std::endl;
|
---|
255 | CPPUNIT_ASSERT(mpqc->getParams().getInt(MpqcParser_Parameters::maxiterParam) == 500);
|
---|
256 | }
|
---|
257 | // test whether unknown key fails
|
---|
258 | std::cout << "The following Assertion warning is desired and does not indicate a failure of the test." << std::endl;
|
---|
259 | {
|
---|
260 | std::stringstream setvalue("hessian = no");
|
---|
261 | #ifndef NDEBUG
|
---|
262 | ASSERT_DO(Assert::Throw);
|
---|
263 | CPPUNIT_ASSERT_THROW(setvalue >> mpqc->getParams(), Assert::AssertionFailure);
|
---|
264 | #else
|
---|
265 | setvalue >> mpqc->getParams();
|
---|
266 | #endif
|
---|
267 | // std::cout << "Hessian is still "
|
---|
268 | // << mpqc->getParams().getString(MpqcParser_Parameters::hessianParam) << std::endl;
|
---|
269 | CPPUNIT_ASSERT(mpqc->getParams().getString(MpqcParser_Parameters::hessianParam) == "yes");
|
---|
270 | }
|
---|
271 | }
|
---|
272 |
|
---|
273 | void ParserMpqcUnitTest::readMpqcTest() {
|
---|
274 | stringstream input(waterMpqc_CLHF);
|
---|
275 | mpqc->getParams().setTheory(MpqcParser_Parameters::CLHF);
|
---|
276 | mpqc->load(&input);
|
---|
277 |
|
---|
278 | CPPUNIT_ASSERT_EQUAL(3, World::getInstance().numAtoms());
|
---|
279 | }
|
---|
280 |
|
---|
281 | void ParserMpqcUnitTest::writeMpqcTest() {
|
---|
282 | // build up water molecule
|
---|
283 | string first;
|
---|
284 | string second;
|
---|
285 | atom *Walker = NULL;
|
---|
286 | Walker = World::getInstance().createAtom();
|
---|
287 | Walker->setType(8);
|
---|
288 | Walker->setPosition(Vector(0,0,0));
|
---|
289 | Walker = World::getInstance().createAtom();
|
---|
290 | Walker->setType(1);
|
---|
291 | Walker->setPosition(Vector(0.758602,0,0.504284));
|
---|
292 | Walker = World::getInstance().createAtom();
|
---|
293 | Walker->setType(1);
|
---|
294 | Walker->setPosition(Vector(0.758602,0,-0.504284));
|
---|
295 | CPPUNIT_ASSERT_EQUAL(3, World::getInstance().numAtoms());
|
---|
296 |
|
---|
297 | // create two stringstreams, one stored, one created
|
---|
298 |
|
---|
299 | std::vector<atom *> atoms = World::getInstance().getAllAtoms();
|
---|
300 | {
|
---|
301 | // compare both configs for CLHF
|
---|
302 | stringstream output;
|
---|
303 | mpqc->getParams().setTheory(MpqcParser_Parameters::CLHF);
|
---|
304 | mpqc->save(&output, atoms);
|
---|
305 | stringstream input(waterMpqc_CLHF);
|
---|
306 | for (; std::getline(input, first) && std::getline(output, second); ) {
|
---|
307 | //std::cout << "Comparing '" << first << "' to '" << second << "'" << std::endl;
|
---|
308 | CPPUNIT_ASSERT(first == second);
|
---|
309 | }
|
---|
310 | }
|
---|
311 | {
|
---|
312 | // compare both configs for CLKS
|
---|
313 | stringstream output;
|
---|
314 | mpqc->getParams().setTheory(MpqcParser_Parameters::CLKS);
|
---|
315 | mpqc->save(&output, atoms);
|
---|
316 | stringstream input(waterMpqc_CLKS);
|
---|
317 | for (; std::getline(input, first) && std::getline(output, second); ) {
|
---|
318 | //std::cout << "Comparing '" << first << "' to '" << second << "'" << std::endl;
|
---|
319 | CPPUNIT_ASSERT(first == second);
|
---|
320 | }
|
---|
321 | }
|
---|
322 | {
|
---|
323 | // compare both configs for MBPT2
|
---|
324 | stringstream output;
|
---|
325 | mpqc->getParams().setTheory(MpqcParser_Parameters::MBPT2);
|
---|
326 | mpqc->save(&output, atoms);
|
---|
327 | stringstream input(waterMpqc_MBPT2);
|
---|
328 | for (; std::getline(input, first) && std::getline(output, second); ) {
|
---|
329 | //std::cout << "Comparing '" << first << "' to '" << second << "'" << std::endl;
|
---|
330 | CPPUNIT_ASSERT(first == second);
|
---|
331 | }
|
---|
332 | }
|
---|
333 | {
|
---|
334 | // compare both configs for MBPT2_R12
|
---|
335 | stringstream output;
|
---|
336 | mpqc->getParams().setTheory(MpqcParser_Parameters::MBPT2_R12);
|
---|
337 | mpqc->save(&output, atoms);
|
---|
338 | stringstream input(waterMpqc_MBPT2_R12);
|
---|
339 | for (; std::getline(input, first) && std::getline(output, second); ) {
|
---|
340 | //std::cout << "Comparing '" << first << "' to '" << second << "'" << std::endl;
|
---|
341 | CPPUNIT_ASSERT(first == second);
|
---|
342 | }
|
---|
343 | }
|
---|
344 | }
|
---|