| 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/analysis_correlation.hpp"
 | 
|---|
| 29 | #include "atom.hpp"
 | 
|---|
| 30 | #include "Tesselation/boundary.hpp"
 | 
|---|
| 31 | #include "CodePatterns/Assert.hpp"
 | 
|---|
| 32 | #include "Descriptors/MoleculeDescriptor.hpp"
 | 
|---|
| 33 | #include "Element/element.hpp"
 | 
|---|
| 34 | #include "molecule.hpp"
 | 
|---|
| 35 | #include "linkedcell.hpp"
 | 
|---|
| 36 | #include "Element/periodentafel.hpp"
 | 
|---|
| 37 | #include "PointCloudAdaptor.hpp"
 | 
|---|
| 38 | #include "Tesselation/tesselation.hpp"
 | 
|---|
| 39 | #include "World.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 |   PointCloudAdaptor<molecule> cloud(TestSurfaceMolecule, TestSurfaceMolecule->name);
 | 
|---|
| 103 |   LC = new LinkedCell(cloud, 5.);
 | 
|---|
| 104 |   FindNonConvexBorder(TestSurfaceMolecule, Surface, (const LinkedCell *&)LC, 2.5, NULL);
 | 
|---|
| 105 | 
 | 
|---|
| 106 |   // add outer atoms
 | 
|---|
| 107 |   carbon = World::getInstance().getPeriode()->FindElement(6);
 | 
|---|
| 108 |   TestSurfaceMolecule = World::getInstance().createMolecule();
 | 
|---|
| 109 |   Walker = World::getInstance().createAtom();
 | 
|---|
| 110 |   Walker->setType(carbon);
 | 
|---|
| 111 |   Walker->setPosition(Vector(4., 0., 4. ));
 | 
|---|
| 112 |   TestSurfaceMolecule->AddAtom(Walker);
 | 
|---|
| 113 | 
 | 
|---|
| 114 |   Walker = World::getInstance().createAtom();
 | 
|---|
| 115 |   Walker->setType(carbon);
 | 
|---|
| 116 |   Walker->setPosition(Vector(0., 4., 4. ));
 | 
|---|
| 117 |   TestSurfaceMolecule->AddAtom(Walker);
 | 
|---|
| 118 | 
 | 
|---|
| 119 |   Walker = World::getInstance().createAtom();
 | 
|---|
| 120 |   Walker->setType(carbon);
 | 
|---|
| 121 |   Walker->setPosition(Vector(4., 4., 0. ));
 | 
|---|
| 122 |   TestSurfaceMolecule->AddAtom(Walker);
 | 
|---|
| 123 | 
 | 
|---|
| 124 |   // add inner atoms
 | 
|---|
| 125 |   Walker = World::getInstance().createAtom();
 | 
|---|
| 126 |   Walker->setType(carbon);
 | 
|---|
| 127 |   Walker->setPosition(Vector(0.5, 0.5, 0.5 ));
 | 
|---|
| 128 |   TestSurfaceMolecule->AddAtom(Walker);
 | 
|---|
| 129 | 
 | 
|---|
| 130 |   World::getInstance().selectAllMolecules(AllMolecules());
 | 
|---|
| 131 |   allMolecules = World::getInstance().getSelectedMolecules();
 | 
|---|
| 132 |   CPPUNIT_ASSERT_EQUAL( (size_t) 2, allMolecules.size());
 | 
|---|
| 133 | 
 | 
|---|
| 134 |   // init maps
 | 
|---|
| 135 |   surfacemap = NULL;
 | 
|---|
| 136 |   binmap = NULL;
 | 
|---|
| 137 | 
 | 
|---|
| 138 | };
 | 
|---|
| 139 | 
 | 
|---|
| 140 | 
 | 
|---|
| 141 | void AnalysisCorrelationToSurfaceUnitTest::tearDown()
 | 
|---|
| 142 | {
 | 
|---|
| 143 |   if (surfacemap != NULL)
 | 
|---|
| 144 |     delete(surfacemap);
 | 
|---|
| 145 |   if (binmap != NULL)
 | 
|---|
| 146 |     delete(binmap);
 | 
|---|
| 147 | 
 | 
|---|
| 148 |   delete(Surface);
 | 
|---|
| 149 |   // note that all the atoms are cleaned by TestMolecule
 | 
|---|
| 150 |   delete(LC);
 | 
|---|
| 151 |   World::purgeInstance();
 | 
|---|
| 152 |   logger::purgeInstance();
 | 
|---|
| 153 | };
 | 
