| 1 | /*
 | 
|---|
| 2 |  * Project: MoleCuilder
 | 
|---|
| 3 |  * Description: creates and alters molecular systems
 | 
|---|
| 4 |  * Copyright (C)  2011 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/PointCloudAdaptor.hpp"
 | 
|---|
| 35 | #include "LinkedCell/unittests/defs.hpp"
 | 
|---|
| 36 | 
 | 
|---|
| 37 | #include "LinkedCell_ModelUnitTest.hpp"
 | 
|---|
| 38 | 
 | 
|---|
| 39 | #ifdef HAVE_TESTRUNNER
 | 
|---|
| 40 | #include "UnitTestMain.hpp"
 | 
|---|
| 41 | #endif /*HAVE_TESTRUNNER*/
 | 
|---|
| 42 | 
 | 
|---|
| 43 | /********************************************** Test classes **************************************/
 | 
|---|
| 44 | 
 | 
|---|
| 45 | // Registers the fixture into the 'registry'
 | 
|---|
| 46 | CPPUNIT_TEST_SUITE_REGISTRATION( LinkedCell_ModelTest );
 | 
|---|
| 47 | 
 | 
|---|
| 48 | 
 | 
|---|
| 49 | void LinkedCell_ModelTest::setUp()
 | 
|---|
| 50 | {
 | 
|---|
| 51 |   // failing asserts should be thrown
 | 
|---|
| 52 |   ASSERT_DO(Assert::Throw);
 | 
|---|
| 53 | 
 | 
|---|
| 54 |   setVerbosity(3);
 | 
|---|
| 55 | 
 | 
|---|
| 56 |   // create diag(20.) matrix
 | 
|---|
| 57 |   RealSpaceMatrix BoxM;
 | 
|---|
| 58 |   BoxM.setIdentity();
 | 
|---|
| 59 |   BoxM *= DOMAINLENGTH;
 | 
|---|
| 60 | 
 | 
|---|
| 61 |   // create Box with this matrix
 | 
|---|
| 62 |   domain = new Box(BoxM);
 | 
|---|
| 63 | 
 | 
|---|
| 64 |   // create LinkedCell structure with this Box
 | 
|---|
| 65 |   LC = new LinkedCell::LinkedCell_Model(EDGELENGTH, *domain);
 | 
|---|
| 66 | 
 | 
|---|
| 67 |   // create a list of nodes and add to LCImpl
 | 
|---|
| 68 |   std::vector< Vector > VectorList;
 | 
|---|
| 69 |   for (size_t i=0;i<((size_t)floor(NUMBERCELLS));++i)
 | 
|---|
| 70 |     VectorList.push_back(Vector((double)i*EDGELENGTH,(double)i*EDGELENGTH,(double)i*EDGELENGTH));
 | 
|---|
| 71 |   for (size_t i=0;i<VectorList.size();++i) {
 | 
|---|
| 72 |     TesselPoint * Walker = new TesselPoint();
 | 
|---|
| 73 |     Walker->setName(std::string("Walker")+toString(i));
 | 
|---|
| 74 |     Walker->setPosition(VectorList[i]);
 | 
|---|
| 75 |     NodeList.insert(Walker);
 | 
|---|
| 76 |   }
 | 
|---|
| 77 | }
 | 
|---|
| 78 | 
 | 
|---|
| 79 | 
 | 
|---|
| 80 | void LinkedCell_ModelTest::tearDown()
 | 
|---|
| 81 | {
 | 
|---|
| 82 |   delete LC;
 | 
|---|
| 83 |   delete domain;
 | 
|---|
| 84 | 
 | 
|---|
| 85 |   // remove all nodes again
 | 
|---|
| 86 |   for (PointSet::iterator iter = NodeList.begin();
 | 
|---|
| 87 |       !NodeList.empty();
 | 
|---|
| 88 |       iter = NodeList.begin()) {
 | 
|---|
| 89 |     delete *iter;
 | 
|---|
| 90 |     NodeList.erase(iter);
 | 
|---|
| 91 |   }
 | 
|---|
| 92 | }
 | 
