[docs]classMCAtom:""" Abstraction of an atom in a molecule: - It's MCElement. - It's 3D coordinates. - The atoms bonded to it. - The bonds associated with it. - It's molecule. - It's index in the molecule. """def__init__(self,element:MCElement,coords:np.array=np.array([0,0,0]),atoms:Optional[list]=None,bonds:Optional[list]=None,molecule:None=None,molecule_index:Optional[int]=None,):self.element=elementself.coords=coordsself.atoms=atomsor[]self.bonds=bondsor[]self.molecule=moleculeself.molecule_index=molecule_index
[docs]defadd_atoms(self,atoms):""" Assigns bonded atoms to MCAtom. Args: atoms (MCAtom or List[MCAtom]) Raises: Exception: In case the atoms are not MCAtoms or a list. """ifnotatoms:passifnotisinstance(atoms,MCAtom)andnotisinstance(atoms,list):raiseException(f"Expected {MCAtom} or list of {MCAtom} when assigning atoms but received {atoms}")ifisinstance(self.atoms,list)andisinstance(atoms,MCAtom):self.atoms.append(atoms)ifisinstance(self.atoms,list)andisinstance(atoms,list):ifnotall([isinstance(atom,MCAtom)foratominatoms]):raiseException(f"Expected {MCAtom} or list of {MCAtom} when assigning atoms but received {atoms}")self.atoms.extend(atoms)returnself.atoms
defadd_bonds(self,bonds):from.mc_bondimportMCBondifnotbonds:passifnotisinstance(bonds,MCBond)andnotisinstance(bonds,list):raiseException(f"Expected {MCBond} or list of {MCBond} when assigning atoms but received {bonds}")ifisinstance(self.bonds,list)andisinstance(bonds,MCBond):self.bonds.append(bonds)ifisinstance(self.bonds,list)andisinstance(bonds,list):ifnotall([isinstance(bond,MCBond)forbondinbonds]):raiseException(f"Expected {MCBond} or list of {MCBond} when assigning atoms but received {bonds}")self.bonds.extend(bonds)returnself.bondsdefassign_molecule(self,molecule):from.mc_moleculeimportMCMoleculeifnotmolecule:passifnotisinstance(molecule,MCMolecule):raiseException(f"Expected {MCMolecule} when assigning molecule but received {molecule}")self.molecule=moleculereturnself.moleculedefassign_molecule_index(self,molecule_index:int):ifisinstance(molecule_index,None):passifnotisinstance(molecule_index,int):raiseException(f"Expected {int} when assigning molecule_index but received {molecule_index}")self.molecule_index=molecule_indexreturnself.molecule_index
[docs]@staticmethoddefconstruct_from_atom_dict(atom_index,atom_data_dict:Dict):""" Given an atom data dict from a parser, returns an MCAtom Args: atom_dict (Dict): See data_parser function from BaseParser Output: MCAtom """ifnotisinstance(atom_data_dict,dict):raiseException(f"Expected {dict} but received {atom_data_dict}")element=atom_data_dict.get("element",None)ifnotelement:raiseException(f"Atom dict {atom_data_dict} does not contain element data.")ifelementnotinMC_ELEMENT_DICT:raiseException(f"Element {element} does not match any known element by human kind. Is that an alien element?")mc_element=MC_ELEMENT_DICT.get(element)coords=atom_data_dict.get("coords")ifnotisinstance(coords,(np.ndarray,np.generic))andnotisinstance(coords,list):raiseException(f"Coordinates {coords} are not correct coordinates.")returnMCAtom(element=mc_element,coords=coords,molecule_index=atom_index)