| 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 |  * ParserCommonUnitTest.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 "ParserCommonUnitTest.hpp"
 | 
|---|
| 21 | 
 | 
|---|
| 22 | #include <cppunit/CompilerOutputter.h>
 | 
|---|
| 23 | #include <cppunit/extensions/TestFactoryRegistry.h>
 | 
|---|
| 24 | #include <cppunit/ui/text/TestRunner.h>
 | 
|---|
| 25 | 
 | 
|---|
| 26 | #include "Parser/MpqcParser.hpp"
 | 
|---|
| 27 | #include "Parser/PdbParser.hpp"
 | 
|---|
| 28 | #include "Parser/PcpParser.hpp"
 | 
|---|
| 29 | #include "Parser/TremoloParser.hpp"
 | 
|---|
| 30 | #include "Parser/XyzParser.hpp"
 | 
|---|
| 31 | #include "World.hpp"
 | 
|---|
| 32 | #include "atom.hpp"
 | 
|---|
| 33 | #include "element.hpp"
 | 
|---|
| 34 | #include "periodentafel.hpp"
 | 
|---|
| 35 | #include "Descriptors/AtomTypeDescriptor.hpp"
 | 
|---|
| 36 | 
 | 
|---|
| 37 | #ifdef HAVE_TESTRUNNER
 | 
|---|
| 38 | #include "UnitTestMain.hpp"
 | 
|---|
| 39 | #endif /*HAVE_TESTRUNNER*/
 | 
|---|
| 40 | 
 | 
|---|
| 41 | using namespace std;
 | 
|---|
| 42 | 
 | 
|---|
| 43 | // Registers the fixture into the 'registry'
 | 
|---|
| 44 | CPPUNIT_TEST_SUITE_REGISTRATION( ParserCommonUnitTest );
 | 
|---|
| 45 | 
 | 
|---|
| 46 | static string waterPcp = "# ParallelCarParinello - main configuration file - created with molecuilder\n\
 | 
|---|
| 47 | \n\
 | 
|---|
| 48 | mainname\tpcp\t# programm name (for runtime files)\n\
 | 
|---|
| 49 | defaultpath\not specified\t# where to put files during runtime\n\
 | 
|---|
| 50 | pseudopotpath\not specified\t# where to find pseudopotentials\n\
 | 
|---|
| 51 | \n\
 | 
|---|
| 52 | ProcPEGamma\t8\t# for parallel computing: share constants\n\
 | 
|---|
| 53 | ProcPEPsi\t1\t# for parallel computing: share wave functions\n\
 | 
|---|
| 54 | DoOutVis\t0\t# Output data for OpenDX\n\
 | 
|---|
| 55 | DoOutMes\t1\t# Output data for measurements\n\
 | 
|---|
| 56 | DoOutOrbitals\t0\t# Output all Orbitals\n\
 | 
|---|
| 57 | DoOutCurr\t0\t# Ouput current density for OpenDx\n\
 | 
|---|
| 58 | DoOutNICS\t0\t# Output Nucleus independent current shieldings\n\
 | 
|---|
| 59 | DoPerturbation\t0\t# Do perturbation calculate and determine susceptibility and shielding\n\
 | 
|---|
| 60 | DoFullCurrent\t0\t# Do full perturbation\n\
 | 
|---|
| 61 | DoConstrainedMD\t0\t# Do perform a constrained (>0, relating to current MD step) instead of unconstrained (0) MD\n\
 | 
|---|
| 62 | Thermostat\tBerendsen\t2.5\t# Which Thermostat and its parameters to use in MD case.\n\
 | 
|---|
| 63 | CommonWannier\t0\t# Put virtual centers at indivual orbits, all common, merged by variance, to grid point, to cell center\n\
 | 
|---|
| 64 | SawtoothStart\t0.01\t# Absolute value for smooth transition at cell border \n\
 | 
|---|
| 65 | VectorPlane\t0\t# Cut plane axis (x, y or z: 0,1,2) for two-dim current vector plot\n\
 | 
|---|
| 66 | VectorCut\t0\t# Cut plane axis value\n\
 | 
|---|
| 67 | AddGramSch\t1\t# Additional GramSchmidtOrtogonalization to be safe\n\
 | 
|---|
| 68 | Seed\t1\t# initial value for random seed for Psi coefficients\n\
 | 
|---|
| 69 | \n\
 | 
|---|
| 70 | MaxOuterStep\t0\t# number of MolecularDynamics/Structure optimization steps\n\
 | 
