def test_case_maker_phi_oxi():
    phi = [0.5 + rand(), 2, 1]
    oxidizer = [0.2 + rand() / 5, 1, 1]
    P = [rand(), 100, 1]
    T = [500 * rand(), 1000, 1]
    mix_params = ('phi_oxi', phi, oxidizer)
    conditions = {
        'Parameters': [P, T, mix_params, 'log'],
        'Mixture': [{
            'H2': 1
        }, {
            'N2': 1
        }, {
            'O2': 1
        }, 'phi_oxi'],
        'Files': [cf.model_folder('grimech30.cti'), None]
    }
    paramlist = cf.case_maker(conditions)
    case = paramlist[0]
    mixture = case[2]

    nptest.assert_allclose(case[:2], [P[0], T[0]])
    nptest.assert_allclose(mixture['O2'], oxidizer[0])
    nptest.assert_allclose(1, mixture['H2'] + mixture['O2'] + mixture['N2'])
    nptest.assert_allclose(phi[0], mixture['H2'] / mixture['O2'] * 0.5)
    assert all([x >= 0 for k, x in mixture.items()])
def test_multi_f_o():
    phi = [0.3 + rand(), 2, 1]
    fuel = [0.2 + rand() / 5, 1, 1]
    P = [rand(), 100, 1]
    T = [500 * rand(), 1000, 1]
    mix_params = ('phi_fuel', phi, fuel)

    conditions = {
        'Parameters': [P, T, mix_params, 'log'],
        'Mixture': [{
            'H2': .3,
            'CO': 0.7
        }, {
            'N2': 0.5,
            'HE': 0.5
        }, {
            'O2': 0.95,
            'AR': 0.05
        }, 'phi_fuel'],
        'Files': [cf.model_folder('grimech30.cti'), None]
    }
    paramlist = cf.case_maker(conditions)
    case = paramlist[0]
    mixture = case[2]

    nptest.assert_allclose(case[:2], [P[0], T[0]])
    nptest.assert_allclose(mixture['H2'] + mixture['CO'], fuel[0])
    nptest.assert_allclose(
        1, mixture['H2'] + mixture['O2'] + mixture['N2'] + mixture['AR'] +
        mixture['CO'] + mixture['HE'])
    nptest.assert_allclose(phi[0], (mixture['H2'] + mixture['CO']) /
                           mixture['O2'] * 0.5)
    nptest.assert_allclose(0.3 / 0.7, mixture['H2'] / mixture['CO'])
    nptest.assert_allclose(0.95 / 0.05, mixture['O2'] / mixture['AR'])
    assert all([x >= 0 for k, x in mixture.items()])
def test_case_maker_oxi_dil():
    oxi = [rand() / 2, 1, 1]
    dil = [rand() / 2, 1, 1]
    P = [rand(), 100, 1]
    T = [500 * rand(), 1000, 1]
    mix_params = ('oxi_dil', oxi, dil)
    conditions = {
        'Parameters': [P, T, mix_params, 'log'],
        'Mixture': [{
            'H2': 1
        }, {
            'N2': 1
        }, {
            'O2': 1
        }, 'oxi_dil'],
        'Files': [cf.model_folder('grimech30.cti'), None]
    }
    paramlist = cf.case_maker(conditions)
    case = paramlist[0]
    mixture = case[2]
    print(mixture)

    nptest.assert_allclose(case[:2], [P[0], T[0]])
    assert all([x >= 0 for k, x in mixture.items()])
    nptest.assert_allclose(1, mixture['H2'] + mixture['O2'] + mixture['N2'])
    nptest.assert_allclose(dil[0], mixture['N2'])
    nptest.assert_allclose(oxi[0], mixture['O2'])
