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.
|
---|
43 | *
|
---|
44 | * \param _begin offset for grid per axis
|
---|
45 | * \param _end edge length of grid per axis
|
---|
46 | * \param _level number of grid points in \f$2^{\mathrm{level}}\f$
|
---|
47 | * \param _sampled_grid sample points
|
---|
48 | */
|
---|
49 | SamplingGrid(const double _begin[3],
|
---|
50 | const double _end[3],
|
---|
51 | const int _level,
|
---|
52 | const sampledvalues_t &_sampled_grid);
|
---|
53 |
|
---|
54 | /** Copy constructor for class SamplingGrid.
|
---|
55 | *
|
---|
56 | * \param _grid grid to copy
|
---|
57 | */
|
---|
58 | SamplingGrid(const SamplingGrid &_grid);
|
---|
59 |
|
---|
60 | /** Copy constructor for class SamplingGrid from SamplingGridProperties.
|
---|
61 | *
|
---|
62 | * \param _props properties to copy
|
---|
63 | */
|
---|
64 | SamplingGrid(const SamplingGridProperties &_props);
|
---|
65 |
|
---|
66 | /** Copy constructor for class SamplingGrid from SamplingGridProperties.
|
---|
67 | *
|
---|
68 | * \param _props properties to copy
|
---|
69 | * \param _sampled_grid sample points
|
---|
70 | */
|
---|
71 | SamplingGrid(
|
---|
72 | const SamplingGridProperties &_props,
|
---|
73 | const sampledvalues_t &_sampled_grid);
|
---|
74 |
|
---|
75 | /** default cstor.
|
---|
76 | */
|
---|
77 | SamplingGrid();
|
---|
78 |
|
---|
79 | virtual ~SamplingGrid();
|
---|
80 |
|
---|
81 | /** Assignment operator.
|
---|
82 | *
|
---|
83 | * \param other other instance to assign ourselves to
|
---|
84 | */
|
---|
85 | SamplingGrid& operator=(const SamplingGrid& other);
|
---|
86 |
|
---|
87 | /** Addition operator with another SamplingGrid instance \a other.
|
---|
88 | *
|
---|
89 | * \param other other instance to sum onto this one.
|
---|
90 | * \return ref to this instance
|
---|
91 | */
|
---|
92 | SamplingGrid& operator+=(const SamplingGrid& other)
|
---|
93 | {
|
---|
94 | superposeOtherGrids(other, +1.);
|
---|
95 | return *this;
|
---|
96 | }
|
---|
97 |
|
---|
98 | /** Element-wise multiplication operator with another SamplingGrid instance \a other.
|
---|
99 | *
|
---|
100 | * \param other other instance to sum onto this one.
|
---|
101 | * \return ref to this instance
|
---|
102 | */
|
---|
103 | SamplingGrid& operator*=(const SamplingGrid& other);
|
---|
104 |
|
---|
105 | /** Subtraction operator with another SamplingGrid instance \a other.
|
---|
106 | *
|
---|
107 | * \param other other instance to subtract from this one.
|
---|
108 | * \return ref to this instance
|
---|
109 | */
|
---|
110 | SamplingGrid& operator-=(const SamplingGrid& other)
|
---|
111 | {
|
---|
112 | superposeOtherGrids(other, -1.);
|
---|
113 | return *this;
|
---|
114 | }
|
---|
115 |
|
---|
116 | /** Returns the numeric integral over the grid.
|
---|
117 | *
|
---|
118 | * @return sum of grid values times volume element
|
---|
119 | */
|
---|
120 | double integral() const;
|
---|
121 |
|
---|
122 | /** Returns the numeric integral over the grid where the grid is element-wise multiplied with \a weight.
|
---|
123 | *
|
---|
124 | * @param weight grid of weights
|
---|
125 | * @return sum of grid values weighted by respective element in weight times volume element
|
---|
126 | */
|
---|
127 | double integral(const SamplingGrid &weight) const;
|
---|
128 |
|
---|
129 | /** Returns the volume of the domain for this sampled function.
|
---|
130 | *
|
---|
131 | * @return volume
|
---|
132 | */
|
---|
133 | const double getVolume() const;
|
---|
134 |
|
---|
135 | /** Returns the total number of gridpoints of the discrete mesh covering the (window) volume.
|
---|
136 | *
|
---|
137 | * @return number of gridpoints sampled_values should have
|
---|
138 | */
|
---|
139 | const size_t getWindowGridPoints() const;
|
---|
140 |
|
---|
141 | /** Returns the total number of gridpoints of the discrete mesh covering the volume.
|
---|
142 | *
|
---|
143 | * @return number of gridpoints sampled_values should have
|
---|
144 | */
|
---|
145 | const size_t getTotalGridPoints() const;
|
---|
146 |
|
---|
147 | /** Returns the number of grid points per axis.
|
---|
148 | *
|
---|
149 | * @return number of grid points per unit length
|
---|
150 | */
|
---|
151 | const size_t getGridPointsPerAxis() const;
|
---|
152 |
|
---|
153 | /** Returns the volume of the domain covered by the current window.
|
---|
154 | *
|
---|
155 | * @return volume of window
|
---|
156 | */
|
---|
157 | const double getWindowVolume() const;
|
---|
158 |
|
---|
159 | /** Sets the size of the window.
|
---|
160 | *
|
---|
161 | * \note also resets the sampled points so far.
|
---|
162 | *
|
---|
163 | * \param _begin_window start of new window
|
---|
164 | * \param _end_window end of window
|
---|
165 | */
|
---|
166 | void setWindow(const double _begin_window[3], const double _end_window[3]);
|
---|
167 |
|
---|
168 | private:
|
---|
169 | /** Extends the window while keeping the values.
|
---|
170 | *
|
---|
171 | * \param _begin_window new start of window
|
---|
172 | * \param _end_window new end of window
|
---|
173 | */
|
---|
174 | void extendWindow(const double _begin_window[3], const double _end_window[3]);
|
---|
175 |
|
---|
176 | /** Adds another window onto the one in this instance.
|
---|
177 | *
|
---|
178 | * \note We assume here that the given window fits on the this one.
|
---|
179 | *
|
---|
180 | * \param _begin_window start of other window
|
---|
181 | * \param _end_window end of other window
|
---|
182 | * \param _sampled_grid other set of sampled values
|
---|
183 | */
|
---|
184 | void addOntoWindow(
|
---|
185 | const double _begin_window[3],
|
---|
186 | const double _end_window[3],
|
---|
187 | const sampledvalues_t &_sampled_grid);
|
---|
188 |
|
---|
189 | /** Helper function that contains all the logic of how to superpose two
|
---|
190 | * grids.
|
---|
191 | *
|
---|
192 | * Is called by SamplingGrid::operator+=() and SamplingGrid::operator-=()
|
---|
193 | *
|
---|
194 | * @param other other histogram
|
---|
195 | * @param prefactor +1. is then addition, -1. is subtraction.
|
---|
196 | */
|
---|
197 | void superposeOtherGrids(const SamplingGrid &other, const double prefactor);
|
---|
198 |
|
---|
199 | /** Sets the size of the window.
|
---|
200 | *
|
---|
201 | * \note also resets the sampled points so far.
|
---|
202 | *
|
---|
203 | * \param _begin_window start of new window
|
---|
204 | * \param _size size of the window
|
---|
205 | */
|
---|
206 | void setWindowSize(const double _begin_window[3], const double _size);
|
---|
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 setWindowSize(const double _begin_window[3], const double _end_window[3]);
|
---|
216 |
|
---|
217 | public:
|
---|
218 | /// We do not store the whole grid if many entries are actually zero
|
---|
219 | /// but only a window wherein the sampled function is non-zero.
|
---|
220 |
|
---|
221 | //!> sample points of the window
|
---|
222 | sampledvalues_t sampled_grid;
|
---|
223 |
|
---|
224 | //!> start of the window relative to SamplingGridProperties::begin and SamplingGridProperties::size
|
---|
225 | double begin_window[3];
|
---|
226 | //!> end of the window relative to SamplingGridProperties::begin and SamplingGridProperties::size
|
---|
227 | double end_window[3];
|
---|
228 |
|
---|
229 | private:
|
---|
230 | friend class MPQCData;
|
---|
231 |
|
---|
232 | friend class boost::serialization::access;
|
---|
233 | // serialization
|
---|
234 | template <typename Archive>
|
---|
235 | void serialize(Archive& ar, const unsigned int version)
|
---|
236 | {
|
---|
237 | ar & boost::serialization::base_object<SamplingGridProperties>(*this);
|
---|
238 | ar & const_cast< sampledvalues_t &>(sampled_grid);
|
---|
239 | for(size_t i=0;i<3;++i) {
|
---|
240 | ar & begin_window[i];
|
---|
241 | ar & end_window[i];
|
---|
242 | }
|
---|
243 | }
|
---|
244 |
|
---|
245 | //!> static typedef to use in cstor when no initial values are given
|
---|
246 | static const double zeroOffset[3];
|
---|
247 | };
|
---|
248 |
|
---|
249 | /** Output operator for class SamplingGrid.
|
---|
250 | *
|
---|
251 | * \param ost output stream to print to
|
---|
252 | * \param other instance to print
|
---|
253 | * \return ref to stream for concatenation
|
---|
254 | */
|
---|
255 | std::ostream & operator<<(std::ostream &ost, const SamplingGrid& other);
|
---|
256 |
|
---|
257 | template<typename T> T ZeroInstance();
|
---|
258 | template<> SamplingGrid ZeroInstance<SamplingGrid>();
|
---|
259 |
|
---|
260 | // we need to give this class a unique key for serialization
|
---|
261 | // its is only serialized through its base class FragmentJob
|
---|
262 | BOOST_CLASS_EXPORT_KEY(SamplingGrid)
|
---|
263 |
|
---|
264 | #endif /* SAMPLINGGRID_HPP_ */
|
---|