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, 24107 (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 |
* Basic Accumulator class for numbers. |
54 |
*/ |
55 |
|
56 |
class Accumulator { |
57 |
|
58 |
typedef RealType ElementType; |
59 |
typedef RealType ResultType; |
60 |
|
61 |
public: |
62 |
|
63 |
Accumulator() { |
64 |
this->clear(); |
65 |
} |
66 |
|
67 |
/** |
68 |
* Accumulate another value |
69 |
*/ |
70 |
virtual void add(ElementType const& val) { |
71 |
Count_++; |
72 |
Avg_ += (val - Avg_ ) / Count_; |
73 |
Avg2_ += (val * val - Avg2_) / Count_; |
74 |
Val_ = val; |
75 |
if (Count_ <= 1) { |
76 |
Max_ = val; |
77 |
Min_ = val; |
78 |
} else { |
79 |
Max_ = val > Max_ ? val : Max_; |
80 |
Min_ = val < Min_ ? val : Min_; |
81 |
} |
82 |
} |
83 |
|
84 |
/** |
85 |
* reset the Accumulator to the empty state |
86 |
*/ |
87 |
void clear() { |
88 |
Count_ = 0; |
89 |
Avg_ = 0; |
90 |
Avg2_ = 0; |
91 |
Val_ = 0; |
92 |
} |
93 |
|
94 |
/** |
95 |
* get the number of accumulated values |
96 |
*/ |
97 |
size_t count() { |
98 |
return Count_; |
99 |
} |
100 |
|
101 |
/** |
102 |
* return the most recently added value |
103 |
*/ |
104 |
void getLastValue(ElementType &ret) { |
105 |
ret = Val_; |
106 |
return; |
107 |
} |
108 |
|
109 |
/** |
110 |
* compute the Mean |
111 |
*/ |
112 |
void getAverage(ResultType &ret) { |
113 |
assert(Count_ != 0); |
114 |
ret = Avg_; |
115 |
return; |
116 |
} |
117 |
|
118 |
/** |
119 |
* compute the Variance |
120 |
*/ |
121 |
void getVariance(ResultType &ret) { |
122 |
assert(Count_ != 0); |
123 |
ret = (Avg2_ - Avg_ * Avg_); |
124 |
return; |
125 |
} |
126 |
|
127 |
/** |
128 |
* compute error of average value |
129 |
*/ |
130 |
void getStdDev(ResultType &ret) { |
131 |
assert(Count_ != 0); |
132 |
RealType var; |
133 |
this->getVariance(var); |
134 |
ret = sqrt(var); |
135 |
return; |
136 |
} |
137 |
|
138 |
/** |
139 |
* return the largest value |
140 |
*/ |
141 |
void getMax(ElementType &ret) { |
142 |
assert(Count_ != 0); |
143 |
ret = Max_; |
144 |
return; |
145 |
} |
146 |
|
147 |
/** |
148 |
* return the smallest value |
149 |
*/ |
150 |
void getMin(ElementType &ret) { |
151 |
assert(Count_ != 0); |
152 |
ret = Max_; |
153 |
return; |
154 |
} |
155 |
|
156 |
protected: |
157 |
size_t Count_; |
158 |
private: |
159 |
ElementType Val_; |
160 |
ResultType Avg_; |
161 |
ResultType Avg2_; |
162 |
ElementType Min_; |
163 |
ElementType Max_; |
164 |
}; |
165 |
|
166 |
class VectorAccumulator : public Accumulator { |
167 |
|
168 |
typedef Vector3d ElementType; |
169 |
typedef Vector3d ResultType; |
170 |
|
171 |
public: |
172 |
VectorAccumulator() : Accumulator() { |
173 |
this->clear(); |
174 |
} |
175 |
|
176 |
/** |
177 |
* Accumulate another value |
178 |
*/ |
179 |
void add(ElementType const& val) { |
180 |
Count_++; |
181 |
RealType len(0.0); |
182 |
for (unsigned int i =0; i < 3; i++) { |
183 |
Avg_[i] += (val[i] - Avg_[i] ) / Count_; |
184 |
Avg2_[i] += (val[i] * val[i] - Avg2_[i]) / Count_; |
185 |
Val_[i] = val[i]; |
186 |
len += val[i]*val[i]; |
187 |
} |
188 |
len = sqrt(len); |
189 |
AvgLen_ += (len - AvgLen_ ) / Count_; |
190 |
AvgLen2_ += (len * len - AvgLen2_) / Count_; |
191 |
|
192 |
if (Count_ <= 1) { |
193 |
Max_ = len; |
194 |
Min_ = len; |
195 |
} else { |
196 |
Max_ = len > Max_ ? len : Max_; |
197 |
Min_ = len < Min_ ? len : Min_; |
198 |
} |
199 |
} |
200 |
|
201 |
/** |
202 |
* reset the Accumulator to the empty state |
203 |
*/ |
204 |
void clear() { |
205 |
Count_ = 0; |
206 |
Avg_ = V3Zero; |
207 |
Avg2_ = V3Zero; |
208 |
Val_ = V3Zero; |
209 |
AvgLen_ = 0; |
210 |
AvgLen2_ = 0; |
211 |
} |
212 |
|
213 |
/** |
214 |
* return the most recently added value |
215 |
*/ |
216 |
void getLastValue(ElementType &ret) { |
217 |
ret = Val_; |
218 |
return; |
219 |
} |
220 |
|
221 |
/** |
222 |
* compute the Mean |
223 |
*/ |
224 |
void getAverage(ResultType &ret) { |
225 |
assert(Count_ != 0); |
226 |
ret = Avg_; |
227 |
return; |
228 |
} |
229 |
|
230 |
/** |
231 |
* compute the Variance |
232 |
*/ |
233 |
void getVariance(ResultType &ret) { |
234 |
assert(Count_ != 0); |
235 |
for (unsigned int i =0; i < 3; i++) { |
236 |
ret[i] = (Avg2_[i] - Avg_[i] * Avg_[i]); |
237 |
} |
238 |
return; |
239 |
} |
240 |
|
241 |
/** |
242 |
* compute error of average value |
243 |
*/ |
244 |
void getStdDev(ResultType &ret) { |
245 |
assert(Count_ != 0); |
246 |
ResultType var; |
247 |
this->getVariance(var); |
248 |
ret[0] = sqrt(var[0]); |
249 |
ret[1] = sqrt(var[1]); |
250 |
ret[2] = sqrt(var[2]); |
251 |
return; |
252 |
} |
253 |
|
254 |
/** |
255 |
* return the largest length |
256 |
*/ |
257 |
void getMaxLength(RealType &ret) { |
258 |
assert(Count_ != 0); |
259 |
ret = Max_; |
260 |
return; |
261 |
} |
262 |
|
263 |
/** |
264 |
* return the smallest length |
265 |
*/ |
266 |
void getMinLength(RealType &ret) { |
267 |
assert(Count_ != 0); |
268 |
ret = Min_; |
269 |
return; |
270 |
} |
271 |
|
272 |
/** |
273 |
* return the largest length |
274 |
*/ |
275 |
void getAverageLength(RealType &ret) { |
276 |
assert(Count_ != 0); |
277 |
ret = AvgLen_; |
278 |
return; |
279 |
} |
280 |
|
281 |
/** |
282 |
* compute the Variance of the length |
283 |
*/ |
284 |
void getLengthVariance(RealType &ret) { |
285 |
assert(Count_ != 0); |
286 |
ret= (AvgLen2_ - AvgLen_ * AvgLen_); |
287 |
return; |
288 |
} |
289 |
|
290 |
/** |
291 |
* compute error of average value |
292 |
*/ |
293 |
void getLengthStdDev(RealType &ret) { |
294 |
assert(Count_ != 0); |
295 |
RealType var; |
296 |
this->getLengthVariance(var); |
297 |
ret = sqrt(var); |
298 |
return; |
299 |
} |
300 |
|
301 |
protected: |
302 |
size_t Count_; |
303 |
private: |
304 |
ResultType Val_; |
305 |
ResultType Avg_; |
306 |
ResultType Avg2_; |
307 |
RealType AvgLen_; |
308 |
RealType AvgLen2_; |
309 |
RealType Min_; |
310 |
RealType Max_; |
311 |
|
312 |
}; |
313 |
|
314 |
class MatrixAccumulator : public Accumulator { |
315 |
|
316 |
typedef Mat3x3d ElementType; |
317 |
typedef Mat3x3d ResultType; |
318 |
|
319 |
public: |
320 |
MatrixAccumulator() : Accumulator() { |
321 |
this->clear(); |
322 |
} |
323 |
|
324 |
/** |
325 |
* Accumulate another value |
326 |
*/ |
327 |
void add(ElementType const& val) { |
328 |
Count_++; |
329 |
for (unsigned int i = 0; i < 3; i++) { |
330 |
for (unsigned int j = 0; j < 3; j++) { |
331 |
Avg_(i,j) += (val(i,j) - Avg_(i,j) ) / Count_; |
332 |
Avg2_(i,j) += (val(i,j) * val(i,j) - Avg2_(i,j)) / Count_; |
333 |
Val_(i,j) = val(i,j); |
334 |
} |
335 |
} |
336 |
} |
337 |
|
338 |
/** |
339 |
* reset the Accumulator to the empty state |
340 |
*/ |
341 |
void clear() { |
342 |
Count_ = 0; |
343 |
Avg_ *= 0.0; |
344 |
Avg2_ *= 0.0; |
345 |
Val_ *= 0.0; |
346 |
} |
347 |
|
348 |
/** |
349 |
* return the most recently added value |
350 |
*/ |
351 |
void getLastValue(ElementType &ret) { |
352 |
ret = Val_; |
353 |
return; |
354 |
} |
355 |
|
356 |
/** |
357 |
* compute the Mean |
358 |
*/ |
359 |
void getAverage(ResultType &ret) { |
360 |
assert(Count_ != 0); |
361 |
ret = Avg_; |
362 |
return; |
363 |
} |
364 |
|
365 |
/** |
366 |
* compute the Variance |
367 |
*/ |
368 |
void getVariance(ResultType &ret) { |
369 |
assert(Count_ != 0); |
370 |
for (unsigned int i = 0; i < 3; i++) { |
371 |
for (unsigned int j = 0; j < 3; j++) { |
372 |
ret(i,j) = (Avg2_(i,j) - Avg_(i,j) * Avg_(i,j)); |
373 |
} |
374 |
} |
375 |
return; |
376 |
} |
377 |
|
378 |
/** |
379 |
* compute error of average value |
380 |
*/ |
381 |
void getStdDev(ResultType &ret) { |
382 |
assert(Count_ != 0); |
383 |
Mat3x3d var; |
384 |
this->getVariance(var); |
385 |
for (unsigned int i = 0; i < 3; i++) { |
386 |
for (unsigned int j = 0; j < 3; j++) { |
387 |
ret(i,j) = sqrt(var(i,j)); |
388 |
} |
389 |
} |
390 |
return; |
391 |
} |
392 |
|
393 |
protected: |
394 |
size_t Count_; |
395 |
private: |
396 |
ElementType Val_; |
397 |
ResultType Avg_; |
398 |
ResultType Avg2_; |
399 |
}; |
400 |
|
401 |
|
402 |
} |
403 |
|
404 |
#endif |