def setAnalysisModule(self, module_name: str) -> None: module_load_success = self.ert().analysisConfig().selectModule( module_name) if not module_load_success: raise ErtRunError("Unable to load analysis module '%s'!" % module_name)
def runSimulations(self, arguments): phase_count = ERT.enkf_facade.get_number_of_iterations() + 1 self.setPhaseCount(phase_count) analysis_module = self.setAnalysisModule(arguments["analysis_module"]) target_case_format = arguments["target_case"] run_context = self.create_context(arguments, 0) self.ert().analysisConfig().getAnalysisIterConfig().setCaseFormat( target_case_format) self._runAndPostProcess(run_context) analysis_config = self.ert().analysisConfig() analysis_iter_config = analysis_config.getAnalysisIterConfig() num_retries_per_iteration = analysis_iter_config.getNumRetries() num_retries = 0 current_iter = 0 previous_ensemble_name = None while current_iter < ERT.enkf_facade.get_number_of_iterations( ) and num_retries < num_retries_per_iteration: pre_analysis_iter_num = analysis_module.getInt("ITER") self.analyzeStep(run_context) current_iter = analysis_module.getInt("ITER") analysis_success = current_iter > pre_analysis_iter_num if analysis_success: analysis_module_name = self.ert().analysisConfig( ).activeModuleName() previous_ensemble_name = dump_to_new_storage( reference=None if previous_ensemble_name is None else ( previous_ensemble_name, analysis_module_name)) run_context = self.create_context(arguments, current_iter, prior_context=run_context) self.ert().getEnkfFsManager().switchFileSystem( run_context.get_target_fs()) self._runAndPostProcess(run_context) num_retries = 0 else: run_context = self.create_context(arguments, current_iter, prior_context=run_context, rerun=True) self._runAndPostProcess(run_context) num_retries += 1 analysis_module_name = self.ert().analysisConfig().activeModuleName() previous_ensemble_name = dump_to_new_storage( reference=None if previous_ensemble_name is None else ( previous_ensemble_name, analysis_module_name)) if current_iter == (phase_count - 1): self.setPhase(phase_count, "Simulations completed.") else: raise ErtRunError( "Iterated Ensemble Smoother stopped: maximum number of iteration retries (%d retries) reached for iteration %d" % (num_retries_per_iteration, current_iter)) return run_context
def update(self, run_context: ErtRunContext, weight: float, ensemble_id: str) -> str: next_iteration = run_context.get_iter() + 1 phase_string = "Analyzing iteration: %d with weight %f" % ( next_iteration, weight, ) self.setPhase(self.currentPhase() + 1, phase_string, indeterminate=True) es_update = self.ert().getESUpdate() es_update.setGlobalStdScaling(weight) try: es_update.smootherUpdate(run_context) except ErtAnalysisError as e: raise ErtRunError( "Analysis of simulation failed for iteration:" f"{next_iteration}. The following error occured {e}") from e # Push update data to new storage analysis_module_name = self.ert().analysisConfig().activeModuleName() update_id = self._post_update_data(parent_ensemble_id=ensemble_id, algorithm=analysis_module_name) return update_id
def runSimulations(self, arguments): phase_count = ERT.enkf_facade.get_number_of_iterations() + 1 self.setPhaseCount(phase_count) analysis_module = self.setAnalysisModule(arguments["analysis_module"]) target_case_format = arguments["target_case"] run_context = self.create_context(arguments, 0) self.ert().analysisConfig().getAnalysisIterConfig().setCaseFormat( target_case_format) ensemble_id = self._runAndPostProcess(run_context, arguments) analysis_config = self.ert().analysisConfig() analysis_iter_config = analysis_config.getAnalysisIterConfig() num_retries_per_iteration = analysis_iter_config.getNumRetries() num_retries = 0 current_iter = 0 while (current_iter < ERT.enkf_facade.get_number_of_iterations() and num_retries < num_retries_per_iteration): pre_analysis_iter_num = analysis_module.getInt("ITER") # We run the PRE_FIRST_UPDATE hook here because the current_iter is explicitly available, versus # in the run_context inside analyzeStep if current_iter == 0: EnkfSimulationRunner.runWorkflows(HookRuntime.PRE_FIRST_UPDATE, ert=ERT.ert) update_id = self.analyzeStep(run_context, ensemble_id) current_iter = analysis_module.getInt("ITER") analysis_success = current_iter > pre_analysis_iter_num if analysis_success: run_context = self.create_context(arguments, current_iter, prior_context=run_context) ensemble_id = self._runAndPostProcess(run_context, arguments, update_id) num_retries = 0 else: run_context = self.create_context(arguments, current_iter, prior_context=run_context, rerun=True) ensemble_id = self._runAndPostProcess(run_context, arguments, update_id) num_retries += 1 if current_iter == (phase_count - 1): self.setPhase(phase_count, "Simulations completed.") else: raise ErtRunError( "Iterated Ensemble Smoother stopped: maximum number of iteration retries (%d retries) reached for iteration %d" % (num_retries_per_iteration, current_iter)) return run_context
def create_context(self, arguments, itr, initialize_mask_from_arguments=True, update=True): target_case_format = arguments["target_case"] model_config = self.ert().getModelConfig() runpath_fmt = model_config.getRunpathFormat() jobname_fmt = model_config.getJobnameFormat() subst_list = self.ert().getDataKW() fs_manager = self.ert().getEnkfFsManager() source_case_name = target_case_format % itr if itr > 0 and not fs_manager.caseExists(source_case_name): raise ErtRunError( "Source case {} for iteration {} does not exists. " "If you are attempting to restart ESMDA from a iteration other than 0, " "make sure the target case format is the same as for the original run! " "(Current target case format: {})".format( source_case_name, itr, target_case_format)) sim_fs = fs_manager.getFileSystem(source_case_name) if update: target_fs = fs_manager.getFileSystem(target_case_format % (itr + 1)) else: target_fs = None if initialize_mask_from_arguments: mask = arguments["active_realizations"] else: state = (RealizationStateEnum.STATE_HAS_DATA | RealizationStateEnum.STATE_INITIALIZED) mask = sim_fs.getStateMap().createMask(state) # Make sure to only run the realizations which was passed in as argument for index, run_realization in enumerate( self.initial_realizations_mask): mask[index] = mask[index] and run_realization # Deleting a run_context removes the possibility to retrospectively # determine detailed progress. Thus, before deletion, the detailed # progress is stored. self.updateDetailedProgress() run_context = ErtRunContext.ensemble_smoother(sim_fs, target_fs, mask, runpath_fmt, jobname_fmt, subst_list, itr) self._run_context = run_context self._last_run_iteration = run_context.get_iter() self.ert().getEnkfFsManager().switchFileSystem(sim_fs) return run_context
def analyzeStep(self, run_context): target_fs = run_context.get_target_fs() self.setPhaseName("Analyzing...", indeterminate=True) source_fs = self.ert().getEnkfFsManager().getCurrentFileSystem() self.setPhaseName("Pre processing update...", indeterminate=True) EnkfSimulationRunner.runWorkflows(HookRuntime.PRE_UPDATE, ert=ERT.ert) es_update = self.ert().getESUpdate() success = es_update.smootherUpdate(run_context) if not success: raise ErtRunError("Analysis of simulation failed!") self.setPhaseName("Post processing update...", indeterminate=True) EnkfSimulationRunner.runWorkflows(HookRuntime.POST_UPDATE, ert=ERT.ert)
def create_context( self, itr: int, initialize_mask_from_arguments: bool = True, update: bool = True, ) -> ErtRunContext: target_case_format = self._simulation_arguments["target_case"] model_config = self.ert().getModelConfig() runpath_fmt = model_config.getRunpathFormat() jobname_fmt = model_config.getJobnameFormat() subst_list = self.ert().getDataKW() fs_manager = self.ert().getEnkfFsManager() source_case_name = target_case_format % itr if itr > 0 and not fs_manager.caseExists(source_case_name): raise ErtRunError( "Source case {} for iteration {} does not exists. " "If you are attempting to restart ESMDA from a iteration other than 0, " "make sure the target case format is the same as for the original run! " "(Current target case format: {})".format( source_case_name, itr, target_case_format)) sim_fs = fs_manager.getFileSystem(source_case_name) if update: target_fs = fs_manager.getFileSystem(target_case_format % (itr + 1)) else: target_fs = None if initialize_mask_from_arguments: mask = self._simulation_arguments["active_realizations"] else: mask = sim_fs.getStateMap().createMask( RealizationStateEnum.STATE_HAS_DATA | RealizationStateEnum.STATE_INITIALIZED) # Make sure to only run the realizations which was passed in as argument for idx, (valid_state, run_realization) in enumerate( zip(mask, self._initial_realizations_mask)): mask[idx] = valid_state and run_realization run_context = ErtRunContext.ensemble_smoother(sim_fs, target_fs, mask, runpath_fmt, jobname_fmt, subst_list, itr) self._run_context = run_context self._last_run_iteration = run_context.get_iter() self.ert().getEnkfFsManager().switchFileSystem(sim_fs) return run_context
def analyzeStep(self, run_context, ensemble_id): target_fs = run_context.get_target_fs() self.setPhaseName("Analyzing...", indeterminate=True) source_fs = self.ert().getEnkfFsManager().getCurrentFileSystem() self.setPhaseName("Pre processing update...", indeterminate=True) EnkfSimulationRunner.runWorkflows(HookRuntime.PRE_UPDATE, ert=ERT.ert) es_update = self.ert().getESUpdate() success = es_update.smootherUpdate(run_context) # Push update data to new storage analysis_module_name = self.ert().analysisConfig().activeModuleName() update_id = self._post_update_data(parent_ensemble_id=ensemble_id, algorithm=analysis_module_name) if not success: raise ErtRunError("Analysis of simulation failed!") self.setPhaseName("Post processing update...", indeterminate=True) EnkfSimulationRunner.runWorkflows(HookRuntime.POST_UPDATE, ert=ERT.ert) return update_id
def analyzeStep(self, run_context: ErtRunContext, ensemble_id: str) -> str: self.setPhaseName("Analyzing...", indeterminate=True) self.setPhaseName("Pre processing update...", indeterminate=True) EnkfSimulationRunner.runWorkflows(HookRuntime.PRE_UPDATE, ert=self.ert()) es_update = self.ert().getESUpdate() try: es_update.smootherUpdate(run_context) except ErtAnalysisError as e: raise ErtRunError( f"Analysis of simulation failed with the following error: {e}" ) from e # Push update data to new storage analysis_module_name = self.ert().analysisConfig().activeModuleName() update_id = self._post_update_data( parent_ensemble_id=ensemble_id, algorithm=analysis_module_name ) self.setPhaseName("Post processing update...", indeterminate=True) EnkfSimulationRunner.runWorkflows(HookRuntime.POST_UPDATE, ert=self.ert()) return update_id
def runSimulations( self, evaluator_server_config: EvaluatorServerConfig) -> ErtRunContext: prior_context = self.create_context() self._checkMinimumActiveRealizations(prior_context) self.setPhase(0, "Running simulations...", indeterminate=False) # self.setAnalysisModule(arguments["analysis_module"]) self.setPhaseName("Pre processing...", indeterminate=True) self.ert().getEnkfSimulationRunner().createRunPath(prior_context) EnkfSimulationRunner.runWorkflows(HookRuntime.PRE_SIMULATION, ert=self.ert()) # Push ensemble, parameters, observations to new storage ensemble_id = self._post_ensemble_data() self.setPhaseName("Running forecast...", indeterminate=False) num_successful_realizations = self.run_ensemble_evaluator( prior_context, evaluator_server_config) # Push simulation results to storage self._post_ensemble_results(ensemble_id) self.checkHaveSufficientRealizations(num_successful_realizations) self.setPhaseName("Post processing...", indeterminate=True) EnkfSimulationRunner.runWorkflows(HookRuntime.POST_SIMULATION, ert=self.ert()) self.setPhaseName("Analyzing...") EnkfSimulationRunner.runWorkflows(HookRuntime.PRE_FIRST_UPDATE, ert=self.ert()) EnkfSimulationRunner.runWorkflows(HookRuntime.PRE_UPDATE, ert=self.ert()) es_update = self.ert().getESUpdate() try: es_update.smootherUpdate(prior_context) except ErtAnalysisError as e: raise ErtRunError( f"Analysis of simulation failed with the following error: {e}" ) from e EnkfSimulationRunner.runWorkflows(HookRuntime.POST_UPDATE, ert=self.ert()) # Create an update object in storage analysis_module_name = self.ert().analysisConfig().activeModuleName() update_id = self._post_update_data(ensemble_id, analysis_module_name) self.setPhase(1, "Running simulations...") self.ert().getEnkfFsManager().switchFileSystem( prior_context.get_target_fs()) self.setPhaseName("Pre processing...") rerun_context = self.create_context(prior_context=prior_context) self.ert().getEnkfSimulationRunner().createRunPath(rerun_context) EnkfSimulationRunner.runWorkflows(HookRuntime.PRE_SIMULATION, ert=self.ert()) # Push ensemble, parameters, observations to new storage ensemble_id = self._post_ensemble_data(update_id=update_id) self.setPhaseName("Running forecast...", indeterminate=False) num_successful_realizations = self.run_ensemble_evaluator( rerun_context, evaluator_server_config) self.checkHaveSufficientRealizations(num_successful_realizations) self.setPhaseName("Post processing...", indeterminate=True) EnkfSimulationRunner.runWorkflows(HookRuntime.POST_SIMULATION, ert=self.ert()) # Push simulation results to storage self._post_ensemble_results(ensemble_id) self.setPhase(2, "Simulations completed.") return prior_context
def runSimulations(self, arguments): prior_context = self.create_context(arguments) self.checkMinimumActiveRealizations(prior_context) self.setPhase(0, "Running simulations...", indeterminate=False) # self.setAnalysisModule(arguments["analysis_module"]) self.setPhaseName("Pre processing...", indeterminate=True) self.ert().getEnkfSimulationRunner().createRunPath(prior_context) # Push ensemble, parameters, observations to new storage ensemble_id = post_ensemble_data() EnkfSimulationRunner.runWorkflows(HookRuntime.PRE_SIMULATION, ert=ERT.ert) self.setPhaseName("Running forecast...", indeterminate=False) if FeatureToggling.is_enabled("ensemble-evaluator"): ee_config = arguments["ee_config"] num_successful_realizations = self.run_ensemble_evaluator( prior_context, ee_config) else: self._job_queue = self._queue_config.create_job_queue() num_successful_realizations = self.ert().getEnkfSimulationRunner( ).runSimpleStep(self._job_queue, prior_context) self.checkHaveSufficientRealizations(num_successful_realizations) self.setPhaseName("Post processing...", indeterminate=True) EnkfSimulationRunner.runWorkflows(HookRuntime.POST_SIMULATION, ert=ERT.ert) self.setPhaseName("Analyzing...") EnkfSimulationRunner.runWorkflows(HookRuntime.PRE_FIRST_UPDATE, ert=ERT.ert) EnkfSimulationRunner.runWorkflows(HookRuntime.PRE_UPDATE, ert=ERT.ert) es_update = self.ert().getESUpdate() success = es_update.smootherUpdate(prior_context) if not success: raise ErtRunError("Analysis of simulation failed!") EnkfSimulationRunner.runWorkflows(HookRuntime.POST_UPDATE, ert=ERT.ert) # Push simulation results to storage post_ensemble_results(ensemble_id) self.setPhase(1, "Running simulations...") self.ert().getEnkfFsManager().switchFileSystem( prior_context.get_target_fs()) self.setPhaseName("Pre processing...") rerun_context = self.create_context(arguments, prior_context=prior_context) self.ert().getEnkfSimulationRunner().createRunPath(rerun_context) # Push ensemble, parameters, observations to new storage analysis_module_name = self.ert().analysisConfig().activeModuleName() ensemble_id = post_ensemble_data((ensemble_id, analysis_module_name)) EnkfSimulationRunner.runWorkflows(HookRuntime.PRE_SIMULATION, ert=ERT.ert) self.setPhaseName("Running forecast...", indeterminate=False) if FeatureToggling.is_enabled("ensemble-evaluator"): ee_config = arguments["ee_config"] num_successful_realizations = self.run_ensemble_evaluator( rerun_context, ee_config) else: self._job_queue = self._queue_config.create_job_queue() num_successful_realizations = self.ert().getEnkfSimulationRunner( ).runSimpleStep(self._job_queue, rerun_context) self.checkHaveSufficientRealizations(num_successful_realizations) self.setPhaseName("Post processing...", indeterminate=True) EnkfSimulationRunner.runWorkflows(HookRuntime.POST_SIMULATION, ert=ERT.ert) self.setPhase(2, "Simulations completed.") # Push simulation results to storage post_ensemble_results(ensemble_id) return prior_context
def checkHaveSufficientRealizations( self, num_successful_realizations: int) -> None: # Should only have one successful realization if num_successful_realizations == 0: raise ErtRunError("Simulation failed!")
def runSimulations(self, arguments): prior_context = self.create_context(arguments) self.checkMinimumActiveRealizations(prior_context) self.setPhase(0, "Running simulations...", indeterminate=False) # self.setAnalysisModule(arguments["analysis_module"]) self.setPhaseName("Pre processing...", indeterminate=True) self.ert().getEnkfSimulationRunner().createRunPath(prior_context) EnkfSimulationRunner.runWorkflows(HookRuntime.PRE_SIMULATION, ert=ERT.ert) self.setPhaseName("Running forecast...", indeterminate=False) self._job_queue = self._queue_config.create_job_queue() num_successful_realizations = self.ert().getEnkfSimulationRunner( ).runSimpleStep(self._job_queue, prior_context) self.checkHaveSufficientRealizations(num_successful_realizations) self.setPhaseName("Post processing...", indeterminate=True) EnkfSimulationRunner.runWorkflows(HookRuntime.POST_SIMULATION, ert=ERT.ert) self.setPhaseName("Analyzing...") EnkfSimulationRunner.runWorkflows(HookRuntime.PRE_FIRST_UPDATE, ert=ERT.ert) EnkfSimulationRunner.runWorkflows(HookRuntime.PRE_UPDATE, ert=ERT.ert) es_update = self.ert().getESUpdate() success = es_update.smootherUpdate(prior_context) if not success: raise ErtRunError("Analysis of simulation failed!") EnkfSimulationRunner.runWorkflows(HookRuntime.POST_UPDATE, ert=ERT.ert) previous_ensemble_name = dump_to_new_storage(reference=None) self.setPhase(1, "Running simulations...") self.ert().getEnkfFsManager().switchFileSystem( prior_context.get_target_fs()) self.setPhaseName("Pre processing...") rerun_context = self.create_context(arguments, prior_context=prior_context) self.ert().getEnkfSimulationRunner().createRunPath(rerun_context) EnkfSimulationRunner.runWorkflows(HookRuntime.PRE_SIMULATION, ert=ERT.ert) self.setPhaseName("Running forecast...", indeterminate=False) self._job_queue = self._queue_config.create_job_queue() num_successful_realizations = self.ert().getEnkfSimulationRunner( ).runSimpleStep(self._job_queue, rerun_context) self.checkHaveSufficientRealizations(num_successful_realizations) self.setPhaseName("Post processing...", indeterminate=True) EnkfSimulationRunner.runWorkflows(HookRuntime.POST_SIMULATION, ert=ERT.ert) self.setPhase(2, "Simulations completed.") analysis_module_name = self.ert().analysisConfig().activeModuleName() dump_to_new_storage(reference=(previous_ensemble_name, analysis_module_name)) return prior_context