{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "# useful to autoreload the module without restarting the kernel\n", "%load_ext autoreload\n", "%autoreload 2" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "from mppi import InputFiles as I" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Tutorial for the PwInput class" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This tutorial describes the main features and the usage of the PwInput class that enables to create and\n", "manage the input files for the QuantumESPRESSO computations" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Setting up an input from scratch" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Create an empty input object from scratch. If not parameters are provided some keys are init from the _default_\n", "data member of the class" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'control': {'calculation': \"'scf'\",\n", " 'verbosity': \"'high'\",\n", " 'prefix': \"'pwscf'\",\n", " 'outdir': \"'./'\"},\n", " 'system': {'force_symmorphic': '.false.'},\n", " 'electrons': {'diago_full_acc': '.false.'},\n", " 'ions': {},\n", " 'cell': {},\n", " 'atomic_species': {},\n", " 'atomic_positions': {},\n", " 'kpoints': {},\n", " 'cell_parameters': {}}" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "inp = I.PwInput()\n", "inp" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The input object can be initialized passing arguments in the constuctor both using the kwargs syntax and/or passing a dictionary.\n", "These operations override the _default_ dictionary " ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'control': {'calculation': 'nscf'},\n", " 'system': {'force_symmorphic': '.false.'},\n", " 'electrons': {'diago_full_acc': '.false.'},\n", " 'ions': {},\n", " 'cell': {},\n", " 'atomic_species': {},\n", " 'atomic_positions': {},\n", " 'kpoints': {},\n", " 'cell_parameters': {}}" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "inp = I.PwInput(control = {'calculation' : 'nscf'})\n", "inp" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "imp_dict = {'system' : {'ntyp' : 1}}" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'control': {'calculation': 'scf'},\n", " 'system': {'ntyp': 1},\n", " 'electrons': {'diago_full_acc': '.false.'},\n", " 'ions': {},\n", " 'cell': {},\n", " 'atomic_species': {},\n", " 'atomic_positions': {},\n", " 'kpoints': {},\n", " 'cell_parameters': {}}" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "inp = I.PwInput(control = {'calculation' : 'scf'}, **imp_dict)\n", "inp" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The attribute of the object can be updated using the standard procedure for python dictionaries, for instance" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'control': {'calculation': 'scf', 'verbosity': \"'high'\"},\n", " 'system': {'ntyp': 1},\n", " 'electrons': {'diago_full_acc': '.false.'},\n", " 'ions': {},\n", " 'cell': {},\n", " 'atomic_species': {},\n", " 'atomic_positions': {},\n", " 'kpoints': {},\n", " 'cell_parameters': {}}" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "inp['control'].update({'verbosity' : \"'high'\"})\n", "inp" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "However it is more powerful to use the specific methods provided in the class (and other can be defined). For instance" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'control': {'calculation': \"'scf'\",\n", " 'verbosity': \"'high'\",\n", " 'prefix': \"'si_scf_pref'\",\n", " 'outdir': \"'./'\",\n", " 'pseudo_dir': \"'pseudos'\"},\n", " 'system': {'force_symmorphic': '.false.',\n", " 'ibrav': 2,\n", " 'celldm(1)': 10.3,\n", " 'ntyp': '1',\n", " 'nat': '2'},\n", " 'electrons': {'diago_full_acc': '.false.', 'conv_thr': 1e-08},\n", " 'ions': {},\n", " 'cell': {},\n", " 'atomic_species': {'Si': ['1.0', 'Si.pbe-mt_fhi.UPF']},\n", " 'atomic_positions': {'type': 'alat',\n", " 'values': [['Si', [0.0, 0.0, 0.0]], ['Si', [0.25, 0.25, 0.25]]]},\n", " 'kpoints': {'type': 'automatic', 'values': ([4, 4, 4], [0.0, 0.0, 0.0])},\n", " 'cell_parameters': {}}" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "inp = I.PwInput()\n", "inp.set_scf()\n", "inp.set_prefix('si_scf_pref')\n", "inp.set_pseudo_dir(pseudo_dir='pseudos')\n", "# specific methods for the cell have still to be added\n", "inp.set_lattice(ibrav=2,celldm1=10.3)\n", "\n", "# Set the atoms type and positions\n", "inp.add_atom('Si','Si.pbe-mt_fhi.UPF')\n", "# add other atoms if needed, then set the number of atoms in the cell.\n", "# This method sets the ntyp variable equal to the number of atoms added\n", "# with the add_atom method\n", "inp.set_atoms_number(2)\n", "inp.set_atomic_positions([['Si',[0.,0.,0.,]],['Si',[0.25,0.25,0.25]]])\n", "\n", "# Set the sampling of kpoints\n", "inp.set_kpoints(type='automatic',points=[4,4,4])\n", "inp" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The usage of the methods is described in the documentation and can be easily accessed as follows" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\u001b[0;31mSignature:\u001b[0m \u001b[0minp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mset_pseudo_dir\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mpseudo_dir\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m'pseudos'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mabs_path\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mFalse\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mDocstring:\u001b[0m\n", "Set the position of the folder with the pseudo-potentials.\n", "If `abs_path` is True the path is converted in a absolute path. In this way it\n", "is possible to provide a relative path (expressed from the root of the folder where the notebook\n", "is located) and the pseudo location can be found from an arbitrary folder.\n", "\n", "Args:\n", " pseudo_dir (:py:class:'string') : (relative) path of the folder with the pseduopotentials\n", "\n", "Note:\n", " If the folder tree contains blank spaces, QuantumESPRESSO cannot be able to find the pseudo, in this\n", " cas it is safer to provide a relative path (expressed from the folder where the input file is\n", " written)\n", "\u001b[0;31mFile:\u001b[0m ~/Applications/MPPI/mppi/InputFiles/PwInput.py\n", "\u001b[0;31mType:\u001b[0m method\n" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "inp.set_pseudo_dir?" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\u001b[0;31mSignature:\u001b[0m\n", "\u001b[0minp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mset_kpoints\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0mtype\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m'automatic'\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0mpoints\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1.0\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m1.0\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m1.0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0mshift\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0.0\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m0.0\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m0.0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m \u001b[0mklist\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", "\u001b[0;34m\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mDocstring:\u001b[0m\n", "Define the sampling of the Brillouin zone.\n", "\n", "Args:\n", " type (:py:class:`string`) : type of sampling (automatic, tpiba, tpiba_b,...)\n", " points (:py:class:`list`) : number of kpoints in the x,y,z directions. Used only if\n", " the type variable is set to `automatic`\n", " shift (:py:class:`list`) : shifts in the x,y,z directions. Used only if the\n", " type varible is set to `automatic`\n", " klist(list) : list with the structure:\n", " [[k1x,k1y,k1z,w1],[k2x,k2y,k2z,w2],....]\n", " Used if type variable is not se to `automatic`\n", "\u001b[0;31mFile:\u001b[0m ~/Applications/MPPI/mppi/InputFiles/PwInput.py\n", "\u001b[0;31mType:\u001b[0m method\n" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "inp.set_kpoints?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Once completed the input object can be converted to string with the correct format of the pw input files" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "&control\n", " calculation = 'scf'\n", " outdir = './'\n", " prefix = 'si_scf_pref'\n", " pseudo_dir = 'pseudos'\n", " verbosity = 'high'\n", "/&end\n", "&system\n", " celldm(1) = 10.3\n", " force_symmorphic = .false.\n", " ibrav = 2\n", " nat = 2\n", " ntyp = 1\n", "/&end\n", "&electrons\n", " conv_thr = 1e-08\n", " diago_full_acc = .false.\n", "/&end\n", "ATOMIC_SPECIES\n", " Si 1.0 Si.pbe-mt_fhi.UPF\n", "ATOMIC_POSITIONS { alat }\n", " Si 0.0000000000 0.0000000000 0.0000000000\n", " Si 0.2500000000 0.2500000000 0.2500000000\n", "K_POINTS { automatic }\n", " 4 4 4 0 0 0\n" ] } ], "source": [ "inp_tostring = inp.convert_string()\n", "print(inp_tostring)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "and finally it can be written on file" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [], "source": [ "inp.write('IO_files/pw_from-scratch.in')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Build and input starting from an existing file" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Consider a second example in which the input is initialized from an existing input file" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'control': {'calculation': \"'scf'\",\n", " 'verbosity': \"'high'\",\n", " 'prefix': \"'ecut_40-k_4'\",\n", " 'outdir': \"'./'\",\n", " 'pseudo_dir': \"'../pseudos'\"},\n", " 'system': {'force_symmorphic': '.false.',\n", " 'occupations': \"'fixed'\",\n", " 'ibrav': 2,\n", " 'celldm(1)': 10.677,\n", " 'ntyp': 2,\n", " 'nat': 2,\n", " 'ecutwfc': 40},\n", " 'electrons': {'diago_full_acc': '.false.', 'conv_thr': 1e-08},\n", " 'ions': {},\n", " 'cell': {},\n", " 'atomic_species': {'Ga': [69.72, 'Ga_hamlu.fhi.UPF'],\n", " 'As': [74.92, 'As_hamlu.fhi.UPF']},\n", " 'atomic_positions': {'type': 'alat',\n", " 'values': [['Ga', [0.0, 0.0, 0.0]], ['As', [0.25, 0.25, 0.25]]]},\n", " 'kpoints': {'type': 'automatic',\n", " 'values': ([4.0, 4.0, 4.0], [0.0, 0.0, 0.0])},\n", " 'cell_parameters': {},\n", " 'file': 'IO_files/gaas_scf.in'}" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "inp = I.PwInput(file='IO_files/gaas_scf.in')\n", "inp" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The variables can be modified, for instance we can modify the input to perform a nscf computation with 12 bands and \n", "a given k-path" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [], "source": [ "G = [0.,0.,0.]\n", "X = [1.,0.,0.]\n", "L = [0.5,0.5,0.5]\n", "W = [1.0,0.5,0.]" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[[0.0, 0.0, 0.0, 30],\n", " [1.0, 0.0, 0.0, 30],\n", " [0.5, 0.5, 0.5, 30],\n", " [1.0, 0.5, 0.0, 0]]" ] }, "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from mppi.Utilities import build_kpath\n", "kpath = build_kpath(G,X,L,W,numstep=30)\n", "kpath" ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [], "source": [ "inp.set_nscf(12)\n", "inp.set_kpoints(type='tpiba_b',klist=kpath)" ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "&control\n", " calculation = 'nscf'\n", " outdir = './'\n", " prefix = 'ecut_40-k_4'\n", " pseudo_dir = '../pseudos'\n", " verbosity = 'high'\n", "/&end\n", "&system\n", " celldm(1) = 10.677\n", " ecutwfc = 40\n", " force_symmorphic = .false.\n", " ibrav = 2\n", " nat = 2\n", " nbnd = 12\n", " ntyp = 2\n", " occupations = 'fixed'\n", "/&end\n", "&electrons\n", " conv_thr = 1e-08\n", " diago_full_acc = .false.\n", "/&end\n", "ATOMIC_SPECIES\n", " Ga 69.72 Ga_hamlu.fhi.UPF\n", " As 74.92 As_hamlu.fhi.UPF\n", "ATOMIC_POSITIONS { alat }\n", " Ga 0.0000000000 0.0000000000 0.0000000000\n", " As 0.2500000000 0.2500000000 0.2500000000\n", "K_POINTS { tpiba_b }\n", "4\n", " 0.00000000 0.00000000 0.00000000 30.00000000 \n", " 1.00000000 0.00000000 0.00000000 30.00000000 \n", " 0.50000000 0.50000000 0.50000000 30.00000000 \n", " 1.00000000 0.50000000 0.00000000 0.00000000 \n" ] } ], "source": [ "inp_tostring = inp.convert_string()\n", "print(inp_tostring)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Finally a last example in which the lattice is specified through the 'cell_parameters' key" ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'control': {'calculation': \"'nscf'\",\n", " 'verbosity': \"'high'\",\n", " 'prefix': \"'ecut\",\n", " 'outdir': \"'./'\",\n", " 'pseudo_dir': \"'../pseudos'\"},\n", " 'system': {'force_symmorphic': '.false.',\n", " 'occupations': \"'smearing'\",\n", " 'smearing': \"'fermi-dirac'\",\n", " 'degauss': '0.0036749326',\n", " 'ibrav': '0',\n", " 'ntyp': '1',\n", " 'nat': '2',\n", " 'ecutwfc': '100',\n", " 'nbnd': '8'},\n", " 'electrons': {'diago_full_acc': '.false.', 'conv_thr': '1e-08'},\n", " 'ions': {},\n", " 'cell': {},\n", " 'atomic_species': {'C': ['12.011', 'C_pbe-20082014.UPF']},\n", " 'atomic_positions': {'type': 'angstrom',\n", " 'values': [['C', [0.0, 0.0, 0.0]], ['C', [0.0, 1.42, 0.0]]]},\n", " 'kpoints': {'type': 'tpiba_b',\n", " 'values': [[0.0, 0.0, 0.0, 40.0],\n", " [0.5, 0.28867513, 0.0, 40.0],\n", " [0.66666667, 0.0, 0.0, 40.0],\n", " [0.0, 0.0, 0.0, 0.0]]},\n", " 'cell_parameters': {'type': 'angstrom',\n", " 'values': [[2.4595121467, 0.0, 0.0],\n", " [1.2297560734, 2.13, 0.0],\n", " [0.0, 0.0, 10.0]]},\n", " 'file': 'IO_files/graphene_nscf.in'}" ] }, "execution_count": 40, "metadata": {}, "output_type": "execute_result" } ], "source": [ "inp = I.PwInput(file='IO_files/graphene_nscf.in')\n", "inp" ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "&control\n", " calculation = 'nscf'\n", " outdir = './'\n", " prefix = 'ecut\n", " pseudo_dir = '../pseudos'\n", " verbosity = 'high'\n", "/&end\n", "&system\n", " degauss = 0.0036749326\n", " ecutwfc = 100\n", " force_symmorphic = .false.\n", " ibrav = 0\n", " nat = 2\n", " nbnd = 8\n", " ntyp = 1\n", " occupations = 'smearing'\n", " smearing = 'fermi-dirac'\n", "/&end\n", "&electrons\n", " conv_thr = 1e-08\n", " diago_full_acc = .false.\n", "/&end\n", "ATOMIC_SPECIES\n", " C 12.011 C_pbe-20082014.UPF\n", "ATOMIC_POSITIONS { angstrom }\n", " C 0.0000000000 0.0000000000 0.0000000000\n", " C 0.0000000000 1.4200000000 0.0000000000\n", "K_POINTS { tpiba_b }\n", "4\n", " 0.00000000 0.00000000 0.00000000 40.00000000 \n", " 0.50000000 0.28867513 0.00000000 40.00000000 \n", " 0.66666667 0.00000000 0.00000000 40.00000000 \n", " 0.00000000 0.00000000 0.00000000 0.00000000 \n", "CELL_PARAMETERS angstrom\n", " 2.4595121467 0.0000000000 0.0000000000 \n", " 1.2297560734 2.1300000000 0.0000000000 \n", " 0.0000000000 0.0000000000 10.0000000000 \n" ] } ], "source": [ "print(inp.convert_string())" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.10.6" } }, "nbformat": 4, "nbformat_minor": 4 }