Importing previous simulation output into a new simulation using an interpolant

Hi,
I am trying to solve 2 simulations sequentially, and use an output from the first simulation as an input to the second. This output is a function of both space and time, j(x,t). I have 2 questions:

  1. I’m trying to use an interpolant to do this, although this doesn’t seem ideal, since there is no need to interpolate in space, as the two simulations are defined on the same mesh - is there a better way to do this?

  2. I’m getting errors using the interpolant, and I’m wondering if it’s not supposed to be used with a SpatialVariable? I am defining the interpolant as follows:

x_p = pybamm.SpatialVariable("x_p", domain=["positive electrode"], coord_sys="cartesian")
j_func = pybamm.Interpolant((x_data, t_data), j_data, (x_p, pybamm.t))

where, x_data and t_data are 1D arrays of length x_pts and t_pts and j_data is a 2d array of shape (x_pts, t_pts).

When trying to do

c_s_p = pybamm.Variable("Positive particle concentration [mol.m-3]", domain="positive electrode")
a = c_s_p + constant * j_func

I get the following error:

cannot reshape array of size 1 into shape (17,1)
AttributeError: 'Interpolant' object has no attribute '_saved_evaluate_for_shape'

Is it possible to use the interpolant class with a spatial variable?

Thanks,

Isaac

@isaac_basil the sim.solve function has a starting_sol argument which takes the final step of a previous solution and uses it as the first step of a new simulation. Is that what you’re looking for?

See this example for more details: Degradation experiments with reference performance tests — PyBaMM v25.1.1 Manual

Hi @Marc thanks for your reply. Unfortunately I need access to the solution at all time steps, so I don’t think that will work for me. Essentially I need to be able to use my solution from Simulation 1 to define j(x,t) which I can use in Simulation 2.

In theory I should be able to do this all in a single simulation. But I’m having problems with that, so I am trying to split into 2 steps to debug.

Hi @isaac_basil, you might be able to make a 1D interpolant in time for each of the 17 spatial positions of j(x,t) and then concatenating them.

Hi @Marc that’s a nice idea I will try that. On that note, I’m not sure where 17 has even come from, since I’m discretising the electrode using 40 nodes.

But how would you go about doing this @Marc ? I’ve only used concatenations to combine symbols that are defined on two different domains, so I’m not sure how I could apply that in this case, where each symbol is relevant to a particular x node.