Skip to content

Commit 58baa98

Browse files
edits, adaptivity fixed
1 parent a193e25 commit 58baa98

File tree

5 files changed

+89
-73
lines changed

5 files changed

+89
-73
lines changed
Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
qmax_default(alg::Union{RadauIIA3, RadauIIA5, RadauIIA9}) = 8
1+
qmax_default(alg::Union{RadauIIA3, RadauIIA5, RadauIIA9, AdaptiveRadau}) = 8
22

33
alg_order(alg::RadauIIA3) = 3
44
alg_order(alg::RadauIIA5) = 5
55
alg_order(alg::RadauIIA9) = 9
6-
alg_order(alg::AdaptiveRadau) = 5
6+
alg_order(alg::AdaptiveRadau) = 5 #dummy value
77

88
isfirk(alg::RadauIIA3) = true
99
isfirk(alg::RadauIIA5) = true
@@ -13,3 +13,6 @@ isfirk(alg::AdaptiveRadau) = true
1313
alg_adaptive_order(alg::RadauIIA3) = 1
1414
alg_adaptive_order(alg::RadauIIA5) = 3
1515
alg_adaptive_order(alg::RadauIIA9) = 5
16+
17+
get_current_alg_order(alg::AdaptiveRadau, cache) = cache.num_stages * 2 - 1
18+
get_current_adaptive_order(alg::AdaptiveRadau, cache) = cache.num_stages

lib/OrdinaryDiffEqFIRK/src/controllers.jl

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
@inline function stepsize_controller!(integrator, controller::PredictiveController, alg)
22
@unpack qmin, qmax, gamma = integrator.opts
33
EEst = DiffEqBase.value(integrator.EEst)
4-
54
if iszero(EEst)
65
q = inv(qmax)
76
else
@@ -51,7 +50,7 @@ end
5150
function step_accept_controller!(integrator, controller::PredictiveController, alg::AdaptiveRadau, q)
5251
@unpack qmin, qmax, gamma, qsteady_min, qsteady_max = integrator.opts
5352
@unpack cache = integrator
54-
@unpack num_stages, step, θ, θprev = cache
53+
@unpack num_stages, step, iter, hist_iter = cache
5554

5655
EEst = DiffEqBase.value(integrator.EEst)
5756

