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

[Bug]: IDAKLU output_variables fails when checking for interpolant_extrapolation events #4439

Closed
pipliggins opened this issue Sep 13, 2024 · 1 comment · Fixed by #4440
Closed
Assignees
Labels
bug Something isn't working

Comments

@pipliggins
Copy link
Contributor

pipliggins commented Sep 13, 2024

PyBaMM Version

develop

Python Version

3.11.6

Describe the bug

When running a model with the IDAKLU solver and some output_variables and the solution contains INTERPOLANT_EXTRAPOLATION events, pybamm complains about y_slice vectors being too short.

The code to reproduce is taken from #4414 .

Steps to Reproduce

import pybamm
import numpy as np

# Define inputs
inputs = {
    "Current function [A]": 0.222,
    "Separator porosity": 0.3,
}

# Set-up the model
options = {"cell geometry": "arbitrary", "thermal": "lumped"}
model = pybamm.lithium_ion.DFN(options=options)
geometry = model.default_geometry
# param = model.default_parameter_values
param = pybamm.ParameterValues.create_from_bpx(r"lfp_18650_cell_BPX.json")
param.update(inputs)
var = pybamm.standard_spatial_vars
var_pts = {var.x_n: 20, var.x_s: 20, var.x_p: 20, var.r_n: 10, var.r_p: 10}

# Use a short time-vector for this example
t_eval = np.linspace(0, 360, 10)

# Include the variable of interest in output_variables
output_variables = [
    "Voltage [V]",
    "Current [A]",
    "Time [min]",
    "X-averaged cell temperature [C]",  # want to retrieve this and print the output
]

# Create the IDAKLU Solver object
idaklu_solver = pybamm.IDAKLUSolver(
    rtol=1e-6, atol=1e-6, output_variables=output_variables
)

sim = pybamm.Simulation(
    model,
    parameter_values=param,
    geometry=geometry,
    var_pts=var_pts,
    solver=idaklu_solver,
    output_variables=output_variables,
)
sol = sim.solve(
    t_eval=t_eval,
    calculate_sensitivities=True,
)

Relevant log output

File "/Users/pipliggins/Documents/repos/pybamm-local/issue_test_copy.py", line 47, in <module>
    sol = sim.solve(
          ^^^^^^^^^^
  File "/Users/pipliggins/Documents/repos/pybamm-local/src/pybamm/simulation.py", line 534, in solve
    self._solution = solver.solve(
                     ^^^^^^^^^^^^^
  File "/Users/pipliggins/Documents/repos/pybamm-local/src/pybamm/solvers/base_solver.py", line 957, in solve
    self.check_extrapolation(solution, model.events)
  File "/Users/pipliggins/Documents/repos/pybamm-local/src/pybamm/solvers/base_solver.py", line 1497, in check_extrapolation
    and event.expression.evaluate(t, y, inputs=inputs) < self.extrap_tol
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/pipliggins/Documents/repos/pybamm-local/src/pybamm/expression_tree/functions.py", line 130, in evaluate
    evaluated_children = [
                         ^
  File "/Users/pipliggins/Documents/repos/pybamm-local/src/pybamm/expression_tree/functions.py", line 131, in <listcomp>
    child.evaluate(t, y, y_dot, inputs) for child in self.children
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/pipliggins/Documents/repos/pybamm-local/src/pybamm/expression_tree/binary_operators.py", line 160, in evaluate
    left = self.left.evaluate(t, y, y_dot, inputs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/pipliggins/Documents/repos/pybamm-local/src/pybamm/expression_tree/binary_operators.py", line 160, in evaluate
    left = self.left.evaluate(t, y, y_dot, inputs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/pipliggins/Documents/repos/pybamm-local/src/pybamm/expression_tree/binary_operators.py", line 161, in evaluate
    right = self.right.evaluate(t, y, y_dot, inputs)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/pipliggins/Documents/repos/pybamm-local/src/pybamm/expression_tree/symbol.py", line 817, in evaluate
    return self._base_evaluate(t, y, y_dot, inputs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/pipliggins/Documents/repos/pybamm-local/src/pybamm/expression_tree/state_vector.py", line 292, in _base_evaluate
    raise ValueError(
ValueError: y is too short, so value with slice is smaller than expected
@BradyPlanden
Copy link
Member

Just as an additional datapoint, we are seeing this on PyBOP as well. Thanks for the fix @pipliggins!

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.

2 participants