def spe_composition_2_atom_scheme(data_dir): """ convert species grouped atom scheme, which refers to file named "spe_composition.json" generated from cantera to a new file named "atom_scheme_base.json" """ spe_comp = rwc.read_configuration( os.path.join(data_dir, "input", "spe_composition.json")) atom_scheme = {} for _, s_1 in enumerate(spe_comp): # print(s_1) for atom_1 in spe_comp[s_1]: if atom_1 not in atom_scheme: atom_scheme[atom_1] = {str(s_1): spe_comp[s_1][atom_1]} else: atom_scheme[atom_1].update({str(s_1): spe_comp[s_1][atom_1]}) fn0 = os.path.join(data_dir, "input", "atom_scheme_base_backup.json") fn1 = os.path.join(data_dir, "input", "atom_scheme_base.json") if os.path.isfile(fn1): copy2(fn1, fn0) rwc.write_configuration(atom_scheme, fn1)
def update_basic_setting(data_dir, g_s): """ update settings.json, the basic information that's will not change for this system """ # there will always be a current setting fn0 = os.path.join(data_dir, "input", "setting_backup.json") fn1 = os.path.join(data_dir, "input", "setting.json") if os.path.isfile(fn1): copy2(fn1, fn0) setting = rwc.read_configuration( os.path.join(data_dir, 'input', 'setting.json')) setting['system']['condition'] = g_s['system']['condition'] setting['system']['initializer'] = g_s['system']['initializer'] setting['network']['merge_chatterings'] = g_s['network'][ 'merge_chatterings'] setting['propagator']['primary_type'] = g_s['propagator']['primary_type'] setting['propagator']['type'] = g_s['propagator']['type'] setting['propagator']['sub_type'] = g_s['propagator']['sub_type'] setting['propagator'][ 'convert_molar_concentration_to_mole_fraction'] = g_s['propagator'][ 'convert_molar_concentration_to_mole_fraction'] setting['propagator']['normalize_initial_concentration'] = g_s[ 'propagator']['normalize_initial_concentration'] rwc.write_configuration(setting, os.path.join(data_dir, 'input', 'setting.json'))
def update_fast_reaction(data_dir, tau=0.7, end_t=1.0, tag="M"): """ update fast reaction based on reference trajectory """ fn0 = os.path.join(data_dir, "input", "reaction_info_base_backup.json") fn1 = os.path.join(data_dir, "input", "reaction_info_base.json") rxn_info = rwc.read_configuration(fn1) time_v = np.loadtxt(os.path.join(data_dir, "output", "time_dlsode_" + str(tag) + ".csv"), delimiter=",") rxn_rates = np.loadtxt(os.path.join( data_dir, "output", "reaction_rate_dlsode_" + str(tag) + ".csv"), delimiter=",") actual_time = float(tau) * float(end_t) for _, val in enumerate(rxn_info): actual_rate = interpolation.interp1d(time_v, rxn_rates[:, int(val)], actual_time) if actual_rate != 0: time_scale = np.log10(actual_rate) if time_scale >= -100 and time_scale <= 100: # print(time_scale) rxn_info[val]["time_scale"] = time_scale if os.path.isfile(fn1): copy2(fn1, fn0) rwc.write_configuration(rxn_info, fn1)
def spe_information_2_atom_scheme(data_dir): """ convert species information "species_information.json" to a new file named "atom_scheme_base.json" """ spe_comp = rwc.read_configuration( os.path.join(data_dir, "input", "species_information.json")) atom_scheme = {} for _, spe_idx in enumerate(spe_comp): s_1 = spe_comp[spe_idx]["name"] for atom_1 in spe_comp[spe_idx]["spe_composition"]: atom_number = spe_comp[spe_idx]["spe_composition"][atom_1] if atom_number == "0": continue if atom_1 not in atom_scheme: atom_scheme[atom_1] = {str(s_1): float(atom_number)} else: atom_scheme[atom_1].update({str(s_1): float(atom_number)}) fn0 = os.path.join(data_dir, "input", "atom_scheme_base_backup.json") fn1 = os.path.join(data_dir, "input", "atom_scheme_base.json") if os.path.isfile(fn1): copy2(fn1, fn0) rwc.write_configuration(atom_scheme, fn1)
def update_eval_path_AT_with_SP(data_dir, top_n=5, n_traj=10000, tau=10.0, begin_t=0.0, end_t=1.0): """ update settings.json, primarily for evaluate pathway arrival time """ # there will always be a current setting fn0 = os.path.join(data_dir, "input", "setting_backup.json") fn1 = os.path.join(data_dir, "input", "setting.json") if os.path.isfile(fn1): copy2(fn1, fn0) setting = rwc.read_configuration( os.path.join(data_dir, 'input', 'setting.json')) setting['job']['job_type'] = "evaluate_path_AT_with_SP_over_time" setting['pathway']['topN'] = [int(top_n)] setting['pathway']['trajectoryNumber'] = int(n_traj) setting['time']['tau'] = tau setting['pathway']['begin_t'] = begin_t setting['pathway']['end_t'] = end_t rwc.write_configuration(setting, os.path.join(data_dir, 'input', 'setting.json'))
def update_a_atom_entry(data_dir, source_atoms=None, entry_name="HA4", number=1.0): """ update a atom entry based on atoms list """ if source_atoms is None or source_atoms is []: return f_n_as = os.path.join(data_dir, "input", "atom_scheme.json") atom_scheme = rwc.read_configuration(f_n_as) new_entry = {} for atom in source_atoms: if atom not in atom_scheme: continue for key in atom_scheme[atom]: if key not in new_entry: new_entry.update({key: number}) atom_scheme.update({entry_name: new_entry}) fn0 = os.path.join(data_dir, "input", "atom_scheme_base_backup.json") fn1 = os.path.join(data_dir, "input", "atom_scheme_base.json") if os.path.isfile(fn1): copy2(fn1, fn0) rwc.write_configuration(atom_scheme, fn1)
def atom_scheme_set_atom_number(data_dir, followed_atom="C", number=1.0): """ modify "atom_scheme_base.json", change atom number to number """ fn1 = os.path.join(data_dir, "input", "atom_scheme_base.json") atom_scheme = rwc.read_configuration(fn1) for key in atom_scheme[followed_atom]: atom_scheme[followed_atom][key] = number fn0 = os.path.join(data_dir, "input", "atom_scheme_base_backup.json") if os.path.isfile(fn1): copy2(fn1, fn0) rwc.write_configuration(atom_scheme, fn1)
def initiate_fast_reaction(data_dir): """ intiate file named "reaction_info_base.json" based on file named "reaction_labelling.csv" """ new_old_index_dict, new_ind_reaction_dict = psri.parse_reaction_and_its_index( data_dir) rxn_pair_dict = dict() # un-paired species unpaired = dict() for _, val1 in enumerate(new_old_index_dict): # print(idx, val1, new_old_index_dict[val1]) this_value = int(new_old_index_dict[val1]) neg_value = -1 * this_value if (neg_value in unpaired): # print(int(val1), unpaired[neg_value]) rxn_pair_dict.update({int(val1): unpaired[neg_value]}) rxn_pair_dict.update({unpaired[neg_value]: int(val1)}) unpaired.pop(neg_value) else: unpaired.update({this_value: int(val1)}) rxn_info = {} for _, val1 in enumerate(new_ind_reaction_dict): entry = { str(val1): { "formula": new_ind_reaction_dict[val1], # default value, 10^-100, assume the reaction is super slow "time_scale": -100, "reverse_reaction": "None" } } if int(val1) in rxn_pair_dict: entry[str(val1)]["reverse_reaction"] = \ str(rxn_pair_dict[int(val1)]) rxn_info.update(entry) fn0 = os.path.join(data_dir, "input", "reaction_info_base_backup.json") fn1 = os.path.join(data_dir, "input", "reaction_info_base.json") if os.path.isfile(fn1): copy2(fn1, fn0) rwc.write_configuration(rxn_info, fn1)
def update_mc_trajectory_setting(data_dir, n_traj=1000000, atom_followed="C", init_spe=114, tau=10.0, begin_t=0.0, end_t=1.0, species_path=False): """ update settings.json, primarily for generate_pathway_running_Monte_carlo_trajectory """ # there will always be a current setting fn0 = os.path.join(data_dir, "input", "setting_backup.json") fn1 = os.path.join(data_dir, "input", "setting.json") if os.path.isfile(fn1): copy2(fn1, fn0) setting = rwc.read_configuration( os.path.join(data_dir, 'input', 'setting.json')) chattering_spe = global_settings.get_chattering_species( data_dir, atom_followed) setting['pathway']['chattering_species'] = chattering_spe setting['time']['tau'] = tau setting['pathway']['trajectoryNumber'] = n_traj setting['pathway']['atom_followed'] = atom_followed setting['pathway']['init_spe'] = init_spe setting['pathway']['begin_t'] = begin_t setting['pathway']['end_t'] = end_t if species_path is True: setting['job'][ 'job_type'] = "generate_species_pathway_running_Monte_carlo_trajectory" else: setting['job'][ 'job_type'] = "generate_pathway_running_Monte_carlo_trajectory" rwc.write_configuration(setting, os.path.join(data_dir, 'input', 'setting.json')) return
def update_basic_setting(data_dir, g_s): """ update settings.json, the basic information that's will not change for this system """ # there will always be a current setting fn0 = os.path.join(data_dir, "input", "setting_backup.json") fn1 = os.path.join(data_dir, "input", "setting.json") if os.path.isfile(fn1): copy2(fn1, fn0) setting = rwc.read_configuration( os.path.join(data_dir, 'input', 'setting.json')) setting['time']['max_time'] = g_s['traj_max_t'] setting['time']['critical_time'] = g_s['traj_critical_t'] setting['time']['tau'] = g_s['tau'] setting['system']['condition'] = g_s['system']['condition'] setting['system']['initializer'] = g_s['system']['initializer'] setting['network']['merge_chatterings'] = g_s['network'][ 'merge_chatterings'] setting['network']['condense_chatterings'] = g_s['network'][ 'condense_chatterings'] setting['propagator']['primary_type'] = g_s['propagator']['primary_type'] setting['propagator']['type'] = g_s['propagator']['type'] setting['propagator']['sub_type'] = g_s['propagator']['sub_type'] setting['propagator'][ 'convert_molar_concentration_to_mole_fraction'] = g_s['propagator'][ 'convert_molar_concentration_to_mole_fraction'] setting['propagator']['normalize_initial_concentration'] = g_s[ 'propagator']['normalize_initial_concentration'] setting['pathway']['fixed_t0_or_tf'] = g_s['fixed_t0_or_tf'] setting['pathway']['not_allowed_out_species'] = g_s['network'][ 'not_allowed_out_species'] setting['pathway']['spe_branching'] = g_s['network']['spe_branching'] setting['pathway']['terminal_sp'] = g_s['network']['terminal_sp'] rwc.write_configuration(setting, os.path.join(data_dir, 'input', 'setting.json'))
def update_spe_concentration_at_time_w2f(data_dir, tau=10.0, end_t=1.0): """ update settings.json, primarily for update_spe_concentration_at_time_w2f """ # there will always be a current setting fn0 = os.path.join(data_dir, "input", "setting_backup.json") fn1 = os.path.join(data_dir, "input", "setting.json") if os.path.isfile(fn1): copy2(fn1, fn0) setting = rwc.read_configuration( os.path.join(data_dir, 'input', 'setting.json')) setting['job']['job_type'] = "write_concentration_at_time_to_file" setting['time']['tau'] = tau setting['pathway']['end_t'] = end_t rwc.write_configuration(setting, os.path.join(data_dir, 'input', 'setting.json'))
def update_dlsode_setting(data_dir, max_time=1.0, critical_time=0.9): """ update settings.json, primarily for dlsode run and pathway generating """ # there will always be a current setting fn0 = os.path.join(data_dir, "input", "setting_backup.json") fn1 = os.path.join(data_dir, "input", "setting.json") if os.path.isfile(fn1): copy2(fn1, fn0) setting = rwc.read_configuration( os.path.join(data_dir, 'input', 'setting.json')) setting['time']['critical_time'] = critical_time setting['time']['max_time'] = max_time setting['job']['job_type'] = "solve_ODEs_for_concentration_using_LSODE" rwc.write_configuration(setting, os.path.join(data_dir, 'input', 'setting.json'))
def update_eval_path_integral(data_dir, top_n=5, n_traj=10000, atom_followed="C", init_spe=114, tau=10.0, begin_t=0.0, end_t=1.0, species_path=False): """ update settings.json, primarily for evaluate path integral """ # there will always be a current setting fn0 = os.path.join(data_dir, "input", "setting_backup.json") fn1 = os.path.join(data_dir, "input", "setting.json") if os.path.isfile(fn1): copy2(fn1, fn0) setting = rwc.read_configuration( os.path.join(data_dir, 'input', 'setting.json')) chattering_spe = global_settings.get_chattering_species( data_dir, atom_followed) setting['pathway']['chattering_species'] = chattering_spe if species_path is True: setting['job']['job_type'] = "evaluate_species_path_integral_over_time" else: setting['job']['job_type'] = "evaluate_path_integral_over_time" setting['pathway']['topN'] = [top_n] setting['pathway']['trajectoryNumber'] = n_traj setting['pathway']['atom_followed'] = atom_followed setting['pathway']['init_spe'] = init_spe setting['time']['tau'] = tau setting['pathway']['begin_t'] = begin_t setting['pathway']['end_t'] = end_t rwc.write_configuration(setting, os.path.join(data_dir, 'input', 'setting.json'))
def update_chattering_species_setting(data_dir, atom_followed="C"): """ update settings.json, primarily for chattering species and fast reactions """ # there will always be a current setting fn0 = os.path.join(data_dir, "input", "setting_backup.json") fn1 = os.path.join(data_dir, "input", "setting.json") if os.path.isfile(fn1): copy2(fn1, fn0) setting = rwc.read_configuration( os.path.join(data_dir, 'input', 'setting.json')) chattering_spe = global_settings.get_chattering_species( data_dir, atom_followed) setting['pathway']['chattering_species'] = chattering_spe rwc.write_configuration(setting, os.path.join(data_dir, 'input', 'setting.json')) return
def fast_reaction_w2f(data_dir, threshold=-7): """ prepare "fast_reaction.json" file, this file will be 1) manually changed later 2) used to generate chattering information later """ fast_transition = {} counter = 0 fn_rib = os.path.join(data_dir, "input", "reaction_info_base.json") rxn_info = rwc.read_configuration(fn_rib) # care fast reaction pair only, both forward and backward reactions are fast unpaired_fast_rxn = set() for _, val in enumerate(rxn_info): if rxn_info[val]["reverse_reaction"] != "None" \ and float(rxn_info[val]["time_scale"]) >= threshold: # print(rxn_info[val]["formula"]) this_rxn = str(val) paired_rxn = rxn_info[val]["reverse_reaction"] if paired_rxn not in unpaired_fast_rxn: unpaired_fast_rxn.add(this_rxn) else: entry = { str(counter): { "formula1": rxn_info[paired_rxn]["formula"], "formula2": rxn_info[this_rxn]["formula"], "reaction1": int(paired_rxn), "reaction2": int(this_rxn) } } fast_transition.update(entry) counter += 1 fn_frb0 = os.path.join(data_dir, "input", "fast_reaction_base_backup.json") fn_frb1 = os.path.join(data_dir, "input", "fast_reaction_base.json") if os.path.isfile(fn_frb1): copy2(fn_frb1, fn_frb0) rwc.write_configuration(fast_transition, fn_frb1)
def update_s_a_setting(data_dir, init_temp=1000, critical_temp=1100, target_temp=1800, end_temp=1900, spe_idx_conc=None): """ update settings.json, primarily for sensitivity analysis the last parameter represents "species index concentration", is a dict """ # there will always be a current setting fn0 = os.path.join(data_dir, "input", "setting_backup.json") fn1 = os.path.join(data_dir, "input", "setting.json") if os.path.isfile(fn1): copy2(fn1, fn0) setting = rwc.read_configuration( os.path.join(data_dir, 'input', 'setting.json')) setting['propagator']['primary_type'] = "not_from_file" setting['propagator']['type'] = "dlsode" setting['propagator']['sub_type'] = "temperature_propagator_cv_s2m_pgt" setting['propagator']['normalize_initial_concentration'] = "no" setting['job']['job_type'] = "evaluate_ignition_delay_time_once" setting['chem_init']['init_temperature'] = init_temp setting['T']['critical_temperature'] = critical_temp setting['T']['target_temperature'] = target_temp setting['T']['end_temperature'] = end_temp if spe_idx_conc is not None: setting['chem_init']['species_index_concentration'] = spe_idx_conc rwc.write_configuration(setting, os.path.join(data_dir, 'input', 'setting.json'))
def update_terminal_species_setting(data_dir, terminal_spe=None): """ update settings.json, primarily for terminal species """ # there will always be a current setting fn0 = os.path.join(data_dir, "input", "setting_backup.json") fn1 = os.path.join(data_dir, "input", "setting.json") if os.path.isfile(fn1): copy2(fn1, fn0) setting = rwc.read_configuration( os.path.join(data_dir, 'input', 'setting.json')) t_s = [] if terminal_spe is not None and terminal_spe is not []: for _, val in enumerate(terminal_spe): t_s.append(val) setting['pathway']['terminal_species'] = t_s rwc.write_configuration(setting, os.path.join(data_dir, 'input', 'setting.json')) return
def generate_fast_rxn_chattering_spe(data_dir): """ generate fast reaction and chattering species based on four files 0) species_information.json 1) reaction_information.json 2) atom_scheme.json 3) fast_reaction_base.json save file named "fast_transition.json", this file will be used to update file "local_settings.py" manually """ f_n_si = os.path.join(data_dir, "input", "species_information.json") f_n_ri = os.path.join(data_dir, "input", "reaction_information.json") f_n_as = os.path.join(data_dir, "input", "atom_scheme.json") f_n_frb = os.path.join(data_dir, "input", "fast_reaction_base.json") spe_info = rwc.read_configuration(f_n_si) rxn_info = rwc.read_configuration(f_n_ri) atom_scheme = rwc.read_configuration(f_n_as) fast_rxn_base = rwc.read_configuration(f_n_frb) fast_transition = [] for _, val1 in enumerate(fast_rxn_base): entry = {} rxn_1_idx = fast_rxn_base[val1]["reaction1"] rxn_2_idx = fast_rxn_base[val1]["reaction2"] reactant1 = rxn_info[str(rxn_1_idx)]["net_reactant"] reactant2 = rxn_info[str(rxn_2_idx)]["net_reactant"] entry.update({"formula1": fast_rxn_base[val1]["formula1"]}) entry.update({"formula2": fast_rxn_base[val1]["formula2"]}) entry.update({"rxn": [int(rxn_1_idx), int(rxn_2_idx)]}) entry.update({"spe": {}}) s_1_idx = "None" s_2_idx = "None" for atom_followed in atom_scheme: for _, val2 in enumerate(reactant1): spe_idx = reactant1[val2]["species_index"] spe_name = spe_info[spe_idx]["name"] if spe_name in atom_scheme[atom_followed]: s_1_idx = str(spe_idx) # only one species allowed break for _, val2 in enumerate(reactant2): spe_idx = reactant2[val2]["species_index"] spe_name = spe_info[spe_idx]["name"] if spe_name in atom_scheme[atom_followed]: s_2_idx = str(spe_idx) # only one species allowed break if s_1_idx != "None" and s_2_idx != "None": entry["spe"].update( {atom_followed: [int(s_1_idx), int(s_2_idx)]}) fast_transition.append(entry) fn_ft0 = os.path.join(data_dir, "input", "fast_transition_backup.json") fn_ft1 = os.path.join(data_dir, "input", "fast_transition.json") if os.path.isfile(fn_ft1): copy2(fn_ft1, fn_ft0) rwc.write_configuration(fast_transition, fn_ft1)