Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion lib/OrdinaryDiffEqFIRK/Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "OrdinaryDiffEqFIRK"
uuid = "5960d6e9-dd7a-4743-88e7-cf307b64f125"
authors = ["ParamThakkar123 <paramthakkar864@gmail.com>"]
version = "2.8.9"
version = "2.8.10"

[deps]
FastBroadcast = "7034ab61-46d4-4ed7-9d0f-46aef9175898"
Expand Down
15 changes: 12 additions & 3 deletions lib/OrdinaryDiffEqFIRK/src/algorithms.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,30 @@ doi={10.1109/HPEC67600.2025.11196706}}"""

extra_keyword_description = """
- `extrapolant`: TBD
- `smooth_est`: TBD
- `smooth_est`: when `true`, the embedded error estimate is `(J - γ/dt M)⁻¹ (f₀ + M Σ eᵢzᵢ/dt)`, filtered
as in Hairer's RADAU5. When `false` and the mass matrix is `I`, the linear solve is skipped and
`-(dt/γ) (f₀ + Σ eᵢzᵢ/dt)`, its limit for small `dt J`, is used instead. With any other mass matrix that
limit needs `M⁻¹`, so the filtered estimate is always used.
- `step_limiter!`: function of the form `limiter!(u, integrator, p, t)`"""
extra_keyword_default = """
extrapolant = :dense,
smooth_est = true,
step_limiter! = trivial_limiter!"""
radau3_extra_keyword_description = """
- `extrapolant`: TBD
- `step_limiter!`: function of the form `limiter!(u, integrator, p, t)`"""
radau3_extra_keyword_default = """
extrapolant = :dense,
step_limiter! = trivial_limiter!"""

@doc differentiation_rk_docstring(
"An A-B-L stable fully implicit Runge-Kutta method with internal tableau complex basis transform for efficiency.
Similar to Hairer's SEULEX.",
"RadauIIA3",
"Fully-Implicit Runge-Kutta Method.";
references = hairer1999stiff,
extra_keyword_description,
extra_keyword_default
extra_keyword_description = radau3_extra_keyword_description,
extra_keyword_default = radau3_extra_keyword_default
)
struct RadauIIA3{AD, F, Tol, C1, C2, StepLimiter, CJ} <:
OrdinaryDiffEqNewtonAdaptiveAlgorithm
Expand Down
52 changes: 40 additions & 12 deletions lib/OrdinaryDiffEqFIRK/src/firk_perform_step.jl
Original file line number Diff line number Diff line change
Expand Up @@ -615,6 +615,7 @@ end
alg = unwrap_alg(integrator, true)
(; maxiters) = alg
mass_matrix = integrator.f.mass_matrix
smooth_est = alg.smooth_est || mass_matrix !== I

# precalculations
rtol = @.. reltol^(2 / 3) / 10
Expand Down Expand Up @@ -765,9 +766,11 @@ end
tmp = @.. e1dt * z1 + e2dt * z2 + e3dt * z3
!_is_identity_massmatrix(mass_matrix) && (tmp = mass_matrix * tmp)
utilde = @.. integrator.fsalfirst + tmp
if alg.smooth_est
if smooth_est
utilde = _reshape(LU1 \ _vec(utilde), axes(u))
integrator.stats.nsolve += 1
else
utilde = @.. -utilde / γdt
end
# RadauIIA5 needs a transformed rtol and atol see
# https://github.com/luchr/ODEInterface.jl/blob/0bd134a5a358c4bc13e0fb6a90e27e4ee79e0115/src/radau5.f#L399-L421
Expand All @@ -779,7 +782,8 @@ end
f0 = f(uprev .+ utilde, p, t)
OrdinaryDiffEqCore.increment_nf!(integrator.stats, 1)
utilde = @.. f0 + tmp
alg.smooth_est && (utilde = LU1 \ utilde; integrator.stats.nsolve += 1)
smooth_est && (utilde = LU1 \ utilde; integrator.stats.nsolve += 1)
smooth_est || (utilde = @.. -utilde / γdt)
atmp = calculate_residuals(utilde, uprev, u, atol, rtol, internalnorm, t)
OrdinaryDiffEqCore.set_EEst!(integrator, internalnorm(atmp, t))
end
Expand Down Expand Up @@ -816,6 +820,7 @@ end
alg = unwrap_alg(integrator, true)
(; maxiters) = alg
mass_matrix = integrator.f.mass_matrix
smooth_est = alg.smooth_est || mass_matrix !== I

# precalculations
c1m1 = c1 - 1
Expand Down Expand Up @@ -1020,12 +1025,14 @@ end
!_is_identity_massmatrix(mass_matrix) && (mul!(w1, mass_matrix, tmp); copyto!(tmp, w1))
@.. ubuff = integrator.fsalfirst + tmp

if alg.smooth_est
if smooth_est
linres1 = dolinsolve(
integrator, cache.linsolve1; b = _vec(ubuff),
linu = _vec(utilde)
)
integrator.stats.nsolve += 1
else
@.. utilde = -ubuff / γdt
end

# RadauIIA5 needs a transformed rtol and atol see
Expand All @@ -1040,12 +1047,14 @@ end
OrdinaryDiffEqCore.increment_nf!(integrator.stats, 1)
@.. ubuff = fsallast + tmp

if alg.smooth_est
if smooth_est
linres1 = dolinsolve(
integrator, cache.linsolve1; b = _vec(ubuff),
linu = _vec(utilde)
)
integrator.stats.nsolve += 1
else
@.. utilde = -ubuff / γdt
end

calculate_residuals!(atmp, utilde, uprev, u, atol, rtol, internalnorm, t)
Expand Down Expand Up @@ -1087,6 +1096,7 @@ end
alg = unwrap_alg(integrator, true)
(; maxiters) = alg
mass_matrix = integrator.f.mass_matrix
smooth_est = alg.smooth_est || mass_matrix !== I

# precalculations rtol pow is (num stages + 1)/(2*num stages)
rtol = @.. reltol^(3 / 5) / 10
Expand Down Expand Up @@ -1326,9 +1336,11 @@ end
tmp = @.. e1dt * z1 + e2dt * z2 + e3dt * z3 + e4dt * z4 + e5dt * z5
!_is_identity_massmatrix(mass_matrix) && (tmp = mass_matrix * tmp)
utilde = @.. integrator.fsalfirst + tmp
if alg.smooth_est
if smooth_est
utilde = _reshape(LU1 \ _vec(utilde), axes(u))
integrator.stats.nsolve += 1
else
utilde = @.. -utilde / γdt
end
atmp = calculate_residuals(utilde, uprev, u, atol, rtol, internalnorm, t)
OrdinaryDiffEqCore.set_EEst!(integrator, internalnorm(atmp, t))
Expand All @@ -1338,7 +1350,8 @@ end
f0 = f(uprev .+ utilde, p, t)
OrdinaryDiffEqCore.increment_nf!(integrator.stats, 1)
utilde = @.. f0 + tmp
alg.smooth_est && (utilde = LU1 \ utilde; integrator.stats.nsolve += 1)
smooth_est && (utilde = LU1 \ utilde; integrator.stats.nsolve += 1)
smooth_est || (utilde = @.. -utilde / γdt)
atmp = calculate_residuals(utilde, uprev, u, atol, rtol, internalnorm, t)
OrdinaryDiffEqCore.set_EEst!(integrator, internalnorm(atmp, t))
end
Expand Down Expand Up @@ -1386,6 +1399,7 @@ end
alg = unwrap_alg(integrator, true)
(; maxiters) = alg
mass_matrix = integrator.f.mass_matrix
smooth_est = alg.smooth_est || mass_matrix !== I

# precalculations
c1m1 = c1 - 1
Expand Down Expand Up @@ -1704,12 +1718,14 @@ end
!_is_identity_massmatrix(mass_matrix) && (mul!(w1, mass_matrix, tmp); copyto!(tmp, w1))
@.. ubuff = integrator.fsalfirst + tmp

if alg.smooth_est
if smooth_est
linres1 = dolinsolve(
integrator, cache.linsolve1; b = _vec(ubuff),
linu = _vec(utilde)
)
integrator.stats.nsolve += 1
else
@.. utilde = -ubuff / γdt
end

# RadauIIA5 needs a transformed rtol and atol see
Expand All @@ -1724,12 +1740,14 @@ end
OrdinaryDiffEqCore.increment_nf!(integrator.stats, 1)
@.. ubuff = fsallast + tmp

if alg.smooth_est
if smooth_est
linres1 = dolinsolve(
integrator, cache.linsolve1; b = _vec(ubuff),
linu = _vec(utilde)
)
integrator.stats.nsolve += 1
else
@.. utilde = -ubuff / γdt
end

calculate_residuals!(atmp, utilde, uprev, u, atol, rtol, internalnorm, t)
Expand Down Expand Up @@ -1766,6 +1784,7 @@ end
alg = unwrap_alg(integrator, true)
(; maxiters) = alg
mass_matrix = integrator.f.mass_matrix
smooth_est = alg.smooth_est || mass_matrix !== I

# precalculations rtol pow is (num stages + 1)/(2*num stages)
rtol = @.. reltol^((num_stages + 1) / (num_stages * 2)) / 10
Expand Down Expand Up @@ -1974,9 +1993,11 @@ end
!_is_identity_massmatrix(mass_matrix) && (tmp = mass_matrix * tmp)
#utilde = @.. 1 / γ * dt * integrator.fsalfirst + tmp
utilde = @.. integrator.fsalfirst + tmp
if alg.smooth_est
if smooth_est
utilde = _reshape(LU1 \ _vec(utilde), axes(u))
integrator.stats.nsolve += 1
else
utilde = @.. -utilde / γdt
end
atmp = calculate_residuals(utilde, uprev, u, atol, rtol, internalnorm, t)
OrdinaryDiffEqCore.set_EEst!(integrator, internalnorm(atmp, t))
Expand All @@ -1987,9 +2008,11 @@ end
OrdinaryDiffEqCore.increment_nf!(integrator.stats, 1)
#utilde = @.. 1 / γ * dt * f0 + tmp
utilde = @.. f0 + tmp
if alg.smooth_est
if smooth_est
utilde = _reshape(LU1 \ _vec(utilde), axes(u))
integrator.stats.nsolve += 1
else
utilde = @.. -utilde / γdt
end
atmp = calculate_residuals(utilde, uprev, u, atol, rtol, internalnorm, t)
OrdinaryDiffEqCore.set_EEst!(integrator, internalnorm(atmp, t))
Expand Down Expand Up @@ -2026,6 +2049,7 @@ end
alg = unwrap_alg(integrator, true)
(; maxiters) = alg
mass_matrix = integrator.f.mass_matrix
smooth_est = alg.smooth_est || mass_matrix !== I

# precalculations
γdt = γ / dt
Expand Down Expand Up @@ -2304,12 +2328,14 @@ end
#@.. ubuff=1 / γ * dt * integrator.fsalfirst + tmp
@.. ubuff = integrator.fsalfirst + tmp

if alg.smooth_est
if smooth_est
dolinsolve(
integrator, linsolve1; b = _vec(ubuff),
linu = _vec(utilde)
)
integrator.stats.nsolve += 1
else
@.. utilde = -ubuff / γdt
end

# RadauIIA5 needs a transformed rtol and atol see
Expand All @@ -2325,12 +2351,14 @@ end
#@.. ubuff = 1 / γ * dt * fsallast + tmp
@.. ubuff = fsallast + tmp

if alg.smooth_est
if smooth_est
dolinsolve(
integrator, linsolve1; b = _vec(ubuff),
linu = _vec(utilde)
)
integrator.stats.nsolve += 1
else
@.. utilde = -ubuff / γdt
end

calculate_residuals!(atmp, utilde, uprev, u, atol, rtol, internalnorm, t)
Expand Down
26 changes: 26 additions & 0 deletions lib/OrdinaryDiffEqFIRK/test/ode_firk_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,32 @@ end
end
end

@testset "Unsmoothed error estimate (smooth_est = false)" begin
f!(du, u, p, t) = (du .= -50 .* (u .- cos(t)); nothing)
f(u, p, t) = -50 .* (u .- cos(t))
for prob in (ODEProblem(f!, [0.0, 1.0], (0.0, 2.0)), ODEProblem(f, [0.0, 1.0], (0.0, 2.0))),
Alg in (RadauIIA5, RadauIIA9, AdaptiveRadau)
smoothed = solve(prob, Alg(); reltol = 1.0e-8, abstol = 1.0e-8)
unsmoothed = solve(prob, Alg(smooth_est = false); reltol = 1.0e-8, abstol = 1.0e-8, maxiters = 1000)
@test unsmoothed.retcode == ReturnCode.Success
@test unsmoothed.stats.naccept < 2 * smoothed.stats.naccept
@test unsmoothed.u[end] ≈ smoothed.u[end] atol = 1.0e-6
@test unsmoothed.u[end] ≈ 50 * (50 * cos(2.0) + sin(2.0)) / 2501 .+ ([0.0, 1.0] .- 2500 / 2501) * exp(-100.0) atol = 1.0e-7
end
g!(du, u, p, t) = (du[1] = -u[1] + sin(t); du[2] = -2u[2] + u[1]; nothing)
mm = ODEFunction((du, u, p, t) -> (du .= -0.5 .* (u .- cos(t)); nothing); mass_matrix = Diagonal([0.01, 0.01]))
for Alg in (RadauIIA5, RadauIIA9, AdaptiveRadau)
eest = map((true, false)) do smooth_est
integ = init(ODEProblem(g!, [1.0, 0.5], (0.0, 1.0)), Alg(; smooth_est); dt = 0.01, reltol = 1.0e-6, abstol = 1.0e-6)
step!(integ)
OrdinaryDiffEqCore.get_EEst(integ)
end
@test eest[2] ≈ eest[1] rtol = 0.02
prob = ODEProblem(mm, [0.0, 1.0], (0.0, 2.0))
@test solve(prob, Alg(smooth_est = false)).u == solve(prob, Alg()).u
end
end

@testset "FIRK stiff member of an AutoSwitch composite (#4364)" begin
function rober_firk!(du, u, p, t)
y1, y2, y3 = u
Expand Down
Loading