Hey All,
I’m trying to calculate the error of a differential equations sol object with respect to specific timepoints.
The system is made up as follows:
function dAdt(dA, A, p, t)
CL, V1, Q, V2 = p
k₁₀ = CL / V1
k₁₂ = Q / V1
k₂₁ = Q / V2
dA[1] = A[2] * k₂₁ - A[1] * (k₁₀ + k₁₂)
dA[2] = A[1] * k₁₂ - A[2] * k₂₁
end
I am using a PresetTimeCallback to change the system at timepoints which differs between individuals. Essentially I put a dose inside the volume (V1) of compartment 1 (A[1]):
dose_timepoint = 0.
dose = 2000.
affect!(integrator) = integrator.u[1] += (dose / integrator.p[2])
callback = PresetTimeCallback(dose_timepoint, affect!; save_positions=(false, false)
I am using save_positions=(false, false)
here since I want the sol object to only return my saveat indexes:
parameters = [200., 2000., 100., 3000.]
measurement_times = [0., 1., 4. 24., 48]
prob = ODEProblem(dAdt, [0., 0.], (-20., 120.), parameters)
sol = solve(prod, tstops=dose_timepoint, saveat=measurement_times, callback=callback)
# for example returns u[1]=[10., 8., 4., 0., 0.]
This works as expected. When I want to use interpolation however the dose from the callback is added to A[1], but this is not reflected in u[1]:
sol = solve(prod, tstops=dose_timepoint, callback=callback)
sol(measurement_times)
# for example returns u[1]=[-5., 3., 1., 0., 0.]
What you can see above is that A[1] is filled with the dose from the callback, causing dA[1] to be negative. This time however u[1] remains 0., causing the result to become negative. After some time the concentration in A[2] is large enough to actually start filling A[1], so A[1] becomes postive again.
Setting save_positions = (true, true) for the callback fixes the issue.
Is this a expected issue when using interpolation? I of course can manage with using saveat, and setting save_positions = true when I need to use interpolation, but I would expect save_positions to only save the positions to the sol.t, and not affect the solution in other ways, as seems to be the case.
What are your thoughts? Is this a bug?