Skip to content

Commit b0ba70c

Browse files
committed
Some more commits
1 parent 3932a53 commit b0ba70c

4 files changed

Lines changed: 57 additions & 38 deletions

File tree

src/continuous.jl

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,42 +14,41 @@ function advance_time(var::Variable, t::Float64)
1414
var.x.coeffs[1]
1515
end
1616

17-
function evaluate(var::Variable, t::Float64)
17+
function evaluate(var::Variable, t::Float64=now(environment(var)))
1818
evaluate(var.x, t - var.t)
1919
end
2020

2121
struct ZeroCrossing <: AbstractEvent
2222

2323
end
2424

25-
macro zerocrossing(expr::Expr)
25+
macro trigger(expr::Expr)
2626
expr.head != :call && error("Expression is not a function call!")
2727
nothing
2828
end
2929

3030
struct Continuous <: AbstractProcess
3131
bev :: BaseEvent
32-
integrator :: Integrator
3332
vars :: Vector{Variable}
34-
function Continuous(env::Environment, integrator::Integrator)
35-
cont = new(BaseEvent(env), integrator, Vector{Variable}())
36-
t = now(env)
37-
x = initial_values(integrator, t)
38-
for (i, x₀) in enumerate(x)
39-
push!(cont.vars, Variable(env, i, x₀, t))
40-
end
41-
for var in cont.vars
42-
@callback step(var, cont, integrator)
43-
schedule(var)
44-
end
45-
cont
33+
function Continuous(env::Environment)
34+
new(BaseEvent(env), Vector{Variable}())
4635
end
4736
end
4837

4938
function Continuous{I<:Integrator}(model::Model, env::Environment, ::Type{I},
5039
x₀::Vector{Float64}, p::Vector{Float64}=Float64[]; args...)
40+
cont = Continuous(env)
41+
t = now(env)
5142
integrator = I(model, now(env), x₀, p; args...)
52-
Continuous(env, integrator)
43+
x = initial_values(integrator, t)
44+
for (i, x₀) in enumerate(x)
45+
push!(cont.vars, Variable(env, i, x₀, t))
46+
end
47+
for var in cont.vars
48+
@callback step(var, cont, integrator)
49+
schedule(var)
50+
end
51+
cont
5352
end
5453

5554
macro continuous(expr::Expr)

