Tip

An interactive online version of this notebook is available, which can be accessed via Open this notebook in Google Colab


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

Attention

You are viewing this notebook on the latest version of the documentation, where these notebooks may not be compatible with the stable release of PyBaMM since they can contain features that are not yet released. We recommend viewing these notebooks from the stable version of the documentation. To install the latest version of PyBaMM that is compatible with the latest notebooks, build PyBaMM from source.

Initializing a model#

Example showing how to initialize a model with another model

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

import pybamm
import pandas as pd
import os

os.chdir(pybamm.__path__[0] + "/..")
WARNING: You are using pip version 21.0.1; however, version 21.1 is available.
You should consider upgrading via the '/Users/vsulzer/Documents/Energy_storage/PyBaMM/.tox/dev/bin/python -m pip install --upgrade pip' command.
Note: you may need to restart the kernel to use updated packages.

Solve a model with a drive cycle#

Load model

[2]:
model = pybamm.lithium_ion.DFN()

Set up drive cycle

[3]:
# import drive cycle from file
drive_cycle = pd.read_csv(
    "pybamm/input/drive_cycles/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 using the CasadiSolver in “fast” mode, remembering to pass in the updated parameters

[4]:
sim_US06_1 = pybamm.Simulation(
    model, parameter_values=param, solver=pybamm.CasadiSolver(mode="fast")
)
sol_US06_1 = sim_US06_1.solve()

Update initial conditions based on a solution and solve again#

Now pre-charge with CCCV, update the initial conditions, and solve again with the US06 drive cycle

[5]:
experiment = pybamm.Experiment(
    ["Charge at 1 A until 4.1 V", "Hold at 4.1 V until 50 mA"]
)
sim_cccv = pybamm.Simulation(model, experiment=experiment)
sol_cccv = sim_cccv.solve()

# 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, solver=pybamm.CasadiSolver(mode="fast")
)
sol_US06_2 = sim_US06_2.solve()

Plot both solutions, we can clearly see the difference now that initial conditions have been updated

[6]:
pybamm.dynamic_plot(
    [sol_US06_1, sol_US06_2], labels=["Default initial conditions", "Fully charged"]
)
[6]:
<pybamm.plotting.quick_plot.QuickPlot at 0x1447078e0>

Initialize using a different model#

We can also initialize the model using the solution of a different model

[7]:
spm = pybamm.lithium_ion.SPM()
sim_spm_cccv = pybamm.Simulation(spm, experiment=experiment)
sol_spm_cccv = sim_spm_cccv.solve()

# 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, solver=pybamm.CasadiSolver(mode="fast")
)
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

[8]:
pybamm.dynamic_plot(
    [sol_US06_1, sol_US06_2, sol_US06_3],
    labels=[
        "Default initial conditions",
        "Fully charged (from DFN)",
        "Fully charged (from SPM)",
    ],
)
[8]:
<pybamm.plotting.quick_plot.QuickPlot at 0x10437c370>

References#

The relevant papers for this notebook are:

[9]:
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] 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.
[4] 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.
[5] Valentin Sulzer, Scott G. Marquis, Robert Timms, Martin Robinson, and S. Jon Chapman. Python Battery Mathematical Modelling (PyBaMM). ECSarXiv. February, 2020. doi:10.1149/osf.io/67ckj.

[ ]: