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
|
from __future__ import annotations
import pathlib
import platform
from requests.exceptions import HTTPError
import meshio
import numpy as np
import pytest
import pyvista as pv
from pyvista import examples
# test for i386/i486/i586/i686, which fails HTTP requests
http_xfail = platform.machine()[0]+platform.machine()[2:] == 'i86'
try:
cow = examples.download_cow().cast_to_unstructured_grid()
beam = pv.UnstructuredGrid(examples.hexbeamfile)
airplane = examples.load_airplane().cast_to_unstructured_grid()
uniform = examples.load_uniform().cast_to_unstructured_grid()
mesh2d = meshio.Mesh(
points=[[0.0, 0.0], [1.0, 0.0], [0.0, 1.0], [1.0, 1.0]],
cells=[("triangle", [[0, 1, 2], [1, 3, 2]])],
)
except HTTPError:
# expect HTTP requests from i386 to get refused
# but reraise the exception on other systems
if not http_xfail:
raise
cow = beam = airplane = uniform = mesh2d = None
polyhedron = meshio.Mesh(
points=[
[0.3568221, -0.49112344, 0.79465446],
[-0.3568221, -0.49112344, 0.79465446],
[0.3568221, 0.49112344, -0.79465446],
[-0.3568221, 0.49112344, -0.79465446],
[0.0, 0.98224693, 0.18759243],
[0.0, 0.60706198, 0.79465446],
[0.0, -0.60706198, -0.79465446],
[0.0, -0.98224693, -0.18759243],
[0.93417233, 0.30353101, 0.18759247],
[0.93417233, -0.30353101, -0.18759247],
[-0.93417233, 0.30353101, 0.18759247],
[-0.93417233, -0.30353101, -0.18759247],
[-0.57735026, 0.18759249, 0.79465446],
[0.57735026, -0.79465446, 0.18759249],
[-0.57735026, -0.18759249, -0.79465446],
[0.57735026, 0.79465446, -0.18759249],
[0.57735026, 0.18759249, 0.79465446],
[-0.57735026, 0.79465446, -0.18759249],
[-0.57735026, -0.79465446, 0.18759249],
[0.57735026, -0.18759249, -0.79465446],
[0.3568221, 0.49112344, -1.0],
[0.57735026, -0.18759249, -1.0],
[0.0, -0.60706198, -1.0],
[-0.57735026, -0.18759249, -1.0],
[-0.3568221, 0.49112344, -1.0],
[0.3568221, -0.49112344, 1.0],
[0.57735026, 0.18759249, 1.0],
[0.0, 0.60706198, 1.0],
[-0.57735026, 0.18759249, 1.0],
[-0.3568221, -0.49112344, 1.0],
],
cells=[
(
"polyhedron20",
[
[
[0, 16, 5, 12, 1],
[1, 18, 7, 13, 0],
[2, 19, 6, 14, 3],
[3, 17, 4, 15, 2],
[4, 5, 16, 8, 15],
[5, 4, 17, 10, 12],
[6, 7, 18, 11, 14],
[7, 6, 19, 9, 13],
[8, 16, 0, 13, 9],
[9, 19, 2, 15, 8],
[10, 17, 3, 14, 11],
[11, 18, 1, 12, 10],
],
],
),
(
"polyhedron10",
[
[
[2, 19, 6, 14, 3],
[20, 21, 19, 2],
[21, 22, 6, 19],
[22, 23, 14, 6],
[23, 24, 3, 14],
[24, 20, 2, 3],
[20, 21, 22, 23, 24],
],
[
[0, 16, 5, 12, 1],
[0, 16, 26, 25],
[16, 5, 27, 26],
[5, 12, 28, 27],
[12, 1, 29, 28],
[1, 0, 25, 29],
[25, 26, 27, 28, 29],
],
],
),
],
)
@pytest.mark.parametrize("mesh_in", [beam, airplane, uniform, mesh2d, polyhedron, cow])
@pytest.mark.xfail(http_xfail, reason=f"test HTTP requests are refused on {platform.machine()}")
def test_meshio(mesh_in, tmpdir):
if isinstance(mesh_in, meshio.Mesh):
mesh_in = pv.from_meshio(mesh_in)
# Save and read reference mesh using meshio
filename = tmpdir.mkdir("tmpdir").join("test_mesh.vtu")
pv.save_meshio(filename, mesh_in)
mesh = pv.read_meshio(filename)
# Assert mesh is still the same
assert np.allclose(mesh_in.points, mesh.points)
if (mesh_in.celltypes == 11).all():
cells = mesh_in.cells.reshape((mesh_in.n_cells, 9))[:, [0, 1, 2, 4, 3, 5, 6, 8, 7]].ravel()
assert np.allclose(cells, mesh.cells)
else:
assert np.allclose(mesh_in.cells, mesh.cells)
for k, v in mesh_in.point_data.items():
assert np.allclose(v, mesh.point_data[k.replace(" ", "_")])
for k, v in mesh_in.cell_data.items():
assert np.allclose(v, mesh.cell_data[k.replace(" ", "_")])
def test_pathlib_read_write(tmpdir, sphere):
path = pathlib.Path(str(tmpdir.mkdir("tmpdir").join('tmp.vtk')))
pv.save_meshio(path, sphere)
assert path.is_file()
mesh = pv.read_meshio(path)
assert isinstance(mesh, pv.UnstructuredGrid)
assert mesh.points.shape == sphere.points.shape
def test_file_format():
from meshio._exceptions import ReadError
from meshio._exceptions import WriteError
with pytest.raises(ReadError):
_ = pv.read_meshio(examples.hexbeamfile, file_format="bar")
if beam:
with pytest.raises((KeyError, WriteError)):
pv.save_meshio("foo.bar", beam, file_format="bar")
with pytest.raises((KeyError, WriteError)):
pv.save_meshio("foo.npy", beam, file_format="npy")
|