@@ -69,16 +68,18 @@ function step_accept_controller!(integrator, controller::PredictiveController, a
6968
end
7069
integrator.dtacc = integrator.dt
7170
integrator.erracc = max(1e-2, EEst)
72-
7371
cache.step = step + 1
74-
@show cache.num_stages
72+
hist_iter = hist_iter * 0.8 + iter * 0.2
73+
cache.hist_iter = hist_iter
7574
if (step > 10)
76-
Ψ = θ * θprev
77-
if<= 0.001 && num_stages < alg.max_stages)
75+
if (hist_iter < 2.6 && num_stages < alg.max_stages)
7876
cache.num_stages += 2
79-
elseif ((Ψ >= 0.1 || cache.status == VerySlowConvergence || cache.status == Divergence) && num_stages > alg.min_stages)
77+
cache.step = 1
78+
cache.hist_iter = iter
79+
elseif ((hist_iter > 8 || cache.status == VerySlowConvergence || cache.status == Divergence) && num_stages > alg.min_stages)
8080
cache.num_stages -= 2
8181
cache.step = 1
82+
cache.hist_iter = iter
8283
end
8384
end
8485
return integrator.dt / qacc
@@ -88,3 +89,21 @@ function step_reject_controller!(integrator, controller::PredictiveController, a
8889
@unpack dt, success_iter, qold = integrator
8990
integrator.dt = success_iter == 0 ? 0.1 * dt : dt / qold
9091
end
92+
93+
function step_reject_controller!(integrator, controller::PredictiveController, alg::AdaptiveRadau)
94+
@unpack dt, success_iter, qold = integrator
95+
@unpack cache = integrator
96+
@unpack num_stages, step, iter, hist_iter = cache
97+
integrator.dt = success_iter == 0 ? 0.1 * dt : dt / qold
98+
cache.step = step + 1
99+
hist_iter = hist_iter * 0.8 + iter * 0.2
100+
cache.hist_iter = hist_iter
101+
if (step > 10)
102+
if ((hist_iter > 8 || cache.status == VerySlowConvergence || cache.status == Divergence) && num_stages > alg.min_stages)
103+
cache.num_stages -= 2
104+
cache.step = 1
105+
cache.hist_iter = iter
106+
end
107+
end
108+
end
109+

lib/OrdinaryDiffEqFIRK/src/firk_caches.jl

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -488,8 +488,7 @@ mutable struct AdaptiveRadauConstantCache{F, Tab, Tol, Dt, U, JType} <:
488488
J::JType
489489
num_stages::Int
490490
step::Int
491-
θ::BigFloat
492-
θprev::BigFloat
491+
hist_iter::Float64
493492
end
494493

495494
function alg_cache(alg::AdaptiveRadau, u, rate_prototype, ::Type{uEltypeNoUnits},
@@ -515,11 +514,11 @@ function alg_cache(alg::AdaptiveRadau, u, rate_prototype, ::Type{uEltypeNoUnits}
515514
κ = alg.κ !== nothing ? convert(uToltype, alg.κ) : convert(uToltype, 1 // 100)
516515
J = false .* _vec(rate_prototype) .* _vec(rate_prototype)'
517516
AdaptiveRadauConstantCache(uf, tabs, κ, one(uToltype), 10000, cont, dt, dt,
518-
Convergence, J, num_stages, 1, big"1.0", big"1.0")
517+
Convergence, J, num_stages, 1, 0.0)
519518
end
520519

521520
mutable struct AdaptiveRadauCache{uType, cuType, tType, uNoUnitsType, rateType, JType, W1Type, W2Type,
522-
UF, JC, F1, F2, Tab, Tol, Dt, rTol, aTol, StepLimiter} <:
521+
UF, JC, F1, F2, #=F3,=# Tab, Tol, Dt, rTol, aTol, StepLimiter} <:
523522
FIRKMutableCache
524523
u::uType
525524
uprev::uType
@@ -551,6 +550,7 @@ mutable struct AdaptiveRadauCache{uType, cuType, tType, uNoUnitsType, rateType,
551550
jac_config::JC
552551
linsolve1::F1 #real
553552
linsolve2::Vector{F2} #complex
553+
#linres2::Vector{F3}
554554
rtol::rTol
555555
atol::aTol
556556
dtprev::Dt
@@ -559,8 +559,7 @@ mutable struct AdaptiveRadauCache{uType, cuType, tType, uNoUnitsType, rateType,
559559
step_limiter!::StepLimiter
560560
num_stages::Int
561561
step::Int
562-
θ::BigFloat
563-
θprev::BigFloat
562+
hist_iter::Float64
564563
end
565564

566565
function alg_cache(alg::AdaptiveRadau, u, rate_prototype, ::Type{uEltypeNoUnits},
@@ -598,24 +597,19 @@ function alg_cache(alg::AdaptiveRadau, u, rate_prototype, ::Type{uEltypeNoUnits}
598597
recursivefill!.(dw2, false)
599598
cubuff = [similar(u, Complex{eltype(u)}) for _ in 1 : (max - 1) ÷ 2]
600599
recursivefill!.(cubuff, false)
601-
dw = Vector{typeof(u)}(undef, max - 1)
600+
dw = [zero(u) for i in 1 : max]
602601

603-
cont = Vector{typeof(u)}(undef, max)
604-
for i in 1 : max
605-
cont[i] = zero(u)
606-
end
602+
cont = [zero(u) for i in 1:max]
607603

608604
derivatives = Matrix{typeof(u)}(undef, max, max)
609605
for i in 1 : max, j in 1 : max
610606
derivatives[i, j] = zero(u)
611607
end
612608

613609
fsalfirst = zero(rate_prototype)
614-
fw = Vector{typeof(rate_prototype)}(undef, max)
615-
ks = Vector{typeof(rate_prototype)}(undef, max)
616-
for i in 1: max
617-
ks[i] = fw[i] = zero(rate_prototype)
618-
end
610+
fw = [zero(rate_prototype) for i in 1 : max]
611+
ks = [zero(rate_prototype) for i in 1 : max]
612+
619613
k = ks[1]
620614

621615
J, W1 = build_J_W(alg, u, uprev, p, t, dt, f, uEltypeNoUnits, Val(true))
@@ -642,7 +636,14 @@ function alg_cache(alg::AdaptiveRadau, u, rate_prototype, ::Type{uEltypeNoUnits}
642636
linsolve2 = [
643637
init(LinearProblem(W2[i], _vec(cubuff[i]); u0 = _vec(dw2[i])), alg.linsolve, alias_A = true, alias_b = true,
644638
assumptions = LinearSolve.OperatorAssumptions(true)) for i in 1 : (max - 1) ÷ 2]
645-
639+
#=
640+
linres_tmp = dolinsolve(nothing, linsolve2[1]; A = W2[1], b = _vec(cubuff[1]), linu = _vec(dw2[1]))
641+
linres2 = Vector{typeof(linres_tmp)}(undef , (max - 1) ÷ 2)
642+
linres2[1] = linres_tmp
643+
for i in 2 : (num_stages - 1) ÷ 2
644+
linres2[i] = dolinsolve(nothing, linsolve2[1]; A = W2[1], b = _vec(cubuff[i]), linu = _vec(dw2[i]))
645+
end
646+
=#
646647
rtol = reltol isa Number ? reltol : zero(reltol)
647648
atol = reltol isa Number ? reltol : zero(reltol)
648649

@@ -652,7 +653,7 @@ function alg_cache(alg::AdaptiveRadau, u, rate_prototype, ::Type{uEltypeNoUnits}
652653
J, W1, W2,
653654
uf, tabs, κ, one(uToltype), 10000, tmp,
654655
atmp, jac_config,
655-
linsolve1, linsolve2, rtol, atol, dt, dt,
656-
Convergence, alg.step_limiter!, num_stages, 1, big"1.0", big"1.0")
656+
linsolve1, linsolve2, #=linres2,=# rtol, atol, dt, dt,
657+
Convergence, alg.step_limiter!, num_stages, 1, 0.0)
657658
end
658659

0 commit comments

Comments
 (0)