Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added flux diagrams #141

Merged
merged 8 commits into from
Nov 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ channels:
- conda-forge
dependencies:
- python=3.7
- pydot
- cantera=2.6
- ca-certificates
- openssl
Expand Down
204 changes: 204 additions & 0 deletions ipython/flux_diagram/Generate Flux Diagram.ipynb

Large diffs are not rendered by default.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
digraph G {
label="Flux diagram at 2.0 s, ROP range: [8.01e-17, 1.51e-02] mol/cm³/s)";
"H(3)" [fillcolor="#DCE5F4", fontsize=8, penwidth=1.7530392047540462, style=filled, xlabel="2.80e-11"];
"CO(8)" [fontsize=8, penwidth=3.999999999999999, xlabel="4.68e-05"];
"H2(4)" [fontsize=8, penwidth=3.927639860174137, xlabel="2.95e-05"];
"OH(5)" [fontsize=8, penwidth=1.6100755035270433, xlabel="1.12e-11"];
"H(3)" -> "CO(8)" [arrowhead=vee, fontsize=8, label="0.9\n+ HOCHO(1)\n- H2(4) - OH(5)", penwidth=3.984676425213845];
"H(3)" -> "H2(4)" [arrowhead=vee, fontsize=8, label="0.9\n+ HOCHO(1)\n- CO(8) - OH(5)", penwidth=3.984676425213845];
"H(3)" -> "OH(5)" [arrowhead=vee, fontsize=8, label="0.9\n+ HOCHO(1)\n- CO(8) - H2(4)", penwidth=3.984676425213845];
"HOCO(10)" [fontsize=8, penwidth=0.5383397186827532, xlabel="1.21e-14"];
"H(3)" -> "H2(4)" [arrowhead=vee, fontsize=8, label="0.2\n+ HOCHO(1)\n- HOCO(10)", penwidth=3.7536269933534743];
"H(3)" -> "HOCO(10)" [arrowhead=vee, fontsize=8, label="0.2\n+ HOCHO(1)\n- H2(4)", penwidth=3.7536269933534743];
"HOCO(10)" -> "CO(8)" [arrowhead=vee, fontsize=8, label="1.0\n- OH(5)", penwidth=3.997757463516357];
"HOCO(10)" -> "OH(5)" [arrowhead=vee, fontsize=8, label="1.0\n- CO(8)", penwidth=3.997757463516357];
"CO2(9)" [fontsize=8, penwidth=3.927639907930275, xlabel="2.95e-05"];
"H2O(17)" [fontsize=8, penwidth=3.9999999645295996, xlabel="4.68e-05"];
"OH(5)" -> "CO2(9)" [arrowhead=vee, fontsize=8, label="1.0\n+ HOCHO(1)\n- H(3) - H2O(17)", penwidth=4.0];
"OH(5)" -> "H(3)" [arrowhead=vee, fontsize=8, label="1.0\n+ HOCHO(1)\n- CO2(9) - H2O(17)", penwidth=4.0];
"OH(5)" -> "H2O(17)" [arrowhead=vee, fontsize=8, label="1.0\n+ HOCHO(1)\n- CO2(9) - H(3)", penwidth=4.0];
"OH(5)" -> "H2O(17)" [arrowhead=vee, fontsize=8, label="0.8\n+ HOCHO(1)\n- HOCO(10)", penwidth=3.9539926576457947];
"OH(5)" -> "HOCO(10)" [arrowhead=vee, fontsize=8, label="0.8\n+ HOCHO(1)\n- H2O(17)", penwidth=3.9539926576457947];
"OCHO(11)" [fontsize=8, penwidth=0.19999999999999937, xlabel="1.40e-15"];
"OH(5)" -> "H2O(17)" [arrowhead=vee, fontsize=8, label="0.1\n+ HOCHO(1)\n- OCHO(11)", penwidth=3.67123391780574];
"OH(5)" -> "OCHO(11)" [arrowhead=vee, fontsize=8, label="0.1\n+ HOCHO(1)\n- H2O(17)", penwidth=3.67123391780574];
"OCHO(11)" -> "CO2(9)" [arrowhead=vee, fontsize=8, label="0.1\n- H(3)", penwidth=3.676279894902382];
"OCHO(11)" -> "H(3)" [arrowhead=vee, fontsize=8, label="0.1\n- CO2(9)", penwidth=3.676279894902382];
"H2O(17)" -> "H(3)" [arrowhead=vee, fontsize=8, label="4.3e-10\n- OH(5)", penwidth=0.43016435137560394];
"H2O(17)" -> "OH(5)" [arrowhead=vee, fontsize=8, label="4.3e-10\n- H(3)", penwidth=0.43016435137560394];
"H2O(17)" -> "CO2(9)" [arrowhead=vee, fontsize=8, label="1.1e-10\n+ CO(8)\n- H2(4)", penwidth=0.20000000000000034];
"H2O(17)" -> "H2(4)" [arrowhead=vee, fontsize=8, label="1.1e-10\n+ CO(8)\n- CO2(9)", penwidth=0.20000000000000034];
"CO2(9)" -> "HOCO(10)" [arrowhead=vee, fontsize=8, label="3.6e-08\n+ HOCHO(1)", penwidth=1.4627966491004998];
"H2(4)" -> "H(3)" [arrowhead=vee, fontsize=8, label="1.0e-04\n+ OH(5)\n- H2O(17)", penwidth=2.483741242797586];
"H2(4)" -> "H2O(17)" [arrowhead=vee, fontsize=8, label="1.0e-04\n+ OH(5)\n- H(3)", penwidth=2.483741242797586];
"CO(8)" -> "CO2(9)" [arrowhead=vee, fontsize=8, label="2.0e-03\n+ OH(5)\n- H(3)", penwidth=2.9739529963274607];
"CO(8)" -> "H(3)" [arrowhead=vee, fontsize=8, label="2.0e-03\n+ OH(5)\n- CO2(9)", penwidth=2.9739529963274607];
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
digraph G {
label="Flux diagram at 2.0 s, ROP range: [8.01e-17, 1.51e-02] mol/cm³/s)";
"HOCHO(1)" [fillcolor="#DCE5F4", fontsize=8, penwidth=4.0, style=filled, xlabel="1.69e-01"];
"CO(8)" [fontsize=8, penwidth=2.6712938924744143, xlabel="4.68e-05"];
"H2O(17)" [fontsize=8, penwidth=2.671293855796359, xlabel="4.68e-05"];
"HOCHO(1)" -> "CO(8)" [arrowhead=vee, fontsize=8, label="1.0\n- H2O(17)", penwidth=4.0];
"HOCHO(1)" -> "H2O(17)" [arrowhead=vee, fontsize=8, label="1.0\n- CO(8)", penwidth=4.0];
"CO2(9)" [fontsize=8, penwidth=2.596470167350586, xlabel="2.95e-05"];
"H2(4)" [fontsize=8, penwidth=2.5964701179685017, xlabel="2.95e-05"];
"HOCHO(1)" -> "CO2(9)" [arrowhead=vee, fontsize=8, label="0.6\n- H2(4)", penwidth=3.9466405710292745];
"HOCHO(1)" -> "H2(4)" [arrowhead=vee, fontsize=8, label="0.6\n- CO2(9)", penwidth=3.9466405710292745];
"H(3)" [fontsize=8, penwidth=0.34783116462852137, xlabel="2.80e-11"];
"H2(4)" -> "H(3)" [arrowhead=vee, fontsize=8, label="5.2e-09\n+ OH(5)\n- H2O(17)", penwidth=1.7955577099046087];
"H2(4)" -> "H2O(17)" [arrowhead=vee, fontsize=8, label="5.2e-09\n+ OH(5)\n- H(3)", penwidth=1.7955577099046087];
"HOCO(10)" [fontsize=8, penwidth=0.7324480422152477, xlabel="1.21e-14"];
"CO2(9)" -> "HOCO(10)" [arrowhead=vee, fontsize=8, label="1.8e-12\n+ HOCHO(1)", penwidth=1.1729774870268643];
"OH(5)" [fontsize=8, penwidth=0.2000000000000002, xlabel="1.12e-11"];
"H2O(17)" -> "H(3)" [arrowhead=vee, fontsize=8, label="2.1e-14\n- OH(5)", penwidth=0.3608065303110556];
"H2O(17)" -> "OH(5)" [arrowhead=vee, fontsize=8, label="2.1e-14\n- H(3)", penwidth=0.3608065303110556];
"H2O(17)" -> "CO2(9)" [arrowhead=vee, fontsize=8, label="5.3e-15\n+ CO(8)\n- H2(4)", penwidth=0.2];
"H2O(17)" -> "H2(4)" [arrowhead=vee, fontsize=8, label="5.3e-15\n+ CO(8)\n- CO2(9)", penwidth=0.2];
"CO(8)" -> "CO2(9)" [arrowhead=vee, fontsize=8, label="1.0e-07\n+ OH(5)\n- H(3)", penwidth=2.138048850394896];
"CO(8)" -> "H(3)" [arrowhead=vee, fontsize=8, label="1.0e-07\n+ OH(5)\n- CO2(9)", penwidth=2.138048850394896];
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
digraph G {
label="Flux diagram at 2.0 s, ROP range: [8.01e-17, 1.51e-02] mol/cm³/s)";
"HOCO(10)" [fillcolor="#DCE5F4", fontsize=8, penwidth=0.5383397186827532, style=filled, xlabel="1.21e-14"];
"CO(8)" [fontsize=8, penwidth=3.999999999999999, xlabel="4.68e-05"];
"OH(5)" [fontsize=8, penwidth=1.6100755035270433, xlabel="1.12e-11"];
"HOCO(10)" -> "CO(8)" [arrowhead=vee, fontsize=8, label="1.0\n- OH(5)", penwidth=3.997757463516357];
"HOCO(10)" -> "OH(5)" [arrowhead=vee, fontsize=8, label="1.0\n- CO(8)", penwidth=3.997757463516357];
"CO2(9)" [fontsize=8, penwidth=3.927639907930275, xlabel="2.95e-05"];
"H(3)" [fontsize=8, penwidth=1.7530392047540462, xlabel="2.80e-11"];
"H2O(17)" [fontsize=8, penwidth=3.9999999645295996, xlabel="4.68e-05"];
"OH(5)" -> "CO2(9)" [arrowhead=vee, fontsize=8, label="1.0\n+ HOCHO(1)\n- H(3) - H2O(17)", penwidth=4.0];
"OH(5)" -> "H(3)" [arrowhead=vee, fontsize=8, label="1.0\n+ HOCHO(1)\n- CO2(9) - H2O(17)", penwidth=4.0];
"OH(5)" -> "H2O(17)" [arrowhead=vee, fontsize=8, label="1.0\n+ HOCHO(1)\n- CO2(9) - H(3)", penwidth=4.0];
"OH(5)" -> "H2O(17)" [arrowhead=vee, fontsize=8, label="0.8\n+ HOCHO(1)\n- HOCO(10)", penwidth=3.9539926576457947];
"OH(5)" -> "HOCO(10)" [arrowhead=vee, fontsize=8, label="0.8\n+ HOCHO(1)\n- H2O(17)", penwidth=3.9539926576457947];
"OCHO(11)" [fontsize=8, penwidth=0.19999999999999937, xlabel="1.40e-15"];
"OH(5)" -> "H2O(17)" [arrowhead=vee, fontsize=8, label="0.1\n+ HOCHO(1)\n- OCHO(11)", penwidth=3.67123391780574];
"OH(5)" -> "OCHO(11)" [arrowhead=vee, fontsize=8, label="0.1\n+ HOCHO(1)\n- H2O(17)", penwidth=3.67123391780574];
"OCHO(11)" -> "CO2(9)" [arrowhead=vee, fontsize=8, label="0.1\n- H(3)", penwidth=3.676279894902382];
"OCHO(11)" -> "H(3)" [arrowhead=vee, fontsize=8, label="0.1\n- CO2(9)", penwidth=3.676279894902382];
"H2O(17)" -> "H(3)" [arrowhead=vee, fontsize=8, label="4.3e-10\n- OH(5)", penwidth=0.43016435137560394];
"H2O(17)" -> "OH(5)" [arrowhead=vee, fontsize=8, label="4.3e-10\n- H(3)", penwidth=0.43016435137560394];
"H2(4)" [fontsize=8, penwidth=3.927639860174137, xlabel="2.95e-05"];
"H2O(17)" -> "CO2(9)" [arrowhead=vee, fontsize=8, label="1.1e-10\n+ CO(8)\n- H2(4)", penwidth=0.20000000000000034];
"H2O(17)" -> "H2(4)" [arrowhead=vee, fontsize=8, label="1.1e-10\n+ CO(8)\n- CO2(9)", penwidth=0.20000000000000034];
"H(3)" -> "CO(8)" [arrowhead=vee, fontsize=8, label="0.9\n+ HOCHO(1)\n- H2(4) - OH(5)", penwidth=3.984676425213845];
"H(3)" -> "H2(4)" [arrowhead=vee, fontsize=8, label="0.9\n+ HOCHO(1)\n- CO(8) - OH(5)", penwidth=3.984676425213845];
"H(3)" -> "OH(5)" [arrowhead=vee, fontsize=8, label="0.9\n+ HOCHO(1)\n- CO(8) - H2(4)", penwidth=3.984676425213845];
"H(3)" -> "H2(4)" [arrowhead=vee, fontsize=8, label="0.2\n+ HOCHO(1)\n- HOCO(10)", penwidth=3.7536269933534743];
"H(3)" -> "HOCO(10)" [arrowhead=vee, fontsize=8, label="0.2\n+ HOCHO(1)\n- H2(4)", penwidth=3.7536269933534743];
"CO2(9)" -> "HOCO(10)" [arrowhead=vee, fontsize=8, label="3.6e-08\n+ HOCHO(1)", penwidth=1.4627966491004998];
"CO(8)" -> "CO2(9)" [arrowhead=vee, fontsize=8, label="2.0e-03\n+ OH(5)\n- H(3)", penwidth=2.9739529963274607];
"CO(8)" -> "H(3)" [arrowhead=vee, fontsize=8, label="2.0e-03\n+ OH(5)\n- CO2(9)", penwidth=2.9739529963274607];
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions t3/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
DATA_BASE_PATH = os.path.join(t3_path, 'tests', 'data')
SIMULATE_DATA_BASE_PATH = os.path.join(t3_path, 'tests', 'test_simulate_adapters', 'data')
EXAMPLES_BASE_PATH = os.path.join(t3_path, 'examples')
SCRATCH_BASE_PATH = os.path.join(t3_path, 'tests', 'scratch')
IPYTHON_SIMULATOR_EXAMPLES_PATH = os.path.join(t3_path, 'ipython', 'simulator_adapter_examples')
PROJECTS_BASE_PATH = os.path.join(t3_path, 'Projects')
VALID_CHARS = "-_=.,%s%s" % (string.ascii_letters, string.digits)
Expand Down
149 changes: 149 additions & 0 deletions t3/utils/fix_cantera.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
"""
t3 utils fix_cantera module
A module to automatically fix issues with RMG-generated Cantera files, mainly resolving mislabeled duplicate reactions.
"""
import os

from typing import List, Optional

import shutil
import time
import traceback

import cantera as ct

from arc.common import read_yaml_file, save_yaml_file


def get_traceback(model_path: str) -> Optional[str]:
"""
Try loading the Cantera model and return the traceback if it fails.

Args:
model_path (str): The path to the cantera YAML model file.

Returns:
Optional[str]: The traceback if the model fails to load.
"""
tb = None
try:
ct.Solution(model_path)
except ct.CanteraError:
tb = traceback.format_exc()
return tb


def fix_cantera(model_path: str):
"""
Fix a Cantera model that has incorrectly marked duplicate reactions.
Creates a backup copy of the Cantera model and fixes the content of the original file in place.

Args:
model_path (str): The path to the cantera YAML model file.

Returns:
bool: Whether the model was fixed.
"""
shutil.copyfile(model_path, model_path + '.bak')
done, fixed = False, False
counter = 0
while not done and counter < 1000:
counter += 1
tb = get_traceback(model_path)
if tb is None:
done = True
break
else:
if 'Undeclared duplicate reactions detected' in tb:
fix_undeclared_duplicate_reactions(model_path, tb)
fixed = True
elif 'No duplicate found for declared duplicate reaction' in tb:
fix_no_duplicate_found(model_path, tb)
fixed = True
else:
print(f'Could not fix {model_path}:\n\n{tb}')
break
time.sleep(1)
if fixed:
print(f'Fixing Cantera model {model_path} (and creating a backup copy with a .bak extension).')
else:
os.remove(model_path + '.bak')
return done


def fix_undeclared_duplicate_reactions(model_path: str, tb: str):
"""
Fix a Cantera model that has undeclared duplicate reactions.

Args:
model_path (str): The path to the cantera YAML model file.
tb (str): The traceback.
"""
content = read_yaml_file(model_path)
rxns = get_dup_rxn_indices(tb)
print(f'Marking reactions {", ".join([str(r) for r in rxns])} as duplicate.')
for i in rxns:
content['reactions'][i - 1]['duplicate'] = True
save_yaml_file(model_path, content)


def fix_no_duplicate_found(model_path: str, tb: str):
"""
Fix a Cantera model that has a reaction marked as duplicate by mistake with no other duplicate reaction.

Args:
model_path (str): The path to the cantera YAML model file.
tb (str): The traceback.
"""
content = read_yaml_file(model_path)
rxns = get_mistakenly_marked_dup_rxns(tb)
for i in rxns:
if 'duplicate' in content['reactions'][i].keys():
print(f'Marking reaction {i} as non-duplicate.')
del content['reactions'][i]['duplicate']
save_yaml_file(model_path, content)


def get_dup_rxn_indices(tb: str) -> List[int]:
"""
Get the duplicate reactions from the traceback.

Args:
tb (str): The traceback.

Returns:
List[int]: The reactions indices.
"""
rxns = list()
if tb is None:
return rxns
lines = tb.split('\n')
read = False
for line in lines:
if 'Undeclared duplicate reactions detected:' in line:
read = True
if '| Line |' in line:
break
if read and 'Reaction' in line:
rxns.append(int(line.split()[1].split(':')[0]))
return rxns


def get_mistakenly_marked_dup_rxns(tb: str) -> List[int]:
"""
Get the duplicate reactions from the traceback.

Args:
tb (str): The traceback.

Returns:
List[int]: The reactions indices.
"""
rxns = list()
if tb is None:
return rxns
lines = tb.split('\n')
for line in lines:
if 'No duplicate found for declared duplicate reaction number' in line:
rxns.append(int(line.split()[8]))
return rxns
Loading
Loading