Skip to content

dummy_derivative_graph!: Jacobian columns not permuted after priority sort — vars[col_order[i]] indexes mismatched orders #102

Closed

Description

@baggepinnen

Summary

In dummy_derivative_graph! (src/partial_state_selection.jl), when state_priority is
provided, the candidate variables are sorted by priority — but the integer Jacobian J
computed before the sort is never column-permuted. The bareiss col_order therefore
refers to columns in the original variable order while being used to index the sorted
vars array, so the selected dummy derivatives can be arbitrary with respect to both the
priority ranking and the numerical pivoting.

Code path (StateSelection v1.9.3)

J = jac(eqs, vars)                       # columns follow `vars` in ORIGINAL order
...
if state_priority !== nothing && isfirst
    sp = extended_sp.(vars)
    sortperm!(var_perm, sp)
    permute!(vars, var_perm)             # `vars` now sorted by priority
    permute!(sp, var_perm)               # ... but `J` is NOT permuted
    ...
end
if J !== nothing
    ...
    N = bareiss.nullspace(J; col_order)  # col_order refers to ORIGINAL column order
    rank = length(col_order) - size(N, 2)
    for i in 1:rank
        push!(dummy_derivatives, vars[col_order[i]])   # indexes SORTED vars — mismatch
    end

Two consequences:

  1. Correctness: vars[col_order[i]] selects a different variable than the one bareiss
    actually pivoted on. The demoted set may not even correspond to a structurally valid
    pivot set of J for the subsequent J = J[next_eq_idxs, next_var_idxs] iterations
    (those index arrays are also built against the sorted vars).
  2. Intent: even with the permutation fixed, for the priority sort to influence the
    Jacobian path, bareiss.nullspace must prefer earlier columns as pivots; if it does,
    adding J = J[:, var_perm] inside the sort branch restores the intended
    "demote lowest-priority first" semantics on this path as well.

The bug is masked whenever jac returns nothing (any non-integer entry, e.g.
trigonometric kinematics → matching path), when the SCC is a singleton, or when priorities
are uniform within the SCC (sort is a no-op modulo stable-sort ties). It triggers for
multi-variable SCCs with an all-integer Jacobian and non-uniform priorities.

Found while investigating #101 (which is not caused by this — that model's loop SCC
falls back to the matching path).

Suggested fix

permute!(vars, var_perm)
permute!(sp, var_perm)
J === nothing || (J = J[:, var_perm])

(plus a test with a small integer-Jacobian SCC with mixed priorities).

Activity

  1. added a commit that references this issue on Oct 13, 2026
    922f0fa
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions