@mtkcompile / System(...): unknowns and required u0 change when I rename a component

Hi everyone,

I am working with a fairly large ModelingToolkit model built from components. I compile it with something like:

@mtkcompile sys = System(assembly, t; systems = elements)

where elements is the list of the different components previously defined with @named, and assembly contains the connect() calls used to build the full system.

Then I provide u0 and guesses (as symbolic mappings) to build an ODEProblem and solve it.

I am observing a confusing behavior regarding the system unknowns.

In my elements list, I have a component named θ_dynamic. If I rename that component to θ (changing both the @named declaration and the corresponding entry in the elements list), the set of unknowns in sys changes, and therefore the variables I need to provide in u0 / guesses also change.

For example, with the name θ_dynamic, I am asked for something like R1.q (the flow through the R1 resistance). But if I only rename that component to θ, I am instead asked for R2.q.

:confused: What confuses me is:

  • I expected u0 to refer to the actual state variables, so I did not expect it to change just because I renamed one component.
  • More generally, I would like the initialization to be consistent and not depend on component names or on the order of components in elements.

I also noticed another strange behavior: I have the “same” model both in a notebook and in a script, but I do not obtain exactly the same system of equations or the same unknowns.

:red_question_mark: So my questions are:

  1. Is it expected that renaming a subsystem can change the compiled unknowns?
  2. Could this change be related to equation sorting, tearing, or alias elimination during mtkcompile?
  3. What is the best practice to make initialization robust so that it does not depend on unstable unknown ordering?

Thank you for your help !