source: src/Jobs/Grid/SamplingGrid.hpp@ fb3485

Action_Thermostats Add_AtomRandomPerturbation Add_FitFragmentPartialChargesAction Add_RotateAroundBondAction Add_SelectAtomByNameAction Added_ParseSaveFragmentResults AddingActions_SaveParseParticleParameters Adding_Graph_to_ChangeBondActions Adding_MD_integration_tests Adding_ParticleName_to_Atom Adding_StructOpt_integration_tests AtomFragments Automaking_mpqc_open AutomationFragmentation_failures Candidate_v1.5.4 Candidate_v1.6.0 Candidate_v1.6.1 ChangeBugEmailaddress ChangingTestPorts ChemicalSpaceEvaluator CombiningParticlePotentialParsing 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_BoundInBox_CenterInBox_MoleculeActions Fix_ChargeSampling_PBC Fix_ChronosMutex Fix_FitPartialCharges Fix_FitPotential_needs_atomicnumbers Fix_ForceAnnealing Fix_IndependentFragmentGrids Fix_ParseParticles Fix_ParseParticles_split_forward_backward_Actions Fix_PopActions Fix_QtFragmentList_sorted_selection Fix_Restrictedkeyset_FragmentMolecule Fix_StatusMsg Fix_StepWorldTime_single_argument Fix_Verbose_Codepatterns Fix_fitting_potentials Fixes ForceAnnealing_goodresults ForceAnnealing_oldresults ForceAnnealing_tocheck ForceAnnealing_with_BondGraph ForceAnnealing_with_BondGraph_continued ForceAnnealing_with_BondGraph_continued_betteresults ForceAnnealing_with_BondGraph_contraction-expansion FragmentAction_writes_AtomFragments FragmentMolecule_checks_bonddegrees GeometryObjects Gui_Fixes Gui_displays_atomic_force_velocity ImplicitCharges IndependentFragmentGrids IndependentFragmentGrids_IndividualZeroInstances IndependentFragmentGrids_IntegrationTest IndependentFragmentGrids_Sole_NN_Calculation JobMarket_RobustOnKillsSegFaults JobMarket_StableWorkerPool JobMarket_unresolvable_hostname_fix MoreRobust_FragmentAutomation ODR_violation_mpqc_open PartialCharges_OrthogonalSummation PdbParser_setsAtomName PythonUI_with_named_parameters QtGui_reactivate_TimeChanged_changes Recreated_GuiChecks Rewrite_FitPartialCharges RotateToPrincipalAxisSystem_UndoRedo SaturateAtoms_findBestMatching SaturateAtoms_singleDegree StoppableMakroAction Subpackage_CodePatterns Subpackage_JobMarket Subpackage_LinearAlgebra Subpackage_levmar Subpackage_mpqc_open Subpackage_vmg Switchable_LogView ThirdParty_MPQC_rebuilt_buildsystem TrajectoryDependenant_MaxOrder TremoloParser_IncreasedPrecision TremoloParser_MultipleTimesteps TremoloParser_setsAtomName Ubuntu_1604_changes stable
Last change on this file since fb3485 was fb3485, checked in by Frederik Heber <heber@…>, 12 years ago

Refactored copy routine out of AddOntoWindow().

  • this is preparatory for preparing a shrinking and copying into a smaller window functionalities.
  • Property mode set to 100644
