PyBaMM icon indicating copy to clipboard operation
PyBaMM copied to clipboard

Observe custom variables after solving + faster simulation build time

Open MarcBerliner opened this issue 3 weeks ago • 3 comments

Description

Adds a few features:

  • All values in the model.variables dict are now lazily evaluated as a post-processing step, which reduces build time by ~2.25x. The total time to solve the first simulation is now ~1.5x faster.
  • Adds the ability to lazily observe custom pybamm expressions after solving the simulation. E.g.,
import pybamm

model = pybamm.lithium_ion.SPM()

sim = pybamm.Simulation(model)

sol = sim.solve([0, 1000])

soc = model.variables["Discharge capacity [A.h]"] / model.variables["Nominal cell capacity [A.h]"]

sol.observe(soc)
# <pybamm.solvers.processed_variable.ProcessedVariable0D at 0x104606c90>

which supports all variables. This requires that the solver does not haveoutput_variables.

Type of change

Please add a line in the relevant section of CHANGELOG.md to document the change (include PR #)

Important checks:

Please confirm the following before marking the PR as ready for review:

  • No style issues: nox -s pre-commit
  • All tests pass: nox -s tests
  • The documentation builds: nox -s doctests
  • Code is commented for hard-to-understand areas
  • Tests added that prove fix is effective or that feature works

MarcBerliner avatar Dec 05 '25 21:12 MarcBerliner