-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgoddard_1d_single_stage.jl
364 lines (289 loc) · 8.04 KB
/
goddard_1d_single_stage.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
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
using JuMP
import Ipopt
import Plots
using LinearAlgebra
using Printf
using Glob
foreach(include, glob("*.jl", "lib"))
# CGL, LGL, LGR, LG
const method = "LG"
# supported by LGR and LG methods
const integral = true
# supported by LGR differentiation method
const costate = false
# number of grid points
const N = 50
#
# Earth constants
#
const µ🜨 = 3.986012e14 # m³/s²
const r🜨 = 6378145 # m
const rho0 = 1.225 # kg/m³
const H0 = 8500 # m
const g0 = 9.80665 # m/s²
#
# Vehicle constants
#
const Aref = 10
const Cd = 0.2
const Isp = 300
const mi = 5000
const mprop = 0.6*mi
const Tmax = mi*g0*2
#
# Initial conditions
#
const hi = 0
const vi = 0
#
# Scaling
#
const r_scale = norm(r🜨)
const v_scale = sqrt(µ🜨/r_scale)
const t_scale = r_scale / v_scale
const m_scale = mi
const a_scale = v_scale / t_scale
const f_scale = m_scale * a_scale
const area_scale = r_scale^2
const vol_scale = r_scale * area_scale
const d_scale = m_scale / vol_scale
const mdot_scale = m_scale / t_scale
#
# Applying scaling
#
const his = hi / r_scale
const vis = vi / v_scale
const mis = mi / m_scale
const r🜨s = r🜨 / r_scale
const mprops = mprop / m_scale
const Tmaxs = Tmax / f_scale
const H0s = H0 / r_scale
const rho0s = rho0 / d_scale
const Arefs = Aref / area_scale
const c = Isp*g0 / v_scale
#
# Computed values
#
const mfs = mis - mprops
function goddard()
model = Model(Ipopt.Optimizer)
set_optimizer_attribute(model, "print_level", 5)
set_optimizer_attribute(model, "print_user_options", "yes")
set_optimizer_attribute(model, "max_iter", 2000)
set_optimizer_attribute(model, "tol", 1e-12)
tau, ptau, xtau, w, D, A, K, P = psmethod(method, N)
#
# Variables
#
@variable(model, 0 <= h[i=1:N] <= Inf, start=0)
@variable(model, 0 <= v[i=1:N] <= Inf, start=0)
@variable(model, mfs <= m[i=1:N] <= mis, start=mis)
@variable(model, 0 <= T[i=1:K] <= Tmaxs, start=Tmaxs)
@variable(model, 0 <= tf <= Inf, start=20/t_scale)
#
# Endpoint variable slices
#
xi = vcat(h[1], v[1], m[1])
xf = vcat(h[N], v[N], m[N])
#
# Collocated variable slices
#
offset = 0
if method == "LGR" || method == "LG"
offset = 1
end
@expression(model, hc[i=1:K], h[i+offset])
@expression(model, vc[i=1:K], v[i+offset])
@expression(model, mc[i=1:K], m[i+offset])
xc = hcat(hc, vc, mc)
#
# Polynomial variable slices
#
@expression(model, hp[i=1:P], h[i])
@expression(model, vp[i=1:P], v[i])
@expression(model, mp[i=1:P], m[i])
xp = hcat(hp, vp, mp)
#
# Fixed constraints
#
fix(h[1], his, force=true)
fix(v[1], vis, force=true)
fix(m[1], mis, force=true)
#
# Dynamical constraints
#
D1 = 2.0 / tf * D
A1 = tf / 2.0 * A
w1 = w * tf ./ 2.0
@expression(model, rho[i=1:K], rho0s*exp(-hc[i]/H0s))
@expression(model, Drag[i=1:K], -0.5*Cd*Arefs*rho[i]*vc[i]^2)
@expression(model, rsqr[i=1:K], (r🜨s + hc[i])^2)
F = hcat(
vc,
-1.0 ./ rsqr + (T + Drag) ./ mc,
-T ./ c,
)
if integral
@constraint(model, xc == xi' .+ A1 * F)
if method == "LG"
@constraint(model, xf == xi + F' * w1)
end
else
@constraint(model, D1 * xp == F)
if method == "LG"
@constraint(model, xf == xi + F' * w1)
end
end
#
# Objective
#
@objective(model, Max, h[N])
#
# Solve the problem
#
solve_and_print_solution(model)
#
# determine absolute and relative errors at collocation points
#
tau_err, ptau_err, xtau_err, w_err, D_err, A_err, K_err, P_err = psmethod(method, N+1)
D1_err = 2.0 / tf * D_err
A1_err = tf / 2.0 * A_err
w1_err = w_err * tf ./ 2.0
L = lagrange_basis(ptau, tau_err)
hc_err = lagrange_interpolation(L, value.(hp))
vc_err = lagrange_interpolation(L, value.(vp))
mc_err = lagrange_interpolation(L, value.(mp))
xc_err = hcat(hc_err, vc_err, mc_err)
L = lagrange_basis(tau, tau_err)
T_err = lagrange_interpolation(L, value.(T))
rho_err = rho0s*exp.(-hc_err./H0s)
Drag_err = -0.5*Cd*Arefs*rho_err.*vc_err.^2
rsqr_err = (r🜨s .+ hc_err).^2
F_err = hcat(
vc_err,
-1.0 ./ rsqr_err + (T_err + Drag_err) ./ mc_err,
-T_err ./ c,
)
xc_err2 = value.(xi' .+ A1_err * F_err)
abs_err = abs.(xc_err2 .- xc_err)
rel_err = abs_err ./ (1.0.+maximum(abs.(xc_err), dims=1))
max_rel_err = maximum(rel_err)
@printf "maximum relative error: %e\n" max_rel_err
#
# Structure Detection
#
nu = 0.05
T_val = value.(T)
T_norm = ( T_val .- minimum(T_val) ) ./ ( 1 + maximum(T_val) - minimum(T_val) )
midpoints = [(tau[i] + tau[i+1]) / 2 for i in 1:length(tau)-1]
function cj(tau, Sidx, j)
m = length(Sidx)-1
val = factorial(big(m))
for i in Sidx
i == j && continue
val /= tau[j] - tau[i]
end
return val
end
MMLm = []
for t in midpoints
sortidx = sortperm(abs.(tau .- t))
arry = []
for m in 1:6 # mu = 6
Sidx = sortidx[1:m+1]
temp = findall(x -> x > t, tau[Sidx])
Splusidx = sortidx[temp]
qm = 0
for j in Splusidx
qm += cj(tau, Sidx, j)
end
Lm = 0
for j in Sidx
Lm += cj(tau, Sidx, j) * T_norm[j]
end
Lm /= qm
push!(arry, Lm)
end
if all(x -> x > 0, arry)
push!(MMLm, minimum(arry))
if minimum(arry) > nu
v = (t * tf / 2.0 + tf / 2.0) * t_scale
display(value(v))
display(minimum(arry))
end
elseif all(x -> x < 0, arry)
push!(MMLm,abs(maximum(arry)))
if abs(maximum(arry)) > nu
v = (t * tf / 2.0 + tf / 2.0) * t_scale
display(value(v))
display(maximum(arry))
end
else
push!(MMLm, 0)
end
end
#
# Descale and interpolate the variables
#
range = LinRange(-1,1,100)
L = lagrange_basis(ptau, range)
h = lagrange_interpolation(L, value.(hp)) * r_scale
v = lagrange_interpolation(L, value.(vp)) * v_scale
m = lagrange_interpolation(L, value.(mp)) * m_scale
L = lagrange_basis(tau, range)
T = lagrange_interpolation(L, value.(T)) * f_scale
#
# Construct ranges of real times at the interpolation points
#
tf = value(tf)
t = (range * tf / 2.0 .+ tf / 2.0) * t_scale
#
# Do some plotting of interpolated results
#
p1 = Plots.plot(
t,
h,
xlabel = "Time (s)",
ylabel = "Height",
legend = false
)
p2 = Plots.plot(
t,
v,
xlabel = "Time (s)",
ylabel = "Velocity",
legend = false
)
p3 = Plots.plot(
t,
m,
xlabel = "Time (s)",
ylabel = "Mass",
legend = false
)
p4 = Plots.plot(
t,
T,
xlabel = "Time (s)",
ylabel = "Thrust",
legend = false
)
p5 = Plots.plot(
tau,
T_norm,
xlabel = "Tau",
ylabel = "T_norm",
legend = false
)
p6 = Plots.plot(
midpoints,
MMLm,
xlabel = "Tau",
ylabel = "MMLm",
legend = false
)
display(Plots.plot(p1, p2, p3, p4, p5, p6, layout=(2,3), legend=false))
readline()
@assert is_solved_and_feasible(model)
end
goddard()