.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/01-Detailed-Examples/05-mesh-exploration.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_01-Detailed-Examples_05-mesh-exploration.py: .. _ref_mesh_exploration_example: Explore the mesh ================ This example shows how to explore and manipulate the mesh object to query mesh data such as connectivity tables, element IDs, element types and so on. .. GENERATED FROM PYTHON SOURCE LINES 11-16 Perform required imports ------------------------ Perform required imports. This example uses a supplied file that you can get by importing the DPF ``examples`` package. .. GENERATED FROM PYTHON SOURCE LINES 16-21 .. code-block:: Python from ansys.dpf import post from ansys.dpf.post import examples from ansys.dpf.post.common import elemental_properties .. GENERATED FROM PYTHON SOURCE LINES 22-28 Load the result file -------------------- Load the result file in a ``Simulation`` object that allows access to the results. The ``Simulation`` object must be instantiated with the path for the result file. For example, ``"C:/Users/user/my_result.rst"`` on Windows or ``"/home/user/my_result.rst"`` on Linux. .. GENERATED FROM PYTHON SOURCE LINES 28-32 .. code-block:: Python example_path = examples.download_harmonic_clamped_pipe() simulation = post.HarmonicMechanicalSimulation(example_path) .. GENERATED FROM PYTHON SOURCE LINES 33-36 Get the mesh ------------ Retrieve the mesh and print it .. GENERATED FROM PYTHON SOURCE LINES 36-39 .. code-block:: Python mesh = simulation.mesh print(mesh) .. rst-class:: sphx-glr-script-out .. code-block:: none DPF Mesh: 9943 nodes 5732 elements Unit: mm With solid (3D) elements, shell (2D) elements, shell (3D) elements .. GENERATED FROM PYTHON SOURCE LINES 40-43 Plot the mesh ------------- Plot the mesh to view the bare mesh of the model .. GENERATED FROM PYTHON SOURCE LINES 43-45 .. code-block:: Python mesh.plot() .. image-sg:: /examples/01-Detailed-Examples/images/sphx_glr_05-mesh-exploration_001.png :alt: 05 mesh exploration :srcset: /examples/01-Detailed-Examples/images/sphx_glr_05-mesh-exploration_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 46-49 Query basic information about the mesh -------------------------------------- The ``Mesh`` object has several properties allowing access to different information such as: .. GENERATED FROM PYTHON SOURCE LINES 51-52 the number of nodes .. GENERATED FROM PYTHON SOURCE LINES 52-54 .. code-block:: Python print(f"This mesh contains {mesh.num_nodes} nodes") .. rst-class:: sphx-glr-script-out .. code-block:: none This mesh contains 9943 nodes .. GENERATED FROM PYTHON SOURCE LINES 55-56 the list of node IDs .. GENERATED FROM PYTHON SOURCE LINES 56-58 .. code-block:: Python print(f"with IDs: {mesh.node_ids}") .. rst-class:: sphx-glr-script-out .. code-block:: none with IDs: [ 1 2 3 ... 9941 9942 9943] .. GENERATED FROM PYTHON SOURCE LINES 59-60 the number of elements .. GENERATED FROM PYTHON SOURCE LINES 60-62 .. code-block:: Python print(f"This mesh contains {mesh.num_elements} elements") .. rst-class:: sphx-glr-script-out .. code-block:: none This mesh contains 5732 elements .. GENERATED FROM PYTHON SOURCE LINES 63-64 the list of element IDs .. GENERATED FROM PYTHON SOURCE LINES 64-66 .. code-block:: Python print(f"with IDs {mesh.element_ids}") .. rst-class:: sphx-glr-script-out .. code-block:: none with IDs [3487 3960 1449 ... 8438 8437 8540] .. GENERATED FROM PYTHON SOURCE LINES 67-68 the unit of the mesh .. GENERATED FROM PYTHON SOURCE LINES 68-70 .. code-block:: Python print(f"The mesh is in '{mesh.unit}'") .. rst-class:: sphx-glr-script-out .. code-block:: none The mesh is in 'mm' .. GENERATED FROM PYTHON SOURCE LINES 71-76 Named selections ---------------- The available named selections are given as a dictionary with the names as keys and the actual ``NamedSelection`` objects as values. Printing the dictionary informs you on the available names. .. GENERATED FROM PYTHON SOURCE LINES 76-79 .. code-block:: Python named_selections = mesh.named_selections print(named_selections) .. rst-class:: sphx-glr-script-out .. code-block:: none NamedSelections dictionary with 4 named selections: - 'CLAMP' - 'PIPE' - 'SCREW' - '_FIXEDSU' .. GENERATED FROM PYTHON SOURCE LINES 80-81 To get a specific named selection, query it using its name as key .. GENERATED FROM PYTHON SOURCE LINES 81-83 .. code-block:: Python print(named_selections["_FIXEDSU"]) .. rst-class:: sphx-glr-script-out .. code-block:: none NamedSelection '_FIXEDSU' with DPF Scoping: with Nodal location and 161 entities .. GENERATED FROM PYTHON SOURCE LINES 84-87 Elements -------- Use ``mesh.elements`` to access the list of Element objects .. GENERATED FROM PYTHON SOURCE LINES 87-89 .. code-block:: Python print(mesh.elements) .. rst-class:: sphx-glr-script-out .. code-block:: none [tet10, ..., point1] .. GENERATED FROM PYTHON SOURCE LINES 90-91 You can then query a specific element by its ID .. GENERATED FROM PYTHON SOURCE LINES 91-93 .. code-block:: Python print(mesh.elements.by_id[1]) .. rst-class:: sphx-glr-script-out .. code-block:: none DPF Element 1 Index: 4239 Nodes: 20 Type: Hex20 Shape: Solid .. GENERATED FROM PYTHON SOURCE LINES 94-95 or by its index .. GENERATED FROM PYTHON SOURCE LINES 95-98 .. code-block:: Python element_0 = mesh.elements[0] print(element_0) .. rst-class:: sphx-glr-script-out .. code-block:: none DPF Element 3487 Index: 0 Nodes: 10 Type: Tet10 Shape: Solid .. GENERATED FROM PYTHON SOURCE LINES 99-102 Query information about a particular element -------------------------------------------- You can request the IDs of the nodes attached to an ``Element`` object .. GENERATED FROM PYTHON SOURCE LINES 102-104 .. code-block:: Python print(element_0.node_ids) .. rst-class:: sphx-glr-script-out .. code-block:: none [3548, 3656, 4099, 3760, 6082, 6650, 6086, 6085, 6647, 7147] .. GENERATED FROM PYTHON SOURCE LINES 105-106 or the list of ``Node`` objects .. GENERATED FROM PYTHON SOURCE LINES 106-108 .. code-block:: Python print(element_0.nodes) .. rst-class:: sphx-glr-script-out .. code-block:: none [, , , , , , , , , ] .. GENERATED FROM PYTHON SOURCE LINES 109-110 To get the number of nodes attached, use .. GENERATED FROM PYTHON SOURCE LINES 110-112 .. code-block:: Python print(element_0.num_nodes) .. rst-class:: sphx-glr-script-out .. code-block:: none 10 .. GENERATED FROM PYTHON SOURCE LINES 113-114 Get the type of the element .. GENERATED FROM PYTHON SOURCE LINES 114-117 .. code-block:: Python print(element_0.type_info) print(element_0.type) .. rst-class:: sphx-glr-script-out .. code-block:: none Element Type ------------ Enum id (dpf.element_types): element_types.Tet10 Element description: Quadratic 10-nodes Tetrahedron Element name (short): tet10 Element shape: solid Number of corner nodes: 4 Number of mid-side nodes: 6 Total number of nodes: 10 Quadratic element: True element_types.Tet10 .. GENERATED FROM PYTHON SOURCE LINES 118-119 Get the shape of the element .. GENERATED FROM PYTHON SOURCE LINES 119-121 .. code-block:: Python print(element_0.shape) .. rst-class:: sphx-glr-script-out .. code-block:: none solid .. GENERATED FROM PYTHON SOURCE LINES 122-126 Element types and materials --------------------------- The ``Mesh`` object provides access to properties defined on all elements, such as their types or their associated materials. .. GENERATED FROM PYTHON SOURCE LINES 128-129 Get the type of all elements .. GENERATED FROM PYTHON SOURCE LINES 129-131 .. code-block:: Python print(mesh.element_types) .. rst-class:: sphx-glr-script-out .. code-block:: none results elem_type_id element_ids 3487 0 3960 0 1449 0 3131 0 3124 0 3126 0 ... ... .. GENERATED FROM PYTHON SOURCE LINES 132-133 Get the materials of all elements .. GENERATED FROM PYTHON SOURCE LINES 133-135 .. code-block:: Python print(mesh.materials) .. rst-class:: sphx-glr-script-out .. code-block:: none results material_id element_ids 3487 1 3960 1 1449 1 3131 1 3124 1 3126 1 ... ... .. GENERATED FROM PYTHON SOURCE LINES 136-139 Elemental connectivity ---------------------- The elemental connectivity maps elements to connected nodes, either using IDs or indexes. .. GENERATED FROM PYTHON SOURCE LINES 141-142 To access the indexes of the connected nodes using an element's index, use .. GENERATED FROM PYTHON SOURCE LINES 142-145 .. code-block:: Python element_to_node_connectivity = mesh.element_to_node_connectivity print(element_to_node_connectivity[0]) .. rst-class:: sphx-glr-script-out .. code-block:: none [3547, 3655, 4098, 3759, 6081, 6649, 6085, 6084, 6646, 7146] .. GENERATED FROM PYTHON SOURCE LINES 146-147 To access the IDs of the connected nodes using an element's index, use .. GENERATED FROM PYTHON SOURCE LINES 147-150 .. code-block:: Python element_to_node_ids_connectivity = mesh.element_to_node_ids_connectivity print(element_to_node_ids_connectivity[0]) .. rst-class:: sphx-glr-script-out .. code-block:: none [3548, 3656, 4099, 3760, 6082, 6650, 6086, 6085, 6647, 7147] .. GENERATED FROM PYTHON SOURCE LINES 151-152 Each connectivity object has a ``by_id`` property which changes the input from index to ID, thus: .. GENERATED FROM PYTHON SOURCE LINES 154-155 To access the indexes of the connected nodes using an element's ID, use .. GENERATED FROM PYTHON SOURCE LINES 155-158 .. code-block:: Python element_to_node_connectivity_by_id = mesh.element_to_node_connectivity.by_id print(element_to_node_connectivity_by_id[3487]) .. rst-class:: sphx-glr-script-out .. code-block:: none [3547, 3655, 4098, 3759, 6081, 6649, 6085, 6084, 6646, 7146] .. GENERATED FROM PYTHON SOURCE LINES 159-160 To access the IDs of the connected nodes using an element's ID, use .. GENERATED FROM PYTHON SOURCE LINES 160-163 .. code-block:: Python element_to_node_ids_connectivity_by_id = mesh.element_to_node_ids_connectivity.by_id print(element_to_node_ids_connectivity_by_id[3487]) .. rst-class:: sphx-glr-script-out .. code-block:: none [3548, 3656, 4099, 3760, 6082, 6650, 6086, 6085, 6647, 7147] .. GENERATED FROM PYTHON SOURCE LINES 164-167 Nodes ----- Get a node by its ID .. GENERATED FROM PYTHON SOURCE LINES 167-170 .. code-block:: Python node_1 = mesh.nodes.by_id[1] print(node_1) .. rst-class:: sphx-glr-script-out .. code-block:: none Node(id=1, coordinates=[44.90718016, 12.57776697, 53.33333333]) .. GENERATED FROM PYTHON SOURCE LINES 171-172 Get a node by its index .. GENERATED FROM PYTHON SOURCE LINES 172-174 .. code-block:: Python print(mesh.nodes[0]) .. rst-class:: sphx-glr-script-out .. code-block:: none Node(id=1, coordinates=[44.90718016, 12.57776697, 53.33333333]) .. GENERATED FROM PYTHON SOURCE LINES 175-176 Get the coordinates of all nodes .. GENERATED FROM PYTHON SOURCE LINES 176-178 .. code-block:: Python print(mesh.coordinates) .. rst-class:: sphx-glr-script-out .. code-block:: none results coord (m) node_ids components 1 X 4.4907e+01 Y 1.2578e+01 Z 5.3333e+01 2 X 4.4907e+01 Y 1.2578e+01 Z 5.1667e+01 ... ... ... .. GENERATED FROM PYTHON SOURCE LINES 179-182 Query information about one particular node ------------------------------------------- Get the coordinates of a node .. GENERATED FROM PYTHON SOURCE LINES 182-184 .. code-block:: Python print(node_1.coordinates) .. rst-class:: sphx-glr-script-out .. code-block:: none [44.90718016, 12.57776697, 53.33333333] .. GENERATED FROM PYTHON SOURCE LINES 185-188 Nodal connectivity ------------------ The nodal connectivity maps nodes to connected elements, either using IDs or indexes. .. GENERATED FROM PYTHON SOURCE LINES 190-191 To access the indexes of the connected elements using a node's index, use .. GENERATED FROM PYTHON SOURCE LINES 191-194 .. code-block:: Python node_to_element_connectivity = mesh.node_to_element_connectivity print(node_to_element_connectivity[0]) .. rst-class:: sphx-glr-script-out .. code-block:: none [4216, 4218, 4219, 4242, 4244, 4245] .. GENERATED FROM PYTHON SOURCE LINES 195-196 To access the IDs of the connected elements using a node's index, use .. GENERATED FROM PYTHON SOURCE LINES 196-199 .. code-block:: Python node_to_element_ids_connectivity = mesh.node_to_element_ids_connectivity print(node_to_element_ids_connectivity[0]) .. rst-class:: sphx-glr-script-out .. code-block:: none [11, 8, 14, 10, 7, 13] .. GENERATED FROM PYTHON SOURCE LINES 200-201 Each connectivity object has a ``by_id`` property which changes the input from index to ID, thus: .. GENERATED FROM PYTHON SOURCE LINES 203-204 To access the indexes of the connected elements using a node's ID, use .. GENERATED FROM PYTHON SOURCE LINES 204-207 .. code-block:: Python node_to_element_connectivity_by_id = mesh.node_to_element_connectivity.by_id print(node_to_element_connectivity_by_id[1]) .. rst-class:: sphx-glr-script-out .. code-block:: none [4216, 4218, 4219, 4242, 4244, 4245] .. GENERATED FROM PYTHON SOURCE LINES 208-209 To access the IDs of the connected elements using a node's ID, use .. GENERATED FROM PYTHON SOURCE LINES 209-212 .. code-block:: Python node_to_element_ids_connectivity_by_id = mesh.node_to_element_ids_connectivity.by_id print(node_to_element_ids_connectivity_by_id[1]) .. rst-class:: sphx-glr-script-out .. code-block:: none [11, 8, 14, 10, 7, 13] .. GENERATED FROM PYTHON SOURCE LINES 213-216 Splitting into meshes --------------------- You can split the global mesh according to mesh properties to work on specific parts of the mesh .. GENERATED FROM PYTHON SOURCE LINES 216-219 .. code-block:: Python meshes = simulation.split_mesh_by_properties( properties=[elemental_properties.material, elemental_properties.element_shape] ) .. GENERATED FROM PYTHON SOURCE LINES 220-221 The object obtained is a ``Meshes`` .. GENERATED FROM PYTHON SOURCE LINES 221-223 .. code-block:: Python print(meshes) .. rst-class:: sphx-glr-script-out .. code-block:: none DPF Meshes Container with 14 mesh(es) defined on labels: elshape mat with: - mesh 0 {mat: 1, elshape: 1, } with 6673 nodes and 3517 elements. - mesh 1 {mat: 9, elshape: 0, } with 189 nodes and 55 elements. - mesh 2 {mat: 10, elshape: 0, } with 189 nodes and 55 elements. - mesh 3 {mat: 5, elshape: 0, } with 842 nodes and 319 elements. - mesh 4 {mat: 6, elshape: 0, } with 842 nodes and 319 elements. - mesh 5 {mat: 7, elshape: 0, } with 676 nodes and 306 elements. - mesh 6 {mat: 4, elshape: 1, } with 503 nodes and 72 elements. - mesh 7 {mat: 8, elshape: 0, } with 676 nodes and 306 elements. - mesh 8 {mat: 2, elshape: 1, } with 2107 nodes and 345 elements. - mesh 9 {mat: 3, elshape: 1, } with 658 nodes and 302 elements. - mesh 10 {mat: 11, elshape: 0, } with 176 nodes and 56 elements. - mesh 11 {mat: 16, elshape: 0, } with 97 nodes and 23 elements. - mesh 12 {mat: 16, elshape: 3, } with 1 nodes and 1 elements. - mesh 13 {mat: 12, elshape: 0, } with 176 nodes and 56 elements. .. GENERATED FROM PYTHON SOURCE LINES 224-225 Plotting a ``Meshes`` object plots a combination of all the ``Mesh`` objects within .. GENERATED FROM PYTHON SOURCE LINES 225-227 .. code-block:: Python meshes.plot(text="Mesh split") .. image-sg:: /examples/01-Detailed-Examples/images/sphx_glr_05-mesh-exploration_002.png :alt: 05 mesh exploration :srcset: /examples/01-Detailed-Examples/images/sphx_glr_05-mesh-exploration_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 228-229 Select a specific ``Mesh`` in the ``Meshes``, by index .. GENERATED FROM PYTHON SOURCE LINES 229-231 .. code-block:: Python meshes[0].plot(text="First mesh in the split mesh") .. image-sg:: /examples/01-Detailed-Examples/images/sphx_glr_05-mesh-exploration_003.png :alt: 05 mesh exploration :srcset: /examples/01-Detailed-Examples/images/sphx_glr_05-mesh-exploration_003.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 232-233 You can split the global mesh and select meshes based on specific property values .. GENERATED FROM PYTHON SOURCE LINES 233-241 .. code-block:: Python meshes_filtered = simulation.split_mesh_by_properties( properties={ elemental_properties.material: [2, 3, 4], elemental_properties.element_shape: 1, } ) meshes_filtered.plot(text="Mesh split and filtered") .. image-sg:: /examples/01-Detailed-Examples/images/sphx_glr_05-mesh-exploration_004.png :alt: 05 mesh exploration :srcset: /examples/01-Detailed-Examples/images/sphx_glr_05-mesh-exploration_004.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 242-243 or with a unique combination of property values .. GENERATED FROM PYTHON SOURCE LINES 243-244 .. code-block:: Python meshes[{"mat": 5, "elshape": 0}].plot(text="Mesh for mat=5 and elshape=0") .. image-sg:: /examples/01-Detailed-Examples/images/sphx_glr_05-mesh-exploration_005.png :alt: 05 mesh exploration :srcset: /examples/01-Detailed-Examples/images/sphx_glr_05-mesh-exploration_005.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 38.966 seconds) .. _sphx_glr_download_examples_01-Detailed-Examples_05-mesh-exploration.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: 05-mesh-exploration.ipynb <05-mesh-exploration.ipynb>` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: 05-mesh-exploration.py <05-mesh-exploration.py>` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_