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