PSCF v1.5.0
AverageStage.h
1#ifndef UTIL_AVERAGE_STAGE_H
2#define UTIL_AVERAGE_STAGE_H
3
4/*
5* Util Package - C++ Utilities for Scientific Computation
6*
7* Copyright 2010 - 2017, David Morse (morse012@um.edu)
8* Distributed under the terms of the GNU General Public License.
9*/
10
11#include <util/global.h>
12
13namespace Util
14{
15
67 {
68
69 public:
70
80 AverageStage(int blockFactor = 2);
81
87 virtual ~AverageStage();
88
94 void setBlockFactor(int blockFactor);
95
99 virtual void clear();
100
106 virtual void sample(double value);
107
114 template <class Archive>
115 void serialize(Archive& ar, const unsigned int version);
116
118
119
123 double average() const;
124
128 double variance() const;
129
135 double stdDeviation() const;
136
142 double error() const;
143
147 long nSample() const;
148
152 long stageInterval() const;
153
155
156 protected:
157
161 bool hasChild() const;
162
167
168 private:
169
170 // Private member variables
171
174 double shift_;
175
177 double sum_;
178
180 double sumSq_;
181
183 double blockSum_;
184
186 long nSample_;
187
189 long nBlockSample_;
190
192 long stageInterval_;
193
195 AverageStage* childPtr_;
196
198 AverageStage* rootPtr_;
199
201 int stageId_;
202
204 int blockFactor_;
205
206 // Private member functions
207
216 AverageStage(long stageInterval, int stageId,
217 AverageStage* rootPtr, int blockFactor);
218
222 AverageStage(const AverageStage& other);
223
227 AverageStage& operator = (const AverageStage& other);
228
236 virtual void registerDescendant(AverageStage* descendantPtr);
237
238 };
239
240 // Inline methods
241
242 /*
243 * Does this object have a child? (protected)
244 */
245 inline bool AverageStage::hasChild() const
246 { return bool(childPtr_); }
247
248 /*
249 * Return child object by reference. (protected)
250 */
252 { return *childPtr_; }
253
254 // Method template
255
256 /*
257 * Serialize this stage.
258 */
259 template <class Archive>
260 void AverageStage::serialize(Archive& ar, const unsigned int version)
261 {
262 // The archive holds the sums of the sampled values themselves,
263 // not of value - shift_, and does not contain shift_.
264 double sum = sum_ + double(nSample_)*shift_;
265 double sumSq = sumSq_ + shift_*(2.0*sum_ + double(nSample_)*shift_);
266 ar & sum;
267 ar & sumSq;
268 ar & blockSum_;
269 ar & nSample_;
270 ar & nBlockSample_;
271 ar & stageInterval_;
272 ar & blockFactor_;
273 if (Archive::is_loading()) {
274 // Take the loaded sums relative to their average. Values
275 // sampled before saving keep the rounding errors of the raw sums.
276 shift_ = (nSample_ > 0) ? sum/double(nSample_) : 0.0;
277 sum_ = sum - double(nSample_)*shift_;
278 sumSq_ = sumSq - double(nSample_)*shift_*shift_;
279 }
280
281 // Does this stage have a child?
282 int hasChild;
283 if (Archive::is_saving()) {
284 hasChild = (childPtr_ == 0) ? 0 : 1;
285 }
286 ar & hasChild;
287
288 // Serialize child (if any)
289 if (hasChild) {
290 if (Archive::is_loading()) {
291 long nextStageInterval = stageInterval_*blockFactor_;
292 int nextStageId = stageId_ + 1;
293 childPtr_ = new AverageStage(nextStageInterval, nextStageId,
294 rootPtr_, blockFactor_);
295 rootPtr_->registerDescendant(childPtr_);
296 }
297 ar & (*childPtr_);
298 } else {
299 if (Archive::is_loading()) {
300 childPtr_ = 0;
301 }
302 }
303
304 }
305
306}
307#endif
bool hasChild() const
Does this object have a child AverageStage for block averages?
void setBlockFactor(int blockFactor)
Reset the value of blockFactor.
long stageInterval() const
Return the number of sampled values per block at this stage.
virtual void sample(double value)
Add a sampled value to the ensemble.
virtual void clear()
Initialize all accumulators and recursively destroy all children.
AverageStage(int blockFactor=2)
Constructor.
void serialize(Archive &ar, const unsigned int version)
Add a sampled value to the ensemble.
double average() const
Return the average of all sampled values.
double variance() const
Return the variance of all sampled values.
double error() const
Return a naive estimate for the std deviation of the average.
AverageStage & child()
Return the child AverageStage by reference.
long nSample() const
Return the number of sampled values in this sequence.
double stdDeviation() const
Return the standard deviation of all sampled values.
File containing preprocessor macros for error handling.
Utility classes for scientific computation.