source: src/Fragmentation/Summation/SetValues/SamplingGrid.hpp@ d56e21

Action_Thermostats Add_AtomRandomPerturbation Add_FitFragmentPartialChargesAction Add_RotateAroundBondAction Add_SelectAtomByNameAction Adding_Graph_to_ChangeBondActions Adding_MD_integration_tests Adding_StructOpt_integration_tests Automaking_mpqc_open AutomationFragmentation_failures Candidate_v1.5.4 Candidate_v1.6.0 Candidate_v1.6.1 ChangeBugEmailaddress ChangingTestPorts ChemicalSpaceEvaluator Combining_Subpackages Debian_Package_split Debian_package_split_molecuildergui_only Disabling_MemDebug Docu_Python_wait EmpiricalPotential_contain_HomologyGraph EmpiricalPotential_contain_HomologyGraph_documentation Enable_parallel_make_install Enhance_userguide Enhanced_StructuralOptimization Enhanced_StructuralOptimization_continued Example_ManyWaysToTranslateAtom Exclude_Hydrogens_annealWithBondGraph FitPartialCharges_GlobalError Fix_ChargeSampling_PBC Fix_ChronosMutex Fix_FitPartialCharges Fix_FitPotential_needs_atomicnumbers Fix_ForceAnnealing Fix_IndependentFragmentGrids Fix_ParseParticles Fix_ParseParticles_split_forward_backward_Actions Fix_StatusMsg Fix_StepWorldTime_single_argument Fix_Verbose_Codepatterns ForceAnnealing_goodresults ForceAnnealing_oldresults ForceAnnealing_tocheck ForceAnnealing_with_BondGraph ForceAnnealing_with_BondGraph_continued ForceAnnealing_with_BondGraph_continued_betteresults ForceAnnealing_with_BondGraph_contraction-expansion GeometryObjects Gui_displays_atomic_force_velocity IndependentFragmentGrids IndependentFragmentGrids_IndividualZeroInstances IndependentFragmentGrids_IntegrationTest IndependentFragmentGrids_Sole_NN_Calculation JobMarket_RobustOnKillsSegFaults JobMarket_StableWorkerPool JobMarket_unresolvable_hostname_fix ODR_violation_mpqc_open PartialCharges_OrthogonalSummation PythonUI_with_named_parameters QtGui_reactivate_TimeChanged_changes Recreated_GuiChecks RotateToPrincipalAxisSystem_UndoRedo StoppableMakroAction Subpackage_CodePatterns Subpackage_JobMarket Subpackage_LinearAlgebra Subpackage_levmar Subpackage_mpqc_open Subpackage_vmg ThirdParty_MPQC_rebuilt_buildsystem TrajectoryDependenant_MaxOrder TremoloParser_IncreasedPrecision TremoloParser_MultipleTimesteps Ubuntu_1604_changes stable
Last change on this file since d56e21 was d56e21, checked in by Frederik Heber <heber@…>, 8 years ago

Added padWithZerosForEvenNumberedSamples(), required by downsample().

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