Building structure with Pymatgen
This tutorial aims to provide a hands-on tutorial of building crystal structures and cutting surfaces with Pymatgen.
Pymatgen (Python Materials Genomics) is an open-source Python library for materials analysis.
Pre Set-up
Getting Pymatgen
(method 1)
conda install --channel conda-forge pymatgen
(method 2)
pip install pymatgen
Import Structure
From Material Project
from mp_api.client import MPRester
Please check you API code here: https://materialsproject.org/api
Read from local file
from pymatgen.core.structure import Structure
# import structure from local cif file
primitive = Structure.from_file("./primitive.cif")
from pymatgen.symmetry.analyzer import SpacegroupAnalyzer
SGA = SpacegroupAnalyzer(primitive)
conventional = SGA.get_conventional_standard_structure()
Building within Pymatgen
statements
from pymatgen import *
create primitive cell from space group
from pymatgen.core import Structure, Lattice
from pymatgen.core.surface import SlabGenerator, Slab
latt_length = number_1 #lattice length, here a=b=c, in A
latt_angle = number_2 #lattice angle, here alpha=beta=gamma, in degree
latt = Lattice.from_parameters(a=latt_length,b=latt_length,c=latt_length,alpha=latt_angle,beta=latt_angle,gamma=latt_angle)
print(latt)
fractional coordinates
coord = [[x1,y1,z1],
[x2,y2,z2],
[x3,y3,z3],
[x4,y4,z4],
[x5,y5,z5],
[x6,y6,z6]]
cell_name = Structure.from_spacegroup("P1", latt, ["ATOM1","ATOM2","ATOM3","ATOM4","ATOM5","ATOM6"], coord,coords_are_cartesian=False)
print(cell_name)
Visulising Structure
Generate slab from bulk system
Build slab model
Generate slab using slab generator
slabgen = SlabGenerator(conventional,
miller_index=(0,0,1),
min_slab_size=10,
min_vacuum_size=10,
center_slab=False)
Search for all possible terminations
slabs = slabgen.get_slabs()
print("The number of distinct terminations is: {}".format(len(slabs)))
save these slabs in .cif files
for n,slab in enumerate(slabs):
print(n, "writing slab{}.cif".format(n))
slab.to(fmt="cif", filename="slab{}.cif".format(n))
print("Slabs terminated at shift = {}".format(slab.shift))
Search for possible adsorption sites
statements
from pymatgen.analysis.adsorption import AdsorbateSiteFinder, plot_slab
from matplotlib import pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111)
find adsorbate sites of slab 0 generated in Step 1
will write a for loop to include all slabs
ASF = AdsorbateSiteFinder(slabs[0]).find_adsorption_sites(distance=2.0,
put_inside=True,
symm_reduce=0.01,
near_reduce=0.01,
positions=('ontop','hollow','bridge'),
no_obtuse_hollow=True)
Plot slab with adsorption sites
plot_slab(slabs[0],ax,adsorption_sites=True)
save as png
fig.savefig("./slab0.png", dpi=200)
print a list of sites coordinate
sites_dict = AdsorbateSiteFinder(slabs[0]).find_adsorption_sites(distance=2.0,
put_inside=True,
symm_reduce=0.01,
near_reduce=0.01,
positions=('ontop','hollow','bridge'),
no_obtuse_hollow=True)
print(sites_dict)
Export Data
statements
from pymatgen.io.cif import CifWriter
from pymatgen.io.xyz import XYZ
Write cif files
wcif = CifWriter(structure)
wcif.write_file("test.cif")
Write xyz files
wxyz = XYZ()
wxyz.write_file("test.xyz")