-
Notifications
You must be signed in to change notification settings - Fork 1
Tutorial 15
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.
To interact with the Materials Project programmatically, you need your personal API key.
- Go to https://next-gen.materialsproject.org/
- Create an account or log in.
- Click the โ๐ค APIโ button in the top-right corner.
- Copy your API Key โ a long string of letters and numbers.
- Keep it safe! Donโt share your key publicly.
Once you have your key, you can begin querying the Materials Project using Python.
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-idstoresult.txt
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!