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