# Ordering of unknowns(system) in MTK when array-valued variables are scalarized via structural\_simplify

**URL:** <https://discourse.julialang.org/t/ordering-of-unknowns-system-in-mtk-when-array-valued-variables-are-scalarized-via-structural-simplify/129358>\
**Category:** Modelling & Simulations\
**Tags:** modelingtoolkit\
**Created:** [May 26, 2025, 7:46pm UTC](https://discourse.julialang.org/t/ordering-of-unknowns-system-in-mtk-when-array-valued-variables-are-scalarized-via-structural-simplify/129358 "2025-05-26T19:46:07Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![sass-jacob](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sass-jacob/32/215582_2.png) [@sass-jacob](https://discourse.julialang.org/u/sass-jacob)\
**Post date:** [May 26, 2025, 7:46pm UTC](https://discourse.julialang.org/t/ordering-of-unknowns-system-in-mtk-when-array-valued-variables-are-scalarized-via-structural-simplify/129358/1 "2025-05-26T19:46:07Z")

</div>

What is the current rule for ordering of array-valued variables once structural\_simplify has been called and sort\_eqs is set to false? My current array has something like this:

```julia
unknowns(sys)[221:230]

 (Φ_s_c(t))[2]
 (Φ_s_c(t))[1]
 (Φ_s_c(t))[3]
 (Φ_s_c(t))[4]
 (Φ_s_c(t))[5]
 (Φ_s_c(t))[6]
 (Φ_s_c(t))[7]
 (Φ_s_c(t))[8]
 (Φ_s_c(t))[9]
 (Φ_s_c(t))[10]

```

but I’d like to swap the first two. Is this possible? Would this be based off of which variable I reference first when constructing the equations?

---

<div class="post-metadata">

**Author:** ![Bart\_van\_de\_Lint](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bart_van_de_lint/32/212161_2.png) [@Bart\_van\_de\_Lint](https://discourse.julialang.org/u/Bart_van_de_Lint)\
**Post date:** [May 26, 2025, 7:54pm UTC](https://discourse.julialang.org/t/ordering-of-unknowns-system-in-mtk-when-array-valued-variables-are-scalarized-via-structural-simplify/129358/2 "2025-05-26T19:54:41Z")

</div>

> Ordering of symbols is not guaranteed after symbolic transformations, and parameters are now stored in a custom `MTKParameters` object instead of a vector.  
> [Frequently Asked Questions · ModelingToolkit.jl](https://docs.sciml.ai/ModelingToolkit/stable/basics/FAQ/#Getting-the-index-for-a-symbol)

So no, you cannot swap ordering, and you don’t need to swap the order if you use symbolic indexing, for example `sol[sys.Φ_s_c[2]]`.

---

<div class="post-metadata">

**Author:** ![sass-jacob](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sass-jacob/32/215582_2.png) [@sass-jacob](https://discourse.julialang.org/u/sass-jacob)\
**Post date:** [May 26, 2025, 9:28pm UTC](https://discourse.julialang.org/t/ordering-of-unknowns-system-in-mtk-when-array-valued-variables-are-scalarized-via-structural-simplify/129358/3 "2025-05-26T21:28:29Z")

</div>

Thanks Bart, I was curious because I am trying to visualize the full system’s Jacobian which contains equations with Laplacian of `Φ_s_c`. This ordering leads to some odd-looking parts at the switched variables.

---

<div class="post-metadata">

**Author:** ![Bart\_van\_de\_Lint](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bart_van_de_lint/32/212161_2.png) [@Bart\_van\_de\_Lint](https://discourse.julialang.org/u/Bart_van_de_Lint)\
**Post date:** [May 26, 2025, 9:41pm UTC](https://discourse.julialang.org/t/ordering-of-unknowns-system-in-mtk-when-array-valued-variables-are-scalarized-via-structural-simplify/129358/4 "2025-05-26T21:41:01Z")

</div>

The function `ModelingToolkit.calculate_jacobian` does have a kwarg `dvs=unknowns(sys)` but I am not sure if passing differential vars with a different ordering will affect the ordering in the calculated jacobian, you could try this.

> <https://github.com/SciML/ModelingToolkit.jl/blob/b939fdc75568d08ac7eae684bee2029338cc7887/src/systems/codegen.jl#L124C1-L128C1>
