This includes generating POSCAR form Materials Project. You need their API key to run functions in this module where required.

  Index 
  Example 
  StaticPlots 
  InteractivePlots 
  SpinProjectedSurfaces 
  StructureIO● 
  Widgets 
  MainAPI 

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.

atomic_number[source]

atomic_number(atom)

Return atomic number of atom

atoms_color[source]

atoms_color()

Defualt color per atom used for plotting the crystal lattice

periodic_table[source]

periodic_table()

Display colorerd elements in periodic table.

periodic_table()
2022-09-02T11:42:33.569208 image/svg+xml Matplotlib v3.3.3, https://matplotlib.org/

class Arrow3D[source]

Arrow3D(x, y, z, u, v, w, *args, **kwargs) :: FancyArrowPatch

Draw 3D fancy arrow.

fancy_quiver3d[source]

fancy_quiver3d(X, Y, Z, U, V, W, ax=None, C='r', L=0.7, mutation_scale=10, **kwargs)

Plots 3D arrows on a given ax. See FancyArrowPatch.

  • Parameters
    • X, Y, Z : 1D arrays of coordinates of arrows' tail point.
    • U, V, W : 1D arrays of dx,dy,dz of arrows.
    • ax: 3D axes, if not given, auto created.
    • C : 1D colors array mapping for arrows. Could be one color.
    • L : 1D linwidths array mapping for arrows. Could be one linewidth.
    • mutation_scale: Arrow head width/size scale. Default is 10.
    • kwargs: FancyArrowPatch's keyword arguments excluding positions,color, lw and mutation_scale, shrinkA, shrinkB which are already used. An important keyword argument is arrowstyle which could be '->','-|>', their inverted forms and many more. See on matplotlib.
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()
2022-09-02T11:42:35.620784 image/svg+xml Matplotlib v3.3.3, https://matplotlib.org/

get_selective_dynamics[source]

get_selective_dynamics(poscar_data, a=None, b=None, c=None)

Returns selective dynamics array for a given POSCAR or None if no inputb given. By default, if a direction is not given, it turns ON with others.

  • Parameters

    • poscar_data: POSCAR data.
    • a, b, c: Arrays of shape (N,2) that contain ranges in fractional coordinates to turn selective dynamics on.
  • Usage

    • get_selective_dynamics(poscar_data, a = [(0,0.1),(0.9,1)]) will turn selective dynamics on for the first and last 10% of the unit cell in a-direction as T T T.
    • get_selective_dynamics(poscar_data, a = [(0,0.1),(0.9,1)], b = [(0,0.1),(0.9,1)]) will turn selective dynamics on for the first and last 10% of the unit cell in ab-plane in form of T T T, F T T and T F T whichever applies.

write_poscar[source]

write_poscar(poscar_data, outfile=None, selective_dynamics=None, overwrite=False)

Writes poscar data object to a file or returns string

  • Parameters
    • poscar_data: Output of export_poscar,join_poscars etc.
    • outfile : str,file path to write on.
    • selective_dynamics : A list ['T T T','F F F',...] strings to turn on selective dynamics at required sites. Use get_selective_dynamics to build this list precisely.
    • overwrite: bool, if file already exists, overwrite=True changes it.

Note: POSCAR is only written in direct format even if it was loaded from cartesian format.

export_poscar[source]

export_poscar(path=None, content=None)

Export POSCAR file to python objects. Only Direct POSCAR supported.

  • Parameters
    • path: Path/to/POSCAR file. Auto picks in CWD.
    • content: POSCAR content as string, This takes precedence to path.

class InvokeMaterialsProject[source]

InvokeMaterialsProject(api_key=None)

Connect to materials project and get data using api_key from their site. Usage:

from pivotpyr.sio import InvokeMaterialsProject # or import pivotpy.InvokeMaterialsProject as InvokeMaterialsProject
mp = InvokeMaterialsProject(api_key='your_api_key')
outputs = mp.request(formula='NaCl') #returns list of structures from response
outupts[0].export_poscar() #returns poscar data
outputs[0].cif #returns cif data
mp = InvokeMaterialsProject()
out = mp.request('AlAs',max_sites=4,min_sites=2)

