Skip to content

Commit 51ba1fb

Browse files
committed
Persistent callbacks
1 parent 494df16 commit 51ba1fb

6 files changed

Lines changed: 18 additions & 20 deletions

File tree

src/base.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,11 @@ end
1414
mutable struct BaseEvent
1515
env :: Environment
1616
id :: UInt
17-
cid :: UInt
18-
callbacks :: DataStructures.PriorityQueue{Function, UInt}
17+
callbacks :: Vector{Function}
1918
state :: EVENT_STATE
2019
value :: Any
2120
function BaseEvent(env::Environment)
22-
new(env, env.eid+=one(UInt), zero(UInt), DataStructures.PriorityQueue(Function, UInt), idle, nothing)
21+
new(env, env.eid+=one(UInt), Vector{Function}(), idle, nothing)
2322
end
2423
end
2524

@@ -42,7 +41,7 @@ end
4241
function append_callback(func::Function, ev::AbstractEvent, args::Any...) :: Function
4342
ev.bev.state == triggered && throw(EventTriggered(ev))
4443
cb = ()->func(ev, args...)
45-
ev.bev.callbacks[cb] = ev.bev.cid+=one(UInt)
44+
push!(ev.bev.callbacks, cb)
4645
cb
4746
end
4847

@@ -54,7 +53,8 @@ macro callback(expr::Expr)
5453
end
5554

5655
function remove_callback(cb::Function, ev::AbstractEvent)
57-
DataStructures.dequeue!(ev.bev.callbacks, cb)
56+
i = indexin(ev.bev.callbacks, [cb])[1]
57+
deleteat!(ev.bev.callbacks, i)
5858
end
5959

6060
function schedule(ev::AbstractEvent, delay::Number=zero(Float64); priority::Int8=zero(Int8), value::Any=nothing)

src/continuous.jl

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,9 @@ struct Continuous <: AbstractProcess
4141
end
4242
end
4343

44-
function Continuous{I<:Integrator}(model::Model, env::Environment, ::Type{I}, x₀::Vector{Float64}, p::Vector{Float64}=Float64[]; args...)
45-
for p₀ in p
46-
push!(model.p, p₀)
47-
end
48-
integrator = I(model, now(env), x₀; args...)
44+
function Continuous{I<:Integrator}(model::Model, env::Environment, ::Type{I},
45+
x₀::Vector{Float64}, p::Vector{Float64}=Float64[]; args...)
46+
integrator = I(model, now(env), x₀, p; args...)
4947
Continuous(env, integrator)
5048
end
5149

src/odes/QSS.jl

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,22 @@
33
struct QSS{T} <: Integrator
44
order :: UInt8
55
model :: Model
6+
p :: Vector{Float64}
67
q :: Vector{Taylor1}
78
t :: Vector{Float64}
89
Δrel :: Float64
910
Δabs :: Float64
10-
function QSS{T}(model::Model, t::Float64, x₀::Vector{Float64};
11+
function QSS{T}(model::Model, t::Float64, x₀::Vector{Float64}, p::Vector{Float64};
1112
order::Number=4, Δrel::Float64=1e-6, Δabs::Float64=1e-6) where T
12-
qss = new(UInt8(order), model, Vector{Taylor1}(), Vector{Float64}(), Δrel, Δabs)
13+
qss = new(UInt8(order), model, p, Vector{Taylor1}(), Vector{Float64}(), Δrel, Δabs)
1314
t₀ = t + Taylor1(Float64, qss.order+1)
1415
for q₀ in x₀
1516
push!(qss.t, t)
1617
push!(qss.q, q₀ + Taylor1(zeros(Float64, qss.order+1)))
1718
end
1819
for i in 1:qss.order-1
1920
for (j, q₀) in enumerate(x₀)
20-
qss.q[j] = integrate(model.f[j](t₀, qss.q, model.p), q₀)
21+
qss.q[j] = integrate(model.f[j](t₀, qss.q, p), q₀)
2122
end
2223
end
2324
qss
@@ -34,7 +35,7 @@ function initial_values(qss::QSS, t::Float64)
3435
t₀ = t + Taylor1(Float64, qss.order+1)
3536
x₀ = Vector{Taylor1}()
3637
for (i, f) in enumerate(qss.model.f)
37-
push!(x₀, integrate(f(t₀, qss.q, qss.model.p), qss.q[i].coeffs[1]))
38+
push!(x₀, integrate(f(t₀, qss.q, qss.p), qss.q[i].coeffs[1]))
3839
end
3940
x₀
4041
end
@@ -56,7 +57,7 @@ function step(var::Variable, cont::Continuous, qss::QSS)
5657
for k in filter(k->qss.model.deps[j,k], 1:n)
5758
advance_time(qss, k, t)
5859
end
59-
dep.x = integrate(qss.model.f[j](t₀, qss.q, qss.model.p), x₀)
60+
dep.x = integrate(qss.model.f[j](t₀, qss.q, qss.p), x₀)
6061
Δt = recompute_next_time(qss, var.x, qss.q[j], max(qss.Δrel*x₀, qss.Δabs))
6162
schedule(dep, cont, qss, Δt)
6263
end

src/odes/base.jl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@ abstract type Integrator end
22

33
struct Model
44
f :: Vector{Function}
5-
p :: Vector{Float64}
65
deps :: Matrix{Bool}
76
function Model(f::Vector{Function}, deps::Matrix{Bool})
8-
new(f, Vector{Float64}(), deps)
7+
new(f, deps)
98
end
109
end

src/simulations.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ function step(sim::Simulation)
3434
DataStructures.dequeue!(sim.heap)
3535
sim.time = key.time
3636
bev.state = triggered
37-
while !isempty(bev.callbacks)
38-
DataStructures.dequeue!(bev.callbacks)()
37+
for callback in bev.callbacks
38+
callback()
3939
end
4040
end
4141

test/continuous.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ end
1515
sim = Simulation()
1616
cont = @continuous diffeq(sim, [0.0, 20.0], [2020.0]; stiff=false, order=4)
1717
zc = @zerocrossing less_prey(cont)
18-
run(sim, 500.0)
18+
run(sim, 10.0)
1919
for var in cont.vars
2020
println(var.x)
2121
end

0 commit comments

Comments
 (0)