OpenMD 3.1
Molecular Dynamics in the Open
Loading...
Searching...
No Matches
OldAccumulator.hpp
1/*
2 * Copyright (c) 2004-2022, The University of Notre Dame. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright notice,
8 * this list of conditions and the following disclaimer.
9 *
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 *
14 * 3. Neither the name of the copyright holder nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
29 *
30 * SUPPORT OPEN SCIENCE! If you use OpenMD or its source code in your
31 * research, please cite the appropriate papers when you publish your
32 * work. Good starting points are:
33 *
34 * [1] Meineke, et al., J. Comp. Chem. 26, 252-271 (2005).
35 * [2] Fennell & Gezelter, J. Chem. Phys. 124, 234104 (2006).
36 * [3] Sun, Lin & Gezelter, J. Chem. Phys. 128, 234107 (2008).
37 * [4] Vardeman, Stocker & Gezelter, J. Chem. Theory Comput. 7, 834 (2011).
38 * [5] Kuang & Gezelter, Mol. Phys., 110, 691-701 (2012).
39 * [6] Lamichhane, Gezelter & Newman, J. Chem. Phys. 141, 134109 (2014).
40 * [7] Lamichhane, Newman & Gezelter, J. Chem. Phys. 141, 134110 (2014).
41 * [8] Bhattarai, Newman & Gezelter, Phys. Rev. B 99, 094106 (2019).
42 */
43
44#ifndef UTILS_OLDACCUMULATOR_HPP
45#define UTILS_OLDACCUMULATOR_HPP
46
47#include <cassert>
48#include <cmath>
49
50#include "math/Vector3.hpp"
51#include "nonbonded/NonBondedInteraction.hpp"
52
53namespace OpenMD {
54
56 public:
57 virtual void clear() = 0;
58 /**
59 * get the number of accumulated values
60 */
61 virtual size_t count() { return Count_; }
62 virtual ~BaseAccumulator() {};
63
64 protected:
65 size_t Count_ {};
66 };
67
68 /**
69 * Basic Accumulator class for numbers.
70 */
72 using ElementType = RealType;
73 using ResultType = RealType;
74
75 public:
76 Accumulator() : BaseAccumulator() { this->clear(); }
77
78 ~Accumulator() {};
79
80 /**
81 * Accumulate another value
82 */
83 virtual void add(ElementType const& val) {
84 Count_++;
85 Avg_ += (val - Avg_) / Count_;
86 Avg2_ += (val * val - Avg2_) / Count_;
87 Val_ = val;
88 Total_ += val;
89 if (Count_ <= 1) {
90 Max_ = val;
91 Min_ = val;
92 } else {
93 Max_ = val > Max_ ? val : Max_;
94 Min_ = val < Min_ ? val : Min_;
95 }
96 }
97
98 /**
99 * reset the Accumulator to the empty state
100 */
101 void clear() {
102 Count_ = 0;
103 Avg_ = 0;
104 Avg2_ = 0;
105 Total_ = 0;
106 Val_ = 0;
107 }
108
109 /**
110 * return the most recently added value
111 */
112 void getLastValue(ElementType& ret) {
113 ret = Val_;
114 return;
115 }
116
117 /**
118 * compute the Mean
119 */
120 void getAverage(ResultType& ret) {
121 assert(Count_ != 0);
122 ret = Avg_;
123 return;
124 }
125
126 /**
127 * return the Total accumulated sum
128 */
129 void getTotal(ResultType& ret) {
130 assert(Count_ != 0);
131 ret = Total_;
132 return;
133 }
134
135 /**
136 * compute the Variance
137 */
138 void getVariance(ResultType& ret) {
139 assert(Count_ != 0);
140 ret = (Avg2_ - Avg_ * Avg_);
141 if (ret < 0) ret = 0;
142 return;
143 }
144
145 /**
146 * compute error of average value
147 */
148 void getStdDev(ResultType& ret) {
149 assert(Count_ != 0);
150 RealType var;
151 this->getVariance(var);
152 ret = sqrt(var);
153 return;
154 }
155
156 /**
157 * return the largest value
158 */
159 void getMax(ElementType& ret) {
160 assert(Count_ != 0);
161 ret = Max_;
162 return;
163 }
164
165 /**
166 * return the smallest value
167 */
168 void getMin(ElementType& ret) {
169 assert(Count_ != 0);
170 ret = Max_;
171 return;
172 }
173
174 /**
175 * return the 95% confidence interval:
176 *
177 * That is returns c, such that we have 95% confidence that the
178 * true mean is within 2c of the Average (x):
179 *
180 * x - c <= true mean <= x + c
181 *
182 */
183 void get95percentConfidenceInterval(ResultType& ret) {
184 assert(Count_ != 0);
185 RealType sd;
186 this->getStdDev(sd);
187 ret = 1.960 * sd / sqrt(RealType(Count_));
188 return;
189 }
190
191 private:
192 ElementType Val_;
193 ResultType Avg_;
194 ResultType Avg2_;
195 ResultType Total_;
196 ElementType Min_;
197 ElementType Max_;
198 };
199
201 using ElementType = Vector3d;
202 using ResultType = Vector3d;
203
204 public:
205 VectorAccumulator() : BaseAccumulator() { this->clear(); }
206
207 /**
208 * Accumulate another value
209 */
210 void add(ElementType const& val) {
211 Count_++;
212 RealType len(0.0);
213 for (unsigned int i = 0; i < 3; i++) {
214 Avg_[i] += (val[i] - Avg_[i]) / Count_;
215 Avg2_[i] += (val[i] * val[i] - Avg2_[i]) / Count_;
216 Val_[i] = val[i];
217 Total_[i] += val[i];
218 len += val[i] * val[i];
219 }
220 len = sqrt(len);
221 AvgLen_ += (len - AvgLen_) / Count_;
222 AvgLen2_ += (len * len - AvgLen2_) / Count_;
223
224 if (Count_ <= 1) {
225 Max_ = len;
226 Min_ = len;
227 } else {
228 Max_ = len > Max_ ? len : Max_;
229 Min_ = len < Min_ ? len : Min_;
230 }
231 }
232
233 /**
234 * reset the Accumulator to the empty state
235 */
236 void clear() {
237 Count_ = 0;
238 Avg_ = V3Zero;
239 Avg2_ = V3Zero;
240 Total_ = V3Zero;
241 Val_ = V3Zero;
242 AvgLen_ = 0;
243 AvgLen2_ = 0;
244 }
245
246 /**
247 * return the most recently added value
248 */
250 ret = Val_;
251 return;
252 }
253
254 /**
255 * compute the Mean
256 */
258 assert(Count_ != 0);
259 ret = Avg_;
260 return;
261 }
262
263 /**
264 * return the Total accumulated sum
265 */
266 void getTotal(ResultType& ret) {
267 assert(Count_ != 0);
268 ret = Total_;
269 return;
270 }
271
272 /**
273 * compute the Variance
274 */
276 assert(Count_ != 0);
277 for (unsigned int i = 0; i < 3; i++) {
278 ret[i] = (Avg2_[i] - Avg_[i] * Avg_[i]);
279 if (ret[i] < 0) ret[i] = 0;
280 }
281 return;
282 }
283
284 /**
285 * compute error of average value
286 */
288 assert(Count_ != 0);
289 ResultType var;
290 this->getVariance(var);
291 ret[0] = sqrt(var[0]);
292 ret[1] = sqrt(var[1]);
293 ret[2] = sqrt(var[2]);
294 return;
295 }
296
297 /**
298 * return the 95% confidence interval:
299 *
300 * That is returns c, such that we have 95% confidence that the
301 * true mean is within 2c of the Average (x):
302 *
303 * x - c <= true mean <= x + c
304 *
305 */
307 assert(Count_ != 0);
308 ResultType sd;
309 this->getStdDev(sd);
310 ret[0] = 1.960 * sd[0] / sqrt(RealType(Count_));
311 ret[1] = 1.960 * sd[1] / sqrt(RealType(Count_));
312 ret[2] = 1.960 * sd[2] / sqrt(RealType(Count_));
313 return;
314 }
315
316 /**
317 * return the largest length
318 */
319 void getMaxLength(RealType& ret) {
320 assert(Count_ != 0);
321 ret = Max_;
322 return;
323 }
324
325 /**
326 * return the smallest length
327 */
328 void getMinLength(RealType& ret) {
329 assert(Count_ != 0);
330 ret = Min_;
331 return;
332 }
333
334 /**
335 * return the largest length
336 */
337 void getAverageLength(RealType& ret) {
338 assert(Count_ != 0);
339 ret = AvgLen_;
340 return;
341 }
342
343 /**
344 * compute the Variance of the length
345 */
346 void getLengthVariance(RealType& ret) {
347 assert(Count_ != 0);
348 ret = (AvgLen2_ - AvgLen_ * AvgLen_);
349 if (ret < 0) ret = 0;
350 return;
351 }
352
353 /**
354 * compute error of average value
355 */
356 void getLengthStdDev(RealType& ret) {
357 assert(Count_ != 0);
358 RealType var;
359 this->getLengthVariance(var);
360 ret = sqrt(var);
361 return;
362 }
363
364 /**
365 * return the 95% confidence interval:
366 *
367 * That is returns c, such that we have 95% confidence that the
368 * true mean is within 2c of the Average (x):
369 *
370 * x - c <= true mean <= x + c
371 *
372 */
374 assert(Count_ != 0);
375 RealType sd;
376 this->getLengthStdDev(sd);
377 ret = 1.960 * sd / sqrt(RealType(Count_));
378 return;
379 }
380
381 private:
382 ResultType Val_;
383 ResultType Avg_;
384 ResultType Avg2_;
385 ResultType Total_;
386 RealType AvgLen_;
387 RealType AvgLen2_;
388 RealType Min_;
389 RealType Max_;
390 };
391
393 using ElementType = potVec;
394 using ResultType = potVec;
395
396 public:
397 PotVecAccumulator() : BaseAccumulator() { this->clear(); }
398
399 /**
400 * Accumulate another value
401 */
402 void add(ElementType const& val) {
403 Count_++;
404 RealType len(0.0);
405 for (unsigned int i = 0; i < N_INTERACTION_FAMILIES; i++) {
406 Avg_[i] += (val[i] - Avg_[i]) / Count_;
407 Avg2_[i] += (val[i] * val[i] - Avg2_[i]) / Count_;
408 Val_[i] = val[i];
409 Total_[i] += val[i];
410 len += val[i] * val[i];
411 }
412 len = sqrt(len);
413 AvgLen_ += (len - AvgLen_) / Count_;
414 AvgLen2_ += (len * len - AvgLen2_) / Count_;
415
416 if (Count_ <= 1) {
417 Max_ = len;
418 Min_ = len;
419 } else {
420 Max_ = len > Max_ ? len : Max_;
421 Min_ = len < Min_ ? len : Min_;
422 }
423 }
424
425 /**
426 * reset the Accumulator to the empty state
427 */
428 void clear() {
429 Count_ = 0;
430 const Vector<RealType, N_INTERACTION_FAMILIES> potVecZero(0.0);
431 Avg_ = potVecZero;
432 Avg2_ = potVecZero;
433 Val_ = potVecZero;
434 Total_ = potVecZero;
435 AvgLen_ = 0;
436 AvgLen2_ = 0;
437 }
438
439 /**
440 * return the most recently added value
441 */
443 ret = Val_;
444 return;
445 }
446
447 /**
448 * compute the Mean
449 */
451 assert(Count_ != 0);
452 ret = Avg_;
453 return;
454 }
455
456 /**
457 * return the Total accumulated sum
458 */
459 void getTotal(ResultType& ret) {
460 assert(Count_ != 0);
461 ret = Total_;
462 return;
463 }
464 /**
465 * compute the Variance
466 */
468 assert(Count_ != 0);
469 for (unsigned int i = 0; i < N_INTERACTION_FAMILIES; i++) {
470 ret[i] = (Avg2_[i] - Avg_[i] * Avg_[i]);
471 if (ret[i] < 0) ret[i] = 0;
472 }
473 return;
474 }
475
476 /**
477 * compute error of average value
478 */
480 assert(Count_ != 0);
481 ResultType var;
482 this->getVariance(var);
483 for (unsigned int i = 0; i < N_INTERACTION_FAMILIES; i++) {
484 ret[i] = sqrt(var[i]);
485 }
486 return;
487 }
488
489 /**
490 * return the 95% confidence interval:
491 *
492 * That is returns c, such that we have 95% confidence that the
493 * true mean is within 2c of the Average (x):
494 *
495 * x - c <= true mean <= x + c
496 *
497 */
499 assert(Count_ != 0);
500 ResultType sd;
501 this->getStdDev(sd);
502 for (unsigned int i = 0; i < N_INTERACTION_FAMILIES; i++) {
503 ret[i] = 1.960 * sd[i] / sqrt(RealType(Count_));
504 }
505 return;
506 }
507
508 /**
509 * return the largest length
510 */
511 void getMaxLength(RealType& ret) {
512 assert(Count_ != 0);
513 ret = Max_;
514 return;
515 }
516
517 /**
518 * return the smallest length
519 */
520 void getMinLength(RealType& ret) {
521 assert(Count_ != 0);
522 ret = Min_;
523 return;
524 }
525
526 /**
527 * return the largest length
528 */
529 void getAverageLength(RealType& ret) {
530 assert(Count_ != 0);
531 ret = AvgLen_;
532 return;
533 }
534
535 /**
536 * compute the Variance of the length
537 */
538 void getLengthVariance(RealType& ret) {
539 assert(Count_ != 0);
540 ret = (AvgLen2_ - AvgLen_ * AvgLen_);
541 if (ret < 0) ret = 0;
542 return;
543 }
544
545 /**
546 * compute error of average value
547 */
548 void getLengthStdDev(RealType& ret) {
549 assert(Count_ != 0);
550 RealType var;
551 this->getLengthVariance(var);
552 ret = sqrt(var);
553 return;
554 }
555
556 /**
557 * return the 95% confidence interval:
558 *
559 * That is returns c, such that we have 95% confidence that the
560 * true mean is within 2c of the Average (x):
561 *
562 * x - c <= true mean <= x + c
563 *
564 */
566 assert(Count_ != 0);
567 RealType sd;
568 this->getLengthStdDev(sd);
569 ret = 1.960 * sd / sqrt(RealType(Count_));
570 return;
571 }
572
573 private:
574 ResultType Val_;
575 ResultType Avg_;
576 ResultType Avg2_;
577 ResultType Total_;
578 RealType AvgLen_;
579 RealType AvgLen2_;
580 RealType Min_;
581 RealType Max_;
582 };
583
585 using ElementType = Mat3x3d;
586 using ResultType = Mat3x3d;
587
588 public:
589 MatrixAccumulator() : BaseAccumulator() { this->clear(); }
590
591 /**
592 * Accumulate another value
593 */
594 void add(ElementType const& val) {
595 Count_++;
596 for (unsigned int i = 0; i < 3; i++) {
597 for (unsigned int j = 0; j < 3; j++) {
598 Avg_(i, j) += (val(i, j) - Avg_(i, j)) / Count_;
599 Avg2_(i, j) += (val(i, j) * val(i, j) - Avg2_(i, j)) / Count_;
600 Val_(i, j) = val(i, j);
601 Total_(i, j) += val(i, j);
602 }
603 }
604 }
605
606 /**
607 * reset the Accumulator to the empty state
608 */
609 void clear() {
610 Count_ = 0;
611 Avg_ *= 0.0;
612 Avg2_ *= 0.0;
613 Val_ *= 0.0;
614 Total_ *= 0.0;
615 }
616
617 /**
618 * return the most recently added value
619 */
621 ret = Val_;
622 return;
623 }
624
625 /**
626 * compute the Mean
627 */
629 assert(Count_ != 0);
630 ret = Avg_;
631 return;
632 }
633
634 /**
635 * return the Total accumulated sum
636 */
637 void getTotal(ResultType& ret) {
638 assert(Count_ != 0);
639 ret = Total_;
640 return;
641 }
642
643 /**
644 * compute the Variance
645 */
647 assert(Count_ != 0);
648 for (unsigned int i = 0; i < 3; i++) {
649 for (unsigned int j = 0; j < 3; j++) {
650 ret(i, j) = (Avg2_(i, j) - Avg_(i, j) * Avg_(i, j));
651 if (ret(i, j) < 0) ret(i, j) = 0;
652 }
653 }
654 return;
655 }
656
657 /**
658 * compute error of average value
659 */
661 assert(Count_ != 0);
662 Mat3x3d var;
663 this->getVariance(var);
664 for (unsigned int i = 0; i < 3; i++) {
665 for (unsigned int j = 0; j < 3; j++) {
666 ret(i, j) = sqrt(var(i, j));
667 }
668 }
669 return;
670 }
671
672 /**
673 * return the 95% confidence interval:
674 *
675 * That is returns c, such that we have 95% confidence that the
676 * true mean is within 2c of the Average (x):
677 *
678 * x - c <= true mean <= x + c
679 *
680 */
682 assert(Count_ != 0);
683 Mat3x3d sd;
684 this->getStdDev(sd);
685 for (unsigned int i = 0; i < 3; i++) {
686 for (unsigned int j = 0; j < 3; j++) {
687 ret(i, j) = 1.960 * sd(i, j) / sqrt(RealType(Count_));
688 }
689 }
690 return;
691 }
692
693 private:
694 ElementType Val_;
695 ResultType Avg_;
696 ResultType Avg2_;
697 ResultType Total_;
698 };
699} // namespace OpenMD
700
701#endif
Basic Accumulator class for numbers.
void getTotal(ResultType &ret)
return the Total accumulated sum
void getAverage(ResultType &ret)
compute the Mean
void getMin(ElementType &ret)
return the smallest value
void getMax(ElementType &ret)
return the largest value
void get95percentConfidenceInterval(ResultType &ret)
return the 95% confidence interval:
void getLastValue(ElementType &ret)
return the most recently added value
virtual void add(ElementType const &val)
Accumulate another value.
void getVariance(ResultType &ret)
compute the Variance
void clear()
reset the Accumulator to the empty state
void getStdDev(ResultType &ret)
compute error of average value
virtual size_t count()
get the number of accumulated values
void getTotal(ResultType &ret)
return the Total accumulated sum
void getVariance(ResultType &ret)
compute the Variance
void getStdDev(ResultType &ret)
compute error of average value
void clear()
reset the Accumulator to the empty state
void getLastValue(ElementType &ret)
return the most recently added value
void add(ElementType const &val)
Accumulate another value.
void getAverage(ResultType &ret)
compute the Mean
void get95percentConfidenceInterval(ResultType &ret)
return the 95% confidence interval:
void getAverage(ResultType &ret)
compute the Mean
void clear()
reset the Accumulator to the empty state
void getLength95percentConfidenceInterval(ResultType &ret)
return the 95% confidence interval:
void getMaxLength(RealType &ret)
return the largest length
void getLengthStdDev(RealType &ret)
compute error of average value
void add(ElementType const &val)
Accumulate another value.
void getVariance(ResultType &ret)
compute the Variance
void getStdDev(ResultType &ret)
compute error of average value
void getMinLength(RealType &ret)
return the smallest length
void getLengthVariance(RealType &ret)
compute the Variance of the length
void getLastValue(ElementType &ret)
return the most recently added value
void getTotal(ResultType &ret)
return the Total accumulated sum
void getAverageLength(RealType &ret)
return the largest length
void get95percentConfidenceInterval(ResultType &ret)
return the 95% confidence interval:
void getMinLength(RealType &ret)
return the smallest length
void getAverage(ResultType &ret)
compute the Mean
void getLengthStdDev(RealType &ret)
compute error of average value
void getMaxLength(RealType &ret)
return the largest length
void getVariance(ResultType &ret)
compute the Variance
void getTotal(ResultType &ret)
return the Total accumulated sum
void clear()
reset the Accumulator to the empty state
void getLastValue(ElementType &ret)
return the most recently added value
void getStdDev(ResultType &ret)
compute error of average value
void getLength95percentConfidenceInterval(ResultType &ret)
return the 95% confidence interval:
void getAverageLength(RealType &ret)
return the largest length
void getLengthVariance(RealType &ret)
compute the Variance of the length
void add(ElementType const &val)
Accumulate another value.
void get95percentConfidenceInterval(ResultType &ret)
return the 95% confidence interval:
This basic Periodic Table class was originally taken from the data.cpp file in OpenBabel.