|
1 | | -abstract type Quantizer end |
| 1 | +@enum ODE_TYPE non_stiff=0 stiff=1 |
2 | 2 |
|
3 | | -struct DirectQuantizer <: Quantizer end |
4 | | - |
5 | | -struct ImplicitQuantizer <: Quantizer end |
6 | | - |
7 | | -struct QSS <: Integrator |
| 3 | +struct QSS{T} <: Integrator |
8 | 4 | order :: UInt8 |
9 | 5 | deps :: Matrix{Bool} |
10 | | - quantizer :: Quantizer |
11 | 6 | q :: Vector{Taylor1} |
12 | 7 | t :: Vector{Float64} |
13 | | - function QSS(;order::Number=4, stiff::Bool=false) |
14 | | - quantizer = stiff ? ImplicitQuantizer() : DirectQuantizer() |
15 | | - new(UInt8(order), Matrix{Bool}(0, 0), quantizer, Vector{Taylor1}(), 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) |
| 15 | + 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₀) |
| 27 | + end |
| 28 | + 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 |
| 38 | + end |
| 39 | +end |
| 40 | + |
| 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) |
| 43 | +end |
| 44 | + |
| 45 | +function step(var::Variable, cont::Continuous, integrator::QSS) |
| 46 | + i = var.id |
| 47 | + env = environment(var) |
| 48 | + t = now(env) |
| 49 | + x₀ = advance_time(var, t) |
| 50 | + update_quantized_state(cont, integrator, i, t) |
| 51 | +end |
| 52 | + |
| 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] |
| 59 | +end |
| 60 | + |
| 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 |
| 64 | +end |
| 65 | + |
| 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) |
16 | 69 | end |
| 70 | + q₋ = deepcopy(integrator.q) |
17 | 71 | end |
0 commit comments