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'))
Exemple #2
0
def get_suffix(data_dir,
               init_spe=None,
               atom_followed=None,
               begin_t=None,
               end_t=None):
    """
    get suffix
    """
    setting = rwc.read_configuration(
        os.path.join(data_dir, 'input', 'setting.json'))
    suffix = ""
    if init_spe is None:
        suffix += "_S" + str(setting['pathway']['init_spe'])
    else:
        suffix += "_S" + str(init_spe)
    if atom_followed is None:
        suffix += "_" + str(setting['pathway']['atom_followed'])
    else:
        suffix += "_" + str(atom_followed)
    if begin_t is None:
        suffix += "_" + str(setting['pathway']['begin_t'])
    else:
        suffix += "_" + str(begin_t)
    if end_t is None:
        suffix += "_" + str(setting['pathway']['end_t'])
    else:
        suffix += "_" + str(end_t)

    return suffix
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'))
Exemple #4
0
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_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)
Exemple #6
0
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 parse_species_pair_reaction(data_dir):
    """
    parse species pairs and associated reactions, coefficients
    """
    f_n = os.path.join(data_dir, "input", "species_pairs_reactions_coefs.json")

    s_p_r_c_old = rwc.read_configuration(f_n)
    s_p_r_c_pair_counter = {}
    s_p_r_c_new = {}
    for idx in s_p_r_c_old:
        src = s_p_r_c_old[idx]['from']
        dst = s_p_r_c_old[idx]['to']
        r_idx = s_p_r_c_old[idx]['r_idx']
        c1 = s_p_r_c_old[idx]['c1']
        c2 = s_p_r_c_old[idx]['c2']
        r_name = s_p_r_c_old[idx]['r_name']

        base_d = {'r_idx': r_idx, 'c1': c1, 'c2': c2, 'r_name': r_name}
        if (src, dst) not in s_p_r_c_new:
            s_p_r_c_pair_counter[(src, dst)] = 1
            s_p_r_c_new[(src, dst)] = {'0': base_d}
        else:
            counter = s_p_r_c_pair_counter[(src, dst)]
            s_p_r_c_new[(src, dst)].update({str(counter): base_d})
            s_p_r_c_pair_counter[(src, dst)] += 1

    # for key in s_p_r_c_new:
    # print(key)
    # print(s_p_r_c_new[key])
    return s_p_r_c_new
Exemple #8
0
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 parse_species_pair_reaction(data_dir):
    """
    parse species pairs and associated reactions, coefficients
    """
    f_n = os.path.join(data_dir, "input", "species_pairs_reactions_coefs.json")

    s_p_r_c = rwc.read_configuration(f_n)
    return s_p_r_c
Exemple #10
0
def get_atom_scheme(data_dir):
    """
    read "atom_scheme.json" and return a dictionary
    """

    f_n_as = os.path.join(data_dir, "input", "atom_scheme.json")
    atom_scheme = rwc.read_configuration(f_n_as)

    return atom_scheme
Exemple #11
0
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 parse_reaction_net_product(data_dir):
    """
    return a dict of "species": number based on reaction product
    """
    f_n = os.path.join(data_dir, "input", "reaction_information.json")

    data = rwc.read_configuration(f_n)

    net_product = {}
    for _, r_idx in enumerate(data):
        entry = {}
        for val1 in data[r_idx]['net_product']:
            entry.update({data[r_idx]['net_product'][val1]['species_index']:
                          data[r_idx]['net_product'][val1]['coefficient']})
        net_product.update({r_idx: entry})

    return net_product
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)