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

Issue 853 casadi safe #956

Merged
merged 17 commits into from
Apr 29, 2020
Merged

Issue 853 casadi safe #956

merged 17 commits into from
Apr 29, 2020

Conversation

rtimms
Copy link
Contributor

@rtimms rtimms commented Apr 14, 2020

Description

Changes the behaviour of casadi "safe" mode. Instead of creating a new integrator for each dt in t_eval (which scales badly with the number of points in t_eval), it now integrates in larger "global" timesteps and checks if an event has been triggered. If an event is triggered in some interval [t, t+dt] then the earliest event time is found by a root finding algorithm and the state at that time is found by interpolation. The solution is then truncated and returned up to the event time.

Fixes #853

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 Apr 15, 2020

Codecov Report

Merging #956 into develop will increase coverage by 0.02%.
The diff coverage is 100.00%.

Impacted file tree graph

@@             Coverage Diff             @@
##           develop     #956      +/-   ##
===========================================
+ Coverage    97.34%   97.36%   +0.02%     
===========================================
  Files          222      222              
  Lines        11622    11689      +67     
===========================================
+ Hits         11313    11381      +68     
+ Misses         309      308       -1     
Impacted Files Coverage Δ
...dels/submodels/current_collector/potential_pair.py 100.00% <ø> (ø)
pybamm/solvers/base_solver.py 100.00% <ø> (+0.27%) ⬆️
pybamm/solvers/casadi_solver.py 100.00% <100.00%> (ø)

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 cde479d...33aff3a. Read the comment docs.

@rtimms
Copy link
Contributor Author

rtimms commented Apr 15, 2020

question: at the moment dt_max is set somewhat arbitrarily. should we allow this to be passed as an option so that users have control over the “global” step?

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.

For dt_max, I think the most consistent thing would be to make it 1 (or 1/2 or 1/4), and assume that the user has chosen a good timescale, but also offer it as an option. (but for battery models the timescale doesn't change with current so it's not failsafe ...)

Can you leave old safe mode as an option (e.g. "old safe")? I think the sensitivity formulation will be closest to that

I also couldn't find the test where you don't suppress stderr - I wanted to see "regularity check": False or "show_eval_warnings": False can do that for you (annoyingly, there's a different options dictionary for when you create the solver and when you call it, and I don't know which you should pass to)

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.

Looks great now, thanks @rtimms !

# Make sure t_eval is monotonic
if (np.diff(t_eval) < 0).any():
raise pybamm.SolverError("t_eval must increase monotonically")

Copy link
Member

Choose a reason for hiding this comment

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

why did you remove this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

the same check is performed on line 589

@rtimms
Copy link
Contributor Author

rtimms commented Apr 18, 2020

thanks, just need to fix a problem where sometimes casadi gets stuck doing Linear solve failed psetup failed forever when you step too far. It doesn’t get caught in the try/except, so I need to find the right option the catches that, but doesn’t cause it to bail out early in other cases.

@valentinsulzer
Copy link
Member

Maybe some kind of timeout? https://medium.com/@chaoren/how-to-timeout-in-python-726002bf2291

@rtimms rtimms merged commit 6852e0e into develop Apr 29, 2020
@rtimms rtimms deleted the issue-853-casadi-safe branch April 29, 2020 05:29
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.

casadi safe mode
2 participants