def saveChemkinFile(path, species, reactions, verbose=True): from rmgpy.chemkin import writeKineticsEntry s = 'REACTIONS KCAL/MOLE MOLES\n\n' for rxn in reactions: s += writeKineticsEntry(rxn, speciesList=species, verbose=verbose) s += '\n' s += 'END\n\n' with open(path, 'w') as f: f.write(s)
def saveChemkinFile(path, species, reactions, verbose = True): from rmgpy.chemkin import writeKineticsEntry s ='REACTIONS KCAL/MOLE MOLES\n\n' for rxn in reactions: s +=writeKineticsEntry(rxn, speciesList=species, verbose=verbose) s +='\n' s +='END\n\n' with open(path, 'w') as f: f.write(s)
def main(chemkin): # Load Chemkin file reactionModel = CoreEdgeReactionModel() reactionModel.core.species, reactionModel.core.reactions = loadChemkinFile( chemkin) # Identify reactions to be removed reactionList = reactionModel.core.reactions duplicateReactionsToRemove = [] for index1 in range(len(reactionList)): reaction1 = reactionList[index1] for index2 in range(index1 + 1, len(reactionList)): reaction2 = reactionList[index2] # Check if the two reactions are identical if (reaction1.reactants == reaction2.reactants and reaction1.products == reaction2.products) or ( reaction1.reactants == reaction2.products and reaction1.products == reaction2.reactants): # Identify if exactly 1 of the reactions is a LibraryReaction if isinstance(reaction1, LibraryReaction) != isinstance( reaction2, LibraryReaction): # Mark the non-LibraryReaction to be removed if isinstance(reaction1, LibraryReaction): duplicateReactionsToRemove.append(reaction2) else: duplicateReactionsToRemove.append(reaction1) # Remove the identified reactions for reaction in duplicateReactionsToRemove: reactionList.remove(reaction) # Write new Chemkin file path = 'chem_annotated_new.inp' speciesList = reactionModel.core.species + reactionModel.outputSpeciesList rxnList = reactionModel.core.reactions + reactionModel.outputReactionList with open(chemkin, 'rb') as old, open(path, 'wb') as new: # Copy species and reactions blocks to new Chemkin file line = old.readline() while 'REACTIONS' not in line.upper(): new.write(line) line = old.readline() new.write('REACTIONS KCAL/MOLE MOLES\n\n') rmgpy.chemkin.__chemkin_reaction_count = 0 for rxn in rxnList: new.write( writeKineticsEntry(rxn, speciesList=speciesList, verbose=True)) new.write('\n') new.write('END\n\n') print "New Chemkin file contains {0} reactions.".format( rmgpy.chemkin.__chemkin_reaction_count)
def save(self, outputFile): logging.info( 'Saving pressure dependence results for {0} network...'.format( self.network.label)) f = open(outputFile, 'a') f_chemkin = open(os.path.join(os.path.dirname(outputFile), 'chem.inp'), 'a') Nreac = self.network.Nisom + self.network.Nreac Nprod = Nreac + self.network.Nprod Tlist = self.Tlist.value_si Plist = self.Plist.value_si Tcount = Tlist.shape[0] Pcount = Plist.shape[0] count = 0 printed_reactions = [] # list of rxns already printed for prod in range(Nprod): for reac in range(Nreac): if reac == prod: continue reaction = self.network.netReactions[count] count += 1 # make sure we aren't double counting any reactions if not any([reaction.isIsomorphic(other_rxn,checkOnlyLabel=True) \ for other_rxn in printed_reactions]): duplicate = False # add reaction to printed reaction printed_reactions.append(reaction) else: # comment out the extra reverse reaction duplicate = True # write chemkin output. string = writeKineticsEntry(reaction, speciesList=None, verbose=False, commented=duplicate) f_chemkin.write('{0}\n'.format(string)) # write to 'output.py' kdata = self.K[:, :, prod, reac].copy() order = len(reaction.reactants) kdata *= 1e6**(order - 1) kunits = { 1: 's^-1', 2: 'cm^3/(mol*s)', 3: 'cm^6/(mol^2*s)' }[order] f.write('# =========== ') f.write('=========== ' * Pcount) f.write('\n') f.write('# T \ P ') f.write(' '.join(['{0:11.3e}'.format(P * 1e-5) for P in Plist])) f.write('\n') f.write('# =========== ') f.write('=========== ' * Pcount) f.write('\n') for t in range(Tcount): f.write('# {0:11g}'.format(Tlist[t])) for p in range(Pcount): f.write(' {0:11.3e}'.format(kdata[t, p])) f.write('\n') f.write('# =========== ') f.write('=========== ' * Pcount) f.write('\n') string = 'pdepreaction(reactants={0!r}, products={1!r}, kinetics={2!r})'.format( [reactant.label for reactant in reaction.reactants], [product.label for product in reaction.products], reaction.kinetics, ) pdep_function = '{0}\n\n'.format(prettify(string)) if duplicate: # add comments to the start of the string pdep_function = '# ' + pdep_function.replace( '\n', '\n# ') f.write(pdep_function) f.close() f_chemkin.close()
def save(self, outputFile): logging.info('Saving pressure dependence results for {0} network...'.format(self.network.label)) f = open(outputFile, 'a') f_chemkin = open(os.path.join(os.path.dirname(outputFile), 'chem.inp'), 'a') Nreac = self.network.Nisom + self.network.Nreac Nprod = Nreac + self.network.Nprod Tlist = self.Tlist.value_si Plist = self.Plist.value_si Tcount = Tlist.shape[0] Pcount = Plist.shape[0] count = 0 printed_reactions = [] # list of rxns already printed for prod in range(Nprod): for reac in range(Nreac): if reac == prod: continue reaction = self.network.netReactions[count] count += 1 # make sure we aren't double counting any reactions if not any([reaction.isIsomorphic(other_rxn,checkOnlyLabel=True) \ for other_rxn in printed_reactions]): duplicate = False # add reaction to printed reaction printed_reactions.append(reaction) else: # comment out the extra reverse reaction duplicate = True # write chemkin output. string = writeKineticsEntry(reaction, speciesList=None, verbose=False, commented = duplicate) f_chemkin.write('{0}\n'.format(string)) # write to 'output.py' kdata = self.K[:,:,prod,reac].copy() order = len(reaction.reactants) kdata *= 1e6 ** (order-1) kunits = {1: 's^-1', 2: 'cm^3/(mol*s)', 3: 'cm^6/(mol^2*s)'}[order] f.write('# =========== ') f.write('=========== ' * Pcount) f.write('\n') f.write('# T \ P ') f.write(' '.join(['{0:11.3e}'.format(P*1e-5) for P in Plist])) f.write('\n') f.write('# =========== ') f.write('=========== ' * Pcount) f.write('\n') for t in range(Tcount): f.write('# {0:11g}'.format(Tlist[t])) for p in range(Pcount): f.write(' {0:11.3e}'.format(kdata[t,p])) f.write('\n') f.write('# =========== ') f.write('=========== ' * Pcount) f.write('\n') string = 'pdepreaction(reactants={0!r}, products={1!r}, kinetics={2!r})'.format( [reactant.label for reactant in reaction.reactants], [product.label for product in reaction.products], reaction.kinetics, ) pdep_function = '{0}\n\n'.format(prettify(string)) if duplicate: # add comments to the start of the string pdep_function = '# ' + pdep_function.replace('\n','\n# ') f.write(pdep_function) f.close() f_chemkin.close()
def save(self, outputFile): logging.info( 'Saving pressure dependence results for {0} network...'.format( self.network.label)) f = open(outputFile, 'a') Nreac = self.network.Nisom + self.network.Nreac Nprod = Nreac + self.network.Nprod Tlist = self.Tlist.value_si Plist = self.Plist.value_si Tcount = Tlist.shape[0] Pcount = Plist.shape[0] count = 0 for prod in range(Nprod): for reac in range(Nreac): if reac == prod: continue reaction = self.network.netReactions[count] count += 1 kdata = self.K[:, :, prod, reac].copy() order = len(reaction.reactants) kdata *= 1e6**(order - 1) kunits = { 1: 's^-1', 2: 'cm^3/(mol*s)', 3: 'cm^6/(mol^2*s)' }[order] f.write('# =========== ') f.write('=========== ' * Pcount) f.write('\n') f.write('# T \ P ') f.write(' '.join(['{0:11.3e}'.format(P * 1e-5) for P in Plist])) f.write('\n') f.write('# =========== ') f.write('=========== ' * Pcount) f.write('\n') for t in range(Tcount): f.write('# {0:11g}'.format(Tlist[t])) for p in range(Pcount): f.write(' {0:11.3e}'.format(kdata[t, p])) f.write('\n') f.write('# =========== ') f.write('=========== ' * Pcount) f.write('\n') string = 'pdepreaction(reactants={0!r}, products={1!r}, kinetics={2!r})'.format( [reactant.label for reactant in reaction.reactants], [product.label for product in reaction.products], reaction.kinetics, ) f.write('{0}\n\n'.format(prettify(string))) f.close() f = open(os.path.join(os.path.dirname(outputFile), 'chem.inp'), 'a') count = 0 for prod in range(Nprod): for reac in range(Nreac): if reac == prod: continue reaction = self.network.netReactions[count] count += 1 string = writeKineticsEntry(reaction, speciesList=None, verbose=False) f.write('{0}\n'.format(string)) f.close()
def save(self, outputFile): logging.info('Saving pressure dependence results for {0} network...'.format(self.network.label)) f = open(outputFile, 'a') Nreac = self.network.Nisom + self.network.Nreac Nprod = Nreac + self.network.Nprod Tlist = self.Tlist.value_si Plist = self.Plist.value_si Tcount = Tlist.shape[0] Pcount = Plist.shape[0] count = 0 for prod in range(Nprod): for reac in range(Nreac): if reac == prod: continue reaction = self.network.netReactions[count] count += 1 kdata = self.K[:,:,prod,reac].copy() order = len(reaction.reactants) kdata *= 1e6 ** (order-1) kunits = {1: 's^-1', 2: 'cm^3/(mol*s)', 3: 'cm^6/(mol^2*s)'}[order] f.write('# =========== ') f.write('=========== ' * Pcount) f.write('\n') f.write('# T \ P ') f.write(' '.join(['{0:11.3e}'.format(P*1e-5) for P in Plist])) f.write('\n') f.write('# =========== ') f.write('=========== ' * Pcount) f.write('\n') for t in range(Tcount): f.write('# {0:11g}'.format(Tlist[t])) for p in range(Pcount): f.write(' {0:11.3e}'.format(kdata[t,p])) f.write('\n') f.write('# =========== ') f.write('=========== ' * Pcount) f.write('\n') string = 'pdepreaction(reactants={0!r}, products={1!r}, kinetics={2!r})'.format( [reactant.label for reactant in reaction.reactants], [product.label for product in reaction.products], reaction.kinetics, ) f.write('{0}\n\n'.format(prettify(string))) f.close() f = open(os.path.join(os.path.dirname(outputFile), 'chem.inp'), 'a') count = 0 for prod in range(Nprod): for reac in range(Nreac): if reac == prod: continue reaction = self.network.netReactions[count] count += 1 string = writeKineticsEntry(reaction, speciesList=None, verbose=False) f.write('{0}\n'.format(string)) f.close()