跳转到主要内容

Python接口到tetgen

项目描述

https://img.shields.io/pypi/v/tetgen.svg?logo=python&logoColor=white

这个Python库是Hang Si的TetGen C++软件的接口。此模块结合了C++的速度、Python的可移植性和易用性,以及与PyVista的集成,用于3D可视化和分析。有关原始创建者的更多信息,请参阅TetGen的GitHub页面。

此Python库使用TetGen的C++源代码(版本1.6.0,于2020年8月31日发布),托管在libigl/tetgen

来自Weierstrass Institute Software的简要描述

TetGen是一个用于生成任何3D多面体域四面体网格的程序。TetGen生成精确的约束Delaunay四面体化、边界符合Delaunay网格和Voronoi划分。

TetGen提供各种功能,生成适用于数值方法(如有限元法或有限体积法)的高质量自适应四面体网格。有关TetGen的更多信息,请参阅功能列表

许可协议(AGPL)

原始的TetGen软件遵循AGPL许可证(参见LICENSE),因此这个Python封装包也必须采用相同的许可证。

在您的下游包中创建到该软件的动态链接之前,请查阅此许可证的条款,并了解商业用途的限制。我们不是律师,不能提供关于此许可证条款的任何指导。

请参阅https://gnu.ac.cn/licenses/agpl-3.0.en.html

安装

PyPI安装

pip install tetgen

GitHub源码安装

git clone https://github.com/pyvista/tetgen
cd tetgen
pip install .

基本示例

本模块中实现的C++ TetGen软件的功能主要针对流形三角表面的四面体化。以下基本示例演示了如何四面体化一个流形表面并绘制网格的一部分。

import pyvista as pv
import tetgen
import numpy as np
pv.set_plot_theme('document')

sphere = pv.Sphere()
tet = tetgen.TetGen(sphere)
tet.tetrahedralize(order=1, mindihedral=20, minratio=1.5)
grid = tet.grid
grid.plot(show_edges=True)
https://github.com/pyvista/tetgen/raw/main/doc/images/sphere.png

四面体化的球体

从xy平面以下提取球体的四面体网格的一部分,并绘制网格质量。

# get cell centroids
cells = grid.cells.reshape(-1, 5)[:, 1:]
cell_center = grid.points[cells].mean(1)

# extract cells below the 0 xy plane
mask = cell_center[:, 2] < 0
cell_ind = mask.nonzero()[0]
subgrid = grid.extract_cells(cell_ind)

# advanced plotting
plotter = pv.Plotter()
plotter.add_mesh(subgrid, 'lightgrey', lighting=True, show_edges=True)
plotter.add_mesh(sphere, 'r', 'wireframe')
plotter.add_legend([[' Input Mesh ', 'r'],
                    [' Tessellated Mesh ', 'black']])
plotter.show()
https://github.com/pyvista/tetgen/raw/main/doc/images/sphere_subgrid.png

以下是根据最小缩放雅可比计算的单元质量。

Compute cell quality

>>> cell_qual = subgrid.compute_cell_quality()['CellQuality']

Plot quality

>>> subgrid.plot(scalars=cell_qual, stitle='Quality', cmap='bwr', clim=[0, 1],
...              flip_scalars=True, show_edges=True)
https://github.com/pyvista/tetgen/raw/main/doc/images/sphere_qual.png

使用背景网格

TetGen中的背景网格用于定义自适应网格细化中的网格尺寸函数。此函数通知TetGen在整个域中期望的单元大小,允许在不必要的整个网格密集化的情况下,在特定区域进行详细细化。以下是如何在您的TetGen工作流程中利用背景网格的方法

  1. 生成背景网格:创建一个覆盖您输入的分段线性复杂(PLC)域全部范围的四面体网格。此网格将作为您尺寸函数的基础。

  2. 定义尺寸函数:在背景网格的节点上,定义所需的网格尺寸。这可以基于几何特征、接近感兴趣区域,或任何与您的模拟需求相关的标准。

  3. 可选:导出背景网格和尺寸函数:将您的背景网格保存为TetGen可读的.node.ele格式,并将尺寸函数值保存到.mtr文件中。这些文件将被TetGen用于指导网格生成过程。

  4. 使用背景网格运行TetGen:调用TetGen,指定背景网格。TetGen将根据提供的尺寸函数调整网格,在需要较小单元的地方进行细化。

完整示例