print("material_id: ",out[1].mp_id)
print("spacegroup symbol: ",out[1].symbol)
write_poscar(out[1].export_poscar())
material_id:  mp-11209
spacegroup symbol:  P6_3/mmc
AlAs  # ['mp-11209']['P6_3/mmc']['hexagonal'] Created by pivotpy using Materials Project Database
  3.78490089000000    
    1.0000000000000000    0.0000000000000000    0.0000000000000000
   -0.4999997063687443    0.8660255733124568    0.0000000000000000
    0.0000000001344811    0.0000000002329280    1.5840575936872205
  Al    As
  2    2
Direct
   0.0000000000000000   0.0000000000000000   0.0000000000000000
   0.0000000000000000   0.0000000000000000   0.5000000000000000
   0.3333330000000000   0.6666670000000000   0.2500000000000000
   0.6666670000000000   0.3333330000000000   0.7500000000000000

get_kpath[source]

get_kpath(*patches, n=5, weight=None, ibzkpt=None, outfile=None, rec_basis=None)

Generate list of kpoints along high symmetry path. Options are write to file or return KPOINTS list. It generates uniformly spaced point with input n as just a scale factor of number of points per unit length. You can also specify custom number of kpoints in an interval by putting number of kpoints as 4th entry in left kpoint.

  • Parameters
    • *ptaches : Any number of disconnected patches where a single patch is a dictionary like {'label': (x,y,z,[N]), ...} where x,y,z is high symmetry point and
            N (optional) is number of points in current inteval, points in a connected path patch are at least two i.e. `{'p1':[x1,y1,z1],'p2':[x2,y2,z2]}`.
            A key of a patch should be string reperenting the label of the high symmetry point. A key that starts with '_' is ignored, so you can add points without high symmetry points as well.
    • n : int, number per length of body diagonal of rec_basis, this makes uniform steps based on distance between points.
    • weight : Float, if None, auto generates weights.
    • ibzkpt : Path to ibzkpt file, required for HSE calculations.
    • outfile: Path/to/file to write kpoints.
    • rec_basis: Reciprocal basis 3x3 array to use for calculating uniform points.

If outfile = None, KPONITS file content is printed.

read_ticks[source]

read_ticks(kpoints_file_path)

Reads ticks values and labels in header of kpoint file. Returns dictionary of ktick_inds,ktick_vals,kseg_inds that can be unpacked to plotting functions. If not exist in header, returns empty values(still valid).

read_ticks('kpt.txt')
{'ktick_inds': [0, 29, -1],
 'ktick_vals': ['M', 'G', 'M|L', 'K'],
 'kseg_inds': [21, 29]}

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({'_L':[0.5,0.5,0.5],'G':[0,0,0]},n=9,ibzkpt='IBZKPT')
Automatically generated using PivotPy with HSK-INDS = [-1], LABELS = ['G'], SEG-INDS = []
	8
Reciprocal Lattice
    0.5000000000    0.5000000000    0.5000000000    0.125000
    0.4285714286    0.4285714286    0.4285714286    0.125000
    0.3571428571    0.3571428571    0.3571428571    0.125000
    0.2857142857    0.2857142857    0.2857142857    0.125000
    0.2142857143    0.2142857143    0.2142857143    0.125000
    0.1428571429    0.1428571429    0.1428571429    0.125000
    0.0714285714    0.0714285714    0.0714285714    0.125000
    0.0000000000    0.0000000000    0.0000000000    0.125000
get_kpath({'M':[0.375,0.375,0.5,21],'skip': [0,0,0]},{'G':[0.5,0.25,0.5],'M':[0,0,0]},{'L':[0.25,0.25,0.25],'K':[0,0,0]},n=11,outfile='kpt.txt')
Automatically generated using PivotPy with HSK-INDS = [0, 29, -1], LABELS = ['M', 'G', 'M|L', 'K'], SEG-INDS = [21, 29]

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[source]

