Skip to content

Tutorial 15

Lee Burton edited this page Jul 16, 2025 · 19 revisions

๐Ÿงช The Materials Project

The Materials Project is a world-leading database of all known materials simulated using VASP. Itโ€™s an incredible resource for finding structural, energetic, and electronic properties of materials โ€” both real and hypothetical.

๐Ÿ‘‰ Fun fact: The Materials Project is built using pymatgen!
Weโ€™ll use pymatgen ourselves in this tutorial to explore case studies and automate our own workflows.


๐Ÿ”‘ Get Your API Key

To interact with the Materials Project programmatically, you need your personal API key.

๐Ÿ“‹ Steps:

  1. Go to https://next-gen.materialsproject.org/
  2. Create an account or log in.
  3. Click the โ€œ๐Ÿ‘ค APIโ€ button in the top-right corner.
  4. Copy your API Key โ€” a long string of letters and numbers.
  5. Keep it safe! Donโ€™t share your key publicly.

Once you have your key, you can begin querying the Materials Project using Python.


๐Ÿ” Querying the Materials Project

Hereโ€™s an example of how to use pymatgen to search for stable oxygen-containing compounds that are known from experiments and have small-to-moderate unit cells:

from mp_api.client import MPRester

API_KEY = '<YOUR API KEY>'  # Replace with your actual API key

with MPRester(API_KEY) as mpr:
    docs = mpr.summary.search(
        elements=["O"],              # Must contain Oxygen
        is_stable=True,              # Thermodynamically stable
        theoretical=False,           # Reported from experiment
        nsites=["1", "40"],          # Unit cell has 1โ€“40 atoms
        fields=["material_id"]       # Only return material IDs
    )

with open('result.txt', 'w') as f:
    for doc in docs:
        f.write(f"{doc.material_id}\n")

๐Ÿ“‚ This script will:

  • Query the database
  • Filter results based on your criteria
  • Write all matching mp-ids to result.txt

โš™๏ธ Automatically Create VASP Input Files

Now that youโ€™ve downloaded some material IDs from the Materials Project, letโ€™s generate full VASP input folders for each one:

from mp_api.client import MPRester
from pymatgen.io.vasp.inputs import Poscar
from pymatgen.io.vasp.sets import MPRelaxSet
import os

API_KEY = '<YOUR API KEY>'  # Replace with your real key

# Load list of material IDs
with open('result.txt') as f:
    lines = f.read().splitlines()

# Optionally sort or preprocess lines if needed
newlines = [line.split() for line in lines]
newlines.sort(key=lambda x: int(x[1]))

# Create VASP input sets
for newline in newlines:
    with MPRester(API_KEY) as mpr:
        struct = mpr.get_structure_by_material_id(newline[0])
        poscar = Poscar(structure=struct, comment=newline[0])
        print(poscar)
        try:
            os.makedirs(newline[0])
        except:
            pass
        relax_set = MPRelaxSet(structure=struct)
        relax_set.write_input(output_dir="./" + newline[0])

This script: โœ… Downloads the structure
โœ… Creates a POSCAR
โœ… Builds a full INCAR, KPOINTS, POTCAR, and POSCAR in a folder named after the material ID


๐ŸŽ‰ Congratulations! Youโ€™ve learned how to use the Materials Project API and generate ready-to-run VASP jobs!

๐Ÿ‘‰ Youโ€™re now ready for Tutorial 16!

Clone this wiki locally