This repository has been archived by the owner on Aug 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
wing.py
272 lines (250 loc) · 12.8 KB
/
wing.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
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
'''
Copyright 2020 University of Liege
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
'''
## @package GeoGen (CFD basic grid creator)
#
# Create an unstructured tetrahedral grid around a wing
# to be meshed with gmsh for Flow or SU2 CFD solvers
# Adrien Crovato
import numpy as np
## Handle wing data
#
# Adrien Crovato
class Wing:
def __init__(self, filenames, span, taper, sweep, dihedral, twist, rootChord, offset):
# Number of airfoils
self.n = len(filenames)
if self.n > 10:
raise Exception('Wing: read {0:d} airfoils but a maximum of 10 is suppored!\n'.format(self.n))
# Convert degrees to radians
sweep = [x * np.pi/180 for x in sweep]
dihedral = [x * np.pi/180 for x in dihedral]
twist = [x * np.pi/180 for x in twist]
# Compute wing shape parameters
self.compShape(span, taper, rootChord)
# Create airfoil points and indices
self.initData(filenames, span, twist, sweep, dihedral, offset)
def compShape(self, span, taper, rootChord):
"""Compute basic shape parameters of the wing
"""
areas = []
self.chord = []
self.spanPos = []
self.chord.append(rootChord)
self.spanPos.append(0.)
for i in range(1, self.n):
self.chord.append(self.chord[i-1]*taper[i-1])
areas.append((self.chord[i-1]+self.chord[i])*span[i-1]/2)
self.spanPos.append(self.spanPos[i-1]+span[i-1])
self.S = sum(areas)
self.b = sum(span)
self.AR = 2 * self.b*self.b/self.S
def initData(self, filenames, span, twist, sweep, dihedral, offset):
"""Read, transform and store airfoil points, and define numbering
"""
self.pts = []
self.ptsN = []
# read and store coordinates (10 airfoils of max. 499 points each: 1-5000)
for i in range(0, self.n):
aPts = self.read(filenames[i])
size = aPts.shape[0]
aPts = np.hstack((aPts, self.spanPos[i]*np.ones([size,1])))
aPts[:,[1,2]] = np.fliplr(aPts[:,[1,2]])
aIdx = np.arange(i*500+1, i*500+1+size)
self.pts.append(aPts)
self.ptsN.append(aIdx)
# transform coordinates
for i in range(0, self.n):
# apply taper (scaling)
self.pts[i][:, [0,2]] = self.pts[i][:, [0,2]]*self.chord[i]
# aplly twist (rotation)
self.pts[i][:, [0,2]] = np.dot(self.pts[i][:, [0,2]],np.array([[np.cos(twist[i]), -np.sin(twist[i])],[np.sin(twist[i]), np.cos(twist[i])]]))
for i in range(1, self.n):
# apply sweep (translation)
self.pts[i][:, 0] = self.pts[i][:, 0] + np.min(self.pts[i-1][:, 0]) + np.tan(sweep[i-1])*span[i-1]
# apply dihedral (translatation)
self.pts[i][:, 2] = self.pts[i][:, 2] + sum(np.tan(dihedral[0:i])*span[0:i])
for i in range(0, self.n):
# apply offset
self.pts[i][:, 0] += offset[0] # x
self.pts[i][:, 2] += offset[1] # z
# get separation points numbering
self.sptsNl = []
self.sptsNg = [] # todo: remove since global index can be recovered from local index: ptsN[local]
for i in range(0, self.n):
numb = self.specPts(i)
self.sptsNl.append(numb)
self.sptsNg.append(numb+i*500+1)
# define line numbering (6 lines per airfoil: 1-60)
self.linaN = []
for i in range(0, self.n):
self.linaN.append(np.arange(i*6+1, (i+1)*6+1))
# define line numbering (6 lines per wing station: 61-114)
self.linpN = []
for i in range(0, self.n-1):
self.linpN.append(np.arange(i*6+61, (i+1)*6+61))
# define surface numbering (6 per wing station: 1-55)
self.surN = []
for i in range(0, self.n-1):
self.surN.append(np.arange(i*6+1, (i+1)*6+1))
def specPts(self, idx):
"""Find (local) index and location of separation points
"""
# fraction of the chord defining separation points (could be given as user-def params)
sepFwd = 0.3
sepAft = 0.9
# trailing and leading edge
te = 0
le = np.argmin(self.pts[idx][:,0])
# find upper separations
teU = np.argmin(np.abs(self.pts[idx][te:le,0]-self.pts[idx][le,0] - sepAft*self.chord[idx]))
leU = np.argmin(np.abs(self.pts[idx][te:le,0]-self.pts[idx][le,0] - sepFwd*self.chord[idx]))
# find lower separations
leL = le + np.argmin(np.abs(self.pts[idx][le:-1,0]-self.pts[idx][le,0] - sepFwd*self.chord[idx]))
teL = le + np.argmin(np.abs(self.pts[idx][le:-1,0]-self.pts[idx][le,0] - sepAft*self.chord[idx]))
return np.array([te , teU, leU, le, leL, teL])
def read(self,fname):
"""Read data from file and stroe in matrix
"""
_file = open(fname, 'r')
label = next(_file).split(',')
_file.close()
data = np.loadtxt(fname, skiprows=1)
return data
def writeInfo(self,fname):
"""Write wing geometrical parameters
"""
file = open(fname, 'a')
file.write('// --- Wing geometry ---\n')
file.write('// Number of spanwise stations: {0:d}\n'.format(self.n))
file.write('// Spanwise stations normalized coordinate: ')
for p in self.spanPos:
file.write('{0:f} '.format(p/self.b))
file.write('\n')
file.write('// Chord lengths: ')
for c in self.chord:
file.write('{0:f} '.format(c))
file.write('\n')
file.write('// Half-wing area: {0:f}\n'.format(self.S))
file.write('// Half-wing span: {0:f}\n'.format(self.b))
file.write('// Full-wing aspect ratio: {0:f}\n'.format(self.AR))
file.write('\n')
file.close()
def writeOpts(self, fname):
"""Write wing gmsh options
"""
file = open(fname, 'a')
file.write('// --- Wing options ---\n')
for i in range(0, self.n):
file.write('DefineConstant[ msLe{0:1d} = {{ {1:f}, Name "leading edge mesh size on {2:1d}th spanwise station" }} ];\n'.format(i, self.chord[i]/100, i))
file.write('DefineConstant[ msTe{0:1d} = {{ {1:f}, Name "trailing edge mesh size on {2:1d}th spanwise station" }} ];\n'.format(i, self.chord[i]/100, i))
file.write('DefineConstant[ gr{0:1d} = {{ {1:f}, Name "growth ratio for {2:1d}th spanwise station" }} ];\n'.format(i, 1.5, i))
file.write('\n')
file.close()
def writePoints(self,fname):
"""Write wing points
"""
file = open(fname, 'a')
file.write('// --- Wing points ---\n')
for i in range(0, self.n):
file.write('// -- Airfoil {0:d}\n'.format(i))
# TE
file.write('Point({0:d}) = {{{1:f},{2:f},{3:f},msTe{4:d}}};\n'.format(self.ptsN[i][self.sptsNl[i][0]], self.pts[i][self.sptsNl[i][0],0], self.pts[i][self.sptsNl[i][0],1], self.pts[i][self.sptsNl[i][0],2], i))
for j in range(self.sptsNl[i][0]+1, self.sptsNl[i][1]):
file.write('Point({0:d}) = {{{1:f},{2:f},{3:f}}};\n'.format(self.ptsN[i][j], self.pts[i][j,0], self.pts[i][j,1], self.pts[i][j,2]))
# upper TE
file.write('Point({0:d}) = {{{1:f},{2:f},{3:f},gr{4:d}*msTe{4:d}}};\n'.format(self.ptsN[i][self.sptsNl[i][1]], self.pts[i][self.sptsNl[i][1],0], self.pts[i][self.sptsNl[i][1],1], self.pts[i][self.sptsNl[i][1],2], i))
for j in range(self.sptsNl[i][1]+1, self.sptsNl[i][2]):
file.write('Point({0:d}) = {{{1:f},{2:f},{3:f}}};\n'.format(self.ptsN[i][j], self.pts[i][j,0], self.pts[i][j,1], self.pts[i][j,2]))
# upper LE
file.write('Point({0:d}) = {{{1:f},{2:f},{3:f},gr{4:d}*msLe{4:d}}};\n'.format(self.ptsN[i][self.sptsNl[i][2]], self.pts[i][self.sptsNl[i][2],0], self.pts[i][self.sptsNl[i][2],1], self.pts[i][self.sptsNl[i][2],2], i))
for j in range(self.sptsNl[i][2]+1, self.sptsNl[i][3]):
file.write('Point({0:d}) = {{{1:f},{2:f},{3:f}}};\n'.format(self.ptsN[i][j], self.pts[i][j,0], self.pts[i][j,1], self.pts[i][j,2]))
# LE
file.write('Point({0:d}) = {{{1:f},{2:f},{3:f},msLe{4:d}}};\n'.format(self.ptsN[i][self.sptsNl[i][3]], self.pts[i][self.sptsNl[i][3],0], self.pts[i][self.sptsNl[i][3],1], self.pts[i][self.sptsNl[i][3],2], i))
for j in range(self.sptsNl[i][3]+1, self.sptsNl[i][4]):
file.write('Point({0:d}) = {{{1:f},{2:f},{3:f}}};\n'.format(self.ptsN[i][j], self.pts[i][j,0], self.pts[i][j,1], self.pts[i][j,2]))
# lower LE
file.write('Point({0:d}) = {{{1:f},{2:f},{3:f},gr{4:d}*msLe{4:d}}};\n'.format(self.ptsN[i][self.sptsNl[i][4]], self.pts[i][self.sptsNl[i][4],0], self.pts[i][self.sptsNl[i][4],1], self.pts[i][self.sptsNl[i][4],2], i))
for j in range(self.sptsNl[i][4]+1, self.sptsNl[i][5]):
file.write('Point({0:d}) = {{{1:f},{2:f},{3:f}}};\n'.format(self.ptsN[i][j], self.pts[i][j,0], self.pts[i][j,1], self.pts[i][j,2]))
# lower TE
file.write('Point({0:d}) = {{{1:f},{2:f},{3:f},gr{4:d}*msTe{4:d}}};\n'.format(self.ptsN[i][self.sptsNl[i][5]], self.pts[i][self.sptsNl[i][5],0], self.pts[i][self.sptsNl[i][5],1], self.pts[i][self.sptsNl[i][5],2], i))
for j in range(self.sptsNl[i][5]+1, self.ptsN[i].shape[0]-1):
file.write('Point({0:d}) = {{{1:f},{2:f},{3:f}}};\n'.format(self.ptsN[i][j], self.pts[i][j,0], self.pts[i][j,1], self.pts[i][j,2]))
file.write('\n')
file.close()
def writeLines(self, fname):
"""Write wing lines
"""
file = open(fname, 'a')
file.write('// --- Wing lines ---\n')
# airfoil lines
for i in range(0, self.n):
file.write('// -- Airfoil {0:d}\n'.format(i))
for j in range(0, self.linaN[i].shape[0]-1):
file.write('Spline({0:d}) = {{'.format(self.linaN[i][j]))
for k in range(self.sptsNg[i][j], self.sptsNg[i][j+1]):
file.write('{0:d}, '.format(k))
file.write('{0:d}'.format(self.sptsNg[i][j+1]))
file.write('};\n')
file.write('Spline({0:d}) = {{'.format(self.linaN[i][-1]))
for k in range(self.sptsNg[i][self.linaN[i].shape[0]-1], self.sptsNg[i][0]+self.ptsN[i].shape[0]-1):
file.write('{0:d}, '.format(k))
file.write('{0:d}'.format(self.sptsNg[i][0]))
file.write('};\n')
# planform lines
for i in range(0, self.n-1):
file.write('// -- Planform {0:d}\n'.format(i))
for j in range(0, self.linpN[i].shape[0]):
file.write('Line({0:d}) = {{{1:d},{2:d}}};\n'.format(self.linpN[i][j], self.sptsNg[i][j], self.sptsNg[i+1][j]))
file.write('\n')
file.close()
def writeSurfaces(self, fname):
"""Write wing line loops and surfaces
"""
file = open(fname, 'a')
file.write('// --- Wing line loops and surfaces ---\n')
for i in range(0, self.n-1):
file.write('// -- Planform {0:d}\n'.format(i))
for j in range(0, self.surN[i].shape[0]):
file.write('Line Loop({0:d}) = {{{1:d},{2:d},{3:d},{4:d}}};\n'.format(self.surN[i][j], self.linaN[i][j], self.linpN[i][np.mod(j+1,self.linpN[i].shape[0])], -self.linaN[i+1][j], -self.linpN[i][j]))
for j in range(0, self.surN[i].shape[0]):
file.write('Surface({0:d}) = {{-{0:d}}};\n'.format(self.surN[i][j]))
file.write('\n')
file.close()
def writePhysical(self, fname):
"""Write wing physical groups
"""
import os
file = open(fname, 'a')
file.write('// --- Wing physical groups ---\n')
file.write('Physical Surface("wing") = {')
for i in range(0, self.n-1):
for j in range(0, 3):
file.write('{0:d},'.format(self.surN[i][j]))
file.seek(0, os.SEEK_END) # first seek end of file; f.seek(0, 2) is legal
file.seek(file.tell() - 1, os.SEEK_SET) # then go backward
file.truncate()
file.write('};\n')
file.write('Physical Surface("wing_") = {')
for i in range(0, self.n-1):
for j in range(3, 6):
file.write('{0:d},'.format(self.surN[i][j]))
file.seek(0, os.SEEK_END)
file.seek(file.tell() - 1, os.SEEK_SET)
file.truncate()
file.write('};\n')
file.write('\n')
file.close()