Bohrium
robot
新建

空间站广场

论文
Notebooks
比赛
课程
Apps
我的主页
我的Notebooks
我的论文库
我的足迹

我的工作空间

任务
节点
文件
数据集
镜像
项目
数据库
公开
Pymatgen Python API 的使用实践
Pymatgen
机器辅助材料设计
Pymatgen机器辅助材料设计
爱学习的王一博
发布于 2024-04-18
推荐镜像 :Basic Image:bohrium-notebook:2023-04-07
推荐机型 :c2_m4_cpu
赞 3
12
5
Pymatgen Python API 的使用实践
Pymatgen简介
Pymatgen学习资源

Pymatgen Python API 的使用实践

Open In Bohrium

Authors:

  • Jiameng Huang
  • Hongshuai Wang
  • Yibo Wang

适用学员:

  1. 对于材料背景同学,需要了解Python基础使用
  2. 对于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的使用,你可以参考以下资源:

注意MP的API对python版本有要求,建议使用Python3.10及以上版本安装

代码
文本
[1]
from pymatgen.core.structure import Structure
import json
代码
文本

第一步,调用Pymatgen库、json库。Pymatgen的调用不用说,json库用于处理json文件。如果你不熟悉json文件,不要紧张,把它当做目录树下存信息的一种形式就好了;不过不要轻易修改大括号{ }和中括号[ ],否则可能导致json格式错误。

代码
文本
[2]
example = {
"@class": "Structure", # 指定这是一个Structure类的实例
"@module": "pymatgen.core.structure", # 指定Structure类定义所在的模块
"charge": 0, # 结构的总电荷量
"lattice": { # 晶格信息
"a": 6.650525597706169, # 晶格参数a
"alpha": 90.0, # 晶格角度α
"b": 6.650525597706169, # 晶格参数b
"beta": 90.0, # 晶格角度β
"c": 3.322297, # 晶格参数c
"gamma": 119.9999959979786, # 晶格角度γ
"matrix": [ # 晶格矩阵,定义了晶胞的基矢量
[3.325263, -5.759524, 0.0], # 第一个基矢量
[3.325263, 5.759524, 0.0], # 第二个基矢量
[0.0, 0.0, 3.322297] # 第三个基矢量
],
"pbc": [True, True, True], # 周期性边界条件,对应x、y、z轴
"volume": 127.25681281981149 # 晶胞体积
},
"sites": [ # 原子位点信息
{
"abc": [0.0, 0.595396, 0.5], # 分数坐标
"label": "Nb", # 元素标签
"properties": {}, # 原子属性
"species": [{ "element": "Nb", "occu": 1 }], # 原子种类及其占据率
"xyz": [1.9798482891480003, 3.429197551504, 1.6611485] # 笛卡尔坐标
},
{
"abc": [0.595396, 0.0, 0.5], # 分数坐标
"label": "Nb", # 元素标签
"properties": {}, # 原子属性
"species": [{ "element": "Nb", "occu": 1 }], # 原子种类及其占据率
"xyz": [1.9798482891480003, -3.429197551504, 1.6611485] # 笛卡尔坐标
},
{
"abc": [0.404604, 0.404604, 0.5], # 分数坐标
"label": "Nb", # 元素标签
"properties": {}, # 原子属性
"species": [{ "element": "Nb", "occu": 1 }], # 原子种类及其占据率
"xyz": [2.690829421704, -1.793616308845003e-16, 1.6611485] # 笛卡尔坐标
},
{
"abc": [0.746388, 0.746388, 0.0], # 分数坐标
"label": "Cr", # 元素标签
"properties": {}, # 原子属性
"species": [{ "element": "Cr", "occu": 1 }], # 原子种类及其占据率
"xyz": [4.963872800088001, 9.230245484559418e-17, 0.0] # 笛卡尔坐标
},
{
"abc": [0.253612, 0.0, 0.0], # 分数坐标
"label": "Cr", # 元素标签
"properties": {}, # 原子属性
"species": [{ "element": "Cr", "occu": 1 }], # 原子种类及其占据率
"xyz": [0.843326599956, -1.460684400688, 0.0] # 笛卡尔坐标
},
{
"abc": [0.0, 0.253612, 0.0], # 分数坐标
"label": "Cr", # 元素标签
"properties": {}, # 原子属性
"species": [{ "element": "Cr", "occu": 1 }], # 原子种类及其占据率
"xyz": [0.843326599956, 1.460684400688, 0.0] # 笛卡尔坐标
},
{
"abc": [0.0, 0.0, 0.5], # 分数坐标
"label": "Si", # 元素标签
"properties": {}, # 原子属性
"species": [{ "element": "Si", "occu": 1 }], # 原子种类及其占据率
"xyz": [0.0, 0.0, 1.6611485] # 笛卡尔坐标
},
{
"abc": [0.666667, 0.333333, 0.0], # 分数坐标
"label": "Si", # 元素标签
"properties": {}, # 原子属性
"species": [{ "element": "Si", "occu": 1 }], # 原子种类及其占据率
"xyz": [3.325263, -1.9198451730160002, 0.0] # 笛卡尔坐标
},
{
"abc": [0.333333, 0.666667, 0.0], # 分数坐标
"label": "Si", # 元素标签
"properties": {}, # 原子属性
"species": [{ "element": "Si", "occu": 1 }], # 原子种类及其占据率
"xyz": [3.325263, 1.9198451730160002, 0.0] # 笛卡尔坐标
}
]
}