str2kpath(kpath_str, n=5, weight=None, ibzkpt=None, outfile=None, rec_basis=None)

Get Kpath from a string of kpoints (Line-Mode like). Useful in Terminal.

  • Parameters

    • kpath_str: str, a multiline string similiar to line mode of KPOINTS, initial 4 lines are not required.
      • If you do not want to label a point, label it as 'skip' and it will be removed.
      • You can add an interger at end of a line to customize number of points in a given patch.
      • Each empty line breaks the path, so similar points before and after empty line are useless here.
    • n : int, number per length of body diagonal of rec_basis, this makes uniform steps based on distance between points.
    • weight : Float, if None, auto generates weights.
    • ibzkpt : Path to ibzkpt file, required for HSE calculations.
    • outfile: Path/to/file to write kpoints.
    • rec_basis: Reciprocal basis 3x3 array to use for calculating uniform points.
  • Example

    str2kpath('''0 0 0 !$\Gamma$ 3 0.25 0.25 0.25 !L''') Automatically generated using PivotPy with HSK-INDS = [0, -1], LABELS = ['$\Gamma$', 'L'], SEG-INDS = [] 3 Reciprocal Lattice 0.0000000000 0.0000000000 0.0000000000 0.333333 0.1250000000 0.1250000000 0.1250000000 0.333333 0.2500000000 0.2500000000 0.2500000000 0.333333

str2kpath('0 0 0 G 2\n 1 1 1 X\n1/2 1/2 1/2 skip',n=4)
Automatically generated using PivotPy with HSK-INDS = [0, 2], LABELS = ['G', 'X'], SEG-INDS = []
	5
Reciprocal Lattice
    0.0000000000    0.0000000000    0.0000000000    0.200000
    1.0000000000    1.0000000000    1.0000000000    0.200000
    1.0000000000    1.0000000000    1.0000000000    0.200000
    0.7500000000    0.7500000000    0.7500000000    0.200000
    0.5000000000    0.5000000000    0.5000000000    0.200000
_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
array([[ 2.49518523e-01, -1.44059586e-01, -1.01865510e-01],
       [ 0.00000000e+00,  2.88119173e-01, -1.01865511e-01],
       [ 0.00000000e+00, -4.46140330e-14,  3.05596531e-01]])

KPOINTS Mesh for Fermi Surface Calculations

  • The command get_kmesh generates a uniform grid in first (primitive) Brillouin zone.
  • You can generate cartesian grid as well which is easier in output plotting.

get_kmesh[source]

get_kmesh(poscar_data, *args, shift=0, weight=None, cartesian=False, ibzkpt=None, outfile=None, endpoint=True)

Note: Use pivotpy.POSCAR.get_kmesh to get k-mesh based on current POSCAR.

  • Generates uniform mesh of kpoints. Options are write to file, or return KPOINTS list.
  • Parameters
    • poscar_data: export_poscar() or export_vasprun().poscar().
    • *args: 1 or 3 integers which decide shape of mesh. If 1, mesh points equally spaced based on data from POSCAR.
    • shift : Only works if cartesian = False. Defualt is 0. Could be a number or list of three numbers to add to interval [0,1].
    • weight : Float, if None, auto generates weights.
    • cartesian: If True, generates cartesian mesh.
    • ibzkpt : Path to ibzkpt file, required for HSE calculations.
    • outfile: Path/to/file to write kpoints.
    • endpoint: Default True, include endpoints in mesh at edges away from origin.

If outfile = None, KPOINTS file content is printed.

mp = InvokeMaterialsProject()
out = mp.request('GaAs',max_sites=2)
get_kmesh(out[0].export_poscar(),3,shift= -0.5,cartesian = True)
Generated uniform mesh using PivotPy, GRID-SHAPE = [3,3,2]
	18
