--- title: Structure IO keywords: fastai sidebar: home_sidebar summary: "This includes generating POSCAR form Materials Project. You need their API key to run functions in this module where required." description: "This includes generating POSCAR form Materials Project. You need their API key to run functions in this module where required." nb_path: "StructureIO.ipynb" ---
Note : To use materials project data frequently, you may need to save their api key into file. Run function below and it saves key to a file and every function will autoload it.
def field(x,y,z):
u, v, w = y, -x , z
norm = 5*(u*u + v*v + w*w)**(1/2)
if norm == 0:
return (x,y,z,0.01,0.01,0.01)
return (x,y,z, u/norm, v/norm, w/norm)
X,Y,Z = np.mgrid[0:1:10j,0:1:10j,0:1:3j]
vs = np.concatenate([X.ravel(),Y.ravel(),Z.ravel()]).reshape((3,-1)).T
ef = np.array([field(*v) for v in vs]).T
C = np.abs(ef[3:,:].T)
C = C/np.max(C)
ax1, ax2 = sp.get_axes(ncols=2,figsize=(6,3),axes_3d=[0,1])
bz = get_bz([[1,0,0],[0,1,0],[0,0,1]],primitive=True)
splot_bz(bz,ax=ax1,vname='x',fill=False,color=(0.8,0.7,0.2,0.7))
splot_bz(bz,ax=ax2,vname='x',fill=False,color=(0.8,0.7,0.2,0.7))
fancy_quiver3d(*ef,C = C,ax=ax1,L=0.01,mutation_scale=7,capstyle='butt',arrowstyle='wedge').set_box_aspect((1,1,1))
ax1.set_axis_off()
ax1.set_title('Fancy Quiver 3D')
# Repeat for each body line and two head lines
C = np.concatenate([C,np.repeat(C, 2, axis=0)])
q = ax2.quiver(*ef,lw=1,colors=C)
ax2.set_title('Default Quiver 3D')
ax2.set_box_aspect((1,1,1))
ax2.set_axis_off()
mp = InvokeMaterialsProject()
mp.request('GaAs',max_sites=2,min_sites=2)
pc = mp.poscars
print("material_id: ",pc[1].mp_id)
print("spacegroup symbol: ",pc[1].symbol)
print("\n",pc[1].content)
read_ticks('kpt.txt')
In example below, the overall value of n = 5
is skipped and 4th entry in the point is used instead. In later example, a 'skipk' label removes high symmetry point index.
get_kpath([[0.5,0.5,0.5,2],[0,0,0]],n=5,ibzkpt='IBZKPT',labels = ['M','Γ'])
get_kpath([[[0.375,0.375,0.5,21],[0,0,0]],[[0.5,0.25,0.5],[0,0,0]],[[0.25,0.25,0.25],[0,0,0]]],n=11,labels = ['M','skip','Γ|M','L','K','N'],outfile='kpt.txt')
Automatically generated using PivotPy with HSK-INDS = [0, 29, -1], LABELS = ['M', 'Γ|M', 'L'], SEG-INDS = [21, 29]
34
HSK-INDS = [0, 29, -1]
should be [0,21,29,-1]
but skip
label removed it. Also after 21, it may have 32 as n=11, but distance smaller than 1 resulted in only 8 points there. This is pretty useful if you don't need to rescale kpoint axis later on based on distance.
str2kpath('0 0 0 G 2\n 1 1 1 X\n1/2 1/2 1/2 L',n=4)
_get_basis([[ 4.00771850e+00, 0.00000000e+00, -0.00000000e+00],
[ 2.00385925e+00, 3.47078603e+00, 5.06700000e-13],
[ 2.00385925e+00, 1.15692868e+00, 3.27228845e+00]]).inverted
get_kmesh
generates a uniform grid in first (primitive) Brillouin zone.outfile
provided, if outfile = None
, thn BZ data object is returned. ibzkpt
to include it automatically, this is important to include for HSE calculations with weight = 0
. If weight = None
, weights are distributed uniformly.n_xyz = integer
,(recommended for auto uniform mesh), all sides have same number of points unless you provide path_pos
too, in that case n_xyz
is chosen for smallest side in reciprocal space and other sides are scaled based on their lengths according to given POSCAR. get_kmesh(n_xyz=[2,2,2],path_pos=[[1,1,0],[0,1,1],[1,0,0.5]])
import numpy as np
print("Interier Angle:",_rad_angle([0,0.1,0],[0,0.5,1]))
print("Angle with x-axis:",_tan_inv(1,0))
pts = np.array([[1,0,3],[0,0,0],[0,1,2]])
inds = order(pts)
pts[inds]
import numpy as np
bbya = 1.2
cbya = 0.8
ofco = [[1,bbya,0],[-1,bbya,0],[0,0,cbya]]
bco = [[1,bbya,cbya],[-1,bbya,cbya],[-1,-bbya,cbya]]
fco = [[1,0,cbya],[1,bbya,0],[0,bbya,cbya]]
bcc = [[1,1,1],[-1,1,1],[-1,-1,1]]
cube = [[1,0,0],[0,1,0],[0,0,1]]
hexa = [[1,0,0],[0.5,np.sqrt(3)/2,0],[0,0,1]]
ctg = [[1,1,cbya],[1,-1,cbya],[-1,-1,cbya]]
tgl = [[0.81915,-0.472937,0.94923],[0,0.94587,0.94923],[-0.81915,-0.472937,0.94923]]
fcc =[[0.0000000000000000, 0.5020149905223673, 0.5020149905223673],
[0.5020149905223673, 0.0000000000000000, 0.5020149905223673],
[0.5020149905223673, 0.5020149905223673, 0.0000000000000000]]
basis,normals,vertices,faces,specials = get_bz(fcc,digits=4)
print(basis)
for k,v in zip(specials.coords[:10],specials.kpoints[:10]):
print(v,'-->',k)
import pivotpy.s_plots as sp
lat = hexa
bz = get_bz(lat,primitive=False,loop=True)
axs = sp.get_axes(ncols=4,figsize=(9,2))
ax2d = splot_bz(bz,plane='xy',ax=axs[0],vectors=True,color=(0.7,0,0.5))
ax3d = splot_bz(bz,ax=axs[1],color=(108/255,204/255,215/255,0),vectors=True,light_from=(10,10,30),alpha=0.5,colormap=None)
prim_bz = get_bz(lat,primitive=True)
ax3d_ = splot_bz(prim_bz,ax=axs[2],color=(212/255,175/255,55/255,0),vectors=True)
_ax3d = splot_bz(get_bz(prim_bz.basis,primitive=True),ax=axs[3],color=(212/255,175/255,55/255,0),vectors=True,vname='a',colormap='turbo_r',light_from=(0,1,1))
ps = bz.specials.coords[bz.specials.near[4]][[0,1,8,2,5,4]]
ax3d.scatter(ps[:,0],ps[:,1],ps[:,2],c='r',s=20)
ax2d.scatter(ps[:,0],ps[:,1],c='r',s=20)
ax3d.set_axis_off()
ax3d.set_title('Regular BZ')
ax3d_.set_axis_off()
ax3d_.set_title('Primitive BZ')
ax2d.set_axis_off()
_ax3d.set_title('Real lattice')
_ax3d.set_axis_off()
ax2d.set_title('XY Projection of Regular BZ')
sp.plt2html()
iplot_bz()
. Same color points lie on a sphere, with radius decreasing as red to blue and gamma point in gold color. These color help distinguishing points but the points not always be equivalent, for example in FCC, there are two points on mid of edges connecting square-hexagon and hexagon-hexagon at equal distance from center but not the same points.
{% include note.html content='iplot_bz
includes special points only if vname = b
, i.e. in recirpocal space. ' %}import pivotpy as pp
fig = iplot_bz([[1,0,1],[1,1,0],[0,1,1]],alpha=1)
pp.iplot2html(fig,modebar=False)
to_R3(_get_basis(hexa).inverted,[[1,1,1],[0.333,0.333,0.5]])
import pivotpy as pp, numpy as np
kpoints = get_kmesh(2)
fig = iplot_bz(get_bz(hexa))
prim_bz = get_bz(hexa,primitive=True)
iplot_bz(get_bz(prim_bz.basis,primitive=True),vname='a',fig=fig,color="rgba(200,220,10,0.4)")
pp.iplot2html(fig,modebar=False)
import matplotlib.pyplot as plt , pivotpy as pp
import matplotlib.tri as tri
axs = pp.get_axes(ncols=5,figsize=(9,2.3),widths=[5,5,0.2,5,5])
evr = pp.Vasprun('../graphene_example/ISPIN_1/dos/vasprun.xml').data
kpoints = np.concatenate([evr.kpoints, -evr.kpoints]) # negative side
bz1 = get_bz(evr.poscar.basis,primitive=True)
bz2 = get_bz(evr.poscar.basis,primitive=False)
out1 = kpoints2bz(bz1,kpoints,primitive=True) + 0.002 #Just fun translate
out2 = kpoints2bz(bz2,kpoints,primitive=False)
splot_bz(bz1,plane='xy',ax = axs[0],color=(1,1,1,0)).set_axis_off()
splot_bz(bz2,plane='xy',ax = axs[1]).set_axis_off()
splot_bz(bz1,plane='xy',ax = axs[3]).set_axis_off()
splot_bz(bz2,plane='xy',ax = axs[4]).set_axis_off()
# Energy as color on BZ.
en = evr.bands.evals[:,8].ravel()
en = np.concatenate([en,en])
s_en = (en-np.min(en))/(np.max(en)-np.min(en)) # bring to range [0,1] for colors
c = plt.cm.get_cmap('plasma')(s_en)
labels = [str(l) + ' eV' for l in np.round(np.min(en)+(np.max(en)-np.min(en))*np.array([1/6,3/5,5/6]),2)]
tri1 = tri.Triangulation(out1.T[0], out1.T[1])
tri2 = tri.Triangulation(out2.T[0], out2.T[1])
axs[0].tricontourf(tri1, en, levels=np.linspace(-2.6, 3.4, 8), cmap='plasma',shading='nearest')
axs[1].tricontour(tri2, en, levels=np.linspace(-2.6, 3.4, 8), cmap='plasma',linewidths=[0.8])
axs[3].scatter(out1[:,0],out1[:,1],s=4,c=c)
axs[4].scatter(out2[:,0],out2[:,1],s=4,c=c)
axs[2].add_colorbar(cmap_or_clist='plasma', ticklabels=labels,vertical=True)
import pivotpy as pp
fig=iplot_lat(evr.poscar,sizes=10,colors=['blue','red','green'],line_width=4,fill=False)
pp.iplot2html(fig,modebar=False)
plt.style.use('ggplot')
axs = pp.get_axes((6,6),ncols=2,axes_3d=[0,])
splot_lat(evr.poscar,sizes=40,plane=None,fill=True,tol=1,alpha=0.4,light_from=(1,-0.1,0),vectors=False,ax=axs[0],edge_color=(0.5,0.5,0,0.4))
splot_lat(evr.poscar,sizes=40,plane='xy',fill=True,tol=1,alpha=0.4,light_from=(1,-0.1,0),vectors=False,ax=axs[1],colors=['red'],translate=[-2/3,-1/3,0]).set_axis_on()
axs[0].view_init(70,75)
print(write_poscar(evr.poscar))
print("===================================================")
write_poscar(evr.poscar,sd_list=['T T T','F F F'],outfile='POSCAR')
print("===================================================")
print(write_poscar(evr.poscar,sd_list=['T T T','F F F']))
import pivotpy as pp
import numpy as np
import pivotpy.vr_parser as vp
poscar = pp.POSCAR('POSCAR')
ax = poscar.splot_lat(plane='xy',colormap='RGB',sizes=100,line_width=5)
scaled = poscar.scale(scale=[3,3,3])
scaled.splot_lat(ax=ax,plane='xy',bond_length=0.1,tol=1e-5,line_width=2)
scaled.scale(scale=[1/3,1/3,1/3],tol=1e-3).splot_lat(ax=ax,plane='xy',colormap='viridis_r',sizes=15,line_width=0.5)