From a0722c7dc417d80af56e2929313b07605246d209 Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Wed, 16 Apr 2025 22:50:33 +0530 Subject: [PATCH 1/2] refactor: allow new clocks to be defined in downstream packages --- src/clock.jl | 49 +++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 45 insertions(+), 4 deletions(-) diff --git a/src/clock.jl b/src/clock.jl index a417ebea4..a7357644b 100644 --- a/src/clock.jl +++ b/src/clock.jl @@ -1,4 +1,6 @@ -@data Clocks begin +abstract type AbstractClock end + +@data Clocks<:AbstractClock begin ContinuousClock struct PeriodicClock dt::Union{Nothing, Float64, Rational{Int}} @@ -63,6 +65,7 @@ issolverstepclock(::Any) = false iscontinuous(::Any) = false is_discrete_time_domain(::Any) = false +# public function first_clock_tick_time(c, t0) @match c begin PeriodicClock(dt) => ceil(t0 / dt) * dt @@ -71,13 +74,51 @@ function first_clock_tick_time(c, t0) end end -struct IndexedClock{I} - clock::TimeDomain +# public +""" + $(TYPEDEF) + +A struct representing the operation of indexing a clock to obtain a subset of the time +points at which it ticked. The actual list of time points depends on the time interval +for which the clock was running, and can be obtained via `canonicalize_indexed_clock` +by providing a timeseries solution object. + +For example, `IndexedClock(PeriodicClock(0.1), 3)` refers to the third time that +`PeriodicClock(0.1)` ticked. If the simulation started at `t = 0`, then this would be +`t = 0.2`. Similarly, `IndexedClock(PeriodicClock(0.1), [1, 5])` refers to `t = 0.0` +and `t = 0.4` in this context. + +# Fields + +$(TYPEDFIELDS) +""" +struct IndexedClock{C <: AbstractClock, I} + """ + The clock being indexed. A subtype of `SciMLBase.AbstractClock` + """ + clock::C + """ + The subset of indexes being referred to. This can be an integer, an array of integers, + a range or `Colon()` to refer to all the points that the clock ticked. + """ idx::I end -Base.getindex(c::TimeDomain, idx) = IndexedClock(c, idx) +# public +""" + $(TYPEDSIGNATURES) + +Return a `SciMLBase.IndexedClock` representing the subset of the time points that the clock +ticked indicated by `idx`. +""" +Base.getindex(c::AbstractClock, idx) = IndexedClock(c, idx) +# public +""" + $(TYPEDSIGNATURES) + +Return the time points in the interval +""" function canonicalize_indexed_clock(ic::IndexedClock, sol::AbstractTimeseriesSolution) c = ic.clock From af2139b989fdcd7be6b28f08f25306dea01190bb Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Thu, 17 Apr 2025 11:02:59 +0530 Subject: [PATCH 2/2] Update src/clock.jl Co-authored-by: Fredrik Bagge Carlson --- src/clock.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/clock.jl b/src/clock.jl index a7357644b..1f4da6e78 100644 --- a/src/clock.jl +++ b/src/clock.jl @@ -79,8 +79,8 @@ end $(TYPEDEF) A struct representing the operation of indexing a clock to obtain a subset of the time -points at which it ticked. The actual list of time points depends on the time interval -for which the clock was running, and can be obtained via `canonicalize_indexed_clock` +points at which it ticked. The actual list of time points depends on the tick instances +on which the clock was ticking, and can be obtained via `canonicalize_indexed_clock` by providing a timeseries solution object. For example, `IndexedClock(PeriodicClock(0.1), 3)` refers to the third time that