-
Notifications
You must be signed in to change notification settings - Fork 12
/
functions.py
67 lines (52 loc) · 1.48 KB
/
functions.py
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
#!/usr/bin/env python3
import sys
import logging
class Functions:
# Exception safe max and min (attention, not working with dictionaries)
# another option: res = [i for i in test_list if i is not None]
def _max(self, x):
try:
return max(x)
except Exception:
return None
def _min(self, x):
try:
return min(x)
except Exception:
return None
# Interpolate f(x) if given lists Y = f(X)
def _interpolate(self, X, Y, x):
if len(X) == len(Y):
_len = len(X)
if x <= X[0]:
return Y[0]
elif x >= X[_len - 1]:
return Y[_len - 1]
else:
for i in range(_len - 1):
if x <= X[i + 1]:
return Y[i] + (Y[i + 1] - Y[i]) / (X[i + 1] - X[i]) * (x - X[i])
else:
logging.error("Both lists must have the same length. Exiting.")
sys.exit()
################
# test program #
################
def main():
import settings as s
fn = Functions()
for x in range(0, 251):
print(
"%.2f %.0f"
% (
x / 100.0,
s.MAX_CHARGE_CURRENT
* fn._interpolate(
s.CELL_CHARGE_LIMITING_VOLTAGE,
s.CELL_CHARGE_LIMITED_CURRENT,
x / 100.0,
),
)
)
if __name__ == "__main__":
main()