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 5 - Run experiments#
In Tutorial 4 we saw how to change the parameters, including the applied current. However, in some cases we might want to prescribe a given voltage, a given power or switch between different conditions to simulate experimental setups. We can use the Experiment class for these simulations.
[1]:
%pip install "pybamm[plot,cite]" -q # install PyBaMM if it is not installed
import pybamm
import numpy as np
Note: you may need to restart the kernel to use updated packages.
String-based instructions#
We start defining an experiment, which consists on a set of instructions on how to cycle the battery. These instructions can be defined in two different ways, but the simplest is to use strings. The instructions can be of the form "(Dis)charge at x A/C/W"
, "Rest"
, or "Hold at x V"
. The instructions can also include how long each step should run for. The duration is introduced by the word "for"
followed by the duration, e.g. "for 10 seconds"
, "for 3 minutes"
or
"for 1 hour"
. Terminating conditions can also be specified. In this case, the step will stop when that condition is met. These conditions should be a circuit state and are introduced by the word "until"
, e.g. "until 1 A"
, "until C/50"
or "until 3 V"
. Duration and termination conditions can be combined using the word "or"
and the step will either finish when the condition is met or after the specified duration (whichever happens first).
Some examples of experiment instructions are:
"Discharge at 1C for 0.5 hours",
"Discharge at C/20 for 0.5 hours",
"Charge at 0.5 C for 45 minutes",
"Discharge at 1 A for 90 seconds",
"Charge at 200mA for 45 minutes",
"Discharge at 1 W for 0.5 hours",
"Charge at 200 mW for 45 minutes",
"Rest for 10 minutes",
"Hold at 1 V for 20 seconds",
"Charge at 1 C until 4.1V",
"Hold at 4.1 V until 50 mA",
"Hold at 3V until C/50",
These steps can be concatenated in a list, so they are executed sequentially. To create an experiment, the list can then be passed when creating an Experiment
object:
[2]:
experiment = pybamm.Experiment(
[
"Discharge at C/10 for 10 hours or until 3.3 V",
"Rest for 1 hour",
"Charge at 1 A until 4.1 V",
"Hold at 4.1 V until 50 mA",
"Rest for 1 hour",
]
)
In order to reproduce real cycling conditions, often the experiments will be composed of several “cycles”, where a cycle is a user-defined collection of steps. In PyBaMM, we can define a cycle as a tuple of steps, which means that we can process the solution in terms of cycles. For more information on this functionality, please see the long experiments notebook. We can also leverage the list addition and multiplication operators to combine and repeat cycles. For example, if we want a three cycles of constant current C/10 discharge, a one hour rest, a constant current (1 A) constant voltage (4.1 V) and another one hour rest, followed by a cycle of 1C discharge we can write:
[3]:
experiment = pybamm.Experiment(
[
(
"Discharge at C/10 for 10 hours or until 3.3 V",
"Rest for 1 hour",
"Charge at 1 A until 4.1 V",
"Hold at 4.1 V until 50 mA",
"Rest for 1 hour",
)
]
* 3
+ [
"Discharge at 1C until 3.3 V",
]
)
Note that if a cycle is made of one step only (like the 1C discharge) we do not need to define it as a tuple. One key difference between cycles and steps, is that PyBaMM allows for steps to be skipped (e.g. if you try to charge an a fully charged battery) but not cycles.
Then we can choose our model and create our simulation, passing our experiment using a keyword argument
[4]:
model = pybamm.lithium_ion.DFN()
sim = pybamm.Simulation(model, experiment=experiment)
We then solve and plot the solution
[5]:
sim.solve()
sim.plot()
At t = 339.952 and h = 1.4337e-18, the corrector convergence failed repeatedly or with |h| = hmin.
At t = 522.687 and h = 4.04917e-14, the corrector convergence failed repeatedly or with |h| = hmin.
[5]:
<pybamm.plotting.quick_plot.QuickPlot at 0x16520e690>
The solution
variable in the simulation
object has a cycles
variable that allows to access the solution for a specific cycle. That solution can be processed and plotted as usual. For example, if we want to plot the first cycle only we can do
[6]:
sim.solution.cycles[0].plot()
[6]:
<pybamm.plotting.quick_plot.QuickPlot at 0x1696cf290>
Note that because sol.cycles
is a list, the indexing starts at 0.
As we will see in the next section, we can pass additional arguments such as a period, temperature, or tags when defining a step. The method pybamm.step.string
can be used to add these additional conditions to a string-defined step:
[7]:
pybamm.step.string(
"Discharge at 1C for 1 hour", period="1 minute", temperature="25oC", tags=["tag1"]
)
[7]:
Step(1.0, duration=1 hour, period=1 minute, temperature=25oC, tags=['tag1'], description=Discharge at 1C for 1 hour, direction=Discharge)
Direct instructions#
Experiments can also be specified programmatically without having to use string formatting. For example,
[8]:
pybamm.step.current(1, duration="1 hour", termination="2.5 V")
[8]:
Step(1, duration=1 hour, termination=2.5 V, direction=Discharge)
is equivalent to
[9]:
pybamm.step.string("Discharge at 1A for 1 hour or until 2.5V")
[9]:
Step(1.0, duration=1 hour, termination=2.5V, description=Discharge at 1A for 1 hour or until 2.5V, direction=Discharge)
The available methods are current
, c_rate
, voltage
, power
, and resistance
. These methods also take optional keyword arguments, such as the period, temperature, tags or starting times (a complete list can be found in the documentation).
These methods can also be used for drive cycles. In this case, the value
argument should be a 2-column array, where the first column is time in seconds (should start at zero) and the second column the values (i.e. current, voltage, power…). Here is an example for a synthetically defined drive cycle:
[10]:
t = np.linspace(0, 1, 60)
sin_t = 0.5 * np.sin(2 * np.pi * t)
drive_cycle_power = np.column_stack([t, sin_t])
experiment = pybamm.Experiment([pybamm.step.power(drive_cycle_power)])
sim = pybamm.Simulation(model, experiment=experiment)
sim.solve()
sim.plot()
2024-07-10 14:41:02.625 - [WARNING] callbacks.on_experiment_infeasible_time(240):
Experiment is infeasible: default duration (1.0 seconds) was reached during 'Step([[ 0.00000000e+00 0.00000000e+00]
[ 1.69491525e-02 5.31467428e-02]
[ 3.38983051e-02 1.05691312e-01]
[ 5.08474576e-02 1.57038356e-01]
[ 6.77966102e-02 2.06606093e-01]
[ 8.47457627e-02 2.53832900e-01]
[ 1.01694915e-01 2.98183679e-01]
[ 1.18644068e-01 3.39155918e-01]
[ 1.35593220e-01 3.76285385e-01]
[ 1.52542373e-01 4.09151388e-01]
[ 1.69491525e-01 4.37381542e-01]
[ 1.86440678e-01 4.60655989e-01]
[ 2.03389831e-01 4.78711019e-01]
[ 2.20338983e-01 4.91342062e-01]
[ 2.37288136e-01 4.98406004e-01]
[ 2.54237288e-01 4.99822806e-01]
[ 2.71186441e-01 4.95576416e-01]
[ 2.88135593e-01 4.85714947e-01]
[ 3.05084746e-01 4.70350133e-01]
[ 3.22033898e-01 4.49656065e-01]
[ 3.38983051e-01 4.23867214e-01]
[ 3.55932203e-01 3.93275778e-01]
[ 3.72881356e-01 3.58228370e-01]
[ 3.89830508e-01 3.19122092e-01]
[ 4.06779661e-01 2.76400033e-01]
[ 4.23728814e-01 2.30546251e-01]
[ 4.40677966e-01 1.82080288e-01]
[ 4.57627119e-01 1.31551282e-01]
[ 4.74576271e-01 7.95317480e-02]
[ 4.91525424e-01 2.66110874e-02]
[ 5.08474576e-01 -2.66110874e-02]
[ 5.25423729e-01 -7.95317480e-02]
[ 5.42372881e-01 -1.31551282e-01]
[ 5.59322034e-01 -1.82080288e-01]
[ 5.76271186e-01 -2.30546251e-01]
[ 5.93220339e-01 -2.76400033e-01]
[ 6.10169492e-01 -3.19122092e-01]
[ 6.27118644e-01 -3.58228370e-01]
[ 6.44067797e-01 -3.93275778e-01]
[ 6.61016949e-01 -4.23867214e-01]
[ 6.77966102e-01 -4.49656065e-01]
[ 6.94915254e-01 -4.70350133e-01]
[ 7.11864407e-01 -4.85714947e-01]
[ 7.28813559e-01 -4.95576416e-01]
[ 7.45762712e-01 -4.99822806e-01]
[ 7.62711864e-01 -4.98406004e-01]
[ 7.79661017e-01 -4.91342062e-01]
[ 7.96610169e-01 -4.78711019e-01]
[ 8.13559322e-01 -4.60655989e-01]
[ 8.30508475e-01 -4.37381542e-01]
[ 8.47457627e-01 -4.09151388e-01]
[ 8.64406780e-01 -3.76285385e-01]
[ 8.81355932e-01 -3.39155918e-01]
[ 8.98305085e-01 -2.98183679e-01]
[ 9.15254237e-01 -2.53832900e-01]
[ 9.32203390e-01 -2.06606093e-01]
[ 9.49152542e-01 -1.57038356e-01]
[ 9.66101695e-01 -1.05691312e-01]
[ 9.83050847e-01 -5.31467428e-02]
[ 1.00000000e+00 -1.22464680e-16]], duration=1.0, period=0.016949152542372836, direction=Rest)'. The returned solution only contains up to step 1 of cycle 1. Please specify a duration in the step instructions.
[10]:
<pybamm.plotting.quick_plot.QuickPlot at 0x16a1d8d90>
For a drive cycle, the duration is until the final time provided and the period is the smallest time step. For best results, we recommend using a constant time step size.
In this notebook we have seen how to use the Experiment class to run simulations of more complex operating conditions. In Tutorial 6 we will see how to manage the outputs of the simulation.
References#
The relevant papers for this notebook are:
[11]:
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] Von DAG Bruggeman. Berechnung verschiedener physikalischer konstanten von heterogenen substanzen. i. dielektrizitätskonstanten und leitfähigkeiten der mischkörper aus isotropen substanzen. Annalen der physik, 416(7):636–664, 1935.
[3] 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.
[4] 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.
[5] 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.
[6] Peyman Mohtat, Suhak Lee, Jason B Siegel, and Anna G Stefanopoulou. Towards better estimability of electrode-specific state of health: decoding the cell expansion. Journal of Power Sources, 427:101–111, 2019.
[7] 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.
[8] Pauli Virtanen, Ralf Gommers, Travis E. Oliphant, Matt Haberland, Tyler Reddy, David Cournapeau, Evgeni Burovski, Pearu Peterson, Warren Weckesser, Jonathan Bright, and others. SciPy 1.0: fundamental algorithms for scientific computing in Python. Nature Methods, 17(3):261–272, 2020. doi:10.1038/s41592-019-0686-2.
[9] Andrew Weng, Jason B Siegel, and Anna Stefanopoulou. Differential voltage analysis for battery manufacturing process control. arXiv preprint arXiv:2303.07088, 2023.