.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "example_gallery/auto_examples/basic/create_tensor_mesh.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_example_gallery_auto_examples_basic_create_tensor_mesh.py: Create a TensorMesh from a PyVista mesh ======================================= This tutorial shows the first step in most ``graphlow`` workflows: build a small PyVista volume mesh, convert it to ``TensorMesh``, and inspect the basic mesh information exposed by the geometry/topology wrappers. .. GENERATED FROM PYTHON SOURCE LINES 11-13 Imports ------- .. GENERATED FROM PYTHON SOURCE LINES 13-19 .. code-block:: Python import numpy as np import pyvista as pv import graphlow .. GENERATED FROM PYTHON SOURCE LINES 20-24 Step 1: Build a small volume mesh --------------------------------- Keep the same mesh family across the beginner tutorials so the API changes are easier to see than the input data changes. .. GENERATED FROM PYTHON SOURCE LINES 24-35 .. code-block:: Python def make_volume_grid() -> pv.UnstructuredGrid: """Create a small warped hexahedral grid.""" x = np.array([0.0, 1.0, 2.2, 3.5], dtype=np.float32) y = np.array([0.0, 0.8, 1.7], dtype=np.float32) z = np.array([0.0, 0.6, 1.4], dtype=np.float32) X, Y, Z = np.meshgrid(x, y, z, indexing="ij") Z = Z + 0.12 * X * Y grid = pv.StructuredGrid(X, Y, Z) return grid.cast_to_unstructured_grid() .. GENERATED FROM PYTHON SOURCE LINES 36-38 Step 2: Convert to TensorMesh ----------------------------- .. GENERATED FROM PYTHON SOURCE LINES 38-64 .. code-block:: Python def main() -> None: """Run the tutorial.""" pv_mesh = make_volume_grid() mesh = graphlow.from_pyvista(pv_mesh, backend="torch") cell_types = [ pv.CellType(int(cell_type)).name for cell_type in mesh.topology.unique_cell_types() ] print(f"Backend tensor type: {type(mesh.points).__name__}") print(f"Number of points: {mesh.n_points}") print(f"Number of cells: {mesh.n_cells}") print(f"Topological dimension: {mesh.topology.mesh_dim().name}") print(f"Cell types: {cell_types}") print(f"Point tensor shape: {tuple(mesh.points.shape)}") plotter = pv.Plotter(window_size=[800, 600]) plotter.add_mesh(mesh.pvmesh, show_edges=True, color="lightgray") plotter.show_bounds(mesh=mesh.pvmesh, location="outer") plotter.camera_position = "iso" plotter.show() if __name__ == "__main__": main() .. image-sg:: /example_gallery/auto_examples/basic/images/sphx_glr_create_tensor_mesh_001.png :alt: create tensor mesh :srcset: /example_gallery/auto_examples/basic/images/sphx_glr_create_tensor_mesh_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Backend tensor type: Tensor Number of points: 36 Number of cells: 12 Topological dimension: VOLUME Cell types: ['HEXAHEDRON'] Point tensor shape: (36, 3) .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 1.647 seconds) .. _sphx_glr_download_example_gallery_auto_examples_basic_create_tensor_mesh.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: create_tensor_mesh.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: create_tensor_mesh.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: create_tensor_mesh.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_