{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n\n# Plotting Inverse participation ratio\n\nOften it is needed to search for **localized** modes within the band structure, typical examples are surface/interface states and defect levels. \nThe usual procedure for detecting them is looking for bands with a large projection around the atoms at the surface or defect. \nThis procedure is both cumbersome for the user and error-prone. For instance, the lowest unoccupied levels\nof the neutral $C_N$ defect in h-BN has practically no projection on the defect atom and its nearest neighbors. \nThis delayed its identification as a single-photon emitter.[jara2021,auburger2021] \nA much simpler way to detect these localized levels is by means of the **Inverse Participation Ratio**, defined as\n\n\\begin{align}IPR_{nk} = \\frac{\\sum_{a} |c_{nki}|^4}{\\left(\\sum_a c_{nka}\\right)^2}\\end{align}\n\nwhere the indexes $n,k,a$ are the band, k-point and atom, respectively. \nThis function has been extensively applied in the context of Anderson localization.[Evers2000] \nHowever, can capture any kind of localization. A perfectly localized state -**i.e.** \nlocalized in a single atom- would have $IPR=1$, but a fully extended state has $IPR=\\frac{1}{N}$, with $N$ the total number of atoms.\n\n## Preparation\nBefore diving into plotting, we need to download the example files. \nUse the following code to do this. Once downloaded, specify the `data_dir` to point to the location of the downloaded data.\n\n.. code-block::\n  :caption: Downloading example\n\n  import pyprocar\n\n  bi2se3_data_dir = pyprocar.download_example(\n                              save_dir='', \n                              material='Bi2Se3-spinorbit-surface',\n                              code='vasp', \n                              spin_calc_type='spin-polarized-colinear',\n                              calc_type='bands'\n                              )\n\n  C_data_dir = pyprocar.download_example(\n                              save_dir='', \n                              material='NV-center',\n                              code='vasp', \n                              spin_calc_type='spin-polarized-colinear',\n                              calc_type='bands'\n                              )\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Setting up the environment\nFirst, we will import the necessary libraries and set up our data directory path.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import os\n\nimport pyprocar\n\n# Define the directory containing the example data\nbi2se3_data_dir = os.path.join(\n    pyprocar.utils.DATA_DIR, \"examples\", \"Bi2Se3-spinorbit-surface\"\n)\n\n\nC_data_dir = os.path.join(pyprocar.utils.DATA_DIR, \"examples\", \"NV-center\")"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Topologically-protected surface states in $Bi_2Se_3$\n\nThe first example is the detection of topologically-protected surface states in $Bi_2Se_3$, [zhang2009].\nThe whole slab has six van der Waals layers (quintuple layers), each is five atom thick. The surface states localize on the outer quintuple layers,\nin contrast a extended state cover the six quintuple layers.\nThe ratio between the localization of both types of states is 1 to 3, and the $IPR$ has enough resolution to provide a clear visual identification.\nThe PyProcar code is:\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "pyprocar.bandsplot(\n    dirname=bi2se3_data_dir,\n    elimit=[-1.0, 1.0],\n    mode=\"ipr\",\n    code=\"vasp\",\n    spins=[0],\n    fermi=2.0446,\n    clim=[0, 0.2],\n)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## $NV^-$ defect in diamond\n\nThe second example is the $NV^-$ defect in diamond, it is a negatively charged N substitution plus an adjacent vacancy.\nThis defect if of interest as a source of single photons. Its ground state is a triplet, allowing the control of the spin by microwave radiation.[DOHERTY2013]\nThe supercell has 215 atoms, hence $IPR\\to0$ for bulk states (blue lines).\nSeveral defect levels lie within the fundamental band gap of diamond (dark red lines). The closest levels to the Fermi energy are double degenerate (**i.e.** triplet),\nbut only occupied for the spin majority. Hence, according to the optical transition takes place between the bands with index $430\\to431$ or $430\\to432$\nof the spin channel labelled `spin-1`. The calculation of the main emission line involves a calculation of the excited state,\nwhich can be simulated by fixing the occupations of the mentioned levels, **i.e.** the $\\Delta$ SCFmethod.[Jin2021]\nThe pyprocar code is:\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "pyprocar.bandsplot(\n    dirname=C_data_dir,\n    elimit=[-3.0, 2.5],\n    mode=\"ipr\",\n    code=\"vasp\",\n    fermi=12.4563,\n    spins=[0, 1],\n    clim=[0, 0.1],\n)"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.9.20"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}