ls1-MarDyn
ls1-MarDyn molecular dynamics code
Accumulator.h
1#ifndef ACCUMULATOR_H
2#define ACCUMULATOR_H
3
4/*
5 * Copyright (c) 2013 Christoph Niethammer <christoph.niethammer@gmail.com>
6 */
7
8#include <algorithm>
9#include "utils/mardyn_assert.h"
10#include <cmath>
11#include <cstdlib>
12
13
18template <typename T>
19class Accumulator {
20public:
24 Accumulator(size_t windowLength = 100) : _windowLength(windowLength), _insertPosition(0), _size(0) {
25 _data = new T[windowLength];
26 }
29 delete[] _data;
30 }
31
36 T sum = 0;
37 for( size_t i = 0; i < _size; i++ ) {
38 sum += _data[i];
39 }
40 return sum / _size;
41 }
42
47 T avg = getAverage();
48 T stddev = 0;
49 for( size_t i = 0; i < _size; i++ ) {
50 T diff = _data[i] - avg;
51 stddev += diff*diff;
52 }
53 stddev /= getSize();
54 stddev = sqrt(stddev);
55 return stddev;
56 }
57
62 T getPercentile(double p) {
63 T *sorted_data = new T[_size];
64 std::copy(_data, _data + _size, sorted_data);
65 std::sort(sorted_data, sorted_data + _size);
66 T retValue = sorted_data[static_cast<size_t>(_size * p) - 1];
67 delete[] sorted_data;
68 return retValue;
69 }
70
76 T getInterPercentileRange(double pmin, double pmax) {
77 T *sorted_data = new T[_size];
78 std::copy(_data, _data + _size, sorted_data);
79 std::sort(sorted_data, sorted_data + _size);
80 T qmin = sorted_data[static_cast<size_t>(_size * pmin) - 1];
81 T qmax = sorted_data[static_cast<size_t>(_size * pmax) - 1];
82 T retValue = qmax - qmin;
83 delete[] sorted_data;
84 return retValue;
85 }
86
90 void addEntry(T value) {
91 _data[_insertPosition] = value;
92 _insertPosition = (_insertPosition + 1) % _windowLength;
93 if (_size < _windowLength)
94 _size += 1;
95 }
96
101 return getEntry(1);
102 }
103
108 T getEntry(size_t id) {
109 mardyn_assert(0 < id && id <= _windowLength);
110 return _data[(_insertPosition + _windowLength - id) % _windowLength];
111 }
112
117 return _windowLength;
118 }
119
126 size_t getSize() {
127 return _size;
128 }
129
130private:
131 size_t _windowLength;
132 size_t _insertPosition;
133 size_t _size;
134 T *_data;
135};
136
137#endif /* ACCUMULATOR_H */
Definition: TemperatureControl.h:211
T getInterPercentileRange(double pmin, double pmax)
Definition: Accumulator.h:76
void addEntry(T value)
Definition: Accumulator.h:90
Accumulator(size_t windowLength=100)
Definition: Accumulator.h:24
T getEntry(size_t id)
Definition: Accumulator.h:108
T getAverage()
Definition: Accumulator.h:35
T getPercentile(double p)
Definition: Accumulator.h:62
~Accumulator()
Definition: Accumulator.h:28
T getLastEntry()
Definition: Accumulator.h:100
size_t getWindowLength()
Definition: Accumulator.h:116
size_t getSize()
Definition: Accumulator.h:126
T getStddev()
Definition: Accumulator.h:46
::xsd::cxx::tree::id< char, ncname > id
C++ type corresponding to the ID XML Schema built-in type.
Definition: vtk-punstructured.h:322