{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n\n# Example of finding the bandgap\n\nThe ElectronicBandStructure is used to handle the information \nrelated to the electronic band structure.\n\n.. code-block::\n   :caption: General Format\n\n   import pyprocar\n   pyprocar.io.Parser(code=\"vasp\", dir=data_dir)\n\n.. code-block::\n   :caption: Downloading example\n\n    data_dir = pyprocar.download_example(save_dir='', \n                                material='Fe',\n                                code='vasp', \n                                spin_calc_type='non-spin-polarized',\n                                calc_type='bands')\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "# sphinx_gallery_thumbnail_number = 1\n\nimport pyvista as pv\n\n# You do not need this. This is to ensure an image is rendered off screen when generating exmaple gallery.\npv.OFF_SCREEN = True"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "importing pyprocar and specify the local data_dir\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import os\n\nimport numpy as np\n\nimport pyprocar\n\ndata_dir = os.path.join(\n    pyprocar.utils.DATA_DIR, \"examples\", \"Fe\", \"vasp\", \"non-spin-polarized\", \"fermi\"\n)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Initialize the parser object and get the ElectronicBandStructure\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "parser = pyprocar.io.Parser(code=\"vasp\", dir=data_dir)\nebs = parser.ebs\ne_fermi = parser.ebs.efermi\nstructure = parser.structure\n# Apply symmetry to get a full kmesh\nif structure.rotations is not None:\n    ebs.ibz2fbz(structure.rotations)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "You can print the object to see some information about the Band Structure\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "print(ebs)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Let's plot the kpoints\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "p = pv.Plotter()\np.add_mesh(ebs.kpoints, color=\"blue\", render_points_as_spheres=True)\np.show()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Other properties\n\n## Bands\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "kpoints = pv.PolyData(ebs.kpoints)\nkpoints[\"band_0\"] = ebs.bands[:, 0, 0]\n\np = pv.Plotter()\np.add_mesh(\n    kpoints,\n    color=\"blue\",\n    scalars=\"band_0\",\n    render_points_as_spheres=True,\n    point_size=10,\n)\np.show()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Projections\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "print(f\"Electron projected shape: {ebs.projected.shape}\")\nkpoints[\"band_0-atom_0-orbital_5-spin-0\"] = ebs.projected[:, 0, 0, 0, 4, 0]\n\np = pv.Plotter()\np.add_mesh(\n    kpoints,\n    color=\"blue\",\n    scalars=\"band_0-atom_0-orbital_5-spin-0\",\n    render_points_as_spheres=True,\n    point_size=10,\n)\np.show()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Gradients\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "print(f\"Band gradient shape: {ebs.bands_gradient.shape}\")\nkpoints[\"band_0-gradients\"] = ebs.bands_gradient[:, 0, 0, :]\n\n# Use the Glyph filter to generate arrows for the vectors\narrows = kpoints.glyph(orient=\"band_0-gradients\", scale=False, factor=0.08)\np = pv.Plotter()\np.add_mesh(arrows, scalar_bar_args={\"title\": \"band_0-band_velocity\"})\np.show()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Band/Fermi velocities\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "print(f\"Fermi velocity shape: {ebs.fermi_velocity.shape}\")\nprint(f\"Fermi speed shape: {ebs.fermi_speed.shape}\")\nkpoints[\"band_0-band_velocity\"] = ebs.fermi_velocity[:, 0, 0, :]\nkpoints[\"band_0-band_speed\"] = ebs.fermi_speed[:, 0, 0]\n\narrows = kpoints.glyph(orient=\"band_0-band_velocity\", scale=False, factor=0.08)\np = pv.Plotter()\np.add_mesh(\n    kpoints,\n    scalars=\"band_0-band_speed\",\n    render_points_as_spheres=True,\n    point_size=0.1,\n    show_scalar_bar=False,\n)\np.add_mesh(arrows, scalar_bar_args={\"title\": \"band_0-band_velocity\"})\np.show()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Effective mass\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "print(\n    f\"Harmonic average effective mass shape: {ebs.harmonic_average_effective_mass.shape}\"\n)\nkpoints[\"band_0-harmonic_average_effective_mass\"] = ebs.harmonic_average_effective_mass[\n    :, 0, 0\n]\n\np = pv.Plotter()\np.add_mesh(\n    kpoints,\n    scalars=\"band_0-harmonic_average_effective_mass\",\n    render_points_as_spheres=True,\n    point_size=10,\n)\np.show()"
      ]
    }
  ],
  "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
}