def execute(inputModelFiles, **kwargs): try: wd = kwargs['wd'] except KeyError: wd = os.getcwd() transport = kwargs['transport'] outputChemkinFile = os.path.join(wd, 'chem.inp') outputSpeciesDictionary = os.path.join(wd, 'species_dictionary.txt') outputTransportFile = os.path.join(wd, 'tran.dat') if transport else None # Load the models to merge models = [] for chemkin, speciesPath, transportPath in inputModelFiles: print 'Loading model #{0:d}...'.format(len(models) + 1) model = ReactionModel() model.species, model.reactions = loadChemkinFile( chemkin, speciesPath, transportPath=transportPath) models.append(model) finalModel = ReactionModel() for i, model in enumerate(models): print 'Ignoring common species and reactions from model #{0:d}...'.format( i + 1) Nspec0 = len(finalModel.species) Nrxn0 = len(finalModel.reactions) finalModel = finalModel.merge(model) Nspec = len(finalModel.species) Nrxn = len(finalModel.reactions) print 'Added {1:d} out of {2:d} ({3:.1f}%) unique species from model #{0:d}.'.format( i + 1, Nspec - Nspec0, len(model.species), (Nspec - Nspec0) * 100. / len(model.species)) print 'Added {1:d} out of {2:d} ({3:.1f}%) unique reactions from model #{0:d}.'.format( i + 1, Nrxn - Nrxn0, len(model.reactions), (Nrxn - Nrxn0) * 100. / len(model.reactions)) print 'The merged model has {0:d} species and {1:d} reactions'.format( len(finalModel.species), len(finalModel.reactions)) # Save the merged model to disk saveChemkinFile(outputChemkinFile, finalModel.species, finalModel.reactions) saveSpeciesDictionary(outputSpeciesDictionary, finalModel.species) if transport: saveTransportFile(outputTransportFile, finalModel.species) print 'Merged Chemkin file saved to {0}'.format(outputChemkinFile) print 'Merged species dictionary saved to {0}'.format( outputSpeciesDictionary) if transport: print 'Merged transport file saved to {0}'.format(outputTransportFile)
def combine_models(models): """ Takes in a list of ReactionModels and and merges them into a single ReactionModel Reindexes species with the same label and index """ final_model = ReactionModel() for i, model in enumerate(models): print('Ignoring common species and reactions from model #{0:d}...'. format(i + 1)) nspec0 = len(final_model.species) nrxn0 = len(final_model.reactions) final_model = final_model.merge(model) nspec = len(final_model.species) nrxn = len(final_model.reactions) if len(model.species) > 0: print( 'Added {1:d} out of {2:d} ({3:.1f}%) unique species from model ' '#{0:d}.'.format(i + 1, nspec - nspec0, len(model.species), (nspec - nspec0) * 100. / len(model.species))) else: print('Added {1:d} out of {2:d} unique species from model ' '#{0:d}.'.format(i + 1, nspec - nspec0, len(model.species))) if len(model.reactions) > 0: print( 'Added {1:d} out of {2:d} ({3:.1f}%) unique reactions from model ' '#{0:d}.'.format(i + 1, nrxn - nrxn0, len(model.reactions), (nrxn - nrxn0) * 100. / len(model.reactions))) else: print('Added {1:d} out of {2:d} unique reactions from model ' '#{0:d}.'.format(i + 1, nrxn - nrxn0, len(model.reactions))) print('The merged model has {0:d} species and {1:d} reactions' ''.format(len(final_model.species), len(final_model.reactions))) # ensure no species with same name and index label_index_dict = {} for s in final_model.species: if s.label not in label_index_dict: label_index_dict[s.label] = [s.index] else: if s.index in label_index_dict[s.label]: # obtained a duplicate s.index = max(label_index_dict[s.label]) + 1 print("Reindexed {0} due to dublicate labels and index".format( s.label)) label_index_dict[s.label].append(s.index) return final_model
def execute(inputModelFiles, **kwargs): try: wd = kwargs['wd'] except KeyError: wd = os.getcwd() transport = kwargs['transport'] outputChemkinFile = os.path.join(wd, 'chem.inp') outputSpeciesDictionary = os.path.join(wd, 'species_dictionary.txt') outputTransportFile = os.path.join(wd, 'tran.dat') if transport else None # Load the models to merge models = [] for chemkin, speciesPath, transportPath in inputModelFiles: print 'Loading model #{0:d}...'.format(len(models)+1) model = ReactionModel() model.species, model.reactions = loadChemkinFile(chemkin, speciesPath, transportPath=transportPath) models.append(model) finalModel = ReactionModel() for i, model in enumerate(models): print 'Ignoring common species and reactions from model #{0:d}...'.format(i+1) Nspec0 = len(finalModel.species) Nrxn0 = len(finalModel.reactions) finalModel = finalModel.merge(model) Nspec = len(finalModel.species) Nrxn = len(finalModel.reactions) print 'Added {1:d} out of {2:d} ({3:.1f}%) unique species from model #{0:d}.'.format(i+1, Nspec - Nspec0, len(model.species), (Nspec - Nspec0) * 100. / len(model.species)) print 'Added {1:d} out of {2:d} ({3:.1f}%) unique reactions from model #{0:d}.'.format(i+1, Nrxn - Nrxn0, len(model.reactions), (Nrxn - Nrxn0) * 100. / len(model.reactions)) print 'The merged model has {0:d} species and {1:d} reactions'.format(len(finalModel.species), len(finalModel.reactions)) # Save the merged model to disk saveChemkinFile(outputChemkinFile, finalModel.species, finalModel.reactions) saveSpeciesDictionary(outputSpeciesDictionary, finalModel.species) if transport: saveTransportFile(outputTransportFile, finalModel.species) print 'Merged Chemkin file saved to {0}'.format(outputChemkinFile) print 'Merged species dictionary saved to {0}'.format(outputSpeciesDictionary) if transport: print 'Merged transport file saved to {0}'.format(outputTransportFile)
def combine_models(models): """ Takes in a list of ReactionModels and and merges them into a single ReactionModel Reindexes species with the same label and index """ final_model = ReactionModel() for i, model in enumerate(models): print('Ignoring common species and reactions from model #{0:d}...'. format(i + 1)) nspec0 = len(final_model.species) nrxn0 = len(final_model.reactions) final_model = final_model.merge(model) nspec = len(final_model.species) nrxn = len(final_model.reactions) if len(model.species) > 0: print( 'Added {1:d} out of {2:d} ({3:.1f}%) unique species from model ' '#{0:d}.'.format(i + 1, nspec - nspec0, len(model.species), (nspec - nspec0) * 100. / len(model.species))) else: print('Added {1:d} out of {2:d} unique species from model ' '#{0:d}.'.format(i + 1, nspec - nspec0, len(model.species))) if len(model.reactions) > 0: print( 'Added {1:d} out of {2:d} ({3:.1f}%) unique reactions from model ' '#{0:d}.'.format(i + 1, nrxn - nrxn0, len(model.reactions), (nrxn - nrxn0) * 100. / len(model.reactions))) else: print('Added {1:d} out of {2:d} unique reactions from model ' '#{0:d}.'.format(i + 1, nrxn - nrxn0, len(model.reactions))) print('The merged model has {0:d} species and {1:d} reactions' ''.format(len(final_model.species), len(final_model.reactions))) # reindex the unique species (to avoid name conflicts on save) speciesIndex = 0 for spec in final_model.species: if spec.label not in ['Ar', 'N2', 'Ne', 'He']: spec.index = speciesIndex + 1 speciesIndex += 1 return final_model
outputTransportFile = 'tran.dat' if transport else None # Load the models to merge models = [] for chemkin, speciesPath, transportPath in inputModelFiles: print 'Loading model #{0:d}...'.format(len(models)+1) model = ReactionModel() model.species, model.reactions = loadChemkinFile(chemkin, speciesPath, transportPath=transportPath) models.append(model) finalModel = ReactionModel() for i, model in enumerate(models): print 'Ignoring common species and reactions from model #{0:d}...'.format(i+1) Nspec0 = len(finalModel.species) Nrxn0 = len(finalModel.reactions) finalModel = finalModel.merge(model) Nspec = len(finalModel.species) Nrxn = len(finalModel.reactions) print 'Added {1:d} out of {2:d} ({3:.1f}%) unique species from model #{0:d}.'.format(i+1, Nspec - Nspec0, len(model.species), (Nspec - Nspec0) * 100. / len(model.species)) print 'Added {1:d} out of {2:d} ({3:.1f}%) unique reactions from model #{0:d}.'.format(i+1, Nrxn - Nrxn0, len(model.reactions), (Nrxn - Nrxn0) * 100. / len(model.reactions)) print 'The merged model has {0:d} species and {1:d} reactions'.format(len(finalModel.species), len(finalModel.reactions)) # Save the merged model to disk saveChemkinFile(outputChemkinFile, finalModel.species, finalModel.reactions) saveSpeciesDictionary(outputSpeciesDictionary, finalModel.species) if transport: saveTransportFile(outputTransportFile, finalModel.species) print 'Merged Chemkin file saved to {0}'.format(outputChemkinFile) print 'Merged species dictionary saved to {0}'.format(outputSpeciesDictionary)