Tip

An interactive online version of this notebook is available, which can be accessed via # 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 things like the operating conditions you are simulating or the parameters you are using, 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 in a shorter amount of 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.

:

%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.


## Changing the number of points in the mesh#

:

model = pybamm.lithium_ion.SPMe()


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

:

model.default_var_pts

:

{'x_n': 20,
'x_s': 20,
'x_p': 20,
'r_n': 20,
'r_p': 20,
'y': 10,
'z': 10,
'R_n': 30,
'R_p': 30}


To run a simulation with a different number of points we can define our own dictionary

:

# 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
}


We then create and solve a simulation, passing the dictionary of points as a keyword argument

:

sim = pybamm.Simulation(model, var_pts=var_pts)
sim.solve([0, 3600])

:

<pybamm.solvers.solution.Solution at 0x7fc810855a60>


and plot the solution in the usual way

:

sim.plot()

:

<pybamm.plotting.quick_plot.QuickPlot at 0x7fc801eb4850>


## 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

:

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

:

# choose model and parameters
model = pybamm.lithium_ion.DFN()
parameter_values = pybamm.ParameterValues("Ecker2015")

# choose solver

# 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

:

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

:

<pybamm.plotting.quick_plot.QuickPlot at 0x7fc802147eb0>


In the next tutorial we show how to create a basic model from scratch in PyBaMM.