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