|---|
| 71 | Deltat\t0.01\t# time per MD step\n\
 | 
|---|
| 72 | OutVisStep\t10\t# Output visual data every ...th step\n\
 | 
|---|
| 73 | OutSrcStep\t5\t# Output \"restart\" data every ..th step\n\
 | 
|---|
| 74 | TargetTemp\t0.000950045\t# Target temperature\n\
 | 
|---|
| 75 | MaxPsiStep\t3\t# number of Minimisation steps per state (0 - default)\n\
 | 
|---|
| 76 | EpsWannier\t1e-07\t# tolerance value for spread minimisation of orbitals\n\
 | 
|---|
| 77 | # Values specifying when to stop\n\
 | 
|---|
| 78 | MaxMinStep\t100\t# Maximum number of steps\n\
 | 
|---|
| 79 | RelEpsTotalE\t1e-07\t# relative change in total energy\n\
 | 
|---|
| 80 | RelEpsKineticE\t1e-05\t# relative change in kinetic energy\n\
 | 
|---|
| 81 | MaxMinStopStep\t2\t# check every ..th steps\n\
 | 
|---|
| 82 | MaxMinGapStopStep\t1\t# check every ..th steps\n\
 | 
|---|
| 83 | \n\
 | 
|---|
| 84 | # Values specifying when to stop for INIT, otherwise same as above\n\
 | 
|---|
| 85 | MaxInitMinStep\t100\t# Maximum number of steps\n\
 | 
|---|
| 86 | InitRelEpsTotalE\t1e-05\t# relative change in total energy\n\
 | 
|---|
| 87 | InitRelEpsKineticE\t0.0001\t# relative change in kinetic energy\n\
 | 
|---|
| 88 | InitMaxMinStopStep\t2\t# check every ..th steps\n\
 | 
|---|
| 89 | InitMaxMinGapStopStep\t1\t# check every ..th steps\n\
 | 
|---|
| 90 | \n\
 | 
|---|
| 91 | BoxLength\t# (Length of a unit cell)\n\
 | 
|---|
| 92 | 20\n\
 | 
|---|
| 93 | 0\t20\n\
 | 
|---|
| 94 | 0\t0\t20\n\
 | 
|---|
| 95 | \n\
 | 
|---|
| 96 | ECut\t128\t# energy cutoff for discretization in Hartrees\n\
 | 
|---|
| 97 | MaxLevel\t5\t# number of different levels in the code, >=2\n\
 | 
|---|
| 98 | Level0Factor\t2\t# factor by which node number increases from S to 0 level\n\
 | 
|---|
| 99 | RiemannTensor\t0\t# (Use metric)\n\
 | 
|---|
| 100 | PsiType\t0\t# 0 - doubly occupied, 1 - SpinUp,SpinDown\n\
 | 
|---|
| 101 | MaxPsiDouble\t2\t# here: specifying both maximum number of SpinUp- and -Down-states\n\
 | 
|---|
| 102 | PsiMaxNoUp\t2\t# here: specifying maximum number of SpinUp-states\n\
 | 
|---|
| 103 | PsiMaxNoDown\t2\t# here: specifying maximum number of SpinDown-states\n\
 | 
|---|
| 104 | AddPsis\t0\t# Additional unoccupied Psis for bandgap determination\n\
 | 
|---|
| 105 | \n\
 | 
|---|
| 106 | RCut\t20\t# R-cut for the ewald summation\n\
 | 
|---|
| 107 | StructOpt\t0\t# Do structure optimization beforehand\n\
 | 
|---|
| 108 | IsAngstroem\t1\t# 0 - Bohr, 1 - Angstroem\n\
 | 
|---|
| 109 | RelativeCoord\t0\t# whether ion coordinates are relative (1) or absolute (0)\n\
 | 
|---|
| 110 | MaxTypes\t2\t# maximum number of different ion types\n\
 | 
|---|
| 111 | \n\
 | 
|---|
| 112 | # Ion type data (PP = PseudoPotential, Z = atomic number)\n\
 | 
|---|
| 113 | #Ion_TypeNr.\tAmount\tZ\tRGauss\tL_Max(PP)L_Loc(PP)IonMass\t# chemical name, symbol\n\
 | 
|---|
| 114 | Ion_Type1\t2\t1\t1.0\t3\t3\t1.008\tHydrogen\tH\n\
 | 
