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

Faster (re)initialization of ODEs in IDA #4453

Merged
merged 5 commits into from
Sep 19, 2024

Conversation

MarcBerliner
Copy link
Member

@MarcBerliner MarcBerliner commented Sep 19, 2024

Description

In IDA, we can compute an analytical solution for consistent initialization/reinitialization of ODEs, which avoids an expensive call to IDACalcIC. This change gives a modest speed improvement to all the ODE models solved by the IDAKLUSolver and will give a bigger speedup to those with a dense t_eval vector. For example,

import pybamm
import numpy as np

model = pybamm.lithium_ion.SPM()
solver = pybamm.IDAKLUSolver()
sim = pybamm.Simulation(model, solver=solver)

t_eval = np.linspace(0, 3600, 10000)
sol = sim.solve(t_eval)

print(sol.integration_time)
  • New integration_time: 367 ms
  • Old integration_time: 514 ms
  • Speedup: 1.4x

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.

  • Optimization (back-end change that speeds up the code)

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

Copy link

codecov bot commented Sep 19, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 99.46%. Comparing base (5bb146f) to head (75ca46b).
Report is 2 commits behind head on develop.

Additional details and impacted files
@@           Coverage Diff            @@
##           develop    #4453   +/-   ##
========================================
  Coverage    99.46%   99.46%           
========================================
  Files          293      293           
  Lines        22381    22381           
========================================
  Hits         22261    22261           
  Misses         120      120           

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

src/pybamm/solvers/c_solvers/idaklu/IDAKLUSolverOpenMP.inl Outdated Show resolved Hide resolved
id_val[ii] = id_np_val[ii];
const bool id_i = id_np_val[ii];
id_val[ii] = id_i;
is_ODE &= id_i == 1;
Copy link
Contributor

Choose a reason for hiding this comment

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

If id_i is a bool, then the id_i == 1 should be redundant

Copy link
Contributor

Choose a reason for hiding this comment

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

This bit is a confusing jumble of types, these are not really bool, but are floats indicating if the state is differential (1.0) or algebraic (0.0), this is just the format that sundials expects. I think introducing a bool here is just confusing an already confusing sundials thing (e.g. they never say in the docs what happens to values that are not 0 or 1!), perhaps:

id_val[ii] = id_np_val[ii];
is_ODE &= id_val[ii] == 1.0

Copy link
Contributor

Choose a reason for hiding this comment

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

Float comparisons are not reliable either. Float equality checks are not good to have in the 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.

@martinjrobins good point! I'll make this change

Copy link
Contributor

@kratman kratman Sep 19, 2024

Choose a reason for hiding this comment

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

@martinjrobins To avoid a float equality comparison we should have something like

is_ODE &= id_val[ii] > SMALL_NUMBER

That way we are not expecting equality and we just check that it is greater than zero.

Copy link
Member Author

Choose a reason for hiding this comment

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

I made this an inequalty check to handle minor floating point arithmetic issues

Copy link
Contributor

@martinjrobins martinjrobins Sep 19, 2024

Choose a reason for hiding this comment

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

@kratman: totally agree! but this is how sundials docs define this vector (as a vector of floats, 1.0 = differential and 0.0 == algebraic) so I think we have to follow this definition, no matter how strange it is. Also, I'm not a fan of implicit type conversion, which is happening here twice (once from realtype -> bool, then from bool -> realtype)

Copy link
Contributor

Choose a reason for hiding this comment

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

saying that, its not a big deal so happy with is_ODE &= id_val[ii] > SMALL_NUMBER, as long as the type conversions are removed. it shouldn't make any difference to the result

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah sorry for the confusion, I was talking to Marc separately removing the type conversion was already part of it

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.

looks great @MarcBerliner, just that one minor comment about the bool. I'm supprised that sundials doesn't do this in CalcIC, it has the id vector so can tell if it is an ode or not. Great find!

@kratman kratman merged commit 7be637c into pybamm-team:develop Sep 19, 2024
26 checks passed
@kratman kratman deleted the fast-ode-reinitialization branch September 19, 2024 19:39
MarcBerliner added a commit to MarcBerliner/PyBaMM that referenced this pull request Sep 22, 2024
commit d362c98
Merge: 7be637c f5717ff
Author: Marc Berliner <34451391+MarcBerliner@users.noreply.github.com>
Date:   Fri Sep 20 10:27:14 2024 -0400

    Merge pull request pybamm-team#4440 from pipliggins/output_vars_extrapolation

    Fix IDAKLU output_variables to work with extrapolation events

commit f5717ff
Merge: b012684 7be637c
Author: Marc Berliner <34451391+MarcBerliner@users.noreply.github.com>
Date:   Fri Sep 20 09:21:26 2024 -0400

    Merge branch 'develop' into output_vars_extrapolation

commit b012684
Author: Pip Liggins <philippa.liggins@dtc.ox.ac.uk>
Date:   Thu Sep 19 17:16:35 2024 -0700

    Switch test to triggered event

commit 7be637c
Author: Marc Berliner <34451391+MarcBerliner@users.noreply.github.com>
Date:   Thu Sep 19 15:39:27 2024 -0400

    Faster (re)initialization of ODEs in `IDA` (pybamm-team#4453)

    * Fast ODE reinitialization

    * Update CHANGELOG.md

    * remove redundant `IDAReInit` call

    * address comments

    * Update IDAKLUSolverOpenMP.inl

commit cee2cad
Author: Pip Liggins <philippa.liggins@dtc.ox.ac.uk>
Date:   Thu Sep 19 10:22:21 2024 -0700

    update changelog

commit 37c94f9
Author: Pip Liggins <philippa.liggins@dtc.ox.ac.uk>
Date:   Thu Sep 19 10:10:32 2024 -0700

    Add test

commit 06d7ecc
Merge: f47c12e 4cda488
Author: Pip Liggins <philippa.liggins@dtc.ox.ac.uk>
Date:   Thu Sep 19 09:25:57 2024 -0700

    Merge branch 'develop' into output_vars_extrapolation

commit 4cda488
Merge: 48dbb68 5bb146f
Author: Valentin Sulzer <valentinsulzer@hotmail.com>
Date:   Thu Sep 19 02:15:59 2024 -0700

    Merge pull request pybamm-team#4330 from parkec3/ocvr_ecm

    ECM with split OCV

commit 5bb146f
Merge: 712a3ee 48dbb68
Author: Valentin Sulzer <valentinsulzer@hotmail.com>
Date:   Thu Sep 19 00:19:24 2024 -0700

    Merge branch 'develop' into ocvr_ecm

commit 48dbb68
Author: Martin Robinson <martinjrobins@gmail.com>
Date:   Wed Sep 18 22:21:38 2024 +0100

    feat: add OpenMP parallelization to IDAKLU solver for lists of input parameters (pybamm-team#4449)

    * new solver option `num_solvers`, indicates how many solves run in parallel
    * existing `num_threads` gives total number of threads which are distributed among `num_solvers`

commit e1118ec
Author: Marc Berliner <34451391+MarcBerliner@users.noreply.github.com>
Date:   Wed Sep 18 12:35:47 2024 -0400

    Update CODEOWNERS (pybamm-team#4452)

commit f47c12e
Merge: d6b213c 05a0b24
Author: Pip Liggins <philippa.liggins@dtc.ox.ac.uk>
Date:   Mon Sep 16 17:36:13 2024 -0700

    Merge branch 'develop' into output_vars_extrapolation

commit 05a0b24
Author: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Date:   Mon Sep 16 22:08:33 2024 +0100

    chore: update pre-commit hooks (pybamm-team#4445)

    updates:
    - [github.com/astral-sh/ruff-pre-commit: v0.6.4 → v0.6.5](astral-sh/ruff-pre-commit@v0.6.4...v0.6.5)

    Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>

commit 8e3eb31
Author: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Date:   Mon Sep 16 16:00:04 2024 -0400

    Build(deps): bump github/codeql-action in the actions group (pybamm-team#4444)

    Bumps the actions group with 1 update: [github/codeql-action](https://github.com/github/codeql-action).

    Updates `github/codeql-action` from 3.26.6 to 3.26.7
    - [Release notes](https://github.com/github/codeql-action/releases)
    - [Changelog](https://github.com/github/codeql-action/blob/main/CHANGELOG.md)
    - [Commits](github/codeql-action@4dd1613...8214744)

    ---
    updated-dependencies:
    - dependency-name: github/codeql-action
      dependency-type: direct:production
      update-type: version-update:semver-patch
      dependency-group: actions
    ...

    Signed-off-by: dependabot[bot] <support@github.com>
    Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

commit d6b213c
Author: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Date:   Fri Sep 13 23:14:56 2024 +0000

    style: pre-commit fixes

commit 343cea4
Author: Pip Liggins <philippa.liggins@dtc.ox.ac.uk>
Date:   Fri Sep 13 15:51:17 2024 -0700

    change check_extrapolation to use t/y_event

commit 712a3ee
Merge: 3b21bf6 6c1815b
Author: Valentin Sulzer <valentinsulzer@hotmail.com>
Date:   Sat Sep 7 13:10:30 2024 -0700

    Merge branch 'develop' into ocvr_ecm

commit 3b21bf6
Author: Eric G. Kratz <kratman@users.noreply.github.com>
Date:   Tue Sep 3 16:45:19 2024 -0400

    Move changelog update to unreleased

commit 4786443
Merge: 13270c2 ac93806
Author: Eric G. Kratz <kratman@users.noreply.github.com>
Date:   Tue Sep 3 16:44:14 2024 -0400

    Merge branch 'develop' into ocvr_ecm

commit 13270c2
Merge: f23ac77 1ab27d1
Author: Eric G. Kratz <kratman@users.noreply.github.com>
Date:   Tue Sep 3 13:20:33 2024 -0400

    Merge branch 'develop' into ocvr_ecm

commit f23ac77
Author: Caitlin Parke <caitlin@ionworks.com>
Date:   Tue Sep 3 13:11:22 2024 -0400

    updated tests and naming convention

commit b820e59
Merge: 9a996c4 ac6c450
Author: Eric G. Kratz <kratman@users.noreply.github.com>
Date:   Wed Aug 28 16:20:47 2024 -0400

    Merge branch 'develop' into ocvr_ecm

commit 9a996c4
Author: Caitlin Parke <caitlin@ionworks.com>
Date:   Wed Aug 21 16:46:28 2024 -0400

    updated for pytest and better coverage

commit e4504a6
Author: Caitlin Parke <caitlin@ionworks.com>
Date:   Tue Aug 20 18:18:42 2024 -0400

    added docs

commit b4c1897
Merge: 3f1e7f5 977dcf9
Author: Caitlin D. Parke <caitlin.parke93@gmail.com>
Date:   Mon Aug 19 20:09:15 2024 -0400

    Merge branch 'pybamm-team:develop' into ocvr_ecm

commit 3f1e7f5
Author: Caitlin Parke <caitlin@ionworks.com>
Date:   Mon Aug 19 20:07:44 2024 -0400

    added default plotting variables

commit 84fba69
Merge: b1de9d1 99e3119
Author: Caitlin Parke <caitlin@ionworks.com>
Date:   Fri Aug 16 00:44:34 2024 -0400

    Merge branch 'ocvr_ecm' of https://github.com/parkec3/PyBaMM into ocvr_ecm

commit b1de9d1
Author: Caitlin Parke <caitlin@ionworks.com>
Date:   Fri Aug 16 00:44:19 2024 -0400

    pre-commit

commit 99e3119
Author: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Date:   Fri Aug 16 04:42:19 2024 +0000

    style: pre-commit fixes

commit 758175f
Author: Caitlin Parke <caitlin@ionworks.com>
Date:   Fri Aug 16 00:42:04 2024 -0400

    added tests

commit 1fc5670
Merge: 3daabce 1e3f139
Author: Caitlin D. Parke <caitlin.parke93@gmail.com>
Date:   Fri Aug 16 00:40:38 2024 -0400

    Merge branch 'pybamm-team:develop' into ocvr_ecm

commit 3daabce
Author: Caitlin Parke <caitlin@ionworks.com>
Date:   Wed Aug 14 12:01:58 2024 -0400

    parameter updates

commit a502d17
Merge: dbeaeb5 9691d09
Author: Caitlin D. Parke <caitlin.parke93@gmail.com>
Date:   Wed Aug 14 11:58:58 2024 -0400

    Merge branch 'pybamm-team:develop' into ocvr_ecm

commit dbeaeb5
Author: Caitlin Parke <caitlin@ionworks.com>
Date:   Tue Aug 13 14:45:42 2024 -0400

    still working

commit 3bc77c2
Merge: 2a945f2 83239a3
Author: Caitlin Parke <caitlin@ionworks.com>
Date:   Mon Aug 12 11:25:01 2024 -0400

    Merged remote into local changes

commit 2a945f2
Author: Caitlin Parke <caitlin@ionworks.com>
Date:   Mon Aug 12 11:17:11 2024 -0400

    Added parameter objects in model

commit 83239a3
Author: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Date:   Fri Aug 9 20:43:34 2024 +0000

    style: pre-commit fixes

commit f93e67b
Author: Caitlin Parke <caitlin@ionworks.com>
Date:   Fri Aug 9 16:39:33 2024 -0400

    pre-commit changes

commit 80428c9
Merge: f3faca4 b1fc595
Author: Caitlin D. Parke <caitlin.parke93@gmail.com>
Date:   Fri Aug 9 16:34:30 2024 -0400

    Merge branch 'pybamm-team:develop' into ocvr_ecm

commit f3faca4
Author: Caitlin Parke <caitlin@ionworks.com>
Date:   Fri Aug 9 16:33:10 2024 -0400

    Added ECM + split OCV model
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.

3 participants