Skip to content

Observe custom variables after solving + faster simulation build time#5308

Merged
MarcBerliner merged 82 commits intodevelopfrom
observe-outputs
Dec 17, 2025
Merged

Observe custom variables after solving + faster simulation build time#5308
MarcBerliner merged 82 commits intodevelopfrom
observe-outputs

Conversation

@MarcBerliner
Copy link
Copy Markdown
Member

@MarcBerliner MarcBerliner commented Dec 5, 2025

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

agriyakhetarpal and others added 30 commits November 13, 2025 09:12
Co-authored-by: Valentin Sulzer <valentinsulzer@hotmail.com>
* Fix typo in concentration description in notebook

* Add CHANGELOG.md entry for typo fix

* Remove unneccesary changelog entry

Co-authored-by: Agriya Khetarpal <74401230+agriyakhetarpal@users.noreply.github.com>

---------

Co-authored-by: Agriya Khetarpal <74401230+agriyakhetarpal@users.noreply.github.com>
* fix `InputParameter` serialisation

* Update CHANGELOG.md
…-fix

Don't be too strict with func_args longer than symbol.children
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
* feat: add`silence_sundial_warnings` solver option

* refactor: `silence_sundials_warnings` -> `silence_sundials_errors`
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
* raise `SolverError` at failure to init sundials

* Update simulation.py

* Update idaklu_solver.py

* reuse `pybammsolvers` error messages

* Update test_idaklu_solver.py

* bump `pybammsolvers`

* Update CHANGELOG.md

* Update CHANGELOG.md

Update CHANGELOG.md
@review-notebook-app
Copy link
Copy Markdown

Check out this pull request on  ReviewNB

See visual diffs & provide feedback on Jupyter Notebooks.


Powered by ReviewNB

@MarcBerliner MarcBerliner changed the title Observe 0D custom variables after solving Observe custom variables after solving Dec 15, 2025
@MarcBerliner MarcBerliner changed the title Observe custom variables after solving Observe custom variables after solving + faster simulation build time Dec 15, 2025
Copy link
Copy Markdown
Member

@valentinsulzer valentinsulzer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a beast of a PR! Just a few small comments but I think this is almost there

Comment thread src/pybamm/expression_tree/operations/serialise.py Outdated
Comment thread src/pybamm/expression_tree/operations/serialise.py Outdated
Comment thread src/pybamm/plotting/quick_plot.py Outdated
Comment thread src/pybamm/solvers/base_solver.py
Comment thread tests/integration/test_solvers/test_solution.py Outdated
@MarcBerliner MarcBerliner merged commit 6ff7848 into develop Dec 17, 2025
34 of 38 checks passed
@MarcBerliner MarcBerliner deleted the observe-outputs branch December 17, 2025 20:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants