| 1 | /* | 
|---|
| 2 | * Project: MoleCuilder | 
|---|
| 3 | * Description: creates and alters molecular systems | 
|---|
| 4 | * Copyright (C)  2012 University of Bonn. All rights reserved. | 
|---|
| 5 | * Please see the LICENSE file or "Copyright notice" in builder.cpp for details. | 
|---|
| 6 | */ | 
|---|
| 7 |  | 
|---|
| 8 | /* | 
|---|
| 9 | * LinkedCell_ModelUnitTest.cpp | 
|---|
| 10 | * | 
|---|
| 11 | *  Created on: Nov 17, 2011 | 
|---|
| 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 <list> | 
|---|
| 27 |  | 
|---|
| 28 | #include "Box.hpp" | 
|---|
| 29 | #include "CodePatterns/Assert.hpp" | 
|---|
| 30 | #include "CodePatterns/Log.hpp" | 
|---|
| 31 | #include "LinearAlgebra/RealSpaceMatrix.hpp" | 
|---|
| 32 | #include "LinkedCell/LinkedCell.hpp" | 
|---|
| 33 | #include "LinkedCell/LinkedCell_Model.hpp" | 
|---|
| 34 | #include "LinkedCell/LinkedCell_Model_changeModel.hpp" | 
|---|
| 35 | #include "LinkedCell/LinkedCell_Model_LinkedCellArrayCache.hpp" | 
|---|
| 36 | #include "LinkedCell/LinkedCell_Model_Update.hpp" | 
|---|
| 37 | #include "LinkedCell/PointCloudAdaptor.hpp" | 
|---|
| 38 | #include "LinkedCell/unittests/defs.hpp" | 
|---|
| 39 | #include "World.hpp" | 
|---|
| 40 | #include "WorldTime.hpp" | 
|---|
| 41 |  | 
|---|
| 42 | #include "LinkedCell_ModelUnitTest.hpp" | 
|---|
| 43 |  | 
|---|
| 44 | #ifdef HAVE_TESTRUNNER | 
|---|
| 45 | #include "UnitTestMain.hpp" | 
|---|
| 46 | #endif /*HAVE_TESTRUNNER*/ | 
|---|
| 47 |  | 
|---|
| 48 | /********************************************** Test classes **************************************/ | 
|---|
| 49 |  | 
|---|
| 50 | // Registers the fixture into the 'registry' | 
|---|
| 51 | CPPUNIT_TEST_SUITE_REGISTRATION( LinkedCell_ModelTest ); | 
|---|
| 52 |  | 
|---|
| 53 |  | 
|---|
| 54 | void LinkedCell_ModelTest::setUp() | 
|---|
| 55 | { | 
|---|
| 56 | // failing asserts should be thrown | 
|---|
| 57 | ASSERT_DO(Assert::Throw); | 
|---|
| 58 |  | 
|---|
| 59 | setVerbosity(3); | 
|---|
| 60 |  | 
|---|
| 61 | // create diag(20.) matrix | 
|---|
| 62 | RealSpaceMatrix BoxM; | 
|---|
| 63 | BoxM.setIdentity(); | 
|---|
| 64 | BoxM *= DOMAINLENGTH; | 
|---|
| 65 |  | 
|---|
| 66 | // create Box with this matrix | 
|---|
| 67 | domain = new Box(BoxM); | 
|---|
| 68 |  | 
|---|
| 69 | // create LinkedCell structure with this Box | 
|---|
| 70 | LC = new LinkedCell::LinkedCell_Model(EDGELENGTH, *domain); | 
|---|
| 71 |  | 
|---|
| 72 | // create a list of nodes and add to LCImpl | 
|---|
| 73 | std::vector< Vector > VectorList; | 
|---|
| 74 | for (size_t i=0;i<((size_t)floor(NUMBERCELLS));++i) | 
|---|
| 75 | VectorList.push_back(Vector((double)i*EDGELENGTH,(double)i*EDGELENGTH,(double)i*EDGELENGTH)); | 
|---|
| 76 | CPPUNIT_ASSERT_EQUAL( ((size_t)floor(NUMBERCELLS)), VectorList.size() ); | 
|---|
| 77 | for (size_t i=0;i<VectorList.size();++i) { | 
|---|
| 78 | TesselPoint * Walker = new TesselPoint(); | 
|---|
| 79 | Walker->setName(std::string("Walker")+toString(i)); | 
|---|
| 80 | Walker->setPosition(VectorList[i]); | 
|---|
| 81 | NodeList.insert(Walker); | 
|---|
| 82 | } | 
|---|
| 83 | CPPUNIT_ASSERT_EQUAL( VectorList.size(), NodeList.size() ); | 
|---|
| 84 | } | 
|---|
| 85 |  | 
|---|
| 86 |  | 
|---|
| 87 | void LinkedCell_ModelTest::tearDown() | 
|---|
| 88 | { | 
|---|
| 89 | delete LC; | 
|---|
| 90 | delete domain; | 
|---|
| 91 |  | 
|---|
| 92 | // remove all nodes again | 
|---|
| 93 | for (PointSet::iterator iter = NodeList.begin(); | 
|---|
| 94 | !NodeList.empty(); | 
|---|
| 95 | iter = NodeList.begin()) { | 
|---|
| 96 | delete *iter; | 
|---|
| 97 | NodeList.erase(iter); | 
|---|
| 98 | } | 
|---|
| 99 |  | 
|---|
| 100 | // delete in correct order | 
|---|
| 101 | World::purgeInstance(); | 
|---|
| 102 | WorldTime::purgeInstance(); | 
|---|
| 103 | } | 
|---|
| 104 |  | 
|---|
| 105 | /** UnitTest for correct construction | 
|---|
| 106 | */ | 
|---|
| 107 | void LinkedCell_ModelTest::AllocationTest() | 
|---|
| 108 | { | 
|---|
| 109 | // check that first cell is allocated | 
|---|
| 110 | LinkedCell::tripleIndex index; | 
|---|
| 111 | index[0] = index[1] = index[2] = 0; | 
|---|
| 112 | CPPUNIT_ASSERT(LC->N->getN()(index) != NULL); | 
|---|
| 113 |  | 
|---|
| 114 | // check that very last cell is allocated | 
|---|
| 115 | index[0] = index[1] = index[2] = (size_t)floor(NUMBERCELLS)-1; | 
|---|
| 116 | CPPUNIT_ASSERT(LC->N->getN()(index) != NULL); | 
|---|
| 117 |  | 
|---|
| 118 | } | 
|---|
| 119 |  | 
|---|
| 120 | /** UnitTest for getSize() | 
|---|
| 121 | */ | 
|---|
| 122 | void LinkedCell_ModelTest::getSizeTest() | 
|---|
| 123 | { | 
|---|
| 124 | // check getSize() | 
|---|
| 125 | for(size_t i=0; i<NDIM; ++i) | 
|---|
| 126 | CPPUNIT_ASSERT_EQUAL((LinkedCell::LinkedCellArray::index)floor(NUMBERCELLS), LC->getSize(i)); | 
|---|
| 127 | #ifndef NDEBUG | 
|---|
| 128 | std::cout << "The following assertion is intended and is not a failure of the code." << std::endl; | 
|---|
| 129 | CPPUNIT_ASSERT_THROW( LC->getSize(4), Assert::AssertionFailure); | 
|---|
| 130 | #endif | 
|---|
| 131 | } | 
|---|
| 132 |  | 
|---|
| 133 | /** UnitTest for Reset() | 
|---|
| 134 | */ | 
|---|
| 135 | void LinkedCell_ModelTest::ResetTest() | 
|---|
| 136 | { | 
|---|
| 137 | LC->Reset(); | 
|---|
| 138 |  | 
|---|
| 139 | for(size_t i=0; i<NDIM; ++i) | 
|---|
| 140 | CPPUNIT_ASSERT_EQUAL((LinkedCell::LinkedCellArray::index)0, LC->getSize(i)); | 
|---|
| 141 | } | 
|---|
| 142 |  | 
|---|
| 143 |  | 
|---|
| 144 | /** UnitTest for insertPointCloud() | 
|---|
| 145 | */ | 
|---|
| 146 | void LinkedCell_ModelTest::insertPointCloudTest() | 
|---|
| 147 | { | 
|---|
| 148 |  | 
|---|
| 149 | // create the linked cell structure | 
|---|
| 150 | PointCloudAdaptor< PointSet > cloud(&NodeList, std::string("NodeList")); | 
|---|
| 151 | LC->insertPointCloud(cloud); | 
|---|
| 152 |  | 
|---|
| 153 | // assure that we are updated before accessing internals | 
|---|
| 154 | CPPUNIT_ASSERT_EQUAL( true, *(LC->N->UpToDate) ); | 
|---|
| 155 | // test structure | 
|---|
| 156 | CPPUNIT_ASSERT_EQUAL(((size_t)floor(NUMBERCELLS)), LC->CellLookup.size()); | 
|---|
| 157 | LinkedCell::tripleIndex index; | 
|---|
| 158 | for (size_t i=0;i<((size_t)floor(NUMBERCELLS));++i) { | 
|---|
| 159 | index[0] = index[1] = index[2] = i; | 
|---|
| 160 | // assert that in the destined cell we have one Walker | 
|---|
| 161 | CPPUNIT_ASSERT_EQUAL((size_t)1, LC->N->getN()(index)->size()); | 
|---|
| 162 | } | 
|---|
| 163 | index[0] = 9; | 
|---|
| 164 | index[1] = index[2] = 0; | 
|---|
| 165 | // assert that in the destined cell we have one Walker | 
|---|
| 166 | CPPUNIT_ASSERT_EQUAL((size_t)0, LC->N->getN()(index)->size()); | 
|---|
| 167 |  | 
|---|
| 168 | } | 
|---|
| 169 |  | 
|---|
| 170 | /** UnitTest for setPartition() | 
|---|
| 171 | */ | 
|---|
| 172 | void LinkedCell_ModelTest::setPartitionTest() | 
|---|
| 173 | { | 
|---|
| 174 | RealSpaceMatrix Pmatrix = LC->Partition; | 
|---|
| 175 | RealSpaceMatrix Dmatrix = LC->Dimensions; | 
|---|
| 176 |  | 
|---|
| 177 | LC->Reset(); | 
|---|
| 178 |  | 
|---|
| 179 | LC->setPartition(2.*EDGELENGTH); | 
|---|
| 180 |  | 
|---|
| 181 | Pmatrix *= 0.5; | 
|---|
| 182 | Dmatrix *= 0.5; | 
|---|
| 183 |  | 
|---|
| 184 | CPPUNIT_ASSERT_EQUAL(Pmatrix, LC->Partition); | 
|---|
| 185 | CPPUNIT_ASSERT_EQUAL(Dmatrix, LC->Dimensions); | 
|---|
| 186 | } | 
|---|
| 187 |  | 
|---|
| 188 | /** UnitTest for getStep() | 
|---|
| 189 | */ | 
|---|
| 190 | void LinkedCell_ModelTest::getStepTest() | 
|---|
| 191 | { | 
|---|
| 192 | // zero distance | 
|---|
| 193 | LinkedCell::tripleIndex index = LC->getStep(0.); | 
|---|
| 194 | for (size_t i = 0; i<NDIM; ++i) | 
|---|
| 195 | CPPUNIT_ASSERT( (size_t)0 == index[i]); | 
|---|
| 196 | // check all possible shells on boundary | 
|---|
| 197 | for (double length = EDGELENGTH; length < DOMAINLENGTH; length+=EDGELENGTH) { | 
|---|
| 198 | LinkedCell::tripleIndex index = LC->getStep(length); | 
|---|
| 199 | for (size_t i = 0; i<NDIM; ++i) { | 
|---|
| 200 | std::cout << (size_t)(length/EDGELENGTH) << " ==" << index[i] << std::endl; | 
|---|
| 201 | CPPUNIT_ASSERT_EQUAL( (LinkedCell::LinkedCellArray::index)(length/EDGELENGTH), index[i]); | 
|---|
| 202 | } | 
|---|
| 203 | } | 
|---|
| 204 | // check all possible shells at half interval | 
|---|
| 205 | for (double length = 0.5 * EDGELENGTH; length < DOMAINLENGTH; length+=EDGELENGTH) { | 
|---|
| 206 | LinkedCell::tripleIndex index = LC->getStep(length); | 
|---|
| 207 | for (size_t i = 0; i<NDIM; ++i) | 
|---|
| 208 | CPPUNIT_ASSERT_EQUAL( (LinkedCell::LinkedCellArray::index)ceil(length/EDGELENGTH), index[i]); | 
|---|
| 209 | } | 
|---|
| 210 | } | 
|---|
| 211 |  | 
|---|
| 212 | /** UnitTest for getIndexToVector() | 
|---|
| 213 | */ | 
|---|
| 214 | void LinkedCell_ModelTest::getIndexToVectorTest() | 
|---|
| 215 | { | 
|---|
| 216 | { | 
|---|
| 217 | const Vector test(0.,0.,0.); | 
|---|
| 218 | const LinkedCell::tripleIndex index = LC->getIndexToVector(test); | 
|---|
| 219 | for (size_t i = 0; i<NDIM; ++i) | 
|---|
| 220 | CPPUNIT_ASSERT( (size_t)0 == index[i]); | 
|---|
| 221 | } | 
|---|
| 222 | { | 
|---|
| 223 | const Vector test(DOMAINLENGTH/2.,DOMAINLENGTH/2.,DOMAINLENGTH/2.); | 
|---|
| 224 | const LinkedCell::tripleIndex index = LC->getIndexToVector(test); | 
|---|
| 225 | for (size_t i = 0; i<NDIM; ++i) | 
|---|
| 226 | CPPUNIT_ASSERT_EQUAL( (LinkedCell::LinkedCellArray::index)floor(DOMAINLENGTH/EDGELENGTH/2.), index[i]); | 
|---|
| 227 | } | 
|---|
| 228 | { | 
|---|
| 229 | const Vector test(DOMAINLENGTH/2.,DOMAINLENGTH/3.,DOMAINLENGTH/4.); | 
|---|
| 230 | const LinkedCell::tripleIndex index = LC->getIndexToVector(test); | 
|---|
| 231 | for (size_t i = 0; i<NDIM; ++i) | 
|---|
| 232 | CPPUNIT_ASSERT_EQUAL( (LinkedCell::LinkedCellArray::index)floor(DOMAINLENGTH/EDGELENGTH/(double)(i+2.)), index[i]); | 
|---|
| 233 | } | 
|---|
| 234 | } | 
|---|
| 235 |  | 
|---|
| 236 | /** UnitTest for updating nodes | 
|---|
| 237 | */ | 
|---|
| 238 | void LinkedCell_ModelTest::nodeTest() | 
|---|
| 239 | { | 
|---|
| 240 | // create point | 
|---|
| 241 | TesselPoint * Walker = new TesselPoint(); | 
|---|
| 242 | Walker->setName(std::string("Walker9")); | 
|---|
| 243 | Walker->setPosition(Vector(9.8,7.6,5.4)); | 
|---|
| 244 | PointCloudAdaptor< PointSet > cloud(&NodeList, std::string("NodeList")); | 
|---|
| 245 | LC->insertPointCloud(cloud); | 
|---|
| 246 |  | 
|---|
| 247 | // check addNode | 
|---|
| 248 | { | 
|---|
| 249 | LC->addNode(Walker); | 
|---|
| 250 | // assure that we are updated before accessing internals | 
|---|
| 251 | CPPUNIT_ASSERT_EQUAL( true, *(LC->N->UpToDate) ); | 
|---|
| 252 | CPPUNIT_ASSERT_EQUAL( NodeList.size()+1, LC->CellLookup.size()); | 
|---|
| 253 | LinkedCell::tripleIndex index1 = LC->getIndexToVector(Walker->getPosition()); | 
|---|
| 254 | const LinkedCell::tripleIndex &index2 = LC->CellLookup[Walker]->getIndices(); | 
|---|
| 255 | CPPUNIT_ASSERT(index1 == index2); | 
|---|
| 256 | } | 
|---|
| 257 |  | 
|---|
| 258 | // check moveNode | 
|---|
| 259 | { | 
|---|
| 260 | LinkedCell::tripleIndex index1 = LC->getIndexToVector(Walker->getPosition()); | 
|---|
| 261 | const LinkedCell::tripleIndex &index2 = LC->CellLookup[Walker]->getIndices(); | 
|---|
| 262 | Walker->setPosition(Vector(0.,0.,0.)); | 
|---|
| 263 | LinkedCell::tripleIndex newindex1 = LC->getIndexToVector(Walker->getPosition()); | 
|---|
| 264 | CPPUNIT_ASSERT( index1 != newindex1); | 
|---|
| 265 | // we have to call moveNode ourselves, as we have just added TesselPoints, not via World | 
|---|
| 266 | LC->moveNode(Walker); | 
|---|
| 267 | // assure that we are updated before accessing internals | 
|---|
| 268 | CPPUNIT_ASSERT_EQUAL( true, *(LC->N->UpToDate) ); | 
|---|
| 269 | const LinkedCell::tripleIndex &newindex2 = LC->CellLookup[Walker]->getIndices(); | 
|---|
| 270 | CPPUNIT_ASSERT( index2 != newindex2); | 
|---|
| 271 | } | 
|---|
| 272 |  | 
|---|
| 273 | // check deleteNode | 
|---|
| 274 | { | 
|---|
| 275 | LC->deleteNode(Walker); | 
|---|
| 276 | // assure that we are updated before accessing internals | 
|---|
| 277 | CPPUNIT_ASSERT_EQUAL( true, *(LC->N->UpToDate) ); | 
|---|
| 278 | CPPUNIT_ASSERT_EQUAL( NodeList.size(), LC->CellLookup.size()); | 
|---|
| 279 | } | 
|---|
| 280 |  | 
|---|
| 281 | delete Walker; | 
|---|
| 282 | } | 
|---|
| 283 |  | 
|---|
| 284 | /** UnitTest whether lazy updates are working. | 
|---|
| 285 | */ | 
|---|
| 286 | void LinkedCell_ModelTest::lazyUpdatesTest() | 
|---|
| 287 | { | 
|---|
| 288 | // create point | 
|---|
| 289 | TesselPoint * Walker = new TesselPoint(); | 
|---|
| 290 | Walker->setName(std::string("Walker9")); | 
|---|
| 291 | Walker->setPosition(Vector(9.8,7.6,5.4)); | 
|---|
| 292 | TesselPoint * Walker2 = new TesselPoint(); | 
|---|
| 293 | Walker2->setName(std::string("Walker10")); | 
|---|
| 294 | Walker2->setPosition(Vector(9.8,7.6,5.4)); | 
|---|
| 295 | PointCloudAdaptor< PointSet > cloud(&NodeList, std::string("NodeList")); | 
|---|
| 296 | LC->insertPointCloud(cloud); | 
|---|
| 297 |  | 
|---|
| 298 | // initial read operation | 
|---|
| 299 | { | 
|---|
| 300 | CPPUNIT_ASSERT( !NodeList.empty() ); | 
|---|
| 301 | LinkedCell::tripleIndex index = LC->getIndexToVector((*NodeList.begin())->getPosition()); | 
|---|
| 302 | const size_t size = LC->N->getN()(index)->size(); // this will cause the update | 
|---|
| 303 | CPPUNIT_ASSERT( size >= (size_t)1 ); | 
|---|
| 304 | } | 
|---|
| 305 |  | 
|---|
| 306 | // do some write ops | 
|---|
| 307 | LC->addNode(Walker); | 
|---|
| 308 | LC->addNode(Walker2); | 
|---|
| 309 | CPPUNIT_ASSERT( LC->Changes->queue.find(Walker) != LC->Changes->queue.end() ); | 
|---|
| 310 | LinkedCell::LinkedCell_Model::Update *update = LC->Changes->queue[Walker]; | 
|---|
| 311 | Walker->setPosition(Vector(0.,0.,0.)); | 
|---|
| 312 | LC->moveNode(Walker); | 
|---|
| 313 |  | 
|---|
| 314 | // check that they all reside in cache and nothing is changed so far | 
|---|
| 315 | CPPUNIT_ASSERT( LC->Changes->queue.size() > (size_t)0 ); | 
|---|
| 316 |  | 
|---|
| 317 | // check that add priority is prefered over move | 
|---|
| 318 | CPPUNIT_ASSERT( LC->Changes->queue[Walker] == update ); | 
|---|
| 319 |  | 
|---|
| 320 | // perform read op | 
|---|
| 321 | { | 
|---|
| 322 | LinkedCell::tripleIndex index = LC->getIndexToVector(Walker->getPosition()); | 
|---|
| 323 | CPPUNIT_ASSERT( LC->N->getN()(index)->size() >= (size_t)1 ); | 
|---|
| 324 | } | 
|---|
| 325 |  | 
|---|
| 326 | // check that cache is emptied | 
|---|
| 327 | CPPUNIT_ASSERT_EQUAL( LC->Changes->queue.size(), (size_t)0 ); | 
|---|
| 328 |  | 
|---|
| 329 | delete Walker; | 
|---|
| 330 | delete Walker2; | 
|---|
| 331 | } | 
|---|