source: src/unittests/LinkedCellUnitTest.cpp@ bf3817

Action_Thermostats Add_AtomRandomPerturbation Add_FitFragmentPartialChargesAction Add_RotateAroundBondAction Add_SelectAtomByNameAction Added_ParseSaveFragmentResults AddingActions_SaveParseParticleParameters Adding_Graph_to_ChangeBondActions Adding_MD_integration_tests Adding_ParticleName_to_Atom Adding_StructOpt_integration_tests AtomFragments Automaking_mpqc_open AutomationFragmentation_failures Candidate_v1.5.4 Candidate_v1.6.0 Candidate_v1.6.1 ChangeBugEmailaddress ChangingTestPorts ChemicalSpaceEvaluator CombiningParticlePotentialParsing Combining_Subpackages Debian_Package_split Debian_package_split_molecuildergui_only Disabling_MemDebug Docu_Python_wait EmpiricalPotential_contain_HomologyGraph EmpiricalPotential_contain_HomologyGraph_documentation Enable_parallel_make_install Enhance_userguide Enhanced_StructuralOptimization Enhanced_StructuralOptimization_continued Example_ManyWaysToTranslateAtom Exclude_Hydrogens_annealWithBondGraph FitPartialCharges_GlobalError Fix_BoundInBox_CenterInBox_MoleculeActions Fix_ChargeSampling_PBC Fix_ChronosMutex Fix_FitPartialCharges Fix_FitPotential_needs_atomicnumbers Fix_ForceAnnealing Fix_IndependentFragmentGrids Fix_ParseParticles Fix_ParseParticles_split_forward_backward_Actions Fix_PopActions Fix_QtFragmentList_sorted_selection Fix_Restrictedkeyset_FragmentMolecule Fix_StatusMsg Fix_StepWorldTime_single_argument Fix_Verbose_Codepatterns Fix_fitting_potentials Fixes ForceAnnealing_goodresults ForceAnnealing_oldresults ForceAnnealing_tocheck ForceAnnealing_with_BondGraph ForceAnnealing_with_BondGraph_continued ForceAnnealing_with_BondGraph_continued_betteresults ForceAnnealing_with_BondGraph_contraction-expansion FragmentAction_writes_AtomFragments FragmentMolecule_checks_bonddegrees GeometryObjects Gui_Fixes Gui_displays_atomic_force_velocity ImplicitCharges IndependentFragmentGrids IndependentFragmentGrids_IndividualZeroInstances IndependentFragmentGrids_IntegrationTest IndependentFragmentGrids_Sole_NN_Calculation JobMarket_RobustOnKillsSegFaults JobMarket_StableWorkerPool JobMarket_unresolvable_hostname_fix MoreRobust_FragmentAutomation ODR_violation_mpqc_open PartialCharges_OrthogonalSummation PdbParser_setsAtomName PythonUI_with_named_parameters QtGui_reactivate_TimeChanged_changes Recreated_GuiChecks Rewrite_FitPartialCharges RotateToPrincipalAxisSystem_UndoRedo SaturateAtoms_findBestMatching SaturateAtoms_singleDegree StoppableMakroAction Subpackage_CodePatterns Subpackage_JobMarket Subpackage_LinearAlgebra Subpackage_levmar Subpackage_mpqc_open Subpackage_vmg Switchable_LogView ThirdParty_MPQC_rebuilt_buildsystem TrajectoryDependenant_MaxOrder TremoloParser_IncreasedPrecision TremoloParser_MultipleTimesteps TremoloParser_setsAtomName Ubuntu_1604_changes stable
Last change on this file since bf3817 was bf3817, checked in by Frederik Heber <heber@…>, 15 years ago

Added ifdef HAVE_CONFIG and config.h include to each and every cpp file.

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