代码
文本

这里我们使用Nb3Cr3Si3作为示例。晶体结构为立方晶系,晶格参数为a=b=3.325263 Å,c=3.322297 Å。其中,晶胞中包含9个原子,3个铌原子、3个铬原子和3个硅原子。晶体的晶格类型为直接晶格,原子位置可以用直接坐标表示。

示例数据以json形式给出。Pymatgen Structure类可以从json文件或POSCAR等文件中读取结构信息。

代码
文本
[3]
structure = Structure.from_dict(example)
print(structure)
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类提供了很多有用的方法来分析和处理晶体结构,常用功能如下:

代码
文本
[4]
print("晶体结构信息:")
print("结构体积:", structure.volume)
print("晶格参数:", structure.lattice.abc)
print("json格式:", structure.as_dict()) # 以JSON文件格式输出
print("POSCAR格式:", structure.to(fmt="poscar")) # 转化成POSCAR形式
晶体结构信息:

结构体积: 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


代码
文本
[ ]
!pip install mp-api
已隐藏输出
代码
文本
[6]
from mp_api.client import MPRester
## 这里输入API KEY
with MPRester("YOUR APIKEY HERE") as m:

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

# 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")
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]
代码
文本
[7]
print(structure)
print(dos)
print(bandstructure)

from pymatgen.electronic_structure.plotter import DosPlotter, BSPlotter
# Adds a DOS with a label.
dosplotter = DosPlotter()

dosplotter.add_dos("Total DOS", dos)
dosplotter.show()

bsplotter = BSPlotter(bandstructure)
bsplotter.show()
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>
代码
文本
Pymatgen
机器辅助材料设计
Pymatgen机器辅助材料设计
已赞3
本文被以下合集收录
python 学习
Stark
更新于 2024-08-21
1 篇0 人关注
机器学习基础
东流
更新于 2024-08-18
1 篇0 人关注
推荐阅读
公开
Pymatgen自动生成表面吸附模型-高通量计算系列教程
AlloyPymatgen化学信息学
AlloyPymatgen化学信息学
wanghongshuai@dp.tech
发布于 2023-09-18
4 赞5 转存文件
公开
Pytorch基础教程(二):自动求导
PyTorchDeep Learning
PyTorchDeep Learning
爱学习的王一博
发布于 2024-03-11
2 赞7 转存文件