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