Skip to content

Commit

Permalink
Merge pull request #1894 from pybamm-team/issue-1886-order-of-sensiti…
Browse files Browse the repository at this point in the history
…vities

#1886 added fix within processed variable
  • Loading branch information
valentinsulzer authored Jan 17, 2022
2 parents 751c18d + 2396ed3 commit 393ff5d
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 7 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

- Parameters can now be imported from any given path in `Windows` ([#1900](https://github.com/pybamm-team/PyBaMM/pull/1900))
- Fixed initial conditions for the EC SEI model ([#1895](https://github.com/pybamm-team/PyBaMM/pull/1895))
- Fixed issue in extraction of sensitivites ([#1894](https://github.com/pybamm-team/PyBaMM/pull/1894))

# [v21.12](https://github.com/pybamm-team/PyBaMM/tree/v21.11) - 2021-12-29

Expand All @@ -14,7 +15,6 @@
- Added cylindrical geometry and finite volume method ([#1824](https://github.com/pybamm-team/PyBaMM/pull/1824))

## Bug fixes

- `PyBaMM` is now importable in `Linux` systems where `jax` is already installed ([#1874](https://github.com/pybamm-team/PyBaMM/pull/1874))
- Simulations with drive cycles now support `initial_soc` ([#1842](https://github.com/pybamm-team/PyBaMM/pull/1842))
- Fixed bug in expression tree simplification ([#1831](https://github.com/pybamm-team/PyBaMM/pull/1831))
Expand Down
7 changes: 6 additions & 1 deletion pybamm/solvers/base_solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -1459,7 +1459,12 @@ def _set_up_ext_and_inputs(
inputs[name] = casadi.MX.sym(name, input_param._expected_size)

external_variables = external_variables or {}
ext_and_inputs = {**external_variables, **inputs}

ordered_inputs_names = list(inputs.keys())
ordered_inputs_names.sort()
ordered_inputs = {name: inputs[name] for name in ordered_inputs_names}

ext_and_inputs = {**external_variables, **ordered_inputs}
return ext_and_inputs


Expand Down
13 changes: 9 additions & 4 deletions pybamm/solvers/processed_variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -351,10 +351,14 @@ def initialise_2D(self):
self.second_dimension = "x"
self.R_sol = first_dim_pts
self.x_sol = second_dim_pts
elif self.domain[0] in [
"negative particle size",
"positive particle size",
] and self.auxiliary_domains["secondary"] == ["current collector"]:
elif (
self.domain[0]
in [
"negative particle size",
"positive particle size",
]
and self.auxiliary_domains["secondary"] == ["current collector"]
):
self.first_dimension = "R"
self.second_dimension = "z"
self.R_sol = first_dim_pts
Expand Down Expand Up @@ -563,6 +567,7 @@ def initialise_sensitivity_explicit_forward(self):
name: casadi.MX.sym(name, value.shape[0])
for name, value in self.all_inputs[0].items()
}

p_casadi_stacked = casadi.vertcat(*[p for p in p_casadi.values()])

# Convert variable to casadi format for differentiating
Expand Down
3 changes: 2 additions & 1 deletion tests/unit/test_solvers/test_casadi_solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -589,9 +589,10 @@ def test_solve_sensitivity_scalar_var_scalar_input(self):
solution = solver.solve(
model,
t_eval,
inputs={"p": 0.1, "q": 2, "r": -1, "s": 0.5},
inputs={"r": -1, "s": 0.5, "q": 2, "p": 0.1},
calculate_sensitivities=True,
)

np.testing.assert_allclose(solution.y[0], -1 + 0.2 * solution.t)
np.testing.assert_allclose(
solution.sensitivities["p"],
Expand Down

0 comments on commit 393ff5d

Please sign in to comment.