{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Introduction\n", "\n", "Pymatgen supports reading of most common file formats, including the Crystallographic Information File and various input and output files of computational codes like VASP. However, it is often easier and quicker to directly query for structures from online sources. Though private databases such as the Inorganic Crystal Structure Database are not open, there are open sources such as the Materials Project and the Crystallographic Open Database (COD) where one can obtain crystal structures. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Materials Project\n", "\n", "Pymatgen contains a high-level interface to the Materials Project, which can be used to query for structures very easily." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "from pymatgen.ext.matproj import MPRester\n", "# Note that you will need to add your Materials API key in your .pmgrc.yaml file as \"PMG_MAPI_KEY\". \n", "# Alternatively, you will need to supply the API key as an arg to MPRester.\n", "mpr = MPRester() " ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[Structure Summary\n", "Lattice\n", " abc : 3.2910717923597561 3.2910718996250861 3.2910720568557887\n", " angles : 60.129710432884849 60.129709521376753 60.129703130390972\n", " volume : 25.279668381289056\n", " A : 2.91738857 0.097894369999999994 1.5200046599999999\n", " B : 0.96463405999999996 2.7550356100000002 1.5200046599999999\n", " C : 0.13320635 0.097894430000000004 3.28691771\n", "PeriodicSite: O (0.0000, 0.0000, 0.0000) [0.0000, 0.0000, 0.0000]\n", "PeriodicSite: Li (3.0121, 2.2136, 4.7463) [0.7502, 0.7502, 0.7502]\n", "PeriodicSite: Li (1.0031, 0.7372, 1.5806) [0.2498, 0.2498, 0.2498], Structure Summary\n", "Lattice\n", " abc : 5.1517948200000001 3.1404278300000001 5.9334081599999999\n", " angles : 90.0 90.0 90.0\n", " volume : 95.995660249910003\n", " A : 5.1517948200000001 0.0 0.0\n", " B : 0.0 3.1404278300000001 0.0\n", " C : 0.0 0.0 5.9334081599999999\n", "PeriodicSite: Li (0.0631, 0.7851, 0.9438) [0.0122, 0.2500, 0.1591]\n", "PeriodicSite: Li (0.7268, 0.7851, 3.4367) [0.1411, 0.2500, 0.5792]\n", "PeriodicSite: Li (1.8490, 2.3553, 0.4700) [0.3589, 0.7500, 0.0792]\n", "PeriodicSite: Li (2.5128, 2.3553, 3.9105) [0.4878, 0.7500, 0.6591]\n", "PeriodicSite: Li (2.6390, 0.7851, 2.0229) [0.5122, 0.2500, 0.3409]\n", "PeriodicSite: Li (3.3027, 0.7851, 5.4634) [0.6411, 0.2500, 0.9208]\n", "PeriodicSite: Li (4.4249, 2.3553, 2.4967) [0.8589, 0.7500, 0.4208]\n", "PeriodicSite: Li (5.0887, 2.3553, 4.9896) [0.9878, 0.7500, 0.8409]\n", "PeriodicSite: O (1.2677, 2.3553, 2.3664) [0.2461, 0.7500, 0.3988]\n", "PeriodicSite: O (1.3082, 0.7851, 5.3331) [0.2539, 0.2500, 0.8988]\n", "PeriodicSite: O (3.8436, 2.3553, 0.6003) [0.7461, 0.7500, 0.1012]\n", "PeriodicSite: O (3.8841, 0.7851, 3.5670) [0.7539, 0.2500, 0.6012]]\n" ] } ], "source": [ "# Querying by formula only.\n", "structures = mpr.get_structures(\"Li2O\")\n", "print(structures)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Li2 O4\n", "Li2 O1\n", "Li8 O4\n", "Li4 O4\n", "Li1 O3\n", "Li16 O16\n" ] } ], "source": [ "# Querying by chemical system only.\n", "structures = mpr.get_structures(\"Li-O\")\n", "for s in structures:\n", " print(s.formula)\n", "# A number of Li-O structures are returned with different Li:O ratios." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## COD\n", "\n", "To obtain structures from COD by the COD id, you just need to know the id. However, most sophisticated searches require that you have installed mysql given that the COD does not support a proper REST API at this time." ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": true }, "outputs": [], "source": [ "from pymatgen.ext.cod import COD\n", "cod = COD()" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Full Formula (Li8 O4)\n", "Reduced Formula: Li2O\n", "abc : 4.610000 4.610000 4.610000\n", "angles: 90.000000 90.000000 90.000000\n", "Sites (12)\n", " # SP a b c\n", "--- ---- ---- ---- ----\n", " 0 Li+ 0.25 0.25 0.25\n", " 1 Li+ 0.25 0.75 0.75\n", " 2 Li+ 0.75 0.25 0.75\n", " 3 Li+ 0.75 0.75 0.25\n", " 4 Li+ 0.75 0.75 0.75\n", " 5 Li+ 0.75 0.25 0.25\n", " 6 Li+ 0.25 0.75 0.25\n", " 7 Li+ 0.25 0.25 0.75\n", " 8 O2- 0 0 0\n", " 9 O2- 0 0.5 0.5\n", " 10 O2- 0.5 0 0.5\n", " 11 O2- 0.5 0.5 0\n" ] } ], "source": [ "structure = cod.get_structure_by_id(1010064)\n", "print(structure)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/Users/shyue/repos/pymatgen/pymatgen/io/cif.py:801: UserWarning: LI parsed as L\n", " warnings.warn(\"{} parsed as {}\".format(sym, v))\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "COD ID: 1010064, Formula: Li8 O4, Spacegroup: F m -3 m\n", "COD ID: 1011372, Formula: Li8 O4, Spacegroup: F m -3 m\n", "COD ID: 9009059, Formula: Li8 O4, Spacegroup: F m -3 m\n", "COD ID: 4311895, Formula: L8 O4, Spacegroup: F m -3 m\n", "COD ID: 1514086, Formula: Li8 O4, Spacegroup: F m -3 m\n", "COD ID: 1514087, Formula: Li8 O4, Spacegroup: F m -3 m\n", "COD ID: 1514088, Formula: Li6 O3, Spacegroup: R -3 m :H\n", "COD ID: 4121514, Formula: Li8 O4, Spacegroup: F m -3 m\n", "COD ID: 4121515, Formula: Li8 O4, Spacegroup: F m -3 m\n", "COD ID: 1514092, Formula: Li8 O4, Spacegroup: F m -3 m\n", "COD ID: 1514093, Formula: Li7.84 O4, Spacegroup: F m -3 m\n", "COD ID: 1514094, Formula: Li7.9984 O4, Spacegroup: F m -3 m\n", "COD ID: 1514095, Formula: Li8.0008 O4, Spacegroup: F m -3 m\n", "COD ID: 1514096, Formula: Li8 O4, Spacegroup: F m -3 m\n", "COD ID: 1514097, Formula: Li8.0008 O4, Spacegroup: F m -3 m\n", "COD ID: 1514098, Formula: Li8 O4, Spacegroup: F m -3 m\n" ] } ], "source": [ "structures = cod.get_structure_by_formula(\"Li2O\")\n", "for d in structures:\n", " print(\"COD ID: %d, Formula: %s, Spacegroup: %s\" % (d[\"cod_id\"], d[\"structure\"].formula, d[\"sg\"]))" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Full Formula (Li8 O4)\n", "Reduced Formula: Li2O\n", "abc : 4.610000 4.610000 4.610000\n", "angles: 90.000000 90.000000 90.000000\n", "Sites (12)\n", " # SP a b c\n", "--- ---- ---- ---- ----\n", " 0 Li+ 0.25 0.25 0.25\n", " 1 Li+ 0.25 0.75 0.75\n", " 2 Li+ 0.75 0.25 0.75\n", " 3 Li+ 0.75 0.75 0.25\n", " 4 Li+ 0.75 0.75 0.75\n", " 5 Li+ 0.75 0.25 0.25\n", " 6 Li+ 0.25 0.75 0.25\n", " 7 Li+ 0.25 0.25 0.75\n", " 8 O2- 0 0 0\n", " 9 O2- 0 0.5 0.5\n", " 10 O2- 0.5 0 0.5\n", " 11 O2- 0.5 0.5 0\n" ] } ], "source": [ "print(structures[0][\"structure\"])" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "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.6.1" } }, "nbformat": 4, "nbformat_minor": 2 }