Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

3937 events with idaklu output variables - solver edit #4300

Merged

Conversation

pipliggins
Copy link
Contributor

@pipliggins pipliggins commented Jul 29, 2024

Description

This is an alternative to #4150 (old pull request will be closed)

Simulations using the IDAKLU solver with output variables specified which terminated with an event rather than the final time step would crash trying to find the termination event.

Termination events are found in the Solution class by using the state vector of the final time-step to evaluate each potential termination event and find the minimum (the event which caused the termination). When output variables are specified in the IDAKLU solver only those variables are output in y_out, not the entire state vector, so the events could not be evaluated outside the solver.

This PR edits the IDAKLU solver to add an additional return value containing the state vector for the final timestep only ('y_term') when output variables are specified. This value is used to fill the Solution.y_event attribute and allows termination events to be calculated as normal, while not impacting the performance advantages of selecting only a few output variables. When output variables are not specified, this returns an empty array as it copies part of the provided state vector.

Fixes #3937

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

@pipliggins
Copy link
Contributor Author

As mentioned in the previous PR (#4150 (comment)), specifying output variables in the IDAKLU solver doesn't currently work with experiments. This bug fix should enable that behaviour to be added, but I think it should be a separate issue/PR for a new feature, rather than added to this one.

Copy link

codecov bot commented Jul 29, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 99.45%. Comparing base (5408ce3) to head (167dd15).
Report is 161 commits behind head on develop.

Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #4300      +/-   ##
===========================================
- Coverage    99.45%   99.45%   -0.01%     
===========================================
  Files          288      288              
  Lines        22092    22091       -1     
===========================================
- Hits         21972    21971       -1     
  Misses         120      120              

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

Copy link
Contributor

@jsbrittain jsbrittain left a comment

Choose a reason for hiding this comment

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

Looks good to me.

Copy link
Contributor

@martinjrobins martinjrobins left a comment

Choose a reason for hiding this comment

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

thanks @pipliggins, looks good

@martinjrobins martinjrobins merged commit f255c38 into pybamm-team:develop Jul 30, 2024
25 of 26 checks passed
@martinjrobins
Copy link
Contributor

@all-contributors please add @pipliggins for code and test

Copy link
Contributor

@martinjrobins

I've put up a pull request to add @pipliggins! 🎉

js1tr3 pushed a commit to js1tr3/PyBaMM that referenced this pull request Aug 12, 2024
)

* Add test that fails

* Remove duplicate attribute assignment

* IDAKLU solver returns additional y_term variable containing the final state vector slice

* Add final state vector slice to python-idaklu, tests pass

* Reduce memory load so y_term is only filled if output variables are specified.
Otherwise empty array.

* Edit changelog

---------

Co-authored-by: Martin Robinson <martinjrobins@gmail.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.

[Bug]: output variables option in idaklu solver fails when event triggered
3 participants