| [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 | 
 | 
|---|
| [4eb4fe] | 42 |   // get elements
 | 
|---|
 | 43 |   hydrogen = World::getInstance().getPeriode()->FindElement(1);
 | 
|---|
 | 44 |   carbon = World::getInstance().getPeriode()->FindElement(6);
 | 
|---|
 | 45 |   CPPUNIT_ASSERT(hydrogen != NULL && "could not find element hydrogen");
 | 
|---|
 | 46 |   CPPUNIT_ASSERT(carbon != NULL && "could not find element carbon");
 | 
|---|
| [96c961] | 47 | 
 | 
|---|
 | 48 |   // construct molecule (tetraeder of hydrogens)
 | 
|---|
| [23b547] | 49 |   TestMolecule = World::getInstance().createMolecule();
 | 
|---|
| [4eb4fe] | 50 |   CPPUNIT_ASSERT(TestMolecule != NULL && "could not create molecule");
 | 
|---|
| [23b547] | 51 |   Walker = World::getInstance().createAtom();
 | 
|---|
| [4eb4fe] | 52 |   CPPUNIT_ASSERT(Walker != NULL && "could not create atom");
 | 
|---|
| [96c961] | 53 |   Walker->type = hydrogen;
 | 
|---|
| [1bd79e] | 54 |   *Walker->node = Vector(1.5, 0., 1.5 );
 | 
|---|
| [96c961] | 55 |   TestMolecule->AddAtom(Walker);
 | 
|---|
| [23b547] | 56 |   Walker = World::getInstance().createAtom();
 | 
|---|
| [4eb4fe] | 57 |   CPPUNIT_ASSERT(Walker != NULL && "could not create atom");
 | 
|---|
| [96c961] | 58 |   Walker->type = hydrogen;
 | 
|---|
| [1bd79e] | 59 |   *Walker->node = Vector(0., 1.5, 1.5 );
 | 
|---|
| [96c961] | 60 |   TestMolecule->AddAtom(Walker);
 | 
|---|
| [23b547] | 61 |   Walker = World::getInstance().createAtom();
 | 
|---|
| [4eb4fe] | 62 |   CPPUNIT_ASSERT(Walker != NULL && "could not create atom");
 | 
|---|
| [96c961] | 63 |   Walker->type = hydrogen;
 | 
|---|
| [1bd79e] | 64 |   *Walker->node = Vector(1.5, 1.5, 0. );
 | 
|---|
| [96c961] | 65 |   TestMolecule->AddAtom(Walker);
 | 
|---|
| [23b547] | 66 |   Walker = World::getInstance().createAtom();
 | 
|---|
| [4eb4fe] | 67 |   CPPUNIT_ASSERT(Walker != NULL && "could not create atom");
 | 
|---|
| [96c961] | 68 |   Walker->type = hydrogen;
 | 
|---|
| [1bd79e] | 69 |   *Walker->node = Vector(0., 0., 0. );
 | 
|---|
| [96c961] | 70 |   TestMolecule->AddAtom(Walker);
 | 
|---|
| [23b547] | 71 |   Walker = World::getInstance().createAtom();
 | 
|---|
| [4eb4fe] | 72 |   CPPUNIT_ASSERT(Walker != NULL && "could not create atom");
 | 
|---|
| [220cf37] | 73 |   Walker->type = carbon;
 | 
|---|
| [1bd79e] | 74 |   *Walker->node = Vector(0.5, 0.5, 0.5 );
 | 
|---|
| [220cf37] | 75 |   TestMolecule->AddAtom(Walker);
 | 
|---|
| [96c961] | 76 | 
 | 
|---|
 | 77 |   // check that TestMolecule was correctly constructed
 | 
|---|
| [220cf37] | 78 |   CPPUNIT_ASSERT_EQUAL( TestMolecule->AtomCount, 5 );
 | 
|---|
| [96c961] | 79 | 
 | 
|---|
 | 80 |   // create a small file with table
 | 
|---|
 | 81 |   filename = new string("test.dat");
 | 
|---|
| [4eb4fe] | 82 |   CPPUNIT_ASSERT(filename != NULL && "could not create string");
 | 
|---|
| [96c961] | 83 |   ofstream test(filename->c_str());
 | 
|---|
| [4eb4fe] | 84 |   test << ".\tH\tHe\tLi\tBe\tB\tC\n";
 | 
|---|
 | 85 |   test << "H\t1.\t1.\t1.\t1.\t1.\t1.2\n";
 | 
|---|
 | 86 |   test << "He\t1.\t1.\t1.\t1.\t1.\t1.\n";
 | 
|---|
 | 87 |   test << "Li\t1.\t1.\t1.\t1.\t1.\t1.\n";
 | 
|---|
 | 88 |   test << "Be\t1.\t1.\t1.\t1.\t1.\t1.\n";
 | 
|---|
 | 89 |   test << "B\t1.\t1.\t1.\t1.\t1.\t1.\n";
 | 
|---|
 | 90 |   test << "C\t1.2\t1.\t1.\t1.\t1.\t1.5\n";
 | 
|---|
| [220cf37] | 91 |   test.close();
 | 
|---|
| [96c961] | 92 |   BG = new BondGraph(true);
 | 
|---|
| [4eb4fe] | 93 |   CPPUNIT_ASSERT(BG != NULL && "could not create BondGraph");
 | 
|---|
| [220cf37] | 94 | 
 | 
|---|
 | 95 |   CPPUNIT_ASSERT_EQUAL( true , BG->LoadBondLengthTable(*filename) );
 | 
|---|
 | 96 |   CPPUNIT_ASSERT_EQUAL( 1., BG->GetBondLength(0,0) );
 | 
|---|
| [4eb4fe] | 97 |   CPPUNIT_ASSERT_EQUAL( 1.2, BG->GetBondLength(0,5) );
 | 
|---|
 | 98 |   CPPUNIT_ASSERT_EQUAL( 1.5, BG->GetBondLength(5,5) );
 | 
|---|
| [220cf37] | 99 | 
 | 
|---|
 | 100 |   BG->ConstructBondGraph(TestMolecule);
 | 
|---|
| [96c961] | 101 | };
 | 
