Skip to content

Commit

Permalink
TMP
Browse files Browse the repository at this point in the history
  • Loading branch information
alongd committed Jul 11, 2024
1 parent 591c682 commit 3b94e7b
Show file tree
Hide file tree
Showing 10 changed files with 483 additions and 312 deletions.
7 changes: 2 additions & 5 deletions arc/checks/ts.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@
)
from arc.imports import settings
from arc.species.converter import check_xyz_dict, displace_xyz, xyz_to_dmat
from arc.mapping.engine import (get_atom_indices_of_labeled_atoms_in_an_rmg_reaction,
get_rmg_reactions_from_arc_reaction,
)
from arc.mapping.engine import get_atom_indices_of_labeled_atoms_in_a_reaction
from arc.statmech.factory import statmech_factory

if TYPE_CHECKING:
Expand Down Expand Up @@ -330,8 +328,7 @@ def check_normal_mode_displacement(reaction: 'ARCReaction',
bond_lone_hydrogens=bond_lone_hs)
got_expected_changing_bonds = False
for i, rmg_reaction in enumerate(rmg_reactions):
r_label_dict = get_atom_indices_of_labeled_atoms_in_an_rmg_reaction(arc_reaction=reaction,
rmg_reaction=rmg_reaction)[0]
r_label_dict = get_atom_indices_of_labeled_atoms_in_a_reaction(arc_reaction=reaction)[0]
if r_label_dict is None:
continue
expected_breaking_bonds, expected_forming_bonds = reaction.get_expected_changing_bonds(r_label_dict=r_label_dict)
Expand Down
2 changes: 1 addition & 1 deletion arc/job/adapters/ts/heuristics.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
from arc.job.factory import register_job_adapter
from arc.plotter import save_geo
from arc.species.converter import compare_zmats, relocate_zmat_dummy_atoms_to_the_end, zmat_from_xyz, zmat_to_xyz
from arc.mapping.engine import map_arc_rmg_species, map_two_species
from arc.mapping.engine import map_two_species
from arc.species.species import ARCSpecies, TSGuess, colliding_atoms
from arc.species.zmat import get_parameter_from_atom_indices, remove_1st_atom, up_param

Expand Down
49 changes: 14 additions & 35 deletions arc/mapping/driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@
create_qc_mol,
flip_map,
fingerprint,
get_atom_indices_of_labeled_atoms_in_an_rmg_reaction,
get_rmg_reactions_from_arc_reaction,
get_atom_indices_of_labeled_atoms_in_a_reaction,
glue_maps,
label_species_atoms,
make_bond_changes,
Expand All @@ -26,28 +25,27 @@
find_all_bdes,
cut_species_based_on_atom_indices,
update_xyz,
RESERVED_FINGERPRINT_KEYS,)
RESERVED_FINGERPRINT_KEYS,
)
from arc.common import logger

from rmgpy.exceptions import ActionError, AtomTypeError

if TYPE_CHECKING:
from rmgpy.data.rmg import RMGDatabase
from arc.reaction import ARCReaction


