Tip

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

Alternatively, you may download this notebook and run it offline.

# Tutorial 9 - Changing the mesh#

In Tutorial 8 we saw how to change the solver options. In this tutorial we will change the mesh used in the simulation, and show how to investigate the influence of the mesh on the solution.

All models in PyBaMM have a default number of mesh points used in a simulation. However, depending on aspects like the operating conditions or the parameters, you may find you need to increase the number points in the mesh to obtain an accurate solution. On the other hand, you may find that you are able to decrease the number of mesh points and still obtain a solution with an acceptable degree of accuracy but with a lower computational time.

It is always good practice to conduct a mesh refinement study, where you simulate the same problem with a finer mesh and compare the results. Here will show how to do this graphically, but in practice you may wish to do a more detailed calculation of the relative error.

```
[1]:
```

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

```
[notice] A new release of pip is available: 23.3.1 -> 24.0
[notice] To update, run: pip install --upgrade pip
Note: you may need to restart the kernel to use updated packages.
```

```
An NVIDIA GPU may be present on this machine, but a CUDA-enabled jaxlib is not installed. Falling back to cpu.
```

## Changing the number of points in the mesh#

First we load a model, in this case the SPMe

```
[2]:
```

```
model = pybamm.lithium_ion.SPMe()
```

We can then look at the number of points that the models uses by default, which are stored as a dictionary whose keys are the variables for each domain:

```
[3]:
```

```
model.default_var_pts
```

```
[3]:
```

```
{'x_n': 20,
'x_s': 20,
'x_p': 20,
'r_n': 20,
'r_p': 20,
'r_n_prim': 20,
'r_p_prim': 20,
'r_n_sec': 20,
'r_p_sec': 20,
'y': 10,
'z': 10,
'R_n': 30,
'R_p': 30}
```

Note how the number of points is a dictionary where the key is the name of the spatial variable, and the value the number of points in the discretisation of that variable. To run a simulation with a different number of points we can define our own dictionary

```
[4]:
```

```
# create our dictionary
var_pts = {
"x_n": 10, # negative electrode
"x_s": 10, # separator
"x_p": 10, # positive electrode
"r_n": 10, # negative particle
"r_p": 10, # positive particle
}
```

and pass it as a keyword argument when creating a simulation

```
[5]:
```

```
sim = pybamm.Simulation(model, var_pts=var_pts)
```

We can then solve and plot the simulation as usual:

```
[6]:
```

```
sim.solve([0, 3600])
sim.plot()
```

```
[6]:
```

```
<pybamm.plotting.quick_plot.QuickPlot at 0x7efd0c200410>
```

## Conducting a mesh refinement study#

In order to investigate the influence of the mesh on the solution we must solve the model multiple times, increasing the mesh resolution as we go. We first create a list of the number of points per domain we would like to use

```
[7]:
```

```
npts = [4, 8, 16, 32, 64]
```

and now we can loop over the list, creating and solving simulations as we go. The solutions are stored in the list `solutions`

, similar to what we did in Tutorial 2 for the various models.

```
[8]:
```

```
# choose model and parameters
model = pybamm.lithium_ion.DFN()
parameter_values = pybamm.ParameterValues("Ecker2015")
# choose solver
solver = pybamm.CasadiSolver(mode="fast")
# loop over number of mesh points
solutions = []
for N in npts:
var_pts = {
"x_n": N, # negative electrode
"x_s": N, # separator
"x_p": N, # positive electrode
"r_n": N, # negative particle
"r_p": N, # positive particle
}
sim = pybamm.Simulation(
model, solver=solver, parameter_values=parameter_values, var_pts=var_pts
)
sim.solve([0, 3600])
solutions.append(sim.solution)
```

We can now pass our list of solutions to the dynamic plot method, allowing use to see the influence of the mesh on the computed voltage. We pass our list of points using the `labels`

keyword so that the plots are labeled with the number of points used in the simulation.

```
[9]:
```

```
pybamm.dynamic_plot(solutions, ["Voltage [V]"], labels=npts)
```

```
[9]:
```

```
<pybamm.plotting.quick_plot.QuickPlot at 0x7efca8f58150>
```

This notebook concludes the “Getting Started” series, that demonstrated all the main features of PyBaMM. You may now want to explore more advanced features, so take a look at all the examples available in our website.

## References#

The relevant papers for this notebook are:

```
[10]:
```

```
pybamm.print_citations()
```

```
[1] Joel A. E. Andersson, Joris Gillis, Greg Horn, James B. Rawlings, and Moritz Diehl. CasADi – A software framework for nonlinear optimization and optimal control. Mathematical Programming Computation, 11(1):1–36, 2019. doi:10.1007/s12532-018-0139-4.
[2] Marc Doyle, Thomas F. Fuller, and John Newman. Modeling of galvanostatic charge and discharge of the lithium/polymer/insertion cell. Journal of the Electrochemical society, 140(6):1526–1533, 1993. doi:10.1149/1.2221597.
[3] Madeleine Ecker, Stefan Käbitz, Izaro Laresgoiti, and Dirk Uwe Sauer. Parameterization of a Physico-Chemical Model of a Lithium-Ion Battery: II. Model Validation. Journal of The Electrochemical Society, 162(9):A1849–A1857, 2015. doi:10.1149/2.0541509jes.
[4] Madeleine Ecker, Thi Kim Dung Tran, Philipp Dechent, Stefan Käbitz, Alexander Warnecke, and Dirk Uwe Sauer. Parameterization of a Physico-Chemical Model of a Lithium-Ion Battery: I. Determination of Parameters. Journal of the Electrochemical Society, 162(9):A1836–A1848, 2015. doi:10.1149/2.0551509jes.
[5] Alastair Hales, Laura Bravo Diaz, Mohamed Waseem Marzook, Yan Zhao, Yatish Patel, and Gregory Offer. The cell cooling coefficient: a standard to define heat rejection from lithium-ion batteries. Journal of The Electrochemical Society, 166(12):A2383, 2019.
[6] 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.
[7] 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.
[8] Giles Richardson, Ivan Korotkin, Rahifa Ranom, Michael Castle, and Jamie M. Foster. Generalised single particle models for high-rate operation of graded lithium-ion electrodes: systematic derivation and validation. Electrochimica Acta, 339:135862, 2020. doi:10.1016/j.electacta.2020.135862.
[9] 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.
[10] Yan Zhao, Yatish Patel, Teng Zhang, and Gregory J Offer. Modeling the effects of thermal gradients induced by tab and surface cooling on lithium ion cell performance. Journal of The Electrochemical Society, 165(13):A3169, 2018.
```