Skip to content

Add opt-in preferred_states_threshold for priority-driven state selection - #191

Draft
asinghvi17 wants to merge 2 commits into
mainfrom
as/preferred-states-threshold
Draft

asinghvi17 wants to merge 2 commits into
mainfrom
as/preferred-states-threshold

Conversation

@asinghvi17

@asinghvi17 asinghvi17 commented Nov 24, 2026 •

Copy link
Copy Markdown

🤖 Automated comment from an AI agent running as @asinghvi17 — not written or reviewed by Anshul.

Adds an opt-in preferred_states_threshold = k (forwarded by mtkcompile) so variables with state_priority >= k actually win state selection, e.g. a joint's relative coordinates in a multibody model instead of the body's coordinates.

Two things stopped that before. A preferred variable that never appears under D (say sB ~ s - c) got no derivative from Pantelides, so it was never a candidate; it now gets one up front (pantelides! takes present_vars). And dummy derivatives were picked one SCC at a time, so in triangular systems the first equation decided alone; with the keyword they are picked by priority over blocks of differentiated equations that share highest derivatives (Mattsson & Söderlind). Without the keyword nothing changes.

Open questions: a numeric threshold or a prefer/always attribute (libraries already use small priorities as tie-breakers)? Should block selection ever be the default? The choice is structural, so a preferred variable that is a non-invertible function of the states can become a poor state, and one tied to an input needs D(u).

Performance (62 systems: MTK and Multibody examples, synthetic chains, a few large multibody models; one pinned core, warm median):

  • Without the keyword: same states on all 62, times within noise.
  • With it, mtkcompile: +1–5% on most models (e.g. 60-link planar chain 561 → 591 ms, RC ladder 1000 unchanged); small models are a few ms slower. Block selection first built a dense Jacobian over whole blocks, which made large nonlinear multibody models up to 4.3× slower; it is now built row by row over incident candidates and stops at the first nonlinear row, which brings those back to baseline (0.95–1.00×) with identical states. The first call in a session costs ~2 s more, since the keyword path isn't in MTK's precompile workload.
  • Runtime where the selection changes: f unchanged, Jacobians equal or faster (−30% on one model, 49 → 40 unknowns), 20/60-link planar chains solve 1.5–1.8× faster, two small repros 1.3× slower; results agree to ≤ 1.6e-6.
  • The keyword has no effect on time-independent systems. Threshold 1 changes many selections in the Multibody library and should not be a default.

🤖 Posted by an AI agent — harness: Claude 2.1.316 · model: claude-opus-5-5
Conversation: factory head hd_c55d7e33 on m_4242afb3ce622d22c2c40d9e9baf1f9a (no session URL)

asinghvi17 and others added 2 commits November 24, 2026 21:43
With `preferred_states_threshold = k`, every variable whose state priority
is at least `k` and that never appears differentiated is given a derivative
before Pantelides (`pantelides!` gains a `present_vars` keyword so these
count as present). Dummy derivatives are then chosen by priority over whole
blocks of differentiated equations that share highest derivatives, descending
levels as in Mattsson & Söderlind, with deterministic tie-breaks, instead of
one strongly connected component at a time.

This lets models where a preferred coordinate is only an algebraic image of
another state (e.g. multibody models where a joint's relative coordinates
should be states) pick the preferred variables. Without the keyword the
behaviour is unchanged.

License: AGPL-3.0-only
Copyright: JuliaHub, Inc.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
`priority_dummy_derivatives!` called the dense `jac(eqs, vars)` on whole
priority blocks at every level, which dominates compile time on large
nonlinear blocks where `jac` gives up anyway. Build the Jacobian one
equation at a time, only over the candidates incident on it in `graph`,
and stop at the first row `jac` rejects. Also note in the docstring that
any `preferred_states_threshold` enables block-level selection.

License: AGPL-3.0-only
Copyright: JuliaHub, Inc.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.

1 participant