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

Order of sensitivites #1886

Closed
Scottmar93 opened this issue Jan 7, 2022 · 1 comment · Fixed by #1894
Closed

Order of sensitivites #1886

Scottmar93 opened this issue Jan 7, 2022 · 1 comment · Fixed by #1894
Assignees
Labels
bug Something isn't working

Comments

@Scottmar93
Copy link
Contributor

  • PyBaMM version - 21.12:
  • Python version - 3.8.10:

Describe the bug
The order of sensitivities seems to not align correctly when there are multiple sensitivities. The example below shows the sensitivity of terminal voltage SPM to D_n in two cases. Case 1 where only D_n is an input and Case 2 where D_n and b_n (negative electrolyte bruggeman coefficient) are inputs. In Case 2, it appears that the sensitivites of the terminal voltage w.r.t D_n and b_n have been switched. Seems likely a switching of order in either the building of the sensitivity rhs or when extracting from casadi.

To Reproduce

import pybamm
import numpy as np
import matplotlib.pyplot as plt

# Case 1
chemistry = pybamm.parameter_sets.Ecker2015
parameter_values = pybamm.ParameterValues(chemistry=chemistry)
parameter_values["Negative electrode diffusivity [m2.s-1]"] = "[input]"

model = pybamm.lithium_ion.SPM()
sim = pybamm.Simulation(model, parameter_values=parameter_values)

inputs = {
    "Negative electrode diffusivity [m2.s-1]": 1e-13,
}

t_eval = np.linspace(0, 30, 1000)
sim.solve(t_eval=t_eval, inputs=inputs, calculate_sensitivities=True)
sol1 = sim.solution


# Case 2
chemistry = pybamm.parameter_sets.Ecker2015
parameter_values = pybamm.ParameterValues(chemistry=chemistry)
parameter_values["Negative electrode diffusivity [m2.s-1]"] = "[input]"
parameter_values["Negative electrode Bruggeman coefficient (electrolyte)"] = "[input]"

model = pybamm.lithium_ion.SPM()
sim = pybamm.Simulation(model, parameter_values=parameter_values)

inputs = {
    "Negative electrode diffusivity [m2.s-1]": 1e-13,
    "Negative electrode Bruggeman coefficient (electrolyte)": 1.5,
}

sim.solve(t_eval=t_eval, inputs=inputs, calculate_sensitivities=True)
sol2 = sim.solution

# Plot
fig, ax = plt.subplots(2, 1, figsize=(16, 9), sharex=True)
ax[0].plot(
    t_eval,
    sol1["Terminal voltage [V]"].sensitivities[
        "Negative electrode diffusivity [m2.s-1]"
    ],
    label="sol1 D_n",
)
ax[0].plot(
    t_eval,
    sol2["Terminal voltage [V]"].sensitivities[
        "Negative electrode diffusivity [m2.s-1]"
    ],
    linestyle="--",
    label="sol2 D_n",
)

ax[1].plot(
    t_eval,
    sol1["Terminal voltage [V]"].sensitivities[
        "Negative electrode diffusivity [m2.s-1]"
    ],
    label="sol1 D_n",
)
ax[1].plot(
    t_eval,
    sol2["Terminal voltage [V]"].sensitivities[
        "Negative electrode Bruggeman coefficient (electrolyte)"
    ],
    linestyle="--",
    label="sol2 b",
)

ax[0].grid()
ax[1].grid()

ax[0].set_xlabel("Time [s]")
ax[1].set_xlabel("Time [s]")

ax[0].legend()
ax[1].legend()

plt.show()

Expected behaviour
The sensitivity of terminal voltage w.r.t D_n should be independent of the number of other inputs.

Screenshots
Plot generated by above example.

image

@Scottmar93 Scottmar93 added the bug Something isn't working label Jan 7, 2022
@Scottmar93
Copy link
Contributor Author

Scottmar93 commented Jan 7, 2022

Some further information:

Placing a debug on line 241 of solution.py (end of _extract_explicit_sensitivites) and then printing np.max(sensitivity["Negative electrode diffusivity [m2.s-1]"]) gives a large number greater than zero which corresponds to the sensitivity of the diffusion coefficient. So it seems to be giving the correct order at this point. Looks that the ordering issue arises after extracting the values from casadi and therefore somewhere within ProcessedVariable

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant