.. _lattice_example: lattice_example ======================== .. code:: ipython3 from pprint import pprint .. code:: ipython3 import logging logging.basicConfig(filename='lattice_example.log',level=logging.DEBUG) The ``lattice`` object keeps track of the lattice used in ``Structure`` object .. code:: ipython3 import pymatgen_core.core.lattice as lattice .. code:: ipython3 lat = lattice.Lattice() .. code:: ipython3 print(lat) print(lat.unit_conf['length']) The default size of the lattice is 100.0 angstroms The lattice also has lattice constants .. code:: ipython3 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 .. code:: ipython3 lat.pbcs = [True,True,True] .. code:: ipython3 pos_i = [25.0,25.0,25.0] pos_j = [-50.0,25.0,25.0] .. code:: ipython3 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 .. code:: ipython3 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 .. code:: ipython3 lat.pbcs = [False,False,False] .. code:: ipython3 print(lat.delta_pos(pos_i,pos_j)) The size of the lattice can be changed using the ``matrix`` or the ``constants`` ``setter`` .. code:: ipython3 lat.matrix = [ 12,0,0,0,12,0,0,0,12 ] .. code:: ipython3 print(lat.matrix) print(lat.constants) print(lat.unit_conf['length']) To set to a triclinic lattice .. code:: ipython3 lat.constants = [ 12,8,15,60.0,120.0,80.0 ] .. code:: ipython3 print(lat.matrix) print(lat.constants) print(lat.unit_conf['length']) Let’s turn pbcs’s back on and calculate the distance .. code:: ipython3 lat.pbcs = [True,True,True] .. code:: ipython3 print(pos_i,pos_j) .. code:: ipython3 dr_ij,mag_dr_ij = lat.delta_pos(pos_i,pos_j) print(dr_ij,mag_dr_ij) Change the units to ``nm`` .. code:: ipython3 lat.update_units({'length':'nm'}) .. code:: ipython3 print(lat.matrix) print(lat.constants) print(lat.unit_conf['length']) If you need your angles in radians .. code:: ipython3 lat.update_units({'angle':'radian'}) .. code:: ipython3 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 .. code:: ipython3 lat_json = lat.export_json('lat_ex',write_file=True) Delete the lattice object .. code:: ipython3 del lat Create a new blank object .. code:: ipython3 lat = lattice.Lattice() And read in the file to get the properties of the lattice back .. code:: ipython3 lat.import_json('lat_ex',read_file=True) Handy for saving or exporting to javascript .. code:: ipython3 print(lat.matrix) print(lat.constants) print(lat.unit_conf['length'],lat.unit_conf['angle'])