source: src/Fragmentation/Summation/SetValues/unittests/SamplingGridUnitTest.cpp@ cb30d9

Action_Thermostats Add_AtomRandomPerturbation Add_FitFragmentPartialChargesAction Add_RotateAroundBondAction Add_SelectAtomByNameAction Adding_Graph_to_ChangeBondActions Adding_MD_integration_tests Adding_StructOpt_integration_tests Automaking_mpqc_open AutomationFragmentation_failures Candidate_v1.5.4 Candidate_v1.6.0 Candidate_v1.6.1 Candidate_v1.7.0 ChangeBugEmailaddress ChangingTestPorts ChemicalSpaceEvaluator 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_ChargeSampling_PBC Fix_ChronosMutex Fix_FitPartialCharges Fix_FitPotential_needs_atomicnumbers Fix_ForceAnnealing Fix_IndependentFragmentGrids Fix_ParseParticles Fix_ParseParticles_split_forward_backward_Actions Fix_StatusMsg Fix_StepWorldTime_single_argument Fix_Verbose_Codepatterns ForceAnnealing_goodresults ForceAnnealing_oldresults ForceAnnealing_tocheck ForceAnnealing_with_BondGraph ForceAnnealing_with_BondGraph_continued ForceAnnealing_with_BondGraph_continued_betteresults ForceAnnealing_with_BondGraph_contraction-expansion GeometryObjects Gui_displays_atomic_force_velocity IndependentFragmentGrids IndependentFragmentGrids_IndividualZeroInstances IndependentFragmentGrids_IntegrationTest IndependentFragmentGrids_Sole_NN_Calculation JobMarket_RobustOnKillsSegFaults JobMarket_StableWorkerPool JobMarket_unresolvable_hostname_fix ODR_violation_mpqc_open PartialCharges_OrthogonalSummation PythonUI_with_named_parameters QtGui_reactivate_TimeChanged_changes Recreated_GuiChecks RotateToPrincipalAxisSystem_UndoRedo StoppableMakroAction Subpackage_CodePatterns Subpackage_JobMarket Subpackage_LinearAlgebra Subpackage_levmar Subpackage_mpqc_open Subpackage_vmg ThirdParty_MPQC_rebuilt_buildsystem TrajectoryDependenant_MaxOrder TremoloParser_IncreasedPrecision TremoloParser_MultipleTimesteps Ubuntu_1604_changes stable
Last change on this file since cb30d9 was cb30d9, checked in by Frederik Heber <heber@…>, 9 years ago

Extended SamplingGridProperties::isCompatible() to allow more finely resolved grids, added ::isEquivalent().

  • isEquivalent contains the old check and we replaced isCompatible() by it.
  • Property mode set to 100644
