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 664 casadi #687

Merged
merged 28 commits into from
Oct 28, 2019
Merged

Issue 664 casadi #687

merged 28 commits into from
Oct 28, 2019

Conversation

valentinsulzer
Copy link
Member

@valentinsulzer valentinsulzer commented Oct 26, 2019

Description

Use CasADi to provide big speed-up to both setup and solve:

  • Convert PyBaMM expression tree to CasADi
  • Wrap CasADi solver

Fixes #664

Still to do:

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.

  • 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 Oct 26, 2019

Codecov Report

Merging #687 into master will increase coverage by 0.02%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff            @@
##           master    #687      +/-   ##
=========================================
+ Coverage   98.48%   98.5%   +0.02%     
=========================================
  Files         171     173       +2     
  Lines        8589    8776     +187     
=========================================
+ Hits         8459    8645     +186     
- Misses        130     131       +1
Impacted Files Coverage Δ
pybamm/expression_tree/operations/jacobian.py 100% <ø> (ø)
...mm/models/submodels/electrode/ohm/composite_ohm.py 100% <ø> (ø) ⬆️
pybamm/expression_tree/operations/simplify.py 94.98% <ø> (ø)
pybamm/models/submodels/particle/base_particle.py 97.29% <ø> (ø) ⬆️
pybamm/expression_tree/operations/evaluate.py 95.74% <ø> (ø)
pybamm/parameters/electrical_parameters.py 100% <ø> (ø) ⬆️
pybamm/parameters/parameter_sets.py 100% <ø> (ø) ⬆️
pybamm/solvers/casadi_solver.py 100% <100%> (ø)
pybamm/models/base_model.py 98.31% <100%> (ø) ⬆️
...mm/expression_tree/operations/convert_to_casadi.py 100% <100%> (ø)
... and 12 more

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 2af6103...466a4c4. Read the comment docs.

Copy link
Contributor

@Scottmar93 Scottmar93 left a comment

Choose a reason for hiding this comment

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

Looks excellent @tinosulzer, really good job. A few bits of code seem to be in place for event which we shouild maybe remove

solve_time = timer.time() - solve_start_time

# Identify the event that caused termination
termination = self.get_termination_reason(solution, self.events)
Copy link
Contributor

Choose a reason for hiding this comment

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

You managed to get events working?

Copy link
Member Author

Choose a reason for hiding this comment

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

Not yet, will remove this

return states_eval - model.mass_matrix.entries @ ydot

# Create event-dependent function to evaluate events
def event_fun(event):
Copy link
Contributor

Choose a reason for hiding this comment

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

If events aren't working should we remove this bit of code?

Copy link
Member Author

Choose a reason for hiding this comment

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

Still need this for setting up with casadi and passing to other solvers

@@ -53,7 +53,7 @@ def rhs(t, y):
def alg(t, y):
return np.array([1 - y[1]])

solver = pybamm.IDAKLU()
solver = pybamm.IDAKLUSolver()
Copy link
Contributor

Choose a reason for hiding this comment

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

Not a fan of my naming haha

Copy link
Contributor

@rtimms rtimms 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 thanks @tinosulzer ! might be good to update the install instructions to say that pybamm comes with casadi so can solve daes, but doesn't support events at the moment. then recommend installing either scikits or KLU (probably recommend KLU since it performs better for large systems). then the line in the scikits install about needing sundials for DFN can be removed as you only need to install sundials separately if you want to use the optional scikits ot KLU

@@ -96,7 +103,7 @@ def __init__(self, name="Unnamed model"):
# Default behaviour is to use the jacobian and simplify
self.use_jacobian = True
self.use_simplify = True
self.use_to_python = True
self.convert_to_format = "python"
Copy link
Contributor

Choose a reason for hiding this comment

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

if casadi performs better should we just use that? though maybe nice to keep the pybamm expression tree by default. maybe it would be good to add a notebook or some kind of readme about using pybamm with casadi to highlight why/when you might want to use this option

Copy link
Member Author

Choose a reason for hiding this comment

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

Will update the install instructions separately in #646 as I've made a few changes there anyway. Casadi is faster than python but harder to debug (not sure how you step through the expression tree to see which bit of code is messing up) so not sure which one should be default? I lean towards casadi

Copy link
Member Author

Choose a reason for hiding this comment

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

Actually, casadi doesn't handle interpolation yet (in pybamm, it has the functionality but needs to be wrapped), and other similar issues might appear, so I will leave python as default until more confident with casadi

Copy link
Contributor

Choose a reason for hiding this comment

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

sounds sensible

@valentinsulzer valentinsulzer merged commit 3676ad0 into master Oct 28, 2019
@valentinsulzer valentinsulzer deleted the issue-664-casadi branch October 28, 2019 13:42
rtimms added a commit that referenced this pull request Oct 31, 2019
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 solver
3 participants