Tip

An interactive online version of this notebook is available, which can be accessed via

# Using latexify in PyBaMM#

In this notebook we show how to use latexify to print all the model equations.

First we import pybamm

[1]:

%pip install "pybamm[plot,cite]" -q    # install PyBaMM if it is not installed
import pybamm

Note: you may need to restart the kernel to use updated packages.


[2]:

model = pybamm.lithium_ion.SPM()


Now that we have defined a model we can use latexify to get the latex of all the model equations

[3]:

model.latexify()

[3]:

$\displaystyle \large{\underline{\textbf{Single Particle Model Equations}}}\\\\\\ \textbf{Discharge capacity [A.h]}\\\\\frac{d}{d t} Q_{Ah} = \frac{I}{3600}\\\\Q_{Ah} = 0.0\quad \text{at}\; t=0\\\\\\ \textbf{X-averaged negative particle concentration [mol.m-3]}\\\\\frac{\partial}{\partial t} \overline{c}_{\mathrm{s,n}} = \nabla\cdot \left(D_{\mathrm{n}} \left(\nabla \overline{c}_{\mathrm{s,n}}\right)\right)\quad 0 < r < R_{\mathrm{n}}^{\mathrm{typ}}\\\\\overline{c}_{\mathrm{s,n}} = \int c_{\mathrm{n}}^{\mathrm{init}}\, dxn\quad \text{at}\; t=0\\\\\nabla \overline{c}_{\mathrm{s,n}} = 0.0\quad \text{at } r = 0\\\\\nabla \overline{c}_{\mathrm{s,n}} = - \frac{i_{\mathrm{cell}}}{D_{\mathrm{n}}^{surf} F L_{\mathrm{n}} \overline{a}_{\mathrm{n}}}\quad \text{at } r = R_{\mathrm{n}}^{\mathrm{typ}}\\\\\\ \textbf{X-averaged positive particle concentration [mol.m-3]}\\\\\frac{\partial}{\partial t} \overline{c}_{\mathrm{s,p}} = \nabla\cdot \left(D_{\mathrm{p}} \left(\nabla \overline{c}_{\mathrm{s,p}}\right)\right)\quad 0 < r < R_{\mathrm{p}}^{\mathrm{typ}}\\\\\overline{c}_{\mathrm{s,p}} = \int c_{\mathrm{p}}^{\mathrm{init}}\, dxn\quad \text{at}\; t=0\\\\\nabla \overline{c}_{\mathrm{s,p}} = 0.0\quad \text{at } r = 0\\\\\nabla \overline{c}_{\mathrm{s,p}} = - \frac{j_{\mathrm{p}}}{D_{\mathrm{p}}^{surf} F}\quad \text{at } r = R_{\mathrm{p}}^{\mathrm{typ}}\\\\\\ \textbf{Voltage [V]}\\\\V = - U_\mathrm{n}(c^\mathrm{surf}_\mathrm{s,n}, T) + U_\mathrm{p}(c^\mathrm{surf}_\mathrm{s,p}, T) + \frac{2.0 R T_{\mathrm{amb}} \operatorname{asinh}{\left(\frac{0.5 j_{\mathrm{p}}}{j_{\mathrm{p}}^{\mathrm{0}}} \right)}}{F ne_{\mathrm{p}}} - \frac{2.0 R T_{\mathrm{amb}} \operatorname{asinh}{\left(\frac{0.5 i_{\mathrm{cell}}}{L_{\mathrm{n}} \overline{a}_{\mathrm{n}} j_{\mathrm{n}}^{\mathrm{0}}} \right)}}{F ne_{\mathrm{n}}}\\\\\\ \textbf{Parameters and Variables}\\\\I = \text{Current function [A]}\\\\\overline{c}_{\mathrm{s,n}} = \text{X-averaged negative particle concentration [mol.m-3]}\\\\D_{\mathrm{n}} = \text{Negative electrode diffusivity [m2.s-1]}\\\\T_{\mathrm{amb}} = \text{Ambient temperature [K]}\\\\c_{\mathrm{n}}^{\mathrm{max}} = \text{Maximum concentration in negative electrode [mol.m-3]}\\\\\overline{c}_{\mathrm{s,p}} = \text{X-averaged positive particle concentration [mol.m-3]}\\\\D_{\mathrm{p}} = \text{Positive electrode diffusivity [m2.s-1]}\\\\c_{\mathrm{p}}^{\mathrm{max}} = \text{Maximum concentration in positive electrode [mol.m-3]}$

