Hi there,
So I’m using a ContinuousCallback
method to simulate a simple Lotka-Volterra consumer-resource system of equations:
My goal is to simply create a callback such that when the resource R goes below a certain value (in my example, 60), the callback function takes the integrator to zero.
Using the bouncing ball example as a basis, I have formatted the code as following:
function dynamics!(du,u,p,t)
α, β, γ, δ = p
du[1] = (α - β * u[2]) * u[1]
du[2] = (-γ + δ * u[1]) * u[2]
end
function zero_condition!(u,t,integrator) # Event when event_f(u,t) <= 60
u[1] <= 60
end
function zero_affect!(integrator)
integrator.u[1] = 0
end
zero_event = ContinuousCallback(zero_condition!,zero_affect!)
u0 = [100, 100]
p = [0.55, 0.028, 0.84, 0.026]
tspan = (0.0,50.0)
prob = ODEProblem{true}(dynamics!,u0,tspan,p)
sol = solve(prob,Tsit5(), callback = zero_event)
plot(sol)
The code runs, and produces the following solution:
print(sol)
u: [[100.0, 100.0], [80.80845686345101, 114.00221332347269], [59.438166455464156, 126.7071869446583], [38.37959894566831, 134.27941573269763], [18.843731217381873, 130.52474821796022], [11.656653966686788, 121.35951343513864], [6.436587909054811, 105.673103443787], [3.874110828007275, 89.5763973309722], [2.2403782888041226, 69.74988819115794], [1.4937160732283663, 52.82058943966623], [1.1299759092760473, 38.650593036020666], [0.9615473179089438, 26.694047651872364], [0.9337419507717553, 17.161970095532308], [1.054390144771701, 9.912042531836985], [1.347009098584734, 5.531988127100285], [1.8952712402506138, 2.955913455350605], [2.8618179586434507, 1.5337792854669305], [4.579730793457142, 0.7807746687626642], [7.765207862902192, 0.3974028644034909], [14.716051089510543, 0.20591489694526696], [26.934456181374337, 0.14530645535833772], [51.10710701790373, 0.17148599156666613], [97.11195013965043, 0.5789527461182298], [0.0, 0.5789527461182298], [0.0, 0.2140524219697758], [0.0, 0.07916734689187103], [0.0, 0.030792621980557648], [0.0, 0.012118725928445456], [0.0, 0.004872909916774048], [0.0, 0.0019705425705026595], [0.0, 0.0013744561046710527]]
Where I am confused is that the callback is doing something, I can see it take the value of u[1]
to zero, but it’s not at the value 60.
I’m sure this is a fundamental misunderstanding on my part of how to handle the ContinuousCallback
, but I can’t find an answer to this in the docs, and would appreciate any help!