Skip to content

Commit

Permalink
Enhance str_to_str function to support unit conversion and handle tup…
Browse files Browse the repository at this point in the history
…le input
  • Loading branch information
calvinp0 committed Dec 22, 2024
1 parent ec38e67 commit ed4b9c7
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions arc/species/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@

def str_to_str(xyz_str: str,
reverse_atoms: bool = False,
units: str = 'angstrom',
convert_to: str = 'angstrom',
project_directory: Optional[str] = None
) -> str:
Expand All @@ -69,6 +70,8 @@ def str_to_str(xyz_str: str,
Returns: str
The converted string xyz format.
"""
if isinstance(xyz_str, tuple):
xyz_str = '\n'.join(xyz_str)
if isinstance(xyz_str, list):
xyz_str = '\n'.join(xyz_str)
if not isinstance(xyz_str, str):
Expand All @@ -80,13 +83,16 @@ def str_to_str(xyz_str: str,
BOHR_TO_ANGSTROM = 0.529177
ANGSTROM_TO_BOHR = 1.8897259886

if convert_to.lower() == 'angstrom':
conversion_factor = BOHR_TO_ANGSTROM
elif convert_to.lower() == 'bohr':
if units.lower() == 'angstrom' and convert_to.lower() == 'angstrom':
conversion_factor = 1
elif units.lower() == 'bohr' and convert_to.lower() == 'bohr':
conversion_factor = 1
elif units.lower() == 'angstrom' and convert_to.lower() == 'bohr':
conversion_factor = ANGSTROM_TO_BOHR
elif units.lower() == 'bohr' and convert_to.lower() == 'angstrom':
conversion_factor = BOHR_TO_ANGSTROM
else:
raise ValueError("Invalid target unit. Choose 'angstrom' or 'bohr'.")

raise ConverterError("Invalid target unit. Choose 'angstrom' or 'bohr'.")

processed_lines = list()
# Split the string into lines
Expand All @@ -111,8 +117,8 @@ def str_to_str(xyz_str: str,
y = float(y_str) * conversion_factor
z = float(z_str) * conversion_factor

except ValueError:
raise ConverterError(f'Could not convert {x_str}, {y_str}, or {z_str} to floats.')
except ValueError as e:
raise ConverterError(f'Could not convert {x_str}, {y_str}, or {z_str} to floats.') from e

if reverse_atoms and atom_first:
formatted_line = f'{x} {y} {z} {atom}'
Expand Down

0 comments on commit ed4b9c7

Please sign in to comment.