Python interface to tetgen
Project description
This Python library is an interface to Hang Si’s TetGen C++ software. This module combines speed of C++ with the portability and ease of installation of Python along with integration to PyVista for 3D visualization and analysis. See the TetGen GitHub page for more details on the original creator.
This Python library uses the C++ source from TetGen (version 1.6.0, released on August 31, 2020) hosted at libigl/tetgen.
Brief description from Weierstrass Institute Software:
TetGen is a program to generate tetrahedral meshes of any 3D polyhedral domains. TetGen generates exact constrained Delaunay tetrahedralization, boundary conforming Delaunay meshes, and Voronoi partitions.
TetGen provides various features to generate good quality and adaptive tetrahedral meshes suitable for numerical methods, such as finite element or finite volume methods. For more information of TetGen, please take a look at a list of features.
License (AGPL)
The original TetGen software is under AGPL (see LICENSE) and thus this Python wrapper package must adopt that license as well.
Please look into the terms of this license before creating a dynamic link to this software in your downstream package and understand commercial use limitations. We are not lawyers and cannot provide any guidance on the terms of this license.
Installation
From PyPI
pip install tetgen
From source at GitHub
git clone https://github.com/pyvista/tetgen
cd tetgen
pip install .
Basic Example
The features of the C++ TetGen software implemented in this module are primarily focused on the tetrahedralization a manifold triangular surface. This basic example demonstrates how to tetrahedralize a manifold surface and plot part of the mesh.
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)
Extract a portion of the sphere’s tetrahedral mesh below the xy plane and plot the mesh quality.
# 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()
Here is the cell quality as computed according to the minimum scaled jacobian.
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)
Using a Background Mesh
A background mesh in TetGen is used to define a mesh sizing function for adaptive mesh refinement. This function informs TetGen of the desired element size throughout the domain, allowing for detailed refinement in specific areas without unnecessary densification of the entire mesh. Here’s how to utilize a background mesh in your TetGen workflow:
Generate the Background Mesh: Create a tetrahedral mesh that spans the entirety of your input piecewise linear complex (PLC) domain. This mesh will serve as the basis for your sizing function.
Define the Sizing Function: At the nodes of your background mesh, define the desired mesh sizes. This can be based on geometric features, proximity to areas of interest, or any criterion relevant to your simulation needs.
Optional: Export the Background Mesh and Sizing Function: Save your background mesh in the TetGen-readable .node and .ele formats, and the sizing function values in a .mtr file. These files will be used by TetGen to guide the mesh generation process.
Run TetGen with the Background Mesh: Invoke TetGen, specifying the background mesh. TetGen will adjust the mesh according to the provided sizing function, refining the mesh where smaller elements are desired.
Full Example
To illustrate, consider a scenario where you want to refine a mesh around a specific region with increased detail. The following steps and code snippets demonstrate how to accomplish this with TetGen and PyVista:
Prepare Your PLC and Background Mesh:
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)
Define the Sizing Function and Write to Disk:
# 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')
Use TetGen with the Background Mesh:
Directly pass the background mesh from PyVista to 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
Alternatively, use the background mesh files.
tet = tetgen.TetGen(sphere) tet.tetrahedralize(bgmeshfilename='bgmesh.b', **tet_kwargs) refined_mesh = tet.grid
This example demonstrates generating a background mesh, defining a spatially varying sizing function, and using this background mesh to guide TetGen in refining a PLC. By following these steps, you can achieve adaptive mesh refinement tailored to your specific simulation requirements.
Acknowledgments
Software was originally created by Hang Si based on work published in TetGen, a Delaunay-Based Quality Tetrahedral Mesh Generator.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
File details
Details for the file tetgen-0.6.5.tar.gz
.
File metadata
- Download URL: tetgen-0.6.5.tar.gz
- Upload date:
- Size: 507.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.0 CPython/3.12.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 56855ccdbca98624c7f3db27ea020d3c7ea51baaa43b1c212b6a79b4ef4a9f35 |
|
MD5 | 89b664ada960668607ef85fd07e3eb01 |
|
BLAKE2b-256 | 2a447b2c2467cc56f59a1e5a7de1f4b8a7c7d6a7a3fcf769cd334badcb8a99de |
File details
Details for the file tetgen-0.6.5-cp312-cp312-win_amd64.whl
.
File metadata
- Download URL: tetgen-0.6.5-cp312-cp312-win_amd64.whl
- Upload date:
- Size: 349.3 kB
- Tags: CPython 3.12, Windows x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.0 CPython/3.12.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d50ff029172ca94c20e9b0e8f6cd604b4f8f9156834205f404a2e9ec3747c0d1 |
|
MD5 | d3dc70ed2b7bf11b5ee09d1a72cdeedf |
|
BLAKE2b-256 | 592ef1acd57edb0e857c567ab1b618234e9de64354fc60dfd1996fc8ea61e14f |
File details
Details for the file tetgen-0.6.5-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
.
File metadata
- Download URL: tetgen-0.6.5-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 2.1 MB
- Tags: CPython 3.12, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.0 CPython/3.12.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e224e35953ca2ca8fc5e95c038d81a4ac73aa9eb726130ab3c66fda721865c1c |
|
MD5 | bf56af16834bed8d6e01c1f7bf06295c |
|
BLAKE2b-256 | b28bcf4ce3ba629388cfd41a5d5f335946c1f99c061e9ab578afee81a0bca3a0 |
File details
Details for the file tetgen-0.6.5-cp312-cp312-macosx_11_0_arm64.whl
.
File metadata
- Download URL: tetgen-0.6.5-cp312-cp312-macosx_11_0_arm64.whl
- Upload date:
- Size: 440.7 kB
- Tags: CPython 3.12, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.0 CPython/3.12.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 712dce7fb72f9e395c301fce7fca136eec9ffbd3c31e5dac85e6a8bd2e36d2bd |
|
MD5 | 2e8aa0bed80bb710e082299297f5b0a8 |
|
BLAKE2b-256 | 145e2edafb4024900a47cfa3a5880d2c0363f0000e9f08c4033756a80ff55d98 |
File details
Details for the file tetgen-0.6.5-cp312-cp312-macosx_10_9_x86_64.whl
.
File metadata
- Download URL: tetgen-0.6.5-cp312-cp312-macosx_10_9_x86_64.whl
- Upload date:
- Size: 486.8 kB
- Tags: CPython 3.12, macOS 10.9+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.0 CPython/3.12.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 938f851e5ff5e092404600493e92bb8a3f4102b8dbfa033cebfb18566f70957d |
|
MD5 | a9168dc8cb24e8e7d55df318d430be3f |
|
BLAKE2b-256 | 07f2a8c6ebb18a0b6bfa7cc157e9410ef5cc943f7ad1c3bcf5fe00c8951f74ce |
File details
Details for the file tetgen-0.6.5-cp311-cp311-win_amd64.whl
.
File metadata
- Download URL: tetgen-0.6.5-cp311-cp311-win_amd64.whl
- Upload date:
- Size: 348.7 kB
- Tags: CPython 3.11, Windows x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.0 CPython/3.12.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2888169d8f721a29cf9ab624b81554fe7fe323352804aa47518ed0453b35c314 |
|
MD5 | f21a0a0e35c86a885e36e68a20b626c5 |
|
BLAKE2b-256 | 559149b24a1d7503dbf06a7f91082a25288f5cc09754d3e545db85dd172c656f |
File details
Details for the file tetgen-0.6.5-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
.
File metadata
- Download URL: tetgen-0.6.5-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 2.1 MB
- Tags: CPython 3.11, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.0 CPython/3.12.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4287f8af1bd82bd1e2e3d6517b0c54fd01b7e0d82889884e72d3b04223232066 |
|
MD5 | 0aa2c57e5f9f5b68b3b8078fdbd51820 |
|
BLAKE2b-256 | 6f7833cc40456a5b8e657e15cac5bbb68902bc7b8ee8be6927a43640a01f6b30 |
File details
Details for the file tetgen-0.6.5-cp311-cp311-macosx_11_0_arm64.whl
.
File metadata
- Download URL: tetgen-0.6.5-cp311-cp311-macosx_11_0_arm64.whl
- Upload date:
- Size: 440.1 kB
- Tags: CPython 3.11, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.0 CPython/3.12.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b330b350da447a39b11a171f9f215a1a634ac825fe07c75b4959a41667a63a37 |
|
MD5 | e52edba88dd40903323ab6b95f3d6d47 |
|
BLAKE2b-256 | 70081d880d9c0ce18052c4878f55758c428caa3346eb0d044bdb55984e840f9c |
File details
Details for the file tetgen-0.6.5-cp311-cp311-macosx_10_9_x86_64.whl
.
File metadata
- Download URL: tetgen-0.6.5-cp311-cp311-macosx_10_9_x86_64.whl
- Upload date:
- Size: 485.8 kB
- Tags: CPython 3.11, macOS 10.9+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.0 CPython/3.12.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ad53c93741618456f980b6203272a6869d8d66631aa52063f6ba8c328d183ffb |
|
MD5 | 73392e80006a2d753bcaac27ad752128 |
|
BLAKE2b-256 | 3a6657dd5b3e9bdcadbfbaa21b78c71c84768c522ab3c53b0b4a95c028befe3d |
File details
Details for the file tetgen-0.6.5-cp310-cp310-win_amd64.whl
.
File metadata
- Download URL: tetgen-0.6.5-cp310-cp310-win_amd64.whl
- Upload date:
- Size: 348.5 kB
- Tags: CPython 3.10, Windows x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.0 CPython/3.12.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c05c60f9c5c5545ea9b4c5e2cb0a25a46e35c3e6c9075207844303039b18a720 |
|
MD5 | a8b03c9888575dec49dec019779344cc |
|
BLAKE2b-256 | 46126b2ff06c3f5ae54e30b9d870afb8b786996f5603e5e8c92cc1543afd7cf5 |
File details
Details for the file tetgen-0.6.5-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
.
File metadata
- Download URL: tetgen-0.6.5-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 2.1 MB
- Tags: CPython 3.10, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.0 CPython/3.12.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e8c16c70c7453e900dd88efa5cd528ce3aae2c83cda8ad02a0eadad7fc64532d |
|
MD5 | 3fc5715056c0c13a6e404c0236dee2f8 |
|
BLAKE2b-256 | 241367b1634127adcc42394a45fc430fcb331f0cebe70b1f93efd6e37fd04e12 |
File details
Details for the file tetgen-0.6.5-cp310-cp310-macosx_11_0_arm64.whl
.
File metadata
- Download URL: tetgen-0.6.5-cp310-cp310-macosx_11_0_arm64.whl
- Upload date:
- Size: 440.0 kB
- Tags: CPython 3.10, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.0 CPython/3.12.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 054a6aaab142c62c73e9567498fc2facf8db274cdf71420e675d8133773b475c |
|
MD5 | 5d5a811f82894a5f9fdcdd53f26ddc6d |
|
BLAKE2b-256 | 75039ccf5fc1d8aeecbd172f32cd819939c2f044bc06579eb8d83b996d9ab0ff |
File details
Details for the file tetgen-0.6.5-cp310-cp310-macosx_10_9_x86_64.whl
.
File metadata
- Download URL: tetgen-0.6.5-cp310-cp310-macosx_10_9_x86_64.whl
- Upload date:
- Size: 485.6 kB
- Tags: CPython 3.10, macOS 10.9+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.0 CPython/3.12.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b89cc99b81cad302a8df952d0fba5baa72893439cb471d61b441cc6a1342ddbf |
|
MD5 | 66bb96a5af7feda93919b07df58aec71 |
|
BLAKE2b-256 | 72f0926b12a25fcd6cd208b7934c4dd38ac630755b06de2986094a7f033e3109 |
File details
Details for the file tetgen-0.6.5-cp39-cp39-win_amd64.whl
.
File metadata
- Download URL: tetgen-0.6.5-cp39-cp39-win_amd64.whl
- Upload date:
- Size: 349.1 kB
- Tags: CPython 3.9, Windows x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.0 CPython/3.12.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1398bb2d21374ddc828fca84dc66df9537641782b58be30942994fe7614a8aac |
|
MD5 | 2cb301a29d34ed24b0ec3c9bea65a311 |
|
BLAKE2b-256 | 211b59796f9bf20fae4b09fec564d62ee0f55b07413e8817a364c20a3a4adc85 |
File details
Details for the file tetgen-0.6.5-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
.
File metadata
- Download URL: tetgen-0.6.5-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 2.1 MB
- Tags: CPython 3.9, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.0 CPython/3.12.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0cad541abbc6a20b8b36e161067ea236ce865c88264bb3f517bc4c2e22a4de17 |
|
MD5 | 2b7854d745c47431f92d088339d7bd26 |
|
BLAKE2b-256 | f573fc68a41ed0a5bbe32290f75f9fcaeb3f04b3b86b1f257b0657f06898b282 |
File details
Details for the file tetgen-0.6.5-cp39-cp39-macosx_11_0_arm64.whl
.
File metadata
- Download URL: tetgen-0.6.5-cp39-cp39-macosx_11_0_arm64.whl
- Upload date:
- Size: 440.5 kB
- Tags: CPython 3.9, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.0 CPython/3.12.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1ce5285bb6504afb12fe9e8d82505a6692c238acb7aee50757743b6934693ecd |
|
MD5 | 966faa45c0d547b912ec355580824b1c |
|
BLAKE2b-256 | 360dabb2e346533c8c2aefb3cd062aad4453c78cccb72a03f88001c3239e7222 |
File details
Details for the file tetgen-0.6.5-cp39-cp39-macosx_10_9_x86_64.whl
.
File metadata
- Download URL: tetgen-0.6.5-cp39-cp39-macosx_10_9_x86_64.whl
- Upload date:
- Size: 486.1 kB
- Tags: CPython 3.9, macOS 10.9+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.0 CPython/3.12.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d664a5f2e7d39b7e59fcfa62d68f6b357f1ccf2820ebd72b08ddbf3b92808f5a |
|
MD5 | bcc6e29dd9d18b083156985a7687e25d |
|
BLAKE2b-256 | 3e6d9740bc10a8fc985e11833ef9b952e8b90ccd3495815f7b211050a9352946 |