-
Notifications
You must be signed in to change notification settings - Fork 0
/
mach.py
296 lines (239 loc) · 10.6 KB
/
mach.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
import math
pi = math.pi
import random
uni_gas_const = uni_gas_const = 8.314472 # m2 kg s-2 K-1 mol-1
from geometry import get_inner_radius_at, get_inner_radius_at_bell
pseudo_infinity = 10e6
# this function calculates the distribution of the combustion gas flow mach number
# across the engine
# see https://www.grc.nasa.gov/WWW/k-12/airplane/nozzled.html for more
# also especially see https://www.grc.nasa.gov/WWW/K-12/airplane/astar.html
# (the R here isn't the universal constant)
def calc_mach_num(x_end, x_thrt, Tc, gamma_thrt, avg_molecular_mass, fineness,
L_engine, D_chm, D_thrt, D_exit, a_chmContract, ROC_chm, a_nzlExp, ROC_thrtDn, ROC_thrtUp):
global uni_gas_const, pi
subsonic_step_size = x_thrt/fineness
subsonic_M = []
subsonic_x = []
# calculate for subsonic region (go backwards from throat)
# m_dot = density * velocity * area
# density and m_dot are const
# so velocity changes with area
for i in range(fineness):
x = x_thrt - (i * subsonic_step_size)
A_star = pi * get_inner_radius_at(x_thrt, L_engine, D_chm, D_thrt, D_exit, a_chmContract, ROC_chm, a_nzlExp, ROC_thrtDn, ROC_thrtUp)**2
A = pi * get_inner_radius_at(x, L_engine, D_chm, D_thrt, D_exit, a_chmContract, ROC_chm, a_nzlExp, ROC_thrtDn, ROC_thrtUp)**2
A_ratio = A/A_star
# Reference (1)
# - - - - - - BEGIN QUOTE - - - - - -
gp1 = gamma_thrt + 1
gm1 = gamma_thrt - 1
arat = A_ratio
aro = 2
macho = 0.30 #
fac1 = gp1/(2*gm1)
machn = macho + 0.05
infinity_fuse = 0
while abs(A_ratio - aro) > 0.0001:
try:
fac = 1 + 0.5 * gm1 * machn**2
arn = 1/(machn * fac**(-fac1) * (gp1/2)**fac1)
deriv = (arn-aro)/(machn-macho)
aro = arn
macho = machn
machn = macho + (arat - aro)/deriv
except ZeroDivisionError:
print("The Mach profile calculator attempted to divide by zero.")
break
# - - - - - - END QUOTE - - - - - -
infinity_fuse += 1
if infinity_fuse > pseudo_infinity:
print("The Mach profile calculator seems to be not converging. Trying a new initial guess...")
macho = random.uniform(0.01, 0.99)
# required failsafe
# NASA's code fails spectacularly if
# mach number is too low or something
if macho >= 1 or macho < 0.2:
macho = 0
subsonic_M.insert(0, macho)
subsonic_x.insert(0, x)
# calculate for supersonic region
# density is variable
# m_dot is const
supersonic_M = []
supersonic_x = []
supersonic_step_size = (x_end - x_thrt)/fineness
for i in range(fineness):
x = x_thrt + (i * supersonic_step_size)
A_star = pi * get_inner_radius_at(x_thrt, L_engine, D_chm, D_thrt, D_exit, a_chmContract, ROC_chm, a_nzlExp, ROC_thrtDn, ROC_thrtUp)**2
A = pi * get_inner_radius_at(x, L_engine, D_chm, D_thrt, D_exit, a_chmContract, ROC_chm, a_nzlExp, ROC_thrtDn, ROC_thrtUp)**2
A_ratio = A/A_star
# Reference (1)
# - - - - - - BEGIN QUOTE - - - - - -
gp1 = gamma_thrt + 1
gm1 = gamma_thrt - 1
arat = A_ratio
aro = 2
macho = 2.2
fac1 = gp1/(2*gm1)
machn = macho + 0.05
infinity_fuse = 0
while abs(A_ratio - aro) > .0001:
try:
fac = 1 + 0.5 * gm1 * machn**2
arn = 1/(machn * fac**(-fac1) * (gp1/2)**fac1)
deriv = (arn-aro)/(machn-macho)
aro = arn
macho = machn
machn = macho + (arat - aro)/deriv
except ZeroDivisionError:
print("The Mach profile calculator attempted to divide by zero.")
break
# - - - - - - END QUOTE - - - - - -
infinity_fuse += 1
if infinity_fuse > pseudo_infinity:
print("Mach number calculator might have entered an infinite loop, because it hasn't converged for", pseudo_infinity, "iterations. (A)bort or (C)ontinue for another", pseudo_infinity, "iterations?")
fuse_replacement = input(" > ")
if fuse_replacement.lower() == "c":
infinity_fuse = 0
# get new initial guess because the old one obviously is no good
macho = random.uniform(1.01, 4.99)
print("DEBUG: New initial guess:", macho)
elif fuse_replacement.lower() == "a":
print("Analysis aborted.")
input("Press Enter to quit...")
quit()
else:
print("Invalid choice!")
supersonic_M.insert(0, macho)
supersonic_x.insert(0, x)
return subsonic_x, subsonic_M, supersonic_x, supersonic_M
def calc_mach_num_bell(x_end, x_thrt, Tc, gamma_thrt, avg_molecular_mass, fineness,
L_engine, D_chm, D_thrt, D_exit, a_chmContract, ROC_chm, length_percent, theta_n, theta_e):
global uni_gas_const, pi
subsonic_step_size = x_thrt/fineness
subsonic_M = []
subsonic_x = []
# calculate for subsonic region (go backwards from throat)
# m_dot = density * velocity * area
# density and m_dot are const
# so velocity changes with area
for i in range(fineness):
x = x_thrt - (i * subsonic_step_size)
A_star = pi * (D_thrt/2)**2
A = pi * get_inner_radius_at_bell(x, L_engine, D_chm, D_thrt, D_exit, ROC_chm, a_chmContract, length_percent, theta_n, theta_e)**2
A_ratio = A/A_star
# Reference (1)
# - - - - - - BEGIN QUOTE - - - - - -
gp1 = gamma_thrt + 1
gm1 = gamma_thrt - 1
arat = A_ratio
aro = 2
macho = 0.30 #
fac1 = gp1/(2*gm1)
machn = macho + 0.05
infinity_fuse = 0
while abs(A_ratio - aro) > 0.0001:
try:
fac = 1 + 0.5 * gm1 * machn**2
arn = 1/(machn * fac**(-fac1) * (gp1/2)**fac1)
deriv = (arn-aro)/(machn-macho)
aro = arn
macho = machn
machn = macho + (arat - aro)/deriv
except ZeroDivisionError:
print("The Mach profile calculator attempted to divide by zero.")
break
# - - - - - - END QUOTE - - - - - -
infinity_fuse += 1
if infinity_fuse > pseudo_infinity:
print("Mach number calculator might have entered an infinite loop, because it hasn't converged for", pseudo_infinity, "iterations. (A)bort or (C)ontinue for another", pseudo_infinity, "iterations?")
fuse_replacement = input(" > ")
if fuse_replacement.lower() == "c":
infinity_fuse = 0
# get new initial guess because the old one obviously is no good
macho = random.uniform(0.01, 0.99)
print("DEBUG: New initial guess:", macho)
elif fuse_replacement.lower() == "a":
print("Analysis aborted.")
input("Press Enter to quit...")
quit()
else:
print("Invalid choice!")
# required failsafe
# NASA's code fails spectacularly if
# mach number is too low or something
if macho >= 1 or macho < 0.2:
macho = 0
subsonic_M.insert(0, macho)
subsonic_x.insert(0, x)
# calculate for supersonic region
# density is variable
# m_dot is const
supersonic_M = []
supersonic_x = []
supersonic_step_size = (x_end - x_thrt)/fineness
for i in range(fineness):
x = x_thrt + (i * supersonic_step_size)
A_star = pi * (D_thrt/2)**2
A = pi * get_inner_radius_at_bell(x, L_engine, D_chm, D_thrt, D_exit, ROC_chm, a_chmContract, length_percent, theta_n, theta_e)**2
A_ratio = A/A_star
# Reference (1)
# - - - - - - BEGIN QUOTE - - - - - -
gp1 = gamma_thrt + 1
gm1 = gamma_thrt - 1
arat = A_ratio
aro = 2
macho = 2.2
fac1 = gp1/(2*gm1)
machn = macho + 0.05
infinity_fuse = 0
while abs(A_ratio - aro) > .0001:
try:
fac = 1 + 0.5 * gm1 * machn**2
arn = 1/(machn * fac**(-fac1) * (gp1/2)**fac1)
deriv = (arn-aro)/(machn-macho)
aro = arn
macho = machn
machn = macho + (arat - aro)/deriv
except ZeroDivisionError:
print("The Mach profile calculator attempted to divide by zero.")
break
# - - - - - - END QUOTE - - - - - -
infinity_fuse += 1
if infinity_fuse > pseudo_infinity:
print("Mach number calculator might have entered an infinite loop, because it hasn't converged for", pseudo_infinity, "iterations. (A)bort or (C)ontinue for another", pseudo_infinity, "iterations?")
fuse_replacement = input(" > ")
if fuse_replacement.lower() == "c":
infinity_fuse = 0
# get new initial guess because the old one obviously is no good
macho = random.uniform(1.01, 4.99)
print("DEBUG: New initial guess:", macho)
elif fuse_replacement.lower() == "a":
print("Analysis aborted.")
input("Press Enter to quit...")
quit()
else:
print("Invalid choice!")
supersonic_M.insert(0, macho)
supersonic_x.insert(0, x)
return subsonic_x, subsonic_M, supersonic_x, supersonic_M
# this one below isn't any rocket science
def get_index_of_closest_num_in_list(x, lst):
min_diff = None
closest_index = None
for i in range(len(lst)):
if not min_diff or abs(x - lst[i]) < min_diff:
min_diff = abs(x - lst[i])
closest_index = i
return closest_index
# this function just reads values from the already-calculated mach number distribution
def get_mach_num_at(x, subsonic_mach, subsonic_x, supersonic_mach, supersonic_x, engine_lengths):
# subsonic region
if x < engine_lengths[4]:
index = get_index_of_closest_num_in_list(x, subsonic_x)
return subsonic_mach[index]
# supersonic region
elif engine_lengths[4] <= x <= engine_lengths[6]:
index = get_index_of_closest_num_in_list(x, supersonic_x)
return supersonic_mach[index]