|---|
| 93 | 
 | 
|---|
| 94 | /** UnitTest for correct construction
 | 
|---|
| 95 |  */
 | 
|---|
| 96 | void LinkedCell_ModelTest::AllocationTest()
 | 
|---|
| 97 | {
 | 
|---|
| 98 |   // check that first cell is allocated
 | 
|---|
| 99 |   LinkedCell::tripleIndex index;
 | 
|---|
| 100 |   index[0] = index[1] = index[2] = 0;
 | 
|---|
| 101 |   CPPUNIT_ASSERT(LC->N(index) != NULL);
 | 
|---|
| 102 | 
 | 
|---|
| 103 |   // check that very last cell is allocated
 | 
|---|
| 104 |   index[0] = index[1] = index[2] = (size_t)floor(NUMBERCELLS)-1;
 | 
|---|
| 105 |   CPPUNIT_ASSERT(LC->N(index) != NULL);
 | 
|---|
| 106 | 
 | 
|---|
| 107 | }
 | 
|---|
| 108 | 
 | 
|---|
| 109 | /** UnitTest for getSize()
 | 
|---|
| 110 |  */
 | 
|---|
| 111 | void LinkedCell_ModelTest::getSizeTest()
 | 
|---|
| 112 | {
 | 
|---|
| 113 |   // check getSize()
 | 
|---|
| 114 |   for(size_t i=0; i<NDIM; ++i)
 | 
|---|
| 115 |     CPPUNIT_ASSERT_EQUAL((LinkedCell::LinkedCellArray::index)floor(NUMBERCELLS), LC->getSize(i));
 | 
|---|
| 116 | #ifndef NDEBUG
 | 
|---|
| 117 |   std::cout << "The following assertion is intended and is not a failure of the code." << std::endl;
 | 
|---|
| 118 |   CPPUNIT_ASSERT_THROW( LC->getSize(4), Assert::AssertionFailure);
 | 
|---|
| 119 | #endif
 | 
|---|
| 120 | }
 | 
|---|
| 121 | 
 | 
|---|
| 122 | /** UnitTest for Reset()
 | 
|---|
| 123 |  */
 | 
|---|
| 124 | void LinkedCell_ModelTest::ResetTest()
 | 
|---|
| 125 | {
 | 
|---|
| 126 |   LC->Reset();
 | 
|---|
| 127 | 
 | 
|---|
| 128 |   for(size_t i=0; i<NDIM; ++i)
 | 
|---|
| 129 |     CPPUNIT_ASSERT_EQUAL((LinkedCell::LinkedCellArray::index)0, LC->getSize(i));
 | 
|---|
| 130 | }
 | 
|---|
| 131 | 
 | 
|---|
| 132 | 
 | 
|---|
| 133 | /** UnitTest for insertPointCloud()
 | 
|---|
| 134 |  */
 | 
|---|
| 135 | void LinkedCell_ModelTest::insertPointCloudTest()
 | 
|---|
| 136 | {
 | 
|---|
| 137 | 
 | 
|---|
| 138 |   // create the linked cell structure
 | 
|---|
| 139 |   PointCloudAdaptor< PointSet > cloud(&NodeList, std::string("NodeList"));
 | 
|---|
| 140 |   LC->insertPointCloud(cloud);
 | 
|---|
| 141 | 
 | 
|---|
| 142 |   // test structure
 | 
|---|
| 143 |   CPPUNIT_ASSERT_EQUAL(((size_t)floor(NUMBERCELLS)), LC->CellLookup.size());
 | 
|---|
| 144 |   LinkedCell::tripleIndex index;
 | 
|---|
| 145 |   for (size_t i=0;i<((size_t)floor(NUMBERCELLS));++i) {
 | 
|---|
| 146 |     index[0] = index[1] = index[2] = i;
 | 
|---|
| 147 |     // assert that in the destined cell we have one Walker
 | 
|---|
| 148 |     CPPUNIT_ASSERT_EQUAL((size_t)1, LC->N(index)->size());
 | 
|---|
| 149 |   }
 | 
|---|
| 150 |   index[0] = 9;
 | 
|---|
| 151 |   index[1] = index[2] = 0;
 | 
|---|
| 152 |   // assert that in the destined cell we have one Walker
 | 
|---|
| 153 |   CPPUNIT_ASSERT_EQUAL((size_t)0, LC->N(index)->size());
 | 
|---|
| 154 | 
 | 
|---|
| 155 | }
 | 
|---|
| 156 | 
 | 
|---|
| 157 | /** UnitTest for insertPointCloud()
 | 
|---|
| 158 |  */
 | 
|---|
| 159 | void LinkedCell_ModelTest::setPartitionTest()
 | 
|---|
| 160 | {
 | 
|---|
| 161 |   RealSpaceMatrix Pmatrix = LC->Partition;
 | 
|---|
| 162 |   RealSpaceMatrix Dmatrix = LC->Dimensions;
 | 
|---|
| 163 | 
 | 
|---|
| 164 |   LC->Reset();
 | 
|---|
| 165 | 
 | 
|---|
| 166 |   LC->setPartition(2.*EDGELENGTH);
 | 
|---|
| 167 | 
 | 
|---|
| 168 |   Pmatrix *= 0.5;
 | 
|---|
| 169 |   Dmatrix *= 0.5;
 | 
|---|
| 170 | 
 | 
|---|
| 171 |   CPPUNIT_ASSERT_EQUAL(Pmatrix, LC->Partition);
 | 
|---|
| 172 |   CPPUNIT_ASSERT_EQUAL(Dmatrix, LC->Dimensions);
 | 
|---|
| 173 | }
 | 
|---|
| 174 | 
 | 
|---|
| 175 | /** UnitTest for insertPointCloud()
 | 
|---|
| 176 |  */
 | 
|---|
| 177 | void LinkedCell_ModelTest::getStepTest()
 | 
|---|
| 178 | {
 | 
|---|
| 179 |   // zero distance
 | 
|---|
| 180 |   LinkedCell::tripleIndex index = LC->getStep(0.);
 | 
|---|
| 181 |   for (size_t i = 0; i<NDIM; ++i)
 | 
|---|
| 182 |     CPPUNIT_ASSERT( (size_t)0 == index[i]);
 | 
|---|
| 183 |   // check all possible shells on boundary
 | 
|---|
| 184 |   for (double length = EDGELENGTH; length < DOMAINLENGTH; length+=EDGELENGTH) {
 | 
|---|
| 185 |     LinkedCell::tripleIndex index = LC->getStep(length);
 | 
|---|
| 186 |     for (size_t i = 0; i<NDIM; ++i) {
 | 
|---|
| 187 |       std::cout << (size_t)(length/EDGELENGTH) << " ==" << index[i] << std::endl;
 | 
|---|
| 188 |       CPPUNIT_ASSERT( (LinkedCell::LinkedCellArray::index)(length/EDGELENGTH) == index[i]);
 | 
|---|
| 189 |     }
 | 
|---|
| 190 |   }
 | 
|---|
| 191 |   // check all possible shells at half interval
 | 
|---|
| 192 |   for (double length = 0.5 * EDGELENGTH; length < DOMAINLENGTH; length+=EDGELENGTH) {
 | 
|---|
| 193 |     LinkedCell::tripleIndex index = LC->getStep(length);
 | 
|---|
| 194 |     for (size_t i = 0; i<NDIM; ++i)
 | 
|---|
| 195 |       CPPUNIT_ASSERT( (LinkedCell::LinkedCellArray::index)ceil(length/EDGELENGTH) == index[i]);
 | 
|---|
| 196 |   }
 | 
|---|
| 197 | }
 | 
|---|
| 198 | 
 | 
|---|
| 199 | /** UnitTest for insertPointCloud()
 | 
|---|
| 200 |  */
 | 
|---|
| 201 | void LinkedCell_ModelTest::getIndexToVectorTest()
 | 
