source: src/Fragmentation/Summation/SetValues/SamplingGrid.cpp@ ca4b372

Action_Thermostats Add_AtomRandomPerturbation Add_FitFragmentPartialChargesAction Add_RotateAroundBondAction Add_SelectAtomByNameAction Adding_Graph_to_ChangeBondActions Adding_MD_integration_tests Adding_StructOpt_integration_tests Automaking_mpqc_open AutomationFragmentation_failures Candidate_v1.5.4 Candidate_v1.6.0 Candidate_v1.6.1 Candidate_v1.7.0 ChangeBugEmailaddress ChangingTestPorts ChemicalSpaceEvaluator Combining_Subpackages Debian_Package_split Debian_package_split_molecuildergui_only Disabling_MemDebug Docu_Python_wait EmpiricalPotential_contain_HomologyGraph EmpiricalPotential_contain_HomologyGraph_documentation Enable_parallel_make_install Enhance_userguide Enhanced_StructuralOptimization Enhanced_StructuralOptimization_continued Example_ManyWaysToTranslateAtom Exclude_Hydrogens_annealWithBondGraph FitPartialCharges_GlobalError Fix_ChargeSampling_PBC Fix_ChronosMutex Fix_FitPartialCharges Fix_FitPotential_needs_atomicnumbers Fix_ForceAnnealing Fix_IndependentFragmentGrids Fix_ParseParticles Fix_ParseParticles_split_forward_backward_Actions Fix_StatusMsg Fix_StepWorldTime_single_argument Fix_Verbose_Codepatterns ForceAnnealing_goodresults ForceAnnealing_oldresults ForceAnnealing_tocheck ForceAnnealing_with_BondGraph ForceAnnealing_with_BondGraph_continued ForceAnnealing_with_BondGraph_continued_betteresults ForceAnnealing_with_BondGraph_contraction-expansion GeometryObjects Gui_displays_atomic_force_velocity IndependentFragmentGrids IndependentFragmentGrids_IndividualZeroInstances IndependentFragmentGrids_IntegrationTest IndependentFragmentGrids_Sole_NN_Calculation JobMarket_RobustOnKillsSegFaults JobMarket_StableWorkerPool JobMarket_unresolvable_hostname_fix ODR_violation_mpqc_open PartialCharges_OrthogonalSummation PythonUI_with_named_parameters QtGui_reactivate_TimeChanged_changes Recreated_GuiChecks RotateToPrincipalAxisSystem_UndoRedo StoppableMakroAction Subpackage_CodePatterns Subpackage_JobMarket Subpackage_LinearAlgebra Subpackage_levmar Subpackage_mpqc_open Subpackage_vmg ThirdParty_MPQC_rebuilt_buildsystem TrajectoryDependenant_MaxOrder TremoloParser_IncreasedPrecision TremoloParser_MultipleTimesteps Ubuntu_1604_changes stable
Last change on this file since ca4b372 was 336da8, checked in by Frederik Heber <heber@…>, 9 years ago

WindowGrid_converter's refactored a bit with getDiscreteWindowOffsets().

  • Added SamplingGrid::getDiscreteWindowOffsets().
  • WindowGrid_converter now also use NDIM instead of 3 to clarify the use of the spatial degrees of freedom (and not some grid level or else).
  • Property mode set to 100644
