pymatgen API的使用-高通量计算初探

  1. Materials Project API申请
  2. 补充内容1
  3. 补充内容2(通过网址传输API指令)

本文章为原创,版权归作者刘锦程所有,文章转载请先取得作者的同意,非常欢迎转发文章链接!严禁以任何方式挪用本文内容,用于以盈利为目的各种活动。

相关内容

pymatgen API的使用-高通量计算初探

pymatgen自动切割表面-高通量计算初探

pymatgen自动生成表面吸附模型-高通量计算初探

pymatgen画能带图方法-高通量计算初探

如果问我最强大的计算的晶体结构数据库是什么?答案无疑是Materials Project (简称mp)。

pymatgen-api1

如果问我Materials Project最强大功能是什么?答案无疑是API。

假如把平时拿过一个体系算一下其物理化学性质,就水一篇文章的学生比作用双手干活的农民。那么使用API获取大量原始数据做高通量计算的学生就是开着自动化收割机在大农场驰骋的农场主。

纯计算的博士生怎么发nature?今年2019年,中科院物理所博士毕业生Tiantian Zhang在Nature上发了一篇题为“Catalogue of topological electronic materials”的纯计算预测文章。虽然和笔者的研究方向并不沾边,但是出于仰慕之心还是把文章粗略读了一下。其中非常关键的技术就是建立一套高通量计算自动化算法,通过研究占据态对称性与拓扑不变量之间的关系,能够高效分析非磁性材料中非平凡能带拓扑学(Nontrivial band algorithm)。基于该算法,在Materials project和ICSD等数据库上,使用API得到39519种材料,计算发现有8056种材料是拓扑非平凡的,不仅如此,通过交互式网页界面,这些被发现的材料均可被查询研究。

Nature 566, 475–479 (2019) https://www.nature.com/articles/s41586-019-0944-6

既然Materials project上有几十万种晶体,那么想要一次性得到这些材料的结构和各种性质,手动去https://materialsproject.org/ 网站上搜索不划算。实际上,Materials project提供了强大的API接口,我们用几行代码就能得到数据库里的任何已有信息。这为我们后续批处理分析提供了方便。

Materials Project API申请

第一步:注册Materials Project账号。

pymatgen-api2

第二步:产生私钥。

pymatgen-api3

第三步: 通过API得到材料的结构和性质数据。

阅读API 手册:https://materialsproject.org/docs/api#materials_.28calculated_materials_data.29

一些简单的API功能:https://pymatgen.org/usage.html

1
2
3
4
5
6
7
8
9
10
11
12
from pymatgen.ext.matproj import MPRester

with MPRester("USER_API_KEY") as m:

# Structure for material id
structure = m.get_structure_by_material_id("mp-1234")

# Dos for material id
dos = m.get_dos_by_material_id("mp-1234")

# Bandstructure for material id
bandstructure = m.get_bandstructure_by_material_id("mp-1234")

通过上面三个功能就把mp数据库里第1234号结构的,结构数据,dos和band数据得到了,分别保存在structure, dos, bandstructure这三个变量里。

想要得到一些列结构的数据,比如所有晶相的Fe2O3, 可以用如下代码实现:

1
2
3
4
5
# To get a list of data for all entries having formula Fe2O3
data = m.get_data("Fe2O3")

# To get the energies of all entries having formula Fe2O3
energies = m.get_data("Fe2O3", "energy")

API得到数据不但可以单独处理,还可以和我们自己计算出来的数据一起处理。比如我们要计算Li,Fe,O三种元素的组成的相图,我们可以用mp_entries = m.get_entries_in_chemsys(["Li", "Fe", "O"])得到数据库里有的数据,还可以通过pymatgen.apps.borg.hive里的VaspToComputedEntryDrone功能把自己计算的结果和数据库种的数据一起分析:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
from pymatgen.ext.matproj import MPRester
from pymatgen.apps.borg.hive import VaspToComputedEntryDrone
from pymatgen.apps.borg.queen import BorgQueen
from pymatgen.entries.compatibility import MaterialsProjectCompatibility
from pymatgen.analysis.phase_diagram import PhaseDiagram, PDPlotter

# Assimilate VASP calculations into ComputedEntry object. Let's assume that
# the calculations are for a series of new LixFeyOz phases that we want to
# know the phase stability.
drone = VaspToComputedEntryDrone()
queen = BorgQueen(drone, rootpath=".")
entries = queen.get_data()

# Obtain all existing Li-Fe-O phases using the Materials Project REST API
with MPRester("USER_API_KEY") as m:
mp_entries = m.get_entries_in_chemsys(["Li", "Fe", "O"])

# Combined entry from calculated run with Materials Project entries
entries.extend(mp_entries)

# Process entries using the MaterialsProjectCompatibility
compat = MaterialsProjectCompatibility()
entries = compat.process_entries(entries)

# Generate and plot Li-Fe-O phase diagram
pd = PhaseDiagram(entries)
plotter = PDPlotter(pd)
plotter.show()

最后得到材料的相图:

pymatgen-api4

补充内容1

我们写程序的时候不用每个程序都把密钥写进去,这样分享给他人的时候很容易把API_KEY泄露,可以把密钥加载到config file.里,这样每次使用密钥的时候都只用MPRester()就可以了。

MPRester can also read the API key via the pymatgen config file. Simply run:

1
2
C:\Users\Jin-cheng Liu>pmg config --add PMG_MAPI_KEY <USER_API_KEY>
New C:\Users\Jin-cheng Liu\.pmgrc.yaml written!

