PSCF v1.1
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, The Regents of the University of Minnesota
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
171 double sum_;
172
174 double sumSq_;
175
177 double blockSum_;
178
180 long nSample_;
181
183 long nBlockSample_;
184
186 long stageInterval_;
187
189 AverageStage* childPtr_;
190
192 AverageStage* rootPtr_;
193
195 int stageId_;
196
198 int blockFactor_;
199
208 AverageStage(long stageInterval, int stageId,
209 AverageStage* rootPtr, int blockFactor);
210
214 AverageStage(const AverageStage& other);
215
219 AverageStage& operator = (const AverageStage& other);
220
228 virtual void registerDescendant(AverageStage* descendantPtr);
229
230 };
231
232 // Inline methods
233
234 /*
235 * Does this object have a child? (protected)
236 */
237 inline bool AverageStage::hasChild() const
238 { return bool(childPtr_); }
239
240 /*
241 * Return child object by reference. (protected)
242 */
244 { return *childPtr_; }
245
246 // Method template
247
248 /*
249 * Serialize this stage.
250 */
251 template <class Archive>
252 void AverageStage::serialize(Archive& ar, const unsigned int version)
253 {
254 ar & sum_;
255 ar & sumSq_;
256 ar & blockSum_;
257 ar & nSample_;
258 ar & nBlockSample_;
259 ar & stageInterval_;
260 ar & blockFactor_;
261
262 // Does this stage have a child?
263 int hasChild;
264 if (Archive::is_saving()) {
265 hasChild = (childPtr_ == 0) ? 0 : 1;
266 }
267 ar & hasChild;
268
269 // Serialize child (if any)
270 if (hasChild) {
271 if (Archive::is_loading()) {
272 long nextStageInterval = stageInterval_*blockFactor_;
273 int nextStageId = stageId_ + 1;
274 childPtr_ = new AverageStage(nextStageInterval, nextStageId,
275 rootPtr_, blockFactor_);
276 rootPtr_->registerDescendant(childPtr_);
277 }
278 ar & (*childPtr_);
279 } else {
280 if (Archive::is_loading()) {
281 childPtr_ = 0;
282 }
283 }
284
285 }
286
287}
288#endif
Evaluate average with hierarchical blocking error analysis.
Definition: AverageStage.h:67
bool hasChild() const
Does this object have a child AverageStage for block averages?
Definition: AverageStage.h:237
void setBlockFactor(int blockFactor)
Reset the value of blockFactor.
virtual ~AverageStage()
Destructor.
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.
void serialize(Archive &ar, const unsigned int version)
Add a sampled value to the ensemble.
Definition: AverageStage.h:252
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.
Definition: AverageStage.h:243
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.
Definition: accumulators.mod:1