ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/OpenMD/branches/development/src/utils/Accumulator.hpp
Revision: 1850
Committed: Wed Feb 20 15:39:39 2013 UTC (12 years, 2 months ago) by gezelter
File size: 9348 byte(s)
Log Message:
Fixed a widespread typo in the license 

File Contents

# User Rev Content
1 gezelter 1765 /*
2     * Copyright (c) 2012 The University of Notre Dame. All Rights Reserved.
3     *
4     * The University of Notre Dame grants you ("Licensee") a
5     * non-exclusive, royalty free, license to use, modify and
6     * redistribute this software in source and binary code form, provided
7     * that the following conditions are met:
8     *
9     * 1. Redistributions of source code must retain the above copyright
10     * notice, this list of conditions and the following disclaimer.
11     *
12     * 2. Redistributions in binary form must reproduce the above copyright
13     * notice, this list of conditions and the following disclaimer in the
14     * documentation and/or other materials provided with the
15     * distribution.
16     *
17     * This software is provided "AS IS," without a warranty of any
18     * kind. All express or implied conditions, representations and
19     * warranties, including any implied warranty of merchantability,
20     * fitness for a particular purpose or non-infringement, are hereby
21     * excluded. The University of Notre Dame and its licensors shall not
22     * be liable for any damages suffered by licensee as a result of
23     * using, modifying or distributing the software or its
24     * derivatives. In no event will the University of Notre Dame or its
25     * licensors be liable for any lost revenue, profit or data, or for
26     * direct, indirect, special, consequential, incidental or punitive
27     * damages, however caused and regardless of the theory of liability,
28     * arising out of the use of or inability to use software, even if the
29     * University of Notre Dame has been advised of the possibility of
30     * such damages.
31     *
32     * SUPPORT OPEN SCIENCE! If you use OpenMD or its source code in your
33     * research, please cite the appropriate papers when you publish your
34     * work. Good starting points are:
35     *
36     * [1] Meineke, et al., J. Comp. Chem. 26, 252-271 (2005).
37     * [2] Fennell & Gezelter, J. Chem. Phys. 124, 234104 (2006).
38 gezelter 1850 * [3] Sun, Lin & Gezelter, J. Chem. Phys. 128, 234107 (2008).
39 gezelter 1765 * [4] Kuang & Gezelter, J. Chem. Phys. 133, 164101 (2010).
40     * [5] Vardeman, Stocker & Gezelter, J. Chem. Theory Comput. 7, 834 (2011).
41     */
42    
43     #ifndef UTILS_ACCUMULATOR_HPP
44     #define UTILS_ACCUMULATOR_HPP
45    
46     #include <cmath>
47     #include <cassert>
48     #include "math/Vector3.hpp"
49    
50     namespace OpenMD {
51    
52 gezelter 1813
53     class BaseAccumulator {
54     public:
55     virtual void clear() = 0;
56     /**
57     * get the number of accumulated values
58     */
59     virtual size_t count() {
60     return Count_;
61     }
62     protected:
63     size_t Count_;
64    
65     };
66    
67    
68    
69 gezelter 1765 /**
70     * Basic Accumulator class for numbers.
71 gezelter 1813 */
72     class Accumulator : public BaseAccumulator {
73 gezelter 1765
74     typedef RealType ElementType;
75     typedef RealType ResultType;
76    
77     public:
78    
79 gezelter 1813 Accumulator() : BaseAccumulator() {
80 gezelter 1765 this->clear();
81     }
82    
83     /**
84     * Accumulate another value
85     */
86     virtual void add(ElementType const& val) {
87     Count_++;
88     Avg_ += (val - Avg_ ) / Count_;
89     Avg2_ += (val * val - Avg2_) / Count_;
90     Val_ = val;
91     if (Count_ <= 1) {
92     Max_ = val;
93     Min_ = val;
94     } else {
95     Max_ = val > Max_ ? val : Max_;
96     Min_ = val < Min_ ? val : Min_;
97     }
98     }
99    
100     /**
101     * reset the Accumulator to the empty state
102     */
103     void clear() {
104     Count_ = 0;
105     Avg_ = 0;
106     Avg2_ = 0;
107     Val_ = 0;
108     }
109    
110    
111     /**
112     * return the most recently added value
113     */
114     void getLastValue(ElementType &ret) {
115     ret = Val_;
116     return;
117     }
118    
119     /**
120     * compute the Mean
121     */
122     void getAverage(ResultType &ret) {
123     assert(Count_ != 0);
124     ret = Avg_;
125     return;
126     }
127    
128     /**
129     * compute the Variance
130     */
131     void getVariance(ResultType &ret) {
132     assert(Count_ != 0);
133     ret = (Avg2_ - Avg_ * Avg_);
134     return;
135     }
136    
137     /**
138     * compute error of average value
139     */
140     void getStdDev(ResultType &ret) {
141     assert(Count_ != 0);
142     RealType var;
143     this->getVariance(var);
144     ret = sqrt(var);
145     return;
146     }
147    
148     /**
149     * return the largest value
150     */
151     void getMax(ElementType &ret) {
152     assert(Count_ != 0);
153     ret = Max_;
154     return;
155     }
156    
157     /**
158     * return the smallest value
159     */
160     void getMin(ElementType &ret) {
161     assert(Count_ != 0);
162     ret = Max_;
163     return;
164     }
165    
166     private:
167     ElementType Val_;
168     ResultType Avg_;
169     ResultType Avg2_;
170     ElementType Min_;
171     ElementType Max_;
172     };
173    
174 gezelter 1813 class VectorAccumulator : public BaseAccumulator {
175 gezelter 1765
176     typedef Vector3d ElementType;
177     typedef Vector3d ResultType;
178    
179     public:
180 gezelter 1813 VectorAccumulator() : BaseAccumulator() {
181 gezelter 1765 this->clear();
182     }
183    
184     /**
185     * Accumulate another value
186     */
187     void add(ElementType const& val) {
188     Count_++;
189     RealType len(0.0);
190     for (unsigned int i =0; i < 3; i++) {
191     Avg_[i] += (val[i] - Avg_[i] ) / Count_;
192     Avg2_[i] += (val[i] * val[i] - Avg2_[i]) / Count_;
193     Val_[i] = val[i];
194     len += val[i]*val[i];
195     }
196     len = sqrt(len);
197     AvgLen_ += (len - AvgLen_ ) / Count_;
198     AvgLen2_ += (len * len - AvgLen2_) / Count_;
199    
200     if (Count_ <= 1) {
201     Max_ = len;
202     Min_ = len;
203     } else {
204     Max_ = len > Max_ ? len : Max_;
205     Min_ = len < Min_ ? len : Min_;
206     }
207     }
208    
209     /**
210     * reset the Accumulator to the empty state
211     */
212     void clear() {
213     Count_ = 0;
214     Avg_ = V3Zero;
215     Avg2_ = V3Zero;
216     Val_ = V3Zero;
217     AvgLen_ = 0;
218     AvgLen2_ = 0;
219     }
220    
221     /**
222     * return the most recently added value
223     */
224     void getLastValue(ElementType &ret) {
225     ret = Val_;
226     return;
227     }
228    
229     /**
230     * compute the Mean
231     */
232     void getAverage(ResultType &ret) {
233     assert(Count_ != 0);
234     ret = Avg_;
235     return;
236     }
237    
238     /**
239     * compute the Variance
240     */
241     void getVariance(ResultType &ret) {
242     assert(Count_ != 0);
243     for (unsigned int i =0; i < 3; i++) {
244     ret[i] = (Avg2_[i] - Avg_[i] * Avg_[i]);
245     }
246     return;
247     }
248    
249     /**
250     * compute error of average value
251     */
252     void getStdDev(ResultType &ret) {
253     assert(Count_ != 0);
254     ResultType var;
255     this->getVariance(var);
256     ret[0] = sqrt(var[0]);
257     ret[1] = sqrt(var[1]);
258     ret[2] = sqrt(var[2]);
259     return;
260     }
261    
262     /**
263     * return the largest length
264     */
265     void getMaxLength(RealType &ret) {
266     assert(Count_ != 0);
267     ret = Max_;
268     return;
269     }
270    
271     /**
272     * return the smallest length
273     */
274     void getMinLength(RealType &ret) {
275     assert(Count_ != 0);
276     ret = Min_;
277     return;
278     }
279    
280     /**
281     * return the largest length
282     */
283     void getAverageLength(RealType &ret) {
284     assert(Count_ != 0);
285     ret = AvgLen_;
286     return;
287     }
288    
289     /**
290     * compute the Variance of the length
291     */
292     void getLengthVariance(RealType &ret) {
293     assert(Count_ != 0);
294     ret= (AvgLen2_ - AvgLen_ * AvgLen_);
295     return;
296     }
297    
298     /**
299     * compute error of average value
300     */
301     void getLengthStdDev(RealType &ret) {
302     assert(Count_ != 0);
303     RealType var;
304     this->getLengthVariance(var);
305     ret = sqrt(var);
306     return;
307     }
308    
309     protected:
310     size_t Count_;
311     private:
312     ResultType Val_;
313     ResultType Avg_;
314     ResultType Avg2_;
315     RealType AvgLen_;
316     RealType AvgLen2_;
317     RealType Min_;
318     RealType Max_;
319    
320     };
321    
322 gezelter 1813 class MatrixAccumulator : public BaseAccumulator {
323 gezelter 1765
324     typedef Mat3x3d ElementType;
325     typedef Mat3x3d ResultType;
326    
327     public:
328 gezelter 1813 MatrixAccumulator() : BaseAccumulator() {
329 gezelter 1765 this->clear();
330     }
331    
332     /**
333     * Accumulate another value
334     */
335     void add(ElementType const& val) {
336     Count_++;
337     for (unsigned int i = 0; i < 3; i++) {
338     for (unsigned int j = 0; j < 3; j++) {
339     Avg_(i,j) += (val(i,j) - Avg_(i,j) ) / Count_;
340     Avg2_(i,j) += (val(i,j) * val(i,j) - Avg2_(i,j)) / Count_;
341     Val_(i,j) = val(i,j);
342     }
343     }
344     }
345    
346     /**
347     * reset the Accumulator to the empty state
348     */
349     void clear() {
350     Count_ = 0;
351     Avg_ *= 0.0;
352     Avg2_ *= 0.0;
353     Val_ *= 0.0;
354     }
355    
356     /**
357     * return the most recently added value
358     */
359     void getLastValue(ElementType &ret) {
360     ret = Val_;
361     return;
362     }
363    
364     /**
365     * compute the Mean
366     */
367     void getAverage(ResultType &ret) {
368     assert(Count_ != 0);
369     ret = Avg_;
370     return;
371     }
372    
373     /**
374     * compute the Variance
375     */
376     void getVariance(ResultType &ret) {
377     assert(Count_ != 0);
378     for (unsigned int i = 0; i < 3; i++) {
379     for (unsigned int j = 0; j < 3; j++) {
380     ret(i,j) = (Avg2_(i,j) - Avg_(i,j) * Avg_(i,j));
381     }
382     }
383     return;
384     }
385    
386     /**
387     * compute error of average value
388     */
389     void getStdDev(ResultType &ret) {
390     assert(Count_ != 0);
391     Mat3x3d var;
392     this->getVariance(var);
393     for (unsigned int i = 0; i < 3; i++) {
394     for (unsigned int j = 0; j < 3; j++) {
395     ret(i,j) = sqrt(var(i,j));
396     }
397     }
398     return;
399     }
400    
401     protected:
402     size_t Count_;
403     private:
404     ElementType Val_;
405     ResultType Avg_;
406     ResultType Avg2_;
407     };
408    
409    
410     }
411    
412     #endif

Properties

Name Value
svn:eol-style native