I am trying to simulate stochastic population dynamics using CorrelatedWienerProcess
to add the noise. To prevent populations going negative I am using callbacks. Ideally I would like to use VectorContinuousCallback
so I can automatically generate the callbacks for any number of species.
However, I keep getting
ERROR: LoadError: MethodError: no method matching (::DiffEqNoiseProcess.var"#101#103")...
when trying to use both CorrelatedWienerProcess
& VectorContinuousCallback
. The solver runs fine when I have either no callbacks, use VectorContinuousCallback
with the default noise process, or use a CallbackSet
of DiscreteCallback
alongside the CorrelatedWienerProcess
- it is just the VectorContinuousCallback
-CorrelatedWienerProcess
combination that is giving me the error, so I am unsure what I am doing wrong.
Here is the code I have used to produce the error and the other working callback-noise combinations:
# Set up the equations
using DifferentialEquations, Plots
function test_equations(du, u, p, t)
du[1] = 0.0
du[2] = 0.0
du[3] = 0.0
end
function test_noise(du, u, p, t)
du[1] = 1.0
du[2] = 1.0
du[3] = 1.0
end
# Vector of continuous callbacks
function condition(out, u, t, integrator)
for i in eachindex(u)
out[i] = u[i]
end
end
function affect!(integrator, idx)
for i in eachindex(u)
if idx == i
integrator.u[i] = 0.001
end
end
end
cont_cb = VectorContinuousCallback(condition, affect!, 3)
# Discrete callback set
condition1(u,t,integrator) = u[1] <= 0.0
affect1!(integrator) = integrator.u[1] = 0.0
cb1 = DiscreteCallback(condition1,affect1!)
condition2(u,t,integrator) = u[2] <= 0.0
affect2!(integrator) = integrator.u[2] = 0.0
cb2 = DiscreteCallback(condition2,affect2!)
condition3(u,t,integrator) = u[3] <= 0.0
affect3!(integrator) = integrator.u[3] = 0.0
cb3 = DiscreteCallback(condition3,affect3!)
disc_cb = CallbackSet(cb1, cb2, cb3)
# Set up problems
u = [1.0, 1.0, 1.0]
tspan = (0.0, 150.0)
W = CorrelatedWienerProcess([0.25 -0.25 0.0; -0.25 0.25 0.0; 0.0 0.0 0.0], 0.0, zeros(3))
default_noise_prob = SDEProblem(test_equations, test_noise, u, tspan)
corr_noise_prob = SDEProblem(test_equations, test_noise, u, tspan, noise = W)
# Noise-callback combinations
default_noise_no_callback = solve(default_noise_prob)
corr_noise_no_callback = solve(corr_noise_prob)
default_noise_disc_callback = solve(default_noise_prob, callback = disc_cb)
corr_noise_disc_callback = solve(corr_noise_prob, callback = disc_cb)
default_noise_cont_callback = solve(default_noise_prob, callback = cont_cb)
# This is the only one that errors
corr_noise_cont_callback = solve(corr_noise_prob, callback = cont_cb)