Skip to content

Commit ccb04d8

Browse files
Merge pull request #3433 from vyudu/add_init
feat: add `isInitial`
2 parents 0d9fde4 + f883b8e commit ccb04d8

File tree

4 files changed

+41
-1
lines changed

4 files changed

+41
-1
lines changed

docs/src/tutorials/initialization.md

+11
Original file line numberDiff line numberDiff line change
@@ -536,3 +536,14 @@ sol[α * x - β * x * y]
536536
```@example init
537537
plot(sol)
538538
```
539+
540+
## Summary of Initialization API
541+
542+
```@docs; canonical=false
543+
Initial
544+
isinitial
545+
generate_initializesystem
546+
initialization_equations
547+
guesses
548+
defaults
549+
```

src/ModelingToolkit.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ export toexpr, get_variables
286286
export simplify, substitute
287287
export build_function
288288
export modelingtoolkitize
289-
export generate_initializesystem, Initial
289+
export generate_initializesystem, Initial, isinitial
290290

291291
export alg_equations, diff_equations, has_alg_equations, has_diff_equations
292292
export get_alg_eqs, get_diff_eqs, has_alg_eqs, has_diff_eqs

src/systems/abstractsystem.jl

+16
Original file line numberDiff line numberDiff line change
@@ -717,6 +717,22 @@ function add_initialization_parameters(sys::AbstractSystem)
717717
return sys
718718
end
719719

720+
"""
721+
Returns true if the parameter `p` is of the form `Initial(x)`.
722+
"""
723+
function isinitial(p)
724+
p = unwrap(p)
725+
if iscall(p)
726+
operation(p) isa Initial && return true
727+
if operation(p) === getindex
728+
operation(arguments(p)[1]) isa Initial && return true
729+
end
730+
else
731+
return false
732+
end
733+
return false
734+
end
735+
720736
"""
721737
$(TYPEDSIGNATURES)
722738

test/variable_utils.jl

+13
Original file line numberDiff line numberDiff line change
@@ -145,3 +145,16 @@ end
145145
@test isequal(parse_variable(sys, str), var)
146146
end
147147
end
148+
149+
@testset "isinitial" begin
150+
t = ModelingToolkit.t_nounits
151+
@variables x(t) z(t)[1:5]
152+
@parameters a b c[1:4]
153+
@test isinitial(Initial(z))
154+
@test isinitial(Initial(x))
155+
@test isinitial(Initial(a))
156+
@test isinitial(Initial(z[1]))
157+
@test isinitial(Initial(c[4]))
158+
@test !isinitial(c)
159+
@test !isinitial(x)
160+
end

0 commit comments

Comments
 (0)