def __init__(self, title='', old_dir='', new_dir='', observables=None, expt_data=None, ck2cti=True): self.title = title self.new_dir = new_dir self.old_dir = old_dir self.conditions = None self.expt_data = expt_data if expt_data else [] self.observables = observables if observables else {} # Detect if the transport file exists old_transport_path = None if os.path.exists(os.path.join(old_dir, 'tran.dat')): old_transport_path = os.path.join(old_dir, 'tran.dat') new_transport_path = None if os.path.exists(os.path.join(new_dir, 'tran.dat')): new_transport_path = os.path.join(new_dir, 'tran.dat') # load the species and reactions from each model old_species_list, old_reaction_list = load_chemkin_file( os.path.join(old_dir, 'chem_annotated.inp'), os.path.join(old_dir, 'species_dictionary.txt'), old_transport_path) new_species_list, new_reaction_list = load_chemkin_file( os.path.join(new_dir, 'chem_annotated.inp'), os.path.join(new_dir, 'species_dictionary.txt'), new_transport_path) self.old_sim = Cantera(species_list=old_species_list, reaction_list=old_reaction_list, output_directory=old_dir) self.new_sim = Cantera(species_list=new_species_list, reaction_list=new_reaction_list, output_directory=new_dir) # load each chemkin file into the cantera model if not ck2cti: self.old_sim.load_model() self.new_sim.load_model() else: self.old_sim.load_chemkin_model(os.path.join( old_dir, 'chem_annotated.inp'), transport_file=old_transport_path, quiet=True) self.new_sim.load_chemkin_model(os.path.join( new_dir, 'chem_annotated.inp'), transport_file=new_transport_path, quiet=True)
def __init__(self, title='', oldDir='', newDir='', observables={}, exptData=[], ck2cti=True): self.title = title self.newDir = newDir self.oldDir = oldDir self.conditions = None self.exptData = exptData self.observables = observables # Detect if the transport file exists oldTransportPath = None if os.path.exists(os.path.join(oldDir, 'tran.dat')): oldTransportPath = os.path.join(oldDir, 'tran.dat') newTransportPath = None if os.path.exists(os.path.join(newDir, 'tran.dat')): newTransportPath = os.path.join(newDir, 'tran.dat') # load the species and reactions from each model oldSpeciesList, oldReactionList = loadChemkinFile( os.path.join(oldDir, 'chem_annotated.inp'), os.path.join(oldDir, 'species_dictionary.txt'), oldTransportPath) newSpeciesList, newReactionList = loadChemkinFile( os.path.join(newDir, 'chem_annotated.inp'), os.path.join(newDir, 'species_dictionary.txt'), newTransportPath) self.oldSim = Cantera(speciesList=oldSpeciesList, reactionList=oldReactionList, outputDirectory=oldDir) self.newSim = Cantera(speciesList=newSpeciesList, reactionList=newReactionList, outputDirectory=newDir) # load each chemkin file into the cantera model if not ck2cti: self.oldSim.loadModel() self.newSim.loadModel() else: self.oldSim.loadChemkinModel(os.path.join(oldDir, 'chem_annotated.inp'), transportFile=oldTransportPath, quiet=True) self.newSim.loadChemkinModel(os.path.join(newDir, 'chem_annotated.inp'), transportFile=newTransportPath, quiet=True)
def setUp(self): """ A function run before each unit test in this class. """ from rmgpy.chemkin import load_chemkin_file folder = os.path.join(os.path.dirname(rmgpy.__file__), 'tools/data/various_kinetics') chemkin_path = os.path.join(folder, 'chem_annotated.inp') dictionary_path = os.path.join(folder, 'species_dictionary.txt') transport_path = os.path.join(folder, 'tran.dat') species, reactions = load_chemkin_file(chemkin_path, dictionary_path, transport_path) self.rmg_ctSpecies = [ spec.to_cantera(use_chemkin_identifier=True) for spec in species ] self.rmg_ctReactions = [] for rxn in reactions: converted_reactions = rxn.to_cantera(species, use_chemkin_identifier=True) if isinstance(converted_reactions, list): self.rmg_ctReactions.extend(converted_reactions) else: self.rmg_ctReactions.append(converted_reactions) job = Cantera() job.load_chemkin_model(chemkin_path, transport_file=transport_path, quiet=True) self.ctSpecies = job.model.species() self.ctReactions = job.model.reactions()
def run_cantera_job( smiles_dictionary, specie_initial_mol_frac, final_time, temp_initial, initial_p, chemkin_file='', species_dictionary_file='', transport_file=None, reactor_type='IdealGasConstPressureTemperatureReactor', time_units='s', temp_units='K', p_units='atm', species_list=None, reaction_list=None, ): """General function for running Cantera jobs from chemkin files with common defaults =========================== ======================================================================= Input (Required) Description =========================== ======================================================================= smiles_dictionary A dictionary with user names as keys and SMILES strings as values specie_initial_mol_frac A dictionary with user specie names as keys and mol fractions as values final_time Termination time for the simulation temp_initial Initial temperature for the simulation initial_p Initial pressure for the simulation =========================== ======================================================================= Inputs with Defaults Description =========================== ======================================================================= chemkin_file String relative path of the chem.inp or chem_annotated.inp file species_dictionary_file String relative path of species_dictionary file reactor_type String with Cantera reactor type time_units Default is s (min and h are also supported) temp_units Default is K (C is also supported) p_units Default is atm (bar and Pa are also supported) =========================== ======================================================================= Optional Inputs Description =========================== ======================================================================= transport_file String relative path of trans.dat file species_list Output from loadChemkinFile for faster simulation (otherwise generated) reaction_list Output from loadChemkinFile for faster simulation (otherwise generated) =================================================================================================== =========================== ======================================================================= Output Description =========================== ======================================================================= all_data Cantera Simulation Data Object [time, [temp, pressure, spc1, spc2,..]] =================================================================================================== """ logging.info( 'Running a cantera job using the chemkin file {}'.format(chemkin_file)) logging.debug('loading chemkin and species dictionary file') cwd = os.getcwd() if chemkin_file == '': chemkin_file = os.path.join(cwd, 'chem_annotated.inp') if species_dictionary_file == '': species_dictionary_file = os.path.join(cwd, 'species_dictionary.txt') user_species_dictionary = create_species_from_smiles(smiles_dictionary) specie_initial_mol_frac = set_species_mol_fractions( specie_initial_mol_frac, user_species_dictionary) if (not species_list) or (not reaction_list): (species_list, reaction_list) = loadChemkinFile(chemkin_file, species_dictionary_file) name_dictionary = getRMGSpeciesFromUserSpecies( user_species_dictionary.values(), species_list) mol_fractions = {} for (user_name, chemkin_name) in name_dictionary.iteritems(): try: mol_fractions[chemkin_name] = specie_initial_mol_frac[user_name] except KeyError: logging.debug( '{} initial mol fractions set to 0'.format(user_name)) if temp_units == 'C': temp_initial += 273.0 temp_initial = ([temp_initial], 'K') initial_p = ([initial_p], p_units) job = Cantera(speciesList=species_list, reactionList=reaction_list, outputDirectory='') job.loadChemkinModel(chemkin_file, transportFile=transport_file) job.generateConditions([reactor_type], ([final_time], time_units), [mol_fractions], temp_initial, initial_p) logging.debug('Starting Cantera Simulation') all_data = job.simulate() all_data = all_data[0] logging.info('Cantera Simulation Complete') logging.debug('Setting labels to user defined species labels') species_index = {} for i in range(len(species_list)): species_index[species_list[i]] = i + 2 user_index = {} for (user_name, specie) in user_species_dictionary.iteritems(): try: user_index[species_index[name_dictionary[specie]]] = user_name except KeyError: logging.info('{0} is not in the model for {1}'.format( user_name, chemkin_file)) for (indices, user_label) in user_index.iteritems(): try: all_data[1][indices].label = user_label except KeyError: pass return all_data