Cartesian
   -0.4390175213   -0.4161438362   -0.2228682831    0.055556
    0.0000000000   -0.4161438362   -0.2228682831    0.055556
    0.4390175213   -0.4161438362   -0.2228682831    0.055556
   -0.4390175213    0.0000000000   -0.2228682831    0.055556
    0.0000000000    0.0000000000   -0.2228682831    0.055556
    0.4390175213    0.0000000000   -0.2228682831    0.055556
   -0.4390175213    0.4161438362   -0.2228682831    0.055556
    0.0000000000    0.4161438362   -0.2228682831    0.055556
    0.4390175213    0.4161438362   -0.2228682831    0.055556
   -0.4390175213   -0.4161438362    0.2228682831    0.055556
    0.0000000000   -0.4161438362    0.2228682831    0.055556
    0.4390175213   -0.4161438362    0.2228682831    0.055556
   -0.4390175213    0.0000000000    0.2228682831    0.055556
    0.0000000000    0.0000000000    0.2228682831    0.055556
    0.4390175213    0.0000000000    0.2228682831    0.055556
   -0.4390175213    0.4161438362    0.2228682831    0.055556
    0.0000000000    0.4161438362    0.2228682831    0.055556
    0.4390175213    0.4161438362    0.2228682831    0.055556

order[source]

order(points, loop=True)

  • Returns indices of counterclockwise ordered vertices of a plane in 3D.
  • Parameters
    • points: numpy array of shape (N,3) or List[List(len=3)].
    • loop : Default is True and appends start point at end to make a loop.
  • Example

    pts = np.array([[1,0,3],[0,0,0],[0,1,2]]) inds = order(pts) pts[inds]

      array([[1, 2, 3],
             [0, 0, 0],
             [1, 0, 3]
             [0, 1, 2]])

rotation[source]

rotation(angle_deg, axis_vec)

Get a scipy Rotation object at given angle_deg around axis_vec. Usage: rot = rotation(60,[0,0,1]) rot.apply([1,1,1]) [-0.3660254 1.3660254 1.] #give this

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]
Interier Angle: 1.1071487177940433
Angle with x-axis: 1.5707963267948966
array([[0, 1, 2],
       [0, 0, 0],
       [1, 0, 3],
       [0, 1, 2]])

get_bz[source]

get_bz(path_pos=None, loop=True, digits=8, primitive=False)

  • Return required information to construct first Brillouin zone in form of tuple (basis, normals, vertices, faces).
  • Parameters

    • path_pos : POSCAR file path or list of 3 Real space vectors in 3D as list[list,list,list].
    • loop : If True, joins the last vertex of a BZ plane to starting vertex in order to complete loop.
    • digits : int, rounding off decimal places, no effect on intermediate calculations, just for pretty final results.
    • primitive: Defualt is False and returns Wigner-Seitz cell, If True returns parallelipiped in rec_basis.
  • Attributes

    • basis : get_bz().basis, recprocal lattice basis.
    • normals : get_bz().normals, all vertors that are perpendicular BZ faces/planes.
    • vertices: get_bz().vertices, all vertices of BZ, could be input into ConvexHull for constructing 3D BZ.
    • faces : get_bz().faces, vertices arranged into faces, could be input to Poly3DCollection of matplotlib for creating BZ from faces' patches.
    • specials : get_bz().specials, Data with attributes coords,kpoints and near in on-one correspondence for high symmetry KPOINTS in recirprocal coordinates space. near gives indices of nearest special points around a vertex. All vertices with z > 0 are included.
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)
[[-0.996  0.996  0.996]
 [ 0.996 -0.996  0.996]
 [ 0.996  0.996 -0.996]]
[-0.  0.  0.] --> [0. 0. 0.]
[-0.  -0.5 -0.5] --> [-0.996  0.     0.   ]
[-0.5 -0.5 -0.5] --> [-0.498 -0.498 -0.498]
[-0.  -0.5  0. ] --> [-0.498  0.498 -0.498]
[-0.   0.  -0.5] --> [-0.498 -0.498  0.498]
[0.5 0.  0. ] --> [-0.498  0.498  0.498]
[-0.5 -0.5  0. ] --> [ 0.     0.    -0.996]
[-0.5  0.  -0.5] --> [ 0.    -0.996  0.   ]
[-0.   0.5  0.5] --> [0.996 0.    0.   ]
[0.  0.5 0. ] --> [ 0.498 -0.498  0.498]

