3D pouch cell
Solve a custom 3D DFN of a pouch cell with tabs on a hand-built finite-volume mesh.
Write a DFN for a pouch cell out equation by equation, rather than picking a built-in
model. It spans both current collectors with their tabs, both electrodes and the
separator, each meshed by hand with hexahedra and solved with PyBaMM's unstructured
finite volumes. solve sends the model and its meshes to Ionworks as they are. Two
limits
matter when you extend it: only scalar outputs such as the voltage come back, not
fields on the 3D mesh, and the meshes must be built by hand as here, because a 3D mesh
from a generator is refused. The negative tab is earthed, the positive tab
carries the current, and both tabs are held at ambient temperature, so the cell
warms over the 1C discharge and cools through its tabs during the rest.
This example uses a published PyBaMM parameter set, so you can
copy it and run it as-is on Ionworks. Set IONWORKS_PYBAMM_LOCAL=1 to run the same script with
PyBaMM on your own machine.
import ionworks.pybamm as pybamm
import matplotlib.pyplot as plt
import numpy as np
parameter_values = pybamm.ParameterValues("Marquis2019")
param = pybamm.LithiumIonParameters()
def length(symbol):
return float(parameter_values.evaluate(symbol))
# Through-cell layers, left to right, with their number of cells in x.
layers = {
"negative current collector": (length(param.n.L_cc), 2),
"negative electrode": (length(param.n.L), 4),
"separator": (length(param.s.L), 3),
"positive electrode": (length(param.p.L), 4),
"positive current collector": (length(param.p.L_cc), 2),
}
ncc, neg, sep, pos, pcc = layers
L_y, L_z, tab_height = length(param.L_y), length(param.L_z), 0.01
tabs = {
ncc: (length(param.n.centre_y_tab), length(param.n.L_tab)),
pcc: (length(param.p.centre_y_tab), length(param.p.L_tab)),
}
# Meshes: one hexahedral block per layer, plus a tab on each collector.
def hex_grid(*edges):
nodes = np.stack(np.meshgrid(*edges, indexing="ij"), -1).reshape(-1, 3)
i, j, k = (
index.ravel()
for index in np.meshgrid(
*(np.arange(len(edge) - 1) for edge in edges), indexing="ij"
)
)
p, q = len(edges[1]) * len(edges[2]), len(edges[2])
n0 = i * p + j * q + k
corners = [0, p, p + q, q, 1, p + 1, p + q + 1, q + 1]
return nodes, np.column_stack([n0 + corner for corner in corners])
# Two cells across each tab and each gap between tabs, so tab edges are faces.
y_knots = [0.0, L_y]
for centre, width in tabs.values():
y_knots += [centre - width / 2, centre + width / 2]
y_knots = sorted(y_knots)
y_edges = np.unique(
np.concatenate(
[np.linspace(a, b, 3) for a, b in zip(y_knots, y_knots[1:], strict=False)]
)
)
z_edges = np.linspace(0, L_z, 11)
z_tab = np.linspace(L_z, L_z + tab_height, 4)
meshes, x_left = {}, 0.0
for domain, (thickness, n_x) in layers.items():
x_edges = np.linspace(x_left, x_left + thickness, n_x + 1)
mesh = pybamm.UnstructuredSubMesh(*hex_grid(x_edges, y_edges, z_edges))
if domain in tabs:
centre, width = tabs[domain]
across = np.abs(y_edges - centre) <= width / 2 + 1e-12
tab = pybamm.UnstructuredSubMesh(*hex_grid(x_edges, y_edges[across], z_tab))
mesh = pybamm.UnstructuredSubMesh.combine([mesh, tab])
mesh.detect_box_boundaries()
# The tab's own faces get their own names, so it can carry the current.
faces = dict(mesh.boundary_faces)
mesh.boundary_faces = {}
for name, index in faces.items():
on_tab = mesh.face_centroids[index, 2] > L_z + 1e-10
for tag, part in ((name, index[~on_tab]), (f"tab_{name}", index[on_tab])):
if len(part) > 0:
mesh.boundary_faces[tag] = part
meshes[domain] = mesh
x_left += thickness
for left, right in zip(layers, list(layers)[1:], strict=False):
pybamm.compute_interface_data(meshes[left], meshes[right], left, right)
def variable(name, domain):
return pybamm.Variable(name, domain=domain)
def over(domain, value):
return pybamm.PrimaryBroadcast(value, domain)
phi_s_ncc = variable("Negative CC potential [V]", ncc)
phi_s_neg = variable("Negative electrode potential [V]", neg)
phi_s_pos = variable("Positive electrode potential [V]", pos)
phi_s_pcc = variable("Positive CC potential [V]", pcc)
# Each collector and its electrode share one solid potential.
phi_s_n = pybamm.concatenation(phi_s_ncc, phi_s_neg)
phi_s_p = pybamm.concatenation(phi_s_pos, phi_s_pcc)
c_e_n, c_e_s, c_e_p = (
variable(f"{d.capitalize()} electrolyte concentration [mol.m-3]", d)
for d in (neg, sep, pos)
)
c_e = pybamm.concatenation(c_e_n, c_e_s, c_e_p)
phi_e_n, phi_e_s, phi_e_p = (
variable(f"{d.capitalize()} electrolyte potential [V]", d)
for d in (neg, sep, pos)
)
phi_e = pybamm.concatenation(phi_e_n, phi_e_s, phi_e_p)
T_ncc, T_n, T_s, T_p, T_pcc = (
variable(f"{d.capitalize()} temperature [K]", d) for d in layers
)
T = pybamm.concatenation(T_ncc, T_n, T_s, T_p, T_pcc)
T_e = pybamm.concatenation(T_n, T_s, T_p)
c_s_n = pybamm.Variable(
"Negative particle concentration [mol.m-3]",
domain="negative particle",
auxiliary_domains={"secondary": neg},
)
c_s_p = pybamm.Variable(
"Positive particle concentration [mol.m-3]",
domain="positive particle",
auxiliary_domains={"secondary": pos},
)
# The layers bring their own meshes. Nothing is solved on the "current collector"
# point, but pybamm cannot build the model without it.
geometry = {
"negative particle": {"r_n": {"min": 0, "max": param.n.prim.R_typ}},
"positive particle": {"r_p": {"min": 0, "max": param.p.prim.R_typ}},
"current collector": {"z": {"position": 0}},
}
def coordinates(domain):
x, z = (
pybamm.SpatialVariable(
f"{axis}_{domain}",
domain=domain,
coord_sys="cartesian",
direction=direction,
)
for axis, direction in (("x", "lr"), ("z", "tb"))
)
return {"Through-cell distance (x) [m]": x, "Vertical distance (z) [m]": z}
eps_n, eps_s, eps_p = (
pybamm.FunctionParameter(f"{d.capitalize()} porosity", coordinates(d))
for d in (neg, sep, pos)
)
eps = pybamm.concatenation(eps_n, eps_s, eps_p)
tor = pybamm.concatenation(
eps_n**param.n.b_e, eps_s**param.s.b_e, eps_p**param.p.b_e
)
eps_s_n, eps_s_p = (
pybamm.FunctionParameter(
f"{d.capitalize()} active material volume fraction", coordinates(d)
)
for d in (neg, pos)
)
def reaction(electrode, c_s, c_e, phi_s, phi_e, T):
c_s_surf = pybamm.surf(c_s)
sto = c_s_surf / electrode.c_max
eta = phi_s - phi_e - electrode.U(sto, T)
j = (
2
* electrode.j0(c_e, c_s_surf, T)
* pybamm.sinh(electrode.ne / 2 * param.F * eta / (param.R * T))
)
a_j = 3 * electrode.epsilon_s_av / electrode.R_typ * j
return j, a_j, a_j * (eta + T * electrode.dUdT(sto))
j_n, a_j_n, heat_n = reaction(param.n.prim, c_s_n, c_e_n, phi_s_neg, phi_e_n, T_n)
j_p, a_j_p, heat_p = reaction(param.p.prim, c_s_p, c_e_p, phi_s_pos, phi_e_p, T_p)
a_j = pybamm.concatenation(a_j_n, over(sep, 0), a_j_p)
model = pybamm.BaseModel("3D pouch DFN")
# ``solve(initial_soc=...)`` reads the stoichiometry limits from here.
model.param = param
for electrode, c_s, T_k, j in (
(param.n.prim, c_s_n, T_n, j_n),
(param.p.prim, c_s_p, T_p, j_p),
):
D = electrode.D(c_s, over(c_s.domain, T_k))
model.rhs[c_s] = pybamm.div(D * pybamm.grad(c_s))
model.boundary_conditions[c_s] = {
"left": (pybamm.Scalar(0), "Neumann"),
"right": (-j / (param.F * pybamm.surf(D)), "Neumann"),
}
model.initial_conditions[c_s] = electrode.c_init
# Boundary conditions: insulated everywhere unless a face says otherwise.
box = ("left", "right", "front", "back", "bottom", "top")
def insulated(tab=False, **faces):
names = box + tuple(f"tab_{name}" for name in box if tab and name != "bottom")
return {name: faces.get(name, (pybamm.Scalar(0), "Neumann")) for name in names}
# Solid potential: the negative tab is earthed, the positive carries I. Unscaled,
# the algebraic equations leave the temperature short of its converged value.
scale = param.L_x**2 * param.L_z**2
sigma_n = pybamm.concatenation(
over(ncc, param.n.sigma_cc), param.n.sigma(None, T_n) * eps_s_n**param.n.b_s
)
sigma_p = pybamm.concatenation(
param.p.sigma(None, T_p) * eps_s_p**param.p.b_s, over(pcc, param.p.sigma_cc)
)
model.algebraic[phi_s_n] = scale * (
pybamm.div(-sigma_n * pybamm.grad(phi_s_n))
+ pybamm.concatenation(over(ncc, 0), a_j_n)
)
model.algebraic[phi_s_p] = scale * (
pybamm.div(-sigma_p * pybamm.grad(phi_s_p))
+ pybamm.concatenation(a_j_p, over(pcc, 0))
)
tab_area = layers[pcc][0] * tabs[pcc][1]
tab_flux = param.current_density_with_time * (L_y * L_z / tab_area)
model.boundary_conditions[phi_s_n] = insulated(
tab=True, tab_top=(pybamm.Scalar(0), "Dirichlet")
)
model.boundary_conditions[phi_s_p] = insulated(
tab=True, tab_top=(-tab_flux / param.p.sigma_cc, "Neumann")
)
model.initial_conditions[phi_s_n] = pybamm.Scalar(0)
model.initial_conditions[phi_s_p] = param.ocv_init
kappa = param.kappa_e(c_e, T_e) * tor
model.algebraic[phi_e] = scale * (
pybamm.div(kappa * param.chiRT_over_Fc(c_e, T_e) * pybamm.grad(c_e))
- pybamm.div(kappa * pybamm.grad(phi_e))
- a_j
)
model.rhs[c_e] = (1 / eps) * (
pybamm.div(tor * param.D_e(c_e, T_e) * pybamm.grad(c_e))
+ (1 - param.t_plus(c_e, T_e)) * a_j / param.F
)
model.boundary_conditions[phi_e] = insulated()
model.boundary_conditions[c_e] = insulated()
model.initial_conditions[phi_e] = -param.n.prim.U_init
model.initial_conditions[c_e] = param.c_e_init
# Heat: reactions warm the electrodes, the tabs are held at ambient.
T_init, n_th, s_th, p_th = param.T_init, param.n.therm, param.s.therm, param.p.therm
rho_c_p = (
n_th.rho_c_p_cc,
n_th.rho_c_p,
s_th.rho_c_p,
p_th.rho_c_p,
p_th.rho_c_p_cc,
)
lambda_ = (n_th.lambda_cc, n_th.lambda_, s_th.lambda_, p_th.lambda_, p_th.lambda_cc)
def per_layer(properties):
return pybamm.concatenation(
*(over(d, f(T_init)) for d, f in zip(layers, properties, strict=True))
)
heat = pybamm.concatenation(
over(ncc, 0), heat_n, over(sep, 0), heat_p, over(pcc, 0)
)
model.rhs[T] = (pybamm.div(per_layer(lambda_) * pybamm.grad(T)) + heat) / per_layer(
rho_c_p
)
T_amb = param.T_amb(pybamm.Scalar(0), pybamm.Scalar(0), pybamm.t)
model.boundary_conditions[T] = insulated(tab=True, tab_top=(T_amb, "Dirichlet"))
model.initial_conditions[T] = T_init
# The positive collector is close to equipotential, so any point on it gives the
# cell voltage: read a corner.
voltage = pybamm.boundary_value(phi_s_pcc, "top-right")
model.variables = {
"Time [s]": pybamm.t,
"Current [A]": param.current_with_time,
"Voltage [V]": voltage,
"Maximum temperature [K]": pybamm.Max(T),
"Minimum temperature [K]": pybamm.Min(T),
}
model.events = [
pybamm.Event("Minimum voltage [V]", voltage - param.voltage_low_cut),
pybamm.Event("Maximum voltage [V]", param.voltage_high_cut - voltage),
]
particles = ("negative particle", "positive particle")
sim = pybamm.Simulation(
model,
geometry=geometry,
parameter_values=parameter_values,
submesh_types={
**meshes,
**dict.fromkeys(particles, pybamm.Uniform1DSubMesh),
"current collector": pybamm.SubMesh0D,
},
spatial_methods={
**{domain: pybamm.FiniteVolumeUnstructured() for domain in layers},
**{domain: pybamm.FiniteVolume() for domain in particles},
"current collector": pybamm.ZeroDimensionalSpatialMethod(),
},
var_pts={"r_n": 10, "r_p": 10},
solver=pybamm.IDAKLUSolver(atol=1e-4, rtol=1e-4),
# A hand-written model has no default outputs, so name the ones to keep.
output_variables=[
"Voltage [V]",
"Maximum temperature [K]",
"Minimum temperature [K]",
],
experiment=pybamm.Experiment(
[("Discharge at 1C for 30 minutes", "Rest for 30 minutes")]
),
)
sol = sim.solve(initial_soc=1)
minutes = sol["Time [s]"].entries / 60
fig, (top, bottom) = plt.subplots(2, 1, sharex=True)
top.plot(minutes, sol["Voltage [V]"].entries)
top.set_ylabel("Voltage [V]")
for name in ("Maximum", "Minimum"):
rise = (
sol[f"{name} temperature [K]"].entries
- sol["Minimum temperature [K]"].entries[0]
)
bottom.plot(minutes, rise, label=name)
bottom.set_xlabel("Time [min]")
bottom.set_ylabel("Temperature rise [K]")
bottom.legend()
plt.show()