From db180fda0ae77bd27b5f7bc510a4bc0294bcd045 Mon Sep 17 00:00:00 2001 From: Hendrik Ranocha Date: Tue, 15 Jul 2025 08:35:50 +0200 Subject: [PATCH 1/4] fix stats of stabilized RK methods --- lib/OrdinaryDiffEqStabilizedRK/src/rkc_perform_step.jl | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/OrdinaryDiffEqStabilizedRK/src/rkc_perform_step.jl b/lib/OrdinaryDiffEqStabilizedRK/src/rkc_perform_step.jl index 9bcdb59674..cfac627868 100644 --- a/lib/OrdinaryDiffEqStabilizedRK/src/rkc_perform_step.jl +++ b/lib/OrdinaryDiffEqStabilizedRK/src/rkc_perform_step.jl @@ -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ᵢ₋₁; @@ -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 @@ -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ᵢ₋₁; @@ -314,6 +317,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 From 388e1b17dbbbdefd6dfdcbd5d491857e1a430d93 Mon Sep 17 00:00:00 2001 From: Hendrik Ranocha Date: Tue, 15 Jul 2025 08:36:02 +0200 Subject: [PATCH 2/4] add tests --- .../test/rkc_tests.jl | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/lib/OrdinaryDiffEqStabilizedRK/test/rkc_tests.jl b/lib/OrdinaryDiffEqStabilizedRK/test/rkc_tests.jl index 3849513e88..372c800d42 100644 --- a/lib/OrdinaryDiffEqStabilizedRK/test/rkc_tests.jl +++ b/lib/OrdinaryDiffEqStabilizedRK/test/rkc_tests.jl @@ -72,3 +72,33 @@ end @test sim.𝒪est[:l∞]≈5 atol=testTol end end + +@testset "Numer 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 -5 * u + end + probip = ODEProblem(u0, tspan) do du, u, p, t + x[] += 1 + @. du = -5 * u + return nothing + end + + @testset "$prob" for prob in [probop, probip] + eigen_est = (integrator) -> integrator.eigen_est = 5 + 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 From c8e16c4d68044dc563032629a20a56188f973b5f Mon Sep 17 00:00:00 2001 From: Hendrik Ranocha Date: Tue, 15 Jul 2025 08:41:23 +0200 Subject: [PATCH 3/4] improve tests --- lib/OrdinaryDiffEqStabilizedRK/src/rkc_perform_step.jl | 1 + lib/OrdinaryDiffEqStabilizedRK/test/rkc_tests.jl | 10 +++++----- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/lib/OrdinaryDiffEqStabilizedRK/src/rkc_perform_step.jl b/lib/OrdinaryDiffEqStabilizedRK/src/rkc_perform_step.jl index cfac627868..89e28e2b1a 100644 --- a/lib/OrdinaryDiffEqStabilizedRK/src/rkc_perform_step.jl +++ b/lib/OrdinaryDiffEqStabilizedRK/src/rkc_perform_step.jl @@ -313,6 +313,7 @@ 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 - κ diff --git a/lib/OrdinaryDiffEqStabilizedRK/test/rkc_tests.jl b/lib/OrdinaryDiffEqStabilizedRK/test/rkc_tests.jl index 372c800d42..beef67023c 100644 --- a/lib/OrdinaryDiffEqStabilizedRK/test/rkc_tests.jl +++ b/lib/OrdinaryDiffEqStabilizedRK/test/rkc_tests.jl @@ -73,25 +73,25 @@ end end end -@testset "Numer of function evaluations" begin +@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 -5 * u + return -500 * u end probip = ODEProblem(u0, tspan) do du, u, p, t x[] += 1 - @. du = -5 * u + @. du = -500 * u return nothing end @testset "$prob" for prob in [probop, probip] - eigen_est = (integrator) -> integrator.eigen_est = 5 + 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), + RKC(), RKC(eigen_est = eigen_est), SERK2(), SERK2(eigen_est = eigen_est), ESERK4(), ESERK4(eigen_est = eigen_est), ESERK5(), ESERK5(eigen_est = eigen_est)] From a0eae4b970a25d81856805191d9ab4416aef2b08 Mon Sep 17 00:00:00 2001 From: Christopher Rackauckas Date: Tue, 15 Jul 2025 07:52:58 -0400 Subject: [PATCH 4/4] Update lib/OrdinaryDiffEqStabilizedRK/src/rkc_perform_step.jl --- lib/OrdinaryDiffEqStabilizedRK/src/rkc_perform_step.jl | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/OrdinaryDiffEqStabilizedRK/src/rkc_perform_step.jl b/lib/OrdinaryDiffEqStabilizedRK/src/rkc_perform_step.jl index 89e28e2b1a..cfac627868 100644 --- a/lib/OrdinaryDiffEqStabilizedRK/src/rkc_perform_step.jl +++ b/lib/OrdinaryDiffEqStabilizedRK/src/rkc_perform_step.jl @@ -313,7 +313,6 @@ 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 - κ