{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "Back to the main [Index](index.ipynb) " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Post-processing DFPT calculations with the DDB file" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This notebook explains how to use AbiPy and the DDB file produced by Abinit to analyze:\n", "\n", "* foo\n", "* bar\n", "\n", "In the last part, we discuss how to use the `DdbRobot` to analyze multiple DDB\n", "files and perform typical convergence studies.\n", "\n", "## Table of Contents\n", "\n", "* [How to create a Ddbfile object](#How-to-create-a-DdbFile-object)\n", "* [Invoking Anaddb from the DdbFile object](#Invoking-Anaddb-from-the-DdbFile-object)\n", "\n", "## Suggested references\n", "\n", "* [Dynamical matrices, Born effective charges, dielectric permittivity tensors, and interatomic force constants from density-functional perturbation theory](https://journals.aps.org/prb/abstract/10.1103/PhysRevB.55.10355)\n", "* [Phonons and related crystal properties from density-functional perturbation theory](https://journals.aps.org/rmp/abstract/10.1103/RevModPhys.73.515)\n", "\n", "AbiPy, [pymatgen](http://pymatgen.org/) and [fireworks](https://materialsproject.github.io/fireworks/)\n", "have been used by [Petretto et al](https://www.sciencedirect.com/science/article/pii/S0927025617307243)\n", "to compute the vibrational properties of more than 1500 compounds with Abinit.\n", "The results are available on the [materials project website](https://materialsproject.org/).\n", "The results for this phase of MgO are available at \n", "\n", "To fetch the DDB file from the materials project database and build a `DdbFile` object, use:\n", "\n", "```python\n", " ddb = abilab.DdbFile.from_mpid(\"mp-1009129\")\n", "```\n", "\n", "
\n", "Remember to set the `PMG_MAPI_KEY` in your ~/.pmgrc.yaml as described \n", "[here](http://pymatgen.org/usage.html#setting-the-pmg-mapi-key-in-the-config-file)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## How to create a DdbFile object \n", "[[back to top](#top)]\n", "\n", "Let us start by importing the basic AbiPy modules we have already used in the other examples:" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "from __future__ import division, print_function, unicode_literals\n", "\n", "import warnings \n", "warnings.filterwarnings(\"ignore\") # Ignore warnings\n", "\n", "from abipy import abilab\n", "abilab.enable_notebook() # This line tells AbiPy we are running inside a notebook\n", "\n", "import abipy.data as abidata\n", "\n", "# This line configures matplotlib to show figures embedded in the notebook, \n", "# instead of popping up a new window. \n", "%matplotlib notebook" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To open a DDB file, use the high-level interface provided by abiopen:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": true }, "outputs": [], "source": [ "#ddb_filepath = abidata.ref_file(\"mp-1009129-9x9x10q_ebecs_DDB\")\n", "#ddb = abilab.abiopen(ddb_filepath)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A DdbFile has a structure object:" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "#print(ddb.structure) # Lengths in Angstrom." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If you are a terminal aficionado, remember that one can use the \n", "[abiopen.py](http://abinit.github.io/abipy/scripts/abiopen.html) script \n", "to open the DDB file directly from the shell and generate a jupyter notebook with the `-nb` option.\n", "For a quick visualization script use [abiview.py](http://abinit.github.io/abipy/scripts/abiview.html)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Invoking Anaddb from the DdbFile object \n", "[[back to top](#top)]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The `DdbFile` object provides specialized methods to invoke anaddb and \n", "compute important physical properties such as the phonon band structure, the phonon density of states etc.\n", "All these methods have a name that begins with the `ana*` prefix followed by a verb (`anaget`, `anacompare`).\n", "These specialized methods \n", "\n", "- build the anaddb input file \n", "- run anaddb\n", "- parse the netcdf files produced by the Fortran code\n", "- build and return [AbiPy objects](http://abinit.github.io/abipy/api/dfpt_api.html) that can be used to plot/analyze the data.\n", "\n", "Note that in order to run anaddb from AbiPy, you need a manager.yml with configuration options.\n", "For further details, please consult the \n", "[TaskManager documentation](http://abinit.github.io/abipy/workflows/taskmanager.html).\n", "\n", "The python API is flexible and exposes several anaddb input variables.\n", "The majority of the arguments have default values covering the most common cases\n", "so that you will need to specify these arguments explicitly only if the default behavior does not suit your needs.\n", "The most important parameters to remember are:\n", "\n", "* **ndivsm**: Number of divisions used for the smallest segment of the high-symmetry q-path\n", "* **nqsmall**: Defines the q-mesh for the phonon DOS in terms of\n", " the number of divisions used to sample the smallest reciprocal lattice vector. 0 to disable DOS computation\n", "* **lo_to_splitting**: Activate the computation of the frequencies in the $q\\rightarrow 0$ with the inclusion of the non-analytical term (requires **dipdip** 1 and DDB with $Z^*_{\\kappa,\\alpha\\beta}$ and $\\epsilon^{\\infty}_{\\alpha\\beta}$).\n", "\n", "The high-symmetry q-path is automatically selected assuming the structure\n", "fulfills the conventions introduced by [Setyawan and Curtarolo](https://arxiv.org/abs/1004.2974)\n", "but you can also specify your own q-path if needed." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Plotting phonon bands and DOS\n", "[[back to top](#top)]\n", "\n", "To compute phonon bands and DOS, use:" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Call anaddb to compute phonon bands and DOS. Return PHBST and PHDOS netcdf files.\n", "#phbstnc, phdosnc = ddb.anaget_phbst_and_phdos_files(\n", "# ndivsm=20, nqsmall=20, lo_to_splitting=True, asr=2, chneut=1, dipdip=1, dos_method=\"tetra\")\n", "\n", "# Extract phbands and phdos from the netcdf object.\n", "#phbands = phbstnc.phbands\n", "#phdos = phdosnc.phdos" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let us have a look at the high symmetry q-path automatically selected by AbiPy with:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Using `DdbRobot` to perform convergence studies\n", "[[back to top](#top)]\n", "\n", "A `DddRobot` receives a list of DDB files and provides methods \n", "to construct [pandas dataframes](https://pandas.pydata.org/pandas-docs/stable/10min.html)\n", "and analyze the results of multiple calculations.\n", "DDbRobots, in particular, are extremely useful to study the convergence of the phonon frequencies with respect to some computational parameters e.g. the number of k-points and the electronic smearing in metallic systems.\n", "\n", "In this example, we are interested in the effect of the k-point sampling and the smearing parameter\n", "on the vibrational properties of magnesium diboride.\n", "$MgB_2$ is a metallic system with a critical temperature of 39 K that is the highest among conventional \n", "(phonon-mediated) superconductors. \n", "We use precomputed DDB files obtained by running GS+DFPT calculations with different values \n", "of `nkpt` and `tsmear`. \n", "\n", "Let's build our `DdbRobot` object with:" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "#import os\n", "#paths = [\n", "# #\"mgb2_444k_0.01tsmear_DDB\",\n", "# #\"mgb2_444k_0.02tsmear_DDB\",\n", "# #\"mgb2_444k_0.04tsmear_DDB\",\n", "# \"mgb2_888k_0.01tsmear_DDB\",\n", "# \"mgb2_888k_0.02tsmear_DDB\",\n", "# \"mgb2_888k_0.04tsmear_DDB\",\n", "# \"mgb2_121212k_0.01tsmear_DDB\",\n", "# \"mgb2_121212k_0.02tsmear_DDB\",\n", "# \"mgb2_121212k_0.04tsmear_DDB\",\n", "#]\n", "#\n", "#paths = [os.path.join(abidata.dirpath, \"refs\", \"mgb2_phonons_nkpt_tsmear\", f) for f in paths]\n", "#\n", "#robot = abilab.DdbRobot.from_files(paths)\n", "#robot" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The [abicomp.py](http://abinit.github.io/abipy/scripts/abicomp.html)\n", "script provides a command line interface to build robots from a list of files/directories\n", "given as arguments\n", "\n", "\n", "The DDB files are now stored in the robot with a label constructed from the file path.\n", "These labels, however, are not very informative. In principle we would like to have a label\n", "that reflects the value of `(nkpt, tsmear)` also because these labels\n", "will be used to generate the labels in our plots.\n", "\n", "Let's fix it with a function that recomputes the labels from the metadata available in ddb.header:" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "#function = lambda ddb: \"nkpt: %s, tsmear: %.2f\" % (ddb.header[\"nkpt\"], ddb.header[\"tsmear\"])\n", "#robot.remap_labels(function);\n", "#robot" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We are usually interested in the convergence behavior with respect to one \n", "or two parameters of the calculations. \n", "Let's build a pandas dataframe with the most important parameters extracted from the DDB header:" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "#robot.get_params_dataframe()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now we tell the robot to invoke anaddb to compute the phonon bands for all DDB files.\n", "Since we are not interested in the phonon DOS, ``nqsmall`` is set to 0" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "[[back to top](#top)]" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python [default]", "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.6.1" }, "latex_envs": { "bibliofile": "biblio.bib", "cite_by": "apalike", "current_citInitial": 1, "eqLabelWithNumbers": true, "eqNumInitial": 0 } }, "nbformat": 4, "nbformat_minor": 2 }