refactor: unify and modernize periodic table utilities and typing
- Replace atomsymb and atommass tuples with AtomInfo dataclass and PTable enum for atomic data - Refactor all usages to access atomic symbol and mass via PTable methods - Remove nptyping dependency, switch to numpy.typing for type annotations - Update molecule and atom classes to use new typing and atomic data access - Bump numpy version to 2.x and remove nptyping from dependencies
This commit is contained in:
@@ -1,52 +1,23 @@
|
||||
from diceplayer.utils.ptable import atommass
|
||||
from diceplayer.utils.ptable import PTable
|
||||
|
||||
from dataclasses import dataclass
|
||||
|
||||
|
||||
@dataclass
|
||||
class Atom:
|
||||
"""
|
||||
Atom class declaration. This class is used throughout the DicePlayer program to represent atoms.
|
||||
|
||||
Atributes:
|
||||
lbl (int): Dice derived variable used to represent atoms with identical energies and simetric positions.
|
||||
na (int): Atomic number of the represented atom.
|
||||
rx (float): x cartesian coordinates of the represented atom.
|
||||
ry (float): y cartesian coordinates of the represented atom.
|
||||
rz (float): z cartesian coordinates of the represented atom.
|
||||
chg (float): charge of the represented atom.
|
||||
eps (float): quantum number epsilon of the represented atom.
|
||||
sig (float): quantum number sigma of the represented atom.
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
lbl: int,
|
||||
na: int,
|
||||
rx: float,
|
||||
ry: float,
|
||||
rz: float,
|
||||
chg: float,
|
||||
eps: float,
|
||||
sig: float,
|
||||
) -> None:
|
||||
"""
|
||||
The constructor function __init__ is used to create new instances of the Atom class.
|
||||
lbl: int
|
||||
na: int
|
||||
rx: float
|
||||
ry: float
|
||||
rz: float
|
||||
chg: float
|
||||
eps: float
|
||||
sig: float
|
||||
|
||||
Args:
|
||||
lbl (int): Dice derived variable used to represent atoms with identical energies and simetric positions.
|
||||
na (int): Atomic number of the represented atom.
|
||||
rx (float): x cartesian coordinates of the represented atom.
|
||||
ry (float): y cartesian coordinates of the represented atom.
|
||||
rz (float): z cartesian coordinates of the represented atom.
|
||||
chg (float): charge of the represented atom.
|
||||
eps (float): quantum number epsilon of the represented atom.
|
||||
sig (float): quantum number sigma of the represented atom.
|
||||
"""
|
||||
|
||||
self.lbl = lbl
|
||||
self.na = na
|
||||
self.rx = rx
|
||||
self.ry = ry
|
||||
self.rz = rz
|
||||
self.chg = chg
|
||||
self.eps = eps
|
||||
self.sig = sig
|
||||
self.mass = atommass[self.na]
|
||||
@property
|
||||
def mass(self) -> float:
|
||||
return PTable.get_atomic_mass(self.na)
|
||||
|
||||
@@ -7,11 +7,12 @@ if TYPE_CHECKING:
|
||||
from nptyping import Float, NDArray, Shape
|
||||
|
||||
from diceplayer import logger
|
||||
from diceplayer.environment.atom import Atom
|
||||
from diceplayer.environment import Atom
|
||||
from diceplayer.utils.misc import BOHR2ANG
|
||||
from diceplayer.utils.ptable import ghost_number
|
||||
from diceplayer.utils.ptable import GHOST_NUMBER
|
||||
|
||||
import numpy as np
|
||||
import numpy.typing as npt
|
||||
from numpy.linalg import linalg
|
||||
from typing_extensions import Any, List, Tuple, Union
|
||||
|
||||
@@ -26,12 +27,12 @@ class Molecule:
|
||||
Atributes:
|
||||
molname (str): The name of the represented molecule
|
||||
atom (List[Atom]): List of atoms of the represented molecule
|
||||
position (NDArray[Any, Any]): The position relative to the internal atoms of the represented molecule
|
||||
energy (NDArray[Any, Any]): The energy of the represented molecule
|
||||
gradient (NDArray[Any, Any]): The first derivative of the energy relative to the position
|
||||
hessian (NDArray[Any, Any]): The second derivative of the energy relative to the position
|
||||
position (npt.NDArray[np.float64]): The position relative to the internal atoms of the represented molecule
|
||||
energy (npt.NDArray[np.float64]): The energy of the represented molecule
|
||||
gradient (npt.NDArray[np.float64]): The first derivative of the energy relative to the position
|
||||
hessian (npt.NDArray[np.float64]): The second derivative of the energy relative to the position
|
||||
total_mass (int): The total mass of the molecule
|
||||
com (NDArray[Any, Any]): The center of mass of the molecule
|
||||
com (npt.NDArray[np.float64]): The center of mass of the molecule
|
||||
"""
|
||||
|
||||
def __init__(self, molname: str) -> None:
|
||||
@@ -44,16 +45,16 @@ class Molecule:
|
||||
self.molname: str = molname
|
||||
|
||||
self.atom: List[Atom] = []
|
||||
self.position: NDArray[Any, Any]
|
||||
self.energy: NDArray[Any, Any]
|
||||
self.gradient: NDArray[Any, Any]
|
||||
self.hessian: NDArray[Any, Any]
|
||||
self.position: npt.NDArray[np.float64]
|
||||
self.energy: npt.NDArray[np.float64]
|
||||
self.gradient: npt.NDArray[np.float64]
|
||||
self.hessian: npt.NDArray[np.float64]
|
||||
|
||||
self.ghost_atoms: List[Atom] = []
|
||||
self.lp_atoms: List[Atom] = []
|
||||
|
||||
self.total_mass: int = 0
|
||||
self.com: Union[None, NDArray[Any, Any]] = None
|
||||
self.com: Union[None, npt.NDArray[np.float64]] = None
|
||||
|
||||
def add_atom(self, a: Atom) -> None:
|
||||
"""
|
||||
@@ -68,7 +69,7 @@ class Molecule:
|
||||
|
||||
self.center_of_mass()
|
||||
|
||||
def center_of_mass(self) -> NDArray[Any, Any]:
|
||||
def center_of_mass(self) -> npt.NDArray[np.float64]:
|
||||
"""
|
||||
Calculates the center of mass of the molecule
|
||||
"""
|
||||
@@ -115,7 +116,7 @@ class Molecule:
|
||||
|
||||
return [charge, dipole[0], dipole[1], dipole[2], total_dipole]
|
||||
|
||||
def distances_between_atoms(self) -> NDArray[Shape["Any,Any"], Float]:
|
||||
def distances_between_atoms(self) -> npt.NDArray[np.float64]:
|
||||
"""
|
||||
Calculates distances between the atoms of the molecule
|
||||
|
||||
@@ -135,16 +136,22 @@ class Molecule:
|
||||
|
||||
return np.array(distances).reshape(dim, dim - 1)
|
||||
|
||||
def inertia_tensor(self) -> NDArray[Shape["3, 3"], Float]:
|
||||
def inertia_tensor(self) -> npt.NDArray[np.float64]:
|
||||
"""
|
||||
Calculates the inertia tensor of the molecule.
|
||||
|
||||
Returns:
|
||||
NDArray[Shape["3, 3"], Float]: inertia tensor of the molecule.
|
||||
npt.NDArray[np.float64]: inertia tensor of the molecule.
|
||||
"""
|
||||
|
||||
self.center_of_mass()
|
||||
Ixx = Ixy = Ixz = Iyy = Iyz = Izz = 0.0
|
||||
|
||||
Ixx = 0.0
|
||||
Ixy = 0.0
|
||||
Ixz = 0.0
|
||||
Iyy = 0.0
|
||||
Iyz = 0.0
|
||||
Izz = 0.0
|
||||
|
||||
for atom in self.atom:
|
||||
dx = atom.rx - self.com[0]
|
||||
@@ -374,9 +381,9 @@ class Molecule:
|
||||
|
||||
distances = []
|
||||
for atom1 in self.atom:
|
||||
if atom1.na != ghost_number:
|
||||
if atom1.na != GHOST_NUMBER:
|
||||
for atom2 in molec.atom:
|
||||
if atom2.na != ghost_number:
|
||||
if atom2.na != GHOST_NUMBER:
|
||||
dx = atom1.rx - atom2.rx
|
||||
dy = atom1.ry - atom2.ry
|
||||
dz = atom1.rz - atom2.rz
|
||||
|
||||
Reference in New Issue
Block a user