Docs/Troubleshooting & FAQ/Troubleshooting Initialization Failures

Troubleshooting Initialization Failures (Over-Determined Systems)

Applies when a simulation fails at startup with ReturnCode.InitialFailure and switching solvers does not help.

1. Symptoms

The backend reports ReturnCode.InitialFailure = 8 during simulation, and the log shows:

Warning: Initialization system is overdetermined. 8 equations for 1 unknowns.
Initialization will default to using least squares.

Or:

Warning: Initialization system is structurally singular.
Warning: Initialization system is underdetermined. 0 equations for 3 unknowns.

Switching solvers (Rodas4, IDA, etc.) does not help, because the problem is in the initialization stage, not the integration stage.


2. Root Cause

The modeling layer gives variable assignments two different semantics:

Syntax Meaning Initialization behavior
B(t) = 1.5 default (default value) Generates an initialization constraint equation B(0) = 1.5
B(t), [guess = 1.5] guess (initial guess) Only provides an initial guess for the nonlinear solver; produces no constraint

If an algebraic variable (one determined by an algebraic equation such as B ~ B_in.u) uses = value (default), the solver generates two constraints at once:

  1. The algebraic equation itself: B ~ B_in.u
  2. The default constraint: B(0) = 1.5

The two conflict → the initialization system becomes over-determined → InitialFailure.

For a differential state (one appearing inside D(x)), = value is correct: the default serves as a necessary initial-value constraint.


3. How to Fix

3.1 Algebraic variables: change = value to [guess = value]

# ❌ Wrong: a default on an algebraic variable over-determines initialization
vars = @variables begin
    B(t) = 1.5
    P_fe(t) = 0.0
end

# ✅ Correct: algebraic variables use guess, providing only an initial guess
vars = @variables begin
    B(t), [guess = 1.5, description = "Magnetic flux density (T)"]
    P_fe(t), [guess = 0.0, description = "Total iron loss (W)"]
end

3.2 Differential states: keep = value unchanged

# ✅ Correct: differential states use default, serving as initial-value constraints
vars = @variables begin
    T_w(t) = 313.15    # initial winding temperature
    T_c(t) = 313.15    # initial core temperature
end

4. Modeling Guidelines

When writing custom MTK components, follow these rules:

  1. Determine the variable type: does the variable appear on the lhs of D(x)?

    • Yes → differential state → use = value (default)
    • No → algebraic variable → use [guess = value]
  2. Common algebraic-variable cases:

    • Signal-passing variables: B in B ~ B_in.u
    • Intermediate computation variables: P_fe in P_fe ~ k_h * f * B^alpha
    • Bridging/assignment variables: Q_w in Q_w ~ P * eta_winding
  3. Common differential-state cases:

    • Thermal network node temperatures: T in C * D(T) ~ ...
    • Capacitor voltages: v in C * D(v) ~ i
    • Inductor currents: i in L * D(i) ~ v
  4. Choosing guess values: stay close to the steady-state solution to speed up convergence of the nonlinear initialization. When in doubt, use 0.0; the system falls back to missing_guess_value = Constant(0.0).

  5. RealInput/RealOutput ports: set the guess= parameter on the port itself (e.g. RealInput(guess=1.5)); do not redefine a default for the port variable in @variables.


5. Benign Warnings

After the fix, the following warning may still appear; it does not affect simulation results:

Warning: Initialization system is underdetermined. 0 equations for 3 unknowns.

This means the initialization system has no algebraic constraint equations and the initial values come entirely from defaults—normal behavior for a pure ODE system.