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