def run_flame_simulation(mech, arrtype, pres, temp, fue, oxi, dilu, mix_params,
                         safi, par, Mingrid, Mul_soret, Loglevel):
    """
    Takes information from initializer and runs necessary functions to perform
    a one-dimensional simulation. Simulation results will be saved if booleans
    are set to True.

    Parameters
    ----------
    mech : str
        A .cti mechanism file containing all reaction and species information.
    arrtype : str
        Defines the scale that conditions are in. Either linear or logarithmic
    pres : list
        A list of pressure conditions to test over [initial, final, number of points].
    temp : list
        A list of temperature conditions to test over [initial, final, number of points].
    fue : str or list
        As a string the variable represents a single species of fuel being used.
        As a list the variable represents multicomponent fuel species
        followed by the percentage to the total fuel [Component1, % of total, ...]
    oxi : str or list
        As a string the variable represents a single species of oxidizer being used.
        As a list the variable represents multicomponent oxidizer species
        followed by the percentage to the total oxidizer [Component1, % of total, ...]
    dilu : str or list
        As a string the variable represents a single species of diluent being used.
        As a list the variable represents multicomponent diluent species
        followed by the percentage to the total diluent [Component1, % of total, ...]
    mix_params : list
        A list of the two mixture parameters and mixtrue type used in creating
        a mixture.
    safi : boolean
        If true simulation conditions and ranking results will be saved.
    par : bool
        If true, run simulations in paralle
    Mingrid: int
        Number of points to be solved in the simulation
    Mul_soret : boolean
        Multicomponent diffuction and Soret effect are calculated if true
    Loglevel : int
        A number from 1 to 10. The larger the number the more information that
        is printed during the simulation to the user.

    Returns
    -------
    None.

    """
    mechan = cf.model_folder(mech)
    condi = initialization(mechan, arrtype, pres, temp, fue, oxi, dilu,
                           mix_params, Mingrid, Mul_soret, Loglevel)
    paralist = cf.case_maker(condi)
    simtime = run_simulations(condi, paralist, par)
    flame_info, flame_info_unfiltered, sim_info = load_filter_flame_info(condi)
    siminfo = [simtime, sim_info[0], simtime + sim_info[1]]
    if safi:
        file_saving(condi, flame_info, paralist, siminfo)
def test_mixture_percentage():
    phi = [0.3 + rand(), 2, 1]
    fuel = [0.2 + rand() / 5, 1, 1]
    P = [rand(), 100, 1]
    T = [500 * rand(), 1000, 1]
    mix_params = ('phi_fuel', phi, fuel)

    multif = {'H2': .3, 'CO': 0.7}
    singlf = {'H2': 1}

    list_conditions = {
        'Parameters': [P, T, mix_params, 'log'],
        'Mixture': [multif, {
            'N2': 1
        }, {
            'O2': 1
        }, 'phi_fuel'],
        'Files': [cf.model_folder('grimech30.cti'), None]
    }
    stri_conditions = {
        'Parameters': [P, T, mix_params, 'log'],
        'Mixture': [singlf, {
            'N2': 1
        }, {
            'O2': 1
        }, 'phi_fuel'],
        'Files': [cf.model_folder('grimech30.cti'), None]
    }
    list_paramlist = cf.case_maker(list_conditions)
    list_case = list_paramlist[0]
    list_mixture = list_case[2]
    stri_paramlist = cf.case_maker(stri_conditions)
    stri_case = stri_paramlist[0]
    stri_mixture = stri_case[2]

    lst_comp = cf.mixture_percentage(multif, list_mixture)
    str_comp = cf.mixture_percentage(singlf, stri_mixture)

    nptest.assert_allclose(list_case[:2], [P[0], T[0]])
    nptest.assert_allclose(stri_case[:2], [P[0], T[0]])
    nptest.assert_allclose(lst_comp, fuel[0])
    nptest.assert_allclose(str_comp, fuel[0])
    assert all([x >= 0 for k, x in list_mixture.items()])
    assert all([x >= 0 for k, x in stri_mixture.items()])
    print('\nStart file saving...')
    file = os.path.join(save_path, 'Flame Information.pkl')
    with open(file, 'wb') as f:
        pickle.dump(fla_inf, f)
    shutil.rmtree(os.path.join(
        save_path, 'all_flame_sims'))  # clean up individual flame files
    save_time_end = time.time()
    save_time = save_time_end - save_time_start
    print('Total File Save Time: ' + format(save_time, '0.5f') + ' seconds.\n')
    print('End file saving')


if __name__ == "__main__":
    #Set experiment parameters
    mech_name = 'Li_model_modified_trioxane.cti'  #Mechanism file
    Mechanism = cf.model_folder(mech_name)

    #Parameters for main loop
    # P and Phi are used in all cases.
    # Whether FTD or OtD is used depends on mixture type.
    # Each list should follow patter [First Point, Last Point, # of Points]
    # Array_type's (log, lin)
    #  log creates a logspace array of parameters
    #  lin creates a linspace array of parameters
    Array_type = 'lin'
    Press = [0.5, 1, 2]  #Pressure [atm]
    E_Ratio = [0.25, 1.2, 2]  #Equivalence ratio
    FO_to_D = [0.05, 0.95,
               2]  #Amount of Fuel/Oxidizer to Diluent........This isn't used?
    F_to_D = [0.75, 0.95, 2]  #Fuel/(Fuel + Diluent)
    O_to_D = [0.05, 0.95, 2]  #Oxidizer/(Oxidizer + Diluent)