File size: 11.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 "Jobs/Grid/SamplingGridProperties.hpp"
24
25class MPQCData;
26class 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 */
33class 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);
39public:
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^{\text{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 * \param other other instance to sum onto this one.
131 * \return ref to this instance
132 */
133 SamplingGrid& operator*=(const SamplingGrid& other);
134
135 /** Subtraction operator with another SamplingGrid instance \a other.
136 *
137 * \param other other instance to subtract from this one.
138 * \return ref to this instance
139 */
140 SamplingGrid& operator-=(const SamplingGrid& other)
141 {
142 superposeOtherGrids(other, -1.);
143 return *this;
144 }
145
146 /** Returns the numeric integral over the grid.
147 *
148 * @return sum of grid values times volume element
149 */
150 double integral() const;
151
152 /** Returns the numeric integral over the grid where the grid is element-wise multiplied with \a weight.
153 *
154 * @param weight grid of weights
155 * @return sum of grid values weighted by respective element in weight times volume element
156 */
157 double integral(const SamplingGrid &weight) const;
158
159 /** Returns the volume of the domain for this sampled function.
160 *
161 * @return volume
162 */
163 const double getVolume() 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 total number of gridpoints of the discrete mesh covering the volume.
180 *
181 * @return number of gridpoints sampled_values should have
182 */
183 const size_t getTotalGridPoints() const;
184
185 /** Returns the number of grid points per axis.
186 *
187 * @return number of grid points per unit length
188 */
189 const size_t getGridPointsPerAxis() const;
190
191 /** Returns the length of the domain for the given \a axis.
192 *
193 * \param axis axis for which to get step length
194 * \return domain length for the given axis, i.e. end - begin
195 */
196 const double getTotalLengthPerAxis(const size_t axis) const;
197
198 /** Returns the length of the window for the given \a axis.
199 *
200 * \param axis axis for which to get step length
201 * \return window length for the given axis, i.e. end - begin
202 */
203 const double getWindowLengthPerAxis(const size_t axis) const;
204
205 /** Returns the real step length from one discrete grid point to the next.
206 *
207 * \param axis axis for which to get step length
208 * \return step length for the given axis, as domain length over getGridPointsPerAxis()
209 */
210 const double getDeltaPerAxis(const size_t axis) const;
211
212 /** Returns the volume of the domain covered by the current window.
213 *
214 * @return volume of window
215 */
216 const double getWindowVolume() const;
217
218 /** Sets the size of the window.
219 *
220 * \note also resets the sampled points so far.
221 *
222 * \param _begin_window start of new window
223 * \param _end_window end of window
224 */
225 void setWindow(const double _begin_window[3], const double _end_window[3]);
226
227private:
228 /** Sets the size of the domain.
229 *
230 * \note also resets the sampled points so far and the window.
231 *
232 * \param _begin start of new window
233 * \param _end end of window
234 */
235 void setDomain(const double _begin[3], const double _end[3]);
236
237 /** Sets the size of the domain.
238 *
239 * \note this is just internally used for easing the array setting.
240 *
241 * \param _begin start of domain
242 * \param _end end of domain
243 */
244 void setDomainSize(const double _begin[3], const double _end[3]);
245
246 /** Extends the window while keeping the values.
247 *
248 * \param _begin_window new start of window
249 * \param _end_window new end of window
250 */
251 void extendWindow(const double _begin_window[3], const double _end_window[3]);
252
253 /** Adds another window onto the one in this instance.
254 *
255 * \note We assume here that the given window fits on the this one.
256 *
257 * \param _begin_window start of other window
258 * \param _end_window end of other window
259 * \param _sampled_grid other set of sampled values
260 * @param prefactor +1. is then addition, -1. is subtraction.
261 */
262 void addOntoWindow(
263 const double _begin_window[3],
264 const double _end_window[3],
265 const sampledvalues_t &_sampled_grid,
266 const double prefactor);
267
268 /** Enum to help in addWindowOntoWindow() decide which iterator needs to be
269 * advanced.
270 */
271 enum LargerWindow {
272 thiswindow,
273 otherwindow
274 };
275
276 /** Helper function to copy one (larger) window into a (smaller) window.
277 *
278 * \note Why do we need the extra \a choice? We need to know which window
279 * tuples is associated with which sampled values that are constrained by
280 * one of them being constant, hence the source values
281 *
282 * \param wbegin start of larger window
283 * \param wend end of larger window
284 * \param other_wbegin start of smaller window
285 * \param other_wend end of smaller window
286 * \param sampled_grid larger set of sampled values
287 * \param other_sampled_grid smaller set of sampled values
288 * \param op operation to perform with the two elements
289 * \param choice indicates which is the larger window
290 */
291 void addWindowOntoWindow(
292 const double wbegin[3],
293 const double wend[3],
294 const double other_wbegin[3],
295 const double other_wend[3],
296 sampledvalues_t &sampled_grid,
297 const sampledvalues_t &other_sampled_grid,
298 boost::function<void (double &, const double &)> op,
299 enum LargerWindow choice);
300
301 /** Helper function that contains all the logic of how to superpose two
302 * grids.
303 *
304 * Is called by SamplingGrid::operator+=() and SamplingGrid::operator-=()
305 *
306 * @param other other histogram
307 * @param prefactor +1. is then addition, -1. is subtraction.
308 */
309 void superposeOtherGrids(const SamplingGrid &other, const double prefactor);
310
311 /** Sets the size of the window.
312 *
313 * \note also resets the sampled points so far.
314 *
315 * \param _begin_window start of new window
316 * \param _end_window end of window
317 */
318 void setWindowSize(const double _begin_window[3], const double _end_window[3]);
319
320 /** Helper function to get point at grid point for given \a axis and less than value.
321 *
322 * @param value value to find nearest grid point to
323 * @param axis axis of the value
324 * @return nearest lower grid point
325 */
326 double getNearestLowerGridPoint(
327 const double value, const size_t axis) const;
328
329 /** Helper function to get point at grid point for given \a axis and greater than value.
330 *
331 * @param value value to find nearest grid point to
332 * @param axis axis of the value
333 * @return nearest lower grid point
334 */
335 double getNearestHigherGridPoint(
336 const double value, const size_t axis) const;
337
338public:
339 /// We do not store the whole grid if many entries are actually zero
340 /// but only a window wherein the sampled function is non-zero.
341
342 //!> sample points of the window
343 sampledvalues_t sampled_grid;
344
345 //!> start of the window relative to SamplingGridProperties::begin and SamplingGridProperties::size
346 double begin_window[3];
347 //!> end of the window relative to SamplingGridProperties::begin and SamplingGridProperties::size
348 double end_window[3];
349
350private:
351 friend class MPQCData;
352
353 friend class boost::serialization::access;
354 // serialization
355 template <typename Archive>
356 void serialize(Archive& ar, const unsigned int version)
357 {
358 ar & boost::serialization::base_object<SamplingGridProperties>(*this);
359 ar & const_cast< sampledvalues_t &>(sampled_grid);
360 for(size_t i=0;i<3;++i) {
361 ar & begin_window[i];
362 ar & end_window[i];
363 }
364 }
365
366 //!> static typedef to use in cstor when no initial values are given
367 static const double zeroOffset[3];
368};
369
370/** Output operator for class SamplingGrid.
371 *
372 * \param ost output stream to print to
373 * \param other instance to print
374 * \return ref to stream for concatenation
375 */
376std::ostream & operator<<(std::ostream &ost, const SamplingGrid& other);
377
378template<typename T> T ZeroInstance();
379template<> SamplingGrid ZeroInstance<SamplingGrid>();
380
381// we need to give this class a unique key for serialization
382// its is only serialized through its base class FragmentJob
383BOOST_CLASS_EXPORT_KEY(SamplingGrid)
384
385// define inline functions
386#include "SamplingGrid_inline.hpp"
387
388#endif /* SAMPLINGGRID_HPP_ */
Note: See TracBrowser for help on using the repository browser.