lattice_example¶
from pprint import pprint
import logging
logging.basicConfig(filename='lattice_example.log',level=logging.DEBUG)
The lattice
object keeps track of the lattice used in Structure
object
import pymatgen_core.core.lattice as lattice
lat = lattice.Lattice()
print(lat)
print(lat.unit_conf['length'])
The default size of the lattice is 100.0 angstroms
The lattice also has lattice constants
print(lat.constants)
print(lat.unit_conf['length'])
Which are returned as [a,b,c,alpha,beta,gamma]
We can calculate the distance between two points in the lattice
Let’s turn on periodic boundary conditions
lat.pbcs = [True,True,True]
pos_i = [25.0,25.0,25.0]
pos_j = [-50.0,25.0,25.0]
dr_ij = lat.d_pos(pos_i,pos_j)
print(dr_ij)
If we want a tuple of the vector and the magnitude we can use
dr_ij,mag_dr_ij = lat.delta_pos(pos_i,pos_j)
print(dr_ij,mag_dr_ij)
We can also turn pbcs off and calculate the distance
lat.pbcs = [False,False,False]
print(lat.delta_pos(pos_i,pos_j))
The size of the lattice can be changed using the matrix
or the
constants
setter
lat.matrix = [ 12,0,0,0,12,0,0,0,12 ]
print(lat.matrix)
print(lat.constants)
print(lat.unit_conf['length'])
To set to a triclinic lattice
lat.constants = [ 12,8,15,60.0,120.0,80.0 ]
print(lat.matrix)
print(lat.constants)
print(lat.unit_conf['length'])
Let’s turn pbcs’s back on and calculate the distance
lat.pbcs = [True,True,True]
print(pos_i,pos_j)
dr_ij,mag_dr_ij = lat.delta_pos(pos_i,pos_j)
print(dr_ij,mag_dr_ij)
Change the units to nm
lat.update_units({'length':'nm'})
print(lat.matrix)
print(lat.constants)
print(lat.unit_conf['length'])
If you need your angles in radians
lat.update_units({'angle':'radian'})
print(lat.matrix)
print(lat.constants)
print(lat.unit_conf['length'],lat.unit_conf['angle'])
We can export the lattice object as json object and dump it into a file
lat_json = lat.export_json('lat_ex',write_file=True)
Delete the lattice object
del lat
Create a new blank object
lat = lattice.Lattice()
And read in the file to get the properties of the lattice back
lat.import_json('lat_ex',read_file=True)
Handy for saving or exporting to javascript
print(lat.matrix)
print(lat.constants)
print(lat.unit_conf['length'],lat.unit_conf['angle'])