Пример #1
0
def simulate(rmg):
    """
    Simulate the RMG job and run the sensitivity analysis if it is on, generating
    output csv files
    """
    util.makeOutputSubdirectory(rmg.outputDirectory, 'solver')

    for index, reactionSystem in enumerate(rmg.reactionSystems):

        if reactionSystem.sensitiveSpecies:
            logging.info(
                'Conducting simulation and sensitivity analysis of reaction system %s...'
                % (index + 1))

        else:
            logging.info('Conducting simulation of reaction system %s...' %
                         (index + 1))

        if rmg.saveSimulationProfiles:
            reactionSystem.attach(
                SimulationProfileWriter(rmg.outputDirectory, index,
                                        rmg.reactionModel.core.species))
            reactionSystem.attach(
                SimulationProfilePlotter(rmg.outputDirectory, index,
                                         rmg.reactionModel.core.species))
        else:
            worksheet = None

        sensWorksheet = []
        for spec in reactionSystem.sensitiveSpecies:
            csvfilePath = os.path.join(
                rmg.outputDirectory, 'solver',
                'sensitivity_{0}_SPC_{1}.csv'.format(index + 1, spec.index))
            sensWorksheet.append(csvfilePath)

        pdepNetworks = []
        for source, networks in rmg.reactionModel.networkDict.items():
            pdepNetworks.extend(networks)
        terminated, obj = reactionSystem.simulate(
            coreSpecies=rmg.reactionModel.core.species,
            coreReactions=rmg.reactionModel.core.reactions,
            edgeSpecies=rmg.reactionModel.edge.species,
            edgeReactions=rmg.reactionModel.edge.reactions,
            toleranceKeepInEdge=0,
            toleranceMoveToCore=1,
            toleranceInterruptSimulation=1,
            pdepNetworks=pdepNetworks,
            absoluteTolerance=rmg.absoluteTolerance,
            relativeTolerance=rmg.relativeTolerance,
            sensitivity=True if reactionSystem.sensitiveSpecies else False,
            sensitivityAbsoluteTolerance=rmg.sensitivityAbsoluteTolerance,
            sensitivityRelativeTolerance=rmg.sensitivityRelativeTolerance,
            sensWorksheet=sensWorksheet,
        )

        if reactionSystem.sensitiveSpecies:
            plotSensitivity(rmg.outputDirectory, index,
                            reactionSystem.sensitiveSpecies)
Пример #2
0
    def sensitivityAnalysis(self, initialMoleFractions, sensitiveSpecies, T, P, terminationTime, sensitivityThreshold=1e-3, number=10, fileformat='.png'):
        """
        Run sensitivity analysis using the RMG solver in a single ReactionSystem object
        
        initialMoleFractions is a dictionary with Species objects as keys and mole fraction initial conditions
        sensitiveSpecies is a list of sensitive Species objects
        number is the number of top species thermo or reaction kinetics desired to be plotted
        """
        
        
        from rmgpy.solver import SimpleReactor, TerminationTime
        from rmgpy.quantity import Quantity
        from rmgpy.tools.sensitivity import plotSensitivity
        from rmgpy.rmg.listener import SimulationProfileWriter, SimulationProfilePlotter

        T = Quantity(T)
        P = Quantity(P)
        termination=[TerminationTime(Quantity(terminationTime))]
                                     
        reactionSystem = SimpleReactor(T, P, initialMoleFractions, termination, sensitiveSpecies, sensitivityThreshold)
        
        # Create the csv worksheets for logging sensitivity
        util.makeOutputSubdirectory(self.outputDirectory, 'solver')
        sensWorksheet = []
        reactionSystemIndex = 0
        for spec in reactionSystem.sensitiveSpecies:
            csvfilePath = os.path.join(self.outputDirectory, 'solver', 'sensitivity_{0}_SPC_{1}.csv'.format(reactionSystemIndex+1, spec.index))
            sensWorksheet.append(csvfilePath)

        reactionSystem.attach(SimulationProfileWriter(
            self.outputDirectory, reactionSystemIndex, self.speciesList))  
        reactionSystem.attach(SimulationProfilePlotter(
            self.outputDirectory, reactionSystemIndex, self.speciesList))
        
        reactionSystem.simulate(
            coreSpecies = self.speciesList,
            coreReactions = self.reactionList,
            edgeSpecies = [],
            edgeReactions = [],
            toleranceKeepInEdge = 0,
            toleranceMoveToCore = 1,
            toleranceInterruptSimulation = 1,
            sensitivity = True,
            sensWorksheet = sensWorksheet,
        )
        
        
        plotSensitivity(self.outputDirectory, reactionSystemIndex, reactionSystem.sensitiveSpecies, number=number, fileformat=fileformat)
