Skip to content

[Bug]: Different sensitivities when calculated with/without output variables #5058

@pipliggins

Description

@pipliggins

PyBaMM Version

25.6

Python Version

3.11

Describe the bug

The value of sensitivities calculated for non-0D variables is different when the solver uses the output variables option in IDAKLU vs not.

Steps to Reproduce

import numpy as np
import pybamm

# logic copied from test_with_output_variables_and_sensitivities()

input_parameters = {  # Sensitivities dictionary
    "Current function [A]": 0.222,
    "Separator porosity": 0.3,
}

# construct model
model = pybamm.lithium_ion.DFN()
geometry = model.default_geometry
param = model.default_parameter_values
param.update({key: "[input]" for key in input_parameters})
param.process_model(model)
param.process_geometry(geometry)
var_pts = {"x_n": 50, "x_s": 50, "x_p": 50, "r_n": 5, "r_p": 5}
mesh = pybamm.Mesh(geometry, model.default_submesh_types, var_pts)
disc = pybamm.Discretisation(mesh, model.default_spatial_methods)
disc.process_model(model)

t_eval = [0, 100]
t_interp = np.linspace(t_eval[0], t_eval[-1], 5)

options = {
    "linear_solver": "SUNLinSol_KLU",
    "jacobian": "sparse",
    "num_threads": 4,
    "max_num_steps": 1000,
}

# Use a selection of variables of different types
output_variables = [
    "Voltage [V]",
    "Time [min]",
    "x [m]",  # 1d
    "Negative particle flux [mol.m-2.s-1]",  # 2d
    "Throughput capacity [A.h]",  # ExplicitTimeIntegral
]

sols = []
for out_vars in [True, False]:
    solver = pybamm.IDAKLUSolver(
        options=options,
        output_variables=output_variables if out_vars else None,
    )

    sol = solver.solve(
        model,
        t_eval,
        inputs=input_parameters,
        calculate_sensitivities=True,
        t_interp=t_interp,
    )
    sols.append(sol)

for varname in output_variables:
    try:
        np.testing.assert_allclose(
            sols[0][varname].sensitivities["Current function [A]"],
            sols[1][varname].sensitivities["Current function [A]"].flatten(),
            err_msg=f"Failed for {varname} sensitivities, variable type: {type(sols[1][varname])}",
        )
    except AssertionError as e:
        print(e)

Relevant log output

Failed for x [m] sensitivities, variable type: <class 'pybamm.solvers.processed_variable.ProcessedVariable1D'>
Mismatched elements: 9 / 750 (1.2%)
Max absolute difference: 0.02777778
Max relative difference: inf
 x: array([0.      , 0.      , 0.      , 0.      , 0.      , 0.001015,
       0.001294, 0.001536, 0.001749, 0.001937, 0.      , 0.006944,
       0.013889, 0.020833, 0.027778, 0.      , 0.      , 0.      ,...
 y: array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
       0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
       0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,...

Not equal to tolerance rtol=1e-07, atol=0
Failed for Negative particle flux [mol.m-2.s-1] sensitivities, variable type: <class 'pybamm.solvers.processed_variable.ProcessedVariable2D'>
Mismatched elements: 1050 / 1500 (70%)
Max absolute difference: 4.04033229e-05
Max relative difference: 1.
 x: array([0., 0., 0., ..., 0., 0., 0.])
 y: array([0.000000e+00, 0.000000e+00, 0.000000e+00, ..., 9.231209e-06,
       2.189335e-05, 4.040332e-05])

Not equal to tolerance rtol=1e-07, atol=0
Failed for Throughput capacity [A.h] sensitivities, variable type: <class 'pybamm.solvers.processed_variable.ProcessedVariable0D'>
Mismatched elements: 4 / 5 (80%)
Max absolute difference: 0.02777778
Max relative difference: 1.
 x: array([0., 0., 0., 0., 0.])
 y: array([0.      , 0.006944, 0.013889, 0.020833, 0.027778])

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingsolver

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions