.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/sphere.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_examples_sphere.py: Using external modules to create a mesh ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Tetrahedralize a sphere .. GENERATED FROM PYTHON SOURCE LINES 7-12 .. code-block:: Python # sphinx_gallery_thumbnail_number = 2 import pyvista as pv import tetgen .. GENERATED FROM PYTHON SOURCE LINES 13-16 Using PyVista ~~~~~~~~~~~~~ Create a surface mesh using ``pyvista`` and then tetrahedralize it. .. GENERATED FROM PYTHON SOURCE LINES 16-23 .. code-block:: Python sphere = pv.Sphere() tet = tetgen.TetGen(sphere) tet.tetrahedralize(order=1, mindihedral=20, minratio=1.5) grid = tet.grid grid.plot(show_edges=True) .. image-sg:: /examples/images/sphx_glr_sphere_001.png :alt: sphere :srcset: /examples/images/sphx_glr_sphere_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 24-25 Use pyvista to plot .. GENERATED FROM PYTHON SOURCE LINES 25-41 .. code-block:: Python # get cell centroids cell_center = grid.cell_centers().points # 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() .. image-sg:: /examples/images/sphx_glr_sphere_002.png :alt: sphere :srcset: /examples/images/sphx_glr_sphere_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 42-44 Use pyvista to compute cell quality. This is the minimum scaled jacobian of each cell. .. GENERATED FROM PYTHON SOURCE LINES 44-59 .. code-block:: Python cell_qual = subgrid.compute_cell_quality()["CellQuality"] print(f"Mean cell quality: {cell_qual.mean():.3}") # plot quality subgrid.plot( scalars=cell_qual, scalar_bar_args={"title": "Quality"}, cmap="bwr", clim=[0, 1], flip_scalars=True, show_edges=True, ) .. image-sg:: /examples/images/sphx_glr_sphere_003.png :alt: sphere :srcset: /examples/images/sphx_glr_sphere_003.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Mean cell quality: 0.616 .. GENERATED FROM PYTHON SOURCE LINES 60-69 Using pyacvd ~~~~~~~~~~~~ We can use `pyacvd `_ to create a more uniform mesh using the mesh generated from ``pyvista``. We can use the ``pyacvd`` module to generate a more uniform surface mesh and then tetrahedralize that. Here we re-run the above example, except using a more uniform surface .. GENERATED FROM PYTHON SOURCE LINES 69-85 .. code-block:: Python import pyacvd n_surf = 1000 clustered = pyacvd.Clustering(sphere) clustered.subdivide(2) clustered.cluster(n_surf) uniform_surf = clustered.create_mesh() # generate interior mesh and plot the surface of it tet = tetgen.TetGen(uniform_surf) tet.tetrahedralize(order=1, mindihedral=20, minratio=1.5) uniform_grid = tet.grid uniform_grid.plot(show_edges=True) .. image-sg:: /examples/images/sphx_glr_sphere_004.png :alt: sphere :srcset: /examples/images/sphx_glr_sphere_004.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 86-87 Plot the cross section of the tetrahedralization .. GENERATED FROM PYTHON SOURCE LINES 87-107 .. code-block:: Python cell_center = uniform_grid.cell_centers().points # extract cells below the 0 xy plane mask = cell_center[:, 2] < 0 cell_ind = mask.nonzero()[0] subgrid = uniform_grid.extract_cells(cell_ind) cell_qual = subgrid.compute_cell_quality()["CellQuality"] print(f"Mean cell quality: {cell_qual.mean():.3}") # plot quality subgrid.plot( scalars=cell_qual, scalar_bar_args={"title": "Quality"}, cmap="bwr", clim=[0, 1], flip_scalars=True, show_edges=True, ) .. image-sg:: /examples/images/sphx_glr_sphere_005.png :alt: sphere :srcset: /examples/images/sphx_glr_sphere_005.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Mean cell quality: 0.642 .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 3.782 seconds) .. _sphx_glr_download_examples_sphere.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: sphere.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: sphere.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_