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

#2789 add "all" to sensitivities for idaklu solver #2883

Merged
merged 4 commits into from
Apr 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
## Bug fixes

- Fixed a bug in the discretisation of initial conditions of a scaled variable ([#2856](https://github.com/pybamm-team/PyBaMM/pull/2856))
- Fixed keyerror on "all" when getting sensitivities from IDAKLU solver([#2883](https://github.com/pybamm-team/PyBaMM/pull/2883))

# Breaking changes

Expand Down
7 changes: 6 additions & 1 deletion pybamm/solvers/idaklu_solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -532,14 +532,19 @@ def _integrate(self, model, t_eval, inputs_dict=None):
y_out = sol.y.reshape((number_of_timesteps, number_of_states))

# return sensitivity solution, we need to flatten yS to
# (#timesteps * #states,) to match format used by Solution
# (#timesteps * #states (where t is changing the quickest),)
# to match format used by Solution
# note that yS is (n_p, n_t, n_y)
if number_of_sensitivity_parameters != 0:
yS_out = {
name: sol.yS[i].reshape(-1, 1)
for i, name in enumerate(sensitivity_names)
}
# add "all" stacked sensitivities ((#timesteps * #states,#sens_params))
yS_out["all"] = np.hstack([yS_out[name] for name in sensitivity_names])
else:
yS_out = False

if sol.flag in [0, 2]:
# 0 = solved for all t_eval
if sol.flag == 0:
Expand Down
5 changes: 5 additions & 0 deletions tests/unit/test_solvers/test_idaklu_solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ def test_ida_roberts_klu_sensitivities(self):
model.rhs = {u: a * v}
model.algebraic = {v: 1 - v}
model.initial_conditions = {u: 0, v: 1}
model.variables = {"2u": 2 * u}

disc = pybamm.Discretisation()
disc.process_model(model)
Expand Down Expand Up @@ -251,6 +252,10 @@ def test_ida_roberts_klu_sensitivities(self):

np.testing.assert_array_almost_equal(dyda_ida, dyda_fd)

# get the sensitivities for the variable
d2uda = sol["2u"].sensitivities["a"]
np.testing.assert_array_almost_equal(2 * dyda_ida[0:200:2], d2uda)

def test_sensitivities_with_events(self):
# this test implements a python version of the ida Roberts
# example provided in sundials
Expand Down