|---|
| 154 | 
 | 
|---|
| 155 | 
 | 
|---|
| 156 | /** Checks whether setup() does the right thing.
 | 
|---|
| 157 |  */
 | 
|---|
| 158 | void AnalysisCorrelationToSurfaceUnitTest::SurfaceTest()
 | 
|---|
| 159 | {
 | 
|---|
| 160 |   CPPUNIT_ASSERT_EQUAL( 4, TestSurfaceMolecule->getAtomCount() );
 | 
|---|
| 161 |   CPPUNIT_ASSERT_EQUAL( (size_t)2, allMolecules.size() );
 | 
|---|
| 162 |   CPPUNIT_ASSERT_EQUAL( (size_t)4, Surface->PointsOnBoundary.size() );
 | 
|---|
| 163 |   CPPUNIT_ASSERT_EQUAL( (size_t)6, Surface->LinesOnBoundary.size() );
 | 
|---|
| 164 |   CPPUNIT_ASSERT_EQUAL( (size_t)4, Surface->TrianglesOnBoundary.size() );
 | 
|---|
| 165 | };
 | 
|---|
| 166 | 
 | 
|---|
| 167 | void AnalysisCorrelationToSurfaceUnitTest::CorrelationToSurfaceTest()
 | 
|---|
| 168 | {
 | 
|---|
| 169 |   // do the pair correlation
 | 
|---|
| 170 |   elements.push_back(hydrogen);
 | 
|---|
| 171 |   surfacemap = CorrelationToSurface( allMolecules, elements, Surface, LC );
 | 
|---|
| 172 | //  OutputCorrelationToSurface ( (ofstream *)&cout, surfacemap );
 | 
|---|
| 173 |   CPPUNIT_ASSERT( surfacemap != NULL );
 | 
|---|
| 174 |   CPPUNIT_ASSERT_EQUAL( (size_t)4, surfacemap->size() );
 | 
|---|
| 175 | };
 | 
|---|
| 176 | 
 | 
|---|
| 177 | void AnalysisCorrelationToSurfaceUnitTest::CorrelationToSurfaceHydrogenBinNoRangeTest()
 | 
|---|
| 178 | {
 | 
|---|
| 179 |   BinPairMap::iterator tester;
 | 
|---|
| 180 |   elements.push_back(hydrogen);
 | 
|---|
| 181 |   surfacemap = CorrelationToSurface( allMolecules, elements, Surface, LC );
 | 
|---|
| 182 |   // put pair correlation into bins and check with no range
 | 
|---|
| 183 | //  OutputCorrelationToSurface ( (ofstream *)&cout, surfacemap );
 | 
|---|
| 184 |   binmap = BinData( surfacemap, 0.5, 0., 0. );
 | 
|---|
| 185 |   CPPUNIT_ASSERT_EQUAL( (size_t)1, binmap->size() );
 | 
|---|
| 186 |   OutputCorrelationMap<BinPairMap> ( (ofstream *)&cout, binmap, OutputCorrelation_Header, OutputCorrelation_Value );
 | 
|---|
| 187 |   tester = binmap->begin();
 | 
|---|
| 188 |   CPPUNIT_ASSERT_EQUAL( 0., tester->first );
 | 
|---|
| 189 |   CPPUNIT_ASSERT_EQUAL( 4, tester->second );
 | 
|---|
| 190 | 
 | 
|---|
| 191 | };
 | 
|---|
| 192 | 
 | 
|---|
| 193 | void AnalysisCorrelationToSurfaceUnitTest::CorrelationToSurfaceHydrogenBinRangeTest()
 | 
