Skip to content

Conversation

@baggepinnen
Copy link
Member

@baggepinnen baggepinnen commented Jun 3, 2025

@baggepinnen
Copy link
Member Author

baggepinnen commented Jun 9, 2025

@AayushSabharwal

A docs example fails with the error below, it appears to be related to symbolic arrays?

julia> prob = ODEProblem(structural_simplify(closed_loop), [F.xd => 0], (0.0, 8.0))
ERROR: ArgumentError: (Cgs₊A(Cgs₊v(t)))[1, 2] is present in the system but (Cgs₊A(Cgs₊v(t)))[1, 2] is not an unknown.
Stacktrace:
 [1] TearingState(sys::System; quick_cancel::Bool, check::Bool, sort_eqs::Bool)
   @ ModelingToolkit ~/.julia/packages/ModelingToolkit/v0QS9/src/systems/systemstructure.jl:376
 [2] __mtkcompile(sys::System; simplify::Bool, inputs::Vector{…}, outputs::Vector{…}, disturbance_inputs::Vector{…}, sort_eqs::Bool, kwargs::@Kwargs{})
   @ ModelingToolkit ~/.julia/packages/ModelingToolkit/v0QS9/src/systems/systems.jl:79
 [3] __mtkcompile
   @ ~/.julia/packages/ModelingToolkit/v0QS9/src/systems/systems.jl:62 [inlined]
 [4] mtkcompile(sys::System; additional_passes::Vector{…}, simplify::Bool, split::Bool, allow_symbolic::Bool, allow_parameter::Bool, conservative::Bool, fully_determined::Bool, inputs::Vector{…}, outputs::Vector{…}, disturbance_inputs::Vector{…}, kwargs::@Kwargs{})
   @ ModelingToolkit ~/.julia/packages/ModelingToolkit/v0QS9/src/systems/systems.jl:37
 [5] mtkcompile
   @ ~/.julia/packages/ModelingToolkit/v0QS9/src/systems/systems.jl:30 [inlined]
 [6] structural_simplify(sys::System; kwargs::@Kwargs{})
   @ ModelingToolkit ./deprecated.jl:105
 [7] structural_simplify(sys::System)
   @ ModelingToolkit ./deprecated.jl:103
 [8] top-level scope
   @ REPL[29]:1
Some type information was truncated. Use `show(err)` to see complete types.

The unknowns do appear to contain this strange variable though

Unknowns (37): see unknowns(closed_loop)
  duffing₊x(t) [defaults to 0]
  duffing₊v(t) [defaults to 0]
  duffing₊u₊u(t): Inner variable in RealInput u
  duffing₊y₊u(t): Inner variable in RealOutput y
  (Cgs₊x(t))[1] [defaults to 0.0]: State variables of gain-scheduled statespace system Cgs
  (Cgs₊x(t))[2] [defaults to 0.0]: State variables of gain-scheduled statespace system Cgs
  (Cgs₊x(t))[3] [defaults to 0.0]: State variables of gain-scheduled statespace system Cgs
  Cgs₊v(t): Scheduling variable of gain-scheduled statespace system Cgs
  Cgs₊scheduling_input₊u(t): Inner variable in RealInput scheduling_input
  (Cgs₊A(v(t)))[1, 1] [defaults to -141.421]
  (Cgs₊A(v(t)))[2, 1] [defaults to 128.0]
  (Cgs₊A(v(t)))[3, 1] [defaults to 0.0]
  (Cgs₊A(v(t)))[1, 2] [defaults to -78.125]
  (Cgs₊A(v(t)))[2, 2] [defaults to 0.0]
  (Cgs₊A(v(t)))[3, 2] [defaults to 4.0]
  (Cgs₊A(v(t)))[1, 3] [defaults to 0.0]
  (Cgs₊A(v(t)))[2, 3] [defaults to 0.0]
  (Cgs₊A(v(t)))[3, 3] [defaults to 0.0]
  (Cgs₊B(v(t)))[1, 1] [defaults to 128.0]
  (Cgs₊B(v(t)))[2, 1] [defaults to 0.0]
  (Cgs₊B(v(t)))[3, 1] [defaults to 0.0]
  (Cgs₊C(v(t)))[1, 1] [defaults to 181.953]
  (Cgs₊C(v(t)))[1, 2] [defaults to 6.47795]

@AayushSabharwal
Copy link
Contributor

Is that meant to be a variable of the system?

@baggepinnen
Copy link
Member Author

Yes, they are defined like this

@variables v(t) [
        description = "Scheduling variable of gain-scheduled statespace system $name",
] 
@variables A(v)[1:nx, 1:nx]

v is a function of t while A is a function of v. This worked as I expected in MTKv9, but MTKv10 does not like it

@AayushSabharwal
Copy link
Contributor

It's a namespacing bug. The unknown is (Cgs₊A(v(t)))[1, 2] but in the namespaced equation it shows up as (Cgs₊A(Cgs₊v(t)))[1, 2]

@baggepinnen
Copy link
Member Author

baggepinnen commented Jun 19, 2025

On MTK master, initialization now fails in the batch_linearization.md file with

julia> controllersy, ssy, ops3, resolved_ops3 = trajectory_ss(closed_loop, closed_loop.r, closed_loop.u, sol; t=timepoints, verbose=true, loop_openings=[closed_loop.y]);
┌ Warning: Initialization system is overdetermined. 1 equations for 0 unknowns. Initialization will default to using least squares. `SCCNonlinearProblem` can only be used for initialization of fully determined systems and hence will not be used here. To suppress this warning pass warn_initialize_determined = false. To make this warning into an error, pass fully_determined = true
└ @ ModelingToolkit ~/.julia/dev/ModelingToolkit/src/problems/initializationproblem.jl:106
ERROR: Initialization algorithm SciMLBase.OverrideInit failed with  ...
Stacktrace:
  [1] error(s::String)
    @ Base ./error.jl:35
  [2] (::ModelingToolkit.LinearizationFunction{…})(u::Vector{…}, p::MTKParameters{…}, t::Float64)
    @ ModelingToolkit ~/.julia/dev/ModelingToolkit/src/linearization.jl:305
  [3] solve(prob::LinearizationProblem{ModelingToolkit.LinearizationFunction{…}, Float64}; allow_input_derivatives::Bool)
    @ ModelingToolkit ~/.julia/dev/ModelingToolkit/src/linearization.jl:442

From Aayush:

Okay so the issue is that simplifiction artificially inserts variable ~ 0 equations for the initialization, essentially SciML/ModelingToolkit.jl#3302 The TL;DR is that when running alias_elimination! MTK somehow adds those equations for the system and I’m yet to figure out why.

@baggepinnen baggepinnen merged commit 9fd3de1 into master Jul 4, 2025
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants