Skip to content

Commit c815833

Browse files
committed
init function for QSS
1 parent 018ccd8 commit c815833

7 files changed

Lines changed: 96 additions & 29 deletions

File tree

src/SimJulia.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ module SimJulia
1111

1212
import Base.run, Base.now, Base.isless, Base.show, Base.interrupt, Base.yield
1313
import Base.(&), Base.(|)
14+
import TaylorSeries.integrate
1415

1516
export AbstractEvent
1617
export value, state, environment
@@ -44,6 +45,9 @@ module SimJulia
4445
include("resources/base.jl")
4546
include("resources/containers.jl")
4647
include("resources/stores.jl")
48+
include("odes/base.jl")
4749
include("odes/macros.jl")
50+
include("odes/QSS.jl")
4851
include("continuous.jl")
52+
include("odes/commons.jl")
4953
end

src/continuous.jl

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,21 @@
1+
mutable struct Variable <: AbstractEvent
2+
bev :: BaseEvent
3+
id :: UInt
4+
f :: Function
5+
x :: Taylor1
6+
t :: Float64
7+
function Variable(env::Environment, id::Int, f::Vector{Function}, x₀::Taylor1)
8+
new(BaseEvent(env), UInt(id), f[id], x₀, now(env))
9+
end
10+
end
11+
112
struct Continuous <: AbstractProcess
213
bev :: BaseEvent
3-
f :: Vector{Function}
4-
order :: UInt8
5-
stiff :: Bool
6-
t :: Vector{Float64}
7-
q :: Vector{Taylor1}
8-
x :: Vector{Taylor1}
14+
vars :: Vector{Variable}
915
p :: Vector{Float64}
10-
function Continuous(func::Function, env::Environment, q::Vector{Float64}, p::Vector{Float64}=Float64[]; order::Number=4, stiff::Bool=false)
11-
cont = new(BaseEvent(env), func(), UInt8(order), stiff, Vector{Float64}(), Vector{Taylor1}(), Vector{Taylor1}(), p)
12-
zero_taylor = 0*Taylor1(Float64, order+1)
13-
for q₀ in q
14-
push!(cont.t, now(env))
15-
push!(cont.q, q₀ + zero_taylor)
16-
end
17-
t₀ = now(env) + Taylor1(Float64, order+1)
18-
for (i, q₀) in enumerate(q)
19-
push!(cont.x, integrate(cont.f[i](t₀, cont.q, cont.p), q₀))
20-
end
21-
for i in 1:order-1
22-
for (i, xᵢ) in enumerate(cont.x)
23-
cont.q[i] = copy(cont.x[i])
24-
end
25-
for (i, qᵢ) in enumerate(cont.q)
26-
cont.x[i] = integrate(cont.f[i](t₀, cont.q, cont.p), q[i])
27-
end
28-
end
16+
function Continuous(model::Function, env::Environment, x₀::Vector{Float64}, p::Vector{Float64}=Float64[]; integrator::Integrator=QSS())
17+
cont = new(BaseEvent(env), Vector{Variable}(), p)
18+
init(env, cont, integrator, model, x₀)
2919
cont
3020
end
3121
end

src/odes/QSS.jl

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
abstract type Quantizer end
2+
3+
struct DirectQuantizer <: Quantizer end
4+
5+
struct ImplicitQuantizer <: Quantizer end
6+
7+
struct QSS <: Integrator
8+
order :: UInt8
9+
quantizer :: Quantizer
10+
q :: Vector{Taylor1}
11+
t :: Vector{Float64}
12+
function QSS(;order::Number=4, stiff::Bool=false)
13+
quantizer = stiff ? ImplicitQuantizer() : DirectQuantizer()
14+
new(UInt8(order), quantizer, Vector{Taylor1}(), Vector{Float64}())
15+
end
16+
end

src/odes/base.jl

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

src/odes/commons.jl

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
function init(env::Environment, cont::Continuous, integrator::QSS, model::Function, x₀::Vector{Float64})
2+
n = length(x₀)
3+
t = now(env)
4+
f, deps = model()
5+
zero_taylor = 0*Taylor1(Float64, integrator.order+1)
6+
for (i, q₀) in enumerate(x₀)
7+
push!(integrator.t, t)
8+
push!(integrator.q, q₀ + zero_taylor)
9+
end
10+
t₀ = t + Taylor1(Float64, integrator.order+1)
11+
x_vec = Vector{Taylor1}()
12+
for (i, q₀) in enumerate(x₀)
13+
push!(x_vec, integrate(f[i](t₀, integrator.q, cont.p), q₀))
14+
end
15+
for i in 1:integrator.order-1
16+
for (j, x) in enumerate(x_vec)
17+
integrator.q[j] = copy(x)
18+
end
19+
for (j, q₀) in enumerate(x₀)
20+
x_vec[j] = integrate(f[j](t₀, integrator.q, cont.p), q₀)
21+
end
22+
end
23+
for (i, x) in enumerate(x_vec)
24+
var = Variable(env, i, f, x)
25+
push!(cont.vars, var)
26+
@callback step(var, cont, integrator)
27+
schedule(var)
28+
end
29+
end
30+
31+
function step(var::Variable, cont::Continuous, integrator::QSS)
32+
i = var.id
33+
env = environment(var)
34+
t = now(env)
35+
println(integrator.q)
36+
end

src/odes/macros.jl

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1+
function check_dependencies(ex, deps::Array{Bool}, i::Int)
2+
if ex isa Expr
3+
if ex.head == :ref
4+
deps[i, ex.args[2]] = true
5+
else
6+
for arg in ex.args
7+
check_dependencies(arg, deps, i)
8+
end
9+
end
10+
end
11+
end
12+
113
macro model(expr::Expr)
214
expr.head != :function && error("Expression is not a function definition!")
315
args = getArguments(expr)
@@ -6,9 +18,16 @@ macro model(expr::Expr)
618
for ex in expr.args[2].args
719
ex isa Expr && ex.head == Symbol("=") && push!(f_vec, ex)
820
end
21+
n = length(f_vec)
22+
deps = zeros(Bool, n, n)
23+
for ex in expr.args[2].args
24+
if ex isa Expr && ex.head == Symbol("=")
25+
check_dependencies(ex.args[2], deps, ex.args[1].args[2])
26+
end
27+
end
928
esc(:(function $func_name()
1029
f = Array{Function}(length($f_vec))
1130
$((:(f[$(f_vec[i].args[1].args[2])] = (t::TaylorSeries.Taylor1, q::Vector{TaylorSeries.Taylor1}, p::Vector{Float64})->$(f_vec[i].args[2])) for i in 1:length(:($f_vec)))...)
12-
f
31+
f, $deps
1332
end))
1433
end

test/continuous.jl

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ using SimJulia
66
end
77

88
sim = Simulation()
9-
cont = @continuous stiffeq(sim, [0.0, 20.0], [2020.0]; order=6, stiff=true)
9+
cont = @continuous stiffeq(sim, [0.0, 20.0], [2020.0])
1010
run(sim, 500.0)
11-
println(cont.x)
12-
println(cont.q)
11+
for var in cont.vars
12+
println(var.x)
13+
end

0 commit comments

Comments
 (0)