-
-
Notifications
You must be signed in to change notification settings - Fork 606
/
Copy pathtest_solution.py
129 lines (110 loc) · 4.15 KB
/
test_solution.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
#
# Tests for the Solution class
#
import pybamm
import unittest
import numpy as np
class TestSolution(unittest.TestCase):
def test_append(self):
model = pybamm.lithium_ion.SPMe()
# create geometry
geometry = model.default_geometry
# load parameter values and process model and geometry
param = model.default_parameter_values
param.process_model(model)
param.process_geometry(geometry)
# set mesh
mesh = pybamm.Mesh(geometry, model.default_submesh_types, model.default_var_pts)
# discretise model
disc = pybamm.Discretisation(mesh, model.default_spatial_methods)
disc.process_model(model)
# solve model
t_eval = np.linspace(0, 3600, 100)
solver = model.default_solver
solution = solver.solve(model, t_eval)
# step model
old_t = 0
step_solver = model.default_solver
step_solution = None
# dt should be dimensional
solution_times_dimensional = solution.t * model.timescale_eval
for t in solution_times_dimensional[1:]:
dt = t - old_t
step_solution = step_solver.step(step_solution, model, dt=dt, npts=10)
if t == solution_times_dimensional[1]:
# Create voltage variable
step_solution.update("Terminal voltage")
old_t = t
# Step solution should have been updated as we go along so be quicker to
# calculate
timer = pybamm.Timer()
step_solution.update("Terminal voltage")
step_sol_time = timer.time()
timer.reset()
solution.update("Terminal voltage")
sol_time = timer.time()
self.assertLess(step_sol_time, sol_time)
# Check both give the same answer
np.testing.assert_array_almost_equal(
solution["Terminal voltage"](solution.t[:-1]),
step_solution["Terminal voltage"](solution.t[:-1]),
decimal=4,
)
def test_append_external_variables(self):
model = pybamm.lithium_ion.SPM(
{
"thermal": "lumped",
"external submodels": ["thermal", "negative particle"],
}
)
# create geometry
geometry = model.default_geometry
# load parameter values and process model and geometry
param = model.default_parameter_values
param.process_model(model)
param.process_geometry(geometry)
# set mesh
mesh = pybamm.Mesh(geometry, model.default_submesh_types, model.default_var_pts)
# discretise model
disc = pybamm.Discretisation(mesh, model.default_spatial_methods)
disc.process_model(model)
# solve model
solver = model.default_solver
T_av = 0
c_s_n_av = np.ones((10, 1)) * 0.6
external_variables = {
"Volume-averaged cell temperature": T_av,
"X-averaged negative particle concentration": c_s_n_av,
}
# Step
dt = 0.1
sol_step = None
for _ in range(5):
sol_step = solver.step(
sol_step, model, dt, external_variables=external_variables
)
np.testing.assert_array_equal(
sol_step.inputs["Volume-averaged cell temperature"],
np.zeros((1, len(sol_step.t))),
)
np.testing.assert_array_equal(
sol_step.inputs["X-averaged negative particle concentration"],
np.ones((mesh["negative particle"][0].npts, len(sol_step.t))) * 0.6,
)
# Solve
t_eval = np.linspace(0, 3600)
sol = solver.solve(model, t_eval, external_variables=external_variables)
np.testing.assert_array_equal(
sol.inputs["Volume-averaged cell temperature"], np.zeros((1, len(sol.t))),
)
np.testing.assert_array_equal(
sol.inputs["X-averaged negative particle concentration"],
np.ones((mesh["negative particle"][0].npts, len(sol.t))) * 0.6,
)
if __name__ == "__main__":
print("Add -v for more debug output")
import sys
if "-v" in sys.argv:
debug = True
pybamm.settings.debug_mode = True
unittest.main()