From 5ba291fce7be463c62b91b0586e026848ad96598 Mon Sep 17 00:00:00 2001 From: Alon Grinberg Dana Date: Tue, 3 Dec 2024 15:49:56 +0200 Subject: [PATCH] Modified the signature of get_sa_coefficients() --- t3/simulate/adapter.py | 8 +++++++- t3/simulate/cantera_constantHP.py | 20 +++++++++++++++++--- t3/simulate/cantera_constantTP.py | 20 ++++++++++++++++++-- t3/simulate/cantera_constantUV.py | 19 +++++++++++++++++-- t3/simulate/rmg_constantTP.py | 17 ++++++++++++++++- 5 files changed, 75 insertions(+), 9 deletions(-) diff --git a/t3/simulate/adapter.py b/t3/simulate/adapter.py index fd0949e9..d91ef614 100755 --- a/t3/simulate/adapter.py +++ b/t3/simulate/adapter.py @@ -5,6 +5,7 @@ """ from abc import ABC, abstractmethod +from typing import Optional class SimulateAdapter(ABC): @@ -27,7 +28,12 @@ def simulate(self): pass @abstractmethod - def get_sa_coefficients(self): + def get_sa_coefficients(self, + top_SA_species: int = 10, + top_SA_reactions: int = 10, + max_workers: int = 24, + save_yaml: bool = True, + ) -> Optional[dict]: """ Obtain the sensitivity analysis coefficients. diff --git a/t3/simulate/cantera_constantHP.py b/t3/simulate/cantera_constantHP.py index 836ae024..07b27c13 100644 --- a/t3/simulate/cantera_constantHP.py +++ b/t3/simulate/cantera_constantHP.py @@ -10,6 +10,8 @@ from rmgpy.tools.canteramodel import generate_cantera_conditions from rmgpy.tools.data import GenericData +from arc.common import save_yaml_file + from t3.common import get_observable_label_from_header, get_parameter_from_header from t3.logger import Logger from t3.simulate.adapter import SimulateAdapter @@ -382,12 +384,23 @@ def simulate(self): self.all_data.append((time, condition_data, reaction_sensitivity_data, thermodynamic_sensitivity_data)) - def get_sa_coefficients(self): + def get_sa_coefficients(self, + top_SA_species: int = 10, + top_SA_reactions: int = 10, + max_workers: int = 24, + save_yaml: bool = True, + ) -> Optional[dict]: """ Obtain the SA coefficients. + Args: + top_SA_species (int, optional): The number of top sensitive species to return. + top_SA_reactions (int, optional): The number of top sensitive reactions to return. + max_workers (int, optional): The maximal number of workers to use for parallel processing. + save_yaml (bool, optional): Save the SA dictionary to a YAML file. + Returns: - sa_dict (dict): a SA dictionary, whose structure is given in the docstring for T3/t3/main.py + sa_dict (Optional[dict]): a SA dictionary, whose structure is given in the docstring for T3/t3/main.py """ sa_dict = {'kinetics': dict(), 'thermo': dict(), 'time': list()} @@ -411,7 +424,8 @@ def get_sa_coefficients(self): sa_dict['thermo'][observable_label] = dict() parameter = get_parameter_from_header(spc) sa_dict['thermo'][observable_label][parameter] = spc.data - + if save_yaml: + save_yaml_file(path=self.paths['SA dict'], content=sa_dict) return sa_dict def get_idt_by_T(self): diff --git a/t3/simulate/cantera_constantTP.py b/t3/simulate/cantera_constantTP.py index 48a186a0..549d3339 100644 --- a/t3/simulate/cantera_constantTP.py +++ b/t3/simulate/cantera_constantTP.py @@ -10,6 +10,8 @@ from rmgpy.tools.canteramodel import generate_cantera_conditions from rmgpy.tools.data import GenericData +from arc.common import save_yaml_file + from t3.logger import Logger from t3.simulate.adapter import SimulateAdapter from t3.simulate.factory import register_simulate_adapter @@ -381,12 +383,23 @@ def simulate(self): self.all_data.append((time, condition_data, reaction_sensitivity_data, thermodynamic_sensitivity_data)) - def get_sa_coefficients(self): + def get_sa_coefficients(self, + top_SA_species: int = 10, + top_SA_reactions: int = 10, + max_workers: int = 24, + save_yaml: bool = True, + ) -> Optional[dict]: """ Obtain the SA coefficients. + Args: + top_SA_species (int, optional): The number of top sensitive species to return. + top_SA_reactions (int, optional): The number of top sensitive reactions to return. + max_workers (int, optional): The maximal number of workers to use for parallel processing. + save_yaml (bool, optional): Save the SA dictionary to a YAML file. + Returns: - sa_dict (dict): a SA dictionary, whose structure is given in the docstring for T3/t3/main.py + sa_dict (Optional[dict]): a SA dictionary, whose structure is given in the docstring for T3/t3/main.py """ sa_dict = {'kinetics': dict(), 'thermo': dict(), 'time': list()} @@ -415,6 +428,9 @@ def get_sa_coefficients(self): parameter = spc.label.split('[')[2].split(']')[0] sa_dict['thermo'][observable_label][parameter] = spc.data + if save_yaml: + save_yaml_file(path=self.paths['SA dict'], content=sa_dict) + return sa_dict def get_idt_by_T(self): diff --git a/t3/simulate/cantera_constantUV.py b/t3/simulate/cantera_constantUV.py index bffbed7a..b7e17caa 100644 --- a/t3/simulate/cantera_constantUV.py +++ b/t3/simulate/cantera_constantUV.py @@ -10,6 +10,8 @@ from rmgpy.tools.canteramodel import generate_cantera_conditions from rmgpy.tools.data import GenericData +from arc.common import save_yaml_file + from t3.logger import Logger from t3.simulate.adapter import SimulateAdapter from t3.simulate.factory import register_simulate_adapter @@ -381,12 +383,23 @@ def simulate(self): self.all_data.append((time, condition_data, reaction_sensitivity_data, thermodynamic_sensitivity_data)) - def get_sa_coefficients(self): + def get_sa_coefficients(self, + top_SA_species: int = 10, + top_SA_reactions: int = 10, + max_workers: int = 24, + save_yaml: bool = True, + ) -> Optional[dict]: """ Obtain the SA coefficients. + Args: + top_SA_species (int, optional): The number of top sensitive species to return. + top_SA_reactions (int, optional): The number of top sensitive reactions to return. + max_workers (int, optional): The maximal number of workers to use for parallel processing. + save_yaml (bool, optional): Save the SA dictionary to a YAML file. + Returns: - sa_dict (dict): a SA dictionary, whose structure is given in the docstring for T3/t3/main.py + sa_dict (Optional[dict]): a SA dictionary, whose structure is given in the docstring for T3/t3/main.py """ sa_dict = {'kinetics': dict(), 'thermo': dict(), 'time': list()} @@ -415,6 +428,8 @@ def get_sa_coefficients(self): parameter = spc.label.split('[')[2].split(']')[0] sa_dict['thermo'][observable_label][parameter] = spc.data + if save_yaml: + save_yaml_file(path=self.paths['SA dict'], content=sa_dict) return sa_dict def get_idt_by_T(self): diff --git a/t3/simulate/rmg_constantTP.py b/t3/simulate/rmg_constantTP.py index 8c300234..a361c578 100755 --- a/t3/simulate/rmg_constantTP.py +++ b/t3/simulate/rmg_constantTP.py @@ -18,6 +18,8 @@ from rmgpy.tools.loader import load_rmg_py_job from rmgpy.tools.plot import plot_sensitivity +from arc.common import save_yaml_file + from t3.common import get_chem_to_rmg_rxn_index_map, get_species_by_label, get_values_within_range, \ get_observable_label_from_header, get_parameter_from_header, time_lapse from t3.simulate.adapter import SimulateAdapter @@ -224,10 +226,21 @@ def simulate(self): self.logger.info(f'Simulation via RMG completed, execution time: {time_lapse(tic)}') - def get_sa_coefficients(self) -> Optional[dict]: + def get_sa_coefficients(self, + top_SA_species: int = 10, + top_SA_reactions: int = 10, + max_workers: int = 24, + save_yaml: bool = True, + ) -> Optional[dict]: """ Obtain the SA coefficients. + Args: + top_SA_species (int, optional): The number of top sensitive species to return. + top_SA_reactions (int, optional): The number of top sensitive reactions to return. + max_workers (int, optional): The maximal number of workers to use for parallel processing. + save_yaml (bool, optional): Save the SA dictionary to a YAML file. + Returns: sa_dict (Optional[dict]): An SA dictionary, structure is given in the docstring for T3/t3/main.py """ @@ -265,6 +278,8 @@ def get_sa_coefficients(self) -> Optional[dict]: parameter = chem_to_rmg_rxn_index_map[int(parameter)] \ if all(c.isdigit() for c in parameter) else parameter sa_dict[sa_type][observable_label][parameter] = df[header].values + if save_yaml: + save_yaml_file(path=self.paths['SA dict'], content=sa_dict) return sa_dict def generate_rmg_reactors_for_simulation(self) -> dict: