Skip to content

Commit 2748f7f

Browse files
docs: add documentation for Initial operator
1 parent c78e9f0 commit 2748f7f

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

src/systems/abstractsystem.jl

+15
Original file line numberDiff line numberDiff line change
@@ -650,6 +650,12 @@ function isscheduled(sys::AbstractSystem)
650650
end
651651
end
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+
"""
653659
struct Initial <: Symbolics.Operator end
654660
Initial(x) = Initial()(x)
655661
SymbolicUtils.promote_symtype(::Type{Initial}, T) = T
@@ -660,30 +666,39 @@ input_timedomain(::Initial, _ = nothing) = Continuous()
660666
output_timedomain(::Initial, _ = nothing) = Continuous()
661667

662668
function (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
685699
end
686700

701+
# This is required so `fast_substitute` works
687702
function SymbolicUtils.maketerm(::Type{<:BasicSymbolic}, ::Initial, args, meta)
688703
return metadata(Initial()(args...), meta)
689704
end

0 commit comments

Comments
 (0)