I would like to use the materials project API to search materials with some properties but to restrict my search only to two-dimensional materials.
How to do it?
Web-pages for materials contain the 'Dimensionality' field in the 'Crystal Structure' section
From the API page, I found that the dimensionality can be obtained from RobocrystallogapherDoc that is accessed with the robocrys
object but whatever I tried to do with MPRester.robocrys.search
, I got errors.
How to use it? Where to get information? I searched on the API page, the MP docs page, MP Workshop, another MP docs, Robocrystallographer github, Robocrystallographer paper - they all contain very little info on how to use the materials project API, and especially Robocrystallographer.
The only thing I got working for Robocrystallographer is querying a material given its materials project ID: robocrys.get_data_by_id
.
Therefore, I first downloaded IDs for all materials into a text file (154718 materials on 23 April, 2023):
from mp_api.client import MPRester
with MPRester(api_key=api_key) as mpr:
ids = mpr.summary.search(fields=["material_id"])
with open('mp_ids.txt', 'w') as f:
for line in ids:
f.write(f"{line.material_id}\n")
This took a few seconds on GPU.
Then, I looped through this text file, used robocrys.get_data_by_id
to get only those IDs that Robocrystallographer identifies as 2D materials, and saved them in another text file:
from mp_api.client import MPRester
with open('mp_ids.txt', 'r') as f:
mpids = [line.rstrip('\n') for line in f.readlines()]
with open('mp_2dids.txt', 'w') as f:
with MPRester(api_key=api_key) as mpr:
for mpid in mpids:
mpd = mpr.robocrys.get_data_by_id(mpid)
try:
dim = mpd.condensed_structure.dimensionality
except:
continue
if dim != None and dim == 2:
f.write(f"{mpid}\n")
This took 14 hours on GPU (8967 2D materials on 23 April, 2023).
Is there a 'more civilized' way to do it, without downloading all IDs to text files, and redoing this every time they add new materials to the materials project database?