| 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 |
|
|---|
| 42 | #include "Fragmentation/Summation/SetValues/SamplingGrid.hpp"
|
|---|
| 43 |
|
|---|
| 44 | #include <boost/bind.hpp>
|
|---|
| 45 | #include <limits>
|
|---|
| 46 |
|
|---|
| 47 | #include "CodePatterns/Assert.hpp"
|
|---|
| 48 | #include "CodePatterns/Log.hpp"
|
|---|
| 49 |
|
|---|
| 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 |
|
|---|
| 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 |
|
|---|
| 71 | SamplingGrid::SamplingGrid(const double _begin[3],
|
|---|
| 72 | const double _end[3],
|
|---|
| 73 | const int _level,
|
|---|
| 74 | const sampledvalues_t &_sampled_grid) :
|
|---|
| 75 | SamplingGridProperties(_begin, _end, _level),
|
|---|
| 76 | sampled_grid(_sampled_grid)
|
|---|
| 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 | }
|
|---|
| 82 |
|
|---|
| 83 | SamplingGrid::SamplingGrid(const SamplingGrid &_grid) :
|
|---|
| 84 | SamplingGridProperties(_grid),
|
|---|
| 85 | sampled_grid(_grid.sampled_grid)
|
|---|
| 86 | {
|
|---|
| 87 | setWindowSize(_grid.begin_window, _grid.end_window);
|
|---|
| 88 | ASSERT( getWindowGridPoints() == _grid.getWindowGridPoints(),
|
|---|
| 89 | "SamplingGrid::SamplingGrid() - incorrect number of samples given for the window.");
|
|---|
| 90 | }
|
|---|
| 91 |
|
|---|
| 92 | SamplingGrid::SamplingGrid(const SamplingGridProperties &_props) :
|
|---|
| 93 | SamplingGridProperties(_props)
|
|---|
| 94 | {
|
|---|
| 95 | setWindowSize(zeroOffset, zeroOffset);
|
|---|
| 96 | ASSERT( getWindowGridPoints() == (size_t)0,
|
|---|
| 97 | "SamplingGrid::SamplingGrid() - incorrect number of samples given for the window.");
|
|---|
| 98 | }
|
|---|
| 99 |
|
|---|
| 100 | SamplingGrid::SamplingGrid(
|
|---|
| 101 | const SamplingGridProperties &_props,
|
|---|
| 102 | const sampledvalues_t &_sampled_grid) :
|
|---|
| 103 | SamplingGridProperties(_props),
|
|---|
| 104 | sampled_grid(_sampled_grid)
|
|---|
| 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 | }
|
|---|
| 110 |
|
|---|
| 111 | SamplingGrid::~SamplingGrid()
|
|---|
| 112 | {}
|
|---|
| 113 |
|
|---|
| 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 |
|
|---|
| 126 | SamplingGrid& SamplingGrid::operator=(const SamplingGrid& other)
|
|---|
| 127 | {
|
|---|
| 128 | // check for self-assignment
|
|---|
| 129 | if (this != &other) {
|
|---|
| 130 | static_cast<SamplingGridProperties &>(*this) = other;
|
|---|
| 131 | setWindowSize(other.begin_window, other.end_window);
|
|---|
| 132 | sampled_grid = other.sampled_grid;
|
|---|
| 133 | }
|
|---|
| 134 | return *this;
|
|---|
| 135 | }
|
|---|
| 136 |
|
|---|
| 137 | static void multiplyElements(
|
|---|
| 138 | double &dest,
|
|---|
| 139 | const double &source,
|
|---|
| 140 | const double prefactor)
|
|---|
| 141 | {
|
|---|
| 142 | dest *= prefactor*(source);
|
|---|
| 143 | }
|
|---|
| 144 |
|
|---|
| 145 |
|
|---|
| 146 | SamplingGrid& SamplingGrid::operator*=(const SamplingGrid& other)
|
|---|
| 147 | {
|
|---|
| 148 | // check that grids are compatible
|
|---|
| 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);
|
|---|
| 181 | } else {
|
|---|
| 182 | ASSERT(0, "SamplingGrid::operator*=() - multiplying incongruent grids is so far not in the cards.");
|
|---|
| 183 | }
|
|---|
| 184 | return *this;
|
|---|
| 185 | }
|
|---|
| 186 |
|
|---|
| 187 | void SamplingGrid::superposeOtherGrids(const SamplingGrid &other, const double prefactor)
|
|---|
| 188 | {
|
|---|
| 189 | /// check that grids are compatible
|
|---|
| 190 | if (isCompatible(other)) {
|
|---|
| 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 | }
|
|---|
| 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] << ".");
|
|---|
| 211 | if (doExtend)
|
|---|
| 212 | extendWindow(max_begin_window, max_end_window);
|
|---|
| 213 | /// and copy other into larger window, too
|
|---|
| 214 | addOntoWindow(other.begin_window, other.end_window, other.sampled_grid, prefactor);
|
|---|
| 215 | } else {
|
|---|
| 216 | ASSERT(0, "SamplingGrid::superposeOtherGrids() - superposing incompatible grids is so far not in the cards.");
|
|---|
| 217 | }
|
|---|
| 218 | }
|
|---|
| 219 |
|
|---|
| 220 | const size_t SamplingGrid::getWindowGridPointsPerAxis(const size_t axis) const
|
|---|
| 221 | {
|
|---|
| 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)
|
|---|
| 231 | return 0;
|
|---|
| 232 | return (size_t)(length/delta+round_offset);
|
|---|
| 233 | }
|
|---|
| 234 |
|
|---|
| 235 | double SamplingGrid::integral() const
|
|---|
| 236 | {
|
|---|
| 237 | const double volume_element = getVolume()/(double)getTotalGridPoints();
|
|---|
| 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 |
|
|---|
| 248 | double SamplingGrid::integral(const SamplingGrid &weight) const
|
|---|
| 249 | {
|
|---|
| 250 | if (isCompatible(weight)) {
|
|---|
| 251 | const double volume_element = getVolume()/(double)getTotalGridPoints();
|
|---|
| 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 |
|
|---|
| 264 | double SamplingGrid::getNearestLowerGridPoint(
|
|---|
| 265 | const double value, const size_t axis) const
|
|---|
| 266 | {
|
|---|
| 267 | const double length = end[axis] - begin[axis];
|
|---|
| 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());
|
|---|
| 281 | }
|
|---|
| 282 |
|
|---|
| 283 | double SamplingGrid::getNearestHigherGridPoint(
|
|---|
| 284 | const double value, const size_t axis) const
|
|---|
| 285 | {
|
|---|
| 286 | const double length = end[axis] - begin[axis];
|
|---|
| 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());
|
|---|
| 300 | }
|
|---|
| 301 |
|
|---|
| 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) {
|
|---|
| 307 | begin_window[index] = getNearestLowerGridPoint(_begin_window[index], index);
|
|---|
| 308 | ASSERT( begin_window[index] >= begin[index],
|
|---|
| 309 | "SamplingGrid::setWindowSize() - window starts earlier than domain on "
|
|---|
| 310 | +toString(index)+"th component.");
|
|---|
| 311 | end_window[index] = getNearestHigherGridPoint(_end_window[index], index);
|
|---|
| 312 | ASSERT( end_window[index] <= end[index],
|
|---|
| 313 | "SamplingGrid::setWindowSize() - window ends later than domain on "
|
|---|
| 314 | +toString(index)+"th component.");
|
|---|
| 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();
|
|---|
| 324 | sampled_grid.clear();
|
|---|
| 325 | sampled_grid.resize(gridpoints_window, 0.);
|
|---|
| 326 | }
|
|---|
| 327 |
|
|---|
| 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 |
|
|---|
| 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) {
|
|---|
| 344 | // check that we truly have to extend the window
|
|---|
| 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.");
|
|---|
| 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.");
|
|---|
| 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);
|
|---|
| 369 | // set new window
|
|---|
| 370 | setWindow(_begin_window,_end_window);
|
|---|
| 371 | // now extend it ...
|
|---|
| 372 | addOntoWindow(old_begin_window, old_end_window, old_values, +1.);
|
|---|
| 373 | LOG(2, "DEBUG: Grid after extension is " << sampled_grid << ".");
|
|---|
| 374 | }
|
|---|
| 375 |
|
|---|
| 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 |
|
|---|
| 414 | static void addElements(
|
|---|
| 415 | double &dest,
|
|---|
| 416 | const double &source,
|
|---|
| 417 | const double prefactor)
|
|---|
| 418 | {
|
|---|
| 419 | dest += prefactor*(source);
|
|---|
| 420 | }
|
|---|
| 421 |
|
|---|
| 422 | void SamplingGrid::addOntoWindow(
|
|---|
| 423 | const double _begin_window[3],
|
|---|
| 424 | const double _end_window[3],
|
|---|
| 425 | const sampledvalues_t &_sampled_grid,
|
|---|
| 426 | const double prefactor)
|
|---|
| 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)),
|
|---|
| 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);
|
|---|
| 454 | }
|
|---|
| 455 |
|
|---|
| 456 | void SamplingGrid::addWindowOntoWindow(
|
|---|
| 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,
|
|---|
| 463 | boost::function<void (double &, const double &)> op,
|
|---|
| 464 | enum eLargerWindow larger_window)
|
|---|
| 465 | {
|
|---|
| 466 | #ifndef NDEBUG
|
|---|
| 467 | for(size_t index=0;index<3;++index) {
|
|---|
| 468 | ASSERT( smaller_wbegin[index] >= larger_wbegin[index],
|
|---|
| 469 | "SamplingGrid::addWindowOntoWindow() - given smaller window starts earlier than larger window in component "
|
|---|
| 470 | +toString(index)+".");
|
|---|
| 471 | ASSERT( smaller_wend[index] <= larger_wend[index],
|
|---|
| 472 | "SamplingGrid::addWindowOntoWindow() - given smaller window ends later than larger window in component "
|
|---|
| 473 | +toString(index)+".");
|
|---|
| 474 | }
|
|---|
| 475 | #endif
|
|---|
| 476 | // the only issue are indices
|
|---|
| 477 | const size_t gridpoints_axis = getGridPointsPerAxis();
|
|---|
| 478 | size_t pre_offset[3];
|
|---|
| 479 | size_t post_offset[3];
|
|---|
| 480 | size_t length[3];
|
|---|
| 481 | size_t total[3];
|
|---|
| 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
|
|---|
| 485 | for(size_t index=0;index<3;++index) {
|
|---|
| 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
|
|---|
| 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;
|
|---|
| 495 | // total is used as safe-guard against loss due to discrete conversion
|
|---|
| 496 | ASSERT( pre_offset[index]+post_offset[index]+length[index] == total[index],
|
|---|
| 497 | "SamplingGrid::addWindowOntoWindow() - pre, post, and length don't sum up to total for "
|
|---|
| 498 | +toString(index)+"th component.");
|
|---|
| 499 | }
|
|---|
| 500 | // assert that calculated lengths match with given vector sizes
|
|---|
| 501 | #ifndef NDEBUG
|
|---|
| 502 | const size_t calculated_size = length[0]*length[1]*length[2];
|
|---|
| 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 | }
|
|---|
| 518 | const size_t total_size = total[0]*total[1]*total[2];
|
|---|
| 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 | }
|
|---|
| 528 | #endif
|
|---|
| 529 | size_t N[3];
|
|---|
| 530 | // size_t counter = 0;
|
|---|
| 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]);
|
|---|
| 535 | else
|
|---|
| 536 | std::advance(sourceiter, pre_offset[0]*total[1]*total[2]);
|
|---|
| 537 | for(N[0]=0; N[0] < length[0]; ++N[0]) {
|
|---|
| 538 | if (larger_window == destwindow)
|
|---|
| 539 | std::advance(destiter, pre_offset[1]*total[2]);
|
|---|
| 540 | else
|
|---|
| 541 | std::advance(sourceiter, pre_offset[1]*total[2]);
|
|---|
| 542 | for(N[1]=0; N[1] < length[1]; ++N[1]) {
|
|---|
| 543 | if (larger_window == destwindow)
|
|---|
| 544 | std::advance(destiter, pre_offset[2]);
|
|---|
| 545 | else
|
|---|
| 546 | std::advance(sourceiter, pre_offset[2]);
|
|---|
| 547 | for(N[2]=0; N[2] < length[2]; ++N[2]) {
|
|---|
| 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;
|
|---|
| 555 | }
|
|---|
| 556 | if (larger_window == destwindow)
|
|---|
| 557 | std::advance(destiter, post_offset[2]);
|
|---|
| 558 | else
|
|---|
| 559 | std::advance(sourceiter, post_offset[2]);
|
|---|
| 560 | }
|
|---|
| 561 | if (larger_window == destwindow)
|
|---|
| 562 | std::advance(destiter, post_offset[1]*total[2]);
|
|---|
| 563 | else
|
|---|
| 564 | std::advance(sourceiter, post_offset[1]*total[2]);
|
|---|
| 565 | }
|
|---|
| 566 | #ifndef NDEBUG
|
|---|
| 567 | if (larger_window == destwindow)
|
|---|
| 568 | std::advance(destiter, post_offset[0]*total[1]*total[2]);
|
|---|
| 569 | else
|
|---|
| 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.");
|
|---|
| 575 | #endif
|
|---|
| 576 | LOG(2, "DEBUG: Grid after adding other is " << dest_sampled_grid << ".");
|
|---|
| 577 | }
|
|---|
| 578 |
|
|---|
| 579 | std::ostream & operator<<(std::ostream &ost, const SamplingGrid& other)
|
|---|
| 580 | {
|
|---|
| 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];
|
|---|
| 586 | ost << ", level of " << other.level;
|
|---|
| 587 | ost << " and integrated value of " << other.integral();
|
|---|
| 588 | return ost;
|
|---|
| 589 | }
|
|---|
| 590 |
|
|---|
| 591 | template<> SamplingGrid ZeroInstance<SamplingGrid>()
|
|---|
| 592 | {
|
|---|
| 593 | SamplingGrid returnvalue;
|
|---|
| 594 | return returnvalue;
|
|---|
| 595 | }
|
|---|
| 596 |
|
|---|
| 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)
|
|---|