|
35 | 35 | import itertools |
36 | 36 | import logging |
37 | 37 | import os |
| 38 | +import re |
38 | 39 |
|
39 | 40 | import numpy as np |
40 | 41 |
|
@@ -248,6 +249,27 @@ def __init__(self, core=None, edge=None, surface=None): |
248 | 249 | """ |
249 | 250 | ) |
250 | 251 | ] |
| 252 | + self.completed_pdep_networks = set() |
| 253 | + |
| 254 | + def add_completed_pdep_network(self, formula): |
| 255 | + """ |
| 256 | + Add a completed pressure-dependent network formula to the set. |
| 257 | + """ |
| 258 | + # turn C2H4 into {'C':2,'H':4} |
| 259 | + if not isinstance(formula, str): |
| 260 | + raise TypeError("Expected string for formula, got {0}".format(formula.__class__)) |
| 261 | + pattern = r'([A-Z][a-z]?)(\d*)' |
| 262 | + element_count = {} |
| 263 | + |
| 264 | + for match in re.finditer(pattern, formula): |
| 265 | + element = match.group(1) |
| 266 | + count = int(match.group(2)) if match.group(2) else 1 |
| 267 | + element_count[element] = count |
| 268 | + # must be hashable and match what is done in add_reaction_to_unimolecular_networks |
| 269 | + key = tuple(sorted(element_count.items())) |
| 270 | + self.completed_pdep_networks.add(key) |
| 271 | + logging.info(f"Added {formula} to list of completed PDep networks that will not be further explored.") |
| 272 | + |
251 | 273 |
|
252 | 274 | def check_for_existing_species(self, molecule): |
253 | 275 | """ |
@@ -1893,6 +1915,20 @@ def add_reaction_to_unimolecular_networks(self, newReaction, new_species, networ |
1893 | 1915 |
|
1894 | 1916 | source = tuple(reactants) |
1895 | 1917 |
|
| 1918 | + if len(reactants) == 1: |
| 1919 | + elements = reactants[0].molecule[0].get_element_count() |
| 1920 | + elif len(products) == 1: |
| 1921 | + elements = products[0].molecule[0].get_element_count() |
| 1922 | + else: |
| 1923 | + raise ValueError("Unimolecular reaction networks can only be formed for unimolecular reactions or isomerizations.") |
| 1924 | + # make a hashable key from the elements dict |
| 1925 | + elements_key = tuple(sorted(elements.items())) |
| 1926 | + if elements_key in self.completed_pdep_networks: |
| 1927 | + formula = ''.join(f'{el}{count}' if count>1 else el for el, count in elements_key) |
| 1928 | + logging.info(f"Not adding reaction {newReaction} to unimolecular networks because the network for {formula} is marked as completed.") |
| 1929 | + return |
| 1930 | + |
| 1931 | + |
1896 | 1932 | # Only search for a network if we don't specify it as a parameter |
1897 | 1933 | if network is None: |
1898 | 1934 | if len(reactants) == 1: |
|
0 commit comments