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