fix: [AI] consider variable derivatives when inserting dummy derivative variables into SCCs#67
Merged
AayushSabharwal merged 1 commit intomainfrom Apr 8, 2026
Merged
Conversation
…ve variables into SCCs Co-authored-by: Claude <noreply@anthropic.com>
Member
Author
|
The above description is anonymized from the MWE and is generated by Claude |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Bug: Raw Differential Term in Observed Equation for 2nd-order Kinematic Constraint
Summary
When structurally simplifying a system that contains a 2nd-order kinematic constraint
relating a SelectedState variable
xto a non-SelectedState variabley, the observedequation for
default_toterm(D(D(y)))can retain a rawD(D(x))on its RHS instead ofbeing fully resolved to algebraic state expressions.
Minimal Reproducer
Consider a system with:
x(e.g., dynamics equation solved forD(D(x)))D(D(y)) ~ D(D(x)) + D(D(z))whereyandzarenon-SelectedState (solved from constraints)
The observed equation for
y_tt = diff2term(D(D(y)))will incorrectly containD(D(x))in its RHS instead of the fully resolved algebraic expression.
Variable roles (post-Pantelides, post-state-selection)
xD(x)D(D(x))yD(y)D(D(y))The 2nd-order kinematic constraint produces an observed equation for
y_tt = diff2term(D(D(y))).Root cause
Step 1 —
substitute_derivatives_algevars!Because
yis non-SelectedState, the function renames:fullvars[D(y)_idx] = y_tfullvars[D(D(y))_idx] = y_ttdiff_to_var[D(D(y))_idx] = nothing, severing the diff-chain primal link.Because
xis SelectedState, the function does not touch it.D(D(x))remains as the symbolic
Differential(t,2)(x)in the 2nd-order constraint equation.Step 2 —
generate_derivative_variables!Because
D(x)_idxis SelectedState, the function creates the dummy variablex_t:add_dd_variable!pushesx_tintofullvarsand sets:var_to_diff[x_t_idx] = D(D(x))_idxDiffGraph.setindex!overwritesdiff_to_primal[D(D(x))_idx]fromD(x)_idx→x_t_idx.0 ~ D(x) - x_tasdummy_eq.var_eq_matching[D(x)_idx] = dummy_eq.The BLT (
var_sccs) is updated: the original SelectedState SCC{D(x)_idx}atposition i becomes
{x_t_idx}, and a new singleton{D(x)_idx}isinserted at position i (line 248:
sccs_to_insert[k] = (i, [dv])).Step 3 — BLT ordering conflict
generate_system_equations!processes SCCs in BLT order and populatestotal_sub(a
DerivativeDict) incrementally.The DiCMO graph gives these ordering constraints:
{D(D(x))_idx}→ must precede → constraint SCC{D(D(y))_idx}(constraint equation is incident on
D(D(x))).{D(x)_idx}→ no ordering constraint relative tothe constraint SCC, because the constraint equation is incident on
D(D(x))_idx,not
D(x)_idx.When the dynamics equation has no velocity-dependent terms (
D(x)is not anincidence variable of the dynamics equation), the original SelectedState SCC
{D(x)_idx}has no forced position in the BLT and can appear after both thedynamics SCC and the constraint SCC.
After
generate_derivative_variables!, the inserted singleton{D(x)_idx}inheritsthat same late position. The actual processing order becomes:
{D(D(x))_idx}:processes the inline linear SCC path, sets
total_sub[D(x_t)] = rhs_dynamics.{D(D(y))_idx}:diff_to_var[D(D(y))_idx] = nothing(cleared in step 1), somake_solved_equation(y_tt, constraint2, total_sub)is called.rhs = D(D(x)) - z_tt(raw fromneweqs)fixpoint_sub(D(D(x)), total_sub, MTKBase.Shift):DerivativeDictresolvesDifferential(t,2)(x)by looking upD(x).D(x)is not yet intotal_sub→ lookup fails →D(D(x))stays.y_tt ~ D(D(x)) - z_tt← BUG{D(x)_idx}:sets
total_sub[D(x)] = x_t— too late.Fix
File:
lib/ModelingToolkitTearing/src/reassemble.jlFunction:
generate_derivative_variables!, ~line 248The singleton
{dv}must be inserted before the SCC containingvar_to_diff[dv](the dynamics SCC), not merely at the position of the originalSelectedState SCC.
v_to_scc[ddv][1]is the BLT position of the SCC containingD(D(x))_idx.Using
min(i, i_ddv)as the insertion point guarantees the dummy singleton runsbefore the dynamics SCC regardless of where the original SelectedState SCC was placed.
Since the constraint SCC depends on dynamics (BLT edge), it runs after both, so
total_sub[D(x)] = x_tis available whenmake_solved_equationis called.When
i ≤ i_ddv(the normal case where the SelectedState SCC was already beforedynamics),
min(i, i_ddv) = iand behavior is unchanged.