| 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 |  | 
|---|
| 8 | /* | 
|---|
| 9 | * AnalysisCorrelationToSurfaceUnitTest.cpp | 
|---|
| 10 | * | 
|---|
| 11 | *  Created on: Oct 13, 2009 | 
|---|
| 12 | *      Author: heber | 
|---|
| 13 | */ | 
|---|
| 14 |  | 
|---|
| 15 | // include config.h | 
|---|
| 16 | #ifdef HAVE_CONFIG_H | 
|---|
| 17 | #include <config.h> | 
|---|
| 18 | #endif | 
|---|
| 19 |  | 
|---|
| 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 <cstring> | 
|---|
| 27 |  | 
|---|
| 28 | #include "analysis_correlation.hpp" | 
|---|
| 29 | #include "Descriptors/MoleculeDescriptor.hpp" | 
|---|
| 30 |  | 
|---|
| 31 | #include "atom.hpp" | 
|---|
| 32 | #include "boundary.hpp" | 
|---|
| 33 | #include "element.hpp" | 
|---|
| 34 | #include "molecule.hpp" | 
|---|
| 35 | #include "linkedcell.hpp" | 
|---|
| 36 | #include "periodentafel.hpp" | 
|---|
| 37 | #include "tesselation.hpp" | 
|---|
| 38 | #include "World.hpp" | 
|---|
| 39 | #include "CodePatterns/Assert.hpp" | 
|---|
| 40 |  | 
|---|
| 41 | #include "AnalysisCorrelationToSurfaceUnitTest.hpp" | 
|---|
| 42 |  | 
|---|
| 43 | #ifdef HAVE_TESTRUNNER | 
|---|
| 44 | #include "UnitTestMain.hpp" | 
|---|
| 45 | #endif /*HAVE_TESTRUNNER*/ | 
|---|
| 46 |  | 
|---|
| 47 | /********************************************** Test classes **************************************/ | 
|---|
| 48 |  | 
|---|
| 49 | // Registers the fixture into the 'registry' | 
|---|
| 50 | CPPUNIT_TEST_SUITE_REGISTRATION( AnalysisCorrelationToSurfaceUnitTest ); | 
|---|
| 51 |  | 
|---|
| 52 | void AnalysisCorrelationToSurfaceUnitTest::setUp() | 
|---|
| 53 | { | 
|---|
| 54 | ASSERT_DO(Assert::Throw); | 
|---|
| 55 |  | 
|---|
| 56 | setVerbosity(5); | 
|---|
| 57 |  | 
|---|
| 58 | atom *Walker = NULL; | 
|---|
| 59 |  | 
|---|
| 60 | // init private all pointers to zero | 
|---|
| 61 | TestSurfaceMolecule = NULL; | 
|---|
| 62 | surfacemap = NULL; | 
|---|
| 63 | binmap = NULL; | 
|---|
| 64 | Surface = NULL; | 
|---|
| 65 | LC = NULL; | 
|---|
| 66 |  | 
|---|
| 67 | // prepare element list | 
|---|
| 68 | hydrogen = World::getInstance().getPeriode()->FindElement(1); | 
|---|
| 69 | CPPUNIT_ASSERT(hydrogen != NULL && "hydrogen element not found"); | 
|---|
| 70 | elements.clear(); | 
|---|
| 71 |  | 
|---|
| 72 | // construct molecule (tetraeder of hydrogens) base | 
|---|
| 73 | TestSurfaceMolecule = World::getInstance().createMolecule(); | 
|---|
| 74 |  | 
|---|
| 75 | Walker = World::getInstance().createAtom(); | 
|---|
| 76 | Walker->setType(hydrogen); | 
|---|
| 77 | Walker->setPosition(Vector(1., 0., 1. )); | 
|---|
| 78 | TestSurfaceMolecule->AddAtom(Walker); | 
|---|
| 79 |  | 
|---|
| 80 | Walker = World::getInstance().createAtom(); | 
|---|
| 81 | Walker->setType(hydrogen); | 
|---|
| 82 | Walker->setPosition(Vector(0., 1., 1. )); | 
|---|
| 83 | TestSurfaceMolecule->AddAtom(Walker); | 
|---|
| 84 |  | 
|---|
| 85 | Walker = World::getInstance().createAtom(); | 
|---|
| 86 | Walker->setType(hydrogen); | 
|---|
| 87 | Walker->setPosition(Vector(1., 1., 0. )); | 
|---|
| 88 | TestSurfaceMolecule->AddAtom(Walker); | 
|---|
| 89 |  | 
|---|
| 90 | Walker = World::getInstance().createAtom(); | 
|---|
| 91 | Walker->setType(hydrogen); | 
|---|
| 92 | Walker->setPosition(Vector(0., 0., 0. )); | 
|---|
| 93 | TestSurfaceMolecule->AddAtom(Walker); | 
|---|
| 94 |  | 
|---|
| 95 | // check that TestMolecule was correctly constructed | 
|---|
| 96 | CPPUNIT_ASSERT_EQUAL( TestSurfaceMolecule->getAtomCount(), 4 ); | 
|---|
| 97 |  | 
|---|
| 98 | TestSurfaceMolecule->ActiveFlag = true; | 
|---|
| 99 |  | 
|---|
| 100 | // init tesselation and linked cell | 
|---|
| 101 | Surface = new Tesselation; | 
|---|
| 102 | LC = new LinkedCell(*TestSurfaceMolecule, 5.); | 
|---|
| 103 | FindNonConvexBorder(TestSurfaceMolecule, Surface, (const LinkedCell *&)LC, 2.5, NULL); | 
|---|
| 104 |  | 
|---|
| 105 | // add outer atoms | 
|---|
| 106 | carbon = World::getInstance().getPeriode()->FindElement(6); | 
|---|
| 107 | TestSurfaceMolecule = World::getInstance().createMolecule(); | 
|---|
| 108 | Walker = World::getInstance().createAtom(); | 
|---|
| 109 | Walker->setType(carbon); | 
|---|
| 110 | Walker->setPosition(Vector(4., 0., 4. )); | 
|---|
| 111 | TestSurfaceMolecule->AddAtom(Walker); | 
|---|
| 112 |  | 
|---|
| 113 | Walker = World::getInstance().createAtom(); | 
|---|
| 114 | Walker->setType(carbon); | 
|---|
| 115 | Walker->setPosition(Vector(0., 4., 4. )); | 
|---|
| 116 | TestSurfaceMolecule->AddAtom(Walker); | 
|---|
| 117 |  | 
|---|
| 118 | Walker = World::getInstance().createAtom(); | 
|---|
| 119 | Walker->setType(carbon); | 
|---|
| 120 | Walker->setPosition(Vector(4., 4., 0. )); | 
|---|
| 121 | TestSurfaceMolecule->AddAtom(Walker); | 
|---|
| 122 |  | 
|---|
| 123 | // add inner atoms | 
|---|
| 124 | Walker = World::getInstance().createAtom(); | 
|---|
| 125 | Walker->setType(carbon); | 
|---|
| 126 | Walker->setPosition(Vector(0.5, 0.5, 0.5 )); | 
|---|
| 127 | TestSurfaceMolecule->AddAtom(Walker); | 
|---|
| 128 |  | 
|---|
| 129 | World::getInstance().selectAllMolecules(AllMolecules()); | 
|---|
| 130 | allMolecules = World::getInstance().getSelectedMolecules(); | 
|---|
| 131 | CPPUNIT_ASSERT_EQUAL( (size_t) 2, allMolecules.size()); | 
|---|
| 132 |  | 
|---|
| 133 | // init maps | 
|---|
| 134 | surfacemap = NULL; | 
|---|
| 135 | binmap = NULL; | 
|---|
| 136 |  | 
|---|
| 137 | }; | 
|---|
| 138 |  | 
|---|
| 139 |  | 
|---|
| 140 | void AnalysisCorrelationToSurfaceUnitTest::tearDown() | 
|---|
| 141 | { | 
|---|
| 142 | if (surfacemap != NULL) | 
|---|
| 143 | delete(surfacemap); | 
|---|
| 144 | if (binmap != NULL) | 
|---|
| 145 | delete(binmap); | 
|---|
| 146 |  | 
|---|
| 147 | delete(Surface); | 
|---|
| 148 | // note that all the atoms are cleaned by TestMolecule | 
|---|
| 149 | delete(LC); | 
|---|
| 150 | World::purgeInstance(); | 
|---|
| 151 | logger::purgeInstance(); | 
|---|
| 152 | }; | 
|---|
| 153 |  | 
|---|
| 154 |  | 
|---|
| 155 | /** Checks whether setup() does the right thing. | 
|---|
| 156 | */ | 
|---|
| 157 | void AnalysisCorrelationToSurfaceUnitTest::SurfaceTest() | 
|---|
| 158 | { | 
|---|
| 159 | CPPUNIT_ASSERT_EQUAL( 4, TestSurfaceMolecule->getAtomCount() ); | 
|---|
| 160 | CPPUNIT_ASSERT_EQUAL( (size_t)2, allMolecules.size() ); | 
|---|
| 161 | CPPUNIT_ASSERT_EQUAL( (size_t)4, Surface->PointsOnBoundary.size() ); | 
|---|
| 162 | CPPUNIT_ASSERT_EQUAL( (size_t)6, Surface->LinesOnBoundary.size() ); | 
|---|
| 163 | CPPUNIT_ASSERT_EQUAL( (size_t)4, Surface->TrianglesOnBoundary.size() ); | 
|---|
| 164 | }; | 
|---|
| 165 |  | 
|---|
| 166 | void AnalysisCorrelationToSurfaceUnitTest::CorrelationToSurfaceTest() | 
|---|
| 167 | { | 
|---|
| 168 | // do the pair correlation | 
|---|
| 169 | elements.push_back(hydrogen); | 
|---|
| 170 | surfacemap = CorrelationToSurface( allMolecules, elements, Surface, LC ); | 
|---|
| 171 | //  OutputCorrelationToSurface ( (ofstream *)&cout, surfacemap ); | 
|---|
| 172 | CPPUNIT_ASSERT( surfacemap != NULL ); | 
|---|
| 173 | CPPUNIT_ASSERT_EQUAL( (size_t)4, surfacemap->size() ); | 
|---|
| 174 | }; | 
|---|
| 175 |  | 
|---|
| 176 | void AnalysisCorrelationToSurfaceUnitTest::CorrelationToSurfaceHydrogenBinNoRangeTest() | 
|---|
| 177 | { | 
|---|
| 178 | BinPairMap::iterator tester; | 
|---|
| 179 | elements.push_back(hydrogen); | 
|---|
| 180 | surfacemap = CorrelationToSurface( allMolecules, elements, Surface, LC ); | 
|---|
| 181 | // put pair correlation into bins and check with no range | 
|---|
| 182 | //  OutputCorrelationToSurface ( (ofstream *)&cout, surfacemap ); | 
|---|
| 183 | binmap = BinData( surfacemap, 0.5, 0., 0. ); | 
|---|
| 184 | CPPUNIT_ASSERT_EQUAL( (size_t)1, binmap->size() ); | 
|---|
| 185 | OutputCorrelationMap<BinPairMap> ( (ofstream *)&cout, binmap, OutputCorrelation_Header, OutputCorrelation_Value ); | 
|---|
| 186 | tester = binmap->begin(); | 
|---|
| 187 | CPPUNIT_ASSERT_EQUAL( 0., tester->first ); | 
|---|
| 188 | CPPUNIT_ASSERT_EQUAL( 4, tester->second ); | 
|---|
| 189 |  | 
|---|
| 190 | }; | 
|---|
| 191 |  | 
|---|
| 192 | void AnalysisCorrelationToSurfaceUnitTest::CorrelationToSurfaceHydrogenBinRangeTest() | 
|---|
| 193 | { | 
|---|
| 194 | BinPairMap::iterator tester; | 
|---|
| 195 | elements.push_back(hydrogen); | 
|---|
| 196 | surfacemap = CorrelationToSurface( allMolecules, elements, Surface, LC ); | 
|---|
| 197 | //  OutputCorrelationToSurface ( (ofstream *)&cout, surfacemap ); | 
|---|
| 198 | // ... and check with [0., 2.] range | 
|---|
| 199 | binmap = BinData( surfacemap, 0.5, 0., 2. ); | 
|---|
| 200 | CPPUNIT_ASSERT_EQUAL( (size_t)5, binmap->size() ); | 
|---|
| 201 | //  OutputCorrelation ( (ofstream *)&cout, binmap ); | 
|---|
| 202 | tester = binmap->begin(); | 
|---|
| 203 | CPPUNIT_ASSERT_EQUAL( 0., tester->first ); | 
|---|
| 204 | CPPUNIT_ASSERT_EQUAL( 4, tester->second ); | 
|---|
| 205 | tester = binmap->find(1.); | 
|---|
| 206 | CPPUNIT_ASSERT_EQUAL( 1., tester->first ); | 
|---|
| 207 | CPPUNIT_ASSERT_EQUAL( 0, tester->second ); | 
|---|
| 208 |  | 
|---|
| 209 | }; | 
|---|
| 210 |  | 
|---|
| 211 | void AnalysisCorrelationToSurfaceUnitTest::CorrelationToSurfaceCarbonBinNoRangeTest() | 
|---|
| 212 | { | 
|---|
| 213 | BinPairMap::iterator tester; | 
|---|
| 214 | elements.push_back(carbon); | 
|---|
| 215 | surfacemap = CorrelationToSurface( allMolecules, elements, Surface, LC ); | 
|---|
| 216 | //  OutputCorrelationToSurface ( (ofstream *)&cout, surfacemap ); | 
|---|
| 217 | // put pair correlation into bins and check with no range | 
|---|
| 218 | binmap = BinData( surfacemap, 0.5, 0., 0. ); | 
|---|
| 219 | //OutputCorrelation ( (ofstream *)&cout, binmap ); | 
|---|
| 220 | CPPUNIT_ASSERT_EQUAL( (size_t)9, binmap->size() ); | 
|---|
| 221 | // inside point is first and must have negative value | 
|---|
| 222 | tester = binmap->lower_bound(4.25-0.5); // start depends on the min value and | 
|---|
| 223 | CPPUNIT_ASSERT( tester != binmap->end() ); | 
|---|
| 224 | CPPUNIT_ASSERT_EQUAL( 3, tester->second ); | 
|---|
| 225 | // inner point | 
|---|
| 226 | tester = binmap->lower_bound(0.); | 
|---|
| 227 | CPPUNIT_ASSERT( tester != binmap->end() ); | 
|---|
| 228 | CPPUNIT_ASSERT_EQUAL( 1, tester->second ); | 
|---|
| 229 | }; | 
|---|
| 230 |  | 
|---|
| 231 | void AnalysisCorrelationToSurfaceUnitTest::CorrelationToSurfaceCarbonBinRangeTest() | 
|---|
| 232 | { | 
|---|
| 233 | BinPairMap::iterator tester; | 
|---|
| 234 | elements.push_back(carbon); | 
|---|
| 235 | surfacemap = CorrelationToSurface( allMolecules, elements, Surface, LC ); | 
|---|
| 236 | //  OutputCorrelationToSurface ( (ofstream *)&cout, surfacemap ); | 
|---|
| 237 | // ... and check with [0., 2.] range | 
|---|
| 238 | binmap = BinData( surfacemap, 0.5, -2., 4. ); | 
|---|
| 239 | //OutputCorrelation ( (ofstream *)&cout, binmap ); | 
|---|
| 240 | CPPUNIT_ASSERT_EQUAL( (size_t)13, binmap->size() ); | 
|---|
| 241 | // three outside points | 
|---|
| 242 | tester = binmap->lower_bound(4.25-0.5); | 
|---|
| 243 | CPPUNIT_ASSERT( tester != binmap->end() ); | 
|---|
| 244 | CPPUNIT_ASSERT_EQUAL( 3, tester->second ); | 
|---|
| 245 | // inner point | 
|---|
| 246 | tester = binmap->lower_bound(0.); | 
|---|
| 247 | CPPUNIT_ASSERT( tester != binmap->end() ); | 
|---|
| 248 | CPPUNIT_ASSERT_EQUAL( 1, tester->second ); | 
|---|
| 249 | }; | 
|---|