Skip to main content
A pybamm.Solution object is returned by sim.solve(). It stores the time vector, state trajectories, and processed variables, and provides methods to plot and export results.

Accessing variables

Index a solution with a variable name to obtain a ProcessedVariable:
sol = sim.solve([0, 3600])

voltage = sol["Terminal voltage [V]"]
print(voltage.entries)   # numpy array of values
print(voltage.t)         # numpy array of times
Variable names in PyBaMM include their units in square brackets, e.g. "Terminal voltage [V]", "Current [A]", "X-averaged cell temperature [K]".

ProcessedVariable

Indexing a Solution returns a ProcessedVariable object.
entries
numpy.ndarray
The variable values evaluated at all solution time points. For spatially resolved variables the shape is (n_spatial_points, n_times).
t
numpy.ndarray
The time points at which this variable was evaluated (seconds).
x
numpy.ndarray | None
Spatial coordinates for spatially resolved variables. None for scalar variables.
data
numpy.ndarray
Alias for entries. Provided for compatibility.
You can also call a ProcessedVariable at a specific time:
voltage = sol["Terminal voltage [V]"]
v_at_t = voltage(t=1800)  # interpolate at t = 1800 s

Solution properties

t
numpy.ndarray
All times at which the solution is evaluated (seconds), concatenated across sub-solutions.
y
numpy.ndarray | casadi.DM
Full state vector of shape (n_states, n_times).
cycles
list[pybamm.Solution]
List of per-cycle sub-solutions. Only populated when using a pybamm.Experiment.
termination
str
Human-readable reason the solver stopped, e.g. "final time", "event: Minimum voltage", or "event: Maximum voltage".
solve_time
pybamm.TimerTime
Wall-clock time spent inside the solver. Prints as a human-readable string (e.g. "1.234 s").
set_up_time
pybamm.TimerTime
Wall-clock time spent building and discretising the model.
summary_variables
pybamm.SummaryVariables | None
Cycle-level summary variables (capacity fade, energy throughput, etc.). Only populated when using a pybamm.Experiment.
all_inputs
list[dict]
The input parameter dictionaries used across all sub-solutions.
first_state
pybamm.Solution
A lightweight Solution containing only the first time point. Useful for initialising subsequent simulations.
last_state
pybamm.Solution
A lightweight Solution containing only the final time point.

Cycle and step access

When running an experiment, you can drill into individual cycles and steps:
sol = sim.solve()

# Access the second cycle (0-indexed)
cycle_2 = sol.cycles[1]

# Access the first step of cycle 2
step_1 = sol.cycles[1].steps[0]

# Plot just one cycle
cycle_2.plot()

# Variable access works the same on sub-solutions
print(cycle_2["Terminal voltage [V]"].entries)

Summary variables

After solving an experiment, sol.summary_variables gives cycle-level aggregate quantities:
sol = sim.solve()

print(sol.summary_variables["Capacity [A.h]"])
print(sol.summary_variables["Energy [W.h]"])
Summary variables include discharge capacity, charge capacity, energy throughput, and (for lithium-ion) eSOH quantities such as loss of lithium inventory and loss of active material.

Plotting

sol.plot()

Launch an interactive pybamm.QuickPlot:
sol.plot(
    output_variables=["Terminal voltage [V]", "Current [A]"]
)

sol.plot_summary_variables()

Plot cycle-summary quantities as a function of cycle number:
sol.plot_summary_variables()
plot_summary_variables() is only available after solving an experiment.

Saving and loading

import json

# Save solution data to JSON
data = {
    "time": sol.t.tolist(),
    "voltage": sol["Terminal voltage [V]"].entries.tolist(),
}
with open("result.json", "w") as f:
    json.dump(data, f)

# Or export to MATLAB .mat format
sol.save_data("result.mat", ["Terminal voltage [V]", "Current [A]"])

Examples

import pybamm
import numpy as np

model = pybamm.lithium_ion.SPM()
sim = pybamm.Simulation(model)
sol = sim.solve([0, 3600])

t = sol.t                                      # times in seconds
V = sol["Terminal voltage [V]"].entries        # voltages
I = sol["Current [A]"].entries                 # currents
T = sol["X-averaged cell temperature [K]"].entries

print(f"Final voltage: {V[-1]:.4f} V")

Build docs developers (and LLMs) love