-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvisualization.py
More file actions
428 lines (334 loc) · 14 KB
/
Copy pathvisualization.py
File metadata and controls
428 lines (334 loc) · 14 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
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
"""
Visualization Module
Handles all plotting and chart generation
"""
import os
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
from aerosandbox.tools.string_formatting import eng_string
from config import PLOT_DPI
class AirfoilPlotter:
"""Creates plots for airfoil analysis results"""
def __init__(self, db, figsize=(10, 6), dpi=PLOT_DPI):
"""
Initialize plotter with database
Args:
db: DataFrame with airfoil analysis results
figsize: Figure size tuple (width, height)
dpi: Dots per inch for plot resolution
"""
self.db = db
self.figsize = figsize
self.dpi = dpi
def plot_cl_vs_alpha(self, airfoil_name, show=True):
"""
Plot CL vs alpha for an airfoil at different Reynolds numbers
Args:
airfoil_name: Name of airfoil to plot
show: If True, displays plot immediately
Returns:
Figure and axes objects
"""
af_data = self.db[self.db["airfoil_name"] == airfoil_name]
if af_data.empty:
raise ValueError(f"Airfoil '{airfoil_name}' not found in database")
fig, ax = plt.subplots(figsize=self.figsize, dpi=self.dpi)
for Re_val in sorted(af_data["Re"].unique()):
d = af_data[af_data["Re"] == Re_val]
ax.plot(
d["alpha_deg"],
d["CL"],
label=f"Re = {eng_string(Re_val)}",
linewidth=2
)
ax.set_title(f"$C_L$ vs $\\alpha$ - {airfoil_name}", fontsize=14, fontweight='bold')
ax.set_xlabel("Angle of attack $\\alpha$ [deg]", fontsize=12)
ax.set_ylabel("Lift coefficient $C_L$", fontsize=12)
ax.legend()
ax.grid(True, alpha=0.3)
plt.tight_layout()
if show:
plt.show()
return fig, ax
def plot_cd_vs_alpha(self, airfoil_name, show=True):
"""Plot CD vs alpha for an airfoil"""
af_data = self.db[self.db["airfoil_name"] == airfoil_name]
if af_data.empty:
raise ValueError(f"Airfoil '{airfoil_name}' not found in database")
fig, ax = plt.subplots(figsize=self.figsize, dpi=self.dpi)
for Re_val in sorted(af_data["Re"].unique()):
d = af_data[af_data["Re"] == Re_val]
ax.plot(
d["alpha_deg"],
d["CD"],
label=f"Re = {eng_string(Re_val)}",
linewidth=2
)
ax.set_title(f"$C_D$ vs $\\alpha$ - {airfoil_name}", fontsize=14, fontweight='bold')
ax.set_xlabel("Angle of attack $\\alpha$ [deg]", fontsize=12)
ax.set_ylabel("Drag coefficient $C_D$", fontsize=12)
ax.legend()
ax.grid(True, alpha=0.3)
plt.tight_layout()
if show:
plt.show()
return fig, ax
def plot_cl_vs_cd(self, airfoil_name, show=True):
"""Plot CL vs CD (drag polar) for an airfoil"""
af_data = self.db[self.db["airfoil_name"] == airfoil_name]
if af_data.empty:
raise ValueError(f"Airfoil '{airfoil_name}' not found in database")
fig, ax = plt.subplots(figsize=self.figsize, dpi=self.dpi)
for Re_val in sorted(af_data["Re"].unique()):
d = af_data[af_data["Re"] == Re_val]
ax.plot(
d["CD"],
d["CL"],
label=f"Re = {eng_string(Re_val)}",
linewidth=2
)
ax.set_title(f"$C_L$ vs $C_D$ (Drag Polar) - {airfoil_name}", fontsize=14, fontweight='bold')
ax.set_xlabel("Drag coefficient $C_D$", fontsize=12)
ax.set_ylabel("Lift coefficient $C_L$", fontsize=12)
ax.legend()
ax.grid(True, alpha=0.3)
plt.tight_layout()
if show:
plt.show()
return fig, ax
def plot_ld_vs_alpha(self, airfoil_name, show=True):
"""Plot L/D ratio vs alpha for an airfoil"""
af_data = self.db[self.db["airfoil_name"] == airfoil_name]
if af_data.empty:
raise ValueError(f"Airfoil '{airfoil_name}' not found in database")
fig, ax = plt.subplots(figsize=self.figsize, dpi=self.dpi)
for Re_val in sorted(af_data["Re"].unique()):
d = af_data[af_data["Re"] == Re_val]
ax.plot(
d["alpha_deg"],
d["CL/CD"],
label=f"Re = {eng_string(Re_val)}",
linewidth=2
)
ax.set_title(f"$C_L/C_D$ vs $\\alpha$ - {airfoil_name}", fontsize=14, fontweight='bold')
ax.set_xlabel("Angle of attack $\\alpha$ [deg]", fontsize=12)
ax.set_ylabel("Lift-to-drag ratio $C_L/C_D$", fontsize=12)
ax.legend()
ax.grid(True, alpha=0.3)
plt.tight_layout()
if show:
plt.show()
return fig, ax
def plot_cm_vs_alpha(self, airfoil_name, show=True):
"""Plot moment coefficient vs alpha for an airfoil"""
af_data = self.db[self.db["airfoil_name"] == airfoil_name]
if af_data.empty:
raise ValueError(f"Airfoil '{airfoil_name}' not found in database")
fig, ax = plt.subplots(figsize=self.figsize, dpi=self.dpi)
for Re_val in sorted(af_data["Re"].unique()):
d = af_data[af_data["Re"] == Re_val]
ax.plot(
d["alpha_deg"],
d["CM"],
label=f"Re = {eng_string(Re_val)}",
linewidth=2
)
ax.set_title(f"$C_M$ vs $\\alpha$ - {airfoil_name}", fontsize=14, fontweight='bold')
ax.set_xlabel("Angle of attack $\\alpha$ [deg]", fontsize=12)
ax.set_ylabel("Moment coefficient $C_M$", fontsize=12)
ax.legend()
ax.grid(True, alpha=0.3)
plt.tight_layout()
if show:
plt.show()
return fig, ax
def plot_all_for_airfoil(self, airfoil_name):
"""Create all plots for a single airfoil"""
self.plot_cl_vs_alpha(airfoil_name, show=False)
self.plot_cd_vs_alpha(airfoil_name, show=False)
self.plot_cl_vs_cd(airfoil_name, show=False)
self.plot_ld_vs_alpha(airfoil_name, show=False)
self.plot_cm_vs_alpha(airfoil_name, show=True)
class ComparisonPlotter:
"""Creates comparison plots for multiple airfoils"""
def __init__(self, db, figsize=(10, 6), dpi=PLOT_DPI):
"""
Initialize comparison plotter
Args:
db: DataFrame with airfoil analysis results
figsize: Figure size tuple
dpi: Plot resolution
"""
self.db = db
self.figsize = figsize
self.dpi = dpi
def plot_cl_comparison(self, airfoil_names, re_target, show=True):
"""
Compare CL vs alpha for multiple airfoils at specific Re
Args:
airfoil_names: List of airfoil names to compare
re_target: Target Reynolds number
show: If True, displays plot immediately
Returns:
Figure and axes objects
"""
fig, ax = plt.subplots(figsize=self.figsize, dpi=self.dpi)
for af in airfoil_names:
d = self.db[
(self.db["airfoil_name"] == af) &
(np.isclose(self.db["Re"], re_target, rtol=1e-4))
]
if not d.empty:
ax.plot(d["alpha_deg"], d["CL"], label=af, linewidth=2, marker='o', markersize=3)
ax.set_title(f"$C_L$ vs $\\alpha$ Comparison at Re={eng_string(re_target)}",
fontsize=14, fontweight='bold')
ax.set_xlabel("Angle of attack $\\alpha$ [deg]", fontsize=12)
ax.set_ylabel("Lift coefficient $C_L$", fontsize=12)
ax.legend()
ax.grid(True, alpha=0.3)
plt.tight_layout()
if show:
plt.show()
return fig, ax
def plot_cd_comparison(self, airfoil_names, re_target, show=True):
"""Compare CD vs alpha for multiple airfoils"""
fig, ax = plt.subplots(figsize=self.figsize, dpi=self.dpi)
for af in airfoil_names:
d = self.db[
(self.db["airfoil_name"] == af) &
(np.isclose(self.db["Re"], re_target, rtol=1e-4))
]
if not d.empty:
ax.plot(d["alpha_deg"], d["CD"], label=af, linewidth=2, marker='o', markersize=3)
ax.set_title(f"$C_D$ vs $\\alpha$ Comparison at Re={eng_string(re_target)}",
fontsize=14, fontweight='bold')
ax.set_xlabel("Angle of attack $\\alpha$ [deg]", fontsize=12)
ax.set_ylabel("Drag coefficient $C_D$", fontsize=12)
ax.legend()
ax.grid(True, alpha=0.3)
plt.tight_layout()
if show:
plt.show()
return fig, ax
def plot_polar_comparison(self, airfoil_names, re_target, show=True):
"""Compare drag polars for multiple airfoils"""
fig, ax = plt.subplots(figsize=self.figsize, dpi=self.dpi)
for af in airfoil_names:
d = self.db[
(self.db["airfoil_name"] == af) &
(np.isclose(self.db["Re"], re_target, rtol=1e-4))
]
if not d.empty:
ax.plot(d["CD"], d["CL"], label=af, linewidth=2, marker='o', markersize=3)
ax.set_title(f"Drag Polar Comparison at Re={eng_string(re_target)}",
fontsize=14, fontweight='bold')
ax.set_xlabel("Drag coefficient $C_D$", fontsize=12)
ax.set_ylabel("Lift coefficient $C_L$", fontsize=12)
ax.legend()
ax.grid(True, alpha=0.3)
plt.tight_layout()
if show:
plt.show()
return fig, ax
def plot_ld_comparison(self, airfoil_names, re_target, show=True):
"""Compare L/D ratio for multiple airfoils"""
fig, ax = plt.subplots(figsize=self.figsize, dpi=self.dpi)
for af in airfoil_names:
d = self.db[
(self.db["airfoil_name"] == af) &
(np.isclose(self.db["Re"], re_target, rtol=1e-4))
]
if not d.empty:
ax.plot(d["alpha_deg"], d["CL/CD"], label=af, linewidth=2, marker='o', markersize=3)
ax.set_title(f"$C_L/C_D$ Comparison at Re={eng_string(re_target)}",
fontsize=14, fontweight='bold')
ax.set_xlabel("Angle of attack $\\alpha$ [deg]", fontsize=12)
ax.set_ylabel("Lift-to-drag ratio $C_L/C_D$", fontsize=12)
ax.legend()
ax.grid(True, alpha=0.3)
plt.tight_layout()
if show:
plt.show()
return fig, ax
def plot_cm_comparison(self, airfoil_names, re_target, show=True):
"""Compare moment coefficient for multiple airfoils"""
fig, ax = plt.subplots(figsize=self.figsize, dpi=self.dpi)
for af in airfoil_names:
d = self.db[
(self.db["airfoil_name"] == af) &
(np.isclose(self.db["Re"], re_target, rtol=1e-4))
]
if not d.empty:
ax.plot(d["alpha_deg"], d["CM"], label=af, linewidth=2, marker='o', markersize=3)
ax.set_title(f"$C_M$ Comparison at Re={eng_string(re_target)}",
fontsize=14, fontweight='bold')
ax.set_xlabel("Angle of attack $\\alpha$ [deg]", fontsize=12)
ax.set_ylabel("Moment coefficient $C_M$", fontsize=12)
ax.legend()
ax.grid(True, alpha=0.3)
plt.tight_layout()
if show:
plt.show()
return fig, ax
def plot_airfoil_shape(airfoil_path, show=True):
"""
Plot the geometry/shape of an airfoil
Args:
airfoil_path: Path to the .dat airfoil file
show: If True, displays plot immediately
Returns:
Figure and axes objects
"""
import aerosandbox as asb
airfoil = asb.Airfoil(airfoil_path)
airfoil_name = os.path.basename(airfoil_path).split('.')[0]
fig, ax = plt.subplots(figsize=(12, 4), dpi=PLOT_DPI)
# Plot airfoil coordinates
ax.plot(airfoil.coordinates[:, 0], airfoil.coordinates[:, 1],
'b-', linewidth=2, label='Airfoil Shape')
ax.fill(airfoil.coordinates[:, 0], airfoil.coordinates[:, 1],
alpha=0.2, color='steelblue')
ax.set_xlabel('x/c', fontsize=12)
ax.set_ylabel('y/c', fontsize=12)
ax.set_title(f'Airfoil Geometry - {airfoil_name}', fontsize=14, fontweight='bold')
ax.grid(True, alpha=0.3)
ax.set_aspect('equal')
ax.legend()
plt.tight_layout()
if show:
plt.show()
return fig, ax
def create_ranking_barplot(ranked_df, top_n=10, show=True):
"""
Create bar plot of top ranked airfoils
Args:
ranked_df: DataFrame with ranked airfoils
top_n: Number of top airfoils to show
show: If True, displays plot immediately
Returns:
Figure and axes objects
"""
top_airfoils = ranked_df.head(top_n)
fig, ax = plt.subplots(figsize=(12, 6), dpi=PLOT_DPI)
bars = ax.barh(
range(len(top_airfoils)),
top_airfoils["score"],
color='steelblue'
)
ax.set_yticks(range(len(top_airfoils)))
ax.set_yticklabels(top_airfoils["airfoil_name"])
ax.set_xlabel("Score", fontsize=12)
ax.set_ylabel("Airfoil", fontsize=12)
ax.set_title(f"Top {top_n} Ranked Airfoils", fontsize=14, fontweight='bold')
ax.grid(True, alpha=0.3, axis='x')
ax.invert_yaxis()
# Add score labels on bars
for i, bar in enumerate(bars):
width = bar.get_width()
ax.text(width, bar.get_y() + bar.get_height()/2,
f'{width:.3f}', ha='left', va='center', fontsize=10)
plt.tight_layout()
if show:
plt.show()
return fig, ax