| 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 "Fragmentation/Summation/SetValues/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 total number of gridpoints of the discrete mesh covering the (window) volume.
 | 
|---|
| 166 |    *
 | 
|---|
| 167 |    * @return number of gridpoints sampled_values should have
 | 
|---|
| 168 |    */
 | 
|---|
| 169 |   const size_t getWindowGridPoints() const;
 | 
|---|
| 170 | 
 | 
|---|
| 171 |   /** Returns the number of gridpoints of the discrete mesh for the current
 | 
|---|
| 172 |    * window size for given axis \axis.
 | 
|---|
| 173 |    *
 | 
|---|
| 174 |    * \param axis axis to calculate number of gridpoints for
 | 
|---|
| 175 |    * \return number of gridpoints along this axis
 | 
|---|
| 176 |    */
 | 
|---|
| 177 |   const size_t getWindowGridPointsPerAxis(const size_t axis) const;
 | 
|---|
| 178 | 
 | 
|---|
| 179 |   /** Returns the length of the window for the given \a axis.
 | 
|---|
| 180 |    *
 | 
|---|
| 181 |    * \param axis axis for which to get step length
 | 
|---|
| 182 |    * \return window length for the given axis, i.e. end - begin
 | 
|---|
| 183 |    */
 | 
|---|
| 184 |   const double getWindowLengthPerAxis(const size_t axis) const;
 | 
|---|
| 185 | 
 | 
|---|
| 186 |   /** Returns the volume of the domain covered by the current window.
 | 
|---|
| 187 |    *
 | 
|---|
| 188 |    * @return volume of window
 | 
|---|
| 189 |    */
 | 
|---|
| 190 |   const double getWindowVolume() const;
 | 
|---|
| 191 | 
 | 
|---|
| 192 |   /** Sets the size of the window.
 | 
|---|
| 193 |    *
 | 
|---|
| 194 |    * \note also resets the sampled points so far.
 | 
|---|
| 195 |    *
 | 
|---|
| 196 |    * \param _begin_window start of new window
 | 
|---|
| 197 |    * \param _end_window end of window
 | 
|---|
| 198 |    */
 | 
|---|
| 199 |   void setWindow(const double _begin_window[3], const double _end_window[3]);
 | 
|---|
| 200 | 
 | 
|---|
| 201 |   /** Helper function to convert begin_window and end_window that are w.r.t.
 | 
|---|
| 202 |    * to domain [begin:end] to indices that can be used when traversing the grid.
 | 
|---|
| 203 |    *
 | 
|---|
| 204 |    * \param larger_wbegin begin of domain
 | 
|---|
| 205 |    * \param larger_wend end of domain
 | 
|---|
| 206 |    * \param smaller_wbegin begin of window
 | 
|---|
| 207 |    * \param smaller_wend end of window
 | 
|---|
| 208 |    * \param pre_offset discrete length from 0 to start of window
 | 
|---|
| 209 |    * \param post_offset discrete length from end of window to end
 | 
|---|
| 210 |    * \param length discrete length of window
 | 
|---|
| 211 |    * \param total total number of points for checking, should be sum of other three
 | 
|---|
| 212 |    */
 | 
|---|
| 213 |   void getDiscreteWindowIndices(
 | 
|---|
| 214 |       const double *larger_wbegin,
 | 
|---|
| 215 |       const double *larger_wend,
 | 
|---|
| 216 |       const double *smaller_wbegin,
 | 
|---|
| 217 |       const double *smaller_wend,
 | 
|---|
| 218 |       size_t *pre_offset,
 | 
|---|
| 219 |       size_t *post_offset,
 | 
|---|
| 220 |       size_t *length,
 | 
|---|
| 221 |       size_t *total) const;
 | 
|---|
| 222 | 
 | 
|---|
| 223 | private:
 | 
|---|
| 224 |   /** Sets the size of the domain.
 | 
|---|
| 225 |    *
 | 
|---|
| 226 |    * \note also resets the sampled points so far and the window.
 | 
|---|
| 227 |    *
 | 
|---|
| 228 |    * \param _begin start of new window
 | 
|---|
| 229 |    * \param _end end of window
 | 
|---|
| 230 |    */
 | 
|---|
| 231 |   void setDomain(const double _begin[3], const double _end[3]);
 | 
|---|
| 232 | 
 | 
|---|
| 233 |   /** Sets the size of the domain.
 | 
|---|
| 234 |    *
 | 
|---|
| 235 |    * \note this is just internally used for easing the array setting.
 | 
|---|
| 236 |    *
 | 
|---|
| 237 |    * \param _begin start of domain
 | 
|---|
| 238 |    * \param _end end of domain
 | 
|---|
| 239 |    */
 | 
|---|
| 240 |   void setDomainSize(const double _begin[3], const double _end[3]);
 | 
|---|
| 241 | 
 | 
|---|
| 242 |   /** Extends the window while keeping the values.
 | 
|---|
| 243 |    *
 | 
|---|
| 244 |    * \param _begin_window new start of window
 | 
|---|
| 245 |    * \param _end_window new end of window
 | 
|---|
| 246 |    */
 | 
|---|
| 247 |   void extendWindow(const double _begin_window[3], const double _end_window[3]);
 | 
|---|
| 248 | 
 | 
|---|
| 249 |   /** Shrinks the window while keeping the values.
 | 
|---|
| 250 |    *
 | 
|---|
| 251 |    * \param _begin_window new start of window
 | 
|---|
| 252 |    * \param _end_window new end of window
 | 
|---|
| 253 |    */
 | 
|---|
| 254 |   void shrinkWindow(const double _begin_window[3], const double _end_window[3]);
 | 
|---|
| 255 | 
 | 
|---|
| 256 |   /** Adds another (smaller) window onto the one in this instance.
 | 
|---|
| 257 |    *
 | 
|---|
| 258 |    * \note We assume here that the given window fits on the this one.
 | 
|---|
| 259 |    *
 | 
|---|
| 260 |    * \param _begin_window start of other window
 | 
|---|
| 261 |    * \param _end_window end of other window
 | 
|---|
| 262 |    * \param _sampled_grid other set of sampled values
 | 
|---|
| 263 |    * @param prefactor +1. is then addition, -1. is subtraction.
 | 
|---|
| 264 |    */
 | 
|---|
| 265 |   void addOntoWindow(
 | 
|---|
| 266 |       const double _begin_window[3],
 | 
|---|
| 267 |       const double _end_window[3],
 | 
|---|
| 268 |       const sampledvalues_t &_sampled_grid,
 | 
|---|
| 269 |       const double prefactor);
 | 
|---|
| 270 | 
 | 
|---|
| 271 |   /** Adds another (larger) window into the one in this instance.
 | 
|---|
| 272 |    *
 | 
|---|
| 273 |    * \note We assume here that the given window is larger than this one.
 | 
|---|
| 274 |    *
 | 
|---|
| 275 |    * \param _begin_window start of other window
 | 
|---|
| 276 |    * \param _end_window end of other window
 | 
|---|
| 277 |    * \param _sampled_grid other set of sampled values
 | 
|---|
| 278 |    * @param prefactor +1. is then addition, -1. is subtraction.
 | 
|---|
| 279 |    */
 | 
|---|
| 280 |   void addIntoWindow(
 | 
|---|
| 281 |       const double _begin_window[3],
 | 
|---|
| 282 |       const double _end_window[3],
 | 
|---|
| 283 |       const sampledvalues_t &_sampled_grid,
 | 
|---|
| 284 |       const double prefactor);
 | 
|---|
| 285 | 
 | 
|---|
| 286 |   /** Enum to help in addWindowOntoWindow() decide which iterator needs to be
 | 
|---|
| 287 |    * advanced.
 | 
|---|
| 288 |    */
 | 
