source: src/unittests/LinkedCellUnitTest.cpp@ 158c594

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 158c594 was 8de375, checked in by Frederik Heber <heber@…>, 15 years ago

Merge branch 'StructureRefactoring' into CommandLineActionMapping

Conflicts:

molecuilder/src/atom_particleinfo.cpp
molecuilder/src/builder.cpp

The following conflicts were solved:

  • in atom_particleinfo constructor the bugfix could not be merged automatically
  • in builder.cpp at the end of main() cleanup and memory:getstate() were still present.
  • Property mode set to 100644
File size: 12.2 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 MemoryUsageObserver::purgeInstance();
73};
74
75
76/** UnitTest for LinkedCell::CheckBounds().
77 */
78void LinkedCellTest::CheckBoundsTest()
79{
80 // check for within bounds
81 LC->n[0] = LC->n[1] = LC->n[2] = 0;
82 CPPUNIT_ASSERT_EQUAL( true, LC->CheckBounds() );
83 LC->n[0] = LC->n[1] = LC->n[2] = 1;
84 CPPUNIT_ASSERT_EQUAL( true, LC->CheckBounds() );
85 LC->n[0] = LC->n[1] = LC->n[2] = 2;
86 CPPUNIT_ASSERT_EQUAL( true, LC->CheckBounds() );
87
88 // check for out of bounds
89 cout << "The following test is supposed to fail and produce an ERROR." << endl;
90 LC->n[0] = 404040;
91 CPPUNIT_ASSERT_EQUAL( false, LC->CheckBounds() );
92 cout << "The following test is supposed to fail and produce an ERROR." << endl;
93 LC->n[0] = 0;
94 LC->n[1] = 5000;
95 CPPUNIT_ASSERT_EQUAL( false, LC->CheckBounds() );
96 cout << "The following test is supposed to fail and produce an ERROR." << endl;
97 LC->n[1] = 0;
98 LC->n[2] = -70;
99 CPPUNIT_ASSERT_EQUAL( false, LC->CheckBounds() );
100 cout << "The following test is supposed to fail and produce an ERROR." << endl;
101 LC->n[0] = LC->n[1] = LC->n[2] = 3;
102 CPPUNIT_ASSERT_EQUAL( false, LC->CheckBounds() );
103};
104
105
106/** UnitTest for LinkedCell::GetCurrentCell().
107 * Note that CheckBounds() is used and has to be tested already.
108 */
109void LinkedCellTest::GetCurrentCellTest()
110{
111 // within bounds
112 LC->n[0] = LC->n[1] = LC->n[2] = 0;
113 CPPUNIT_ASSERT_EQUAL( (const LinkedCell::LinkedNodes*)&LC->LC[0 * 3*3 + 0 * 3 + 0], LC->GetCurrentCell() );
114 LC->n[0] = LC->n[1] = LC->n[2] = 1;
115 CPPUNIT_ASSERT_EQUAL( (const LinkedCell::LinkedNodes*)&LC->LC[1 * 3*3 + 1 * 3 + 1], LC->GetCurrentCell() );
116 LC->n[0] = LC->n[1] = LC->n[2] = 2;
117 CPPUNIT_ASSERT_EQUAL( (const LinkedCell::LinkedNodes*)&LC->LC[2 * 3*3 + 2 * 3 + 2], LC->GetCurrentCell() );
118
119 // out of bounds
120 LC->n[0] = LC->n[1] = LC->n[2] = 3;
121 cout << "The following test is supposed to fail and produce an ERROR." << endl;
122 CPPUNIT_ASSERT_EQUAL( (const LinkedCell::LinkedNodes*)NULL, LC->GetCurrentCell() );
123 LC->n[0] = LC->n[1] = LC->n[2] = -1;
124 cout << "The following test is supposed to fail and produce an ERROR." << endl;
125 CPPUNIT_ASSERT_EQUAL( (const LinkedCell::LinkedNodes*)NULL, LC->GetCurrentCell() );
126};
127
128/** UnitTest for LinkedCell::GetRelativeToCurrentCell().
129 */
130void LinkedCellTest::GetRelativeToCurrentCellTest()
131{
132 int offset[3];
133
134 // offset to (0,0,0) always
135 offset[0] = offset[1] = offset[2] = 0;
136 LC->n[0] = LC->n[1] = LC->n[2] = 0;
137 CPPUNIT_ASSERT_EQUAL( (const LinkedCell::LinkedNodes*)&LC->LC[0], LC->GetRelativeToCurrentCell(offset) );
138 offset[0] = offset[1] = offset[2] = -1;
139 LC->n[0] = LC->n[1] = LC->n[2] = 1;
140 CPPUNIT_ASSERT_EQUAL( (const LinkedCell::LinkedNodes*)&LC->LC[0], LC->GetRelativeToCurrentCell(offset) );
141 offset[0] = offset[1] = offset[2] = -2;
142 LC->n[0] = LC->n[1] = LC->n[2] = 2;
143 CPPUNIT_ASSERT_EQUAL( (const LinkedCell::LinkedNodes*)&LC->LC[0], LC->GetRelativeToCurrentCell(offset) );
144
145 // offset to (0,0,0) - 1.*(x/y/z) out of bounds
146 offset[0] = offset[1] = offset[2] = 0;
147 offset[0] = -1;
148 LC->n[0] = LC->n[1] = LC->n[2] = 0;
149 cout << "The following test is supposed to fail and produce an ERROR." << endl;
150 CPPUNIT_ASSERT_EQUAL( (const LinkedCell::LinkedNodes*)NULL, LC->GetRelativeToCurrentCell(offset) );
151 offset[0] = offset[1] = offset[2] = 0;
152 offset[1] = -1;
153 LC->n[0] = LC->n[1] = LC->n[2] = 0;
154 cout << "The following test is supposed to fail and produce an ERROR." << endl;
155 CPPUNIT_ASSERT_EQUAL( (const LinkedCell::LinkedNodes*)NULL, LC->GetRelativeToCurrentCell(offset) );
156 offset[0] = offset[1] = offset[2] = 0;
157 offset[2] = -1;
158 LC->n[0] = LC->n[1] = LC->n[2] = 0;
159 cout << "The following test is supposed to fail and produce an ERROR." << endl;
160 CPPUNIT_ASSERT_EQUAL( (const LinkedCell::LinkedNodes*)NULL, LC->GetRelativeToCurrentCell(offset) );
161
162 // out of bounds
163 offset[0] = offset[1] = offset[2] = -5054932;
164 LC->n[0] = LC->n[1] = LC->n[2] = 1;
165 cout << "The following test is supposed to fail and produce an ERROR." << endl;
166 CPPUNIT_ASSERT_EQUAL( (const LinkedCell::LinkedNodes*)NULL, LC->GetRelativeToCurrentCell(offset) );
167 offset[0] = offset[1] = offset[2] = 192345;
168 LC->n[0] = LC->n[1] = LC->n[2] = 1;
169 cout << "The following test is supposed to fail and produce an ERROR." << endl;
170 CPPUNIT_ASSERT_EQUAL( (const LinkedCell::LinkedNodes*)NULL, LC->GetRelativeToCurrentCell(offset) );
171
172 // index is out of bounds, offset points within
173 offset[0] = offset[1] = offset[2] = -2;
174 LC->n[0] = LC->n[1] = LC->n[2] = 4;
175 CPPUNIT_ASSERT_EQUAL( (const LinkedCell::LinkedNodes*)&LC->LC[2 * 3*3 + 2 * 3 + 2], LC->GetRelativeToCurrentCell(offset) );
176
177 // index is within bounds, offset points out
178 offset[0] = offset[1] = offset[2] = 2;
179 LC->n[0] = LC->n[1] = LC->n[2] = 2;
180 cout << "The following test is supposed to fail and produce an ERROR." << endl;
181 CPPUNIT_ASSERT_EQUAL( (const LinkedCell::LinkedNodes*)NULL, LC->GetRelativeToCurrentCell(offset) );
182};
183
184
185/** UnitTest for LinkedCell::SetIndexToNode().
186 */
187void LinkedCellTest::SetIndexToNodeTest()
188{
189 // check all atoms
190 atom *Walker = TestMolecule->start;
191 while (Walker->next != TestMolecule->end) {
192 Walker = Walker->next;
193 CPPUNIT_ASSERT_EQUAL( true, LC->SetIndexToNode(Walker) );
194 }
195
196 // check internal vectors, returns false, because this atom is not in LC-list!
197 Walker = World::getInstance().createAtom();
198 Walker->setName("test");
199 Walker->x= Vector(1,1,1);
200 CPPUNIT_ASSERT_EQUAL( false, LC->SetIndexToNode(Walker) );
201 World::getInstance().destroyAtom(Walker);
202
203 // check out of bounds vectors
204 Walker = World::getInstance().createAtom();
205 Walker->setName("test");
206 Walker->x = Vector(0,-1,0);
207 CPPUNIT_ASSERT_EQUAL( false, LC->SetIndexToNode(Walker) );
208 World::getInstance().destroyAtom(Walker);
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 atom *Walker = NULL;
272 size_t size = 0;
273
274 // get all atoms
275 tester= Vector(1.5,1.5,1.5);
276 CPPUNIT_ASSERT_EQUAL ( true, LC->SetIndexToVector(&tester) );
277 ListOfPoints = LC->GetallNeighbours();
278 size = ListOfPoints->size();
279 CPPUNIT_ASSERT_EQUAL( (size_t)27, size );
280 Walker = TestMolecule->start;
281 Walker = TestMolecule->start;
282 while (Walker->next != TestMolecule->end) {
283 Walker = Walker->next;
284 ListOfPoints->remove(Walker);
285 size--;
286 CPPUNIT_ASSERT_EQUAL( size, ListOfPoints->size() );
287 }
288 CPPUNIT_ASSERT_EQUAL( (size_t)0, size );
289 CPPUNIT_ASSERT_EQUAL( (size_t)0, ListOfPoints->size() );
290 CPPUNIT_ASSERT_EQUAL( true, ListOfPoints->empty() );
291 delete(ListOfPoints);
292
293 // get all atoms in one corner
294 tester= Vector(0.5, 0.5, 0.5);
295 CPPUNIT_ASSERT_EQUAL ( true, LC->SetIndexToVector(&tester) );
296 ListOfPoints = LC->GetallNeighbours();
297 size=ListOfPoints->size();
298 CPPUNIT_ASSERT_EQUAL( (size_t)8, size );
299 Walker = TestMolecule->start;
300 while (Walker->next != TestMolecule->end) {
301 Walker = Walker->next;
302 if ((Walker->x[0] <2) && (Walker->x[1] <2) && (Walker->x[2] <2)) {
303 ListOfPoints->remove(Walker);
304 size--;
305 CPPUNIT_ASSERT_EQUAL( size, ListOfPoints->size() );
306 }
307 }
308 CPPUNIT_ASSERT_EQUAL( (size_t)0, size );
309 CPPUNIT_ASSERT_EQUAL( (size_t)0, ListOfPoints->size() );
310 CPPUNIT_ASSERT_EQUAL( true, ListOfPoints->empty() );
311 delete(ListOfPoints);
312
313 // get all atoms from one corner
314 tester = Vector(0.5, 0.5, 0.5);
315 CPPUNIT_ASSERT_EQUAL ( true, LC->SetIndexToVector(&tester) );
316 ListOfPoints = LC->GetallNeighbours(3);
317 size=ListOfPoints->size();
318 CPPUNIT_ASSERT_EQUAL( (size_t)27, size );
319 Walker = TestMolecule->start;
320 while (Walker->next != TestMolecule->end) {
321 Walker = Walker->next;
322 ListOfPoints->remove(Walker);
323 size--;
324 CPPUNIT_ASSERT_EQUAL( size, ListOfPoints->size() );
325 }
326 CPPUNIT_ASSERT_EQUAL( (size_t)0, size );
327 CPPUNIT_ASSERT_EQUAL( (size_t)0, ListOfPoints->size() );
328 CPPUNIT_ASSERT_EQUAL( true, ListOfPoints->empty() );
329 delete(ListOfPoints);
330};
331
332
333/** UnitTest for LinkedCell::GetPointsInsideSphere().
334 */
335void LinkedCellTest::GetPointsInsideSphereTest()
336{
337 Vector tester;
338 LinkedCell::LinkedNodes *ListOfPoints = NULL;
339 atom *Walker = NULL;
340 size_t size = 0;
341
342 // get all points around central arom with radius 1.
343 tester= Vector(1.5,1.5,1.5);
344 CPPUNIT_ASSERT_EQUAL ( true, LC->SetIndexToVector(&tester) );
345 ListOfPoints = LC->GetPointsInsideSphere(1., &tester);
346 size = ListOfPoints->size();
347 CPPUNIT_ASSERT_EQUAL( (size_t)7, size );
348 Walker = TestMolecule->start;
349 while (Walker->next != TestMolecule->end) {
350 Walker = Walker->next;
351 if ((Walker->x.DistanceSquared(tester) - 1.) < MYEPSILON ) {
352 ListOfPoints->remove(Walker);
353 size--;
354 CPPUNIT_ASSERT_EQUAL( size, ListOfPoints->size() );
355 }
356 }
357 CPPUNIT_ASSERT_EQUAL( (size_t)0, size );
358 CPPUNIT_ASSERT_EQUAL( (size_t)0, ListOfPoints->size() );
359 CPPUNIT_ASSERT_EQUAL( true, ListOfPoints->empty() );
360 delete(ListOfPoints);
361};
Note: See TracBrowser for help on using the repository browser.