@@ -20,7 +20,7 @@ class is written.
2020import scipy .constants as const
2121from monty .json import MSONable
2222
23- from pymatgen .core import Structure
23+ from pymatgen .core import Lattice , Structure
2424from pymatgen .core .periodic_table import Element
2525from pymatgen .core .units import bohr_to_ang
2626from pymatgen .io .jdftx .generic_tags import AbstractTag , BoolTagContainer , DumpTagContainer , MultiformatTag , TagContainer
@@ -788,17 +788,8 @@ def from_jdftxinfile(cls, jdftxinfile: JDFTXInfile, sort_structure: bool = False
788788 Returns:
789789 JDFTXStructure: The created JDFTXStructure object.
790790 """
791- jl = jdftxinfile ["lattice" ]
792- lattice = np .zeros ([3 , 3 ])
793- for i in range (3 ):
794- for j in range (3 ):
795- lattice [i ][j ] += float (jl [f"R{ i } { j } " ])
796- if "latt-scale" in jdftxinfile :
797- latt_scale = np .array ([jdftxinfile ["latt-scale" ][x ] for x in ["s0" , "s1" , "s2" ]])
798- lattice *= latt_scale
799- lattice = lattice .T # convert to row vector format
800- lattice *= const .value ("Bohr radius" ) * 10 ** 10 # Bohr radius in Ang; convert to Ang
801791
792+ lattice = _infile_to_pmg_lattice (jdftxinfile )
802793 atomic_symbols = [x ["species-id" ] for x in jdftxinfile ["ion" ]]
803794 coords = np .array ([[x ["x0" ], x ["x1" ], x ["x2" ]] for x in jdftxinfile ["ion" ]])
804795 coords *= const .value ("Bohr radius" ) * 10 ** 10 # Bohr radius in Ang; convert to Ang
@@ -1016,3 +1007,58 @@ def _multi_getattr(varbase: Any, varname: str):
10161007 for var in varlist :
10171008 varbase = getattr (varbase , var )
10181009 return varbase
1010+
1011+
1012+ def _infile_to_pmg_lattice (jdftxinfile : JDFTXInfile ) -> Lattice :
1013+ jl = jdftxinfile ["lattice" ]
1014+ if "R00" not in jl :
1015+ return _infile_special_format_to_pmg_lattice (jdftxinfile )
1016+ return _infile_matrix_format_to_pmg_lattice (jdftxinfile )
1017+
1018+
1019+ def _infile_special_format_to_pmg_lattice (jdftxinfile : JDFTXInfile ) -> Lattice :
1020+ jl = jdftxinfile ["lattice" ]
1021+ if "modification" in jl :
1022+ raise NotImplementedError ("Special case lattices with modification not implemented yet" )
1023+ # Second boolean to check if latt-scale was passed but does nothing
1024+ if ("latt-scale" in jl ) and (not all (np .isclose (float (x ), 1 ) in jl for x in ["s0" , "s1" , "s2" ])):
1025+ raise NotImplementedError ("latt-scale for special case lattices not implemented yet" )
1026+ if "Monoclinic" in jl :
1027+ lattice = Lattice .monoclinic (jl ["a" ], jl ["b" ], jl ["c" ], jl ["beta" ])
1028+ elif "Orthorhombic" in jl :
1029+ lattice = Lattice .orthorhombic (jl ["a" ], jl ["b" ], jl ["c" ])
1030+ elif "Cubic" in jl :
1031+ lattice = Lattice .cubic (jl ["a" ])
1032+ elif "Hexagonal" in jl :
1033+ lattice = Lattice .hexagonal (jl ["a" ], jl ["c" ])
1034+ elif "Rhombohedral" in jl :
1035+ lattice = Lattice .rhombohedral (jl ["a" ], jl ["alpha" ])
1036+ elif "Tetragonal" in jl :
1037+ lattice = Lattice .tetragonal (jl ["a" ], jl ["c" ])
1038+ elif "Triclinic" in jl :
1039+ lattice = Lattice .from_parameters (jl ["a" ], jl ["b" ], jl ["c" ], jl ["alpha" ], jl ["beta" ], jl ["gamma" ])
1040+ else :
1041+ raise ValueError (f"Unable to convert JDFTX lattice tag { jl } to pymatgen Lattice object" )
1042+ return lattice
1043+
1044+
1045+ def _infile_matrix_format_to_pmg_lattice (jdftxinfile : JDFTXInfile ) -> Lattice :
1046+ """Convert JDFTX lattice tag to pymatgen Lattice object.
1047+
1048+ Args:
1049+ jl (dict[str, Any]): JDFTX lattice tag.
1050+
1051+ Returns:
1052+ Lattice: Pymatgen Lattice object.
1053+ """
1054+ _lattice = np .zeros ([3 , 3 ])
1055+ jl = jdftxinfile ["lattice" ]
1056+ for i in range (3 ):
1057+ for j in range (3 ):
1058+ _lattice [i ][j ] += float (jl [f"R{ i } { j } " ])
1059+ if "latt-scale" in jdftxinfile :
1060+ latt_scale = np .array ([jdftxinfile ["latt-scale" ][x ] for x in ["s0" , "s1" , "s2" ]])
1061+ _lattice *= latt_scale
1062+ _lattice = _lattice .T # convert to row vector format
1063+ _lattice *= const .value ("Bohr radius" ) * 10 ** 10 # Bohr radius in Ang; convert to Ang
1064+ return Lattice (_lattice )
0 commit comments