Skip to content

Commit 977ceb6

Browse files
feat: add is_transparent_operator trait
1 parent c38ab96 commit 977ceb6

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

src/discretedomain.jl

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
using Symbolics: Operator, Num, Term, value, recursive_hasoperator
22

3+
"""
4+
$(TYPEDSIGNATURES)
5+
6+
Trait to be implemented for operators which determines whether application of the operator
7+
generates a semantically different variable or not. For example, `Differential` and `Shift`
8+
are not transparent but `Sample` and `Hold` are. Defaults to `false` if not implemented.
9+
"""
10+
is_transparent_operator(x) = is_transparent_operator(typeof(x))
11+
is_transparent_operator(::Type) = false
12+
313
"""
414
function SampleTime()
515
@@ -128,6 +138,8 @@ struct Sample <: Operator
128138
Sample(clock::Union{TimeDomain, InferredTimeDomain} = InferredDiscrete()) = new(clock)
129139
end
130140

141+
is_transparent_operator(::Type{Sample}) = true
142+
131143
function Sample(arg::Real)
132144
arg = unwrap(arg)
133145
if symbolic_type(arg) == NotSymbolic()
@@ -179,6 +191,9 @@ cont_x = Hold()(disc_x)
179191
"""
180192
struct Hold <: Operator
181193
end
194+
195+
is_transparent_operator(::Type{Hold}) = true
196+
182197
(D::Hold)(x) = Term{symtype(x)}(D, Any[x])
183198
(D::Hold)(x::Num) = Num(D(value(x)))
184199
SymbolicUtils.promote_symtype(::Hold, x) = x

src/systems/systemstructure.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ function TearingState(sys; quick_cancel = false, check = true, sort_eqs = true)
363363
isdelay(v, iv) && continue
364364

365365
if !symbolic_contains(v, dvs)
366-
isvalid = iscall(v) && operation(v) isa Union{Shift, Sample, Hold}
366+
isvalid = iscall(v) && (operation(v) isa Shift || is_transparent_operator(operation(v)))
367367
v′ = v
368368
while !isvalid && iscall(v′) && operation(v′) isa Union{Differential, Shift}
369369
v′ = arguments(v′)[1]

0 commit comments

Comments
 (0)