Skip to content

Add "voltage as a state" option#4507

Merged
rtimms merged 8 commits into
developfrom
voltage-as-a-state
Oct 15, 2024
Merged

Add "voltage as a state" option#4507
rtimms merged 8 commits into
developfrom
voltage-as-a-state

Conversation

@rtimms

@rtimms rtimms commented Oct 11, 2024

Copy link
Copy Markdown
Contributor

Description

Adds an option "voltage as a state" that can be "false" (default) or "true". If "true" adds an explicit algebraic equation for the voltage. This can be used to reformulate the SPM(e) as a DAE rather than and ODE. This increases the solve time but evaluating the voltage as an explicit state rather than as a function of other state variables can be faster if the time eval is dense.

import pybamm
import numpy as np

t = np.linspace(0, 3600, 100000)
for value in ["true", "false"]:
    model = pybamm.lithium_ion.SPMe({"voltage as a state": value})
    sim = pybamm.Simulation(model, solver=pybamm.IDAKLUSolver())
    sim.solve([0, 3600])
    print("voltage as a state:", value)
    solve_time = sim.solution.solve_time
    print("solve time:", solve_time)
    timer = pybamm.Timer()
    sim.solution["Voltage [V]"](t)
    eval_time = timer.time()
    print("eval time:", eval_time)
    print("total time:", solve_time + eval_time, "\n")

returns

voltage as a state: true
solve time: 3.293 ms
eval time: 8.836 ms
total time: 12.129 ms 

voltage as a state: false
solve time: 2.311 ms
eval time: 687.892 ms
total time: 690.204 ms 

Fixes # (issue)

Type of change

Please add a line in the relevant section of CHANGELOG.md to document the change (include PR #) - note reverse order of PR #s. If necessary, also add to the list of breaking changes.

  • New feature (non-breaking change which adds functionality)
  • Optimization (back-end change that speeds up the code)
  • Bug fix (non-breaking change which fixes an issue)

Key checklist:

  • No style issues: $ pre-commit run (or $ nox -s pre-commit) (see CONTRIBUTING.md for how to set this up to run automatically when committing locally, in just two lines of code)
  • All tests pass: $ python run-tests.py --all (or $ nox -s tests)
  • The documentation builds: $ python run-tests.py --doctest (or $ nox -s doctests)

You can run integration tests, unit tests, and doctests together at once, using $ python run-tests.py --quick (or $ nox -s quick).

Further checks:

  • Code is commented, particularly in hard-to-understand areas
  • Tests added that prove fix is effective or that feature works

@codecov

codecov Bot commented Oct 11, 2024

Copy link
Copy Markdown

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 99.42%. Comparing base (d48f8c6) to head (0648a91).
Report is 160 commits behind head on develop.

Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #4507      +/-   ##
===========================================
- Coverage    99.42%   99.42%   -0.01%     
===========================================
  Files          301      299       -2     
  Lines        22736    22715      -21     
===========================================
- Hits         22605    22584      -21     
  Misses         131      131              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

valentinsulzer
valentinsulzer previously approved these changes Oct 14, 2024

@valentinsulzer valentinsulzer left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Looks good, thanks. Those timings are crazy - is that even with the latest changes from Marc?

@rtimms

rtimms commented Oct 15, 2024

Copy link
Copy Markdown
Contributor Author

Yep. Evaluating all those asinh terms is slow if you have a large t_eval.

@rtimms rtimms merged commit 47c165d into develop Oct 15, 2024
@rtimms rtimms deleted the voltage-as-a-state branch October 15, 2024 14:37
pkalbhor pushed a commit to pkalbhor/PyBaMM that referenced this pull request Nov 15, 2024
* add voltage as a state option

* improve coverage

* update CHANGELOG

---------

Co-authored-by: Valentin Sulzer <valentinsulzer@hotmail.com>
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.

3 participants