为了说明,假设您想要在特定区域进行细化以提高细节。以下步骤和代码片段演示了如何使用TetGen和PyVista实现这一点

  1. 准备您的PLC和背景网格:

    import pyvista as pv
    import tetgen
    import numpy as np
    
    # Load or create your PLC
    sphere = pv.Sphere(theta_resolution=10, phi_resolution=10)
    
    # Generate a background mesh with desired resolution
    def generate_background_mesh(bounds, resolution=20, eps=1e-6):
        x_min, x_max, y_min, y_max, z_min, z_max = bounds
        grid_x, grid_y, grid_z = np.meshgrid(
            np.linspace(xmin - eps, xmax + eps, resolution),
            np.linspace(ymin - eps, ymax + eps, resolution),
            np.linspace(zmin - eps, zmax + eps, resolution),
            indexing="ij",
        )
        return pv.StructuredGrid(grid_x, grid_y, grid_z).triangulate()
    
    bg_mesh = generate_background_mesh(sphere.bounds)
  2. 定义尺寸函数并将其写入磁盘:

    # Define sizing function based on proximity to a point of interest
    def sizing_function(points, focus_point=np.array([0, 0, 0]), max_size=1.0, min_size=0.1):
        distances = np.linalg.norm(points - focus_point, axis=1)
        return np.clip(max_size - distances, min_size, max_size)
    
    bg_mesh.point_data['target_size'] = sizing_function(bg_mesh.points)
    
    # Optionally write out the background mesh
    def write_background_mesh(background_mesh, out_stem):
        """Write a background mesh to a file.
    
        This writes the mesh in tetgen format (X.b.node, X.b.ele) and a X.b.mtr file
        containing the target size for each node in the background mesh.
        """
        mtr_content = [f"{background_mesh.n_points} 1"]
        target_size = background_mesh.point_data["target_size"]
        for i in range(background_mesh.n_points):
            mtr_content.append(f"{target_size[i]:.8f}")
    
        pv.save_meshio(f"{out_stem}.node", background_mesh)
        mtr_file = f"{out_stem}.mtr"
    
        with open(mtr_file, "w") as f:
            f.write("\n".join(mtr_content))
    
    write_background_mesh(bg_mesh, 'bgmesh.b')
  3. 使用背景网格运行TetGen:

    直接从PyVista传递背景网格到tetgen

    tet_kwargs = dict(order=1, mindihedral=20, minratio=1.5)
    tet = tetgen.TetGen(mesh)
    tet.tetrahedralize(bgmesh=bgmesh, **tet_kwargs)
    refined_mesh = tet.grid

    或者,使用背景网格文件。

    tet = tetgen.TetGen(sphere)
    tet.tetrahedralize(bgmeshfilename='bgmesh.b', **tet_kwargs)
    refined_mesh = tet.grid

此示例演示了生成背景网格、定义空间变化的尺寸函数以及使用此背景网格引导TetGen细化PLC。通过遵循这些步骤,您可以实现符合您特定模拟要求的自适应网格细化。

致谢

该软件最初由Hang Si基于在基于Delaunay的质量四面体网格生成器TetGen中发表的工作创建。

项目详情


下载文件

下载适用于您平台的文件。如果您不确定选择哪个,请了解有关安装包的更多信息。

源分发

tetgen-0.6.5.tar.gz (507.2 kB 查看哈希值)

上传时间

构建分发

tetgen-0.6.5-cp312-cp312-win_amd64.whl (349.3 kB 查看哈希值)

上传时间 CPython 3.12 Windows x86-64

tetgen-0.6.5-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.1 MB 查看哈希值)

上传时间 CPython 3.12 manylinux: glibc 2.17+ x86-64

tetgen-0.6.5-cp312-cp312-macosx_11_0_arm64.whl (440.7 kB 查看哈希值)

上传时间 CPython 3.12 macOS 11.0+ ARM64

tetgen-0.6.5-cp312-cp312-macosx_10_9_x86_64.whl (486.8 kB 查看哈希值)

上传时间 CPython 3.12 macOS 10.9+ x86-64

tetgen-0.6.5-cp311-cp311-win_amd64.whl (348.7 kB 查看哈希值)

上传时间 CPython 3.11 Windows x86-64

tetgen-0.6.5-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.1 MB 查看哈希值)

上传时间 CPython 3.11 manylinux: glibc 2.17+ x86-64

tetgen-0.6.5-cp311-cp311-macosx_11_0_arm64.whl (440.1 kB 查看哈希值)

上传时间 CPython 3.11 macOS 11.0+ ARM64

tetgen-0.6.5-cp311-cp311-macosx_10_9_x86_64.whl (485.8 kB 查看哈希值)

上传时间 CPython 3.11 macOS 10.9+ x86-64

tetgen-0.6.5-cp310-cp310-win_amd64.whl (348.5 kB 查看哈希值)

上传时间 CPython 3.10 Windows x86-64

tetgen-0.6.5-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.1 MB 查看哈希值)

上传时间: CPython 3.10 manylinux: glibc 2.17+ x86-64

tetgen-0.6.5-cp310-cp310-macosx_11_0_arm64.whl (440.0 kB 查看哈希值)

上传时间: CPython 3.10 macOS 11.0+ ARM64

tetgen-0.6.5-cp310-cp310-macosx_10_9_x86_64.whl (485.6 kB 查看哈希值)

上传时间: CPython 3.10 macOS 10.9+ x86-64

tetgen-0.6.5-cp39-cp39-win_amd64.whl (349.1 kB 查看哈希值)

上传时间: CPython 3.9 Windows x86-64

tetgen-0.6.5-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.1 MB 查看哈希值)

上传时间: CPython 3.9 manylinux: glibc 2.17+ x86-64

tetgen-0.6.5-cp39-cp39-macosx_11_0_arm64.whl (440.5 kB 查看哈希值)

上传时间: CPython 3.9 macOS 11.0+ ARM64

tetgen-0.6.5-cp39-cp39-macosx_10_9_x86_64.whl (486.1 kB 查看哈希值)

上传时间: CPython 3.9 macOS 10.9+ x86-64

由以下支持:

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF 赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误记录 StatusPage StatusPage 状态页面