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