I am getting a completely different structure and atomic positions when I query for the structure in pymatgen and the new mp-api than when I download the same files from the website from catalysis explorer for the bulk material. For example, mp-30811. The ones on the website seem to have more atoms and detail which is what I want. How do I get these files from the api as well? Thanks for any help, I am new to this stuff
Here is the code I had so far:
from mp_api.client import MPRester
m = MPRester(api_key=“api key”)
#Open the file and iterate over each line, each line has an mp-id
with open(‘top1k.txt’, ‘r’) as file:
for line in file:
x = line.strip()
structure = m.get_structure_by_material_id(x)
print(structure)
cif_data = structure.to(fmt=“json”)
fn = x + “.json”
with open(fn, “w”) as f:
f.write(cif_data)
@Abhinav_Maru that looks good. Are you looking for the conventional cell instead of the primitive one? If so, you can use the code below to download it directly to a CIF file:
m = MPRester(api_key=“api key”)
with open(‘top1k.txt’, ‘r’) as file:
for line in file:
x = line.strip()
structure = m.get_structure_by_material_id(x, conventional_unit_cell=True)
cif_data = structure.to(filename=f“{x}.cif”) # Dumps to a file in current dir
This was exactly what I was looking for and I think I can make these cifs work. However I was wondering out of curiosity what to do to get a cif of the interactive model on the website as well? It is not necessary for my project but I was just wondering if you knew.
Is there a way to get the cartesian coordinates instead of the fractional coordinates from the MPContribs api? If not, is there an easy way to turn the fractional coordinates into cartesian for a 500+ structures at once?
Sorry if I am bothering everyone with questions, I am still a little new to the api and the field in general. Thanks for any help!
When you use the query/download_contributions() functions in mpcontribs-client, the structures are returned as pymatgen objects. Any pymatgen structure object has a cart_coords attribute that should give you the cartesian coordinates for a structure. Please refer to the pymatgen documentation for more info. The structure object in Jason’s code snippet is a pymatgen Structure, too. HTH.