-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgmeshing.py
235 lines (179 loc) · 8.66 KB
/
gmeshing.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
#Using gmsh to define the points on the profile
#Author: Danick Lamoureux and Clément Audefroy, based on Olivier Duchesne and David Lessard's codes
#Project under Frédérick Gosselin and Sébastien Houde's supervision
#Date: 2022-05-04
import gmsh
import numpy as np
import os
def airfoil_gmeshing(polygon_vertices, mesh_size, n=1, show = False):
"""Meshing the airfoil using GMSH
Args:
polygon_vertices (list): Vertices of the airfoil
mesh_size (float): Mesh size.
n (int, optional): Number of hydrofoils in cascade. Defaults to 1.
show (bool, optional): Bool to define if GMSH software is used to show the mesh. Defaults to False.
Returns:
list, list: List of nodes and elements
"""
gmsh.initialize()
gmsh.option.setNumber("General.Terminal", 1)
gmsh.model.add("modele_test")
npoints = int(len(polygon_vertices)/n)
for vertex_index in range(len(polygon_vertices)): # Adding all the points of the profile
gmsh.model.geo.addPoint(polygon_vertices[vertex_index,0],polygon_vertices[vertex_index,1],
polygon_vertices[vertex_index,2], mesh_size, vertex_index)
# Adding the overall spline and creating the surface
for i in range(n):
gmsh.model.geo.addSpline(np.linspace(i*npoints,(i+1)*npoints-1,npoints),2*i)
gmsh.model.geo.addLine((i+1)*npoints-1,i*npoints,2*i+1)
gmsh.model.geo.addCurveLoop([2*i,2*i+1],tag = 10*i + 2*n + 1)
gmsh.model.geo.addPlaneSurface(wireTags= [10*i + 2*n + 1],tag = i)
gmsh.model.geo.synchronize()
gmsh.option.setNumber("Mesh.Smoothing", 10)
gmsh.option.setNumber('Mesh.SurfaceFaces', 1)
gmsh.option.setNumber('Mesh.Points', 1)
gmsh.option.setNumber('Mesh.MeshSizeMin', mesh_size)
gmsh.option.setNumber('Mesh.MeshSizeMax', mesh_size)
gmsh.option.setNumber("Mesh.ElementOrder", 2)
gmsh.model.mesh.generate(2)
gmsh.option.setNumber("Mesh.Format", 31)
# Gmsh plotting
if show:
gmsh.fltk.run()
# Getting all the nodes
nodeTags, coord, parametricCoord = gmsh.model.mesh.getNodes(includeBoundary = True)
nNodes = int(max(nodeTags))
nodelisting = np.zeros([nNodes,2])
for i in range(len(nodeTags)):
index = int(nodeTags[i]-1)
nodelisting[index, 0] = coord[int(3*i)]
nodelisting[index, 1] = coord[int(3*i+2)]
filename = 'Polygon.bdf'
gmsh.write(filename)
# Finding the simplices from the written file
f = open(filename,'r')
rows = f.readlines()
simplices = np.array([[0,0,0,0,0,0]])
for i in range(len(rows)):
row = rows[i].split(' ')
row = [item for item in row if item != '']
if row[0] == 'CTRIA6':
new_simplice = np.array([int(row[3])-1,int(row[4])-1,int(row[5])-1,int(row[6])-1,int(row[7])-1,int(row[8])-1])
simplices = np.append(simplices,[new_simplice],axis=0)
simplices = np.delete(simplices,0,0)
f.close()
nodelisting, simplices = is_on_element(nodelisting, simplices)
gmsh.finalize()
os.remove(filename)
return nodelisting, simplices
def envelope_gmeshing(envelope_vertices, airfoil_vertices, mesh_size, n = 1, spacing = 0, show = False):
"""Meshing the airfoil using GMSH
Args:
envelope_vertices (list): Vertices of the envelope
airfoil_vertices (list): Vertices of the airfoil
mesh_size (float): Mesh size.
n (int, optional): Number of hydrofoils in cascade. Defaults to 1.
show (bool, optional): Bool to define if GMSH software is used to show the mesh. Defaults to False.
Returns:
list, list: List of nodes and elements
"""
gmsh.initialize()
gmsh.option.setNumber("General.Terminal", 1)
gmsh.model.add("modele_test")
npoints_profile = int(len(airfoil_vertices)/n)
npoints_envelope = len(envelope_vertices) - 1
npoints = len(envelope_vertices)+len(airfoil_vertices)-1
geom = gmsh.model.geo
for vertex_index in range(len(airfoil_vertices)): #Adding all the points of the profile
gmsh.model.geo.addPoint(airfoil_vertices[vertex_index,0],airfoil_vertices[vertex_index,1],
airfoil_vertices[vertex_index,2], mesh_size, vertex_index)
last_index = vertex_index
for vertex_index in range(len(envelope_vertices)): #Adding all the points of the envelope
gmsh.model.geo.addPoint(envelope_vertices[vertex_index,0],envelope_vertices[vertex_index,1],
envelope_vertices[vertex_index,2], mesh_size*8, vertex_index+last_index+1)
new_last_index = vertex_index+last_index+1
#Adding the overall spline and creating the surface
gmsh.model.geo.addLine(new_last_index, new_last_index-1, 0)
gmsh.model.geo.addLine(new_last_index-1, new_last_index-2, 1)
gmsh.model.geo.addLine(new_last_index-2, new_last_index-3, 2)
gmsh.model.geo.addLine(new_last_index-3, new_last_index, 3)
gmsh.model.geo.addCurveLoop([0, 1, 2, 3], tag=10+2*n)
# Adding the overall spline and creating the surface
small_envelope_vertices = np.array(([[1.5,0,-0.25 ],[1.5,0,0.25+ (n-1) * spacing],[-0.5,0,0.25+ (n-1) * spacing],[-0.5,0,-0.25 ]]))
for x in range(1,len(small_envelope_vertices)+1): #Adding all the points of the small envelope
gmsh.model.geo.addPoint(small_envelope_vertices[x-1,0],small_envelope_vertices[x-1,1], small_envelope_vertices[x-1,2], mesh_size*4, x + new_last_index )
gmsh.model.geo.addLine(new_last_index + 1, new_last_index + 2, 6 )
gmsh.model.geo.addLine(new_last_index + 2, new_last_index + 3, 7 )
gmsh.model.geo.addLine(new_last_index + 3, new_last_index + 4, 8 )
gmsh.model.geo.addLine(new_last_index + 4, new_last_index + 1, 9 )
gmsh.model.geo.addCurveLoop([6, 7, 8, 9], tag= 2 * n + 25)
wiretags = [2 * n + 10]
for i in range(n):
gmsh.model.geo.addSpline(np.linspace(i * npoints_profile, (i + 1) * npoints_profile - 1, npoints_profile), 6 * i+4)
gmsh.model.geo.addLine((i + 1) * npoints_profile - 1, i * npoints_profile, 6 * i + 5)
gmsh.model.geo.addCurveLoop([6 * i + 4, 6 * i + 5], tag=10 * i + 2 * n + 20)
wiretags.append(10 * i + 2 * n + 20)
gmsh.model.geo.addPlaneSurface(wireTags= wiretags,tag = 0)
gmsh.model.geo.synchronize()
gmsh.option.setNumber("Mesh.Smoothing", 10)
gmsh.option.setNumber('Mesh.SurfaceFaces', 1)
gmsh.option.setNumber('Mesh.Points', 1)
gmsh.option.setNumber("Mesh.ElementOrder", 2)
gmsh.model.mesh.generate(2)
gmsh.option.setNumber("Mesh.Format", 31)
# GMSH VISUALISATION (plot of the meshing)
if show:
gmsh.fltk.run()
#Getting all the nodes
nodeTags, coord, parametricCoord = gmsh.model.mesh.getNodes(includeBoundary = True)
nNodes = int(max(nodeTags))
nodelisting = np.zeros([nNodes,2])
for i in range(len(nodeTags)):
index = int(nodeTags[i]-1)
nodelisting[index, 0] = coord[int(3*i)]
nodelisting[index, 1] = coord[int(3*i+2)]
filename = 'Polygon.bdf'
gmsh.write(filename)
#Finding the simplices from the written file
f = open(filename,'r')
rows = f.readlines()
simplices = np.array([[0,0,0,0,0,0]])
for i in range(len(rows)):
row = rows[i].split(' ')
row = [item for item in row if item != '']
if row[0] == 'CTRIA6':
new_simplice = np.array([int(row[3])-1,int(row[4])-1,int(row[5])-1,int(row[6])-1,int(row[7])-1,int(row[8])-1])
simplices = np.append(simplices,[new_simplice],axis=0)
simplices = np.delete(simplices,0,0)
f.close()
nodelisting, simplices = is_on_element(nodelisting, simplices)
gmsh.finalize()
os.remove(filename)
return nodelisting, simplices
def is_on_element(nodelisting, simplices):
"""Determine if a node is used by an element, if not, delete it
Args:
nodelisting (nodes): List of nodes
simplices (list): List of nodes on elements
Returns:
list, list: List of existing nodes on 2D elements and simplices
"""
i = 0
d = len(nodelisting)
while i<d:
used = False
for j in range(len(simplices)):
if i == simplices[j,0] or i == simplices[j,1] or i == simplices[j,2] or i == simplices[j,3] \
or i == simplices[j,4] or i == simplices[j,5]:
used = True
break
if used == False:
nodelisting = np.delete(nodelisting, i, axis = 0)
for j in range(len(simplices)):
for k in range(6):
if simplices[j,k]>i:
simplices[j,k] = simplices[j,k] - 1
d = len(nodelisting)
i = i - 1
i = i + 1
return nodelisting, simplices