| 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 "analysis_bonds.hpp"
 | 
|---|
| 20 | #include "atom.hpp"
 | 
|---|
| 21 | #include "bond.hpp"
 | 
|---|
| 22 | #include "bondgraph.hpp"
 | 
|---|
| 23 | #include "element.hpp"
 | 
|---|
| 24 | #include "molecule.hpp"
 | 
|---|
| 25 | #include "periodentafel.hpp"
 | 
|---|
| 26 | #include "World.hpp"
 | 
|---|
| 27 | #include "CountBondsUnitTest.hpp"
 | 
|---|
| 28 | 
 | 
|---|
| 29 | #ifdef HAVE_TESTRUNNER
 | 
|---|
| 30 | #include "UnitTestMain.hpp"
 | 
|---|
| 31 | #endif /*HAVE_TESTRUNNER*/
 | 
|---|
| 32 | 
 | 
|---|
| 33 | /********************************************** Test classes **************************************/
 | 
|---|
| 34 | 
 | 
|---|
| 35 | // Registers the fixture into the 'registry'
 | 
|---|
| 36 | CPPUNIT_TEST_SUITE_REGISTRATION( CountBondsTest );
 | 
|---|
| 37 | 
 | 
|---|
| 38 | 
 | 
|---|
| 39 | void CountBondsTest::setUp()
 | 
|---|
| 40 | {
 | 
|---|
| 41 |   atom *Walker = NULL;
 | 
|---|
| 42 |   BG = NULL;
 | 
|---|
| 43 |   filename = NULL;
 | 
|---|
| 44 | 
 | 
|---|
| 45 |   // init private all pointers to zero
 | 
|---|
| 46 |   molecules = NULL;
 | 
|---|
| 47 |   TestMolecule1 = NULL;
 | 
|---|
| 48 |   TestMolecule2 = NULL;
 | 
|---|
| 49 |   hydrogen = NULL;
 | 
|---|
| 50 |   oxygen = NULL;
 | 
|---|
| 51 |   tafel = NULL;
 | 
|---|
| 52 | 
 | 
|---|
| 53 |   // construct element
 | 
|---|
| 54 |   hydrogen = new element;
 | 
|---|
| 55 |   hydrogen->Z = 1;
 | 
|---|
| 56 |   hydrogen->CovalentRadius = 0.23;
 | 
|---|
| 57 |   strcpy(hydrogen->name, "hydrogen");
 | 
|---|
| 58 |   strcpy(hydrogen->symbol, "H");
 | 
|---|
| 59 |   oxygen = new element;
 | 
|---|
| 60 |   oxygen->Z = 8;
 | 
|---|
| 61 |   oxygen->CovalentRadius = 0.68;
 | 
|---|
| 62 |   strcpy(oxygen->name, "oxygen");
 | 
|---|
| 63 |   strcpy(oxygen->symbol, "O");
 | 
|---|
| 64 | 
 | 
|---|
| 65 |   // construct periodentafel
 | 
|---|
| 66 |   tafel = World::getInstance().getPeriode();
 | 
|---|
| 67 |   tafel->AddElement(hydrogen);
 | 
|---|
| 68 |   tafel->AddElement(oxygen);
 | 
|---|
| 69 | 
 | 
|---|
| 70 |   // construct molecule (water molecule)
 | 
|---|
| 71 |   TestMolecule1 = World::getInstance().createMolecule();
 | 
|---|
| 72 |   Walker = World::getInstance().createAtom();
 | 
|---|
| 73 |   Walker->type = hydrogen;
 | 
|---|
| 74 |   *Walker->node = Vector(-0.2418, 0.9350, 0. );
 | 
|---|
| 75 |   TestMolecule1->AddAtom(Walker);
 | 
|---|
| 76 |   Walker = World::getInstance().createAtom();
 | 
|---|
| 77 |   Walker->type = hydrogen;
 | 
|---|
| 78 |   *Walker->node = Vector(0.9658, 0., 0. );
 | 
|---|
| 79 |   TestMolecule1->AddAtom(Walker);
 | 
|---|
| 80 |   Walker = World::getInstance().createAtom();
 | 
|---|
| 81 |   Walker->type = oxygen;
 | 
|---|
| 82 |   *Walker->node = Vector(0., 0., 0. );
 | 
|---|
| 83 |   TestMolecule1->AddAtom(Walker);
 | 
|---|
| 84 | 
 | 
|---|
| 85 |   TestMolecule2 = World::getInstance().createMolecule();
 | 
|---|
| 86 |   Walker = World::getInstance().createAtom();
 | 
|---|
| 87 |   Walker->type = hydrogen;
 | 
|---|
| 88 |   *Walker->node = Vector(-0.2418, 0.9350, 0. );
 | 
|---|
| 89 |   TestMolecule2->AddAtom(Walker);
 | 
|---|
| 90 |   Walker = World::getInstance().createAtom();
 | 
|---|
| 91 |   Walker->type = hydrogen;
 | 
|---|
| 92 |   *Walker->node = Vector(0.9658, 0., 0. );
 | 
|---|
| 93 |   TestMolecule2->AddAtom(Walker);
 | 
|---|
| 94 |   Walker = World::getInstance().createAtom();
 | 
|---|
| 95 |   Walker->type = oxygen;
 | 
|---|
| 96 |   *Walker->node = Vector(0., 0., 0. );
 | 
|---|
| 97 |   TestMolecule2->AddAtom(Walker);
 | 
|---|
| 98 | 
 | 
|---|
| 99 |   molecules = World::getInstance().getMolecules();
 | 
|---|
| 100 |   molecules->insert(TestMolecule1);
 | 
|---|
| 101 |   molecules->insert(TestMolecule2);
 | 
|---|
| 102 | 
 | 
|---|
| 103 |   // check that TestMolecule was correctly constructed
 | 
|---|
| 104 |   CPPUNIT_ASSERT_EQUAL( TestMolecule1->AtomCount, 3 );
 | 
|---|
| 105 |   Walker = TestMolecule1->start->next;
 | 
|---|
| 106 |   CPPUNIT_ASSERT( TestMolecule1->end != Walker );
 | 
|---|
| 107 |   CPPUNIT_ASSERT_EQUAL( TestMolecule2->AtomCount, 3 );
 | 
|---|
| 108 |   Walker = TestMolecule2->start->next;
 | 
|---|
| 109 |   CPPUNIT_ASSERT( TestMolecule2->end != Walker );
 | 
|---|
| 110 | 
 | 
|---|
| 111 |   // create a small file with table
 | 
|---|
| 112 |   BG = new BondGraph(true);
 | 
|---|
| 113 | 
 | 
|---|
| 114 |   // construct bond graphs
 | 
|---|
| 115 |   CPPUNIT_ASSERT_EQUAL( true , BG->ConstructBondGraph(TestMolecule1) );
 | 
|---|
| 116 |   CPPUNIT_ASSERT_EQUAL( true , BG->ConstructBondGraph(TestMolecule2) );
 | 
|---|
| 117 | //  TestMolecule1->Output((ofstream *)&cout);
 | 
|---|
| 118 | //  TestMolecule1->OutputBondsList();
 | 
|---|
| 119 | };
 | 
