forked from dalcinl/meshio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.py
426 lines (380 loc) · 12.1 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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
import copy
import os
import string
import tempfile
from pathlib import Path
import numpy
import meshio
TEST_DIR = Path(__file__).resolve().parent
MESHES_DIR = TEST_DIR / "meshes"
# In general:
# Use values with an infinite decimal representation to test precision.
line_mesh = meshio.Mesh(
numpy.array([[0.0, 0.0, 0.0], [1.0, 0.0, 0.0], [1.0, 1.0, 0.0], [0.0, 1.0, 0.0]])
/ 3,
[("line", numpy.array([[0, 1], [0, 2], [0, 3], [1, 2], [2, 3]]))],
)
tri_mesh_2d = meshio.Mesh(
numpy.array([[0.0, 0.0], [1.0, 0.0], [1.0, 1.0], [0.0, 1.0]]) / 3,
[("triangle", numpy.array([[0, 1, 2], [0, 2, 3]]))],
)
tri_mesh = meshio.Mesh(
numpy.array([[0.0, 0.0, 0.0], [1.0, 0.0, 0.0], [1.0, 1.0, 0.0], [0.0, 1.0, 0.0]])
/ 3,
[("triangle", numpy.array([[0, 1, 2], [0, 2, 3]]))],
)
line_tri_mesh = meshio.Mesh(line_mesh.points, line_mesh.cells + tri_mesh.cells)
triangle6_mesh = meshio.Mesh(
numpy.array(
[
[0.0, 0.0, 0.0],
[1.0, 0.0, 0.0],
[1.0, 1.0, 0.0],
[0.5, 0.25, 0.0],
[1.25, 0.5, 0.0],
[0.25, 0.75, 0.0],
[2.0, 1.0, 0.0],
[1.5, 1.25, 0.0],
[1.75, 0.25, 0.0],
]
)
/ 3.0,
[("triangle6", numpy.array([[0, 1, 2, 3, 4, 5], [1, 6, 2, 8, 7, 4]]))],
)
quad_mesh = meshio.Mesh(
numpy.array(
[
[0.0, 0.0, 0.0],
[1.0, 0.0, 0.0],
[2.0, 0.0, 0.0],
[2.0, 1.0, 0.0],
[1.0, 1.0, 0.0],
[0.0, 1.0, 0.0],
]
)
/ 3.0,
[("quad", numpy.array([[0, 1, 4, 5], [1, 2, 3, 4]]))],
)
d = 0.1
quad8_mesh = meshio.Mesh(
numpy.array(
[
[0.0, 0.0, 0.0],
[1.0, 0.0, 0.0],
[1.0, 1.0, 0.0],
[0.0, 1.0, 0.0],
[0.5, d, 0.0],
[1 - d, 0.5, 0.0],
[0.5, 1 - d, 0.0],
[d, 0.5, 0.0],
[2.0, 0.0, 0.0],
[2.0, 1.0, 0.0],
[1.5, -d, 0.0],
[2 + d, 0.5, 0.0],
[1.5, 1 + d, 0.0],
]
)
/ 3.0,
[("quad8", numpy.array([[0, 1, 2, 3, 4, 5, 6, 7], [1, 8, 9, 2, 10, 11, 12, 5]]))],
)
tri_quad_mesh = meshio.Mesh(
numpy.array(
[
[0.0, 0.0, 0.0],
[1.0, 0.0, 0.0],
[2.0, 0.0, 0.0],
[2.0, 1.0, 0.0],
[1.0, 1.0, 0.0],
[0.0, 1.0, 0.0],
]
)
/ 3.0,
[
("triangle", numpy.array([[0, 1, 4], [0, 4, 5]])),
("quad", numpy.array([[1, 2, 3, 4]])),
],
)
# same as tri_quad_mesh with reversed cell type order
quad_tri_mesh = meshio.Mesh(
numpy.array(
[
[0.0, 0.0, 0.0],
[1.0, 0.0, 0.0],
[2.0, 0.0, 0.0],
[2.0, 1.0, 0.0],
[1.0, 1.0, 0.0],
[0.0, 1.0, 0.0],
]
)
/ 3.0,
[
("quad", numpy.array([[1, 2, 3, 4]])),
("triangle", numpy.array([[0, 1, 4], [0, 4, 5]])),
],
)
tet_mesh = meshio.Mesh(
numpy.array(
[
[0.0, 0.0, 0.0],
[1.0, 0.0, 0.0],
[1.0, 1.0, 0.0],
[0.0, 1.0, 0.0],
[0.5, 0.5, 0.5],
]
)
/ 3.0,
[("tetra", numpy.array([[0, 1, 2, 4], [0, 2, 3, 4]]))],
)
tet10_mesh = meshio.Mesh(
numpy.array(
[
[0.0, 0.0, 0.0],
[1.0, 0.0, 0.0],
[1.0, 1.0, 0.0],
[0.5, 0.5, 0.5],
#
[0.5, 0.0, 0.1],
[1.0, 0.5, 0.1],
[0.5, 0.5, 0.1],
[0.25, 0.3, 0.25],
[0.8, 0.25, 0.25],
[0.7, 0.7, 0.3],
]
)
/ 3.0,
[("tetra10", numpy.array([[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]]))],
)
hex_mesh = meshio.Mesh(
numpy.array(
[
[0.0, 0.0, 0.0],
[1.0, 0.0, 0.0],
[1.0, 1.0, 0.0],
[0.0, 1.0, 0.0],
[0.0, 0.0, 1.0],
[1.0, 0.0, 1.0],
[1.0, 1.0, 1.0],
[0.0, 1.0, 1.0],
]
),
[("hexahedron", numpy.array([[0, 1, 2, 3, 4, 5, 6, 7]]))],
)
hex20_mesh = meshio.Mesh(
numpy.array(
[
[0.0, 0.0, 0.0],
[1.0, 0.0, 0.0],
[1.0, 1.0, 0.0],
[0.0, 1.0, 0.0],
[0.0, 0.0, 1.0],
[1.0, 0.0, 1.0],
[1.0, 1.0, 1.0],
[0.0, 1.0, 1.0],
#
[0.5, 0.0, 0.0],
[1.0, 0.5, 0.0],
[0.5, 1.0, 0.0],
[0.0, 0.5, 0.0],
#
[0.0, 0.0, 0.5],
[1.0, 0.0, 0.5],
[1.0, 1.0, 0.5],
[0.0, 1.0, 0.5],
#
[0.5, 0.0, 1.0],
[1.0, 0.5, 1.0],
[0.5, 1.0, 1.0],
[0.0, 0.5, 1.0],
]
),
[("hexahedron20", numpy.array([numpy.arange(20)]))],
)
polygon_mesh = meshio.Mesh(
numpy.array(
[
[0.0, 0.0, 0.0],
[1.0, 0.0, 0.0],
[1.0, 1.0, 0.0],
[0.0, 1.0, 0.0],
[1.5, 0.0, 0.0],
[1.7, 0.5, 0.0],
[1.5, 1.2, 0.0],
[-0.1, 1.1, 0.0],
[-0.5, 1.4, 0.0],
[-0.7, 0.8, 0.0],
[-0.3, -0.1, 0.0],
]
),
[
("triangle", numpy.array([[0, 1, 2], [4, 5, 6]])),
("quad", numpy.array([[0, 1, 2, 3]])),
("polygon5", numpy.array([[1, 4, 5, 6, 2]])),
("polygon6", numpy.array([[0, 3, 7, 8, 9, 10], [1, 3, 7, 8, 9, 10]])),
],
)
polyhedron_mesh = meshio.Mesh(
numpy.array(
[ # Two layers of a unit square
[0.0, 0.0, 0.0],
[1.0, 0.0, 0.0],
[1.0, 1.0, 0.0],
[0.0, 1.0, 0.0],
[0.0, 0.0, 1.0],
[1.0, 0.0, 1.0],
[1.0, 1.0, 1.0],
[0.0, 1.0, 1.0],
]
),
[ # Split the cube into tets and pyramids.
(
"polyhedron4",
[
[
numpy.array([1, 2, 5]),
numpy.array([1, 2, 7]),
numpy.array([1, 5, 7]),
numpy.array([2, 5, 7]),
],
[
numpy.array([2, 5, 6]),
numpy.array([2, 6, 7]),
numpy.array([2, 5, 7]),
numpy.array([5, 6, 7]),
],
],
),
(
"polyhedron5",
[
[
numpy.array([0, 1, 2, 3]), # pyramid base is a rectangle
numpy.array([0, 1, 7]),
numpy.array([1, 2, 7]),
numpy.array([2, 3, 7]),
numpy.array([3, 0, 7]),
],
[
numpy.array([0, 1, 5]), # pyramid base split in two triangles
numpy.array([0, 4, 5]),
numpy.array([0, 1, 7]),
numpy.array([1, 5, 7]),
numpy.array([5, 4, 7]),
numpy.array([0, 4, 7]),
],
],
),
],
)
def add_point_data(mesh, dim, num_tags=2, seed=0, dtype=numpy.float):
numpy.random.seed(seed)
mesh2 = copy.deepcopy(mesh)
shape = (len(mesh.points),) if dim == 1 else (len(mesh.points), dim)
data = [(100 * numpy.random.rand(*shape)).astype(dtype) for _ in range(num_tags)]
mesh2.point_data = {string.ascii_lowercase[k]: d for k, d in enumerate(data)}
return mesh2
def add_cell_data(mesh, specs):
mesh2 = copy.deepcopy(mesh)
numpy.random.seed(0)
mesh2.cell_data = {
name: [
(100 * numpy.random.rand(*((len(cells),) + shape))).astype(dtype)
for _, cells in mesh.cells
]
for name, shape, dtype in specs
}
# Keep cell-data from the original mesh. This is needed to preserve
# face-cell relations for polyhedral meshes.
for key, val in mesh.cell_data.items():
mesh2.cell_data[key] = val
return mesh2
def add_field_data(mesh, value, dtype):
mesh2 = copy.deepcopy(mesh)
mesh2.field_data = {"a": numpy.array(value, dtype=dtype)}
return mesh2
def add_point_sets(mesh):
mesh2 = copy.deepcopy(mesh)
mesh2.point_sets = {"fixed": numpy.array([1, 2])}
return mesh2
def add_cell_sets(mesh):
mesh2 = copy.deepcopy(mesh)
assert len(mesh.cells) == 1
n = len(mesh.cells[0])
mesh2.cell_sets = {
"grain0": [numpy.array([0])],
"grain1": [numpy.arange(1, n)],
}
return mesh2
def write_read(writer, reader, input_mesh, atol, extension=".dat"):
"""Write and read a file, and make sure the data is the same as before."""
in_mesh = copy.deepcopy(input_mesh)
with tempfile.TemporaryDirectory() as temp_dir:
filepath = os.path.join(temp_dir, "test" + extension)
p = Path(filepath)
writer(p, input_mesh)
mesh = reader(p)
# Make sure the output is writeable
assert mesh.points.flags["WRITEABLE"]
for cells in input_mesh.cells:
if isinstance(cells.data, numpy.ndarray):
assert cells.data.flags["WRITEABLE"]
else:
# This is assumed to be a polyhedron
for cell in cells.data:
for face in cell:
assert face.flags["WRITEABLE"]
# assert that the input mesh hasn't changed at all
assert numpy.allclose(in_mesh.points, input_mesh.points, atol=atol, rtol=0.0)
# Numpy's array_equal is too strict here, cf.
# <https://mail.scipy.org/pipermail/numpy-discussion/2015-December/074410.html>.
# Use allclose.
n = in_mesh.points.shape[1]
assert numpy.allclose(in_mesh.points, mesh.points[:, :n], atol=atol, rtol=0.0)
# To avoid errors from sorted (below), specify the key as first cell type
# then index of the first point of the first cell. This may still lead to
# comparison of what should be different blocks, but chances seem low.
def cell_sorter(cell):
if cell.type[:10] == "polyhedron":
# Polyhedra blocks should be well enough distinguished by their type
return cell.type
else:
return (cell.type, cell.data[0, 0])
# to make sure we are testing same type of cells we sort the list
for cells0, cells1 in zip(
sorted(input_mesh.cells, key=cell_sorter), sorted(mesh.cells, key=cell_sorter)
):
assert cells0.type == cells1.type, f"{cells0.type} != {cells1.type}"
if cells0.type[:10] == "polyhedron":
# Special treatment of polyhedron cells
# Data is a list (one item per cell) of numpy arrays
for c_in, c_out in zip(cells0.data, cells1.data):
for face_in, face_out in zip(c_in, c_out):
assert numpy.allclose(face_in, face_out, atol=atol, rtol=0.0)
else:
assert numpy.array_equal(cells0.data, cells1.data)
for key in input_mesh.point_data.keys():
assert numpy.allclose(
input_mesh.point_data[key], mesh.point_data[key], atol=atol, rtol=0.0
)
for name, cell_type_data in input_mesh.cell_data.items():
for d0, d1 in zip(cell_type_data, mesh.cell_data[name]):
# assert d0.dtype == d1.dtype, (d0.dtype, d1.dtype)
assert numpy.allclose(d0, d1, atol=atol, rtol=0.0)
for name, data in input_mesh.field_data.items():
assert numpy.allclose(data, mesh.field_data[name], atol=atol, rtol=0.0)
# Test of cell sets (assumed to be a list of numpy arrays),
for name, data in input_mesh.cell_sets.items():
# Skip the test if the key is not in the read cell set
if name not in mesh.cell_sets.keys():
continue
data2 = mesh.cell_sets[name]
for var1, var2 in zip(data, data2):
assert numpy.allclose(var1, var2, atol=atol, rtol=0.0)
def generic_io(filename):
with tempfile.TemporaryDirectory() as temp_dir:
filepath = os.path.join(temp_dir, filename)
meshio.write_points_cells(filepath, tri_mesh.points, tri_mesh.cells)
out_mesh = meshio.read(filepath)
assert (abs(out_mesh.points - tri_mesh.points) < 1.0e-15).all()
for c0, c1 in zip(tri_mesh.cells, out_mesh.cells):
assert c0.type == c1.type
assert (c0.data == c1.data).all()