
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "../../docs/source/_gallery/example_adjacency_mesh_computation.py"
.. LINE NUMBERS ARE GIVEN BELOW.

.. only:: html

    .. note::
        :class: sphx-glr-download-link-note

        :ref:`Go to the end <sphx_glr_download_.._.._docs_source__gallery_example_adjacency_mesh_computation.py>`
        to download the full example code.

.. rst-class:: sphx-glr-example-title

.. _sphx_glr_.._.._docs_source__gallery_example_adjacency_mesh_computation.py:


.. _example_adjacency_mesh_computation:

Adjacency matrix computation for meshes
=========================================

.. contents:: Table of Contents
   :local:
   :depth: 2
   :backlinks: top


This example demonstrates a simple workflow for computing the vertices and elements adjacency matrices for a mesh using the ``pysdic`` library.

.. seealso::

    :class:`pysdic.Mesh` - Official documentation for the Mesh class.

.. GENERATED FROM PYTHON SOURCE LINES 22-34

Creating a Mesh
---------------------------

Start by creating a mesh with triangle 3 elements.

The complete mesh is composed by :

- a tetrahedral mesh at the origin.
- an additional element connected to the tetrahedral mesh by two vertices, creating a non-manifold edge in the mesh.
- an additional element not connected to the rest of the mesh, creating an isolated element in the mesh.
- an additional vertex not connected to any element, creating an isolated vertex in the mesh.


.. GENERATED FROM PYTHON SOURCE LINES 34-65

.. code-block:: Python


    import numpy as np
    from pysdic import Mesh

    points = np.array([
        [0.0, 0.0, 0.0],  # Vertex 0
        [1.0, 0.0, 0.0],  # Vertex 1
        [0.0, 1.0, 0.0],  # Vertex 2
        [0.0, 0.0, 1.0],  # Vertex 3
        [1.0, 1.0, 1.0],  # Vertex 4 (additional vertex for the non-manifold edge)
        [0.0, 0.0, 2.0],  # Vertex 5 (isolated element)
        [1.0, 0.0, 2.0],  # Vertex 6 (isolated element)
        [0.0, 1.0, 2.0],  # Vertex 7 (isolated element)
        [2.0, 2.0, 1.0],  # Vertex 8 (isolated vertex)
    ]) # (N_v=9, E=3)

    elements = np.array([
        [0, 1, 2],  # Element 0 (tetrahedral mesh)
        [0, 1, 3],  # Element 1 (tetrahedral mesh)
        [0, 2, 3],  # Element 2 (tetrahedral mesh)
        [1, 2, 3],  # Element 3 (tetrahedral mesh)
        [1, 2, 4],  # Element 4 (additional element creating a non-manifold edge with vertices 1 and 2)
        [5, 6, 7],  # Element 5 (isolated element)
    ]) # (N_e=6, N_npe=3)

    mesh = Mesh(
        vertices=points,
        connectivity=elements,
        element_type="triangle_3"
    )








.. GENERATED FROM PYTHON SOURCE LINES 66-71

Visualize the Mesh
---------------------------

Visualize the mesh using the built-in visualization method with ``pyvista``.


.. GENERATED FROM PYTHON SOURCE LINES 71-86

.. code-block:: Python


    mesh.visualize(
        show_edges=True,
        edge_color="black",
        show_vertices=True,
        vertex_size=10,
        vertex_color="red",
        face_opacity=0.1,
        title="Example Mesh with Non-Manifold Edge \nand Isolated Element/Vertex",
        camera_position=[1.0, -8.0, 1.0],
        camera_focal_point=[1.0, 1.0, 1.0],
        camera_view_up=[0.0, 0.0, 1.0]
    )





.. image-sg:: /_gallery/images/sphx_glr_example_adjacency_mesh_computation_001.png
   :alt: example adjacency mesh computation
   :srcset: /_gallery/images/sphx_glr_example_adjacency_mesh_computation_001.png
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 87-92

Compute the adjacency matrices
--------------------------------
Use the ``compute_vertices_adjacency_matrix`` and ``compute_elements_adjacency_matrix`` methods of the Mesh class
to compute the vertices and elements adjacency matrices, respectively.


.. GENERATED FROM PYTHON SOURCE LINES 92-103

.. code-block:: Python


    vertices_adjacency_matrix = mesh.compute_vertices_adjacency_matrix()

    elements_adjacency_matrix = mesh.compute_elements_adjacency_matrix()

    print("Vertices adjacency matrix:")
    print(vertices_adjacency_matrix)

    print("Elements adjacency matrix:")
    print(elements_adjacency_matrix)