We can also get a list of all the equations instead of a single line joined by newline

[4]:

model.latexify(newline=False)

[4]:

$\displaystyle \left[ \large{\underline{\textbf{Single Particle Model Equations}}}, \ \\ \textbf{Discharge capacity [A.h]}, \ \frac{d}{d t} Q_{Ah} = \frac{I}{3600}, \ Q_{Ah} = 0.0\quad \text{at}\; t=0, \ \\ \textbf{X-averaged negative particle concentration [mol.m-3]}, \ \frac{\partial}{\partial t} \overline{c}_{\mathrm{s,n}} = \nabla\cdot \left(D_{\mathrm{n}} \left(\nabla \overline{c}_{\mathrm{s,n}}\right)\right)\quad 0 < r < R_{\mathrm{n}}^{\mathrm{typ}}, \ \overline{c}_{\mathrm{s,n}} = \int c_{\mathrm{n}}^{\mathrm{init}}\, dxn\quad \text{at}\; t=0, \ \nabla \overline{c}_{\mathrm{s,n}} = 0.0\quad \text{at } r = 0, \ \nabla \overline{c}_{\mathrm{s,n}} = - \frac{i_{\mathrm{cell}}}{D_{\mathrm{n}}^{surf} F L_{\mathrm{n}} \overline{a}_{\mathrm{n}}}\quad \text{at } r = R_{\mathrm{n}}^{\mathrm{typ}}, \ \\ \textbf{X-averaged positive particle concentration [mol.m-3]}, \ \frac{\partial}{\partial t} \overline{c}_{\mathrm{s,p}} = \nabla\cdot \left(D_{\mathrm{p}} \left(\nabla \overline{c}_{\mathrm{s,p}}\right)\right)\quad 0 < r < R_{\mathrm{p}}^{\mathrm{typ}}, \ \overline{c}_{\mathrm{s,p}} = \int c_{\mathrm{p}}^{\mathrm{init}}\, dxn\quad \text{at}\; t=0, \ \nabla \overline{c}_{\mathrm{s,p}} = 0.0\quad \text{at } r = 0, \ \nabla \overline{c}_{\mathrm{s,p}} = - \frac{j_{\mathrm{p}}}{D_{\mathrm{p}}^{surf} F}\quad \text{at } r = R_{\mathrm{p}}^{\mathrm{typ}}, \ \\ \textbf{Voltage [V]}, \ V = - U_\mathrm{n}(c^\mathrm{surf}_\mathrm{s,n}, T) + U_\mathrm{p}(c^\mathrm{surf}_\mathrm{s,p}, T) + \frac{2.0 R T_{\mathrm{amb}} \operatorname{asinh}{\left(\frac{0.5 j_{\mathrm{p}}}{j_{\mathrm{p}}^{\mathrm{0}}} \right)}}{F ne_{\mathrm{p}}} - \frac{2.0 R T_{\mathrm{amb}} \operatorname{asinh}{\left(\frac{0.5 i_{\mathrm{cell}}}{L_{\mathrm{n}} \overline{a}_{\mathrm{n}} j_{\mathrm{n}}^{\mathrm{0}}} \right)}}{F ne_{\mathrm{n}}}, \ \\ \textbf{Parameters and Variables}, \ I = \text{Current function [A]}, \ \overline{c}_{\mathrm{s,n}} = \text{X-averaged negative particle concentration [mol.m-3]}, \ D_{\mathrm{n}} = \text{Negative electrode diffusivity [m2.s-1]}, \ T_{\mathrm{amb}} = \text{Ambient temperature [K]}, \ c_{\mathrm{n}}^{\mathrm{max}} = \text{Maximum concentration in negative electrode [mol.m-3]}, \ \overline{c}_{\mathrm{s,p}} = \text{X-averaged positive particle concentration [mol.m-3]}, \ D_{\mathrm{p}} = \text{Positive electrode diffusivity [m2.s-1]}, \ c_{\mathrm{p}}^{\mathrm{max}} = \text{Maximum concentration in positive electrode [mol.m-3]}\right]$

