| 1 | /*
 | 
|---|
| 2 |  * CountBondsUnitTest.cpp
 | 
|---|
| 3 |  *
 | 
|---|
| 4 |  *  Created on: Mar 30, 2010
 | 
|---|
| 5 |  *      Author: heber
 | 
|---|
| 6 |  */
 | 
|---|
| 7 | 
 | 
|---|
| 8 | 
 | 
|---|
| 9 | using namespace std;
 | 
|---|
| 10 | 
 | 
|---|
| 11 | #include <cppunit/CompilerOutputter.h>
 | 
|---|
| 12 | #include <cppunit/extensions/TestFactoryRegistry.h>
 | 
|---|
| 13 | #include <cppunit/ui/text/TestRunner.h>
 | 
|---|
| 14 | 
 | 
|---|
| 15 | #include <iostream>
 | 
|---|
| 16 | #include <stdio.h>
 | 
|---|
| 17 | #include <cstring>
 | 
|---|
| 18 | 
 | 
|---|
| 19 | #include "Helpers/Assert.hpp"
 | 
|---|
| 20 | 
 | 
|---|
| 21 | #include "analysis_bonds.hpp"
 | 
|---|
| 22 | #include "atom.hpp"
 | 
|---|
| 23 | #include "bond.hpp"
 | 
|---|
| 24 | #include "bondgraph.hpp"
 | 
|---|
| 25 | #include "element.hpp"
 | 
|---|
| 26 | #include "molecule.hpp"
 | 
|---|
| 27 | #include "periodentafel.hpp"
 | 
|---|
| 28 | #include "World.hpp"
 | 
|---|
| 29 | #include "CountBondsUnitTest.hpp"
 | 
|---|
| 30 | 
 | 
|---|
| 31 | #ifdef HAVE_TESTRUNNER
 | 
|---|
| 32 | #include "UnitTestMain.hpp"
 | 
|---|
| 33 | #endif /*HAVE_TESTRUNNER*/
 | 
|---|
| 34 | 
 | 
|---|
| 35 | /********************************************** Test classes **************************************/
 | 
|---|
| 36 | 
 | 
|---|
| 37 | // Registers the fixture into the 'registry'
 | 
|---|
| 38 | CPPUNIT_TEST_SUITE_REGISTRATION( CountBondsTest );
 | 
|---|
| 39 | 
 | 
|---|
| 40 | 
 | 
|---|
| 41 | void CountBondsTest::setUp()
 | 
