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