latexify can also be used to get the equations in a file format like png, jpg, pdf or tex.

[5]:

model.latexify("spm_equations_.png")


will create a png file titled spm_equations.png in the working directory.

[6]:

model_spme = pybamm.lithium_ion.SPMe()


Jupyter notebook sometimes cannot render latex output that is too large. In that case we use newline=False and loop over the lines. When newline=False it returns a list, so it inherits all the properties of a list and you can also do something like this

[7]:

spme_latex = model_spme.latexify(newline=False)
for line in spme_latex:
display(line)

$\displaystyle \large{\underline{\textbf{Single Particle Model with electrolyte Equations}}}$
$\displaystyle \\ \textbf{Discharge capacity [A.h]}$
$\displaystyle \frac{d}{d t} Q_{Ah} = \frac{I}{3600}$
$\displaystyle Q_{Ah} = 0.0\quad \text{at}\; t=0$
$\displaystyle \\ \textbf{X-averaged negative particle concentration [mol.m-3]}$
$\displaystyle \frac{\partial}{\partial t} \overline{c}_{\mathrm{s,n}} = \nabla\cdot \left(D_{\mathrm{n}} \left(\nabla \overline{c}_{\mathrm{s,n}}\right)\right)\quad 0 < r < R_{\mathrm{n}}^{\mathrm{typ}}$
$\displaystyle \overline{c}_{\mathrm{s,n}} = \int c_{\mathrm{n}}^{\mathrm{init}}\, dxn\quad \text{at}\; t=0$
$\displaystyle \nabla \overline{c}_{\mathrm{s,n}} = 0.0\quad \text{at } r = 0$
$\displaystyle \nabla \overline{c}_{\mathrm{s,n}} = - \frac{i_{\mathrm{cell}}}{D_{\mathrm{n}}^{surf} F L_{\mathrm{n}} \overline{a}_{\mathrm{n}}}\quad \text{at } r = R_{\mathrm{n}}^{\mathrm{typ}}$
$\displaystyle \\ \textbf{X-averaged positive particle concentration [mol.m-3]}$
$\displaystyle \frac{\partial}{\partial t} \overline{c}_{\mathrm{s,p}} = \nabla\cdot \left(D_{\mathrm{p}} \left(\nabla \overline{c}_{\mathrm{s,p}}\right)\right)\quad 0 < r < R_{\mathrm{p}}^{\mathrm{typ}}$
$\displaystyle \overline{c}_{\mathrm{s,p}} = \int c_{\mathrm{p}}^{\mathrm{init}}\, dxn\quad \text{at}\; t=0$
$\displaystyle \nabla \overline{c}_{\mathrm{s,p}} = 0.0\quad \text{at } r = 0$
$\displaystyle \nabla \overline{c}_{\mathrm{s,p}} = \frac{i_{\mathrm{cell}}}{D_{\mathrm{p}}^{surf} F L_{\mathrm{p}} \overline{a}_{\mathrm{p}}}\quad \text{at } r = R_{\mathrm{p}}^{\mathrm{typ}}$
$\displaystyle \\ \textbf{Porosity times concentration [mol.m-3](Negative electrode porosity times concentration [mol.m-3], Separator porosity times concentration [mol.m-3], Positive electrode porosity times concentration [mol.m-3])}$
$\displaystyle \frac{\partial}{\partial t} (\epsilon c)_{\mathrm{e}} = - \nabla\cdot \left(- D_{\mathrm{e}} \epsilon^{b_e} \left(\nabla c_{\mathrm{e}}\right)\right) + \frac{- \begin{cases}a_{\mathrm{n}} j_{\mathrm{n}}\\0.0\\a_{\mathrm{p}} j_{\mathrm{p}}\end{cases} t_{\mathrm{+}} + aj}{F}\quad 0 < x < L_x$
$\displaystyle (\epsilon c)_{\mathrm{e}} = \epsilon^{\mathrm{init}} c_{\mathrm{e}}^{\mathrm{init}}\quad \text{at}\; t=0$
$\displaystyle \\ \textbf{Voltage [V]}$
$\displaystyle V = - L_{\mathrm{n}} i_{\mathrm{cell}} \left(- \frac{1.0}{\kappa_{\mathrm{e}} \int \left(\epsilon_{\mathrm{s}}^{\mathrm{init}}\right)^{b_{\mathrm{e,s}}}\, dxn} + \frac{0.333333333333333}{\kappa_{\mathrm{e}} \int \left(\epsilon_{\mathrm{n}}^{\mathrm{init}}\right)^{b_{\mathrm{e,n}}}\, dxn}\right) - U_\mathrm{n}(c^\mathrm{surf}_\mathrm{s,n}, T) + U_\mathrm{p}(c^\mathrm{surf}_\mathrm{s,p}, T) - \int \frac{2.0 R T_{\mathrm{amb}} \operatorname{asinh}{\left(\frac{0.5 i_{\mathrm{cell}}}{L_{\mathrm{n}} \overline{a}_{\mathrm{n}} j_{\mathrm{n}}^{\mathrm{0}}} \right)}}{F ne_{\mathrm{n}}}\, dxn + \int \frac{2.0 R T_{\mathrm{amb}} \operatorname{asinh}{\left(\frac{0.5 j_{\mathrm{p}}}{j_{\mathrm{p}}^{\mathrm{0}}} \right)}}{F ne_{\mathrm{p}}}\, dxn - \int \frac{0.5 i_{\mathrm{cell}} \left(- L_{x}^{2.0} + L_{\mathrm{p}}^{2.0} + x_{p} \left(2.0 L_{x} - x_{p}\right)\right)}{L_{\mathrm{p}} \kappa_{\mathrm{e}} \int \left(\epsilon_{\mathrm{p}}^{\mathrm{init}}\right)^{b_{\mathrm{e,p}}}\, dxn}\, dxn + \int \frac{R T_{\mathrm{amb}} \left(1+\frac{dlnf}{dlnc}\right) \left(2.0 - 2.0 t_{\mathrm{+}}\right) \log{\left(\max\left(1.0 \cdot 10^{-15}, \frac{(\epsilon c)_{\mathrm{e,p}}}{\epsilon_{\mathrm{p}}^{\mathrm{init}} \int c_{\mathrm{e}}\, dxn}\right) \right)}}{F}\, dxn + \int \frac{0.5 i_{\mathrm{cell}} x_{n} \left(- 2.0 L_{\mathrm{n}} + x_{n}\right)}{L_{\mathrm{n}} \sigma_{\mathrm{n}} \int \epsilon_{\mathrm{s,n}}^{b_{\mathrm{s,n}}}\, dxn}\, dxn + \frac{i_{\mathrm{cell}} \left(L_{x} - 0.333333333333333 L_{\mathrm{p}}\right)}{\sigma_{\mathrm{p}} \int \epsilon_{\mathrm{s,p}}^{b_{\mathrm{s,p}}}\, dxn} - \frac{i_{\mathrm{cell}} \left(x_{p} + \frac{0.5 \left(- L_{x} + x_{p}\right)^{2.0}}{L_{\mathrm{p}}}\right)}{\sigma_{\mathrm{p}} \int \epsilon_{\mathrm{s,p}}^{b_{\mathrm{s,p}}}\, dxn} - \frac{i_{\mathrm{cell}} \left(L_{\mathrm{n}} + L_{\mathrm{s}}\right)}{\kappa_{\mathrm{e}} \int \left(\epsilon_{\mathrm{s}}^{\mathrm{init}}\right)^{b_{\mathrm{e,s}}}\, dxn} - \frac{R T_{\mathrm{amb}} \left(1+\frac{dlnf}{dlnc}\right) \left(2.0 - 2.0 t_{\mathrm{+}}\right) \int \log{\left(\max\left(1.0 \cdot 10^{-15}, \frac{(\epsilon c)_{\mathrm{e,n}}}{\epsilon_{\mathrm{n}}^{\mathrm{init}} \int c_{\mathrm{e}}\, dxn}\right) \right)}\, dxn}{F}^{\mathtt{\text{right}}}$
$\displaystyle \\ \textbf{Parameters and Variables}$
$\displaystyle I = \text{Current function [A]}$
$\displaystyle \overline{c}_{\mathrm{s,n}} = \text{X-averaged negative particle concentration [mol.m-3]}$
$\displaystyle D_{\mathrm{n}} = \text{Negative electrode diffusivity [m2.s-1]}$
$\displaystyle T_{\mathrm{amb}} = \text{Ambient temperature [K]}$
$\displaystyle c_{\mathrm{n}}^{\mathrm{max}} = \text{Maximum concentration in negative electrode [mol.m-3]}$
$\displaystyle \overline{c}_{\mathrm{s,p}} = \text{X-averaged positive particle concentration [mol.m-3]}$
$\displaystyle D_{\mathrm{p}} = \text{Positive electrode diffusivity [m2.s-1]}$
$\displaystyle c_{\mathrm{p}}^{\mathrm{max}} = \text{Maximum concentration in positive electrode [mol.m-3]}$
$\displaystyle \epsilon_{\mathrm{p}}^{\mathrm{init}} = \text{Positive electrode porosity}$
$\displaystyle \epsilon_{\mathrm{s}}^{\mathrm{init}} = \text{Separator porosity}$
$\displaystyle \epsilon_{\mathrm{n}}^{\mathrm{init}} = \text{Negative electrode porosity}$
$\displaystyle (\epsilon c)_{\mathrm{e,p}} = \text{Positive electrode porosity times concentration [mol.m-3]}$
$\displaystyle (\epsilon c)_{\mathrm{e,s}} = \text{Separator porosity times concentration [mol.m-3]}$
$\displaystyle (\epsilon c)_{\mathrm{e,n}} = \text{Negative electrode porosity times concentration [mol.m-3]}$
$\displaystyle D_{\mathrm{e}} = \text{Electrolyte diffusivity [m2.s-1]}$
$\displaystyle b_{\mathrm{e,p}} = \text{Positive electrode Bruggeman coefficient (electrolyte)}$
$\displaystyle b_{\mathrm{e,s}} = \text{Separator Bruggeman coefficient (electrolyte)}$
$\displaystyle b_{\mathrm{e,n}} = \text{Negative electrode Bruggeman coefficient (electrolyte)}$
$\displaystyle F = \text{Faraday constant [C.mol-1]}$
$\displaystyle R_{\mathrm{p}} = \text{Positive particle radius [m]}$
$\displaystyle \epsilon_{\mathrm{s,p}} = \text{Positive electrode active material volume fraction}$
$\displaystyle L_{\mathrm{p}} = \text{Positive electrode thickness [m]}$
$\displaystyle L_{z} = \text{Electrode height [m]}$
$\displaystyle L_{y} = \text{Electrode width [m]}$
$\displaystyle n_{electrodes parallel} = \text{Number of electrodes connected in parallel to make a cell}$
$\displaystyle R_{\mathrm{n}} = \text{Negative particle radius [m]}$
$\displaystyle \epsilon_{\mathrm{s,n}} = \text{Negative electrode active material volume fraction}$
$\displaystyle L_{\mathrm{n}} = \text{Negative electrode thickness [m]}$
$\displaystyle t_{\mathrm{+}} = \text{Cation transference number}$
$\displaystyle c_{\mathrm{e}} = \frac{(\epsilon c)_{\mathrm{e}}}{\begin{cases}\epsilon_{\mathrm{n}}^{\mathrm{init}}\\\epsilon_{\mathrm{s}}^{\mathrm{init}}\\\epsilon_{\mathrm{p}}^{\mathrm{init}}\end{cases}}$
$\displaystyle (\epsilon c)_{\mathrm{e}} = \begin{cases}(\epsilon c)_{\mathrm{e,n}}\\(\epsilon c)_{\mathrm{e,s}}\\(\epsilon c)_{\mathrm{e,p}}\end{cases}$
$\displaystyle \epsilon^{b_e} = \begin{cases}\left(\epsilon_{\mathrm{n}}^{\mathrm{init}}\right)^{b_{\mathrm{e,n}}}\\\left(\epsilon_{\mathrm{s}}^{\mathrm{init}}\right)^{b_{\mathrm{e,s}}}\\\left(\epsilon_{\mathrm{p}}^{\mathrm{init}}\right)^{b_{\mathrm{e,p}}}\end{cases}$
$\displaystyle j_{\mathrm{p}} = - \frac{i_{\mathrm{cell}}}{L_{\mathrm{p}} \overline{a}_{\mathrm{p}}}$
$\displaystyle \overline{a}_{\mathrm{p}} = \int a_{\mathrm{p}}\, dxn$
$\displaystyle a_{\mathrm{p}} = \frac{3.0 \epsilon_{\mathrm{s,p}}}{R_{\mathrm{p}}}$
$\displaystyle i_{\mathrm{cell}} = \frac{I}{A_{\mathrm{cc}} n_{electrodes parallel}}$
$\displaystyle A_{\mathrm{cc}} = L_{y} L_{z}$
$\displaystyle j_{\mathrm{n}} = \frac{i_{\mathrm{cell}}}{L_{\mathrm{n}} \overline{a}_{\mathrm{n}}}$
$\displaystyle \overline{a}_{\mathrm{n}} = \int a_{\mathrm{n}}\, dxn$
$\displaystyle a_{\mathrm{n}} = \frac{3.0 \epsilon_{\mathrm{s,n}}}{R_{\mathrm{n}}}$
$\displaystyle aj = \begin{cases}a_{\mathrm{n}} j_{\mathrm{n}}\\0.0\\a_{\mathrm{p}} j_{\mathrm{p}}\end{cases}$

## References#

The relevant papers for this notebook are:

[8]:

pybamm.print_citations()

[1] Charles R. Harris, K. Jarrod Millman, Stéfan J. van der Walt, Ralf Gommers, Pauli Virtanen, David Cournapeau, Eric Wieser, Julian Taylor, Sebastian Berg, Nathaniel J. Smith, and others. Array programming with NumPy. Nature, 585(7825):357–362, 2020. doi:10.1038/s41586-020-2649-2.
[2] Scott G. Marquis, Valentin Sulzer, Robert Timms, Colin P. Please, and S. Jon Chapman. An asymptotic derivation of a single particle model with electrolyte. Journal of The Electrochemical Society, 166(15):A3693–A3706, 2019. doi:10.1149/2.0341915jes.
[3] Valentin Sulzer, Scott G. Marquis, Robert Timms, Martin Robinson, and S. Jon Chapman. Python Battery Mathematical Modelling (PyBaMM). Journal of Open Research Software, 9(1):14, 2021. doi:10.5334/jors.309.