| 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 | * ParserUnitTest.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 "ParserUnitTest.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/PcpParser.hpp" | 
|---|
| 28 | #include "Parser/TremoloParser.hpp" | 
|---|
| 29 | #include "Parser/XyzParser.hpp" | 
|---|
| 30 | #include "World.hpp" | 
|---|
| 31 | #include "atom.hpp" | 
|---|
| 32 | #include "element.hpp" | 
|---|
| 33 | #include "periodentafel.hpp" | 
|---|
| 34 | #include "Descriptors/AtomTypeDescriptor.hpp" | 
|---|
| 35 |  | 
|---|
| 36 | #ifdef HAVE_TESTRUNNER | 
|---|
| 37 | #include "UnitTestMain.hpp" | 
|---|
| 38 | #endif /*HAVE_TESTRUNNER*/ | 
|---|
| 39 |  | 
|---|
| 40 | using namespace std; | 
|---|
| 41 |  | 
|---|
| 42 | // Registers the fixture into the 'registry' | 
|---|
| 43 | CPPUNIT_TEST_SUITE_REGISTRATION( ParserUnitTest ); | 
|---|
| 44 |  | 
|---|
| 45 | static string waterPcp = "# ParallelCarParinello - main configuration file - created with molecuilder\n\ | 
|---|
| 46 | \n\ | 
|---|
| 47 | mainname\tpcp\t# programm name (for runtime files)\n\ | 
|---|
| 48 | defaultpath\not specified\t# where to put files during runtime\n\ | 
|---|
| 49 | pseudopotpath\not specified\t# where to find pseudopotentials\n\ | 
|---|
| 50 | \n\ | 
|---|
| 51 | ProcPEGamma\t8\t# for parallel computing: share constants\n\ | 
|---|
| 52 | ProcPEPsi\t1\t# for parallel computing: share wave functions\n\ | 
|---|
| 53 | DoOutVis\t0\t# Output data for OpenDX\n\ | 
|---|
| 54 | DoOutMes\t1\t# Output data for measurements\n\ | 
|---|
| 55 | DoOutOrbitals\t0\t# Output all Orbitals\n\ | 
|---|
| 56 | DoOutCurr\t0\t# Ouput current density for OpenDx\n\ | 
|---|
| 57 | DoOutNICS\t0\t# Output Nucleus independent current shieldings\n\ | 
|---|
| 58 | DoPerturbation\t0\t# Do perturbation calculate and determine susceptibility and shielding\n\ | 
|---|
| 59 | DoFullCurrent\t0\t# Do full perturbation\n\ | 
|---|
| 60 | DoConstrainedMD\t0\t# Do perform a constrained (>0, relating to current MD step) instead of unconstrained (0) MD\n\ | 
|---|
| 61 | Thermostat\tBerendsen\t2.5\t# Which Thermostat and its parameters to use in MD case.\n\ | 
|---|
| 62 | CommonWannier\t0\t# Put virtual centers at indivual orbits, all common, merged by variance, to grid point, to cell center\n\ | 
|---|
| 63 | SawtoothStart\t0.01\t# Absolute value for smooth transition at cell border \n\ | 
|---|
| 64 | VectorPlane\t0\t# Cut plane axis (x, y or z: 0,1,2) for two-dim current vector plot\n\ | 
|---|
| 65 | VectorCut\t0\t# Cut plane axis value\n\ | 
|---|
| 66 | AddGramSch\t1\t# Additional GramSchmidtOrtogonalization to be safe\n\ | 
|---|
| 67 | Seed\t1\t# initial value for random seed for Psi coefficients\n\ | 
|---|
| 68 | \n\ | 
|---|
| 69 | MaxOuterStep\t0\t# number of MolecularDynamics/Structure optimization steps\n\ | 
|---|
| 70 | Deltat\t0.01\t# time per MD step\n\ | 
|---|
| 71 | OutVisStep\t10\t# Output visual data every ...th step\n\ | 
|---|
| 72 | OutSrcStep\t5\t# Output \"restart\" data every ..th step\n\ | 
|---|
| 73 | TargetTemp\t0.000950045\t# Target temperature\n\ | 
|---|
| 74 | MaxPsiStep\t3\t# number of Minimisation steps per state (0 - default)\n\ | 
|---|
| 75 | EpsWannier\t1e-07\t# tolerance value for spread minimisation of orbitals\n\ | 
|---|
| 76 | # Values specifying when to stop\n\ | 
|---|
| 77 | MaxMinStep\t100\t# Maximum number of steps\n\ | 
|---|
| 78 | RelEpsTotalE\t1e-07\t# relative change in total energy\n\ | 
|---|
| 79 | RelEpsKineticE\t1e-05\t# relative change in kinetic energy\n\ | 
|---|
| 80 | MaxMinStopStep\t2\t# check every ..th steps\n\ | 
|---|
| 81 | MaxMinGapStopStep\t1\t# check every ..th steps\n\ | 
|---|
| 82 | \n\ | 
|---|
| 83 | # Values specifying when to stop for INIT, otherwise same as above\n\ | 
|---|
| 84 | MaxInitMinStep\t100\t# Maximum number of steps\n\ | 
|---|
| 85 | InitRelEpsTotalE\t1e-05\t# relative change in total energy\n\ | 
|---|
| 86 | InitRelEpsKineticE\t0.0001\t# relative change in kinetic energy\n\ | 
|---|
| 87 | InitMaxMinStopStep\t2\t# check every ..th steps\n\ | 
|---|
| 88 | InitMaxMinGapStopStep\t1\t# check every ..th steps\n\ | 
|---|
| 89 | \n\ | 
|---|
| 90 | BoxLength\t# (Length of a unit cell)\n\ | 
|---|
| 91 | 20\n\ | 
|---|
| 92 | 0\t20\n\ | 
|---|
| 93 | 0\t0\t20\n\ | 
|---|
| 94 | \n\ | 
|---|
| 95 | ECut\t128\t# energy cutoff for discretization in Hartrees\n\ | 
|---|
| 96 | MaxLevel\t5\t# number of different levels in the code, >=2\n\ | 
|---|
| 97 | Level0Factor\t2\t# factor by which node number increases from S to 0 level\n\ | 
|---|
| 98 | RiemannTensor\t0\t# (Use metric)\n\ | 
|---|
| 99 | PsiType\t0\t# 0 - doubly occupied, 1 - SpinUp,SpinDown\n\ | 
|---|
| 100 | MaxPsiDouble\t2\t# here: specifying both maximum number of SpinUp- and -Down-states\n\ | 
|---|
| 101 | PsiMaxNoUp\t2\t# here: specifying maximum number of SpinUp-states\n\ | 
|---|
| 102 | PsiMaxNoDown\t2\t# here: specifying maximum number of SpinDown-states\n\ | 
|---|
| 103 | AddPsis\t0\t# Additional unoccupied Psis for bandgap determination\n\ | 
|---|
| 104 | \n\ | 
|---|
| 105 | RCut\t20\t# R-cut for the ewald summation\n\ | 
|---|
| 106 | StructOpt\t0\t# Do structure optimization beforehand\n\ | 
|---|
| 107 | IsAngstroem\t1\t# 0 - Bohr, 1 - Angstroem\n\ | 
|---|
| 108 | RelativeCoord\t0\t# whether ion coordinates are relative (1) or absolute (0)\n\ | 
|---|
| 109 | MaxTypes\t2\t# maximum number of different ion types\n\ | 
|---|
| 110 | \n\ | 
|---|
| 111 | # Ion type data (PP = PseudoPotential, Z = atomic number)\n\ | 
|---|
| 112 | #Ion_TypeNr.\tAmount\tZ\tRGauss\tL_Max(PP)L_Loc(PP)IonMass\t# chemical name, symbol\n\ | 
|---|
| 113 | Ion_Type1\t2\t1\t1.0\t3\t3\t1.008\tHydrogen\tH\n\ | 
|---|
| 114 | Ion_Type2\t1\t8\t1.0\t3\t3\t15.999\tOxygen\tO\n\ | 
|---|
| 115 | #Ion_TypeNr._Nr.R[0]\tR[1]\tR[2]\tMoveType (0 MoveIon, 1 FixedIon)\n\ | 
|---|
| 116 | Ion_Type2_1\t0.000000000\t0.000000000\t0.000000000\t0 # molecule nr 0\n\ | 
|---|
| 117 | Ion_Type1_1\t0.758602\t0.000000000\t0.504284\t0 # molecule nr 1\n\ | 
|---|
| 118 | Ion_Type1_2\t0.758602\t0.000000000\t-0.504284\t0 # molecule nr 2\n"; | 
|---|
| 119 | static string waterMpqc ="% Created by MoleCuilder\n\ | 
|---|
| 120 | mpqc: (\n\ | 
|---|
| 121 | \tsavestate = no\n\ | 
|---|
| 122 | \tdo_gradient = yes\n\ | 
|---|
| 123 | \tmole<MBPT2>: (\n\ | 
|---|
| 124 | \t\tmaxiter = 200\n\ | 
|---|
| 125 | \t\tbasis = $:basis\n\ | 
|---|
| 126 | \t\tmolecule = $:molecule\n\ | 
|---|
| 127 | \t\treference<CLHF>: (\n\ | 
|---|
| 128 | \t\t\tbasis = $:basis\n\ | 
|---|
| 129 | \t\t\tmolecule = $:molecule\n\ | 
|---|
| 130 | \t\t)\n\ | 
|---|
| 131 | \t)\n\ | 
|---|
| 132 | )\n\ | 
|---|
| 133 | molecule<Molecule>: (\n\ | 
|---|
| 134 | \tunit = angstrom\n\ | 
|---|
| 135 | \t{ atoms geometry } = {\n\ | 
|---|
| 136 | \t\tO [ -0.505735\t0\t0 ]\n\ | 
|---|
| 137 | \t\tH [ 0.252867\t0\t0.504284 ]\n\ | 
|---|
| 138 | \t\tH [ 0.252867\t0\t-0.504284 ]\n\ | 
|---|
| 139 | \t}\n\ | 
|---|
| 140 | )\n\ | 
|---|
| 141 | basis<GaussianBasisSet>: (\n\ | 
|---|
| 142 | \tname = \"3-21G\"\n\ | 
|---|
| 143 | \tmolecule = $:molecule\n\ | 
|---|
| 144 | )\n"; | 
|---|
| 145 | 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"; | 
|---|
| 146 | static string Tremolo_Atomdata1 = "# ATOMDATA\tId\tname\tType\tx=3\n"; | 
|---|
| 147 | static string Tremolo_Atomdata2 = "#\n#ATOMDATA Id name Type x=3\n1 hydrogen H 3.0 4.5 0.1\n\n"; | 
|---|
| 148 | static string Tremolo_invalidkey = "#\n#ATOMDATA Id name foo Type x=3\n\n\n"; | 
|---|
| 149 | static string Tremolo_velocity = "#\n#ATOMDATA Id name Type u=3\n1 hydrogen H 3.0 4.5 0.1\n\n"; | 
|---|
| 150 | static string Tremolo_neighbours = "#\n#ATOMDATA Id Type neighbors=2\n1 H 3 0\n2 H 3 0\n3 O 1 2\n"; | 
|---|
| 151 | static string Tremolo_improper = "#\n#ATOMDATA Id Type imprData\n8 H 9-10\n9 H 10-8,8-10\n10 O -\n"; | 
|---|
| 152 | static string Tremolo_torsion = "#\n#ATOMDATA Id Type torsion\n8 H 9-10\n9 H 10-8,8-10\n10 O -\n"; | 
|---|
| 153 | static string Tremolo_full = "# ATOMDATA\tx=3\tu=3\tF\tstress\tId\tneighbors=5\timprData\tGroupMeasureTypeNo\tType\textType\tname\tresName\tchainID\tresSeq\toccupancy\ttempFactor\tsegID\tCharge\tcharge\tGrpTypeNo\ttorsion\n0\t0\t0\t0\t0\t0\t0\t0\t0\t0\t0\t0\t0\t0\t-\t0\tH\t-\t-\t-\t0\t0\t0\t0\t0\t0\t0\t0\t-\t\n"; | 
|---|
| 154 |  | 
|---|
| 155 | void ParserUnitTest::setUp() { | 
|---|
| 156 | World::getInstance(); | 
|---|
| 157 |  | 
|---|
| 158 | // we need hydrogens and oxygens in the following tests | 
|---|
| 159 | CPPUNIT_ASSERT(World::getInstance().getPeriode()->FindElement(1) != NULL); | 
|---|
| 160 | CPPUNIT_ASSERT(World::getInstance().getPeriode()->FindElement(8) != NULL); | 
|---|
| 161 | } | 
|---|
| 162 |  | 
|---|
| 163 | void ParserUnitTest::tearDown() { | 
|---|
| 164 | ChangeTracker::purgeInstance(); | 
|---|
| 165 | World::purgeInstance(); | 
|---|
| 166 | } | 
|---|
| 167 |  | 
|---|
| 168 | /************************************ tests ***********************************/ | 
|---|
| 169 |  | 
|---|
| 170 | void ParserUnitTest::rewriteAnXyzTest() { | 
|---|
| 171 | cout << "Testing the XYZ parser." << endl; | 
|---|
| 172 | XyzParser* testParser = new XyzParser(); | 
|---|
| 173 | stringstream input; | 
|---|
| 174 | input << waterXyz; | 
|---|
| 175 | testParser->load(&input); | 
|---|
| 176 |  | 
|---|
| 177 | CPPUNIT_ASSERT_EQUAL(3, World::getInstance().numAtoms()); | 
|---|
| 178 |  | 
|---|
| 179 | string newWaterXyz = ""; | 
|---|
| 180 | stringstream output; | 
|---|
| 181 | testParser->save(&output); | 
|---|
| 182 | newWaterXyz = output.str(); | 
|---|
| 183 |  | 
|---|
| 184 | CPPUNIT_ASSERT(waterXyz == newWaterXyz); | 
|---|
| 185 | } | 
|---|
| 186 |  | 
|---|
| 187 | void ParserUnitTest::readTremoloPreliminaryCommentsTest() { | 
|---|
| 188 | cout << "Testing the tremolo parser." << endl; | 
|---|
| 189 | TremoloParser* testParser = new TremoloParser(); | 
|---|
| 190 | stringstream input, output; | 
|---|
| 191 |  | 
|---|
| 192 | // Atomdata beginning with "# ATOMDATA" | 
|---|
| 193 | input << Tremolo_Atomdata1; | 
|---|
| 194 | testParser->load(&input); | 
|---|
| 195 | testParser->save(&output); | 
|---|
| 196 | CPPUNIT_ASSERT(Tremolo_Atomdata1 == output.str()); | 
|---|
| 197 | input.clear(); | 
|---|
| 198 | output.clear(); | 
|---|
| 199 |  | 
|---|
| 200 | // Atomdata beginning with "#ATOMDATA" | 
|---|
| 201 | input << Tremolo_Atomdata2; | 
|---|
| 202 | testParser->load(&input); | 
|---|
| 203 | testParser->save(&output); | 
|---|
| 204 | CPPUNIT_ASSERT(output.str().find("hydrogen") != string::npos); | 
|---|
| 205 | input.clear(); | 
|---|
| 206 | output.clear(); | 
|---|
| 207 |  | 
|---|
| 208 | // Invalid key in Atomdata line | 
|---|
| 209 | input << Tremolo_invalidkey; | 
|---|
| 210 | testParser->load(&input); | 
|---|
| 211 | //TODO: proove invalidity | 
|---|
| 212 | input.clear(); | 
|---|
| 213 | } | 
|---|
| 214 |  | 
|---|
| 215 | void ParserUnitTest::readTremoloCoordinatesTest() { | 
|---|
| 216 | TremoloParser* testParser = new TremoloParser(); | 
|---|
| 217 | stringstream input; | 
|---|
| 218 |  | 
|---|
| 219 | // One simple data line | 
|---|
| 220 | input << Tremolo_Atomdata2; | 
|---|
| 221 | testParser->load(&input); | 
|---|
| 222 | CPPUNIT_ASSERT(World::getInstance().getAtom(AtomByType(1))->at(0) == 3.0); | 
|---|
| 223 | input.clear(); | 
|---|
| 224 | } | 
|---|
| 225 |  | 
|---|
| 226 | void ParserUnitTest::readTremoloVelocityTest() { | 
|---|
| 227 | TremoloParser* testParser = new TremoloParser(); | 
|---|
| 228 | stringstream input; | 
|---|
| 229 |  | 
|---|
| 230 | // One simple data line | 
|---|
| 231 | input << Tremolo_velocity; | 
|---|
| 232 | testParser->load(&input); | 
|---|
| 233 | CPPUNIT_ASSERT(World::getInstance().getAtom(AtomByType(1))->AtomicVelocity[0] == 3.0); | 
|---|
| 234 | input.clear(); | 
|---|
| 235 | } | 
|---|
| 236 |  | 
|---|
| 237 | void ParserUnitTest::readTremoloNeighborInformationTest() { | 
|---|
| 238 | TremoloParser* testParser = new TremoloParser(); | 
|---|
| 239 | stringstream input; | 
|---|
| 240 |  | 
|---|
| 241 | // Neighbor data | 
|---|
| 242 | input << Tremolo_neighbours; | 
|---|
| 243 | testParser->load(&input); | 
|---|
| 244 |  | 
|---|
| 245 | CPPUNIT_ASSERT_EQUAL(3, World::getInstance().numAtoms()); | 
|---|
| 246 | CPPUNIT_ASSERT(World::getInstance().getAtom(AtomByType(8))-> | 
|---|
| 247 | IsBondedTo(World::getInstance().getAtom(AtomByType(1)))); | 
|---|
| 248 | input.clear(); | 
|---|
| 249 | } | 
|---|
| 250 |  | 
|---|
| 251 | void ParserUnitTest::readAndWriteTremoloImprDataInformationTest() { | 
|---|
| 252 | TremoloParser* testParser = new TremoloParser(); | 
|---|
| 253 | stringstream input, output; | 
|---|
| 254 |  | 
|---|
| 255 | // Neighbor data | 
|---|
| 256 | input << Tremolo_improper; | 
|---|
| 257 | testParser->load(&input); | 
|---|
| 258 | testParser->save(&output); | 
|---|
| 259 | CPPUNIT_ASSERT_EQUAL(3, World::getInstance().numAtoms()); | 
|---|
| 260 | CPPUNIT_ASSERT(output.str().find("2-0,0-2") != string::npos); | 
|---|
| 261 | input.clear(); | 
|---|
| 262 | output.clear(); | 
|---|
| 263 | } | 
|---|
| 264 |  | 
|---|
| 265 | void ParserUnitTest::readAndWriteTremoloTorsionInformationTest() { | 
|---|
| 266 | TremoloParser* testParser = new TremoloParser(); | 
|---|
| 267 | stringstream input, output; | 
|---|
| 268 |  | 
|---|
| 269 | // Neighbor data | 
|---|
| 270 | input << Tremolo_torsion; | 
|---|
| 271 | testParser->load(&input); | 
|---|
| 272 | testParser->save(&output); | 
|---|
| 273 | CPPUNIT_ASSERT_EQUAL(3, World::getInstance().numAtoms()); | 
|---|
| 274 | CPPUNIT_ASSERT(output.str().find("2-0,0-2") != string::npos); | 
|---|
| 275 | input.clear(); | 
|---|
| 276 | output.clear(); | 
|---|
| 277 | } | 
|---|
| 278 |  | 
|---|
| 279 | void ParserUnitTest::writeTremoloTest() { | 
|---|
| 280 | TremoloParser* testParser = new TremoloParser(); | 
|---|
| 281 | stringstream output; | 
|---|
| 282 |  | 
|---|
| 283 | // with the maximum number of fields and minimal information, default values are printed | 
|---|
| 284 | atom* newAtom = World::getInstance().createAtom(); | 
|---|
| 285 | newAtom->setType(1); | 
|---|
| 286 | testParser->setFieldsForSave("x=3 u=3 F stress Id neighbors=5 imprData GroupMeasureTypeNo Type extType name resName chainID resSeq occupancy tempFactor segID Charge charge GrpTypeNo torsion"); | 
|---|
| 287 | testParser->save(&output); | 
|---|
| 288 | CPPUNIT_ASSERT(output.str() == Tremolo_full); | 
|---|
| 289 |  | 
|---|
| 290 | cout << "testing the tremolo parser is done" << endl; | 
|---|
| 291 | } | 
|---|
| 292 |  | 
|---|
| 293 | void ParserUnitTest::readwritePcpTest() { | 
|---|
| 294 | stringstream input(waterPcp); | 
|---|
| 295 | PcpParser* testParser = new PcpParser(); | 
|---|
| 296 | testParser->load(&input); | 
|---|
| 297 | input.clear(); | 
|---|
| 298 |  | 
|---|
| 299 | CPPUNIT_ASSERT_EQUAL(3, World::getInstance().numAtoms()); | 
|---|
| 300 |  | 
|---|
| 301 | string newWaterPcp = ""; | 
|---|
| 302 | stringstream output; | 
|---|
| 303 | testParser->save(&output); | 
|---|
| 304 |  | 
|---|
| 305 | input << output; | 
|---|
| 306 | PcpParser* testParser2 = new PcpParser(); | 
|---|
| 307 | testParser2->load(&input); | 
|---|
| 308 |  | 
|---|
| 309 | CPPUNIT_ASSERT_EQUAL(6, World::getInstance().numAtoms()); | 
|---|
| 310 |  | 
|---|
| 311 | CPPUNIT_ASSERT(*testParser == *testParser2); | 
|---|
| 312 | } | 
|---|
| 313 |  | 
|---|
| 314 | void ParserUnitTest::writeMpqcTest() { | 
|---|
| 315 | // build up water molecule | 
|---|
| 316 | atom *Walker = NULL; | 
|---|
| 317 | Walker = World::getInstance().createAtom(); | 
|---|
| 318 | Walker->setType(8); | 
|---|
| 319 | Walker->setPosition(Vector(0,0,0)); | 
|---|
| 320 | Walker = World::getInstance().createAtom(); | 
|---|
| 321 | Walker->setType(1); | 
|---|
| 322 | Walker->setPosition(Vector(0.758602,0,0.504284)); | 
|---|
| 323 | Walker = World::getInstance().createAtom(); | 
|---|
| 324 | Walker->setType(1); | 
|---|
| 325 | Walker->setPosition(Vector(0.758602,0,-0.504284)); | 
|---|
| 326 | CPPUNIT_ASSERT_EQUAL(3, World::getInstance().numAtoms()); | 
|---|
| 327 |  | 
|---|
| 328 | // create two stringstreams, one stored, one created | 
|---|
| 329 | stringstream input(waterMpqc); | 
|---|
| 330 | MpqcParser* testParser = new MpqcParser(); | 
|---|
| 331 | stringstream output; | 
|---|
| 332 | testParser->save(&output); | 
|---|
| 333 |  | 
|---|
| 334 | // compare both configs | 
|---|
| 335 | string first = input.str(); | 
|---|
| 336 | string second = output.str(); | 
|---|
| 337 | CPPUNIT_ASSERT(first == second); | 
|---|
| 338 | } | 
|---|