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

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 58fcbe5 was 5aaa43, checked in by Frederik Heber <heber@…>, 12 years ago

FIX: Fixed new copyright line since start of 2013 in CodeChecks test.

  • we must look for either Uni Bonn or myself.
  • added second copyright line since from 1st of Jan 2013 I am not employed by University of Bonn anymore, hence changes to the code are my own copyright.
  • Property mode set to 100644
File size: 15.8 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 "SamplingGridUnitTest.hpp"
43
44#include "CodePatterns/Assert.hpp"
45
46#include <boost/assign.hpp>
47#include <cmath>
48#include <numeric>
49
50#ifdef HAVE_TESTRUNNER
51#include "UnitTestMain.hpp"
52#endif /*HAVE_TESTRUNNER*/
53
54using namespace boost::assign;
55
56/********************************************** Test classes **************************************/
57
58const double grid_value=1.;
59
60#define NUMBEROFSAMPLES(n) (size_t)(pow(pow(2,n),3))
61#define DOMAINVOLUME(l) (size_t)pow(l,3)
62
63// Registers the fixture into the 'registry'
64CPPUNIT_TEST_SUITE_REGISTRATION( SamplingGridTest );
65
66
67void SamplingGridTest::setUp()
68{
69 // failing asserts should be thrown
70 ASSERT_DO(Assert::Throw);
71
72 // create the grid
73 const double begin[3] = { 0., 0., 0. };
74 const double end[3] = { 1., 1., 1. };
75 for (size_t i=0; i< DOMAINVOLUME(1)*NUMBEROFSAMPLES(2); ++i)
76 values += grid_value;
77 grid = new SamplingGrid(begin, end, 2, values);
78 CPPUNIT_ASSERT_EQUAL( grid_value, *(grid->sampled_grid.begin()) );
79}
80
81
82void SamplingGridTest::tearDown()
83{
84 delete grid;
85}
86
87/** UnitTest on compatible combination of props and values
88 */
89void SamplingGridTest::compatibleGrids_Test()
90{
91 // check illegal grid
92 const double begin[3] = { 0., 0., 0. };
93 const double end[3] = { 2., 2., 2. };
94 SamplingGridProperties illegal_props(begin, end, 5);
95 SamplingGridProperties legal_props(begin, end, 2);
96 CPPUNIT_ASSERT( !grid->isCompatible(illegal_props) );
97 CPPUNIT_ASSERT( grid->isCompatible(legal_props) );
98 SamplingGrid::sampledvalues_t illegal_values;
99 for (size_t i=0; i< NUMBEROFSAMPLES(1); ++i)
100 illegal_values += 1.5;
101 SamplingGrid::sampledvalues_t legal_values;
102 for (size_t i=0; i< NUMBEROFSAMPLES(2); ++i)
103 legal_values += 1.5;
104#ifndef NDEBUG
105 // throws because props and size of illegal_values don't match
106 std::cout << "The following assertion is intended and does not indicate a failure of the test." << std::endl;
107 CPPUNIT_ASSERT_THROW( SamplingGrid illegal_grid(illegal_props, illegal_values), Assert::AssertionFailure );
108 std::cout << "The following assertion is intended and does not indicate a failure of the test." << std::endl;
109 CPPUNIT_ASSERT_THROW( SamplingGrid illegal_grid(legal_props, illegal_values), Assert::AssertionFailure );
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, legal_values), Assert::AssertionFailure );
112#endif
113 // check that grid is still the same
114 for (SamplingGrid::sampledvalues_t::const_iterator iter = grid->sampled_grid.begin();
115 iter != grid->sampled_grid.end(); ++iter)
116 CPPUNIT_ASSERT_EQUAL( grid_value, *iter );
117}
118
119/** UnitTest for isCongruent()
120 */
121void SamplingGridTest::isCongruent_Test()
122{
123 const double begin[3] = { 0., 0., 0. };
124 const double end[3] = { 2., 2., 2. };
125 const double otherbegin[3] = { 0.1, 0.1, 0.1 };
126 const double otherend[3] = { 1., 1., 1. };
127 SamplingGridProperties illegal_begin_props(otherbegin, end, 5);
128 SamplingGridProperties illegal_end_props(begin, otherend, 5);
129 SamplingGridProperties illegal_level_props(begin, end, 5);
130 SamplingGridProperties legal_props(begin, end, 2);
131
132 // differing windows
133 const double begin_window[3] = { 0.5, 0.5, 0.5 };
134 const double end_window[3] = { 1., 1., 1. };
135 const double otherbegin_window[3] = { 0.45, 0.45, 0.45 };
136 const double otherend_window[3] = { 1.05, 1.05, 1.05 };
137
138 // check that incompatible grid are also incongruent
139 SamplingGrid default_grid(legal_props);
140 // note that we always construct a temporary SamplingGrid from given props
141 CPPUNIT_ASSERT( default_grid.isCompatible(illegal_begin_props) == default_grid.isCongruent(illegal_begin_props));
142 CPPUNIT_ASSERT( default_grid.isCompatible(illegal_end_props) == default_grid.isCongruent(illegal_end_props));
143 CPPUNIT_ASSERT( default_grid.isCompatible(illegal_level_props) == default_grid.isCongruent(illegal_level_props));
144 CPPUNIT_ASSERT( default_grid.isCompatible(legal_props) == default_grid.isCongruent(legal_props) );
145
146 default_grid.setWindowSize(begin, end);
147 // same window
148 {
149 SamplingGrid illegal_begin_grid(illegal_begin_props);
150 SamplingGrid illegal_end_grid(illegal_end_props);
151 SamplingGrid illegal_level_grid(illegal_level_props);
152 SamplingGrid legal_grid(legal_props);
153 illegal_begin_grid.setWindowSize(begin, end);
154 illegal_end_grid.setWindowSize(begin, end);
155 illegal_level_grid.setWindowSize(begin, end);
156 legal_grid.setWindowSize(begin, end);
157 CPPUNIT_ASSERT( !illegal_begin_grid.isCongruent(default_grid) );
158 CPPUNIT_ASSERT( !illegal_end_grid.isCongruent(default_grid) );
159 CPPUNIT_ASSERT( !illegal_level_grid.isCongruent(default_grid) );
160 CPPUNIT_ASSERT( legal_grid.isCongruent(default_grid) );
161 }
162
163 // different begin
164 {
165 SamplingGrid illegal_begin_grid(illegal_begin_props);
166 SamplingGrid illegal_end_grid(illegal_end_props);
167 SamplingGrid illegal_level_grid(illegal_level_props);
168 SamplingGrid legal_grid(legal_props);
169 illegal_begin_grid.setWindowSize(otherbegin_window, end);
170 illegal_end_grid.setWindowSize(otherbegin_window, end);
171 illegal_level_grid.setWindowSize(otherbegin_window, end);
172 legal_grid.setWindowSize(begin, end);
173 CPPUNIT_ASSERT( !illegal_begin_grid.isCongruent(legal_grid) );
174 CPPUNIT_ASSERT( !illegal_end_grid.isCongruent(legal_grid) );
175 CPPUNIT_ASSERT( !illegal_level_grid.isCongruent(legal_grid) );
176 CPPUNIT_ASSERT( legal_grid.isCongruent(default_grid) );
177 }
178
179 // different end
180 {
181 SamplingGrid illegal_begin_grid(illegal_begin_props);
182 SamplingGrid illegal_end_grid(illegal_end_props);
183 SamplingGrid illegal_level_grid(illegal_level_props);
184 SamplingGrid legal_grid(legal_props);
185 illegal_begin_grid.setWindowSize(begin, otherend_window);
186 illegal_end_grid.setWindowSize(begin, otherend_window);
187 illegal_level_grid.setWindowSize(begin, otherend_window);
188 legal_grid.setWindowSize(begin, end);
189 CPPUNIT_ASSERT( !illegal_begin_grid.isCongruent(legal_grid) );
190 CPPUNIT_ASSERT( !illegal_end_grid.isCongruent(legal_grid) );
191 CPPUNIT_ASSERT( !illegal_level_grid.isCongruent(legal_grid) );
192 CPPUNIT_ASSERT( legal_grid.isCongruent(default_grid) );
193 }
194
195 // different begin and end
196 {
197 SamplingGrid illegal_begin_grid(illegal_begin_props);
198 SamplingGrid illegal_end_grid(illegal_end_props);
199 SamplingGrid illegal_level_grid(illegal_level_props);
200 SamplingGrid legal_grid(legal_props);
201 illegal_begin_grid.setWindowSize(otherbegin_window, otherend_window);
202 illegal_end_grid.setWindowSize(otherbegin_window, otherend_window);
203 illegal_level_grid.setWindowSize(otherbegin_window, otherend_window);
204 legal_grid.setWindowSize(begin, end);
205 CPPUNIT_ASSERT( !illegal_begin_grid.isCongruent(legal_grid) );
206 CPPUNIT_ASSERT( !illegal_end_grid.isCongruent(legal_grid) );
207 CPPUNIT_ASSERT( !illegal_level_grid.isCongruent(legal_grid) );
208 CPPUNIT_ASSERT( legal_grid.isCongruent(default_grid) );
209 }
210}
211
212/** UnitTest for integral()
213 */
214void SamplingGridTest::integral_Test()
215{
216 double sum = 0.;
217 sum = std::accumulate( grid->sampled_grid.begin(), grid->sampled_grid.end(), sum );
218 CPPUNIT_ASSERT_EQUAL( sum*grid->getVolume()/grid->getWindowGridPoints(), grid->integral() );
219}
220
221/** UnitTest for getVolume()
222 */
223void SamplingGridTest::getVolume_Test()
224{
225 CPPUNIT_ASSERT_EQUAL( 1., grid->getVolume() );
226}
227
228/** UnitTest for getWindowSize()
229 */
230void SamplingGridTest::getWindowSize_Test()
231{
232 // check size of default grid
233 CPPUNIT_ASSERT_EQUAL( (size_t)(NUMBEROFSAMPLES(grid->level)), grid->getWindowGridPoints() );
234
235 // create another one and check its size, too
236 const double begin[3] = { 0., 0., 0. };
237 const double end[3] = { 1., 1., 1. };
238 for (size_t level = 3; level<=6; ++level) {
239 values.clear();
240 for (size_t i=0; i< NUMBEROFSAMPLES(level); ++i)
241 values += grid_value;
242 delete grid;
243 // use other pointer in case something fails
244 SamplingGrid *tmpgrid = new SamplingGrid(begin, end, level, values);
245 grid = tmpgrid;
246 CPPUNIT_ASSERT_EQUAL( (size_t)NUMBEROFSAMPLES(level), grid->getWindowGridPoints() );
247 }
248}
249
250/** UnitTest for extendWindow()
251 */
252void SamplingGridTest::extendWindow_Test()
253{
254 // we have a grid with size of one, extend to twice the size and check
255 const double begin[3] = { 0., 0., 0. };
256 const double size = 2.;
257 const double end[3] = { size, size, size };
258 double offset[3];
259 for (offset[0] = 0.; offset[0] <= 1.; offset[0] += .5)
260 for (offset[1] = 0.; offset[1] <= 1.; offset[1] += .5)
261 for (offset[2] = 0.; offset[2] <= 1.; offset[2] += .5) {
262 const double window_begin[3] = { 0.+offset[0], 0.+offset[1], 0.+offset[2]};
263 const double window_end[3] = { 1.+offset[0], 1.+offset[1], 1.+offset[2]};
264 SamplingGrid newgrid(begin, end, 2);
265 newgrid.setWindowSize(window_begin, window_end);
266 // resize values by hand to new window size. Otherwise they get zero'd.
267 newgrid.sampled_grid = values;
268 newgrid.sampled_grid.resize(NUMBEROFSAMPLES(1));
269 newgrid.extendWindow(begin, end);
270
271 // check integral
272 CPPUNIT_ASSERT_EQUAL(
273 grid_value/(NUMBEROFSAMPLES(grid->level)/NUMBEROFSAMPLES(grid->level)),
274 grid->integral()
275 );
276
277 // check number of points
278 CPPUNIT_ASSERT_EQUAL(
279 (size_t)NUMBEROFSAMPLES(grid->level),
280 grid->getWindowGridPoints()
281 );
282 }
283}
284
285/** UnitTest for extendWindow() with asymmetric values
286 */
287void SamplingGridTest::extendWindow_asymmetric_Test()
288{
289 std::cout << "SamplingGridTest::extendWindow_asymmetric_Test()" << std::endl;
290 const double begin[3] = { 0., 0., 0. };
291 const double end[3] = { 2., 2., 2. };
292 double offset[3];
293 for (offset[0] = 0.; offset[0] <= 1.; offset[0] += .5)
294 for (offset[1] = 0.; offset[1] <= 1.; offset[1] += .5)
295 for (offset[2] = 0.; offset[2] <= 1.; offset[2] += .5) {
296 const double window_begin[3] = { 0.+offset[0], 0.+offset[1], 0.+offset[2]};
297 const double window_end[3] = { 1.+offset[0], 1.+offset[1], 1.+offset[2]};
298 SamplingGrid newgrid(begin, end, 2);
299 CPPUNIT_ASSERT_EQUAL( (size_t)0, newgrid.getWindowGridPoints() );
300 newgrid.setWindowSize(window_begin, window_end);
301 // window size is only half of domain size
302 const size_t max_samples = NUMBEROFSAMPLES(newgrid.level)*pow(0.5,3);
303 for (size_t i=0; i< max_samples; ++i)
304 newgrid.sampled_grid += grid_value*i;
305 const size_t sum_weight = (max_samples)*(max_samples-1)/2;
306 const double integral = newgrid.integral();
307 newgrid.extendWindow(begin, end);
308
309 // check that integral has remained the same
310 CPPUNIT_ASSERT_EQUAL( integral, newgrid.integral() );
311 CPPUNIT_ASSERT_EQUAL( grid_value*sum_weight/DOMAINVOLUME(2), newgrid.integral() );
312 }
313}
314
315/** UnitTest for addOntoWindow()
316 */
317void SamplingGridTest::addOntoWindow_Test()
318{
319 // first window is from (0,0,0) to (1,1,1)
320 CPPUNIT_ASSERT_EQUAL( 1.*grid_value, grid->integral() );
321
322 // create values for half-sized window
323 values.clear();
324 for (size_t i=0; i< (size_t)pow(.5*pow(2,2),3); ++i)
325 values += grid_value;
326
327 // check that too large a window throws
328#ifndef NDEBUG
329 const double begin[3] = { .5, .5, .5 };
330 const double wrongend[3] = { 1.5, 1.5, 1.5 };
331 std::cout << "The following assertion is intended and does not indicate a failure of the test." << std::endl;
332 CPPUNIT_ASSERT_THROW( grid->addOntoWindow(begin, wrongend, values, +1.), Assert::AssertionFailure );
333#endif
334
335 // create another window from (.5,.5,.5) to (1., 1., 1.)
336 double offset[3];
337 for (offset[0] = 0.; offset[0] <= .5; offset[0] += .5)
338 for (offset[1] = 0.; offset[1] <= .5; offset[1] += .5)
339 for (offset[2] = 0.; offset[2] <= .5; offset[2] += .5) {
340 const double window_begin[3] = { 0.+offset[0], 0.+offset[1], 0.+offset[2]};
341 const double window_end[3] = { .5+offset[0], .5+offset[1], .5+offset[2]};
342
343 SamplingGrid newgrid(*grid);
344 // now perform working operation
345 newgrid.addOntoWindow(window_begin, window_end, values, +1.);
346
347 // check integral to be one and one eighth times the old value
348 CPPUNIT_ASSERT_EQUAL( (1.+pow(.5,3))*grid_value, newgrid.integral() );
349 }
350}
351
352/** UnitTest for addOntoWindow() with asymmetric values
353 */
354void SamplingGridTest::addOntoWindow_asymmetric_Test()
355{
356 const size_t size = grid->end[0]-grid->begin[0];
357 // check with asymmetric values
358 grid->sampled_grid.clear();
359 grid->sampled_grid.resize(DOMAINVOLUME(size)*NUMBEROFSAMPLES(grid->level), 0.);
360
361 for (size_t i=0;i<grid->level*(grid->end[0]-grid->begin[0]);++i)
362 grid->sampled_grid[(i*2+0)*2+0] += .5*grid_value;
363 for (size_t i=0;i<grid->level*(grid->end[1]-grid->begin[1]);++i)
364 grid->sampled_grid[(0*2+i)*2+0] += 1.*grid_value;
365 for (size_t i=0;i<grid->level*(grid->end[2]-grid->begin[2]);++i)
366 grid->sampled_grid[(0*2+0)*2+i] += 1.5*grid_value;
367
368 const double integral = grid->integral();
369
370 // now perform working operation
371 grid->addOntoWindow(grid->begin, grid->end, values, +1.);
372 // values is equal to integral of 1.
373 CPPUNIT_ASSERT_EQUAL( 1.+integral, grid->integral() );
374}
375
376/** UnitTest for operator+=()
377 */
378void SamplingGridTest::operatorPlusEqual_Test()
379{
380 // create other grid
381 const double begin[3] = { 0., 0., 0. };
382 const double end[3] = { 1., 1., 1. };
383 SamplingGrid::sampledvalues_t othervalues;
384 const double othergrid_value = 1.5;
385 for (size_t i=0; i< NUMBEROFSAMPLES(2); ++i)
386 othervalues += othergrid_value;
387 SamplingGrid othergrid(begin, end, 2, othervalues);
388 CPPUNIT_ASSERT_EQUAL( othergrid_value, *(othergrid.sampled_grid.begin()) );
389
390 // perform operation
391 CPPUNIT_ASSERT_NO_THROW( *grid += othergrid );
392
393 // check the contents of the grid
394 const double sum = grid_value+othergrid_value;
395 for (SamplingGrid::sampledvalues_t::const_iterator iter = grid->sampled_grid.begin();
396 iter != grid->sampled_grid.end(); ++iter)
397 CPPUNIT_ASSERT_EQUAL( sum, *iter );
398}
399
400/** UnitTest for operator-=()
401 */
402void SamplingGridTest::operatorMinusEqual_Test()
403{
404 // create other grid
405 const double begin[3] = { 0., 0., 0. };
406 const double end[3] = { 1., 1., 1. };
407 SamplingGrid::sampledvalues_t othervalues;
408 const double othergrid_value = 1.5;
409 for (size_t i=0; i< NUMBEROFSAMPLES(2); ++i)
410 othervalues += othergrid_value;
411 SamplingGrid othergrid(begin, end, 2, othervalues);
412 CPPUNIT_ASSERT_EQUAL( othergrid_value, *(othergrid.sampled_grid.begin()) );
413
414 // perform operation
415 CPPUNIT_ASSERT_NO_THROW( *grid -= othergrid );
416
417 // check the contents of the grid
418 const double difference = grid_value-othergrid_value;
419 for (SamplingGrid::sampledvalues_t::const_iterator iter = grid->sampled_grid.begin();
420 iter != grid->sampled_grid.end(); ++iter)
421 CPPUNIT_ASSERT_EQUAL( difference, *iter );
422}
423
Note: See TracBrowser for help on using the repository browser.