Skip to content

Commit d7e1300

Browse files
format
1 parent c82d8dd commit d7e1300

File tree

7 files changed

+104
-87
lines changed

7 files changed

+104
-87
lines changed

src/OrdinaryDiffEq.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ PrecompileTools.@compile_workload begin
261261
]
262262

263263
default_ode = [
264-
DefaultODEAlgorithm(autodiff=false),
264+
DefaultODEAlgorithm(autodiff = false),
265265
DefaultODEAlgorithm()
266266
]
267267

src/alg_utils.jl

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -208,33 +208,33 @@ qmax_default(alg::Union{RadauIIA3, RadauIIA5}) = 8
208208

209209
function has_chunksize(alg::OrdinaryDiffEqAlgorithm)
210210
return alg isa Union{OrdinaryDiffEqExponentialAlgorithm,
211-
OrdinaryDiffEqAdaptiveExponentialAlgorithm,
212-
OrdinaryDiffEqImplicitAlgorithm,
213-
OrdinaryDiffEqAdaptiveImplicitAlgorithm,
214-
DAEAlgorithm,
215-
CompositeAlgorithm}
211+
OrdinaryDiffEqAdaptiveExponentialAlgorithm,
212+
OrdinaryDiffEqImplicitAlgorithm,
213+
OrdinaryDiffEqAdaptiveImplicitAlgorithm,
214+
DAEAlgorithm,
215+
CompositeAlgorithm}
216216
end
217217
function get_chunksize(alg::OrdinaryDiffEqAlgorithm)
218218
error("This algorithm does not have a chunk size defined.")
219219
end
220220
function get_chunksize(alg::Union{OrdinaryDiffEqExponentialAlgorithm{CS},
221-
OrdinaryDiffEqAdaptiveExponentialAlgorithm{CS},
222-
OrdinaryDiffEqImplicitAlgorithm{CS},
223-
OrdinaryDiffEqAdaptiveImplicitAlgorithm{CS},
224-
DAEAlgorithm{CS},
225-
CompositeAlgorithm{CS}}) where {CS}
221+
OrdinaryDiffEqAdaptiveExponentialAlgorithm{CS},
222+
OrdinaryDiffEqImplicitAlgorithm{CS},
223+
OrdinaryDiffEqAdaptiveImplicitAlgorithm{CS},
224+
DAEAlgorithm{CS},
225+
CompositeAlgorithm{CS}}) where {CS}
226226
Val(CS)
227227
end
228228

229229
function get_chunksize_int(alg::OrdinaryDiffEqAlgorithm)
230230
error("This algorithm does not have a chunk size defined.")
231231
end
232232
function get_chunksize_int(alg::Union{OrdinaryDiffEqExponentialAlgorithm{CS},
233-
OrdinaryDiffEqAdaptiveExponentialAlgorithm{CS},
234-
OrdinaryDiffEqImplicitAlgorithm{CS},
235-
OrdinaryDiffEqAdaptiveImplicitAlgorithm{CS},
236-
DAEAlgorithm{CS},
237-
CompositeAlgorithm{CS}}) where {CS}
233+
OrdinaryDiffEqAdaptiveExponentialAlgorithm{CS},
234+
OrdinaryDiffEqImplicitAlgorithm{CS},
235+
OrdinaryDiffEqAdaptiveImplicitAlgorithm{CS},
236+
DAEAlgorithm{CS},
237+
CompositeAlgorithm{CS}}) where {CS}
238238
CS
239239
end
240240
# get_chunksize(alg::CompositeAlgorithm) = get_chunksize(alg.algs[alg.current_alg])
@@ -1082,4 +1082,7 @@ is_mass_matrix_alg(alg::CompositeAlgorithm) = all(is_mass_matrix_alg, alg.algs)
10821082
is_mass_matrix_alg(alg::RosenbrockAlgorithm) = true
10831083
is_mass_matrix_alg(alg::NewtonAlgorithm) = !isesdirk(alg)
10841084
# hack for the default alg
1085-
is_mass_matrix_alg(alg::CompositeAlgorithm{<:Any, <:Tuple{Tsit5, Vern7, Rosenbrock23, Rodas5P, FBDF, FBDF}}) = true
1085+
function is_mass_matrix_alg(alg::CompositeAlgorithm{
1086+
<:Any, <:Tuple{Tsit5, Vern7, Rosenbrock23, Rodas5P, FBDF, FBDF}})
1087+
true
1088+
end

