1 | /*
|
---|
2 | * Project: MoleCuilder
|
---|
3 | * Description: creates and alters molecular systems
|
---|
4 | * Copyright (C) 2010-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 | * LinkedCellUnitTest.cpp
|
---|
10 | *
|
---|
11 | * Created on: Apr 9, 2010
|
---|
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 <iostream>
|
---|
27 | #include <stdio.h>
|
---|
28 | #include <cstring>
|
---|
29 |
|
---|
30 | #include "Atom/atom.hpp"
|
---|
31 | #include "CodePatterns/Assert.hpp"
|
---|
32 | #include "Descriptors/MoleculeDescriptor.hpp"
|
---|
33 | #include "Element/element.hpp"
|
---|
34 | #include "Element/periodentafel.hpp"
|
---|
35 | #include "LinkedCell/linkedcell.hpp"
|
---|
36 | #include "LinkedCell/PointCloudAdaptor.hpp"
|
---|
37 | #include "molecule.hpp"
|
---|
38 | #include "World.hpp"
|
---|
39 |
|
---|
40 | #include "linkedcellUnitTest.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( linkedcelltest );
|
---|
50 |
|
---|
51 |
|
---|
52 | void linkedcelltest::setUp()
|
---|
53 | {
|
---|
54 | // failing asserts should be thrown
|
---|
55 | ASSERT_DO(Assert::Throw);
|
---|
56 |
|
---|
57 | atom *Walker = NULL;
|
---|
58 |
|
---|
59 | // construct element
|
---|
60 | hydrogen = World::getInstance().getPeriode()->FindElement(1);
|
---|
61 | CPPUNIT_ASSERT(hydrogen != NULL && "could not find element hydrogen");
|
---|
62 |
|
---|
63 | // construct molecule (water molecule)
|
---|
64 | TestMolecule = World::getInstance().createMolecule();
|
---|
65 | CPPUNIT_ASSERT(TestMolecule != NULL && "could not create molecule");
|
---|
66 | for (double x=0.5;x<3;x+=1.)
|
---|
67 | for (double y=0.5;y<3;y+=1.)
|
---|
68 | for (double z=0.5;z<3;z+=1.) {
|
---|
69 | Walker = World::getInstance().createAtom();
|
---|
70 | CPPUNIT_ASSERT(Walker != NULL && "could not create atom");
|
---|
71 | Walker->setType(hydrogen);
|
---|
72 | Walker->setPosition(Vector(x, y, z ));
|
---|
73 | TestMolecule->AddAtom(Walker);
|
---|
74 | }
|
---|
75 |
|
---|
76 | // construct linked cell
|
---|
77 | PointCloudAdaptor<molecule> cloud(TestMolecule, TestMolecule->name);
|
---|
78 | LC = new LinkedCell_deprecated (cloud, 1.);
|
---|
79 | CPPUNIT_ASSERT(LC != NULL && "could not create LinkedCell");
|
---|
80 |
|
---|
81 | // check that TestMolecule was correctly constructed
|
---|
82 | CPPUNIT_ASSERT_EQUAL( TestMolecule->getAtomCount(), 3*3*3 );
|
---|
83 | };
|
---|
84 |
|
---|
85 |
|
---|
86 | void linkedcelltest::tearDown()
|
---|
87 | {
|
---|
88 | delete(LC);
|
---|
89 | World::purgeInstance();
|
---|
90 | };
|
---|
91 |
|
---|
92 |
|
---|
93 | /** UnitTest for LinkedCell_deprecated::CheckBounds().
|
---|
94 | */
|
---|
95 | void linkedcelltest::CheckBoundsTest()
|
---|
96 | {
|
---|
97 | // check for within bounds
|
---|
98 | LC->n[0] = LC->n[1] = LC->n[2] = 0;
|
---|
99 | CPPUNIT_ASSERT_EQUAL( true, LC->CheckBounds() );
|
---|
100 | LC->n[0] = LC->n[1] = LC->n[2] = 1;
|
---|
101 | CPPUNIT_ASSERT_EQUAL( true, LC->CheckBounds() );
|
---|
102 | LC->n[0] = LC->n[1] = LC->n[2] = 2;
|
---|
103 | CPPUNIT_ASSERT_EQUAL( true, LC->CheckBounds() );
|
---|
104 |
|
---|
105 | // check for out of bounds
|
---|
106 | cout << "The following test is supposed to fail and produce an ERROR." << endl;
|
---|
107 | LC->n[0] = 404040;
|
---|
108 | CPPUNIT_ASSERT_EQUAL( false, LC->CheckBounds() );
|
---|
109 | cout << "The following test is supposed to fail and produce an ERROR." << endl;
|
---|
110 | LC->n[0] = 0;
|
---|
111 | LC->n[1] = 5000;
|
---|
112 | CPPUNIT_ASSERT_EQUAL( false, LC->CheckBounds() );
|
---|
113 | cout << "The following test is supposed to fail and produce an ERROR." << endl;
|
---|
114 | LC->n[1] = 0;
|
---|
115 | LC->n[2] = -70;
|
---|
116 | CPPUNIT_ASSERT_EQUAL( false, LC->CheckBounds() );
|
---|
117 | cout << "The following test is supposed to fail and produce an ERROR." << endl;
|
---|
118 | LC->n[0] = LC->n[1] = LC->n[2] = 3;
|
---|
119 | CPPUNIT_ASSERT_EQUAL( false, LC->CheckBounds() );
|
---|
120 | };
|
---|
121 |
|
---|
122 |
|
---|
123 | /** UnitTest for LinkedCell_deprecated::GetCurrentCell().
|
---|
124 | * Note that CheckBounds() is used and has to be tested already.
|
---|
125 | */
|
---|
126 | void linkedcelltest::GetCurrentCellTest()
|
---|
127 | {
|
---|
128 | // within bounds
|
---|
129 | LC->n[0] = LC->n[1] = LC->n[2] = 0;
|
---|
130 | CPPUNIT_ASSERT_EQUAL( (const TesselPointSTLList*)&LC->LC[0 * 3*3 + 0 * 3 + 0], LC->GetCurrentCell() );
|
---|
131 | LC->n[0] = LC->n[1] = LC->n[2] = 1;
|
---|
132 | CPPUNIT_ASSERT_EQUAL( (const TesselPointSTLList*)&LC->LC[1 * 3*3 + 1 * 3 + 1], LC->GetCurrentCell() );
|
---|
133 | LC->n[0] = LC->n[1] = LC->n[2] = 2;
|
---|
134 | CPPUNIT_ASSERT_EQUAL( (const TesselPointSTLList*)&LC->LC[2 * 3*3 + 2 * 3 + 2], LC->GetCurrentCell() );
|
---|
135 |
|
---|
136 | // out of bounds
|
---|
137 | LC->n[0] = LC->n[1] = LC->n[2] = 3;
|
---|
138 | cout << "The following test is supposed to fail and produce an ERROR." << endl;
|
---|
139 | CPPUNIT_ASSERT_EQUAL( (const TesselPointSTLList*)NULL, LC->GetCurrentCell() );
|
---|
140 | LC->n[0] = LC->n[1] = LC->n[2] = -1;
|
---|
141 | cout << "The following test is supposed to fail and produce an ERROR." << endl;
|
---|
142 | CPPUNIT_ASSERT_EQUAL( (const TesselPointSTLList*)NULL, LC->GetCurrentCell() );
|
---|
143 | };
|
---|
144 |
|
---|
145 | /** UnitTest for LinkedCell_deprecated::GetRelativeToCurrentCell().
|
---|
146 | */
|
---|
147 | void linkedcelltest::GetRelativeToCurrentCellTest()
|
---|
148 | {
|
---|
149 | int offset[3];
|
---|
150 |
|
---|
151 | // offset to (0,0,0) always
|
---|
152 | offset[0] = offset[1] = offset[2] = 0;
|
---|
153 | LC->n[0] = LC->n[1] = LC->n[2] = 0;
|
---|
154 | CPPUNIT_ASSERT_EQUAL( (const TesselPointSTLList*)&LC->LC[0], LC->GetRelativeToCurrentCell(offset) );
|
---|
155 | offset[0] = offset[1] = offset[2] = -1;
|
---|
156 | LC->n[0] = LC->n[1] = LC->n[2] = 1;
|
---|
157 | CPPUNIT_ASSERT_EQUAL( (const TesselPointSTLList*)&LC->LC[0], LC->GetRelativeToCurrentCell(offset) );
|
---|
158 | offset[0] = offset[1] = offset[2] = -2;
|
---|
159 | LC->n[0] = LC->n[1] = LC->n[2] = 2;
|
---|
160 | CPPUNIT_ASSERT_EQUAL( (const TesselPointSTLList*)&LC->LC[0], LC->GetRelativeToCurrentCell(offset) );
|
---|
161 |
|
---|
162 | // offset to (0,0,0) - 1.*(x/y/z) out of bounds
|
---|
163 | offset[0] = offset[1] = offset[2] = 0;
|
---|
164 | offset[0] = -1;
|
---|
165 | LC->n[0] = LC->n[1] = LC->n[2] = 0;
|
---|
166 | cout << "The following test is supposed to fail and produce an ERROR." << endl;
|
---|
167 | CPPUNIT_ASSERT_EQUAL( (const TesselPointSTLList*)NULL, LC->GetRelativeToCurrentCell(offset) );
|
---|
168 | offset[0] = offset[1] = offset[2] = 0;
|
---|
169 | offset[1] = -1;
|
---|
170 | LC->n[0] = LC->n[1] = LC->n[2] = 0;
|
---|
171 | cout << "The following test is supposed to fail and produce an ERROR." << endl;
|
---|
172 | CPPUNIT_ASSERT_EQUAL( (const TesselPointSTLList*)NULL, LC->GetRelativeToCurrentCell(offset) );
|
---|
173 | offset[0] = offset[1] = offset[2] = 0;
|
---|
174 | offset[2] = -1;
|
---|
175 | LC->n[0] = LC->n[1] = LC->n[2] = 0;
|
---|
176 | cout << "The following test is supposed to fail and produce an ERROR." << endl;
|
---|
177 | CPPUNIT_ASSERT_EQUAL( (const TesselPointSTLList*)NULL, LC->GetRelativeToCurrentCell(offset) );
|
---|
178 |
|
---|
179 | // out of bounds
|
---|
180 | offset[0] = offset[1] = offset[2] = -5054932;
|
---|
181 | LC->n[0] = LC->n[1] = LC->n[2] = 1;
|
---|
182 | cout << "The following test is supposed to fail and produce an ERROR." << endl;
|
---|
183 | CPPUNIT_ASSERT_EQUAL( (const TesselPointSTLList*)NULL, LC->GetRelativeToCurrentCell(offset) );
|
---|
184 | offset[0] = offset[1] = offset[2] = 192345;
|
---|
185 | LC->n[0] = LC->n[1] = LC->n[2] = 1;
|
---|
186 | cout << "The following test is supposed to fail and produce an ERROR." << endl;
|
---|
187 | CPPUNIT_ASSERT_EQUAL( (const TesselPointSTLList*)NULL, LC->GetRelativeToCurrentCell(offset) );
|
---|
188 |
|
---|
189 | // index is out of bounds, offset points within
|
---|
190 | offset[0] = offset[1] = offset[2] = -2;
|
---|
191 | LC->n[0] = LC->n[1] = LC->n[2] = 4;
|
---|
192 | CPPUNIT_ASSERT_EQUAL( (const TesselPointSTLList*)&LC->LC[2 * 3*3 + 2 * 3 + 2], LC->GetRelativeToCurrentCell(offset) );
|
---|
193 |
|
---|
194 | // index is within bounds, offset points out
|
---|
195 | offset[0] = offset[1] = offset[2] = 2;
|
---|
196 | LC->n[0] = LC->n[1] = LC->n[2] = 2;
|
---|
197 | cout << "The following test is supposed to fail and produce an ERROR." << endl;
|
---|
198 | CPPUNIT_ASSERT_EQUAL( (const TesselPointSTLList*)NULL, LC->GetRelativeToCurrentCell(offset) );
|
---|
199 | };
|
---|
200 |
|
---|
201 |
|
---|
202 | /** UnitTest for LinkedCell_deprecated::SetIndexToNode().
|
---|
203 | */
|
---|
204 | void linkedcelltest::SetIndexToNodeTest()
|
---|
205 | {
|
---|
206 | // check all atoms
|
---|
207 | for(molecule::iterator iter = TestMolecule->begin(); iter != TestMolecule->end();++iter){
|
---|
208 | CPPUNIT_ASSERT_EQUAL( true, LC->SetIndexToNode(*iter) );
|
---|
209 | }
|
---|
210 |
|
---|
211 | // check internal vectors, returns false, because this atom is not in LC-list!
|
---|
212 | atom *newAtom = World::getInstance().createAtom();
|
---|
213 | newAtom->setName("test");
|
---|
214 | newAtom->setPosition(Vector(1,1,1));
|
---|
215 | CPPUNIT_ASSERT_EQUAL( false, LC->SetIndexToNode(newAtom) );
|
---|
216 | World::getInstance().destroyAtom(newAtom);
|
---|
217 |
|
---|
218 | // check out of bounds vectors
|
---|
219 | newAtom = World::getInstance().createAtom();
|
---|
220 | newAtom->setName("test");
|
---|
221 | newAtom->setPosition(Vector(0,-1,0));
|
---|
222 | CPPUNIT_ASSERT_EQUAL( false, LC->SetIndexToNode(newAtom) );
|
---|
223 | World::getInstance().destroyAtom(newAtom);
|
---|
224 | };
|
---|
225 |
|
---|
226 |
|
---|
227 | /** UnitTest for LinkedCell_deprecated::SetIndexToVector().
|
---|
228 | */
|
---|
229 | void linkedcelltest::SetIndexToVectorTest()
|
---|
230 | {
|
---|
231 | Vector tester;
|
---|
232 |
|
---|
233 | // check center of each cell
|
---|
234 | for (double x=0.5;x<3;x+=1.)
|
---|
235 | for (double y=0.5;y<3;y+=1.)
|
---|
236 | for (double z=0.5;z<3;z+=1.) {
|
---|
237 | tester = Vector(x,y,z);
|
---|
238 | CPPUNIT_ASSERT_EQUAL( true, LC->SetIndexToVector(tester) );
|
---|
239 | }
|
---|
240 | // check corners of each cell
|
---|
241 | for (double x=1.;x<4;x+=1.)
|
---|
242 | for (double y=1.;y<4;y+=1.)
|
---|
243 | for (double z=1.;z<4;z+=1.) {
|
---|
244 | tester= Vector(x,y,z);
|
---|
245 | cout << "Tester is at " << tester << "." << endl;
|
---|
246 | CPPUNIT_ASSERT_EQUAL( true, LC->SetIndexToVector(tester) );
|
---|
247 | }
|
---|
248 | // check out of bounds
|
---|
249 | for (double x=0.5-1e-10;x<5;x+=3.1)
|
---|
250 | for (double y=0.5-1e-10;y<5;y+=3.1)
|
---|
251 | for (double z=0.5-1e-10;z<5;z+=3.1) {
|
---|
252 | tester = Vector(x,y,z);
|
---|
253 | cout << "The following test is supposed to fail and produce an ERROR." << endl;
|
---|
254 | CPPUNIT_ASSERT_EQUAL( false, LC->SetIndexToVector(tester) );
|
---|
255 | }
|
---|
256 | // check nonsense vectors
|
---|
257 | tester= Vector(-423598,3245978,29349);
|
---|
258 | cout << "The following test is supposed to fail and produce an ERROR." << endl;
|
---|
259 | CPPUNIT_ASSERT_EQUAL( false, LC->SetIndexToVector(tester) );
|
---|
260 | };
|
---|
261 |
|
---|
262 |
|
---|
263 | /** UnitTest for LinkedCell_deprecated::GetNeighbourBounds().
|
---|
264 | */
|
---|
265 | void linkedcelltest::GetNeighbourBoundsTest()
|
---|
266 | {
|
---|
267 | Vector tester;
|
---|
268 | int lower[NDIM], upper[NDIM];
|
---|
269 |
|
---|
270 | tester= Vector(0.5,0.5,0.5);
|
---|
271 | LC->SetIndexToVector(tester);
|
---|
272 | LC->GetNeighbourBounds(lower, upper);
|
---|
273 | for (int i=0;i<NDIM;i++)
|
---|
274 | CPPUNIT_ASSERT_EQUAL( 0, lower[i]);
|
---|
275 | for (int i=0;i<NDIM;i++)
|
---|
276 | CPPUNIT_ASSERT_EQUAL( 1, upper[i]);
|
---|
277 | };
|
---|
278 |
|
---|
279 |
|
---|
280 | /** UnitTest for LinkedCell_deprecated::GetallNeighbours().
|
---|
281 | */
|
---|
282 | void linkedcelltest::GetallNeighboursTest()
|
---|
283 | {
|
---|
284 | Vector tester;
|
---|
285 | TesselPointSTLList *ListOfPoints = NULL;
|
---|
286 | size_t size = 0;
|
---|
287 |
|
---|
288 | // get all atoms
|
---|
289 | tester= Vector(1.5,1.5,1.5);
|
---|
290 | CPPUNIT_ASSERT_EQUAL ( true, LC->SetIndexToVector(tester) );
|
---|
291 | ListOfPoints = LC->GetallNeighbours();
|
---|
292 | size = ListOfPoints->size();
|
---|
293 | CPPUNIT_ASSERT_EQUAL( (size_t)27, size );
|
---|
294 |
|
---|
295 | for(molecule::iterator iter = TestMolecule->begin(); iter != TestMolecule->end(); ++iter){
|
---|
296 | ListOfPoints->remove((*iter));
|
---|
297 | size--;
|
---|
298 | CPPUNIT_ASSERT_EQUAL( size, ListOfPoints->size() );
|
---|
299 | }
|
---|
300 | CPPUNIT_ASSERT_EQUAL( (size_t)0, size );
|
---|
301 | CPPUNIT_ASSERT_EQUAL( (size_t)0, ListOfPoints->size() );
|
---|
302 | CPPUNIT_ASSERT_EQUAL( true, ListOfPoints->empty() );
|
---|
303 | delete(ListOfPoints);
|
---|
304 |
|
---|
305 | // get all atoms in one corner
|
---|
306 | tester= Vector(0.5, 0.5, 0.5);
|
---|
307 | CPPUNIT_ASSERT_EQUAL ( true, LC->SetIndexToVector(tester) );
|
---|
308 | ListOfPoints = LC->GetallNeighbours();
|
---|
309 | size=ListOfPoints->size();
|
---|
310 | CPPUNIT_ASSERT_EQUAL( (size_t)8, size );
|
---|
311 | for(molecule::iterator iter = TestMolecule->begin(); iter != TestMolecule->end(); ++iter){
|
---|
312 | if (((*iter)->at(0) <2) && ((*iter)->at(1) <2) && ((*iter)->at(2) <2)) {
|
---|
313 | ListOfPoints->remove(*iter);
|
---|
314 | size--;
|
---|
315 | CPPUNIT_ASSERT_EQUAL( size, ListOfPoints->size() );
|
---|
316 | }
|
---|
317 | }
|
---|
318 | CPPUNIT_ASSERT_EQUAL( (size_t)0, size );
|
---|
319 | CPPUNIT_ASSERT_EQUAL( (size_t)0, ListOfPoints->size() );
|
---|
320 | CPPUNIT_ASSERT_EQUAL( true, ListOfPoints->empty() );
|
---|
321 | delete(ListOfPoints);
|
---|
322 |
|
---|
323 | // get all atoms from one corner
|
---|
324 | tester = Vector(0.5, 0.5, 0.5);
|
---|
325 | CPPUNIT_ASSERT_EQUAL ( true, LC->SetIndexToVector(tester) );
|
---|
326 | ListOfPoints = LC->GetallNeighbours(3);
|
---|
327 | size=ListOfPoints->size();
|
---|
328 | CPPUNIT_ASSERT_EQUAL( (size_t)27, size );
|
---|
329 | for(molecule::iterator iter = TestMolecule->begin(); iter!=TestMolecule->end();++iter){
|
---|
330 | ListOfPoints->remove(*iter);
|
---|
331 | size--;
|
---|
332 | CPPUNIT_ASSERT_EQUAL( size, ListOfPoints->size() );
|
---|
333 | }
|
---|
334 | CPPUNIT_ASSERT_EQUAL( (size_t)0, size );
|
---|
335 | CPPUNIT_ASSERT_EQUAL( (size_t)0, ListOfPoints->size() );
|
---|
336 | CPPUNIT_ASSERT_EQUAL( true, ListOfPoints->empty() );
|
---|
337 | delete(ListOfPoints);
|
---|
338 | };
|
---|
339 |
|
---|
340 |
|
---|
341 | /** UnitTest for LinkedCell_deprecated::GetPointsInsideSphere().
|
---|
342 | */
|
---|
343 | void linkedcelltest::GetPointsInsideSphereTest()
|
---|
344 | {
|
---|
345 | Vector tester;
|
---|
346 | TesselPointSTLList *ListOfPoints = NULL;
|
---|
347 | size_t size = 0;
|
---|
348 |
|
---|
349 | // get all points around central arom with radius 1.
|
---|
350 | tester= Vector(1.5,1.5,1.5);
|
---|
351 | CPPUNIT_ASSERT_EQUAL ( true, LC->SetIndexToVector(tester) );
|
---|
352 | ListOfPoints = LC->GetPointsInsideSphere(1., &tester);
|
---|
353 | size = ListOfPoints->size();
|
---|
354 | CPPUNIT_ASSERT_EQUAL( (size_t)7, size );
|
---|
355 | for(molecule::iterator iter = TestMolecule->begin(); iter!=TestMolecule->end();++iter){
|
---|
356 | if (((*iter)->DistanceSquared(tester) - 1.) < MYEPSILON ) {
|
---|
357 | ListOfPoints->remove(*iter);
|
---|
358 | size--;
|
---|
359 | CPPUNIT_ASSERT_EQUAL( size, ListOfPoints->size() );
|
---|
360 | }
|
---|
361 | }
|
---|
362 | CPPUNIT_ASSERT_EQUAL( (size_t)0, size );
|
---|
363 | CPPUNIT_ASSERT_EQUAL( (size_t)0, ListOfPoints->size() );
|
---|
364 | CPPUNIT_ASSERT_EQUAL( true, ListOfPoints->empty() );
|
---|
365 | delete(ListOfPoints);
|
---|
366 | };
|
---|