|---|
| 194 | {
 | 
|---|
| 195 |   BinPairMap::iterator tester;
 | 
|---|
| 196 |   elements.push_back(hydrogen);
 | 
|---|
| 197 |   surfacemap = CorrelationToSurface( allMolecules, elements, Surface, LC );
 | 
|---|
| 198 | //  OutputCorrelationToSurface ( (ofstream *)&cout, surfacemap );
 | 
|---|
| 199 |   // ... and check with [0., 2.] range
 | 
|---|
| 200 |   binmap = BinData( surfacemap, 0.5, 0., 2. );
 | 
|---|
| 201 |   CPPUNIT_ASSERT_EQUAL( (size_t)5, binmap->size() );
 | 
|---|
| 202 | //  OutputCorrelation ( (ofstream *)&cout, binmap );
 | 
|---|
| 203 |   tester = binmap->begin();
 | 
|---|
| 204 |   CPPUNIT_ASSERT_EQUAL( 0., tester->first );
 | 
|---|
| 205 |   CPPUNIT_ASSERT_EQUAL( 4, tester->second );
 | 
|---|
| 206 |   tester = binmap->find(1.);
 | 
|---|
| 207 |   CPPUNIT_ASSERT_EQUAL( 1., tester->first );
 | 
|---|
| 208 |   CPPUNIT_ASSERT_EQUAL( 0, tester->second );
 | 
|---|
| 209 | 
 | 
|---|
| 210 | };
 | 
|---|
| 211 | 
 | 
|---|
| 212 | void AnalysisCorrelationToSurfaceUnitTest::CorrelationToSurfaceCarbonBinNoRangeTest()
 | 
|---|
| 213 | {
 | 
|---|
| 214 |   BinPairMap::iterator tester;
 | 
|---|
| 215 |   elements.push_back(carbon);
 | 
|---|
| 216 |   surfacemap = CorrelationToSurface( allMolecules, elements, Surface, LC );
 | 
|---|
| 217 | //  OutputCorrelationToSurface ( (ofstream *)&cout, surfacemap );
 | 
|---|
| 218 |   // put pair correlation into bins and check with no range
 | 
|---|
| 219 |   binmap = BinData( surfacemap, 0.5, 0., 0. );
 | 
|---|
| 220 |   //OutputCorrelation ( (ofstream *)&cout, binmap );
 | 
|---|
| 221 |   CPPUNIT_ASSERT_EQUAL( (size_t)9, binmap->size() );
 | 
|---|
| 222 |   // inside point is first and must have negative value
 | 
|---|
| 223 |   tester = binmap->lower_bound(4.25-0.5); // start depends on the min value and
 | 
|---|
| 224 |   CPPUNIT_ASSERT( tester != binmap->end() );
 | 
|---|
| 225 |   CPPUNIT_ASSERT_EQUAL( 3, tester->second );
 | 
|---|
| 226 |   // inner point
 | 
|---|
| 227 |   tester = binmap->lower_bound(0.);
 | 
|---|
| 228 |   CPPUNIT_ASSERT( tester != binmap->end() );
 | 
|---|
| 229 |   CPPUNIT_ASSERT_EQUAL( 1, tester->second );
 | 
|---|
| 230 | };
 | 
|---|
| 231 | 
 | 
|---|
| 232 | void AnalysisCorrelationToSurfaceUnitTest::CorrelationToSurfaceCarbonBinRangeTest()
 | 
|---|
| 233 | {
 | 
|---|
| 234 |   BinPairMap::iterator tester;
 | 
|---|
| 235 |   elements.push_back(carbon);
 | 
|---|
| 236 |   surfacemap = CorrelationToSurface( allMolecules, elements, Surface, LC );
 | 
|---|
| 237 | //  OutputCorrelationToSurface ( (ofstream *)&cout, surfacemap );
 | 
|---|
| 238 |   // ... and check with [0., 2.] range
 | 
|---|
| 239 |   binmap = BinData( surfacemap, 0.5, -2., 4. );
 | 
|---|
| 240 |   //OutputCorrelation ( (ofstream *)&cout, binmap );
 | 
|---|
| 241 |   CPPUNIT_ASSERT_EQUAL( (size_t)13, binmap->size() );
 | 
|---|
| 242 |   // three outside points
 | 
|---|
| 243 |   tester = binmap->lower_bound(4.25-0.5);
 | 
|---|
| 244 |   CPPUNIT_ASSERT( tester != binmap->end() );
 | 
|---|
| 245 |   CPPUNIT_ASSERT_EQUAL( 3, tester->second );
 | 
|---|
| 246 |   // inner point
 | 
|---|
| 247 |   tester = binmap->lower_bound(0.);
 | 
|---|
| 248 |   CPPUNIT_ASSERT( tester != binmap->end() );
 | 
|---|
| 249 |   CPPUNIT_ASSERT_EQUAL( 1, tester->second );
 | 
|---|
| 250 | };
 | 
|---|