|
| 1 | +module Clocks |
| 2 | + |
| 3 | +export TimeDomain |
| 4 | + |
| 5 | +using Expronicon.ADT: @adt, @match |
| 6 | + |
| 7 | +@adt TimeDomain begin |
| 8 | + Continuous |
| 9 | + struct PeriodicClock |
| 10 | + dt::Union{Nothing, Float64, Rational{Int}} |
| 11 | + end |
| 12 | + SolverStepClock |
| 13 | +end |
| 14 | + |
| 15 | +Base.Broadcast.broadcastable(d::TimeDomain) = Ref(d) |
| 16 | + |
| 17 | +end |
| 18 | + |
| 19 | +using .Clocks |
| 20 | + |
| 21 | +""" |
| 22 | + Clock(dt) |
| 23 | + Clock() |
| 24 | +
|
| 25 | +The default periodic clock with tick interval `dt`. If `dt` is left unspecified, it will |
| 26 | +be inferred (if possible). |
| 27 | +""" |
| 28 | +Clock(dt::Union{<:Rational, Float64}) = PeriodicClock(dt) |
| 29 | +Clock(dt) = PeriodicClock(convert(Float64, dt)) |
| 30 | +Clock() = PeriodicClock(nothing) |
| 31 | + |
| 32 | +@doc """ |
| 33 | + SolverStepClock |
| 34 | +
|
| 35 | +A clock that ticks at each solver step (sometimes referred to as "continuous sample time"). |
| 36 | +This clock **does generally not have equidistant tick intervals**, instead, the tick |
| 37 | +interval depends on the adaptive step-size selection of the continuous solver, as well as |
| 38 | +any continuous event handling. If adaptivity of the solver is turned off and there are no |
| 39 | +continuous events, the tick interval will be given by the fixed solver time step `dt`. |
| 40 | +
|
| 41 | +Due to possibly non-equidistant tick intervals, this clock should typically not be used with |
| 42 | +discrete-time systems that assume a fixed sample time, such as PID controllers and digital |
| 43 | +filters. |
| 44 | +""" SolverStepClock |
| 45 | + |
| 46 | +isclock(c) = @match c begin |
| 47 | + PeriodicClock(_) => true |
| 48 | + _ => false |
| 49 | +end |
| 50 | + |
| 51 | +issolverstepclock(c) = @match c begin |
| 52 | + &SolverStepClock => true |
| 53 | + _ => false |
| 54 | +end |
| 55 | + |
| 56 | +iscontinuous(c) = @match c begin |
| 57 | + &Continuous => true |
| 58 | + _ => false |
| 59 | +end |
| 60 | + |
| 61 | +is_discrete_time_domain(c) = !iscontinuous(c) |
| 62 | + |
| 63 | +function first_clock_tick_time(c, t0) |
| 64 | + @match c begin |
| 65 | + PeriodicClock(dt) => ceil(t0 / dt) * dt |
| 66 | + &SolverStepClock => t0 |
| 67 | + &Continuous => error("Continuous is not a discrete clock") |
| 68 | + end |
| 69 | +end |
| 70 | + |
| 71 | +struct IndexedClock{I} |
| 72 | + clock::TimeDomain |
| 73 | + idx::I |
| 74 | +end |
| 75 | + |
| 76 | +Base.getindex(c::TimeDomain, idx) = IndexedClock(c, idx) |
| 77 | + |
| 78 | +function canonicalize_indexed_clock(ic::IndexedClock, sol::AbstractTimeseriesSolution) |
| 79 | + c = ic.clock |
| 80 | + |
| 81 | + return @match c begin |
| 82 | + PeriodicClock(dt) => ceil(sol.prob.tspan[1] / dt) * dt .+ (ic.idx .- 1) .* dt |
| 83 | + &SolverStepClock => begin |
| 84 | + ssc_idx = findfirst(eachindex(sol.discretes)) do i |
| 85 | + !isa(sol.discretes[i].t, AbstractRange) |
| 86 | + end |
| 87 | + sol.discretes[ssc_idx].t[ic.idx] |
| 88 | + end |
| 89 | + &Continuous => sol.t[ic.idx] |
| 90 | + end |
| 91 | +end |
0 commit comments