-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathrepickle_airfoil.py
More file actions
324 lines (295 loc) · 11 KB
/
repickle_airfoil.py
File metadata and controls
324 lines (295 loc) · 11 KB
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
import aerosandbox as asb
import aerosandbox.aerodynamics.aero_2D.xfoil as xfoil
import aerosandbox.numpy as np
import pandas as pd
import aerosandbox.modeling.interpolation
# from aerosandbox.tools.carpet_plot_utils import time_limit, patch_nans # Moved to aerosandbox.visualization.carpet_plot_utils in new ASB version; change if you have errors
from aerosandbox.modeling.interpolation import InterpolatedModel
import dill as pickle
import pathlib
from pathlib import Path
from scipy.interpolate import griddata
from scipy.interpolate import Rbf
# from scipy.interpolate import interp2d
# from scipy.interpolate import CloughTocher2DInterpolator
# from scipy.ndimage import gaussian_filter
import matplotlib.pyplot as plt
from aerosandbox.tools.airfoil_fitter.airfoil_fitter import AirfoilFitter
def run_xfoil():
### load wing airfoil from datafile
wing_airfoil = asb.geometry.Airfoil(name="HALE_03", coordinates=r"studies/airfoil_optimizer/HALE_03.dat")
reynolds = np.geomspace(1000, 100000000, 200)[139:]
alphas = np.arange(-15, 15, 0.1)
alpha = alphas[180:]
alpha = np.append(alpha, alphas[0:180])
#reynolds = [10000]
output = {}
# sweep over wide range of logspaced Re values
for Re in reynolds:
# initialize xfoil through asb
xf = xfoil.XFoil(
airfoil=wing_airfoil,
Re=Re,
n_crit=7,
mach=0,
xtr_upper=1,
xtr_lower=1,
max_iter=40,
verbose=True,
xfoil_command = "/Users/annickdewald/Desktop/Xfoil/bin/xfoil",
)
# test range of alpha values
result = xf.alpha([alpha])
# pickle each output dict
import dill as pickle
import pathlib
#
path = str(
pathlib.Path(__file__).parent.absolute()
)
with open(path + f"/data/airfoil_xfoil_results/airfoil_{Re}.cache", "wb+") as f:
pickle.dump(result, f)
def get_Xfoil_dat():
reynolds = np.geomspace(1000, 100000000, 200)
# alphas = np.arange(-15, 15, 0.1)
# alpha = alphas[180:]
# np.append(alpha, alphas[0:180])
alpha_list = []
reynolds_list = []
cl_values = []
cd_values = []
cm_values = []
for Re in reynolds:
xfoil_function = {}
path = str(pathlib.Path(__file__).parent.absolute())
try:
with open(path + f"/data/airfoil_xfoil_results/airfoil_{Re}.cache", "rb") as f:
unpickle = pickle.Unpickler(f)
xfoil_function = unpickle.load()
alpha = list(xfoil_function['alpha'])
reynold = [Re] * len(alpha)
cl_out = list(xfoil_function['CL'])
cd_out = list(xfoil_function['CD'])
cm_out = list(xfoil_function['CM'])
alpha_list.extend(alpha)
reynolds_list.extend(reynold)
cl_values.extend(cl_out)
cd_values.extend(cd_out)
cm_values.extend(cm_out)
except (FileNotFoundError, TypeError):
pass
return alpha_list, reynolds_list, cl_values, cd_values, cm_values
def plot_xfoil(alpha_list, reynolds_list, cl_values, cd_values, cm_values):
fig, ax = plt.subplots()
ax.tricontour(reynolds_list, alpha_list, cl_values, levels=10, linewidths=0.01, colors='k')
cntr2 = ax.tricontourf(reynolds_list, alpha_list, cl_values, levels=10)
fig.colorbar(cntr2, ax=ax, cmap='viridis')
# ax.plot(reynolds_list, alpha_list, 'ko', ms=1)
ax.set_xscale("log")
ax.set(xlabel="Reynolds Number", ylabel=r"$\alpha$ (angle)",
title=r"$C_l$ from Xfoil")
plt.show()
fig, ax = plt.subplots()
ax.tricontour(reynolds_list, alpha_list, np.log(cd_values), levels=10, linewidths=0.01, colors='k')
cntr2 = ax.tricontourf(reynolds_list, alpha_list, np.log(cd_values), levels=10)
fig.colorbar(cntr2, ax=ax, cmap='viridis')
# ax.plot(reynolds_list, alpha_list, 'ko', ms=3)
ax.set_xscale("log")
ax.set(xlabel="Reynolds Number", ylabel=r"$\alpha$ (angle)",
title=r"$C_d$ from Xfoil")
plt.show()
fig, ax = plt.subplots()
ax.tricontour(reynolds_list, alpha_list, cm_values, levels=10, linewidths=0.01, colors='k')
cntr2 = ax.tricontourf(reynolds_list, alpha_list, cm_values, levels=10)
fig.colorbar(cntr2, ax=ax, cmap='viridis')
# ax.plot(reynolds_list, alpha_list, 'ko', ms=3)
ax.set_xscale("log")
ax.set(xlabel="Reynolds Number", ylabel=r"$\alpha$ (angle)",
title=r"$C_m$ from Xfoil")
plt.show()
fig, ax = plt.subplots()
cl_cd = np.divide(cl_values, cd_values)
ax.tricontour(reynolds_list, alpha_list, cl_cd, levels=10, linewidths=0.01, colors='k')
cntr2 = ax.tricontourf(reynolds_list, alpha_list, cl_cd, levels=10)
fig.colorbar(cntr2, ax=ax, cmap='viridis')
# ax.plot(reynolds_list, alpha_list, 'ko', ms=3)
ax.set_xscale("log")
ax.set(xlabel="Reynolds Number", ylabel=r"$\alpha$ (angle)",
title=r"$C_l$/$C_d$ from Xfoil")
plt.show()
def run_cl(alpha_list, reynolds_list, cl_values, smooth_val):
cl_rbf = Rbf(
np.log(np.array(reynolds_list)),
np.array(alpha_list),
np.array(cl_values),
function='linear',
smooth=smooth_val,
)
reynolds = np.geomspace(1000, 100000000, 200)
alphas = np.arange(-15, 15, 0.1)
Reynolds, Alpha = np.meshgrid(reynolds[::8], alphas[::12], indexing="ij")
cl_grid = cl_rbf(np.log(Reynolds.flatten()), Alpha.flatten()).reshape(len(reynolds[::8]), len(alphas[::12])).T
np.save('data/cl_function.npy', cl_grid)
np.save('data/alpha.npy', alphas[::12])
np.save('data/reynolds.npy', reynolds[::8])
return cl_grid
def run_cd(alpha_list, reynolds_list, cd_values, smooth_val):
cd_rbf = Rbf(
np.log(np.array(reynolds_list)),
np.array(alpha_list),
np.log(np.array(cd_values)),
function='linear',
smooth=smooth_val,
)
reynolds = np.geomspace(1000, 100000000, 200)
alphas = np.arange(-15, 15, 0.1)
Reynolds, Alpha = np.meshgrid(reynolds[::8], alphas[::12], indexing="ij")
cd_grid = cd_rbf(np.log(Reynolds.flatten()), Alpha.flatten()).reshape(len(reynolds[::8]), len(alphas[::12])).T
np.save('data/cd_function.npy', cd_grid)
np.save('data/alpha.npy', alphas[::12])
np.save('data/reynolds.npy', reynolds[::8])
return cd_grid
def run_cm(alpha_list, reynolds_list, cm_values, smooth_val):
cm_rbf = Rbf(
np.log(np.array(reynolds_list)),
np.array(alpha_list),
np.array(cm_values),
function='linear',
smooth=smooth_val,
)
reynolds = np.geomspace(1000, 100000000, 200)
alphas = np.arange(-15, 15, 0.1)
Reynolds, Alpha = np.meshgrid(reynolds[::8], alphas[::12], indexing="ij")
cm_grid = cm_rbf(np.log(Reynolds.flatten()), Alpha.flatten()).reshape(len(reynolds[::8]), len(alphas[::12])).T
np.save('data/cm_function.npy', cm_grid)
np.save('data/alpha.npy', alphas[::12])
np.save('data/reynolds.npy', reynolds[::8])
return cm_grid
def run_cl_plot(grid):
fig, ax = plt.subplots()
alphas = np.arange(-15, 15, 0.1)
reynolds = np.geomspace(1000, 100000000, 200)
Reynolds, Alpha = np.meshgrid(reynolds[::8], alphas[::12], indexing="ij")
clr = plt.contourf(
reynolds[::8],
alphas[::12],
grid,
levels=50,
)
plt.contour(
reynolds[::8],
alphas[::12],
grid,
levels=50,
colors='black',
linewidths=0.7
)
ax.set_xscale("log")
ax.set(xlabel="Reynolds Number", ylabel=r"$\alpha$ (angle)",
title=r"$C_l$ after RBF")
fig.colorbar(clr, ax=ax)
plt.show()
def run_cd_plot(cd_grid):
fig, ax = plt.subplots()
alphas = np.arange(-15, 15, 0.1)
reynolds = np.geomspace(1000, 100000000, 200)
Reynolds, Alpha = np.meshgrid(reynolds[::8], alphas[::12], indexing="ij")
clr = plt.contourf(
reynolds[::8],
alphas[::12],
np.exp(cd_grid),
levels=50,
)
plt.contour(
reynolds[::8],
alphas[::12],
np.exp(cd_grid),
levels=50,
colors='black',
linewidths=0.7
)
ax.set_xscale("log")
ax.set(xlabel="Reynolds Number", ylabel=r"$\alpha$ (angle)",
title=r"$C_d$ after RBF")
fig.colorbar(clr, ax=ax)
plt.show()
def run_cm_plot(cm_grid):
fig, ax = plt.subplots()
alphas = np.arange(-15, 15, 0.1)
reynolds = np.geomspace(1000, 100000000, 200)
Reynolds, Alpha = np.meshgrid(reynolds[::8], alphas[::12], indexing="ij")
clr = plt.contourf(
reynolds[::8],
alphas[::12],
cm_grid,
levels=50,
)
plt.contour(
reynolds[::8],
alphas[::12],
cm_grid,
levels=50,
colors='black',
linewidths=0.7
)
ax.set_xscale("log")
ax.set(xlabel="Reynolds Number", ylabel=r"$\alpha$ (angle)",
title=r"$C_m$ after RBF")
fig.colorbar(clr, ax=ax)
plt.show()
def run_cl_cd_plot(cl_grid, cd_grid):
fig, ax = plt.subplots()
alphas = np.arange(-15, 15, 0.1)
reynolds = np.geomspace(1000, 100000000, 200)
Reynolds, Alpha = np.meshgrid(reynolds[::8], alphas[::12], indexing="ij")
clr = plt.contourf(
reynolds[::8],
alphas[::12],
np.divide(cl_grid, np.exp(cd_grid)),
levels=50,
)
plt.contour(
reynolds[::8],
alphas[::12],
np.divide(cl_grid, np.exp(cd_grid)),
levels=50,
colors='black',
linewidths=0.7
)
ax.set_xscale("log")
ax.set(xlabel="Reynolds Number", ylabel=r"$\alpha$ (angle)",
title=r"$C_l/C_d$ after RBF")
fig.colorbar(clr, ax=ax)
plt.show()
path = str(
pathlib.Path(__file__).parent.absolute()
)
# run_xfoil()
alpha_list, reynolds_list, cl_values, cd_values, cm_values = get_Xfoil_dat()
plot_xfoil(alpha_list, reynolds_list, cl_values, cd_values, cm_values)
# sample_resolution = 3
# smooth_val = 10
# alpha_list = alpha_list[::sample_resolution]
# reynolds_list = reynolds_list[::sample_resolution]
# cl_values = cl_values[::sample_resolution]
# cd_values = cd_values[::sample_resolution]
# cm_values = cm_values[::sample_resolution]
#
# alpha_array = np.load(path + '/cache/alpha.npy')
# reynolds_array = np.load(path + '/cache/reynolds.npy')
# cl_grid = run_cl(alpha_list, reynolds_list, cl_values, smooth_val)
# run_cl_plot(cl_grid)
# cl_function = InterpolatedModel({"alpha": alpha_array, "reynolds": np.log(reynolds_array)},
# cl_grid, "bspline")
#
# cd_grid = run_cd(alpha_list, reynolds_list, cd_values, smooth_val)
# run_cd_plot(cd_grid)
# cd_function = InterpolatedModel({"alpha": alpha_array, "reynolds": np.log(reynolds_array)},
# cd_grid, "bspline")
#
# cm_grid = run_cm(alpha_list, reynolds_list, cm_values, smooth_val)
# run_cm_plot(cm_grid)
# cm_function = InterpolatedModel({"alpha": alpha_array, "reynolds": np.log(reynolds_array)},
# cm_grid, "bspline")
#
# # run_cl_cd_plot(cl_grid, cd_grid)