Skip to content

Commit 00c56f2

Browse files
feat: reorder parameters in complete(sys) according to portions
1 parent 52a1ebf commit 00c56f2

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

src/systems/abstractsystem.jl

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -882,6 +882,44 @@ namespace its subsystems or variables, i.e. `isequal(complete(sys).v.i, v.i)`.
882882
function complete(sys::AbstractSystem; split = true)
883883
if split && has_index_cache(sys)
884884
@set! sys.index_cache = IndexCache(sys)
885+
all_ps = parameters(sys)
886+
if !isempty(all_ps)
887+
# reorder parameters by portions
888+
ps_split = reorder_parameters(sys, all_ps)
889+
# if there are no tunables, vcat them
890+
if isempty(get_index_cache(sys).tunable_idx)
891+
ordered_ps = reduce(vcat, ps_split)
892+
else
893+
# if there are tunables, they will all be in `ps_split[1]`
894+
# and the arrays will have been scalarized
895+
ordered_ps = eltype(all_ps)[]
896+
i = 1
897+
# go through all the tunables
898+
while i <= length(ps_split[1])
899+
sym = ps_split[1][i]
900+
# if the sym is not a scalarized array symbolic OR it was already scalarized,
901+
# just push it as-is
902+
if !iscall(sym) || operation(sym) != getindex ||
903+
any(isequal(sym), all_ps)
904+
push!(ordered_ps, sym)
905+
i += 1
906+
continue
907+
end
908+
# the next `length(sym)` symbols should be scalarized versions of the same
909+
# array symbolic
910+
if !allequal(first(arguments(x))
911+
for x in view(ps_split[1], i:(i + length(sym) - 1)))
912+
error("This should not be possible. Please open an issue in ModelingToolkit.jl with an MWE and stacktrace.")
913+
end
914+
arrsym = first(arguments(sym))
915+
push!(ordered_ps, arrsym)
916+
i += length(arrsym)
917+
end
918+
ordered_ps = vcat(
919+
ordered_ps, reduce(vcat, ps_split[2:end]; init = eltype(ordered_ps)[]))
920+
end
921+
@set! sys.ps = ordered_ps
922+
end
885923
end
886924
if isdefined(sys, :initializesystem) && get_initializesystem(sys) !== nothing
887925
@set! sys.initializesystem = complete(get_initializesystem(sys); split)

0 commit comments

Comments
 (0)