

Pymatgen Python API 的使用实践
Authors:
- Jiameng Huang
- Hongshuai Wang
- Yibo Wang
适用学员:
- 对于材料背景同学,需要了解Python基础使用
- 对于AI背景同学,需要对结构化学涉及的数据有直观认识
Aims:
- 通过Pymatgen的Python API处理数据
- 加载json类型的数据到Structure类
- 用Structure类的成员函数(就是<Structure.某个函数名()>)查看一些材料特征
- 将Structure类变量内容输出为json格式
- 将Structure类变量内容输出为POSCAR格式
镜像建议:bohiurm-notebook:2023-04-07
Reference:
Shyue Ping Ong, William Davidson Richards, Anubhav Jain, Geoffroy Hautier, Michael Kocher, Shreyas Cholia, Dan Gunter, Vincent Chevrier, Kristin A. Persson, Gerbrand Ceder. Python Materials Genomics (pymatgen) : A Robust, Open-Source Python Library for Materials Analysis. Computational Materials Science, 2013, 68, 314–319. doi:10.1016/j.commatsci.2012.10.028
Pymatgen简介
Pymatgen是一个用于材料分析的强大的开源Python库。它提供了一些高度灵活的类来表示元素、位点、分子和结构对象,以及丰富的输入/输出支持,包括VASP、ABINIT、CIF等多种文件格式。它还提供了强大的分析工具,如相图、Pourbaix图、扩散分析、反应等。此外,它还可以与Materials Project REST API、Crystallography Open Database等外部数据源进行集成。
Pymatgen学习资源
如果你想学习Pymatgen的使用,你可以参考以下资源:
- 官方文档,提供了详细的介绍和教程。
- matgenb,提供了一些Jupyter笔记本来演示功能。
- Usage — pymatgen 2023.3.23 documentation,提供了一个快速的概览和示例代码。
- 一些其他案例资料,提供了一些Pymatgen案例
注意MP的API对python版本有要求,建议使用Python3.10及以上版本安装
第一步,调用Pymatgen库、json库。Pymatgen的调用不用说,json库用于处理json文件。如果你不熟悉json文件,不要紧张,把它当做目录树下存信息的一种形式就好了;不过不要轻易修改大括号{ }
和中括号[ ]
,否则可能导致json格式错误。
这里我们使用Nb3Cr3Si3作为示例。晶体结构为立方晶系,晶格参数为a=b=3.325263 Å,c=3.322297 Å。其中,晶胞中包含9个原子,3个铌原子、3个铬原子和3个硅原子。晶体的晶格类型为直接晶格,原子位置可以用直接坐标表示。
示例数据以json形式给出。Pymatgen Structure类可以从json文件或POSCAR等文件中读取结构信息。
Full Formula (Nb3 Cr3 Si3) Reduced Formula: NbCrSi abc : 6.650526 6.650526 3.322297 angles: 90.000000 90.000000 119.999996 pbc : True True True Sites (9) # SP a b c --- ---- -------- -------- --- 0 Nb 0 0.595396 0.5 1 Nb 0.595396 0 0.5 2 Nb 0.404604 0.404604 0.5 3 Cr 0.746388 0.746388 0 4 Cr 0.253612 0 0 5 Cr 0 0.253612 0 6 Si 0 0 0.5 7 Si 0.666667 0.333333 0 8 Si 0.333333 0.666667 0
接下来,创建结构对象,并·检查一下结构信息是否成功被赋给structure。 Pymatgen的Structure是一个用于表示晶体结构的类,它可以通过晶格参数和原子坐标来创建晶体结构,也可以通过读取文件、从json文件中获取。Structure类提供了很多有用的方法来分析和处理晶体结构,常用功能如下:
晶体结构信息: 结构体积: 127.25681281981149 晶格参数: (6.650525597706169, 6.650525597706169, 3.322297) json格式: {'@module': 'pymatgen.core.structure', '@class': 'Structure', 'charge': 0, 'lattice': {'matrix': [[3.325263, -5.759524, 0.0], [3.325263, 5.759524, 0.0], [0.0, 0.0, 3.322297]], 'pbc': (True, True, True), 'a': 6.650525597706169, 'b': 6.650525597706169, 'c': 3.322297, 'alpha': 90.0, 'beta': 90.0, 'gamma': 119.9999959979786, 'volume': 127.25681281981149}, 'properties': {}, 'sites': [{'species': [{'element': 'Nb', 'occu': 1}], 'abc': [0.0, 0.595396, 0.5], 'xyz': [1.9798482891480003, 3.429197551504, 1.6611485], 'properties': {}, 'label': 'Nb'}, {'species': [{'element': 'Nb', 'occu': 1}], 'abc': [0.595396, 0.0, 0.5], 'xyz': [1.9798482891480003, -3.429197551504, 1.6611485], 'properties': {}, 'label': 'Nb'}, {'species': [{'element': 'Nb', 'occu': 1}], 'abc': [0.404604, 0.404604, 0.5], 'xyz': [2.690829421704, 0.0, 1.6611485], 'properties': {}, 'label': 'Nb'}, {'species': [{'element': 'Cr', 'occu': 1}], 'abc': [0.746388, 0.746388, 0.0], 'xyz': [4.963872800088001, 0.0, 0.0], 'properties': {}, 'label': 'Cr'}, {'species': [{'element': 'Cr', 'occu': 1}], 'abc': [0.253612, 0.0, 0.0], 'xyz': [0.843326599956, -1.460684400688, 0.0], 'properties': {}, 'label': 'Cr'}, {'species': [{'element': 'Cr', 'occu': 1}], 'abc': [0.0, 0.253612, 0.0], 'xyz': [0.843326599956, 1.460684400688, 0.0], 'properties': {}, 'label': 'Cr'}, {'species': [{'element': 'Si', 'occu': 1}], 'abc': [0.0, 0.0, 0.5], 'xyz': [0.0, 0.0, 1.6611485], 'properties': {}, 'label': 'Si'}, {'species': [{'element': 'Si', 'occu': 1}], 'abc': [0.666667, 0.333333, 0.0], 'xyz': [3.325263, -1.9198451730160002, 0.0], 'properties': {}, 'label': 'Si'}, {'species': [{'element': 'Si', 'occu': 1}], 'abc': [0.333333, 0.666667, 0.0], 'xyz': [3.325263, 1.9198451730160002, 0.0], 'properties': {}, 'label': 'Si'}]} POSCAR格式: Nb3 Cr3 Si3 1.0 3.3252630000000001 -5.7595239999999999 0.0000000000000000 3.3252630000000001 5.7595239999999999 0.0000000000000000 0.0000000000000000 0.0000000000000000 3.3222969999999998 Nb Cr Si 3 3 3 direct 0.0000000000000000 0.5953960000000000 0.5000000000000000 Nb 0.5953960000000000 0.0000000000000000 0.5000000000000000 Nb 0.4046040000000000 0.4046040000000000 0.5000000000000000 Nb 0.7463880000000001 0.7463880000000001 0.0000000000000000 Cr 0.2536120000000000 0.0000000000000000 0.0000000000000000 Cr 0.0000000000000000 0.2536120000000000 0.0000000000000000 Cr 0.0000000000000000 0.0000000000000000 0.5000000000000000 Si 0.6666670000000000 0.3333330000000000 0.0000000000000000 Si 0.3333330000000000 0.6666670000000000 0.0000000000000000 Si
c:\Users\likefallwind\anaconda3\envs\pytorch\lib\site-packages\tqdm\auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html from .autonotebook import tqdm as notebook_tqdm h Retrieving MaterialsDoc documents: 100%|██████████| 1/1 [00:00<?, ?it/s] Full Formula (Lu2 Al4) Reduced Formula: LuAl2 abc : 5.453441 5.453442 5.453442 angles: 59.999994 59.999988 60.000012 pbc : True True True Sites (6) # SP a b c magmom --- ---- ----- ------ ----- -------- 0 Lu 0.875 0.875 0.875 -0 1 Lu 0.125 0.125 0.125 -0 2 Al 0.5 0.5 0.5 -0 3 Al 0.5 0.5 0 -0 4 Al 0 0.5 0.5 -0 5 Al 0.5 -0 0.5 -0 Retrieving ElectronicStructureDoc documents: 100%|██████████| 1/1 [00:00<?, ?it/s] Retrieving ElectronicStructureDoc documents: 100%|██████████| 1/1 [00:00<?, ?it/s]
Full Formula (Lu2 Al4) Reduced Formula: LuAl2 abc : 5.453441 5.453442 5.453442 angles: 59.999994 59.999988 60.000012 pbc : True True True Sites (6) # SP a b c magmom --- ---- ----- ------ ----- -------- 0 Lu 0.875 0.875 0.875 -0 1 Lu 0.125 0.125 0.125 -0 2 Al 0.5 0.5 0.5 -0 3 Al 0.5 0.5 0 -0 4 Al 0 0.5 0.5 -0 5 Al 0.5 -0 0.5 -0 Complete DOS for Full Formula (Lu2 Al4) Reduced Formula: LuAl2 abc : 5.488740 5.488740 5.488740 angles: 60.000000 60.000000 60.000000 pbc : True True True Sites (6) # SP a b c --- ---- ----- ----- ----- 0 Lu 0.25 0.25 0.25 1 Lu 0 0 0 2 Al 0.625 0.125 0.625 3 Al 0.625 0.625 0.125 4 Al 0.125 0.625 0.625 5 Al 0.625 0.625 0.625 <pymatgen.electronic_structure.bandstructure.BandStructureSymmLine object at 0x0000013D0F1ABAF0>







