Skip to content

Read a scalar mass matrix instead of its missing diagonal - #4582

Closed
singhharsh1708 wants to merge 2 commits into
SciML:masterfrom
singhharsh1708:sdirk-scalar-operator-mass-matrix
Closed

singhharsh1708 wants to merge 2 commits into
SciML:masterfrom
singhharsh1708:sdirk-scalar-operator-mass-matrix

Conversation

@singhharsh1708

@singhharsh1708 singhharsh1708 commented Sep 17, 2026 •

Copy link
Copy Markdown
Member

Explicit-first-stage SDIRK methods take a scalar mass matrix again: the FSAL conversion added in #4560 reads diag(M), which neither λ*I nor ScalarOperator(λ) has, so every solve with one threw MethodError: no method matching diag(...).
_mmdiag in generic_imex_perform_step.jl now dispatches on UniformScaling and ScalarOperator and reads the scalar itself, at the start of the step for the FSAL it consumes and at the end for the FSAL it produces, which a ScalarOperator with an update_func needs. SDIRK takes ScalarOperator and update_coefficients from SciMLOperators, as BDF and ExponentialRK do, so it no longer touches Differentiation internals and its Differentiation compat is unchanged.
lib/OrdinaryDiffEqDifferentiation/test/scalar_operator_massmatrix_tests.jl, added with #3916, goes from 5 passed and 1 error to 9 passed. TRBDF2, KenCarp4 and Kvaerno5 solve M u' = -u with 2.0*I (in and out of place) and in place with ScalarOperator(2.0) and a time-dependent ScalarOperator; the out-of-place ScalarOperator cases stop earlier, in NLNewton's W construction.
SDIRK's DAE tests pass 78 of 78 and its qa.jl passes 18 of 18.
AI Disclosure: Claude assisted with this change.

Comment thread lib/OrdinaryDiffEqSDIRK/Project.toml Resolved
@singhharsh1708
singhharsh1708 force-pushed the sdirk-scalar-operator-mass-matrix branch from 6b6e95f to 235c25c Compare September 17, 2026 19:01
@singhharsh1708
singhharsh1708 force-pushed the sdirk-scalar-operator-mass-matrix branch 2 times, most recently from ff6f48d to e96782f Compare September 26, 2026 13:12
@ChrisRackauckas

Copy link
Copy Markdown
Member

🤖 Automated review from an AI agent running as @ChrisRackauckas — not written or reviewed by Chris. It is posted so that you can act on it. Chris or a maintainer may disagree.

Verdict: changes needed before merge (reviewed at e96782f).

The PR fixes a real regression from #4560. Explicit-first-stage SDIRK methods (TRBDF2, KenCarp*, Kvaerno*) crash on any scalar mass matrix (λ*I or ScalarOperator) because the code calls diag(M). The fix reads the scalar instead, and the existing regression test goes red → green locally. The verdict is CHANGES for one reason: SDIRK now imports two non-public, _-prefixed internals of OrdinaryDiffEqDifferentiation (_is_scalar_massmatrix, _scalar_massmatrix_λ), and SDIRK's own ExplicitImports QA check flags both.

