示例#1
0
    def chemkin_format(self, gas=[], path=[]):
        if not gas:
            gas = self.gas
        if not path:
            path = self.path['Mech.ck']

        soln2ck.write(gas, path, self.path['Cantera_Mech'])
示例#2
0
文件: home.py 项目: arghdos/pyMARS
def readin(args='none', **argv):
    """Main function for pyMARS

    Parameters
    ----------
    file:
        Input mechanism file (ex. file='gri30.cti')
    species:
        Species to eliminate (ex. species='H, OH')
    thermo:
        Thermo data file if Chemkin format (ex. thermo= 'thermo.dat')
    transport:
        Transport data file if Chemkin format
    plot:
        plot ignition curve (ex. plot='y')
    points:
        print ignition point and sample range (ex. points='y')
    writecsv:
        write data to csv (ex. writecsv='y')
    writehdf5:
        write data to hdf5 (ex. writehdf5='y')
    write_ai_times:
        write autoignition times for each inital condition
    run_drg:
        Run DRG model reduction
    run_drgep:
	Run drgep model.
    error:
	Maximum ammount of error allowed. 
    keepers: list of strings
	The string names of the species that should be kept in the model no matter what.
    targets: list of strings
	The string names of the species that should be used as target species.

    Returns
    -------
        Converted mechanism file
        Trimmed Solution Object
        Trimmed Mechanism file

    Examples
    --------
    readin(file='gri30.cti', plot='y', species='OH, H')
    """
    class args():

        #package from terminal use case
        if args is not 'none':
            plot = args.plot
            points = args.points
            writecsv = True
            writehdf5 = True
            data_file = args.file
            thermo_file = args.thermo
            transport_file = args.transport
            run_drg = args.run_drg
            conditions_file = args.conditions
            convert = args.convert
            error = args.error
            run_drgep = args.run_drgep
            write_ai_times = args.write_ai_times
            target = 0
            if args.species is None:
                keepers = []
            else:
                keepers = [str(item) for item in args.species.split(',')]
                #strip spaces
                for i, sp in enumerate(keepers):
                    keepers[i] = sp.strip()
            if args.target is None:
                target = []
            else:
                target = [str(item) for item in args.target.split(',')]
                #strip spaces
                for i, sp in enumerate(target):
                    target[i] = sp.strip()

    file_extension = os.path.splitext(args.data_file)[1]

    if file_extension == ".cti" or file_extension == ".xml":  #If the file is a Cantera file.
        print("\nThis is a Cantera xml or cti file\n")
        solution_object = ct.Solution(args.data_file)

        #runs simulation once with additional features on
        if args.plot is True or args.writecsv is True or args.points is True or args.writehdf5 is True or args.write_ai_times is True:
            if os.path.exists('mass_fractions.hdf5'):
                os.system('rm mass_fractions.hdf5')
            if args.write_ai_times is True:
                if os.path.exists('autoignition_times.txt'):
                    os.system('rm autoignition_times.txt')
            print 'running simulation\n'
            sim_result = autoignition_loop_control(solution_object, args, True)

        if args.run_drg is True:
            run_drg(args, solution_object)

        if args.convert is True:
            soln2ck.write(solution_object)

        if args.run_drgep is True:  #If the user wants to run drgep and specifies it as a command line argument.
            run_drgep(args, solution_object)

    elif file_extension == ".inp" or file_extension == ".dat" or file_extension == ".txt":
        print("\n\nThis is a Chemkin file")
        #convert file to cti
        converted_file_name = convert(args.data_file, args.thermo_file,
                                      args.transport_file)

    else:
        print("\n\nFile type not supported")
示例#3
0
def readin(args='none', **argv):
    """Main function for pyMARS

    Parameters
    ----------
    file:
        Input mechanism file (ex. file='gri30.cti')
    species:
        Species to eliminate (ex. species='H, OH')
    thermo:
        Thermo data file if Chemkin format (ex. thermo= 'thermo.dat')
    transport:
        Transport data file if Chemkin format
    plot:
        plot ignition curve (ex. plot='y')
    points:
        print ignition point and sample range (ex. points='y')
    writecsv:
        write data to csv (ex. writecsv='y')
    writehdf5:
        write data to hdf5 (ex. writehdf5='y')
    run_drg:
        Run DRG model reduction

    Returns
    -------
        Converted mechanism file
        Trimmed Solution Object
        Trimmed Mechanism file

    Examples
    --------
    readin(file='gri30.cti', plot='y', species='OH, H')
    """
    class args():

        #package from terminal use case
        if args is not 'none':
            plot = args.plot
            points = args.points
            writecsv = args.writecsv
            writehdf5 = args.writehdf5
            data_file = args.file
            thermo_file = args.thermo
            transport_file = args.transport
            run_drg = args.run_drg
            threshold_values = args.thresholds
            conditions_file = args.conditions
            convert = args.convert
            if args.species is None:
                exclusion_list = []
            else:
                exclusion_list = [
                    str(item) for item in args.species.split(',')
                ]
                #strip spaces
                for i, sp in enumerate(exclusion_list):
                    exclusion_list[i] = sp.strip()

    file_extension = os.path.splitext(args.data_file)[1]

    if file_extension == ".cti" or file_extension == ".xml":
        print("\nThis is an Cantera xml or cti file\n")
        solution_object = ct.Solution(args.data_file)
        if args.plot is True or args.writecsv is True or args.points is True or args.writehdf5 is True:
            print 'running sim'
            sim_result = autoignition_loop_control(solution_object, args)
        if args.run_drg is True:
            new_solution_objects = drg_loop_control(solution_object, args)
            os.system('rm production_rates.hdf5')
            os.system('rm mass_fractions.hdf5')
            drg_trimmed_file = soln2cti.write(new_solution_objects[1])
            try:
                os.system('rm production_rates.hdf5')
            except Exception:
                pass
        if args.convert is True:
            soln2ck.write(solution_object)

    elif file_extension == ".inp" or file_extension == ".dat" or file_extension == ".txt":
        print("\n\nThis is a Chemkin file")
        #convert file to cti
        converted_file_name = convert(args.data_file, args.thermo_file,
                                      args.transport_file)

    else:
        print("\n\nFile type not supported")
示例#4
0
import soln2ck


mech_name = 'Mech_test.inp'

path = {'main': Path(sys.argv[0]).parents[0].resolve()}
path['mech'] = path['main'] / mech_name
path['yaml_mech'] = path['main'] / 'generated_mech.yaml'
path['out'] = path['main'] / 'mech.ck'


# Define a gas mixture at a high temperature that will undergo a reaction:
surfaces = ck2yaml.convert_mech(path['mech'], phase_name='gas', out_name=path['yaml_mech'], 
                quiet=False, permissive=True)

try:                                            # This test taken from ck2cti
    print('Validating mechanism...', end='')
    gas = ct.Solution(str(path['yaml_mech']))
    for surfname in surfaces:
        phase = ct.Interface(outName, surfname, [gas])
    print('PASSED.')
except RuntimeError as e:
    print('FAILED.')
    print(e)                

gas = ct.Solution(str(path['yaml_mech']))
print(gas.reaction(0).high_rate)
print(gas.reaction(0).low_rate)
soln2ck.write(gas, path['out'], path['yaml_mech'])
示例#5
0
                phase_name='gas', out_name=path['Cantera_Mech'], quiet=False, permissive=True)

try:                                            # This test taken from ck2yaml
    print('Validating mechanism...', end='')
    gas = ct.Solution(str(path['Cantera_Mech']))
    for surfname in surfaces:
        phase = ct.Interface(outName, surfname, [gas])
    print('PASSED.')
except RuntimeError as e:
    print('FAILED.')
    print(e)                



gas = ct.Solution(str(path['Cantera_Mech']))
soln2ck.write(gas, str(Path.cwd() / 'mech.ck'), path['Cantera_Mech'])
quit()
remove_elements = ['O', 'N']

remove_species = []
for remove_element in remove_elements:
    try:
        ele_num = gas.element_index(remove_element)
        for n in range(gas.n_species):
            if gas.n_atoms(n, ele_num) > 0.0:
                remove_species.append(gas.species_name(n))
    except:
        pass
    
# gas.n_atoms(0, element)
# remove_species = np.setdiff1d(gas.species_names, key_species)
示例#6
0
    if l < loss:
        loss = l
        best_reactions = reactions_new
        best_factors = factors
        best_taus = taus
        write_reactions.write_reactions(best_reactions,
                                        OUTPUT_REACTONS_FILE_NAME)
        write_reactions.write_factors(best_factors, OUTPUT_FACTORS_FILE_NAME)

        # use solution to save ck or cti file
        if OUTPUT_CK:
            gas = ct.Solution(thermo='IdealGas',
                              kinetics='GasKinetics',
                              species=species,
                              reactions=best_reactions)
            soln2ck.write(gas)
        if OUTPUT_CTI:
            gas = ct.Solution(thermo='IdealGas',
                              kinetics='GasKinetics',
                              species=species,
                              reactions=best_reactions)
            soln2cti.write(gas)

    t_iteration_1 = time.time()
    #print('Counter: %d, Loss: %.5f, Time cost: %.2f s' % (counter, loss, (t_iteration_1 - t_iteration_0)))
    num_space = 5 - len(str(counter))
    print('Counter:%s%d, Loss: %.5f, Time cost: %.2f s' %
          (num_space * ' ', counter, loss, (t_iteration_1 - t_iteration_0)))

    # end metrics
    if loss <= MIN_LOSS or counter >= MAX_GAUSS:
示例#7
0
def readin(args='none', **argv):
    """Main function for pyMARS

    Parameters
    ----------
    file:
        Input mechanism file (ex. file='gri30.cti')
    species:
        Species to eliminate (ex. species='H, OH')
    thermo:
        Thermo data file if Chemkin format (ex. thermo= 'thermo.dat')
    transport:
        Transport data file if Chemkin format
    plot:
        plot ignition curve (ex. plot='y')
    points:
        print ignition point and sample range (ex. points='y')
    writecsv:
        write data to csv (ex. writecsv='y')
    writehdf5:
        write data to hdf5 (ex. writehdf5='y')
    run_drg:
        Run DRG model reduction

    Returns
    -------
        Converted mechanism file
        Trimmed Solution Object
        Trimmed Mechanism file

    Examples
    --------
    readin(file='gri30.cti', plot='y', species='OH, H')
    """

    class args():

        #package from terminal use case
        if args is not 'none':
            plot = args.plot
            points = args.points
            writecsv = args.writecsv
            writehdf5 = args.writehdf5
            data_file= args.file
            thermo_file = args.thermo
            transport_file = args.transport
            run_drg = args.run_drg
            threshold_values = args.thresholds
            conditions_file = args.conditions
            convert = args.convert
            if args.species is None:
                exclusion_list = []
            else:
                exclusion_list = [str(item) for item in args.species.split(',')]
                #strip spaces
                for i, sp in enumerate(exclusion_list):
                    exclusion_list[i]=sp.strip()

    file_extension= os.path.splitext(args.data_file)[1]

    if file_extension == ".cti" or file_extension == ".xml":
        print("\nThis is an Cantera xml or cti file\n")
        solution_object = ct.Solution(args.data_file)
        if args.plot is True or args.writecsv is True or args.points is True or args.writehdf5 is True:
            print 'running sim'
            sim_result = autoignition_loop_control(solution_object, args)
        if args.run_drg is True:
            new_solution_objects = drg_loop_control(solution_object, args)
            os.system('rm production_rates.hdf5')
            os.system('rm mass_fractions.hdf5')
            drg_trimmed_file = soln2cti.write(new_solution_objects[1])
            try:
                os.system('rm production_rates.hdf5')
            except Exception:
                pass
        if args.convert is True:
            soln2ck.write(solution_object)


    elif file_extension == ".inp" or file_extension == ".dat" or file_extension == ".txt":
        print("\n\nThis is a Chemkin file")
        #convert file to cti
        converted_file_name = convert(args.data_file, args.thermo_file, args.transport_file)

    else:
        print("\n\nFile type not supported")