@@ -650,6 +650,12 @@ function isscheduled(sys::AbstractSystem)
650
650
end
651
651
end
652
652
653
+ """
654
+ Initial(x)
655
+
656
+ The `Initial` operator. Used by initializaton to store constant constraints on variables
657
+ of a system. See the documentation section on initialization for more information.
658
+ """
653
659
struct Initial <: Symbolics.Operator end
654
660
Initial (x) = Initial ()(x)
655
661
SymbolicUtils. promote_symtype (:: Type{Initial} , T) = T
@@ -660,30 +666,39 @@ input_timedomain(::Initial, _ = nothing) = Continuous()
660
666
output_timedomain (:: Initial , _ = nothing ) = Continuous ()
661
667
662
668
function (f:: Initial )(x)
669
+ # wrap output if wrapped input
663
670
iw = Symbolics. iswrapped (x)
664
671
x = unwrap (x)
672
+ # non-symbolic values don't change
665
673
if symbolic_type (x) == NotSymbolic ()
666
674
return x
667
675
end
676
+ # differential variables are default-toterm-ed
668
677
if iscall (x) && operation (x) isa Differential
669
678
x = default_toterm (x)
670
679
end
680
+ # don't double wrap
671
681
iscall (x) && operation (x) isa Initial && return x
672
682
result = if symbolic_type (x) == ArraySymbolic ()
683
+ # create an array for `Initial(array)`
673
684
Symbolics. array_term (f, toparam (x))
674
685
elseif iscall (x) && operation (x) == getindex
686
+ # instead of `Initial(x[1])` create `Initial(x)[1]`
687
+ # which allows parameter indexing to handle this case automatically.
675
688
arr = arguments (x)[1 ]
676
689
term (getindex, f (toparam (arr)), arguments (x)[2 : end ]. .. )
677
690
else
678
691
term (f, toparam (x))
679
692
end
693
+ # the result should be a parameter
680
694
result = toparam (result)
681
695
if iw
682
696
result = wrap (result)
683
697
end
684
698
return result
685
699
end
686
700
701
+ # This is required so `fast_substitute` works
687
702
function SymbolicUtils. maketerm (:: Type{<:BasicSymbolic} , :: Initial , args, meta)
688
703
return metadata (Initial ()(args... ), meta)
689
704
end
0 commit comments