splot_bz[source]

splot_bz(bz_data, ax=None, plane=None, color='blue', fill=True, vectors=True, v3=False, vname='b', colormap='plasma', light_from=(1, 1, 1), alpha=0.4)

  • Plots matplotlib's static figure.
  • Parameters
    • bz_data : Output of get_bz.
    • fill : True by defult, determines whether to fill surface of BZ or not.
    • color : color to fill surface and stroke color.
    • vectors : Plots basis vectors, default is True.
    • v3 : Plots 3rd vector as well. Only works in 2D and when vectors=True.
    • plane : Default is None and plots 3D surface. Can take 'xy','yz','zx' to plot in 2D.
    • ax : Auto generated by default, 2D/3D axes, auto converts in 3D on demand as well.
    • vname : Default is b for reciprocal space, can set a for plotting cell as after get_bz(get_bz().basis) you get real space lattice back if primitive=True both times.
    • colormap : If None, single color is applied, only works in 3D and fill=True. Colormap is applied along z.
    • light_from: Point from where light is thrown on BZ planes, default is (1,1,1). Only works on plane in 3D.
    • alpha : Opacity of filling in range [0,1]. Increase for clear viewpoint.
  • Returns
    • ax : Matplotlib's 2D axes if plane=None.
    • ax3d : Matplotlib's 2D axes if plane is given.

Tip: splot_bz(rec_basis,primitive=True) will plot cell in real space.

import pivotpy.splots 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()
2022-09-02T11:42:37.518131 image/svg+xml Matplotlib v3.3.3, https://matplotlib.org/
<Figure size 648x144 with 0 Axes>

iplot_bz[source]

iplot_bz(bz_data, fill=True, color='rgba(168,204,216,0.4)', background='rgb(255,255,255)', vname='b', special_kpoints=True, alpha=0.4, ortho3d=True, fig=None)

  • Plots interactive figure showing axes,BZ surface, special points and basis, each of which could be hidden or shown.
  • Parameters
    • bz_data : Output of get_bz.
    • fill : True by defult, determines whether to fill surface of BZ or not.
    • color : color to fill surface 'rgba(168,204,216,0.4)` by default.
    • background : Plot background color, default is 'rgb(255,255,255)'.
    • vname : Default is b for reciprocal space, can set a for plotting cell as after get_bz(get_bz().basis) you get real space lattice back if primitive=True both times.
    • special_kpoints : True by default, determines whether to plot special points or not.
    • alpha : Opacity of BZ planes.
    • ortho3d : Default is True, decides whether x,y,z are orthogonal or perspective.
    • fig : (Optional) Plotly's go.Figure. If you want to plot on another plotly's figure, provide that.
  • Returns
    • fig : plotly.graph_object's Figure instance.

Tip: iplot_bz(rec_basis,primitive=True) will plot cell in real space.

  • Below is a BZ plot using command 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.
import pivotpy as pp
fig = iplot_bz(get_bz([[1,0,1],[1,1,0],[0,1,1]]),alpha=1)
pp.iplot2html(fig,modebar=False)

to_R3[source]

to_R3(basis, points)

Transforms coordinates of points (relative to non-othogonal basis) into orthogonal space.

  • Parameters
    • basis : Non-orthogonal basis of real or reciprocal space.
    • points: 3D points relative to basis, such as KPOINTS and Lattice Points.

Note: Do not use this function if points are Cartesian or provide identity basis.

to_basis[source]

to_basis(basis, coords)

Transforms coordinates of points (relative to othogonal basis) into basis space.

  • Parameters
    • basis : Non-orthogonal basis of real or reciprocal space.
    • points: 3D points relative to cartesian axes, such as KPOINTS and Lattice Points.
x = to_R3(_get_basis(hexa).inverted,[[1,1,1],[0.333,0.333,0.5]])
y = to_basis(_get_basis(hexa).inverted,x)
print(x,y)
[[1.         0.57735027 1.        ]
 [0.333      0.19225764 0.5       ]] [[1.    1.    1.   ]
 [0.333 0.333 0.5  ]]

