-
-
Notifications
You must be signed in to change notification settings - Fork 213
/
Copy pathdiscretedomain.jl
280 lines (221 loc) · 7.5 KB
/
discretedomain.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
using Symbolics: Operator, Num, Term, value, recursive_hasoperator
struct SampleTime <: Operator
SampleTime() = SymbolicUtils.term(SampleTime, type = Real)
end
SymbolicUtils.promote_symtype(::Type{<:SampleTime}, t...) = Real
Base.nameof(::SampleTime) = :SampleTime
SymbolicUtils.isbinop(::SampleTime) = false
# Shift
"""
$(TYPEDEF)
Represents a shift operator.
# Fields
$(FIELDS)
# Examples
```jldoctest
julia> using Symbolics
julia> Δ = Shift(t)
(::Shift) (generic function with 2 methods)
```
"""
struct Shift <: Operator
"""Fixed Shift"""
t::Union{Nothing, Symbolic}
steps::Int
Shift(t, steps = 1) = new(value(t), steps)
end
Shift(steps::Int) = new(nothing, steps)
normalize_to_differential(s::Shift) = Differential(s.t)^s.steps
Base.nameof(::Shift) = :Shift
SymbolicUtils.isbinop(::Shift) = false
function (D::Shift)(x, allow_zero = false)
!allow_zero && D.steps == 0 && return x
Term{symtype(x)}(D, Any[x])
end
function (D::Shift)(x::Num, allow_zero = false)
!allow_zero && D.steps == 0 && return x
vt = value(x)
if iscall(vt)
op = operation(vt)
if op isa Sample
error("Cannot shift a `Sample`. Create a variable to represent the sampled value and shift that instead")
elseif op isa Shift
if D.t === nothing || isequal(D.t, op.t)
arg = arguments(vt)[1]
newsteps = D.steps + op.steps
return Num(newsteps == 0 ? arg : Shift(D.t, newsteps)(arg))
end
end
end
Num(D(vt, allow_zero))
end
SymbolicUtils.promote_symtype(::Shift, t) = t
Base.show(io::IO, D::Shift) = print(io, "Shift(", D.t, ", ", D.steps, ")")
Base.:(==)(D1::Shift, D2::Shift) = isequal(D1.t, D2.t) && isequal(D1.steps, D2.steps)
Base.hash(D::Shift, u::UInt) = hash(D.steps, hash(D.t, xor(u, 0x055640d6d952f101)))
Base.:^(D::Shift, n::Integer) = Shift(D.t, D.steps * n)
Base.literal_pow(f::typeof(^), D::Shift, ::Val{n}) where {n} = Shift(D.t, D.steps * n)
hasshift(eq::Equation) = hasshift(eq.lhs) || hasshift(eq.rhs)
"""
hasshift(O)
Returns true if the expression or equation `O` contains [`Shift`](@ref) terms.
"""
hasshift(O) = recursive_hasoperator(Shift, O)
# Sample
"""
$(TYPEDEF)
Represents a sample operator. A discrete-time signal is created by sampling a continuous-time signal.
# Constructors
`Sample(clock::Union{TimeDomain, InferredTimeDomain} = InferredDiscrete)`
`Sample(dt::Real)`
`Sample(x::Num)`, with a single argument, is shorthand for `Sample()(x)`.
# Fields
$(FIELDS)
# Examples
```jldoctest
julia> using Symbolics
julia> t = ModelingToolkit.t_nounits
julia> Δ = Sample(0.01)
(::Sample) (generic function with 2 methods)
```
"""
struct Sample <: Operator
clock::Any
Sample(clock::Union{TimeDomain, InferredTimeDomain} = InferredDiscrete) = new(clock)
end
function Sample(arg::Real)
arg = unwrap(arg)
if symbolic_type(arg) == NotSymbolic()
Sample(Clock(arg))
else
Sample()(arg)
end
end
(D::Sample)(x) = Term{symtype(x)}(D, Any[x])
(D::Sample)(x::Num) = Num(D(value(x)))
SymbolicUtils.promote_symtype(::Sample, x) = x
Base.nameof(::Sample) = :Sample
SymbolicUtils.isbinop(::Sample) = false
Base.show(io::IO, D::Sample) = print(io, "Sample(", D.clock, ")")
Base.:(==)(D1::Sample, D2::Sample) = isequal(D1.clock, D2.clock)
Base.hash(D::Sample, u::UInt) = hash(D.clock, xor(u, 0x055640d6d952f101))
"""
hassample(O)
Returns true if the expression or equation `O` contains [`Sample`](@ref) terms.
"""
hassample(O) = recursive_hasoperator(Sample, unwrap(O))
# Hold
"""
$(TYPEDEF)
Represents a hold operator. A continuous-time signal is produced by holding a discrete-time signal `x` with zero-order hold.
```
cont_x = Hold()(disc_x)
```
"""
struct Hold <: Operator
end
(D::Hold)(x) = Term{symtype(x)}(D, Any[x])
(D::Hold)(x::Num) = Num(D(value(x)))
SymbolicUtils.promote_symtype(::Hold, x) = x
Base.nameof(::Hold) = :Hold
SymbolicUtils.isbinop(::Hold) = false
Hold(x) = Hold()(x)
"""
hashold(O)
Returns true if the expression or equation `O` contains [`Hold`](@ref) terms.
"""
hashold(O) = recursive_hasoperator(Hold, unwrap(O))
# ShiftIndex
"""
ShiftIndex
The `ShiftIndex` operator allows you to index a signal and obtain a shifted discrete-time signal. If the signal is continuous-time, the signal is sampled before shifting.
# Examples
```
julia> t = ModelingToolkit.t_nounits;
julia> @variables x(t);
julia> k = ShiftIndex(t, 0.1);
julia> x(k) # no shift
x(t)
julia> x(k+1) # shift
Shift(1)(x(t))
```
"""
struct ShiftIndex
clock::Union{InferredTimeDomain, TimeDomain, IntegerSequence}
steps::Int
function ShiftIndex(
clock::Union{TimeDomain, InferredTimeDomain, IntegerSequence} = Inferred, steps::Int = 0)
new(clock, steps)
end
ShiftIndex(dt::Real, steps::Int = 0) = new(Clock(dt), steps)
ShiftIndex(::Num, steps::Int) = new(IntegerSequence(), steps)
end
function (xn::Num)(k::ShiftIndex)
@unpack clock, steps = k
x = value(xn)
# Verify that the independent variables of k and x match and that the expression doesn't have multiple variables
vars = Symbolics.get_variables(x)
length(vars) == 1 ||
error("Cannot shift a multivariate expression $x. Either create a new unknown and shift this, or shift the individual variables in the expression.")
args = Symbolics.arguments(vars[]) # args should be one element vector with the t in x(t)
length(args) == 1 ||
error("Cannot shift an expression with multiple independent variables $x.")
# d, _ = propagate_time_domain(xn)
# if d != clock # this is only required if the variable has another clock
# xn = Sample(t, clock)(xn)
# end
# QUESTION: should we return a variable with time domain set to k.clock?
xn = setmetadata(xn, VariableTimeDomain, k.clock)
if steps == 0
return xn # x(k) needs no shift operator if the step of k is 0
end
Shift(t, steps)(xn) # a shift of k steps
end
Base.:+(k::ShiftIndex, i::Int) = ShiftIndex(k.clock, k.steps + i)
Base.:-(k::ShiftIndex, i::Int) = k + (-i)
"""
input_timedomain(op::Operator)
Return the time-domain type (`Continuous` or `InferredDiscrete`) that `op` operates on.
"""
function input_timedomain(s::Shift, arg = nothing)
if has_time_domain(arg)
return get_time_domain(arg)
end
InferredDiscrete
end
"""
output_timedomain(op::Operator)
Return the time-domain type (`Continuous` or `InferredDiscrete`) that `op` results in.
"""
function output_timedomain(s::Shift, arg = nothing)
if has_time_domain(t, arg)
return get_time_domain(t, arg)
end
InferredDiscrete
end
input_timedomain(::Sample, _ = nothing) = Continuous()
output_timedomain(s::Sample, _ = nothing) = s.clock
function input_timedomain(h::Hold, arg = nothing)
if has_time_domain(arg)
return get_time_domain(arg)
end
InferredDiscrete # the Hold accepts any discrete
end
output_timedomain(::Hold, _ = nothing) = Continuous()
sampletime(op::Sample, _ = nothing) = sampletime(op.clock)
sampletime(op::ShiftIndex, _ = nothing) = sampletime(op.clock)
changes_domain(op) = isoperator(op, Union{Sample, Hold})
function output_timedomain(x)
if isoperator(x, Operator)
return output_timedomain(operation(x), arguments(x)[])
else
throw(ArgumentError("$x of type $(typeof(x)) is not an operator expression"))
end
end
function input_timedomain(x)
if isoperator(x, Operator)
return input_timedomain(operation(x), arguments(x)[])
else
throw(ArgumentError("$x of type $(typeof(x)) is not an operator expression"))
end
end