|---|
| 115 | Ion_Type2\t1\t8\t1.0\t3\t3\t15.999\tOxygen\tO\n\
 | 
|---|
| 116 | #Ion_TypeNr._Nr.R[0]\tR[1]\tR[2]\tMoveType (0 MoveIon, 1 FixedIon)\n\
 | 
|---|
| 117 | Ion_Type2_1\t0.000000000\t0.000000000\t0.000000000\t0 # molecule nr 0\n\
 | 
|---|
| 118 | Ion_Type1_1\t0.758602\t0.000000000\t0.504284\t0 # molecule nr 1\n\
 | 
|---|
| 119 | Ion_Type1_2\t0.758602\t0.000000000\t-0.504284\t0 # molecule nr 2\n";
 | 
|---|
| 120 | //----|----*|---||--*||---|***|-------|-------|-------|-----|---------------|-|-
 | 
|---|
| 121 | //000000011111111112222222222333333333344444444445555555555666666666677777777778
 | 
|---|
| 122 | //345678901234567890123456789012345678901234567890123456789012345678901234567890
 | 
|---|
| 123 | static string waterPdb = "\
 | 
|---|
| 124 | REMARK  This is a test water molecule as written by TREMOLO.\n\
 | 
|---|
| 125 | ATOM      1  OT  GMT-    0       1.583   1.785   1.480  1.00178.02           O-2\n\
 | 
|---|
| 126 | ATOM      2  HT  GMT-    0       1.186   1.643   2.213  1.00103.58           H+1\n\
 | 
|---|
| 127 | ATOM      3  HT  GMT-    0       2.642   1.896   1.730  1.00126.00           H+1\n\
 | 
|---|
| 128 | ATOM      4  OT  GMT-    1       3.583   1.785   1.480  1.00178.02           O-2\n\
 | 
|---|
| 129 | ATOM      5  HT  GMT-    1       3.186   1.643   2.213  1.00103.58           H+1\n\
 | 
|---|
| 130 | ATOM      6  HT  GMT-    1       4.642   1.896   1.730  1.00126.00           H+1\n\
 | 
|---|
| 131 | CONECT    1    2    3\n\
 | 
|---|
| 132 | CONECT    2    1\n\
 | 
|---|
| 133 | CONECT    3    1\n\
 | 
|---|
| 134 | CONECT    4    5    6\n\
 | 
|---|
| 135 | CONECT    5    4\n\
 | 
|---|
| 136 | CONECT    6    4\n\
 | 
|---|
| 137 | END";
 | 
|---|
| 138 | static string waterMpqc ="% Created by MoleCuilder\n\
 | 
|---|
| 139 | mpqc: (\n\
 | 
|---|
| 140 | \tsavestate = no\n\
 | 
|---|
| 141 | \tdo_gradient = yes\n\
 | 
|---|
| 142 | \tmole<MBPT2>: (\n\
 | 
|---|
| 143 | \t\tmaxiter = 200\n\
 | 
|---|
| 144 | \t\tbasis = $:basis\n\
 | 
|---|
| 145 | \t\tmolecule = $:molecule\n\
 | 
|---|
| 146 | \t\treference<CLHF>: (\n\
 | 
|---|
| 147 | \t\t\tbasis = $:basis\n\
 | 
|---|
| 148 | \t\t\tmolecule = $:molecule\n\
 | 
|---|
| 149 | \t\t)\n\
 | 
|---|
| 150 | \t)\n\
 | 
|---|
| 151 | )\n\
 | 
|---|
| 152 | molecule<Molecule>: (\n\
 | 
|---|
| 153 | \tunit = angstrom\n\
 | 
|---|
| 154 | \t{ atoms geometry } = {\n\
 | 
|---|
| 155 | \t\tO [ -0.505735\t0\t0 ]\n\
 | 
|---|
| 156 | \t\tH [ 0.252867\t0\t0.504284 ]\n\
 | 
|---|
| 157 | \t\tH [ 0.252867\t0\t-0.504284 ]\n\
 | 
|---|
| 158 | \t}\n\
 | 
|---|
| 159 | )\n\
 | 
|---|
| 160 | basis<GaussianBasisSet>: (\n\
 | 
|---|
| 161 | \tname = \"3-21G\"\n\
 | 
|---|
| 162 | \tmolecule = $:molecule\n\
 | 
|---|
| 163 | )\n";
 | 
