Skip to content

Commit

Permalink
sync benchmarking
Browse files Browse the repository at this point in the history
  • Loading branch information
peterdsharpe committed Mar 12, 2024
1 parent c3e7ba4 commit cae4f58
Show file tree
Hide file tree
Showing 2 changed files with 225 additions and 167 deletions.
225 changes: 225 additions & 0 deletions benchmarking/neuralfoil_point_comparison.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,225 @@
import aerosandbox as asb
import aerosandbox.numpy as np
from aerosandbox.tools.string_formatting import eng_string
from neuralfoil import get_aero_from_airfoil
from pathlib import Path
from matplotlib.colors import LinearSegmentedColormap
from tqdm import tqdm

af = asb.Airfoil(name="HALE_03", coordinates=Path(__file__).parent / "assets" / "hale_03mod.dat")
alphas_xfoil = np.linspace(-5, 15, 50)
alphas_nf = np.linspace(-6, 17, 300)
Re_values_to_test = [1e4, 8e4, 2e5, 1e6, 1e8]

# Obtain data
aeros = {}

aeros["xfoil"] = [
asb.XFoil(
airfoil=af,
Re=Re,
timeout=30,
xfoil_repanel=False,
max_iter=100,
).alpha(alphas_xfoil)
for Re in tqdm(Re_values_to_test, desc="XFoil")
]

nf_model_sizes = [
"medium", "xxxlarge"
]
nf_linestyles = ["--", "-"]
xfoil_linestyle = "k:"

for model_size in nf_model_sizes:
aeros[model_size] = [
get_aero_from_airfoil(
airfoil=af,
alpha=alphas_nf,
Re=Re,
model_size=model_size
)
for Re in tqdm(Re_values_to_test, desc=f"NeuralFoil {model_size}")
]

import matplotlib.pyplot as plt
import aerosandbox.tools.pretty_plots as p

fig, ax = plt.subplots(figsize=(8,5))
plt.xscale('log')

cmap = LinearSegmentedColormap.from_list(
"custom_cmap",
colors=[
p.adjust_lightness(c, 0.8) for c in
["orange", "darkseagreen", "dodgerblue"]
]
)
colors = cmap(np.linspace(0, 1, len(Re_values_to_test)))
transparency = 0.7

for i, (Re, color) in enumerate(zip(Re_values_to_test, colors)):

nf_line2ds = []

for model_size, linestyle in zip(nf_model_sizes, nf_linestyles):
nf_aero = aeros[model_size][i]
nf_line2d, = plt.plot(
nf_aero["CD"],
nf_aero["CL"],
linestyle,
zorder=4,
color=color,
alpha=transparency,
)
nf_line2ds.append(nf_line2d)

xfoil_line2d, = plt.plot(
aeros["xfoil"][i]["CD"],
aeros["xfoil"][i]["CL"],
linestyle=":",
color=p.adjust_lightness(color, 0.4),
alpha=1
)


annotate_x = np.max(np.array([
aero[i]["CD"][-1]
for aero in aeros.values()
]))
annotate_y = np.median(np.array([
aero[i]["CL"][-1]
for aero in aeros.values()
]))

plt.annotate(
f" ${{\\rm Re}} = \\mathrm{{{eng_string(Re)}}}$",
xy=(annotate_x, annotate_y),
color=p.adjust_lightness(color, 0.8),
ha="left", va="center", fontsize=10
)

plt.annotate(
text="Note the log-scale on $C_D$, which is unconventional - it's\nthe only way to keep it readable given the wide range.",
xy=(0.01, 0.01),
xycoords="figure fraction",
ha="left",
va="bottom",
fontsize=8,
alpha=0.5
)

from matplotlib.lines import Line2D

legend_handles = [
Line2D([], [], color="k", linestyle=xfoil_line2d.get_linestyle(), label="XFoil (ground truth)"),
*[
Line2D([], [], color=color, linestyle=linestyle, label=f"NF \"{model_size}\"")
for model_size, linestyle, color in zip(nf_model_sizes, nf_linestyles, colors)
]
]

for h in legend_handles:
h.set_color("k")

plt.legend(
handles=legend_handles,
title="Analysis Method",
# loc=(0.8, 0.15),
loc='lower left',
# bbox_to_anchor=(0.5, 0., 0.5, 0.5),
fontsize=11,
labelspacing=0.3, columnspacing=1.5, handletextpad=0.4,
framealpha=0.8,
)

plt.xlim()
plt.ylim(bottom=-0.8)

afax = ax.inset_axes([0.76, 0.802, 0.23, 0.23])
afax.fill(
af.x(), af.y(),
facecolor=(0, 0, 0, 0.2), linewidth=1, edgecolor=(0, 0, 0, 0.7)
)
afax.annotate(
text=f"{af.name} Airfoil",
xy=(0.5, 0.15),
xycoords="data",
ha="center",
va="bottom",
fontsize=10,
alpha=0.7
)

afax.grid(False)
afax.set_xticks([])
afax.set_yticks([])
# afax.axis('off')
afax.set_facecolor((1, 1, 1, 0.5))
afax.set_xlim(-0.05, 1.05)
afax.set_ylim(-0.05, 0.28)
afax.set_aspect("equal", adjustable='box')


plt.suptitle("Comparison of $C_L$-$C_D$ Polar for NeuralFoil vs. XFoil", fontsize=16,y=0.94)
plt.title(f"On {af.name} Airfoil (out-of-sample)", fontsize=12, alpha=0.7)

plt.xlabel("Drag Coefficient $C_D$")
plt.ylabel("Lift Coefficient $C_L$")

p.show_plot(
None,
legend=False,
savefig="neuralfoil_point_comparison.svg",
savefig_transparent=False,
rotate_axis_labels=False
)

# p.plot_color_by_value(
# nf_aero_xl["CD"],
# nf_aero_xl["CL"],
# c=nf_aero_xl["analysis_confidence"],
# clim=(0.8, 1),
# colorbar=Re == Re_values_to_test[-1],
# colorbar_label="Analysis Confidence" if Re == Re_values_to_test[-1] else None,
# cmap="turbo_r",
# # "--",
# # color=color,
# zorder=4,
# )

# plt.plot(
# nf_aero_xl["CD"],
# nf_aero_xl["CL"],
# "--",
# color=color, alpha=transparency
# )

# nf_aero_m = get_aero_from_airfoil(
# airfoil=af,
# alpha=alphas_nf,
# Re=Re,
# model_size="medium"
# )
#
# plt.plot(
# nf_aero_m["CD"],
# nf_aero_m["CL"],
# ":",
# color=color, alpha=transparency
# )

# xfoil_aero = asb.XFoil(
# airfoil=af,
# Re=Re,
# timeout=30,
# max_iter=100,
# ).alpha(alpha=alphas_xfoil, start_at=5)
#
# plt.plot(
# xfoil_aero["CD"],
# xfoil_aero["CL"],
# "--",
# color="k", alpha=0.4
# )
# xfoil_aero = nf_aero
167 changes: 0 additions & 167 deletions benchmarking/neuralfoil_point_validation.py

This file was deleted.

0 comments on commit cae4f58

Please sign in to comment.