def map_reaction(rxn: 'ARCReaction',
backend: str = 'ARC',
db: Optional['RMGDatabase'] = None,
flip = False
flip: bool = False
) -> Optional[List[int]]:
"""
Map a reaction.
Args:
rxn (ARCReaction): An ARCReaction object instance.
backend (str, optional): Whether to use ``'QCElemental'`` or ``ARC``'s method as the backend.
db (RMGDatabase, optional): The RMG database instance.
flip (bool, optional): Whether to attempts fliping the reaction.
Returns:
Optional[List[int]]:
Expand All @@ -57,35 +55,31 @@ def map_reaction(rxn: 'ARCReaction',
if flip:
logger.warning(f"The requested ARC reaction {rxn} could not be atom mapped using {backend}. Trying again with the flipped reaction.")
try:
_map = flip_map(map_rxn(rxn.flip_reaction(), backend=backend, db=db))
_map = flip_map(map_rxn(rxn.flip_reaction(), backend=backend))
except ValueError:
return None
return _map
else:
if rxn.family is None:
logger.warning(f'Could not determine the reaction family for {rxn.label}. '
f'Mapping as a general or isomerization reaction.')
_map = map_general_rxn(rxn, backend=backend)
return _map if _map is not None else map_reaction(rxn, backend=backend, db=db, flip=True)
_map = map_general_rxn(rxn)
return _map if _map is not None else map_reaction(rxn, backend=backend, flip=True)
try:
_map = map_rxn(rxn, backend=backend, db=db)
except ValueError as e:
return map_reaction(rxn, backend=backend, db=db, flip=True)
return _map if _map is not None else map_reaction(rxn, backend=backend, db=db, flip=True)
_map = map_rxn(rxn, backend=backend)
except ValueError:
return map_reaction(rxn, backend=backend, flip=True)
return _map if _map is not None else map_reaction(rxn, backend=backend, flip=True)


def map_general_rxn(rxn: 'ARCReaction',
backend: str = 'ARC',
db: Optional['RMGDatabase'] = None,
) -> Optional[List[int]]:
"""
Map a general reaction (one that was not categorized into a reaction family by RMG).
The general method isn't great, a family-specific method should be implemented where possible.
Args:
rxn (ARCReaction): An ARCReaction object instance.
backend (str, optional): Whether to use ``'QCElemental'`` or ``ARC``'s method as the backend.
db (RMGDatabase, optional): The RMG database instance.
Returns:
Optional[List[int]]:
Expand Down Expand Up @@ -121,7 +115,6 @@ def map_isomerization_reaction(rxn: 'ARCReaction',
Args:
rxn (ARCReaction): An ARCReaction object instance.
backend (str, optional): Whether to use ``'QCElemental'`` or ``ARC``'s method as the backend.
db (RMGDatabase, optional): The RMG database instance.
Returns:
Optional[List[int]]:
Expand Down Expand Up @@ -209,7 +202,6 @@ def map_isomerization_reaction(rxn: 'ARCReaction',

def map_rxn(rxn: 'ARCReaction',
backend: str = 'ARC',
db: Optional['RMGDatabase'] = None,
) -> Optional[List[int]]:
"""
A wrapper function for mapping reaction, uses databases for mapping with the correct reaction family parameters.
Expand All @@ -224,26 +216,16 @@ def map_rxn(rxn: 'ARCReaction',
Args:
rxn (ARCReaction): An ARCReaction object instance that belongs to the RMG H_Abstraction reaction family.
backend (str, optional): Whether to use ``'QCElemental'`` or ``ARC``'s method as the backend.
db (RMGDatabase, optional): The RMG database instance.
Returns:
Optional[List[int]]:
Entry indices are running atom indices of the reactants,
corresponding entry values are running atom indices of the products.
"""
# step 1:
rmg_reactions = get_rmg_reactions_from_arc_reaction(arc_reaction=rxn, backend=backend)

if not rmg_reactions:
return None

r_label_dict, p_label_dict = get_atom_indices_of_labeled_atoms_in_an_rmg_reaction(arc_reaction=rxn,
rmg_reaction=rmg_reactions[0])
r_label_dict, p_label_dict = get_atom_indices_of_labeled_atoms_in_a_reaction(arc_reaction=rxn)

# step 2:
assign_labels_to_products(rxn, p_label_dict)

#step 3:

reactants, products = copy_species_list_for_mapping(rxn.r_species), copy_species_list_for_mapping(rxn.p_species)
label_species_atoms(reactants), label_species_atoms(products)

Expand All @@ -259,13 +241,10 @@ def map_rxn(rxn: 'ARCReaction',

r_cuts, p_cuts = update_xyz(r_cuts), update_xyz(p_cuts)

#step 4:
pairs_of_reactant_and_products = pairing_reactants_and_products_for_mapping(r_cuts, p_cuts)
if len(p_cuts):
logger.error(f"Could not find isomorphism for scissored species: {[cut.mol.smiles for cut in p_cuts]}")
return None
# step 5:
maps = map_pairs(pairs_of_reactant_and_products)

#step 6:
return glue_maps(maps, pairs_of_reactant_and_products)
70 changes: 33 additions & 37 deletions arc/mapping/driver_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ def setUpClass(cls):
(-0.8942590, -0.8537420, 0.0000000)),
'isotopes': (16, 16, 1), 'symbols': ('O', 'O', 'H')}
cls.nh2_xyz = """N 0.00022972 0.40059496 0.00000000
H -0.83174214 -0.19982058 0.00000000
H 0.83151242 -0.20077438 0.00000000"""
H -0.83174214 -0.19982058 0.00000000
H 0.83151242 -0.20077438 0.00000000"""
cls.n2h4_xyz = """N -0.67026921 -0.02117571 -0.25636419
N 0.64966276 0.05515705 0.30069593
H -1.27787600 0.74907557 0.03694453
Expand Down Expand Up @@ -481,7 +481,6 @@ def test_map_abstractions(self):
self.assertIn(atom_map[5], [4, 5])
self.assertTrue(check_atom_map(rxn))


# H + CH3NH2 <=> H2 + CH2NH2
ch3nh2_xyz = {'coords': ((-0.5734111454228507, 0.0203516083213337, 0.03088703933770556),
(0.8105595891860601, 0.00017446498908627427, -0.4077728757313545),
Expand Down Expand Up @@ -560,7 +559,6 @@ def test_map_abstractions(self):
self.assertTrue(any(atom_map[r_index] in [6, 7, 8] for r_index in [5, 6, 7, 8]))
self.assertTrue(check_atom_map(rxn))


# CH3OO + CH3CH2OH <=> CH3OOH + CH3CH2O / peroxyl to alkoxyl, modified atom and product order
r_1 = ARCSpecies(
label="CH3OO",
Expand Down Expand Up @@ -669,7 +667,6 @@ def test_map_abstractions(self):
self.assertEqual(atom_map[23],23)
self.assertTrue(check_atom_map(rxn))


# ClCH3 + H <=> CH3 + HCl
r_1 = ARCSpecies(label="ClCH3", smiles="CCl", xyz=self.ch3cl_xyz)
r_2 = ARCSpecies(label="H", smiles="[H]", xyz=self.h_rad_xyz)
Expand Down Expand Up @@ -714,23 +711,23 @@ def test_map_abstractions(self):
(0.3717352549047681, -1.308596593192221, 0.7750989547682503),
(-2.0374518517222544, -0.751480024679671, 0.37217669645466245))}

p_1_xyz = {'symbols': ('O', 'Cl', 'H'), 'isotopes': (16, 35, 1), 'coords': (
(-0.3223044372303026, 0.4343354356368888, 0.0), (1.2650242694442462, -0.12042710381137228, 0.0),
(-0.9427198322139436, -0.3139083318255167, 0.0))}
p_1_xyz = {'symbols': ('O', 'Cl', 'H'), 'isotopes': (16, 35, 1),
'coords': ((-0.3223044372303026, 0.4343354356368888, 0.0),
(1.2650242694442462, -0.12042710381137228, 0.0),
(-0.9427198322139436, -0.3139083318255167, 0.0))}

p_2_xyz = {'symbols': ('C', 'C', 'C', 'Cl', 'Cl', 'H', 'H', 'H', 'H', 'H'),
'isotopes': (12, 12, 12, 35, 35, 1, 1, 1, 1, 1), 'coords': (
(-1.3496376883278178, -0.020445981649800302, -0.1995184115269273),
(-0.051149096449292386, -0.3885500107837139, 0.4222976979623008),
(1.217696701041357, 0.15947991928242372, -0.1242718714010236),
(1.7092794464102241, 1.570982412202936, 0.8295196720275746),
(2.474584210365428, -1.0919019396606517, -0.06869614478411318),
(-1.6045061896547035, 1.0179450876989615, 0.03024632893682861),
(-1.3137314500783486, -0.14754777860704252, -1.2853589013330937),
(-2.1459595425475264, -0.6625965540242661, 0.188478021031359),
(-0.044412318929613885, -0.9093853981117669, 1.373599947353138),
(1.1078359281702537, 0.47202024365290884, -1.1662963382659064))}

'isotopes': (12, 12, 12, 35, 35, 1, 1, 1, 1, 1),
'coords': ((-1.3496376883278178, -0.020445981649800302, -0.1995184115269273),
(-0.051149096449292386, -0.3885500107837139, 0.4222976979623008),
(1.217696701041357, 0.15947991928242372, -0.1242718714010236),
(1.7092794464102241, 1.570982412202936, 0.8295196720275746),
(2.474584210365428, -1.0919019396606517, -0.06869614478411318),
(-1.6045061896547035, 1.0179450876989615, 0.03024632893682861),
(-1.3137314500783486, -0.14754777860704252, -1.2853589013330937),
(-2.1459595425475264, -0.6625965540242661, 0.188478021031359),
(-0.044412318929613885, -0.9093853981117669, 1.373599947353138),
(1.1078359281702537, 0.47202024365290884, -1.1662963382659064))}
r_1 = ARCSpecies(label='r1', smiles=smiles[0],xyz=r_1_xyz )
r_2 = ARCSpecies(label='r2', smiles=smiles[1],xyz=r_2_xyz)
p_1 = ARCSpecies(label='p1', smiles=smiles[2],xyz=p_1_xyz)
Expand All @@ -750,7 +747,6 @@ def test_map_abstractions(self):
self.assertTrue(check_atom_map(rxn))

# Br abstraction

# OH + CH3Br <=> HOBr + CH3
r_1_xyz = {'symbols': ('O', 'H'), 'isotopes': (16, 1),
'coords': ((0.48890386738601, 0.0, 0.0), (-0.48890386738601, 0.0, 0.0))}
Expand Down Expand Up @@ -788,22 +784,22 @@ def test_map_abstractions(self):
# [H] + CC(=O)Br <=> [H][Br] + C[C](=O)
r_1_xyz = {'symbols': ('H',), 'isotopes': (1,), 'coords': ((0.0, 0.0, 0.0),)}

r_2_xyz = {'symbols': ('C', 'C', 'O', 'Br', 'H', 'H', 'H'), 'isotopes': (12, 12, 16, 79, 1, 1, 1), 'coords': (
(-0.7087772076387326, -0.08697184565826255, 0.08295914062572969),
(0.7238141593293749, 0.2762480677183181, -0.14965326856248656),
(1.1113560248255752, 1.3624373452907719, -0.554840372311578),
(2.0636725443687616, -1.041297021241265, 0.20693447296577364),
(-0.9844931733249197, -0.9305935329026733, -0.5546432084044857),
(-0.8586221633621384, -0.3455305862905263, 1.134123935245044),
(-1.3469501841979155, 0.7657075730836449, -0.16488069955797996))}
r_2_xyz = {'symbols': ('C', 'C', 'O', 'Br', 'H', 'H', 'H'), 'isotopes': (12, 12, 16, 79, 1, 1, 1),
'coords': ((-0.7087772076387326, -0.08697184565826255, 0.08295914062572969),
(0.7238141593293749, 0.2762480677183181, -0.14965326856248656),
(1.1113560248255752, 1.3624373452907719, -0.554840372311578),
(2.0636725443687616, -1.041297021241265, 0.20693447296577364),
(-0.9844931733249197, -0.9305935329026733, -0.5546432084044857),
(-0.8586221633621384, -0.3455305862905263, 1.134123935245044),
(-1.3469501841979155, 0.7657075730836449, -0.16488069955797996))}

p_1_xyz = {'symbols': ('C', 'C', 'O', 'H', 'H', 'H'), 'isotopes': (12, 12, 16, 1, 1, 1), 'coords': (
(-0.4758624005470258, 0.015865899777425058, -0.11215987340300927),
(0.9456990856850401, -0.031530842469194666, 0.2228995599390481),
(2.0897646616994816, -0.06967555524967288, 0.492553667108967),
(-1.08983188764878, -0.06771143046366379, 0.7892594299969324),
(-0.7261604551815313, 0.9578749227991876, -0.6086176800339509),
(-0.7436090040071672, -0.8048229943940851, -0.7839351036079769))}
p_1_xyz = {'symbols': ('C', 'C', 'O', 'H', 'H', 'H'), 'isotopes': (12, 12, 16, 1, 1, 1),
'coords': ((-0.4758624005470258, 0.015865899777425058, -0.11215987340300927),
(0.9456990856850401, -0.031530842469194666, 0.2228995599390481),
(2.0897646616994816, -0.06967555524967288, 0.492553667108967),
(-1.08983188764878, -0.06771143046366379, 0.7892594299969324),
(-0.7261604551815313, 0.9578749227991876, -0.6086176800339509),
(-0.7436090040071672, -0.8048229943940851, -0.7839351036079769))}

p_2_xyz = {'symbols': ('Br', 'H'), 'isotopes': (79, 1),
'coords': ((0.7644788559644482, 0.0, 0.0), (-0.7644788559644482, 0.0, 0.0))}
Expand All @@ -819,7 +815,7 @@ def test_map_abstractions(self):
self.assertIn(tuple(atom_map[5:]), permutations([5, 6, 7]))
self.assertTrue(check_atom_map(rxn))

#Change Order [H] + CC(=O)Br <=> C[C](=O) + [H][Br]
# Change Order [H] + CC(=O)Br <=> C[C](=O) + [H][Br]
r_1 = ARCSpecies(label='r1', smiles='[H]', xyz=r_1_xyz)
r_2 = ARCSpecies(label='r2', smiles='CC(=O)Br', xyz=r_2_xyz)
p_1 = ARCSpecies(label='p1', smiles='C[C](=O)', xyz=p_1_xyz)
Expand Down
Loading

0 comments on commit 3b94e7b

Please sign in to comment.