|---|
| 164 | static string waterXyz = "3\n\tH2O: water molecule\nO\t0\t0\t0\nH\t0.758602\t0\t0.504284\nH\t0.758602\t0\t-0.504284\n";
 | 
|---|
| 165 | 
 | 
|---|
| 166 | void ParserCommonUnitTest::setUp() {
 | 
|---|
| 167 |   World::getInstance();
 | 
|---|
| 168 | 
 | 
|---|
| 169 |   setVerbosity(2);
 | 
|---|
| 170 | 
 | 
|---|
| 171 |   // we need hydrogens and oxygens in the following tests
 | 
|---|
| 172 |   CPPUNIT_ASSERT(World::getInstance().getPeriode()->FindElement(1) != NULL);
 | 
|---|
| 173 |   CPPUNIT_ASSERT(World::getInstance().getPeriode()->FindElement(8) != NULL);
 | 
|---|
| 174 | }
 | 
|---|
| 175 | 
 | 
|---|
| 176 | void ParserCommonUnitTest::tearDown() {
 | 
|---|
| 177 |   ChangeTracker::purgeInstance();
 | 
|---|
| 178 |   World::purgeInstance();
 | 
|---|
| 179 | }
 | 
|---|
| 180 | 
 | 
|---|
| 181 | /************************************ tests ***********************************/
 | 
|---|
| 182 | 
 | 
|---|
| 183 | void ParserCommonUnitTest::rewriteAnXyzTest() {
 | 
|---|
| 184 |   cout << "Testing the XYZ parser." << endl;
 | 
|---|
| 185 |   XyzParser* testParser = new XyzParser();
 | 
|---|
| 186 |   stringstream input;
 | 
|---|
| 187 |   input << waterXyz;
 | 
|---|
| 188 |   testParser->load(&input);
 | 
|---|
| 189 |   input.clear();
 | 
|---|
| 190 | 
 | 
|---|
| 191 |   CPPUNIT_ASSERT_EQUAL(3, World::getInstance().numAtoms());
 | 
|---|
| 192 | 
 | 
|---|
| 193 |   // store and parse in again
 | 
|---|
| 194 |   {
 | 
|---|
| 195 |     stringstream output;
 | 
|---|
| 196 |     std::vector<atom *> atoms = World::getInstance().getAllAtoms();
 | 
|---|
| 197 |     testParser->save(&output, atoms);
 | 
|---|
| 198 |     input << output.str();
 | 
|---|
| 199 |     testParser->load(&input);
 | 
|---|
| 200 |   }
 | 
|---|
| 201 | 
 | 
|---|
| 202 |   // now twice as many
 | 
|---|
| 203 |   CPPUNIT_ASSERT_EQUAL(6, World::getInstance().numAtoms());
 | 
|---|
| 204 | 
 | 
|---|
| 205 |   // check every atom
 | 
|---|
| 206 |   std::vector<atom *> atoms = World::getInstance().getAllAtoms();
 | 
|---|
| 207 |   std::vector<atom *>::const_iterator firstiter = atoms.begin();
 | 
|---|
| 208 |   std::vector<atom *>::const_iterator seconditer = atoms.begin();
 | 
|---|
| 209 |   for (size_t i=0;i<3;i++)
 | 
|---|
| 210 |     ++seconditer;
 | 
|---|
| 211 |   for (;
 | 
|---|
| 212 |       seconditer != atoms.end();
 | 
|---|
| 213 |       ++firstiter,++seconditer) {
 | 
|---|
| 214 |     // check position and type (only stuff xyz stores)
 | 
|---|
| 215 |     CPPUNIT_ASSERT_EQUAL((*firstiter)->getPosition(),(*seconditer)->getPosition());
 | 
|---|
| 216 |     CPPUNIT_ASSERT_EQUAL((*firstiter)->getType(),(*seconditer)->getType());
 | 
|---|
| 217 |   }
 | 
|---|
| 218 | }
 | 
|---|
| 219 | 
 | 
