-
-
Notifications
You must be signed in to change notification settings - Fork 254
Expand file tree
/
Copy pathbvproblem.jl
More file actions
390 lines (333 loc) · 12.8 KB
/
bvproblem.jl
File metadata and controls
390 lines (333 loc) · 12.8 KB
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
### TODO: update when BoundaryValueDiffEqAscher is updated to use the normal boundary condition conventions
using OrdinaryDiffEq
using BoundaryValueDiffEqMIRK, BoundaryValueDiffEqAscher
using OptimizationIpopt
using ModelingToolkitBase
using SciMLBase
using ModelingToolkitBase: t_nounits as t, D_nounits as D
using Test
### Test Collocation solvers on simple problems
solvers = [MIRK4]
daesolvers = [Ascher2, Ascher4, Ascher6]
@testset "Lotka-Volterra" begin
@parameters α = 7.5 β = 4.0 γ = 8.0 δ = 5.0
@variables x(t) = 1.0 y(t) = 2.0
eqs = [
D(x) ~ α * x - β * x * y,
D(y) ~ -γ * y + δ * x * y,
]
u0map = [x => 1.0, y => 2.0]
parammap = [α => 7.5, β => 4, γ => 8.0, δ => 5.0]
tspan = (0.0, 10.0)
@mtkcompile lotkavolterra = System(eqs, t)
op = ODEProblem(lotkavolterra, [u0map; parammap], tspan)
osol = solve(op, Vern9())
bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}(
lotkavolterra, [u0map; parammap], tspan
)
for solver in solvers
sol = solve(bvp, solver(), dt = 0.01)
@test isapprox(sol.u[end], osol.u[end]; atol = 0.01)
@test sol[[x, y], 1] == [1.0, 2.0]
end
# Test out of place
bvp2 = SciMLBase.BVProblem{false, SciMLBase.AutoSpecialize}(
lotkavolterra, [u0map; parammap], tspan
)
for solver in solvers
sol = solve(bvp2, solver(), dt = 0.01)
@test isapprox(sol.u[end], osol.u[end]; atol = 0.01)
@test sol[[x, y], 1] == [1.0, 2.0]
end
end
### Testing on pendulum
@testset "Pendulum" begin
@parameters g = 9.81 L = 1.0
@variables θ(t) = π / 2 θ_t(t)
eqs = [
D(θ) ~ θ_t
D(θ_t) ~ -(g / L) * sin(θ)
]
@mtkcompile pend = System(eqs, t)
u0map = [θ => π / 2, θ_t => π / 2]
parammap = [:L => 1.0, :g => 9.81]
tspan = (0.0, 6.0)
op = ODEProblem(pend, [u0map; parammap], tspan)
osol = solve(op, Vern9(), abstol=1e-10, reltol=1e-10)
bvp = BVProblem(pend, [u0map; parammap], tspan)
for solver in solvers
sol = solve(bvp, solver(), dt = 1e-2)
@test isapprox(sol.u[end], osol.u[end])
@test sol.u[1] == [π / 2, π / 2]
end
# Test out-of-place
bvp2 = BVProblem{false, SciMLBase.FullSpecialize}(pend, [u0map; parammap], tspan)
for solver in solvers
sol = solve(bvp2, solver(), dt = 0.01)
@test isapprox(sol.u[end], osol.u[end])
@test sol.u[1] == [π / 2, π / 2]
end
end
##################################################################
### System with constraint equations, DAEs with constraints ###
##################################################################
# Test generation of boundary condition function using `generate_function_bc`. Compare solutions to manually written boundary conditions
@testset "Boundary Condition Compilation" begin
@parameters α = 1.5 β = 1.0 γ = 3.0 δ = 1.0
@variables x(..) y(..)
eqs = [
D(x(t)) ~ α * x(t) - β * x(t) * y(t),
D(y(t)) ~ -γ * y(t) + δ * x(t) * y(t),
]
tspan = (0.0, 1.0)
@mtkcompile lksys = System(eqs, t)
function lotkavolterra!(du, u, p, t)
du[1] = p[1] * u[1] - p[2] * u[1] * u[2]
du[2] = -p[4] * u[2] + p[3] * u[1] * u[2]
end
function lotkavolterra(u, p, t)
[p[1] * u[1] - p[2] * u[1] * u[2], -p[4] * u[2] + p[3] * u[1] * u[2]]
end
# Test with a constraint.
constr = [y(0.5) ~ 2.0]
@mtkcompile lksys = System(eqs, t; constraints = constr)
function bc!(resid, u, p, t)
resid[1] = u(0.0)[1] - 1.0
resid[2] = u(0.5)[2] - 2.0
end
function bc(u, p, t)
[u(0.0)[1] - 1.0, u(0.5)[2] - 2.0]
end
u0 = [1.0, 1.0]
tspan = (0.0, 1.0)
p = [1.5, 1.0, 1.0, 3.0]
bvpi1 = SciMLBase.BVProblem(lotkavolterra!, bc!, u0, tspan, p)
bvpi2 = SciMLBase.BVProblem(lotkavolterra, bc, u0, tspan, p)
bvpi3 = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}(
lksys, [x(t) => 1.0], tspan; guesses = [y(t) => 1.0]
)
bvpi4 = SciMLBase.BVProblem{false, SciMLBase.FullSpecialize}(
lksys, [x(t) => 1.0], tspan; guesses = [y(t) => 1.0]
)
sol1 = solve(bvpi1, MIRK4(), dt = 0.01)
sol2 = solve(bvpi2, MIRK4(), dt = 0.01)
sol3 = solve(bvpi3, MIRK4(), dt = 0.01)
sol4 = solve(bvpi4, MIRK4(), dt = 0.01)
@test sol1.t ≈ sol2.t ≈ sol3.t ≈ sol4.t
@test sol1.u ≈ sol2.u ≈ sol3[[x(t), y(t)]] ≈ sol4[[x(t), y(t)]]
# @test sol1 ≈ sol2 ≈ sol3 ≈ sol4 # don't get true equality here, not sure why
end
function test_solvers(
solvers, prob, u0map, constraints, equations = []; dt = 0.005, atol = 5.0e-6
)
for solver in solvers
println("Solver: $solver")
sol = solve(prob, solver(), dt = dt)
@test SciMLBase.successful_retcode(sol.retcode)
p = prob.p
t = sol.t
bc = prob.f.bc
ns = length(prob.u0)
if isinplace(prob.f)
resid = zeros(ns)
bc(resid, sol, p, t)
@test isapprox(zeros(ns), resid; atol)
@show resid
else
@test isapprox(zeros(ns), bc(sol, p, t); atol)
@show bc(sol, p, t)
end
for (k, v) in u0map
@test sol[k][1] == v
end
# for cons in constraints
# @test sol[cons.rhs - cons.lhs] ≈ 0
# end
for eq in equations
@test sol[eq] ≈ 0
end
end
return
end
# Simple System with BVP constraints.
@testset "ODE with constraints" begin
@parameters α = 1.5 β = 1.0 γ = 3.0 δ = 1.0
@variables x(t) y(t)
eqs = [
D(x) ~ α * x - β * x * y,
D(y) ~ -γ * y + δ * x * y,
]
u0map = []
tspan = (0.0, 1.0)
guess = [x => 4.0, y => 2.0]
constr = [EvalAt(0.6)(x) ~ 3.5, EvalAt(0.3)(x) ~ 7.0]
@mtkcompile lksys = System(eqs, t; constraints = constr)
bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}(
lksys, u0map, tspan; guesses = guess, cse = false
)
test_solvers(solvers, bvp, u0map, constr; dt = 1.0e-2)
# Testing that more complicated constraints give correct solutions.
constr = [EvalAt(0.2)(y) + EvalAt(0.8)(x) ~ 3.0, EvalAt(0.3)(y) ~ 2.0]
@mtkcompile lksys = System(eqs, t; constraints = constr)
bvp = SciMLBase.BVProblem{false, SciMLBase.FullSpecialize}(
lksys, u0map, tspan; guesses = guess
)
test_solvers(solvers, bvp, u0map, constr; dt = 1.0e-2)
constr = [α * β - EvalAt(0.6)(x) ~ 0.0, EvalAt(0.2)(y) ~ 3.0]
@mtkcompile lksys = System(eqs, t; constraints = constr)
bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}(
lksys, u0map, tspan; guesses = guess
)
test_solvers(solvers, bvp, u0map, constr; dt = 1.0e-2)
end
# Cartesian pendulum from the docs.
# DAE IVP solved using BoundaryValueDiffEq solvers.
# let
# @parameters g
# @variables x(t) y(t) [state_priority = 10] λ(t)
# eqs = [D(D(x)) ~ λ * x
# D(D(y)) ~ λ * y - g
# x^2 + y^2 ~ 1]
# @mtkcompile pend = System(eqs, t)
#
# tspan = (0.0, 1.5)
# u0map = [x => 1, y => 0]
# pmap = [g => 1]
# guess = [λ => 1]
#
# prob = ODEProblem(pend, u0map, tspan, pmap; guesses = guess)
# osol = solve(prob, Rodas5P())
#
# zeta = [0., 0., 0., 0., 0.]
# bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}(pend, u0map, tspan, parammap; guesses = guess)
#
# for solver in solvers
# sol = solve(bvp, solver(zeta), dt = 0.001)
# @test isapprox(sol.u[end], osol.u[end]; atol = 0.01)
# conditions = getfield.(equations(pend)[3:end], :rhs)
# @test isapprox([sol[conditions][1]; sol[x][1] - 1; sol[y][1]], zeros(5), atol = 0.001)
# end
#
# bvp2 = SciMLBase.BVProblem{false, SciMLBase.FullSpecialize}(pend, u0map, tspan, parammap)
# for solver in solvers
# sol = solve(bvp, solver(zeta), dt = 0.01)
# @test isapprox(sol.u[end], osol.u[end]; atol = 0.01)
# conditions = getfield.(equations(pend)[3:end], :rhs)
# @test [sol[conditions][1]; sol[x][1] - 1; sol[y][1]] ≈ 0
# end
# end
# Adding a midpoint boundary constraint.
# Solve using BVDAE solvers.
# let
# @parameters g
# @variables x(..) y(t) [state_priority = 10] λ(t)
# eqs = [D(D(x(t))) ~ λ * x(t)
# D(D(y)) ~ λ * y - g
# x(t)^2 + y^2 ~ 1]
# constr = [x(0.5) ~ 1]
# @mtkcompile pend = System(eqs, t; constr)
#
# tspan = (0.0, 1.5)
# u0map = [x(t) => 0.6, y => 0.8]
# parammap = [g => 1]
# guesses = [λ => 1]
#
# bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}(pend, u0map, tspan, parammap; guesses, check_length = false)
# test_solvers(daesolvers, bvp, u0map, constr)
#
# bvp2 = SciMLBase.BVProblem{false, SciMLBase.FullSpecialize}(pend, u0map, tspan, parammap)
# test_solvers(daesolvers, bvp2, u0map, constr, get_alg_eqs(pend))
#
# # More complicated constr.
# u0map = [x(t) => 0.6]
# guesses = [λ => 1, y(t) => 0.8]
#
# constr = [x(0.5) ~ 1,
# x(0.3)^3 + y(0.6)^2 ~ 0.5]
# @mtkcompile pend = System(eqs, t; constr)
# bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}(pend, u0map, tspan, parammap; guesses, check_length = false)
# test_solvers(daesolvers, bvp, u0map, constr, get_alg_eqs(pend))
#
# constr = [x(0.4) * g ~ y(0.2),
# y(0.7) ~ 0.3]
# @mtkcompile pend = System(eqs, t; constr)
# bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}(pend, u0map, tspan, parammap; guesses, check_length = false)
# test_solvers(daesolvers, bvp, u0map, constr, get_alg_eqs(pend))
# end
@testset "Cost function compilation" begin
@parameters α = 1.5 β = 1.0 γ = 3.0 δ = 1.0
@variables x(..) y(..)
t = ModelingToolkitBase.t_nounits
eqs = [
D(x(t)) ~ α * x(t) - β * x(t) * y(t),
D(y(t)) ~ -γ * y(t) + δ * x(t) * y(t),
]
tspan = (0.0, 1.0)
u0map = [x(t) => 4.0, y(t) => 2.0]
parammap = [α => 7.5, β => 4, γ => 8.0, δ => 5.0]
costs = [x(0.6), x(0.3)^2]
consolidate(u, sub) = (u[1] + 3)^2 + u[2] + sum(sub; init = 0)
@mtkcompile lksys = System(eqs, t; costs, consolidate)
@test_throws ModelingToolkitBase.SystemCompatibilityError ODEProblem(
lksys, [u0map; parammap], tspan
)
prob = ODEProblem(lksys, [u0map; parammap], tspan; check_compatibility = false)
sol = solve(prob, Tsit5())
costfn = ModelingToolkitBase.generate_cost(
lksys; expression = Val{false}, wrap_gfw = Val{true}
)
_t = tspan[2]
@test costfn(sol, prob.p, _t) ≈ (sol(0.6; idxs = x(t)) + 3)^2 + sol(0.3; idxs = x(t))^2
bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}(lksys, [u0map; parammap], tspan)
sol = solve(bvp, MIRK4(), dt = 0.05)
@test SciMLBase.successful_retcode(sol)
costfn = ModelingToolkitBase.generate_bvp_cost(
lksys; expression = Val{false}, wrap_gfw = Val{true}
)
@test costfn(sol, bvp.p) ≈ (sol(0.6; idxs = x(t)) + 3)^2 + sol(0.3; idxs = x(t))^2
### With a parameter
@parameters t_c
costs = [y(t_c) + x(0.0), x(0.4)^2]
consolidate(u, sub) = log(u[1]) - u[2] + sum(sub; init = 0)
@mtkcompile lksys = System(eqs, t; costs, consolidate)
@test t_c ∈ Set(parameters(lksys))
push!(parammap, t_c => 0.56)
prob = ODEProblem(lksys, [u0map; parammap], tspan; check_compatibility = false)
sol = solve(prob, Tsit5())
costfn = ModelingToolkitBase.generate_cost(
lksys; expression = Val{false}, wrap_gfw = Val{true}
)
@test costfn(sol, prob.p, _t) ≈
log(sol(0.56; idxs = y(t)) + sol(0.0; idxs = x(t))) - sol(0.4; idxs = x(t))^2
bvp = SciMLBase.BVProblem{true, SciMLBase.AutoSpecialize}(lksys, [u0map; parammap], tspan)
sol = solve(bvp, MIRK4(), dt = 0.05)
@test SciMLBase.successful_retcode(sol)
costfn = ModelingToolkitBase.generate_bvp_cost(
lksys; expression = Val{false}, wrap_gfw = Val{true}
)
@test costfn(sol, bvp.p) ≈ log(sol(0.56; idxs = y(t)) + sol(0.0; idxs = x(t))) - sol(0.4; idxs = x(t))^2
end
@testset "Parameter estimation" begin
@parameters α = 1.5 β = 1.0 [tunable = false] γ = 3.0 δ = 1.0
@variables x(t) y(t)
eqs = [
D(x) ~ α * x - β * x * y,
D(y) ~ -γ * y + δ * x * y,
]
@mtkcompile sys0 = System(eqs, t)
tspan = (0.0, 1.0)
u0map = [x => 4.0, y => 2.0]
parammap = [α => 1.8, γ => 6.5]
oprob = ODEProblem(sys0, [u0map; parammap], tspan)
osol = solve(oprob, Tsit5())
ts = range(tspan..., length = 51)
data = osol(ts, idxs = x).u
costs = [EvalAt(t)(x) - data[i] for (i, t) in enumerate(ts)]
consolidate(u, sub) = sum(abs2.(u))
@mtkcompile sys = System(eqs, t; costs, consolidate)
sys′ = subset_tunables(sys, [γ, α])
bprob = BVProblem(sys′, u0map, tspan; tune_parameters = true)
bsol = solve(bprob, MIRK4(; optimize = IpoptOptimizer()), dt = 1.0e-3)
@test bsol.ps[α] ≈ 1.8 rtol = 1.0e-2
@test bsol.ps[γ] ≈ 6.5 rtol = 1.0e-2
end