|---|
 | 102 | 
 | 
|---|
 | 103 | 
 | 
|---|
 | 104 | void AnalysisBondsTest::tearDown()
 | 
|---|
 | 105 | {
 | 
|---|
 | 106 |   // remove the file
 | 
|---|
 | 107 |   remove(filename->c_str());
 | 
|---|
 | 108 |   delete(filename);
 | 
|---|
 | 109 |   delete(BG);
 | 
|---|
 | 110 | 
 | 
|---|
 | 111 |   // remove molecule
 | 
|---|
| [23b547] | 112 |   World::getInstance().destroyMolecule(TestMolecule);
 | 
|---|
| [96c961] | 113 |   // note that all the atoms are cleaned by TestMolecule
 | 
|---|
| [23b547] | 114 |   World::purgeInstance();
 | 
|---|
| [96c961] | 115 | };
 | 
|---|
 | 116 | 
 | 
|---|
| [220cf37] | 117 | /** UnitTest for GetMaxMinMeanBondCount().
 | 
|---|
| [96c961] | 118 |  */
 | 
|---|
| [220cf37] | 119 | void AnalysisBondsTest::GetMaxMinMeanBondCountTest()
 | 
|---|
| [96c961] | 120 | {
 | 
|---|
| [220cf37] | 121 |   double Min = 20.; // check that initialization resets these arbitrary values
 | 
|---|
 | 122 |   double Mean = 200.;
 | 
|---|
 | 123 |   double Max = 1e-6;
 | 
|---|
 | 124 |   GetMaxMinMeanBondCount(TestMolecule, Min, Mean, Max);
 | 
|---|
 | 125 |   CPPUNIT_ASSERT_EQUAL( 1., Min );
 | 
|---|
 | 126 |   CPPUNIT_ASSERT_EQUAL( 1.6, Mean );
 | 
|---|
 | 127 |   CPPUNIT_ASSERT_EQUAL( 4., Max );
 | 
|---|
| [96c961] | 128 | 
 | 
|---|
| [220cf37] | 129 | };
 | 
|---|
| [96c961] | 130 | 
 | 
|---|
| [220cf37] | 131 | /** UnitTest for MinMaxBondDistanceBetweenElements().
 | 
|---|
 | 132 |  */
 | 
|---|
 | 133 | void AnalysisBondsTest::MinMeanMaxBondDistanceBetweenElementsTest()
 | 
|---|
 | 134 | {
 | 
|---|
 | 135 |   double Min = 20.; // check that initialization resets these arbitrary values
 | 
|---|
 | 136 |   double Mean = 2e+6;
 | 
|---|
 | 137 |   double Max = 1e-6;
 | 
|---|
 | 138 |   double Min2 = 20.;
 | 
|---|
 | 139 |   double Mean2 = 2e+6;
 | 
|---|
 | 140 |   double Max2 = 1e-6;
 | 
|---|
 | 141 |   const double maxbondlength = sqrt(1.*1. + 1.*1. + .5*.5);
 | 
|---|
 | 142 |   const double minbondlength = sqrt(.5*.5 + .5*.5 + .5*.5);
 | 
|---|
 | 143 |   const double meanbondlength = (minbondlength+3.*maxbondlength)/4.;
 | 
|---|
 | 144 |   // check bond lengths C-H
 | 
|---|
 | 145 |   MinMeanMaxBondDistanceBetweenElements(TestMolecule, hydrogen, carbon, Min, Mean, Max);
 | 
|---|
 | 146 |   CPPUNIT_ASSERT_EQUAL( minbondlength , Min );
 | 
|---|
 | 147 |   CPPUNIT_ASSERT_EQUAL( meanbondlength , Mean );
 | 
|---|
 | 148 |   CPPUNIT_ASSERT_EQUAL( maxbondlength , Max );
 | 
|---|
 | 149 | 
 | 
|---|
 | 150 |   // check that elements are symmetric, i.e. C-H == H-C
 | 
|---|
 | 151 |   MinMeanMaxBondDistanceBetweenElements(TestMolecule, carbon, hydrogen, Min2, Mean2, Max2);
 | 
|---|
 | 152 |   CPPUNIT_ASSERT_EQUAL( Min , Min2 );
 | 
|---|
 | 153 |   CPPUNIT_ASSERT_EQUAL( Mean , Mean2 );
 | 
|---|
 | 154 |   CPPUNIT_ASSERT_EQUAL( Max , Max2 );
 | 
|---|
 | 155 | 
 | 
|---|
 | 156 |   // check no bond case (no bonds H-H in system!)
 | 
|---|
 | 157 |   MinMeanMaxBondDistanceBetweenElements(TestMolecule, hydrogen, hydrogen, Min, Mean, Max);
 | 
|---|
 | 158 |   CPPUNIT_ASSERT_EQUAL( 0. , Min );
 | 
|---|
 | 159 |   CPPUNIT_ASSERT_EQUAL( 0. , Mean );
 | 
|---|
 | 160 |   CPPUNIT_ASSERT_EQUAL( 0. , Max );
 | 
|---|
| [96c961] | 161 | };
 | 
|---|