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 "Jobs/Grid/SamplingGrid.hpp"
|
---|
43 |
|
---|
44 | #include <limits>
|
---|
45 |
|
---|
46 | #include "CodePatterns/Assert.hpp"
|
---|
47 | #include "CodePatterns/Log.hpp"
|
---|
48 |
|
---|
49 | // static instances
|
---|
50 | const double SamplingGrid::zeroOffset[3] = { 0., 0., 0. };
|
---|
51 |
|
---|
52 | SamplingGrid::SamplingGrid() :
|
---|
53 | SamplingGridProperties()
|
---|
54 | {
|
---|
55 | setWindowSize(zeroOffset, zeroOffset);
|
---|
56 | ASSERT( getWindowGridPoints() == (size_t)0,
|
---|
57 | "SamplingGrid::SamplingGrid() - incorrect number of samples given for the window.");
|
---|
58 | }
|
---|
59 |
|
---|
60 | SamplingGrid::SamplingGrid(const double _begin[3],
|
---|
61 | const double _end[3],
|
---|
62 | const int _level) :
|
---|
63 | SamplingGridProperties(_begin, _end, _level)
|
---|
64 | {
|
---|
65 | setWindowSize(zeroOffset, zeroOffset);
|
---|
66 | ASSERT( getWindowGridPoints() == (size_t)0,
|
---|
67 | "SamplingGrid::SamplingGrid() - incorrect number of samples given for the window.");
|
---|
68 | }
|
---|
69 |
|
---|
70 | SamplingGrid::SamplingGrid(const double _begin[3],
|
---|
71 | const double _end[3],
|
---|
72 | const int _level,
|
---|
73 | const sampledvalues_t &_sampled_grid) :
|
---|
74 | SamplingGridProperties(_begin, _end, _level),
|
---|
75 | sampled_grid(_sampled_grid)
|
---|
76 | {
|
---|
77 | setWindowSize(_begin, _end);
|
---|
78 | ASSERT( getWindowGridPoints() == (size_t)_sampled_grid.size(),
|
---|
79 | "SamplingGrid::SamplingGrid() - incorrect number of samples given for the window.");
|
---|
80 | }
|
---|
81 |
|
---|
82 | SamplingGrid::SamplingGrid(const SamplingGrid &_grid) :
|
---|
83 | SamplingGridProperties(_grid),
|
---|
84 | sampled_grid(_grid.sampled_grid)
|
---|
85 | {
|
---|
86 | setWindowSize(_grid.begin_window, _grid.end_window);
|
---|
87 | ASSERT( getWindowGridPoints() == _grid.getWindowGridPoints(),
|
---|
88 | "SamplingGrid::SamplingGrid() - incorrect number of samples given for the window.");
|
---|
89 | }
|
---|
90 |
|
---|
91 | SamplingGrid::SamplingGrid(const SamplingGridProperties &_props) :
|
---|
92 | SamplingGridProperties(_props)
|
---|
93 | {
|
---|
94 | setWindowSize(zeroOffset, zeroOffset);
|
---|
95 | ASSERT( getWindowGridPoints() == (size_t)0,
|
---|
96 | "SamplingGrid::SamplingGrid() - incorrect number of samples given for the window.");
|
---|
97 | }
|
---|
98 |
|
---|
99 | SamplingGrid::SamplingGrid(
|
---|
100 | const SamplingGridProperties &_props,
|
---|
101 | const sampledvalues_t &_sampled_grid) :
|
---|
102 | SamplingGridProperties(_props),
|
---|
103 | sampled_grid(_sampled_grid)
|
---|
104 | {
|
---|
105 | setWindowSize(_props.begin, _props.end);
|
---|
106 | ASSERT( getWindowGridPoints() == (size_t)_sampled_grid.size(),
|
---|
107 | "SamplingGrid::SamplingGrid() - incorrect number of samples given for the window.");
|
---|
108 | }
|
---|
109 |
|
---|
110 | SamplingGrid::~SamplingGrid()
|
---|
111 | {}
|
---|
112 |
|
---|
113 | SamplingGrid& SamplingGrid::operator=(const SamplingGrid& other)
|
---|
114 | {
|
---|
115 | // check for self-assignment
|
---|
116 | if (this != &other) {
|
---|
117 | static_cast<SamplingGridProperties &>(*this) = other;
|
---|
118 | setWindowSize(other.begin_window, other.end_window);
|
---|
119 | sampled_grid = other.sampled_grid;
|
---|
120 | }
|
---|
121 | return *this;
|
---|
122 | }
|
---|
123 |
|
---|
124 | SamplingGrid& SamplingGrid::operator*=(const SamplingGrid& other)
|
---|
125 | {
|
---|
126 | // check that grids are compatible
|
---|
127 | if (isCompatible(other)) {
|
---|
128 | sampledvalues_t::iterator iter = sampled_grid.begin();
|
---|
129 | sampledvalues_t::const_iterator otheriter = other.sampled_grid.begin();
|
---|
130 | for(; iter != sampled_grid.end(); ++iter, ++otheriter )
|
---|
131 | *iter *= (*otheriter);
|
---|
132 | } else {
|
---|
133 | ASSERT(0, "SamplingGrid::operator*=() - superposing incompatible grids is so far not in the cards.");
|
---|
134 | }
|
---|
135 | return *this;
|
---|
136 | }
|
---|
137 |
|
---|
138 | void SamplingGrid::superposeOtherGrids(const SamplingGrid &other, const double prefactor)
|
---|
139 | {
|
---|
140 | /// check that grids are compatible
|
---|
141 | if (isCompatible(other)) {
|
---|
142 | /// get maximum of window
|
---|
143 | double max_begin_window[3];
|
---|
144 | double max_end_window[3];
|
---|
145 | bool doExtend = false;
|
---|
146 | for (size_t index=0; index<3;++index) {
|
---|
147 | if (begin_window[index] >= other.begin_window[index]) {
|
---|
148 | max_begin_window[index] = other.begin_window[index];
|
---|
149 | doExtend = true;
|
---|
150 | } else {
|
---|
151 | max_begin_window[index] = begin_window[index];
|
---|
152 | }
|
---|
153 | if (end_window[index] >= other.end_window[index]) {
|
---|
154 | max_end_window[index] = end_window[index];
|
---|
155 | } else {
|
---|
156 | max_end_window[index] = other.end_window[index];
|
---|
157 | doExtend = true;
|
---|
158 | }
|
---|
159 | }
|
---|
160 | LOG(2, "DEBUG: max begin is " << max_begin_window[0] << "," << max_begin_window[1] << "," << max_begin_window[2] << ".");
|
---|
161 | LOG(2, "DEBUG: max end is " << max_end_window[0] << "," << max_end_window[1] << "," << max_end_window[2] << ".");
|
---|
162 | if (doExtend)
|
---|
163 | extendWindow(max_begin_window, max_end_window);
|
---|
164 | /// and copy other into larger window, too
|
---|
165 | addOntoWindow(other.begin_window, other.end_window, other.sampled_grid, prefactor);
|
---|
166 | } else {
|
---|
167 | ASSERT(0, "SamplingGrid::superposeOtherGrids() - superposing incompatible grids is so far not in the cards.");
|
---|
168 | }
|
---|
169 | }
|
---|
170 |
|
---|
171 | const double SamplingGrid::getVolume() const
|
---|
172 | {
|
---|
173 | double volume = 1.;
|
---|
174 | for (size_t i=0;i<3;++i)
|
---|
175 | volume *= end[i]-begin[i];
|
---|
176 | return volume;
|
---|
177 | }
|
---|
178 |
|
---|
179 | const double SamplingGrid::getWindowVolume() const
|
---|
180 | {
|
---|
181 | double volume = 1.;
|
---|
182 | for (size_t i=0;i<3;++i)
|
---|
183 | volume *= end_window[i]-begin_window[i];
|
---|
184 | return volume;
|
---|
185 | }
|
---|
186 |
|
---|
187 | const size_t SamplingGrid::getTotalGridPoints() const
|
---|
188 | {
|
---|
189 | return pow(getGridPointsPerAxis(),3);
|
---|
190 | }
|
---|
191 |
|
---|
192 | const size_t SamplingGrid::getGridPointsPerAxis() const
|
---|
193 | {
|
---|
194 | return pow(2, level);
|
---|
195 | }
|
---|
196 |
|
---|
197 | const size_t SamplingGrid::getWindowGridPoints() const
|
---|
198 | {
|
---|
199 | const double volume = getVolume();
|
---|
200 | if (fabs(volume) < std::numeric_limits<double>::epsilon())
|
---|
201 | return 0;
|
---|
202 | else
|
---|
203 | return (getWindowVolume()*getTotalGridPoints())/volume;
|
---|
204 | }
|
---|
205 |
|
---|
206 | double SamplingGrid::integral() const
|
---|
207 | {
|
---|
208 | const double volume_element = getVolume()/(double)getTotalGridPoints();
|
---|
209 | double int_value = 0.;
|
---|
210 | for (sampledvalues_t::const_iterator iter = sampled_grid.begin();
|
---|
211 | iter != sampled_grid.end();
|
---|
212 | ++iter)
|
---|
213 | int_value += *iter;
|
---|
214 | int_value *= volume_element;
|
---|
215 | LOG(2, "DEBUG: SamplingGrid::integral() is " << scientific << setprecision(13) << int_value << ".");
|
---|
216 | return int_value;
|
---|
217 | }
|
---|
218 |
|
---|
219 | double SamplingGrid::integral(const SamplingGrid &weight) const
|
---|
220 | {
|
---|
221 | if (isCompatible(weight)) {
|
---|
222 | const double volume_element = getVolume()/(double)getTotalGridPoints();
|
---|
223 | double int_value = 0.;
|
---|
224 | sampledvalues_t::const_iterator iter = sampled_grid.begin();
|
---|
225 | sampledvalues_t::const_iterator weightiter = weight.sampled_grid.begin();
|
---|
226 | for (;iter != sampled_grid.end();++iter,++weightiter)
|
---|
227 | int_value += (*weightiter) * (*iter);
|
---|
228 | int_value *= volume_element;
|
---|
229 | //LOG(2, "DEBUG: SamplingGrid::integral() is " << scientific << setprecision(13) << int_value << ".");
|
---|
230 | return int_value;
|
---|
231 | } else
|
---|
232 | return 0.;
|
---|
233 | }
|
---|
234 |
|
---|
235 | void SamplingGrid::setWindowSize(
|
---|
236 | const double _begin_window[3],
|
---|
237 | const double _end_window[3])
|
---|
238 | {
|
---|
239 | for (size_t index=0;index<3;++index) {
|
---|
240 | ASSERT( _begin_window[index] >= begin[index],
|
---|
241 | "SamplingGrid::setWindowSize() - window starts earlier than domain on "
|
---|
242 | +toString(index)+"th component.");
|
---|
243 | begin_window[index] = _begin_window[index];
|
---|
244 | ASSERT( _end_window[index] <= end[index],
|
---|
245 | "SamplingGrid::setWindowSize() - window ends later than domain on "
|
---|
246 | +toString(index)+"th component.");
|
---|
247 | end_window[index] = _end_window[index];
|
---|
248 | }
|
---|
249 | }
|
---|
250 |
|
---|
251 | void SamplingGrid::setWindow(
|
---|
252 | const double _begin_window[3],
|
---|
253 | const double _end_window[3])
|
---|
254 | {
|
---|
255 | setWindowSize(_begin_window, _end_window);
|
---|
256 | const size_t gridpoints_window = getWindowGridPoints();
|
---|
257 | sampled_grid.clear();
|
---|
258 | sampled_grid.resize(gridpoints_window, 0.);
|
---|
259 | }
|
---|
260 |
|
---|
261 | void SamplingGrid::setDomainSize(
|
---|
262 | const double _begin[3],
|
---|
263 | const double _end[3])
|
---|
264 | {
|
---|
265 | for (size_t index=0;index<3;++index) {
|
---|
266 | begin[index] = _begin[index];
|
---|
267 | end[index] = _end[index];
|
---|
268 | }
|
---|
269 | }
|
---|
270 |
|
---|
271 | void SamplingGrid::setDomain(
|
---|
272 | const double _begin[3],
|
---|
273 | const double _end[3])
|
---|
274 | {
|
---|
275 | setDomainSize(_begin, _end);
|
---|
276 | setWindowSize(_begin, _end);
|
---|
277 | const size_t gridpoints = getTotalGridPoints();
|
---|
278 | sampled_grid.resize(gridpoints, 0.);
|
---|
279 | }
|
---|
280 |
|
---|
281 | void SamplingGrid::extendWindow(
|
---|
282 | const double _begin_window[3],
|
---|
283 | const double _end_window[3])
|
---|
284 | {
|
---|
285 | #ifndef NDEBUG
|
---|
286 | for(size_t index=0;index < 3; ++index) {
|
---|
287 | // check that we truly have to extend the window
|
---|
288 | ASSERT ( begin_window[index] >= _begin_window[index],
|
---|
289 | "SamplingGrid::extendWindow() - component "+toString(index)+
|
---|
290 | " of window start is greater than old value.");
|
---|
291 | ASSERT ( end_window[index] <= _end_window[index],
|
---|
292 | "SamplingGrid::extendWindow() - component "+toString(index)+
|
---|
293 | " of window end is less than old value.");
|
---|
294 |
|
---|
295 | // check that we are still less than domain
|
---|
296 | ASSERT ( _begin_window[index] >= begin[index],
|
---|
297 | "SamplingGrid::extendWindow() - component "+toString(index)+
|
---|
298 | " of window start is less than domain start.");
|
---|
299 | ASSERT ( _end_window[index] <= end[index],
|
---|
300 | "SamplingGrid::extendWindow() - component "+toString(index)+
|
---|
301 | " of window end is greater than domain end.");
|
---|
302 | }
|
---|
303 | #endif
|
---|
304 | // copy old window size and values
|
---|
305 | double old_begin_window[3];
|
---|
306 | double old_end_window[3];
|
---|
307 | for(size_t index=0;index<3;++index) {
|
---|
308 | old_begin_window[index] = begin_window[index];
|
---|
309 | old_end_window[index] = end_window[index];
|
---|
310 | }
|
---|
311 | sampledvalues_t old_values(sampled_grid);
|
---|
312 | // set new window
|
---|
313 | setWindow(_begin_window,_end_window);
|
---|
314 | // now extend it ...
|
---|
315 | addOntoWindow(old_begin_window, old_end_window, old_values, +1.);
|
---|
316 | LOG(2, "DEBUG: Grid after extension is " << sampled_grid << ".");
|
---|
317 | }
|
---|
318 |
|
---|
319 | void SamplingGrid::addOntoWindow(
|
---|
320 | const double _begin_window[3],
|
---|
321 | const double _end_window[3],
|
---|
322 | const sampledvalues_t &_sampled_grid,
|
---|
323 | const double prefactor)
|
---|
324 | {
|
---|
325 | #ifndef NDEBUG
|
---|
326 | for(size_t index=0;index<3;++index) {
|
---|
327 | ASSERT( _begin_window[index] >= begin_window[index],
|
---|
328 | "SamplingGrid::addOntoWindow() - given window starts earlier in component "
|
---|
329 | +toString(index)+".");
|
---|
330 | ASSERT( _end_window[index] <= end_window[index],
|
---|
331 | "SamplingGrid::addOntoWindow() - given window ends later in component "
|
---|
332 | +toString(index)+".");
|
---|
333 | }
|
---|
334 | #endif
|
---|
335 | // the only issue are indices
|
---|
336 | const size_t gridpoints_axis = getGridPointsPerAxis();
|
---|
337 | size_t pre_offset[3];
|
---|
338 | size_t post_offset[3];
|
---|
339 | size_t length[3];
|
---|
340 | size_t total[3];
|
---|
341 | for(size_t index=0;index<3;++index) {
|
---|
342 | pre_offset[index] = (_begin_window[index] - begin_window[index])*gridpoints_axis;
|
---|
343 | length[index] = (_end_window[index] - _begin_window[index])*gridpoints_axis;
|
---|
344 | post_offset[index] = (end_window[index] - _end_window[index])*gridpoints_axis;
|
---|
345 | total[index] = (end_window[index] - begin_window[index])*gridpoints_axis;
|
---|
346 | ASSERT( pre_offset[index]+post_offset[index]+length[index] == total[index],
|
---|
347 | "SamplingGrid::addOntoWindow() - pre, post, and length don't sum up to total for "
|
---|
348 | +toString(index)+"th component.");
|
---|
349 | }
|
---|
350 | #ifndef NDEBUG
|
---|
351 | const size_t calculated_size = length[0]*length[1]*length[2];
|
---|
352 | ASSERT( calculated_size == _sampled_grid.size(),
|
---|
353 | "SamplingGrid::addOntoWindow() - not enough sampled values given: "
|
---|
354 | +toString(calculated_size)+" != "+toString(_sampled_grid.size())+".");
|
---|
355 | ASSERT( calculated_size <= sampled_grid.size(),
|
---|
356 | "SamplingGrid::addOntoWindow() - not enough sampled values available: "
|
---|
357 | +toString(calculated_size)+" <= "+toString(sampled_grid.size())+".");
|
---|
358 | const size_t total_size = total[0]*total[1]*total[2];
|
---|
359 | ASSERT( total_size == sampled_grid.size(),
|
---|
360 | "SamplingGrid::addOntoWindow() - total size is not equal to number of present points: "
|
---|
361 | +toString(total_size)+" != "+toString(sampled_grid.size())+".");
|
---|
362 | #endif
|
---|
363 | size_t N[3];
|
---|
364 | size_t counter = 0;
|
---|
365 | sampledvalues_t::iterator griditer = sampled_grid.begin();
|
---|
366 | std::advance(griditer, pre_offset[0]*total[1]*total[2]);
|
---|
367 | sampledvalues_t::const_iterator copyiter = _sampled_grid.begin();
|
---|
368 | for(N[0]=0; N[0] < length[0]; ++N[0]) {
|
---|
369 | std::advance(griditer, pre_offset[1]*total[2]);
|
---|
370 | for(N[1]=0; N[1] < length[1]; ++N[1]) {
|
---|
371 | std::advance(griditer, pre_offset[2]);
|
---|
372 | for(N[2]=0; N[2] < length[2]; ++N[2]) {
|
---|
373 | ASSERT( griditer != sampled_grid.end(),
|
---|
374 | "SamplingGrid::addOntoWindow() - griditer is already at end of window.");
|
---|
375 | ASSERT( copyiter != _sampled_grid.end(),
|
---|
376 | "SamplingGrid::addOntoWindow() - griditer is already at end of window.");
|
---|
377 | *griditer++ += prefactor*(*copyiter++);
|
---|
378 | }
|
---|
379 | std::advance(griditer, post_offset[2]);
|
---|
380 | }
|
---|
381 | std::advance(griditer, post_offset[1]*total[2]);
|
---|
382 | }
|
---|
383 | #ifndef NDEBUG
|
---|
384 | std::advance(griditer, post_offset[0]*total[1]*total[2]);
|
---|
385 | ASSERT( griditer == sampled_grid.end(),
|
---|
386 | "SamplingGrid::addOntoWindow() - griditer is not at end of window.");
|
---|
387 | ASSERT( copyiter == _sampled_grid.end(),
|
---|
388 | "SamplingGrid::addOntoWindow() - copyiter is not at end of window.");
|
---|
389 | #endif
|
---|
390 | LOG(2, "DEBUG: Grid after adding other is " << sampled_grid << ".");
|
---|
391 | }
|
---|
392 |
|
---|
393 | std::ostream & operator<<(std::ostream &ost, const SamplingGrid& other)
|
---|
394 | {
|
---|
395 | ost << "SamplingGrid";
|
---|
396 | ost << " starting at " << other.begin[0] << "," << other.begin[1] << "," << other.begin[2];
|
---|
397 | ost << " ending at " << other.end[0] << "," << other.end[1] << "," << other.end[2];
|
---|
398 | ost << ", window starting at " << other.begin_window[0] << "," << other.begin_window[1] << "," << other.begin_window[2];
|
---|
399 | ost << ", window ending at " << other.end_window[0] << "," << other.end_window[1] << "," << other.end_window[2];
|
---|
400 | ost << ", level of " << other.level;
|
---|
401 | ost << " and integrated value of " << other.integral();
|
---|
402 | return ost;
|
---|
403 | }
|
---|
404 |
|
---|
405 | template<> SamplingGrid ZeroInstance<SamplingGrid>()
|
---|
406 | {
|
---|
407 | SamplingGrid returnvalue;
|
---|
408 | return returnvalue;
|
---|
409 | }
|
---|
410 |
|
---|
411 | // we need to explicitly instantiate the serialization functions as
|
---|
412 | // its is only serialized through its base class FragmentJob
|
---|
413 | BOOST_CLASS_EXPORT_IMPLEMENT(SamplingGrid)
|
---|