Skip to content

Commit c00b795

Browse files
Formatted with Runic
1 parent 6247cd9 commit c00b795

6 files changed

Lines changed: 94 additions & 54 deletions

File tree

lib/OrdinaryDiffEqFIRK/src/alg_utils.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,4 @@ alg_adaptive_order(alg::GaussLegendre) = 2 * alg.num_stages
3333
has_stiff_interpolation(::GaussLegendre) = true
3434

3535
get_current_alg_order(alg::GaussLegendre, cache) = 2 * alg.num_stages
36-
get_current_adaptive_order(alg::GaussLegendre, cache) = 2 * alg.num_stages
36+
get_current_adaptive_order(alg::GaussLegendre, cache) = 2 * alg.num_stages

lib/OrdinaryDiffEqFIRK/src/algorithms.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,4 +302,4 @@ function GaussLegendre(;
302302
num_stages,
303303
AD_choice
304304
)
305-
end
305+
end

lib/OrdinaryDiffEqFIRK/src/firk_caches.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -861,4 +861,4 @@ function alg_cache(
861861
tmp, atmp, jac_config, linsolve, rtol, atol, dt, dt,
862862
Convergence, alg.step_limiter!, num_stages
863863
)
864-
end
864+
end

lib/OrdinaryDiffEqFIRK/src/firk_perform_step.jl

Lines changed: 56 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2257,9 +2257,13 @@ function initialize!(integrator, cache::GaussLegendreConstantCache)
22572257

22582258
# adaptive Richardson controller requires num_stages >= 2
22592259
if integrator.opts.adaptive && integrator.alg.num_stages < 2
2260-
throw(ArgumentError("GaussLegendre with num_stages = $(integrator.alg.num_stages) " *
2261-
"does not support adaptive stepping (Richardson controller " *
2262-
"requires num_stages ≥ 2). Use num_stages ≥ 2 or pass adaptive = false."))
2260+
throw(
2261+
ArgumentError(
2262+
"GaussLegendre with num_stages = $(integrator.alg.num_stages) " *
2263+
"does not support adaptive stepping (Richardson controller " *
2264+
"requires num_stages ≥ 2). Use num_stages ≥ 2 or pass adaptive = false."
2265+
)
2266+
)
22632267
end
22642268
return nothing
22652269
end
@@ -2276,9 +2280,13 @@ function initialize!(integrator, cache::GaussLegendreCache)
22762280
OrdinaryDiffEqCore.increment_nf!(integrator.stats, 1)
22772281
if integrator.opts.adaptive
22782282
if cache.num_stages < 2
2279-
throw(ArgumentError("GaussLegendre with num_stages = $(cache.num_stages) " *
2280-
"does not support adaptive stepping (Richardson controller " *
2281-
"requires num_stages ≥ 2). Use num_stages ≥ 2 or pass adaptive = false."))
2283+
throw(
2284+
ArgumentError(
2285+
"GaussLegendre with num_stages = $(cache.num_stages) " *
2286+
"does not support adaptive stepping (Richardson controller " *
2287+
"requires num_stages ≥ 2). Use num_stages ≥ 2 or pass adaptive = false."
2288+
)
2289+
)
22822290
end
22832291
(; abstol, reltol) = integrator.opts
22842292
if reltol isa Number
@@ -2346,10 +2354,13 @@ end
23462354
end
23472355

23482356
ndwprev = ndw
2349-
ndw = sum(internalnorm(
2350-
calculate_residuals(dw[i], uprev_local, uprev_local, atol, rtol, internalnorm, t_local),
2351-
t_local)
2352-
for i in 1:num_stages)
2357+
ndw = sum(
2358+
internalnorm(
2359+
calculate_residuals(dw[i], uprev_local, uprev_local, atol, rtol, internalnorm, t_local),
2360+
t_local
2361+
)
2362+
for i in 1:num_stages
2363+
)
23532364

23542365
if iter > 1
23552366
θ = ndw / ndwprev
@@ -2390,8 +2401,10 @@ end
23902401
return (u_out, z, true)
23912402
end
23922403