Пример #3
0
    def sensitivity_analysis(self,
                             initial_mole_fractions,
                             sensitive_species,
                             T,
                             P,
                             termination_time,
                             sensitivity_threshold=1e-3,
                             number=10,
                             fileformat='.png'):
        """
        Run sensitivity analysis using the RMG solver in a single ReactionSystem object
        
        initial_mole_fractions is a dictionary with Species objects as keys and mole fraction initial conditions
        sensitive_species is a list of sensitive Species objects
        number is the number of top species thermo or reaction kinetics desired to be plotted
        """

        from rmgpy.solver import SimpleReactor, TerminationTime
        from rmgpy.quantity import Quantity
        from rmgpy.rmg.listener import SimulationProfileWriter, SimulationProfilePlotter
        from rmgpy.rmg.settings import ModelSettings, SimulatorSettings
        T = Quantity(T)
        P = Quantity(P)
        termination = [TerminationTime(Quantity(termination_time))]

        reaction_system = SimpleReactor(
            T=T,
            P=P,
            initial_mole_fractions=initial_mole_fractions,
            termination=termination,
            sensitive_species=sensitive_species,
            sensitivity_threshold=sensitivity_threshold)

        # Create the csv worksheets for logging sensitivity
        util.make_output_subdirectory(self.output_directory, 'solver')
        sens_worksheet = []
        reaction_system_index = 0
        for spec in reaction_system.sensitive_species:
            csvfile_path = os.path.join(
                self.output_directory, 'solver',
                'sensitivity_{0}_SPC_{1}.csv'.format(reaction_system_index + 1,
                                                     spec.index))
            sens_worksheet.append(csvfile_path)

        reaction_system.attach(
            SimulationProfileWriter(self.output_directory,
                                    reaction_system_index, self.species_list))
        reaction_system.attach(
            SimulationProfilePlotter(self.output_directory,
                                     reaction_system_index, self.species_list))

        simulator_settings = SimulatorSettings()  # defaults

        model_settings = ModelSettings()  # defaults
        model_settings.tol_move_to_core = 0.1
        model_settings.tol_interrupt_simulation = 1.0
        model_settings.tol_keep_in_edge = 0.0

        reaction_system.simulate(
            core_species=self.species_list,
            core_reactions=self.reaction_list,
            edge_species=[],
            edge_reactions=[],
            surface_species=[],
            surface_reactions=[],
            model_settings=model_settings,
            simulator_settings=simulator_settings,
            sensitivity=True,
            sens_worksheet=sens_worksheet,
        )

        plot_sensitivity(self.output_directory,
                         reaction_system_index,
                         reaction_system.sensitive_species,
                         number=number,
                         fileformat=fileformat)
