11function test_goddard_indirect ()
22
3- prob = Problem (:goddard , :classical , :altitude , :x_dim_3 , :u_dim_1 , :mayer , :x_cons , :u_cons , :singular_arc )
4- ocp = prob. model
5- sol = prob. solution
6- title = prob. title
7-
8- # parameters
9- Cd = 310
10- Tmax = 3.5
11- β = 500
12- b = 2
13- t0 = 0
14- r0 = 1
15- v0 = 0
16- vmax = 0.1
17- m0 = 1
18- mf = 0.6
19- x0 = [ r0, v0, m0 ]
20-
21- #
22- remove_constraint! (ocp, :x_con_rmin )
23- g (x) = vmax- constraint (ocp, :x_con_vmax )(x, Real[]) # g(x, u) ≥ 0 (cf. nonnegative multiplier)
24- final_mass_cons (xf) = constraint (ocp, :final_con )(x0, xf, Real[])- mf
25-
26- function F0 (x)
27- r, v, m = x
28- D = Cd * v^ 2 * exp (- β* (r - 1 ))
29- return [ v, - D/ m - 1 / r^ 2 , 0 ]
30- end
31-
32- function F1 (x)
33- r, v, m = x
34- return [ 0 , Tmax/ m, - b* Tmax ]
35- end
36-
37- # bang controls
38- u0 = 0
39- u1 = 1
40-
41- # singular control
42- H0 = Lift (F0)
43- H1 = Lift (F1)
44- H01 = @Lie {H0, H1}
45- H001 = @Lie {H0, H01}
46- H101 = @Lie {H1, H01}
47- us (x, p) = - H001 (x, p) / H101 (x, p)
48-
49- # boundary control
50- ub (x) = - Lie (F0, g)(x) / Lie (F1, g)(x)
51- μ (x, p) = H01 (x, p) / Lie (F1, g)(x)
52-
53- # flows
54- f0 = Flow (ocp, (x, p, v) -> u0)
55- f1 = Flow (ocp, (x, p, v) -> u1)
56- fs = Flow (ocp, (x, p, v) -> us (x, p))
57- fb = Flow (ocp, (x, p, v) -> ub (x), (x, u, v) -> g (x), (x, p, v) -> μ (x, p))
58-
59- # shooting function
60- function shoot! (s, p0, t1, t2, t3, tf) # B+ S C B0 structure
61-
62- x1, p1 = f1 (t0, x0, p0, t1)
63- x2, p2 = fs (t1, x1, p1, t2)
64- x3, p3 = fb (t2, x2, p2, t3)
65- xf, pf = f0 (t3, x3, p3, tf)
66- s[1 ] = final_mass_cons (xf)
67- s[2 : 3 ] = pf[1 : 2 ] - [ 1 , 0 ]
68- s[4 ] = H1 (x1, p1)
69- s[5 ] = H01 (x1, p1)
70- s[6 ] = g (x2)
71- s[7 ] = H0 (xf, pf) # free tf
72-
73- end
74-
75- # tests
76- t1 = 0.025246759388000528
77- t2 = 0.061602092906721286
78- t3 = 0.10401664867856217
79- tf = 0.20298394547952422
80- p0 = [3.9428857983400074 , 0.14628855388160236 , 0.05412448008321635 ]
81-
82- # test shooting function
83- s = zeros (eltype (p0), 7 )
84- shoot! (s, p0, t1, t2, t3, tf)
85- s_guess_sol = [- 0.02456074767656735 ,
86- - 0.05699760226157302 ,
87- 0.0018629693253921868 ,
88- - 0.027013078908634858 ,
89- - 0.21558816838342798 ,
90- - 0.0121146739026253 ,
91- 0.015713236406057297 ]
92- @test s ≈ s_guess_sol atol= 1e-6
93-
94- #
95- ξ0 = [ p0 ; t1 ; t2 ; t3 ; tf ]
96-
97- #
98- foo! (s, ξ, λ) = shoot! (s, ξ[1 : 3 ], ξ[4 ], ξ[5 ], ξ[6 ], ξ[7 ])
99- # sol = fsolve(foo!, ξ0, show_trace=true); println(sol)
100- prob = NonlinearProblem (foo!, ξ0)
101- # sol = NonlinearSolve.solve(prob)
102- sol = solve (prob)
103-
104- p0 = sol. u[1 : 3 ]
105- t1 = sol. u[4 ]
106- t2 = sol. u[5 ]
107- t3 = sol. u[6 ]
108- tf = sol. u[7 ];
109-
110- shoot! (s, p0, t1, t2, t3, tf)
111-
112- # @test sol.converged
113- @test SciMLBase. successful_retcode (sol)
114- @test norm (s) < 1e-6
3+ #
4+ include (" Goddard.jl" )
5+ ocp, obj = Goddard ()
6+
7+ # parameters
8+ Cd = 310
9+ Tmax = 3.5
10+ β = 500
11+ b = 2
12+ t0 = 0
13+ r0 = 1
14+ v0 = 0
15+ vmax = 0.1
16+ m0 = 1
17+ mf = 0.6
18+ x0 = [ r0, v0, m0 ]
19+
20+ #
21+ remove_constraint! (ocp, :x_con_rmin )
22+ g (x) = vmax- constraint (ocp, :x_con_vmax )(x, Real[]) # g(x, u) ≥ 0 (cf. nonnegative multiplier)
23+ final_mass_cons (xf) = constraint (ocp, :final_con )(x0, xf, Real[])- mf
24+
25+ function F0 (x)
26+ r, v, m = x
27+ D = Cd * v^ 2 * exp (- β* (r - 1 ))
28+ return [ v, - D/ m - 1 / r^ 2 , 0 ]
29+ end
30+
31+ function F1 (x)
32+ r, v, m = x
33+ return [ 0 , Tmax/ m, - b* Tmax ]
34+ end
35+
36+ # bang controls
37+ u0 = 0
38+ u1 = 1
39+
40+ # singular control
41+ H0 = Lift (F0)
42+ H1 = Lift (F1)
43+ H01 = @Lie {H0, H1}
44+ H001 = @Lie {H0, H01}
45+ H101 = @Lie {H1, H01}
46+ us (x, p) = - H001 (x, p) / H101 (x, p)
47+
48+ # boundary control
49+ ub (x) = - Lie (F0, g)(x) / Lie (F1, g)(x)
50+ μ (x, p) = H01 (x, p) / Lie (F1, g)(x)
51+
52+ # flows
53+ f0 = Flow (ocp, (x, p, v) -> u0)
54+ f1 = Flow (ocp, (x, p, v) -> u1)
55+ fs = Flow (ocp, (x, p, v) -> us (x, p))
56+ fb = Flow (ocp, (x, p, v) -> ub (x), (x, u, v) -> g (x), (x, p, v) -> μ (x, p))
57+
58+ # shooting function
59+ function shoot! (s, p0, t1, t2, t3, tf) # B+ S C B0 structure
60+
61+ x1, p1 = f1 (t0, x0, p0, t1)
62+ x2, p2 = fs (t1, x1, p1, t2)
63+ x3, p3 = fb (t2, x2, p2, t3)
64+ xf, pf = f0 (t3, x3, p3, tf)
65+ s[1 ] = final_mass_cons (xf)
66+ s[2 : 3 ] = pf[1 : 2 ] - [ 1 , 0 ]
67+ s[4 ] = H1 (x1, p1)
68+ s[5 ] = H01 (x1, p1)
69+ s[6 ] = g (x2)
70+ s[7 ] = H0 (xf, pf) # free tf
71+
72+ end
73+
74+ # tests
75+ t1 = 0.025246759388000528
76+ t2 = 0.061602092906721286
77+ t3 = 0.10401664867856217
78+ tf = 0.20298394547952422
79+ p0 = [3.9428857983400074 , 0.14628855388160236 , 0.05412448008321635 ]
80+
81+ # test shooting function
82+ s = zeros (eltype (p0), 7 )
83+ shoot! (s, p0, t1, t2, t3, tf)
84+ s_guess_sol = [- 0.02456074767656735 ,
85+ - 0.05699760226157302 ,
86+ 0.0018629693253921868 ,
87+ - 0.027013078908634858 ,
88+ - 0.21558816838342798 ,
89+ - 0.0121146739026253 ,
90+ 0.015713236406057297 ]
91+ @test s ≈ s_guess_sol atol= 1e-6
92+
93+ #
94+ ξ0 = [ p0 ; t1 ; t2 ; t3 ; tf ]
95+
96+ #
97+ foo! (s, ξ, λ) = shoot! (s, ξ[1 : 3 ], ξ[4 ], ξ[5 ], ξ[6 ], ξ[7 ])
98+ # sol = fsolve(foo!, ξ0, show_trace=true); println(sol)
99+ prob = NonlinearProblem (foo!, ξ0)
100+ # sol = NonlinearSolve.solve(prob)
101+ sol = solve (prob)
102+
103+ p0 = sol. u[1 : 3 ]
104+ t1 = sol. u[4 ]
105+ t2 = sol. u[5 ]
106+ t3 = sol. u[6 ]
107+ tf = sol. u[7 ];
108+
109+ shoot! (s, p0, t1, t2, t3, tf)
110+
111+ # @test sol.converged
112+ @test SciMLBase. successful_retcode (sol)
113+ @test norm (s) < 1e-6
115114
116115end
0 commit comments