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 1221 convert to casadi #1316

Merged
merged 19 commits into from
Dec 31, 2020
Merged

Conversation

valentinsulzer
Copy link
Member

Description

Convert variables to casadi for faster post-processing.
The conversion is done the first time the variable is called, and the casadi function is then stored in the model (in model._variables_casadi. Therefore, when doing repeated solutions of the same model (e.g. for parameter sweeps) the conversion is only performed the first time.

Fixes #1221

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 Dec 31, 2020

Codecov Report

Merging #1316 (4b84f39) into develop (66b9589) will increase coverage by 0.00%.
The diff coverage is 100.00%.

Impacted file tree graph

@@           Coverage Diff            @@
##           develop    #1316   +/-   ##
========================================
  Coverage    98.10%   98.10%           
========================================
  Files          272      272           
  Lines        15277    15290   +13     
========================================
+ Hits         14988    15001   +13     
  Misses         289      289           
Impacted Files Coverage Δ
pybamm/models/base_model.py 99.11% <100.00%> (+0.03%) ⬆️
pybamm/simulation.py 96.91% <100.00%> (-0.05%) ⬇️
pybamm/solvers/base_solver.py 99.04% <100.00%> (+<0.01%) ⬆️
pybamm/solvers/casadi_solver.py 99.47% <100.00%> (-0.01%) ⬇️
pybamm/solvers/processed_symbolic_variable.py 100.00% <100.00%> (ø)
pybamm/solvers/processed_variable.py 99.59% <100.00%> (-0.03%) ⬇️
pybamm/solvers/solution.py 99.43% <100.00%> (+0.05%) ⬆️
pybamm/expression_tree/unary_operators.py 99.79% <0.00%> (-0.21%) ⬇️
pybamm/expression_tree/operations/evaluate.py 98.09% <0.00%> (ø)
pybamm/solvers/idaklu_solver.py 90.26% <0.00%> (+0.88%) ⬆️

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 66b9589...4b84f39. Read the comment docs.

Copy link
Sponsor Member

@brosaplanella brosaplanella 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, happy to merge once the test passes. Thanks Tino!

)

# Solve using "safe" mode with debug off
pybamm.settings.debug_mode = False
solver = pybamm.CasadiSolver(mode="safe", rtol=1e-8, atol=1e-8, dt_max=1)
t_eval = np.linspace(0, 5, 100)
solution = solver.solve(model, t_eval)
np.testing.assert_array_less(solution.y[0], 1.5)
np.testing.assert_array_less(solution.y[-1], 2.5 + 1e-10)
np.testing.assert_array_less(solution.y.full()[0], 1.5)
Copy link
Sponsor Member

Choose a reason for hiding this comment

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

This test fails but I can't understand why it doesn't take the .full().

Copy link
Member Author

Choose a reason for hiding this comment

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

get_termination_reason had changed in #1300 and was returning a numpy array

@valentinsulzer valentinsulzer merged commit 0926080 into develop Dec 31, 2020
@valentinsulzer valentinsulzer deleted the issue-1221-convert-to-casadi branch December 31, 2020 22:45
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.

Optionally convert variable to casadi format before evaluating
2 participants