Skip to content

Commit 210f585

Browse files
committed
return kinetic library entry when parsing comments
Before this, the kinetics version of extract_source_from_comment returned the library name, but now it returns a tuple of the library name and the library entry. This makes things much easier downstream in the uncertainty tool.
1 parent 6f5a3ea commit 210f585

2 files changed

Lines changed: 21 additions & 3 deletions

File tree

rmgpy/data/kinetics/database.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -695,7 +695,7 @@ def extract_source_from_comments(self, reaction):
695695
A reaction can only be estimated using one of these methods.
696696
697697
source = {'RateRules': (Family_Label, OriginalTemplate, RateRules),
698-
'Library': String_Name_of_Library_Used,
698+
'Library': (String_Name_of_Library_Used, Library_Entry),
699699
'PDep': Network_Index,
700700
'Training': (Family_Label, Training_Reaction_Entry),
701701
}
@@ -715,7 +715,25 @@ def extract_source_from_comments(self, reaction):
715715
source['Rate Rules'] = data_source
716716
elif isinstance(reaction, LibraryReaction):
717717
# This reaction comes from a reaction library or seed mechanism
718-
source['Library'] = reaction.library
718+
if reaction.library not in self.libraries:
719+
raise ValueError(f'Library {reaction.library} not found in kinetics database. Make sure libraries match input file used to generate kinetics.')
720+
library_reactions = self.generate_reactions_from_library(
721+
library=self.libraries[reaction.library],
722+
reactants=reaction.reactants,
723+
products=reaction.products
724+
)
725+
if not library_reactions:
726+
raise ValueError(f'Could not find reaction {reaction} in library {reaction.library} when trying to extract source data from comments.')
727+
entry = self.libraries[reaction.library].entries[library_reactions[0].entry.index]
728+
if len(library_reactions) > 1:
729+
# Library contains multiple reactions of the same type, so pick the one with matching kinetics
730+
for rxn in library_reactions:
731+
if rxn.entry.data.is_similar_to(reaction.kinetics): # units might have been changed, so use is_similar_to instead of direct comparison
732+
entry = self.libraries[reaction.library].entries[rxn.entry.index]
733+
break
734+
else:
735+
raise ValueError(f'Found multiple instances of reaction {reaction} in library {reaction.library}, but none of them have kinetics identical to the reaction kinetics.')
736+
source['Library'] = (reaction.library, entry)
719737

720738
elif isinstance(reaction, PDepReaction):
721739
# This reaction is a pressure-dependent reaction

test/rmgpy/data/kinetics/kineticsTest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -672,7 +672,7 @@ def test_parse_kinetics(self):
672672
# ------------------------------------------------------------------- #
673673
# Source 0 comes from a kinetics library
674674
assert "Library" in sources[0]
675-
assert sources[0]["Library"] == "GRI-Mech3.0"
675+
assert sources[0]["Library"][0] == "GRI-Mech3.0"
676676

677677
reconstructed_kinetics = self.database.kinetics.reconstruct_kinetics_from_source(reactions[0], sources[0], fix_barrier_height=True)
678678
A = reconstructed_kinetics.A.value_si

0 commit comments

Comments
 (0)