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 <iosfwd>
|
---|
17 | #include <vector>
|
---|
18 |
|
---|
19 | #include "boost/serialization/export.hpp"
|
---|
20 | #include "boost/serialization/vector.hpp"
|
---|
21 |
|
---|
22 | #include "Jobs/Grid/SamplingGridProperties.hpp"
|
---|
23 |
|
---|
24 | class MPQCData;
|
---|
25 | class SamplingGridTest;
|
---|
26 |
|
---|
27 | /** This class stores a sample function on a three-dimensional grid.
|
---|
28 | *
|
---|
29 | * \note We do not use boost::multi_array because it is not trivial to serialize.
|
---|
30 | *
|
---|
31 | */
|
---|
32 | class SamplingGrid : public SamplingGridProperties
|
---|
33 | {
|
---|
34 | //!> grant unit test access to private parts
|
---|
35 | friend class SamplingGridTest;
|
---|
36 | //!> grant output operator access
|
---|
37 | friend std::ostream & operator<<(std::ostream &ost, const SamplingGrid& other);
|
---|
38 | public:
|
---|
39 | //!> typedef for sampled values
|
---|
40 | typedef std::vector< double > sampledvalues_t;
|
---|
41 |
|
---|
42 | /** Constructor for class SamplingGrid for full window.
|
---|
43 | *
|
---|
44 | * Here, the window of sampled values spans the given domain.
|
---|
45 | *
|
---|
46 | * \param _begin offset for grid per axis
|
---|
47 | * \param _end edge length of grid per axis
|
---|
48 | * \param _level number of grid points in \f$2^{\mathrm{level}}\f$
|
---|
49 | * \param _sampled_grid sample points
|
---|
50 | */
|
---|
51 | SamplingGrid(const double _begin[3],
|
---|
52 | const double _end[3],
|
---|
53 | const int _level,
|
---|
54 | const sampledvalues_t &_sampled_grid);
|
---|
55 |
|
---|
56 | /** Constructor for class SamplingGrid for empty window.
|
---|
57 | *
|
---|
58 | * Here, the window is initially of size zero.
|
---|
59 | *
|
---|
60 | * \param _begin offset for grid per axis
|
---|
61 | * \param _end edge length of grid per axis
|
---|
62 | * \param _level number of grid points in \f$2^{\text{level}}\f$
|
---|
63 | */
|
---|
64 | SamplingGrid(const double _begin[3],
|
---|
65 | const double _end[3],
|
---|
66 | const int _level);
|
---|
67 |
|
---|
68 | /** Copy constructor for class SamplingGrid with full window from SamplingGridProperties.
|
---|
69 | *
|
---|
70 | * Here, the window is initially empty.
|
---|
71 | *
|
---|
72 | * \param _props properties to copy
|
---|
73 | */
|
---|
74 | SamplingGrid(const SamplingGridProperties &_props);
|
---|
75 |
|
---|
76 | /** Copy constructor for class SamplingGrid with empty window from SamplingGridProperties.
|
---|
77 | *
|
---|
78 | * Here, the window must span the whole domain
|
---|
79 | *
|
---|
80 | * \param _props properties to copy
|
---|
81 | * \param _sampled_grid sample points
|
---|
82 | */
|
---|
83 | SamplingGrid(
|
---|
84 | const SamplingGridProperties &_props,
|
---|
85 | const sampledvalues_t &_sampled_grid);
|
---|
86 |
|
---|
87 | /** Copy constructor for class SamplingGrid.
|
---|
88 | *
|
---|
89 | * The window of sampled values corresponds to the one on \a _grid.
|
---|
90 | *
|
---|
91 | * \param _grid grid to copy
|
---|
92 | */
|
---|
93 | SamplingGrid(const SamplingGrid &_grid);
|
---|
94 |
|
---|
95 | /** default cstor.
|
---|
96 | */
|
---|
97 | SamplingGrid();
|
---|
98 |
|
---|
99 | virtual ~SamplingGrid();
|
---|
100 |
|
---|
101 | /** Assignment operator.
|
---|
102 | *
|
---|
103 | * \param other other instance to assign ourselves to
|
---|
104 | */
|
---|
105 | SamplingGrid& operator=(const SamplingGrid& other);
|
---|
106 |
|
---|
107 | /** Addition operator with another SamplingGrid instance \a other.
|
---|
108 | *
|
---|
109 | * \param other other instance to sum onto this one.
|
---|
110 | * \return ref to this instance
|
---|
111 | */
|
---|
112 | SamplingGrid& operator+=(const SamplingGrid& other)
|
---|
113 | {
|
---|
114 | superposeOtherGrids(other, +1.);
|
---|
115 | return *this;
|
---|
116 | }
|
---|
117 |
|
---|
118 | /** Element-wise multiplication operator with another SamplingGrid instance \a other.
|
---|
119 | *
|
---|
120 | * \param other other instance to sum onto this one.
|
---|
121 | * \return ref to this instance
|
---|
122 | */
|
---|
123 | SamplingGrid& operator*=(const SamplingGrid& other);
|
---|
124 |
|
---|
125 | /** Subtraction operator with another SamplingGrid instance \a other.
|
---|
126 | *
|
---|
127 | * \param other other instance to subtract from this one.
|
---|
128 | * \return ref to this instance
|
---|
129 | */
|
---|
130 | SamplingGrid& operator-=(const SamplingGrid& other)
|
---|
131 | {
|
---|
132 | superposeOtherGrids(other, -1.);
|
---|
133 | return *this;
|
---|
134 | }
|
---|
135 |
|
---|
136 | /** Returns the numeric integral over the grid.
|
---|
137 | *
|
---|
138 | * @return sum of grid values times volume element
|
---|
139 | */
|
---|
140 | double integral() const;
|
---|
141 |
|
---|
142 | /** Returns the numeric integral over the grid where the grid is element-wise multiplied with \a weight.
|
---|
143 | *
|
---|
144 | * @param weight grid of weights
|
---|
145 | * @return sum of grid values weighted by respective element in weight times volume element
|
---|
146 | */
|
---|
147 | double integral(const SamplingGrid &weight) const;
|
---|
148 |
|
---|
149 | /** Returns the volume of the domain for this sampled function.
|
---|
150 | *
|
---|
151 | * @return volume
|
---|
152 | */
|
---|
153 | const double getVolume() const;
|
---|
154 |
|
---|
155 | /** Returns the total number of gridpoints of the discrete mesh covering the (window) volume.
|
---|
156 | *
|
---|
157 | * @return number of gridpoints sampled_values should have
|
---|
158 | */
|
---|
159 | const size_t getWindowGridPoints() const;
|
---|
160 |
|
---|
161 | /** Returns the number of gridpoints of the discrete mesh for the current
|
---|
162 | * window size for given axis \axis.
|
---|
163 | *
|
---|
164 | * \param axis axis to calculate number of gridpoints for
|
---|
165 | * \return number of gridpoints along this axis
|
---|
166 | */
|
---|
167 | const size_t getWindowGridPointsPerAxis(const size_t axis) const;
|
---|
168 |
|
---|
169 | /** Returns the total number of gridpoints of the discrete mesh covering the volume.
|
---|
170 | *
|
---|
171 | * @return number of gridpoints sampled_values should have
|
---|
172 | */
|
---|
173 | const size_t getTotalGridPoints() const;
|
---|
174 |
|
---|
175 | /** Returns the number of grid points per axis.
|
---|
176 | *
|
---|
177 | * @return number of grid points per unit length
|
---|
178 | */
|
---|
179 | const size_t getGridPointsPerAxis() const;
|
---|
180 |
|
---|
181 | /** Returns the length of the domain for the given \a axis.
|
---|
182 | *
|
---|
183 | * \param axis axis for which to get step length
|
---|
184 | * \return domain length for the given axis, i.e. end - begin
|
---|
185 | */
|
---|
186 | const double getTotalLengthPerAxis(const size_t axis) const;
|
---|
187 |
|
---|
188 | /** Returns the length of the window for the given \a axis.
|
---|
189 | *
|
---|
190 | * \param axis axis for which to get step length
|
---|
191 | * \return window length for the given axis, i.e. end - begin
|
---|
192 | */
|
---|
193 | const double getWindowLengthPerAxis(const size_t axis) const;
|
---|
194 |
|
---|
195 | /** Returns the real step length from one discrete grid point to the next.
|
---|
196 | *
|
---|
197 | * \param axis axis for which to get step length
|
---|
198 | * \return step length for the given axis, as domain length over getGridPointsPerAxis()
|
---|
199 | */
|
---|
200 | const double getDeltaPerAxis(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[3], const double _end_window[3]);
|
---|
216 |
|
---|
217 | private:
|
---|
218 | /** Sets the size of the domain.
|
---|
219 | *
|
---|
220 | * \note also resets the sampled points so far and the window.
|
---|
221 | *
|
---|
222 | * \param _begin start of new window
|
---|
223 | * \param _end end of window
|
---|
224 | */
|
---|
225 | void setDomain(const double _begin[3], const double _end[3]);
|
---|
226 |
|
---|
227 | /** Sets the size of the domain.
|
---|
228 | *
|
---|
229 | * \note this is just internally used for easing the array setting.
|
---|
230 | *
|
---|
231 | * \param _begin start of domain
|
---|
232 | * \param _end end of domain
|
---|
233 | */
|
---|
234 | void setDomainSize(const double _begin[3], const double _end[3]);
|
---|
235 |
|
---|
236 | /** Extends the window while keeping the values.
|
---|
237 | *
|
---|
238 | * \param _begin_window new start of window
|
---|
239 | * \param _end_window new end of window
|
---|
240 | */
|
---|
241 | void extendWindow(const double _begin_window[3], const double _end_window[3]);
|
---|
242 |
|
---|
243 | /** Adds another window onto the one in this instance.
|
---|
244 | *
|
---|
245 | * \note We assume here that the given window fits on the this one.
|
---|
246 | *
|
---|
247 | * \param _begin_window start of other window
|
---|
248 | * \param _end_window end of other window
|
---|
249 | * \param _sampled_grid other set of sampled values
|
---|
250 | * @param prefactor +1. is then addition, -1. is subtraction.
|
---|
251 | */
|
---|
252 | void addOntoWindow(
|
---|
253 | const double _begin_window[3],
|
---|
254 | const double _end_window[3],
|
---|
255 | const sampledvalues_t &_sampled_grid,
|
---|
256 | const double prefactor);
|
---|
257 |
|
---|
258 | /** Helper function that contains all the logic of how to superpose two
|
---|
259 | * grids.
|
---|
260 | *
|
---|
261 | * Is called by SamplingGrid::operator+=() and SamplingGrid::operator-=()
|
---|
262 | *
|
---|
263 | * @param other other histogram
|
---|
264 | * @param prefactor +1. is then addition, -1. is subtraction.
|
---|
265 | */
|
---|
266 | void superposeOtherGrids(const SamplingGrid &other, const double prefactor);
|
---|
267 |
|
---|
268 | /** Sets the size of the window.
|
---|
269 | *
|
---|
270 | * \note also resets the sampled points so far.
|
---|
271 | *
|
---|
272 | * \param _begin_window start of new window
|
---|
273 | * \param _end_window end of window
|
---|
274 | */
|
---|
275 | void setWindowSize(const double _begin_window[3], const double _end_window[3]);
|
---|
276 |
|
---|
277 | /** Helper function to get point at grid point for given \a axis and less than value.
|
---|
278 | *
|
---|
279 | * @param value value to find nearest grid point to
|
---|
280 | * @param axis axis of the value
|
---|
281 | * @return nearest lower grid point
|
---|
282 | */
|
---|
283 | double getNearestLowerGridPoint(
|
---|
284 | const double value, const size_t axis) const;
|
---|
285 |
|
---|
286 | /** Helper function to get point at grid point for given \a axis and greater than value.
|
---|
287 | *
|
---|
288 | * @param value value to find nearest grid point to
|
---|
289 | * @param axis axis of the value
|
---|
290 | * @return nearest lower grid point
|
---|
291 | */
|
---|
292 | double getNearestHigherGridPoint(
|
---|
293 | const double value, const size_t axis) const;
|
---|
294 |
|
---|
295 | public:
|
---|
296 | /// We do not store the whole grid if many entries are actually zero
|
---|
297 | /// but only a window wherein the sampled function is non-zero.
|
---|
298 |
|
---|
299 | //!> sample points of the window
|
---|
300 | sampledvalues_t sampled_grid;
|
---|
301 |
|
---|
302 | //!> start of the window relative to SamplingGridProperties::begin and SamplingGridProperties::size
|
---|
303 | double begin_window[3];
|
---|
304 | //!> end of the window relative to SamplingGridProperties::begin and SamplingGridProperties::size
|
---|
305 | double end_window[3];
|
---|
306 |
|
---|
307 | private:
|
---|
308 | friend class MPQCData;
|
---|
309 |
|
---|
310 | friend class boost::serialization::access;
|
---|
311 | // serialization
|
---|
312 | template <typename Archive>
|
---|
313 | void serialize(Archive& ar, const unsigned int version)
|
---|
314 | {
|
---|
315 | ar & boost::serialization::base_object<SamplingGridProperties>(*this);
|
---|
316 | ar & const_cast< sampledvalues_t &>(sampled_grid);
|
---|
317 | for(size_t i=0;i<3;++i) {
|
---|
318 | ar & begin_window[i];
|
---|
319 | ar & end_window[i];
|
---|
320 | }
|
---|
321 | }
|
---|
322 |
|
---|
323 | //!> static typedef to use in cstor when no initial values are given
|
---|
324 | static const double zeroOffset[3];
|
---|
325 | };
|
---|
326 |
|
---|
327 | /** Output operator for class SamplingGrid.
|
---|
328 | *
|
---|
329 | * \param ost output stream to print to
|
---|
330 | * \param other instance to print
|
---|
331 | * \return ref to stream for concatenation
|
---|
332 | */
|
---|
333 | std::ostream & operator<<(std::ostream &ost, const SamplingGrid& other);
|
---|
334 |
|
---|
335 | template<typename T> T ZeroInstance();
|
---|
336 | template<> SamplingGrid ZeroInstance<SamplingGrid>();
|
---|
337 |
|
---|
338 | // we need to give this class a unique key for serialization
|
---|
339 | // its is only serialized through its base class FragmentJob
|
---|
340 | BOOST_CLASS_EXPORT_KEY(SamplingGrid)
|
---|
341 |
|
---|
342 | // define inline functions
|
---|
343 | #include "SamplingGrid_inline.hpp"
|
---|
344 |
|
---|
345 | #endif /* SAMPLINGGRID_HPP_ */
|
---|