
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "examples\06-PyPoscar\plot_clusters_pyposcar.py"
.. LINE NUMBERS ARE GIVEN BELOW.

.. only:: html

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

        :ref:`Go to the end <sphx_glr_download_examples_06-PyPoscar_plot_clusters_pyposcar.py>`
        to download the full example code.

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

.. _sphx_glr_examples_06-PyPoscar_plot_clusters_pyposcar.py:


.. _ref_example_cluster_visualization:

Visualizing Clusters in Bi2Se3 Slab
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

In this example, we'll explore the atomic clusters within a slab of Bi2Se3. The slab structure is formed by quintuple layers (QL) where each QL can be regarded as a cluster. We'll:

1. Parse the POSCAR file to obtain atomic positions.
2. Identify and label the atomic clusters within the slab.
3. Visualize the atomic clusters using `pyvista`.

Let's get started!

.. GENERATED FROM PYTHON SOURCE LINES 15-28

.. code-block:: Python


    import os
    from itertools import product

    import numpy as np
    import pyvista as pv

    import pyprocar.pyposcar as p
    from pyprocar.utils import DATA_DIR

    data_dir = os.path.join(DATA_DIR, "examples", "PyPoscar", "05-clusters")
    # You do not need this. This is to ensure an image is rendered off screen when generating exmaple gallery.
    pv.OFF_SCREEN = True







.. GENERATED FROM PYTHON SOURCE LINES 29-31

Utility function for creating GIF visualizations
++++++++++++++++++++++++++++++++++++++++++++++++

.. GENERATED FROM PYTHON SOURCE LINES 31-59

.. code-block:: Python



    def create_gif(atoms, labels, unit_cell, scalars, save_file):
        plotter = pv.Plotter()
        title = save_file.split(os.sep)[-1].split(".")[0]
        plotter.add_title(title)
        plotter.add_mesh(
            unit_cell.delaunay_3d().extract_feature_edges(),
            color="black",
            line_width=5,
            render_lines_as_tubes=True,
        )
        plotter.add_point_labels(
            points=atoms.points, labels=labels, show_points=False, always_visible=True
        )
        plotter.add_mesh(
            atoms,
            scalars=scalars,
            point_size=30,
            render_points_as_spheres=True,
            show_scalar_bar=False,
        )
        path = plotter.generate_orbital_path(n_points=36)
        plotter.open_gif(os.path.join(data_dir, save_file))
        plotter.orbit_on_path(path, write_frames=True, viewup=[0, 0, 1], step=0.05)
        plotter.close()









.. GENERATED FROM PYTHON SOURCE LINES 60-62

Parsing the POSCAR and Identifying Clusters
+++++++++++++++++++++++++++++++++++++++++++

.. GENERATED FROM PYTHON SOURCE LINES 62-81

.. code-block:: Python


    print("Loading a slab of Bi2Se3")
    a = p.poscar.Poscar(os.path.join(data_dir, "Bi2Se3.vasp"), verbose=False)
    a.parse()

    print(
        "It structure is formed by the so-called Quintuple layers (QL)."
        " Each QL can be regarded as a `cluster`"
    )
    print("The Quintuple layers are")
    c = p.Clusters(a)
    print(c.clusters)

    clusters_mapping = {}
    for i, cluster in enumerate(c.clusters):
        for i_atom in cluster:
            clusters_mapping.update({i_atom: i})
    clusters_array = np.array([clusters_mapping[i] for i in range(len(a.elm))])





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

 .. code-block:: none

    Loading a slab of Bi2Se3
    It structure is formed by the so-called Quintuple layers (QL). Each QL can be regarded as a `cluster`
    The Quintuple layers are
    [[0, 1, 2, 21, 22], [3, 4, 5, 23, 24], [6, 7, 8, 25, 26], [11, 9, 10, 27, 28], [13, 14, 12, 29, 30], [32, 16, 17, 15, 31], [33, 34, 19, 18, 20]]




.. GENERATED FROM PYTHON SOURCE LINES 82-84

Visualizing the Clusters
++++++++++++++++++++++++

.. GENERATED FROM PYTHON SOURCE LINES 84-117

.. code-block:: Python


    # Convert positions to Cartesian coordinates for visualization
    atoms_before = pv.PolyData(np.dot(a.dpos, a.lat))
    atoms_before["atoms"] = a.elm

    atoms_before["clusters"] = clusters_array
    labels_before = [elm for elm, point in zip(a.elm, a.dpos)]
    labels_after = clusters_array
    # Define the unit cell using lattice vectors
    unit_cell_comb = list(product([0, 1], repeat=3))
    unit_cell = np.array(
        [
            comb[0] * a.lat[0] + comb[1] * a.lat[1] + comb[2] * a.lat[2]
            for comb in unit_cell_comb
        ]
    )
    unit_cell_before = pv.PolyData(unit_cell)


    create_gif(
        atoms=atoms_before,
        labels=labels_before,
        unit_cell=unit_cell_before,
        scalars="atoms",
        save_file="atoms_before_cluster.gif",
    )
    create_gif(
        atoms=atoms_before,
        labels=labels_after,
        unit_cell=unit_cell_before,
        scalars="clusters",
        save_file="atoms_after_cluster.gif",
    )



.. rst-class:: sphx-glr-horizontal


    *

      .. image-sg:: /examples/06-PyPoscar/images/sphx_glr_plot_clusters_pyposcar_001.gif
          :alt: plot clusters pyposcar
          :srcset: /examples/06-PyPoscar/images/sphx_glr_plot_clusters_pyposcar_001.gif
          :class: sphx-glr-multi-img

    *

      .. image-sg:: /examples/06-PyPoscar/images/sphx_glr_plot_clusters_pyposcar_002.gif
          :alt: plot clusters pyposcar
          :srcset: /examples/06-PyPoscar/images/sphx_glr_plot_clusters_pyposcar_002.gif
          :class: sphx-glr-multi-img






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

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


.. _sphx_glr_download_examples_06-PyPoscar_plot_clusters_pyposcar.py:

.. only:: html

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

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

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

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

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

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

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


.. only:: html

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

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