source: src/LinkedCell/unittests/LinkedCell_ModelUnitTest.cpp@ e44956

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 v1.2.4
Last change on this file since e44956 was c96423, checked in by Frederik Heber <heber@…>, 13 years ago

BUGFIX: LinkedCell_Model should use ceil, not floor for calculating array size.

  • we stumbled over it because floor(20./2.) = 9 where it should come out as 10.
  • along the way: Converted some ASSERTs in LinkedCell_ModelUnitTest to ASSERT_EQUALs.
  • Property mode set to 100644
File size: 10.0 KB
Line 
1/*
2 * Project: MoleCuilder
3 * Description: creates and alters molecular systems
4 * Copyright (C) 2012 University of Bonn. All rights reserved.
5 * Please see the LICENSE file or "Copyright notice" in builder.cpp for details.
6 */
7
8/*
9 * LinkedCell_ModelUnitTest.cpp
10 *
11 * Created on: Nov 17, 2011
12 * Author: heber
13 */
14
15// include config.h
16#ifdef HAVE_CONFIG_H
17#include <config.h>
18#endif
19
20using namespace std;
21
22#include <cppunit/CompilerOutputter.h>
23#include <cppunit/extensions/TestFactoryRegistry.h>
24#include <cppunit/ui/text/TestRunner.h>
25
26#include <list>
27
28#include "Box.hpp"
29#include "CodePatterns/Assert.hpp"
30#include "CodePatterns/Log.hpp"
31#include "LinearAlgebra/RealSpaceMatrix.hpp"
32#include "LinkedCell/LinkedCell.hpp"
33#include "LinkedCell/LinkedCell_Model.hpp"
34#include "LinkedCell/LinkedCell_Model_changeModel.hpp"
35#include "LinkedCell/LinkedCell_Model_LinkedCellArrayCache.hpp"
36#include "LinkedCell/LinkedCell_Model_Update.hpp"
37#include "LinkedCell/PointCloudAdaptor.hpp"
38#include "LinkedCell/unittests/defs.hpp"
39#include "World.hpp"
40#include "WorldTime.hpp"
41
42#include "LinkedCell_ModelUnitTest.hpp"
43
44#ifdef HAVE_TESTRUNNER
45#include "UnitTestMain.hpp"
46#endif /*HAVE_TESTRUNNER*/
47
48/********************************************** Test classes **************************************/
49
50// Registers the fixture into the 'registry'
51CPPUNIT_TEST_SUITE_REGISTRATION( LinkedCell_ModelTest );
52
53
54void LinkedCell_ModelTest::setUp()
55{
56 // failing asserts should be thrown
57 ASSERT_DO(Assert::Throw);
58
59 setVerbosity(3);
60
61 // create diag(20.) matrix
62 RealSpaceMatrix BoxM;
63 BoxM.setIdentity();
64 BoxM *= DOMAINLENGTH;
65
66 // create Box with this matrix
67 domain = new Box(BoxM);
68
69 // create LinkedCell structure with this Box
70 LC = new LinkedCell::LinkedCell_Model(EDGELENGTH, *domain);
71
72 // create a list of nodes and add to LCImpl
73 std::vector< Vector > VectorList;
74 for (size_t i=0;i<((size_t)floor(NUMBERCELLS));++i)
75 VectorList.push_back(Vector((double)i*EDGELENGTH,(double)i*EDGELENGTH,(double)i*EDGELENGTH));
76 CPPUNIT_ASSERT_EQUAL( ((size_t)floor(NUMBERCELLS)), VectorList.size() );
77 for (size_t i=0;i<VectorList.size();++i) {
78 TesselPoint * Walker = new TesselPoint();
79 Walker->setName(std::string("Walker")+toString(i));
80 Walker->setPosition(VectorList[i]);
81 NodeList.insert(Walker);
82 }
83 CPPUNIT_ASSERT_EQUAL( VectorList.size(), NodeList.size() );
84}
85
86
87void LinkedCell_ModelTest::tearDown()
88{
89 delete LC;
90 delete domain;
91
92 // remove all nodes again
93 for (PointSet::iterator iter = NodeList.begin();
94 !NodeList.empty();
95 iter = NodeList.begin()) {
96 delete *iter;
97 NodeList.erase(iter);
98 }
99
100 // delete in correct order
101 World::purgeInstance();
102 WorldTime::purgeInstance();
103}
104
105/** UnitTest for correct construction
106 */
107void LinkedCell_ModelTest::AllocationTest()
108{
109 // check that first cell is allocated
110 LinkedCell::tripleIndex index;
111 index[0] = index[1] = index[2] = 0;
112 CPPUNIT_ASSERT(LC->N->getN()(index) != NULL);
113
114 // check that very last cell is allocated
115 index[0] = index[1] = index[2] = (size_t)floor(NUMBERCELLS)-1;
116 CPPUNIT_ASSERT(LC->N->getN()(index) != NULL);
117
118}
119
120/** UnitTest for getSize()
121 */
122void LinkedCell_ModelTest::getSizeTest()
123{
124 // check getSize()
125 for(size_t i=0; i<NDIM; ++i)
126 CPPUNIT_ASSERT_EQUAL((LinkedCell::LinkedCellArray::index)floor(NUMBERCELLS), LC->getSize(i));
127#ifndef NDEBUG
128 std::cout << "The following assertion is intended and is not a failure of the code." << std::endl;
129 CPPUNIT_ASSERT_THROW( LC->getSize(4), Assert::AssertionFailure);
130#endif
131}
132
133/** UnitTest for Reset()
134 */
135void LinkedCell_ModelTest::ResetTest()
136{
137 LC->Reset();
138
139 for(size_t i=0; i<NDIM; ++i)
140 CPPUNIT_ASSERT_EQUAL((LinkedCell::LinkedCellArray::index)0, LC->getSize(i));
141}
142
143
144/** UnitTest for insertPointCloud()
145 */
146void LinkedCell_ModelTest::insertPointCloudTest()
147{
148
149 // create the linked cell structure
150 PointCloudAdaptor< PointSet > cloud(&NodeList, std::string("NodeList"));
151 LC->insertPointCloud(cloud);
152
153 // assure that we are updated before accessing internals
154 CPPUNIT_ASSERT_EQUAL( true, *(LC->N->UpToDate) );
155 // test structure
156 CPPUNIT_ASSERT_EQUAL(((size_t)floor(NUMBERCELLS)), LC->CellLookup.size());
157 LinkedCell::tripleIndex index;
158 for (size_t i=0;i<((size_t)floor(NUMBERCELLS));++i) {
159 index[0] = index[1] = index[2] = i;
160 // assert that in the destined cell we have one Walker
161 CPPUNIT_ASSERT_EQUAL((size_t)1, LC->N->getN()(index)->size());
162 }
163 index[0] = 9;
164 index[1] = index[2] = 0;
165 // assert that in the destined cell we have one Walker
166 CPPUNIT_ASSERT_EQUAL((size_t)0, LC->N->getN()(index)->size());
167
168}
169
170/** UnitTest for setPartition()
171 */
172void LinkedCell_ModelTest::setPartitionTest()
173{
174 RealSpaceMatrix Pmatrix = LC->Partition;
175 RealSpaceMatrix Dmatrix = LC->Dimensions;
176
177 LC->Reset();
178
179 LC->setPartition(2.*EDGELENGTH);
180
181 Pmatrix *= 0.5;
182 Dmatrix *= 0.5;
183
184 CPPUNIT_ASSERT_EQUAL(Pmatrix, LC->Partition);
185 CPPUNIT_ASSERT_EQUAL(Dmatrix, LC->Dimensions);
186}
187
188/** UnitTest for getStep()
189 */
190void LinkedCell_ModelTest::getStepTest()
191{
192 // zero distance
193 LinkedCell::tripleIndex index = LC->getStep(0.);
194 for (size_t i = 0; i<NDIM; ++i)
195 CPPUNIT_ASSERT( (size_t)0 == index[i]);
196 // check all possible shells on boundary
197 for (double length = EDGELENGTH; length < DOMAINLENGTH; length+=EDGELENGTH) {
198 LinkedCell::tripleIndex index = LC->getStep(length);
199 for (size_t i = 0; i<NDIM; ++i) {
200 std::cout << (size_t)(length/EDGELENGTH) << " ==" << index[i] << std::endl;
201 CPPUNIT_ASSERT_EQUAL( (LinkedCell::LinkedCellArray::index)(length/EDGELENGTH), index[i]);
202 }
203 }
204 // check all possible shells at half interval
205 for (double length = 0.5 * EDGELENGTH; length < DOMAINLENGTH; length+=EDGELENGTH) {
206 LinkedCell::tripleIndex index = LC->getStep(length);
207 for (size_t i = 0; i<NDIM; ++i)
208 CPPUNIT_ASSERT_EQUAL( (LinkedCell::LinkedCellArray::index)ceil(length/EDGELENGTH), index[i]);
209 }
210}
211
212/** UnitTest for getIndexToVector()
213 */
214void LinkedCell_ModelTest::getIndexToVectorTest()
215{
216 {
217 const Vector test(0.,0.,0.);
218 const LinkedCell::tripleIndex index = LC->getIndexToVector(test);
219 for (size_t i = 0; i<NDIM; ++i)
220 CPPUNIT_ASSERT( (size_t)0 == index[i]);
221 }
222 {
223 const Vector test(DOMAINLENGTH/2.,DOMAINLENGTH/2.,DOMAINLENGTH/2.);
224 const LinkedCell::tripleIndex index = LC->getIndexToVector(test);
225 for (size_t i = 0; i<NDIM; ++i)
226 CPPUNIT_ASSERT_EQUAL( (LinkedCell::LinkedCellArray::index)floor(DOMAINLENGTH/EDGELENGTH/2.), index[i]);
227 }
228 {
229 const Vector test(DOMAINLENGTH/2.,DOMAINLENGTH/3.,DOMAINLENGTH/4.);
230 const LinkedCell::tripleIndex index = LC->getIndexToVector(test);
231 for (size_t i = 0; i<NDIM; ++i)
232 CPPUNIT_ASSERT_EQUAL( (LinkedCell::LinkedCellArray::index)floor(DOMAINLENGTH/EDGELENGTH/(double)(i+2.)), index[i]);
233 }
234}
235
236/** UnitTest for updating nodes
237 */
238void LinkedCell_ModelTest::nodeTest()
239{
240 // create point
241 TesselPoint * Walker = new TesselPoint();
242 Walker->setName(std::string("Walker9"));
243 Walker->setPosition(Vector(9.8,7.6,5.4));
244 PointCloudAdaptor< PointSet > cloud(&NodeList, std::string("NodeList"));
245 LC->insertPointCloud(cloud);
246
247 // check addNode
248 {
249 LC->addNode(Walker);
250 // assure that we are updated before accessing internals
251 CPPUNIT_ASSERT_EQUAL( true, *(LC->N->UpToDate) );
252 CPPUNIT_ASSERT_EQUAL( NodeList.size()+1, LC->CellLookup.size());
253 LinkedCell::tripleIndex index1 = LC->getIndexToVector(Walker->getPosition());
254 const LinkedCell::tripleIndex &index2 = LC->CellLookup[Walker]->getIndices();
255 CPPUNIT_ASSERT(index1 == index2);
256 }
257
258 // check moveNode
259 {
260 LinkedCell::tripleIndex index1 = LC->getIndexToVector(Walker->getPosition());
261 const LinkedCell::tripleIndex &index2 = LC->CellLookup[Walker]->getIndices();
262 Walker->setPosition(Vector(0.,0.,0.));
263 LinkedCell::tripleIndex newindex1 = LC->getIndexToVector(Walker->getPosition());
264 CPPUNIT_ASSERT( index1 != newindex1);
265 // we have to call moveNode ourselves, as we have just added TesselPoints, not via World
266 LC->moveNode(Walker);
267 // assure that we are updated before accessing internals
268 CPPUNIT_ASSERT_EQUAL( true, *(LC->N->UpToDate) );
269 const LinkedCell::tripleIndex &newindex2 = LC->CellLookup[Walker]->getIndices();
270 CPPUNIT_ASSERT( index2 != newindex2);
271 }
272
273 // check deleteNode
274 {
275 LC->deleteNode(Walker);
276 // assure that we are updated before accessing internals
277 CPPUNIT_ASSERT_EQUAL( true, *(LC->N->UpToDate) );
278 CPPUNIT_ASSERT_EQUAL( NodeList.size(), LC->CellLookup.size());
279 }
280
281 delete Walker;
282}
283
284/** UnitTest whether lazy updates are working.
285 */
286void LinkedCell_ModelTest::lazyUpdatesTest()
287{
288 // create point
289 TesselPoint * Walker = new TesselPoint();
290 Walker->setName(std::string("Walker9"));
291 Walker->setPosition(Vector(9.8,7.6,5.4));
292 TesselPoint * Walker2 = new TesselPoint();
293 Walker2->setName(std::string("Walker10"));
294 Walker2->setPosition(Vector(9.8,7.6,5.4));
295 PointCloudAdaptor< PointSet > cloud(&NodeList, std::string("NodeList"));
296 LC->insertPointCloud(cloud);
297
298 // initial read operation
299 {
300 CPPUNIT_ASSERT( !NodeList.empty() );
301 LinkedCell::tripleIndex index = LC->getIndexToVector((*NodeList.begin())->getPosition());
302 const size_t size = LC->N->getN()(index)->size(); // this will cause the update
303 CPPUNIT_ASSERT( size >= (size_t)1 );
304 }
305
306 // do some write ops
307 LC->addNode(Walker);
308 LC->addNode(Walker2);
309 CPPUNIT_ASSERT( LC->Changes->queue.find(Walker) != LC->Changes->queue.end() );
310 LinkedCell::LinkedCell_Model::Update *update = LC->Changes->queue[Walker];
311 Walker->setPosition(Vector(0.,0.,0.));
312 LC->moveNode(Walker);
313
314 // check that they all reside in cache and nothing is changed so far
315 CPPUNIT_ASSERT( LC->Changes->queue.size() > (size_t)0 );
316
317 // check that add priority is prefered over move
318 CPPUNIT_ASSERT( LC->Changes->queue[Walker] == update );
319
320 // perform read op
321 {
322 LinkedCell::tripleIndex index = LC->getIndexToVector(Walker->getPosition());
323 CPPUNIT_ASSERT( LC->N->getN()(index)->size() >= (size_t)1 );
324 }
325
326 // check that cache is emptied
327 CPPUNIT_ASSERT_EQUAL( LC->Changes->queue.size(), (size_t)0 );
328
329 delete Walker;
330 delete Walker2;
331}
Note: See TracBrowser for help on using the repository browser.