|---|
| 42 | {
 | 
|---|
| 43 |   atom *Walker = NULL;
 | 
|---|
| 44 | 
 | 
|---|
| 45 |   // construct element
 | 
|---|
| 46 |   hydrogen = World::getInstance().getPeriode()->FindElement(1);
 | 
|---|
| 47 |   oxygen = World::getInstance().getPeriode()->FindElement(8);
 | 
|---|
| 48 |   CPPUNIT_ASSERT(hydrogen != NULL && "could not find element hydrogen");
 | 
|---|
| 49 |   CPPUNIT_ASSERT(oxygen != NULL && "could not find element oxygen");
 | 
|---|
| 50 | 
 | 
|---|
| 51 |   // construct molecule (water molecule)
 | 
|---|
| 52 |   TestMolecule1 = World::getInstance().createMolecule();
 | 
|---|
| 53 |   CPPUNIT_ASSERT(TestMolecule1 != NULL && "could not create first molecule");
 | 
|---|
| 54 |   Walker = World::getInstance().createAtom();
 | 
|---|
| 55 |   CPPUNIT_ASSERT(Walker != NULL && "could not create atom");
 | 
|---|
| 56 |   Walker->type = hydrogen;
 | 
|---|
| 57 |   *Walker->node = Vector(-0.2418, 0.9350, 0. );
 | 
|---|
| 58 |   TestMolecule1->AddAtom(Walker);
 | 
|---|
| 59 |   Walker = World::getInstance().createAtom();
 | 
|---|
| 60 |   CPPUNIT_ASSERT(Walker != NULL && "could not create atom");
 | 
|---|
| 61 |   Walker->type = hydrogen;
 | 
|---|
| 62 |   *Walker->node = Vector(0.9658, 0., 0. );
 | 
|---|
| 63 |   TestMolecule1->AddAtom(Walker);
 | 
|---|
| 64 |   Walker = World::getInstance().createAtom();
 | 
|---|
| 65 |   CPPUNIT_ASSERT(Walker != NULL && "could not create atom");
 | 
|---|
| 66 |   Walker->type = oxygen;
 | 
|---|
| 67 |   *Walker->node = Vector(0., 0., 0. );
 | 
|---|
| 68 |   TestMolecule1->AddAtom(Walker);
 | 
|---|
| 69 | 
 | 
|---|
| 70 |   TestMolecule2 = World::getInstance().createMolecule();
 | 
|---|
| 71 |   CPPUNIT_ASSERT(TestMolecule2 != NULL && "could not create second molecule");
 | 
|---|
| 72 |   Walker = World::getInstance().createAtom();
 | 
|---|
| 73 |   CPPUNIT_ASSERT(Walker != NULL && "could not create atom");
 | 
|---|
| 74 |   Walker->type = hydrogen;
 | 
|---|
| 75 |   *Walker->node = Vector(-0.2418, 0.9350, 0. );
 | 
|---|
| 76 |   TestMolecule2->AddAtom(Walker);
 | 
|---|
| 77 |   Walker = World::getInstance().createAtom();
 | 
|---|
| 78 |   CPPUNIT_ASSERT(Walker != NULL && "could not create atom");
 | 
|---|
| 79 |   Walker->type = hydrogen;
 | 
|---|
| 80 |   *Walker->node = Vector(0.9658, 0., 0. );
 | 
|---|
| 81 |   TestMolecule2->AddAtom(Walker);
 | 
|---|
| 82 |   Walker = World::getInstance().createAtom();
 | 
|---|
| 83 |   CPPUNIT_ASSERT(Walker != NULL && "could not create atom");
 | 
|---|
| 84 |   Walker->type = oxygen;
 | 
|---|
| 85 |   *Walker->node = Vector(0., 0., 0. );
 | 
|---|
| 86 |   TestMolecule2->AddAtom(Walker);
 | 
|---|
| 87 | 
 | 
|---|
| 88 |   molecules = World::getInstance().getMolecules();
 | 
|---|
| 89 |   CPPUNIT_ASSERT(molecules != NULL && "could not obtain list of molecules");
 | 
|---|
| 90 |   molecules->insert(TestMolecule1);
 | 
|---|
| 91 |   molecules->insert(TestMolecule2);
 | 
|---|
| 92 | 
 | 
|---|
| 93 |   // check that TestMolecule was correctly constructed
 | 
|---|
| 94 |   CPPUNIT_ASSERT_EQUAL( TestMolecule1->getAtomCount(), 3 );
 | 
|---|
| 95 |   CPPUNIT_ASSERT_EQUAL( TestMolecule2->getAtomCount(), 3 );
 | 
|---|
| 96 | 
 | 
|---|
| 97 |   // create a small file with table
 | 
|---|
| 98 |   BG = new BondGraph(true);
 | 
|---|
| 99 |   CPPUNIT_ASSERT(BG != NULL && "could not create BondGraph");
 | 
|---|
| 100 | 
 | 
|---|
| 101 |   // construct bond graphs
 | 
|---|
| 102 |   CPPUNIT_ASSERT_EQUAL( true , BG->ConstructBondGraph(TestMolecule1) );
 | 
|---|
| 103 |   CPPUNIT_ASSERT_EQUAL( true , BG->ConstructBondGraph(TestMolecule2) );
 | 
|---|
| 104 | //  TestMolecule1->Output((ofstream *)&cout);
 | 
|---|
| 105 | //  TestMolecule1->OutputBondsList();
 | 
|---|
| 106 | };
 | 
|---|
| 107 | 
 | 
|---|
| 108 | 
 | 
|---|
| 109 | void CountBondsTest::tearDown()
 | 
|---|
| 110 | {
 | 
|---|
| 111 |   // remove the file
 | 
|---|
| 112 |   delete(BG);
 | 
|---|
| 113 | 
 | 
|---|
| 114 |   World::purgeInstance();
 | 
|---|
| 115 | };
 | 
|---|
| 116 | 
 | 
|---|
| 117 | /** UnitTest for CountBondsTest::BondsOfTwoTest().
 | 
|---|
| 118 |  */
 | 
|---|
| 119 | void CountBondsTest::BondsOfTwoTest()
 | 
|---|
| 120 | {
 | 
|---|
| 121 |   CPPUNIT_ASSERT_EQUAL( 4 , CountBondsOfTwo(molecules, hydrogen, oxygen) );
 | 
|---|
| 122 |   CPPUNIT_ASSERT_EQUAL( 0 , CountBondsOfTwo(molecules, hydrogen, hydrogen) );
 | 
|---|
| 123 |   CPPUNIT_ASSERT_EQUAL( 0 , CountBondsOfTwo(molecules, oxygen, oxygen) );
 | 
|---|
| 124 | };
 | 
|---|
| 125 | 
 | 
|---|
| 126 | /** UnitTest for CountBondsTest::BondsOfThreeTest().
 | 
|---|
| 127 |  */
 | 
