[c889b7] | 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 |
|
---|
| 35 | using 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>
|
---|
[c6355f] | 46 | #include <cmath>
|
---|
| 47 | #include <numeric>
|
---|
[c889b7] | 48 |
|
---|
| 49 | #ifdef HAVE_TESTRUNNER
|
---|
| 50 | #include "UnitTestMain.hpp"
|
---|
| 51 | #endif /*HAVE_TESTRUNNER*/
|
---|
| 52 |
|
---|
| 53 | using namespace boost::assign;
|
---|
| 54 |
|
---|
| 55 | /********************************************** Test classes **************************************/
|
---|
| 56 |
|
---|
| 57 | const double grid_value=1.;
|
---|
| 58 |
|
---|
[3d9a8d] | 59 | #define NUMBEROFSAMPLES(n) (size_t)(pow(pow(2,n),3))
|
---|
[c6355f] | 60 | #define DOMAINVOLUME(l) (size_t)pow(l,3)
|
---|
[3d9a8d] | 61 |
|
---|
[c889b7] | 62 | // Registers the fixture into the 'registry'
|
---|
| 63 | CPPUNIT_TEST_SUITE_REGISTRATION( SamplingGridTest );
|
---|
| 64 |
|
---|
| 65 |
|
---|
| 66 | void 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. };
|
---|
[3d9a8d] | 73 | const double end[3] = { 1., 1., 1. };
|
---|
[c6355f] | 74 | for (size_t i=0; i< DOMAINVOLUME(1)*NUMBEROFSAMPLES(2); ++i)
|
---|
[c889b7] | 75 | values += grid_value;
|
---|
[3d9a8d] | 76 | grid = new SamplingGrid(begin, end, 2, values);
|
---|
[c889b7] | 77 | CPPUNIT_ASSERT_EQUAL( grid_value, *(grid->sampled_grid.begin()) );
|
---|
| 78 | }
|
---|
| 79 |
|
---|
| 80 |
|
---|
| 81 | void SamplingGridTest::tearDown()
|
---|
| 82 | {
|
---|
| 83 | delete grid;
|
---|
| 84 | }
|
---|
| 85 |
|
---|
[1a00bb] | 86 | /** UnitTest on compatible combination of props and values
|
---|
[c889b7] | 87 | */
|
---|
[1a00bb] | 88 | void SamplingGridTest::compatibleGrids_Test()
|
---|
[c889b7] | 89 | {
|
---|
| 90 | // check illegal grid
|
---|
| 91 | const double begin[3] = { 0., 0., 0. };
|
---|
[3d9a8d] | 92 | const double end[3] = { 2., 2., 2. };
|
---|
[c6355f] | 93 | SamplingGridProperties illegal_props(begin, end, 5);
|
---|
[1a00bb] | 94 | SamplingGridProperties legal_props(begin, end, 2);
|
---|
[c889b7] | 95 | CPPUNIT_ASSERT( !grid->isCompatible(illegal_props) );
|
---|
[1a00bb] | 96 | CPPUNIT_ASSERT( grid->isCompatible(legal_props) );
|
---|
[c889b7] | 97 | SamplingGrid::sampledvalues_t illegal_values;
|
---|
| 98 | for (size_t i=0; i< NUMBEROFSAMPLES(1); ++i)
|
---|
| 99 | illegal_values += 1.5;
|
---|
[1a00bb] | 100 | SamplingGrid::sampledvalues_t legal_values;
|
---|
| 101 | for (size_t i=0; i< NUMBEROFSAMPLES(2); ++i)
|
---|
[c6355f] | 102 | legal_values += 1.5;
|
---|
[c889b7] | 103 | #ifndef NDEBUG
|
---|
[1a00bb] | 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 );
|
---|
[3d9a8d] | 107 | std::cout << "The following assertion is intended and does not indicate a failure of the test." << std::endl;
|
---|
[1a00bb] | 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 );
|
---|
[c889b7] | 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 |
|
---|
[c6355f] | 118 | /** UnitTest for integral()
|
---|
| 119 | */
|
---|
| 120 | void SamplingGridTest::integral_Test()
|
---|
| 121 | {
|
---|
| 122 | double sum = 0.;
|
---|
| 123 | sum = std::accumulate( grid->sampled_grid.begin(), grid->sampled_grid.end(), sum );
|
---|
| 124 | CPPUNIT_ASSERT_EQUAL( sum*grid->getVolume()/grid->getWindowGridPoints(), grid->integral() );
|
---|
| 125 | }
|
---|
| 126 |
|
---|
| 127 | /** UnitTest for getVolume()
|
---|
[3d9a8d] | 128 | */
|
---|
| 129 | void SamplingGridTest::getVolume_Test()
|
---|
| 130 | {
|
---|
| 131 | CPPUNIT_ASSERT_EQUAL( 1., grid->getVolume() );
|
---|
| 132 | }
|
---|
| 133 |
|
---|
[c6355f] | 134 | /** UnitTest for getWindowSize()
|
---|
[1a00bb] | 135 | */
|
---|
| 136 | void SamplingGridTest::getWindowSize_Test()
|
---|
| 137 | {
|
---|
| 138 | // check size of default grid
|
---|
[e2404f] | 139 | CPPUNIT_ASSERT_EQUAL( (size_t)(NUMBEROFSAMPLES(grid->level)), grid->getWindowGridPoints() );
|
---|
[1a00bb] | 140 |
|
---|
| 141 | // create another one and check its size, too
|
---|
| 142 | const double begin[3] = { 0., 0., 0. };
|
---|
| 143 | const double end[3] = { 1., 1., 1. };
|
---|
| 144 | for (size_t level = 3; level<=6; ++level) {
|
---|
| 145 | values.clear();
|
---|
| 146 | for (size_t i=0; i< NUMBEROFSAMPLES(level); ++i)
|
---|
| 147 | values += grid_value;
|
---|
| 148 | delete grid;
|
---|
[e2404f] | 149 | // use other pointer in case something fails
|
---|
| 150 | SamplingGrid *tmpgrid = new SamplingGrid(begin, end, level, values);
|
---|
| 151 | grid = tmpgrid;
|
---|
[1a00bb] | 152 | CPPUNIT_ASSERT_EQUAL( (size_t)NUMBEROFSAMPLES(level), grid->getWindowGridPoints() );
|
---|
| 153 | }
|
---|
| 154 | }
|
---|
| 155 |
|
---|
| 156 | /** UnitTest for extendWindow()
|
---|
| 157 | */
|
---|
| 158 | void SamplingGridTest::extendWindow_Test()
|
---|
| 159 | {
|
---|
[c6355f] | 160 | // we have a grid with size of one, extend to twice the size and check
|
---|
[1a00bb] | 161 | const double begin[3] = { 0., 0., 0. };
|
---|
[de6dfb] | 162 | const double size = 2.;
|
---|
| 163 | const double end[3] = { size, size, size };
|
---|
[c6355f] | 164 | double offset[3];
|
---|
| 165 | for (offset[0] = 0.; offset[0] <= 1.; offset[0] += .5)
|
---|
| 166 | for (offset[1] = 0.; offset[1] <= 1.; offset[1] += .5)
|
---|
| 167 | for (offset[2] = 0.; offset[2] <= 1.; offset[2] += .5) {
|
---|
| 168 | const double window_begin[3] = { 0.+offset[0], 0.+offset[1], 0.+offset[2]};
|
---|
| 169 | const double window_end[3] = { 1.+offset[0], 1.+offset[1], 1.+offset[2]};
|
---|
| 170 | SamplingGrid newgrid(begin, end, 2);
|
---|
| 171 | newgrid.setWindowSize(window_begin, window_end);
|
---|
| 172 | // resize values by hand to new window size. Otherwise they get zero'd.
|
---|
| 173 | newgrid.sampled_grid = values;
|
---|
| 174 | newgrid.sampled_grid.resize(NUMBEROFSAMPLES(1));
|
---|
| 175 | newgrid.extendWindow(begin, end);
|
---|
| 176 |
|
---|
| 177 | // check integral
|
---|
| 178 | CPPUNIT_ASSERT_EQUAL(
|
---|
| 179 | grid_value/(NUMBEROFSAMPLES(grid->level)/NUMBEROFSAMPLES(grid->level)),
|
---|
| 180 | grid->integral()
|
---|
| 181 | );
|
---|
| 182 |
|
---|
| 183 | // check number of points
|
---|
| 184 | CPPUNIT_ASSERT_EQUAL(
|
---|
| 185 | (size_t)NUMBEROFSAMPLES(grid->level),
|
---|
| 186 | grid->getWindowGridPoints()
|
---|
| 187 | );
|
---|
| 188 | }
|
---|
[de6dfb] | 189 | }
|
---|
| 190 |
|
---|
| 191 | /** UnitTest for extendWindow() with asymmetric values
|
---|
| 192 | */
|
---|
| 193 | void SamplingGridTest::extendWindow_asymmetric_Test()
|
---|
| 194 | {
|
---|
[c6355f] | 195 | std::cout << "SamplingGridTest::extendWindow_asymmetric_Test()" << std::endl;
|
---|
[de6dfb] | 196 | const double begin[3] = { 0., 0., 0. };
|
---|
| 197 | const double end[3] = { 2., 2., 2. };
|
---|
[c6355f] | 198 | double offset[3];
|
---|
| 199 | for (offset[0] = 0.; offset[0] <= 1.; offset[0] += .5)
|
---|
| 200 | for (offset[1] = 0.; offset[1] <= 1.; offset[1] += .5)
|
---|
| 201 | for (offset[2] = 0.; offset[2] <= 1.; offset[2] += .5) {
|
---|
| 202 | const double window_begin[3] = { 0.+offset[0], 0.+offset[1], 0.+offset[2]};
|
---|
| 203 | const double window_end[3] = { 1.+offset[0], 1.+offset[1], 1.+offset[2]};
|
---|
| 204 | SamplingGrid newgrid(begin, end, 2);
|
---|
| 205 | CPPUNIT_ASSERT_EQUAL( (size_t)0, newgrid.getWindowGridPoints() );
|
---|
| 206 | newgrid.setWindowSize(window_begin, window_end);
|
---|
| 207 | // window size is only half of domain size
|
---|
| 208 | const size_t max_samples = NUMBEROFSAMPLES(newgrid.level)*pow(0.5,3);
|
---|
| 209 | for (size_t i=0; i< max_samples; ++i)
|
---|
| 210 | newgrid.sampled_grid += grid_value*i;
|
---|
| 211 | const size_t sum_weight = (max_samples)*(max_samples-1)/2;
|
---|
| 212 | const double integral = newgrid.integral();
|
---|
| 213 | newgrid.extendWindow(begin, end);
|
---|
| 214 |
|
---|
| 215 | // check that integral has remained the same
|
---|
| 216 | CPPUNIT_ASSERT_EQUAL( integral, newgrid.integral() );
|
---|
| 217 | CPPUNIT_ASSERT_EQUAL( grid_value*sum_weight/DOMAINVOLUME(2), newgrid.integral() );
|
---|
| 218 | }
|
---|
[1a00bb] | 219 | }
|
---|
| 220 |
|
---|
| 221 | /** UnitTest for addOntoWindow()
|
---|
| 222 | */
|
---|
| 223 | void SamplingGridTest::addOntoWindow_Test()
|
---|
| 224 | {
|
---|
| 225 | // first window is from (0,0,0) to (1,1,1)
|
---|
| 226 | CPPUNIT_ASSERT_EQUAL( 1.*grid_value, grid->integral() );
|
---|
| 227 |
|
---|
[c6355f] | 228 | // create values for half-sized window
|
---|
[1a00bb] | 229 | values.clear();
|
---|
[c6355f] | 230 | for (size_t i=0; i< (size_t)pow(.5*pow(2,2),3); ++i)
|
---|
[1a00bb] | 231 | values += grid_value;
|
---|
[c6355f] | 232 |
|
---|
[1a00bb] | 233 | // check that too large a window throws
|
---|
| 234 | #ifndef NDEBUG
|
---|
[c6355f] | 235 | const double begin[3] = { .5, .5, .5 };
|
---|
[1a00bb] | 236 | const double wrongend[3] = { 1.5, 1.5, 1.5 };
|
---|
| 237 | std::cout << "The following assertion is intended and does not indicate a failure of the test." << std::endl;
|
---|
[de6dfb] | 238 | CPPUNIT_ASSERT_THROW( grid->addOntoWindow(begin, wrongend, values, +1.), Assert::AssertionFailure );
|
---|
[1a00bb] | 239 | #endif
|
---|
| 240 |
|
---|
[c6355f] | 241 | // create another window from (.5,.5,.5) to (1., 1., 1.)
|
---|
| 242 | double offset[3];
|
---|
| 243 | for (offset[0] = 0.; offset[0] <= .5; offset[0] += .5)
|
---|
| 244 | for (offset[1] = 0.; offset[1] <= .5; offset[1] += .5)
|
---|
| 245 | for (offset[2] = 0.; offset[2] <= .5; offset[2] += .5) {
|
---|
| 246 | const double window_begin[3] = { 0.+offset[0], 0.+offset[1], 0.+offset[2]};
|
---|
| 247 | const double window_end[3] = { .5+offset[0], .5+offset[1], .5+offset[2]};
|
---|
| 248 |
|
---|
| 249 | SamplingGrid newgrid(*grid);
|
---|
| 250 | // now perform working operation
|
---|
| 251 | newgrid.addOntoWindow(window_begin, window_end, values, +1.);
|
---|
| 252 |
|
---|
| 253 | // check integral to be one and one eighth times the old value
|
---|
| 254 | CPPUNIT_ASSERT_EQUAL( (1.+pow(.5,3))*grid_value, newgrid.integral() );
|
---|
| 255 | }
|
---|
[1a00bb] | 256 | }
|
---|
| 257 |
|
---|
[de6dfb] | 258 | /** UnitTest for addOntoWindow() with asymmetric values
|
---|
| 259 | */
|
---|
| 260 | void SamplingGridTest::addOntoWindow_asymmetric_Test()
|
---|
| 261 | {
|
---|
[c6355f] | 262 | const size_t size = grid->end[0]-grid->begin[0];
|
---|
[de6dfb] | 263 | // check with asymmetric values
|
---|
| 264 | grid->sampled_grid.clear();
|
---|
[c6355f] | 265 | grid->sampled_grid.resize(DOMAINVOLUME(size)*NUMBEROFSAMPLES(grid->level), 0.);
|
---|
[de6dfb] | 266 |
|
---|
| 267 | for (size_t i=0;i<grid->level*(grid->end[0]-grid->begin[0]);++i)
|
---|
| 268 | grid->sampled_grid[(i*2+0)*2+0] += .5*grid_value;
|
---|
| 269 | for (size_t i=0;i<grid->level*(grid->end[1]-grid->begin[1]);++i)
|
---|
| 270 | grid->sampled_grid[(0*2+i)*2+0] += 1.*grid_value;
|
---|
| 271 | for (size_t i=0;i<grid->level*(grid->end[2]-grid->begin[2]);++i)
|
---|
| 272 | grid->sampled_grid[(0*2+0)*2+i] += 1.5*grid_value;
|
---|
| 273 |
|
---|
| 274 | const double integral = grid->integral();
|
---|
[c6355f] | 275 |
|
---|
| 276 | // now perform working operation
|
---|
| 277 | grid->addOntoWindow(grid->begin, grid->end, values, +1.);
|
---|
| 278 | // values is equal to integral of 1.
|
---|
| 279 | CPPUNIT_ASSERT_EQUAL( 1.+integral, grid->integral() );
|
---|
[de6dfb] | 280 | }
|
---|
| 281 |
|
---|
[c889b7] | 282 | /** UnitTest for operator+=()
|
---|
| 283 | */
|
---|
| 284 | void SamplingGridTest::operatorPlusEqual_Test()
|
---|
| 285 | {
|
---|
| 286 | // create other grid
|
---|
| 287 | const double begin[3] = { 0., 0., 0. };
|
---|
[3d9a8d] | 288 | const double end[3] = { 1., 1., 1. };
|
---|
[c889b7] | 289 | SamplingGrid::sampledvalues_t othervalues;
|
---|
| 290 | const double othergrid_value = 1.5;
|
---|
| 291 | for (size_t i=0; i< NUMBEROFSAMPLES(2); ++i)
|
---|
| 292 | othervalues += othergrid_value;
|
---|
[3d9a8d] | 293 | SamplingGrid othergrid(begin, end, 2, othervalues);
|
---|
[c889b7] | 294 | CPPUNIT_ASSERT_EQUAL( othergrid_value, *(othergrid.sampled_grid.begin()) );
|
---|
| 295 |
|
---|
| 296 | // perform operation
|
---|
| 297 | CPPUNIT_ASSERT_NO_THROW( *grid += othergrid );
|
---|
| 298 |
|
---|
| 299 | // check the contents of the grid
|
---|
| 300 | const double sum = grid_value+othergrid_value;
|
---|
| 301 | for (SamplingGrid::sampledvalues_t::const_iterator iter = grid->sampled_grid.begin();
|
---|
| 302 | iter != grid->sampled_grid.end(); ++iter)
|
---|
| 303 | CPPUNIT_ASSERT_EQUAL( sum, *iter );
|
---|
| 304 | }
|
---|
| 305 |
|
---|
| 306 | /** UnitTest for operator-=()
|
---|
| 307 | */
|
---|
| 308 | void SamplingGridTest::operatorMinusEqual_Test()
|
---|
| 309 | {
|
---|
| 310 | // create other grid
|
---|
| 311 | const double begin[3] = { 0., 0., 0. };
|
---|
[3d9a8d] | 312 | const double end[3] = { 1., 1., 1. };
|
---|
[c889b7] | 313 | SamplingGrid::sampledvalues_t othervalues;
|
---|
| 314 | const double othergrid_value = 1.5;
|
---|
| 315 | for (size_t i=0; i< NUMBEROFSAMPLES(2); ++i)
|
---|
| 316 | othervalues += othergrid_value;
|
---|
[3d9a8d] | 317 | SamplingGrid othergrid(begin, end, 2, othervalues);
|
---|
[c889b7] | 318 | CPPUNIT_ASSERT_EQUAL( othergrid_value, *(othergrid.sampled_grid.begin()) );
|
---|
| 319 |
|
---|
| 320 | // perform operation
|
---|
| 321 | CPPUNIT_ASSERT_NO_THROW( *grid -= othergrid );
|
---|
| 322 |
|
---|
| 323 | // check the contents of the grid
|
---|
| 324 | const double difference = grid_value-othergrid_value;
|
---|
| 325 | for (SamplingGrid::sampledvalues_t::const_iterator iter = grid->sampled_grid.begin();
|
---|
| 326 | iter != grid->sampled_grid.end(); ++iter)
|
---|
| 327 | CPPUNIT_ASSERT_EQUAL( difference, *iter );
|
---|
| 328 | }
|
---|
| 329 |
|
---|