-
-
Notifications
You must be signed in to change notification settings - Fork 31
/
BoundaryValueDiffEq.jl
278 lines (225 loc) · 9.31 KB
/
BoundaryValueDiffEq.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
module BoundaryValueDiffEq
import PrecompileTools: @compile_workload, @setup_workload
using ADTypes, Adapt, DiffEqBase, ForwardDiff, LinearAlgebra, NonlinearSolve,
OrdinaryDiffEq, Preferences, RecursiveArrayTools, Reexport, SciMLBase, Setfield,
SparseDiffTools
using PreallocationTools: PreallocationTools, DiffCache
# Special Matrix Types
using BandedMatrices, FastAlmostBandedMatrices, SparseArrays
import ADTypes: AbstractADType
import ArrayInterface: matrix_colors, parameterless_type, undefmatrix, fast_scalar_indexing
import ConcreteStructs: @concrete
import DiffEqBase: solve
import FastClosures: @closure
import ForwardDiff: ForwardDiff, pickchunksize
import Logging
import RecursiveArrayTools: ArrayPartition, DiffEqArray
import SciMLBase: AbstractDiffEqInterpolation, StandardBVProblem, __solve, _unwrap_val
@reexport using ADTypes, DiffEqBase, NonlinearSolve, OrdinaryDiffEq, SparseDiffTools,
SciMLBase
include("types.jl")
include("utils.jl")
include("algorithms.jl")
include("alg_utils.jl")
include("mirk_tableaus.jl")
include("solve/single_shooting.jl")
include("solve/multiple_shooting.jl")
include("solve/mirk.jl")
include("collocation.jl")
include("sparse_jacobians.jl")
include("adaptivity.jl")
include("interpolation.jl")
include("default_nlsolve.jl")
function __solve(prob::BVProblem, alg::BoundaryValueDiffEqAlgorithm, args...; kwargs...)
cache = init(prob, alg, args...; kwargs...)
return solve!(cache)
end
@setup_workload begin
f1! = @closure (du, u, p, t) -> begin
du[1] = u[2]
du[2] = 0
end
f1 = @closure (u, p, t) -> [u[2], 0]
bc1! = @closure (residual, u, p, t) -> begin
residual[1] = u[1][1] - 5
residual[2] = u[lastindex(u)][1]
end
bc1 = @closure (u, p, t) -> [u[1][1] - 5, u[lastindex(u)][1]]
bc1_a! = @closure (residual, ua, p) -> (residual[1] = ua[1] - 5)
bc1_b! = @closure (residual, ub, p) -> (residual[1] = ub[1])
bc1_a = @closure (ua, p) -> [ua[1] - 5]
bc1_b = @closure (ub, p) -> [ub[1]]
tspan = (0.0, 5.0)
u0 = [5.0, -3.5]
bcresid_prototype = (Array{Float64}(undef, 1), Array{Float64}(undef, 1))
probs = [BVProblem(f1!, bc1!, u0, tspan; nlls = Val(false)),
BVProblem(f1, bc1, u0, tspan; nlls = Val(false)),
TwoPointBVProblem(
f1!, (bc1_a!, bc1_b!), u0, tspan; bcresid_prototype, nlls = Val(false)),
TwoPointBVProblem(
f1, (bc1_a, bc1_b), u0, tspan; bcresid_prototype, nlls = Val(false))]
algs = []
jac_alg = BVPJacobianAlgorithm(AutoForwardDiff(; chunksize = 2))
if Preferences.@load_preference("PrecompileMIRK", true)
append!(algs, [MIRK2(; jac_alg), MIRK4(; jac_alg), MIRK6(; jac_alg)])
end
@compile_workload begin
@sync for prob in probs, alg in algs
Threads.@spawn solve(prob, alg; dt = 0.2)
end
end
f1_nlls! = @closure (du, u, p, t) -> begin
du[1] = u[2]
du[2] = -u[1]
end
f1_nlls = @closure (u, p, t) -> [u[2], -u[1]]
bc1_nlls! = @closure (resid, sol, p, t) -> begin
solₜ₁ = sol[1]
solₜ₂ = sol[lastindex(sol)]
resid[1] = solₜ₁[1]
resid[2] = solₜ₂[1] - 1
resid[3] = solₜ₂[2] + 1.729109
return nothing
end
bc1_nlls = @closure (sol, p, t) -> [
sol[1][1], sol[lastindex(sol)][1] - 1, sol[lastindex(sol)][2] + 1.729109]
bc1_nlls_a! = @closure (resid, ua, p) -> (resid[1] = ua[1])
bc1_nlls_b! = @closure (resid, ub, p) -> (resid[1] = ub[1] - 1;
resid[2] = ub[2] + 1.729109)
bc1_nlls_a = @closure (ua, p) -> [ua[1]]
bc1_nlls_b = @closure (ub, p) -> [ub[1] - 1, ub[2] + 1.729109]
tspan = (0.0, 100.0)
u0 = [0.0, 1.0]
bcresid_prototype1 = Array{Float64}(undef, 3)
bcresid_prototype2 = (Array{Float64}(undef, 1), Array{Float64}(undef, 2))
probs = [
BVProblem(BVPFunction(f1_nlls!, bc1_nlls!; bcresid_prototype = bcresid_prototype1),
u0, tspan, nlls = Val(true)),
BVProblem(BVPFunction(f1_nlls, bc1_nlls; bcresid_prototype = bcresid_prototype1),
u0, tspan, nlls = Val(true)),
TwoPointBVProblem(f1_nlls!, (bc1_nlls_a!, bc1_nlls_b!), u0, tspan;
bcresid_prototype = bcresid_prototype2, nlls = Val(true)),
TwoPointBVProblem(f1_nlls, (bc1_nlls_a, bc1_nlls_b), u0, tspan;
bcresid_prototype = bcresid_prototype2, nlls = Val(true))]
jac_alg = BVPJacobianAlgorithm(AutoForwardDiff(; chunksize = 2))
nlsolvers = [LevenbergMarquardt(; disable_geodesic = Val(true)), GaussNewton()]
algs = []
if Preferences.@load_preference("PrecompileMIRKNLLS", false)
for nlsolve in nlsolvers
append!(algs, [MIRK2(; jac_alg, nlsolve), MIRK6(; jac_alg, nlsolve)])
end
end
@compile_workload begin
@sync for prob in probs, alg in algs
Threads.@spawn solve(prob, alg; dt = 0.2, abstol = 1e-2)
end
end
bc1! = @closure (residual, u, p, t) -> begin
residual[1] = u(0.0)[1] - 5
residual[2] = u(5.0)[1]
end
bc1 = @closure (u, p, t) -> [u(0.0)[1] - 5, u(5.0)[1]]
tspan = (0.0, 5.0)
u0 = [5.0, -3.5]
bcresid_prototype = (Array{Float64}(undef, 1), Array{Float64}(undef, 1))
probs = [BVProblem(BVPFunction{true}(f1!, bc1!), u0, tspan; nlls = Val(false)),
BVProblem(BVPFunction{false}(f1, bc1), u0, tspan; nlls = Val(false)),
BVProblem(
BVPFunction{true}(
f1!, (bc1_a!, bc1_b!); bcresid_prototype, twopoint = Val(true)),
u0,
tspan;
nlls = Val(false)),
BVProblem(
BVPFunction{false}(f1, (bc1_a, bc1_b); bcresid_prototype, twopoint = Val(true)),
u0, tspan; nlls = Val(false))]
algs = []
if @load_preference("PrecompileShooting", true)
push!(algs,
Shooting(Tsit5(); nlsolve = NewtonRaphson(),
jac_alg = BVPJacobianAlgorithm(AutoForwardDiff(; chunksize = 2))))
end
if @load_preference("PrecompileMultipleShooting", true)
push!(algs,
MultipleShooting(10,
Tsit5();
nlsolve = NewtonRaphson(),
jac_alg = BVPJacobianAlgorithm(;
bc_diffmode = AutoForwardDiff(; chunksize = 2),
nonbc_diffmode = AutoSparse(AutoForwardDiff(; chunksize = 2)))))
end
@compile_workload begin
@sync for prob in probs, alg in algs
Threads.@spawn solve(prob, alg)
end
end
bc1_nlls! = @closure (resid, sol, p, t) -> begin
solₜ₁ = sol(0.0)
solₜ₂ = sol(100.0)
resid[1] = solₜ₁[1]
resid[2] = solₜ₂[1] - 1
resid[3] = solₜ₂[2] + 1.729109
return nothing
end
bc1_nlls = @closure (sol, p, t) -> [
sol(0.0)[1], sol(100.0)[1] - 1, sol(1.0)[2] + 1.729109]
tspan = (0.0, 100.0)
u0 = [0.0, 1.0]
bcresid_prototype1 = Array{Float64}(undef, 3)
bcresid_prototype2 = (Array{Float64}(undef, 1), Array{Float64}(undef, 2))
probs = [
BVProblem(
BVPFunction{true}(f1_nlls!, bc1_nlls!; bcresid_prototype = bcresid_prototype1),
u0, tspan; nlls = Val(true)),
BVProblem(
BVPFunction{false}(f1_nlls, bc1_nlls; bcresid_prototype = bcresid_prototype1),
u0, tspan; nlls = Val(true)),
BVProblem(
BVPFunction{true}(f1_nlls!, (bc1_nlls_a!, bc1_nlls_b!);
bcresid_prototype = bcresid_prototype2, twopoint = Val(true)),
u0,
tspan;
nlls = Val(true)),
BVProblem(
BVPFunction{false}(f1_nlls, (bc1_nlls_a, bc1_nlls_b);
bcresid_prototype = bcresid_prototype2, twopoint = Val(true)),
u0,
tspan;
nlls = Val(true))]
algs = []
if @load_preference("PrecompileShootingNLLS", true)
append!(algs,
[
Shooting(
Tsit5(); nlsolve = LevenbergMarquardt(; disable_geodesic = Val(true)),
jac_alg = BVPJacobianAlgorithm(AutoForwardDiff(; chunksize = 2))),
Shooting(Tsit5(); nlsolve = GaussNewton(),
jac_alg = BVPJacobianAlgorithm(AutoForwardDiff(; chunksize = 2)))])
end
if @load_preference("PrecompileMultipleShootingNLLS", true)
append!(algs,
[
MultipleShooting(10,
Tsit5();
nlsolve = LevenbergMarquardt(; disable_geodesic = Val(true)),
jac_alg = BVPJacobianAlgorithm(;
bc_diffmode = AutoForwardDiff(; chunksize = 2),
nonbc_diffmode = AutoSparse(AutoForwardDiff(; chunksize = 2)))),
MultipleShooting(10,
Tsit5();
nlsolve = GaussNewton(),
jac_alg = BVPJacobianAlgorithm(;
bc_diffmode = AutoForwardDiff(; chunksize = 2),
nonbc_diffmode = AutoSparse(AutoForwardDiff(; chunksize = 2))))])
end
@compile_workload begin
@sync for prob in probs, alg in algs
Threads.@spawn solve(prob, alg; nlsolve_kwargs = (; abstol = 1e-2))
end
end
end
export Shooting, MultipleShooting
export MIRK2, MIRK3, MIRK4, MIRK5, MIRK6
export BVPM2, BVPSOL, COLNEW # From ODEInterface.jl
export MIRKJacobianComputationAlgorithm, BVPJacobianAlgorithm
end