.. rst-class:: sphx-glr-script-out

 .. code-block:: none

    Vertices adjacency matrix:
    [[ 0  1  1  1  2 -1 -1 -1 -1]
     [ 1  0  1  1  1 -1 -1 -1 -1]
     [ 1  1  0  1  1 -1 -1 -1 -1]
     [ 1  1  1  0  2 -1 -1 -1 -1]
     [ 2  1  1  2  0 -1 -1 -1 -1]
     [-1 -1 -1 -1 -1  0  1  1 -1]
     [-1 -1 -1 -1 -1  1  0  1 -1]
     [-1 -1 -1 -1 -1  1  1  0 -1]
     [-1 -1 -1 -1 -1 -1 -1 -1  0]]
    Elements adjacency matrix:
    [[ 0  1  1  1  1 -1]
     [ 1  0  1  1  1 -1]
     [ 1  1  0  1  1 -1]
     [ 1  1  1  0  1 -1]
     [ 1  1  1  1  0 -1]
     [-1 -1 -1 -1 -1  0]]




.. GENERATED FROM PYTHON SOURCE LINES 104-106

Example on a larger mesh
--------------------------------

.. GENERATED FROM PYTHON SOURCE LINES 106-128

.. code-block:: Python


    from pysdic import create_triangle_3_heightmap
    import time

    surface_mesh = create_triangle_3_heightmap(
        height_function=lambda x, y: 0.5 * np.sin(np.pi * x) * np.cos(np.pi * y),
        x_bounds=(-1.0, 1.0),
        y_bounds=(-1.0, 1.0),
        n_x=50,
        n_y=50,
    )

    t0 = time.perf_counter()
    vertices_adjacency_matrix = surface_mesh.compute_vertices_adjacency_matrix(max_distance=10)
    t1 = time.perf_counter()
    t2 = time.perf_counter()
    elements_adjacency_matrix = surface_mesh.compute_elements_adjacency_matrix(max_distance=10)
    t3 = time.perf_counter()
    print(f"Vertices adjacency matrix shape: {vertices_adjacency_matrix.shape}, computed in {t1 - t0:.4f} seconds.")
    print(f"Elements adjacency matrix shape: {elements_adjacency_matrix.shape}, computed in {t3 - t2:.4f} seconds.")






.. rst-class:: sphx-glr-script-out

 .. code-block:: none

    Vertices adjacency matrix shape: (2500, 2500), computed in 0.3241 seconds.
    Elements adjacency matrix shape: (4802, 4802), computed in 1.0909 seconds.




.. GENERATED FROM PYTHON SOURCE LINES 129-135

Extract the neighborhood of a vertex
--------------------------------------

If you are only interested in the neighborhood of vertices or elements, you can use the ``compute_vertices_neighborhood`` and ``compute_elements_neighborhood`` methods of the Mesh class, respectively, which return a list of :class:`numpy.ndarray` containing the indices of the neighboring vertices or elements for each vertex or element in the mesh.
The neighborhood of a vertex (or element) is defined as the set of vertices (or elements) that are within a specified distance from the vertex (or element) in the connectivity of the mesh.


.. GENERATED FROM PYTHON SOURCE LINES 135-155

.. code-block:: Python


    from pysdic import create_triangle_3_heightmap
    import time

    surface_mesh = create_triangle_3_heightmap(
        height_function=lambda x, y: 0.5 * np.sin(np.pi * x) * np.cos(np.pi * y),
        x_bounds=(-1.0, 1.0),
        y_bounds=(-1.0, 1.0),
        n_x=50,
        n_y=50,
    )

    t0 = time.perf_counter()
    vertices_neighborhood = surface_mesh.compute_vertices_neighborhood(max_distance=2, self_neighborhood=True)
    t1 = time.perf_counter()
    t2 = time.perf_counter()
    elements_neighborhood = surface_mesh.compute_elements_neighborhood(max_distance=2, self_neighborhood=True)
    t3 = time.perf_counter()
    print(f"Vertices neighborhood of vertex 0: {vertices_neighborhood[0]}, computed in {t1 - t0:.4f} seconds.")
    print(f"Elements neighborhood of element 0: {elements_neighborhood[0]}, computed in {t3 - t2:.4f} seconds.")




.. rst-class:: sphx-glr-script-out

 .. code-block:: none

    Vertices neighborhood of vertex 0: [0, 1, 50, 51, 2, 52, 100, 101, 102], computed in 1.6481 seconds.
    Elements neighborhood of element 0: [0, 1, 2, 3, 100, 98, 101, 99, 4, 5, 102, 103, 198, 200, 201, 196, 199], computed in 6.0926 seconds.





.. rst-class:: sphx-glr-timing

   **Total running time of the script:** (0 minutes 9.455 seconds)


.. _sphx_glr_download_.._.._docs_source__gallery_example_adjacency_mesh_computation.py:

.. only:: html

  .. container:: sphx-glr-footer sphx-glr-footer-example

    .. container:: sphx-glr-download sphx-glr-download-jupyter

      :download:`Download Jupyter notebook: example_adjacency_mesh_computation.ipynb <example_adjacency_mesh_computation.ipynb>`

    .. container:: sphx-glr-download sphx-glr-download-python

      :download:`Download Python source code: example_adjacency_mesh_computation.py <example_adjacency_mesh_computation.py>`

    .. container:: sphx-glr-download sphx-glr-download-zip

      :download:`Download zipped: example_adjacency_mesh_computation.zip <example_adjacency_mesh_computation.zip>`


.. only:: html

 .. rst-class:: sphx-glr-signature

    `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_
