@@ -882,6 +882,41 @@ namespace its subsystems or variables, i.e. `isequal(complete(sys).v.i, v.i)`.
882882function  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 ||  any (isequal (sym), all_ps)
903+                         push! (ordered_ps, sym)
904+                         i +=  1 
905+                         continue 
906+                     end 
907+                     #  the next `length(sym)` symbols should be scalarized versions of the same
908+                     #  array symbolic
909+                     if  ! allequal (first (arguments (x)) for  x in  view (ps_split[1 ], i: i+ length (sym)- 1 ))
910+                         error (" This should not be possible. Please open an issue in ModelingToolkit.jl with an MWE and stacktrace." 
911+                     end 
912+                     arrsym =  first (arguments (sym))
913+                     push! (ordered_ps, arrsym)
914+                     i +=  length (arrsym)
915+                 end 
916+                 ordered_ps =  vcat (ordered_ps, reduce (vcat, ps_split[2 : end ]))
917+             end 
918+             @set!  sys. ps =  ordered_ps
919+         end 
885920    end 
886921    if  isdefined (sys, :initializesystem ) &&  get_initializesystem (sys) != =  nothing 
887922        @set!  sys. initializesystem =  complete (get_initializesystem (sys); split)
0 commit comments