kpoints2bz[source]

kpoints2bz(bz_data, kpoints, sys_info=None, primitive=False, shift=0)

Brings KPOINTS inside BZ. Applies to_R3 only if primitive=True.

  • Parameters
    • bz_data : Output of get_bz(), make sure use same value of primitive there and here.
    • kpoints : List or array of KPOINTS to transorm into BZ or R3.
    • sys_info : If given, returns kpoints using that information. Useful If kpoints are cartesian and you need to scale those.
    • primitive: Default is False and brings kpoints into regular BZ. If True, returns to_R3().
    • shift : This value is added to kpoints before any other operation, single number of list of 3 numbers for each direction.

Note: If kpoints are Cartesian, provide sys_info, otherwise it will go wrong.

import pivotpy as pp, numpy as np

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)

Example: Showing Energy as Color on BZ

In below figure, left side of colorbar is tricontourf and right side is scatter plot with color based on energy value of VBM.
For a more easier way of doing this, see pivotpy.SpinDataFrame and See Example

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.5,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, shift=0.5)
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')
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(cax=axs[2],cmap_or_clist='plasma', ticklabels=labels,vertical=True)
<AxesSubplot:>
2022-09-02T11:42:43.709111 image/svg+xml Matplotlib v3.3.3, https://matplotlib.org/

fix_sites[source]

fix_sites(poscar_data, tol=0.01, eqv_sites=False, translate=None)

Add equivalent sites to make a full data shape of lattice. Returns same data after fixing.

  • Parameters
    • poscar_data: Output of export_poscar or export_vasprun().poscar.
    • tol : Tolerance value. Default is 0.01.
    • eqv_sites: If True, add sites on edges and faces. If False, just fix coordinates, i.e. pos > 1 - tol -> pos - 1, useful for merging poscars to make slabs.
    • translate: A number(+/-) or list of three numbers to translate in a,b,c directions.

translate_poscar[source]

translate_poscar(poscar_data, offset)

Translate sites of a PPSCAR. Usully a farction of integarers like 1/2,1/4 etc.

  • Parameters
    • poscar_data: Output of export_poscar or export_vasprun().poscar.
    • offset: A number(+/-) or list of three numbers to translate in a,b,c directions.

get_pairs[source]

get_pairs(poscar_data, positions, r, tol=0.001)

Returns a tuple of Lattice (coords,pairs), so coords[pairs] given nearest site bonds.

  • Parameters
    • poscar_data: Output of export_poscar or export_vasprun().poscar.
    • positions: Array(N,3) of fractional positions of lattice sites. If coordinates positions, provide unity basis.
    • r : Cartesian distance between the pairs in units of Angstrom e.g. 1.2 -> 1.2E-10.
    • tol : Tolerance value. Default is 10^-3.

Lattice Plotting

iplot_lat[source]

iplot_lat(poscar_data, sizes=10, colors=None, bond_length=None, tol=0.01, bond_tol=0.001, eqv_sites=True, translate=None, line_width=4, edge_color='black', fill=False, alpha=0.4, ortho3d=True, fig=None)

Interactive plot of lattice.

  • Main Parameters
    • poscar_data: Output of export_poscar or export_vasprun().poscar.
    • sizes : Size of sites. Either one int/float or list equal to type of ions.
    • colors : Sequence of colors for each type. Automatically generated if not provided.
    • bond_length: Length of bond in fractional unit [0,1]. It is scaled to V^1/3 and auto calculated if not provides. Other parameters just mean what they seem to be.
import pivotpy as pp
fig=iplot_lat(evr.poscar,sizes=10,line_width=4,fill=False)
pp.iplot2html(fig,modebar=False)

splot_lat[source]

splot_lat(poscar_data, plane=None, sizes=50, colors=None, colormap=None, bond_length=None, tol=0.01, bond_tol=0.001, eqv_sites=True, translate=None, line_width=1, edge_color=(1, 0.5, 0, 0.4), vectors=True, v3=False, light_from=(1, 1, 1), fill=False, alpha=0.4, ax=None, alpha_points=0.7)

Static plot of lattice.

  • Main Parameters
    • poscar_data: Output of export_poscar or export_vasprun().poscar.
    • plane : Plane to plot. Either 'xy','xz','yz' or None for 3D plot.
    • sizes : Size of sites. Either one int/float or list equal to type of ions.
    • bond_length: Length of bond in fractional unit [0,1]. It is scaled to V^1/3 and auto calculated if not provides.
    • colors: Sequence of colors for each ion type. If None, automatically generated.
    • colormap: This is passed to splot_bz.
    • alpha_points: Opacity of points and bonds. Other parameters just mean what they seem to be.

Tip: Use plt.style.use('ggplot') for better 3D perception.

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=0.2,alpha=0.4,light_from=(1,-0.1,0),vectors=False,ax=axs[0],edge_color=(0.5,0.5,0,0.4), eqv_sites=True)
splot_lat(evr.poscar,sizes=40,plane='xy',fill=True,tol=0.1,alpha=1,light_from=(1,-0.1,0),vectors=False,ax=axs[1],colors=['r'],eqv_sites=True).set_axis_on()
axs[0].view_init(70,75)
2022-09-02T11:42:44.499440 image/svg+xml Matplotlib v3.3.3, https://matplotlib.org/

Joining Multiple POSCARS

This is useful to create slabs for interface calculations or even to make supercell in a straightforward way.

join_poscars[source]

join_poscars(poscar1, poscar2, direction='c', tol=0.01, system=None)

Joins two POSCARs in a given direction. In-plane lattice parameters are kept from poscar1 and basis of poscar2 parallel to direction is modified while volume is kept same.

  • Parameters
    • poscar1, poscar2: Base and secondary POSCARs respectivly. Output of export_poscar or similar object from other functions.
    • direction: The joining direction. It is general and can join in any direction along basis. Expect one of ['a','b','c'].
    • tol: Default is 0.01. It is used to bring sites near 1 to near zero in order to complete sites in plane. Vasp relaxation could move a point, say at 0.00100 to 0.99800 which is not useful while merging sites.
    • system: If system is given, it is written on top of file. Otherwise, it is infered from atomic species.
write_poscar(evr.poscar)
print("===================================================")
sd = get_selective_dynamics(evr.poscar,a=[(0,0.5)],c= [(0,0.5)])
write_poscar(evr.poscar,selective_dynamics=sd)
C2  # Exported from vasprun.xml
  0.99999999310600    
    2.4680310170146056    0.0000000000000000    0.0000000000000000
   -1.2340150085072996    2.1373770147350770    0.0000000000000000
    0.0000000000000000    0.0000000000000000   19.9982931378682345
  C
  2
Direct
   0.3333330000000000   0.6666670000000000   0.0000000000000000
   0.6666670000000000   0.3333330000000000   0.0000000000000000
===================================================
C2  # Exported from vasprun.xml
  0.99999999310600    
    2.4680310170146056    0.0000000000000000    0.0000000000000000
   -1.2340150085072996    2.1373770147350770    0.0000000000000000
    0.0000000000000000    0.0000000000000000   19.9982931378682345
  C
  2
Selective Dynamics
Direct
   0.3333330000000000   0.6666670000000000   0.0000000000000000   T  T  T
   0.6666670000000000   0.3333330000000000   0.0000000000000000   F  T  T

repeat_poscar[source]

repeat_poscar(poscar_data, n, direction)

Repeat a given POSCAR.

  • Parameters
    • path_poscar: Path/to/POSCAR or poscar data object.
    • n: Number of repetitions.
    • direction: Direction of repetition. Can be 'a', 'b' or 'c'.

scale_poscar[source]

scale_poscar(poscar_data, scale=(1, 1, 1), tol=0.01)

