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.

Tutorial 6 - Managing simulation outputs#

In the previous tutorials we have interacted with the outputs of the simulation via the default dynamic plot. However, usually we need to access the output data to manipulate it or transfer to another software which is the topic of this notebook.

We start by building and solving our model as shown in previous notebooks:

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

model = pybamm.lithium_ion.SPMe()
sim = pybamm.Simulation(model)
sim.solve([0, 3600])
Note: you may need to restart the kernel to use updated packages.
[1]:
<pybamm.solvers.solution.Solution at 0x7f996c4a7e90>

Accessing solution variables#

We can now access the solved variables directly to visualise or create our own plots. We first extract the solution object:

[2]:
solution = sim.solution

and now we can create a post-processed variable (for a list of all the available variables see Tutorial 3)

[3]:
t = solution["Time [s]"]
V = solution["Voltage [V]"]

One option is to visualise the data set returned by the solver directly

[4]:
V.entries
[4]:
array([3.77047806, 3.75305182, 3.74567027, 3.74038822, 3.73581196,
       3.73153391, 3.72742393, 3.72343929, 3.71956623, 3.71580184,
       3.71214621, 3.7086004 , 3.70516561, 3.70184253, 3.69863121,
       3.69553118, 3.69254137, 3.68966018, 3.68688562, 3.68421526,
       3.68164637, 3.67917591, 3.6768006 , 3.67451688, 3.67232094,
       3.67020869, 3.66817572, 3.66621717, 3.66432762, 3.6625009 ,
       3.66072974, 3.65900536, 3.65731692, 3.65565066, 3.65398895,
       3.65230898, 3.65058135, 3.6487688 , 3.64682546, 3.64469798,
       3.64232968, 3.63966973, 3.63668796, 3.63339303, 3.62984711,
       3.62616692, 3.6225045 , 3.61901241, 3.61580868, 3.6129572 ,
       3.61046847, 3.60831405, 3.60644483, 3.60480596, 3.60334607,
       3.60202167, 3.60079822, 3.5996495 , 3.59855637, 3.59750531,
       3.59648723, 3.59549638, 3.59452954, 3.59358541, 3.59266405,
       3.59176646, 3.59089417, 3.59004885, 3.58923192, 3.58844407,
       3.58768477, 3.58695179, 3.58624057, 3.58554372, 3.58485045,
       3.58414611, 3.58341187, 3.58262441, 3.58175587, 3.58077378,
       3.57964098, 3.57831538, 3.5767492 , 3.57488745, 3.57266504,
       3.5700019 , 3.56679523, 3.56290766, 3.5581495 , 3.55225276,
       3.54483361, 3.53533853, 3.52296795, 3.50656968, 3.48449277,
       3.45439366, 3.41299182, 3.35578871, 3.27680072, 3.16842636])

which correspond to the data at the times

[5]:
t.entries
[5]:
array([   0.        ,   36.36363636,   72.72727273,  109.09090909,
        145.45454545,  181.81818182,  218.18181818,  254.54545455,
        290.90909091,  327.27272727,  363.63636364,  400.        ,
        436.36363636,  472.72727273,  509.09090909,  545.45454545,
        581.81818182,  618.18181818,  654.54545455,  690.90909091,
        727.27272727,  763.63636364,  800.        ,  836.36363636,
        872.72727273,  909.09090909,  945.45454545,  981.81818182,
       1018.18181818, 1054.54545455, 1090.90909091, 1127.27272727,
       1163.63636364, 1200.        , 1236.36363636, 1272.72727273,
       1309.09090909, 1345.45454545, 1381.81818182, 1418.18181818,
       1454.54545455, 1490.90909091, 1527.27272727, 1563.63636364,
       1600.        , 1636.36363636, 1672.72727273, 1709.09090909,
       1745.45454545, 1781.81818182, 1818.18181818, 1854.54545455,
       1890.90909091, 1927.27272727, 1963.63636364, 2000.        ,
       2036.36363636, 2072.72727273, 2109.09090909, 2145.45454545,
       2181.81818182, 2218.18181818, 2254.54545455, 2290.90909091,
       2327.27272727, 2363.63636364, 2400.        , 2436.36363636,
       2472.72727273, 2509.09090909, 2545.45454545, 2581.81818182,
       2618.18181818, 2654.54545455, 2690.90909091, 2727.27272727,
       2763.63636364, 2800.        , 2836.36363636, 2872.72727273,
       2909.09090909, 2945.45454545, 2981.81818182, 3018.18181818,
       3054.54545455, 3090.90909091, 3127.27272727, 3163.63636364,
       3200.        , 3236.36363636, 3272.72727273, 3309.09090909,
       3345.45454545, 3381.81818182, 3418.18181818, 3454.54545455,
       3490.90909091, 3527.27272727, 3563.63636364, 3600.        ])

In addition, post-processed variables can be called at any time (by interpolation)

[6]:
V([200, 400, 780, 1236])  # times in seconds
[6]:
array([3.72947892, 3.7086004 , 3.67810702, 3.65400557])

Saving the simulation and output data#

In some cases simulations might take a long time to run so it is advisable to save in your computer so it can be analysed later without re-running the simulation. You can save the whole simulation doing:

[7]:
sim.save("SPMe.pkl")

If you now check the root directory of your notebooks you will notice that a new file called "SPMe.pkl" has appeared. We can load the stored simulation doing

[8]:
sim2 = pybamm.load("SPMe.pkl")

which allows the same manipulation as the original simulation would allow

[9]:
sim2.plot()

Alternatively, we can just save the solution of the simulation in a similar way

[10]:
sol = sim.solution
sol.save("SPMe_sol.pkl")

and load it in a similar way too

[11]:
sol2 = pybamm.load("SPMe_sol.pkl")
pybamm.dynamic_plot(sol2)
[11]:
<pybamm.plotting.quick_plot.QuickPlot at 0x7f995f4bd390>

Another option is to just save the data for some variables

[12]:
sol.save_data("sol_data.pkl", ["Current [A]", "Voltage [V]"])

or save in csv or mat format

[13]:
sol.save_data("sol_data.csv", ["Current [A]", "Voltage [V]"], to_format="csv")
# matlab needs names without spaces
sol.save_data(
    "sol_data.mat",
    ["Current [A]", "Voltage [V]"],
    to_format="matlab",
    short_names={"Current [A]": "I", "Voltage [V]": "V"},
)

In this notebook we have shown how to extract and store the outputs of PyBaMM’s simulations. Next, in Tutorial 7 we will show how to change the model options.

Before finishing we will remove the data files we saved so that we leave the directory as we found it

[14]:
import os

os.remove("SPMe.pkl")
os.remove("SPMe_sol.pkl")
os.remove("sol_data.pkl")
os.remove("sol_data.csv")
os.remove("sol_data.mat")

References#

The relevant papers for this notebook are:

[15]:
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] 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.
[3] 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.
[4] 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.