|---|
| 202 | {
 | 
|---|
| 203 |   {
 | 
|---|
| 204 |     const Vector test(0.,0.,0.);
 | 
|---|
| 205 |     const LinkedCell::tripleIndex index = LC->getIndexToVector(test);
 | 
|---|
| 206 |     for (size_t i = 0; i<NDIM; ++i)
 | 
|---|
| 207 |       CPPUNIT_ASSERT( (size_t)0 == index[i]);
 | 
|---|
| 208 |   }
 | 
|---|
| 209 |   {
 | 
|---|
| 210 |     const Vector test(DOMAINLENGTH/2.,DOMAINLENGTH/2.,DOMAINLENGTH/2.);
 | 
|---|
| 211 |     const LinkedCell::tripleIndex index = LC->getIndexToVector(test);
 | 
|---|
| 212 |     for (size_t i = 0; i<NDIM; ++i)
 | 
|---|
| 213 |       CPPUNIT_ASSERT( (size_t)floor(DOMAINLENGTH/EDGELENGTH/2.) == index[i]);
 | 
|---|
| 214 |   }
 | 
|---|
| 215 |   {
 | 
|---|
| 216 |     const Vector test(DOMAINLENGTH/2.,DOMAINLENGTH/3.,DOMAINLENGTH/4.);
 | 
|---|
| 217 |     const LinkedCell::tripleIndex index = LC->getIndexToVector(test);
 | 
|---|
| 218 |     for (size_t i = 0; i<NDIM; ++i)
 | 
|---|
| 219 |       CPPUNIT_ASSERT( (LinkedCell::LinkedCellArray::index)floor(DOMAINLENGTH/EDGELENGTH/(double)(i+2.)) == index[i]);
 | 
|---|
| 220 |   }
 | 
|---|
| 221 | }
 | 
|---|
| 222 | 
 | 
|---|
| 223 | /** UnitTest for insertPointCloud()
 | 
|---|
| 224 |  */
 | 
|---|
| 225 | void LinkedCell_ModelTest::nodeTest()
 | 
|---|
| 226 | {
 | 
|---|
| 227 |   // create point
 | 
|---|
| 228 |   TesselPoint * Walker = new TesselPoint();
 | 
|---|
| 229 |   Walker->setName(std::string("Walker9"));
 | 
|---|
| 230 |   Walker->setPosition(Vector(9.8,7.6,5.4));
 | 
|---|
| 231 |   PointCloudAdaptor< PointSet > cloud(&NodeList, std::string("NodeList"));
 | 
|---|
| 232 |   LC->insertPointCloud(cloud);
 | 
|---|
| 233 | 
 | 
|---|
| 234 |   // check addNode
 | 
|---|
| 235 |   LC->addNode(Walker);
 | 
|---|
| 236 |   CPPUNIT_ASSERT_EQUAL( NodeList.size()+1, LC->CellLookup.size());
 | 
|---|
| 237 |   LinkedCell::tripleIndex index1 = LC->getIndexToVector(Walker->getPosition());
 | 
|---|
| 238 |   const LinkedCell::tripleIndex &index2 = LC->CellLookup[Walker]->getIndices();
 | 
|---|
| 239 |   CPPUNIT_ASSERT(index1 == index2);
 | 
|---|
| 240 | 
 | 
|---|
| 241 |   // check moveNode
 | 
|---|
| 242 | #ifndef NDEBUG
 | 
|---|
| 243 |   std::cout << "The following assertion is intended and is not a failure of the code." << std::endl;
 | 
|---|
| 244 |   CPPUNIT_ASSERT_THROW( LC->moveNode(Walker), Assert::AssertionFailure );
 | 
|---|
| 245 | #endif
 | 
|---|
| 246 | 
 | 
|---|
| 247 |   // check deleteNode
 | 
|---|
| 248 |   LC->deleteNode(Walker);
 | 
|---|
| 249 |   CPPUNIT_ASSERT_EQUAL( NodeList.size(), LC->CellLookup.size());
 | 
|---|
| 250 | 
 | 
|---|
| 251 |   delete Walker;
 | 
|---|
| 252 | }
 | 
|---|