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