src/odes/QSS.jl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ struct QSS{T} <: Integrator
1111
function QSS{T}(model::Model, t::Float64, x₀::Vector{Float64}, p::Vector{Float64};
1212
order::Number=4, Δrel::Float64=1e-6, Δabs::Float64=1e-6) where T
1313
qss = new(UInt8(order), model, p, Vector{Taylor1}(), Vector{Float64}(), Δrel, Δabs)
14-
t₀ = t + Taylor1(Float64, qss.order+1)
14+
t₀ = t + Taylor1(Float64, order)
1515
for q₀ in x₀
1616
push!(qss.t, t)
17-
push!(qss.q, q₀ + Taylor1(zeros(Float64, qss.order+1)))
17+
push!(qss.q, q₀ + Taylor1(zeros(Float64, order+1)))
1818
end
1919
for i in 1:qss.order-1
2020
for (j, q₀) in enumerate(x₀)
@@ -32,7 +32,7 @@ function Continuous(model::Model, env::Environment, x₀::Vector{Float64}, p::Ve
3232
end
3333

3434
function initial_values(qss::QSS, t::Float64)
35-
t₀ = t + Taylor1(Float64, qss.order+1)
35+
t₀ = t + Taylor1(Float64, qss.order+0)
3636
x₀ = Vector{Taylor1}()
3737
for (i, f) in enumerate(qss.model.f)
3838
push!(x₀, integrate(f(t₀, qss.q, qss.p), qss.q[i].coeffs[1]))
@@ -44,7 +44,7 @@ function step(var::Variable, cont::Continuous, qss::QSS)
4444
t = now(environment(var))
4545
n = length(qss.model.f)
4646
i = var.id
47-
t₀ = t + Taylor1(Float64, qss.order+1)
47+
t₀ = t + Taylor1(Float64, qss.order+0)
4848
x₀ = advance_time(var, t)
4949
update_quantized_state(qss, cont.vars, i, t)
5050
Δt = compute_next_time(var.x, max(qss.Δrel*x₀, qss.Δabs))
@@ -63,7 +63,7 @@ function step(var::Variable, cont::Continuous, qss::QSS)
6363
end
6464

6565
function advance_time(qss::QSS, i::Int, t::Float64)
66-
qss.q[i] = evaluate(qss.q[i], t - qss.t[i] + Taylor1(qss.q[i].order))
66+
qss.q[i] = evaluate(qss.q[i], t - qss.t[i] + Taylor1(qss.order+0))
6767
qss.t[i] = t
6868
qss.q[i].coeffs[1]
6969
end
@@ -86,7 +86,7 @@ function compute_next_time(x::Taylor1, Δq::Float64)
8686
end
8787

8888
function recompute_next_time(::QSS{non_stiff}, x::Taylor1, q::Taylor1, Δq::Float64)
89-
p = x.coeffs - q.coeffs
89+
p = (x-q).coeffs
9090
p[1] -= Δq
9191
neg = roots(p)
9292
p[1] += 2Δq

src/odes/macros.jl

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
function check_dependencies(ex, deps::Array{Bool}, i::Int, x::Symbol)
22
if ex isa Expr
3-
if ex.head == :ref && x == ex.args[1]
3+
if ex.head == :ref && ex.args[1] == x
44
deps[i, ex.args[2]] = true
55
else
66
for arg in ex.args
@@ -15,13 +15,14 @@ macro model(expr::Expr)
1515
args = getArguments(expr)
1616
func_name = shift!(args)
1717
f_vec = Vector{Expr}()
18+
dx = expr.args[1].args[5]
1819
for ex in expr.args[2].args
19-
ex isa Expr && ex.head == Symbol("=") && push!(f_vec, ex)
20+
ex isa Expr && ex.head == Symbol("=") && ex.args[1] isa Expr && ex.args[1].head == :ref && ex.args[1].args[1] == dx && push!(f_vec, ex)
2021
end
2122
n = length(f_vec)
2223
deps = zeros(Bool, n, n)
2324
for ex in expr.args[2].args
24-
ex isa Expr && ex.head == Symbol("=") && check_dependencies(ex.args[2], deps, ex.args[1].args[2], expr.args[1].args[3])
25+
ex isa Expr && ex.head == Symbol("=") && ex.args[1] isa Expr && ex.args[1].head == :ref && ex.args[1].args[1] == dx && check_dependencies(ex.args[2], deps, ex.args[1].args[2], expr.args[1].args[3])
2526
end
2627
esc(:(function $func_name()
2728
f = Array{Function}($n)
@@ -30,7 +31,7 @@ macro model(expr::Expr)
3031
end))
3132
end
3233

33-
macro trigger(expr::Expr)
34+
macro zerocrossing(expr::Expr)
3435
expr.head != :function && error("Expression is not a function definition!")
3536
nothing
3637
end

test/continuous.jl

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,46 @@
11
using SimJulia
22

3-
@model function diffeq(t, x, p, dq)
3+
@model function diffeq(t, x, p, dx)
44
dx[1] = 0.01*x[2]
55
dx[2] = p[1]-100.0*x[1]-100.0*x[2]
66
end
77

8-
@trigger function less_prey(t, x, p, res)
9-
res = x[1] < x[2]
10-
if res
11-
p[1] = 0.0
12-
end
13-
end
14-
158
function report(sim::Simulation, cont::Continuous)
169
while true
17-
t = now(sim)
18-
println(t, " ", evaluate(cont.vars[1], t), " ", evaluate(cont.vars[2], t))
10+
println(now(sim), " ", evaluate(cont.vars[1]), " ", evaluate(cont.vars[2]))
1911
yield(Timeout(sim, 1.0))
2012
end
2113
end
2214

2315
sim = Simulation()
24-
cont = @continuous diffeq(sim, [0.0, 20.0], [2020.0]; stiff=false, order=5)
16+
cont = @continuous diffeq(sim, [0.0, 20.0], [2020.0]; stiff=false, order=4)
2517
@process report(sim, cont)
26-
zc = @zerocrossing less_prey(cont)
2718
run(sim, 71)
19+
20+
@model function bouncing_ball(t, x, p, dx)
21+
dx[1] = x[3]
22+
dx[3] = zero(t)
23+
dx[2] = x[4]
24+
dx[4] = -9.81 + p[1]*(2.0e4/0.1*(x[2]-(3.0-ceil(evaluate(x[1])/0.2)*0.2)))
25+
end
26+
27+
@zerocrossing function contact(t, x, p, res)
28+
yf = 3.0 - ceil(x[1]/0.2)*0.2
29+
res = x[4] < 0.0 && x[2] - yf < 0.05
30+
if res
31+
p[1] = 1.0
32+
end
33+
end
34+
35+
@zerocrossing function up(t, x, p, res)
36+
yf = 3.0 - ceil(x[1]/0.2)*0.2
37+
res = x[4] > 0.0 && x[2] - yf > 0.05
38+
if res
39+
p[1] = 0.0
40+
end
41+
end
42+
43+
sim = Simulation()
44+
cont = @continuous bouncing_ball(sim, [0.0, 3.05, 1.0, 0.0], [0.0])
45+
@process report(sim, cont)
46+
run(sim, 5)

0 commit comments

Comments
 (0)