Initial Work on Reading Crystal and Creation Gaussian Input
This commit is contained in:
0
crystalpol/shared/system/__init__.py
Normal file
0
crystalpol/shared/system/__init__.py
Normal file
46
crystalpol/shared/system/atom.py
Normal file
46
crystalpol/shared/system/atom.py
Normal file
@@ -0,0 +1,46 @@
|
||||
from crystalpol.shared.utils.ptable import atom_mass, atom_symbol
|
||||
|
||||
|
||||
class Atom:
|
||||
"""
|
||||
Atom class declaration. This class is used throughout the DicePlayer program to represent atoms.
|
||||
Attributes:
|
||||
na (int): Atomic number of the represented atom.
|
||||
symbol (str): Atomic symbol 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.
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
rx: float,
|
||||
ry: float,
|
||||
rz: float,
|
||||
na: int = None,
|
||||
symbol: str = None,
|
||||
|
||||
) -> None:
|
||||
"""
|
||||
The constructor function __init__ is used to create new instances of the Atom class.
|
||||
Args:
|
||||
na (int): Atomic number of the represented atom.
|
||||
symbol (str): Atomic symbol 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.
|
||||
"""
|
||||
|
||||
if na is not None:
|
||||
self.na = na
|
||||
self.symbol = atom_symbol[self.na]
|
||||
|
||||
if symbol is not None and symbol in atom_symbol:
|
||||
self.symbol = symbol
|
||||
self.na = atom_symbol.index(self.symbol)
|
||||
|
||||
self.rx = rx
|
||||
self.ry = ry
|
||||
self.rz = rz
|
||||
self.chg = None
|
||||
self.mass = atom_mass[self.na]
|
||||
43
crystalpol/shared/system/crystal.py
Normal file
43
crystalpol/shared/system/crystal.py
Normal file
@@ -0,0 +1,43 @@
|
||||
from crystalpol.shared.system.molecule import Molecule
|
||||
|
||||
from typing import List
|
||||
|
||||
|
||||
class Crystal:
|
||||
"""
|
||||
This class represents a crystal, it will be organized in a list structure.
|
||||
Each element is a unitary cell in the crystal. And this unitary cell will have
|
||||
Molecules in it.
|
||||
"""
|
||||
|
||||
def __init__(self, structure: List[List[str]]):
|
||||
self.structure = structure
|
||||
|
||||
self.cells = []
|
||||
|
||||
def __iter__(self):
|
||||
for cell in self.cells:
|
||||
yield cell
|
||||
|
||||
def __len__(self):
|
||||
return len(self.cells)
|
||||
|
||||
def __getitem__(self, index):
|
||||
return self.cells[index]
|
||||
|
||||
def add_cell(self, cell: List[Molecule]) -> None:
|
||||
valid = self._is_valid_cell(cell)
|
||||
if not valid:
|
||||
raise ValueError(
|
||||
"This cell does not obey the declared format for this Crystal."
|
||||
)
|
||||
else:
|
||||
self.cells.append(cell)
|
||||
|
||||
def _is_valid_cell(self, cell: List[Molecule]) -> bool:
|
||||
if len(cell) == len(self.structure):
|
||||
for i, molecule in enumerate(cell):
|
||||
if len(molecule.atoms) == len(self.structure[i]) \
|
||||
and all(atom.symbol == self.structure[i][j] for j, atom in enumerate(molecule.atoms)):
|
||||
return True
|
||||
return False
|
||||
88
crystalpol/shared/system/molecule.py
Normal file
88
crystalpol/shared/system/molecule.py
Normal file
@@ -0,0 +1,88 @@
|
||||
from crystalpol.shared.system.atom import Atom
|
||||
from crystalpol.shared.utils.ptable import atom_symbol
|
||||
|
||||
from typing import Final, List, TextIO
|
||||
import math
|
||||
|
||||
|
||||
""" Constants of unit conversion """
|
||||
BOHR2ANG: Final[float] = 0.52917721092
|
||||
ANG2BOHR: Final[float] = 1 / BOHR2ANG
|
||||
|
||||
|
||||
class Molecule:
|
||||
"""
|
||||
Molecule class declaration. This class is used throughout the DicePlayer program to represent molecules.
|
||||
|
||||
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
|
||||
total_mass (int): The total mass of the molecule
|
||||
com (NDArray[Any, Any]): The center of mass of the molecule
|
||||
"""
|
||||
|
||||
__slots__ = (
|
||||
'mol_name',
|
||||
'atoms',
|
||||
'position',
|
||||
'energy',
|
||||
'gradient',
|
||||
'hessian',
|
||||
'total_mass',
|
||||
'com'
|
||||
)
|
||||
|
||||
def __init__(self, mol_name: str) -> None:
|
||||
"""
|
||||
The constructor function __init__ is used to create new instances of the Molecule class.
|
||||
|
||||
Args:
|
||||
mol_name (str): Molecule name
|
||||
"""
|
||||
self.mol_name: str = mol_name
|
||||
|
||||
self.atoms: List[Atom] = []
|
||||
|
||||
def __iter__(self):
|
||||
for atom in self.atoms:
|
||||
yield atom
|
||||
|
||||
def add_atom(self, a: Atom) -> None:
|
||||
"""
|
||||
Adds Atom instance to the molecule.
|
||||
|
||||
Args:
|
||||
a (Atom): Atom instance to be added to atom list.
|
||||
"""
|
||||
|
||||
self.atoms.append(a)
|
||||
|
||||
def update_charges(self, charges: List[float]) -> None:
|
||||
|
||||
if len(charges) != len(self.atoms):
|
||||
raise ValueError(
|
||||
f"The number of charges ({len(charges)}) does not match the number of atoms ({len(self.atoms)})"
|
||||
)
|
||||
|
||||
for i, atom in enumerate(self.atoms):
|
||||
atom.chg = charges[i]
|
||||
|
||||
def print_mol_info(self, fh: TextIO) -> None:
|
||||
"""
|
||||
Prints the Molecule information into a Output File
|
||||
|
||||
Args:
|
||||
fh (TextIO): Output File
|
||||
"""
|
||||
|
||||
fh.write("-"*80)
|
||||
fh.write(f"Molecule Name: {self.mol_name}\n")
|
||||
|
||||
fh.write("\n")
|
||||
|
||||
for atom in self.atoms:
|
||||
fh.write(f"{atom_symbol[atom.na]} r: [{atom.rx}, {atom.ry}, {atom.rz}] charge: {atom.chg}")
|
||||
Reference in New Issue
Block a user