/
08_Rocket_Ascent_Polar_SSTO.py
348 lines (296 loc) · 10.9 KB
/
08_Rocket_Ascent_Polar_SSTO.py
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
# -*- coding: utf-8 -*-
# Copyright 2017 Interstellar Technologies Inc. All Rights Reserved.
from __future__ import print_function
import numpy as np
import matplotlib.pyplot as plt
from OpenGoddard.optimize import Problem, Guess, Condition, Dynamics
class Rocket:
GMe = 3.986004418 * 10**14 # Earth gravitational constant [m^3/s^2]
Re = 6371.0 * 1000 # Earth Radius [m]
g0 = 9.80665 # Gravitational acceleration on Earth surface [m/s^2]
def __init__(self):
self.Vr = np.sqrt(self.GMe / self.Re) # m/s
self.H0 = 10.0 # m
self.V0 = 0.0
self.M0 = 100000.0 # kg
self.Mp = self.M0 * 0.99
self.Cd = 0.6
self.A = 4.0 # m2
self.Isp = 300.0 # s
self.g0 = 9.80665 # m/s2
self.Tmax = self.M0 * self.g0 * 1.5
self.MaxQ = 14000.0 # Pa
self.MaxG = 8.0 # G
self.Htarget = 400.0 * 1000 # m
self.Rtarget = self.Re + self.Htarget # m/s
self.Vtarget = np.sqrt(self.GMe / self.Rtarget) # m/s
def air_density(self, h):
beta = 1/8500.0 # scale factor [1/m]
rho0 = 1.225 # kg/m3
return rho0*np.exp(-beta*h)
def dynamics(prob, obj, section):
R = prob.states(0, section)
theta = prob.states(1, section)
Vr = prob.states(2, section)
Vt = prob.states(3, section)
m = prob.states(4, section)
Tr = prob.controls(0, section)
Tt = prob.controls(1, section)
rho = obj.air_density(R - obj.Re)
Dr = 0.5 * rho * Vr * np.sqrt(Vr**2 + Vt**2) \
* obj.Cd * obj.A # [N]
Dt = 0.5 * rho * Vt * np.sqrt(Vr**2 + Vt**2) \
* obj.Cd * obj.A # [N]
g = obj.g0 * (obj.Re / R)**2 # [m/s2]
g0 = obj.g0
Isp = obj.Isp
dx = Dynamics(prob, section)
dx[0] = Vr
dx[1] = Vt / R
dx[2] = Tr / m - Dr / m - g + Vt**2 / R
dx[3] = Tt / m - Dt / m - (Vr * Vt) / R
dx[4] = - np.sqrt(Tr**2 + Tt**2) / g0 / Isp
return dx()
def equality(prob, obj):
R = prob.states_all_section(0)
theta = prob.states_all_section(1)
Vr = prob.states_all_section(2)
Vt = prob.states_all_section(3)
m = prob.states_all_section(4)
Tr = prob.controls_all_section(0)
Tt = prob.controls_all_section(1)
tf = prob.time_final(-1)
result = Condition()
# event condition
result.equal(R[0], obj.Re, unit=prob.unit_states[0][0])
result.equal(theta[0], 0.0, unit=prob.unit_states[0][1])
result.equal(Vr[0], 0.0, unit=prob.unit_states[0][2])
result.equal(Vt[0], 0.0, unit=prob.unit_states[0][3])
result.equal(m[0], obj.M0, unit=prob.unit_states[0][4])
result.equal(R[-1], obj.Rtarget, unit=prob.unit_states[0][1])
result.equal(Vr[-1], 0.0, unit=prob.unit_states[0][2])
result.equal(Vt[-1], obj.Vtarget, unit=prob.unit_states[0][3])
return result()
def inequality(prob, obj):
R = prob.states_all_section(0)
theta = prob.states_all_section(1)
Vr = prob.states_all_section(2)
Vt = prob.states_all_section(3)
m = prob.states_all_section(4)
Tr = prob.controls_all_section(0)
Tt = prob.controls_all_section(1)
tf = prob.time_final(-1)
rho = obj.air_density(R - obj.Re)
Dr = 0.5 * rho * Vr * np.sqrt(Vr**2 + Vt**2) \
* obj.Cd * obj.A # [N]
Dt = 0.5 * rho * Vt * np.sqrt(Vr**2 + Vt**2) \
* obj.Cd * obj.A # [N]
g = obj.g0 * (obj.Re / R)**2 # [m/s2]
# dynamic pressure
q = 0.5 * rho * (Vr**2 + Vt**2) # [Pa]
# accelaration
a_r = (Tr - Dr) / m
a_t = (Tt - Dt) / m
a_mag = np.sqrt(a_r**2 + a_t**2) # [m/s2]
# Thrust
T = np.sqrt(Tr**2 + Tt**2)
result = Condition()
# lower bounds
result.lower_bound(R, obj.Re, unit=prob.unit_states[0][0])
# result.lower_bound(Vr, 0.0, unit=prob.unit_states[0][2])
# result.lower_bound(Vt, 0.0, unit=prob.unit_states[0][3])
result.lower_bound(m[1:], (obj.M0 - obj.Mp), unit=prob.unit_states[0][4])
result.lower_bound(Tr, 0.0, unit=prob.unit_controls[0][0])
# result.lower_bound(Tt, obj.Tmax / obj.unit_T, unit=prob.unit_controls[0][0])
result.lower_bound(Tt, 0.0, unit=prob.unit_controls[0][0])
# upper bounds
result.upper_bound(m, obj.M0, unit=prob.unit_states[0][4])
result.upper_bound(Tr, obj.Tmax, unit=prob.unit_controls[0][0])
result.upper_bound(Tt, obj.Tmax, unit=prob.unit_controls[0][0])
result.upper_bound(T, obj.Tmax, unit=prob.unit_controls[0][0])
# result.upper_bound(q, obj.MaxQ, unit = prob.unit_states[0][0])
result.upper_bound(a_mag, obj.MaxG * obj.g0)
return result()
def cost(prob, obj):
m = prob.states_all_section(4)
# return -m[-1]
# ==== Caution ====
# cost function should be near 1.0
return -m[-1] / prob.unit_states[0][4]
def cost_derivative(prob, obj):
jac = Condition(prob.number_of_variables)
index_m0 = prob.index_states(4, 0, 0)
index_mf = prob.index_states(4, 0, -1)
m = prob.states_all_section(4)
# jac.change_value(index_m0, - m[-1] / m[0]**2)
# jac.change_value(index_mf, - 1.0 / m[0])
jac.change_value(index_mf, - 1.0)
return jac()
# ========================
# plt.close("all")
plt.ion()
# Program Starting Point
time_init = [0.0, 200]
n = [30]
num_states = [5]
num_controls = [2]
max_iteration = 20
flag_savefig = True
savefig_file = "08_Rocket_Ascent_Polar/SSTO_"
# ------------------------
# set OpenGoddard class for algorithm determination
prob = Problem(time_init, n, num_states, num_controls, max_iteration)
# ------------------------
# create instance of operating object
obj = Rocket()
unit_R = obj.Re
unit_theta = 1
unit_V = np.sqrt(obj.GMe / obj.Re)
unit_m = obj.M0
unit_t = unit_R / unit_V
unit_T = unit_m * unit_R / unit_t ** 2
prob.set_unit_states_all_section(0, unit_R)
prob.set_unit_states_all_section(1, unit_theta)
prob.set_unit_states_all_section(2, unit_V)
prob.set_unit_states_all_section(3, unit_V)
prob.set_unit_states_all_section(4, unit_m)
prob.set_unit_controls_all_section(0, unit_T)
prob.set_unit_controls_all_section(1, unit_T)
prob.set_unit_time(unit_t)
# ========================
# Initial parameter guess
# altitude profile
R_init = Guess.cubic(prob.time_all_section, obj.Re, 0.0, obj.Rtarget, 0.0)
# Guess.plot(prob.time_all_section, R_init, "Altitude", "time", "Altitude")
# if(flag_savefig):plt.savefig(savefig_file + "guess_alt" + ".png")
# theta
theta_init = Guess.cubic(prob.time_all_section, 0.0, 0.0, np.deg2rad(25.0), 0.0)
# velocity
Vr_init = Guess.linear(prob.time_all_section, 0.0, 0.0)
Vt_init = Guess.linear(prob.time_all_section, 0.0, obj.Vtarget)
# Guess.plot(prob.time_all_section, V_init, "Velocity", "time", "Velocity")
# mass profile
M_init = Guess.cubic(prob.time_all_section, obj.M0, -0.6, obj.M0-obj.Mp, 0.0)
# Guess.plot(prob.time_all_section, M_init, "Mass", "time", "Mass")
# if(flag_savefig):plt.savefig(savefig_file + "guess_mass" + ".png")
# thrust profile
# T_init = Guess.zeros(prob.time_all_section)
Tr_init = Guess.cubic(prob.time_all_section, obj.Tmax/2, 0.0, 0.0, 0.0)
Tt_init = Guess.cubic(prob.time_all_section, obj.Tmax/2, 0.0, 0.0, 0.0)
# Guess.plot(prob.time_all_section, T_init, "Thrust Guess", "time", "Thrust")
# if(flag_savefig):plt.savefig(savefig_file + "guess_thrust" + ".png")
# plt.show()
# ========================
# Substitution initial value to parameter vector to be optimized
# non dimensional values (Divide by scale factor)
prob.set_states_all_section(0, R_init)
prob.set_states_all_section(1, theta_init)
prob.set_states_all_section(2, Vr_init)
prob.set_states_all_section(3, Vt_init)
prob.set_states_all_section(4, M_init)
prob.set_controls_all_section(0, Tr_init)
prob.set_controls_all_section(1, Tt_init)
# ========================
# Main Process
# Assign problem to SQP solver
prob.dynamics = [dynamics]
prob.knot_states_smooth = []
prob.cost = cost
# prob.cost_derivative = cost_derivative
prob.equality = equality
prob.inequality = inequality
def display_func():
R = prob.states_all_section(0)
m = prob.states_all_section(4)
tf = prob.time_final(-1)
print("m0 : {0:.5f}".format(m[0]))
print("mf : {0:.5f}".format(m[-1]))
print("max altitude: {0:.5f}".format(R[-1]))
print("final time : {0:.3f}".format(tf))
prob.solve(obj, display_func, ftol=1e-8)
# ========================
# Post Process
# ------------------------
# Convert parameter vector to variable
R = prob.states_all_section(0)
theta = prob.states_all_section(1)
Vr = prob.states_all_section(2)
Vt = prob.states_all_section(3)
m = prob.states_all_section(4)
Tr = prob.controls_all_section(0)
Tt = prob.controls_all_section(1)
time = prob.time_update()
# ------------------------
# Calculate necessary variables
rho = obj.air_density(R - obj.Re)
Dr = 0.5 * rho * Vr * np.sqrt(Vr**2 + Vt**2) \
* obj.Cd * obj.A # [N]
Dt = 0.5 * rho * Vt * np.sqrt(Vr**2 + Vt**2) \
* obj.Cd * obj.A # [N]
g = obj.g0 * (obj.Re / R)**2 # [m/s2]
# dynamic pressure
q = 0.5 * rho * (Vr**2 + Vt**2) # [Pa]
# accelaration
a_r = (Tr - Dr) / m
a_t = (Tt - Dt) / m
a_mag = np.sqrt(a_r**2 + a_t**2) # [m/s2]
# Thrust
T = np.sqrt(Tr**2 + Tt**2)
# ------------------------
# Visualizetion
plt.figure()
plt.title("Altitude profile")
plt.plot(time, (R - obj.Re)/1000, marker="o", label="Altitude")
for line in prob.time_knots():
plt.axvline(line, color="k", alpha=0.5)
plt.grid()
plt.xlabel("time [s]")
plt.ylabel("Altitude [km]")
if(flag_savefig): plt.savefig(savefig_file + "altitude" + ".png")
plt.figure()
plt.title("Velocity")
plt.plot(time, Vr, marker="o", label="Vr")
plt.plot(time, Vt, marker="o", label="Vt")
for line in prob.time_knots():
plt.axvline(line, color="k", alpha=0.5)
plt.grid()
plt.xlabel("time [s]")
plt.ylabel("Velocity [m/s]")
plt.legend(loc="best")
if(flag_savefig): plt.savefig(savefig_file + "velocity" + ".png")
plt.figure()
plt.title("Mass")
plt.plot(time, m, marker="o", label="Mass")
for line in prob.time_knots():
plt.axvline(line, color="k", alpha=0.5)
plt.grid()
plt.xlabel("time [s]")
plt.ylabel("Mass [kg]")
if(flag_savefig): plt.savefig(savefig_file + "mass" + ".png")
plt.figure()
plt.title("Acceleration")
plt.plot(time, a_r, marker="o", label="Acc r")
plt.plot(time, a_t, marker="o", label="Acc t")
plt.plot(time, a_mag, marker="o", label="Acc")
for line in prob.time_knots():
plt.axvline(line, color="k", alpha=0.5)
plt.grid()
plt.xlabel("time [s]")
plt.ylabel("Acceleration [m/s2]")
if(flag_savefig): plt.savefig(savefig_file + "acceleration" + ".png")
plt.figure()
plt.title("Thrust profile")
plt.plot(time, Tr / 1000, marker="o", label="Tr")
plt.plot(time, Tt / 1000, marker="o", label="Tt")
plt.plot(time, T / 1000, marker="o", label="Thrust")
plt.plot(time, Dr / 1000, marker="o", label="Dr")
plt.plot(time, Dt / 1000, marker="o", label="Dt")
plt.plot(time, m * g / 1000, marker="o", label="Gravity")
for line in prob.time_knots():
plt.axvline(line, color="k", alpha=0.5)
plt.grid()
plt.xlabel("time [s]")
plt.ylabel("Thrust [kN]")
plt.legend(loc="best")
if(flag_savefig): plt.savefig(savefig_file + "force" + ".png")
plt.show()