|---|
| 128 | void CountBondsTest::BondsOfThreeTest()
 | 
|---|
| 129 | {
 | 
|---|
| 130 |   CPPUNIT_ASSERT_EQUAL( 2 , CountBondsOfThree(molecules, hydrogen, oxygen, hydrogen) );
 | 
|---|
| 131 |   CPPUNIT_ASSERT_EQUAL( 0 , CountBondsOfThree(molecules, oxygen, hydrogen, oxygen) );
 | 
|---|
| 132 | };
 | 
|---|
| 133 | 
 | 
|---|
| 134 | void OutputTestMolecule(molecule *mol, const char *name)
 | 
|---|
| 135 | {
 | 
|---|
| 136 |   ofstream output(name);
 | 
|---|
| 137 |   mol->OutputXYZ(&output);
 | 
|---|
| 138 |   output.close();
 | 
|---|
| 139 | }
 | 
|---|
| 140 | 
 | 
|---|
| 141 | /** UnitTest for CountBondsTest::HydrogenBridgeBondsTest().
 | 
|---|
| 142 |  */
 | 
|---|
| 143 | void CountBondsTest::HydrogenBridgeBondsTest()
 | 
|---|
| 144 | {
 | 
|---|
| 145 |   double *mirror = new double[3];
 | 
|---|
| 146 |   CPPUNIT_ASSERT(mirror != NULL && "could not create array of doubles");
 | 
|---|
| 147 |   for (int i=0;i<3;i++)
 | 
|---|
| 148 |     mirror[i] = -1.;
 | 
|---|
| 149 |   Vector Translator;
 | 
|---|
| 150 | 
 | 
|---|
| 151 |   //OutputTestMolecule(TestMolecule1, "testmolecule1.xyz");
 | 
|---|
| 152 | 
 | 
|---|
| 153 |   cout << "Case 1: offset of (3,0,0), hence angles are (104.5, 0, 75.5, 180) < 30." << endl;
 | 
|---|
| 154 |   Translator  = Vector(3,0,0);
 | 
|---|
| 155 |   TestMolecule2->Translate(&Translator);
 | 
|---|
| 156 |   CPPUNIT_ASSERT_EQUAL( 1 , CountHydrogenBridgeBonds(molecules, NULL) );
 | 
|---|
| 157 |   CPPUNIT_ASSERT_EQUAL( 0 , CountHydrogenBridgeBonds(molecules, oxygen) );
 | 
|---|
| 158 |   //OutputTestMolecule(TestMolecule2, "testmolecule2-1.xyz");
 | 
|---|
| 159 |   Translator = Vector(-3,0,0);
 | 
|---|
| 160 |   TestMolecule2->Translate(&Translator);
 | 
|---|
| 161 | 
 | 
|---|
| 162 |   cout << "Case 2: offset of (0,3,0), hence angle are (14.5, 165.5, 90) < 30 (only three, because other 90 is missing due to first H01 only fulfilling H-bond criteria)." << endl;
 | 
|---|
| 163 |   Translator = Vector(0,3,0);
 | 
|---|
| 164 |   TestMolecule2->Translate(&Translator);
 | 
|---|
| 165 |   CPPUNIT_ASSERT_EQUAL( 1 , CountHydrogenBridgeBonds(molecules, NULL) );
 | 
|---|
| 166 |   //OutputTestMolecule(TestMolecule2, "testmolecule2-2.xyz");
 | 
|---|
| 167 |   Translator = Vector(0,-3,0);
 | 
|---|
| 168 |   TestMolecule2->Translate(&Translator);
 | 
|---|
| 169 | 
 | 
|---|
| 170 |   cout << "Case 3: offset of (0,-3,0) and mirror, hence angle are (165.5, 90, 165.5, 90) > 30." << endl;
 | 
|---|
| 171 |   Translator = Vector(0,-3,0);
 | 
|---|
| 172 |   TestMolecule2->Scale((const double ** const)&mirror);
 | 
|---|
| 173 |   TestMolecule2->Translate(&Translator);
 | 
|---|
| 174 |   CPPUNIT_ASSERT_EQUAL( 0 , CountHydrogenBridgeBonds(molecules, NULL) );
 | 
|---|
| 175 |   //OutputTestMolecule(TestMolecule2, "testmolecule2-3.xyz");
 | 
|---|
| 176 |   Translator = Vector(0,3,0);
 | 
|---|
| 177 |   TestMolecule2->Translate(&Translator);
 | 
|---|
| 178 |   TestMolecule2->Scale((const double ** const)&mirror);
 | 
|---|
| 179 | 
 | 
|---|
| 180 |   cout << "Case 4: offset of (2,1,0), hence angle are (78, 26.6, 102, 153.4) < 30." << endl;
 | 
|---|
| 181 |   Translator = Vector(2,1,0);
 | 
|---|
| 182 |   TestMolecule2->Translate(&Translator);
 | 
|---|
| 183 |   CPPUNIT_ASSERT_EQUAL( 1 , CountHydrogenBridgeBonds(molecules, NULL) );
 | 
|---|
| 184 |   //OutputTestMolecule(TestMolecule2, "testmolecule2-4.xyz");
 | 
|---|
| 185 |   Translator = Vector(-2,-1,0);
 | 
|---|
| 186 |   TestMolecule2->Translate(&Translator);
 | 
|---|
| 187 | 
 | 
|---|
| 188 |   cout << "Case 5: offset of (0,0,3), hence angle are (90, 90, 90, 90) > 30." << endl;
 | 
|---|
| 189 |   Translator = Vector(0,0,3);
 | 
|---|
| 190 |   TestMolecule2->Translate(&Translator);
 | 
|---|
| 191 |   CPPUNIT_ASSERT_EQUAL( 0 , CountHydrogenBridgeBonds(molecules, NULL) );
 | 
|---|
| 192 |   //OutputTestMolecule(TestMolecule2, "testmolecule2-5.xyz");
 | 
|---|
| 193 |   Translator = Vector(0,0,-3);
 | 
|---|
| 194 |   TestMolecule2->Translate(&Translator);
 | 
|---|
| 195 | 
 | 
|---|
| 196 |   cout << "Case 6: offset of (-3,0,0) and mirror, hence angle are (75.5, 180, 104.5, 180) > 30." << endl;
 | 
|---|
| 197 |   Translator = Vector(-3,0,0);
 | 
|---|
| 198 |   TestMolecule2->Scale((const double ** const)&mirror);
 | 
|---|
| 199 |   TestMolecule2->Translate(&Translator);
 | 
|---|
| 200 |   CPPUNIT_ASSERT_EQUAL( 0 , CountHydrogenBridgeBonds(molecules, NULL) );
 | 
|---|
| 201 |   //OutputTestMolecule(TestMolecule2, "testmolecule2-6.xyz");
 | 
|---|
| 202 |   Translator = Vector(3,0,0);
 | 
|---|
| 203 |   TestMolecule2->Translate(&Translator);
 | 
|---|
| 204 |   TestMolecule2->Scale((const double ** const)&mirror);
 | 
|---|
| 205 | 
 | 
|---|
| 206 |   cout << "Case 7: offset of (3,0,0) and mirror, hence angles are (104.5, 0, 104.5, 0) < 30, but interfering hydrogens." << endl;
 | 
|---|
| 207 |   Translator = Vector(3,0,0);
 | 
|---|
| 208 |   TestMolecule2->Scale((const double ** const)&mirror);
 | 
|---|
| 209 |   TestMolecule2->Translate(&Translator);
 | 
|---|
| 210 |   CPPUNIT_ASSERT_EQUAL( 0 , CountHydrogenBridgeBonds(molecules, NULL) );
 | 
|---|
| 211 |   //OutputTestMolecule(TestMolecule2, "testmolecule2-7.xyz");
 | 
|---|
| 212 |   Translator = Vector(-3,0,0);
 | 
|---|
| 213 |   TestMolecule2->Translate(&Translator);
 | 
|---|
| 214 |   TestMolecule2->Scale((const double ** const)&mirror);
 | 
|---|
| 215 | 
 | 
|---|
| 216 |   cout << "Case 8: offset of (0,3,0), hence angle are (14.5, 90, 14.5, 90) < 30, but interfering hydrogens." << endl;
 | 
|---|
| 217 |   Translator = Vector(0,3,0);
 | 
|---|
| 218 |   TestMolecule2->Scale((const double ** const)&mirror);
 | 
|---|
| 219 |   TestMolecule2->Translate(&Translator);
 | 
|---|
| 220 |   //OutputTestMolecule(TestMolecule2, "testmolecule2-8.xyz");
 | 
|---|
| 221 |   CPPUNIT_ASSERT_EQUAL( 0 , CountHydrogenBridgeBonds(molecules, NULL) );
 | 
|---|
| 222 |   Translator = Vector(0,-3,0);
 | 
|---|
| 223 |   TestMolecule2->Translate(&Translator);
 | 
|---|
| 224 |   TestMolecule2->Scale((const double ** const)&mirror);
 | 
|---|
| 225 | 
 | 
|---|
| 226 |   delete[](mirror);
 | 
|---|
| 227 | };
 | 
|---|