-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgmsh2triangle
executable file
·210 lines (169 loc) · 5.89 KB
/
gmsh2triangle
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
#!/usr/bin/env python
from optparse import OptionParser
import re
import sys
import os.path
#####################################################################
# Script starts here.
optparser=OptionParser(usage='usage: %prog [options] <filename>',
add_help_option=True,
description="""This takes a Gmsh 2.0 .msh ascii file """ +
"""and produces .node, .ele and .edge or .face files.""")
optparser.add_option("--2D", "--2d", "-2",
help="discard 3rd coordinate of node positions",
action="store_const", const=2, dest="dim", default=3)
optparser.add_option("--internal-boundary", "-i",
help="mesh contains internal faces - this option is required if you have assigned " +
"a physical boundary id to lines (2D) or surfaces (3D) that are not on the domain boundary",
action="store_const", const=True, dest="internal_faces", default=False)
(options, argv) = optparser.parse_args()
if len(argv)<1:
optparser.print_help()
sys.exit(1)
if argv[0][-4:]!=".msh":
sys.stderr.write("Mesh filename must end in .msh\n")
optparser.print_help()
sys.exit(1)
basename=argv[0][:-4]
mshfile=file(argv[0], 'r')
# Header section
assert(mshfile.readline().strip()=="$MeshFormat")
assert(mshfile.readline().strip()in["2 0 8", "2.1 0 8", "2.2 0 8"])
assert(mshfile.readline().strip()=="$EndMeshFormat")
# Nodes section
while mshfile.readline().strip() !="$Nodes":
pass
nodecount=int(mshfile.readline())
if nodecount==0:
sys.stderr.write("ERROR: No nodes found in mesh.\n")
sys.exit(1)
if nodecount<0:
sys.stderr.write("ERROR: Negative number of nodes found in mesh.\n")
sys.exit(1)
dim=options.dim
nodefile_linelist = []
for i in range(nodecount):
# Node syntax
line = mshfile.readline().split()
# compare node id assigned by gmsh to consecutive node id (assumed by fluidity)
if eval(line[0])!=i+1:
print line[0], i+1
sys.stderr.write("ERROR: Nodes in gmsh .msh file must be numbered consecutively.")
nodefile_linelist.append( line[1:dim+1] )
assert(mshfile.readline().strip()=="$EndNodes")
# Elements section
assert(mshfile.readline().strip()=="$Elements")
elementcount=int(mshfile.readline())
# Now loop over the elements placing them in the appropriate buckets.
edges=[]
triangles=[]
tets=[]
quads=[]
hexes=[]
for i in range(elementcount):
element=mshfile.readline().split()
if (element[1]=="1"):
edges.append(element[-2:]+[element[3]])
elif (element[1]=="2"):
triangles.append(element[-3:]+[element[3]])
elif (element[1]=="3"):
quads.append(element[-4:]+[element[3]])
elif (element[1]=="4"):
tets.append(element[-4:]+[element[3]])
elif (element[1]=="5"):
hexes.append(element[-8:]+[element[3]])
elif(element[1]=="15"):
# Ignore point elements
pass
else:
sys.stderr.write("Unknown element type "+`element[1]`+'\n')
sys.exit(1)
if len(tets) > 0:
if len(hexes) > 0:
sys.stderr.write("Warning: Mixed tet/hex mesh encountered - discarding hexes")
if len(quads) > 0:
sys.stderr.write("Warning: Mixed tet/quad mesh encountered - discarding quads")
elif len(triangles) > 0:
if len(hexes) > 0:
sys.stderr.write("Warning: Mixed triangle/hex mesh encountered - discarding hexes")
if len(quads) > 0:
sys.stderr.write("Warning: Mixed triangle/quad mesh encountered - discarding quads")
if len(tets)>0:
dim=3
loc=4
node_order=[1, 2, 3, 4]
elements=tets
faces=triangles
elefile=file(basename+".ele", "w")
facefile=file(basename+".face", "w")
elif len(triangles)>0:
dim=2
loc=3
node_order=[1, 2, 3]
elements=triangles
faces=edges
elefile=file(basename+".ele", "w")
facefile=file(basename+".edge", "w")
elif len(hexes)>0:
dim=3
loc=8
node_order=[1, 2, 4, 3, 5, 6, 8, 7]
elements=hexes
faces=quads
elefile=file(basename+".ele", "w")
facefile=file(basename+".face", "w")
elif len(quads)>0:
dim=2
loc=4
node_order=[1, 2, 4, 3] # don't really know if this is right
elements=quads
faces=edges
elefile=file(basename+".ele", "w")
facefile=file(basename+".edge", "w")
else:
sys.stderr.write("Unable to determine dimension of problem\n")
sys.exit(1)
nodefile=file(basename+".node", 'w')
nodefile.write(`nodecount`+" "+`options.dim`+" 0 0\n")
j=0
for i in range(nodecount):
j=j+1
nodefile.write(" ".join( [str(j)] + nodefile_linelist[i] )+"\n")
nodefile.write("# Produced by: "+" ".join(argv)+"\n")
nodefile.close()
# Output ele file
elefile.write(`len(elements)`+" "+`loc`+" 1\n")
for i, element in enumerate(elements):
elefile.write(`i+1`+" ")
for j in node_order:
elefile.write(" ".join(element[j-1:j])+" ")
elefile.write(" ".join(element[-1:]))
elefile.write(" "+"\n")
elefile.write("# Produced by: "+" ".join(sys.argv)+"\n")
elefile.close()
# Output ele or face file
if options.internal_faces:
# make node element list
ne_list = [set() for i in range(nodecount)]
for i, element in enumerate(elements):
element=[eval(element[j-1]) for j in node_order]
for node in element:
ne_list[node-1].add(i)
# make face list, containing: face_nodes, surface_id, element_owner
facelist=[]
for face in faces:
# last entry of face is surface-id
face_nodes=[eval(node) for node in face[:-1]]
# loop through elements around node face_nodes[0]
for ele in ne_list[face_nodes[0]-1]:
element=[eval(elements[ele][j-1]) for j in node_order]
if set(face_nodes) < set(element):
facelist.append(face+[`ele+1`])
facefile.write(`len(facelist)`+" 2\n")
faces=facelist
else:
facefile.write(`len(faces)`+" 1\n")
for i,face in enumerate(faces):
facefile.write(`i+1`+" "+" ".join(face)+"\n")
facefile.write("# Produced by: "+" ".join(sys.argv)+"\n")
facefile.close()