| 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 |
|
|---|
| 36 | #include "LinkedCell_ModelUnitTest.hpp"
|
|---|
| 37 |
|
|---|
| 38 | #ifdef HAVE_TESTRUNNER
|
|---|
| 39 | #include "UnitTestMain.hpp"
|
|---|
| 40 | #endif /*HAVE_TESTRUNNER*/
|
|---|
| 41 |
|
|---|
| 42 | /********************************************** Test classes **************************************/
|
|---|
| 43 |
|
|---|
| 44 | #define DOMAINLENGTH 20.
|
|---|
| 45 | #define EDGELENGTH 2.
|
|---|
| 46 | #define NUMBERCELLS DOMAINLENGTH/EDGELENGTH
|
|---|
| 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(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(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 | // test structure
|
|---|
| 146 | CPPUNIT_ASSERT_EQUAL(((size_t)floor(NUMBERCELLS)), LC->CellLookup.size());
|
|---|
| 147 | LinkedCell::tripleIndex index;
|
|---|
| 148 | for (size_t i=0;i<((size_t)floor(NUMBERCELLS));++i) {
|
|---|
| 149 | index[0] = index[1] = index[2] = i;
|
|---|
| 150 | // assert that in the destined cell we have one Walker
|
|---|
| 151 | CPPUNIT_ASSERT_EQUAL((size_t)1, LC->N(index)->size());
|
|---|
| 152 | }
|
|---|
| 153 | index[0] = 9;
|
|---|
| 154 | index[1] = index[2] = 0;
|
|---|
| 155 | // assert that in the destined cell we have one Walker
|
|---|
| 156 | CPPUNIT_ASSERT_EQUAL((size_t)0, LC->N(index)->size());
|
|---|
| 157 |
|
|---|
| 158 | }
|
|---|
| 159 |
|
|---|
| 160 | /** UnitTest for insertPointCloud()
|
|---|
| 161 | */
|
|---|
| 162 | void LinkedCell_ModelTest::setPartitionTest()
|
|---|
| 163 | {
|
|---|
| 164 | RealSpaceMatrix Pmatrix = LC->Partition;
|
|---|
| 165 | RealSpaceMatrix Dmatrix = LC->Dimensions;
|
|---|
| 166 |
|
|---|
| 167 | LC->Reset();
|
|---|
| 168 |
|
|---|
| 169 | LC->setPartition(2.*EDGELENGTH);
|
|---|
| 170 |
|
|---|
| 171 | Pmatrix *= 0.5;
|
|---|
| 172 | Dmatrix *= 0.5;
|
|---|
| 173 |
|
|---|
| 174 | CPPUNIT_ASSERT_EQUAL(Pmatrix, LC->Partition);
|
|---|
| 175 | CPPUNIT_ASSERT_EQUAL(Dmatrix, LC->Dimensions);
|
|---|
| 176 | }
|
|---|
| 177 |
|
|---|
| 178 | /** UnitTest for insertPointCloud()
|
|---|
| 179 | */
|
|---|
| 180 | void LinkedCell_ModelTest::getStepTest()
|
|---|
| 181 | {
|
|---|
| 182 | // zero distance
|
|---|
| 183 | LinkedCell::tripleIndex index = LC->getStep(0.);
|
|---|
| 184 | for (size_t i = 0; i<NDIM; ++i)
|
|---|
| 185 | CPPUNIT_ASSERT( (size_t)0 == index[i]);
|
|---|
| 186 | // check all possible shells on boundary
|
|---|
| 187 | for (double length = EDGELENGTH; length < DOMAINLENGTH; length+=EDGELENGTH) {
|
|---|
| 188 | LinkedCell::tripleIndex index = LC->getStep(length);
|
|---|
| 189 | for (size_t i = 0; i<NDIM; ++i) {
|
|---|
| 190 | std::cout << (size_t)(length/EDGELENGTH) << " ==" << index[i] << std::endl;
|
|---|
| 191 | CPPUNIT_ASSERT( (LinkedCell::LinkedCellArray::index)(length/EDGELENGTH) == index[i]);
|
|---|
| 192 | }
|
|---|
| 193 | }
|
|---|
| 194 | // check all possible shells at half interval
|
|---|
| 195 | for (double length = 0.5 * EDGELENGTH; length < DOMAINLENGTH; length+=EDGELENGTH) {
|
|---|
| 196 | LinkedCell::tripleIndex index = LC->getStep(length);
|
|---|
| 197 | for (size_t i = 0; i<NDIM; ++i)
|
|---|
| 198 | CPPUNIT_ASSERT( (LinkedCell::LinkedCellArray::index)ceil(length/EDGELENGTH) == index[i]);
|
|---|
| 199 | }
|
|---|
| 200 | }
|
|---|
| 201 |
|
|---|
| 202 | /** UnitTest for insertPointCloud()
|
|---|
| 203 | */
|
|---|
| 204 | void LinkedCell_ModelTest::getIndexToVectorTest()
|
|---|
| 205 | {
|
|---|
| 206 | {
|
|---|
| 207 | const Vector test(0.,0.,0.);
|
|---|
| 208 | const LinkedCell::tripleIndex index = LC->getIndexToVector(test);
|
|---|
| 209 | for (size_t i = 0; i<NDIM; ++i)
|
|---|
| 210 | CPPUNIT_ASSERT( (size_t)0 == index[i]);
|
|---|
| 211 | }
|
|---|
| 212 | {
|
|---|
| 213 | const Vector test(DOMAINLENGTH/2.,DOMAINLENGTH/2.,DOMAINLENGTH/2.);
|
|---|
| 214 | const LinkedCell::tripleIndex index = LC->getIndexToVector(test);
|
|---|
| 215 | for (size_t i = 0; i<NDIM; ++i)
|
|---|
| 216 | CPPUNIT_ASSERT( (size_t)floor(DOMAINLENGTH/EDGELENGTH/2.) == index[i]);
|
|---|
| 217 | }
|
|---|
| 218 | {
|
|---|
| 219 | const Vector test(DOMAINLENGTH/2.,DOMAINLENGTH/3.,DOMAINLENGTH/4.);
|
|---|
| 220 | const LinkedCell::tripleIndex index = LC->getIndexToVector(test);
|
|---|
| 221 | for (size_t i = 0; i<NDIM; ++i)
|
|---|
| 222 | CPPUNIT_ASSERT( (LinkedCell::LinkedCellArray::index)floor(DOMAINLENGTH/EDGELENGTH/(double)(i+2.)) == index[i]);
|
|---|
| 223 | }
|
|---|
| 224 | }
|
|---|
| 225 |
|
|---|
| 226 | /** UnitTest for insertPointCloud()
|
|---|
| 227 | */
|
|---|
| 228 | void LinkedCell_ModelTest::nodeTest()
|
|---|
| 229 | {
|
|---|
| 230 | // create point
|
|---|
| 231 | TesselPoint * Walker = new TesselPoint();
|
|---|
| 232 | Walker->setName(std::string("Walker9"));
|
|---|
| 233 | Walker->setPosition(Vector(9.8,7.6,5.4));
|
|---|
| 234 | PointCloudAdaptor< PointSet > cloud(&NodeList, std::string("NodeList"));
|
|---|
| 235 | LC->insertPointCloud(cloud);
|
|---|
| 236 |
|
|---|
| 237 | // check addNode
|
|---|
| 238 | LC->addNode(Walker);
|
|---|
| 239 | CPPUNIT_ASSERT_EQUAL( NodeList.size()+1, LC->CellLookup.size());
|
|---|
| 240 | LinkedCell::tripleIndex index1 = LC->getIndexToVector(Walker->getPosition());
|
|---|
| 241 | const LinkedCell::tripleIndex &index2 = LC->CellLookup[Walker]->getIndices();
|
|---|
| 242 | CPPUNIT_ASSERT(index1 == index2);
|
|---|
| 243 |
|
|---|
| 244 | // check moveNode
|
|---|
| 245 | #ifndef NDEBUG
|
|---|
| 246 | std::cout << "The following assertion is intended and is not a failure of the code." << std::endl;
|
|---|
| 247 | CPPUNIT_ASSERT_THROW( LC->moveNode(Walker), Assert::AssertionFailure );
|
|---|
| 248 | #endif
|
|---|
| 249 |
|
|---|
| 250 | // check deleteNode
|
|---|
| 251 | LC->deleteNode(Walker);
|
|---|
| 252 | CPPUNIT_ASSERT_EQUAL( NodeList.size(), LC->CellLookup.size());
|
|---|
| 253 |
|
|---|
| 254 | delete Walker;
|
|---|
| 255 | }
|
|---|