def determine_rmg_kinetics( rmgdb: RMGDatabase, reaction: Reaction, dh_rxn298: float, ) -> List[Reaction]: """ Determine kinetics for `reaction` (an RMG Reaction object) from RMG's database, if possible. Assigns a list of all matching entries from both libraries and families. Args: rmgdb (RMGDatabase): The RMG database instance. reaction (Reaction): The RMG Reaction object. dh_rxn298 (float): The heat of reaction at 298 K in J/mol. Returns: List[Reaction] All matching RMG reactions (both libraries and families) with a populated ``.kinetics`` attribute. """ reaction = reaction.copy( ) # use a copy to avoid changing atom order in the molecules by RMG rmg_reactions = list() # Libraries: for library in rmgdb.kinetics.libraries.values(): library_reactions = library.get_library_reactions() for library_reaction in library_reactions: if reaction.is_isomorphic(library_reaction): library_reaction.comment = f'Library: {library.label}' rmg_reactions.append(library_reaction) break # Families: fam_list = loop_families(rmgdb, reaction) for family, degenerate_reactions in fam_list: for deg_rxn in degenerate_reactions: template = family.retrieve_template(deg_rxn.template) kinetics = family.estimate_kinetics_using_rate_rules(template)[0] kinetics.change_rate(deg_rxn.degeneracy) kinetics = kinetics.to_arrhenius( dh_rxn298 ) # Convert ArrheniusEP to Arrhenius using the dHrxn at 298K deg_rxn.kinetics = kinetics deg_rxn.comment = f'Family: {deg_rxn.family}' deg_rxn.reactants = reaction.reactants deg_rxn.products = reaction.products rmg_reactions.extend(degenerate_reactions) worked_through_nist_fams = [] # NIST: for family, degenerate_reactions in fam_list: if family not in worked_through_nist_fams: worked_through_nist_fams.append(family) for depo in family.depositories: if 'NIST' in depo.label: for entry in depo.entries.values(): rxn = entry.item rxn.kinetics = entry.data rxn.comment = f'NIST: {entry.index}' if entry.reference is not None: rxn.comment += f'{entry.reference.authors[0]} {entry.reference.year}' rmg_reactions.append(rxn) return rmg_reactions
def testInplaceRemoveIsotopeForReactions(self): """ Test that removeIsotope and redoIsotope works with reactions """ eth = Species().fromAdjacencyList( """ 1 C u0 p0 c0 {2,S} {3,S} {4,S} {5,S} 2 C u0 p0 c0 {1,S} {6,S} {7,S} {8,S} 3 H u0 p0 c0 {1,S} 4 H u0 p0 c0 {1,S} 5 H u0 p0 c0 {1,S} 6 H u0 p0 c0 {2,S} 7 H u0 p0 c0 {2,S} 8 H u0 p0 c0 {2,S} """ ) ethi = Species().fromAdjacencyList( """ 1 C u0 p0 c0 {2,S} {3,S} {4,S} {5,S} 2 C u0 p0 c0 i13 {1,S} {6,S} {7,S} {8,S} 3 H u0 p0 c0 {1,S} 4 H u0 p0 c0 {1,S} 5 H u0 p0 c0 {1,S} 6 H u0 p0 c0 {2,S} 7 H u0 p0 c0 {2,S} 8 H u0 p0 c0 {2,S} """ ) unlabeledRxn = Reaction(reactants=[eth], products = [eth]) labeledRxn = Reaction(reactants=[ethi], products = [ethi]) storedLabeledRxn = labeledRxn.copy() modifiedAtoms = remove_isotope(labeledRxn, inplace=True) self.assertTrue(unlabeledRxn.isIsomorphic(labeledRxn)) redo_isotope(modifiedAtoms) self.assertTrue(storedLabeledRxn.isIsomorphic(labeledRxn))
def test_inplace_remove_isotope_for_reactions(self): """ Test that removeIsotope and redoIsotope works with reactions """ eth = Species().from_adjacency_list(""" 1 C u0 p0 c0 {2,S} {3,S} {4,S} {5,S} 2 C u0 p0 c0 {1,S} {6,S} {7,S} {8,S} 3 H u0 p0 c0 {1,S} 4 H u0 p0 c0 {1,S} 5 H u0 p0 c0 {1,S} 6 H u0 p0 c0 {2,S} 7 H u0 p0 c0 {2,S} 8 H u0 p0 c0 {2,S} """) ethi = Species().from_adjacency_list(""" 1 C u0 p0 c0 {2,S} {3,S} {4,S} {5,S} 2 C u0 p0 c0 i13 {1,S} {6,S} {7,S} {8,S} 3 H u0 p0 c0 {1,S} 4 H u0 p0 c0 {1,S} 5 H u0 p0 c0 {1,S} 6 H u0 p0 c0 {2,S} 7 H u0 p0 c0 {2,S} 8 H u0 p0 c0 {2,S} """) unlabeled_rxn = Reaction(reactants=[eth], products=[eth]) labeled_rxn = Reaction(reactants=[ethi], products=[ethi]) stored_labeled_rxn = labeled_rxn.copy() modified_atoms = remove_isotope(labeled_rxn, inplace=True) self.assertTrue(unlabeled_rxn.is_isomorphic(labeled_rxn)) redo_isotope(modified_atoms) self.assertTrue(stored_labeled_rxn.is_isomorphic(labeled_rxn))
def loop_families( rmgdb: RMGDatabase, reaction: Reaction, ) -> List[Tuple['KineticsFamily', list]]: """ Loop through kinetic families and return a list of tuples of (family, degenerate_reactions) corresponding to ``reaction``. Args: rmgdb (RMGDatabase): The RMG database instance. reaction (Reaction): The RMG Reaction object instance. Returns: List[Tuple['KineticsFamily', list]] Entries are tuples of a corresponding RMG KineticsFamily instance and a list of degenerate reactions. """ reaction = reaction.copy( ) # Use a copy to avoid changing atom order in the molecules by RMG. for spc in reaction.reactants + reaction.products: spc.generate_resonance_structures(save_order=True) fam_list = list() for family in rmgdb.kinetics.families.values(): family.save_order = True degenerate_reactions = list() family_reactions_by_r = list( ) # Family reactions for the specified reactants. family_reactions_by_rnp = list( ) # Family reactions for the specified reactants and products. if len(reaction.reactants) == 1: for reactant0 in reaction.reactants[0].molecule: fam_rxn = family.generate_reactions( reactants=[reactant0], products=reaction.products, delete_labels=False, ) if fam_rxn: family_reactions_by_r.extend(fam_rxn) elif len(reaction.reactants) == 2: for reactant0 in reaction.reactants[0].molecule: for reactant1 in reaction.reactants[1].molecule: fam_rxn = family.generate_reactions( reactants=[reactant0, reactant1], products=reaction.products, delete_labels=False, ) if fam_rxn: family_reactions_by_r.extend(fam_rxn) elif len(reaction.reactants) == 3: for reactant0 in reaction.reactants[0].molecule: for reactant1 in reaction.reactants[1].molecule: for reactant2 in reaction.reactants[2].molecule: fam_rxn = family.generate_reactions( reactants=[reactant0, reactant1, reactant2], products=reaction.products, delete_labels=False, ) if fam_rxn: family_reactions_by_r.extend(fam_rxn) if len(reaction.products) == 1: for fam_rxn in family_reactions_by_r: for product0 in reaction.products[0].molecule: if same_species_lists([product0], fam_rxn.products, save_order=True): family_reactions_by_rnp.append(fam_rxn) degenerate_reactions = find_degenerate_reactions( rxn_list=family_reactions_by_rnp, same_reactants=False, kinetics_database=rmgdb.kinetics, save_order=True) elif len(reaction.products) == 2: for fam_rxn in family_reactions_by_r: for product0 in reaction.products[0].molecule: for product1 in reaction.products[1].molecule: if same_species_lists([product0, product1], fam_rxn.products, save_order=True): family_reactions_by_rnp.append(fam_rxn) degenerate_reactions = find_degenerate_reactions( rxn_list=family_reactions_by_rnp, same_reactants=False, kinetics_database=rmgdb.kinetics, save_order=True) elif len(reaction.products) == 3: for fam_rxn in family_reactions_by_r: for product0 in reaction.products[0].molecule: for product1 in reaction.products[1].molecule: for product2 in reaction.products[2].molecule: if same_species_lists( [product0, product1, product2], fam_rxn.products, save_order=True): family_reactions_by_rnp.append(fam_rxn) degenerate_reactions = find_degenerate_reactions( rxn_list=family_reactions_by_rnp, same_reactants=False, kinetics_database=rmgdb.kinetics, save_order=True) if degenerate_reactions: fam_list.append((family, degenerate_reactions)) return fam_list
def loop_families( rmgdb: RMGDatabase, reaction: Reaction, ) -> List['KineticsFamily']: """ Loop through kinetic families and return a list of tuples of (family, degenerate_reactions) `reaction` is an RMG Reaction object. Returns a list of (family, degenerate_reactions) tuples. Args: rmgdb (RMGDatabase): The RMG database instance. reaction (Reaction): The RMG Reaction object. Returns: List[KineticsFamily] Entries are corresponding RMG KineticsFamily instances. """ reaction = reaction.copy( ) # use a copy to avoid changing atom order in the molecules by RMG fam_list = list() for family in rmgdb.kinetics.families.values(): degenerate_reactions = list() family_reactions_by_r = list( ) # family reactions for the specified reactants family_reactions_by_rnp = list( ) # family reactions for the specified reactants and products if len(reaction.reactants) == 1: for reactant0 in reaction.reactants[0].molecule: fam_rxn = family.generate_reactions(reactants=[reactant0], products=reaction.products) if fam_rxn: family_reactions_by_r.extend(fam_rxn) elif len(reaction.reactants) == 2: for reactant0 in reaction.reactants[0].molecule: for reactant1 in reaction.reactants[1].molecule: fam_rxn = family.generate_reactions( reactants=[reactant0, reactant1], products=reaction.products) if fam_rxn: family_reactions_by_r.extend(fam_rxn) elif len(reaction.reactants) == 3: for reactant0 in reaction.reactants[0].molecule: for reactant1 in reaction.reactants[1].molecule: for reactant2 in reaction.reactants[2].molecule: fam_rxn = family.generate_reactions( reactants=[reactant0, reactant1, reactant2], products=reaction.products) if fam_rxn: family_reactions_by_r.extend(fam_rxn) if len(reaction.products) == 1: for fam_rxn in family_reactions_by_r: for product0 in reaction.products[0].molecule: if same_species_lists([product0], fam_rxn.products): family_reactions_by_rnp.append(fam_rxn) degenerate_reactions = find_degenerate_reactions( rxn_list=family_reactions_by_rnp, same_reactants=False, kinetics_database=rmgdb.kinetics) elif len(reaction.products) == 2: for fam_rxn in family_reactions_by_r: for product0 in reaction.products[0].molecule: for product1 in reaction.products[1].molecule: if same_species_lists([product0, product1], fam_rxn.products): family_reactions_by_rnp.append(fam_rxn) degenerate_reactions = find_degenerate_reactions( rxn_list=family_reactions_by_rnp, same_reactants=False, kinetics_database=rmgdb.kinetics) elif len(reaction.products) == 3: for fam_rxn in family_reactions_by_r: for product0 in reaction.products[0].molecule: for product1 in reaction.products[1].molecule: for product2 in reaction.products[2].molecule: if same_species_lists( [product0, product1, product2], fam_rxn.products): family_reactions_by_rnp.append(fam_rxn) degenerate_reactions = find_degenerate_reactions( rxn_list=family_reactions_by_rnp, same_reactants=False, kinetics_database=rmgdb.kinetics) if degenerate_reactions: fam_list.append((family, degenerate_reactions)) return fam_list