refactor: modernize Molecule class with cached properties and vectorized operations

- Replace manual property updates with @cached_property for total_mass, com, and inertia_tensor
- Introduce invalidate_computed_properties decorator to auto-invalidate cached properties on atom changes
- Vectorize distances_between_atoms, sizes_of_molecule, and minimum_distance calculations using numpy
- Unify and clarify center of mass and standard orientation methods (move_center_of_mass_to_origin, rotate_to_standard_orientation)
- Remove redundant or outdated code, improve typing and error handling
- Update dependent files and tests to use new method names and behaviors
This commit is contained in:
2026-02-28 09:58:42 -03:00
parent d400970e8f
commit a5504b0435
7 changed files with 156 additions and 278 deletions

View File

@@ -41,7 +41,7 @@ class TestMolecule(unittest.TestCase):
Atom(lbl=1, na=1, rx=1.0, ry=1.0, rz=1.0, chg=1.0, eps=1.0, sig=1.0)
)
mol.center_of_mass_to_origin()
mol.move_center_of_mass_to_origin()
npt.assert_equal(mol.com, [0, 0, 0])
@@ -68,11 +68,14 @@ class TestMolecule(unittest.TestCase):
Atom(lbl=1, na=1, rx=1.0, ry=1.0, rz=1.0, chg=1.0, eps=1.0, sig=1.0)
)
expected_distance_between_atoms = [[1.73205081], [1.73205081]]
actual_distance_between_atoms = mol.distances_between_atoms()
expected = [
[0.0, 1.73205081],
[1.73205081, 0.0]
]
actual = mol.distances_between_atoms()
npt.assert_almost_equal(
expected_distance_between_atoms, actual_distance_between_atoms
expected, actual
)
def test_inertia_tensor(self):
@@ -91,7 +94,7 @@ class TestMolecule(unittest.TestCase):
[-0.50395, -0.50395, 1.0079],
]
actual_inertia_tensor = mol.inertia_tensor()
actual_inertia_tensor = mol.inertia_tensor
npt.assert_equal(expected_inertia_tensor, actual_inertia_tensor)
@@ -163,7 +166,7 @@ class TestMolecule(unittest.TestCase):
Atom(lbl=1, na=1, rx=1.0, ry=1.0, rz=1.0, chg=1.0, eps=1.0, sig=1.0)
)
mol.standard_orientation()
mol.rotate_to_standard_orientation()
expected_position = [0.0, 0.0, 0.0]