|---|
| 120 | 
 | 
|---|
| 121 | 
 | 
|---|
| 122 | void CountBondsTest::tearDown()
 | 
|---|
| 123 | {
 | 
|---|
| 124 |   // remove the file
 | 
|---|
| 125 |   delete(BG);
 | 
|---|
| 126 | 
 | 
|---|
| 127 |   World::purgeInstance();
 | 
|---|
| 128 |   MemoryUsageObserver::purgeInstance();
 | 
|---|
| 129 | };
 | 
|---|
| 130 | 
 | 
|---|
| 131 | /** UnitTest for CountBondsTest::BondsOfTwoTest().
 | 
|---|
| 132 |  */
 | 
|---|
| 133 | void CountBondsTest::BondsOfTwoTest()
 | 
|---|
| 134 | {
 | 
|---|
| 135 |   CPPUNIT_ASSERT_EQUAL( 4 , CountBondsOfTwo(molecules, hydrogen, oxygen) );
 | 
|---|
| 136 |   CPPUNIT_ASSERT_EQUAL( 0 , CountBondsOfTwo(molecules, hydrogen, hydrogen) );
 | 
|---|
| 137 |   CPPUNIT_ASSERT_EQUAL( 0 , CountBondsOfTwo(molecules, oxygen, oxygen) );
 | 
|---|
| 138 | };
 | 
|---|
| 139 | 
 | 
|---|
| 140 | /** UnitTest for CountBondsTest::BondsOfThreeTest().
 | 
|---|
| 141 |  */
 | 
|---|
| 142 | void CountBondsTest::BondsOfThreeTest()
 | 
|---|
| 143 | {
 | 
|---|
| 144 |   CPPUNIT_ASSERT_EQUAL( 2 , CountBondsOfThree(molecules, hydrogen, oxygen, hydrogen) );
 | 
|---|
| 145 |   CPPUNIT_ASSERT_EQUAL( 0 , CountBondsOfThree(molecules, oxygen, hydrogen, oxygen) );
 | 
|---|
| 146 | };
 | 
|---|
| 147 | 
 | 
|---|
| 148 | void OutputTestMolecule(molecule *mol, const char *name)
 | 
|---|
| 149 | {
 | 
|---|
| 150 |   ofstream output(name);
 | 
|---|
| 151 |   mol->OutputXYZ(&output);
 | 
|---|
| 152 |   output.close();
 | 
|---|
| 153 | }
 | 
|---|
| 154 | 
 | 
|---|
| 155 | /** UnitTest for CountBondsTest::HydrogenBridgeBondsTest().
 | 
|---|
| 156 |  */
 | 
|---|
| 157 | void CountBondsTest::HydrogenBridgeBondsTest()
 | 
|---|
| 158 | {
 | 
|---|
| 159 |   double *mirror = new double[3];
 | 
|---|
| 160 |   for (int i=0;i<3;i++)
 | 
|---|
| 161 |     mirror[i] = -1.;
 | 
|---|
| 162 |   Vector Translator;
 | 
|---|
| 163 | 
 | 
|---|
| 164 |   //OutputTestMolecule(TestMolecule1, "testmolecule1.xyz");
 | 
|---|
| 165 | 
 | 
|---|
| 166 |   cout << "Case 1: offset of (3,0,0), hence angles are (104.5, 0, 75.5, 180) < 30." << endl;
 | 
|---|
| 167 |   Translator  = Vector(3,0,0);
 | 
|---|
| 168 |   TestMolecule2->Translate(&Translator);
 | 
|---|
| 169 |   CPPUNIT_ASSERT_EQUAL( 1 , CountHydrogenBridgeBonds(molecules, NULL) );
 | 
|---|
| 170 |   CPPUNIT_ASSERT_EQUAL( 0 , CountHydrogenBridgeBonds(molecules, oxygen) );
 | 
|---|
| 171 |   //OutputTestMolecule(TestMolecule2, "testmolecule2-1.xyz");
 | 
|---|
| 172 |   Translator = Vector(-3,0,0);
 | 
|---|
| 173 |   TestMolecule2->Translate(&Translator);
 | 
|---|
| 174 | 
 | 
|---|
| 175 |   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;
 | 
|---|
| 176 |   Translator = Vector(0,3,0);
 | 
|---|
| 177 |   TestMolecule2->Translate(&Translator);
 | 
|---|
| 178 |   CPPUNIT_ASSERT_EQUAL( 1 , CountHydrogenBridgeBonds(molecules, NULL) );
 | 
|---|
| 179 |   //OutputTestMolecule(TestMolecule2, "testmolecule2-2.xyz");
 | 
|---|
| 180 |   Translator = Vector(0,-3,0);
 | 
|---|
| 181 |   TestMolecule2->Translate(&Translator);
 | 
|---|
| 182 | 
 | 
|---|
| 183 |   cout << "Case 3: offset of (0,-3,0) and mirror, hence angle are (165.5, 90, 165.5, 90) > 30." << endl;
 | 
|---|
| 184 |   Translator = Vector(0,-3,0);
 | 
|---|
| 185 |   TestMolecule2->Scale((const double ** const)&mirror);
 | 
|---|
| 186 |   TestMolecule2->Translate(&Translator);
 | 
|---|
| 187 |   CPPUNIT_ASSERT_EQUAL( 0 , CountHydrogenBridgeBonds(molecules, NULL) );
 | 
|---|
| 188 |   //OutputTestMolecule(TestMolecule2, "testmolecule2-3.xyz");
 | 
|---|
| 189 |   Translator = Vector(0,3,0);
 | 
|---|
| 190 |   TestMolecule2->Translate(&Translator);
 | 
|---|
| 191 |   TestMolecule2->Scale((const double ** const)&mirror);
 | 
|---|
| 192 | 
 | 
|---|
| 193 |   cout << "Case 4: offset of (2,1,0), hence angle are (78, 26.6, 102, 153.4) < 30." << endl;
 | 
|---|
| 194 |   Translator = Vector(2,1,0);
 | 
|---|
| 195 |   TestMolecule2->Translate(&Translator);
 | 
|---|
| 196 |   CPPUNIT_ASSERT_EQUAL( 1 , CountHydrogenBridgeBonds(molecules, NULL) );
 | 
|---|
| 197 |   //OutputTestMolecule(TestMolecule2, "testmolecule2-4.xyz");
 | 
|---|
| 198 |   Translator = Vector(-2,-1,0);
 | 
|---|
| 199 |   TestMolecule2->Translate(&Translator);
 | 
|---|
| 200 | 
 | 
|---|
| 201 |   cout << "Case 5: offset of (0,0,3), hence angle are (90, 90, 90, 90) > 30." << endl;
 | 
|---|
| 202 |   Translator = Vector(0,0,3);
 | 
|---|
| 203 |   TestMolecule2->Translate(&Translator);
 | 
|---|
| 204 |   CPPUNIT_ASSERT_EQUAL( 0 , CountHydrogenBridgeBonds(molecules, NULL) );
 | 
|---|
| 205 |   //OutputTestMolecule(TestMolecule2, "testmolecule2-5.xyz");
 | 
|---|
| 206 |   Translator = Vector(0,0,-3);
 | 
|---|
| 207 |   TestMolecule2->Translate(&Translator);
 | 
|---|
| 208 | 
 | 
|---|
| 209 |   cout << "Case 6: offset of (-3,0,0) and mirror, hence angle are (75.5, 180, 104.5, 180) > 30." << endl;
 | 
|---|
| 210 |   Translator = Vector(-3,0,0);
 | 
|---|
| 211 |   TestMolecule2->Scale((const double ** const)&mirror);
 | 
|---|
| 212 |   TestMolecule2->Translate(&Translator);
 | 
|---|
| 213 |   CPPUNIT_ASSERT_EQUAL( 0 , CountHydrogenBridgeBonds(molecules, NULL) );
 | 
|---|
| 214 |   //OutputTestMolecule(TestMolecule2, "testmolecule2-6.xyz");
 | 
|---|
| 215 |   Translator = Vector(3,0,0);
 | 
|---|
| 216 |   TestMolecule2->Translate(&Translator);
 | 
|---|
| 217 |   TestMolecule2->Scale((const double ** const)&mirror);
 | 
|---|
| 218 | 
 | 
|---|
| 219 |   cout << "Case 7: offset of (3,0,0) and mirror, hence angles are (104.5, 0, 104.5, 0) < 30, but interfering hydrogens." << endl;
 | 
|---|
| 220 |   Translator = Vector(3,0,0);
 | 
|---|
| 221 |   TestMolecule2->Scale((const double ** const)&mirror);
 | 
|---|
| 222 |   TestMolecule2->Translate(&Translator);
 | 
|---|
| 223 |   CPPUNIT_ASSERT_EQUAL( 0 , CountHydrogenBridgeBonds(molecules, NULL) );
 | 
|---|
| 224 |   //OutputTestMolecule(TestMolecule2, "testmolecule2-7.xyz");
 | 
|---|
| 225 |   Translator = Vector(-3,0,0);
 | 
|---|
| 226 |   TestMolecule2->Translate(&Translator);
 | 
|---|
| 227 |   TestMolecule2->Scale((const double ** const)&mirror);
 | 
|---|
| 228 | 
 | 
|---|
| 229 |   cout << "Case 8: offset of (0,3,0), hence angle are (14.5, 90, 14.5, 90) < 30, but interfering hydrogens." << endl;
 | 
|---|
| 230 |   Translator = Vector(0,3,0);
 | 
|---|
| 231 |   TestMolecule2->Scale((const double ** const)&mirror);
 | 
|---|
| 232 |   TestMolecule2->Translate(&Translator);
 | 
|---|
| 233 |   //OutputTestMolecule(TestMolecule2, "testmolecule2-8.xyz");
 | 
|---|
| 234 |   CPPUNIT_ASSERT_EQUAL( 0 , CountHydrogenBridgeBonds(molecules, NULL) );
 | 
|---|
| 235 |   Translator = Vector(0,-3,0);
 | 
|---|
| 236 |   TestMolecule2->Translate(&Translator);
 | 
|---|
| 237 |   TestMolecule2->Scale((const double ** const)&mirror);
 | 
|---|
| 238 | 
 | 
|---|
| 239 |   delete[](mirror);
 | 
|---|
| 240 | };
 | 
|---|