Skip to content

Commit

Permalink
fixes to consistent ion coordinate units - added missing bohr_to_ang …
Browse files Browse the repository at this point in the history
…when reading initial coordinates in joustructures.py (_get_initial_coords), added missing 1/bohr_to_ang when creating a JDFTXInfile from a pmg Structure, added specification of coords-type when creating JDFTXInfile from a pmg Structure, added keyword argument for from_structure to store ion position tags as fractional or cartesian coordinates
  • Loading branch information
benrich37 committed Dec 20, 2024
1 parent fe6a8b9 commit bcc9d1d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
14 changes: 11 additions & 3 deletions src/pymatgen/io/jdftx/inputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class is written.

from pymatgen.core import Structure
from pymatgen.core.periodic_table import Element
from pymatgen.core.units import bohr_to_ang
from pymatgen.io.jdftx.generic_tags import AbstractTag, BoolTagContainer, DumpTagContainer, MultiformatTag, TagContainer
from pymatgen.io.jdftx.jdftxinfile_master_format import (
__PHONON_TAGS__,
Expand Down Expand Up @@ -315,6 +316,7 @@ def from_structure(
cls,
structure: Structure,
selective_dynamics: ArrayLike | None = None,
write_cart_coords: bool = False,
) -> JDFTXInfile:
"""Create a JDFTXInfile object from a pymatgen Structure.
Expand All @@ -327,6 +329,7 @@ def from_structure(
JDFTXInfile: The created JDFTXInfile object.
"""
jdftxstructure = JDFTXStructure(structure, selective_dynamics)
jdftxstructure.write_cart_coords = write_cart_coords
return cls.from_jdftxstructure(jdftxstructure)

@classmethod
Expand Down Expand Up @@ -652,6 +655,7 @@ class JDFTXStructure(MSONable):
structure: Structure = None
selective_dynamics: ArrayLike | None = None
sort_structure: bool = False
write_cart_coords: bool = False

def __post_init__(self) -> None:
"""Post init function for JDFTXStructure.
Expand Down Expand Up @@ -777,7 +781,7 @@ def from_jdftxinfile(cls, jdftxinfile: JDFTXInfile, sort_structure: bool = False
)
return cls(struct, selective_dynamics, sort_structure=sort_structure)

def get_str(self, in_cart_coords: bool = False) -> str:
def get_str(self, in_cart_coords: bool | None = None) -> str:
"""Return a string to be written as JDFTXInfile tags.
Allows extra options as compared to calling str(JDFTXStructure) directly.
Expand All @@ -788,19 +792,23 @@ def get_str(self, in_cart_coords: bool = False) -> str:
Returns:
str: Representation of JDFTXInfile structure tags.
"""
jdftx_tag_dict = {}
jdftx_tag_dict: dict[str, Any] = {}

if in_cart_coords is None:
in_cart_coords = self.write_cart_coords

lattice = np.copy(self.structure.lattice.matrix)
lattice = lattice.T # transpose to get into column-vector format
lattice /= const.value("Bohr radius") * 10**10 # Bohr radius in Ang; convert to Bohr

jdftx_tag_dict["lattice"] = lattice
jdftx_tag_dict["ion"] = []
jdftx_tag_dict["coords-type"] = "Cartesian" if in_cart_coords else "Lattice"
valid_labels = [
value.symbol for key, value in Element.__dict__.items() if not key.startswith("_") and not callable(value)
]
for i, site in enumerate(self.structure):
coords = site.coords if in_cart_coords else site.frac_coords
coords = site.coords * (1 / bohr_to_ang) if in_cart_coords else site.frac_coords
sd = self.selective_dynamics[i] if self.selective_dynamics is not None else 1
label = site.label
# TODO: This is needlessly complicated, simplify this
Expand Down
2 changes: 2 additions & 0 deletions src/pymatgen/io/jdftx/joutstructures.py
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,8 @@ def _get_initial_coords(pre_out_slice: list[str]) -> np.ndarray:
coords_type = pre_out_slice[coords_type_lines[0]].strip().split()[1]
if coords_type.lower() != "cartesian":
coords = np.dot(coords, _get_initial_lattice(pre_out_slice))
else:
coords *= bohr_to_ang
return coords


Expand Down

0 comments on commit bcc9d1d

Please sign in to comment.