|---|
| 220 | void ParserCommonUnitTest::readwritePcpTest() {
 | 
|---|
| 221 |   stringstream input(waterPcp);
 | 
|---|
| 222 |   PcpParser* testParser = new PcpParser();
 | 
|---|
| 223 |   testParser->load(&input);
 | 
|---|
| 224 |   input.clear();
 | 
|---|
| 225 | 
 | 
|---|
| 226 |   CPPUNIT_ASSERT_EQUAL(3, World::getInstance().numAtoms());
 | 
|---|
| 227 | 
 | 
|---|
| 228 |   // check that equality function is ok
 | 
|---|
| 229 |   CPPUNIT_ASSERT(*testParser == *testParser);
 | 
|---|
| 230 | 
 | 
|---|
| 231 |   stringstream output;
 | 
|---|
| 232 |   std::vector<atom *> atoms = World::getInstance().getAllAtoms();
 | 
|---|
| 233 |   testParser->save(&output, atoms);
 | 
|---|
| 234 | 
 | 
|---|
| 235 |   input << output.str();
 | 
|---|
| 236 |   PcpParser* testParser2 = new PcpParser();
 | 
|---|
| 237 |   testParser2->load(&input);
 | 
|---|
| 238 | 
 | 
|---|
| 239 |   CPPUNIT_ASSERT_EQUAL(6, World::getInstance().numAtoms());
 | 
|---|
| 240 | 
 | 
|---|
| 241 |   CPPUNIT_ASSERT(*testParser == *testParser2);
 | 
|---|
| 242 | }
 | 
|---|
| 243 | 
 | 
|---|
| 244 | void ParserCommonUnitTest::readwritePdbTest() {
 | 
|---|
| 245 |   stringstream input;
 | 
|---|
| 246 |   input << waterPdb;
 | 
|---|
| 247 |   PdbParser* testParser = new PdbParser();
 | 
|---|
| 248 |   testParser->load(&input);
 | 
|---|
| 249 |   input.clear();
 | 
|---|
| 250 | 
 | 
|---|
| 251 |   CPPUNIT_ASSERT_EQUAL(6, World::getInstance().numAtoms());
 | 
|---|
| 252 | 
 | 
|---|
| 253 |   stringstream output;
 | 
|---|
| 254 |   std::vector<atom *> atoms = World::getInstance().getAllAtoms();
 | 
|---|
| 255 |   testParser->save(&output, atoms);
 | 
|---|
| 256 | 
 | 
|---|
| 257 | //  std::cout << "Save PDB is:" << std::endl;
 | 
|---|
| 258 | //  std::cout << output.str() << std::endl;
 | 
|---|
| 259 | 
 | 
|---|
| 260 |   input << output.str();
 | 
|---|
| 261 |   PdbParser* testParser2 = new PdbParser();
 | 
|---|
| 262 |   testParser2->load(&input);
 | 
|---|
| 263 | 
 | 
|---|
| 264 |   CPPUNIT_ASSERT_EQUAL(12, World::getInstance().numAtoms());
 | 
|---|
| 265 | }
 | 
|---|
| 266 | 
 | 
|---|
| 267 | void ParserCommonUnitTest::writeMpqcTest() {
 | 
|---|
| 268 |   // build up water molecule
 | 
|---|
| 269 |   atom *Walker = NULL;
 | 
|---|
| 270 |   Walker = World::getInstance().createAtom();
 | 
|---|
| 271 |   Walker->setType(8);
 | 
|---|
| 272 |   Walker->setPosition(Vector(0,0,0));
 | 
|---|
| 273 |   Walker = World::getInstance().createAtom();
 | 
|---|
| 274 |   Walker->setType(1);
 | 
|---|
| 275 |   Walker->setPosition(Vector(0.758602,0,0.504284));
 | 
|---|
| 276 |   Walker = World::getInstance().createAtom();
 | 
|---|
| 277 |   Walker->setType(1);
 | 
|---|
| 278 |   Walker->setPosition(Vector(0.758602,0,-0.504284));
 | 
|---|
| 279 |   CPPUNIT_ASSERT_EQUAL(3, World::getInstance().numAtoms());
 | 
|---|
| 280 | 
 | 
|---|
| 281 |   // create two stringstreams, one stored, one created
 | 
|---|
| 282 |   stringstream input(waterMpqc);
 | 
|---|
| 283 |   MpqcParser* testParser = new MpqcParser();
 | 
|---|
| 284 |   stringstream output;
 | 
|---|
| 285 |   std::vector<atom *> atoms = World::getInstance().getAllAtoms();
 | 
|---|
| 286 |   testParser->save(&output, atoms);
 | 
|---|
| 287 | 
 | 
|---|
| 288 |   // compare both configs
 | 
|---|
| 289 |   string first = input.str();
 | 
|---|
| 290 |   string second = output.str();
 | 
|---|
| 291 |   CPPUNIT_ASSERT(first == second);
 | 
|---|
| 292 | }
 | 
|---|