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