|---|
| 289 |   enum eLargerWindow {
 | 
|---|
| 290 |     destwindow,
 | 
|---|
| 291 |     sourcewindow
 | 
|---|
| 292 |   };
 | 
|---|
| 293 | 
 | 
|---|
| 294 |   /** Helper function to copy one (larger) window into a (smaller) window.
 | 
|---|
| 295 |    *
 | 
|---|
| 296 |    * \note Why do we need the extra \a choice? We need to know which window
 | 
|---|
| 297 |    * tuples is associated with which sampled values that are constrained by
 | 
|---|
| 298 |    * one of them being constant, hence the source values
 | 
|---|
| 299 |    *
 | 
|---|
| 300 |    * \param larger_wbegin start of larger window
 | 
|---|
| 301 |    * \param larger_wend end of larger window
 | 
|---|
| 302 |    * \param smaller_wbegin start of smaller window
 | 
|---|
| 303 |    * \param smaller_wend end of smaller window
 | 
|---|
| 304 |    * \param dest_sampled_grid larger set of sampled values
 | 
|---|
| 305 |    * \param source_sampled_grid smaller set of sampled values
 | 
|---|
| 306 |    * \param op operation to perform with the two elements
 | 
|---|
| 307 |    * \param larger_window indicates which is the larger window
 | 
|---|
| 308 |    */
 | 
|---|
| 309 |   void addWindowOntoWindow(
 | 
|---|
| 310 |       const double larger_wbegin[3],
 | 
|---|
| 311 |       const double larger_wend[3],
 | 
|---|
| 312 |       const double smaller_wbegin[3],
 | 
|---|
| 313 |       const double smaller_wend[3],
 | 
|---|
| 314 |       sampledvalues_t &dest_sampled_grid,
 | 
|---|
| 315 |       const sampledvalues_t &source_sampled_grid,
 | 
|---|
| 316 |       boost::function<void (double &, const double &)> op,
 | 
|---|
| 317 |       enum eLargerWindow larger_window);
 | 
|---|
| 318 | 
 | 
|---|
| 319 |   /** Helper function that contains all the logic of how to superpose two
 | 
|---|
| 320 |    * grids.
 | 
|---|
| 321 |    *
 | 
|---|
| 322 |    * Is called by SamplingGrid::operator+=() and SamplingGrid::operator-=()
 | 
|---|
| 323 |    *
 | 
|---|
| 324 |    * @param other other histogram
 | 
|---|
| 325 |    * @param prefactor +1. is then addition, -1. is subtraction.
 | 
|---|
| 326 |    */
 | 
|---|
| 327 |   void superposeOtherGrids(const SamplingGrid &other, const double prefactor);
 | 
|---|
| 328 | 
 | 
|---|
| 329 |   /** Sets the size of the window.
 | 
|---|
| 330 |    *
 | 
|---|
| 331 |    * \note also resets the sampled points so far.
 | 
|---|
| 332 |    *
 | 
|---|
| 333 |    * \param _begin_window start of new window
 | 
|---|
| 334 |    * \param _end_window end of window
 | 
|---|
| 335 |    */
 | 
|---|
| 336 |   void setWindowSize(const double _begin_window[3], const double _end_window[3]);
 | 
|---|
| 337 | 
 | 
|---|
| 338 |   /** Helper function to get point at grid point for given \a axis and less than value.
 | 
|---|
| 339 |    *
 | 
|---|
| 340 |    * @param value value to find nearest grid point to
 | 
|---|
| 341 |    * @param axis axis of the value
 | 
|---|
| 342 |    * @return nearest lower grid point
 | 
|---|
| 343 |    */
 | 
|---|
| 344 |   double getNearestLowerGridPoint(
 | 
|---|
| 345 |       const double value, const size_t axis) const;
 | 
|---|
| 346 | 
 | 
|---|
| 347 |   /** Helper function to get point at grid point for given \a axis and greater than value.
 | 
|---|
| 348 |    *
 | 
|---|
| 349 |    * @param value value to find nearest grid point to
 | 
|---|
| 350 |    * @param axis axis of the value
 | 
|---|
| 351 |    * @return nearest lower grid point
 | 
|---|
| 352 |    */
 | 
|---|
| 353 |   double getNearestHigherGridPoint(
 | 
|---|
| 354 |       const double value, const size_t axis) const;
 | 
|---|
| 355 | 
 | 
|---|
| 356 | public:
 | 
|---|
| 357 |   /// We do not store the whole grid if many entries are actually zero
 | 
|---|
| 358 |   /// but only a window wherein the sampled function is non-zero.
 | 
|---|
| 359 | 
 | 
|---|
| 360 |   //!> sample points of the window
 | 
|---|
| 361 |   sampledvalues_t sampled_grid;
 | 
|---|
| 362 | 
 | 
|---|
| 363 |   //!> start of the window relative to SamplingGridProperties::begin and SamplingGridProperties::size
 | 
|---|
| 364 |   double begin_window[3];
 | 
|---|
| 365 |   //!> end of the window relative to SamplingGridProperties::begin and SamplingGridProperties::size
 | 
|---|
| 366 |   double end_window[3];
 | 
|---|
| 367 | 
 | 
|---|
| 368 | private:
 | 
|---|
| 369 |   friend class MPQCData;
 | 
|---|
| 370 | 
 | 
|---|
| 371 |   friend class boost::serialization::access;
 | 
|---|
| 372 |   // serialization
 | 
|---|
| 373 |   template <typename Archive>
 | 
|---|
| 374 |   void serialize(Archive& ar, const unsigned int version)
 | 
|---|
| 375 |   {
 | 
|---|
| 376 |     ar & boost::serialization::base_object<SamplingGridProperties>(*this);
 | 
|---|
| 377 |     ar & const_cast< sampledvalues_t &>(sampled_grid);
 | 
|---|
| 378 |     for(size_t i=0;i<3;++i) {
 | 
|---|
| 379 |       ar & begin_window[i];
 | 
|---|
| 380 |       ar & end_window[i];
 | 
|---|
| 381 |     }
 | 
|---|
| 382 |   }
 | 
|---|
| 383 | 
 | 
|---|
| 384 |   //!> static typedef to use in cstor when no initial values are given
 | 
|---|
| 385 |   static const double zeroOffset[3];
 | 
|---|
| 386 | };
 | 
|---|
| 387 | 
 | 
|---|
| 388 | /** Output operator for class SamplingGrid.
 | 
|---|
| 389 |  *
 | 
|---|
| 390 |  * \param ost output stream to print to
 | 
|---|
| 391 |  * \param other instance to print
 | 
|---|
| 392 |  * \return ref to stream for concatenation
 | 
|---|
| 393 |  */
 | 
|---|
| 394 | std::ostream & operator<<(std::ostream &ost, const SamplingGrid& other);
 | 
|---|
| 395 | 
 | 
|---|
| 396 | template<typename T> T ZeroInstance();
 | 
|---|
| 397 | template<> SamplingGrid ZeroInstance<SamplingGrid>();
 | 
|---|
| 398 | 
 | 
|---|
| 399 | // we need to give this class a unique key for serialization
 | 
|---|
| 400 | // its is only serialized through its base class FragmentJob
 | 
|---|
| 401 | BOOST_CLASS_EXPORT_KEY(SamplingGrid)
 | 
|---|
| 402 | 
 | 
|---|
| 403 | // define inline functions
 | 
|---|
| 404 | #include "SamplingGrid_inline.hpp"
 | 
|---|
| 405 | 
 | 
|---|
| 406 | #endif /* SAMPLINGGRID_HPP_ */
 | 
|---|