Skip to content

fix stats of stabilized RK methods #2753

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 15, 2025
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions lib/OrdinaryDiffEqStabilizedRK/src/rkc_perform_step.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ end
μ, κ = recf[cache.start + (i - 2) * 2 + 1], recf[cache.start + (i - 2) * 2 + 2]
ν = -1 - κ
u = f(uᵢ₋₁, p, tᵢ₋₁)
OrdinaryDiffEqCore.increment_nf!(integrator.stats, 1)
tᵢ₋₁ = dt * μ - ν * tᵢ₋₂ - κ * tᵢ₋₃
u = (dt * μ) * u - ν * uᵢ₋₁ - κ * uᵢ₋₂
i < cache.mdeg && (uᵢ₋₂ = uᵢ₋₁;
Expand Down Expand Up @@ -110,6 +111,7 @@ end
μ, κ = recf[ccache.start + (i - 2) * 2 + 1], recf[ccache.start + (i - 2) * 2 + 2]
ν = -1 - κ
f(k, uᵢ₋₁, p, tᵢ₋₁)
OrdinaryDiffEqCore.increment_nf!(integrator.stats, 1)
tᵢ₋₁ = dt * μ - ν * tᵢ₋₂ - κ * tᵢ₋₃
@.. broadcast=false u=(dt * μ) * k - ν * uᵢ₋₁ - κ * uᵢ₋₂
if i < ccache.mdeg
Expand Down Expand Up @@ -192,6 +194,7 @@ end
μ, κ = recf[cache.start + (i - 2) * 2 + 1], recf[cache.start + (i - 2) * 2 + 2]
ν = -1 - κ
u = f(uᵢ₋₁, p, tᵢ₋₁)
OrdinaryDiffEqCore.increment_nf!(integrator.stats, 1)
tᵢ₋₁ = dt * μ - ν * tᵢ₋₂ - κ * tᵢ₋₃
u = (dt * μ) * u - ν * uᵢ₋₁ - κ * uᵢ₋₂
i < cache.mdeg && (uᵢ₋₂ = uᵢ₋₁;
Expand Down Expand Up @@ -310,10 +313,12 @@ end
@.. broadcast=false uᵢ₋₁=uprev + (dt * recf[ccache.start]) * fsalfirst
ccache.mdeg < 2 && (@.. broadcast=false u=uᵢ₋₁)
# for the second to the ccache.mdeg th stages
@show ccache.mdeg
for i in 2:(ccache.mdeg)
μ, κ = recf[ccache.start + (i - 2) * 2 + 1], recf[ccache.start + (i - 2) * 2 + 2]
ν = -1 - κ
f(k, uᵢ₋₁, p, tᵢ₋₁)
OrdinaryDiffEqCore.increment_nf!(integrator.stats, 1)
tᵢ₋₁ = (dt * μ) - ν * tᵢ₋₂ - κ * tᵢ₋₃
@.. broadcast=false u=(dt * μ) * k - ν * uᵢ₋₁ - κ * uᵢ₋₂
if i < ccache.mdeg
Expand Down
30 changes: 30 additions & 0 deletions lib/OrdinaryDiffEqStabilizedRK/test/rkc_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,33 @@ end
@test sim.𝒪est[:l∞]5 atol=testTol
end
end

@testset "Number of function evaluations" begin
x = Ref(0)
u0 = [1.0, 1.0]
tspan = (0.0, 1.0)
probop = ODEProblem(u0, tspan) do u, p, t
x[] += 1
return -500 * u
end
probip = ODEProblem(u0, tspan) do du, u, p, t
x[] += 1
@. du = -500 * u
return nothing
end

@testset "$prob" for prob in [probop, probip]
eigen_est = (integrator) -> integrator.eigen_est = 500
algs = [ROCK2(), ROCK2(eigen_est = eigen_est),
ROCK4(), ROCK4(eigen_est = eigen_est),
RKC(), RKC(eigen_est = eigen_est),
SERK2(), SERK2(eigen_est = eigen_est),
ESERK4(), ESERK4(eigen_est = eigen_est),
ESERK5(), ESERK5(eigen_est = eigen_est)]
@testset "$alg" for alg in algs
x[] = 0
sol = solve(prob, alg)
@test x[] == sol.stats.nf
end
end
end
Loading