forked from djlee9812/prop-design
-
Notifications
You must be signed in to change notification settings - Fork 0
/
qmil_design.py
177 lines (164 loc) · 5.65 KB
/
qmil_design.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
import subprocess
import numpy as np
import re
import matplotlib.pyplot as plt
from scipy.optimize import minimize_scalar
from air_data import change_air_data
R = 1.10
NUM_MOTOR = 4
HUB_R = .12
def get_num(line):
"""
Regex function to find efficiency number in qmil output
"""
nums = re.findall("\d+\.\d+", line)
try:
num = float(nums[0])
except:
# catch error of efficiency not found
print("no number found here")
return 0
if len(nums) > 1 or num > 1 or num < 0:
# check if too many values found or efficiency out of range
print("something wrong")
return 0
return num
def design_prop(rpm, out_file="temp_prop", traj_eval=False, opt=False):
"""
Rewrites template.qmil file to change desired rpm and returns
efficiency of new designed prop
If out_file is specified, that prop geometry is saved to the given filename
"""
with open('template.mil', 'r') as file:
data = file.readlines()
data[18] = " " + str(rpm) + " ! rpm\n"
with open('output.mil', 'w') as file:
file.writelines(data)
filename = 'output.mil'
cmd = ['qmil', filename, out_file]
process = subprocess.Popen(cmd, stdout=subprocess.PIPE)
output = process.communicate()[0]
for line in output.splitlines():
l = str(line)
if "eff = " in l:
eff = get_num(l)
process.wait()
if traj_eval:
from trajectory import follow_trajectory
traj_data = np.load('time_altitude_airspeed.npz')
ts = traj_data['t']/3600
hs = traj_data['h']
vs = traj_data['v']
thrusts = traj_data['thrust']/NUM_MOTOR
eff = follow_trajectory(ts, hs, vs, thrusts, optimize=opt,
prop="temp_prop", save=False)
return -eff
def change_prop_area(area):
"""
Change tip_radius in template.qmil using given total propulsive area and
scaled according to number of motors and hub diameter
"""
tip_r = np.round(np.sqrt((area/NUM_MOTOR)/np.pi + HUB_R**2), 3)
with open('template.mil', 'r') as file:
data = file.readlines()
data[16] = " " + str(tip_r) + " ! tip radius(m)\n"
with open('template.mil', 'w') as file:
file.writelines(data)
design_opt_rpm()
def plot_prop(propfile):
"""
Plot the propeller r/c and beta/c distribution and the actual geometry
:params propfile: Filename or path to file containing propeller geometry
"""
f = open(propfile, "r")
contents = f.readlines()
prop_geom = contents[-32:]
P = len(prop_geom)
r = []
c = []
beta = []
for i in range(1,P):
line = prop_geom[i].split()
# print(line)
r.append(float(line[0])/R)
c.append(float(line[1])/R)
beta.append(float(line[2]))
f = plt.figure()
plt.subplot(211)
plt.plot(r,c,'b')
plt.axhline(y=np.mean(c), linestyle=":", color="gray")
plt.annotate("Average c/R", xy=(0.9,np.mean(c)+.01))
plt.ylabel("c/R", fontsize="16")
plt.title(r"Propeller Chord and $\beta$, R = " + str(R) + "m", fontsize="18")
plt.subplot(212)
plt.plot(r, beta, 'b', label = "QMIL")
plt.ylabel(r"$\beta$", fontsize="16")
plt.xlabel('r/R', fontsize="16")
radius = HUB_R / R
hub_x = np.arange(0, radius, 0.002)
hub_y = np.sqrt(radius**2 - hub_x**2)
hub_x = np.concatenate([hub_x, hub_x[::-1]])
hub_y = np.concatenate([hub_y, -hub_y[::-1]])
fig, ax = plt.subplots()
c = np.array(c)
prop_r = np.concatenate([r, r[::-1]])
prop_c = np.concatenate([c/4, -3*c[::-1]/4])
prop_c += np.mean(c/4)
print("Avg c/R:", np.mean(c))
plt.plot(prop_r, prop_c, 'b', label="Flattened propeller")
plt.plot(hub_x, hub_y, 'r', label="Propeller hub")
# circle = plt.Circle((0, 0), 0.15, color='r')
plt.xlim(0, 1)
plt.ylim(-0.5, 0.5)
plt.title("Propeller, R = " + str(R) + "m", fontsize="18")
plt.ylabel("x/R", fontsize="16")
plt.xlabel('y/R', fontsize="16")
plt.gca().set_aspect('equal', adjustable='box')
plt.legend()
# ax.add_artist(circle)
plt.show()
def plot_blade_cl():
plt.figure()
plt.plot([0, 0.1, 0.2, 0.5, 1.0], [0.84, 0.80, 0.64, 0.59, 0.55])
plt.xlabel("r/R", fontsize="16")
plt.ylabel("Blade cl", fontsize="16")
plt.title("Prop Blade cl Distribution", fontsize="18")
plt.show()
def design_opt_rpm(h=21000, plot=False, traj=False, opt=False):
"""
Design a propeller by optimizing for efficiency on rpm for flight conditions
given in template.mil and optional argument altitude
"""
change_air_data(h)
res = minimize_scalar(design_prop, bounds=(1100, 1600), method='bounded',
args=("temp_prop", traj, opt), options={'xatol': 2})
if res.success:
design_prop(res.x, "best_prop")
if plot:
print(-res.fun, res.x)
plot_prop("best_prop")
return res.x
else:
print("Unsuccessful optimization", res.x, res.fun)
if __name__ == "__main__":
# change_prop_area(24)
# design_opt_rpm(h=19800, plot=True, traj=True, opt=False)
# change_air_data(20000)
# design_prop(1100, "best_prop")
plot_prop('best_prop')
# plot_blade_cl()
# print(-design_prop(1200, "test_prop"))
# Sensitivity study to design RPM
# rpms = np.arange(600.0, 2500.0, 100.0)
# etas = np.zeros(len(rpms))
#
# for i, rpm in enumerate(rpms):
# eff = design_prop(rpm)
# etas[i] = eff
# plt.plot(rpms, -etas)
# plt.xlabel("rpm")
# plt.ylabel(r"$\eta$")
# plt.show()
# best_rpm_i = np.argmax(etas)
# print(best_rpm_i, rpms[best_rpm_i], etas[best_rpm_i])
# design_prop(rpms[best_rpm_i], "best_prop")