| [28c025] | 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. | 
|---|
| [28c025] | 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 | * SamplingGrid.cpp | 
|---|
|  | 26 | * | 
|---|
|  | 27 | *  Created on: 25.07.2012 | 
|---|
|  | 28 | *      Author: heber | 
|---|
|  | 29 | */ | 
|---|
|  | 30 |  | 
|---|
|  | 31 | // include config.h | 
|---|
|  | 32 | #ifdef HAVE_CONFIG_H | 
|---|
|  | 33 | #include <config.h> | 
|---|
|  | 34 | #endif | 
|---|
|  | 35 |  | 
|---|
|  | 36 | // include headers that implement a archive in simple text format | 
|---|
|  | 37 | // otherwise BOOST_CLASS_EXPORT_IMPLEMENT has no effect | 
|---|
|  | 38 | #include <boost/archive/text_oarchive.hpp> | 
|---|
|  | 39 | #include <boost/archive/text_iarchive.hpp> | 
|---|
|  | 40 |  | 
|---|
|  | 41 | #include "CodePatterns/MemDebug.hpp" | 
|---|
|  | 42 |  | 
|---|
| [fbf143] | 43 | #include "Fragmentation/Summation/SetValues/SamplingGrid.hpp" | 
|---|
| [28c025] | 44 |  | 
|---|
| [fb3485] | 45 | #include <boost/bind.hpp> | 
|---|
| [98f8fe] | 46 | #include <limits> | 
|---|
|  | 47 |  | 
|---|
| [c889b7] | 48 | #include "CodePatterns/Assert.hpp" | 
|---|
| [cb3363] | 49 | #include "CodePatterns/Log.hpp" | 
|---|
| [28c025] | 50 |  | 
|---|
| [1a00bb] | 51 | // static instances | 
|---|
|  | 52 | const double SamplingGrid::zeroOffset[3] = { 0., 0., 0. }; | 
|---|
|  | 53 |  | 
|---|
|  | 54 | SamplingGrid::SamplingGrid() : | 
|---|
|  | 55 | SamplingGridProperties() | 
|---|
|  | 56 | { | 
|---|
|  | 57 | setWindowSize(zeroOffset, zeroOffset); | 
|---|
|  | 58 | ASSERT( getWindowGridPoints() == (size_t)0, | 
|---|
|  | 59 | "SamplingGrid::SamplingGrid() - incorrect number of samples given for the window."); | 
|---|
|  | 60 | } | 
|---|
|  | 61 |  | 
|---|
| [c6355f] | 62 | SamplingGrid::SamplingGrid(const double _begin[3], | 
|---|
|  | 63 | const double _end[3], | 
|---|
|  | 64 | const int _level) : | 
|---|
|  | 65 | SamplingGridProperties(_begin, _end, _level) | 
|---|
|  | 66 | { | 
|---|
|  | 67 | setWindowSize(zeroOffset, zeroOffset); | 
|---|
|  | 68 | ASSERT( getWindowGridPoints() == (size_t)0, | 
|---|
|  | 69 | "SamplingGrid::SamplingGrid() - incorrect number of samples given for the window."); | 
|---|
|  | 70 | } | 
|---|
|  | 71 |  | 
|---|
| [28c025] | 72 | SamplingGrid::SamplingGrid(const double _begin[3], | 
|---|
| [3d9a8d] | 73 | const double _end[3], | 
|---|
| [28c025] | 74 | const int _level, | 
|---|
| [c889b7] | 75 | const sampledvalues_t &_sampled_grid) : | 
|---|
| [3d9a8d] | 76 | SamplingGridProperties(_begin, _end, _level), | 
|---|
| [28c025] | 77 | sampled_grid(_sampled_grid) | 
|---|
| [1a00bb] | 78 | { | 
|---|
|  | 79 | setWindowSize(_begin, _end); | 
|---|
|  | 80 | ASSERT( getWindowGridPoints() == (size_t)_sampled_grid.size(), | 
|---|
|  | 81 | "SamplingGrid::SamplingGrid() - incorrect number of samples given for the window."); | 
|---|
|  | 82 | } | 
|---|
| [28c025] | 83 |  | 
|---|
|  | 84 | SamplingGrid::SamplingGrid(const SamplingGrid &_grid) : | 
|---|
|  | 85 | SamplingGridProperties(_grid), | 
|---|
|  | 86 | sampled_grid(_grid.sampled_grid) | 
|---|
| [1a00bb] | 87 | { | 
|---|
| [c6355f] | 88 | setWindowSize(_grid.begin_window, _grid.end_window); | 
|---|
|  | 89 | ASSERT( getWindowGridPoints() == _grid.getWindowGridPoints(), | 
|---|
|  | 90 | "SamplingGrid::SamplingGrid() - incorrect number of samples given for the window."); | 
|---|
| [1a00bb] | 91 | } | 
|---|
| [28c025] | 92 |  | 
|---|
|  | 93 | SamplingGrid::SamplingGrid(const SamplingGridProperties &_props) : | 
|---|
|  | 94 | SamplingGridProperties(_props) | 
|---|
| [1a00bb] | 95 | { | 
|---|
| [c6355f] | 96 | setWindowSize(zeroOffset, zeroOffset); | 
|---|
|  | 97 | ASSERT( getWindowGridPoints() == (size_t)0, | 
|---|
|  | 98 | "SamplingGrid::SamplingGrid() - incorrect number of samples given for the window."); | 
|---|
| [1a00bb] | 99 | } | 
|---|
| [28c025] | 100 |  | 
|---|
| [c889b7] | 101 | SamplingGrid::SamplingGrid( | 
|---|
|  | 102 | const SamplingGridProperties &_props, | 
|---|
|  | 103 | const sampledvalues_t &_sampled_grid) : | 
|---|
|  | 104 | SamplingGridProperties(_props), | 
|---|
|  | 105 | sampled_grid(_sampled_grid) | 
|---|
| [1a00bb] | 106 | { | 
|---|
|  | 107 | setWindowSize(_props.begin, _props.end); | 
|---|
|  | 108 | ASSERT( getWindowGridPoints() == (size_t)_sampled_grid.size(), | 
|---|
|  | 109 | "SamplingGrid::SamplingGrid() - incorrect number of samples given for the window."); | 
|---|
|  | 110 | } | 
|---|
| [c889b7] | 111 |  | 
|---|
| [28c025] | 112 | SamplingGrid::~SamplingGrid() | 
|---|
|  | 113 | {} | 
|---|
|  | 114 |  | 
|---|
| [c0e8fb] | 115 | bool SamplingGrid::isCongruent(const SamplingGrid &_props) const | 
|---|
|  | 116 | { | 
|---|
|  | 117 | bool status = true; | 
|---|
|  | 118 | status &= (static_cast<const SamplingGridProperties &>(*this) == | 
|---|
|  | 119 | static_cast<const SamplingGridProperties &>(_props)); | 
|---|
|  | 120 | for(size_t i = 0; i<3; ++i) { | 
|---|
|  | 121 | status &= begin_window[i] == _props.begin_window[i]; | 
|---|
|  | 122 | status &= end_window[i] == _props.end_window[i]; | 
|---|
|  | 123 | } | 
|---|
|  | 124 | return status; | 
|---|
|  | 125 | } | 
|---|
|  | 126 |  | 
|---|
| [c889b7] | 127 | SamplingGrid& SamplingGrid::operator=(const SamplingGrid& other) | 
|---|
|  | 128 | { | 
|---|
|  | 129 | // check for self-assignment | 
|---|
|  | 130 | if (this != &other) { | 
|---|
|  | 131 | static_cast<SamplingGridProperties &>(*this) = other; | 
|---|
| [e2404f] | 132 | setWindowSize(other.begin_window, other.end_window); | 
|---|
| [c889b7] | 133 | sampled_grid = other.sampled_grid; | 
|---|
|  | 134 | } | 
|---|
|  | 135 | return *this; | 
|---|
|  | 136 | } | 
|---|
|  | 137 |  | 
|---|
| [313f83] | 138 | static void multiplyElements( | 
|---|
|  | 139 | double &dest, | 
|---|
|  | 140 | const double &source, | 
|---|
|  | 141 | const double prefactor) | 
|---|
|  | 142 | { | 
|---|
|  | 143 | dest *= prefactor*(source); | 
|---|
|  | 144 | } | 
|---|
|  | 145 |  | 
|---|
|  | 146 |  | 
|---|
| [a1fcc6] | 147 | SamplingGrid& SamplingGrid::operator*=(const SamplingGrid& other) | 
|---|
|  | 148 | { | 
|---|
|  | 149 | // check that grids are compatible | 
|---|
| [313f83] | 150 | if (isCompatible(other)) { | 
|---|
|  | 151 | /// get minimum of window | 
|---|
|  | 152 | double min_begin_window[3]; | 
|---|
|  | 153 | double min_end_window[3]; | 
|---|
|  | 154 | bool doShrink = false; | 
|---|
|  | 155 | for (size_t index=0; index<3;++index) { | 
|---|
|  | 156 | if (begin_window[index] <= other.begin_window[index]) { | 
|---|
|  | 157 | min_begin_window[index] = other.begin_window[index]; | 
|---|
|  | 158 | doShrink = true; | 
|---|
|  | 159 | } else { | 
|---|
|  | 160 | min_begin_window[index] = begin_window[index]; | 
|---|
|  | 161 | } | 
|---|
|  | 162 | if (end_window[index] <= other.end_window[index]) { | 
|---|
|  | 163 | min_end_window[index] = end_window[index]; | 
|---|
|  | 164 | } else { | 
|---|
|  | 165 | min_end_window[index] = other.end_window[index]; | 
|---|
|  | 166 | doShrink = true; | 
|---|
|  | 167 | } | 
|---|
|  | 168 | } | 
|---|
| [10f0cb] | 169 | LOG(4, "DEBUG: min begin is " << min_begin_window[0] << "," << min_begin_window[1] << "," << min_begin_window[2] << "."); | 
|---|
|  | 170 | LOG(4, "DEBUG: min end is " << min_end_window[0] << "," << min_end_window[1] << "," << min_end_window[2] << "."); | 
|---|
| [313f83] | 171 | if (doShrink) | 
|---|
|  | 172 | shrinkWindow(min_begin_window, min_end_window); | 
|---|
|  | 173 | addWindowOntoWindow( | 
|---|
|  | 174 | other.begin_window, | 
|---|
|  | 175 | other.end_window, | 
|---|
|  | 176 | begin_window, | 
|---|
|  | 177 | end_window, | 
|---|
|  | 178 | sampled_grid, | 
|---|
|  | 179 | other.sampled_grid, | 
|---|
|  | 180 | boost::bind(multiplyElements, _1, _2, 1.), | 
|---|
|  | 181 | sourcewindow); | 
|---|
| [a1fcc6] | 182 | } else { | 
|---|
| [c0e8fb] | 183 | ASSERT(0, "SamplingGrid::operator*=() - multiplying incongruent grids is so far not in the cards."); | 
|---|
| [a1fcc6] | 184 | } | 
|---|
|  | 185 | return *this; | 
|---|
|  | 186 | } | 
|---|
|  | 187 |  | 
|---|
| [c889b7] | 188 | void SamplingGrid::superposeOtherGrids(const SamplingGrid &other, const double prefactor) | 
|---|
|  | 189 | { | 
|---|
| [1a00bb] | 190 | /// check that grids are compatible | 
|---|
| [c889b7] | 191 | if (isCompatible(other)) { | 
|---|
| [1a00bb] | 192 | /// get maximum of window | 
|---|
|  | 193 | double max_begin_window[3]; | 
|---|
|  | 194 | double max_end_window[3]; | 
|---|
|  | 195 | bool doExtend = false; | 
|---|
|  | 196 | for (size_t index=0; index<3;++index) { | 
|---|
|  | 197 | if (begin_window[index] >= other.begin_window[index]) { | 
|---|
|  | 198 | max_begin_window[index] = other.begin_window[index]; | 
|---|
|  | 199 | doExtend = true; | 
|---|
|  | 200 | } else { | 
|---|
|  | 201 | max_begin_window[index] = begin_window[index]; | 
|---|
|  | 202 | } | 
|---|
|  | 203 | if (end_window[index] >= other.end_window[index]) { | 
|---|
|  | 204 | max_end_window[index] = end_window[index]; | 
|---|
|  | 205 | } else { | 
|---|
|  | 206 | max_end_window[index] = other.end_window[index]; | 
|---|
|  | 207 | doExtend = true; | 
|---|
|  | 208 | } | 
|---|
|  | 209 | } | 
|---|
| [10f0cb] | 210 | LOG(4, "DEBUG: max begin is " << max_begin_window[0] << "," << max_begin_window[1] << "," << max_begin_window[2] << "."); | 
|---|
|  | 211 | LOG(4, "DEBUG: max end is " << max_end_window[0] << "," << max_end_window[1] << "," << max_end_window[2] << "."); | 
|---|
| [1a00bb] | 212 | if (doExtend) | 
|---|
|  | 213 | extendWindow(max_begin_window, max_end_window); | 
|---|
|  | 214 | /// and copy other into larger window, too | 
|---|
| [de6dfb] | 215 | addOntoWindow(other.begin_window, other.end_window, other.sampled_grid, prefactor); | 
|---|
| [c889b7] | 216 | } else { | 
|---|
|  | 217 | ASSERT(0, "SamplingGrid::superposeOtherGrids() - superposing incompatible grids is so far not in the cards."); | 
|---|
|  | 218 | } | 
|---|
|  | 219 | } | 
|---|
|  | 220 |  | 
|---|
| [620517] | 221 | const size_t SamplingGrid::getWindowGridPointsPerAxis(const size_t axis) const | 
|---|
| [3d9a8d] | 222 | { | 
|---|
| [620517] | 223 | static const double round_offset( | 
|---|
|  | 224 | (std::numeric_limits<size_t>::round_style == std::round_toward_zero) ? | 
|---|
|  | 225 | 0.5 : 0.); // need offset to get to round_toward_nearest behavior | 
|---|
|  | 226 | //  const double total = getTotalLengthPerAxis(axis); | 
|---|
|  | 227 | const double delta = getDeltaPerAxis(axis); | 
|---|
|  | 228 | if (delta == 0) | 
|---|
|  | 229 | return 0; | 
|---|
|  | 230 | const double length = getWindowLengthPerAxis(axis); | 
|---|
|  | 231 | if (length == 0) | 
|---|
| [1a00bb] | 232 | return 0; | 
|---|
| [620517] | 233 | return (size_t)(length/delta+round_offset); | 
|---|
| [1a00bb] | 234 | } | 
|---|
|  | 235 |  | 
|---|
| [cb3363] | 236 | double SamplingGrid::integral() const | 
|---|
|  | 237 | { | 
|---|
| [98f8fe] | 238 | const double volume_element = getVolume()/(double)getTotalGridPoints(); | 
|---|
| [cb3363] | 239 | double int_value = 0.; | 
|---|
|  | 240 | for (sampledvalues_t::const_iterator iter = sampled_grid.begin(); | 
|---|
|  | 241 | iter != sampled_grid.end(); | 
|---|
|  | 242 | ++iter) | 
|---|
|  | 243 | int_value += *iter; | 
|---|
|  | 244 | int_value *= volume_element; | 
|---|
|  | 245 | LOG(2, "DEBUG: SamplingGrid::integral() is " << scientific << setprecision(13) << int_value << "."); | 
|---|
|  | 246 | return int_value; | 
|---|
|  | 247 | } | 
|---|
|  | 248 |  | 
|---|
| [e72c61] | 249 | double SamplingGrid::integral(const SamplingGrid &weight) const | 
|---|
|  | 250 | { | 
|---|
|  | 251 | if (isCompatible(weight)) { | 
|---|
| [98f8fe] | 252 | const double volume_element = getVolume()/(double)getTotalGridPoints(); | 
|---|
| [e72c61] | 253 | double int_value = 0.; | 
|---|
|  | 254 | sampledvalues_t::const_iterator iter = sampled_grid.begin(); | 
|---|
|  | 255 | sampledvalues_t::const_iterator weightiter = weight.sampled_grid.begin(); | 
|---|
|  | 256 | for (;iter != sampled_grid.end();++iter,++weightiter) | 
|---|
|  | 257 | int_value += (*weightiter) * (*iter); | 
|---|
|  | 258 | int_value *= volume_element; | 
|---|
|  | 259 | //LOG(2, "DEBUG: SamplingGrid::integral() is " << scientific << setprecision(13) << int_value << "."); | 
|---|
|  | 260 | return int_value; | 
|---|
|  | 261 | } else | 
|---|
|  | 262 | return 0.; | 
|---|
|  | 263 | } | 
|---|
|  | 264 |  | 
|---|
| [64bafe0] | 265 | double SamplingGrid::getNearestLowerGridPoint( | 
|---|
|  | 266 | const double value, const size_t axis) const | 
|---|
|  | 267 | { | 
|---|
| [955051] | 268 | const double length = getTotalLengthPerAxis(axis); | 
|---|
| [1f1f80] | 269 | if ((fabs(length) < std::numeric_limits<double>::epsilon()) || (getGridPointsPerAxis() == 0)) | 
|---|
|  | 270 | return begin[axis]; | 
|---|
| [955051] | 271 | if ((value - end[axis]) > -std::numeric_limits<double>::epsilon()*1.e4) | 
|---|
| [1f1f80] | 272 | return end[axis]; | 
|---|
|  | 273 | const double offset = value - begin[axis]; | 
|---|
| [955051] | 274 | if (offset < std::numeric_limits<double>::epsilon()*1.e4) | 
|---|
| [1f1f80] | 275 | return begin[axis]; | 
|---|
|  | 276 | // modify value a little if value actually has been on a grid point but | 
|---|
|  | 277 | // perturbed by numerical rounding: here up, as we always go lower | 
|---|
|  | 278 | const double factor = | 
|---|
|  | 279 | floor((double)getGridPointsPerAxis()*(offset/length) | 
|---|
|  | 280 | +std::numeric_limits<double>::epsilon()*1.e4); | 
|---|
| [955051] | 281 | return begin[axis]+factor*getDeltaPerAxis(axis); | 
|---|
| [64bafe0] | 282 | } | 
|---|
|  | 283 |  | 
|---|
|  | 284 | double SamplingGrid::getNearestHigherGridPoint( | 
|---|
|  | 285 | const double value, const size_t axis) const | 
|---|
|  | 286 | { | 
|---|
| [955051] | 287 | const double length = getTotalLengthPerAxis(axis); | 
|---|
| [1f1f80] | 288 | if ((fabs(length) < std::numeric_limits<double>::epsilon()) || (getGridPointsPerAxis() == 0)) | 
|---|
|  | 289 | return end[axis]; | 
|---|
| [955051] | 290 | if ((value - end[axis]) > -std::numeric_limits<double>::epsilon()*1.e4) | 
|---|
| [1f1f80] | 291 | return end[axis]; | 
|---|
|  | 292 | const double offset = value - begin[axis]; | 
|---|
| [955051] | 293 | if (offset < std::numeric_limits<double>::epsilon()*1.e4) | 
|---|
| [1f1f80] | 294 | return begin[axis]; | 
|---|
|  | 295 | // modify value a little if value actually has been on a grid point but | 
|---|
|  | 296 | // perturbed by numerical rounding: here down, as we always go higher | 
|---|
|  | 297 | const double factor = | 
|---|
|  | 298 | ceil((double)getGridPointsPerAxis()*(offset/length) | 
|---|
|  | 299 | -std::numeric_limits<double>::epsilon()*1.e4); | 
|---|
| [955051] | 300 | return begin[axis]+factor*getDeltaPerAxis(axis); | 
|---|
| [64bafe0] | 301 | } | 
|---|
|  | 302 |  | 
|---|
| [1a00bb] | 303 | void SamplingGrid::setWindowSize( | 
|---|
|  | 304 | const double _begin_window[3], | 
|---|
|  | 305 | const double _end_window[3]) | 
|---|
|  | 306 | { | 
|---|
|  | 307 | for (size_t index=0;index<3;++index) { | 
|---|
| [64bafe0] | 308 | begin_window[index] = getNearestLowerGridPoint(_begin_window[index], index); | 
|---|
|  | 309 | ASSERT( begin_window[index] >= begin[index], | 
|---|
| [e2404f] | 310 | "SamplingGrid::setWindowSize() - window starts earlier than domain on " | 
|---|
|  | 311 | +toString(index)+"th component."); | 
|---|
| [64bafe0] | 312 | end_window[index] = getNearestHigherGridPoint(_end_window[index], index); | 
|---|
|  | 313 | ASSERT( end_window[index] <= end[index], | 
|---|
| [e2404f] | 314 | "SamplingGrid::setWindowSize() - window ends later than domain on " | 
|---|
|  | 315 | +toString(index)+"th component."); | 
|---|
| [1a00bb] | 316 | } | 
|---|
|  | 317 | } | 
|---|
|  | 318 |  | 
|---|
|  | 319 | void SamplingGrid::setWindow( | 
|---|
|  | 320 | const double _begin_window[3], | 
|---|
|  | 321 | const double _end_window[3]) | 
|---|
|  | 322 | { | 
|---|
|  | 323 | setWindowSize(_begin_window, _end_window); | 
|---|
|  | 324 | const size_t gridpoints_window = getWindowGridPoints(); | 
|---|
| [c6355f] | 325 | sampled_grid.clear(); | 
|---|
| [1a00bb] | 326 | sampled_grid.resize(gridpoints_window, 0.); | 
|---|
|  | 327 | } | 
|---|
|  | 328 |  | 
|---|
| [e2404f] | 329 | void SamplingGrid::setDomain( | 
|---|
|  | 330 | const double _begin[3], | 
|---|
|  | 331 | const double _end[3]) | 
|---|
|  | 332 | { | 
|---|
|  | 333 | setDomainSize(_begin, _end); | 
|---|
|  | 334 | setWindowSize(_begin, _end); | 
|---|
|  | 335 | const size_t gridpoints = getTotalGridPoints(); | 
|---|
|  | 336 | sampled_grid.resize(gridpoints, 0.); | 
|---|
|  | 337 | } | 
|---|
|  | 338 |  | 
|---|
| [1a00bb] | 339 | void SamplingGrid::extendWindow( | 
|---|
|  | 340 | const double _begin_window[3], | 
|---|
|  | 341 | const double _end_window[3]) | 
|---|
|  | 342 | { | 
|---|
|  | 343 | #ifndef NDEBUG | 
|---|
|  | 344 | for(size_t index=0;index < 3; ++index) { | 
|---|
| [c6355f] | 345 | // check that we truly have to extend the window | 
|---|
| [1a00bb] | 346 | ASSERT ( begin_window[index] >= _begin_window[index], | 
|---|
|  | 347 | "SamplingGrid::extendWindow() - component "+toString(index)+ | 
|---|
|  | 348 | " of window start is greater than old value."); | 
|---|
|  | 349 | ASSERT ( end_window[index] <= _end_window[index], | 
|---|
|  | 350 | "SamplingGrid::extendWindow() - component "+toString(index)+ | 
|---|
|  | 351 | " of window end is less than old value."); | 
|---|
| [c6355f] | 352 |  | 
|---|
|  | 353 | // check that we are still less than domain | 
|---|
|  | 354 | ASSERT ( _begin_window[index] >= begin[index], | 
|---|
|  | 355 | "SamplingGrid::extendWindow() - component "+toString(index)+ | 
|---|
|  | 356 | " of window start is less than domain start."); | 
|---|
|  | 357 | ASSERT ( _end_window[index] <= end[index], | 
|---|
|  | 358 | "SamplingGrid::extendWindow() - component "+toString(index)+ | 
|---|
|  | 359 | " of window end is greater than domain end."); | 
|---|
| [1a00bb] | 360 | } | 
|---|
|  | 361 | #endif | 
|---|
|  | 362 | // copy old window size and values | 
|---|
|  | 363 | double old_begin_window[3]; | 
|---|
|  | 364 | double old_end_window[3]; | 
|---|
|  | 365 | for(size_t index=0;index<3;++index) { | 
|---|
|  | 366 | old_begin_window[index] = begin_window[index]; | 
|---|
|  | 367 | old_end_window[index] = end_window[index]; | 
|---|
|  | 368 | } | 
|---|
|  | 369 | sampledvalues_t old_values(sampled_grid); | 
|---|
| [de6dfb] | 370 | // set new window | 
|---|
|  | 371 | setWindow(_begin_window,_end_window); | 
|---|
| [1a00bb] | 372 | // now extend it ... | 
|---|
| [de6dfb] | 373 | addOntoWindow(old_begin_window, old_end_window, old_values, +1.); | 
|---|
| [10f0cb] | 374 | LOG(6, "DEBUG: Grid after extension is " << sampled_grid << "."); | 
|---|
| [1a00bb] | 375 | } | 
|---|
|  | 376 |  | 
|---|
| [313f83] | 377 | void SamplingGrid::shrinkWindow( | 
|---|
|  | 378 | const double _begin_window[3], | 
|---|
|  | 379 | const double _end_window[3]) | 
|---|
|  | 380 | { | 
|---|
|  | 381 | #ifndef NDEBUG | 
|---|
|  | 382 | for(size_t index=0;index < 3; ++index) { | 
|---|
|  | 383 | // check that we truly have to shrink the window | 
|---|
|  | 384 | ASSERT ( begin_window[index] <= _begin_window[index], | 
|---|
|  | 385 | "SamplingGrid::shrinkWindow() - component "+toString(index)+ | 
|---|
|  | 386 | " of window start is less than old value."); | 
|---|
|  | 387 | ASSERT ( end_window[index] >= _end_window[index], | 
|---|
|  | 388 | "SamplingGrid::shrinkWindow() - component "+toString(index)+ | 
|---|
|  | 389 | " of window end is greater than old value."); | 
|---|
|  | 390 |  | 
|---|
|  | 391 | // check that we are still less than domain | 
|---|
|  | 392 | ASSERT ( _begin_window[index] >= begin[index], | 
|---|
|  | 393 | "SamplingGrid::shrinkWindow() - component "+toString(index)+ | 
|---|
|  | 394 | " of window start is less than domain start."); | 
|---|
|  | 395 | ASSERT ( _end_window[index] <= end[index], | 
|---|
|  | 396 | "SamplingGrid::shrinkWindow() - component "+toString(index)+ | 
|---|
|  | 397 | " of window end is greater than domain end."); | 
|---|
|  | 398 | } | 
|---|
|  | 399 | #endif | 
|---|
|  | 400 | // copy old window size and values | 
|---|
|  | 401 | double old_begin_window[3]; | 
|---|
|  | 402 | double old_end_window[3]; | 
|---|
|  | 403 | for(size_t index=0;index<3;++index) { | 
|---|
|  | 404 | old_begin_window[index] = begin_window[index]; | 
|---|
|  | 405 | old_end_window[index] = end_window[index]; | 
|---|
|  | 406 | } | 
|---|
|  | 407 | sampledvalues_t old_values(sampled_grid); | 
|---|
|  | 408 | // set new window | 
|---|
|  | 409 | setWindow(_begin_window,_end_window); | 
|---|
|  | 410 | // now extend it ... | 
|---|
|  | 411 | addIntoWindow(old_begin_window, old_end_window, old_values, +1.); | 
|---|
| [10f0cb] | 412 | LOG(6, "DEBUG: Grid after extension is " << sampled_grid << "."); | 
|---|
| [313f83] | 413 | } | 
|---|
|  | 414 |  | 
|---|
| [fb3485] | 415 | static void addElements( | 
|---|
|  | 416 | double &dest, | 
|---|
|  | 417 | const double &source, | 
|---|
|  | 418 | const double prefactor) | 
|---|
|  | 419 | { | 
|---|
|  | 420 | dest += prefactor*(source); | 
|---|
|  | 421 | } | 
|---|
|  | 422 |  | 
|---|
| [1a00bb] | 423 | void SamplingGrid::addOntoWindow( | 
|---|
|  | 424 | const double _begin_window[3], | 
|---|
|  | 425 | const double _end_window[3], | 
|---|
| [de6dfb] | 426 | const sampledvalues_t &_sampled_grid, | 
|---|
|  | 427 | const double prefactor) | 
|---|
| [fb3485] | 428 | { | 
|---|
|  | 429 | addWindowOntoWindow( | 
|---|
|  | 430 | begin_window, | 
|---|
|  | 431 | end_window, | 
|---|
|  | 432 | _begin_window, | 
|---|
|  | 433 | _end_window, | 
|---|
|  | 434 | sampled_grid, | 
|---|
|  | 435 | _sampled_grid, | 
|---|
|  | 436 | boost::bind(addElements, _1, _2, boost::cref(prefactor)), | 
|---|
| [313f83] | 437 | destwindow); | 
|---|
|  | 438 | } | 
|---|
|  | 439 |  | 
|---|
|  | 440 | void SamplingGrid::addIntoWindow( | 
|---|
|  | 441 | const double _begin_window[3], | 
|---|
|  | 442 | const double _end_window[3], | 
|---|
|  | 443 | const sampledvalues_t &_sampled_grid, | 
|---|
|  | 444 | const double prefactor) | 
|---|
|  | 445 | { | 
|---|
|  | 446 | addWindowOntoWindow( | 
|---|
|  | 447 | _begin_window, | 
|---|
|  | 448 | _end_window, | 
|---|
|  | 449 | begin_window, | 
|---|
|  | 450 | end_window, | 
|---|
|  | 451 | sampled_grid, | 
|---|
|  | 452 | _sampled_grid, | 
|---|
|  | 453 | boost::bind(addElements, _1, _2, boost::cref(prefactor)), | 
|---|
|  | 454 | sourcewindow); | 
|---|
| [fb3485] | 455 | } | 
|---|
|  | 456 |  | 
|---|
| [3f64ee] | 457 | void SamplingGrid::getDiscreteWindowIndices( | 
|---|
|  | 458 | const double *larger_wbegin, | 
|---|
|  | 459 | const double *larger_wend, | 
|---|
|  | 460 | const double *smaller_wbegin, | 
|---|
|  | 461 | const double *smaller_wend, | 
|---|
|  | 462 | size_t *pre_offset, | 
|---|
|  | 463 | size_t *post_offset, | 
|---|
|  | 464 | size_t *length, | 
|---|
|  | 465 | size_t *total) const | 
|---|
|  | 466 | { | 
|---|
|  | 467 | const size_t gridpoints_axis = getGridPointsPerAxis(); | 
|---|
|  | 468 | const double round_offset = | 
|---|
|  | 469 | (std::numeric_limits<size_t>::round_style == std::round_toward_zero) ? | 
|---|
|  | 470 | 0.5 : 0.; // need offset to get to round_toward_nearest behavior | 
|---|
|  | 471 | for(size_t index=0;index<3;++index) { | 
|---|
|  | 472 | if (fabs(end[index] - begin[index]) > std::numeric_limits<double>::epsilon()*1e4) { | 
|---|
|  | 473 | // we refrain from using floor/ceil as the window's starts and ends, | 
|---|
|  | 474 | // the grids have to be compatible (equal level), should always be on | 
|---|
|  | 475 | // discrete grid point locations. | 
|---|
|  | 476 | const double delta = (double)gridpoints_axis/(end[index] - begin[index]); | 
|---|
|  | 477 | // delta is conversion factor from box length to discrete length, i.e. number of points | 
|---|
|  | 478 | pre_offset[index] = delta*(smaller_wbegin[index] - larger_wbegin[index])+round_offset; | 
|---|
|  | 479 | length[index] = delta*(smaller_wend[index] - smaller_wbegin[index])+round_offset; | 
|---|
|  | 480 | post_offset[index] = delta*(larger_wend[index] - smaller_wend[index])+round_offset; | 
|---|
|  | 481 | total[index] = delta*(larger_wend[index] - larger_wbegin[index])+round_offset; | 
|---|
|  | 482 | } else { | 
|---|
|  | 483 | pre_offset[index] = 0; | 
|---|
|  | 484 | length[index] = 0; | 
|---|
|  | 485 | post_offset[index] = 0; | 
|---|
|  | 486 | total[index] = 0; | 
|---|
|  | 487 | } | 
|---|
|  | 488 | // total is used as safe-guard against loss due to discrete conversion | 
|---|
|  | 489 | ASSERT( pre_offset[index]+post_offset[index]+length[index] == total[index], | 
|---|
|  | 490 | "SamplingGrid::getDiscreteWindowIndices() - pre, post, and length don't sum up to total for " | 
|---|
|  | 491 | +toString(index)+"th component."); | 
|---|
|  | 492 | } | 
|---|
|  | 493 | } | 
|---|
|  | 494 |  | 
|---|
| [fb3485] | 495 | void SamplingGrid::addWindowOntoWindow( | 
|---|
| [313f83] | 496 | const double larger_wbegin[3], | 
|---|
|  | 497 | const double larger_wend[3], | 
|---|
|  | 498 | const double smaller_wbegin[3], | 
|---|
|  | 499 | const double smaller_wend[3], | 
|---|
|  | 500 | sampledvalues_t &dest_sampled_grid, | 
|---|
|  | 501 | const sampledvalues_t &source_sampled_grid, | 
|---|
| [fb3485] | 502 | boost::function<void (double &, const double &)> op, | 
|---|
| [313f83] | 503 | enum eLargerWindow larger_window) | 
|---|
| [1a00bb] | 504 | { | 
|---|
|  | 505 | #ifndef NDEBUG | 
|---|
|  | 506 | for(size_t index=0;index<3;++index) { | 
|---|
| [313f83] | 507 | ASSERT( smaller_wbegin[index] >= larger_wbegin[index], | 
|---|
| [fb3485] | 508 | "SamplingGrid::addWindowOntoWindow() - given smaller window starts earlier than larger window in component " | 
|---|
| [1a00bb] | 509 | +toString(index)+"."); | 
|---|
| [313f83] | 510 | ASSERT( smaller_wend[index] <= larger_wend[index], | 
|---|
| [fb3485] | 511 | "SamplingGrid::addWindowOntoWindow() - given smaller window ends later than larger window in component " | 
|---|
| [1a00bb] | 512 | +toString(index)+"."); | 
|---|
|  | 513 | } | 
|---|
|  | 514 | #endif | 
|---|
|  | 515 | // the only issue are indices | 
|---|
| [de6dfb] | 516 | size_t pre_offset[3]; | 
|---|
|  | 517 | size_t post_offset[3]; | 
|---|
| [1a00bb] | 518 | size_t length[3]; | 
|---|
| [de6dfb] | 519 | size_t total[3]; | 
|---|
| [3f64ee] | 520 | getDiscreteWindowIndices( | 
|---|
|  | 521 | larger_wbegin, larger_wend, | 
|---|
|  | 522 | smaller_wbegin, smaller_wend, | 
|---|
|  | 523 | pre_offset, | 
|---|
|  | 524 | post_offset, | 
|---|
|  | 525 | length, | 
|---|
|  | 526 | total | 
|---|
|  | 527 | ); | 
|---|
| [64bafe0] | 528 | // assert that calculated lengths match with given vector sizes | 
|---|
| [c6355f] | 529 | #ifndef NDEBUG | 
|---|
|  | 530 | const size_t calculated_size = length[0]*length[1]*length[2]; | 
|---|
| [313f83] | 531 | if (larger_window == destwindow) { | 
|---|
|  | 532 | ASSERT( calculated_size == source_sampled_grid.size(), | 
|---|
|  | 533 | "SamplingGrid::addWindowOntoWindow() - not enough source sampled values given: " | 
|---|
|  | 534 | +toString(calculated_size)+" != "+toString(source_sampled_grid.size())+"."); | 
|---|
|  | 535 | ASSERT( calculated_size <= dest_sampled_grid.size(), | 
|---|
|  | 536 | "SamplingGrid::addWindowOntoWindow() - not enough sampled values available: " | 
|---|
|  | 537 | +toString(calculated_size)+" <= "+toString(dest_sampled_grid.size())+"."); | 
|---|
|  | 538 | } else { | 
|---|
|  | 539 | ASSERT( calculated_size == dest_sampled_grid.size(), | 
|---|
|  | 540 | "SamplingGrid::addWindowOntoWindow() - not enough dest sampled values given: " | 
|---|
|  | 541 | +toString(calculated_size)+" != "+toString(dest_sampled_grid.size())+"."); | 
|---|
|  | 542 | ASSERT( calculated_size <= source_sampled_grid.size(), | 
|---|
|  | 543 | "SamplingGrid::addWindowOntoWindow() - not enough source sampled values available: " | 
|---|
|  | 544 | +toString(calculated_size)+" <= "+toString(source_sampled_grid.size())+"."); | 
|---|
|  | 545 | } | 
|---|
| [c6355f] | 546 | const size_t total_size = total[0]*total[1]*total[2]; | 
|---|
| [313f83] | 547 | if (larger_window == destwindow) { | 
|---|
|  | 548 | ASSERT( total_size == dest_sampled_grid.size(), | 
|---|
|  | 549 | "SamplingGrid::addWindowOntoWindow() - total size is not equal to number of present dest points: " | 
|---|
|  | 550 | +toString(total_size)+" != "+toString(dest_sampled_grid.size())+"."); | 
|---|
|  | 551 | } else { | 
|---|
|  | 552 | ASSERT( total_size == source_sampled_grid.size(), | 
|---|
|  | 553 | "SamplingGrid::addWindowOntoWindow() - total size is not equal to number of present source points: " | 
|---|
|  | 554 | +toString(total_size)+" != "+toString(source_sampled_grid.size())+"."); | 
|---|
|  | 555 | } | 
|---|
| [c6355f] | 556 | #endif | 
|---|
| [1a00bb] | 557 | size_t N[3]; | 
|---|
| [64bafe0] | 558 | //  size_t counter = 0; | 
|---|
| [313f83] | 559 | sampledvalues_t::iterator destiter = dest_sampled_grid.begin(); | 
|---|
|  | 560 | sampledvalues_t::const_iterator sourceiter = source_sampled_grid.begin(); | 
|---|
|  | 561 | if (larger_window == destwindow) | 
|---|
|  | 562 | std::advance(destiter, pre_offset[0]*total[1]*total[2]); | 
|---|
| [fb3485] | 563 | else | 
|---|
| [313f83] | 564 | std::advance(sourceiter, pre_offset[0]*total[1]*total[2]); | 
|---|
| [de6dfb] | 565 | for(N[0]=0; N[0] < length[0]; ++N[0]) { | 
|---|
| [313f83] | 566 | if (larger_window == destwindow) | 
|---|
|  | 567 | std::advance(destiter, pre_offset[1]*total[2]); | 
|---|
| [fb3485] | 568 | else | 
|---|
| [313f83] | 569 | std::advance(sourceiter, pre_offset[1]*total[2]); | 
|---|
| [de6dfb] | 570 | for(N[1]=0; N[1] < length[1]; ++N[1]) { | 
|---|
| [313f83] | 571 | if (larger_window == destwindow) | 
|---|
|  | 572 | std::advance(destiter, pre_offset[2]); | 
|---|
| [fb3485] | 573 | else | 
|---|
| [313f83] | 574 | std::advance(sourceiter, pre_offset[2]); | 
|---|
| [de6dfb] | 575 | for(N[2]=0; N[2] < length[2]; ++N[2]) { | 
|---|
| [313f83] | 576 | ASSERT( destiter != dest_sampled_grid.end(), | 
|---|
|  | 577 | "SamplingGrid::addWindowOntoWindow() - destiter is already at end of window."); | 
|---|
|  | 578 | ASSERT( sourceiter != source_sampled_grid.end(), | 
|---|
|  | 579 | "SamplingGrid::addWindowOntoWindow() - destiter is already at end of window."); | 
|---|
|  | 580 | op(*destiter, *sourceiter); | 
|---|
|  | 581 | ++destiter; | 
|---|
|  | 582 | ++sourceiter; | 
|---|
| [1a00bb] | 583 | } | 
|---|
| [313f83] | 584 | if (larger_window == destwindow) | 
|---|
|  | 585 | std::advance(destiter, post_offset[2]); | 
|---|
| [fb3485] | 586 | else | 
|---|
| [313f83] | 587 | std::advance(sourceiter, post_offset[2]); | 
|---|
| [1a00bb] | 588 | } | 
|---|
| [313f83] | 589 | if (larger_window == destwindow) | 
|---|
|  | 590 | std::advance(destiter, post_offset[1]*total[2]); | 
|---|
| [fb3485] | 591 | else | 
|---|
| [313f83] | 592 | std::advance(sourceiter, post_offset[1]*total[2]); | 
|---|
| [1a00bb] | 593 | } | 
|---|
| [c6355f] | 594 | #ifndef NDEBUG | 
|---|
| [313f83] | 595 | if (larger_window == destwindow) | 
|---|
|  | 596 | std::advance(destiter, post_offset[0]*total[1]*total[2]); | 
|---|
| [fb3485] | 597 | else | 
|---|
| [313f83] | 598 | std::advance(sourceiter, post_offset[0]*total[1]*total[2]); | 
|---|
|  | 599 | ASSERT( destiter == dest_sampled_grid.end(), | 
|---|
|  | 600 | "SamplingGrid::addWindowOntoWindow() - destiter is not at end of window."); | 
|---|
|  | 601 | ASSERT( sourceiter == source_sampled_grid.end(), | 
|---|
|  | 602 | "SamplingGrid::addWindowOntoWindow() - sourceiter is not at end of window."); | 
|---|
| [c6355f] | 603 | #endif | 
|---|
| [10f0cb] | 604 | LOG(8, "DEBUG: Grid after adding other is " << dest_sampled_grid << "."); | 
|---|
| [1a00bb] | 605 | } | 
|---|
|  | 606 |  | 
|---|
| [955051] | 607 |  | 
|---|
|  | 608 | bool SamplingGrid::operator==(const SamplingGrid &other) const | 
|---|
|  | 609 | { | 
|---|
|  | 610 | bool status = | 
|---|
|  | 611 | static_cast<const SamplingGridProperties &>(*this) | 
|---|
|  | 612 | == static_cast<const SamplingGridProperties &>(other); | 
|---|
|  | 613 | // compare general properties | 
|---|
|  | 614 | if (status) { | 
|---|
|  | 615 | // compare windows | 
|---|
|  | 616 | for (size_t i=0; i<3; ++i) { | 
|---|
|  | 617 | status &= begin_window[i] == other.begin_window[i]; | 
|---|
|  | 618 | status &= end_window[i] == other.end_window[i]; | 
|---|
|  | 619 | } | 
|---|
|  | 620 | // compare grids | 
|---|
|  | 621 | if (status) | 
|---|
|  | 622 | status &= sampled_grid == other.sampled_grid; | 
|---|
|  | 623 | } | 
|---|
|  | 624 | return status; | 
|---|
|  | 625 | } | 
|---|
|  | 626 |  | 
|---|
| [c889b7] | 627 | std::ostream & operator<<(std::ostream &ost, const SamplingGrid& other) | 
|---|
|  | 628 | { | 
|---|
| [1a00bb] | 629 | ost << "SamplingGrid"; | 
|---|
|  | 630 | ost << " starting at " << other.begin[0] << "," << other.begin[1] << "," << other.begin[2]; | 
|---|
|  | 631 | ost << " ending at " << other.end[0] << "," << other.end[1] << "," << other.end[2]; | 
|---|
|  | 632 | ost << ", window starting at " << other.begin_window[0] << "," << other.begin_window[1] << "," << other.begin_window[2]; | 
|---|
|  | 633 | ost << ", window ending at " << other.end_window[0] << "," << other.end_window[1] << "," << other.end_window[2]; | 
|---|
| [80959a1] | 634 | ost << ", level of " << other.level; | 
|---|
|  | 635 | ost << " and integrated value of " << other.integral(); | 
|---|
| [c889b7] | 636 | return ost; | 
|---|
|  | 637 | } | 
|---|
|  | 638 |  | 
|---|
| [beb16e] | 639 | template<> SamplingGrid ZeroInstance<SamplingGrid>() | 
|---|
|  | 640 | { | 
|---|
|  | 641 | SamplingGrid returnvalue; | 
|---|
|  | 642 | return returnvalue; | 
|---|
|  | 643 | } | 
|---|
|  | 644 |  | 
|---|
| [28c025] | 645 | // we need to explicitly instantiate the serialization functions as | 
|---|
|  | 646 | // its is only serialized through its base class FragmentJob | 
|---|
|  | 647 | BOOST_CLASS_EXPORT_IMPLEMENT(SamplingGrid) | 
|---|