Skip to content

Commit

Permalink
Squashed commit of the following:
Browse files Browse the repository at this point in the history
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
  • Loading branch information
MarcBerliner committed Sep 22, 2024
1 parent 017c4f4 commit ed8a997
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 24 deletions.
6 changes: 4 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,17 @@

- Added sensitivity calculation support for `pybamm.Simulation` and `pybamm.Experiment` ([#4415](https://github.com/pybamm-team/PyBaMM/pull/4415))
- Added OpenMP parallelization to IDAKLU solver for lists of input parameters ([#4449](https://github.com/pybamm-team/PyBaMM/pull/4449))
- Added phase-dependent particle options to LAM #4369
- Added a lithium ion equivalent circuit model with split open circuit voltages for each electrode (`SplitOCVR`). ([#4330](https://github.com/pybamm-team/PyBaMM/pull/4330))

## Optimizations

- Improved performance of initialization and reinitialization of ODEs in the (`IDAKLUSolver`). ([#4453](https://github.com/pybamm-team/PyBaMM/pull/4453))
- Removed the `start_step_offset` setting and disabled minimum `dt` warnings for drive cycles with the (`IDAKLUSolver`). ([#4416](https://github.com/pybamm-team/PyBaMM/pull/4416))

## Features
## Bug Fixes

- Added phase-dependent particle options to LAM #4369
- Fixed bug where IDAKLU solver failed when `output variables` were specified and an extrapolation event is present. ([#4440](https://github.com/pybamm-team/PyBaMM/pull/4440))

## Breaking changes

Expand Down
8 changes: 6 additions & 2 deletions src/pybamm/solvers/base_solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -1489,8 +1489,12 @@ def check_extrapolation(self, solution, events):

# second pass: check if the extrapolation events are within the tolerance
last_state = solution.last_state
t = last_state.all_ts[0][0]
y = last_state.all_ys[0][:, 0]
if solution.t_event:
t = solution.t_event[0]
y = solution.y_event[:, 0]
else:
t = last_state.all_ts[0][0]
y = last_state.all_ys[0][:, 0]
inputs = last_state.all_inputs[0]

if isinstance(y, casadi.DM):
Expand Down
29 changes: 28 additions & 1 deletion src/pybamm/solvers/c_solvers/idaklu/IDAKLUSolverOpenMP.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class IDAKLUSolverOpenMP : public IDAKLUSolver
int const number_of_events; // cppcheck-suppress unusedStructMember
int number_of_timesteps;
int precon_type; // cppcheck-suppress unusedStructMember
N_Vector yy, yyp, avtol; // y, y', and absolute tolerance
N_Vector yy, yp, y_cache, avtol; // y, y', y cache vector, and absolute tolerance
N_Vector *yyS; // cppcheck-suppress unusedStructMember
N_Vector *yypS; // cppcheck-suppress unusedStructMember
N_Vector id; // rhs_alg_id
Expand All @@ -71,6 +71,7 @@ class IDAKLUSolverOpenMP : public IDAKLUSolver
bool const sensitivity; // cppcheck-suppress unusedStructMember
bool const save_outputs_only; // cppcheck-suppress unusedStructMember
bool save_hermite; // cppcheck-suppress unusedStructMember
bool is_ODE; // cppcheck-suppress unusedStructMember
int length_of_return_vector; // cppcheck-suppress unusedStructMember
vector<realtype> t; // cppcheck-suppress unusedStructMember
vector<vector<realtype>> y; // cppcheck-suppress unusedStructMember
Expand Down Expand Up @@ -166,6 +167,32 @@ class IDAKLUSolverOpenMP : public IDAKLUSolver
*/
void PrintStats();

/**
* @brief Set a consistent initialization for ODEs
*/
void ReinitializeIntegrator(const realtype& t_val);

/**
* @brief Set a consistent initialization for the system of equations
*/
void ConsistentInitialization(
const realtype& t_val,
const realtype& t_next,
const int& icopt);

/**
* @brief Set a consistent initialization for DAEs
*/
void ConsistentInitializationDAE(
const realtype& t_val,
const realtype& t_next,
const int& icopt);

/**
* @brief Set a consistent initialization for ODEs
*/
void ConsistentInitializationODE(const realtype& t_val);

/**
* @brief Extend the adaptive arrays by 1
*/
Expand Down
83 changes: 65 additions & 18 deletions src/pybamm/solvers/c_solvers/idaklu/IDAKLUSolverOpenMP.inl
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ IDAKLUSolverOpenMP<ExprSet>::IDAKLUSolverOpenMP(
if (this->setup_opts.preconditioner != "none") {
precon_type = SUN_PREC_LEFT;
}

// The default is to solve a DAE for generality. This may be changed
// to an ODE during the Initialize() call
is_ODE = false;
}

template <class ExprSet>
Expand All @@ -95,12 +99,14 @@ void IDAKLUSolverOpenMP<ExprSet>::AllocateVectors() {
if (setup_opts.num_threads == 1) {
yy = N_VNew_Serial(number_of_states, sunctx);
yyp = N_VNew_Serial(number_of_states, sunctx);
y_cache = N_VNew_Serial(number_of_states, sunctx);
avtol = N_VNew_Serial(number_of_states, sunctx);
id = N_VNew_Serial(number_of_states, sunctx);
} else {
DEBUG("IDAKLUSolverOpenMP::AllocateVectors OpenMP");
yy = N_VNew_OpenMP(number_of_states, setup_opts.num_threads, sunctx);
yyp = N_VNew_OpenMP(number_of_states, setup_opts.num_threads, sunctx);
y_cache = N_VNew_OpenMP(number_of_states, setup_opts.num_threads, sunctx);
avtol = N_VNew_OpenMP(number_of_states, setup_opts.num_threads, sunctx);
id = N_VNew_OpenMP(number_of_states, setup_opts.num_threads, sunctx);
}
Expand Down Expand Up @@ -312,9 +318,13 @@ void IDAKLUSolverOpenMP<ExprSet>::Initialize() {
realtype *id_val;
id_val = N_VGetArrayPointer(id);

int ii;
for (ii = 0; ii < number_of_states; ii++) {
// Determine if the system is an ODE
is_ODE = number_of_states > 0;
for (int ii = 0; ii < number_of_states; ii++) {
id_val[ii] = id_np_val[ii];
// check if id_val[ii] approximately equals 1 (>0.999) handles
// cases where id_val[ii] is not exactly 1 due to numerical errors
is_ODE &= id_val[ii] > 0.999;
}

// Variable types: differential (1) and algebraic (0)
Expand All @@ -335,6 +345,7 @@ IDAKLUSolverOpenMP<ExprSet>::~IDAKLUSolverOpenMP() {
N_VDestroy(avtol);
N_VDestroy(yy);
N_VDestroy(yyp);
N_VDestroy(y_cache);
N_VDestroy(id);

if (sensitivity) {
Expand Down Expand Up @@ -367,9 +378,7 @@ SolutionData IDAKLUSolverOpenMP<ExprSet>::solve(
!save_outputs_only
);

// if (t.size() < number_of_evals + number_of_interps) {
InitializeStorage(number_of_evals + number_of_interps);
// }

int i_save = 0;

Expand Down Expand Up @@ -415,21 +424,16 @@ SolutionData IDAKLUSolverOpenMP<ExprSet>::solve(

SetSolverOptions();

CheckErrors(IDAReInit(ida_mem, t0, yy, yyp));
if (sensitivity) {
CheckErrors(IDASensReInit(ida_mem, IDA_SIMULTANEOUS, yyS, yypS));
}

// Prepare first time step
i_eval = 1;
realtype t_eval_next = t_eval[i_eval];


// Consistent initialization
ReinitializeIntegrator(t0);
int const init_type = solver_opts.init_all_y_ic ? IDA_Y_INIT : IDA_YA_YDP_INIT;
if (solver_opts.calc_ic) {
DEBUG("IDACalcIC");
// IDACalcIC will throw a warning if it fails to find initial conditions
IDACalcIC(ida_mem, init_type, t_eval_next);
ConsistentInitialization(t0, t_eval_next, init_type);
}

// Set the initial stop time
Expand Down Expand Up @@ -521,12 +525,8 @@ SolutionData IDAKLUSolverOpenMP<ExprSet>::solve(
CheckErrors(IDASetStopTime(ida_mem, t_eval_next));

// Reinitialize the solver to deal with the discontinuity at t = t_val.
// We must reinitialize the algebraic terms, so do not use init_type.
IDACalcIC(ida_mem, IDA_YA_YDP_INIT, t_eval_next);
CheckErrors(IDAReInit(ida_mem, t_val, yy, yyp));
if (sensitivity) {
CheckErrors(IDASensReInit(ida_mem, IDA_SIMULTANEOUS, yyS, yypS));
}
ReinitializeIntegrator(t_val);
ConsistentInitialization(t_val, t_eval_next, IDA_YA_YDP_INIT);
}

t_prev = t_val;
Expand Down Expand Up @@ -666,6 +666,53 @@ void IDAKLUSolverOpenMP<ExprSet>::ExtendHermiteArrays() {
}
}

template <class ExprSet>
void IDAKLUSolverOpenMP<ExprSet>::ReinitializeIntegrator(const realtype& t_val) {
DEBUG("IDAKLUSolver::ReinitializeIntegrator");
CheckErrors(IDAReInit(ida_mem, t_val, yy, yp));
if (sensitivity) {
CheckErrors(IDASensReInit(ida_mem, IDA_SIMULTANEOUS, yyS, ypS));
}
}

template <class ExprSet>
void IDAKLUSolverOpenMP<ExprSet>::ConsistentInitialization(
const realtype& t_val,
const realtype& t_next,
const int& icopt) {
DEBUG("IDAKLUSolver::ConsistentInitialization");

if (is_ODE && icopt == IDA_YA_YDP_INIT) {
ConsistentInitializationODE(t_val);
} else {
ConsistentInitializationDAE(t_val, t_next, icopt);
}
}

template <class ExprSet>
void IDAKLUSolverOpenMP<ExprSet>::ConsistentInitializationDAE(
const realtype& t_val,
const realtype& t_next,
const int& icopt) {
DEBUG("IDAKLUSolver::ConsistentInitializationDAE");
IDACalcIC(ida_mem, icopt, t_next);
}

template <class ExprSet>
void IDAKLUSolverOpenMP<ExprSet>::ConsistentInitializationODE(
const realtype& t_val) {
DEBUG("IDAKLUSolver::ConsistentInitializationODE");

// For ODEs where the mass matrix M = I, we can simplify the problem
// by analytically computing the yp values. If we take our implicit
// DAE system res(t,y,yp) = f(t,y) - I*yp, then yp = res(t,y,0). This
// avoids an expensive call to IDACalcIC.
realtype *y_cache_val = N_VGetArrayPointer(y_cache);
std::memset(y_cache_val, 0, number_of_states * sizeof(realtype));
// Overwrite yp
residual_eval<ExprSet>(t_val, yy, y_cache, yp, functions.get());
}

template <class ExprSet>
void IDAKLUSolverOpenMP<ExprSet>::SetStep(
realtype &tval,
Expand Down
2 changes: 1 addition & 1 deletion src/pybamm/solvers/idaklu_solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -1011,7 +1011,7 @@ def _rhs_dot_consistent_initialization(self, y0, model, time, inputs_dict):

rhs0 = rhs_alg0[: model.len_rhs]

# for the differential terms, ydot = -M^-1 * (rhs)
# for the differential terms, ydot = M^-1 * (rhs)
ydot0[: model.len_rhs] = model.mass_matrix_inv.entries @ rhs0

return ydot0
Expand Down
21 changes: 21 additions & 0 deletions tests/unit/test_solvers/test_idaklu_solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -1159,3 +1159,24 @@ def test_python_idaklu_deprecation_errors(self):
match="Unsupported evaluation engine for convert_to_format=jax",
):
_ = solver.solve(model, t_eval)

def test_extrapolation_events_with_output_variables(self):
# Make sure the extrapolation checks work with output variables
model = pybamm.BaseModel()
v = pybamm.Variable("v")
c = pybamm.Variable("c")
model.variables = {"v": v, "c": c}
model.rhs = {v: -1, c: 0}
model.initial_conditions = {v: 1, c: 2}
model.events.append(
pybamm.Event(
"Triggered event",
v - 0.5,
pybamm.EventType.INTERPOLANT_EXTRAPOLATION,
)
)
solver = pybamm.IDAKLUSolver(output_variables=["c"])
solver.set_up(model)

with pytest.warns(pybamm.SolverWarning, match="extrapolation occurred for"):
solver.solve(model, t_eval=[0, 1])

0 comments on commit ed8a997

Please sign in to comment.