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

Re-solve before root finding when handling events in casadi #1328

Merged
merged 4 commits into from
Jan 18, 2021

Conversation

rtimms
Copy link
Contributor

@rtimms rtimms commented Jan 18, 2021

Description

Currently when an event is triggered in casadi we construct a linear interpolant, use a root finding algorithm to find the event time, and then go back and solve again up to the event time. This can give the wrong event time when the linear interpolant isn't a good fit to the actual solution (see #1318).

This PR changes it so that when an event is triggered we first go back and re-solve in the current time window using a more dense array of times. This solution is then used to construct a higher-order interpolant for use in the root finding algorithm. This allows the event time to be found more accurately.

I'm leaving #1318 open as there are some other (more sophisticated) methods for handling events that could be explored.

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: $ flake8
  • All tests pass: $ python run-tests.py --unit
  • The documentation builds: $ cd docs and then $ make clean; make html

You can run all three at once, using $ python run-tests.py --quick.

Further checks:

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

@codecov
Copy link

codecov bot commented Jan 18, 2021

Codecov Report

Merging #1328 (1c40966) into develop (948505e) will increase coverage by 0.00%.
The diff coverage is 100.00%.

Impacted file tree graph

@@           Coverage Diff            @@
##           develop    #1328   +/-   ##
========================================
  Coverage    98.12%   98.12%           
========================================
  Files          272      272           
  Lines        15439    15443    +4     
========================================
+ Hits         15150    15154    +4     
  Misses         289      289           
Impacted Files Coverage Δ
pybamm/solvers/base_solver.py 99.14% <100.00%> (ø)
pybamm/solvers/casadi_solver.py 99.54% <100.00%> (+<0.01%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 948505e...1c40966. Read the comment docs.

@rtimms rtimms requested a review from valentinsulzer January 18, 2021 17:00
Copy link
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.

thanks @rtimms !

@rtimms rtimms merged commit 6e1aa13 into develop Jan 18, 2021
@rtimms rtimms deleted the issue-1318-casadi-event branch January 18, 2021 21:25
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.

2 participants