[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 |
|
---|
[c889b7] | 16 | #include <iosfwd>
|
---|
[28c025] | 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;
|
---|
[c889b7] | 25 | class SamplingGridTest;
|
---|
[28c025] | 26 |
|
---|
| 27 | /** This class stores a sample function on a three-dimensional grid.
|
---|
[3d9a8d] | 28 | *
|
---|
| 29 | * \note We do not use boost::multi_array because it is not trivial to serialize.
|
---|
[28c025] | 30 | *
|
---|
| 31 | */
|
---|
[c889b7] | 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);
|
---|
[28c025] | 38 | public:
|
---|
[c889b7] | 39 | //!> typedef for sampled values
|
---|
| 40 | typedef std::vector< double > sampledvalues_t;
|
---|
| 41 |
|
---|
[c6355f] | 42 | /** Constructor for class SamplingGrid for full window.
|
---|
| 43 | *
|
---|
| 44 | * Here, the window of sampled values spans the given domain.
|
---|
[28c025] | 45 | *
|
---|
[3d9a8d] | 46 | * \param _begin offset for grid per axis
|
---|
| 47 | * \param _end edge length of grid per axis
|
---|
| 48 | * \param _level number of grid points in \f$2^{\mathrm{level}}\f$
|
---|
[28c025] | 49 | * \param _sampled_grid sample points
|
---|
| 50 | */
|
---|
| 51 | SamplingGrid(const double _begin[3],
|
---|
[3d9a8d] | 52 | const double _end[3],
|
---|
[28c025] | 53 | const int _level,
|
---|
[c889b7] | 54 | const sampledvalues_t &_sampled_grid);
|
---|
[28c025] | 55 |
|
---|
[c6355f] | 56 | /** Constructor for class SamplingGrid for empty window.
|
---|
[28c025] | 57 | *
|
---|
[c6355f] | 58 | * Here, the window is initially of size zero.
|
---|
| 59 | *
|
---|
| 60 | * \param _begin offset for grid per axis
|
---|
| 61 | * \param _end edge length of grid per axis
|
---|
| 62 | * \param _level number of grid points in \f$2^{\text{level}}\f$
|
---|
[28c025] | 63 | */
|
---|
[c6355f] | 64 | SamplingGrid(const double _begin[3],
|
---|
| 65 | const double _end[3],
|
---|
| 66 | const int _level);
|
---|
[28c025] | 67 |
|
---|
[c6355f] | 68 | /** Copy constructor for class SamplingGrid with full window from SamplingGridProperties.
|
---|
| 69 | *
|
---|
| 70 | * Here, the window is initially empty.
|
---|
[28c025] | 71 | *
|
---|
| 72 | * \param _props properties to copy
|
---|
| 73 | */
|
---|
| 74 | SamplingGrid(const SamplingGridProperties &_props);
|
---|
| 75 |
|
---|
[c6355f] | 76 | /** Copy constructor for class SamplingGrid with empty window from SamplingGridProperties.
|
---|
| 77 | *
|
---|
| 78 | * Here, the window must span the whole domain
|
---|
[c889b7] | 79 | *
|
---|
| 80 | * \param _props properties to copy
|
---|
| 81 | * \param _sampled_grid sample points
|
---|
| 82 | */
|
---|
| 83 | SamplingGrid(
|
---|
| 84 | const SamplingGridProperties &_props,
|
---|
| 85 | const sampledvalues_t &_sampled_grid);
|
---|
| 86 |
|
---|
[c6355f] | 87 | /** Copy constructor for class SamplingGrid.
|
---|
| 88 | *
|
---|
| 89 | * The window of sampled values corresponds to the one on \a _grid.
|
---|
| 90 | *
|
---|
| 91 | * \param _grid grid to copy
|
---|
| 92 | */
|
---|
| 93 | SamplingGrid(const SamplingGrid &_grid);
|
---|
| 94 |
|
---|
[28c025] | 95 | /** default cstor.
|
---|
| 96 | */
|
---|
[1a00bb] | 97 | SamplingGrid();
|
---|
[28c025] | 98 |
|
---|
| 99 | virtual ~SamplingGrid();
|
---|
| 100 |
|
---|
[c889b7] | 101 | /** Assignment operator.
|
---|
| 102 | *
|
---|
| 103 | * \param other other instance to assign ourselves to
|
---|
| 104 | */
|
---|
| 105 | SamplingGrid& operator=(const SamplingGrid& other);
|
---|
| 106 |
|
---|
| 107 | /** Addition operator with another SamplingGrid instance \a other.
|
---|
| 108 | *
|
---|
| 109 | * \param other other instance to sum onto this one.
|
---|
| 110 | * \return ref to this instance
|
---|
| 111 | */
|
---|
| 112 | SamplingGrid& operator+=(const SamplingGrid& other)
|
---|
| 113 | {
|
---|
| 114 | superposeOtherGrids(other, +1.);
|
---|
| 115 | return *this;
|
---|
| 116 | }
|
---|
| 117 |
|
---|
[a1fcc6] | 118 | /** Element-wise multiplication 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 |
|
---|
[c889b7] | 125 | /** Subtraction operator with another SamplingGrid instance \a other.
|
---|
| 126 | *
|
---|
| 127 | * \param other other instance to subtract from this one.
|
---|
| 128 | * \return ref to this instance
|
---|
| 129 | */
|
---|
| 130 | SamplingGrid& operator-=(const SamplingGrid& other)
|
---|
| 131 | {
|
---|
| 132 | superposeOtherGrids(other, -1.);
|
---|
| 133 | return *this;
|
---|
| 134 | }
|
---|
| 135 |
|
---|
[cb3363] | 136 | /** Returns the numeric integral over the grid.
|
---|
| 137 | *
|
---|
| 138 | * @return sum of grid values times volume element
|
---|
| 139 | */
|
---|
| 140 | double integral() const;
|
---|
| 141 |
|
---|
[e72c61] | 142 | /** Returns the numeric integral over the grid where the grid is element-wise multiplied with \a weight.
|
---|
| 143 | *
|
---|
| 144 | * @param weight grid of weights
|
---|
| 145 | * @return sum of grid values weighted by respective element in weight times volume element
|
---|
| 146 | */
|
---|
| 147 | double integral(const SamplingGrid &weight) const;
|
---|
| 148 |
|
---|
[3d9a8d] | 149 | /** Returns the volume of the domain for this sampled function.
|
---|
| 150 | *
|
---|
| 151 | * @return volume
|
---|
| 152 | */
|
---|
[1a00bb] | 153 | const double getVolume() const;
|
---|
[3d9a8d] | 154 |
|
---|
[98f8fe] | 155 | /** Returns the total number of gridpoints of the discrete mesh covering the (window) volume.
|
---|
| 156 | *
|
---|
| 157 | * @return number of gridpoints sampled_values should have
|
---|
| 158 | */
|
---|
[1a00bb] | 159 | const size_t getWindowGridPoints() const;
|
---|
| 160 |
|
---|
| 161 | /** Returns the total number of gridpoints of the discrete mesh covering the volume.
|
---|
| 162 | *
|
---|
| 163 | * @return number of gridpoints sampled_values should have
|
---|
| 164 | */
|
---|
[98f8fe] | 165 | const size_t getTotalGridPoints() const;
|
---|
| 166 |
|
---|
| 167 | /** Returns the number of grid points per axis.
|
---|
| 168 | *
|
---|
| 169 | * @return number of grid points per unit length
|
---|
| 170 | */
|
---|
| 171 | const size_t getGridPointsPerAxis() const;
|
---|
| 172 |
|
---|
[1a00bb] | 173 | /** Returns the volume of the domain covered by the current window.
|
---|
| 174 | *
|
---|
| 175 | * @return volume of window
|
---|
| 176 | */
|
---|
| 177 | const double getWindowVolume() const;
|
---|
| 178 |
|
---|
[e2404f] | 179 | private:
|
---|
[1a00bb] | 180 | /** Sets the size of the window.
|
---|
| 181 | *
|
---|
| 182 | * \note also resets the sampled points so far.
|
---|
| 183 | *
|
---|
| 184 | * \param _begin_window start of new window
|
---|
| 185 | * \param _end_window end of window
|
---|
| 186 | */
|
---|
| 187 | void setWindow(const double _begin_window[3], const double _end_window[3]);
|
---|
| 188 |
|
---|
[e2404f] | 189 | /** Sets the size of the domain.
|
---|
| 190 | *
|
---|
| 191 | * \note also resets the sampled points so far and the window.
|
---|
| 192 | *
|
---|
| 193 | * \param _begin start of new window
|
---|
| 194 | * \param _end end of window
|
---|
| 195 | */
|
---|
| 196 | void setDomain(const double _begin[3], const double _end[3]);
|
---|
| 197 |
|
---|
| 198 | /** Sets the size of the domain.
|
---|
| 199 | *
|
---|
| 200 | * \note this is just internally used for easing the array setting.
|
---|
| 201 | *
|
---|
| 202 | * \param _begin start of domain
|
---|
| 203 | * \param _end end of domain
|
---|
| 204 | */
|
---|
| 205 | void setDomainSize(const double _begin[3], const double _end[3]);
|
---|
| 206 |
|
---|
[1a00bb] | 207 | /** Extends the window while keeping the values.
|
---|
| 208 | *
|
---|
| 209 | * \param _begin_window new start of window
|
---|
| 210 | * \param _end_window new end of window
|
---|
| 211 | */
|
---|
| 212 | void extendWindow(const double _begin_window[3], const double _end_window[3]);
|
---|
| 213 |
|
---|
| 214 | /** Adds another window onto the one in this instance.
|
---|
| 215 | *
|
---|
| 216 | * \note We assume here that the given window fits on the this one.
|
---|
| 217 | *
|
---|
| 218 | * \param _begin_window start of other window
|
---|
| 219 | * \param _end_window end of other window
|
---|
| 220 | * \param _sampled_grid other set of sampled values
|
---|
[de6dfb] | 221 | * @param prefactor +1. is then addition, -1. is subtraction.
|
---|
[1a00bb] | 222 | */
|
---|
| 223 | void addOntoWindow(
|
---|
| 224 | const double _begin_window[3],
|
---|
| 225 | const double _end_window[3],
|
---|
[de6dfb] | 226 | const sampledvalues_t &_sampled_grid,
|
---|
| 227 | const double prefactor);
|
---|
[98f8fe] | 228 |
|
---|
[c889b7] | 229 | /** Helper function that contains all the logic of how to superpose two
|
---|
| 230 | * grids.
|
---|
| 231 | *
|
---|
| 232 | * Is called by SamplingGrid::operator+=() and SamplingGrid::operator-=()
|
---|
| 233 | *
|
---|
| 234 | * @param other other histogram
|
---|
| 235 | * @param prefactor +1. is then addition, -1. is subtraction.
|
---|
| 236 | */
|
---|
| 237 | void superposeOtherGrids(const SamplingGrid &other, const double prefactor);
|
---|
| 238 |
|
---|
[1a00bb] | 239 | /** Sets the size of the window.
|
---|
| 240 | *
|
---|
| 241 | * \note also resets the sampled points so far.
|
---|
| 242 | *
|
---|
| 243 | * \param _begin_window start of new window
|
---|
| 244 | * \param _end_window end of window
|
---|
| 245 | */
|
---|
| 246 | void setWindowSize(const double _begin_window[3], const double _end_window[3]);
|
---|
| 247 |
|
---|
[64bafe0] | 248 | /** Helper function to get point at grid point for given \a axis and less than value.
|
---|
| 249 | *
|
---|
| 250 | * @param value value to find nearest grid point to
|
---|
| 251 | * @param axis axis of the value
|
---|
| 252 | * @return nearest lower grid point
|
---|
| 253 | */
|
---|
| 254 | double getNearestLowerGridPoint(
|
---|
| 255 | const double value, const size_t axis) const;
|
---|
| 256 |
|
---|
| 257 | /** Helper function to get point at grid point for given \a axis and greater than value.
|
---|
| 258 | *
|
---|
| 259 | * @param value value to find nearest grid point to
|
---|
| 260 | * @param axis axis of the value
|
---|
| 261 | * @return nearest lower grid point
|
---|
| 262 | */
|
---|
| 263 | double getNearestHigherGridPoint(
|
---|
| 264 | const double value, const size_t axis) const;
|
---|
| 265 |
|
---|
[28c025] | 266 | public:
|
---|
[1a00bb] | 267 | /// We do not store the whole grid if many entries are actually zero
|
---|
| 268 | /// but only a window wherein the sampled function is non-zero.
|
---|
| 269 |
|
---|
| 270 | //!> sample points of the window
|
---|
[c889b7] | 271 | sampledvalues_t sampled_grid;
|
---|
[28c025] | 272 |
|
---|
[1a00bb] | 273 | //!> start of the window relative to SamplingGridProperties::begin and SamplingGridProperties::size
|
---|
| 274 | double begin_window[3];
|
---|
| 275 | //!> end of the window relative to SamplingGridProperties::begin and SamplingGridProperties::size
|
---|
| 276 | double end_window[3];
|
---|
| 277 |
|
---|
[28c025] | 278 | private:
|
---|
| 279 | friend class MPQCData;
|
---|
| 280 |
|
---|
| 281 | friend class boost::serialization::access;
|
---|
| 282 | // serialization
|
---|
| 283 | template <typename Archive>
|
---|
| 284 | void serialize(Archive& ar, const unsigned int version)
|
---|
| 285 | {
|
---|
| 286 | ar & boost::serialization::base_object<SamplingGridProperties>(*this);
|
---|
[c889b7] | 287 | ar & const_cast< sampledvalues_t &>(sampled_grid);
|
---|
[1a00bb] | 288 | for(size_t i=0;i<3;++i) {
|
---|
| 289 | ar & begin_window[i];
|
---|
| 290 | ar & end_window[i];
|
---|
| 291 | }
|
---|
[28c025] | 292 | }
|
---|
[1a00bb] | 293 |
|
---|
| 294 | //!> static typedef to use in cstor when no initial values are given
|
---|
| 295 | static const double zeroOffset[3];
|
---|
[28c025] | 296 | };
|
---|
| 297 |
|
---|
[c889b7] | 298 | /** Output operator for class SamplingGrid.
|
---|
| 299 | *
|
---|
| 300 | * \param ost output stream to print to
|
---|
| 301 | * \param other instance to print
|
---|
| 302 | * \return ref to stream for concatenation
|
---|
| 303 | */
|
---|
| 304 | std::ostream & operator<<(std::ostream &ost, const SamplingGrid& other);
|
---|
| 305 |
|
---|
[beb16e] | 306 | template<typename T> T ZeroInstance();
|
---|
| 307 | template<> SamplingGrid ZeroInstance<SamplingGrid>();
|
---|
| 308 |
|
---|
[28c025] | 309 | // we need to give this class a unique key for serialization
|
---|
| 310 | // its is only serialized through its base class FragmentJob
|
---|
| 311 | BOOST_CLASS_EXPORT_KEY(SamplingGrid)
|
---|
| 312 |
|
---|
| 313 | #endif /* SAMPLINGGRID_HPP_ */
|
---|