Skip to content

Commit 31ec93c

Browse files
Merge pull request #2883 from pybamm-team/i2789-short-discharge
#2789 add "all" to sensitivities for idaklu solver
2 parents 1d6bd48 + d3391b0 commit 31ec93c

File tree

3 files changed

+12
-1
lines changed

3 files changed

+12
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
## Bug fixes
1111

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

1415
# Breaking changes
1516

pybamm/solvers/idaklu_solver.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -532,14 +532,19 @@ def _integrate(self, model, t_eval, inputs_dict=None):
532532
y_out = sol.y.reshape((number_of_timesteps, number_of_states))
533533

534534
# return sensitivity solution, we need to flatten yS to
535-
# (#timesteps * #states,) to match format used by Solution
535+
# (#timesteps * #states (where t is changing the quickest),)
536+
# to match format used by Solution
537+
# note that yS is (n_p, n_t, n_y)
536538
if number_of_sensitivity_parameters != 0:
537539
yS_out = {
538540
name: sol.yS[i].reshape(-1, 1)
539541
for i, name in enumerate(sensitivity_names)
540542
}
543+
# add "all" stacked sensitivities ((#timesteps * #states,#sens_params))
544+
yS_out["all"] = np.hstack([yS_out[name] for name in sensitivity_names])
541545
else:
542546
yS_out = False
547+
543548
if sol.flag in [0, 2]:
544549
# 0 = solved for all t_eval
545550
if sol.flag == 0:

tests/unit/test_solvers/test_idaklu_solver.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ def test_ida_roberts_klu_sensitivities(self):
200200
model.rhs = {u: a * v}
201201
model.algebraic = {v: 1 - v}
202202
model.initial_conditions = {u: 0, v: 1}
203+
model.variables = {"2u": 2 * u}
203204

204205
disc = pybamm.Discretisation()
205206
disc.process_model(model)
@@ -251,6 +252,10 @@ def test_ida_roberts_klu_sensitivities(self):
251252

252253
np.testing.assert_array_almost_equal(dyda_ida, dyda_fd)
253254

255+
# get the sensitivities for the variable
256+
d2uda = sol["2u"].sensitivities["a"]
257+
np.testing.assert_array_almost_equal(2 * dyda_ida[0:200:2], d2uda)
258+
254259
def test_sensitivities_with_events(self):
255260
# this test implements a python version of the ida Roberts
256261
# example provided in sundials

0 commit comments

Comments
 (0)