Skip to content

Commit 88b73fe

Browse files
feat: initial implementation of new DiscreteSystem
1 parent 26960c8 commit 88b73fe

File tree

9 files changed

+576
-5
lines changed

9 files changed

+576
-5
lines changed

src/ModelingToolkit.jl

+3
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,8 @@ include("systems/diffeqs/first_order_transform.jl")
147147
include("systems/diffeqs/modelingtoolkitize.jl")
148148
include("systems/diffeqs/basic_transformations.jl")
149149

150+
include("systems/discrete_system/discrete_system.jl")
151+
150152
include("systems/jumps/jumpsystem.jl")
151153

152154
include("systems/optimization/constraints_system.jl")
@@ -209,6 +211,7 @@ export ODESystem,
209211
export DAEFunctionExpr, DAEProblemExpr
210212
export SDESystem, SDEFunction, SDEFunctionExpr, SDEProblemExpr
211213
export SystemStructure
214+
export DiscreteSystem, DiscreteProblem, DiscreteFunction, DiscreteFunctionExpr
212215
export JumpSystem
213216
export ODEProblem, SDEProblem
214217
export NonlinearFunction, NonlinearFunctionExpr

src/clock.jl

+6
Original file line numberDiff line numberDiff line change
@@ -124,3 +124,9 @@ function Base.:(==)(c1::Clock, c2::Clock)
124124
end
125125

126126
is_concrete_time_domain(x) = x isa Union{AbstractClock, Continuous}
127+
128+
struct IntegerSequence <: AbstractClock
129+
t::Union{Nothing, Symbolic}
130+
IntegerSequence(t::Union{Num, Symbolic}) = new(value(t))
131+
end
132+

src/discretedomain.jl

+2-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ struct Shift <: Operator
2828
Shift(t, steps = 1) = new(value(t), steps)
2929
end
3030
Shift(steps::Int) = new(nothing, steps)
31-
normalize_to_differential(s::Shift) = Differential(s.t)^s.steps
31+
normalize_to_differential(s::Shift) = Differential(s.t)^abs(s.steps)
3232
function (D::Shift)(x, allow_zero = false)
3333
!allow_zero && D.steps == 0 && return x
3434
Term{symtype(x)}(D, Any[x])
@@ -168,6 +168,7 @@ struct ShiftIndex
168168
steps::Int
169169
ShiftIndex(clock::TimeDomain = Inferred(), steps::Int = 0) = new(clock, steps)
170170
ShiftIndex(t::Num, dt::Real, steps::Int = 0) = new(Clock(t, dt), steps)
171+
ShiftIndex(t::Num, steps::Int = 0) = new(IntegerSequence(t), steps)
171172
end
172173

173174
function (xn::Num)(k::ShiftIndex)

src/systems/diffeqs/abstractodesystem.jl

+3-3
Original file line numberDiff line numberDiff line change
@@ -812,7 +812,7 @@ function get_u0_p(sys,
812812
u0, p, defs
813813
end
814814

815-
function get_u0(sys, u0map, parammap = nothing; symbolic_u0 = false)
815+
function get_u0(sys, u0map, parammap = nothing; symbolic_u0 = false, toterm = default_toterm)
816816
dvs = unknowns(sys)
817817
ps = parameters(sys)
818818
defs = defaults(sys)
@@ -834,9 +834,9 @@ function get_u0(sys, u0map, parammap = nothing; symbolic_u0 = false)
834834
end
835835

836836
if symbolic_u0
837-
u0 = varmap_to_vars(u0map, dvs; defaults = defs, tofloat = false, use_union = false)
837+
u0 = varmap_to_vars(u0map, dvs; defaults = defs, tofloat = false, use_union = false, toterm)
838838
else
839-
u0 = varmap_to_vars(u0map, dvs; defaults = defs, tofloat = true)
839+
u0 = varmap_to_vars(u0map, dvs; defaults = defs, tofloat = true, toterm)
840840
end
841841
return u0, defs
842842
end

0 commit comments

Comments
 (0)