File size: 24.7 KB
Line 
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 <algorithm>
47#include <limits>
48
49#include "CodePatterns/Assert.hpp"
50#include "CodePatterns/Log.hpp"
51
52// static instances
53const double SamplingGrid::zeroOffset[NDIM] = { 0., 0., 0. };
54
55SamplingGrid::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
63SamplingGrid::SamplingGrid(const double _begin[NDIM],
64 const double _end[NDIM],
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
73SamplingGrid::SamplingGrid(const double _begin[NDIM],
74 const double _end[NDIM],
75 const int _level,
76 const sampledvalues_t &_sampled_grid) :
77 SamplingGridProperties(_begin, _end, _level),
78 sampled_grid(_sampled_grid)
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}
84
85SamplingGrid::SamplingGrid(const SamplingGrid &_grid) :
86 SamplingGridProperties(_grid),
87 sampled_grid(_grid.sampled_grid)
88{
89 setWindowSize(_grid.begin_window, _grid.end_window);
90 ASSERT( getWindowGridPoints() == _grid.getWindowGridPoints(),
91 "SamplingGrid::SamplingGrid() - incorrect number of samples given for the window.");
92}
93
94SamplingGrid::SamplingGrid(const SamplingGridProperties &_props) :
95 SamplingGridProperties(_props)
96{
97 setWindowSize(zeroOffset, zeroOffset);
98 ASSERT( getWindowGridPoints() == (size_t)0,
99 "SamplingGrid::SamplingGrid() - incorrect number of samples given for the window.");
100}
101
102SamplingGrid::SamplingGrid(
103 const SamplingGridProperties &_props,
104 const sampledvalues_t &_sampled_grid) :
105 SamplingGridProperties(_props),
106 sampled_grid(_sampled_grid)
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}
112
113SamplingGrid::~SamplingGrid()
114{}
115
116bool 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<NDIM; ++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
128SamplingGrid& SamplingGrid::operator=(const SamplingGrid& other)
129{
130 // check for self-assignment
131 if (this != &other) {
132 static_cast<SamplingGridProperties &>(*this) = other;
133 setWindowSize(other.begin_window, other.end_window);
134 sampled_grid = other.sampled_grid;
135 }
136 return *this;
137}
138
139static void multiplyElements(
140 double &dest,
141 const double &source,
142 const double prefactor)
143{
144 dest *= prefactor*(source);
145}
146
147SamplingGrid& 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}
156
157SamplingGrid& SamplingGrid::operator*=(const SamplingGrid& other)
158{
159 // check that grids are compatible
160 if (isCompatible(other)) {
161 /// get minimum of window
162 double min_begin_window[NDIM];
163 double min_end_window[NDIM];
164 bool doShrink = false;
165 for (size_t index=0; index<NDIM;++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 }
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] << ".");
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);
192 } else {
193 ASSERT(0, "SamplingGrid::operator*=() - multiplying incongruent grids is so far not in the cards.");
194 }
195 return *this;
196}
197
198void SamplingGrid::superposeOtherGrids(const SamplingGrid &other, const double prefactor)
199{
200 /// check that grids are compatible
201 if (isCompatible(other)) {
202 /// get maximum of window
203 double max_begin_window[NDIM];
204 double max_end_window[NDIM];
205 bool doExtend = false;
206 for (size_t index=0; index<NDIM;++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 }
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] << ".");
222 if (doExtend)
223 extendWindow(max_begin_window, max_end_window);
224 /// and copy other into larger window, too
225 addOntoWindow(other.begin_window, other.end_window, other.sampled_grid, prefactor);
226 } else {
227 ASSERT(0, "SamplingGrid::superposeOtherGrids() - superposing incompatible grids is so far not in the cards.");
228 }
229}
230
231const size_t SamplingGrid::getWindowGridPointsPerAxis(const size_t axis) const
232{
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)
242 return 0;
243 return (size_t)(length/delta+round_offset);
244}
245
246double SamplingGrid::integral() const
247{
248 const double volume_element = getVolume()/(double)getTotalGridPoints();
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
259double SamplingGrid::integral(const SamplingGrid &weight) const
260{
261 if (isCompatible(weight)) {
262 const double volume_element = getVolume()/(double)getTotalGridPoints();
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}
274void SamplingGrid::setWindowSize(
275 const double _begin_window[NDIM],
276 const double _end_window[NDIM])
277{
278 for (size_t index=0;index<NDIM;++index) {
279 begin_window[index] = getNearestLowerGridPoint(_begin_window[index], index);
280 ASSERT( begin_window[index] >= begin[index],
281 "SamplingGrid::setWindowSize() - window starts earlier than domain on "
282 +toString(index)+"th component.");
283 end_window[index] = getNearestHigherGridPoint(_end_window[index], index);
284 ASSERT( end_window[index] <= end[index],
285 "SamplingGrid::setWindowSize() - window ends later than domain on "
286 +toString(index)+"th component.");
287 }
288}
289
290void SamplingGrid::setWindow(
291 const double _begin_window[NDIM],
292 const double _end_window[NDIM])
293{
294 setWindowSize(_begin_window, _end_window);
295 const size_t gridpoints_window = getWindowGridPoints();
296 sampled_grid.clear();
297 sampled_grid.resize(gridpoints_window, 0.);
298}
299
300void SamplingGrid::setDomain(
301 const double _begin[NDIM],
302 const double _end[NDIM])
303{
304 setDomainSize(_begin, _end);
305 setWindowSize(_begin, _end);
306 const size_t gridpoints = getTotalGridPoints();
307 sampled_grid.resize(gridpoints, 0.);
308}
309
310void SamplingGrid::extendWindow(
311 const double _begin_window[NDIM],
312 const double _end_window[NDIM])
313{
314#ifndef NDEBUG
315 for(size_t index=0;index < NDIM; ++index) {
316 // check that we truly have to extend the window
317 ASSERT ( begin_window[index] >= _begin_window[index],
318 "SamplingGrid::extendWindow() - component "+toString(index)+
319 " of window start is greater than old value.");
320 ASSERT ( end_window[index] <= _end_window[index],
321 "SamplingGrid::extendWindow() - component "+toString(index)+
322 " of window end is less than old value.");
323
324 // check that we are still less than domain
325 ASSERT ( _begin_window[index] >= begin[index],
326 "SamplingGrid::extendWindow() - component "+toString(index)+
327 " of window start is less than domain start.");
328 ASSERT ( _end_window[index] <= end[index],
329 "SamplingGrid::extendWindow() - component "+toString(index)+
330 " of window end is greater than domain end.");
331 }
332#endif
333 // copy old window size and values
334 double old_begin_window[NDIM];
335 double old_end_window[NDIM];
336 for(size_t index=0;index<NDIM;++index) {
337 old_begin_window[index] = begin_window[index];
338 old_end_window[index] = end_window[index];
339 }
340 sampledvalues_t old_values(sampled_grid);
341 // set new window
342 setWindow(_begin_window,_end_window);
343 // now extend it ...
344 addOntoWindow(old_begin_window, old_end_window, old_values, +1.);
345 LOG(6, "DEBUG: Grid after extension is " << sampled_grid << ".");
346}
347
348void SamplingGrid::shrinkWindow(
349 const double _begin_window[NDIM],
350 const double _end_window[NDIM])
351{
352#ifndef NDEBUG
353 for(size_t index=0;index < NDIM; ++index) {
354 // check that we truly have to shrink the window
355 ASSERT ( begin_window[index] <= _begin_window[index],
356 "SamplingGrid::shrinkWindow() - component "+toString(index)+
357 " of window start is less than old value.");
358 ASSERT ( end_window[index] >= _end_window[index],
359 "SamplingGrid::shrinkWindow() - component "+toString(index)+
360 " of window end is greater than old value.");
361
362 // check that we are still less than domain
363 ASSERT ( _begin_window[index] >= begin[index],
364 "SamplingGrid::shrinkWindow() - component "+toString(index)+
365 " of window start is less than domain start.");
366 ASSERT ( _end_window[index] <= end[index],
367 "SamplingGrid::shrinkWindow() - component "+toString(index)+
368 " of window end is greater than domain end.");
369 }
370#endif
371 // copy old window size and values
372 double old_begin_window[NDIM];
373 double old_end_window[NDIM];
374 for(size_t index=0;index<NDIM;++index) {
375 old_begin_window[index] = begin_window[index];
376 old_end_window[index] = end_window[index];
377 }
378 sampledvalues_t old_values(sampled_grid);
379 // set new window
380 setWindow(_begin_window,_end_window);
381 // now extend it ...
382 addIntoWindow(old_begin_window, old_end_window, old_values, +1.);
383 LOG(6, "DEBUG: Grid after extension is " << sampled_grid << ".");
384}
385
386static void addElements(
387 double &dest,
388 const double &source,
389 const double prefactor)
390{
391 dest += prefactor*(source);
392}
393
394void SamplingGrid::addOntoWindow(
395 const double _begin_window[NDIM],
396 const double _end_window[NDIM],
397 const sampledvalues_t &_sampled_grid,
398 const double prefactor)
399{
400 addWindowOntoWindow(
401 begin_window,
402 end_window,
403 _begin_window,
404 _end_window,
405 sampled_grid,
406 _sampled_grid,
407 boost::bind(addElements, _1, _2, boost::cref(prefactor)),
408 destwindow);
409}
410
411void SamplingGrid::addIntoWindow(
412 const double _begin_window[NDIM],
413 const double _end_window[NDIM],
414 const sampledvalues_t &_sampled_grid,
415 const double prefactor)
416{
417 addWindowOntoWindow(
418 _begin_window,
419 _end_window,
420 begin_window,
421 end_window,
422 sampled_grid,
423 _sampled_grid,
424 boost::bind(addElements, _1, _2, boost::cref(prefactor)),
425 sourcewindow);
426}
427
428void SamplingGrid::getDiscreteWindowIndices(
429 size_t _wbegin[NDIM],
430 size_t _wlength[NDIM],
431 size_t _wend[NDIM]) const
432{
433 const double round_offset =
434 (std::numeric_limits<size_t>::round_style == std::round_toward_zero) ?
435 0.5 : 0.; // need offset to get to round_toward_nearest behavior
436 for(size_t index=0;index<NDIM;++index) {
437 if (fabs(end[index] - begin[index]) > std::numeric_limits<double>::epsilon()*1e4) {
438 // we refrain from using floor/ceil as the window's starts and ends,
439 // the grids have to be compatible (equal level), should always be on
440 // discrete grid point locations.
441 const double delta = getDeltaPerAxis(index);
442 // delta is conversion factor from box length to discrete length, i.e. number of points
443 _wbegin[index] = (begin_window[index] - begin[index])/delta+round_offset;
444 _wlength[index] = (end_window[index] - begin_window[index])/delta+round_offset;
445 _wend[index] = (end_window[index] - begin[index])/delta+round_offset;
446 } else {
447 _wbegin[index] = 0;
448 _wlength[index] = 0;
449 _wend[index] = 0;
450 }
451 // total is used as safe-guard against loss due to discrete conversion
452 ASSERT( (_wend[index] - _wbegin[index]) == _wlength[index],
453 "SamplingGrid::getDiscreteWindowCopyIndices() - end - begin is not equal to length for "
454 +toString(index)+"th component.");
455 }
456}
457
458void SamplingGrid::getDiscreteWindowOffsets(
459 size_t _pre_offset[NDIM],
460 size_t _post_offset[NDIM],
461 size_t _length[NDIM],
462 size_t _total[NDIM]) const
463{
464 const double round_offset =
465 (std::numeric_limits<size_t>::round_style == std::round_toward_zero) ?
466 0.5 : 0.; // need offset to get to round_toward_nearest behavior
467 for(size_t index=0;index<NDIM;++index) {
468 if (fabs(end[index] - begin[index]) > std::numeric_limits<double>::epsilon()*1e4) {
469 // we refrain from using floor/ceil as the window's starts and ends,
470 // the grids have to be compatible (equal level), should always be on
471 // discrete grid point locations.
472 const double delta = getDeltaPerAxis(index);
473 // delta is conversion factor from box length to discrete length, i.e. number of points
474 _pre_offset[index] = (begin_window[index] - begin[index])/delta+round_offset;
475 _post_offset[index] = (end[index] - end_window[index])/delta+round_offset;
476 _length[index] = (end_window[index] - begin_window[index])/delta+round_offset;
477 _total[index] = (end[index] - begin[index])/delta+round_offset;
478 } else {
479 _pre_offset[index] = 0;
480 _post_offset[index] = 0;
481 _length[index] = 0;
482 _total[index] = 0;
483 }
484 // total is used as safe-guard against loss due to discrete conversion
485 ASSERT( (_pre_offset[index] + _post_offset[index]) + _length[index] == _total[index],
486 "SamplingGrid::getDiscreteWindowCopyIndices() - pre, length, post are not equal to total for "
487 +toString(index)+"th component.");
488 }
489}
490
491void SamplingGrid::getDiscreteWindowCopyIndices(
492 const double *larger_wbegin,
493 const double *larger_wend,
494 const double *smaller_wbegin,
495 const double *smaller_wend,
496 size_t *pre_offset,
497 size_t *post_offset,
498 size_t *length,
499 size_t *total) const
500{
501 const double round_offset =
502 (std::numeric_limits<size_t>::round_style == std::round_toward_zero) ?
503 0.5 : 0.; // need offset to get to round_toward_nearest behavior
504 for(size_t index=0;index<NDIM;++index) {
505 if (fabs(end[index] - begin[index]) > std::numeric_limits<double>::epsilon()*1e4) {
506 // we refrain from using floor/ceil as the window's starts and ends,
507 // the grids have to be compatible (equal level), should always be on
508 // discrete grid point locations.
509 const double delta = getDeltaPerAxis(index);
510 // delta is conversion factor from box length to discrete length, i.e. number of points
511 pre_offset[index] = (smaller_wbegin[index] - larger_wbegin[index])/delta+round_offset;
512 length[index] = (smaller_wend[index] - smaller_wbegin[index])/delta+round_offset;
513 post_offset[index] = (larger_wend[index] - smaller_wend[index])/delta+round_offset;
514 total[index] = (larger_wend[index] - larger_wbegin[index])/delta+round_offset;
515 } else {
516 pre_offset[index] = 0;
517 length[index] = 0;
518 post_offset[index] = 0;
519 total[index] = 0;
520 }
521 // total is used as safe-guard against loss due to discrete conversion
522 ASSERT( pre_offset[index]+post_offset[index]+length[index] == total[index],
523 "SamplingGrid::getDiscreteWindowCopyIndices() - pre, post, and length don't sum up to total for "
524 +toString(index)+"th component.");
525 }
526}
527
528void SamplingGrid::addWindowOntoWindow(
529 const double larger_wbegin[NDIM],
530 const double larger_wend[NDIM],
531 const double smaller_wbegin[NDIM],
532 const double smaller_wend[NDIM],
533 sampledvalues_t &dest_sampled_grid,
534 const sampledvalues_t &source_sampled_grid,
535 boost::function<void (double &, const double &)> op,
536 enum eLargerWindow larger_window)
537{
538#ifndef NDEBUG
539 for(size_t index=0;index<NDIM;++index) {
540 ASSERT( smaller_wbegin[index] >= larger_wbegin[index],
541 "SamplingGrid::addWindowOntoWindow() - given smaller window starts earlier than larger window in component "
542 +toString(index)+".");
543 ASSERT( smaller_wend[index] <= larger_wend[index],
544 "SamplingGrid::addWindowOntoWindow() - given smaller window ends later than larger window in component "
545 +toString(index)+".");
546 }
547#endif
548 // the only issue are indices
549 size_t pre_offset[NDIM];
550 size_t post_offset[NDIM];
551 size_t length[NDIM];
552 size_t total[NDIM];
553 getDiscreteWindowCopyIndices(
554 larger_wbegin, larger_wend,
555 smaller_wbegin, smaller_wend,
556 pre_offset,
557 post_offset,
558 length,
559 total
560 );
561 // assert that calculated lengths match with given vector sizes
562#ifndef NDEBUG
563 const size_t calculated_size = length[0]*length[1]*length[2];
564 if (larger_window == destwindow) {
565 ASSERT( calculated_size == source_sampled_grid.size(),
566 "SamplingGrid::addWindowOntoWindow() - not enough source sampled values given: "
567 +toString(calculated_size)+" != "+toString(source_sampled_grid.size())+".");
568 ASSERT( calculated_size <= dest_sampled_grid.size(),
569 "SamplingGrid::addWindowOntoWindow() - not enough sampled values available: "
570 +toString(calculated_size)+" <= "+toString(dest_sampled_grid.size())+".");
571 } else {
572 ASSERT( calculated_size == dest_sampled_grid.size(),
573 "SamplingGrid::addWindowOntoWindow() - not enough dest sampled values given: "
574 +toString(calculated_size)+" != "+toString(dest_sampled_grid.size())+".");
575 ASSERT( calculated_size <= source_sampled_grid.size(),
576 "SamplingGrid::addWindowOntoWindow() - not enough source sampled values available: "
577 +toString(calculated_size)+" <= "+toString(source_sampled_grid.size())+".");
578 }
579 const size_t total_size = total[0]*total[1]*total[2];
580 if (larger_window == destwindow) {
581 ASSERT( total_size == dest_sampled_grid.size(),
582 "SamplingGrid::addWindowOntoWindow() - total size is not equal to number of present dest points: "
583 +toString(total_size)+" != "+toString(dest_sampled_grid.size())+".");
584 } else {
585 ASSERT( total_size == source_sampled_grid.size(),
586 "SamplingGrid::addWindowOntoWindow() - total size is not equal to number of present source points: "
587 +toString(total_size)+" != "+toString(source_sampled_grid.size())+".");
588 }
589#endif
590 size_t N[NDIM];
591// size_t counter = 0;
592 sampledvalues_t::iterator destiter = dest_sampled_grid.begin();
593 sampledvalues_t::const_iterator sourceiter = source_sampled_grid.begin();
594 if (larger_window == destwindow)
595 std::advance(destiter, pre_offset[0]*total[1]*total[2]);
596 else
597 std::advance(sourceiter, pre_offset[0]*total[1]*total[2]);
598 for(N[0]=0; N[0] < length[0]; ++N[0]) {
599 if (larger_window == destwindow)
600 std::advance(destiter, pre_offset[1]*total[2]);
601 else
602 std::advance(sourceiter, pre_offset[1]*total[2]);
603 for(N[1]=0; N[1] < length[1]; ++N[1]) {
604 if (larger_window == destwindow)
605 std::advance(destiter, pre_offset[2]);
606 else
607 std::advance(sourceiter, pre_offset[2]);
608 for(N[2]=0; N[2] < length[2]; ++N[2]) {
609 ASSERT( destiter != dest_sampled_grid.end(),
610 "SamplingGrid::addWindowOntoWindow() - destiter is already at end of window.");
611 ASSERT( sourceiter != source_sampled_grid.end(),
612 "SamplingGrid::addWindowOntoWindow() - destiter is already at end of window.");
613 op(*destiter, *sourceiter);
614 ++destiter;
615 ++sourceiter;
616 }
617 if (larger_window == destwindow)
618 std::advance(destiter, post_offset[2]);
619 else
620 std::advance(sourceiter, post_offset[2]);
621 }
622 if (larger_window == destwindow)
623 std::advance(destiter, post_offset[1]*total[2]);
624 else
625 std::advance(sourceiter, post_offset[1]*total[2]);
626 }
627#ifndef NDEBUG
628 if (larger_window == destwindow)
629 std::advance(destiter, post_offset[0]*total[1]*total[2]);
630 else
631 std::advance(sourceiter, post_offset[0]*total[1]*total[2]);
632 ASSERT( destiter == dest_sampled_grid.end(),
633 "SamplingGrid::addWindowOntoWindow() - destiter is not at end of window.");
634 ASSERT( sourceiter == source_sampled_grid.end(),
635 "SamplingGrid::addWindowOntoWindow() - sourceiter is not at end of window.");
636#endif
637 LOG(8, "DEBUG: Grid after adding other is " << dest_sampled_grid << ".");
638}
639
640
641bool SamplingGrid::operator==(const SamplingGrid &other) const
642{
643 bool status =
644 static_cast<const SamplingGridProperties &>(*this)
645 == static_cast<const SamplingGridProperties &>(other);
646 // compare general properties
647 if (status) {
648 // compare windows
649 for (size_t i=0; i<NDIM; ++i) {
650 status &= begin_window[i] == other.begin_window[i];
651 status &= end_window[i] == other.end_window[i];
652 }
653 // compare grids
654 if (status)
655 status &= sampled_grid == other.sampled_grid;
656 }
657 return status;
658}
659
660std::ostream & operator<<(std::ostream &ost, const SamplingGrid& other)
661{
662 ost << "SamplingGrid";
663 ost << " starting at " << other.begin[0] << "," << other.begin[1] << "," << other.begin[2];
664 ost << " ending at " << other.end[0] << "," << other.end[1] << "," << other.end[2];
665 ost << ", window starting at " << other.begin_window[0] << "," << other.begin_window[1] << "," << other.begin_window[2];
666 ost << ", window ending at " << other.end_window[0] << "," << other.end_window[1] << "," << other.end_window[2];
667 ost << ", level of " << other.level;
668 ost << " and integrated value of " << other.integral();
669 return ost;
670}
671
672template<> SamplingGrid ZeroInstance<SamplingGrid>()
673{
674 SamplingGrid returnvalue;
675 return returnvalue;
676}
677
678// we need to explicitly instantiate the serialization functions as
679// its is only serialized through its base class FragmentJob
680BOOST_CLASS_EXPORT_IMPLEMENT(SamplingGrid)
Note: See TracBrowser for help on using the repository browser.