Pass current timestep to affect! to modify integrator values with lookup table

I am trying to simulate patient response to a drug and am currently fitting a model to patient data. However the frequency and strength of the dose changes frequently, so I have the time and strength for each dose recorded in a dataframe. Is it possible to pass the current timestep t to affect! so that it can lookup the value in the table?

example_doses = DataFrame([[0,3,10,40], [50,88,88,12]], ["time", "dose"])

function condition(u, t, integrator)
    t in example_doses.time
end

function affect!(integrator)
    integrator.u[10] = example_doses.dose[findfirst(==(t), example_doses.time)]
end

cb = ContinuousCallback(condition, affect!)

Note the actual doses dataframe has > 1000 rows and this must be simulated for many patients, so as far as I can see storing the doses in the integrator parameters to be accessed via integrator.p as mentioned here is not practical.

it’s just integrator.t to get the current time.

Though note that if you’re going to do this you need to set tstop. You may want to use the PresetTimeCallback for this.

1 Like

Thanks Chris, this was exactly what I was looking for.