-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathZeroAccumulator.java
192 lines (172 loc) · 6.66 KB
/
ZeroAccumulator.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package zeroPackage;
import zeroPackage.ZeroMath;
import java.lang.Math;
import java.util.Arrays;
import java.lang.RuntimeException;
public class ZeroAccumulator {
public boolean hasTimeSeries = false;
private double[] timeSeries;
private double duration_h;
private double signalResolution_h = 0.25;
private double timeStep_h = 0.25;
private double sampleWeight = timeStep_h / signalResolution_h;
private int arraySize;
private double sum = 0;
private double posSum = 0;
private double negSum = 0;
private int numStepsAdded = 0;
/**
* Default constructor
*/
public ZeroAccumulator() {
}
/**
* Constructor initializing the fields
*/
public ZeroAccumulator(boolean hasTimeSeries, double signalResolution_h, double duration_h) {
this.hasTimeSeries = hasTimeSeries;
this.signalResolution_h = signalResolution_h;
sampleWeight = timeStep_h / signalResolution_h;
this.duration_h = duration_h;
this.arraySize = (int) Math.round(duration_h / signalResolution_h);
if (hasTimeSeries) { // Allocate memory for timeSeries, only when timeSeries is used.
timeSeries = new double[(int) Math.round(duration_h / signalResolution_h)];
}
}
public void setTimeStep_h(double timeStep_h) {
this.timeStep_h = timeStep_h;
sampleWeight = timeStep_h / signalResolution_h;
}
public void reset() {
sum = 0;
posSum = 0;
negSum = 0;
numStepsAdded = 0;
if (hasTimeSeries) { // Allocate memory for timeSeries, only when timeSeries is used.
timeSeries = new double[(int) Math.round(duration_h / signalResolution_h)];
}
}
// public void addStep(double t_h, double value) {
public void addValue(double t_h, double value) {
if (hasTimeSeries) {
timeSeries[(int) Math.floor(t_h / signalResolution_h)] += value; // averages
// multiple
// timesteps
// when
// timeSeries
// has
// longer
// resolution
// than
// timestep.
} else {
sum += value;
posSum += Math.max(0.0, value);
negSum += Math.min(0.0, value);
}
}
public void addStep(double value) {
if (hasTimeSeries) {
timeSeries[numStepsAdded] += value; // averages
// multiple
// timesteps
// when
// timeSeries
// has
// longer
// resolution
// than
// timestep.
} else {
sum += value;
posSum += Math.max(0.0, value);
negSum += Math.min(0.0, value);
}
numStepsAdded++;
}
public double getSum() {
if (hasTimeSeries) {
sum = ZeroMath.arraySum(timeSeries);
}
return sum;
}
public double getIntegral() { // For getting total energy when addSteps was called with power as value
if (hasTimeSeries) {
sum = ZeroMath.arraySum(timeSeries);
}
return sum * signalResolution_h * sampleWeight;
}
public double getSumPos() {
if (hasTimeSeries) {
posSum = ZeroMath.arraySumPos(timeSeries);
}
return posSum;
}
public double getSumNeg() {
if (hasTimeSeries) {
negSum = ZeroMath.arraySumNeg(timeSeries);
}
return negSum;
}
public double[] getTimeSeries() {
if (!hasTimeSeries) { // Fill timeseries with constant value
double[] timeSeriesTemp = new double[arraySize];
double avgValue = sum / arraySize;
Arrays.fill(timeSeriesTemp, avgValue);
/*
* for (int i = 0; i < arraySize; i++) {
* timeSeries[i] = avgValue;
* }
*/
return timeSeriesTemp;
} else {
return timeSeries;
}
}
public double[] getTimeSeriesIntegral() {
if (!hasTimeSeries) { // Fill timeseries with constant value
double[] timeSeriesTemp = new double[arraySize];
double avgValue = sum / arraySize * sampleWeight;
Arrays.fill(timeSeriesTemp, avgValue);
/*
* for (int i = 0; i < arraySize; i++) {
* timeSeries[i] = avgValue;
* }
*/
return timeSeriesTemp;
} else {
return ZeroMath.arrayMultiply(timeSeries.clone(), sampleWeight);
}
}
public Double getY(int i) {
if (!hasTimeSeries) {
return null;
} else {
return timeSeries[i];
}
}
public ZeroAccumulator add(ZeroAccumulator acc) {
if ((this.hasTimeSeries && acc.hasTimeSeries) && (this.duration_h == acc.duration_h)
&& (this.signalResolution_h == acc.signalResolution_h)) {
for (int i = 0; i < timeSeries.length; i++) {
this.timeSeries[i] += acc.timeSeries[i];
}
} else {
throw new RuntimeException("Impossible to add these incompatible accumulators");
// throw some error? or make some assumptions?
}
return this;
}
public ZeroAccumulator subtract(ZeroAccumulator acc) {
if ((this.hasTimeSeries && acc.hasTimeSeries) && (this.duration_h == acc.duration_h)
&& (this.signalResolution_h == acc.signalResolution_h)) {
for (int i = 0; i < timeSeries.length; i++) {
this.timeSeries[i] -= acc.timeSeries[i];
}
} else {
throw new RuntimeException("Impossible to subtract these incompatible accumulators");
// throw some error? or make some assumptions?
}
return this;
}
}