1 | /*
|
---|
2 | * SamplingGrid.hpp
|
---|
3 | *
|
---|
4 | * Created on: 25.07.2012
|
---|
5 | * Author: heber
|
---|
6 | */
|
---|
7 |
|
---|
8 | #ifndef SAMPLINGGRID_HPP_
|
---|
9 | #define SAMPLINGGRID_HPP_
|
---|
10 |
|
---|
11 | // include config.h
|
---|
12 | #ifdef HAVE_CONFIG_H
|
---|
13 | #include <config.h>
|
---|
14 | #endif
|
---|
15 |
|
---|
16 | #include <boost/function.hpp>
|
---|
17 | #include <iosfwd>
|
---|
18 | #include <vector>
|
---|
19 |
|
---|
20 | #include "boost/serialization/export.hpp"
|
---|
21 | #include "boost/serialization/vector.hpp"
|
---|
22 |
|
---|
23 | #include "LinearAlgebra/defs.hpp"
|
---|
24 |
|
---|
25 | #include "Fragmentation/Summation/SetValues/SamplingGridProperties.hpp"
|
---|
26 | #include "Fragmentation/Summation/ZeroInstance.hpp"
|
---|
27 |
|
---|
28 | class MPQCData;
|
---|
29 | class SamplingGridTest;
|
---|
30 |
|
---|
31 | /** This class stores a sample function on a three-dimensional grid.
|
---|
32 | *
|
---|
33 | * \note We do not use boost::multi_array because it is not trivial to serialize.
|
---|
34 | *
|
---|
35 | */
|
---|
36 | class SamplingGrid : public SamplingGridProperties
|
---|
37 | {
|
---|
38 | //!> grant unit test access to private parts
|
---|
39 | friend class SamplingGridTest;
|
---|
40 | //!> grant output operator access
|
---|
41 | friend std::ostream & operator<<(std::ostream &ost, const SamplingGrid& other);
|
---|
42 | public:
|
---|
43 | //!> typedef for sampled values
|
---|
44 | typedef std::vector< double > sampledvalues_t;
|
---|
45 |
|
---|
46 | /** Constructor for class SamplingGrid for full window.
|
---|
47 | *
|
---|
48 | * Here, the window of sampled values spans the given domain.
|
---|
49 | *
|
---|
50 | * \param _begin offset for grid per axis
|
---|
51 | * \param _end edge length of grid per axis
|
---|
52 | * \param _level number of grid points in \f$2^{\mathrm{level}}\f$
|
---|
53 | * \param _sampled_grid sample points
|
---|
54 | */
|
---|
55 | SamplingGrid(const double _begin[NDIM],
|
---|
56 | const double _end[NDIM],
|
---|
57 | const int _level,
|
---|
58 | const sampledvalues_t &_sampled_grid);
|
---|
59 |
|
---|
60 | /** Constructor for class SamplingGrid for empty window.
|
---|
61 | *
|
---|
62 | * Here, the window is initially of size zero.
|
---|
63 | *
|
---|
64 | * \param _begin offset for grid per axis
|
---|
65 | * \param _end edge length of grid per axis
|
---|
66 | * \param _level number of grid points in \f$2^{\mathrm{level}}\f$
|
---|
67 | */
|
---|
68 | SamplingGrid(const double _begin[NDIM],
|
---|
69 | const double _end[NDIM],
|
---|
70 | const int _level);
|
---|
71 |
|
---|
72 | /** Copy constructor for class SamplingGrid with full window from SamplingGridProperties.
|
---|
73 | *
|
---|
74 | * Here, the window is initially empty.
|
---|
75 | *
|
---|
76 | * \param _props properties to copy
|
---|
77 | */
|
---|
78 | SamplingGrid(const SamplingGridProperties &_props);
|
---|
79 |
|
---|
80 | /** Copy constructor for class SamplingGrid with empty window from SamplingGridProperties.
|
---|
81 | *
|
---|
82 | * Here, the window must span the whole domain
|
---|
83 | *
|
---|
84 | * \param _props properties to copy
|
---|
85 | * \param _sampled_grid sample points
|
---|
86 | */
|
---|
87 | SamplingGrid(
|
---|
88 | const SamplingGridProperties &_props,
|
---|
89 | const sampledvalues_t &_sampled_grid);
|
---|
90 |
|
---|
91 | /** Copy constructor for class SamplingGrid.
|
---|
92 | *
|
---|
93 | * The window of sampled values corresponds to the one on \a _grid.
|
---|
94 | *
|
---|
95 | * \param _grid grid to copy
|
---|
96 | */
|
---|
97 | SamplingGrid(const SamplingGrid &_grid);
|
---|
98 |
|
---|
99 | /** default cstor.
|
---|
100 | */
|
---|
101 | SamplingGrid();
|
---|
102 |
|
---|
103 | virtual ~SamplingGrid();
|
---|
104 |
|
---|
105 | /** Checks whether another instance is consistent with this one.
|
---|
106 | *
|
---|
107 | * \note Conistency is stronger as grids must have the same window.
|
---|
108 | *
|
---|
109 | * \param _props other properties to check against
|
---|
110 | * \return true - are consistent, false - else
|
---|
111 | */
|
---|
112 | bool isCongruent(const SamplingGrid &_props) const;
|
---|
113 |
|
---|
114 | /** Assignment operator.
|
---|
115 | *
|
---|
116 | * \param other other instance to assign ourselves to
|
---|
117 | */
|
---|
118 | SamplingGrid& operator=(const SamplingGrid& other);
|
---|
119 |
|
---|
120 | /** Addition operator with another SamplingGrid instance \a other.
|
---|
121 | *
|
---|
122 | * \param other other instance to sum onto this one.
|
---|
123 | * \return ref to this instance
|
---|
124 | */
|
---|
125 | SamplingGrid& operator+=(const SamplingGrid& other)
|
---|
126 | {
|
---|
127 | superposeOtherGrids(other, +1.);
|
---|
128 | return *this;
|
---|
129 | }
|
---|
130 |
|
---|
131 | /** Element-wise multiplication operator with another SamplingGrid instance \a other.
|
---|
132 | *
|
---|
133 | * With non-zero windows we have to pay some more attention here.
|
---|
134 | * Now, the windows may not be congruent but we have to find the intersection
|
---|
135 | * of the two windows and then construct the new window only of this size and
|
---|
136 | * multiply. The trick then is not to copy&change the other grid but to
|
---|
137 | * access it properly.
|
---|
138 | *
|
---|
139 | * \param other other instance to sum onto this one.
|
---|
140 | * \return ref to this instance
|
---|
141 | */
|
---|
142 | SamplingGrid& operator*=(const SamplingGrid& other);
|
---|
143 |
|
---|
144 | /** Scaling values in SamplingGrid instance by \a _value.
|
---|
145 | *
|
---|
146 | * With non-zero windows we have to pay some more attention here.
|
---|
147 | * Now, the windows may not be congruent but we have to find the intersection
|
---|
148 | * of the two windows and then construct the new window only of this size and
|
---|
149 | * multiply. The trick then is not to copy&change the other grid but to
|
---|
150 | * access it properly.
|
---|
151 | *
|
---|
152 | * \param other other instance to sum onto this one.
|
---|
153 | * \return ref to this instance
|
---|
154 | */
|
---|
155 | SamplingGrid& operator*=(const double _value);
|
---|
156 |
|
---|
157 | /** Subtraction operator with another SamplingGrid instance \a other.
|
---|
158 | *
|
---|
159 | * \param other other instance to subtract from this one.
|
---|
160 | * \return ref to this instance
|
---|
161 | */
|
---|
162 | SamplingGrid& operator-=(const SamplingGrid& other)
|
---|
163 | {
|
---|
164 | superposeOtherGrids(other, -1.);
|
---|
165 | return *this;
|
---|
166 | }
|
---|
167 |
|
---|
168 | /** Returns the numeric integral over the grid.
|
---|
169 | *
|
---|
170 | * @return sum of grid values times volume element
|
---|
171 | */
|
---|
172 | double integral() const;
|
---|
173 |
|
---|
174 | /** Returns the numeric integral over the grid where the grid is element-wise multiplied with \a weight.
|
---|
175 | *
|
---|
176 | * @param weight grid of weights
|
---|
177 | * @return sum of grid values weighted by respective element in weight times volume element
|
---|
178 | */
|
---|
179 | double integral(const SamplingGrid &weight) const;
|
---|
180 |
|
---|
181 | /** Returns the total number of gridpoints of the discrete mesh covering the (window) volume.
|
---|
182 | *
|
---|
183 | * @return number of gridpoints sampled_values should have
|
---|
184 | */
|
---|
185 | const size_t getWindowGridPoints() const;
|
---|
186 |
|
---|
187 | /** Returns the number of gridpoints of the discrete mesh for the current
|
---|
188 | * window size for given axis \axis.
|
---|
189 | *
|
---|
190 | * \param axis axis to calculate number of gridpoints for
|
---|
191 | * \return number of gridpoints along this axis
|
---|
192 | */
|
---|
193 | const size_t getWindowGridPointsPerAxis(const size_t axis) const;
|
---|
194 |
|
---|
195 | /** Returns the length of the window for the given \a axis.
|
---|
196 | *
|
---|
197 | * \param axis axis for which to get step length
|
---|
198 | * \return window length for the given axis, i.e. end - begin
|
---|
199 | */
|
---|
200 | const double getWindowLengthPerAxis(const size_t axis) const;
|
---|
201 |
|
---|
202 | /** Returns the volume of the domain covered by the current window.
|
---|
203 | *
|
---|
204 | * @return volume of window
|
---|
205 | */
|
---|
206 | const double getWindowVolume() const;
|
---|
207 |
|
---|
208 | /** Sets the size of the window.
|
---|
209 | *
|
---|
210 | * \note also resets the sampled points so far.
|
---|
211 | *
|
---|
212 | * \param _begin_window start of new window
|
---|
213 | * \param _end_window end of window
|
---|
214 | */
|
---|
215 | void setWindow(const double _begin_window[NDIM], const double _end_window[NDIM]);
|
---|
216 |
|
---|
217 | /** Helper function to convert begin_window and end_window that are w.r.t.
|
---|
218 | * to domain [begin:end] to indices that can be used when traversing the grid.
|
---|
219 | *
|
---|
220 | * \param larger_wbegin begin of domain
|
---|
221 | * \param larger_wend end of domain
|
---|
222 | * \param smaller_wbegin begin of window
|
---|
223 | * \param smaller_wend end of window
|
---|
224 | * \param pre_offset discrete length from 0 to start of window
|
---|
225 | * \param post_offset discrete length from end of window to end
|
---|
226 | * \param length discrete length of window
|
---|
227 | * \param total total number of points for checking, should be sum of other three
|
---|
228 | */
|
---|
229 | void getDiscreteWindowIndices(
|
---|
230 | const double *larger_wbegin,
|
---|
231 | const double *larger_wend,
|
---|
232 | const double *smaller_wbegin,
|
---|
233 | const double *smaller_wend,
|
---|
234 | size_t *pre_offset,
|
---|
235 | size_t *post_offset,
|
---|
236 | size_t *length,
|
---|
237 | size_t *total) const;
|
---|
238 |
|
---|
239 | /** Equality operator.
|
---|
240 | *
|
---|
241 | * @param other other instance to check against
|
---|
242 | * @return true - both are equal, false - grids differ
|
---|
243 | */
|
---|
244 | bool operator==(const SamplingGrid& other) const;
|
---|
245 |
|
---|
246 | bool operator!=(const SamplingGrid& other) const
|
---|
247 | {
|
---|
248 | return (!(*this == other));
|
---|
249 | }
|
---|
250 |
|
---|
251 | private:
|
---|
252 | /** Sets the size of the domain.
|
---|
253 | *
|
---|
254 | * \note also resets the sampled points so far and the window.
|
---|
255 | *
|
---|
256 | * \param _begin start of new window
|
---|
257 | * \param _end end of window
|
---|
258 | */
|
---|
259 | void setDomain(const double _begin[NDIM], const double _end[NDIM]);
|
---|
260 |
|
---|
261 | /** Sets the size of the domain.
|
---|
262 | *
|
---|
263 | * \note this is just internally used for easing the array setting.
|
---|
264 | *
|
---|
265 | * \param _begin start of domain
|
---|
266 | * \param _end end of domain
|
---|
267 | */
|
---|
268 | void setDomainSize(const double _begin[NDIM], const double _end[NDIM]);
|
---|
269 |
|
---|
270 | /** Extends the window while keeping the values.
|
---|
271 | *
|
---|
272 | * \param _begin_window new start of window
|
---|
273 | * \param _end_window new end of window
|
---|
274 | */
|
---|
275 | void extendWindow(const double _begin_window[NDIM], const double _end_window[NDIM]);
|
---|
276 |
|
---|
277 | /** Shrinks the window while keeping the values.
|
---|
278 | *
|
---|
279 | * \param _begin_window new start of window
|
---|
280 | * \param _end_window new end of window
|
---|
281 | */
|
---|
282 | void shrinkWindow(const double _begin_window[NDIM], const double _end_window[NDIM]);
|
---|
283 |
|
---|
284 | /** Adds another (smaller) window onto the one in this instance.
|
---|
285 | *
|
---|
286 | * \note We assume here that the given window fits on the this one.
|
---|
287 | *
|
---|
288 | * \param _begin_window start of other window
|
---|
289 | * \param _end_window end of other window
|
---|
290 | * \param _sampled_grid other set of sampled values
|
---|
291 | * @param prefactor +1. is then addition, -1. is subtraction.
|
---|
292 | */
|
---|
293 | void addOntoWindow(
|
---|
294 | const double _begin_window[NDIM],
|
---|
295 | const double _end_window[NDIM],
|
---|
296 | const sampledvalues_t &_sampled_grid,
|
---|
297 | const double prefactor);
|
---|
298 |
|
---|
299 | /** Adds another (larger) window into the one in this instance.
|
---|
300 | *
|
---|
301 | * \note We assume here that the given window is larger than this one.
|
---|
302 | *
|
---|
303 | * \param _begin_window start of other window
|
---|
304 | * \param _end_window end of other window
|
---|
305 | * \param _sampled_grid other set of sampled values
|
---|
306 | * @param prefactor +1. is then addition, -1. is subtraction.
|
---|
307 | */
|
---|
308 | void addIntoWindow(
|
---|
309 | const double _begin_window[NDIM],
|
---|
310 | const double _end_window[NDIM],
|
---|
311 | const sampledvalues_t &_sampled_grid,
|
---|
312 | const double prefactor);
|
---|
313 |
|
---|
314 | /** Enum to help in addWindowOntoWindow() decide which iterator needs to be
|
---|
315 | * advanced.
|
---|
316 | */
|
---|
317 | enum eLargerWindow {
|
---|
318 | destwindow,
|
---|
319 | sourcewindow
|
---|
320 | };
|
---|
321 |
|
---|
322 | /** Helper function to copy one (larger) window into a (smaller) window.
|
---|
323 | *
|
---|
324 | * \note Why do we need the extra \a choice? We need to know which window
|
---|
325 | * tuples is associated with which sampled values that are constrained by
|
---|
326 | * one of them being constant, hence the source values
|
---|
327 | *
|
---|
328 | * \param larger_wbegin start of larger window
|
---|
329 | * \param larger_wend end of larger window
|
---|
330 | * \param smaller_wbegin start of smaller window
|
---|
331 | * \param smaller_wend end of smaller window
|
---|
332 | * \param dest_sampled_grid larger set of sampled values
|
---|
333 | * \param source_sampled_grid smaller set of sampled values
|
---|
334 | * \param op operation to perform with the two elements
|
---|
335 | * \param larger_window indicates which is the larger window
|
---|
336 | */
|
---|
337 | void addWindowOntoWindow(
|
---|
338 | const double larger_wbegin[NDIM],
|
---|
339 | const double larger_wend[NDIM],
|
---|
340 | const double smaller_wbegin[NDIM],
|
---|
341 | const double smaller_wend[NDIM],
|
---|
342 | sampledvalues_t &dest_sampled_grid,
|
---|
343 | const sampledvalues_t &source_sampled_grid,
|
---|
344 | boost::function<void (double &, const double &)> op,
|
---|
345 | enum eLargerWindow larger_window);
|
---|
346 |
|
---|
347 | /** Helper function that contains all the logic of how to superpose two
|
---|
348 | * grids.
|
---|
349 | *
|
---|
350 | * Is called by SamplingGrid::operator+=() and SamplingGrid::operator-=()
|
---|
351 | *
|
---|
352 | * @param other other histogram
|
---|
353 | * @param prefactor +1. is then addition, -1. is subtraction.
|
---|
354 | */
|
---|
355 | void superposeOtherGrids(const SamplingGrid &other, const double prefactor);
|
---|
356 |
|
---|
357 | /** Sets the size of the window.
|
---|
358 | *
|
---|
359 | * \note also resets the sampled points so far.
|
---|
360 | *
|
---|
361 | * \param _begin_window start of new window
|
---|
362 | * \param _end_window end of window
|
---|
363 | */
|
---|
364 | void setWindowSize(const double _begin_window[NDIM], const double _end_window[NDIM]);
|
---|
365 |
|
---|
366 | public:
|
---|
367 | /// We do not store the whole grid if many entries are actually zero
|
---|
368 | /// but only a window wherein the sampled function is non-zero.
|
---|
369 |
|
---|
370 | //!> sample points of the window
|
---|
371 | sampledvalues_t sampled_grid;
|
---|
372 |
|
---|
373 | //!> start of the window relative to SamplingGridProperties::begin and SamplingGridProperties::size
|
---|
374 | double begin_window[NDIM];
|
---|
375 | //!> end of the window relative to SamplingGridProperties::begin and SamplingGridProperties::size
|
---|
376 | double end_window[NDIM];
|
---|
377 |
|
---|
378 | private:
|
---|
379 | friend class MPQCData;
|
---|
380 |
|
---|
381 | friend class boost::serialization::access;
|
---|
382 | // serialization
|
---|
383 | template <typename Archive>
|
---|
384 | void serialize(Archive& ar, const unsigned int version)
|
---|
385 | {
|
---|
386 | ar & boost::serialization::base_object<SamplingGridProperties>(*this);
|
---|
387 | ar & const_cast< sampledvalues_t &>(sampled_grid);
|
---|
388 | for(size_t i=0;i<NDIM;++i) {
|
---|
389 | ar & begin_window[i];
|
---|
390 | ar & end_window[i];
|
---|
391 | }
|
---|
392 | }
|
---|
393 |
|
---|
394 | //!> static typedef to use in cstor when no initial values are given
|
---|
395 | static const double zeroOffset[NDIM];
|
---|
396 | };
|
---|
397 |
|
---|
398 | /** Output operator for class SamplingGrid.
|
---|
399 | *
|
---|
400 | * \param ost output stream to print to
|
---|
401 | * \param other instance to print
|
---|
402 | * \return ref to stream for concatenation
|
---|
403 | */
|
---|
404 | std::ostream & operator<<(std::ostream &ost, const SamplingGrid& other);
|
---|
405 |
|
---|
406 | template<> SamplingGrid ZeroInstance<SamplingGrid>();
|
---|
407 |
|
---|
408 | // we need to give this class a unique key for serialization
|
---|
409 | // its is only serialized through its base class FragmentJob
|
---|
410 | BOOST_CLASS_EXPORT_KEY(SamplingGrid)
|
---|
411 |
|
---|
412 | // define inline functions
|
---|
413 | #include "SamplingGrid_inline.hpp"
|
---|
414 |
|
---|
415 | #endif /* SAMPLINGGRID_HPP_ */
|
---|