多种网格格式的 I/O
项目描述
网格文件的 I/O。
有多种网格格式可用于表示非结构化网格。meshio 可以读取和写入以下所有内容并在它们之间平滑转换:
Abaqus (
.inp
)、ANSYS msh (.msh
)、 AVS-UCD (.avs
)、 CGNS (.cgns
)、 DOLFIN XML (.xml
)、 Exodus (.e
,.exo
)、 FLAC3D (.f3grid
)、 H5M (.h5m
)、 Kratos/MDPA (.mdpa
)、 Medit (.mesh
,.meshb
)、 MED/ Salome (.med
)、 Nastran(批量数据.bdf
、、、、)、.fem
Netgen (、) 、 Neuroglancer 预计算格式、 Gmsh(格式版本 2.2、4.0 和 4.1,.nas
.vol
.vol.gz
.msh
), OBJ (.obj
), OFF (.off
), PERMAS (.post
,.post.gz
,.dato
,.dato.gz
), PLY (.ply
), STL (.stl
), Tecplot .dat , TetGen .node/.ele , SVG (仅二维输出) (.svg
), SU2 (.su2
), UGRID (.ugrid
)、 VTK (.vtk
)、 VTU (.vtu
)、 WKT ( TIN ) (.wkt
)、 XDMF (.xdmf
,.xmf
)。
(这里有一个关于实际使用哪些格式的小调查。)
安装其中之一
pip install meshio[all]
conda install -c conda-forge meshio
([all]
拉入所有可选依赖项。默认情况下,meshio 仅使用 numpy。)然后您可以使用命令行工具
meshio convert input.msh output.vtk # convert between two formats
meshio info input.xdmf # show some info about the mesh
meshio compress input.vtu # compress the mesh file
meshio decompress input.vtu # decompress the mesh file
meshio binary input.msh # convert to binary format
meshio ascii input.msh # convert to ASCII format
任何支持的格式。
在 Python 中,只需执行
import meshio
mesh = meshio.read(
filename, # string, os.PathLike, or a buffer/open file
# file_format="stl", # optional if filename is a path; inferred from extension
# see meshio-convert -h for all possible formats
)
# mesh.points, mesh.cells, mesh.cells_dict, ...
# mesh.vtk.read() is also possible
读取网格。写,做
import meshio
# two triangles and one quad
points = [
[0.0, 0.0],
[1.0, 0.0],
[0.0, 1.0],
[1.0, 1.0],
[2.0, 0.0],
[2.0, 1.0],
]
cells = [
("triangle", [[0, 1, 2], [1, 3, 2]]),
("quad", [[1, 4, 5, 3]]),
]
mesh = meshio.Mesh(
points,
cells,
# Optionally provide extra data on points, cells, etc.
point_data={"T": [0.3, -1.2, 0.5, 0.7, 0.0, -3.0]},
# Each item in cell data must match the cells array
cell_data={"a": [[0.1, 0.2], [0.4]]},
)
mesh.write(
"foo.vtk", # str, os.PathLike, or buffer/open file
# file_format="vtk", # optional if first argument is a path; inferred from extension
)
# Alternative with the same options
meshio.write_points_cells("foo.vtk", points, cells)
对于输入和输出,您可以选择指定确切的file_format
值(例如,如果您想在二进制 VTK 上强制执行 ASCII)。
时间序列
XDMF 格式支持具有共享网格的时间序列。您可以使用 meshio 编写时间序列数据
with meshio.xdmf.TimeSeriesWriter(filename) as writer:
writer.write_points_cells(points, cells)
for t in [0.0, 0.1, 0.21]:
writer.write_data(t, point_data={"phi": data})
并阅读它
with meshio.xdmf.TimeSeriesReader(filename) as reader:
points, cells = reader.read_points_cells()
for k in range(reader.num_steps):
t, point_data, cell_data = reader.read_data(k)
ParaView 插件
如果您已经下载了 ParaView 的二进制版本,您可以进行如下操作。
- 为 ParaView 使用的 Python 主要版本安装 meshio(检查
pvpython --version
) - 打开 ParaView
- 找到
paraview-meshio-plugin.py
你的 meshio 安装文件(在 Linux 上:) 并在Tools / Manage Plugins / Load New~/.local/share/paraview-5.9/plugins/
下加载它 - 可选:激活自动加载
您现在可以在 ParaView 中打开所有 meshio 支持的文件。
性能比较
这里的比较是针对具有大约 900k 点和 1.8M 三角形的三角形网格。红线标记了内存中网格的大小。
文件大小
输入输出速度
最大内存使用量
安装
meshio可从 Python Package Index 获得,因此只需运行
pip install meshio
安装。
某些输出格式需要额外的依赖项 ( netcdf4
, h5py
),并且可以通过
pip install meshio[all]
您还可以从Anaconda安装 meshio :
conda install -c conda-forge meshio
测试
要运行 meshio 单元测试,请查看此存储库并键入
tox
执照
meshio 是在MIT 许可下发布的。