src/algorithms.jl

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3242,8 +3242,8 @@ end
32423242
struct CompositeAlgorithm{CS, T, F} <: OrdinaryDiffEqCompositeAlgorithm
32433243
algs::T
32443244
choice_function::F
3245-
function CompositeAlgorithm(algs::T, choice_function::F) where {T,F}
3246-
CS = mapreduce(alg->has_chunksize(alg) ? get_chunksize_int(alg) : 0, max, algs)
3245+
function CompositeAlgorithm(algs::T, choice_function::F) where {T, F}
3246+
CS = mapreduce(alg -> has_chunksize(alg) ? get_chunksize_int(alg) : 0, max, algs)
32473247
new{CS, T, F}(algs, choice_function)
32483248
end
32493249
end
@@ -3269,33 +3269,32 @@ mutable struct AutoSwitchCache{nAlg, sAlg, tolType, T}
32693269
switch_max::Int
32703270
current::Int
32713271
function AutoSwitchCache(count::Int,
3272-
successive_switches::Int,
3273-
nonstiffalg::nAlg,
3274-
stiffalg::sAlg,
3275-
is_stiffalg::Bool,
3276-
maxstiffstep::Int,
3277-
maxnonstiffstep::Int,
3278-
nonstifftol::tolType,
3279-
stifftol::tolType,
3280-
dtfac::T,
3281-
stiffalgfirst::Bool,
3282-
switch_max::Int,
3283-
current::Int=0) where {nAlg, sAlg, tolType, T}
3272+
successive_switches::Int,
3273+
nonstiffalg::nAlg,
3274+
stiffalg::sAlg,
3275+
is_stiffalg::Bool,
3276+
maxstiffstep::Int,
3277+
maxnonstiffstep::Int,
3278+
nonstifftol::tolType,
3279+
stifftol::tolType,
3280+
dtfac::T,
3281+
stiffalgfirst::Bool,
3282+
switch_max::Int,
3283+
current::Int = 0) where {nAlg, sAlg, tolType, T}
32843284
new{nAlg, sAlg, tolType, T}(count,
3285-
successive_switches,
3286-
nonstiffalg,
3287-
stiffalg,
3288-
is_stiffalg,
3289-
maxstiffstep,
3290-
maxnonstiffstep,
3291-
nonstifftol,
3292-
stifftol,
3293-
dtfac,
3294-
stiffalgfirst,
3295-
switch_max,
3296-
current)
3285+
successive_switches,
3286+
nonstiffalg,
3287+
stiffalg,
3288+
is_stiffalg,
3289+
maxstiffstep,
3290+
maxnonstiffstep,
3291+
nonstifftol,
3292+
stifftol,
3293+
dtfac,
3294+
stiffalgfirst,
3295+
switch_max,
3296+
current)
32973297
end
3298-
32993298
end
33003299

33013300
struct AutoSwitch{nAlg, sAlg, tolType, T}

