@@ -650,6 +650,12 @@ function isscheduled(sys::AbstractSystem)
650650 end
651651end
652652
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+ """
653659struct Initial <: Symbolics.Operator end
654660Initial (x) = Initial ()(x)
655661SymbolicUtils. promote_symtype (:: Type{Initial} , T) = T
@@ -660,30 +666,39 @@ input_timedomain(::Initial, _ = nothing) = Continuous()
660666output_timedomain (:: Initial , _ = nothing ) = Continuous ()
661667
662668function (f:: Initial )(x)
669+ # wrap output if wrapped input
663670 iw = Symbolics. iswrapped (x)
664671 x = unwrap (x)
672+ # non-symbolic values don't change
665673 if symbolic_type (x) == NotSymbolic ()
666674 return x
667675 end
676+ # differential variables are default-toterm-ed
668677 if iscall (x) && operation (x) isa Differential
669678 x = default_toterm (x)
670679 end
680+ # don't double wrap
671681 iscall (x) && operation (x) isa Initial && return x
672682 result = if symbolic_type (x) == ArraySymbolic ()
683+ # create an array for `Initial(array)`
673684 Symbolics. array_term (f, toparam (x))
674685 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.
675688 arr = arguments (x)[1 ]
676689 term (getindex, f (toparam (arr)), arguments (x)[2 : end ]. .. )
677690 else
678691 term (f, toparam (x))
679692 end
693+ # the result should be a parameter
680694 result = toparam (result)
681695 if iw
682696 result = wrap (result)
683697 end
684698 return result
685699end
686700
701+ # This is required so `fast_substitute` works
687702function SymbolicUtils. maketerm (:: Type{<:BasicSymbolic} , :: Initial , args, meta)
688703 return metadata (Initial ()(args... ), meta)
689704end
0 commit comments