2393-
@muladd function perform_step!(integrator, cache::GaussLegendreConstantCache,
2394-
repeat_step = false)
2404+
@muladd function perform_step!(
2405+
integrator, cache::GaussLegendreConstantCache,
2406+
repeat_step = false
2407+
)
23952408
(; t, dt, uprev, f, p) = integrator
23962409
(; num_stages) = cache
23972410
(; internalnorm, abstol, reltol, adaptive) = integrator.opts
@@ -2405,7 +2418,8 @@ end
24052418
if adaptive && num_stages >= 2
24062419
# Richardson step-doubling: one full step at dt, two successive half-steps.
24072420
u_H, z_full, ok1 = _gausslegendre_substep_constant(
2408-
integrator, cache, alg, uprev, t, dt, J, atol, rtol)
2421+
integrator, cache, alg, uprev, t, dt, J, atol, rtol
2422+
)
24092423
if !ok1
24102424
integrator.force_stepfail = true
24112425
integrator.stats.nnonlinconvfail += 1
@@ -2414,15 +2428,17 @@ end
24142428

24152429
half_dt = dt / 2
24162430
u_h1, _, ok2 = _gausslegendre_substep_constant(
2417-
integrator, cache, alg, uprev, t, half_dt, J, atol, rtol)
2431+
integrator, cache, alg, uprev, t, half_dt, J, atol, rtol
2432+
)
24182433
if !ok2
24192434
integrator.force_stepfail = true
24202435
integrator.stats.nnonlinconvfail += 1
24212436
return
24222437
end
24232438

24242439
u_h, _, ok3 = _gausslegendre_substep_constant(
2425-
integrator, cache, alg, u_h1, t + half_dt, half_dt, J, atol, rtol)
2440+
integrator, cache, alg, u_h1, t + half_dt, half_dt, J, atol, rtol
2441+
)
24262442
if !ok3
24272443
integrator.force_stepfail = true
24282444
integrator.stats.nnonlinconvfail += 1
@@ -2432,13 +2448,15 @@ end
24322448
p_order = 2 * num_stages
24332449
utilde = @.. (u_h - u_H) / (2^p_order - 1)
24342450
integrator.EEst = internalnorm(
2435-
calculate_residuals(utilde, uprev, u_h, atol, rtol, internalnorm, t), t)
2451+
calculate_residuals(utilde, uprev, u_h, atol, rtol, internalnorm, t), t
2452+
)
24362453

24372454
u = u_h
24382455
z_for_fsal = z_full
24392456
else
24402457
u_out, z_out, ok = _gausslegendre_substep_constant(
2441-
integrator, cache, alg, uprev, t, dt, J, atol, rtol)
2458+
integrator, cache, alg, uprev, t, dt, J, atol, rtol
2459+
)
24422460
if !ok
24432461
integrator.force_stepfail = true
24442462
integrator.stats.nnonlinconvfail += 1
@@ -2471,8 +2489,10 @@ end
24712489
u_dest, uprev_local, t_local, dt_local, J_local,
24722490
cache::GaussLegendreCache, integrator, alg
24732491
)
2474-
(; tab, κ, z, dw, ks, W, ubuff, tmp, atmp, linsolve,
2475-
rtol, atol, num_stages) = cache
2492+
(;
2493+
tab, κ, z, dw, ks, W, ubuff, tmp, atmp, linsolve,
2494+
rtol, atol, num_stages,
2495+
) = cache
24762496
(; A, b, c) = tab
24772497
(; internalnorm) = integrator.opts
24782498
(; maxiters) = alg
@@ -2510,8 +2530,10 @@ end
25102530
end
25112531

25122532
needfactor = iter == 1
2513-
linres = dolinsolve(integrator, linsolve;
2514-
A = needfactor ? W : nothing, b = ubuff, linu = ubuff)
2533+
linres = dolinsolve(
2534+
integrator, linsolve;
2535+
A = needfactor ? W : nothing, b = ubuff, linu = ubuff
2536+
)
25152537
cache.linsolve = linres.cache
25162538
integrator.stats.nsolve += 1
25172539