src/caches/basic_caches.jl

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ mutable struct DefaultCache{T1, T2, T3, T4, T5, T6, A, F} <: OrdinaryDiffEqCache
2222
cache4::T4
2323
cache5::T5
2424
cache6::T6
25-
function DefaultCache{T1, T2, T3, T4, T5, T6, F}(args, choice_function, current) where {T1, T2, T3, T4, T5, T6, F}
25+
function DefaultCache{T1, T2, T3, T4, T5, T6, F}(
26+
args, choice_function, current) where {T1, T2, T3, T4, T5, T6, F}
2627
new{T1, T2, T3, T4, T5, T6, typeof(args), F}(args, choice_function, current)
2728
end
2829
end
@@ -44,21 +45,22 @@ function alg_cache(alg::CompositeAlgorithm, u, rate_prototype, ::Type{uEltypeNoU
4445
end
4546

4647
function alg_cache(alg::CompositeAlgorithm{CS, Tuple{A1, A2, A3, A4, A5, A6}}, u,
47-
rate_prototype, ::Type{uEltypeNoUnits}, ::Type{uBottomEltypeNoUnits}, ::Type{tTypeNoUnits},
48-
uprev, uprev2, f, t, dt, reltol, p, calck,
49-
::Val{V}) where {CS, V, uEltypeNoUnits, uBottomEltypeNoUnits, tTypeNoUnits, A1, A2, A3, A4, A5, A6}
50-
48+
rate_prototype, ::Type{uEltypeNoUnits}, ::Type{uBottomEltypeNoUnits}, ::Type{tTypeNoUnits},
49+
uprev, uprev2, f, t, dt, reltol, p, calck,
50+
::Val{V}) where {
51+
CS, V, uEltypeNoUnits, uBottomEltypeNoUnits, tTypeNoUnits, A1, A2, A3, A4, A5, A6}
5152
args = (u, rate_prototype, uEltypeNoUnits,
52-
uBottomEltypeNoUnits, tTypeNoUnits, uprev, uprev2, f, t, dt,
53-
reltol, p, calck, Val(V))
53+
uBottomEltypeNoUnits, tTypeNoUnits, uprev, uprev2, f, t, dt,
54+
reltol, p, calck, Val(V))
5455
argT = map(typeof, args)
5556
T1 = Base.promote_op(alg_cache, A1, argT...)
5657
T2 = Base.promote_op(alg_cache, A2, argT...)
5758
T3 = Base.promote_op(alg_cache, A3, argT...)
5859
T4 = Base.promote_op(alg_cache, A4, argT...)
5960
T5 = Base.promote_op(alg_cache, A5, argT...)
6061
T6 = Base.promote_op(alg_cache, A6, argT...)
61-
DefaultCache{T1, T2, T3, T4, T5, T6, typeof(alg.choice_function)}(args, alg.choice_function, 1)
62+
DefaultCache{T1, T2, T3, T4, T5, T6, typeof(alg.choice_function)}(
63+
args, alg.choice_function, 1)
6264
end
6365

6466
# map + closure approach doesn't infer

src/composite_algs.jl

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ end
3131

3232
function (AS::AutoSwitchCache)(integrator)
3333
#horrible awful hack
34-
isdefault = integrator.alg isa CompositeAlgorithm{<:Any, <:Tuple{Tsit5, Vern7, Rosenbrock23, Rodas5P, FBDF, FBDF}}
34+
isdefault = integrator.alg isa CompositeAlgorithm{
35+
<:Any, <:Tuple{Tsit5, Vern7, Rosenbrock23, Rodas5P, FBDF, FBDF}}
3536
if isdefault
3637
return default_autoswitch(AS, integrator)
3738
end
@@ -58,7 +59,8 @@ function (AS::AutoSwitchCache)(integrator)
5859
return AS.current
5960
end
6061

61-
function AutoAlgSwitch(nonstiffalg::OrdinaryDiffEqAlgorithm, stiffalg::OrdinaryDiffEqAlgorithm; kwargs...)
62+
function AutoAlgSwitch(
63+
nonstiffalg::OrdinaryDiffEqAlgorithm, stiffalg::OrdinaryDiffEqAlgorithm; kwargs...)
6264
AS = AutoSwitch(nonstiffalg, stiffalg; kwargs...)
6365
CompositeAlgorithm((nonstiffalg, stiffalg), AS)
6466
end
@@ -94,23 +96,29 @@ const EXTREME_TOL = 1e-9
9496
const SMALLSIZE = 50
9597
const MEDIUMSIZE = 500
9698
const STABILITY_SIZES = (alg_stability_size(Tsit5()), alg_stability_size(Vern7()))
97-
const DEFAULTBETA2S = (beta2_default(Tsit5()), beta2_default(Vern7()), beta2_default(Rosenbrock23()), beta2_default(Rodas5P()), beta2_default(FBDF()), beta2_default(FBDF()))
98-
const DEFAULTBETA1S = (beta1_default(Tsit5(),DEFAULTBETA2S[1]), beta1_default(Vern7(),DEFAULTBETA2S[2]),
99-
beta1_default(Rosenbrock23(), DEFAULTBETA2S[3]), beta1_default(Rodas5P(), DEFAULTBETA2S[4]),
100-
beta1_default(FBDF(), DEFAULTBETA2S[5]), beta1_default(FBDF(), DEFAULTBETA2S[6]))
99+
const DEFAULTBETA2S = (
100+
beta2_default(Tsit5()), beta2_default(Vern7()), beta2_default(Rosenbrock23()),
101+
beta2_default(Rodas5P()), beta2_default(FBDF()), beta2_default(FBDF()))
102+
const DEFAULTBETA1S = (
103+
beta1_default(Tsit5(), DEFAULTBETA2S[1]), beta1_default(Vern7(), DEFAULTBETA2S[2]),
104+
beta1_default(Rosenbrock23(), DEFAULTBETA2S[3]), beta1_default(
105+
Rodas5P(), DEFAULTBETA2S[4]),
106+
beta1_default(FBDF(), DEFAULTBETA2S[5]), beta1_default(FBDF(), DEFAULTBETA2S[6]))
101107

102108
callbacks_exists(integrator) = !isempty(integrator.opts.callbacks)
103-
current_nonstiff(current) = ifelse(current <= NUM_NONSTIFF,current,current-NUM_STIFF)
109+
current_nonstiff(current) = ifelse(current <= NUM_NONSTIFF, current, current - NUM_STIFF)
104110

105111
function DefaultODEAlgorithm(; lazy = true, stiffalgfirst = false, kwargs...)
106112
nonstiff = (Tsit5(), Vern7(lazy = lazy))
107-
stiff = (Rosenbrock23(;kwargs...), Rodas5P(;kwargs...), FBDF(;kwargs...), FBDF(;linsolve = LinearSolve.KrylovJL_GMRES()))
113+
stiff = (Rosenbrock23(; kwargs...), Rodas5P(; kwargs...), FBDF(; kwargs...),
114+
FBDF(; linsolve = LinearSolve.KrylovJL_GMRES()))
108115
AutoAlgSwitch(nonstiff, stiff; stiffalgfirst)
109116
end
110117

111118
function is_stiff(integrator, alg, ntol, stol, is_stiffalg, current)
112119
eigen_est, dt = integrator.eigen_est, integrator.dt
113-
stiffness = abs(eigen_est * dt / STABILITY_SIZES[nonstiffchoice(integrator.opts.reltol)]) # `abs` here is just for safety
120+
stiffness = abs(eigen_est * dt /
121+
STABILITY_SIZES[nonstiffchoice(integrator.opts.reltol)]) # `abs` here is just for safety
114122
tol = is_stiffalg ? stol : ntol
115123
os = oneunit(stiffness)
116124
bool = stiffness > os * tol

src/solve.jl

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,8 @@ function DiffEqBase.__init(
415415
differential_vars = prob isa DAEProblem ? prob.differential_vars :
416416
get_differential_vars(f, u)
417417

418-
id = InterpolationData(f, timeseries, ts, ks, alg_choice, dense, cache, differential_vars, false)
418+
id = InterpolationData(
419+
f, timeseries, ts, ks, alg_choice, dense, cache, differential_vars, false)
419420
sol = DiffEqBase.build_solution(prob, _alg, ts, timeseries,
420421
dense = dense, k = ks, interp = id, alg_choice = alg_choice,
421422
calculate_error = false, stats = stats)
@@ -533,10 +534,10 @@ function DiffEqBase.__init(
533534
end
534535

535536
function DiffEqBase.__init(prob::ODEProblem, ::Nothing, args...; kwargs...)
536-
DiffEqBase.init(prob, DefaultODEAlgorithm(autodiff=false), args...; kwargs...)
537+
DiffEqBase.init(prob, DefaultODEAlgorithm(autodiff = false), args...; kwargs...)
537538
end
538539
function DiffEqBase.__solve(prob::ODEProblem, ::Nothing, args...; kwargs...)
539-
DiffEqBase.solve(prob, DefaultODEAlgorithm(autodiff=false), args...; kwargs...)
540+
DiffEqBase.solve(prob, DefaultODEAlgorithm(autodiff = false), args...; kwargs...)
540541
end
541542

542543
function DiffEqBase.solve!(integrator::ODEIntegrator)

test/interface/default_solver_tests.jl

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,15 @@ tsitsol = solve(prob_ode_2Dlinear, Tsit5())
1111
@test sol.stats.nf == tsitsol.stats.nf
1212
@test all(isequal(1), sol.alg_choice)
1313

14-
sol = solve(prob_ode_2Dlinear, reltol=1e-10)
15-
vernsol = solve(prob_ode_2Dlinear, Vern7(), reltol=1e-10)
14+
sol = solve(prob_ode_2Dlinear, reltol = 1e-10)
15+
vernsol = solve(prob_ode_2Dlinear, Vern7(), reltol = 1e-10)
1616
# test that default isn't much worse than Tsit5 (we expect it to use Tsit5 for this).
1717
@test sol.stats.naccept == vernsol.stats.naccept
1818
@test sol.stats.nf == vernsol.stats.nf
1919
@test all(isequal(2), sol.alg_choice)
2020

21-
prob_ode_linear_fast = ODEProblem(ODEFunction(f_2dlinear, mass_matrix=2*I(2)), rand(2), (0.0, 1.0), 1.01)
21+
prob_ode_linear_fast = ODEProblem(
22+
ODEFunction(f_2dlinear, mass_matrix = 2 * I(2)), rand(2), (0.0, 1.0), 1.01)
2223
sol = solve(prob_ode_linear_fast)
2324
@test all(isequal(3), sol.alg_choice)
2425
# for some reason the timestepping here is different from regular Rosenbrock23 (including the initial timestep)
@@ -27,56 +28,59 @@ function rober(u, p, t)
2728
y₁, y₂, y₃ = u
2829
k₁, k₂, k₃ = p
2930
[-k₁ * y₁ + k₃ * y₂ * y₃,
30-
k₁ * y₁ - k₃ * y₂ * y₃ - k₂ * y₂^2,
31-
k₂ * y₂^2]
31+
k₁ * y₁ - k₃ * y₂ * y₃ - k₂ * y₂^2,
32+
k₂ * y₂^2]
3233
end
33-
prob_rober = ODEProblem(rober, [1.0,0.0,0.0],(0.0,1e3),(0.04,3e7,1e4))
34+
prob_rober = ODEProblem(rober, [1.0, 0.0, 0.0], (0.0, 1e3), (0.04, 3e7, 1e4))
3435
sol = solve(prob_rober)
35-
rosensol = solve(prob_rober, AutoTsit5(Rosenbrock23(autodiff=false)))
36+
rosensol = solve(prob_rober, AutoTsit5(Rosenbrock23(autodiff = false)))
3637
# test that default has the same performance as AutoTsit5(Rosenbrock23()) (which we expect it to use for this).
3738
@test sol.stats.naccept == rosensol.stats.naccept
3839
@test sol.stats.nf == rosensol.stats.nf
39-
@test unique(sol.alg_choice) == [1,3]
40+
@test unique(sol.alg_choice) == [1, 3]
4041
@test sol.alg_choice[1] == 1
4142
@test sol.alg_choice[end] == 3
4243

43-
sol = solve(prob_rober, reltol=1e-7, abstol=1e-7)
44-
rosensol = solve(prob_rober, AutoVern7(Rodas5P(autodiff=false)), reltol=1e-7, abstol=1e-7)
44+
sol = solve(prob_rober, reltol = 1e-7, abstol = 1e-7)
45+
rosensol = solve(
46+
prob_rober, AutoVern7(Rodas5P(autodiff = false)), reltol = 1e-7, abstol = 1e-7)
4547
# test that default has the same performance as AutoTsit5(Rosenbrock23()) (which we expect it to use for this).
4648
@test sol.stats.naccept == rosensol.stats.naccept
4749
@test sol.stats.nf == rosensol.stats.nf
48-
@test unique(sol.alg_choice) == [2,4]
50+
@test unique(sol.alg_choice) == [2, 4]
4951
@test sol.alg_choice[1] == 2
5052
@test sol.alg_choice[end] == 4
5153

5254
function exrober(du, u, p, t)
5355
y₁, y₂, y₃ = u
5456
k₁, k₂, k₃ = p
55-
du .= vcat([-k₁ * y₁ + k₃ * y₂ * y₃,
56-
k₁ * y₁ - k₃ * y₂ * y₃ - k₂ * y₂^2,
57-
k₂ * y₂^2, ], u[4:end])
57+
du .= vcat(
58+
[-k₁ * y₁ + k₃ * y₂ * y₃,
59+
k₁ * y₁ - k₃ * y₂ * y₃ - k₂ * y₂^2,
60+
k₂ * y₂^2], u[4:end])
5861
end
5962

60-
for n in (100, ) # 600 should be added but currently is broken for unknown reasons
63+
for n in (100,) # 600 should be added but currently is broken for unknown reasons
6164
stiffalg = n < 50 ? 4 : n < 500 ? 5 : 6
6265
linsolve = n < 500 ? nothing : KrylovJL_GMRES()
63-
jac_prototype = sparse(I(n+3))
66+
jac_prototype = sparse(I(n + 3))
6467
jac_prototype[1:3, 1:3] .= 1.0
6568

66-
prob_ex_rober = ODEProblem(ODEFunction(exrober; jac_prototype), vcat([1.0,0.0,0.0], ones(n)),(0.0,100.0),(0.04,3e7,1e4))
69+
prob_ex_rober = ODEProblem(ODEFunction(exrober; jac_prototype),
70+
vcat([1.0, 0.0, 0.0], ones(n)), (0.0, 100.0), (0.04, 3e7, 1e4))
6771
sol = solve(prob_ex_rober)
68-
fsol = solve(prob_ex_rober, AutoTsit5(FBDF(;autodiff=false, linsolve)))
72+
fsol = solve(prob_ex_rober, AutoTsit5(FBDF(; autodiff = false, linsolve)))
6973
# test that default has the same performance as AutoTsit5(Rosenbrock23()) (which we expect it to use for this).
7074
@test sol.stats.naccept == fsol.stats.naccept
7175
@test sol.stats.nf == fsol.stats.nf
72-
@test unique(sol.alg_choice) == [1,stiffalg]
76+
@test unique(sol.alg_choice) == [1, stiffalg]
7377
end
7478

7579
function swaplinear(u, p, t)
76-
[u[2], u[1]].*p
80+
[u[2], u[1]] .* p
7781
end
78-
swaplinearf = ODEFunction(swaplinear, mass_matrix=ones(2,2)-I(2))
79-
prob_swaplinear = ODEProblem(swaplinearf, rand(2), (0., 1.), 1.01)
80-
sol = solve(prob_swaplinear, reltol=1e-7) # reltol must be set to avoid running into a bug with Rosenbrock23
82+
swaplinearf = ODEFunction(swaplinear, mass_matrix = ones(2, 2) - I(2))
83+
prob_swaplinear = ODEProblem(swaplinearf, rand(2), (0.0, 1.0), 1.01)
84+
sol = solve(prob_swaplinear, reltol = 1e-7) # reltol must be set to avoid running into a bug with Rosenbrock23
8185
@test all(isequal(4), sol.alg_choice)
8286
# for some reason the timestepping here is different from regular Rodas5P (including the initial timestep)

0 commit comments

Comments
 (0)