Skip to content

Commit 2a4a37f

Browse files
author
Johannes Steinmetzer
committed
add: support for atomic number instead of element symbols
1 parent 383ee43 commit 2a4a37f

File tree

3 files changed

+26
-7
lines changed

3 files changed

+26
-7
lines changed

pysisyphus/Geometry.py

+24-5
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,11 @@
2323
from pysisyphus.constants import BOHR2ANG
2424
from pysisyphus.hessian_proj import get_hessian_projector, inertia_tensor
2525
from pysisyphus.elem_data import (
26-
MASS_DICT,
27-
ISOTOPE_DICT,
2826
ATOMIC_NUMBERS,
2927
COVALENT_RADII as CR,
28+
INV_ATOMIC_NUMBERS,
29+
ISOTOPE_DICT,
30+
MASS_DICT,
3031
VDW_RADII as VDWR,
3132
)
3233
from pysisyphus.helpers_pure import (
@@ -57,6 +58,22 @@
5758
from pysisyphus.xyzloader import make_xyz_str
5859

5960

61+
def normalize_atoms(atoms) -> tuple[str]:
62+
atomic_numbers = set(INV_ATOMIC_NUMBERS.keys())
63+
_atoms = list()
64+
for atom in atoms:
65+
try:
66+
atom_int = int(atom)
67+
if atom_int in atomic_numbers:
68+
atom = INV_ATOMIC_NUMBERS[atom_int]
69+
except ValueError:
70+
pass
71+
# Was atom.capitalize() before ...
72+
atom = atom.lower()
73+
_atoms.append(atom)
74+
return tuple(_atoms)
75+
76+
6077
class Geometry:
6178
coord_types = {
6279
"cart": None,
@@ -121,7 +138,7 @@ def __init__(
121138
name : str, optional
122139
Verbose name of the geometry, e.g. methanal or water. Used for printing
123140
"""
124-
self.atoms = tuple([atom.capitalize() for atom in atoms])
141+
self.atoms = normalize_atoms(atoms)
125142
# self._coords always holds cartesian coordinates.
126143
self._coords = np.array(coords, dtype=float).flatten()
127144
assert self._coords.size == (3 * len(self.atoms)), (
@@ -208,8 +225,10 @@ def moving_atoms_jmol(self):
208225

209226
@property
210227
def sum_formula(self):
211-
unique_atoms = sorted(set(self.atoms))
212-
counter = Counter(self.atoms)
228+
atoms = self.atoms
229+
atoms = [atom.capitalize() for atom in atoms]
230+
unique_atoms = sorted(set(atoms))
231+
counter = Counter(atoms)
213232
atoms = list()
214233
num_strs = list()
215234

pysisyphus/io/pdb.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ def atoms_coords_to_pdb_str(atoms, coords, fragments=None, resname="", conect=Tr
212212
serial = 1
213213
for resSeq, fragment in enumerate(fragments, 1):
214214
for id_ in fragment:
215-
name = atoms[id_]
215+
name = atoms[id_].capitalize()
216216
xyz = coords3d_ang[id_]
217217
line = hetatm_fmt.format(
218218
serial,

tests/test_replacements/test_replacements.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,4 @@ def test_replace_atom_with_atom():
3030
geom = geom_loader("lib:methane.xyz")
3131
repl_geom = Geometry(("I",), (0.0, 0.0, 0.0))
3232
union = replace_atom(geom, 3, repl_geom, 0)
33-
assert union.atoms[3] == "I"
33+
assert union.atoms[3] == "i"

0 commit comments

Comments
 (0)