@@ -2520,10 +2542,12 @@ end
25202542
end
25212543

25222544
ndwprev = ndw
2523-
ndw = sum(begin
2524-
calculate_residuals!(atmp, dw[i], uprev_local, uprev_local, atol, rtol, internalnorm, t_local)
2525-
internalnorm(atmp, t_local)
2526-
end for i in 1:num_stages)
2545+
ndw = sum(
2546+
begin
2547+
calculate_residuals!(atmp, dw[i], uprev_local, uprev_local, atol, rtol, internalnorm, t_local)
2548+
internalnorm(atmp, t_local)
2549+
end for i in 1:num_stages
2550+
)
25272551

25282552
if iter > 1
25292553
θ = ndw / ndwprev
@@ -2567,16 +2591,18 @@ end
25672591

25682592
@muladd function perform_step!(integrator, cache::GaussLegendreCache, repeat_step = false)
25692593
(; t, dt, uprev, u, f, p, fsallast) = integrator
2570-
(; atmp, J, z, z_last, u_full, u_half, rtol, atol,
2571-
step_limiter!, num_stages) = cache
2594+
(;
2595+
atmp, J, z, z_last, u_full, u_half, rtol, atol,
2596+
step_limiter!, num_stages,
2597+
) = cache
25722598
(; internalnorm, adaptive) = integrator.opts
25732599
alg = unwrap_alg(integrator, true)
25742600

25752601
new_jac = do_newJ(integrator, alg, cache, repeat_step)
25762602
new_jac && (calc_J!(J, integrator, cache); cache.W_γdt = dt)
25772603

25782604
if adaptive && num_stages >= 2
2579-
# fll step at dt
2605+
# fll step at dt
25802606
ok1 = _gausslegendre_substep!(u_full, uprev, t, dt, J, cache, integrator, alg)
25812607
if !ok1
25822608
integrator.force_stepfail = true
@@ -2609,7 +2635,7 @@ end
26092635

26102636
p_order = 2 * num_stages
26112637
denom = 2^p_order - 1
2612-
@.. u_full = (u - u_full) / denom
2638+
@.. u_full = (u - u_full) / denom
26132639
calculate_residuals!(atmp, u_full, uprev, u, atol, rtol, internalnorm, t)
26142640
integrator.EEst = internalnorm(atmp, t)
26152641
else
@@ -2637,4 +2663,4 @@ end
26372663
f(fsallast, u, p, t + dt)
26382664
OrdinaryDiffEqCore.increment_nf!(integrator.stats, 1)
26392665
return
2640-
end
2666+
end

lib/OrdinaryDiffEqFIRK/src/firk_tableaus.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -427,4 +427,4 @@ const GaussLegendreTableauCache = Dict{
427427
(Float64, Float64, 3) => generateGaussLegendreTableau(Float64, Float64, 3),
428428
(Float64, Float64, 4) => generateGaussLegendreTableau(Float64, Float64, 4),
429429
(Float64, Float64, 5) => generateGaussLegendreTableau(Float64, Float64, 5),
430-
)
430+
)
Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
using OrdinaryDiffEqFIRK, DiffEqDevTools, Test, LinearAlgebra
22
import ODEProblemLibrary: prob_ode_linear, prob_ode_2Dlinear
33

4-
testTol = 0.6
4+
testTol = 0.6
55

66
# test orders 2 and 3 for convergence with fixed dt, anything higher is too sensitive to floating point precision
77
@testset "GaussLegendre: fixed-dt empirical order (s = 2, 3)" begin
88
dts = Float64.(1 ./ 2 .^ (5:-1:2)) # 1/32 … 1/4
99
for s in 2:3
1010
alg = GaussLegendre(num_stages = s; maxiters = 100)
11-
sim = test_convergence(dts, prob_ode_linear, alg;
11+
sim = test_convergence(
12+
dts, prob_ode_linear, alg;
1213
dense_errors = false,
13-
abstol = 1e-12, reltol = 1e-12)
14+
abstol = 1.0e-12, reltol = 1.0e-12
15+
)
1416
@test sim.𝒪est[:final] 2 * s atol = testTol
1517
end
1618
end
@@ -19,45 +21,57 @@ end
1921
@testset "GaussLegendre: fixed-dt accuracy (s = 4, order 8)" begin
2022
s = 4
2123
alg = GaussLegendre(num_stages = s; maxiters = 100)
22-
sol = solve(prob_ode_linear, alg; adaptive = false, dt = 1 // 256,
23-
abstol = 1e-14, reltol = 1e-14)
24+
sol = solve(
25+
prob_ode_linear, alg; adaptive = false, dt = 1 // 256,
26+
abstol = 1.0e-14, reltol = 1.0e-14
27+
)
2428
@test sol.retcode == ReturnCode.Success
2529
exact = prob_ode_linear.u0 * exp(1.01 * (sol.t[end] - sol.t[1]))
26-
@test isapprox(sol.u[end], exact; rtol = 1e-9, atol = 1e-12)
30+
@test isapprox(sol.u[end], exact; rtol = 1.0e-9, atol = 1.0e-12)
2731
end
2832

2933
# test adaptive stepping with tolerance matching
3034
@testset "GaussLegendre: adaptive run matches tolerance" begin
3135
for s in 2:4
32-
reltol = 1e-6
33-
abstol = 1e-9
34-
sol = solve(prob_ode_linear, GaussLegendre(num_stages = s);
35-
reltol = reltol, abstol = abstol)
36+
reltol = 1.0e-6
37+
abstol = 1.0e-9
38+
sol = solve(
39+
prob_ode_linear, GaussLegendre(num_stages = s);
40+
reltol = reltol, abstol = abstol
41+
)
3642
@test sol.retcode == ReturnCode.Success
3743
exact = prob_ode_linear.u0 * exp(1.01 * (sol.t[end] - sol.t[1]))
38-
@test isapprox(sol.u[end], exact; rtol = 1e-3, atol = 1e-6)
44+
@test isapprox(sol.u[end], exact; rtol = 1.0e-3, atol = 1.0e-6)
3945
end
4046
end
4147

4248
# test that Richardson step-doubling tightens the step count when the tolerance is tightened
4349
@testset "GaussLegendre: Richardson tightens step count when tol tightens" begin
4450
s = 3
45-
sol_loose = solve(prob_ode_linear, GaussLegendre(num_stages = s);
46-
reltol = 1e-3, abstol = 1e-6)
47-
sol_tight = solve(prob_ode_linear, GaussLegendre(num_stages = s);
48-
reltol = 1e-8, abstol = 1e-10)
51+
sol_loose = solve(
52+
prob_ode_linear, GaussLegendre(num_stages = s);
53+
reltol = 1.0e-3, abstol = 1.0e-6
54+
)
55+
sol_tight = solve(
56+
prob_ode_linear, GaussLegendre(num_stages = s);
57+
reltol = 1.0e-8, abstol = 1.0e-10
58+
)
4959
@test length(sol_tight.t) >= length(sol_loose.t)
5060
end
5161

5262
# test that num_stages = 1 with adaptive throws
5363
@testset "GaussLegendre: num_stages = 1 with adaptive throws" begin
54-
@test_throws ArgumentError solve(prob_ode_linear, GaussLegendre(num_stages = 1);
55-
reltol = 1e-4, abstol = 1e-7)
64+
@test_throws ArgumentError solve(
65+
prob_ode_linear, GaussLegendre(num_stages = 1);
66+
reltol = 1.0e-4, abstol = 1.0e-7
67+
)
5668
end
5769

5870
# test that num_stages = 1 with adaptive = false runs
5971
@testset "GaussLegendre: num_stages = 1 with adaptive = false runs" begin
60-
sol = solve(prob_ode_linear, GaussLegendre(num_stages = 1);
61-
adaptive = false, dt = 1 // 32)
72+
sol = solve(
73+
prob_ode_linear, GaussLegendre(num_stages = 1);
74+
adaptive = false, dt = 1 // 32
75+
)
6276
@test sol.retcode == ReturnCode.Success
63-
end
77+
end

0 commit comments

Comments
 (0)