|
2 | 2 |
|
3 | 3 | struct QSS{T} <: Integrator |
4 | 4 | order :: UInt8 |
5 | | - deps :: Matrix{Bool} |
| 5 | + model :: Model |
6 | 6 | q :: Vector{Taylor1} |
7 | 7 | t :: Vector{Float64} |
8 | | - function QSS{T}(model::Function, cont::Continuous, t::Float64, vec_x₀::Vector{Float64}; order::Number=4) where T |
9 | | - f, deps = model() |
10 | | - integrator = new(UInt8(order), deps, Vector{Taylor1}(), Vector{Float64}()) |
11 | | - zero_taylor = 0*Taylor1(Float64, integrator.order+1) |
12 | | - for x₀ in vec_x₀ |
13 | | - push!(integrator.t, t) |
14 | | - push!(integrator.q, x₀ + zero_taylor) |
| 8 | + Δrel :: Float64 |
| 9 | + Δabs :: Float64 |
| 10 | + function QSS{T}(model::Model, t::Float64, x₀::Vector{Float64}; |
| 11 | + 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 | + t₀ = t + Taylor1(Float64, qss.order+1) |
| 14 | + for q₀ in x₀ |
| 15 | + push!(qss.t, t) |
| 16 | + push!(qss.q, q₀ + Taylor1(zeros(Float64, qss.order+1))) |
15 | 17 | end |
16 | | - t₀ = t + Taylor1(Float64, integrator.order+1) |
17 | | - vec_x = Vector{Taylor1}() |
18 | | - for (i, x₀) in enumerate(vec_x₀) |
19 | | - push!(vec_x, integrate(f[i](t₀, integrator.q, cont.p), x₀)) |
20 | | - end |
21 | | - for i in 1:integrator.order-1 |
22 | | - for (j, x) in enumerate(vec_x) |
23 | | - integrator.q[j] = copy(x) |
24 | | - end |
25 | | - for (j, x₀) in enumerate(vec_x₀) |
26 | | - vec_x[j] = integrate(f[j](t₀, integrator.q, cont.p), x₀) |
| 18 | + for i in 1:qss.order-1 |
| 19 | + for (j, q₀) in enumerate(x₀) |
| 20 | + qss.q[j] = integrate(model.f[j](t₀, qss.q, model.p), q₀) |
27 | 21 | end |
28 | 22 | end |
29 | | - for (i, x) in enumerate(vec_x) |
30 | | - var = cont.vars[i] |
31 | | - var.f = f[i] |
32 | | - var.x = x |
33 | | - var.t = t |
34 | | - @callback step(var, cont, integrator) |
35 | | - schedule(var) |
36 | | - end |
37 | | - integrator |
| 23 | + qss |
38 | 24 | end |
39 | 25 | end |
40 | 26 |
|
41 | | -function Continuous(model::Function, env::Environment, x₀::Vector{Float64}, p::Vector{Float64}=Float64[]; order::Number=4) |
42 | | - Continuous(model, QSS{non_stiff}, env, x₀, p; order=order) |
| 27 | +function Continuous(model::Model, env::Environment, x₀::Vector{Float64}, p::Vector{Float64}=Float64[]; |
| 28 | + stiff::Bool=false, order::Number=4, Δrel::Float64=1e-6, Δabs::Float64=1e-6) |
| 29 | + stiff ? Continuous(model, env, QSS{SimJulia.stiff}, x₀, p; order=order, Δrel=Δrel, Δabs=Δabs) : |
| 30 | + Continuous(model, env, QSS{SimJulia.non_stiff}, x₀, p; order=order, Δrel=Δrel, Δabs=Δabs) |
| 31 | +end |
| 32 | + |
| 33 | +function initial_values(qss::QSS, t::Float64) |
| 34 | + t₀ = t + Taylor1(Float64, qss.order+1) |
| 35 | + x₀ = Vector{Taylor1}() |
| 36 | + 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 | + end |
| 39 | + x₀ |
43 | 40 | end |
44 | 41 |
|
45 | | -function step(var::Variable, cont::Continuous, integrator::QSS) |
| 42 | +function step(var::Variable, cont::Continuous, qss::QSS) |
| 43 | + t = now(environment(var)) |
| 44 | + n = length(qss.model.f) |
46 | 45 | i = var.id |
47 | | - env = environment(var) |
48 | | - t = now(env) |
| 46 | + t₀ = t + Taylor1(Float64, qss.order+1) |
49 | 47 | x₀ = advance_time(var, t) |
50 | | - update_quantized_state(cont, integrator, i, t) |
| 48 | + println(t, " ", i, " ", x₀) |
| 49 | + update_quantized_state(qss, cont.vars, i, t) |
| 50 | + Δt = compute_next_time(var.x, max(qss.Δrel*x₀, qss.Δabs)) |
| 51 | + schedule(var, cont, qss, Δt) |
| 52 | + for j in filter(j->qss.model.deps[j,i], 1:n) |
| 53 | + dep = cont.vars[j] |
| 54 | + x₀ = evaluate(dep.x, t - dep.t) |
| 55 | + dep.t = t |
| 56 | + for k in filter(k->qss.model.deps[j,k], 1:n) |
| 57 | + advance_time(qss, k, t) |
| 58 | + end |
| 59 | + dep.x = integrate(qss.model.f[j](t₀, qss.q, qss.model.p), x₀) |
| 60 | + Δt = recompute_next_time(qss, var.x, qss.q[j], max(qss.Δrel*x₀, qss.Δabs)) |
| 61 | + schedule(dep, cont, qss, Δt) |
| 62 | + end |
| 63 | +end |
| 64 | + |
| 65 | +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)) |
| 67 | + qss.t[i] = t |
| 68 | + qss.q[i].coeffs[1] |
| 69 | +end |
| 70 | + |
| 71 | +function update_quantized_state(qss::QSS{non_stiff}, vars::Vector{Variable}, i::UInt, t::Float64) |
| 72 | + qss.q[i] = copy(vars[i].x) |
| 73 | + qss.q[i].coeffs[end] = 0.0 |
| 74 | + qss.t[i] = t |
| 75 | +end |
| 76 | + |
| 77 | +function update_quantized_state(qss::QSS{stiff}, vars::Vector{Variable}, i::UInt, t::Float64) |
| 78 | + for (j, istrue) in enumerate(qss.model.deps[i, :]) |
| 79 | + istrue && advance_time(qss, j, t) |
| 80 | + end |
| 81 | + q₋ = deepcopy(qss.q) |
51 | 82 | end |
52 | 83 |
|
53 | | -function advance_time(integrator::QSS, i::Int, t::Float64) |
54 | | - q = integrator.q[i] |
55 | | - Δt = t - integrator.t[i] |
56 | | - integrator.q[i] = evaluate(q, Δt + Taylor1(q.order)) |
57 | | - integrator.t[i] = t |
58 | | - integrator.q[i].coeffs[1] |
| 84 | +function compute_next_time(x::Taylor1, Δq::Float64) |
| 85 | + (abs(Δq/x.coeffs[end]))^(1.0/x.order) |
59 | 86 | end |
60 | 87 |
|
61 | | -function update_quantized_state(cont::Continuous, integrator::QSS{non_stiff}, i::UInt, t::Float64) |
62 | | - integrator.q[i] = Taylor1(cont.vars[i].x.coeffs[1:integrator.order]) |
63 | | - integrator.t[i] = t |
| 88 | +function recompute_next_time(::QSS{non_stiff}, x::Taylor1, q::Taylor1, Δq::Float64) |
| 89 | + p = x.coeffs - q.coeffs |
| 90 | + p[1] -= Δq |
| 91 | + neg = roots(p) |
| 92 | + p[1] += 2Δq |
| 93 | + pos = roots(p) |
| 94 | + select_root(neg, pos) |
64 | 95 | end |
65 | 96 |
|
66 | | -function update_quantized_state(cont::Continuous, integrator::QSS{stiff}, i::UInt, t::Float64) |
67 | | - for (j, istrue) in enumerate(integrator.deps[i, :]) |
68 | | - istrue && advance_time(integrator, j, t) |
| 97 | +function select_root(neg::Vector{Complex{Float64}}, pos::Vector{Complex{Float64}}) |
| 98 | + selected = Inf |
| 99 | + for v in filter(v->abs(imag(v)) < 1e-15 && real(v) >= 0, [neg..., pos...]) |
| 100 | + selected = real(v) < selected ? real(v) : selected |
69 | 101 | end |
70 | | - q₋ = deepcopy(integrator.q) |
| 102 | + selected |
71 | 103 | end |
0 commit comments