使用MPAPI批量下载结构文件

1、需要安装Anaconda

2、然后确认pip3是不是在Anaconda中,然后就可以通过pip3 在自己用户下安装pymatgenMP_api

3、代码如下,参考点

3.1 通过化学式下载。APIKEY只有你注册了就有

######这个是通过化学式下载的,只有搜索到的元素

import pymatgen.core as mg
API_KEY = '填入网站给你的APIKEY'
import itertools
import random
import numpy as np
from mp_api.client import MPRester
########导入库函数
num_type = 3 ###元素种类
num_rndom = 50 ###随机取样数

all_symbols = ["H", "Li", "Be","C", "N", "O", "Na",
            "Mg", "Al", "Si", "P", "S", "K", "Ca", "Sc", "Ti",
            "V", "Cr", "Mn", "Fe", "Co", "Ni", "Cu", "Zn", "Ga", "Ge", "As",
            "Se", "Br", "Rb", "Sr", "Y", "Zr", "Nb", "Mo", "Tc", "Ru",
            "Rh", "Pd", "Ag", "Cd", "In", "Sn", "Sb", "Te", "Cs",
            "Ba", "La", "Ta", "W", "Re", "Os"]
complx = list(itertools.combinations(all_symbols, num_type)) ###多元化物

complx_picked=random.sample(complx,num_rndom)  #####随机抽样300种
for i in range(num_rndom) :  #####
    complx_picked[i] = "-".join(complx_picked[i]) ####以-拼接起来

#print(complx_picked)
#Ements = ["O","Ti"]
for Ements in complx_picked :
    with MPRester(API_KEY) as mpr:
         docs =  mpr.materials.summary.search(
              chemsys=Ements, fields=["material_id","formula_pretty","structure"]
         )
#material_id = docs[0].material_id
#structure   =  docs[0].is_stable
#print(material_id,structure)
    for idoc in docs[:10]:  # 保存前10个结构到cif文件
        idoc.structure.to(idoc.formula_pretty+idoc.material_id+".cif")

3.2 通过包含元素下载

import pymatgen.core as mg
API_KEY = '填入网站给你的APIKEY'
import itertools
import random
import numpy as np
from mp_api.client import MPRester
########导入库函数

num_type = 2 ###元素种类
num_rndom = 50 ###随机取样数

all_symbols = ["H", "Li", "Be","C", "N", "O", "Na",
            "Mg", "Al", "Si", "P", "S", "K", "Ca", "Sc", "Ti",
            "V", "Cr", "Mn", "Fe", "Co", "Ni", "Cu", "Zn", "Ga", "Ge", "As",
            "Se", "Br", "Rb", "Sr", "Y", "Zr", "Nb", "Mo", "Tc", "Ru",
            "Rh", "Pd", "Ag", "Cd", "In", "Sn", "Sb", "Te", "Cs",
            "Ba", "La", "Ta", "W", "Re", "Os"]
complx = list(itertools.combinations(all_symbols, num_type)) ###多元化物

complx_picked=random.sample(complx,num_rndom)  #####随机抽样300种
#print(complx_picked)
#Ements = ["O","Ti"]
for Ements in complx_picked :
    with MPRester(API_KEY) as mpr:
         docs =  mpr.materials.summary.search(
              elements=Ements, fields=["material_id","formula_pretty","structure"]
         )
#material_id = docs[0].material_id
#structure   =  docs[0].structure
#print(material_id,structure)
    for idoc in docs[:10]:  # 保存前10个结构到cif文件
        idoc.structure.to(idoc.formula_pretty+idoc.material_id+".cif")

发表评论