forked from FaradayInstitution/BPX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_schema.py
263 lines (242 loc) · 10.2 KB
/
test_schema.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
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
import unittest
import copy
from pydantic import parse_obj_as, ValidationError
from bpx import BPX
class TestSchema(unittest.TestCase):
def setUp(self):
self.base = {
"Header": {
"BPX": 1.0,
"Model": "DFN",
},
"Parameterisation": {
"Cell": {
"Ambient temperature [K]": 299.0,
"Initial temperature [K]": 299.0,
"Reference temperature [K]": 299.0,
"Electrode area [m2]": 2.0,
"External surface area [m2]": 2.2,
"Volume [m3]": 1.0,
"Number of electrode pairs connected in parallel to make a cell": 1,
"Nominal cell capacity [A.h]": 5.0,
"Lower voltage cut-off [V]": 2.0,
"Upper voltage cut-off [V]": 4.0,
},
"Electrolyte": {
"Initial concentration [mol.m-3]": 1000,
"Cation transference number": 0.259,
"Conductivity [S.m-1]": 1.0,
"Diffusivity [m2.s-1]": (
"8.794e-7 * x * x - 3.972e-6 * x + 4.862e-6"
),
},
"Negative electrode": {
"Particle radius [m]": 5.86e-6,
"Thickness [m]": 85.2e-6,
"Diffusivity [m2.s-1]": 3.3e-14,
"OCP [V]": {"x": [0, 0.1, 1], "y": [1.72, 1.2, 0.06]},
"Conductivity [S.m-1]": 215.0,
"Surface area per unit volume [m-1]": 383959,
"Porosity": 0.25,
"Transport efficiency": 0.125,
"Reaction rate constant [mol.m-2.s-1]": 1e-10,
"Maximum concentration [mol.m-3]": 33133,
"Minimum stoichiometry": 0.01,
"Maximum stoichiometry": 0.99,
},
"Positive electrode": {
"Thickness [m]": 75.6e-6,
"Conductivity [S.m-1]": 0.18,
"Porosity": 0.335,
"Transport efficiency": 0.1939,
"Particle": {
"Primary": {
"Particle radius [m]": 5.22e-6,
"Diffusivity [m2.s-1]": 4.0e-15,
"OCP [V]": {"x": [0, 0.1, 1], "y": [1.72, 1.2, 0.06]},
"Surface area per unit volume [m-1]": 382184,
"Reaction rate constant [mol.m-2.s-1]": 1e-10,
"Maximum concentration [mol.m-3]": 63104.0,
"Minimum stoichiometry": 0.1,
"Maximum stoichiometry": 0.9,
},
"Secondary": {
"Particle radius [m]": 10.0e-6,
"Diffusivity [m2.s-1]": 4.0e-15,
"OCP [V]": {"x": [0, 0.1, 1], "y": [1.72, 1.2, 0.06]},
"Surface area per unit volume [m-1]": 382184,
"Reaction rate constant [mol.m-2.s-1]": 1e-10,
"Maximum concentration [mol.m-3]": 63104.0,
"Minimum stoichiometry": 0.1,
"Maximum stoichiometry": 0.9,
},
},
},
"Separator": {
"Thickness [m]": 1.2e-5,
"Porosity": 0.47,
"Transport efficiency": 0.3222,
},
},
}
# SPM parameter set
self.base_spm = {
"Header": {
"BPX": 1.0,
"Model": "SPM",
},
"Parameterisation": {
"Cell": {
"Ambient temperature [K]": 299.0,
"Initial temperature [K]": 299.0,
"Reference temperature [K]": 299.0,
"Electrode area [m2]": 2.0,
"External surface area [m2]": 2.2,
"Volume [m3]": 1.0,
"Number of electrode pairs connected in parallel to make a cell": 1,
"Nominal cell capacity [A.h]": 5.0,
"Lower voltage cut-off [V]": 2.0,
"Upper voltage cut-off [V]": 4.0,
},
"Negative electrode": {
"Particle radius [m]": 5.86e-6,
"Thickness [m]": 85.2e-6,
"Diffusivity [m2.s-1]": 3.3e-14,
"OCP [V]": {"x": [0, 0.1, 1], "y": [1.72, 1.2, 0.06]},
"Surface area per unit volume [m-1]": 383959,
"Reaction rate constant [mol.m-2.s-1]": 1e-10,
"Maximum concentration [mol.m-3]": 33133,
"Minimum stoichiometry": 0.01,
"Maximum stoichiometry": 0.99,
},
"Positive electrode": {
"Thickness [m]": 75.6e-6,
"Particle": {
"Primary": {
"Particle radius [m]": 5.22e-6,
"Diffusivity [m2.s-1]": 4.0e-15,
"OCP [V]": {"x": [0, 0.1, 1], "y": [1.72, 1.2, 0.06]},
"Surface area per unit volume [m-1]": 382184,
"Reaction rate constant [mol.m-2.s-1]": 1e-10,
"Maximum concentration [mol.m-3]": 63104.0,
"Minimum stoichiometry": 0.1,
"Maximum stoichiometry": 0.9,
},
"Secondary": {
"Particle radius [m]": 10.0e-6,
"Diffusivity [m2.s-1]": 4.0e-15,
"OCP [V]": {"x": [0, 0.1, 1], "y": [1.72, 1.2, 0.06]},
"Surface area per unit volume [m-1]": 382184,
"Reaction rate constant [mol.m-2.s-1]": 1e-10,
"Maximum concentration [mol.m-3]": 63104.0,
"Minimum stoichiometry": 0.1,
"Maximum stoichiometry": 0.9,
},
},
},
},
}
def test_simple(self):
test = copy.copy(self.base)
parse_obj_as(BPX, test)
def test_simple_spme(self):
test = copy.copy(self.base)
test["Header"]["Model"] = "SPMe"
parse_obj_as(BPX, test)
def test_simple_spm(self):
test = copy.copy(self.base_spm)
parse_obj_as(BPX, test)
def test_bad_model(self):
test = copy.copy(self.base)
test["Header"]["Model"] = "Wrong model type"
with self.assertRaises(ValidationError):
parse_obj_as(BPX, test)
def test_bad_dfn(self):
test = copy.copy(self.base_spm)
test["Header"]["Model"] = "DFN"
with self.assertWarnsRegex(
UserWarning,
"The model type DFN does not correspond to the parameter set",
):
parse_obj_as(BPX, test)
def test_bad_spme(self):
test = copy.copy(self.base_spm)
test["Header"]["Model"] = "SPMe"
with self.assertWarnsRegex(
UserWarning,
"The model type SPMe does not correspond to the parameter set",
):
parse_obj_as(BPX, test)
def test_bad_spm(self):
test = copy.copy(self.base)
test["Header"]["Model"] = "SPM"
with self.assertWarnsRegex(
UserWarning,
"The model type SPM does not correspond to the parameter set",
):
parse_obj_as(BPX, test)
def test_table(self):
test = copy.copy(self.base)
test["Parameterisation"]["Electrolyte"]["Conductivity [S.m-1]"] = {
"x": [1.0, 2.0],
"y": [2.3, 4.5],
}
parse_obj_as(BPX, test)
def test_bad_table(self):
test = copy.copy(self.base)
test["Parameterisation"]["Electrolyte"]["Conductivity [S.m-1]"] = {
"x": [1.0, 2.0],
"y": [2.3],
}
with self.assertRaisesRegex(
ValidationError,
"x & y should be same length",
):
parse_obj_as(BPX, test)
def test_function(self):
test = copy.copy(self.base)
test["Parameterisation"]["Electrolyte"]["Conductivity [S.m-1]"] = "1.0 * x + 3"
parse_obj_as(BPX, test)
def test_function_with_exp(self):
test = copy.copy(self.base)
test["Parameterisation"]["Electrolyte"][
"Conductivity [S.m-1]"
] = "1.0 * exp(x) + 3"
parse_obj_as(BPX, test)
def test_bad_function(self):
test = copy.copy(self.base)
test["Parameterisation"]["Electrolyte"][
"Conductivity [S.m-1]"
] = "this is not a function"
with self.assertRaises(ValidationError):
parse_obj_as(BPX, test)
def test_to_python_function(self):
test = copy.copy(self.base)
test["Parameterisation"]["Electrolyte"]["Conductivity [S.m-1]"] = "2.0 * x"
obj = parse_obj_as(BPX, test)
funct = obj.parameterisation.electrolyte.conductivity
pyfunct = funct.to_python_function()
self.assertEqual(pyfunct(2.0), 4.0)
def test_bad_input(self):
test = copy.copy(self.base)
test["Parameterisation"]["Electrolyte"]["bad"] = "this shouldn't be here"
with self.assertRaises(ValidationError):
parse_obj_as(BPX, test)
def test_validation_data(self):
test = copy.copy(self.base)
test["Validation"] = {
"Experiment 1": {
"Time [s]": [0, 1000, 2000],
"Current [A]": [-0.625, -0.625, -0.625],
"Voltage [V]": [4.19367569, 4.1677888, 4.14976386],
"Temperature [K]": [298.15, 298.15, 298.15],
},
"Experiment 2": {
"Time [s]": [0, 1000],
"Current [A]": [-0.625, -0.625],
"Voltage [V]": [4.19367569, 4.1677888],
"Temperature [K]": [298.15, 298.15],
},
}
if __name__ == "__main__":
unittest.main()