source: src/Parser/unittests/ParserPsi3UnitTest.cpp@ ff4fff9

CombiningParticlePotentialParsing
Last change on this file since ff4fff9 was fac58f, checked in by Frederik Heber <heber@…>, 9 years ago

Converted FormatParser::save() to using vector of const atom ptrs.

  • required to change all save() functions in all parsers.
  • Property mode set to 100644
File size: 15.4 KB
Line 
1/*
2 * Project: MoleCuilder
3 * Description: creates and alters molecular systems
4 * Copyright (C) 2010-2012 University of Bonn. All rights reserved.
5 * Copyright (C) 2013 Frederik Heber. All rights reserved.
6 *
7 *
8 * This file is part of MoleCuilder.
9 *
10 * MoleCuilder is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation, either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * MoleCuilder is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with MoleCuilder. If not, see <http://www.gnu.org/licenses/>.
22 */
23
24/*
25 * ParserPsi3UnitTest.cpp
26 *
27 * Created on: Mar 3, 2010
28 * Author: metzler
29 */
30
31// include config.h
32#ifdef HAVE_CONFIG_H
33#include <config.h>
34#endif
35
36#include "ParserPsi3UnitTest.hpp"
37
38#include <cppunit/CompilerOutputter.h>
39#include <cppunit/extensions/TestFactoryRegistry.h>
40#include <cppunit/ui/text/TestRunner.h>
41
42#include <boost/any.hpp>
43
44#include "CodePatterns/Log.hpp"
45
46#include "Atom/atom.hpp"
47#include "Atom/AtomObserver.hpp"
48#include "Element/element.hpp"
49#include "Element/periodentafel.hpp"
50#include "CodePatterns/Assert.hpp"
51#include "Descriptors/AtomTypeDescriptor.hpp"
52#include "Parser/ChangeTracker.hpp"
53#include "Parser/Psi3Parser.hpp"
54#include "World.hpp"
55
56#ifdef HAVE_TESTRUNNER
57#include "UnitTestMain.hpp"
58#endif /*HAVE_TESTRUNNER*/
59
60using namespace std;
61
62// Registers the fixture into the 'registry'
63CPPUNIT_TEST_SUITE_REGISTRATION( ParserPsi3UnitTest );
64
65static string hydrogenPsi3_RHF ="% Created by MoleCuilder\n\
66psi: (\n\
67\tlabel = \"cc-pVTZ SCF H2\"\n\
68\tjobtype = sp\n\
69\twfn = scf\n\
70\tmaxiter = 80\n\
71\treference = rhf\n\
72\tbasis = \"cc-pVTZ\"\n\
73\tfreeze_core = no\n\
74\tunits = angstrom\n\
75\tgeometry = (\n\
76\t( H\t0\t0\t-0.37 )\n\
77\t( H\t0\t0\t0.37 )\n\
78\t)\n\
79\torigin = (0.0\t0.0\t0.0)\n\
80)\n"; // tested with ???
81
82static string hydrogenPsi3_ROHF ="% Created by MoleCuilder\n\
83psi: (\n\
84\tlabel = \"cc-pVTZ SCF H2\"\n\
85\tjobtype = sp\n\
86\twfn = scf\n\
87\tmaxiter = 80\n\
88\treference = rohf\n\
89\tbasis = \"cc-pVTZ\"\n\
90\tfreeze_core = no\n\
91\tunits = angstrom\n\
92\tgeometry = (\n\
93\t( H\t0\t0\t-0.37 )\n\
94\t( H\t0\t0\t0.37 )\n\
95\t)\n\
96\torigin = (0.0\t0.0\t0.0)\n\
97)\n"; // tested with ???
98
99static string hydrogenPsi3_UHF ="% Created by MoleCuilder\n\
100psi: (\n\
101\tlabel = \"cc-pVTZ SCF H2\"\n\
102\tjobtype = sp\n\
103\twfn = scf\n\
104\tmaxiter = 80\n\
105\treference = uhf\n\
106\tbasis = \"cc-pVTZ\"\n\
107\tmultp = 3\n\
108\tcharge = 2\n\
109\tfreeze_core = no\n\
110\tunits = angstrom\n\
111\tgeometry = (\n\
112\t( H\t0\t0\t-0.37 )\n\
113\t( H\t0\t0\t0.37 )\n\
114\t)\n\
115\torigin = (0.0\t0.0\t0.0)\n\
116)\n"; // tested with ???
117
118static string hydrogenPsi3_TWOCON ="% Created by MoleCuilder\n\
119psi: (\n\
120\tlabel = \"cc-pVTZ SCF H2\"\n\
121\tjobtype = sp\n\
122\twfn = scf\n\
123\tmaxiter = 80\n\
124\treference = twocon\n\
125\tbasis = \"cc-pVTZ\"\n\
126\tmultp = 3\n\
127\tcharge = 2\n\
128\tsocc = ( 1 1 0 0 0 0 0 0 )\n\
129\tdocc = ( 0 0 0 0 0 0 0 0 )\n\
130\tsubgroup = c2v\n\
131\tunique_axis = x\n\
132\tfreeze_core = no\n\
133\tunits = angstrom\n\
134\tgeometry = (\n\
135\t( H\t0\t0\t-0.37 )\n\
136\t( H\t0\t0\t0.37 )\n\
137\t)\n\
138\torigin = (0.0\t0.0\t0.0)\n\
139)\n"; // tested with ???
140
141void ParserPsi3UnitTest::setUp()
142{
143 // failing asserts should be thrown
144 ASSERT_DO(Assert::Throw);
145
146 parser = new FormatParser<psi3>();
147
148 params = &parser->getParams();
149
150 World::getInstance();
151
152 setVerbosity(2);
153
154 // we need hydrogens and oxygens in the following tests
155 CPPUNIT_ASSERT(World::getInstance().getPeriode()->FindElement(1) != NULL);
156}
157
158void ParserPsi3UnitTest::tearDown()
159{
160 params = NULL;
161 delete parser;
162 ChangeTracker::purgeInstance();
163 World::purgeInstance();
164 AtomObserver::purgeInstance();
165}
166
167/************************************ tests ***********************************/
168
169void ParserPsi3UnitTest::ParameterDefaultTest() {
170 // check default values
171 CPPUNIT_ASSERT(params->getParameter(Psi3Parser_Parameters::labelParam) == std::string("unknown job"));
172 CPPUNIT_ASSERT(params->getParameter(Psi3Parser_Parameters::jobtypeParam) == params->ValidJobtypes[Psi3Parser_Parameters::SP]);
173 CPPUNIT_ASSERT(params->getParameter(Psi3Parser_Parameters::wavefunctionParam) == params->ValidWavefunction[Psi3Parser_Parameters::SCF]);
174 CPPUNIT_ASSERT(params->getParameter(Psi3Parser_Parameters::maxiterParam) == std::string("80"));
175 CPPUNIT_ASSERT(params->getParameter(Psi3Parser_Parameters::referenceParam) == params->ValidReference[Psi3Parser_Parameters::RHF]);
176 CPPUNIT_ASSERT(params->getParameter(Psi3Parser_Parameters::basisParam) == std::string("cc-pVTZ"));
177 CPPUNIT_ASSERT(params->getParameter(Psi3Parser_Parameters::freeze_coreParam) == params->ValidFreezeCore[Psi3Parser_Parameters::YES]);
178 CPPUNIT_ASSERT(params->getParameter(Psi3Parser_Parameters::unitsParam) == params->ValidUnits[Psi3Parser_Parameters::angstrom]);
179 CPPUNIT_ASSERT(params->getParameter(Psi3Parser_Parameters::dertypeParam) == params->ValidDerivativeType[Psi3Parser_Parameters::NONE]);
180 CPPUNIT_ASSERT(params->getParameter(Psi3Parser_Parameters::originParam) == std::string("(0.0\t0.0\t0.0)"));
181 CPPUNIT_ASSERT(params->getParameter(Psi3Parser_Parameters::multiplicityParam) == std::string("1"));
182 CPPUNIT_ASSERT(params->getParameter(Psi3Parser_Parameters::chargeParam) == std::string("0"));
183 CPPUNIT_ASSERT(params->getParameter(Psi3Parser_Parameters::soccParam) == std::string("()"));
184 CPPUNIT_ASSERT(params->getParameter(Psi3Parser_Parameters::doccParam) == std::string("()"));
185 CPPUNIT_ASSERT(params->getParameter(Psi3Parser_Parameters::subgroupParam) == std::string(""));
186 CPPUNIT_ASSERT(params->getParameter(Psi3Parser_Parameters::unique_axisParam) == params->ValidUniqueAxis[Psi3Parser_Parameters::X]);
187}
188
189void ParserPsi3UnitTest::ParameterCloneTest() {
190 FormatParser_Parameters *clone = params->clone();
191 CPPUNIT_ASSERT(params->getParameter(Psi3Parser_Parameters::jobtypeParam) == params->ValidJobtypes[Psi3Parser_Parameters::SP]);
192 std::stringstream setvalue(params->ParamNames[Psi3Parser_Parameters::jobtypeParam]+"="+params->ValidJobtypes[Psi3Parser_Parameters::OPT]);
193 setvalue >> *params;
194 CPPUNIT_ASSERT(params->getParameter(Psi3Parser_Parameters::jobtypeParam) == params->ValidJobtypes[Psi3Parser_Parameters::OPT]);
195 params->makeClone(*clone);
196 CPPUNIT_ASSERT(params->getParameter(Psi3Parser_Parameters::jobtypeParam) == params->ValidJobtypes[Psi3Parser_Parameters::SP]);
197}
198
199void ParserPsi3UnitTest::ParameterSetterTest() {
200 // test a jobtype
201 {
202 std::stringstream setvalue(
203 params->ParamNames[Psi3Parser_Parameters::jobtypeParam]
204 +" = "+params->ValidJobtypes[Psi3Parser_Parameters::OPT]
205 );
206 setvalue >> *params;
207 CPPUNIT_ASSERT(params->getParameter(Psi3Parser_Parameters::jobtypeParam) == params->ValidJobtypes[Psi3Parser_Parameters::OPT]);
208 }
209 // test a wavefunction
210 {
211 std::stringstream setvalue(
212 params->ParamNames[Psi3Parser_Parameters::wavefunctionParam]
213 +" = "+params->ValidWavefunction[Psi3Parser_Parameters::MP2]
214 );
215 setvalue >> *params;
216 CPPUNIT_ASSERT(params->getParameter(Psi3Parser_Parameters::wavefunctionParam) == params->ValidWavefunction[Psi3Parser_Parameters::MP2]);
217 }
218 // test a reference
219 {
220 std::stringstream setvalue(
221 params->ParamNames[Psi3Parser_Parameters::referenceParam]
222 +" = "+params->ValidReference[Psi3Parser_Parameters::ROHF]
223 );
224 setvalue >> *params;
225 CPPUNIT_ASSERT(params->getParameter(Psi3Parser_Parameters::referenceParam) == params->ValidReference[Psi3Parser_Parameters::ROHF]);
226 }
227 // test a unique_axis
228 {
229 std::stringstream setvalue(
230 params->ParamNames[Psi3Parser_Parameters::unique_axisParam]
231 +" = "+params->ValidUniqueAxis[Psi3Parser_Parameters::Y]
232 );
233 setvalue >> *params;
234 CPPUNIT_ASSERT(params->getParameter(Psi3Parser_Parameters::unique_axisParam) == params->ValidUniqueAxis[Psi3Parser_Parameters::Y]);
235 }
236 // test a units
237 {
238 std::stringstream setvalue(
239 params->ParamNames[Psi3Parser_Parameters::unitsParam]
240 +" = "+params->ValidUnits[Psi3Parser_Parameters::bohr]
241 );
242 setvalue >> *params;
243 CPPUNIT_ASSERT(params->getParameter(Psi3Parser_Parameters::unitsParam) == params->ValidUnits[Psi3Parser_Parameters::bohr]);
244 }
245 // test a dertype
246 {
247 std::stringstream setvalue(
248 params->ParamNames[Psi3Parser_Parameters::dertypeParam]
249 +" = "+params->ValidDerivativeType[Psi3Parser_Parameters::NONE]
250 );
251 setvalue >> *params;
252 CPPUNIT_ASSERT(params->getParameter(Psi3Parser_Parameters::dertypeParam) == params->ValidDerivativeType[Psi3Parser_Parameters::NONE]);
253 }
254 // test a freeze_core
255 {
256 std::stringstream setvalue(
257 params->ParamNames[Psi3Parser_Parameters::freeze_coreParam]
258 +" = "+params->ValidFreezeCore[Psi3Parser_Parameters::LARGE]
259 );
260 setvalue >> *params;
261 CPPUNIT_ASSERT(params->getParameter(Psi3Parser_Parameters::freeze_coreParam) == params->ValidFreezeCore[Psi3Parser_Parameters::LARGE]);
262 }
263 // test int
264 {
265 std::stringstream setvalue(
266 params->ParamNames[Psi3Parser_Parameters::maxiterParam]
267 +" = 500"
268 );
269 setvalue >> *params;
270// std::cout << "maxiter is "
271// << params->getString(Psi3Parser_Parameters::maxiterParam) << std::endl;
272 CPPUNIT_ASSERT(params->getParameter(Psi3Parser_Parameters::maxiterParam) == std::string("500"));
273 }
274 // test whether unknown key fails
275 std::cout << "The following Assertion warning is desired and does not indicate a failure of the test." << std::endl;
276 {
277 std::stringstream setvalue("sd = no");
278#ifndef NDEBUG
279 ASSERT_DO(Assert::Throw);
280 CPPUNIT_ASSERT_THROW(setvalue >> *params, Assert::AssertionFailure);
281#else
282 setvalue >> *params;
283#endif
284// std::cout << "Hessian is still "
285// << params->getString(Psi3Parser_Parameters::hessianParam) << std::endl;
286 CPPUNIT_ASSERT(params->getParameter(Psi3Parser_Parameters::jobtypeParam) == params->ValidJobtypes[Psi3Parser_Parameters::OPT]);
287 }
288}
289
290void ParserPsi3UnitTest::readPsi3Test() {
291 stringstream input(hydrogenPsi3_RHF);
292 // set some other parameter for jobtype
293 params->setParameter(
294 Psi3Parser_Parameters::jobtypeParam,
295 params->ValidJobtypes[Psi3Parser_Parameters::OPT]
296 );
297 parser->load(&input);
298
299 // check for jobtype from file
300 CPPUNIT_ASSERT(params->getParameter(Psi3Parser_Parameters::jobtypeParam) == params->ValidJobtypes[Psi3Parser_Parameters::SP]);
301 // check for 2 hydrogens
302 CPPUNIT_ASSERT_EQUAL(2, World::getInstance().numAtoms());
303 // check that positions are right
304 Vector PositionSum;
305 std::vector<const atom *> atoms = const_cast<const World &>(World::getInstance()).
306 getAllAtoms();
307 for (std::vector<const atom *>::const_iterator iter = atoms.begin();
308 iter != atoms.end();
309 ++iter)
310 PositionSum += (*iter)->getPosition();
311 CPPUNIT_ASSERT_EQUAL( PositionSum, Vector(0.,0.,0.) );
312}
313
314void ParserPsi3UnitTest::writePsi3Test() {
315 // build up hydrogen molecule
316 string first;
317 string second;
318 atom *Walker = NULL;
319 Walker = World::getInstance().createAtom();
320 Walker->setType(1);
321 Walker->setPosition(Vector(0.,0.,0.));
322 Walker = World::getInstance().createAtom();
323 Walker->setType(1);
324 Walker->setPosition(Vector(0.,0,0.74));
325 CPPUNIT_ASSERT_EQUAL(2, World::getInstance().numAtoms());
326
327 // set general parameters: label and freeze_core
328 params->setParameter(
329 Psi3Parser_Parameters::labelParam,
330 "cc-pVTZ SCF H2"
331 );
332 params->setParameter(
333 Psi3Parser_Parameters::freeze_coreParam,
334 params->ValidFreezeCore[Psi3Parser_Parameters::NO]
335 );
336 params->setParameter(
337 Psi3Parser_Parameters::unitsParam,
338 params->ValidUnits[Psi3Parser_Parameters::angstrom]
339 );
340
341 // create two stringstreams, one stored, one created
342 std::vector<const atom *> atoms = const_cast<const World &>(World::getInstance()).
343 getAllAtoms();
344 {
345 // compare both configs for RHF
346 stringstream output;
347 params->setParameter(
348 Psi3Parser_Parameters::referenceParam,
349 params->ValidReference[Psi3Parser_Parameters::RHF]
350 );
351 parser->save(&output, atoms);
352 stringstream input(hydrogenPsi3_RHF);
353 // check for non-empty streams
354 input.peek();
355 output.peek();
356 CPPUNIT_ASSERT(input.good() && output.good());
357 // check equality of streams per line (for debugging)
358 for (; std::getline(input, first) && std::getline(output, second); ) {
359 //std::cout << "Comparing '" << first << "' to '" << second << "'" << std::endl;
360 CPPUNIT_ASSERT(first == second);
361 }
362 }
363 {
364 // compare both configs for ROHF
365 stringstream output;
366 params->setParameter(
367 Psi3Parser_Parameters::referenceParam,
368 params->ValidReference[Psi3Parser_Parameters::ROHF]
369 );
370 parser->save(&output, atoms);
371 stringstream input(hydrogenPsi3_ROHF);
372 // check for non-empty streams
373 input.peek();
374 output.peek();
375 CPPUNIT_ASSERT(input.good() && output.good());
376 // check equality of streams per line (for debugging)
377 for (; std::getline(input, first) && std::getline(output, second); ) {
378 //std::cout << "Comparing '" << first << "' to '" << second << "'" << std::endl;
379 CPPUNIT_ASSERT(first == second);
380 }
381 }
382 {
383 params->setParameter(Psi3Parser_Parameters::multiplicityParam, "2");
384 params->setParameter(Psi3Parser_Parameters::chargeParam, "2");
385 // compare both configs for UHF
386 stringstream output;
387 params->setParameter(
388 Psi3Parser_Parameters::referenceParam,
389 params->ValidReference[Psi3Parser_Parameters::UHF]
390 );
391 parser->save(&output, atoms);
392 params->setParameter(Psi3Parser_Parameters::chargeParam, "0");
393 params->setParameter(Psi3Parser_Parameters::multiplicityParam, "1");
394 stringstream input(hydrogenPsi3_UHF);
395 // check for non-empty streams
396 input.peek();
397 output.peek();
398 CPPUNIT_ASSERT(input.good() && output.good());
399 // check equality of streams per line (for debugging)
400 for (; std::getline(input, first) && std::getline(output, second); ) {
401 //std::cout << "Comparing '" << first << "' to '" << second << "'" << std::endl;
402 CPPUNIT_ASSERT(first == second);
403 }
404 }
405 {
406 params->setParameter(Psi3Parser_Parameters::multiplicityParam, "1");
407 params->setParameter(Psi3Parser_Parameters::chargeParam, "2");
408 params->setParameter(Psi3Parser_Parameters::soccParam, "( 1 1 0 0 0 0 0 0 )");
409 params->setParameter(Psi3Parser_Parameters::doccParam, "( 0 0 0 0 0 0 0 0 )");
410 params->setParameter(Psi3Parser_Parameters::subgroupParam, "c2v");
411 params->setParameter(Psi3Parser_Parameters::unique_axisParam, "x");
412 // compare both configs for TWOCON
413 stringstream output;
414 params->setParameter(
415 Psi3Parser_Parameters::referenceParam,
416 params->ValidReference[Psi3Parser_Parameters::TWOCON]
417 );
418 parser->save(&output, atoms);
419 params->setParameter(Psi3Parser_Parameters::multiplicityParam, "1");
420 params->setParameter(Psi3Parser_Parameters::chargeParam, "0");
421 stringstream input(hydrogenPsi3_TWOCON);
422 // check for non-empty streams
423 input.peek();
424 output.peek();
425 CPPUNIT_ASSERT(input.good() && output.good());
426 // check equality of streams per line (for debugging)
427 for (; std::getline(input, first) && std::getline(output, second); ) {
428 //std::cout << "Comparing '" << first << "' to '" << second << "'" << std::endl;
429 CPPUNIT_ASSERT(first == second);
430 }
431 }
432}
Note: See TracBrowser for help on using the repository browser.