to add this to the .pmgrc.yaml, and you can now call MPRester without any arguments. This makes it much easier for heavy users of the Materials API to use MPRester without having to constantly insert their API key in the scripts.

补充内容2(通过网址传输API指令)

一般的使用方式是直接输入网址得到自己想要性质,

https://www.materialsproject.org/rest/v2/materials/mp-1234/vasp?API_KEY=YOUR_API_KEY

就能得到一个字典的返回值,我们想要的结构和基本性质都在这个字典里:

1
{"response": [{"energy": -26.94573468, "energy_per_atom": -4.49095578, "volume": 116.92375473740876, "formation_energy_per_atom": -0.4835973866666663, "nsites": 6, "unit_cell_formula": {"Al": 4.0, "Lu": 2.0}, "pretty_formula": "LuAl2", "is_hubbard": false, "elements": ["Lu", "Al"], "nelements": 2, "e_above_hull": 0, "hubbards": {}, "is_compatible": true, "spacegroup": {"source": "spglib", "symbol": "Fd-3m", "number": 227, "point_group": "m-3m", "crystal_system": "cubic", "hall": "F 4d 2 3 -1d"}, "task_ids": ["mp-940654", "mp-940234", "mp-1234", "mp-925833"], "band_gap": 0.0, "density": 6.502482433523648, "icsd_id": null, "icsd_ids": [608375, 57958, 608376, 608372, 608371, 608370], "cif": "# generated using pymatgen\ndata_LuAl2\n_symmetry_space_group_name_H-M   'P 1'\n_cell_length_a   5.48873905\n_cell_length_b   5.48873905\n_cell_length_c   5.48873905\n_cell_angle_alpha   60.00000005\n_cell_angle_beta   60.00000003\n_cell_angle_gamma   60.00000007\n_symmetry_Int_Tables_number   1\n_chemical_formula_structural   LuAl2\n_chemical_formula_sum   'Lu2 Al4'\n_cell_volume   116.92375474\n_cell_formula_units_Z   2\nloop_\n _symmetry_equiv_pos_site_id\n _symmetry_equiv_pos_as_xyz\n  1  'x, y, z'\nloop_\n _atom_site_type_symbol\n _atom_site_label\n _atom_site_symmetry_multiplicity\n _atom_site_fract_x\n _atom_site_fract_y\n _atom_site_fract_z\n _atom_site_occupancy\n  Al  Al0  1  0.500000  0.500000  0.500000  1\n  Al  Al1  1  0.500000  0.500000  0.000000  1\n  Al  Al2  1  0.000000  0.500000  0.500000  1\n  Al  Al3  1  0.500000  0.000000  0.500000  1\n  Lu  Lu4  1  0.875000  0.875000  0.875000  1\n  Lu  Lu5  1  0.125000  0.125000  0.125000  1\n", "total_magnetization": 0.00062595, "material_id": "mp-1234", "oxide_type": "None", "tags": ["Aluminium lutetium (2/1)"], "elasticity": {"G_Reuss": 61.0, "G_VRH": 61.0, "G_Voigt": 62.0, "G_Voigt_Reuss_Hill": 61.0, "K_Reuss": 82.0, "K_VRH": 82.0, "K_Voigt": 82.0, "K_Voigt_Reuss_Hill": 82.0, "elastic_anisotropy": 0.04, "elastic_tensor": [[174.0, 36.0, 36.0, 0.0, 0.0, 0.0], [36.0, 174.0, 36.0, 0.0, 0.0, 0.0], [36.0, 36.0, 174.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 57.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 57.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.0, 57.0]], "homogeneous_poisson": 0.2, "poisson_ratio": 0.2, "universal_anisotropy": 0.04, "elastic_tensor_original": [[173.8039399290068, 36.29190678313171, 36.29190678313171, 0.0, 0.0, 0.0], [36.29190678313171, 173.8039399290068, 36.29190678313171, 0.0, 0.0, 0.0], [36.29190678313171, 36.29190678313171, 173.8039399290068, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 56.801403605413576, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 56.801403605413576, 0.0], [0.0, 0.0, 0.0, 0.0, 0.0, 56.801403605413576]], "compliance_tensor": [[6.2, -1.1, -1.1, 0.0, -0.0, 0.0], [-1.1, 6.2, -1.1, -0.0, 0.0, -0.0], [-1.1, -1.1, 6.2, -0.0, -0.0, 0.0], [0.0, -0.0, -0.0, 17.6, 0.0, -0.0], [-0.0, 0.0, -0.0, 0.0, 17.6, -0.0], [0.0, -0.0, 0.0, -0.0, -0.0, 17.6]], "warnings": [], "nsites": 6}, "piezo": null, "diel": null, "full_formula": "Lu2Al4"}], "valid_response": true, "created_at": "2019-06-12T06:47:28.901438", "version": {"db": "2019.05", "pymatgen": "2019.5.28", "rest": "2.0"}, "copyright": "Materials Project, 2019"}

在vasp后面再跟一个\ 可以得到特定的性质,比如想要得到能带结构:

https://www.materialsproject.org/rest/v2/materials/mp-1234/vasp/bandstructure?API_KEY=YOUR_API_KEY

会返回能带结构的信息。更多的功能可以通过改变mp-1234, vasp, bandstructre这些地方实现,选项非常多,可以参阅API手册。

这种传输方式API的密钥是没有加密的,确定地址是https,密钥不会泄露。和笔者用的比特币交易所的API是类似,不过交易所一般还要求API_KEY先加密。


转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。