| [96c961] | 1 | /* | 
|---|
|  | 2 | * analysisbondsunittest.cpp | 
|---|
|  | 3 | * | 
|---|
|  | 4 | *  Created on: Nov 7, 2009 | 
|---|
|  | 5 | *      Author: heber | 
|---|
|  | 6 | */ | 
|---|
|  | 7 |  | 
|---|
|  | 8 | using namespace std; | 
|---|
|  | 9 |  | 
|---|
|  | 10 | #include <cppunit/CompilerOutputter.h> | 
|---|
|  | 11 | #include <cppunit/extensions/TestFactoryRegistry.h> | 
|---|
|  | 12 | #include <cppunit/ui/text/TestRunner.h> | 
|---|
|  | 13 |  | 
|---|
|  | 14 | #include <iostream> | 
|---|
|  | 15 | #include <stdio.h> | 
|---|
| [49e1ae] | 16 | #include <cstring> | 
|---|
| [96c961] | 17 |  | 
|---|
| [46d958] | 18 | #include "World.hpp" | 
|---|
| [220cf37] | 19 | #include "analysis_bonds.hpp" | 
|---|
|  | 20 | #include "analysisbondsunittest.hpp" | 
|---|
| [96c961] | 21 | #include "atom.hpp" | 
|---|
|  | 22 | #include "bond.hpp" | 
|---|
|  | 23 | #include "bondgraph.hpp" | 
|---|
|  | 24 | #include "element.hpp" | 
|---|
|  | 25 | #include "molecule.hpp" | 
|---|
|  | 26 | #include "periodentafel.hpp" | 
|---|
|  | 27 |  | 
|---|
| [9b6b2f] | 28 | #ifdef HAVE_TESTRUNNER | 
|---|
|  | 29 | #include "UnitTestMain.hpp" | 
|---|
|  | 30 | #endif /*HAVE_TESTRUNNER*/ | 
|---|
|  | 31 |  | 
|---|
| [96c961] | 32 | /********************************************** Test classes **************************************/ | 
|---|
|  | 33 |  | 
|---|
|  | 34 | // Registers the fixture into the 'registry' | 
|---|
|  | 35 | CPPUNIT_TEST_SUITE_REGISTRATION( AnalysisBondsTest ); | 
|---|
|  | 36 |  | 
|---|
|  | 37 |  | 
|---|
|  | 38 | void AnalysisBondsTest::setUp() | 
|---|
|  | 39 | { | 
|---|
|  | 40 | atom *Walker = NULL; | 
|---|
|  | 41 |  | 
|---|
|  | 42 | // init private all pointers to zero | 
|---|
|  | 43 | TestMolecule = NULL; | 
|---|
|  | 44 | hydrogen = NULL; | 
|---|
|  | 45 | tafel = NULL; | 
|---|
|  | 46 |  | 
|---|
|  | 47 | // construct element | 
|---|
|  | 48 | hydrogen = new element; | 
|---|
|  | 49 | hydrogen->Z = 1; | 
|---|
| [220cf37] | 50 | hydrogen->Valence = 1; | 
|---|
|  | 51 | hydrogen->NoValenceOrbitals = 1; | 
|---|
| [96c961] | 52 | strcpy(hydrogen->name, "hydrogen"); | 
|---|
|  | 53 | strcpy(hydrogen->symbol, "H"); | 
|---|
|  | 54 | carbon = new element; | 
|---|
| [ead4e6] | 55 | carbon->Z = 2; | 
|---|
| [220cf37] | 56 | carbon->Valence = 4; | 
|---|
|  | 57 | carbon->NoValenceOrbitals = 4; | 
|---|
| [96c961] | 58 | strcpy(carbon->name, "carbon"); | 
|---|
|  | 59 | strcpy(carbon->symbol, "C"); | 
|---|
|  | 60 |  | 
|---|
|  | 61 |  | 
|---|
|  | 62 | // construct periodentafel | 
|---|
| [23b547] | 63 | tafel = World::getInstance().getPeriode(); | 
|---|
| [96c961] | 64 | tafel->AddElement(hydrogen); | 
|---|
|  | 65 | tafel->AddElement(carbon); | 
|---|
|  | 66 |  | 
|---|
|  | 67 | // construct molecule (tetraeder of hydrogens) | 
|---|
| [23b547] | 68 | TestMolecule = World::getInstance().createMolecule(); | 
|---|
|  | 69 | Walker = World::getInstance().createAtom(); | 
|---|
| [96c961] | 70 | Walker->type = hydrogen; | 
|---|
| [1bd79e] | 71 | *Walker->node = Vector(1.5, 0., 1.5 ); | 
|---|
| [96c961] | 72 | TestMolecule->AddAtom(Walker); | 
|---|
| [23b547] | 73 | Walker = World::getInstance().createAtom(); | 
|---|
| [96c961] | 74 | Walker->type = hydrogen; | 
|---|
| [1bd79e] | 75 | *Walker->node = Vector(0., 1.5, 1.5 ); | 
|---|
| [96c961] | 76 | TestMolecule->AddAtom(Walker); | 
|---|
| [23b547] | 77 | Walker = World::getInstance().createAtom(); | 
|---|
| [96c961] | 78 | Walker->type = hydrogen; | 
|---|
| [1bd79e] | 79 | *Walker->node = Vector(1.5, 1.5, 0. ); | 
|---|
| [96c961] | 80 | TestMolecule->AddAtom(Walker); | 
|---|
| [23b547] | 81 | Walker = World::getInstance().createAtom(); | 
|---|
| [96c961] | 82 | Walker->type = hydrogen; | 
|---|
| [1bd79e] | 83 | *Walker->node = Vector(0., 0., 0. ); | 
|---|
| [96c961] | 84 | TestMolecule->AddAtom(Walker); | 
|---|
| [23b547] | 85 | Walker = World::getInstance().createAtom(); | 
|---|
| [220cf37] | 86 | Walker->type = carbon; | 
|---|
| [1bd79e] | 87 | *Walker->node = Vector(0.5, 0.5, 0.5 ); | 
|---|
| [220cf37] | 88 | TestMolecule->AddAtom(Walker); | 
|---|
| [96c961] | 89 |  | 
|---|
|  | 90 | // check that TestMolecule was correctly constructed | 
|---|
| [220cf37] | 91 | CPPUNIT_ASSERT_EQUAL( TestMolecule->AtomCount, 5 ); | 
|---|
| [96c961] | 92 |  | 
|---|
|  | 93 | // create a small file with table | 
|---|
|  | 94 | filename = new string("test.dat"); | 
|---|
|  | 95 | ofstream test(filename->c_str()); | 
|---|
|  | 96 | test << ".\tH\tC\n"; | 
|---|
|  | 97 | test << "H\t1.\t1.2\n"; | 
|---|
|  | 98 | test << "C\t1.2\t1.5\n"; | 
|---|
| [220cf37] | 99 | test.close(); | 
|---|
| [96c961] | 100 | BG = new BondGraph(true); | 
|---|
| [220cf37] | 101 |  | 
|---|
|  | 102 | CPPUNIT_ASSERT_EQUAL( true , BG->LoadBondLengthTable(*filename) ); | 
|---|
|  | 103 | CPPUNIT_ASSERT_EQUAL( 1., BG->GetBondLength(0,0) ); | 
|---|
|  | 104 | CPPUNIT_ASSERT_EQUAL( 1.2, BG->GetBondLength(0,1) ); | 
|---|
|  | 105 | CPPUNIT_ASSERT_EQUAL( 1.5, BG->GetBondLength(1,1) ); | 
|---|
|  | 106 |  | 
|---|
|  | 107 | BG->ConstructBondGraph(TestMolecule); | 
|---|
| [96c961] | 108 | }; | 
|---|
|  | 109 |  | 
|---|
|  | 110 |  | 
|---|
|  | 111 | void AnalysisBondsTest::tearDown() | 
|---|
|  | 112 | { | 
|---|
|  | 113 | // remove the file | 
|---|
|  | 114 | remove(filename->c_str()); | 
|---|
|  | 115 | delete(filename); | 
|---|
|  | 116 | delete(BG); | 
|---|
|  | 117 |  | 
|---|
|  | 118 | // remove molecule | 
|---|
| [23b547] | 119 | World::getInstance().destroyMolecule(TestMolecule); | 
|---|
| [96c961] | 120 | // note that all the atoms are cleaned by TestMolecule | 
|---|
| [23b547] | 121 | World::purgeInstance(); | 
|---|
| [96c961] | 122 | }; | 
|---|
|  | 123 |  | 
|---|
| [220cf37] | 124 | /** UnitTest for GetMaxMinMeanBondCount(). | 
|---|
| [96c961] | 125 | */ | 
|---|
| [220cf37] | 126 | void AnalysisBondsTest::GetMaxMinMeanBondCountTest() | 
|---|
| [96c961] | 127 | { | 
|---|
| [220cf37] | 128 | double Min = 20.; // check that initialization resets these arbitrary values | 
|---|
|  | 129 | double Mean = 200.; | 
|---|
|  | 130 | double Max = 1e-6; | 
|---|
|  | 131 | GetMaxMinMeanBondCount(TestMolecule, Min, Mean, Max); | 
|---|
|  | 132 | CPPUNIT_ASSERT_EQUAL( 1., Min ); | 
|---|
|  | 133 | CPPUNIT_ASSERT_EQUAL( 1.6, Mean ); | 
|---|
|  | 134 | CPPUNIT_ASSERT_EQUAL( 4., Max ); | 
|---|
| [96c961] | 135 |  | 
|---|
| [220cf37] | 136 | }; | 
|---|
| [96c961] | 137 |  | 
|---|
| [220cf37] | 138 | /** UnitTest for MinMaxBondDistanceBetweenElements(). | 
|---|
|  | 139 | */ | 
|---|
|  | 140 | void AnalysisBondsTest::MinMeanMaxBondDistanceBetweenElementsTest() | 
|---|
|  | 141 | { | 
|---|
|  | 142 | double Min = 20.; // check that initialization resets these arbitrary values | 
|---|
|  | 143 | double Mean = 2e+6; | 
|---|
|  | 144 | double Max = 1e-6; | 
|---|
|  | 145 | double Min2 = 20.; | 
|---|
|  | 146 | double Mean2 = 2e+6; | 
|---|
|  | 147 | double Max2 = 1e-6; | 
|---|
|  | 148 | const double maxbondlength = sqrt(1.*1. + 1.*1. + .5*.5); | 
|---|
|  | 149 | const double minbondlength = sqrt(.5*.5 + .5*.5 + .5*.5); | 
|---|
|  | 150 | const double meanbondlength = (minbondlength+3.*maxbondlength)/4.; | 
|---|
|  | 151 | // check bond lengths C-H | 
|---|
|  | 152 | MinMeanMaxBondDistanceBetweenElements(TestMolecule, hydrogen, carbon, Min, Mean, Max); | 
|---|
|  | 153 | CPPUNIT_ASSERT_EQUAL( minbondlength , Min ); | 
|---|
|  | 154 | CPPUNIT_ASSERT_EQUAL( meanbondlength , Mean ); | 
|---|
|  | 155 | CPPUNIT_ASSERT_EQUAL( maxbondlength , Max ); | 
|---|
|  | 156 |  | 
|---|
|  | 157 | // check that elements are symmetric, i.e. C-H == H-C | 
|---|
|  | 158 | MinMeanMaxBondDistanceBetweenElements(TestMolecule, carbon, hydrogen, Min2, Mean2, Max2); | 
|---|
|  | 159 | CPPUNIT_ASSERT_EQUAL( Min , Min2 ); | 
|---|
|  | 160 | CPPUNIT_ASSERT_EQUAL( Mean , Mean2 ); | 
|---|
|  | 161 | CPPUNIT_ASSERT_EQUAL( Max , Max2 ); | 
|---|
|  | 162 |  | 
|---|
|  | 163 | // check no bond case (no bonds H-H in system!) | 
|---|
|  | 164 | MinMeanMaxBondDistanceBetweenElements(TestMolecule, hydrogen, hydrogen, Min, Mean, Max); | 
|---|
|  | 165 | CPPUNIT_ASSERT_EQUAL( 0. , Min ); | 
|---|
|  | 166 | CPPUNIT_ASSERT_EQUAL( 0. , Mean ); | 
|---|
|  | 167 | CPPUNIT_ASSERT_EQUAL( 0. , Max ); | 
|---|
| [96c961] | 168 | }; | 
|---|