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

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 a3427f was fbf143, checked in by Frederik Heber <heber@…>, 12 years ago

Placed Containers, Converter, and SetValues as subfolders into Summation.

  • also libMolecuilderFragmentationSummation is now a shared library, easing linkage to libMolecuilderJobs, and contains all of the three convenience libraries.
  • libMolecuilderFragmentationSetValues is now again convenience, as contained in ..Summation which in turn is shared.
  • KeySetsContainer right now is the link between lib..Summation and lib.. Fragmentation. Hence, we had to extract the module and change it into a shared library, as it is required by libMolecuilderJobs through ..Summation but also by ..Fragmentation that heavily relies on this container.
  • moved parseKeySetFile down into Fragmentation folder to KeySetsContainer, it is also contained in new shared library libMolecuilderFragmentation_ KeySetsContainer.
  • Property mode set to 100644
File size: 15.7 KB
Line 
1/*
2 * Project: MoleCuilder
3 * Description: creates and alters molecular systems
4 * Copyright (C) 2012 University of Bonn. All rights reserved.
5 *
6 *
7 * This file is part of MoleCuilder.
8 *
9 * MoleCuilder is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation, either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * MoleCuilder is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with MoleCuilder. If not, see <http://www.gnu.org/licenses/>.
21 */
22
23/*
24 * SamplingGridUnitTest.cpp
25 *
26 * Created on: Jul 29, 2012
27 * Author: heber
28 */
29
30// include config.h
31#ifdef HAVE_CONFIG_H
32#include <config.h>
33#endif
34
35using namespace std;
36
37#include <cppunit/CompilerOutputter.h>
38#include <cppunit/extensions/TestFactoryRegistry.h>
39#include <cppunit/ui/text/TestRunner.h>
40
41#include "SamplingGridUnitTest.hpp"
42
43#include "CodePatterns/Assert.hpp"
44
45#include <boost/assign.hpp>
46#include <cmath>
47#include <numeric>
48
49#ifdef HAVE_TESTRUNNER
50#include "UnitTestMain.hpp"
51#endif /*HAVE_TESTRUNNER*/
52
53using namespace boost::assign;
54
55/********************************************** Test classes **************************************/
56
57const double grid_value=1.;
58
59#define NUMBEROFSAMPLES(n) (size_t)(pow(pow(2,n),3))
60#define DOMAINVOLUME(l) (size_t)pow(l,3)
61
62// Registers the fixture into the 'registry'
63CPPUNIT_TEST_SUITE_REGISTRATION( SamplingGridTest );
64
65
66void SamplingGridTest::setUp()
67{
68 // failing asserts should be thrown
69 ASSERT_DO(Assert::Throw);
70
71 // create the grid
72 const double begin[3] = { 0., 0., 0. };
73 const double end[3] = { 1., 1., 1. };
74 for (size_t i=0; i< DOMAINVOLUME(1)*NUMBEROFSAMPLES(2); ++i)
75 values += grid_value;
76 grid = new SamplingGrid(begin, end, 2, values);
77 CPPUNIT_ASSERT_EQUAL( grid_value, *(grid->sampled_grid.begin()) );
78}
79
80
81void SamplingGridTest::tearDown()
82{
83 delete grid;
84}
85
86/** UnitTest on compatible combination of props and values
87 */
88void SamplingGridTest::compatibleGrids_Test()
89{
90 // check illegal grid
91 const double begin[3] = { 0., 0., 0. };
92 const double end[3] = { 2., 2., 2. };
93 SamplingGridProperties illegal_props(begin, end, 5);
94 SamplingGridProperties legal_props(begin, end, 2);
95 CPPUNIT_ASSERT( !grid->isCompatible(illegal_props) );
96 CPPUNIT_ASSERT( grid->isCompatible(legal_props) );
97 SamplingGrid::sampledvalues_t illegal_values;
98 for (size_t i=0; i< NUMBEROFSAMPLES(1); ++i)
99 illegal_values += 1.5;
100 SamplingGrid::sampledvalues_t legal_values;
101 for (size_t i=0; i< NUMBEROFSAMPLES(2); ++i)
102 legal_values += 1.5;
103#ifndef NDEBUG
104 // throws because props and size of illegal_values don't match
105 std::cout << "The following assertion is intended and does not indicate a failure of the test." << std::endl;
106 CPPUNIT_ASSERT_THROW( SamplingGrid illegal_grid(illegal_props, illegal_values), Assert::AssertionFailure );
107 std::cout << "The following assertion is intended and does not indicate a failure of the test." << std::endl;
108 CPPUNIT_ASSERT_THROW( SamplingGrid illegal_grid(legal_props, illegal_values), Assert::AssertionFailure );
109 std::cout << "The following assertion is intended and does not indicate a failure of the test." << std::endl;
110 CPPUNIT_ASSERT_THROW( SamplingGrid illegal_grid(illegal_props, legal_values), Assert::AssertionFailure );
111#endif
112 // check that grid is still the same
113 for (SamplingGrid::sampledvalues_t::const_iterator iter = grid->sampled_grid.begin();
114 iter != grid->sampled_grid.end(); ++iter)
115 CPPUNIT_ASSERT_EQUAL( grid_value, *iter );
116}
117
118/** UnitTest for isCongruent()
119 */
120void SamplingGridTest::isCongruent_Test()
121{
122 const double begin[3] = { 0., 0., 0. };
123 const double end[3] = { 2., 2., 2. };
124 const double otherbegin[3] = { 0.1, 0.1, 0.1 };
125 const double otherend[3] = { 1., 1., 1. };
126 SamplingGridProperties illegal_begin_props(otherbegin, end, 5);
127 SamplingGridProperties illegal_end_props(begin, otherend, 5);
128 SamplingGridProperties illegal_level_props(begin, end, 5);
129 SamplingGridProperties legal_props(begin, end, 2);
130
131 // differing windows
132 const double begin_window[3] = { 0.5, 0.5, 0.5 };
133 const double end_window[3] = { 1., 1., 1. };
134 const double otherbegin_window[3] = { 0.45, 0.45, 0.45 };
135 const double otherend_window[3] = { 1.05, 1.05, 1.05 };
136
137 // check that incompatible grid are also incongruent
138 SamplingGrid default_grid(legal_props);
139 // note that we always construct a temporary SamplingGrid from given props
140 CPPUNIT_ASSERT( default_grid.isCompatible(illegal_begin_props) == default_grid.isCongruent(illegal_begin_props));
141 CPPUNIT_ASSERT( default_grid.isCompatible(illegal_end_props) == default_grid.isCongruent(illegal_end_props));
142 CPPUNIT_ASSERT( default_grid.isCompatible(illegal_level_props) == default_grid.isCongruent(illegal_level_props));
143 CPPUNIT_ASSERT( default_grid.isCompatible(legal_props) == default_grid.isCongruent(legal_props) );
144
145 default_grid.setWindowSize(begin, end);
146 // same window
147 {
148 SamplingGrid illegal_begin_grid(illegal_begin_props);
149 SamplingGrid illegal_end_grid(illegal_end_props);
150 SamplingGrid illegal_level_grid(illegal_level_props);
151 SamplingGrid legal_grid(legal_props);
152 illegal_begin_grid.setWindowSize(begin, end);
153 illegal_end_grid.setWindowSize(begin, end);
154 illegal_level_grid.setWindowSize(begin, end);
155 legal_grid.setWindowSize(begin, end);
156 CPPUNIT_ASSERT( !illegal_begin_grid.isCongruent(default_grid) );
157 CPPUNIT_ASSERT( !illegal_end_grid.isCongruent(default_grid) );
158 CPPUNIT_ASSERT( !illegal_level_grid.isCongruent(default_grid) );
159 CPPUNIT_ASSERT( legal_grid.isCongruent(default_grid) );
160 }
161
162 // different begin
163 {
164 SamplingGrid illegal_begin_grid(illegal_begin_props);
165 SamplingGrid illegal_end_grid(illegal_end_props);
166 SamplingGrid illegal_level_grid(illegal_level_props);
167 SamplingGrid legal_grid(legal_props);
168 illegal_begin_grid.setWindowSize(otherbegin_window, end);
169 illegal_end_grid.setWindowSize(otherbegin_window, end);
170 illegal_level_grid.setWindowSize(otherbegin_window, end);
171 legal_grid.setWindowSize(begin, end);
172 CPPUNIT_ASSERT( !illegal_begin_grid.isCongruent(legal_grid) );
173 CPPUNIT_ASSERT( !illegal_end_grid.isCongruent(legal_grid) );
174 CPPUNIT_ASSERT( !illegal_level_grid.isCongruent(legal_grid) );
175 CPPUNIT_ASSERT( legal_grid.isCongruent(default_grid) );
176 }
177
178 // different end
179 {
180 SamplingGrid illegal_begin_grid(illegal_begin_props);
181 SamplingGrid illegal_end_grid(illegal_end_props);
182 SamplingGrid illegal_level_grid(illegal_level_props);
183 SamplingGrid legal_grid(legal_props);
184 illegal_begin_grid.setWindowSize(begin, otherend_window);
185 illegal_end_grid.setWindowSize(begin, otherend_window);
186 illegal_level_grid.setWindowSize(begin, otherend_window);
187 legal_grid.setWindowSize(begin, end);
188 CPPUNIT_ASSERT( !illegal_begin_grid.isCongruent(legal_grid) );
189 CPPUNIT_ASSERT( !illegal_end_grid.isCongruent(legal_grid) );
190 CPPUNIT_ASSERT( !illegal_level_grid.isCongruent(legal_grid) );
191 CPPUNIT_ASSERT( legal_grid.isCongruent(default_grid) );
192 }
193
194 // different begin and end
195 {
196 SamplingGrid illegal_begin_grid(illegal_begin_props);
197 SamplingGrid illegal_end_grid(illegal_end_props);
198 SamplingGrid illegal_level_grid(illegal_level_props);
199 SamplingGrid legal_grid(legal_props);
200 illegal_begin_grid.setWindowSize(otherbegin_window, otherend_window);
201 illegal_end_grid.setWindowSize(otherbegin_window, otherend_window);
202 illegal_level_grid.setWindowSize(otherbegin_window, otherend_window);
203 legal_grid.setWindowSize(begin, end);
204 CPPUNIT_ASSERT( !illegal_begin_grid.isCongruent(legal_grid) );
205 CPPUNIT_ASSERT( !illegal_end_grid.isCongruent(legal_grid) );
206 CPPUNIT_ASSERT( !illegal_level_grid.isCongruent(legal_grid) );
207 CPPUNIT_ASSERT( legal_grid.isCongruent(default_grid) );
208 }
209}
210
211/** UnitTest for integral()
212 */
213void SamplingGridTest::integral_Test()
214{
215 double sum = 0.;
216 sum = std::accumulate( grid->sampled_grid.begin(), grid->sampled_grid.end(), sum );
217 CPPUNIT_ASSERT_EQUAL( sum*grid->getVolume()/grid->getWindowGridPoints(), grid->integral() );
218}
219
220/** UnitTest for getVolume()
221 */
222void SamplingGridTest::getVolume_Test()
223{
224 CPPUNIT_ASSERT_EQUAL( 1., grid->getVolume() );
225}
226
227/** UnitTest for getWindowSize()
228 */
229void SamplingGridTest::getWindowSize_Test()
230{
231 // check size of default grid
232 CPPUNIT_ASSERT_EQUAL( (size_t)(NUMBEROFSAMPLES(grid->level)), grid->getWindowGridPoints() );
233
234 // create another one and check its size, too
235 const double begin[3] = { 0., 0., 0. };
236 const double end[3] = { 1., 1., 1. };
237 for (size_t level = 3; level<=6; ++level) {
238 values.clear();
239 for (size_t i=0; i< NUMBEROFSAMPLES(level); ++i)
240 values += grid_value;
241 delete grid;
242 // use other pointer in case something fails
243 SamplingGrid *tmpgrid = new SamplingGrid(begin, end, level, values);
244 grid = tmpgrid;
245 CPPUNIT_ASSERT_EQUAL( (size_t)NUMBEROFSAMPLES(level), grid->getWindowGridPoints() );
246 }
247}
248
249/** UnitTest for extendWindow()
250 */
251void SamplingGridTest::extendWindow_Test()
252{
253 // we have a grid with size of one, extend to twice the size and check
254 const double begin[3] = { 0., 0., 0. };
255 const double size = 2.;
256 const double end[3] = { size, size, size };
257 double offset[3];
258 for (offset[0] = 0.; offset[0] <= 1.; offset[0] += .5)
259 for (offset[1] = 0.; offset[1] <= 1.; offset[1] += .5)
260 for (offset[2] = 0.; offset[2] <= 1.; offset[2] += .5) {
261 const double window_begin[3] = { 0.+offset[0], 0.+offset[1], 0.+offset[2]};
262 const double window_end[3] = { 1.+offset[0], 1.+offset[1], 1.+offset[2]};
263 SamplingGrid newgrid(begin, end, 2);
264 newgrid.setWindowSize(window_begin, window_end);
265 // resize values by hand to new window size. Otherwise they get zero'd.
266 newgrid.sampled_grid = values;
267 newgrid.sampled_grid.resize(NUMBEROFSAMPLES(1));
268 newgrid.extendWindow(begin, end);
269
270 // check integral
271 CPPUNIT_ASSERT_EQUAL(
272 grid_value/(NUMBEROFSAMPLES(grid->level)/NUMBEROFSAMPLES(grid->level)),
273 grid->integral()
274 );
275
276 // check number of points
277 CPPUNIT_ASSERT_EQUAL(
278 (size_t)NUMBEROFSAMPLES(grid->level),
279 grid->getWindowGridPoints()
280 );
281 }
282}
283
284/** UnitTest for extendWindow() with asymmetric values
285 */
286void SamplingGridTest::extendWindow_asymmetric_Test()
287{
288 std::cout << "SamplingGridTest::extendWindow_asymmetric_Test()" << std::endl;
289 const double begin[3] = { 0., 0., 0. };
290 const double end[3] = { 2., 2., 2. };
291 double offset[3];
292 for (offset[0] = 0.; offset[0] <= 1.; offset[0] += .5)
293 for (offset[1] = 0.; offset[1] <= 1.; offset[1] += .5)
294 for (offset[2] = 0.; offset[2] <= 1.; offset[2] += .5) {
295 const double window_begin[3] = { 0.+offset[0], 0.+offset[1], 0.+offset[2]};
296 const double window_end[3] = { 1.+offset[0], 1.+offset[1], 1.+offset[2]};
297 SamplingGrid newgrid(begin, end, 2);
298 CPPUNIT_ASSERT_EQUAL( (size_t)0, newgrid.getWindowGridPoints() );
299 newgrid.setWindowSize(window_begin, window_end);
300 // window size is only half of domain size
301 const size_t max_samples = NUMBEROFSAMPLES(newgrid.level)*pow(0.5,3);
302 for (size_t i=0; i< max_samples; ++i)
303 newgrid.sampled_grid += grid_value*i;
304 const size_t sum_weight = (max_samples)*(max_samples-1)/2;
305 const double integral = newgrid.integral();
306 newgrid.extendWindow(begin, end);
307
308 // check that integral has remained the same
309 CPPUNIT_ASSERT_EQUAL( integral, newgrid.integral() );
310 CPPUNIT_ASSERT_EQUAL( grid_value*sum_weight/DOMAINVOLUME(2), newgrid.integral() );
311 }
312}
313
314/** UnitTest for addOntoWindow()
315 */
316void SamplingGridTest::addOntoWindow_Test()
317{
318 // first window is from (0,0,0) to (1,1,1)
319 CPPUNIT_ASSERT_EQUAL( 1.*grid_value, grid->integral() );
320
321 // create values for half-sized window
322 values.clear();
323 for (size_t i=0; i< (size_t)pow(.5*pow(2,2),3); ++i)
324 values += grid_value;
325
326 // check that too large a window throws
327#ifndef NDEBUG
328 const double begin[3] = { .5, .5, .5 };
329 const double wrongend[3] = { 1.5, 1.5, 1.5 };
330 std::cout << "The following assertion is intended and does not indicate a failure of the test." << std::endl;
331 CPPUNIT_ASSERT_THROW( grid->addOntoWindow(begin, wrongend, values, +1.), Assert::AssertionFailure );
332#endif
333
334 // create another window from (.5,.5,.5) to (1., 1., 1.)
335 double offset[3];
336 for (offset[0] = 0.; offset[0] <= .5; offset[0] += .5)
337 for (offset[1] = 0.; offset[1] <= .5; offset[1] += .5)
338 for (offset[2] = 0.; offset[2] <= .5; offset[2] += .5) {
339 const double window_begin[3] = { 0.+offset[0], 0.+offset[1], 0.+offset[2]};
340 const double window_end[3] = { .5+offset[0], .5+offset[1], .5+offset[2]};
341
342 SamplingGrid newgrid(*grid);
343 // now perform working operation
344 newgrid.addOntoWindow(window_begin, window_end, values, +1.);
345
346 // check integral to be one and one eighth times the old value
347 CPPUNIT_ASSERT_EQUAL( (1.+pow(.5,3))*grid_value, newgrid.integral() );
348 }
349}
350
351/** UnitTest for addOntoWindow() with asymmetric values
352 */
353void SamplingGridTest::addOntoWindow_asymmetric_Test()
354{
355 const size_t size = grid->end[0]-grid->begin[0];
356 // check with asymmetric values
357 grid->sampled_grid.clear();
358 grid->sampled_grid.resize(DOMAINVOLUME(size)*NUMBEROFSAMPLES(grid->level), 0.);
359
360 for (size_t i=0;i<grid->level*(grid->end[0]-grid->begin[0]);++i)
361 grid->sampled_grid[(i*2+0)*2+0] += .5*grid_value;
362 for (size_t i=0;i<grid->level*(grid->end[1]-grid->begin[1]);++i)
363 grid->sampled_grid[(0*2+i)*2+0] += 1.*grid_value;
364 for (size_t i=0;i<grid->level*(grid->end[2]-grid->begin[2]);++i)
365 grid->sampled_grid[(0*2+0)*2+i] += 1.5*grid_value;
366
367 const double integral = grid->integral();
368
369 // now perform working operation
370 grid->addOntoWindow(grid->begin, grid->end, values, +1.);
371 // values is equal to integral of 1.
372 CPPUNIT_ASSERT_EQUAL( 1.+integral, grid->integral() );
373}
374
375/** UnitTest for operator+=()
376 */
377void SamplingGridTest::operatorPlusEqual_Test()
378{
379 // create other grid
380 const double begin[3] = { 0., 0., 0. };
381 const double end[3] = { 1., 1., 1. };
382 SamplingGrid::sampledvalues_t othervalues;
383 const double othergrid_value = 1.5;
384 for (size_t i=0; i< NUMBEROFSAMPLES(2); ++i)
385 othervalues += othergrid_value;
386 SamplingGrid othergrid(begin, end, 2, othervalues);
387 CPPUNIT_ASSERT_EQUAL( othergrid_value, *(othergrid.sampled_grid.begin()) );
388
389 // perform operation
390 CPPUNIT_ASSERT_NO_THROW( *grid += othergrid );
391
392 // check the contents of the grid
393 const double sum = grid_value+othergrid_value;
394 for (SamplingGrid::sampledvalues_t::const_iterator iter = grid->sampled_grid.begin();
395 iter != grid->sampled_grid.end(); ++iter)
396 CPPUNIT_ASSERT_EQUAL( sum, *iter );
397}
398
399/** UnitTest for operator-=()
400 */
401void SamplingGridTest::operatorMinusEqual_Test()
402{
403 // create other grid
404 const double begin[3] = { 0., 0., 0. };
405 const double end[3] = { 1., 1., 1. };
406 SamplingGrid::sampledvalues_t othervalues;
407 const double othergrid_value = 1.5;
408 for (size_t i=0; i< NUMBEROFSAMPLES(2); ++i)
409 othervalues += othergrid_value;
410 SamplingGrid othergrid(begin, end, 2, othervalues);
411 CPPUNIT_ASSERT_EQUAL( othergrid_value, *(othergrid.sampled_grid.begin()) );
412
413 // perform operation
414 CPPUNIT_ASSERT_NO_THROW( *grid -= othergrid );
415
416 // check the contents of the grid
417 const double difference = grid_value-othergrid_value;
418 for (SamplingGrid::sampledvalues_t::const_iterator iter = grid->sampled_grid.begin();
419 iter != grid->sampled_grid.end(); ++iter)
420 CPPUNIT_ASSERT_EQUAL( difference, *iter );
421}
422
Note: See TracBrowser for help on using the repository browser.