@@ -2,7 +2,7 @@ using ModelingToolkit
2
2
import JuMP, InfiniteOpt
3
3
using DiffEqDevTools, DiffEqBase
4
4
using SimpleDiffEq
5
- using OrdinaryDiffEqSDIRK
5
+ using OrdinaryDiffEqSDIRK, OrdinaryDiffEqVerner, OrdinaryDiffEqTsit5, OrdinaryDiffEqFIRK
6
6
using Ipopt
7
7
using BenchmarkTools
8
8
using CairoMakie
100
100
constr = [v (1.0 ) ~ 0.0 ]
101
101
cost = [- x (1.0 )] # Maximize the final distance.
102
102
@named block = ODESystem (
103
- [D (x (t)) ~ v (t), D (v (t)) ~ u], t; costs = cost, constraints = constr)
104
- block, input_idxs = structural_simplify (block, ([u], []))
103
+ [D (x (t)) ~ v (t), D (v (t)) ~ u (t) ], t; costs = cost, constraints = constr)
104
+ block, input_idxs = structural_simplify (block, ([u (t) ], []))
105
105
106
106
u0map = [x (t) => 0.0 , v (t) => 0.0 ]
107
107
tspan = (0.0 , 1.0 )
@@ -113,19 +113,19 @@ end
113
113
# Test reached final position.
114
114
@test ≈ (jsol. sol. u[end ][1 ], 0.25 , rtol = 1e-5 )
115
115
# Test dynamics
116
- @parameters (u_interp:: LinearInterpolation )(.. )
117
- block_ode = ODESystem ([D (x (t)) ~ v (t), D (v (t)) ~ u_interp (t)], t)
118
- spline = ctrl_to_spline (jsol. input_sol, LinearInterpolation )
119
- oprob = ODEProblem (block , u0map, tspan, [u_interp => spline])
120
- osol = solve (oprob, Vern8 ())
121
- @test jsol. sol. u ≈ osol. u
116
+ @parameters (u_interp:: ConstantInterpolation )(.. )
117
+ @mtkbuild block_ode = ODESystem ([D (x (t)) ~ v (t), D (v (t)) ~ u_interp (t)], t)
118
+ spline = ctrl_to_spline (jsol. input_sol, ConstantInterpolation )
119
+ oprob = ODEProblem (block_ode , u0map, tspan, [u_interp => spline])
120
+ osol = solve (oprob, Vern8 (), dt = 0.01 , adaptive = false )
121
+ @test ≈ ( jsol. sol. u, osol. u, rtol = 0.05 )
122
122
123
123
iprob = InfiniteOptControlProblem (block, u0map, tspan, parammap; dt = 0.01 )
124
124
isol = solve (iprob, Ipopt. Optimizer; silent = true )
125
125
@test is_bangbang (isol. input_sol, [- 1.0 ], [1.0 ])
126
126
@test ≈ (isol. sol. u[end ][1 ], 0.25 , rtol = 1e-5 )
127
- osol = solve (oprob, ImplicitEuler ())
128
- @test isol. sol. u ≈ osol. u
127
+ osol = solve (oprob, ImplicitEuler (); dt = 0.01 , adaptive = false )
128
+ @test ≈ ( isol. sol. u, osol. u, rtol = 0.05 )
129
129
130
130
# ##################
131
131
# ## Bee example ###
@@ -154,10 +154,12 @@ end
154
154
@parameters (α_interp:: LinearInterpolation )(.. )
155
155
eqs = [D (w (t)) ~ - μ * w (t) + b * s * α_interp (t) * w (t),
156
156
D (q (t)) ~ - ν * q (t) + c * (1 - α_interp (t)) * s * w (t)]
157
- beesys_ode = ODESystem (eqs, t)
158
- oprob = ODEProblem (beesys_ode, u0map, tspan, [α_interp => ctrl_to_spline (jsol. input_sol, LinearInterpolation)])
159
- osol = solve (oprob, Tsit5 ())
160
- @test osol. u ≈ jsol. sol. u
157
+ @mtkbuild beesys_ode = ODESystem (eqs, t)
158
+ oprob = ODEProblem (beesys_ode, u0map, tspan, merge (Dict (pmap), Dict (α_interp => ctrl_to_spline (jsol. input_sol, LinearInterpolation))))
159
+ osol = solve (oprob, Tsit5 (); dt = 0.01 , adaptive = false )
160
+ @test ≈ (osol. u, jsol. sol. u, rtol = 0.01 )
161
+ osol2 = solve (oprob, ImplicitEuler (); dt = 0.01 , adaptive = false )
162
+ @test ≈ (osol2. u, isol. sol. u, rtol = 0.01 )
161
163
end
162
164
163
165
@testset " Rocket launch" begin
@@ -175,27 +177,31 @@ end
175
177
176
178
(ts, te) = (0.0 , 0.2 )
177
179
costs = [- h (te)]
178
- constraints = [T (te) ~ 0 ]
179
- @named rocket = ODESystem (eqs, t; costs, constraints)
180
+ cons = [T (te) ~ 0 ]
181
+ @named rocket = ODESystem (eqs, t; costs, constraints = cons )
180
182
rocket, input_idxs = structural_simplify (rocket, ([T (t)], []))
181
183
182
184
u0map = [h (t) => h₀, m (t) => m₀, v (t) => 0 ]
183
185
pmap = [
184
186
g₀ => 1 , m₀ => 1.0 , h_c => 500 , c => 0.5 * √ (g₀ * h₀), D_c => 0.5 * 620 * m₀ / g₀,
185
187
Tₘ => 3.5 * g₀ * m₀, T (t) => 0.0 , h₀ => 1 , m_c => 0.6 ]
186
188
jprob = JuMPControlProblem (rocket, u0map, (ts, te), pmap; dt = 0.005 , cse = false )
187
- jsol = solve (jprob, Ipopt. Optimizer, :RadauIA3 )
189
+ jsol = solve (jprob, Ipopt. Optimizer, :RadauIIA3 )
188
190
@test jsol. sol. u[end ][1 ] > 1.012
191
+
192
+ iprob = InfiniteOptControlProblem (rocket, u0map, (ts, te), pmap; dt = 0.005 , cse = false )
193
+ isol = solve (iprob, Ipopt. Optimizer, derivative_method = OrthogonalCollocation (3 ))
194
+ @test isol. sol. u[end ][1 ] > 1.012
189
195
190
196
# Test solution
191
197
@parameters (T_interp:: CubicSpline )(.. )
192
198
eqs = [D (h (t)) ~ v (t),
193
199
D (v (t)) ~ (T_interp (t) - drag (h (t), v (t))) / m (t) - gravity (h (t)),
194
200
D (m (t)) ~ - T_interp (t) / c]
195
- rocket_ode = ODESystem (eqs, t)
196
- interpmap = Dict (T_interp => ctrl_to_spline (jsol. inputsol , CubicSpline))
197
- oprob = ODEProblem (rocket_ode, u0map, tspan, merge (pmap, interpmap))
198
- osol = solve (oprob, RadauIA3 ())
201
+ @mtkbuild rocket_ode = ODESystem (eqs, t)
202
+ interpmap = Dict (T_interp => ctrl_to_spline (jsol. input_sol , CubicSpline))
203
+ oprob = ODEProblem (rocket_ode, u0map, (ts, te), merge (Dict ( pmap) , interpmap))
204
+ osol = solve (oprob, RadauIIA3 ())
199
205
@test jsol. sol. u ≈ osol. u
200
206
end
201
207
@@ -223,10 +229,44 @@ end
223
229
@test isapprox (isol. sol. t[end ], 10.0 , rtol = 1e-3 )
224
230
end
225
231
226
- using JuliaSimCompiler
227
- using Multibody. PlanarMechanics
228
-
229
232
@testset " Cart-pole problem" begin
233
+ # gravity, length, moment of Inertia, drag coeff
234
+ @parameters g l mₚ mₖ
235
+ @variables x (.. ) θ (.. ) u (t) [input = true , bounds = (- 10 , 10 )]
236
+
237
+ s = sin (θ (t))
238
+ c = cos (θ (t))
239
+ H = [mₖ+ mₚ mₚ* l* c
240
+ mₚ* l* c mₚ* l^ 2 ]
241
+ C = [0 - mₚ* D (θ (t))* l* s
242
+ 0 0 ]
243
+ qd = [D (x (t)), D (θ (t))]
244
+ G = [0 , mₚ* g* l* s]
245
+ B = [1 , 0 ]
246
+
247
+ tf = 5
248
+ rhss = - H \ Vector (C* qd + G - B* u)
249
+ eqs = [D (D (x (t))) ~ rhss[1 ], D (D (θ (t))) ~ rhss[2 ]]
250
+ cons = [θ (tf) ~ π, x (tf) ~ 0 , D (θ (tf)) ~ 0 , D (x (tf)) ~ 0 ]
251
+ costs = [∫ (u^ 2 )]
252
+ tspan = (0 , tf)
253
+
254
+ @named cartpole = ODESystem (eqs, t; costs, constraints = cons)
255
+ cartpole, input_idxs = structural_simplify (cartpole, ([u], []))
256
+
257
+ u0map = [D (x (t)) => 0. , D (θ (t)) => 0. , θ (t) => 0. , x (t) => 0. ]
258
+ pmap = [mₖ => 1. , mₚ => 0.2 , l => 0.5 , g => 9.81 , u => 0 ]
259
+ jprob = JuMPControlProblem (cartpole, u0map, tspan, pmap; dt = 0.04 )
260
+ jsol = solve (jprob, Ipopt. Optimizer, :RK4 )
261
+ @test jsol. sol. u[end ] ≈ [π, 0 , 0 , 0 ]
262
+
263
+ iprob = InfiniteOptControlProblem (cartpole, u0map, tspan, pmap; dt = 0.04 )
264
+ isol = solve (iprob, Ipopt. Optimizer)
265
+ @test isol. sol. u[end ] ≈ [π, 0 , 0 , 0 ]
266
+ end
267
+
268
+ # RC Circuit
269
+ @testset " MTK Components" begin
230
270
end
231
271
232
272
# @testset "Constrained optimal control problems" begin
0 commit comments