{ "cells": [ { "cell_type": "markdown", "source": [ "# Creating supercells with pymatgen\n", "\n", "The [Pymatgen](https://pymatgen.org/) python library allows to setup\n", "solid-state calculations using a flexible set of classes as well as an API\n", "to an online data base of structures. Its `Structure` and `Lattice`\n", "objects are directly supported by the DFTK `load_atoms` and `load_lattice`\n", "functions, such that DFTK may be readily used to run calculation on systems\n", "defined in pymatgen. Using the `pymatgen_structure` function a conversion\n", "from DFTK to pymatgen structures is also possible. In the following we\n", "use this to create a silicon supercell and find its LDA ground state\n", "using direct minimisation. To run this example Julia's `PyCall` package\n", "needs to be able to find an installation of `pymatgen`." ], "metadata": {} }, { "cell_type": "markdown", "source": [ "First we setup the silicon lattice in DFTK." ], "metadata": {} }, { "outputs": [], "cell_type": "code", "source": [ "using DFTK\n", "\n", "a = 10.263141334305942 # Lattice constant in Bohr\n", "lattice = a / 2 .* [[0 1 1.]; [1 0 1.]; [1 1 0.]]\n", "Si = ElementPsp(:Si, psp=load_psp(\"hgh/lda/Si-q4\"))\n", "atoms = [Si => [ones(3)/8, -ones(3)/8]];" ], "metadata": {}, "execution_count": 1 }, { "cell_type": "markdown", "source": [ "Next we make a `[2, 2, 2]` supercell using pymatgen" ], "metadata": {} }, { "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "┌ Warning: `vendor()` is deprecated, use `BLAS.get_config()` and inspect the output instead\n", "│ caller = npyinitialize() at numpy.jl:67\n", "└ @ PyCall /home/runner/.julia/packages/PyCall/L0fLP/src/numpy.jl:67\n" ] } ], "cell_type": "code", "source": [ "pystruct = pymatgen_structure(lattice, atoms)\n", "pystruct.make_supercell([2, 2, 2])\n", "lattice = load_lattice(pystruct)\n", "atoms = [Si => [s.frac_coords for s in pystruct.sites]];" ], "metadata": {}, "execution_count": 2 }, { "cell_type": "markdown", "source": [ "Setup an LDA model and discretize using\n", "a single k-point and a small `Ecut` of 5 Hartree." ], "metadata": {} }, { "outputs": [ { "output_type": "execute_result", "data": { "text/plain": "PlaneWaveBasis discretization:\n Ecut : 5.0 Ha\n fft_size : (32, 32, 32)\n kgrid type : Monkhorst-Pack\n kgrid : [1, 1, 1]\n num. irred. kpoints : 1\n\n Discretized Model(lda_xc_teter93, 3D):\n lattice (in Bohr) : [0 , 10.2631 , 10.2631 ]\n [10.2631 , 0 , 10.2631 ]\n [10.2631 , 10.2631 , 0 ]\n unit cell volume : 2162.1 Bohr³\n \n atoms : Si₁₆\n atom potentials : ElementPsp(Si, psp=hgh/lda/si-q4)\n \n num. electrons : 64\n spin polarization : none\n temperature : 0 Ha\n \n terms : Kinetic()\n AtomicLocal()\n AtomicNonlocal()\n Ewald()\n PspCorrection()\n Hartree()\n Xc(:lda_xc_teter93)" }, "metadata": {}, "execution_count": 3 } ], "cell_type": "code", "source": [ "model = model_LDA(lattice, atoms)\n", "basis = PlaneWaveBasis(model; Ecut=5, kgrid=(1, 1, 1))" ], "metadata": {}, "execution_count": 3 }, { "cell_type": "markdown", "source": [ "Find the ground state using direct minimisation (always using SCF is boring ...)" ], "metadata": {} }, { "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Iter Function value Gradient norm \n", " 0 1.116062e+02 1.517882e+00\n", " * time: 0.8374879360198975\n", " 1 1.074000e+01 8.582361e-01\n", " * time: 2.374027967453003\n", " 2 -1.134891e+01 9.564115e-01\n", " * time: 3.0860068798065186\n", " 3 -3.392864e+01 7.674659e-01\n", " * time: 4.179577827453613\n", " 4 -4.744996e+01 5.731013e-01\n", " * time: 5.210864782333374\n", " 5 -5.688425e+01 2.348844e-01\n", " * time: 6.2739949226379395\n", " 6 -5.983825e+01 1.758409e-01\n", " * time: 6.982828855514526\n", " 7 -6.094845e+01 5.582174e-02\n", " * time: 7.708448886871338\n", " 8 -6.133676e+01 9.125249e-02\n", " * time: 8.413647890090942\n", " 9 -6.160948e+01 3.923540e-02\n", " * time: 9.125579833984375\n", " 10 -6.180592e+01 2.782618e-02\n", " * time: 9.793018817901611\n", " 11 -6.195302e+01 2.462728e-02\n", " * time: 10.50530195236206\n", " 12 -6.201198e+01 1.780274e-02\n", " * time: 11.215266942977905\n", " 13 -6.207398e+01 1.724701e-02\n", " * time: 11.910501956939697\n", " 14 -6.210939e+01 1.915338e-02\n", " * time: 12.623705863952637\n", " 15 -6.215032e+01 1.554781e-02\n", " * time: 13.3208909034729\n", " 16 -6.218157e+01 1.261166e-02\n", " * time: 14.052361965179443\n", " 17 -6.220570e+01 1.017819e-02\n", " * time: 14.727994918823242\n", " 18 -6.222428e+01 7.246597e-03\n", " * time: 15.435925006866455\n", " 19 -6.223493e+01 7.214679e-03\n", " * time: 16.158989906311035\n", " 20 -6.224150e+01 6.314786e-03\n", " * time: 16.858760833740234\n", " 21 -6.224625e+01 4.292858e-03\n", " * time: 17.566049814224243\n", " 22 -6.225074e+01 4.058197e-03\n", " * time: 18.272429943084717\n", " 23 -6.225429e+01 3.602924e-03\n", " * time: 18.96968388557434\n", " 24 -6.225701e+01 2.957667e-03\n", " * time: 19.662516832351685\n", " 25 -6.225901e+01 2.542853e-03\n", " * time: 20.36894178390503\n", " 26 -6.226025e+01 1.819695e-03\n", " * time: 21.061124801635742\n", " 27 -6.226095e+01 1.054844e-03\n", " * time: 21.76589798927307\n", " 28 -6.226130e+01 1.066389e-03\n", " * time: 22.4663348197937\n", " 29 -6.226145e+01 6.920819e-04\n", " * time: 23.182384967803955\n", " 30 -6.226153e+01 5.939634e-04\n", " * time: 23.892266988754272\n", " 31 -6.226158e+01 4.624261e-04\n", " * time: 24.60138487815857\n", " 32 -6.226161e+01 4.021984e-04\n", " * time: 25.302117824554443\n", " 33 -6.226163e+01 3.950532e-04\n", " * time: 25.98001480102539\n", " 34 -6.226164e+01 2.510007e-04\n", " * time: 26.66957998275757\n", " 35 -6.226165e+01 2.122974e-04\n", " * time: 27.343291997909546\n", " 36 -6.226166e+01 1.651182e-04\n", " * time: 28.031521797180176\n", " 37 -6.226166e+01 9.719340e-05\n", " * time: 28.719368934631348\n", " 38 -6.226166e+01 8.177096e-05\n", " * time: 29.411049842834473\n", " 39 -6.226166e+01 7.317111e-05\n", " * time: 30.11129593849182\n", " 40 -6.226167e+01 4.129686e-05\n", " * time: 30.806140899658203\n", " 41 -6.226167e+01 3.298540e-05\n", " * time: 31.497408866882324\n", " 42 -6.226167e+01 3.020884e-05\n", " * time: 32.1739239692688\n", " 43 -6.226167e+01 1.806679e-05\n", " * time: 32.90002989768982\n", " 44 -6.226167e+01 1.691626e-05\n", " * time: 33.58825898170471\n", " 45 -6.226167e+01 1.223037e-05\n", " * time: 34.267826795578\n", " 46 -6.226167e+01 1.068305e-05\n", " * time: 34.949291944503784\n", " 47 -6.226167e+01 1.002592e-05\n", " * time: 35.65868401527405\n", " 48 -6.226167e+01 7.064774e-06\n", " * time: 36.35013699531555\n", " 49 -6.226167e+01 5.377785e-06\n", " * time: 37.05099892616272\n", " 50 -6.226167e+01 3.751058e-06\n", " * time: 37.77816987037659\n", " 51 -6.226167e+01 2.727381e-06\n", " * time: 38.58723497390747\n" ] } ], "cell_type": "code", "source": [ "scfres = direct_minimization(basis, tol=1e-5);" ], "metadata": {}, "execution_count": 4 }, { "outputs": [ { "output_type": "execute_result", "data": { "text/plain": "Energy breakdown (in Ha):\n Kinetic 25.7671060\n AtomicLocal -18.8557714\n AtomicNonlocal 14.8522689\n Ewald -67.1831486\n PspCorrection -2.3569765\n Hartree 4.8485372 \n Xc -19.3336820\n\n total -62.261666460506" }, "metadata": {}, "execution_count": 5 } ], "cell_type": "code", "source": [ "scfres.energies" ], "metadata": {}, "execution_count": 5 } ], "nbformat_minor": 3, "metadata": { "language_info": { "file_extension": ".jl", "mimetype": "application/julia", "name": "julia", "version": "1.7.1" }, "kernelspec": { "name": "julia-1.7", "display_name": "Julia 1.7.1", "language": "julia" } }, "nbformat": 4 }