source: src/unittests/LinkedCellUnitTest.cpp@ 673c7f

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 673c7f was 4eb4fe, checked in by Frederik Heber <heber@…>, 15 years ago

"-e <db path>" not necessary anymore.

Removed necessity of specifying path to databases (this was one check of molecuilder/test/testsuite.at which cannot be fulfilled anymore with boost::program_options)
For this to work a great number of small changes have been necessary:

class periodentafel:

  • all .db files merged into const char * arrays in elements_db.cpp
  • periodentafel rewritten:
  • FindElement(), AskElement() and EnterElement return element * const instead of const element * (i.e. the contents of the pointer is const (the element) not the pointer itself which is very vexatious (i.e. FindElement() yields const element * which can subsequently not be used for RemoveElement(), ...)
  • parsedElems is not needed anymore. Instead we operate on map elements directly
  • new unittest periodentafelTest which is made friend of periodentafel to be able to access private loading functions directly

A number of unit tests had to be changed (all that create elements during setUp() which is now unnecessary)

Some of the analysis_bonds function's signatures were changed in the process:

Finally, the respective tests are removed from molecuilder/tests/testsuite.at.

  • Property mode set to 100644
File size: 12.4 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->Name = Malloc<char>(6, "LinkedCellTest::SetIndexToNodeTest - Walker");
199 strcpy(Walker->Name, "test");
200 Walker->x= Vector(1,1,1);
201 CPPUNIT_ASSERT_EQUAL( false, LC->SetIndexToNode(Walker) );
202 World::getInstance().destroyAtom(Walker);
203
204 // check out of bounds vectors
205 Walker = World::getInstance().createAtom();
206 Walker->Name = Malloc<char>(6, "LinkedCellTest::SetIndexToNodeTest - Walker");
207 strcpy(Walker->Name, "test");
208 Walker->x = Vector(0,-1,0);
209 CPPUNIT_ASSERT_EQUAL( false, LC->SetIndexToNode(Walker) );
210 World::getInstance().destroyAtom(Walker);
211};
212
213
214/** UnitTest for LinkedCell::SetIndexToVector().
215 */
216void LinkedCellTest::SetIndexToVectorTest()
217{
218 Vector tester;
219
220 // check center of each cell
221 for (double x=0.5;x<3;x+=1.)
222 for (double y=0.5;y<3;y+=1.)
223 for (double z=0.5;z<3;z+=1.) {
224 tester = Vector(x,y,z);
225 CPPUNIT_ASSERT_EQUAL( true, LC->SetIndexToVector(&tester) );
226 }
227 // check corners of each cell
228 for (double x=1.;x<4;x+=1.)
229 for (double y=1.;y<4;y+=1.)
230 for (double z=1.;z<4;z+=1.) {
231 tester= Vector(x,y,z);
232 cout << "Tester is at " << tester << "." << endl;
233 CPPUNIT_ASSERT_EQUAL( true, LC->SetIndexToVector(&tester) );
234 }
235 // check out of bounds
236 for (double x=0.5-1e-10;x<5;x+=3.1)
237 for (double y=0.5-1e-10;y<5;y+=3.1)
238 for (double z=0.5-1e-10;z<5;z+=3.1) {
239 tester = Vector(x,y,z);
240 cout << "The following test is supposed to fail and produce an ERROR." << endl;
241 CPPUNIT_ASSERT_EQUAL( false, LC->SetIndexToVector(&tester) );
242 }
243 // check nonsense vectors
244 tester= Vector(-423598,3245978,29349);
245 cout << "The following test is supposed to fail and produce an ERROR." << endl;
246 CPPUNIT_ASSERT_EQUAL( false, LC->SetIndexToVector(&tester) );
247};
248
249
250/** UnitTest for LinkedCell::GetNeighbourBounds().
251 */
252void LinkedCellTest::GetNeighbourBoundsTest()
253{
254 Vector tester;
255 int lower[NDIM], upper[NDIM];
256
257 tester= Vector(0.5,0.5,0.5);
258 LC->SetIndexToVector(&tester);
259 LC->GetNeighbourBounds(lower, upper);
260 for (int i=0;i<NDIM;i++)
261 CPPUNIT_ASSERT_EQUAL( 0, lower[i]);
262 for (int i=0;i<NDIM;i++)
263 CPPUNIT_ASSERT_EQUAL( 1, upper[i]);
264};
265
266
267/** UnitTest for LinkedCell::GetallNeighbours().
268 */
269void LinkedCellTest::GetallNeighboursTest()
270{
271 Vector tester;
272 LinkedCell::LinkedNodes *ListOfPoints = NULL;
273 atom *Walker = NULL;
274 size_t size = 0;
275
276 // get all atoms
277 tester= Vector(1.5,1.5,1.5);
278 CPPUNIT_ASSERT_EQUAL ( true, LC->SetIndexToVector(&tester) );
279 ListOfPoints = LC->GetallNeighbours();
280 size = ListOfPoints->size();
281 CPPUNIT_ASSERT_EQUAL( (size_t)27, size );
282 Walker = TestMolecule->start;
283 Walker = TestMolecule->start;
284 while (Walker->next != TestMolecule->end) {
285 Walker = Walker->next;
286 ListOfPoints->remove(Walker);
287 size--;
288 CPPUNIT_ASSERT_EQUAL( size, ListOfPoints->size() );
289 }
290 CPPUNIT_ASSERT_EQUAL( (size_t)0, size );
291 CPPUNIT_ASSERT_EQUAL( (size_t)0, ListOfPoints->size() );
292 CPPUNIT_ASSERT_EQUAL( true, ListOfPoints->empty() );
293 delete(ListOfPoints);
294
295 // get all atoms in one corner
296 tester= Vector(0.5, 0.5, 0.5);
297 CPPUNIT_ASSERT_EQUAL ( true, LC->SetIndexToVector(&tester) );
298 ListOfPoints = LC->GetallNeighbours();
299 size=ListOfPoints->size();
300 CPPUNIT_ASSERT_EQUAL( (size_t)8, size );
301 Walker = TestMolecule->start;
302 while (Walker->next != TestMolecule->end) {
303 Walker = Walker->next;
304 if ((Walker->x[0] <2) && (Walker->x[1] <2) && (Walker->x[2] <2)) {
305 ListOfPoints->remove(Walker);
306 size--;
307 CPPUNIT_ASSERT_EQUAL( size, ListOfPoints->size() );
308 }
309 }
310 CPPUNIT_ASSERT_EQUAL( (size_t)0, size );
311 CPPUNIT_ASSERT_EQUAL( (size_t)0, ListOfPoints->size() );
312 CPPUNIT_ASSERT_EQUAL( true, ListOfPoints->empty() );
313 delete(ListOfPoints);
314
315 // get all atoms from one corner
316 tester = Vector(0.5, 0.5, 0.5);
317 CPPUNIT_ASSERT_EQUAL ( true, LC->SetIndexToVector(&tester) );
318 ListOfPoints = LC->GetallNeighbours(3);
319 size=ListOfPoints->size();
320 CPPUNIT_ASSERT_EQUAL( (size_t)27, size );
321 Walker = TestMolecule->start;
322 while (Walker->next != TestMolecule->end) {
323 Walker = Walker->next;
324 ListOfPoints->remove(Walker);
325 size--;
326 CPPUNIT_ASSERT_EQUAL( size, ListOfPoints->size() );
327 }
328 CPPUNIT_ASSERT_EQUAL( (size_t)0, size );
329 CPPUNIT_ASSERT_EQUAL( (size_t)0, ListOfPoints->size() );
330 CPPUNIT_ASSERT_EQUAL( true, ListOfPoints->empty() );
331 delete(ListOfPoints);
332};
333
334
335/** UnitTest for LinkedCell::GetPointsInsideSphere().
336 */
337void LinkedCellTest::GetPointsInsideSphereTest()
338{
339 Vector tester;
340 LinkedCell::LinkedNodes *ListOfPoints = NULL;
341 atom *Walker = NULL;
342 size_t size = 0;
343
344 // get all points around central arom with radius 1.
345 tester= Vector(1.5,1.5,1.5);
346 CPPUNIT_ASSERT_EQUAL ( true, LC->SetIndexToVector(&tester) );
347 ListOfPoints = LC->GetPointsInsideSphere(1., &tester);
348 size = ListOfPoints->size();
349 CPPUNIT_ASSERT_EQUAL( (size_t)7, size );
350 Walker = TestMolecule->start;
351 while (Walker->next != TestMolecule->end) {
352 Walker = Walker->next;
353 if ((Walker->x.DistanceSquared(tester) - 1.) < MYEPSILON ) {
354 ListOfPoints->remove(Walker);
355 size--;
356 CPPUNIT_ASSERT_EQUAL( size, ListOfPoints->size() );
357 }
358 }
359 CPPUNIT_ASSERT_EQUAL( (size_t)0, size );
360 CPPUNIT_ASSERT_EQUAL( (size_t)0, ListOfPoints->size() );
361 CPPUNIT_ASSERT_EQUAL( true, ListOfPoints->empty() );
362 delete(ListOfPoints);
363};
Note: See TracBrowser for help on using the repository browser.