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