Skip to content

Commit a0722c7

Browse files
refactor: allow new clocks to be defined in downstream packages
1 parent 5f4ac56 commit a0722c7

File tree

1 file changed

+45
-4
lines changed

1 file changed

+45
-4
lines changed

src/clock.jl

+45-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
@data Clocks begin
1+
abstract type AbstractClock end
2+
3+
@data Clocks<:AbstractClock begin
24
ContinuousClock
35
struct PeriodicClock
46
dt::Union{Nothing, Float64, Rational{Int}}
@@ -63,6 +65,7 @@ issolverstepclock(::Any) = false
6365
iscontinuous(::Any) = false
6466
is_discrete_time_domain(::Any) = false
6567

68+
# public
6669
function first_clock_tick_time(c, t0)
6770
@match c begin
6871
PeriodicClock(dt) => ceil(t0 / dt) * dt
@@ -71,13 +74,51 @@ function first_clock_tick_time(c, t0)
7174
end
7275
end
7376

74-
struct IndexedClock{I}
75-
clock::TimeDomain
77+
# public
78+
"""
79+
$(TYPEDEF)
80+
81+
A struct representing the operation of indexing a clock to obtain a subset of the time
82+
points at which it ticked. The actual list of time points depends on the time interval
83+
for which the clock was running, and can be obtained via `canonicalize_indexed_clock`
84+
by providing a timeseries solution object.
85+
86+
For example, `IndexedClock(PeriodicClock(0.1), 3)` refers to the third time that
87+
`PeriodicClock(0.1)` ticked. If the simulation started at `t = 0`, then this would be
88+
`t = 0.2`. Similarly, `IndexedClock(PeriodicClock(0.1), [1, 5])` refers to `t = 0.0`
89+
and `t = 0.4` in this context.
90+
91+
# Fields
92+
93+
$(TYPEDFIELDS)
94+
"""
95+
struct IndexedClock{C <: AbstractClock, I}
96+
"""
97+
The clock being indexed. A subtype of `SciMLBase.AbstractClock`
98+
"""
99+
clock::C
100+
"""
101+
The subset of indexes being referred to. This can be an integer, an array of integers,
102+
a range or `Colon()` to refer to all the points that the clock ticked.
103+
"""
76104
idx::I
77105
end
78106

79-
Base.getindex(c::TimeDomain, idx) = IndexedClock(c, idx)
107+
# public
108+
"""
109+
$(TYPEDSIGNATURES)
110+
111+
Return a `SciMLBase.IndexedClock` representing the subset of the time points that the clock
112+
ticked indicated by `idx`.
113+
"""
114+
Base.getindex(c::AbstractClock, idx) = IndexedClock(c, idx)
80115

116+
# public
117+
"""
118+
$(TYPEDSIGNATURES)
119+
120+
Return the time points in the interval
121+
"""
81122
function canonicalize_indexed_clock(ic::IndexedClock, sol::AbstractTimeseriesSolution)
82123
c = ic.clock
83124

0 commit comments

Comments
 (0)