From fd12f0c1fd56f16dac30c5c168b906f469a67368 Mon Sep 17 00:00:00 2001 From: Alon Grinberg Dana Date: Tue, 24 Oct 2023 22:26:46 +0300 Subject: [PATCH 1/8] Added pydot to env --- environment.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/environment.yml b/environment.yml index 98e36bb8..39864b76 100644 --- a/environment.yml +++ b/environment.yml @@ -3,6 +3,7 @@ channels: - conda-forge dependencies: - python=3.7 + - pydot - cantera=2.6 - ca-certificates - openssl From 3c1620fcfed227755696b487170fdf336d72ce3b Mon Sep 17 00:00:00 2001 From: Alon Grinberg Dana Date: Tue, 24 Oct 2023 22:27:38 +0300 Subject: [PATCH 2/8] Added almost_equal() to tests common --- tests/common.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/tests/common.py b/tests/common.py index dc33779b..6a68f359 100644 --- a/tests/common.py +++ b/tests/common.py @@ -61,3 +61,22 @@ def check_expected_generated_radicals(radicals: list, expected_radicals: list): for rad_tuple in radicals: assert rad_tuple[0] in expected_labels assert any(isomorphic_smiles(rad_tuple[1], expected_rad_tuple[1]) for expected_rad_tuple in expected_radicals) + + +def almost_equal(a: float, + b: float, + places: int = 4, + ratio: Optional[float] = None, + ) -> bool: + """ + A helper function for testing almost equal assertions. + + Args: + a (float): The first number. + b (float): The second number. + places (int, optional): The number of decimal places to round to. Default is 4. + ratio (float, optional): The ratio between the two numbers. Default is None. + """ + if ratio is not None: + return abs(a - b) / abs(a) < ratio + return round(abs(a - b), places) == 0 From f275fe8b600678c5a087c7d736c8dfb87009d939 Mon Sep 17 00:00:00 2001 From: Alon Grinberg Dana Date: Thu, 2 Nov 2023 11:05:28 +0200 Subject: [PATCH 3/8] Added the flux module --- t3/utils/flux.py | 938 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 938 insertions(+) create mode 100644 t3/utils/flux.py diff --git a/t3/utils/flux.py b/t3/utils/flux.py new file mode 100644 index 00000000..b84aedd0 --- /dev/null +++ b/t3/utils/flux.py @@ -0,0 +1,938 @@ +""" +t3 utils flux module +""" + +from typing import Dict, List, Optional, Set, Tuple + +import os + +import cantera as ct +import matplotlib.pyplot as plt +import numpy as np +import pydot + +from t3.utils.fix_cantera import fix_cantera + + +def generate_flux(model_path: str, + folder_path: str, + observables: List[str], + times: List[float], + composition: Dict[str, float], + T: float, + P: float, + V: Optional[float] = None, + reactor_type: str = 'JSR', + energy: bool = False, + a_tol: float = 1e-16, + r_tol: float = 1e-10, + explore_tol: float = 0.95, + dead_end_tol: float = 0.10, + generate_separate_diagrams_per_observable: bool = False, + display_flux_ratio: bool = True, + display_concentrations: bool = True, + display_r_n_p: bool = True, + scaling: Optional[float] = None, + fix_cantera_model: bool = True, + ): + """ + Generate a flux diagram for a given model and composition. + The flux diagram is generated for a specific time using the given reactor type. + + Args: + model_path (str): The path to the cantera YAML model file. + folder_path (str): The path to the folder in which to save the flux diagrams and accompanied data. + observables (List[str]): The species to generate ROP diagrams for. + times (List[float]): The times to generate flux diagrams for, in seconds. + For a JSR, these times represent residence times. + composition (Dict[str, float]): The composition of the mixture. Keys are species labels as appears in the model, + values are mole fractions. + T (float): The temperature of the mixture, in Kelvin. + P (float): The pressure of the mixture, in bar. + V (Optional[float], optional): The reactor volume in cm^3, if relevant. + reactor_type (str, optional): The reactor type. Supported reactor types are: + 'JSR': Jet stirred reactor, which is a CSTR with constant T/P/V + 'BatchP': An ideal gas constant pressure and constant volume batch reactor + energy (bool, optional): Whether to turn energy equations on (False means adiabatic conditions). + a_tol (float, optional): The absolute tolerance for the simulation. + r_tol (float, optional): The relative tolerance for the simulation. + explore_tol (float, optional): The minimal flux to capture of each species consumption pathway. + dead_end_tol (float, optional): A flux exploration termination criterion. + Don't explore further consumption is lower than this tolerance + times the net rate of production. + generate_separate_diagrams_per_observable (bool, optional): Whether to generate a separate flux diagram for each observable. + display_flux_ratio (bool, optional): Whether to display the flux ratio. + display_concentrations (bool, optional): Whether to display the concentrations. + display_r_n_p (bool, optional): Whether to display the other reactants and products on each arrow. + scaling (Optional[float], optional): The scaling of the final image, 100 means no scaling. + fix_cantera_model (bool, optional): Whether to fix the Cantera model before running the simulation. + + Structures: + profiles: {