source: src/Fragmentation/Summation/SetValues/SamplingGrid.hpp@ 91e7658

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 91e7658 was 06653a, checked in by Frederik Heber <heber@…>, 9 years ago

SamplingGrid has downsample() function.

  • we need this when small fragment grids are calculated at a higher resolution than full_sample grid is supposed to be calculated at.
  • extended SamplingGridUnitTest to downsample() function, needed special attention on boundary of grid.
  • Property mode set to 100644
File size: 14.4 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 /** Sets the size of the domain.
292 *
293 * \note also resets the sampled points so far and the window.
294 *
295 * \param _begin start of new window
296 * \param _end end of window
297 */
298 void setDomain(const double _begin[NDIM], const double _end[NDIM]);
299
300 /** Sets the size of the domain.
301 *
302 * \note this is just internally used for easing the array setting.
303 *
304 * \param _begin start of domain
305 * \param _end end of domain
306 */
307 void setDomainSize(const double _begin[NDIM], const double _end[NDIM]);
308
309 /** Extends the window while keeping the values.
310 *
311 * \param _begin_window new start of window
312 * \param _end_window new end of window
313 */
314 void extendWindow(const double _begin_window[NDIM], const double _end_window[NDIM]);
315
316 /** Shrinks the window while keeping the values.
317 *
318 * \param _begin_window new start of window
319 * \param _end_window new end of window
320 */
321 void shrinkWindow(const double _begin_window[NDIM], const double _end_window[NDIM]);
322
323 /** Adds another (smaller) window onto the one in this instance.
324 *
325 * \note We assume here that the given window fits on the this one.
326 *
327 * \param _begin_window start of other window
328 * \param _end_window end of other window
329 * \param _sampled_grid other set of sampled values
330 * @param prefactor +1. is then addition, -1. is subtraction.
331 */
332 void addOntoWindow(
333 const double _begin_window[NDIM],
334 const double _end_window[NDIM],
335 const sampledvalues_t &_sampled_grid,
336 const double prefactor);
337
338 /** Adds another (larger) window into the one in this instance.
339 *
340 * \note We assume here that the given window is larger than this one.
341 *
342 * \param _begin_window start of other window
343 * \param _end_window end of other window
344 * \param _sampled_grid other set of sampled values
345 * @param prefactor +1. is then addition, -1. is subtraction.
346 */
347 void addIntoWindow(
348 const double _begin_window[NDIM],
349 const double _end_window[NDIM],
350 const sampledvalues_t &_sampled_grid,
351 const double prefactor);
352
353 /** Enum to help in addWindowOntoWindow() decide which iterator needs to be
354 * advanced.
355 */
356 enum eLargerWindow {
357 destwindow,
358 sourcewindow
359 };
360
361 /** Helper function to copy one (larger) window into a (smaller) window.
362 *
363 * \note Why do we need the extra \a choice? We need to know which window
364 * tuples is associated with which sampled values that are constrained by
365 * one of them being constant, hence the source values
366 *
367 * \param larger_wbegin start of larger window
368 * \param larger_wend end of larger window
369 * \param smaller_wbegin start of smaller window
370 * \param smaller_wend end of smaller window
371 * \param dest_sampled_grid larger set of sampled values
372 * \param source_sampled_grid smaller set of sampled values
373 * \param op operation to perform with the two elements
374 * \param larger_window indicates which is the larger window
375 */
376 void addWindowOntoWindow(
377 const double larger_wbegin[NDIM],
378 const double larger_wend[NDIM],
379 const double smaller_wbegin[NDIM],
380 const double smaller_wend[NDIM],
381 sampledvalues_t &dest_sampled_grid,
382 const sampledvalues_t &source_sampled_grid,
383 boost::function<void (double &, const double &)> op,
384 enum eLargerWindow larger_window);
385
386 /** Helper function that contains all the logic of how to superpose two
387 * grids.
388 *
389 * Is called by SamplingGrid::operator+=() and SamplingGrid::operator-=()
390 *
391 * @param other other histogram
392 * @param prefactor +1. is then addition, -1. is subtraction.
393 */
394 void superposeOtherGrids(const SamplingGrid &other, const double prefactor);
395
396 /** Sets the size of the window.
397 *
398 * \note also resets the sampled points so far.
399 *
400 * \param _begin_window start of new window
401 * \param _end_window end of window
402 */
403 void setWindowSize(const double _begin_window[NDIM], const double _end_window[NDIM]);
404
405public:
406 /// We do not store the whole grid if many entries are actually zero
407 /// but only a window wherein the sampled function is non-zero.
408
409 //!> sample points of the window
410 sampledvalues_t sampled_grid;
411
412 //!> start of the window relative to SamplingGridProperties::begin and SamplingGridProperties::size
413 double begin_window[NDIM];
414 //!> end of the window relative to SamplingGridProperties::begin and SamplingGridProperties::size
415 double end_window[NDIM];
416
417private:
418 friend class MPQCData;
419
420 friend class boost::serialization::access;
421 // serialization
422 template <typename Archive>
423 void serialize(Archive& ar, const unsigned int version)
424 {
425 ar & boost::serialization::base_object<SamplingGridProperties>(*this);
426 ar & const_cast< sampledvalues_t &>(sampled_grid);
427 for(size_t i=0;i<NDIM;++i) {
428 ar & begin_window[i];
429 ar & end_window[i];
430 }
431 }
432
433 //!> static typedef to use in cstor when no initial values are given
434 static const double zeroOffset[NDIM];
435};
436
437/** Output operator for class SamplingGrid.
438 *
439 * \param ost output stream to print to
440 * \param other instance to print
441 * \return ref to stream for concatenation
442 */
443std::ostream & operator<<(std::ostream &ost, const SamplingGrid& other);
444
445template<> SamplingGrid ZeroInstance<SamplingGrid>();
446
447// we need to give this class a unique key for serialization
448// its is only serialized through its base class FragmentJob
449BOOST_CLASS_EXPORT_KEY(SamplingGrid)
450
451// define inline functions
452#include "SamplingGrid_inline.hpp"
453
454#endif /* SAMPLINGGRID_HPP_ */
Note: See TracBrowser for help on using the repository browser.