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