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