Risk assessment

  • Risk: low
  • Blast radius: Only the explicit-first-stage paths of the generic ESDIRK/IMEX perform_step when a mass matrix is present.
    • The new branch runs only when _is_scalar_massmatrix(M) is true, and that case throws on master anyway.
    • The diagonal path (diag(M)) is unchanged, except that it is now also computed at the end of the step. That means one extra diag allocation per step for Diagonal mass matrices.
    • The SDIRK compat floor rises to OrdinaryDiffEqDifferentiation 3.12.4, which is not registered.
  • Evidence:
    • CI on head e96782f (checked 14:55Z): 176 pass, 8 fail, 7 pending, 1 skipping.
    • Local runs, Julia 1.12, scratch envs with the local lib/ packages developed. Logs are in work/…/tmp/*_v2.log.
      • Target test: lib/OrdinaryDiffEqDifferentiation/test/scalar_operator_massmatrix_tests.jl.
        • Base 1cbe52c: 5 pass, 1 error, MethodError: no method matching diag(::ScalarOperator{…}).
        • Head e96782f: 9 of 9 pass.
      • Wider coverage script: M u' = -u, tol 1e-10, with M = 2.0*I, ScalarOperator(2.0), and ScalarOperator with update_func = 1+t. Algorithms TRBDF2, KenCarp4, Kvaerno5, each iip and oop.
        • Base: all 18 cases throw MethodError(diag, …). 2.0*I is broken too, not only ScalarOperator.
        • Head: 12 of 18 solve. Max error against the exact solution at t=1 is 3e-10 to 6e-6. The time-varying case gets 6e-6 (TRBDF2) and 1e-8 (KenCarp4, Kvaerno5), so update_func is honoured.
        • The other 6 are out-of-place ScalarOperator cases. They now fail with a different error in NLNewton's W construction ("Out-of-place NLNewton does not support non-concrete W"), outside this PR's code.
        • IMEX SplitODEProblem + KenCarp4 + 2.0*I goes from a MethodError to MaxIters. The earlier review found a dense Matrix(2.0I) does the same on base, so this gap is pre-existing.
      • SDIRK test/qa/qa.jl, run directly on the head: 19 pass, 1 error. all_explicit_imports_are_public flags three names:
        • _is_scalar_massmatrix, from OrdinaryDiffEqSDIRK.jl:39: new in this PR.
        • _scalar_massmatrix_λ, from OrdinaryDiffEqSDIRK.jl:40: new in this PR.
        • stage_predictor, from :42: pre-existing, and flagged on base too.
      • Formatting and spelling: Runic --check --diff on the 3 changed .jl files exits 0 with no diff, and typos on the diff exits 0.
      • I did not run the SDIRK Core suite locally, because it was too slow on this loaded machine. CI runs it on Julia 1, lts and pre, and it passes.
    • Tests in the PR: none. The regression test already exists (added with Fix jacobian2W! DimensionMismatch for ScalarOperator mass matrix (#3915) #3916) and fails on master, so the fail-before / pass-after requirement is met by that test.
  • Merge: needs changes (non-public cross-package imports, finding 1). Also, Differentiation 3.12.4 has to be registered before or together with SDIRK 2.9.8.

Findings

  1. Non-public cross-package imports. lib/OrdinaryDiffEqSDIRK/src/OrdinaryDiffEqSDIRK.jl:39-40 imports _is_scalar_massmatrix and _scalar_massmatrix_λ from OrdinaryDiffEqDifferentiation.

    • Neither name is in Differentiation's public list (lib/OrdinaryDiffEqDifferentiation/src/OrdinaryDiffEqDifferentiation.jl:100-113).
    • SDIRK's all_explicit_imports_are_public check flags both, as shown above.
    • The header of lib/OrdinaryDiffEqSDIRK/test/qa/qa.jl:1-11 says non-public sibling imports were deliberately removed, and each remaining one is listed with a reason.
    • The exact-patch compat floor (3.12.4) is a symptom: SDIRK now depends on an internal method signature that was added in a patch release.
    • Two ways to fix it:
      • Declare both names public, with docstrings, next to jacobian2W! in the solver-author API list.
      • Or read the scalar locally in SDIRK's _mmdiag: mm isa UniformScaling ? mm.λ : convert(Number, update_coefficients(mm, u, p, t)), dispatching on ScalarOperator. This also lets the SDIRK compat floor go back down.

    Don't just add the names to the ignore list.

  2. Pre-existing, not caused by this PR: SDIRK's QA group never runs QA. lib/OrdinaryDiffEqSDIRK/test/runtests.jl:13-15 activates the qa env under GROUP=QA but never includes qa/qa.jl. So the lib/OrdinaryDiffEqSDIRK [QA] lane just runs the Core tests and passes, and that is why CI misses finding 1. Worth fixing in a separate small PR. Once it is enabled, the check will also report the existing stage_predictor import.

  3. Registration order. lib/OrdinaryDiffEqSDIRK/Project.toml:50 raises the floor to OrdinaryDiffEqDifferentiation = "3.12.4", which is correct if finding 1 is fixed by making the names public. Because of that floor, lib/OrdinaryDiffEqIMEXMultistep [QA] stays unsatisfiable until Differentiation 3.12.4 is registered.

    • Register Differentiation first, then SDIRK.
    • Both version bumps are patch bumps, which is right.
    • The head currently bumps SDIRK to 2.9.8. Master may take that number first, which would force another rebase.
  4. Minor: comment density. 7 of the 25 added lines are comments, about 28%: lib/OrdinaryDiffEqSDIRK/src/generic_imex_perform_step.jl:60-62, 1363-1364 and lib/OrdinaryDiffEqDifferentiation/src/derivative_utils.jl:611. The three-line _mmdiag comment could be one line, or could fold into the block comment above it (lines 40-52), which already explains the elementwise contract.

  5. Minor: extra allocation per step for Diagonal mass matrices. generic_imex_perform_step.jl:1365 and :2448 now call _mmdiag again at the end of the step. For a non-scalar M that is a second diag(M) allocation per step, where before the stage-1 mmd was reused. Recomputing only when _is_scalar_massmatrix(M) holds, and otherwise reusing mmd, avoids it.

  6. Optional test gap. The regression test exercises only in-place TRBDF2. An out-of-place λ*I case would guard the separate _perform_step_oop! edits at lines 1442 and 2448. That case passes on the head and fails on base with the diag MethodError.

Push a fix and the PR is reviewed again automatically at the new head.


🤖 Posted by an AI agent — harness: Claude Code · model: claude-opus-5-5[1m] (fleet master); review by Devin CLI (Mac) / fusion-claude-opus-5-5-high-sidekick-swe-2-medium
Conversation: local Claude Code session 3cd6500a-1f81-46b5-ac0b-c466e15b6a53 on Chris's Mac (session ID, no URL)

@singhharsh1708
singhharsh1708 force-pushed the sdirk-scalar-operator-mass-matrix branch from e96782f to d97ba63 Compare September 28, 2026 03:30
@singhharsh1708

Copy link
Copy Markdown
Member Author

Took the second option: _mmdiag dispatches on UniformScaling and ScalarOperator inside SDIRK, the Differentiation change and compat floor are gone, SDIRK is 2.9.9, and qa.jl passes 18/18.

# Conflicts:
#	lib/OrdinaryDiffEqSDIRK/Project.toml
@ChrisRackauckas

Copy link
Copy Markdown
Member

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

Independent review (Devin CLI 3000.11.3, model fusion-claude-opus-5-5-high-sidekick-swe-2-medium): REJECT, risk low.

Full review

VERDICT: REJECT
RISK: low

Blocking findings

  1. Superseded by master, and now conflicting. lib/OrdinaryDiffEqSDIRK/src/generic_imex_perform_step.jl:58-65,129,1363,1440,2446 and lib/OrdinaryDiffEqSDIRK/src/OrdinaryDiffEqSDIRK.jl:36-37.
    • Commit eaf3d5a (Fix four master CI reds: Rosenbrock32 DAE QA, DiffEqBase 1.11 erasure test, ScalarOperator mass matrix in SDIRK, GlobalDiffEq erased callback #4780, merged to master 2026-10-11 06:18 UTC) already fixes the same bug in the same function. It is the "Handle scalar mass matrices in the explicit-first-stage FSAL conversion" and "Evaluate a time-varying mass matrix at each FSAL conversion's own point" commits.
    • Master's version reads λ for UniformScaling. For a SciMLOperator it calls update_coefficients (taken from SciMLBase, so there is no new dependency) at (uprev, t) for stage 1 and at (u, t+dt) for the FSAL. For a plain AbstractMatrix it reuses the stage-1 mmd.
    • This PR's merge-base is 65f5e50, which is older than that commit.
    • Confirmed by running:
      • gh pr view 4582 --json mergeable returns CONFLICTING / DIRTY.
      • git merge-tree --write-tree origin/master HEAD reports content conflicts in both OrdinaryDiffEqSDIRK.jl and generic_imex_perform_step.jl.
    • Confirmed by running: I ran the same probe script (scratch/probe.jl plus scratch/probe_sup.jl) against the PR head and against origin/master b1d7885. The probe covers 72 adaptive solves, 6 fixed-step order sweeps, IMEX split solves, and non-scalar operator cases.
      • The output is byte-identical on every row except the allocation line.
      • scalar_operator_massmatrix_tests.jl gives 9/9 on both.
    • So the PR adds no behaviour that master lacks.
    • Required change: close as superseded by Fix four master CI reds: Rosenbrock32 DAE QA, DiffEqBase 1.11 erasure test, ScalarOperator mass matrix in SDIRK, GlobalDiffEq erased callback #4780. Rebasing would just resolve the conflicts back to master's code, leaving only a version bump and an unused SciMLOperators dependency.

Non-blocking findings

These only matter if the PR were kept alive.

  1. An extra allocation per step for Diagonal mass matrices, which master avoids. Confirmed by running. At lines 1363 and 2446 the PR calls _mmdiag again at step end, so diag(M) allocates a second vector every step.
    • TRBDF2 iip, Diagonal([2.0,2.0]), dt=1e-3, 100 step! calls: PR head 896 B/step, master 816 B/step, base 816 B/step.
  2. New direct dependency SciMLOperators (Project.toml:23,54).
    • It is MIT licensed and already a transitive dependency, so there is no licensing issue.
    • It is unnecessary, because update_coefficients is reachable through SciMLBase, which is what master uses.
  3. PR claims that did not reproduce here.
    • The author's comment says "SDIRK is 2.9.9". At head db794dd, Project.toml:4 is version = "2.9.8", and master is still at 2.9.7.
    • The body says "qa.jl passes 18 of 18". Running lib/OrdinaryDiffEqSDIRK/test/qa/qa.jl at the head gave 19 pass, 1 error: all_explicit_imports_are_public flags stage_predictor (OrdinaryDiffEqSDIRK.jl:42). That import predates this PR (the earlier review saw the same flag on base), so the error is not caused by the PR. The claimed count is still wrong.
  4. No test added. The PR relies on the existing scalar_operator_massmatrix_tests.jl, which covers only in-place TRBDF2. The out-of-place 2.0*I path at lines 1440/2446 has no regression test, on master either.
  5. Pre-existing problems seen while probing. They are identical on master and, where it runs, on base. None is caused by this PR, and each would be a separate issue:
    • Out-of-place solves with a ScalarOperator mass matrix fail in NLNewton ("does not support non-concrete W").
    • MatrixOperator(Diagonal(...)) and DiagonalOperator mass matrices with TRBDF2 throw MethodError: diag(::MatrixOperator) (master too).
    • Suspicious result: SplitODEProblem with KenCarp4, f1 = f2 = -u/2 and M = 2I returns Success with error 4.17e-1 against exp(-1/2)·u0. That is identical for 2.0*I, ScalarOperator(2.0) and Diagonal, and Diagonal on base gives the same 4.17e-1. I did not root-cause it.
    • Suspicious result: fixed-step Kvaerno5 with M = (1+t)I shows observed order 2.94/2.97/2.99, against 4.98/4.99 for constant M = 2I. TRBDF2 (2.0) and KenCarp4 (4.0) keep their order. I did not determine whether the FSAL conversion or the nonlinear-solve residual's mass-matrix time causes this.

What I ran

All runs used Julia 1.12, with separate scratch envs that Pkg.develop the tree's lib/{OrdinaryDiffEqSDIRK,OrdinaryDiffEqCore,OrdinaryDiffEqDifferentiation,OrdinaryDiffEqNonlinearSolve,DiffEqBase}. I checked pathof in each run. Logs are in scratch/logs/.

  • git diff origin/master...HEAD, git show eaf3d5a5d, git merge-tree against origin/master, and gh pr view --json mergeable.
  • scalar_operator_massmatrix_tests.jl:
    • base: 5 pass, 1 error (diag(::ScalarOperator)).
    • head: 9/9.
    • master: 9/9.
  • scratch/probe.jl on all three trees: M u' = -u with M ∈ {2.0*I, ScalarOperator(2.0), ScalarOperator with update_func 1+t, Diagonal}, algorithms {TRBDF2, KenCarp4, Kvaerno5, ESDIRK436L2SA2, Cash4, SDIRK2}, iip and oop, tol 1e-10, compared against the exact solution.
    • Base: every explicit-first-stage method with a scalar M throws a diag MethodError.
    • Head and master: in-place solves succeed with errors of 3e-11 to 2e-6, consistent with the tolerance and method order.
  • scratch/probe_sup.jl: fixed-step order sweep at dt = 1/8 to 1/64 (numbers above). Head and master are identical; base errors.
  • At head: lib/OrdinaryDiffEqSDIRK/test/dae_esdirk_test.jl gave 78/78, and test/qa/qa.jl gave 19 pass, 1 error (pre-existing stage_predictor).
  • I killed every Julia process I started. The ones still running belong to the parallel review of Fix the stage times of Kvaerno3/4/5, SSPSDIRK2 and ESDIRK547L2SA2 #4730.

What I did not verify

  • The SDIRK Core test suite (convergence, tableau, predictor) at head. I skipped it because of host limits, and the change is moot given finding 1.
  • The root cause of the IMEX-split error and of the Kvaerno5 order drop with time-varying M (non-blocking 5).
  • Behaviour on Julia lts and pre, and the CI status at this head.

Links


🤖 Posted by an AI agent — harness: Devin CLI 3000.11.3 (review), Claude Code 2.1.285 (posting) · model: fusion-claude-opus-5-5-high-sidekick-swe-2-medium
Conversation: local Claude session 3c311569-dda6-4687-bc9f-65febb212c33 (fleet master)

@singhharsh1708

Copy link
Copy Markdown
Member Author

Superseded by #4780.

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.

2 participants