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