コード例 #1
0
ファイル: uncertainty.py プロジェクト: mbprend/RMG-Py
    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)
コード例 #2
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()