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:
29
diceplayer/utils/cache.py
Normal file
29
diceplayer/utils/cache.py
Normal file
@@ -0,0 +1,29 @@
|
||||
from functools import cached_property
|
||||
|
||||
|
||||
def invalidate_computed_properties():
|
||||
"""
|
||||
Decorator function to invalidate the cached properties of the molecule when a new atom is added
|
||||
|
||||
Args:
|
||||
properties (list[str]): list of the names of the properties to be invalidated
|
||||
"""
|
||||
|
||||
def get_cached_properies(cls: type) -> set[str]:
|
||||
return {
|
||||
name
|
||||
for name, value in cls.__dict__.items()
|
||||
if isinstance(value, cached_property)
|
||||
}
|
||||
|
||||
def decorator(func):
|
||||
def wrapper(self, *args, **kwargs):
|
||||
result = func(self, *args, **kwargs)
|
||||
for prop in get_cached_properies(self.__class__):
|
||||
if hasattr(self, prop):
|
||||
delattr(self, prop)
|
||||
return result
|
||||
|
||||
return wrapper
|
||||
|
||||
return decorator
|
||||
@@ -13,6 +13,8 @@ import time
|
||||
BOHR2ANG: Final[float] = 0.52917721092
|
||||
ANG2BOHR: Final[float] = 1 / BOHR2ANG
|
||||
|
||||
EA_2_DEBYE = 1 / 0.20819434
|
||||
|
||||
|
||||
####################################### functions ######################################
|
||||
|
||||
|
||||
Reference in New Issue
Block a user