source: src/unittests/LinkedCellUnitTest.cpp@ 42af9e

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 Candidate_v1.7.0 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 42af9e was 920c70, checked in by Frederik Heber <heber@…>, 15 years ago

Removed all Malloc/Calloc/ReAlloc (&Free) and replaced by new and delete/delete[].

Due to the new MemDebug framework there is no need (or even unnecessary/unwanted competition between it and) for the MemoryAllocator and ..UsageObserver anymore.
They can however still be used with c codes such as pcp and alikes.

In Molecuilder lots of glibc corruptions arose and the C-like syntax make it generally harder to get allocation and deallocation straight.

Signed-off-by: Frederik Heber <heber@…>

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