Skip to content

Commit d8e93d4

Browse files
refactor: improve precompile-friendliness
1 parent a78cf3a commit d8e93d4

File tree

3 files changed

+38
-34
lines changed

3 files changed

+38
-34
lines changed

lib/ModelingToolkitBase/src/systems/abstractsystem.jl

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1460,6 +1460,14 @@ function unknowns_toplevel(sys::AbstractSystem)
14601460
return get_unknowns(sys)
14611461
end
14621462

1463+
function __no_initial_params_pred(x::SymbolicT)
1464+
arr, _ = split_indexed_var(x)
1465+
Moshi.Match.@match arr begin
1466+
BSImpl.Term(; f) && if f isa Initial end => false
1467+
_ => false
1468+
end
1469+
end
1470+
14631471
"""
14641472
$(TYPEDSIGNATURES)
14651473
@@ -1482,11 +1490,7 @@ function parameters(sys::AbstractSystem; initial_parameters = false)
14821490
end
14831491
result = collect(result)
14841492
if !initial_parameters && !is_initializesystem(sys)
1485-
filter!(result) do sym
1486-
return !(isoperator(sym, Initial) ||
1487-
iscall(sym) && operation(sym) === getindex &&
1488-
isoperator(arguments(sym)[1], Initial))
1489-
end
1493+
filter!(__no_initial_params_pred, result)
14901494
end
14911495
return result
14921496
end
@@ -1699,7 +1703,7 @@ Recursively substitute `dict` into `expr`. Use `Symbolics.simplify` on the expre
16991703
if `simplify == true`.
17001704
"""
17011705
function substitute_and_simplify(expr, dict::AbstractDict, simplify::Bool)
1702-
expr = Symbolics.fixpoint_sub(expr, dict; operator = Union{ModelingToolkitBase.Initial, Pre})
1706+
expr = Symbolics.fixpoint_sub(expr, dict, Union{Initial, Pre})
17031707
simplify ? Symbolics.simplify(expr) : expr
17041708
end
17051709

lib/ModelingToolkitBase/src/systems/callbacks.jl

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1043,13 +1043,13 @@ The `SymbolicDiscreteCallback`s in the returned vector are structs with two fiel
10431043
See also `get_discrete_events`, which only returns the events of the top-level system.
10441044
"""
10451045
function discrete_events(sys::AbstractSystem)
1046-
obs = get_discrete_events(sys)
1046+
cbs = get_discrete_events(sys)
10471047
systems = get_systems(sys)
1048-
cbs = [obs;
1049-
reduce(vcat,
1050-
(map(cb -> namespace_callback(cb, s), discrete_events(s)) for s in systems),
1051-
init = SymbolicDiscreteCallback[])]
1052-
cbs
1048+
cbs = copy(cbs)
1049+
for s in systems
1050+
append!(cbs, map(Base.Fix2(namespace_callback, s), discrete_events(s)))
1051+
end
1052+
return cbs
10531053
end
10541054

10551055
"""
@@ -1100,15 +1100,13 @@ The `SymbolicContinuousCallback`s in the returned vector are structs with two fi
11001100
See also `get_continuous_events`, which only returns the events of the top-level system.
11011101
"""
11021102
function continuous_events(sys::AbstractSystem)
1103-
obs = get_continuous_events(sys)
1104-
filter(!isempty, obs)
1105-
1103+
cbs = get_discrete_events(sys)
11061104
systems = get_systems(sys)
1107-
cbs = [obs;
1108-
reduce(vcat,
1109-
(map(o -> namespace_callback(o, s), continuous_events(s)) for s in systems),
1110-
init = SymbolicContinuousCallback[])]
1111-
filter(!isempty, cbs)
1105+
cbs = copy(cbs)
1106+
for s in systems
1107+
append!(cbs, map(Base.Fix2(namespace_callback, s), discrete_events(s)))
1108+
end
1109+
return cbs
11121110
end
11131111

11141112
"""

lib/ModelingToolkitBase/src/systems/systems.jl

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,15 @@ function canonicalize_io(iovars, type::String)
1010
iobuffer = OrderedSet{SymbolicT}()
1111
arrsyms = AtomicArrayDict{OrderedSet{SymbolicT}}()
1212
for var in iovars
13-
if Symbolics.isarraysymbolic(var)
14-
if !symbolic_has_known_size(var)
15-
throw(ArgumentError("""
16-
All $(type)s must have known shape. Found $var with unknown shape.
17-
"""))
13+
sh = SU.shape(var)
14+
if SU.is_array_shape(sh)
15+
if sh isa SU.ShapeVecT
16+
union!(iobuffer, vec(collect(var)::Array{SymbolicT})::Vector{SymbolicT})
17+
continue
1818
end
19-
union!(iobuffer, vec(collect(var)::Array{SymbolicT})::Vector{SymbolicT})
20-
continue
19+
throw(ArgumentError("""
20+
All $(type)s must have known shape. Found $var with unknown shape.
21+
"""))
2122
end
2223
arr, isarr = split_indexed_var(var)
2324
if isarr
@@ -41,7 +42,7 @@ function canonicalize_io(iovars, type::String)
4142
or simply pass $k as an $type.
4243
"""))
4344
end
44-
if type != "output" && !isequal(vec(collect(k))::Vector{SymbolicT}, collect(v))
45+
if type != "output" && !isequal(vec(collect(k)::Array{SymbolicT})::Vector{SymbolicT}, collect(v))
4546
throw(ArgumentError("""
4647
Elements of scalarized array variables must be in sorted order in $(type)s. \
4748
Either pass all scalarized elements in sorted order as $(type)s \
@@ -469,16 +470,17 @@ function __num_isdiag_noise(mat)
469470
true
470471
end
471472

472-
function __get_num_diag_noise(mat)
473-
map(axes(mat, 1)) do i
473+
function __get_num_diag_noise(mat::Matrix{SymbolicT})
474+
result = fill(Symbolics.COMMON_ZERO, size(mat, 1))
475+
for i in axes(mat, 1)
474476
for j in axes(mat, 2)
475477
mij = mat[i, j]
476-
if !_iszero(mij)
477-
return mij
478-
end
478+
_iszero(mij) && continue
479+
result[i] = mij
480+
break
479481
end
480-
0
481482
end
483+
return result
482484
end
483485

484486
"""

0 commit comments

Comments
 (0)