Skip to content

Instantly share code, notes, and snippets.

@dwinston
Last active August 29, 2015 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dwinston/890587de75d6a75324fe to your computer and use it in GitHub Desktop.
Save dwinston/890587de75d6a75324fe to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import argparse
from pymatgen.core.structure import IStructure
from pymatgen.matproj.rest import MPRester
from pymatgen.analysis.structure_matcher import StructureMatcher,\
ElementComparator
parser = argparse.ArgumentParser(
description="""find_struct_in_mp is a script that reads structures from
files and looks for matching structures in the Materials Project and
returns the matching materials ids.""",
epilog="Author: Shyue Ping Ong"
)
parser.add_argument("filenames", metavar="filenames", type=str, nargs="+",
help="""Filenames to read structure from. The filenames can
be a CIF, POSCAR, or any other structure file supported
by pymatgen's smartio.""")
args = parser.parse_args()
#You may need to put in your API key or set your MAPI_KEY environment variable.
r = MPRester()
m = StructureMatcher(comparator=ElementComparator())
for fname in args.filenames:
s = IStructure.from_file(fname)
formula = s.composition.reduced_formula
data = r.get_data(formula, prop="structure")
matches = filter(lambda d: m.fit(s, d["structure"]), data)
print("Filename: {}".format(fname))
if matches:
print("Matching material ids:")
for d in matches:
print("- {mid} (link: http://www.materialsproject.org/materials/"
"{mid})".format(mid=d["material_id"]))
else:
print("Structure not found in Materials Project")
print
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment