-
Notifications
You must be signed in to change notification settings - Fork 3
/
goddard-exa2.jl
160 lines (125 loc) · 5.3 KB
/
goddard-exa2.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
# Solve Goddard problem using ExaModels.jl
using ExaModels
using NLPModelsIpopt
using MadNLP
using MadNLPGPU
using CUDA
using KernelAbstractions
using BenchmarkTools
using Interpolations
# Parameters
const n = 3 # State dim
const m = 1 # Control dim
const Cd = 310 # Drag (1/2)
const β = 500 # Drag (2/2)
const Tmax = 3.5 # Max thrust
const b = 2 # Fuel consumption
r0 = 1 # Initial altitude
v0 = 0 # Initial speed
m0 = 1 # Initial mass
vmax = 0.1 # Maximal authorized speed
mf = 0.6 # Final mass to target
# Intialisation from OptimalControl sol (N = 5):
# julia> tfs
# 0.18761155665063417
#
# julia> xs
# 3×6 Matrix{Float64}:
# 1.0 1.00105 1.00398 1.00751 1.01009 1.01124
# -1.83989e-40 0.056163 0.1 0.0880311 0.0492518 0.0123601
# 1.0 0.811509 0.650867 0.6 0.6 0.6
#
# julia> us
# 1×6 Matrix{Float64}:
# 0.599377 0.835887 0.387328 -5.87733e-9 -9.03538e-9 -8.62101e-9
tfs = 0.18761155665063417
xs = [ 1.0 1.00105 1.00398 1.00751 1.01009 1.01124
-1.83989e-40 0.056163 0.1 0.0880311 0.0492518 0.0123601
1.0 0.811509 0.650867 0.6 0.6 0.6 ]
us = [0.599377 0.835887 0.387328 -5.87733e-9 -9.03538e-9 -8.62101e-9]
N0 = 5
t = tfs * 0:N0
xs = linear_interpolation(t, [xs[:, j] for j ∈ 1:N0+1], extrapolation_bc=Line())
us = linear_interpolation(t, [us[:, j] for j ∈ 1:N0+1], extrapolation_bc=Line())
N = 5000
t = tfs * 0:N
xs = xs.(t); xs = stack(xs[:])
us = us.(t); us = stack(us[:])
print_level = MadNLP.WARN
tol = 1e-7 # 1e-5
# Model
function docp_exa(N=100; backend=nothing, tfs=0.1, xs=0.1, us=0.1)
c = ExaModels.ExaCore(; backend=backend)
tf = ExaModels.variable(c, 1; start=tfs)
x = ExaModels.variable(c, n, 1:N+1; start=xs)
u = ExaModels.variable(c, m, 1:N+1; start=us)
dt = tf[1] / N
ExaModels.constraint(c, tf[1]; lcon=0, ucon=Inf)
__it1 = [(i, [r0, v0, m0][i]) for i ∈ 1:n]
ExaModels.constraint(c, x[i, 1] - __v for (i, __v) ∈ __it1)
ExaModels.constraint(c, x[3, N+1] - mf)
ExaModels.constraint(c, u[1, j] for j ∈ 1:N+1; lcon=0, ucon=1)
ExaModels.constraint(c, x[1, j] for j ∈ 1:N+1; lcon=r0, ucon=Inf)
ExaModels.constraint(c, x[2, j] for j ∈ 1:N+1; lcon=0, ucon=vmax)
dr(r, v, m, u) = v
dv(r, v, m, u) = -Cd * v^2 * exp(-β * (r - 1)) / m - 1 / r^2 + u * Tmax / m
dm(r, v, m, u) = -b * Tmax * u
rk2(x1, x2, rhs1, rhs2, dt) = x2 - x1 - dt / 2 * (rhs1 + rhs2)
ExaModels.constraint(c, rk2(x[1, j], x[1, j+1], dr(x[1, j], x[2, j], x[3, j], u[1, j]), dr(x[1, j+1], x[2, j+1], x[3, j+1], u[1, j+1]), dt) for j ∈ 1:N)
ExaModels.constraint(c, rk2(x[2, j], x[2, j+1], dv(x[1, j], x[2, j], x[3, j], u[1, j]), dv(x[1, j+1], x[2, j+1], x[3, j+1], u[1, j+1]), dt) for j ∈ 1:N)
ExaModels.constraint(c, rk2(x[3, j], x[3, j+1], dm(x[1, j], x[2, j], x[3, j], u[1, j]), dm(x[1, j+1], x[2, j+1], x[3, j+1], u[1, j+1]), dt) for j ∈ 1:N)
ExaModels.objective(c, -x[1, N+1])
return ExaModels.ExaModel(c)
end
# Model (alternative version)
function docp_exa_s(N=100; backend=nothing, tfs=0.1, xs=0.1, us=0.1)
c = ExaModels.ExaCore(; backend=backend)
tf = ExaModels.variable(c, 1; start=tfs)
x = ExaModels.variable(c, n, 1:N+1; start=xs)
u = ExaModels.variable(c, m, 1:N+1; start=us)
s = ExaModels.variable(c, n, 1:N+1)
dt = tf[1] / N
ExaModels.constraint(c, tf[1]; lcon=0, ucon=Inf)
__it1 = [(i, [r0, v0, m0][i]) for i ∈ 1:n]
ExaModels.constraint(c, x[i, 1] - __v for (i, __v) ∈ __it1)
ExaModels.constraint(c, x[3, N+1] - mf)
ExaModels.constraint(c, u[1, j] for j ∈ 1:N+1; lcon=0, ucon=1)
ExaModels.constraint(c, x[1, j] for j ∈ 1:N+1; lcon=r0, ucon=Inf)
ExaModels.constraint(c, x[2, j] for j ∈ 1:N+1; lcon=0, ucon=vmax)
dr(r, v, m, u) = v
dv(r, v, m, u) = -Cd * v^2 * exp(-β * (r - 1)) / m - 1 / r^2 + u * Tmax / m
dm(r, v, m, u) = -b * Tmax * u
rk2(x1, x2, rhs1, rhs2, dt) = x2 - x1 - dt / 2 * (rhs1 + rhs2)
ExaModels.constraint(c, s[1, j] - dr(x[1, j], x[2, j], x[3, j], u[1, j]) for j ∈ 1:N+1)
ExaModels.constraint(c, s[2, j] - dv(x[1, j], x[2, j], x[3, j], u[1, j]) for j ∈ 1:N+1)
ExaModels.constraint(c, s[3, j] - dm(x[1, j], x[2, j], x[3, j], u[1, j]) for j ∈ 1:N+1)
ExaModels.constraint(c, rk2(x[1, j], x[1, j+1], s[1, j], s[1, j+1], dt) for j ∈ 1:N)
ExaModels.constraint(c, rk2(x[2, j], x[2, j+1], s[2, j], s[2, j+1], dt) for j ∈ 1:N)
ExaModels.constraint(c, rk2(x[3, j], x[3, j+1], s[3, j], s[3, j+1], dt) for j ∈ 1:N)
ExaModels.objective(c, -x[1, N+1])
return ExaModels.ExaModel(c)
end
_docp = docp_exa_s
exa0 = _docp(N; tfs=tfs, xs=xs, us=us)
exa1 = _docp(N; tfs=tfs, xs=xs, us=us, backend=CPU())
exa2 = _docp(N; tfs=tfs, xs=xs, us=us, backend=CUDABackend())
# Solve
println("\n******************** exa0:")
output0 = madnlp(exa0; tol=tol)
#println("\n******************** exa1:")
#output1 = madnlp(exa1; tol=tol)
println("\n******************** exa2:")
output2 = madnlp(exa2; tol=tol)
println()
println("exa0: ", output0)
#println("exa1: ", output1)
println("exa2: ", output2)
println()
println("N = ", N)
print("exa0:")
@btime madnlp(exa0; print_level=print_level, tol=tol)
#print("exa1:")
#@btime madnlp(exa1; print_level=print_level, tol=tol)
print("exa2:")
@btime madnlp(exa2; print_level=print_level, tol=tol)
nothing