def plotSensitivity(outputDirectory, reactionSystemIndex, sensitiveSpeciesList, number=10, fileformat='.png'): """ A function for plotting the top reaction thermo sensitivities (the number is inputted as the variable `number`) in bar plot format. To be called after running a simulation on a particular reactionSystem. """ for species in sensitiveSpeciesList: csvFile = os.path.join( outputDirectory, 'solver', 'sensitivity_{0}_SPC_{1}.csv'.format( reactionSystemIndex + 1, species.index ) ) reactionPlotFile = os.path.join( outputDirectory, 'solver', 'sensitivity_{0}_SPC_{1}_reactions'.format( reactionSystemIndex + 1, species.index ) + fileformat ) thermoPlotFile = os.path.join( outputDirectory, 'solver', 'sensitivity_{0}_SPC_{1}_thermo'.format( reactionSystemIndex + 1, species.index ) + fileformat ) ReactionSensitivityPlot(csvFile=csvFile, numReactions=number).barplot(reactionPlotFile) ThermoSensitivityPlot(csvFile=csvFile, numSpecies=number).barplot(thermoPlotFile)
def plotSensitivity(outputDirectory, reactionSystemIndex, sensitiveSpeciesList): """ A function for plotting the top 10 reaction and top 10 thermo sensitivities in bar plot format. To be called after running a simulation on a particular reactionSystem. """ for species in sensitiveSpeciesList: csvFile = os.path.join( outputDirectory, 'solver', 'sensitivity_{0}_SPC_{1}.csv'.format(reactionSystemIndex + 1, species.index)) reactionPlotFile = os.path.join( outputDirectory, 'solver', 'sensitivity_{0}_SPC_{1}_reactions.png'.format( reactionSystemIndex + 1, species.index)) thermoPlotFile = os.path.join( outputDirectory, 'solver', 'sensitivity_{0}_SPC_{1}_thermo.png'.format( reactionSystemIndex + 1, species.index)) ReactionSensitivityPlot(csvFile=csvFile, numReactions=10).barplot(reactionPlotFile) ThermoSensitivityPlot(csvFile=csvFile, numSpecies=10).barplot(thermoPlotFile)
def plot(self, data, topSpecies=10, topSensitiveReactions=10): """ Plots data from the simulations from this cantera job. Takes data in the format of a list of tuples containing (time, [list of temperature, pressure, and species data]) 3 plots will be created for each condition: - T vs. time - P vs. time - Maximum species mole fractions (the number of species plotted is based on the `topSpecies` argument) Reaction sensitivity plots will also be plotted automatically if there were sensitivities evaluated. The number of reactions to be plotted is defined by the `topSensitiveReactions` argument. """ numCtReactions = len(self.model.reactions()) for i, conditionData in enumerate(data): time, dataList, reactionSensitivityData = conditionData # In RMG, any species with an index of -1 is an inert and should not be plotted inertList = [species for species in self.speciesList if species.index == -1 ] TData = dataList[0] PData = dataList[1] speciesData = [data for data in dataList if data.species not in inertList] # plot GenericPlot(xVar=time, yVar=TData).plot(os.path.join(self.outputDirectory,'{0}_temperature.png'.format(i+1))) GenericPlot(xVar=time, yVar=PData).plot(os.path.join(self.outputDirectory,'{0}_pressure.png'.format(i+1))) SimulationPlot(xVar=time, yVar=speciesData, numSpecies=topSpecies, ylabel='Mole Fraction').plot(os.path.join(self.outputDirectory,'{0}_mole_fractions.png'.format(i+1))) for j, species in enumerate(self.sensitiveSpecies): ReactionSensitivityPlot(xVar=time, yVar=reactionSensitivityData[j*numCtReactions:(j+1)*numCtReactions], numReactions=topSensitiveReactions).barplot(os.path.join(self.outputDirectory,'{0}_{1}_sensitivity.png'.format(i+1,species.toChemkin())))
def local_analysis(self, sensitive_species, reaction_system_index=0, correlated=False, number=10, fileformat='.png'): """ Conduct local uncertainty analysis on the reaction model. sensitive_species is a list of sensitive Species objects number is the number of highest contributing uncertain parameters desired to be plotted fileformat can be either .png, .pdf, or .svg """ output = {} for sens_species in sensitive_species: csvfile_path = os.path.join( self.output_directory, 'solver', 'sensitivity_{0}_SPC_{1}.csv'.format(reaction_system_index + 1, sens_species.index)) time, data_list = parse_csv_data(csvfile_path) # Assign uncertainties thermo_data_list = [] reaction_data_list = [] for data in data_list: if data.species: for species in self.species_list: if species.to_chemkin() == data.species: index = self.species_list.index(species) break else: raise Exception( 'Chemkin name {} of species in the CSV file does not match anything in the ' 'species list.'.format(data.species)) data.uncertainty = self.thermo_input_uncertainties[index] thermo_data_list.append(data) if data.reaction: rxn_index = int(data.index) - 1 data.uncertainty = self.kinetic_input_uncertainties[ rxn_index] reaction_data_list.append(data) if correlated: correlated_thermo_data = {} correlated_reaction_data = {} for data in thermo_data_list: for label, dpG in data.uncertainty.items(): if label in correlated_thermo_data: # Unpack the labels and partial uncertainties correlated_thermo_data[label].data[-1] += data.data[ -1] * dpG # Multiply the sensitivity with the partial uncertainty else: correlated_thermo_data[label] = GenericData( data=[data.data[-1] * dpG], uncertainty=1, label=label, species='dummy') for data in reaction_data_list: for label, dplnk in data.uncertainty.items(): if label in correlated_reaction_data: correlated_reaction_data[label].data[ -1] += data.data[-1] * dplnk else: correlated_reaction_data[label] = GenericData( data=[data.data[-1] * dplnk], uncertainty=1, label=label, reaction='dummy') thermo_data_list = list(correlated_thermo_data.values()) reaction_data_list = list(correlated_reaction_data.values()) # Compute total variance total_variance = 0.0 for data in thermo_data_list: total_variance += (data.data[-1] * data.uncertainty)**2 for data in reaction_data_list: total_variance += (data.data[-1] * data.uncertainty)**2 if not correlated: # Add the reaction index to the data label of the reaction uncertainties # data.index stores the physical index of the reaction + 1, so we convert it to the RMG index here for data in reaction_data_list: data.label = 'k' + str(self.reaction_list[ data.index - 1].index) + ': ' + data.label.split()[-1] if correlated: folder = os.path.join(self.output_directory, 'correlated') else: folder = os.path.join(self.output_directory, 'uncorrelated') if not os.path.exists(folder): try: os.makedirs(folder) except OSError as e: raise OSError( 'Uncertainty output directory could not be created: {0!s}' .format(e)) r_path = os.path.join( folder, 'kineticsLocalUncertainty_{0}'.format( sens_species.to_chemkin()) + fileformat) t_path = os.path.join( folder, 'thermoLocalUncertainty_{0}'.format( sens_species.to_chemkin()) + fileformat) reaction_uncertainty = ReactionSensitivityPlot( x_var=time, y_var=reaction_data_list, num_reactions=number).uncertainty_plot(total_variance, filename=r_path) thermo_uncertainty = ThermoSensitivityPlot( x_var=time, y_var=thermo_data_list, num_species=number).uncertainty_plot(total_variance, filename=t_path) output[sens_species] = (total_variance, reaction_uncertainty, thermo_uncertainty) return output
def plot(self, data, top_species=10, top_sensitive_reactions=10, top_sensitive_species=10): """ Plots data from the simulations from this cantera job. Takes data in the format of a list of tuples containing (time, [list of temperature, pressure, and species data]) 3 plots will be created for each condition: - T vs. time - P vs. time - Maximum species mole fractions (the number of species plotted is based on the `top_species` argument) Reaction sensitivity plots will also be plotted automatically if there were sensitivities evaluated. The number of reactions to be plotted is defined by the `top_sensitive_reactions` argument. """ num_ct_reactions = len(self.model.reactions()) num_ct_species = len(self.model.species()) for i, condition_data in enumerate(data): time, data_list, reaction_sensitivity_data, thermodynamic_sensitivity_data = condition_data # In RMG, any species with an index of -1 is an inert and should not be plotted inert_list = [ species for species in self.species_list if species.index == -1 ] T_data = data_list[0] P_data = data_list[1] species_data = [ data for data in data_list if data.species not in inert_list ] # plot GenericPlot(x_var=time, y_var=T_data).plot( os.path.join(self.output_directory, '{0}_temperature.png'.format(i + 1))) GenericPlot(x_var=time, y_var=P_data).plot( os.path.join(self.output_directory, '{0}_pressure.png'.format(i + 1))) SimulationPlot(x_var=time, y_var=species_data, num_species=top_species, ylabel='Mole Fraction').plot( os.path.join( self.output_directory, '{0}_mole_fractions.png'.format(i + 1))) for j, species in enumerate(self.sensitive_species): ReactionSensitivityPlot( x_var=time, y_var=reaction_sensitivity_data[j * num_ct_reactions:(j + 1) * num_ct_reactions], num_reactions=top_sensitive_reactions).barplot( os.path.join( self.output_directory, '{0}_{1}_reaction_sensitivity.png'.format( i + 1, species.to_chemkin()))) if self.thermo_SA: ThermoSensitivityPlot( x_var=time, y_var=thermodynamic_sensitivity_data[j * num_ct_species: (j + 1) * num_ct_species] * 4184000, xlabel='dln(c)/d(H_i) [(kcal/mol)^-1]', num_species=top_sensitive_species).barplot( os.path.join( self.output_directory, '{0}_{1}_thermo_sensitivity.png'.format( i + 1, species.to_chemkin())))