File size: 20.9 KB
RevLine 
[c889b7]1/*
2 * Project: MoleCuilder
3 * Description: creates and alters molecular systems
4 * Copyright (C) 2012 University of Bonn. All rights reserved.
[5aaa43]5 * Copyright (C) 2013 Frederik Heber. All rights reserved.
[c889b7]6 *
7 *
8 * This file is part of MoleCuilder.
9 *
10 * MoleCuilder is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation, either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * MoleCuilder is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with MoleCuilder. If not, see <http://www.gnu.org/licenses/>.
22 */
23
24/*
25 * SamplingGridUnitTest.cpp
26 *
27 * Created on: Jul 29, 2012
28 * Author: heber
29 */
30
31// include config.h
32#ifdef HAVE_CONFIG_H
33#include <config.h>
34#endif
35
36using namespace std;
37
38#include <cppunit/CompilerOutputter.h>
39#include <cppunit/extensions/TestFactoryRegistry.h>
40#include <cppunit/ui/text/TestRunner.h>
41
[955051]42// include headers that implement a archive in simple text format
43#include <boost/archive/text_oarchive.hpp>
44#include <boost/archive/text_iarchive.hpp>
45
[c889b7]46#include "SamplingGridUnitTest.hpp"
47
48#include "CodePatterns/Assert.hpp"
49
50#include <boost/assign.hpp>
[c6355f]51#include <cmath>
52#include <numeric>
[c889b7]53
54#ifdef HAVE_TESTRUNNER
55#include "UnitTestMain.hpp"
56#endif /*HAVE_TESTRUNNER*/
57
58using namespace boost::assign;
59
60/********************************************** Test classes **************************************/
61
62const double grid_value=1.;
63
[5b1e5e]64#define NUMBEROFSAMPLES(n) (size_t)(pow(pow(2,n),(int)NDIM))
65#define DOMAINVOLUME(l) (size_t)pow(l,(int)NDIM)
[3d9a8d]66
[c889b7]67// Registers the fixture into the 'registry'
68CPPUNIT_TEST_SUITE_REGISTRATION( SamplingGridTest );
69
70
71void SamplingGridTest::setUp()
72{
73 // failing asserts should be thrown
74 ASSERT_DO(Assert::Throw);
75
76 // create the grid
[5b1e5e]77 const double begin[NDIM] = { 0., 0., 0. };
78 const double end[NDIM] = { 1., 1., 1. };
[c6355f]79 for (size_t i=0; i< DOMAINVOLUME(1)*NUMBEROFSAMPLES(2); ++i)
[c889b7]80 values += grid_value;
[3d9a8d]81 grid = new SamplingGrid(begin, end, 2, values);
[c889b7]82 CPPUNIT_ASSERT_EQUAL( grid_value, *(grid->sampled_grid.begin()) );
83}
84
85
86void SamplingGridTest::tearDown()
87{
88 delete grid;
89}
90
[cb30d9]91/** UnitTest on equivalent combination of props and values
[c889b7]92 */
[cb30d9]93void SamplingGridTest::equivalentGrids_Test()
[c889b7]94{
95 // check illegal grid
[5b1e5e]96 const double begin[NDIM] = { 0., 0., 0. };
[cb30d9]97 const double end[NDIM] = { 1., 1., 1. };
[c6355f]98 SamplingGridProperties illegal_props(begin, end, 5);
[1a00bb]99 SamplingGridProperties legal_props(begin, end, 2);
[cb30d9]100 CPPUNIT_ASSERT( !grid->isEquivalent(illegal_props) );
101 CPPUNIT_ASSERT( grid->isEquivalent(legal_props) );
102 SamplingGrid::sampledvalues_t illegal_values;
103 for (size_t i=0; i< NUMBEROFSAMPLES(1); ++i)
104 illegal_values += 1.5;
105 SamplingGrid::sampledvalues_t legal_values;
106 for (size_t i=0; i< NUMBEROFSAMPLES(2); ++i)
107 legal_values += 1.5;
108#ifndef NDEBUG
109 // throws because props and size of illegal_values don't match
110 std::cout << "The following assertion is intended and does not indicate a failure of the test." << std::endl;
111 CPPUNIT_ASSERT_THROW( SamplingGrid illegal_grid(illegal_props, illegal_values), Assert::AssertionFailure );
112 std::cout << "The following assertion is intended and does not indicate a failure of the test." << std::endl;
113 CPPUNIT_ASSERT_THROW( SamplingGrid illegal_grid(legal_props, illegal_values), Assert::AssertionFailure );
114 std::cout << "The following assertion is intended and does not indicate a failure of the test." << std::endl;
115 CPPUNIT_ASSERT_THROW( SamplingGrid illegal_grid(illegal_props, legal_values), Assert::AssertionFailure );
116#endif
117 // check that grid is still the same
118 for (SamplingGrid::sampledvalues_t::const_iterator iter = grid->sampled_grid.begin();
119 iter != grid->sampled_grid.end(); ++iter)
120 CPPUNIT_ASSERT_EQUAL( grid_value, *iter );
121}
122
123/** UnitTest on compatible combination of props and values
124 */
125void SamplingGridTest::compatibleGrids_Test()
126{
127 // check illegal grid
128 const double begin[NDIM] = { 0., 0., 0. };
129 const double end[NDIM] = { 1., 1., 1. };
130 const double otherend[NDIM] = { 2.5, 2.5, 2.5 };
131 SamplingGridProperties illegal_props(begin, otherend, 5);
132 SamplingGridProperties legal_props(begin, end, 3);
[c889b7]133 CPPUNIT_ASSERT( !grid->isCompatible(illegal_props) );
[1a00bb]134 CPPUNIT_ASSERT( grid->isCompatible(legal_props) );
[c889b7]135 SamplingGrid::sampledvalues_t illegal_values;
136 for (size_t i=0; i< NUMBEROFSAMPLES(1); ++i)
137 illegal_values += 1.5;
[1a00bb]138 SamplingGrid::sampledvalues_t legal_values;
139 for (size_t i=0; i< NUMBEROFSAMPLES(2); ++i)
[c6355f]140 legal_values += 1.5;
[c889b7]141#ifndef NDEBUG
[1a00bb]142 // throws because props and size of illegal_values don't match
143 std::cout << "The following assertion is intended and does not indicate a failure of the test." << std::endl;
144 CPPUNIT_ASSERT_THROW( SamplingGrid illegal_grid(illegal_props, illegal_values), Assert::AssertionFailure );
[3d9a8d]145 std::cout << "The following assertion is intended and does not indicate a failure of the test." << std::endl;
[1a00bb]146 CPPUNIT_ASSERT_THROW( SamplingGrid illegal_grid(legal_props, illegal_values), Assert::AssertionFailure );
147 std::cout << "The following assertion is intended and does not indicate a failure of the test." << std::endl;
148 CPPUNIT_ASSERT_THROW( SamplingGrid illegal_grid(illegal_props, legal_values), Assert::AssertionFailure );
[c889b7]149#endif
150 // check that grid is still the same
151 for (SamplingGrid::sampledvalues_t::const_iterator iter = grid->sampled_grid.begin();
152 iter != grid->sampled_grid.end(); ++iter)
153 CPPUNIT_ASSERT_EQUAL( grid_value, *iter );
154}
155
[c0e8fb]156/** UnitTest for isCongruent()
157 */
158void SamplingGridTest::isCongruent_Test()
159{
[5b1e5e]160 const double begin[NDIM] = { 0., 0., 0. };
161 const double end[NDIM] = { 2., 2., 2. };
162 const double otherbegin[NDIM] = { 0.1, 0.1, 0.1 };
163 const double otherend[NDIM] = { 1., 1., 1. };
[c0e8fb]164 SamplingGridProperties illegal_begin_props(otherbegin, end, 5);
165 SamplingGridProperties illegal_end_props(begin, otherend, 5);
166 SamplingGridProperties illegal_level_props(begin, end, 5);
167 SamplingGridProperties legal_props(begin, end, 2);
168
169 // differing windows
[5b1e5e]170// const double begin_window[NDIM] = { 0.5, 0.5, 0.5 };
171// const double end_window[NDIM] = { 1., 1., 1. };
172 const double otherbegin_window[NDIM] = { 0.45, 0.45, 0.45 };
173 const double otherend_window[NDIM] = { 1.05, 1.05, 1.05 };
[c0e8fb]174
175 // check that incompatible grid are also incongruent
176 SamplingGrid default_grid(legal_props);
177 // note that we always construct a temporary SamplingGrid from given props
[cb30d9]178 CPPUNIT_ASSERT( default_grid.isEquivalent(illegal_begin_props) == default_grid.isCongruent(illegal_begin_props));
179 CPPUNIT_ASSERT( default_grid.isEquivalent(illegal_end_props) == default_grid.isCongruent(illegal_end_props));
180 CPPUNIT_ASSERT( default_grid.isEquivalent(illegal_level_props) == default_grid.isCongruent(illegal_level_props));
181 CPPUNIT_ASSERT( default_grid.isEquivalent(legal_props) == default_grid.isCongruent(legal_props) );
[c0e8fb]182
183 default_grid.setWindowSize(begin, end);
184 // same window
185 {
186 SamplingGrid illegal_begin_grid(illegal_begin_props);
187 SamplingGrid illegal_end_grid(illegal_end_props);
188 SamplingGrid illegal_level_grid(illegal_level_props);
189 SamplingGrid legal_grid(legal_props);
190 illegal_begin_grid.setWindowSize(begin, end);
191 illegal_end_grid.setWindowSize(begin, end);
192 illegal_level_grid.setWindowSize(begin, end);
193 legal_grid.setWindowSize(begin, end);
194 CPPUNIT_ASSERT( !illegal_begin_grid.isCongruent(default_grid) );
195 CPPUNIT_ASSERT( !illegal_end_grid.isCongruent(default_grid) );
196 CPPUNIT_ASSERT( !illegal_level_grid.isCongruent(default_grid) );
197 CPPUNIT_ASSERT( legal_grid.isCongruent(default_grid) );
198 }
199
200 // different begin
201 {
202 SamplingGrid illegal_begin_grid(illegal_begin_props);
203 SamplingGrid illegal_end_grid(illegal_end_props);
204 SamplingGrid illegal_level_grid(illegal_level_props);
205 SamplingGrid legal_grid(legal_props);
206 illegal_begin_grid.setWindowSize(otherbegin_window, end);
207 illegal_end_grid.setWindowSize(otherbegin_window, end);
208 illegal_level_grid.setWindowSize(otherbegin_window, end);
209 legal_grid.setWindowSize(begin, end);
210 CPPUNIT_ASSERT( !illegal_begin_grid.isCongruent(legal_grid) );
211 CPPUNIT_ASSERT( !illegal_end_grid.isCongruent(legal_grid) );
212 CPPUNIT_ASSERT( !illegal_level_grid.isCongruent(legal_grid) );
213 CPPUNIT_ASSERT( legal_grid.isCongruent(default_grid) );
214 }
215
216 // different end
217 {
218 SamplingGrid illegal_begin_grid(illegal_begin_props);
219 SamplingGrid illegal_end_grid(illegal_end_props);
220 SamplingGrid illegal_level_grid(illegal_level_props);
221 SamplingGrid legal_grid(legal_props);
222 illegal_begin_grid.setWindowSize(begin, otherend_window);
223 illegal_end_grid.setWindowSize(begin, otherend_window);
224 illegal_level_grid.setWindowSize(begin, otherend_window);
225 legal_grid.setWindowSize(begin, end);
226 CPPUNIT_ASSERT( !illegal_begin_grid.isCongruent(legal_grid) );
227 CPPUNIT_ASSERT( !illegal_end_grid.isCongruent(legal_grid) );
228 CPPUNIT_ASSERT( !illegal_level_grid.isCongruent(legal_grid) );
229 CPPUNIT_ASSERT( legal_grid.isCongruent(default_grid) );
230 }
231
232 // different begin and end
233 {
234 SamplingGrid illegal_begin_grid(illegal_begin_props);
235 SamplingGrid illegal_end_grid(illegal_end_props);
236 SamplingGrid illegal_level_grid(illegal_level_props);
237 SamplingGrid legal_grid(legal_props);
238 illegal_begin_grid.setWindowSize(otherbegin_window, otherend_window);
239 illegal_end_grid.setWindowSize(otherbegin_window, otherend_window);
240 illegal_level_grid.setWindowSize(otherbegin_window, otherend_window);
241 legal_grid.setWindowSize(begin, end);
242 CPPUNIT_ASSERT( !illegal_begin_grid.isCongruent(legal_grid) );
243 CPPUNIT_ASSERT( !illegal_end_grid.isCongruent(legal_grid) );
244 CPPUNIT_ASSERT( !illegal_level_grid.isCongruent(legal_grid) );
245 CPPUNIT_ASSERT( legal_grid.isCongruent(default_grid) );
246 }
247}
248
[c6355f]249/** UnitTest for integral()
250 */
251void SamplingGridTest::integral_Test()
252{
253 double sum = 0.;
254 sum = std::accumulate( grid->sampled_grid.begin(), grid->sampled_grid.end(), sum );
255 CPPUNIT_ASSERT_EQUAL( sum*grid->getVolume()/grid->getWindowGridPoints(), grid->integral() );
256}
257
258/** UnitTest for getVolume()
[3d9a8d]259 */
260void SamplingGridTest::getVolume_Test()
261{
262 CPPUNIT_ASSERT_EQUAL( 1., grid->getVolume() );
263}
264
[c6355f]265/** UnitTest for getWindowSize()
[1a00bb]266 */
267void SamplingGridTest::getWindowSize_Test()
268{
269 // check size of default grid
[e2404f]270 CPPUNIT_ASSERT_EQUAL( (size_t)(NUMBEROFSAMPLES(grid->level)), grid->getWindowGridPoints() );
[1a00bb]271
272 // create another one and check its size, too
[5b1e5e]273 const double begin[NDIM] = { 0., 0., 0. };
274 const double end[NDIM] = { 1., 1., 1. };
[1a00bb]275 for (size_t level = 3; level<=6; ++level) {
276 values.clear();
277 for (size_t i=0; i< NUMBEROFSAMPLES(level); ++i)
278 values += grid_value;
279 delete grid;
[e2404f]280 // use other pointer in case something fails
281 SamplingGrid *tmpgrid = new SamplingGrid(begin, end, level, values);
282 grid = tmpgrid;
[1a00bb]283 CPPUNIT_ASSERT_EQUAL( (size_t)NUMBEROFSAMPLES(level), grid->getWindowGridPoints() );
284 }
285}
286
287/** UnitTest for extendWindow()
288 */
289void SamplingGridTest::extendWindow_Test()
290{
[c6355f]291 // we have a grid with size of one, extend to twice the size and check
[5b1e5e]292 const double begin[NDIM] = { 0., 0., 0. };
[de6dfb]293 const double size = 2.;
[5b1e5e]294 const double end[NDIM] = { size, size, size };
295 double offset[NDIM];
[c6355f]296 for (offset[0] = 0.; offset[0] <= 1.; offset[0] += .5)
297 for (offset[1] = 0.; offset[1] <= 1.; offset[1] += .5)
298 for (offset[2] = 0.; offset[2] <= 1.; offset[2] += .5) {
[5b1e5e]299 const double window_begin[NDIM] = { 0.+offset[0], 0.+offset[1], 0.+offset[2]};
300 const double window_end[NDIM] = { 1.+offset[0], 1.+offset[1], 1.+offset[2]};
[c6355f]301 SamplingGrid newgrid(begin, end, 2);
302 newgrid.setWindowSize(window_begin, window_end);
303 // resize values by hand to new window size. Otherwise they get zero'd.
304 newgrid.sampled_grid = values;
305 newgrid.sampled_grid.resize(NUMBEROFSAMPLES(1));
306 newgrid.extendWindow(begin, end);
307
308 // check integral
309 CPPUNIT_ASSERT_EQUAL(
310 grid_value/(NUMBEROFSAMPLES(grid->level)/NUMBEROFSAMPLES(grid->level)),
311 grid->integral()
312 );
313
314 // check number of points
315 CPPUNIT_ASSERT_EQUAL(
316 (size_t)NUMBEROFSAMPLES(grid->level),
317 grid->getWindowGridPoints()
318 );
319 }
[de6dfb]320}
321
322/** UnitTest for extendWindow() with asymmetric values
323 */
324void SamplingGridTest::extendWindow_asymmetric_Test()
325{
[c6355f]326 std::cout << "SamplingGridTest::extendWindow_asymmetric_Test()" << std::endl;
[5b1e5e]327 const double begin[NDIM] = { 0., 0., 0. };
328 const double end[NDIM] = { 2., 2., 2. };
329 double offset[NDIM];
[c6355f]330 for (offset[0] = 0.; offset[0] <= 1.; offset[0] += .5)
331 for (offset[1] = 0.; offset[1] <= 1.; offset[1] += .5)
332 for (offset[2] = 0.; offset[2] <= 1.; offset[2] += .5) {
[5b1e5e]333 const double window_begin[NDIM] = { 0.+offset[0], 0.+offset[1], 0.+offset[2]};
334 const double window_end[NDIM] = { 1.+offset[0], 1.+offset[1], 1.+offset[2]};
[c6355f]335 SamplingGrid newgrid(begin, end, 2);
336 CPPUNIT_ASSERT_EQUAL( (size_t)0, newgrid.getWindowGridPoints() );
337 newgrid.setWindowSize(window_begin, window_end);
338 // window size is only half of domain size
[5b1e5e]339 const size_t max_samples = NUMBEROFSAMPLES(newgrid.level)*pow(0.5,(int)NDIM);
[c6355f]340 for (size_t i=0; i< max_samples; ++i)
341 newgrid.sampled_grid += grid_value*i;
342 const size_t sum_weight = (max_samples)*(max_samples-1)/2;
343 const double integral = newgrid.integral();
344 newgrid.extendWindow(begin, end);
345
346 // check that integral has remained the same
347 CPPUNIT_ASSERT_EQUAL( integral, newgrid.integral() );
348 CPPUNIT_ASSERT_EQUAL( grid_value*sum_weight/DOMAINVOLUME(2), newgrid.integral() );
349 }
[1a00bb]350}
351
352/** UnitTest for addOntoWindow()
353 */
354void SamplingGridTest::addOntoWindow_Test()
355{
356 // first window is from (0,0,0) to (1,1,1)
357 CPPUNIT_ASSERT_EQUAL( 1.*grid_value, grid->integral() );
358
[c6355f]359 // create values for half-sized window
[1a00bb]360 values.clear();
[5b1e5e]361 for (size_t i=0; i< (size_t)pow(.5*pow(2,2),(int)NDIM); ++i)
[1a00bb]362 values += grid_value;
[c6355f]363
[1a00bb]364 // check that too large a window throws
365#ifndef NDEBUG
[5b1e5e]366 const double begin[NDIM] = { .5, .5, .5 };
367 const double wrongend[NDIM] = { 1.5, 1.5, 1.5 };
[1a00bb]368 std::cout << "The following assertion is intended and does not indicate a failure of the test." << std::endl;
[de6dfb]369 CPPUNIT_ASSERT_THROW( grid->addOntoWindow(begin, wrongend, values, +1.), Assert::AssertionFailure );
[1a00bb]370#endif
371
[c6355f]372 // create another window from (.5,.5,.5) to (1., 1., 1.)
[5b1e5e]373 double offset[NDIM];
[c6355f]374 for (offset[0] = 0.; offset[0] <= .5; offset[0] += .5)
375 for (offset[1] = 0.; offset[1] <= .5; offset[1] += .5)
376 for (offset[2] = 0.; offset[2] <= .5; offset[2] += .5) {
[5b1e5e]377 const double window_begin[NDIM] = { 0.+offset[0], 0.+offset[1], 0.+offset[2]};
378 const double window_end[NDIM] = { .5+offset[0], .5+offset[1], .5+offset[2]};
[c6355f]379
380 SamplingGrid newgrid(*grid);
381 // now perform working operation
382 newgrid.addOntoWindow(window_begin, window_end, values, +1.);
383
384 // check integral to be one and one eighth times the old value
[5b1e5e]385 CPPUNIT_ASSERT_EQUAL( (1.+pow(.5,(int)NDIM))*grid_value, newgrid.integral() );
[c6355f]386 }
[1a00bb]387}
388
[de6dfb]389/** UnitTest for addOntoWindow() with asymmetric values
390 */
391void SamplingGridTest::addOntoWindow_asymmetric_Test()
392{
[c6355f]393 const size_t size = grid->end[0]-grid->begin[0];
[de6dfb]394 // check with asymmetric values
395 grid->sampled_grid.clear();
[c6355f]396 grid->sampled_grid.resize(DOMAINVOLUME(size)*NUMBEROFSAMPLES(grid->level), 0.);
[de6dfb]397
398 for (size_t i=0;i<grid->level*(grid->end[0]-grid->begin[0]);++i)
399 grid->sampled_grid[(i*2+0)*2+0] += .5*grid_value;
400 for (size_t i=0;i<grid->level*(grid->end[1]-grid->begin[1]);++i)
401 grid->sampled_grid[(0*2+i)*2+0] += 1.*grid_value;
402 for (size_t i=0;i<grid->level*(grid->end[2]-grid->begin[2]);++i)
403 grid->sampled_grid[(0*2+0)*2+i] += 1.5*grid_value;
404
405 const double integral = grid->integral();
[c6355f]406
407 // now perform working operation
408 grid->addOntoWindow(grid->begin, grid->end, values, +1.);
409 // values is equal to integral of 1.
410 CPPUNIT_ASSERT_EQUAL( 1.+integral, grid->integral() );
[de6dfb]411}
412
[c889b7]413/** UnitTest for operator+=()
414 */
415void SamplingGridTest::operatorPlusEqual_Test()
416{
417 // create other grid
[5b1e5e]418 const double begin[NDIM] = { 0., 0., 0. };
419 const double end[NDIM] = { 1., 1., 1. };
[c889b7]420 SamplingGrid::sampledvalues_t othervalues;
421 const double othergrid_value = 1.5;
422 for (size_t i=0; i< NUMBEROFSAMPLES(2); ++i)
423 othervalues += othergrid_value;
[3d9a8d]424 SamplingGrid othergrid(begin, end, 2, othervalues);
[c889b7]425 CPPUNIT_ASSERT_EQUAL( othergrid_value, *(othergrid.sampled_grid.begin()) );
426
427 // perform operation
428 CPPUNIT_ASSERT_NO_THROW( *grid += othergrid );
429
430 // check the contents of the grid
431 const double sum = grid_value+othergrid_value;
432 for (SamplingGrid::sampledvalues_t::const_iterator iter = grid->sampled_grid.begin();
433 iter != grid->sampled_grid.end(); ++iter)
434 CPPUNIT_ASSERT_EQUAL( sum, *iter );
435}
436
437/** UnitTest for operator-=()
438 */
439void SamplingGridTest::operatorMinusEqual_Test()
440{
441 // create other grid
[5b1e5e]442 const double begin[NDIM] = { 0., 0., 0. };
443 const double end[NDIM] = { 1., 1., 1. };
[c889b7]444 SamplingGrid::sampledvalues_t othervalues;
445 const double othergrid_value = 1.5;
446 for (size_t i=0; i< NUMBEROFSAMPLES(2); ++i)
447 othervalues += othergrid_value;
[3d9a8d]448 SamplingGrid othergrid(begin, end, 2, othervalues);
[c889b7]449 CPPUNIT_ASSERT_EQUAL( othergrid_value, *(othergrid.sampled_grid.begin()) );
450
451 // perform operation
452 CPPUNIT_ASSERT_NO_THROW( *grid -= othergrid );
453
454 // check the contents of the grid
455 const double difference = grid_value-othergrid_value;
456 for (SamplingGrid::sampledvalues_t::const_iterator iter = grid->sampled_grid.begin();
457 iter != grid->sampled_grid.end(); ++iter)
458 CPPUNIT_ASSERT_EQUAL( difference, *iter );
459}
460
[955051]461
462/** UnitTest for operator==()
463 */
464void SamplingGridTest::equality_Test()
465{
[5b1e5e]466 const double begin[NDIM] = { 0., 0., 0. };
467 const double otherbegin[NDIM] = { .5, 0.1, -0.5 };
468 const double end[NDIM] = { 1., 1., 1. };
469 const double otherend[NDIM] = { 2., 2., 2. };
470 const double begin_window[NDIM] = { 0., 0., 0. };
471 const double otherbegin_window[NDIM] = { .75, 0.1, 0. };
472 const double end_window[NDIM] = { 1., 1., 1. };
473 const double otherend_window[NDIM] = { .9, .9, .9 };
[955051]474
475 // create other grid
476 SamplingGrid samegrid(begin, end, 2, values);
477 SamplingGrid differentbegin(otherbegin, end, 2, values);
478 SamplingGrid differentend(begin, otherend, 2, values);
479 SamplingGrid::sampledvalues_t morevalues;
480 {
481 const double othergrid_value = 1.5;
482 for (size_t i=0; i< NUMBEROFSAMPLES(4); ++i)
483 morevalues += othergrid_value;
484 }
485 SamplingGrid differentlevel(begin, end, 4, morevalues);
486 SamplingGrid::sampledvalues_t othervalues;
487 {
488 const double othergrid_value = 1.5;
489 for (size_t i=0; i< NUMBEROFSAMPLES(2); ++i)
490 othervalues += othergrid_value;
491 }
492 SamplingGrid differentvalues(begin, end, 2, othervalues);
493
494 // check for same level, begin, and end
495 CPPUNIT_ASSERT( *grid == *grid );
496 CPPUNIT_ASSERT( *grid == samegrid );
497 CPPUNIT_ASSERT( samegrid == *grid);
498 CPPUNIT_ASSERT( *grid != differentbegin);
499 CPPUNIT_ASSERT( differentbegin != *grid);
500 CPPUNIT_ASSERT( *grid != differentend );
501 CPPUNIT_ASSERT( differentend != *grid );
502 CPPUNIT_ASSERT( *grid != differentlevel );
503 CPPUNIT_ASSERT( differentlevel != *grid );
504
505 // check for all but differing values
506 CPPUNIT_ASSERT( *grid != differentvalues );
507 CPPUNIT_ASSERT( differentvalues != *grid );
508
509 // check for different windows
510 SamplingGrid differentwindowbegin(begin, end, 2);
511 differentwindowbegin.setWindow(otherbegin_window, end_window);
512 SamplingGrid differentwindowend(begin, end, 2);
513 differentwindowend.setWindow(begin_window, otherend_window);
514 CPPUNIT_ASSERT( *grid != differentwindowbegin );
515 CPPUNIT_ASSERT( differentwindowbegin != *grid );
516 CPPUNIT_ASSERT( *grid != differentwindowend );
517 CPPUNIT_ASSERT( differentwindowend != *grid );
518
519 // check against ZeroInstance
520 CPPUNIT_ASSERT( *grid != ZeroInstance<SamplingGrid>() );
521 CPPUNIT_ASSERT( samegrid != ZeroInstance<SamplingGrid>() );
522 CPPUNIT_ASSERT( differentbegin != ZeroInstance<SamplingGrid>() );
523 CPPUNIT_ASSERT( differentend != ZeroInstance<SamplingGrid>() );
524 CPPUNIT_ASSERT( differentlevel != ZeroInstance<SamplingGrid>() );
525 CPPUNIT_ASSERT( differentvalues != ZeroInstance<SamplingGrid>() );
526 CPPUNIT_ASSERT( differentwindowbegin != ZeroInstance<SamplingGrid>() );
527 CPPUNIT_ASSERT( differentwindowend != ZeroInstance<SamplingGrid>() );
528}
529
530/** UnitTest for serialization
531 */
532void SamplingGridTest::serializeTest()
533{
534 // serialize
535 std::stringstream outputstream;
536 boost::archive::text_oarchive oa(outputstream);
537 oa << grid;
538
539 // deserialize
540 SamplingGrid *samegrid = NULL;
541 std::stringstream returnstream(outputstream.str());
542 boost::archive::text_iarchive ia(returnstream);
543 ia >> samegrid;
544
545 CPPUNIT_ASSERT( samegrid != NULL );
546 CPPUNIT_ASSERT( *grid == *samegrid );
547
548 delete samegrid;
549}
[5b1e5e]550
Note: See TracBrowser for help on using the repository browser.