| 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 "CodePatterns/Assert.hpp"
|
|---|
| 31 |
|
|---|
| 32 | #include "Analysis/analysis_bonds.hpp"
|
|---|
| 33 | #include "atom.hpp"
|
|---|
| 34 | #include "Bond/bond.hpp"
|
|---|
| 35 | #include "Element/element.hpp"
|
|---|
| 36 | #include "Element/periodentafel.hpp"
|
|---|
| 37 | #include "Graph/BondGraph.hpp"
|
|---|
| 38 | #include "molecule.hpp"
|
|---|
| 39 | #include "MoleculeListClass.hpp"
|
|---|
| 40 | #include "World.hpp"
|
|---|
| 41 |
|
|---|
| 42 | #include "CountBondsUnitTest.hpp"
|
|---|
| 43 |
|
|---|
| 44 | #ifdef HAVE_TESTRUNNER
|
|---|
| 45 | #include "UnitTestMain.hpp"
|
|---|
| 46 | #endif /*HAVE_TESTRUNNER*/
|
|---|
| 47 |
|
|---|
| 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
|
|---|
| 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");
|
|---|
| 63 |
|
|---|
| 64 | // construct molecule (water molecule)
|
|---|
| 65 | TestMolecule1 = World::getInstance().createMolecule();
|
|---|
| 66 | CPPUNIT_ASSERT(TestMolecule1 != NULL && "could not create first molecule");
|
|---|
| 67 | Walker = World::getInstance().createAtom();
|
|---|
| 68 | CPPUNIT_ASSERT(Walker != NULL && "could not create atom");
|
|---|
| 69 | Walker->setType(hydrogen);
|
|---|
| 70 | Walker->setPosition(Vector(-0.2418, 0.9350, 0. ));
|
|---|
| 71 | TestMolecule1->AddAtom(Walker);
|
|---|
| 72 | Walker = World::getInstance().createAtom();
|
|---|
| 73 | CPPUNIT_ASSERT(Walker != NULL && "could not create atom");
|
|---|
| 74 | Walker->setType(hydrogen);
|
|---|
| 75 | Walker->setPosition(Vector(0.9658, 0., 0. ));
|
|---|
| 76 | TestMolecule1->AddAtom(Walker);
|
|---|
| 77 | Walker = World::getInstance().createAtom();
|
|---|
| 78 | CPPUNIT_ASSERT(Walker != NULL && "could not create atom");
|
|---|
| 79 | Walker->setType(oxygen);
|
|---|
| 80 | Walker->setPosition(Vector(0., 0., 0. ));
|
|---|
| 81 | TestMolecule1->AddAtom(Walker);
|
|---|
| 82 |
|
|---|
| 83 | TestMolecule2 = World::getInstance().createMolecule();
|
|---|
| 84 | CPPUNIT_ASSERT(TestMolecule2 != NULL && "could not create second molecule");
|
|---|
| 85 | Walker = World::getInstance().createAtom();
|
|---|
| 86 | CPPUNIT_ASSERT(Walker != NULL && "could not create atom");
|
|---|
| 87 | Walker->setType(hydrogen);
|
|---|
| 88 | Walker->setPosition(Vector(-0.2418, 0.9350, 0. ));
|
|---|
| 89 | TestMolecule2->AddAtom(Walker);
|
|---|
| 90 | Walker = World::getInstance().createAtom();
|
|---|
| 91 | CPPUNIT_ASSERT(Walker != NULL && "could not create atom");
|
|---|
| 92 | Walker->setType(hydrogen);
|
|---|
| 93 | Walker->setPosition(Vector(0.9658, 0., 0. ));
|
|---|
| 94 | TestMolecule2->AddAtom(Walker);
|
|---|
| 95 | Walker = World::getInstance().createAtom();
|
|---|
| 96 | CPPUNIT_ASSERT(Walker != NULL && "could not create atom");
|
|---|
| 97 | Walker->setType(oxygen);
|
|---|
| 98 | Walker->setPosition(Vector(0., 0., 0. ));
|
|---|
| 99 | TestMolecule2->AddAtom(Walker);
|
|---|
| 100 |
|
|---|
| 101 | molecules = World::getInstance().getMolecules();
|
|---|
| 102 | CPPUNIT_ASSERT(molecules != NULL && "could not obtain list of molecules");
|
|---|
| 103 | molecules->insert(TestMolecule1);
|
|---|
| 104 | molecules->insert(TestMolecule2);
|
|---|
| 105 |
|
|---|
| 106 | // check that TestMolecule was correctly constructed
|
|---|
| 107 | CPPUNIT_ASSERT_EQUAL( TestMolecule1->getAtomCount(), 3 );
|
|---|
| 108 | CPPUNIT_ASSERT_EQUAL( TestMolecule2->getAtomCount(), 3 );
|
|---|
| 109 |
|
|---|
| 110 | // create a small file with table
|
|---|
| 111 | BG = new BondGraph(true);
|
|---|
| 112 | CPPUNIT_ASSERT(BG != NULL && "could not create BondGraph");
|
|---|
| 113 |
|
|---|
| 114 | // construct bond graphs
|
|---|
| 115 | World::AtomComposite Set1 = TestMolecule1->getAtomSet();
|
|---|
| 116 | BG->CreateAdjacency(Set1);
|
|---|
| 117 | CPPUNIT_ASSERT( TestMolecule1->getBondCount() != 0);
|
|---|
| 118 | World::AtomComposite Set2 = TestMolecule2->getAtomSet();
|
|---|
| 119 | BG->CreateAdjacency(Set2);
|
|---|
| 120 | CPPUNIT_ASSERT( TestMolecule2->getBondCount() != 0);
|
|---|
| 121 | // TestMolecule1->Output((ofstream *)&cout);
|
|---|
| 122 | // TestMolecule1->OutputBondsList(std::cout);
|
|---|
| 123 | };
|
|---|
| 124 |
|
|---|
| 125 |
|
|---|
| 126 | void CountBondsTest::tearDown()
|
|---|
| 127 | {
|
|---|
| 128 | // remove the file
|
|---|
| 129 | delete(BG);
|
|---|
| 130 |
|
|---|
| 131 | World::purgeInstance();
|
|---|
| 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 |
|
|---|
| 151 | void OutputTestMolecule(molecule *mol, const char *name)
|
|---|
| 152 | {
|
|---|
| 153 | ofstream output(name);
|
|---|
| 154 | mol->OutputXYZ(&output);
|
|---|
| 155 | output.close();
|
|---|
| 156 | }
|
|---|
| 157 |
|
|---|
| 158 | /** UnitTest for CountBondsTest::HydrogenBridgeBondsTest().
|
|---|
| 159 | */
|
|---|
| 160 | void CountBondsTest::HydrogenBridgeBondsTest()
|
|---|
| 161 | {
|
|---|
| 162 | double *mirror = new double[3];
|
|---|
| 163 | CPPUNIT_ASSERT(mirror != NULL && "could not create array of doubles");
|
|---|
| 164 | for (int i=0;i<3;i++)
|
|---|
| 165 | mirror[i] = -1.;
|
|---|
| 166 | Vector Translator;
|
|---|
| 167 |
|
|---|
| 168 | //OutputTestMolecule(TestMolecule1, "testmolecule1.xyz");
|
|---|
| 169 |
|
|---|
| 170 | cout << "Case 1: offset of (3,0,0), hence angles are (104.5, 0, 75.5, 180) < 30." << endl;
|
|---|
| 171 | Translator = Vector(3,0,0);
|
|---|
| 172 | TestMolecule2->Translate(&Translator);
|
|---|
| 173 | CPPUNIT_ASSERT_EQUAL( 1 , CountHydrogenBridgeBonds(molecules, NULL, NULL) );
|
|---|
| 174 | CPPUNIT_ASSERT_EQUAL( 0 , CountHydrogenBridgeBonds(molecules, oxygen, NULL) );
|
|---|
| 175 | //OutputTestMolecule(TestMolecule2, "testmolecule2-1.xyz");
|
|---|
| 176 | Translator = Vector(-3,0,0);
|
|---|
| 177 | TestMolecule2->Translate(&Translator);
|
|---|
| 178 |
|
|---|
| 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;
|
|---|
| 180 | Translator = Vector(0,3,0);
|
|---|
| 181 | TestMolecule2->Translate(&Translator);
|
|---|
| 182 | CPPUNIT_ASSERT_EQUAL( 1 , CountHydrogenBridgeBonds(molecules, NULL, NULL) );
|
|---|
| 183 | //OutputTestMolecule(TestMolecule2, "testmolecule2-2.xyz");
|
|---|
| 184 | Translator = Vector(0,-3,0);
|
|---|
| 185 | TestMolecule2->Translate(&Translator);
|
|---|
| 186 |
|
|---|
| 187 | cout << "Case 3: offset of (0,-3,0) and mirror, hence angle are (165.5, 90, 165.5, 90) > 30." << endl;
|
|---|
| 188 | Translator = Vector(0,-3,0);
|
|---|
| 189 | TestMolecule2->Scale((const double ** const)&mirror);
|
|---|
| 190 | TestMolecule2->Translate(&Translator);
|
|---|
| 191 | CPPUNIT_ASSERT_EQUAL( 0 , CountHydrogenBridgeBonds(molecules, NULL, NULL) );
|
|---|
| 192 | //OutputTestMolecule(TestMolecule2, "testmolecule2-3.xyz");
|
|---|
| 193 | Translator = Vector(0,3,0);
|
|---|
| 194 | TestMolecule2->Translate(&Translator);
|
|---|
| 195 | TestMolecule2->Scale((const double ** const)&mirror);
|
|---|
| 196 |
|
|---|
| 197 | cout << "Case 4: offset of (2,1,0), hence angle are (78, 26.6, 102, 153.4) < 30." << endl;
|
|---|
| 198 | Translator = Vector(2,1,0);
|
|---|
| 199 | TestMolecule2->Translate(&Translator);
|
|---|
| 200 | CPPUNIT_ASSERT_EQUAL( 1 , CountHydrogenBridgeBonds(molecules, NULL, NULL) );
|
|---|
| 201 | //OutputTestMolecule(TestMolecule2, "testmolecule2-4.xyz");
|
|---|
| 202 | Translator = Vector(-2,-1,0);
|
|---|
| 203 | TestMolecule2->Translate(&Translator);
|
|---|
| 204 |
|
|---|
| 205 | cout << "Case 5: offset of (0,0,3), hence angle are (90, 90, 90, 90) > 30." << endl;
|
|---|
| 206 | Translator = Vector(0,0,3);
|
|---|
| 207 | TestMolecule2->Translate(&Translator);
|
|---|
| 208 | CPPUNIT_ASSERT_EQUAL( 0 , CountHydrogenBridgeBonds(molecules, NULL, NULL) );
|
|---|
| 209 | //OutputTestMolecule(TestMolecule2, "testmolecule2-5.xyz");
|
|---|
| 210 | Translator = Vector(0,0,-3);
|
|---|
| 211 | TestMolecule2->Translate(&Translator);
|
|---|
| 212 |
|
|---|
| 213 | cout << "Case 6: offset of (-3,0,0) and mirror, hence angle are (75.5, 180, 104.5, 180) > 30." << endl;
|
|---|
| 214 | Translator = Vector(-3,0,0);
|
|---|
| 215 | TestMolecule2->Scale((const double ** const)&mirror);
|
|---|
| 216 | TestMolecule2->Translate(&Translator);
|
|---|
| 217 | CPPUNIT_ASSERT_EQUAL( 0 , CountHydrogenBridgeBonds(molecules, NULL, NULL) );
|
|---|
| 218 | //OutputTestMolecule(TestMolecule2, "testmolecule2-6.xyz");
|
|---|
| 219 | Translator = Vector(3,0,0);
|
|---|
| 220 | TestMolecule2->Translate(&Translator);
|
|---|
| 221 | TestMolecule2->Scale((const double ** const)&mirror);
|
|---|
| 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;
|
|---|
| 224 | Translator = Vector(3,0,0);
|
|---|
| 225 | TestMolecule2->Scale((const double ** const)&mirror);
|
|---|
| 226 | TestMolecule2->Translate(&Translator);
|
|---|
| 227 | CPPUNIT_ASSERT_EQUAL( 0 , CountHydrogenBridgeBonds(molecules, NULL, NULL) );
|
|---|
| 228 | //OutputTestMolecule(TestMolecule2, "testmolecule2-7.xyz");
|
|---|
| 229 | Translator = Vector(-3,0,0);
|
|---|
| 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;
|
|---|
| 234 | Translator = Vector(0,3,0);
|
|---|
| 235 | TestMolecule2->Scale((const double ** const)&mirror);
|
|---|
| 236 | TestMolecule2->Translate(&Translator);
|
|---|
| 237 | //OutputTestMolecule(TestMolecule2, "testmolecule2-8.xyz");
|
|---|
| 238 | CPPUNIT_ASSERT_EQUAL( 0 , CountHydrogenBridgeBonds(molecules, NULL, NULL) );
|
|---|
| 239 | Translator = Vector(0,-3,0);
|
|---|
| 240 | TestMolecule2->Translate(&Translator);
|
|---|
| 241 | TestMolecule2->Scale((const double ** const)&mirror);
|
|---|
| 242 |
|
|---|
| 243 | delete[](mirror);
|
|---|
| 244 | };
|
|---|