Skip to content

Commit bc9cad4

Browse files
committed
Simplified rmgdb.loop_families()
1 parent f8c5606 commit bc9cad4

1 file changed

Lines changed: 31 additions & 72 deletions

File tree

arc/rmgdb.py

Lines changed: 31 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,24 @@ def determine_reaction_family(rmgdb: RMGDatabase,
213213
return None, False
214214

215215

216+
def match_family_by_label(rmgdb: RMGDatabase,
217+
family_label: str,
218+
) -> Optional['KineticsFamily']:
219+
"""
220+
Find a family in the RMG database by its label.
221+
222+
Args:
223+
rmgdb (RMGDatabase): The RMG database instance.
224+
family_label (str): The family label.
225+
226+
Returns: Optional[KineticsFamily]
227+
The corresponding family object instance.
228+
"""
229+
for family in rmgdb.kinetics.families.values():
230+
if family.label == family_label:
231+
return family
232+
233+
216234
def loop_families(rmgdb: RMGDatabase,
217235
reaction: Reaction,
218236
) -> List[Tuple['KineticsFamily', list]]:
@@ -227,79 +245,20 @@ def loop_families(rmgdb: RMGDatabase,
227245
Returns: List[Tuple['KineticsFamily', list]]
228246
Entries are tuples of a corresponding RMG KineticsFamily instance and a list of degenerate reactions.
229247
"""
230-
reaction = reaction.copy() # Use a copy to avoid changing atom order in the molecules by RMG.
231-
for spc in reaction.reactants + reaction.products:
232-
generate_resonance_structures(spc)
233-
fam_list = list()
234-
for family in rmgdb.kinetics.families.values():
235-
family.save_order = True
236-
degenerate_reactions = list()
237-
family_reactions_by_r = list() # Family reactions for the specified reactants.
238-
family_reactions_by_rnp = list() # Family reactions for the specified reactants and products.
239-
240-
if len(reaction.reactants) == 1:
241-
for reactant0 in reaction.reactants[0].molecule:
242-
fam_rxn = family.generate_reactions(reactants=[reactant0],
243-
products=reaction.products,
244-
delete_labels=False,
248+
rxn_copy = reaction.copy()
249+
reactions_f = rmgdb.kinetics.generate_reactions(reactants=rxn_copy.reactants,
250+
products=rxn_copy.products,
251+
only_families=True,
252+
resonance=True,
253+
)
254+
reactions_r = rmgdb.kinetics.generate_reactions(reactants=rxn_copy.products,
255+
products=rxn_copy.reactants,
256+
only_families=True,
257+
resonance=True,
245258
)
246-
if fam_rxn:
247-
family_reactions_by_r.extend(fam_rxn)
248-
elif len(reaction.reactants) == 2:
249-
for reactant0 in reaction.reactants[0].molecule:
250-
for reactant1 in reaction.reactants[1].molecule:
251-
fam_rxn = family.generate_reactions(reactants=[reactant0, reactant1],
252-
products=reaction.products,
253-
delete_labels=False,
254-
)
255-
if fam_rxn:
256-
family_reactions_by_r.extend(fam_rxn)
257-
elif len(reaction.reactants) == 3:
258-
for reactant0 in reaction.reactants[0].molecule:
259-
for reactant1 in reaction.reactants[1].molecule:
260-
for reactant2 in reaction.reactants[2].molecule:
261-
fam_rxn = family.generate_reactions(reactants=[reactant0, reactant1, reactant2],
262-
products=reaction.products,
263-
delete_labels=False,
264-
)
265-
if fam_rxn:
266-
family_reactions_by_r.extend(fam_rxn)
267-
268-
if len(reaction.products) == 1:
269-
for fam_rxn in family_reactions_by_r:
270-
for product0 in reaction.products[0].molecule:
271-
if same_species_lists([product0], fam_rxn.products, save_order=True):
272-
family_reactions_by_rnp.append(fam_rxn)
273-
degenerate_reactions = find_degenerate_reactions(rxn_list=family_reactions_by_rnp,
274-
same_reactants=False,
275-
kinetics_database=rmgdb.kinetics,
276-
save_order=True
277-
)
278-
elif len(reaction.products) == 2:
279-
for fam_rxn in family_reactions_by_r:
280-
for product0 in reaction.products[0].molecule:
281-
for product1 in reaction.products[1].molecule:
282-
if same_species_lists([product0, product1], fam_rxn.products, save_order=True):
283-
family_reactions_by_rnp.append(fam_rxn)
284-
degenerate_reactions = find_degenerate_reactions(rxn_list=family_reactions_by_rnp,
285-
same_reactants=False,
286-
kinetics_database=rmgdb.kinetics,
287-
save_order=True
288-
)
289-
elif len(reaction.products) == 3:
290-
for fam_rxn in family_reactions_by_r:
291-
for product0 in reaction.products[0].molecule:
292-
for product1 in reaction.products[1].molecule:
293-
for product2 in reaction.products[2].molecule:
294-
if same_species_lists([product0, product1, product2], fam_rxn.products, save_order=True):
295-
family_reactions_by_rnp.append(fam_rxn)
296-
degenerate_reactions = find_degenerate_reactions(rxn_list=family_reactions_by_rnp,
297-
same_reactants=False,
298-
kinetics_database=rmgdb.kinetics,
299-
save_order=True
300-
)
301-
if degenerate_reactions:
302-
fam_list.append((family, degenerate_reactions))
259+
fam_list = list()
260+
for rxn in reactions_f + reactions_r:
261+
fam_list.append((match_family_by_label(rmgdb=rmgdb, family_label=rxn.family), rxn))
303262
return fam_list
304263

305264

0 commit comments

Comments
 (0)