Can't use pybamm.Interpolant as input

When trying to use a pybamm.Interpolant as an input argument to the solve() method.

parameter_values[“Current function [A]”] = “[input]”

interpolant = pybamm.Interpolant(experiment[:, 0], experiment[:, 1], pybamm.t)
model = pybamm.lithium_ion.DFN()
solver = pybamm.IDAKLUSolver()
sim = pybamm.Simulation(
model, solver=solver, parameter_values=parameter_values
)

solution = sim.solve(inputs={“Current function [A]”: interpolant})

The simulation fails with the following error

Traceback (most recent call last):
File line 84, in
solution = sim.solve([0, 300.0], inputs={“Current function [A]”: interpolant})
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/home/oarcelus/miniconda3/envs/pybamm/lib/python3.12/site-packages/pybamm/simulation.py”, line 541, in solve
self._solution = solver.solve(
^^^^^^^^^^^^^
File “/home/oarcelus/miniconda3/envs/pybamm/lib/python3.12/site-packages/pybamm/solvers/base_solver.py”, line 799, in solve
self.set_up(model, model_inputs_list[0], t_eval)
File “/home/oarcelus/miniconda3/envs/pybamm/lib/python3.12/site-packages/pybamm/solvers/idaklu_solver.py”, line 265, in set_up
base_set_up_return = super().set_up(model, inputs, t_eval, ics_only)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/home/oarcelus/miniconda3/envs/pybamm/lib/python3.12/site-packages/pybamm/solvers/base_solver.py”, line 170, in set_up
self._set_initial_conditions(model, 0.0, inputs)
File “/home/oarcelus/miniconda3/envs/pybamm/lib/python3.12/site-packages/pybamm/solvers/base_solver.py”, line 293, in _set_initial_conditions
inputs_y0_ics = casadi.vertcat(*[x for x in inputs.values()])
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/home/oarcelus/miniconda3/envs/pybamm/lib/python3.12/site-packages/casadi/casadi.py”, line 518, in vertcat
return _vertcat(args)
^^^^^^^^^^^^^^
File “/home/oarcelus/miniconda3/envs/pybamm/lib/python3.12/site-packages/casadi/casadi.py”, line 34167, in _vertcat
return _casadi._vertcat(*args)
^^^^^^^^^^^^^^^^^^^^^^^
NotImplementedError: Wrong number or type of arguments for overloaded function ‘_vertcat’.
Possible prototypes are:
_vertcat([Sparsity])
_vertcat([DM])
_vertcat([SX])
_vertcat([MX])
You have: ‘((Interpolant))’

I want to select my current input only after I generate the pybamm.Simulation object, such that I do not need to reinstantiante the entire simulation object each time I want to change the current (beit constant current or a dyanmic profile). How could I do it?

Hi @oarcelus, the simulation is created with a fixed value for the interpolant, and it’s not possible to update it after the simulation is created. You can run a new constant current step without regenerating the simulation, like

parameter_values["Current function [A]"] = "[input]"
model = pybamm.lithium_ion.DFN()
solver = pybamm.IDAKLUSolver()
sim = pybamm.Simulation(
    model, solver=solver, parameter_values=parameter_values
)

solution = sim.solve([0, 1000], inputs={"Current function [A]": 2})

You can input in pybamm.Simulation(experiment= pybamm.Experiment( [pybamm.step.current(in as a 2d array where first column is time and 2nd current]). This would be a better work aprund for this problem.

Thank you!

Then, I am thinking whether or not parameters that depend on Callables might be changed aswell. For example. Inside a get_parameter_values() function is the following construction valid?

“Negative electrode reaction rate constant [m.s-1]”: 9.09e-7,
“Negative electrode exchange-current density [A.m-2]”: graphite_electrolyte_exchange_current_density

where

def graphite_electrolyte_exchange_current_density(c_e, c_s_surf, c_s_max, T):
k_n = pybamm.Parameter(“Negative electrode reaction rate constant [m.s-1]”)
c_e0 = pybamm.Parameter(“Initial concentration in electrolyte [mol.m-3]”)
alpha_a = pybamm.Parameter(“Negative electrode charge transfer coefficient”)
alpha_c = 1.0 - alpha_a

return (
pybamm.constants.F
* k_n
* (c_e / c_e0) ** alpha_a
* c_s_surf**alpha_c
* (c_s_max - c_s_surf) ** alpha_a
/ 2
)

and where I am afterwords adding the paramater[“Negative electrode reaction rate constant [m.s-1]”] = “[input]”, to then change it when running solve(inputs=…) hopping that the exchange current density will be affected.

Is this the case or am I mistaken?

Best regards,

@oarcelus yes, that’s right.