Skip to content

Commit

Permalink
add: support for atomic number instead of element symbols
Browse files Browse the repository at this point in the history
  • Loading branch information
Johannes Steinmetzer committed Oct 9, 2023
1 parent 383ee43 commit 2a4a37f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 7 deletions.
29 changes: 24 additions & 5 deletions pysisyphus/Geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@
from pysisyphus.constants import BOHR2ANG
from pysisyphus.hessian_proj import get_hessian_projector, inertia_tensor
from pysisyphus.elem_data import (
MASS_DICT,
ISOTOPE_DICT,
ATOMIC_NUMBERS,
COVALENT_RADII as CR,
INV_ATOMIC_NUMBERS,
ISOTOPE_DICT,
MASS_DICT,
VDW_RADII as VDWR,
)
from pysisyphus.helpers_pure import (
Expand Down Expand Up @@ -57,6 +58,22 @@
from pysisyphus.xyzloader import make_xyz_str


def normalize_atoms(atoms) -> tuple[str]:
atomic_numbers = set(INV_ATOMIC_NUMBERS.keys())
_atoms = list()
for atom in atoms:
try:
atom_int = int(atom)
if atom_int in atomic_numbers:
atom = INV_ATOMIC_NUMBERS[atom_int]

Check warning on line 68 in pysisyphus/Geometry.py

View check run for this annotation

Codecov / codecov/patch

pysisyphus/Geometry.py#L67-L68

Added lines #L67 - L68 were not covered by tests
except ValueError:

Check notice

Code scanning / CodeQL

Empty except Note

'except' clause does nothing but pass and there is no explanatory comment.
pass
# Was atom.capitalize() before ...
atom = atom.lower()
_atoms.append(atom)
return tuple(_atoms)


class Geometry:
coord_types = {
"cart": None,
Expand Down Expand Up @@ -121,7 +138,7 @@ def __init__(
name : str, optional
Verbose name of the geometry, e.g. methanal or water. Used for printing
"""
self.atoms = tuple([atom.capitalize() for atom in atoms])
self.atoms = normalize_atoms(atoms)
# self._coords always holds cartesian coordinates.
self._coords = np.array(coords, dtype=float).flatten()
assert self._coords.size == (3 * len(self.atoms)), (
Expand Down Expand Up @@ -208,8 +225,10 @@ def moving_atoms_jmol(self):

@property
def sum_formula(self):
unique_atoms = sorted(set(self.atoms))
counter = Counter(self.atoms)
atoms = self.atoms
atoms = [atom.capitalize() for atom in atoms]
unique_atoms = sorted(set(atoms))
counter = Counter(atoms)
atoms = list()
num_strs = list()

Expand Down
2 changes: 1 addition & 1 deletion pysisyphus/io/pdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ def atoms_coords_to_pdb_str(atoms, coords, fragments=None, resname="", conect=Tr
serial = 1
for resSeq, fragment in enumerate(fragments, 1):
for id_ in fragment:
name = atoms[id_]
name = atoms[id_].capitalize()
xyz = coords3d_ang[id_]
line = hetatm_fmt.format(
serial,
Expand Down
2 changes: 1 addition & 1 deletion tests/test_replacements/test_replacements.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@ def test_replace_atom_with_atom():
geom = geom_loader("lib:methane.xyz")
repl_geom = Geometry(("I",), (0.0, 0.0, 0.0))
union = replace_atom(geom, 3, repl_geom, 0)
assert union.atoms[3] == "I"
assert union.atoms[3] == "i"

0 comments on commit 2a4a37f

Please sign in to comment.