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