Skip to content

TSRKC3 method - #3420

Merged
ChrisRackauckas merged 14 commits into
SciML:masterfrom
MoisaAndrew:master
Apr 15, 2026
Merged

ChrisRackauckas merged 14 commits into
SciML:masterfrom
MoisaAndrew:master

Conversation

@MoisaAndrew

Copy link
Copy Markdown
Contributor

Checklist

  • [+] Appropriate tests were added
  • [+] Any code changes were done in a way that does not break public API
  • [+] All documentation related to code changes were updated
  • [+] The new code follows the
    contributor guidelines, in particular the SciML Style Guide and
    COLPRAC.
  • [+] Any new documentation only uses public API

Additional context

Add any other context about the problem here.
Filament


### Recommended stabilized methods

- **`ROCK2`**: Second-order ROW-type stabilized method with extended stability

@MoisaAndrew MoisaAndrew Apr 13, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes to consider. I was not able to run multiple tests due to conflicts between dependencies. You can revert these changes.

There were several bugs in RKC implementation, this is the reason why it was loosing against ROCK2.
Now RKC is at least similar to ROCK2, but more stable and robust (see https://doi.org/10.1007/978-3-662-09017-6).

TSRKC3 is more effecient than RKC for moderate accuracy (in most real-life cases) while ROCK4 is very unstable and almost always slower than either CVODE_BDF or TSRKC3.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like even in your WP-diagram that RKC is outperformed by ROCK2? ROCK2 should probably still be one of the recommended. But yes TSRKC3 looks pretty good.

@MoisaAndrew MoisaAndrew Apr 14, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ROCK2 usually wins a few percents of the execultion time against RKC. However, there are two conditions:

  1. 200 stages of ROCK2 is enough to archieve stability without a step size decrease.
  2. The order reduction does not occur.

So the dilemma is would you like to take a risk in order to win 3% with a possibility to loose 50?
(see https://doi.org/10.1007/978-3-662-09017-6 page 437)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wouldn’t want to draw conclusions from a single test, so maybe we just should wait for the community tests.

alg.eigen_est === nothing ? maxeig!(integrator, cache) : alg.eigen_est(integrator)
# The the number of degree for Chebyshev polynomial
#maxm = max(2,Int(floor(sqrt(integrator.opts.internalnorm(integrator.opts.reltol,t)/(10*eps(integrator.opts.internalnorm(uprev,t)))))))
maxm = 50

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This limitation was removed due to the following reasons:

  1. The function dtnew_modification(integrator, alg::ROCK2, dtnew), which should recalculate the step size if mdeg was updated by maxm, is not implemented.
  2. The original paper (https://doi.org/10.1016/S0377-0427(97)00219-7) and the code (https://www.netlib.org/ode/rkc.f) contains the value of maxm: sqrt(rtol / (10. * uround)). I've never seen it reached in contrast to the hardcoded value 50. This value is more theoretical, RKC-type methods gain stability with greater number of internal stages (in contrast to ROCK family).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay that's a reasonable change.

# The number of degree for Chebyshev polynomial
mdeg = floor(Int, sqrt(1.54 * abs(dt) * integrator.eigen_est + 1)) + 1

w0 = 1 + 2 / (13 * (mdeg^2))

@MoisaAndrew MoisaAndrew Apr 13, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

w0 has Float64 type now. That is why this method cannot be used on GPU. The reason why I have not fixed this is because with the type Float32 the values of w0 and the variables below will be too inaccurate. I need more time to choose the right way.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We might as well just make this constant be a rational so the method is generic? It might not be a good method to use with Float32 but I don't see a reason to block the implementation to allow testing and benchmarking.

OrdinaryDiffEqCore.increment_nf!(integrator.stats, 1)
# error estimate
if integrator.opts.adaptive
tmp = 0.8 * (uprev - u) + 0.4 * dt * (fsalfirst + gprev)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There was a bug in the estimator

if (iter < mdeg)
gprev2 = gprev
gprev = u
@.. broadcast = false gprev2 = gprev

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the reason why RKC was rubbish: it just did not work correctly.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh thanks, is there a test that catches this? Seems odd it would still converge at higher order? I guess order 2 is just relatively easy to hit and usually higher order methods hit order 1 or 2 when they have a bug like this.

@MoisaAndrew MoisaAndrew Apr 14, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the reason is that the current convergence tests ODEs are nonstiff, mdeg is always 2 and the code inside this if statement is never achieved

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we really need to stop using linear problems for convergance testing

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think the linear problems are unsuitable, but we need to force stabilized methods to use more stages using eigen_est parameter. I am going to update the test.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah linear can be stiff, that's separate. But https://github.com/SciML/DiffEqProblemLibrary.jl/blob/master/lib/ODEProblemLibrary/src/nonlinchem.jl is probably a good test case. I think we should generally be moving to this, + a stiff case, + a time-dependent case for everything. It would be great to just extend that nonlinchem problem to cover all 3 with a variable parameter, it's probably not too hard to derive a variation with + t in there. But that's a whole separate point and should wait for the v7.

@ChrisRackauckas

Copy link
Copy Markdown
Member

Thanks! It would be good to go back through with a fine-toothed comb the SERK methods as well, since they have always underperformed and I wondered if there was some improvements to the error estimators (or bug fixes) required there.

Comment thread src/OrdinaryDiffEq.jl Resolved

println("ROCKC")
sim = test_convergence(dts, prob, RKC())
println("RKC")

@MoisaAndrew MoisaAndrew Apr 14, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The old RKC code fails now

@@ -55,9 +55,26 @@ end
sim = test_convergence(dts, prob, ROCK4(min_stages = 21))

@MoisaAndrew MoisaAndrew Apr 14, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, the ROCK4 convergence troubles with greater number of stages could be seen here. If you put min_stages = 100 you get

Runge-Kutta-Chebyshev Convergence Tests: Test Failed
  Expression: ≈(sim.𝒪est[:l∞], 4, atol = testTol)
   Evaluated: 3.3838195804669873 ≈ 4 (atol=0.1)

In contrast, RKC and TSRKC3 pass this test even with thousands of stages.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting, a floating point error thing? ROCK4 was tested against the original Fortran code so it should be pretty much 1-1 there, but I'm curious if that's a bug or a numerical thing. If you plot(sim) from the convergence does it flatline?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nevermind, this time it just achieved the highest possible accuracy
image

Comment thread test/regression/iipvsoop_tests.jl Resolved
@ChrisRackauckas

Copy link
Copy Markdown
Member

The failures are just from the CI machine's disk filling yesterday, so I'll pull this in and handle the random https://github.com/SciML/OrdinaryDiffEq.jl/actions/runs/24400891421/job/71282844187?pr=3420 to make the static analyzer happy.

I don't know about the suggestions change right now because I need to see it in action, we'll need to bump the SciMLBenchmarks and see how TSRKC3 performs across the board. But we can always come back to that. My guess is that ROCK2 is added back and that's the only change, I'd have to empirically see RKC do a lot better than ROCK2 (which historically has been the most robust of these methods) before pulling ROCK2 from the list. Not saying I don't believe it, I mean this kind of change will really change RKC and I'll be curious to see how the benchmarks look, but pulling what was the best method of this type in almost all benchmarks completely off the suggestion list is a very bold statement that needs evidence, so let's bump the benchmarks and see if that evidence arrives.

@ChrisRackauckas
ChrisRackauckas merged commit 023d7c3 into SciML:master Apr 15, 2026
86 of 92 checks passed
ChrisRackauckas-Claude pushed a commit to ChrisRackauckas-Claude/OrdinaryDiffEq.jl that referenced this pull request Apr 15, 2026
…ly import

The package imports SVector from StaticArraysCore rather than full
StaticArrays, which only supplies the tuple-form constructor and
leaves out the varargs constructor and getindex. In the isolated QA
test environment (no transitive StaticArrays) this surfaced two
distinct failures:

1. SERK2, ESERK4, ESERK5 cache construction failed because the
   struct fields ms/Cᵤ/Cₑ were typed as SVector{N, Int} but the
   tableaus returned plain Vector{Int}, and no Vector -> SVector
   convert method was available.
2. ROCK2 and ROCK4 cache construction failed because the tableaus
   called SVector{N, Int}(args...) with 46 / 50 varargs, a
   constructor not defined in StaticArraysCore.
3. ROCK4 perform_step failed with CanonicalIndexError on
   SVector{6, Float64} because StaticArraysCore does not define
   getindex for SArray.

Switch the ms / fp1 / fp2 / Cᵤ / Cₑ struct fields to plain Vector
(they are built once per cache at solve time and indexed, so the
SVector specialization is not buying anything in the hot loop) and
drop the SVector wrapping from the ROCK2 / ROCK4 tableaus. Convert
the ROCK4 per-step tableau blocks from SVector{N, T} to NTuple{N, T}
to preserve the static-length, tuple-indexable shape without
requiring the full StaticArrays package.

Also fix a separate JET regression introduced in SciML#3420 (TSRKC3):
dtsw0 and d2tsw0 are defined only in the non-rkcstep branch but
read unconditionally in a later if (!rkcstep) block; JET flagged
them as possibly-undefined. Initialize both to
zero(typeof(cache.tsw0)) (and the constantcache variant) before
the branch so the definitions dominate every use.

Fixes CI failures in the OrdinaryDiffEqStabilizedRK_QA sublibrary
job exposed by SciML#3439 and the TSRKC3 JET failure introduced by SciML#3420.

Co-Authored-By: Chris Rackauckas <[email protected]>
ChrisRackauckas added a commit that referenced this pull request Apr 15, 2026
…ly import (#3442)

The package imports SVector from StaticArraysCore rather than full
StaticArrays, which only supplies the tuple-form constructor and
leaves out the varargs constructor and getindex. In the isolated QA
test environment (no transitive StaticArrays) this surfaced two
distinct failures:

1. SERK2, ESERK4, ESERK5 cache construction failed because the
   struct fields ms/Cᵤ/Cₑ were typed as SVector{N, Int} but the
   tableaus returned plain Vector{Int}, and no Vector -> SVector
   convert method was available.
2. ROCK2 and ROCK4 cache construction failed because the tableaus
   called SVector{N, Int}(args...) with 46 / 50 varargs, a
   constructor not defined in StaticArraysCore.
3. ROCK4 perform_step failed with CanonicalIndexError on
   SVector{6, Float64} because StaticArraysCore does not define
   getindex for SArray.

Switch the ms / fp1 / fp2 / Cᵤ / Cₑ struct fields to plain Vector
(they are built once per cache at solve time and indexed, so the
SVector specialization is not buying anything in the hot loop) and
drop the SVector wrapping from the ROCK2 / ROCK4 tableaus. Convert
the ROCK4 per-step tableau blocks from SVector{N, T} to NTuple{N, T}
to preserve the static-length, tuple-indexable shape without
requiring the full StaticArrays package.

Also fix a separate JET regression introduced in #3420 (TSRKC3):
dtsw0 and d2tsw0 are defined only in the non-rkcstep branch but
read unconditionally in a later if (!rkcstep) block; JET flagged
them as possibly-undefined. Initialize both to
zero(typeof(cache.tsw0)) (and the constantcache variant) before
the branch so the definitions dominate every use.

Fixes CI failures in the OrdinaryDiffEqStabilizedRK_QA sublibrary
job exposed by #3439 and the TSRKC3 JET failure introduced by #3420.

Co-authored-by: ChrisRackauckas-Claude <[email protected]>
ChrisRackauckas-Claude pushed a commit to ChrisRackauckas-Claude/OrdinaryDiffEq.jl that referenced this pull request Apr 15, 2026
TSRKC3 was added in OrdinaryDiffEqStabilizedRK 1.11.0 (PR SciML#3420). The
test at regression/iipvsoop_tests.jl:148 references it unconditionally
via OrdinaryDiffEq.OrdinaryDiffEqStabilizedRK.TSRKC3(). Pkg on Julia
1.10 (LTS) ignores the [sources] path directive and resolves the
sublibrary from the registry, where TSRKC3 is not yet released. That
causes "UndefVarError: TSRKC3 not defined" on the LTS Regression_II
CI matrix entry while 1.11/1/pre all pass.

Gate the push with isdefined so the test degrades gracefully when
running against an older registered sublibrary and automatically
includes TSRKC3 once the new version is tagged.

Co-Authored-By: Chris Rackauckas <[email protected]>
singhharsh1708 pushed a commit to singhharsh1708/OrdinaryDiffEq.jl that referenced this pull request Apr 16, 2026
* rkc method fix

* tsrck3 method implementation

* rkc tests

* minor refactor

* md file update

* minor misprint fix

* refactor

* version rollback

* Apply suggestion from @ChrisRackauckas

* rkc tests update

* Apply suggestion from @ChrisRackauckas

---------

Co-authored-by: Christopher Rackauckas <[email protected]>
singhharsh1708 pushed a commit to singhharsh1708/OrdinaryDiffEq.jl that referenced this pull request Apr 16, 2026
…ly import (SciML#3442)

The package imports SVector from StaticArraysCore rather than full
StaticArrays, which only supplies the tuple-form constructor and
leaves out the varargs constructor and getindex. In the isolated QA
test environment (no transitive StaticArrays) this surfaced two
distinct failures:

1. SERK2, ESERK4, ESERK5 cache construction failed because the
   struct fields ms/Cᵤ/Cₑ were typed as SVector{N, Int} but the
   tableaus returned plain Vector{Int}, and no Vector -> SVector
   convert method was available.
2. ROCK2 and ROCK4 cache construction failed because the tableaus
   called SVector{N, Int}(args...) with 46 / 50 varargs, a
   constructor not defined in StaticArraysCore.
3. ROCK4 perform_step failed with CanonicalIndexError on
   SVector{6, Float64} because StaticArraysCore does not define
   getindex for SArray.

Switch the ms / fp1 / fp2 / Cᵤ / Cₑ struct fields to plain Vector
(they are built once per cache at solve time and indexed, so the
SVector specialization is not buying anything in the hot loop) and
drop the SVector wrapping from the ROCK2 / ROCK4 tableaus. Convert
the ROCK4 per-step tableau blocks from SVector{N, T} to NTuple{N, T}
to preserve the static-length, tuple-indexable shape without
requiring the full StaticArrays package.

Also fix a separate JET regression introduced in SciML#3420 (TSRKC3):
dtsw0 and d2tsw0 are defined only in the non-rkcstep branch but
read unconditionally in a later if (!rkcstep) block; JET flagged
them as possibly-undefined. Initialize both to
zero(typeof(cache.tsw0)) (and the constantcache variant) before
the branch so the definitions dominate every use.

Fixes CI failures in the OrdinaryDiffEqStabilizedRK_QA sublibrary
job exposed by SciML#3439 and the TSRKC3 JET failure introduced by SciML#3420.

Co-authored-by: ChrisRackauckas-Claude <[email protected]>
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.

3 participants