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