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.
Initializing a model#
Example showing how to initialize a model with another model
[19]:
%pip install "pybamm[plot,cite]" -q # install PyBaMM if it is not installed
import os
import pandas as pd
import pybamm
os.chdir(pybamm.__path__[0] + "/..")
[notice] A new release of pip is available: 25.0.1 -> 25.1.1
[notice] To update, run: pip install --upgrade pip
Note: you may need to restart the kernel to use updated packages.
Solve a model with a drive cycle#
Load model
[20]:
model = pybamm.lithium_ion.DFN()
Set up drive cycle
[21]:
# import drive cycle from file
data_loader = pybamm.DataLoader()
drive_cycle = pd.read_csv(
f"{data_loader.get_data('US06.csv')}", comment="#", header=None
).to_numpy()
# create interpolant
param = model.default_parameter_values
current_interpolant = pybamm.Interpolant(drive_cycle[:, 0], drive_cycle[:, 1], pybamm.t)
# set drive cycle
param["Current function [A]"] = current_interpolant
Create and run simulation with the updated parameters
[22]:
sim_US06_1 = pybamm.Simulation(model, parameter_values=param)
sol_US06_1 = sim_US06_1.solve()
Update initial conditions based on a solution and solve again#
Starting with an initial SOC of 0%, pre-charge with CCCV, update the initial conditions, and solve again with the US06 drive cycle
[23]:
experiment = pybamm.Experiment(
["Charge at 1 A until 3.9 V", "Hold at 3.9 V until 50 mA"],
)
sim_cccv = pybamm.Simulation(model, experiment=experiment)
sol_cccv = sim_cccv.solve(initial_soc=0)
# MODEL RE-INITIALIZATION: #############################################################
# Now initialize the model with the solution of the charge, and then discharge with
# the US06 drive cycle
# We could also do this inplace by setting inplace to True, which modifies the original
# model in place
new_model = model.set_initial_conditions_from(sol_cccv, inplace=False)
########################################################################################
sim_US06_2 = pybamm.Simulation(new_model, parameter_values=param)
sol_US06_2 = sim_US06_2.solve()
Plot both solutions, we can clearly see the difference now that initial conditions have been updated
[24]:
pybamm.dynamic_plot(
[sol_US06_1, sol_US06_2], labels=["Default initial conditions", "Fully charged"]
)
[24]:
<pybamm.plotting.quick_plot.QuickPlot at 0x16fa6b4d0>
Initialize using a different model#
We can also initialize the model using the solution of a different model
[25]:
spm = pybamm.lithium_ion.SPM()
sim_spm_cccv = pybamm.Simulation(spm, experiment=experiment)
sol_spm_cccv = sim_spm_cccv.solve(initial_soc=0)
# MODEL RE-INITIALIZATION: #############################################################
# Now initialize the model with the solution of the charge, and then discharge with
# the US06 drive cycle
# We could also do this inplace by setting inplace to True, which modifies the original
# model in place
new_dfn = model.set_initial_conditions_from(sol_spm_cccv, inplace=False)
########################################################################################
sim_US06_3 = pybamm.Simulation(new_dfn, parameter_values=param)
sol_US06_3 = sim_US06_3.solve()
Now the model initialized by the DFN and the model initialized by the SPM give the same solution
[26]:
pybamm.dynamic_plot(
[sol_US06_1, sol_US06_2, sol_US06_3],
labels=[
"Default initial conditions",
"Fully charged (from DFN)",
"Fully charged (from SPM)",
],
)
[26]:
<pybamm.plotting.quick_plot.QuickPlot at 0x3777e3ad0>
References#
The relevant papers for this notebook are:
[27]:
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] Alan C. Hindmarsh. The PVODE and IDA algorithms. Technical Report, Lawrence Livermore National Lab., CA (US), 2000. doi:10.2172/802599.
[6] Alan C. Hindmarsh, Peter N. Brown, Keith E. Grant, Steven L. Lee, Radu Serban, Dan E. Shumaker, and Carol S. Woodward. SUNDIALS: Suite of nonlinear and differential/algebraic equation solvers. ACM Transactions on Mathematical Software (TOMS), 31(3):363–396, 2005. doi:10.1145/1089014.1089020.
[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] 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.
[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] 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.
[11] Andrew Weng, Jason B Siegel, and Anna Stefanopoulou. Differential voltage analysis for battery manufacturing process control. arXiv preprint arXiv:2303.07088, 2023.
[ ]: