from mp_api.client import MPRester from pymatgen.io.cif import CifWriter
# 替换成你自己的API_KEY API_KEY = "YOUR_API_KEY"
# 初始化 MPRester 客户端 with MPRester(API_KEY) as mpr: # 我们可以使用 mpr.materials.search 方法搜索 Materials Project 上的材料, # 下面是mpr.materials.search的文档,可以通过 material_ids, chemsys, crystal_system, density, # deprecated, elements, exclude_elements, formula, num_elements, num_sites, spacegroup_number, # spacegroup_symbol, task_ids, volume, num_chunks, chunk_size, all_fields, fields 参数搜索材料,可以 # 根据自己的需求选择参数 """Query core material docs using a variety of search criteria. Arguments: material_ids (str, List[str]): A single Material ID string or list of strings (e.g., mp-149, [mp-149, mp-13]). chemsys (str, List[str]): A chemical system or list of chemical systems (e.g., Li-Fe-O, Si-*, [Si-O, Li-Fe-P]). crystal_system (CrystalSystem): Crystal system of material. density (Tuple[float,float]): Minimum and maximum density to consider. deprecated (bool): Whether the material is tagged as deprecated. elements (List[str]): A list of elements. exclude_elements (List[str]): A list of elements to exclude. formula (str, List[str]): A formula including anonymized formula or wild cards (e.g., Fe2O3, ABO3, Si*). A list of chemical formulas can also be passed (e.g., [Fe2O3, ABO3]). num_elements (Tuple[int,int]): Minimum and maximum number of elements to consider. num_sites (Tuple[int,int]): Minimum and maximum number of sites to consider. spacegroup_number (int): Space group number of material. spacegroup_symbol (str): Space group symbol of the material in international short symbol notation. task_ids (List[str]): List of Materials Project IDs to return data for. volume (Tuple[float,float]): Minimum and maximum volume to consider. num_chunks (int): Maximum number of chunks of data to yield. None will yield all possible. chunk_size (int): Number of data entries per chunk. all_fields (bool): Whether to return all fields in the document. Defaults to True. fields (List[str]): List of fields in MaterialsCoreDoc to return data for. Default is material_id, last_updated, and formula_pretty if all_fields is False. Returns: ([MaterialsDoc], [dict]) List of material documents or dictionaries. """ # 在这里通过 chemsys 搜索 Fe-Si 化合物 results = mpr.materials.search(chemsys='Fe-Si', deprecated=False) # 使用列表格式传递元素
# 所有符合搜索条件的材料都会被保存在 results 中 # 下面打印每个材料的mp-id和保存晶体结构信息 for material in results: print(f"Material ID: {material.material_id}") print(f"Formula: {material.formula_pretty}") structure = material.structure cif_writer = CifWriter(structure) cif_writer.write_file(f"{material.material_id}.cif") # 使用材料的 mp-id 作为文件名