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