-
Notifications
You must be signed in to change notification settings - Fork 77
Expand file tree
/
Copy pathpythonmesh.py
More file actions
131 lines (122 loc) · 4.28 KB
/
pythonmesh.py
File metadata and controls
131 lines (122 loc) · 4.28 KB
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
def parse_gmsh_file(file):
sections = set(["$MeshFormat", "$PhysicalNames", "$Nodes", "$Elements"])
lineno = 0
section_count = 0
section_expected = 0
dimension = 0
with open(file, "r") as ih:
state = "begin"
for line in ih:
line = line.rstrip()
lineno += 1
if state == "begin":
if line in sections:
state = line
else:
raise RuntimeError("Error Line %d" % lineno)
elif line.find("$End") == 0:
state = "begin"
section_count = 0
section_expected = None
elif state == "$MeshFormat":
continue
elif state == "$PhysicalNames":
if not section_expected:
section_expected = int(line)
physical_names = []
else:
line = line.split()
name = line[2][1:-1]
physical_names.append((name, int(line[0]), int(line[1])))
section_count += 1
elif state == "$Nodes":
if not section_expected:
section_expected = int(line)
nodes = []
else:
line = line.split()
nodes.append(
[int(line[0]), float(line[1]), float(line[2]), float(line[3])]
)
section_count += 1
elif state == "$Elements":
if not section_expected:
section_expected = int(line)
elements = [None] * section_expected
else:
elements[section_count] = [int(x) for x in line.split()]
section_count += 1
else:
raise RuntimeError("Unknown %s" % line)
dimension = max([x[1] for x in physical_names])
return {
"dimension": dimension,
"physical_names": physical_names,
"coordinates": nodes,
"elements": elements,
}
def read_gmsh_file(filename):
"""reads gmsh file and converts to python representation"""
data = parse_gmsh_file(filename)
# namemap = {}
print("%d dimension" % data["dimension"])
print("%d coordinates" % len(data["coordinates"]))
# for i in sorted(data['physical_names'].keys()):
# print "%s %s %d" % (i, data['physical_names'][i], len(data['elements'][i]))
# namemap[data['physical_names'][i]] = i
coordinate_indexes = [x[0] for x in data["coordinates"]]
max_coordinate_index = max(coordinate_indexes)
coordinate_to_index = [-1] * (max_coordinate_index + 1)
for i, j in enumerate(coordinate_indexes):
coordinate_to_index[j] = i
coordinates = []
for i in data["coordinates"]:
coordinates.extend(i[1:])
_all_physical_numbers = sorted(set([x[3] for x in data["elements"]]))
sorted_physical_names = sorted(data["physical_names"], key=lambda x: x[0])
physical_number_map = {
1: {},
2: {},
3: {},
}
for i, j in enumerate(sorted_physical_names):
# mapped dimension, physical number, new index
physical_number_map[j[1]][j[2]] = i
physical_names = [x[0] for x in sorted_physical_names]
# gmsh format
# elm-number elm-type number-of-tags < tag > ... node-number-list
# our format
elements = []
for i in data["elements"]:
t = i[1]
if t == 4:
# tetrahedron
etype = 3
dimension = 3
elif t == 2:
# triangle
etype = 2
dimension = 2
elif t == 1:
# edge
etype = 1
dimension = 1
elif t == 15:
# point
etype = 0
else:
raise RuntimeError("Cannot handle element type " % i)
# physical number
# will be remapped later
pnum = physical_number_map[dimension][i[3]]
# nodes
# position 2 + number of tags
nodes = i[i[2] + 3 :]
nodes = [coordinate_to_index[x] for x in nodes]
elements.extend([etype, pnum])
elements.extend(nodes)
return {
"physical_names": physical_names,
"coordinates": coordinates,
"elements": elements,
}