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