Skip to content

Commit f8cd3a8

Browse files
author
Your Name
committed
LowStorageRK: unify 2C family on LowStorageRKTableau
1 parent eedc675 commit f8cd3a8

3 files changed

Lines changed: 53 additions & 58 deletions

File tree

lib/OrdinaryDiffEqLowStorageRK/src/generic_low_storage_perform_step.jl

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,47 @@ end
4949
integrator.u = u
5050
return nothing
5151
end
52+
53+
@muladd function _perform_step_iip!(
54+
integrator, cache, tab::LowStorageRKTableau{:two_c}
55+
)
56+
(; t, dt, u, f, p) = integrator
57+
(; k, fsalfirst, tmp, stage_limiter!, step_limiter!, thread) = cache
58+
(; A2end, B1, B2end, c2end) = tab
59+
60+
@.. broadcast = false thread = thread k = integrator.fsalfirst
61+
@.. broadcast = false thread = thread u = u + B1 * dt * k
62+
63+
for i in eachindex(A2end)
64+
@.. broadcast = false thread = thread tmp = u + A2end[i] * dt * k
65+
f(k, tmp, p, t + c2end[i] * dt)
66+
OrdinaryDiffEqCore.increment_nf!(integrator.stats, 1)
67+
@.. broadcast = false thread = thread u = u + B2end[i] * dt * k
68+
end
69+
step_limiter!(u, integrator, p, t + dt)
70+
f(k, u, p, t + dt)
71+
OrdinaryDiffEqCore.increment_nf!(integrator.stats, 1)
72+
return nothing
73+
end
74+
75+
@muladd function _perform_step_oop!(
76+
integrator, tab::LowStorageRKTableau{:two_c}
77+
)
78+
(; t, dt, u, f, p) = integrator
79+
(; A2end, B1, B2end, c2end) = tab
80+
81+
k = integrator.fsalfirst = integrator.f(u, p, t)
82+
integrator.k[1] = integrator.fsalfirst
83+
OrdinaryDiffEqCore.increment_nf!(integrator.stats, 1)
84+
u = u + B1 * dt * k
85+
86+
for i in eachindex(A2end)
87+
tmp = u + A2end[i] * dt * k
88+
k = integrator.f(tmp, p, t + c2end[i] * dt)
89+
OrdinaryDiffEqCore.increment_nf!(integrator.stats, 1)
90+
u = u + B2end[i] * dt * k
91+
end
92+
93+
integrator.u = u
94+
return nothing
95+
end

lib/OrdinaryDiffEqLowStorageRK/src/low_storage_rk_caches.jl

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -605,13 +605,6 @@ end
605605
thread::Thread
606606
end
607607

608-
struct LowStorageRK2CConstantCache{N, T, T2} <: OrdinaryDiffEqConstantCache
609-
A2end::NTuple{N, T} # A1 is always zero
610-
B1::T
611-
B2end::NTuple{N, T}
612-
c2end::NTuple{N, T2} # c1 is always zero
613-
end
614-
615608
function CFRLDDRK64ConstantCache(::Type{T}, ::Type{T2}) where {T, T2}
616609
A2 = convert(T, 0.17985400977138)
617610
A3 = convert(T, 0.14081893152111)
@@ -635,7 +628,7 @@ function CFRLDDRK64ConstantCache(::Type{T}, ::Type{T2}) where {T, T2}
635628
c6 = convert(T2, 0.83050587987157)
636629
c2end = (c2, c3, c4, c5, c6)
637630

638-
return LowStorageRK2CConstantCache(A2end, B1, B2end, c2end)
631+
return LowStorageRKTableau{:two_c}(A2end, B1, B2end, c2end)
639632
end
640633

641634
function TSLDDRK74ConstantCache(::Type{T}, ::Type{T2}) where {T, T2}
@@ -664,7 +657,7 @@ function TSLDDRK74ConstantCache(::Type{T}, ::Type{T2}) where {T, T2}
664657
c7 = convert(T2, 0.91124223849547205)
665658
c2end = (c2, c3, c4, c5, c6, c7)
666659

667-
return LowStorageRK2CConstantCache(A2end, B1, B2end, c2end)
660+
return LowStorageRKTableau{:two_c}(A2end, B1, B2end, c2end)
668661
end
669662

