-
Notifications
You must be signed in to change notification settings - Fork 192
/
utils.py
164 lines (139 loc) · 5.04 KB
/
utils.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
# Python 3
# Utils for airfoil.py
# Author: Tristan CB
import numpy as np
import time
import imageio
import matplotlib.pyplot as plt
import matplotlib.lines as lines
from matplotlib import gridspec
from matplotlib import pyplot, transforms
import os
import math
import NACA
from airfoil import panelMethod
def plotFoil(seriesNumber, angle, panels = [12,48]):
fig, ax = plt.subplots()
gs = gridspec.GridSpec(2, 1, height_ratios=[1, 1])
ax = plt.subplot(gs[0])
# first of all, the base transformation of the data points is needed
# Coincides closely enough with the values using the book.
for i in panels:
numberOfPanels = i
XB, YB = NACA.fourDigitSeries(seriesNumber, numberOfPanels)
results = panelMethod(XB,YB, angle=angle)
if i == 12:
marker = 'o'
color = 'black'
label = f'{i}-panel solution'
else:
marker = 'x'
color = 'gray'
label = f'{i}-panel solution'
ax.plot(results["X"], results["CP"], marker,label=label, color='black', markersize=4)
# Format plot
ax.set_ylim(1, -6)
ax.set_xlim(0, 1)
ax.grid(True)
line = lines.Line2D([1, 0], [0,0],
lw=2, color='black', axes=ax)
ax.add_line(line)
ax.set_ylabel('Cp')
ax.set_xlabel('x/c')
ax.legend()
# Plot foil under graph for cp
ax = plt.subplot(gs[1])
XB, YB = NACA.fourDigitSeries(seriesNumber, 48)
# Rotate profile to match incident angle
# We rotate profile arounf 0.4 rougly the center of mass for symmetrical NACA foil.
for i, ij in enumerate(XB):
XB[i], YB[i] = rotate_around_point((XB[i],YB[i]), math.radians(angle), org=(0.4,0.0))
ax.plot(XB, YB, marker, color='black',linestyle='solid', markersize=1)
# Limits for foil plot
ax.set_ylim(-0.2, 0.2)
ax.set_xlim(0, 1)
seriesNumbFormatted = str(seriesNumber).zfill(4)
ax.set_xlabel(f"NACA {seriesNumbFormatted} foil -- @ {angle:.3f} deg")
# Misc figure formatting
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['bottom'].set_visible(False)
plt.tick_params(bottom=False)
ax.set_xticklabels([])
fig.tight_layout(pad=2.0)
# Show plot then return resutls.
return plt
def timeit(method):
"""
Used to time the execution of functions. Useful to use as decorator.
"""
def timed(*args, **kw):
ts = time.time()
result = method(*args, **kw)
te = time.time()
print(f"{method.__name__} Took --> {(te - ts)} <-- seconds")
return result
return timed
def rotateNumpy(xy, radians):
"""
Lyle Scott, III // [email protected]
Use numpy to build a rotation matrix and take the dot product.
https://gist.github.com/LyleScott/e36e08bfb23b1f87af68c9051f985302
"""
x, y = xy
c, s = np.cos(radians), np.sin(radians)
j = np.matrix([[c, s], [-s, c]])
m = np.dot(j, [x, y])
return float(m.T[0]), float(m.T[1])
def rotate_around_point(point, radians, org=(0, 0)):
"""Rotate a point around a given point."""
x, y = point
ox, oy = org
qx = ox + math.cos(radians) * (x - ox) + math.sin(radians) * (y - oy)
qy = oy + -math.sin(radians) * (x - ox) + math.cos(radians) * (y - oy)
return qx, qy
def generateGIF(inputPATH, outputNAME):
"""
Example use: generateGIF("./TMP_GIF/","output")
"""
with imageio.get_writer(f'{inputPATH}{outputNAME}.gif', mode='I') as writer:
for filename in [i for i in (os.listdir(inputPATH)) if "png" in i.split(".")[-1]]:
pathToim = os.path.join(inputPATH,filename)
print(pathToim)
image = imageio.imread(pathToim)
writer.append_data(image)
if __name__ == "__main__":
# Generates Gif for NACA 2412 at varying angles from 0 to 15 deg
for i, ij in enumerate(np.linspace(0,15,100)):
seriesNumber = 2412
seriesNumberFormatted = str(seriesNumber).zfill(4)
plotFoil(seriesNumber,ij).savefig(f"./TMP_GIF/{str(i).zfill(5)}NACA{seriesNumberFormatted}foil.png")
generateGIF("./TMP_GIF/","output")
## Code Graveyard ##
# For all series numbers
# for i in range(6310,9999,10):
# print(i)
# # Skip invalid profiles
# if i % 100 == 0:
# continue
# invalidProfiles = []
# try:
# savFIG(i)
# except:
# invalidProfiles.append(i)
# print(f"Invalid profile: {
# For all series numbers
# for i in range(6310,9999,10):
# print(i)
# # Skip invalid profiles
# if i % 100 == 0:
# continue
# invalidProfiles = []
# try:
# savFIG(i)
# except:
# invalidProfiles.append(i)
# print(f"Invalid profile: {i}")
# For various angles
## Plot pressure distribution for all angles in between -15 and 15
# plt.savefig(f"./TMP_GIF/{seriesNumbFormatted}-{int(angle*10)}NACAfoil.png")