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

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 06653a was 06653a, checked in by Frederik Heber <heber@…>, 9 years ago

SamplingGrid has downsample() function.

  • we need this when small fragment grids are calculated at a higher resolution than full_sample grid is supposed to be calculated at.
  • extended SamplingGridUnitTest to downsample() function, needed special attention on boundary of grid.
  • Property mode set to 100644
File size: 25.3 KB
Line 
1/*
2 * Project: MoleCuilder
3 * Description: creates and alters molecular systems
4 * Copyright (C) 2012 University of Bonn. All rights reserved.
5 * Copyright (C) 2013 Frederik Heber. All rights reserved.
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
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
46#include "SamplingGridUnitTest.hpp"
47
48#include "CodePatterns/Assert.hpp"
49
50#include <boost/assign.hpp>
51
52#include <cmath>
53#include <numeric>
54
55#ifdef HAVE_TESTRUNNER
56#include "UnitTestMain.hpp"
57#endif /*HAVE_TESTRUNNER*/
58
59using namespace boost::assign;
60
61/********************************************** Test classes **************************************/
62
63const double grid_value=1.;
64
65#define NUMBEROFSAMPLES(n) (size_t)(pow(pow(2,n),(int)NDIM))
66#define DOMAINVOLUME(l) (size_t)pow(l,(int)NDIM)
67
68// Registers the fixture into the 'registry'
69CPPUNIT_TEST_SUITE_REGISTRATION( SamplingGridTest );
70
71
72void SamplingGridTest::setUp()
73{
74 // failing asserts should be thrown
75 ASSERT_DO(Assert::Throw);
76
77 // create the grid
78 const double begin[NDIM] = { 0., 0., 0. };
79 const double end[NDIM] = { 1., 1., 1. };
80 for (size_t i=0; i< DOMAINVOLUME(1)*NUMBEROFSAMPLES(2); ++i)
81 values += grid_value;
82 grid = new SamplingGrid(begin, end, 2, values);
83 CPPUNIT_ASSERT_EQUAL( grid_value, *(grid->sampled_grid.begin()) );
84}
85
86
87void SamplingGridTest::tearDown()
88{
89 delete grid;
90}
91
92/** UnitTest on equivalent combination of props and values
93 */
94void SamplingGridTest::equivalentGrids_Test()
95{
96 // check illegal grid
97 const double begin[NDIM] = { 0., 0., 0. };
98 const double end[NDIM] = { 1., 1., 1. };
99 SamplingGridProperties illegal_props(begin, end, 5);
100 SamplingGridProperties legal_props(begin, end, 2);
101 CPPUNIT_ASSERT( !grid->isEquivalent(illegal_props) );
102 CPPUNIT_ASSERT( grid->isEquivalent(legal_props) );
103 SamplingGrid::sampledvalues_t illegal_values;
104 for (size_t i=0; i< NUMBEROFSAMPLES(1); ++i)
105 illegal_values += 1.5;
106 SamplingGrid::sampledvalues_t legal_values;
107 for (size_t i=0; i< NUMBEROFSAMPLES(2); ++i)
108 legal_values += 1.5;
109#ifndef NDEBUG
110 // throws because props and size of illegal_values don't match
111 std::cout << "The following assertion is intended and does not indicate a failure of the test." << std::endl;
112 CPPUNIT_ASSERT_THROW( SamplingGrid illegal_grid(illegal_props, illegal_values), Assert::AssertionFailure );
113 std::cout << "The following assertion is intended and does not indicate a failure of the test." << std::endl;
114 CPPUNIT_ASSERT_THROW( SamplingGrid illegal_grid(legal_props, illegal_values), Assert::AssertionFailure );
115 std::cout << "The following assertion is intended and does not indicate a failure of the test." << std::endl;
116 CPPUNIT_ASSERT_THROW( SamplingGrid illegal_grid(illegal_props, legal_values), Assert::AssertionFailure );
117#endif
118 // check that grid is still the same
119 for (SamplingGrid::sampledvalues_t::const_iterator iter = grid->sampled_grid.begin();
120 iter != grid->sampled_grid.end(); ++iter)
121 CPPUNIT_ASSERT_EQUAL( grid_value, *iter );
122}
123
124/** UnitTest on compatible combination of props and values
125 */
126void SamplingGridTest::compatibleGrids_Test()
127{
128 // check illegal grid
129 const double begin[NDIM] = { 0., 0., 0. };
130 const double end[NDIM] = { 1., 1., 1. };
131 const double otherend[NDIM] = { 2.5, 2.5, 2.5 };
132 SamplingGridProperties illegal_props(begin, otherend, 5);
133 SamplingGridProperties legal_props(begin, end, 3);
134 CPPUNIT_ASSERT( !grid->isCompatible(illegal_props) );
135 CPPUNIT_ASSERT( grid->isCompatible(legal_props) );
136 SamplingGrid::sampledvalues_t illegal_values;
137 for (size_t i=0; i< NUMBEROFSAMPLES(1); ++i)
138 illegal_values += 1.5;
139 SamplingGrid::sampledvalues_t legal_values;
140 for (size_t i=0; i< NUMBEROFSAMPLES(2); ++i)
141 legal_values += 1.5;
142#ifndef NDEBUG
143 // throws because props and size of illegal_values don't match
144 std::cout << "The following assertion is intended and does not indicate a failure of the test." << std::endl;
145 CPPUNIT_ASSERT_THROW( SamplingGrid illegal_grid(illegal_props, illegal_values), Assert::AssertionFailure );
146 std::cout << "The following assertion is intended and does not indicate a failure of the test." << std::endl;
147 CPPUNIT_ASSERT_THROW( SamplingGrid illegal_grid(legal_props, illegal_values), Assert::AssertionFailure );
148 std::cout << "The following assertion is intended and does not indicate a failure of the test." << std::endl;
149 CPPUNIT_ASSERT_THROW( SamplingGrid illegal_grid(illegal_props, legal_values), Assert::AssertionFailure );
150#endif
151 // check that grid is still the same
152 for (SamplingGrid::sampledvalues_t::const_iterator iter = grid->sampled_grid.begin();
153 iter != grid->sampled_grid.end(); ++iter)
154 CPPUNIT_ASSERT_EQUAL( grid_value, *iter );
155}
156
157/** UnitTest for isCongruent()
158 */
159void SamplingGridTest::isCongruent_Test()
160{
161 const double begin[NDIM] = { 0., 0., 0. };
162 const double end[NDIM] = { 2., 2., 2. };
163 const double otherbegin[NDIM] = { 0.1, 0.1, 0.1 };
164 const double otherend[NDIM] = { 1., 1., 1. };
165 SamplingGridProperties illegal_begin_props(otherbegin, end, 5);
166 SamplingGridProperties illegal_end_props(begin, otherend, 5);
167 SamplingGridProperties illegal_level_props(begin, end, 5);
168 SamplingGridProperties legal_props(begin, end, 2);
169
170 // differing windows
171// const double begin_window[NDIM] = { 0.5, 0.5, 0.5 };
172// const double end_window[NDIM] = { 1., 1., 1. };
173 const double otherbegin_window[NDIM] = { 0.45, 0.45, 0.45 };
174 const double otherend_window[NDIM] = { 1.05, 1.05, 1.05 };
175
176 // check that incompatible grid are also incongruent
177 SamplingGrid default_grid(legal_props);
178 // note that we always construct a temporary SamplingGrid from given props
179 CPPUNIT_ASSERT( default_grid.isEquivalent(illegal_begin_props) == default_grid.isCongruent(illegal_begin_props));
180 CPPUNIT_ASSERT( default_grid.isEquivalent(illegal_end_props) == default_grid.isCongruent(illegal_end_props));
181 CPPUNIT_ASSERT( default_grid.isEquivalent(illegal_level_props) == default_grid.isCongruent(illegal_level_props));
182 CPPUNIT_ASSERT( default_grid.isEquivalent(legal_props) == default_grid.isCongruent(legal_props) );
183
184 default_grid.setWindowSize(begin, end);
185 // same window
186 {
187 SamplingGrid illegal_begin_grid(illegal_begin_props);
188 SamplingGrid illegal_end_grid(illegal_end_props);
189 SamplingGrid illegal_level_grid(illegal_level_props);
190 SamplingGrid legal_grid(legal_props);
191 illegal_begin_grid.setWindowSize(begin, end);
192 illegal_end_grid.setWindowSize(begin, end);
193 illegal_level_grid.setWindowSize(begin, end);
194 legal_grid.setWindowSize(begin, end);
195 CPPUNIT_ASSERT( !illegal_begin_grid.isCongruent(default_grid) );
196 CPPUNIT_ASSERT( !illegal_end_grid.isCongruent(default_grid) );
197 CPPUNIT_ASSERT( !illegal_level_grid.isCongruent(default_grid) );
198 CPPUNIT_ASSERT( legal_grid.isCongruent(default_grid) );
199 }
200
201 // different begin
202 {
203 SamplingGrid illegal_begin_grid(illegal_begin_props);
204 SamplingGrid illegal_end_grid(illegal_end_props);
205 SamplingGrid illegal_level_grid(illegal_level_props);
206 SamplingGrid legal_grid(legal_props);
207 illegal_begin_grid.setWindowSize(otherbegin_window, end);
208 illegal_end_grid.setWindowSize(otherbegin_window, end);
209 illegal_level_grid.setWindowSize(otherbegin_window, end);
210 legal_grid.setWindowSize(begin, end);
211 CPPUNIT_ASSERT( !illegal_begin_grid.isCongruent(legal_grid) );
212 CPPUNIT_ASSERT( !illegal_end_grid.isCongruent(legal_grid) );
213 CPPUNIT_ASSERT( !illegal_level_grid.isCongruent(legal_grid) );
214 CPPUNIT_ASSERT( legal_grid.isCongruent(default_grid) );
215 }
216
217 // different end
218 {
219 SamplingGrid illegal_begin_grid(illegal_begin_props);
220 SamplingGrid illegal_end_grid(illegal_end_props);
221 SamplingGrid illegal_level_grid(illegal_level_props);
222 SamplingGrid legal_grid(legal_props);
223 illegal_begin_grid.setWindowSize(begin, otherend_window);
224 illegal_end_grid.setWindowSize(begin, otherend_window);
225 illegal_level_grid.setWindowSize(begin, otherend_window);
226 legal_grid.setWindowSize(begin, end);
227 CPPUNIT_ASSERT( !illegal_begin_grid.isCongruent(legal_grid) );
228 CPPUNIT_ASSERT( !illegal_end_grid.isCongruent(legal_grid) );
229 CPPUNIT_ASSERT( !illegal_level_grid.isCongruent(legal_grid) );
230 CPPUNIT_ASSERT( legal_grid.isCongruent(default_grid) );
231 }
232
233 // different begin and end
234 {
235 SamplingGrid illegal_begin_grid(illegal_begin_props);
236 SamplingGrid illegal_end_grid(illegal_end_props);
237 SamplingGrid illegal_level_grid(illegal_level_props);
238 SamplingGrid legal_grid(legal_props);
239 illegal_begin_grid.setWindowSize(otherbegin_window, otherend_window);
240 illegal_end_grid.setWindowSize(otherbegin_window, otherend_window);
241 illegal_level_grid.setWindowSize(otherbegin_window, otherend_window);
242 legal_grid.setWindowSize(begin, end);
243 CPPUNIT_ASSERT( !illegal_begin_grid.isCongruent(legal_grid) );
244 CPPUNIT_ASSERT( !illegal_end_grid.isCongruent(legal_grid) );
245 CPPUNIT_ASSERT( !illegal_level_grid.isCongruent(legal_grid) );
246 CPPUNIT_ASSERT( legal_grid.isCongruent(default_grid) );
247 }
248}
249
250/** UnitTest for integral()
251 */
252void SamplingGridTest::integral_Test()
253{
254 double sum = 0.;
255 sum = std::accumulate( grid->sampled_grid.begin(), grid->sampled_grid.end(), sum );
256 CPPUNIT_ASSERT_EQUAL( sum*grid->getVolume()/grid->getWindowGridPoints(), grid->integral() );
257}
258
259/** UnitTest for getVolume()
260 */
261void SamplingGridTest::getVolume_Test()
262{
263 CPPUNIT_ASSERT_EQUAL( 1., grid->getVolume() );
264}
265
266/** UnitTest for getWindowSize()
267 */
268void SamplingGridTest::getWindowSize_Test()
269{
270 // check size of default grid
271 CPPUNIT_ASSERT_EQUAL( (size_t)(NUMBEROFSAMPLES(grid->level)), grid->getWindowGridPoints() );
272
273 // create another one and check its size, too
274 const double begin[NDIM] = { 0., 0., 0. };
275 const double end[NDIM] = { 1., 1., 1. };
276 for (size_t level = 3; level<=6; ++level) {
277 values.clear();
278 for (size_t i=0; i< NUMBEROFSAMPLES(level); ++i)
279 values += grid_value;
280 delete grid;
281 // use other pointer in case something fails
282 SamplingGrid *tmpgrid = new SamplingGrid(begin, end, level, values);
283 grid = tmpgrid;
284 CPPUNIT_ASSERT_EQUAL( (size_t)NUMBEROFSAMPLES(level), grid->getWindowGridPoints() );
285 }
286}
287
288/** UnitTest for extendWindow()
289 */
290void SamplingGridTest::extendWindow_Test()
291{
292 // we have a grid with size of one, extend to twice the size and check
293 const double begin[NDIM] = { 0., 0., 0. };
294 const double size = 2.;
295 const double end[NDIM] = { size, size, size };
296 double offset[NDIM];
297 for (offset[0] = 0.; offset[0] <= 1.; offset[0] += .5)
298 for (offset[1] = 0.; offset[1] <= 1.; offset[1] += .5)
299 for (offset[2] = 0.; offset[2] <= 1.; offset[2] += .5) {
300 const double window_begin[NDIM] = { 0.+offset[0], 0.+offset[1], 0.+offset[2]};
301 const double window_end[NDIM] = { 1.+offset[0], 1.+offset[1], 1.+offset[2]};
302 SamplingGrid newgrid(begin, end, 2);
303 newgrid.setWindowSize(window_begin, window_end);
304 // resize values by hand to new window size. Otherwise they get zero'd.
305 newgrid.sampled_grid = values;
306 newgrid.sampled_grid.resize(NUMBEROFSAMPLES(1));
307 newgrid.extendWindow(begin, end);
308
309 // check integral
310 CPPUNIT_ASSERT_EQUAL(
311 grid_value/(NUMBEROFSAMPLES(grid->level)/NUMBEROFSAMPLES(grid->level)),
312 grid->integral()
313 );
314
315 // check number of points
316 CPPUNIT_ASSERT_EQUAL(
317 (size_t)NUMBEROFSAMPLES(grid->level),
318 grid->getWindowGridPoints()
319 );
320 }
321}
322
323/** UnitTest for extendWindow() with asymmetric values
324 */
325void SamplingGridTest::extendWindow_asymmetric_Test()
326{
327 std::cout << "SamplingGridTest::extendWindow_asymmetric_Test()" << std::endl;
328 const double begin[NDIM] = { 0., 0., 0. };
329 const double end[NDIM] = { 2., 2., 2. };
330 double offset[NDIM];
331 for (offset[0] = 0.; offset[0] <= 1.; offset[0] += .5)
332 for (offset[1] = 0.; offset[1] <= 1.; offset[1] += .5)
333 for (offset[2] = 0.; offset[2] <= 1.; offset[2] += .5) {
334 const double window_begin[NDIM] = { 0.+offset[0], 0.+offset[1], 0.+offset[2]};
335 const double window_end[NDIM] = { 1.+offset[0], 1.+offset[1], 1.+offset[2]};
336 SamplingGrid newgrid(begin, end, 2);
337 CPPUNIT_ASSERT_EQUAL( (size_t)0, newgrid.getWindowGridPoints() );
338 newgrid.setWindowSize(window_begin, window_end);
339 // window size is only half of domain size
340 const size_t max_samples = NUMBEROFSAMPLES(newgrid.level)*pow(0.5,(int)NDIM);
341 for (size_t i=0; i< max_samples; ++i)
342 newgrid.sampled_grid += grid_value*i;
343 const size_t sum_weight = (max_samples)*(max_samples-1)/2;
344 const double integral = newgrid.integral();
345 newgrid.extendWindow(begin, end);
346
347 // check that integral has remained the same
348 CPPUNIT_ASSERT_EQUAL( integral, newgrid.integral() );
349 CPPUNIT_ASSERT_EQUAL( grid_value*sum_weight/DOMAINVOLUME(2), newgrid.integral() );
350 }
351}
352
353/** UnitTest for addOntoWindow()
354 */
355void SamplingGridTest::addOntoWindow_Test()
356{
357 // first window is from (0,0,0) to (1,1,1)
358 CPPUNIT_ASSERT_EQUAL( 1.*grid_value, grid->integral() );
359
360 // create values for half-sized window
361 values.clear();
362 for (size_t i=0; i< (size_t)pow(.5*pow(2,2),(int)NDIM); ++i)
363 values += grid_value;
364
365 // check that too large a window throws
366#ifndef NDEBUG
367 const double begin[NDIM] = { .5, .5, .5 };
368 const double wrongend[NDIM] = { 1.5, 1.5, 1.5 };
369 std::cout << "The following assertion is intended and does not indicate a failure of the test." << std::endl;
370 CPPUNIT_ASSERT_THROW( grid->addOntoWindow(begin, wrongend, values, +1.), Assert::AssertionFailure );
371#endif
372
373 // create another window from (.5,.5,.5) to (1., 1., 1.)
374 double offset[NDIM];
375 for (offset[0] = 0.; offset[0] <= .5; offset[0] += .5)
376 for (offset[1] = 0.; offset[1] <= .5; offset[1] += .5)
377 for (offset[2] = 0.; offset[2] <= .5; offset[2] += .5) {
378 const double window_begin[NDIM] = { 0.+offset[0], 0.+offset[1], 0.+offset[2]};
379 const double window_end[NDIM] = { .5+offset[0], .5+offset[1], .5+offset[2]};
380
381 SamplingGrid newgrid(*grid);
382 // now perform working operation
383 newgrid.addOntoWindow(window_begin, window_end, values, +1.);
384
385 // check integral to be one and one eighth times the old value
386 CPPUNIT_ASSERT_EQUAL( (1.+pow(.5,(int)NDIM))*grid_value, newgrid.integral() );
387 }
388}
389
390/** UnitTest for addOntoWindow() with asymmetric values
391 */
392void SamplingGridTest::addOntoWindow_asymmetric_Test()
393{
394 const size_t size = grid->end[0]-grid->begin[0];
395 // check with asymmetric values
396 grid->sampled_grid.clear();
397 grid->sampled_grid.resize(DOMAINVOLUME(size)*NUMBEROFSAMPLES(grid->level), 0.);
398
399 for (size_t i=0;i<grid->level*(grid->end[0]-grid->begin[0]);++i)
400 grid->sampled_grid[(i*2+0)*2+0] += .5*grid_value;
401 for (size_t i=0;i<grid->level*(grid->end[1]-grid->begin[1]);++i)
402 grid->sampled_grid[(0*2+i)*2+0] += 1.*grid_value;
403 for (size_t i=0;i<grid->level*(grid->end[2]-grid->begin[2]);++i)
404 grid->sampled_grid[(0*2+0)*2+i] += 1.5*grid_value;
405
406 const double integral = grid->integral();
407
408 // now perform working operation
409 grid->addOntoWindow(grid->begin, grid->end, values, +1.);
410 // values is equal to integral of 1.
411 CPPUNIT_ASSERT_EQUAL( 1.+integral, grid->integral() );
412}
413
414/** UnitTest for operator+=()
415 */
416void SamplingGridTest::operatorPlusEqual_Test()
417{
418 // create other grid
419 const double begin[NDIM] = { 0., 0., 0. };
420 const double end[NDIM] = { 1., 1., 1. };
421 SamplingGrid::sampledvalues_t othervalues;
422 const double othergrid_value = 1.5;
423 for (size_t i=0; i< NUMBEROFSAMPLES(2); ++i)
424 othervalues += othergrid_value;
425 SamplingGrid othergrid(begin, end, 2, othervalues);
426 CPPUNIT_ASSERT_EQUAL( othergrid_value, *(othergrid.sampled_grid.begin()) );
427
428 // perform operation
429 CPPUNIT_ASSERT_NO_THROW( *grid += othergrid );
430
431 // check the contents of the grid
432 const double sum = grid_value+othergrid_value;
433 for (SamplingGrid::sampledvalues_t::const_iterator iter = grid->sampled_grid.begin();
434 iter != grid->sampled_grid.end(); ++iter)
435 CPPUNIT_ASSERT_EQUAL( sum, *iter );
436}
437
438/** UnitTest for operator-=()
439 */
440void SamplingGridTest::operatorMinusEqual_Test()
441{
442 // create other grid
443 const double begin[NDIM] = { 0., 0., 0. };
444 const double end[NDIM] = { 1., 1., 1. };
445 SamplingGrid::sampledvalues_t othervalues;
446 const double othergrid_value = 1.5;
447 for (size_t i=0; i< NUMBEROFSAMPLES(2); ++i)
448 othervalues += othergrid_value;
449 SamplingGrid othergrid(begin, end, 2, othervalues);
450 CPPUNIT_ASSERT_EQUAL( othergrid_value, *(othergrid.sampled_grid.begin()) );
451
452 // perform operation
453 CPPUNIT_ASSERT_NO_THROW( *grid -= othergrid );
454
455 // check the contents of the grid
456 const double difference = grid_value-othergrid_value;
457 for (SamplingGrid::sampledvalues_t::const_iterator iter = grid->sampled_grid.begin();
458 iter != grid->sampled_grid.end(); ++iter)
459 CPPUNIT_ASSERT_EQUAL( difference, *iter );
460}
461
462
463/** UnitTest for operator==()
464 */
465void SamplingGridTest::equality_Test()
466{
467 const double begin[NDIM] = { 0., 0., 0. };
468 const double otherbegin[NDIM] = { .5, 0.1, -0.5 };
469 const double end[NDIM] = { 1., 1., 1. };
470 const double otherend[NDIM] = { 2., 2., 2. };
471 const double begin_window[NDIM] = { 0., 0., 0. };
472 const double otherbegin_window[NDIM] = { .75, 0.1, 0. };
473 const double end_window[NDIM] = { 1., 1., 1. };
474 const double otherend_window[NDIM] = { .9, .9, .9 };
475
476 // create other grid
477 SamplingGrid samegrid(begin, end, 2, values);
478 SamplingGrid differentbegin(otherbegin, end, 2, values);
479 SamplingGrid differentend(begin, otherend, 2, values);
480 SamplingGrid::sampledvalues_t morevalues;
481 {
482 const double othergrid_value = 1.5;
483 for (size_t i=0; i< NUMBEROFSAMPLES(4); ++i)
484 morevalues += othergrid_value;
485 }
486 SamplingGrid differentlevel(begin, end, 4, morevalues);
487 SamplingGrid::sampledvalues_t othervalues;
488 {
489 const double othergrid_value = 1.5;
490 for (size_t i=0; i< NUMBEROFSAMPLES(2); ++i)
491 othervalues += othergrid_value;
492 }
493 SamplingGrid differentvalues(begin, end, 2, othervalues);
494
495 // check for same level, begin, and end
496 CPPUNIT_ASSERT( *grid == *grid );
497 CPPUNIT_ASSERT( *grid == samegrid );
498 CPPUNIT_ASSERT( samegrid == *grid);
499 CPPUNIT_ASSERT( *grid != differentbegin);
500 CPPUNIT_ASSERT( differentbegin != *grid);
501 CPPUNIT_ASSERT( *grid != differentend );
502 CPPUNIT_ASSERT( differentend != *grid );
503 CPPUNIT_ASSERT( *grid != differentlevel );
504 CPPUNIT_ASSERT( differentlevel != *grid );
505
506 // check for all but differing values
507 CPPUNIT_ASSERT( *grid != differentvalues );
508 CPPUNIT_ASSERT( differentvalues != *grid );
509
510 // check for different windows
511 SamplingGrid differentwindowbegin(begin, end, 2);
512 differentwindowbegin.setWindow(otherbegin_window, end_window);
513 SamplingGrid differentwindowend(begin, end, 2);
514 differentwindowend.setWindow(begin_window, otherend_window);
515 CPPUNIT_ASSERT( *grid != differentwindowbegin );
516 CPPUNIT_ASSERT( differentwindowbegin != *grid );
517 CPPUNIT_ASSERT( *grid != differentwindowend );
518 CPPUNIT_ASSERT( differentwindowend != *grid );
519
520 // check against ZeroInstance
521 CPPUNIT_ASSERT( *grid != ZeroInstance<SamplingGrid>() );
522 CPPUNIT_ASSERT( samegrid != ZeroInstance<SamplingGrid>() );
523 CPPUNIT_ASSERT( differentbegin != ZeroInstance<SamplingGrid>() );
524 CPPUNIT_ASSERT( differentend != ZeroInstance<SamplingGrid>() );
525 CPPUNIT_ASSERT( differentlevel != ZeroInstance<SamplingGrid>() );
526 CPPUNIT_ASSERT( differentvalues != ZeroInstance<SamplingGrid>() );
527 CPPUNIT_ASSERT( differentwindowbegin != ZeroInstance<SamplingGrid>() );
528 CPPUNIT_ASSERT( differentwindowend != ZeroInstance<SamplingGrid>() );
529}
530
531/** UnitTest for serialization
532 */
533void SamplingGridTest::serializeTest()
534{
535 // serialize
536 std::stringstream outputstream;
537 boost::archive::text_oarchive oa(outputstream);
538 oa << grid;
539
540 // deserialize
541 SamplingGrid *samegrid = NULL;
542 std::stringstream returnstream(outputstream.str());
543 boost::archive::text_iarchive ia(returnstream);
544 ia >> samegrid;
545
546 CPPUNIT_ASSERT( samegrid != NULL );
547 CPPUNIT_ASSERT( *grid == *samegrid );
548
549 delete samegrid;
550}
551
552#ifdef HAVE_INLINE
553inline
554#endif
555static int calculateIndex(const int N[NDIM], const int &_length)
556{
557 return N[2] + N[1]*_length + N[0]*_length*_length;
558}
559
560#ifdef HAVE_INLINE
561inline
562#endif
563static double calculateDistanceSquared(const int N[NDIM], const int &_length)
564{
565 return
566 ::pow(N[0]/(double)_length-.5,2)
567 + ::pow(N[1]/(double)_length-.5,2)
568 + ::pow(N[2]/(double)_length-.5,2);
569}
570/** UnitTest for downsample()
571 */
572void SamplingGridTest::downsampleTest()
573{
574 const double begin[NDIM] = { 0., 0., 0. };
575 const double end[NDIM] = { 1., 1., 1. };
576
577 // simple test, one level difference, same value everywhere
578 {
579 SamplingGrid::sampledvalues_t checkvalues;
580 SamplingGrid::sampledvalues_t othervalues;
581 const double othergrid_value = 1.5;
582 for (size_t i=0; i< NUMBEROFSAMPLES(2); ++i)
583 checkvalues += othergrid_value;
584 for (size_t i=0; i< NUMBEROFSAMPLES(3); ++i)
585 othervalues += othergrid_value;
586
587 SamplingGrid largegrid(begin, end, 3, othervalues);
588// std::cout << " largegrid value " << largegrid.sampled_grid << std::endl;
589 SamplingGrid smallgrid(begin, end, 2);
590 SamplingGrid::downsample(smallgrid, largegrid, 2);
591 SamplingGrid checkgrid(begin, end, 2, checkvalues);
592// std::cout << " smallgrid value " << smallgrid.sampled_grid << std::endl;
593// std::cout << " checkgrid value " << checkgrid.sampled_grid << std::endl;
594 CPPUNIT_ASSERT_EQUAL( smallgrid, checkgrid );
595 }
596
597 // simple test, two level difference, same value everywhere
598 {
599 SamplingGrid::sampledvalues_t checkvalues;
600 SamplingGrid::sampledvalues_t othervalues;
601 const double othergrid_value = 1.5;
602 for (size_t i=0; i< NUMBEROFSAMPLES(2); ++i)
603 checkvalues += othergrid_value;
604 for (size_t i=0; i< NUMBEROFSAMPLES(4); ++i)
605 othervalues += othergrid_value;
606
607 SamplingGrid largegrid(begin, end, 4, othervalues);
608// std::cout << " largegrid value " << largegrid.sampled_grid << std::endl;
609 SamplingGrid smallgrid(begin, end, 2);
610 SamplingGrid::downsample(smallgrid, largegrid, 2);
611 SamplingGrid checkgrid(begin, end, 2, checkvalues);
612// std::cout << " smallgrid value " << smallgrid.sampled_grid << std::endl;
613// std::cout << " checkgrid value " << checkgrid.sampled_grid << std::endl;
614 CPPUNIT_ASSERT_EQUAL( smallgrid, checkgrid );
615 }
616
617 // same grid, window equals grids, different values
618 {
619 const int length_c = ::pow(2,2);
620 const int length_f = ::pow(2,3);
621 SamplingGrid::sampledvalues_t checkvalues((int)::pow(length_c, NDIM), 0.);
622 SamplingGrid::sampledvalues_t othervalues((int)::pow(length_f, NDIM), 0.);
623 int N[NDIM];
624 for (N[0]=0; N[0]< length_f; ++N[0]) {
625 for (N[1]=0; N[1]< length_f; ++N[1]) {
626 for (N[2]=0; N[2]< length_f; ++N[2]) {
627 const int index = calculateIndex(N, length_f);
628 const double dist = calculateDistanceSquared(N, length_f);
629 othervalues[index] = cos(M_PI*dist/1.5);
630 }
631 }
632 }
633 for (N[0]=0; N[0]< length_c; ++N[0]) {
634 for (N[1]=0; N[1]< length_c; ++N[1]) {
635 for (N[2]=0; N[2]< length_c; ++N[2]) {
636 const int index = calculateIndex(N, length_c);
637 const double dist = calculateDistanceSquared(N, length_c);
638 checkvalues[index] = cos(M_PI*dist/1.5);
639 }
640 }
641 }
642
643 SamplingGrid largegrid(begin, end, 3, othervalues);
644// std::cout << " largegrid value " << largegrid.sampled_grid << std::endl;
645 SamplingGrid smallgrid(begin, end, 2);
646 SamplingGrid::downsample(smallgrid, largegrid, 2);
647 SamplingGrid checkgrid(begin, end, 2, checkvalues);
648// std::cout << " smallgrid value " << smallgrid.sampled_grid << std::endl;
649// std::cout << " checkgrid value " << checkgrid.sampled_grid << std::endl;
650 static const double threshold = 1.2e-1;
651 for (N[0]=0; N[0]< length_c; ++N[0])
652 for (N[1]=0; N[1]< length_c; ++N[1])
653 for (N[2]=0; N[2]< length_c; ++N[2]) {
654 const double check_threshold =
655 (((N[0] == 0) || (N[0] == length_c-1))
656 && ((N[1] == 0) || (N[1] == length_c-1))
657 && ((N[2] == 0) || (N[2] == length_c-1)))
658 ? 2.*threshold : threshold;
659 const int index = calculateIndex(N, length_c);
660// std::cout << "Comparing "
661// << fabs((smallgrid.sampled_grid[index] - checkgrid.sampled_grid[index]))
662// << " < " << threshold << std::endl;
663 CPPUNIT_ASSERT(
664 fabs((smallgrid.sampled_grid[index] - checkgrid.sampled_grid[index])) < check_threshold);
665 }
666 }
667}
Note: See TracBrowser for help on using the repository browser.