forked from nschloe/meshio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.py
269 lines (249 loc) · 6.99 KB
/
helpers.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
# -*- coding: utf-8 -*-
#
import pathlib
from . import ansys_io
from . import dolfin_io
from . import exodus_io
from . import h5m_io
from . import med_io
from . import medit_io
from . import gmsh_io
from . import off_io
from . import permas_io
from . import stl_io
from . import vtk_io
from . import vtu_io
from . import xdmf_io
input_filetypes = [
'ansys',
'exodus',
'gmsh-ascii',
'gmsh-binary',
'dolfin-xml',
'med',
'medit',
'permas',
'moab',
'off',
'stl-ascii',
'stl-binary',
'vtk-ascii',
'vtk-binary',
'vtu-ascii',
'vtu-binary',
'xdmf',
]
output_filetypes = [
'ansys-ascii',
'ansys-binary',
'exodus',
'gmsh-ascii',
'gmsh-binary',
'dolfin-xml',
'med',
'medit',
'permas',
'moab',
'off',
'stl-ascii',
'stl-binary',
'vtk-ascii',
'vtk-binary',
'vtu-ascii',
'vtu-binary',
'xdmf',
]
_extension_to_filetype = {
'.e': 'exodus',
'.ex2': 'exodus',
'.exo': 'exodus',
'.med': 'med',
'.mesh': 'medit',
'.msh': 'gmsh-binary',
'.xml': 'dolfin-xml',
'.post': 'permas',
'.post.gz': 'permas',
'.dato': 'permas',
'.dato.gz': 'permas',
'.h5m': 'moab',
'.off': 'off',
'.stl': 'stl-binary',
'.vtu': 'vtu-binary',
'.vtk': 'vtk-binary',
'.xdmf': 'xdmf',
'.xmf': 'xdmf',
}
def _filetype_from_filename(filename):
suffixes = pathlib.PurePath(filename).suffixes
ext = ''
for i in reversed(range(len(suffixes))):
ext = suffixes[i] + ext
if ext in _extension_to_filetype:
return _extension_to_filetype[ext]
raise RuntimeError('Could not deduce file format from extension \'{}\'.'
.format(ext))
def read(filename, file_format=None):
'''Reads an unstructured mesh with added data.
:param filenames: The files to read from.
:type filenames: str
:returns mesh{2,3}d: The mesh data.
:returns point_data: Point data read from file.
:type point_data: dict
:returns field_data: Field data read from file.
:type field_data: dict
'''
# https://stackoverflow.com/q/4843173/353337
assert isinstance(filename, str)
if not file_format:
# deduce file format from extension
file_format = _filetype_from_filename(filename)
format_to_reader = {
'ansys': ansys_io,
'ansys-ascii': ansys_io,
'ansys-binary': ansys_io,
#
'gmsh': gmsh_io,
'gmsh-ascii': gmsh_io,
'gmsh-binary': gmsh_io,
#
'med': med_io,
'medit': medit_io,
'dolfin-xml': dolfin_io,
'permas': permas_io,
'moab': h5m_io,
'off': off_io,
#
'stl': stl_io,
'stl-ascii': stl_io,
'stl-binary': stl_io,
#
'vtu-ascii': vtu_io,
'vtu-binary': vtu_io,
#
'vtk-ascii': vtk_io,
'vtk-binary': vtk_io,
#
'xdmf': xdmf_io,
'exodus': exodus_io,
}
assert file_format in format_to_reader, \
'Unknown file format \'{}\' of \'{}\'.'.format(file_format, filename)
return format_to_reader[file_format].read(filename)
def write(filename,
points,
cells,
point_data=None,
cell_data=None,
field_data=None,
file_format=None):
'''Writes mesh together with data to a file.
:params filename: File to write to.
:type filename: str
:params point_data: Named additional point data to write to the file.
:type point_data: dict
'''
point_data = {} if point_data is None else point_data
cell_data = {} if cell_data is None else cell_data
field_data = {} if field_data is None else field_data
if not file_format:
# deduce file format from extension
file_format = _filetype_from_filename(filename)
# check cells for sanity
for key in cells:
assert cells[key].shape[1] == gmsh_io.num_nodes_per_cell[key]
if file_format == 'moab':
h5m_io.write(
filename, points, cells,
point_data=point_data,
cell_data=cell_data,
field_data=field_data
)
elif file_format in ['ansys-ascii', 'ansys-binary']:
ansys_io.write(
filename, points, cells,
point_data=point_data,
cell_data=cell_data,
write_binary=(file_format == 'ansys-binary')
)
elif file_format in ['gmsh-ascii', 'gmsh-binary']:
gmsh_io.write(
filename, points, cells,
point_data=point_data,
cell_data=cell_data,
field_data=field_data,
write_binary=(file_format == 'gmsh-binary')
)
elif file_format == 'med':
med_io.write(
filename, points, cells,
point_data=point_data,
cell_data=cell_data)
elif file_format == 'medit':
medit_io.write(filename, points, cells)
elif file_format == 'dolfin-xml':
dolfin_io.write(filename, points, cells, cell_data=cell_data)
elif file_format == 'off':
off_io.write(filename, points, cells)
elif file_format == 'permas':
permas_io.write(filename, points, cells)
elif file_format in ['stl-ascii', 'stl-binary']:
stl_io.write(
filename, points, cells,
point_data=point_data,
cell_data=cell_data,
field_data=field_data,
write_binary=(file_format != 'stl-ascii')
)
elif file_format == 'vtu-ascii':
vtu_io.write(
filename, points, cells,
point_data=point_data,
cell_data=cell_data,
field_data=field_data,
write_binary=False
)
elif file_format in ['vtu', 'vtu-binary']:
vtu_io.write(
filename, points, cells,
point_data=point_data,
cell_data=cell_data,
field_data=field_data,
write_binary=True
)
elif file_format == 'vtk-ascii':
vtk_io.write(
filename,
points, cells,
point_data=point_data,
cell_data=cell_data,
field_data=field_data,
write_binary=False
)
elif file_format in ['vtk', 'vtk-binary']:
vtk_io.write(
filename,
points, cells,
point_data=point_data,
cell_data=cell_data,
field_data=field_data,
write_binary=True
)
elif file_format in ['xdmf', 'xdmf3']: # XDMF
xdmf_io.write(
filename, points, cells,
point_data=point_data,
cell_data=cell_data,
field_data=field_data
)
else:
assert file_format == 'exodus', (
'Unknown file format \'{}\' of \'{}\'.'
.format(file_format, filename)
)
exodus_io.write(
filename, points, cells,
point_data=point_data,
cell_data=cell_data,
field_data=field_data
)
return