Create larger/smaller cell from a given POSCAR. Can be used to repeat a POSCAR with integer scale values.

  • Parameters
    • poscar_data: poscar data object.
    • scale: Tuple of three values along (a,b,c) vectors. int or float values. If number of sites are not as expected in output, tweak tol instead of scale. You can put a minus sign with tol to get more sites and plus sign to reduce sites.
    • tol: It is used such that site positions are blow 1 - tol, as 1 belongs to next cell, not previous one. Tip: scale = (2,2,2) enlarges a cell and next operation of (1/2,1/2,1/2) should bring original cell back. Caveat: A POSACR scaled with Non-integer values should only be used for visualization purposes, Not for any other opration such as making supercells, joining POSCARs.

rotate_poscar[source]

rotate_poscar(poscar_data, angle_deg, axis_vec)

Rotate a given POSCAR.

  • Parameters
    • path_poscar: Path/to/POSCAR or poscar data object.
    • angle_deg: Rotation angle in degrees.
    • axis_vec : (x,y,z) of axis about which rotation takes place. Axis passes through origin.

mirror_poscar[source]

mirror_poscar(poscar_data, direction)

Mirror a POSCAR in a given direction. Sometime you need it before joining two POSCARs

convert_poscar[source]

convert_poscar(poscar_data, atoms_mapping, basis_factor)

Convert a POSCAR to a similar structure of other atomic types or same type with strained basis. atoms_mapping is a dictionary of {old_atom: new_atom} like {'Ga':'Al'} will convert GaAs to AlAs structure. basis_factor is a scaling factor multiplied with basis vectors, single value (useful for conversion to another type) or list of three values to scale along (a,b,c) vectors (useful for strained structures).

get_transform_matrix[source]

get_transform_matrix(poscar_data, target_basis)

Returns a transformation matrix that gives target_bsis when applied on basis of current lattice. Useful in transforming crystal structure.

transform_poscar[source]

transform_poscar(poscar_data, transform_matrix, repeat_given=[2, 2, 2], tol=0.01)

Transform a POSCAR with a given transformation matrix. Use get_transform_matrix to get transformation matrix from one basis to another. repeat_given is used to repeat the POSCAR before applying transformation matrix to include all possible sites in resultant cell.

add_vaccum[source]

add_vaccum(poscar_data, thickness, direction, left=False)

Add vacuum to a POSCAR.

  • Parameters
    • poscar_data: poscar data object.
    • thickness: Thickness of vacuum in Angstrom.
    • direction: Direction of vacuum. Can be 'a', 'b' or 'c'.
    • left: If True, vacuum is added to left of sites. By default, vacuum is added to right of sites.
import numpy as np
import pivotpy.parser as vp
pscr = export_poscar('POSCAR')
ax = splot_lat(pscr,plane='xy',colors='r',sizes=100,line_width=5)
scaled = scale_poscar(pscr, scale=[3,3,3])
splot_lat(scaled, ax=ax,plane='xy',colors='b',bond_length=0.1,tol=1e-5,line_width=2)
new_scaled = scale_poscar(scaled, scale=[1/3,1/3,1/3],tol=1e-3)
splot_lat(new_scaled,ax=ax,plane='xy',colormap='viridis_r',sizes=15,line_width=0.5)

trans = transform_poscar(scaled, get_transform_matrix(scaled, -scaled.basis))
splot_lat(trans,ax=ax,plane='xy',colormap='viridis_r',sizes=15,colors='g',line_width=0.5)
<AxesSubplot:xlabel='x', ylabel='y'>
2022-09-02T11:42:45.159029 image/svg+xml Matplotlib v3.3.3, https://matplotlib.org/

transpose_poscar[source]

transpose_poscar(poscar_data, axes=[1, 0, 2])

Transpose a POSCAR by switching basis from [0,1,2] -> axes. By Default, x and y are transposed.

add_atoms[source]

add_atoms(poscar_data, name, positions)

Add atoms with a name to a POSCAR at given positions in fractional coordinates.

  Index 
  Example 
  StaticPlots 
  InteractivePlots 
  SpinProjectedSurfaces 
  StructureIO● 
  Widgets 
  MainAPI