diff --git a/CHANGELOG.md b/CHANGELOG.md index ef7d7c5a2a..29d1f3f990 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 @@ -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)) diff --git a/pybamm/solvers/base_solver.py b/pybamm/solvers/base_solver.py index 98fa02dd59..1ed9785162 100644 --- a/pybamm/solvers/base_solver.py +++ b/pybamm/solvers/base_solver.py @@ -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 diff --git a/pybamm/solvers/processed_variable.py b/pybamm/solvers/processed_variable.py index 7261a9aaad..20a6150200 100644 --- a/pybamm/solvers/processed_variable.py +++ b/pybamm/solvers/processed_variable.py @@ -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 @@ -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 diff --git a/tests/unit/test_solvers/test_casadi_solver.py b/tests/unit/test_solvers/test_casadi_solver.py index 4678e7fec9..1b44445115 100644 --- a/tests/unit/test_solvers/test_casadi_solver.py +++ b/tests/unit/test_solvers/test_casadi_solver.py @@ -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"],