def simulate(rmg, diffusionLimited=True):
    """
    Simulate the RMG job and run the sensitivity analysis if it is on, generating
    output csv files
    diffusionLimited=True implies that if it is a liquid reactor diffusion limitations will be enforced
    otherwise they will not be in a liquid reactor
    """
    util.makeOutputSubdirectory(rmg.outputDirectory, 'solver')

    for index, reactionSystem in enumerate(rmg.reactionSystems):

        if reactionSystem.sensitiveSpecies:
            logging.info(
                'Conducting simulation and sensitivity analysis of reaction system %s...'
                % (index + 1))

        else:
            logging.info('Conducting simulation of reaction system %s...' %
                         (index + 1))

        if rmg.saveSimulationProfiles:
            reactionSystem.attach(
                SimulationProfileWriter(rmg.outputDirectory, index,
                                        rmg.reactionModel.core.species))
            reactionSystem.attach(
                SimulationProfilePlotter(rmg.outputDirectory, index,
                                         rmg.reactionModel.core.species))

        sensWorksheet = []
        for spec in reactionSystem.sensitiveSpecies:
            csvfilePath = os.path.join(
                rmg.outputDirectory, 'solver',
                'sensitivity_{0}_SPC_{1}.csv'.format(index + 1, spec.index))
            sensWorksheet.append(csvfilePath)

        pdepNetworks = []
        for source, networks in rmg.reactionModel.networkDict.items():
            pdepNetworks.extend(networks)

        modelSettings = ModelSettings(toleranceKeepInEdge=0,
                                      toleranceMoveToCore=1,
                                      toleranceInterruptSimulation=1)
        simulatorSettings = rmg.simulatorSettingsList[-1]

        if isinstance(reactionSystem, LiquidReactor):
            if diffusionLimited:
                rmg.loadDatabase()
                solventData = rmg.database.solvation.getSolventData(
                    rmg.solvent)
                diffusionLimiter.enable(solventData, rmg.database.solvation)

            # Store constant species indices
            if reactionSystem.constSPCNames is not None:
                reactionSystem.get_constSPCIndices(
                    rmg.reactionModel.core.species)

        reactionSystem.simulate(
            coreSpecies=rmg.reactionModel.core.species,
            coreReactions=rmg.reactionModel.core.reactions,
            edgeSpecies=rmg.reactionModel.edge.species,
            edgeReactions=rmg.reactionModel.edge.reactions,
            surfaceSpecies=[],
            surfaceReactions=[],
            pdepNetworks=pdepNetworks,
            sensitivity=True if reactionSystem.sensitiveSpecies else False,
            sensWorksheet=sensWorksheet,
            modelSettings=modelSettings,
            simulatorSettings=simulatorSettings,
        )

        if reactionSystem.sensitiveSpecies:
            plotSensitivity(rmg.outputDirectory, index,
                            reactionSystem.sensitiveSpecies)
Пример #5
0
def simulate(rmg, diffusion_limited=True):
    """
    Simulate the RMG job and run the sensitivity analysis if it is on, generating
    output csv files
    diffusion_limited=True implies that if it is a liquid reactor diffusion limitations will be enforced
    otherwise they will not be in a liquid reactor
    """
    util.make_output_subdirectory(rmg.output_directory, 'solver')

    for index, reaction_system in enumerate(rmg.reaction_systems):

        if reaction_system.sensitive_species:
            logging.info(
                'Conducting simulation and sensitivity analysis of reaction system %s...'
                % (index + 1))
            if reaction_system.sensitive_species == ['all']:
                reaction_system.sensitive_species = rmg.reaction_model.core.species

        else:
            logging.info('Conducting simulation of reaction system %s...' %
                         (index + 1))

        reaction_system.attach(
            SimulationProfileWriter(rmg.output_directory, index,
                                    rmg.reaction_model.core.species))
        reaction_system.attach(
            SimulationProfilePlotter(rmg.output_directory, index,
                                     rmg.reaction_model.core.species))

        sens_worksheet = []
        for spec in reaction_system.sensitive_species:
            csvfile_path = os.path.join(
                rmg.output_directory, 'solver',
                'sensitivity_{0}_SPC_{1}.csv'.format(index + 1, spec.index))
            sens_worksheet.append(csvfile_path)

        pdep_networks = []
        for source, networks in rmg.reaction_model.network_dict.items():
            pdep_networks.extend(networks)

        model_settings = ModelSettings(tol_keep_in_edge=0,
                                       tol_move_to_core=1,
                                       tol_interrupt_simulation=1)
        simulator_settings = rmg.simulator_settings_list[-1]

        if isinstance(reaction_system, LiquidReactor):
            if diffusion_limited:
                rmg.load_database()
                solvent_data = rmg.database.solvation.get_solvent_data(
                    rmg.solvent)
                diffusion_limiter.enable(solvent_data, rmg.database.solvation)

            # Store constant species indices
            if reaction_system.const_spc_names is not None:
                reaction_system.get_const_spc_indices(
                    rmg.reaction_model.core.species)
        elif rmg.uncertainty is not None:
            rmg.verbose_comments = True
            rmg.load_database()

        reaction_system.simulate(
            core_species=rmg.reaction_model.core.species,
            core_reactions=rmg.reaction_model.core.reactions,
            edge_species=rmg.reaction_model.edge.species,
            edge_reactions=rmg.reaction_model.edge.reactions,
            surface_species=[],
            surface_reactions=[],
            pdep_networks=pdep_networks,
            sensitivity=True if reaction_system.sensitive_species else False,
            sens_worksheet=sens_worksheet,
            model_settings=model_settings,
            simulator_settings=simulator_settings,
        )

        if reaction_system.sensitive_species:
            plot_sensitivity(rmg.output_directory, index,
                             reaction_system.sensitive_species)
            rmg.run_uncertainty_analysis()