| 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 |   for (size_t i=0;i<VectorList.size();++i) {
 | 
|---|
| 77 |     TesselPoint * Walker = new TesselPoint();
 | 
|---|
| 78 |     Walker->setName(std::string("Walker")+toString(i));
 | 
|---|
| 79 |     Walker->setPosition(VectorList[i]);
 | 
|---|
| 80 |     NodeList.insert(Walker);
 | 
|---|
| 81 |   }
 | 
|---|
| 82 | }
 | 
|---|
| 83 | 
 | 
|---|
| 84 | 
 | 
|---|
| 85 | void LinkedCell_ModelTest::tearDown()
 | 
|---|
| 86 | {
 | 
|---|
| 87 |   delete LC;
 | 
|---|
| 88 |   delete domain;
 | 
|---|
| 89 | 
 | 
|---|
| 90 |   // remove all nodes again
 | 
|---|
| 91 |   for (PointSet::iterator iter = NodeList.begin();
 | 
|---|
| 92 |       !NodeList.empty();
 | 
|---|
| 93 |       iter = NodeList.begin()) {
 | 
|---|
| 94 |     delete *iter;
 | 
|---|
| 95 |     NodeList.erase(iter);
 | 
|---|
| 96 |   }
 | 
|---|
| 97 | 
 | 
|---|
| 98 |   // delete in correct order
 | 
|---|
| 99 |   World::purgeInstance();
 | 
|---|
| 100 |   WorldTime::purgeInstance();
 | 
|---|
| 101 | }
 | 
|---|
| 102 | 
 | 
|---|
| 103 | /** UnitTest for correct construction
 | 
|---|
| 104 |  */
 | 
|---|
| 105 | void LinkedCell_ModelTest::AllocationTest()
 | 
|---|
| 106 | {
 | 
|---|
| 107 |   // check that first cell is allocated
 | 
|---|
| 108 |   LinkedCell::tripleIndex index;
 | 
|---|
| 109 |   index[0] = index[1] = index[2] = 0;
 | 
|---|
| 110 |   CPPUNIT_ASSERT(LC->N->getN()(index) != NULL);
 | 
|---|
| 111 | 
 | 
|---|
| 112 |   // check that very last cell is allocated
 | 
|---|
| 113 |   index[0] = index[1] = index[2] = (size_t)floor(NUMBERCELLS)-1;
 | 
|---|
| 114 |   CPPUNIT_ASSERT(LC->N->getN()(index) != NULL);
 | 
|---|
| 115 | 
 | 
|---|
| 116 | }
 | 
|---|
| 117 | 
 | 
|---|
| 118 | /** UnitTest for getSize()
 | 
|---|
| 119 |  */
 | 
|---|
| 120 | void LinkedCell_ModelTest::getSizeTest()
 | 
|---|
| 121 | {
 | 
|---|
| 122 |   // check getSize()
 | 
|---|
| 123 |   for(size_t i=0; i<NDIM; ++i)
 | 
|---|
| 124 |     CPPUNIT_ASSERT_EQUAL((LinkedCell::LinkedCellArray::index)floor(NUMBERCELLS), LC->getSize(i));
 | 
|---|
| 125 | #ifndef NDEBUG
 | 
|---|
| 126 |   std::cout << "The following assertion is intended and is not a failure of the code." << std::endl;
 | 
|---|
| 127 |   CPPUNIT_ASSERT_THROW( LC->getSize(4), Assert::AssertionFailure);
 | 
|---|
| 128 | #endif
 | 
|---|
| 129 | }
 | 
|---|
| 130 | 
 | 
|---|
| 131 | /** UnitTest for Reset()
 | 
|---|
| 132 |  */
 | 
|---|
| 133 | void LinkedCell_ModelTest::ResetTest()
 | 
|---|
| 134 | {
 | 
|---|
| 135 |   LC->Reset();
 | 
|---|
| 136 | 
 | 
|---|
| 137 |   for(size_t i=0; i<NDIM; ++i)
 | 
|---|
| 138 |     CPPUNIT_ASSERT_EQUAL((LinkedCell::LinkedCellArray::index)0, LC->getSize(i));
 | 
|---|
| 139 | }
 | 
|---|
| 140 | 
 | 
|---|
| 141 | 
 | 
|---|
| 142 | /** UnitTest for insertPointCloud()
 | 
|---|
| 143 |  */
 | 
|---|
| 144 | void LinkedCell_ModelTest::insertPointCloudTest()
 | 
|---|
| 145 | {
 | 
|---|
| 146 | 
 | 
|---|
| 147 |   // create the linked cell structure
 | 
|---|
| 148 |   PointCloudAdaptor< PointSet > cloud(&NodeList, std::string("NodeList"));
 | 
|---|
| 149 |   LC->insertPointCloud(cloud);
 | 
|---|
| 150 | 
 | 
|---|
| 151 |   // assure that we are updated before accessing internals
 | 
|---|
| 152 |   CPPUNIT_ASSERT_EQUAL( true, *(LC->N->UpToDate) );
 | 
|---|
| 153 |   // test structure
 | 
|---|
| 154 |   CPPUNIT_ASSERT_EQUAL(((size_t)floor(NUMBERCELLS)), LC->CellLookup.size());
 | 
|---|
| 155 |   LinkedCell::tripleIndex index;
 | 
|---|
| 156 |   for (size_t i=0;i<((size_t)floor(NUMBERCELLS));++i) {
 | 
|---|
| 157 |     index[0] = index[1] = index[2] = i;
 | 
|---|
| 158 |     // assert that in the destined cell we have one Walker
 | 
|---|
| 159 |     CPPUNIT_ASSERT_EQUAL((size_t)1, LC->N->getN()(index)->size());
 | 
|---|
| 160 |   }
 | 
|---|
| 161 |   index[0] = 9;
 | 
|---|
| 162 |   index[1] = index[2] = 0;
 | 
|---|
| 163 |   // assert that in the destined cell we have one Walker
 | 
|---|
| 164 |   CPPUNIT_ASSERT_EQUAL((size_t)0, LC->N->getN()(index)->size());
 | 
|---|
| 165 | 
 | 
|---|
| 166 | }
 | 
|---|
| 167 | 
 | 
|---|
| 168 | /** UnitTest for setPartition()
 | 
|---|
| 169 |  */
 | 
|---|
| 170 | void LinkedCell_ModelTest::setPartitionTest()
 | 
|---|
| 171 | {
 | 
|---|
| 172 |   RealSpaceMatrix Pmatrix = LC->Partition;
 | 
|---|
| 173 |   RealSpaceMatrix Dmatrix = LC->Dimensions;
 | 
|---|
| 174 | 
 | 
|---|
| 175 |   LC->Reset();
 | 
|---|
| 176 | 
 | 
|---|
| 177 |   LC->setPartition(2.*EDGELENGTH);
 | 
|---|
| 178 | 
 | 
|---|
| 179 |   Pmatrix *= 0.5;
 | 
|---|
| 180 |   Dmatrix *= 0.5;
 | 
|---|
| 181 | 
 | 
|---|
| 182 |   CPPUNIT_ASSERT_EQUAL(Pmatrix, LC->Partition);
 | 
|---|
| 183 |   CPPUNIT_ASSERT_EQUAL(Dmatrix, LC->Dimensions);
 | 
|---|
| 184 | }
 | 
|---|
| 185 | 
 | 
|---|
| 186 | /** UnitTest for getStep()
 | 
|---|
| 187 |  */
 | 
|---|
| 188 | void LinkedCell_ModelTest::getStepTest()
 | 
|---|
| 189 | {
 | 
|---|
| 190 |   // zero distance
 | 
|---|
| 191 |   LinkedCell::tripleIndex index = LC->getStep(0.);
 | 
|---|
| 192 |   for (size_t i = 0; i<NDIM; ++i)
 | 
|---|
| 193 |     CPPUNIT_ASSERT( (size_t)0 == index[i]);
 | 
|---|
| 194 |   // check all possible shells on boundary
 | 
|---|
| 195 |   for (double length = EDGELENGTH; length < DOMAINLENGTH; length+=EDGELENGTH) {
 | 
|---|
| 196 |     LinkedCell::tripleIndex index = LC->getStep(length);
 | 
|---|
| 197 |     for (size_t i = 0; i<NDIM; ++i) {
 | 
|---|
| 198 |       std::cout << (size_t)(length/EDGELENGTH) << " ==" << index[i] << std::endl;
 | 
|---|
| 199 |       CPPUNIT_ASSERT( (LinkedCell::LinkedCellArray::index)(length/EDGELENGTH) == index[i]);
 | 
|---|
| 200 |     }
 | 
|---|
| 201 |   }
 | 
|---|
| 202 |   // check all possible shells at half interval
 | 
|---|
| 203 |   for (double length = 0.5 * EDGELENGTH; length < DOMAINLENGTH; length+=EDGELENGTH) {
 | 
|---|
| 204 |     LinkedCell::tripleIndex index = LC->getStep(length);
 | 
|---|
| 205 |     for (size_t i = 0; i<NDIM; ++i)
 | 
|---|
| 206 |       CPPUNIT_ASSERT( (LinkedCell::LinkedCellArray::index)ceil(length/EDGELENGTH) == index[i]);
 | 
|---|
| 207 |   }
 | 
|---|
| 208 | }
 | 
|---|
| 209 | 
 | 
|---|
| 210 | /** UnitTest for getIndexToVector()
 | 
|---|
| 211 |  */
 | 
|---|
| 212 | void LinkedCell_ModelTest::getIndexToVectorTest()
 | 
|---|
| 213 | {
 | 
|---|
| 214 |   {
 | 
|---|
| 215 |     const Vector test(0.,0.,0.);
 | 
|---|
| 216 |     const LinkedCell::tripleIndex index = LC->getIndexToVector(test);
 | 
|---|
| 217 |     for (size_t i = 0; i<NDIM; ++i)
 | 
|---|
| 218 |       CPPUNIT_ASSERT( (size_t)0 == index[i]);
 | 
|---|
| 219 |   }
 | 
|---|
| 220 |   {
 | 
|---|
| 221 |     const Vector test(DOMAINLENGTH/2.,DOMAINLENGTH/2.,DOMAINLENGTH/2.);
 | 
|---|
| 222 |     const LinkedCell::tripleIndex index = LC->getIndexToVector(test);
 | 
|---|
| 223 |     for (size_t i = 0; i<NDIM; ++i)
 | 
|---|
| 224 |       CPPUNIT_ASSERT( (size_t)floor(DOMAINLENGTH/EDGELENGTH/2.) == index[i]);
 | 
|---|
| 225 |   }
 | 
|---|
| 226 |   {
 | 
|---|
| 227 |     const Vector test(DOMAINLENGTH/2.,DOMAINLENGTH/3.,DOMAINLENGTH/4.);
 | 
|---|
| 228 |     const LinkedCell::tripleIndex index = LC->getIndexToVector(test);
 | 
|---|
| 229 |     for (size_t i = 0; i<NDIM; ++i)
 | 
|---|
| 230 |       CPPUNIT_ASSERT( (LinkedCell::LinkedCellArray::index)floor(DOMAINLENGTH/EDGELENGTH/(double)(i+2.)) == index[i]);
 | 
|---|
| 231 |   }
 | 
|---|
| 232 | }
 | 
|---|
| 233 | 
 | 
|---|
| 234 | /** UnitTest for updating nodes
 | 
|---|
| 235 |  */
 | 
|---|
| 236 | void LinkedCell_ModelTest::nodeTest()
 | 
|---|
| 237 | {
 | 
|---|
| 238 |   // create point
 | 
|---|
| 239 |   TesselPoint * Walker = new TesselPoint();
 | 
|---|
| 240 |   Walker->setName(std::string("Walker9"));
 | 
|---|
| 241 |   Walker->setPosition(Vector(9.8,7.6,5.4));
 | 
|---|
| 242 |   PointCloudAdaptor< PointSet > cloud(&NodeList, std::string("NodeList"));
 | 
|---|
| 243 |   LC->insertPointCloud(cloud);
 | 
|---|
| 244 | 
 | 
|---|
| 245 |   // check addNode
 | 
|---|
| 246 |   {
 | 
|---|
| 247 |     LC->addNode(Walker);
 | 
|---|
| 248 |     // assure that we are updated before accessing internals
 | 
|---|
| 249 |     CPPUNIT_ASSERT_EQUAL( true, *(LC->N->UpToDate) );
 | 
|---|
| 250 |     CPPUNIT_ASSERT_EQUAL( NodeList.size()+1, LC->CellLookup.size());
 | 
|---|
| 251 |     LinkedCell::tripleIndex index1 = LC->getIndexToVector(Walker->getPosition());
 | 
|---|
| 252 |     const LinkedCell::tripleIndex &index2 = LC->CellLookup[Walker]->getIndices();
 | 
|---|
| 253 |     CPPUNIT_ASSERT(index1 == index2);
 | 
|---|
| 254 |   }
 | 
|---|
| 255 | 
 | 
|---|
| 256 |   // check moveNode
 | 
|---|
| 257 |   {
 | 
|---|
| 258 |     LinkedCell::tripleIndex index1 = LC->getIndexToVector(Walker->getPosition());
 | 
|---|
| 259 |     const LinkedCell::tripleIndex &index2 = LC->CellLookup[Walker]->getIndices();
 | 
|---|
| 260 |     Walker->setPosition(Vector(0.,0.,0.));
 | 
|---|
| 261 |     LinkedCell::tripleIndex newindex1 = LC->getIndexToVector(Walker->getPosition());
 | 
|---|
| 262 |     CPPUNIT_ASSERT( index1 != newindex1);
 | 
|---|
| 263 |     // we have to call moveNode ourselves, as we have just added TesselPoints, not via World
 | 
|---|
| 264 |     LC->moveNode(Walker);
 | 
|---|
| 265 |     // assure that we are updated before accessing internals
 | 
|---|
| 266 |     CPPUNIT_ASSERT_EQUAL( true, *(LC->N->UpToDate) );
 | 
|---|
| 267 |     const LinkedCell::tripleIndex &newindex2 = LC->CellLookup[Walker]->getIndices();
 | 
|---|
| 268 |     CPPUNIT_ASSERT( index2 != newindex2);
 | 
|---|
| 269 |   }
 | 
|---|
| 270 | 
 | 
|---|
| 271 |   // check deleteNode
 | 
|---|
| 272 |   {
 | 
|---|
| 273 |     LC->deleteNode(Walker);
 | 
|---|
| 274 |     // assure that we are updated before accessing internals
 | 
|---|
| 275 |     CPPUNIT_ASSERT_EQUAL( true, *(LC->N->UpToDate) );
 | 
|---|
| 276 |     CPPUNIT_ASSERT_EQUAL( NodeList.size(), LC->CellLookup.size());
 | 
|---|
| 277 |   }
 | 
|---|
| 278 | 
 | 
|---|
| 279 |   delete Walker;
 | 
|---|
| 280 | }
 | 
|---|
| 281 | 
 | 
|---|
| 282 | /** UnitTest whether lazy updates are working.
 | 
|---|
| 283 |  */
 | 
|---|
| 284 | void LinkedCell_ModelTest::lazyUpdatesTest()
 | 
|---|
| 285 | {
 | 
|---|
| 286 |   // create point
 | 
|---|
| 287 |   TesselPoint * Walker = new TesselPoint();
 | 
|---|
| 288 |   Walker->setName(std::string("Walker9"));
 | 
|---|
| 289 |   Walker->setPosition(Vector(9.8,7.6,5.4));
 | 
|---|
| 290 |   TesselPoint * Walker2 = new TesselPoint();
 | 
|---|
| 291 |   Walker2->setName(std::string("Walker10"));
 | 
|---|
| 292 |   Walker2->setPosition(Vector(9.8,7.6,5.4));
 | 
|---|
| 293 |   PointCloudAdaptor< PointSet > cloud(&NodeList, std::string("NodeList"));
 | 
|---|
| 294 |   LC->insertPointCloud(cloud);
 | 
|---|
| 295 | 
 | 
|---|
| 296 |   // initial read operation
 | 
|---|
| 297 |   {
 | 
|---|
| 298 |     CPPUNIT_ASSERT( !NodeList.empty() );
 | 
|---|
| 299 |     LinkedCell::tripleIndex index = LC->getIndexToVector((*NodeList.begin())->getPosition());
 | 
|---|
| 300 |     const size_t size = LC->N->getN()(index)->size(); // this will cause the update
 | 
|---|
| 301 |     CPPUNIT_ASSERT( size >= (size_t)1 );
 | 
|---|
| 302 |   }
 | 
|---|
| 303 | 
 | 
|---|
| 304 |   // do some write ops
 | 
|---|
| 305 |   LC->addNode(Walker);
 | 
|---|
| 306 |   LC->addNode(Walker2);
 | 
|---|
| 307 |   CPPUNIT_ASSERT( LC->Changes->queue.find(Walker) != LC->Changes->queue.end() );
 | 
|---|
| 308 |   LinkedCell::LinkedCell_Model::Update *update = LC->Changes->queue[Walker];
 | 
|---|
| 309 |   Walker->setPosition(Vector(0.,0.,0.));
 | 
|---|
| 310 |   LC->moveNode(Walker);
 | 
|---|
| 311 | 
 | 
|---|
| 312 |   // check that they all reside in cache and nothing is changed so far
 | 
|---|
| 313 |   CPPUNIT_ASSERT( LC->Changes->queue.size() > (size_t)0 );
 | 
|---|
| 314 | 
 | 
|---|
| 315 |   // check that add priority is prefered over move
 | 
|---|
| 316 |   CPPUNIT_ASSERT( LC->Changes->queue[Walker] == update );
 | 
|---|
| 317 | 
 | 
|---|
| 318 |   // perform read op
 | 
|---|
| 319 |   {
 | 
|---|
| 320 |     LinkedCell::tripleIndex index = LC->getIndexToVector(Walker->getPosition());
 | 
|---|
| 321 |     CPPUNIT_ASSERT( LC->N->getN()(index)->size() >= (size_t)1 );
 | 
|---|
| 322 |   }
 | 
|---|
| 323 | 
 | 
|---|
| 324 |   // check that cache is emptied
 | 
|---|
| 325 |   CPPUNIT_ASSERT( LC->Changes->queue.size() == (size_t)0 );
 | 
|---|
| 326 | 
 | 
|---|
| 327 |   delete Walker;
 | 
|---|
| 328 |   delete Walker2;
 | 
|---|
| 329 | }
 | 
|---|