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