Skip to content

Fix VelocityVerletCache tmp field typing for unitful state - #4602

Merged
ChrisRackauckas merged 2 commits into
SciML:masterfrom
ChrisRackauckas-Claude:fix-1397-velocityverlet-units
Sep 25, 2026
Merged

ChrisRackauckas merged 2 commits into
SciML:masterfrom
ChrisRackauckas-Claude:fix-1397-velocityverlet-units

Conversation

@ChrisRackauckas-Claude

@ChrisRackauckas-Claude ChrisRackauckas-Claude commented Sep 23, 2026 •

Copy link
Copy Markdown
Member

VelocityVerletCache.tmp was declared uType but holds rate-typed buffers: alg_cache builds it from zero(rate_prototype) and alloc_symp_state uses cache.tmp.x as the kdu/ku derivative buffers. With Unitful initial conditions uType != rateType, so the parametric inner constructor had no matching method and solve threw a MethodError (#1397); the k/tmp arguments were also swapped relative to the field order (harmless only because both were zero(rate_prototype)). The field is now rateType and arguments are passed in field order. Unitful is added as a test-only dependency for the regression test, matching the precedent in lib/DelayDiffEq.

Verification

New testset in lib/OrdinaryDiffEqSymplecticRK/test/symplectic_tests.jl ("VelocityVerlet with units iip=true/false"), run in a scratch env dev'ing this branch.

Failing on unfixed code (src change stashed):

MethodError: no method matching OrdinaryDiffEqSymplecticRK.VelocityVerletCache(
  ::ArrayPartition{Quantity{Float64}, ...𝐋 𝐓^-1... , ...𝐋...},   # u, uprev
  ::ArrayPartition{Quantity{Float64}, ...𝐋 𝐓^-2... , ...𝐋 𝐓^-1...}, ...) # rate args
Closest candidates:
  VelocityVerletCache(::uType, ::uType, !Matched::uType, ::rateType, ::rateType, ::uEltypeNoUnits)
Test Summary:                      | Error  Total  Time
VelocityVerlet with units iip=true |     1      1  7.8s
ERROR: Some tests did not pass: 0 passed, 0 failed, 1 errored, 0 broken.

Passing with the fix (now including a constant-acceleration case dv .= [-9.81, 0] m/s^2 checked against the analytic x(t) = x0 + v0*t + g*t^2/2, exact for VelocityVerlet — expected x(T) = [-49000.0, 0.1] m, v(T) = [-980.5, 0] m/s at T = 100 s):

Test Summary:                      | Pass  Total   Time
VelocityVerlet with units iip=true |    4      4  35.0s
Test Summary:                       | Pass  Total  Time
VelocityVerlet with units iip=false |    4      4  14.2s

Test group run: cd lib/OrdinaryDiffEqSymplecticRK && GROUP=Core julia --project=. -e 'using Pkg; Pkg.test()' — tail:

Test Summary:                | Pass  Broken  Total     Time
Synplectic Convergence Tests |   78       4     82  5m07.6s
Test Summary:    | Pass  Total      Time
Synplectic Tests |  164    164      40m33.0s
     Testing OrdinaryDiffEqSymplecticRK tests passed

(The 4 broken are the pre-existing @test_broken McAte42 cases.) Runic: no changes needed on the touched files. typos: clean.

NOT verified

  • GROUP=QA pieces: Allocation Tests passed (18 @test_broken, 0 failures); JET test_package(..., mode=:typo) 1/1 pass on this branch. The suite-level run died on EMFILE (file-descriptor exhaustion on the shared CI host) before the Aqua lane; SciMLTesting run_qa passed 20/20 on the sibling branch Fix tmp field typing in remaining symplectic caches for unitful state #4603 which carries the same change pattern in the same file.
  • The same tmp::uType + swapped-constructor-args pattern existed in LeapfrogDriftKickDriftCache, VerletLeapfrogCache, and SymplecticGenericCache in the same file — fixed separately in Fix tmp field typing in remaining symplectic caches for unitful state #4603 per review request.
  • No check of downstream packages or of symbolic/DynamicalODEFunction cases beyond SecondOrderODEProblem.

What a reviewer should push back on

  • Whether tmp should stay semantically "u-typed scratch" for get_tmp_cache/callback signatures. Note the field already held a rateType object before this change (the k argument landed in the tmp slot), so the retype only makes the declared type match actual contents; behavior is unchanged.
  • Whether the sibling caches listed above should be fixed in this PR rather than a follow-up.
  • Adding Unitful as a test-only dep to this sublibrary ([extras] + [targets] + compat 1.25.1, mirroring DelayDiffEq).

Fixes #1397

Please ignore this draft until reviewed by @ChrisRackauckas.

Risk assessment

  • Risk: low
  • Blast radius: VelocityVerletCache in OrdinaryDiffEqSymplecticRK only: the field type of tmp (uType → rateType) and the constructor argument order. For unitless problems uType == rateType and the field already held a rate-typed object, so numerics are unchanged. No public API change. Unitful is added as a test-only dependency.
  • Evidence: the new Unitful testset fails before the fix with the issue's MethodError and passes after. It checks against the analytic solution, including a constant-acceleration case. GROUP=Core of the sublibrary passes. Runic and typos are clean.
  • Independent review: Claude Code (Opus 5.5) rated it low with high confidence: a 2-line typing fix, fail-before/pass-after reproduced independently, and non-unit results unchanged. Verdict MERGE. Its request for a constant-acceleration test was added afterwards. A re-review of that commit is still running.
  • Merge: auto-merge candidate once CI is green and the re-review of the added test is clean. The same bug in sibling caches is fixed separately in Fix tmp field typing in remaining symplectic caches for unitful state #4603.

🤖 Generated with Devin CLI (model: swe-2-high), transcript /home/crackauc/sandbox/goals/issue-backlog/jobs/fix-OrdinaryDiffEq.jl-1397/log.txt on amdci2.julia.csail.mit.edu

Risk assessment and review coordination by 🤖 Claude Code (model: claude-opus-5-5[1m]), https://claude.ai/code/session_01NdFVhANTF4nepTbqYxjfRP


Independent review (Claude Code cloud / claude-opus-5-5)

VERDICT: MERGE

(no risk section)

Full review

VERDICT: MERGE

Risk assessment

  • Risk: low
  • Blast radius: only VelocityVerletCache in lib/OrdinaryDiffEqSymplecticRK.
    The PR changes the declared type of the tmp field and the order of the
    constructor arguments, and adds Unitful as a test-only dependency. Nothing
    public changes, so no SemVer concern.
  • Evidence:
    • With the old signature (u::uType, uprev::uType, tmp::uType, k::rateType,
      ...), the old call passed k (a rate-typed zero) into the tmp slot. Julia's
      default parametric constructor does not convert arguments. So any problem
      where uType != rateType already failed with a MethodError.
    • When uType == rateType, the new struct gets the same concrete type. It is
      built from two identical zero(rate_prototype) objects, only in a different
      order.
    • So the PR cannot change behavior for any input that worked before. It only
      makes the previously failing case work.
    • The PR body pastes the new test failing before the fix and passing after.
      Sublibrary CI is green for Core (Julia 1, lts, pre), QA and downgrade.
  • Independent review: this review.
  • Merge: needs review.

Blocking findings

None.

Non-blocking findings

  1. tmp is now a rate-typed buffer. If get_tmp_cache or a callback treats
    cache.tmp as u-shaped scratch, a Unitful u would get a buffer with the
    wrong units. This is not a regression, because the unitful case never ran
    before and unitless storage is unchanged. It is worth a follow-up check of
    what get_tmp_cache returns for this cache.
  2. The test does not catch the k/tmp swap. Both are zero(rate_prototype), so
    the argument reorder is only checked by the fact that the code compiles.
    That is acceptable, since the swap never changed values.
  3. Tolerances are not fitted to observed error. The tests use the default ≈
    (relative tolerance √eps) against analytic solutions. The
    constant-acceleration case (VelocityVerlet is exact there, up to rounding
    over 1000 steps) does check the update formula, not just cache
    construction.
  4. Compat. The Unitful 1.25.1 compat and the [targets] entry are test-only.
    They do not constrain downstream resolution. The downgrade job for
    lib/OrdinaryDiffEqSymplecticRK passed, so the lower bound resolves.
  5. Scope. The same bug in the sibling caches (LeapfrogDriftKickDriftCache,
    VerletLeapfrogCache, SymplecticGenericCache) is left to Fix tmp field typing in remaining symplectic caches for unitful state #4603. Splitting it
    out is the right call. This PR fixes Second order ODE errors with Unitful units #1397 only for VelocityVerlet.
  6. PR body. It contains a local transcript path on a CI host and process
    narrative. This is cosmetic.

Red CI: SciMLSensitivity Core1/Core3, downgrade
DiffEqBase/OrdinaryDiffEqDifferentiation

  • Probably not caused by this PR. From the argument above, the only runtime
    change is for inputs with uType != rateType on VelocityVerlet, which used to
    throw. A downstream test could only turn red if it:

    • (a) expected that MethodError, which is implausible, or
    • (b) now gets further on a VelocityVerlet solve with non-matching u/rate
      types (for example tracked or dual types in the adjoint path) and fails
      somewhere later.

    Scenario (b) is possible in principle, but that test would have been
    erroring on master too, so it would not be green there.

  • The untouched lanes are also red. Core1 is red as well. So are downgrade
    DiffEqBase and OrdinaryDiffEqDifferentiation, which this PR does not touch;
    that points to base or environment drift rather than this diff. Core3's
    runtime (26m) is close to Core4's green 25m52s, so it does not look like a
    hang.

  • Evidence that would settle it:
    a. The failing testset name and error from the Core3 log (job 107223530580
    (https://github.com/SciML/OrdinaryDiffEq.jl/actions/runs/35873400912/job/
    107223530580)). Check whether it mentions VelocityVerlet, SymplecticRK or
    SecondOrderODEProblem.
    b. The same Core3 job on master at this PR's base commit, run in the same
    time window. Resolved SciMLSensitivity, Zygote and Enzyme versions are
    the usual cause of drift.
    c. A rerun of Core3 on this PR. If it is still red, run Core3 locally with
    this branch dev'd, then again with the two src lines reverted, and
    compare.

What I checked

  • The diff against the struct's field order and the parametric constructor
    semantics.
  • That the MethodError in the PR body matches the old signature.
  • Test physics: x(T) = x0 + v0T + gT²/2, v(T) = v0 + g*T. For
    SecondOrderODEProblem, x[1] is velocity and x[2] is position.
  • The OOP accel_units returns m/s² (1/s .* m/s).
  • No @test_broken, @test_skip or loosened tolerances were added.
  • The Project.toml changes are test-only.
  • The check list: every lane touching SymplecticRK is green.

What I did not verify

  • I ran nothing locally and did not read any CI logs.
  • The IIPS constant and the tmp = zero(rate_prototype) line are outside the
    diff hunks.
  • get_tmp_cache and callback behavior with Unitful.
  • The root cause of the SciMLSensitivity Core1/Core3 and downgrade
    DiffEqBase/Differentiation failures.
  • The Aqua lane locally. The PR says it died on EMFILE, but the CI QA lane
    passed.

Links


🤖 Posted by an AI agent — orchestrator: Devin Sidekick on behalf of @ChrisRackauckas · reviewer: Claude Code cloud / claude-opus-5-5
Conversation: cloud review session; transcript ~/sandbox/fleet-master/jobs/carry/cloud-out-4602.txt on Chris's Mac

ChrisRackauckas and others added 2 commits September 23, 2026 08:48
The tmp field of VelocityVerletCache was declared as uType but holds
rate-typed buffers: alg_cache built it from zero(rate_prototype) and
alloc_symp_state uses cache.tmp.x as the kdu/ku derivative buffers in
perform_step!. With Unitful initial conditions uType != rateType, so the
parametric inner constructor had no matching method and solve() threw a
MethodError. The k/tmp arguments were also swapped relative to the field
order, which happened to be harmless only because both were
zero(rate_prototype).

The field is now typed rateType and the constructor arguments are passed
in field order. Unitful is added as a test-only dependency for a
SecondOrderODEProblem regression test covering both the in-place and
out-of-place paths.

Fixes SciML#1397

Co-Authored-By: Chris Rackauckas <[email protected]>
Co-Authored-By: Devin <[email protected]>
Agent-Harness: Devin CLI
Agent-Model: swe-2-high
Agent-Session: local session, transcript at /home/crackauc/sandbox/goals/issue-backlog/jobs/fix-OrdinaryDiffEq.jl-1397/log.txt on amdci2.julia.csail.mit.edu
The zero-acceleration case only exercises the cache construction, not
the update formula. Add a gravity case (dv .= [-9.81, 0] m/s^2) checked
against the analytic x(t) = x0 + v0*t + g*t^2/2, which VelocityVerlet
integrates exactly for constant acceleration.

Co-Authored-By: Chris Rackauckas <[email protected]>
Co-Authored-By: Devin <[email protected]>
Agent-Harness: Devin CLI
Agent-Model: swe-2-high
Agent-Session: local session, transcript at /home/crackauc/sandbox/goals/issue-backlog/jobs/fix-OrdinaryDiffEq.jl-1397/log.txt on amdci2.julia.csail.mit.edu
@ChrisRackauckas
ChrisRackauckas marked this pull request as ready for review September 25, 2026 05:01
@ChrisRackauckas
ChrisRackauckas merged commit 7e9117f into SciML:master Sep 25, 2026
140 of 145 checks passed
ChrisRackauckas referenced this pull request Sep 29, 2026
* Bump DiffEqBase to 7.21.3

Co-Authored-By: Chris Rackauckas <[email protected]>
Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Agent-Harness: Devin CLI 3000.11.3 (Mac)
Agent-Model: Fusion (claude-opus-5-5 medium + swe-2 medium)
Agent-Session: local transcript ~/.local/share/devin/cli/transcripts/helpful-exception.json

* Bump OrdinaryDiffEqBDF to 2.4.12

Co-Authored-By: Chris Rackauckas <[email protected]>
Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Agent-Harness: Devin CLI 3000.11.3 (Mac)
Agent-Model: Fusion (claude-opus-5-5 medium + swe-2 medium)
Agent-Session: local transcript ~/.local/share/devin/cli/transcripts/helpful-exception.json

* Bump OrdinaryDiffEqSDIRK to 2.9.7

Co-Authored-By: Chris Rackauckas <[email protected]>
Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Agent-Harness: Devin CLI 3000.11.3 (Mac)
Agent-Model: Fusion (claude-opus-5-5 medium + swe-2 medium)
Agent-Session: local transcript ~/.local/share/devin/cli/transcripts/helpful-exception.json

* Bump OrdinaryDiffEqRosenbrock to 2.7.5

Co-Authored-By: Chris Rackauckas <[email protected]>
Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Agent-Harness: Devin CLI 3000.11.3 (Mac)
Agent-Model: Fusion (claude-opus-5-5 medium + swe-2 medium)
Agent-Session: local transcript ~/.local/share/devin/cli/transcripts/helpful-exception.json

* Bump OrdinaryDiffEqLowOrderRK to 2.2.6

Co-Authored-By: Chris Rackauckas <[email protected]>
Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Agent-Harness: Devin CLI 3000.11.3 (Mac)
Agent-Model: Fusion (claude-opus-5-5 medium + swe-2 medium)
Agent-Session: local transcript ~/.local/share/devin/cli/transcripts/helpful-exception.json

* Bump OrdinaryDiffEqTsit5 to 2.1.5

Co-Authored-By: Chris Rackauckas <[email protected]>
Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Agent-Harness: Devin CLI 3000.11.3 (Mac)
Agent-Model: Fusion (claude-opus-5-5 medium + swe-2 medium)
Agent-Session: local transcript ~/.local/share/devin/cli/transcripts/helpful-exception.json

* Bump OrdinaryDiffEqVerner to 2.4.2

Co-Authored-By: Chris Rackauckas <[email protected]>
Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Agent-Harness: Devin CLI 3000.11.3 (Mac)
Agent-Model: Fusion (claude-opus-5-5 medium + swe-2 medium)
Agent-Session: local transcript ~/.local/share/devin/cli/transcripts/helpful-exception.json

* Bump OrdinaryDiffEqSymplecticRK to 2.2.3

Co-Authored-By: Chris Rackauckas <[email protected]>
Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Agent-Harness: Devin CLI 3000.11.3 (Mac)
Agent-Model: Fusion (claude-opus-5-5 medium + swe-2 medium)
Agent-Session: local transcript ~/.local/share/devin/cli/transcripts/helpful-exception.json

---------

Co-authored-by: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.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.

Second order ODE errors with Unitful units

2 participants