670663
# 3S low storage methods introduced by Ketcheson

lib/OrdinaryDiffEqLowStorageRK/src/low_storage_rk_perform_step.jl

Lines changed: 7 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -26,71 +26,29 @@ function perform_step!(integrator, cache::LowStorageRK2NCache, repeat_step = fal
2626
return _perform_step_iip!(integrator, cache, cache.tab)
2727
end
2828

29-
# 2C low storage methods
30-
function initialize!(integrator, cache::LowStorageRK2CConstantCache)
31-
integrator.fsalfirst = integrator.f(integrator.uprev, integrator.p, integrator.t) # Pre-start fsal
29+
function initialize!(integrator, cache::LowStorageRKTableau{:two_c})
30+
integrator.fsalfirst = integrator.f(integrator.uprev, integrator.p, integrator.t)
3231
OrdinaryDiffEqCore.increment_nf!(integrator.stats, 1)
3332
integrator.kshortsize = 1
3433
integrator.k = typeof(integrator.k)(undef, integrator.kshortsize)
35-
36-
# Avoid undefined entries if k is an array of arrays
3734
integrator.fsallast = zero(integrator.fsalfirst)
3835
return integrator.k[1] = integrator.fsalfirst
3936
end
4037

41-
@muladd function perform_step!(
42-
integrator, cache::LowStorageRK2CConstantCache,
43-
repeat_step = false
44-
)
45-
(; t, dt, u, f, p) = integrator
46-
(; A2end, B1, B2end, c2end) = cache
47-
48-
# u1
49-
k = integrator.fsalfirst = f(u, p, t)
50-
integrator.k[1] = integrator.fsalfirst
51-
OrdinaryDiffEqCore.increment_nf!(integrator.stats, 1)
52-
u = u + B1 * dt * k
53-
54-
# other stages
55-
for i in eachindex(A2end)
56-
tmp = u + A2end[i] * dt * k
57-
k = f(tmp, p, t + c2end[i] * dt)
58-
OrdinaryDiffEqCore.increment_nf!(integrator.stats, 1)
59-
u = u + B2end[i] * dt * k
60-
end
61-
62-
integrator.u = u
38+
function perform_step!(integrator, cache::LowStorageRKTableau{:two_c}, repeat_step = false)
39+
return _perform_step_oop!(integrator, cache)
6340
end
6441

6542
function initialize!(integrator, cache::LowStorageRK2CCache)
66-
(; k, fsalfirst) = cache
67-
6843
integrator.kshortsize = 1
6944
resize!(integrator.k, integrator.kshortsize)
7045
integrator.k[1] = integrator.fsalfirst
71-
integrator.f(integrator.fsalfirst, integrator.uprev, integrator.p, integrator.t) # FSAL for interpolation
46+
integrator.f(integrator.fsalfirst, integrator.uprev, integrator.p, integrator.t)
7247
return OrdinaryDiffEqCore.increment_nf!(integrator.stats, 1)
7348
end
7449

75-
@muladd function perform_step!(integrator, cache::LowStorageRK2CCache, repeat_step = false)
76-
(; t, dt, u, f, p) = integrator
77-
(; k, fsalfirst, tmp, stage_limiter!, step_limiter!, thread) = cache
78-
(; A2end, B1, B2end, c2end) = cache.tab
79-
80-
# u1
81-
@.. broadcast = false thread = thread k = integrator.fsalfirst
82-
@.. broadcast = false thread = thread u = u + B1 * dt * k
83-
84-
# other stages
85-
for i in eachindex(A2end)
86-
@.. broadcast = false thread = thread tmp = u + A2end[i] * dt * k
87-
f(k, tmp, p, t + c2end[i] * dt)
88-
OrdinaryDiffEqCore.increment_nf!(integrator.stats, 1)
89-
@.. broadcast = false thread = thread u = u + B2end[i] * dt * k
90-
end
91-
step_limiter!(u, integrator, p, t + dt)
92-
f(k, u, p, t + dt)
93-
OrdinaryDiffEqCore.increment_nf!(integrator.stats, 1)
50+
function perform_step!(integrator, cache::LowStorageRK2CCache, repeat_step = false)
51+
return _perform_step_iip!(integrator, cache, cache.tab)
9452
end
9553

9654
# 3S low storage methods

0 commit comments

Comments
 (0)