Skip to content

Make IDAKLU output variables compatible with Experiments #4386

@brosaplanella

Description

@brosaplanella

This was initially reported as a bug (see report below) but it turns out it is not an implemented feature yet. It seems the main issue is that when a new experiment step is initialised with the previous step solution, that one is empty so the relevant variables are not available. I believe that amending the last_state so it stores the whole state vector even when output variables are used should fix this.

PyBaMM Version

develop

Python Version

3.11.6

Describe the bug

I am running a model with the IDAKLUSolver and some output_variables. These include, amongst others, the "Discharge capacity [A.h]", however PyBaMM complains that that variable is not found.

Steps to Reproduce

import pybamm

pybamm.set_logging_level("NOTICE")

model = pybamm.lithium_ion.DFN()

output_variables = ["Discharge capacity [A.h]", "Time [s]", "Current [A]", "Voltage [V]"]
solver = pybamm.IDAKLUSolver(output_variables=output_variables)

experiment = pybamm.Experiment(
    [
        (
            "Charge at 1C until 4.2 V",
            "Hold at 4.2 V until C/50",
            "Rest for 1 hour",
        )
    ]
)

sim = pybamm.Simulation(
    model,
    experiment=experiment,
    solver=solver,
)

sol = sim.solve()

Relevant log output

Traceback (most recent call last):
  File "/home/brosaplanella/PyBaMM/src/pybamm/models/base_model.py", line 808, in set_initial_conditions_from
    final_state = solution[var.name]
                  ~~~~~~~~^^^^^^^^^^
  File "/home/brosaplanella/PyBaMM/src/pybamm/solvers/solution.py", line 547, in __getitem__
    self.update(key)
  File "/home/brosaplanella/PyBaMM/src/pybamm/solvers/solution.py", line 481, in update
    raise KeyError(
KeyError: "Cannot process variable 'Discharge capacity [A.h]' as it was not part of the solve. Please re-run the solve with `output_variables` set to include this variable."

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/brosaplanella/battery_modelling/degradation-data/examples/mwe.py", line 29, in <module>
    sol = sim.solve()
          ^^^^^^^^^^^
  File "/home/brosaplanella/PyBaMM/src/pybamm/simulation.py", line 702, in solve
    step_solution = solver.step(
                    ^^^^^^^^^^^^
  File "/home/brosaplanella/PyBaMM/src/pybamm/solvers/base_solver.py", line 1222, in step
    _, concatenated_initial_conditions = model.set_initial_conditions_from(
                                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/brosaplanella/PyBaMM/src/pybamm/models/base_model.py", line 810, in set_initial_conditions_from
    raise pybamm.ModelError(
pybamm.expression_tree.exceptions.ModelError: To update a model from a solution, each variable in model.initial_conditions must appear in the solution with the same key as the variable name. In the solution provided, 'Cannot process variable 'Discharge capacity [A.h]' as it was not part of the solve. Please re-run the solve with `output_variables` set to include this variable.' was not found.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions