ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/OpenMD/branches/development/src/utils/Accumulator.hpp
Revision: 1855
Committed: Tue Apr 2 18:31:51 2013 UTC (12 years ago) by gezelter
File size: 9276 byte(s)
Log Message:
Fixed a bunch of bugs in CubicSpline debug sections, ForceMatrix Decomp 
computing costhetas for non-existent rotation matrices, Hidden accumulator counts, and SMIPD non-coupling

File Contents

# Content
1 /*
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 * [3] Sun, Lin & Gezelter, J. Chem. Phys. 128, 234107 (2008).
39 * [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
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 /**
70 * Basic Accumulator class for numbers.
71 */
72 class Accumulator : public BaseAccumulator {
73
74 typedef RealType ElementType;
75 typedef RealType ResultType;
76
77 public:
78
79 Accumulator() : BaseAccumulator() {
80 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 class VectorAccumulator : public BaseAccumulator {
175
176 typedef Vector3d ElementType;
177 typedef Vector3d ResultType;
178
179 public:
180 VectorAccumulator() : BaseAccumulator() {
181 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 private:
310 ResultType Val_;
311 ResultType Avg_;
312 ResultType Avg2_;
313 RealType AvgLen_;
314 RealType AvgLen2_;
315 RealType Min_;
316 RealType Max_;
317
318 };
319
320 class MatrixAccumulator : public BaseAccumulator {
321
322 typedef Mat3x3d ElementType;
323 typedef Mat3x3d ResultType;
324
325 public:
326 MatrixAccumulator() : BaseAccumulator() {
327 this->clear();
328 }
329
330 /**
331 * Accumulate another value
332 */
333 void add(ElementType const& val) {
334 Count_++;
335 for (unsigned int i = 0; i < 3; i++) {
336 for (unsigned int j = 0; j < 3; j++) {
337 Avg_(i,j) += (val(i,j) - Avg_(i,j) ) / Count_;
338 Avg2_(i,j) += (val(i,j) * val(i,j) - Avg2_(i,j)) / Count_;
339 Val_(i,j) = val(i,j);
340 }
341 }
342 }
343
344 /**
345 * reset the Accumulator to the empty state
346 */
347 void clear() {
348 Count_ = 0;
349 Avg_ *= 0.0;
350 Avg2_ *= 0.0;
351 Val_ *= 0.0;
352 }
353
354 /**
355 * return the most recently added value
356 */
357 void getLastValue(ElementType &ret) {
358 ret = Val_;
359 return;
360 }
361
362 /**
363 * compute the Mean
364 */
365 void getAverage(ResultType &ret) {
366 assert(Count_ != 0);
367 ret = Avg_;
368 return;
369 }
370
371 /**
372 * compute the Variance
373 */
374 void getVariance(ResultType &ret) {
375 assert(Count_ != 0);
376 for (unsigned int i = 0; i < 3; i++) {
377 for (unsigned int j = 0; j < 3; j++) {
378 ret(i,j) = (Avg2_(i,j) - Avg_(i,j) * Avg_(i,j));
379 }
380 }
381 return;
382 }
383
384 /**
385 * compute error of average value
386 */
387 void getStdDev(ResultType &ret) {
388 assert(Count_ != 0);
389 Mat3x3d var;
390 this->getVariance(var);
391 for (unsigned int i = 0; i < 3; i++) {
392 for (unsigned int j = 0; j < 3; j++) {
393 ret(i,j) = sqrt(var(i,j));
394 }
395 }
396 return;
397 }
398
399 private:
400 ElementType Val_;
401 ResultType Avg_;
402 ResultType Avg2_;
403 };
404
405
406 }
407
408 #endif

Properties

Name Value
svn:eol-style native