-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcompare_models.py
323 lines (258 loc) · 8 KB
/
compare_models.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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
#
# Compare "full" 2D (COMSOL) to recued 1+1D DFN and DFNCC
# Here "bar" refers to the averaged through-cell model (i.e. DFNCC)
#
import pybamm
import sys
import pickle
import matplotlib
import matplotlib.pyplot as plt
import shared
import numpy as np
# set style
matplotlib.rc_file("_matplotlibrc", use_default_template=True)
# increase recursion limit for large expression trees
sys.setrecursionlimit(100000)
pybamm.set_logging_level("INFO")
"-----------------------------------------------------------------------------"
"Load comsol data"
comsol_variables = pickle.load(open("comsol_data/comsol_1plus1D_3C.pickle", "rb"))
"-----------------------------------------------------------------------------"
"Set up and solve pybamm simulation"
# load current collector and DFN models
cc_model = pybamm.current_collector.EffectiveResistance1D()
dfn_av = pybamm.lithium_ion.DFN({"thermal": "x-lumped"}, name="Average DFN")
dfn = pybamm.lithium_ion.DFN(
{"current collector": "potential pair", "dimensionality": 1, "thermal": "x-lumped"},
name="1+1D DFN",
)
models = {"Current collector": cc_model, "Average DFN": dfn_av, "1+1D DFN": dfn}
# parameters
param = dfn.default_parameter_values
param.update({"C-rate": 3})
# process model and geometry, and discretise
meshes = {}
for name, model in models.items():
param.process_model(model)
geometry = model.default_geometry
param.process_geometry(geometry)
# set mesh
var = pybamm.standard_spatial_vars
submesh_types = model.default_submesh_types
# set npts
var = pybamm.standard_spatial_vars
npts = 16
var_pts = {
var.x_n: npts,
var.x_s: npts,
var.x_p: npts,
var.r_n: npts,
var.r_p: npts,
var.z: npts,
}
meshes[name] = pybamm.Mesh(geometry, submesh_types, var_pts)
disc = pybamm.Discretisation(meshes[name], model.default_spatial_methods)
disc.process_model(model, check_model=False)
# discharge timescale
tau = param.evaluate(pybamm.standard_parameters_lithium_ion.tau_discharge)
# solve model at comsol times
t_eval = comsol_variables["time"] / tau
solutions = {}
for name, model in models.items():
if name == "Current collector":
solver = pybamm.AlgebraicSolver(tol=1e-6)
solutions[name] = solver.solve(model)
else:
# solver
solver = pybamm.CasadiSolver(
atol=1e-6, rtol=1e-6, root_tol=1e-3, root_method="hybr", mode="fast"
)
solutions[name] = solver.solve(model, t_eval)
"-----------------------------------------------------------------------------"
"Make Comsol 'model' for comparison"
mesh = meshes["1+1D DFN"]
comsol_model = shared.make_comsol_model(comsol_variables, mesh, param, thermal=True)
"-----------------------------------------------------------------------------"
"Make plots"
t_plot = comsol_model.t
z_plot = comsol_model.z_interp
t_slices = np.array([600, 1200, 1800, 2400, 3000]) / 3
# get processed potentials from DFNCC
R_cc = param.process_symbol(
cc_model.variables["Effective current collector resistance"]
).evaluate(t=solutions["Current collector"].t, y=solutions["Current collector"].y)[0][0]
V_av_1D = pybamm.ProcessedVariable(
dfn_av.variables["Terminal voltage"],
solutions["Average DFN"].t,
solutions["Average DFN"].y,
mesh=meshes["Average DFN"],
)
I_av = pybamm.ProcessedVariable(
dfn_av.variables["Total current density"],
solutions["Average DFN"].t,
solutions["Average DFN"].y,
mesh=meshes["Average DFN"],
)
def V_av(t):
return V_av_1D(t) - I_av(t) * R_cc
potentials = cc_model.get_processed_potentials(
solutions["Current collector"], meshes["Current collector"], param, V_av, I_av
)
# plot negative current collector potential
var = "Negative current collector potential [V]"
comsol_var_fun = pybamm.ProcessedVariable(
comsol_model.variables[var],
solutions["1+1D DFN"].t,
solutions["1+1D DFN"].y,
mesh=meshes["1+1D DFN"],
)
pybamm_var_fun = pybamm.ProcessedVariable(
models["1+1D DFN"].variables[var],
solutions["1+1D DFN"].t,
solutions["1+1D DFN"].y,
mesh=meshes["1+1D DFN"],
)
pybamm_bar_var_fun = potentials[var]
shared.plot_tz_var(
t_plot,
z_plot,
t_slices,
"$\phi^*_{\mathrm{s,cn}}$",
"[V]",
comsol_var_fun,
pybamm_var_fun,
pybamm_bar_var_fun,
param,
cmap="cividis",
)
# plot positive current collector potential (with respect to terminal voltage)
var = "Positive current collector potential [V]"
comsol_var = pybamm.ProcessedVariable(
comsol_model.variables[var],
solutions["1+1D DFN"].t,
solutions["1+1D DFN"].y,
mesh=meshes["1+1D DFN"],
)
V_comsol = pybamm.ProcessedVariable(
comsol_model.variables["Terminal voltage [V]"],
solutions["1+1D DFN"].t,
solutions["1+1D DFN"].y,
mesh=meshes["1+1D DFN"],
)
def comsol_var_fun(t, z):
return comsol_var(t=t, z=z) - V_comsol(t=t)
pybamm_var = pybamm.ProcessedVariable(
models["1+1D DFN"].variables[var],
solutions["1+1D DFN"].t,
solutions["1+1D DFN"].y,
mesh=meshes["1+1D DFN"],
)
V = pybamm.ProcessedVariable(
models["1+1D DFN"].variables["Terminal voltage [V]"],
solutions["1+1D DFN"].t,
solutions["1+1D DFN"].y,
mesh=meshes["1+1D DFN"],
)
def pybamm_var_fun(t, z):
return pybamm_var(t=t, z=z) - V(t=t)
pybamm_bar_var = potentials[var]
pot_scale = param.evaluate(pybamm.standard_parameters_lithium_ion.potential_scale)
U_ref = param.evaluate(pybamm.standard_parameters_lithium_ion.U_p_ref) - param.evaluate(
pybamm.standard_parameters_lithium_ion.U_n_ref
)
def V_av_dim(t):
return U_ref + V_av(t) * pot_scale
def pybamm_bar_var_fun(t, z):
return pybamm_bar_var(t=t, z=z) - V_av_dim(t)
shared.plot_tz_var(
t_plot,
z_plot,
t_slices,
"$\phi^*_{\mathrm{s,cp}} - V^*$",
"[V]",
comsol_var_fun,
pybamm_var_fun,
pybamm_bar_var_fun,
param,
cmap="viridis",
)
# plot through-cell current
var = "Current collector current density [A.m-2]"
comsol_var_fun = pybamm.ProcessedVariable(
comsol_model.variables[var],
solutions["1+1D DFN"].t,
solutions["1+1D DFN"].y,
mesh=meshes["1+1D DFN"],
)
pybamm_var_fun = pybamm.ProcessedVariable(
models["1+1D DFN"].variables[var],
solutions["1+1D DFN"].t,
solutions["1+1D DFN"].y,
mesh=meshes["1+1D DFN"],
)
I_av = pybamm.ProcessedVariable(
dfn_av.variables["Current collector current density [A.m-2]"],
solutions["Average DFN"].t,
solutions["Average DFN"].y,
mesh=meshes["Average DFN"],
)
def pybamm_bar_var_fun(t, z):
if t.shape[0] == 1:
return np.repeat(I_av(t)[:, np.newaxis], len(z), axis=0)
else:
return np.transpose(np.repeat(I_av(t)[:, np.newaxis], len(z), axis=1))
shared.plot_tz_var(
t_plot,
z_plot,
t_slices,
"$\mathcal{I}^*$",
"[A/m${}^2$]",
comsol_var_fun,
pybamm_var_fun,
pybamm_bar_var_fun,
param,
cmap="plasma",
)
# plot temperature
T_ref = param.evaluate(pybamm.standard_parameters_lithium_ion.T_ref)
var = "X-averaged cell temperature [K]"
comsol_var = pybamm.ProcessedVariable(
comsol_model.variables[var],
solutions["1+1D DFN"].t,
solutions["1+1D DFN"].y,
mesh=meshes["1+1D DFN"],
)
def comsol_var_fun(t, z):
return comsol_var(t=t, z=z) - T_ref
pybamm_var = pybamm.ProcessedVariable(
models["1+1D DFN"].variables[var],
solutions["1+1D DFN"].t,
solutions["1+1D DFN"].y,
mesh=meshes["1+1D DFN"],
)
def pybamm_var_fun(t, z):
return pybamm_var(t=t, z=z) - T_ref
T_av = pybamm.ProcessedVariable(
dfn_av.variables["X-averaged cell temperature [K]"],
solutions["Average DFN"].t,
solutions["Average DFN"].y,
mesh=meshes["Average DFN"],
)
def pybamm_bar_var_fun(t, z):
if t.shape[0] == 1:
return np.repeat(T_av(t)[:, np.newaxis], len(z), axis=0) - T_ref
else:
return np.transpose(np.repeat(T_av(t)[:, np.newaxis], len(z), axis=1)) - T_ref
shared.plot_tz_var(
t_plot,
z_plot,
t_slices,
"$\\bar{T}^* - \\bar{T}_0^*$",
"[K]",
comsol_var_fun,
pybamm_var_fun,
pybamm_bar_var_fun,
param,
cmap="inferno",
)
plt.show()