def _runAndPostProcess(
        self,
        run_context: ErtRunContext,
        evaluator_server_config: EvaluatorServerConfig,
        update_id: Optional[str] = None,
    ) -> str:
        phase_msg = "Running iteration %d of %d simulation iterations..." % (
            run_context.get_iter(),
            self.phaseCount() - 1,
        )
        self.setPhase(run_context.get_iter(), phase_msg, indeterminate=False)

        self.setPhaseName("Pre processing...", indeterminate=True)
        self.ert().getEnkfSimulationRunner().createRunPath(run_context)
        EnkfSimulationRunner.runWorkflows(HookRuntime.PRE_SIMULATION, ert=self.ert())
        # create ensemble
        ensemble_id = self._post_ensemble_data(update_id=update_id)
        self.setPhaseName("Running forecast...", indeterminate=False)
        num_successful_realizations = self.run_ensemble_evaluator(
            run_context, evaluator_server_config
        )

        self.checkHaveSufficientRealizations(num_successful_realizations)

        self.setPhaseName("Post processing...", indeterminate=True)
        EnkfSimulationRunner.runWorkflows(HookRuntime.POST_SIMULATION, ert=self.ert())
        self._post_ensemble_results(ensemble_id)
        return ensemble_id
Beispiel #2
0
    def test_create(self):
        with TestAreaContext("run_context"):
            arg = None
            sim_fs = EnkfFs.createFileSystem("sim_fs",
                                             EnKFFSType.BLOCK_FS_DRIVER_ID,
                                             arg)
            target_fs = None

            mask = BoolVector(initial_size=100, default_value=True)
            mask[50] = False
            runpath_fmt = PathFormat("path/to/sim%d")
            subst_list = SubstitutionList()
            itr = 0
            jobname_fmt = "job%d"
            run_context1 = ErtRunContext(EnkfRunType.ENSEMBLE_EXPERIMENT,
                                         sim_fs, target_fs, mask, runpath_fmt,
                                         jobname_fmt, subst_list, itr)
            run_id1 = run_context1.get_id()

            run_arg0 = run_context1[0]
            with self.assertRaises(ValueError):
                run_arg0.getQueueIndex()

            self.assertEqual(run_arg0.iter_id, itr)
            self.assertEqual(run_id1, run_arg0.get_run_id())

            run_context2 = ErtRunContext(EnkfRunType.ENSEMBLE_EXPERIMENT,
                                         sim_fs, target_fs, mask, runpath_fmt,
                                         jobname_fmt, subst_list, itr)
            run_id2 = run_context2.get_id()

            self.assertFalse(run_id1 == run_id2)

            self.assertTrue(run_context1.is_active(49))
            self.assertFalse(run_context1.is_active(50))
Beispiel #3
0
    def run(self):
        target = self._run_widget.target_case()
        source = self._run_widget.source_case()

        ert = ERT.ert
        fs_manager = ert.getEnkfFsManager()
        es_update = ESUpdate(ert)

        target_fs = fs_manager.getFileSystem(target)
        source_fs = fs_manager.getFileSystem(source)
        run_context = ErtRunContext.ensemble_smoother_update(
            source_fs,
            target_fs,
        )
        success = es_update.smootherUpdate(run_context)

        if not success:
            msg = QMessageBox()
            msg.setIcon(QMessageBox.Warning)
            msg.setWindowTitle("Run Analysis")
            msg.setText("Unable to run analysis for case '%s'." % source)
            msg.setStandardButtons(QMessageBox.Ok)
            msg.exec_()
            return

        ERT.ertChanged.emit()
        self._dialog.accept()
def test_assert_symlink_deleted(setup_case):
    res_config = setup_case("local/snake_oil_field", "snake_oil.ert")
    ert = EnKFMain(res_config)
    runpath_list = ert.getRunpathList()

    runner = ert.getEnkfSimulationRunner()

    # create directory structure
    model_config = ert.getModelConfig()
    run_context = ErtRunContext.ensemble_experiment(
        ert.getEnkfFsManager().getCurrentFileSystem(),
        [True],
        model_config.getRunpathFormat(),
        model_config.getJobnameFormat(),
        ert.getDataKW(),
        0,
    )
    runner.createRunPath(run_context)

    # replace field file with symlink
    linkpath = "%s/permx.grdcel" % str(runpath_list[0].runpath)
    targetpath = "%s/permx.grdcel.target" % str(runpath_list[0].runpath)
    open(targetpath, "a").close()
    os.remove(linkpath)
    os.symlink(targetpath, linkpath)

    # recreate directory structure
    runner.createRunPath(run_context)

    # ensure field symlink is replaced by file
    assert not os.path.islink(linkpath)
Beispiel #5
0
def _create_runpath(ert: LibresFacade, iteration: int = 0) -> ErtRunContext:
    """
    Instantiate an ERT runpath. This will create the parameter coefficients.
    """
    enkf_main = ert._enkf_main
    result_fs = ert.get_current_fs()
    target_fs = ert._enkf_main.getEnkfFsManager().getFileSystem("iter")

    model_config = enkf_main.getModelConfig()
    runpath_fmt = model_config.getRunpathFormat()
    jobname_fmt = model_config.getJobnameFormat()
    subst_list = enkf_main.getDataKW()

    run_context = ErtRunContext.ensemble_smoother(
        result_fs,
        target_fs,
        BoolVector(default_value=True, initial_size=ert.get_ensemble_size()),
        runpath_fmt,
        jobname_fmt,
        subst_list,
        iteration,
    )

    enkf_main.getEnkfSimulationRunner().createRunPath(run_context)
    return run_context
Beispiel #6
0
    def test_simulated_custom_kw(self):
        config = self.createTestPath("local/custom_kw/mini_config")
        with ErtTestContext("python/enkf/data/custom_kw_simulated", config) as context:
            ert = context.getErt()

            ensemble_config = ert.ensembleConfig()
            self.assertTrue("AGGREGATED" in ensemble_config)

            config = ensemble_config.getNode("AGGREGATED").getCustomKeywordModelConfig()

            self.assertEqual(len(config.getKeys()), 0)

            simulation_runner = EnkfSimulationRunner(ert)
            job_queue = ert.get_queue_config().create_job_queue()

            iteration_count = 0
            active = BoolVector(default_value = True, initial_size = 4)
            subst_list = ert.getDataKW( )
            runpath_fmt = ert.getModelConfig( ).getRunpathFormat( )
            fs_manager = ert.getEnkfFsManager( )
            fs = fs_manager.getFileSystem("fs")
            jobname_fmt = ert.getModelConfig( ).getJobnameFormat( )

            run_context = ErtRunContext( EnkfRunType.ENSEMBLE_EXPERIMENT , fs, None , active , runpath_fmt, jobname_fmt, subst_list , iteration_count)

            simulation_runner.createRunPath( run_context )
            simulation_runner.runEnsembleExperiment(job_queue, run_context)

            config = ensemble_config.getNode("AGGREGATED").getCustomKeywordModelConfig()

            self.assertEqual(len(config.getKeys()), 4)
            self.assertItemsEqual(config.getKeys(), ["PERLIN_1", "PERLIN_2", "PERLIN_3", "STATE"])
Beispiel #7
0
    def create_context(
            self,
            prior_context: Optional[ErtRunContext] = None) -> ErtRunContext:

        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()
        if prior_context is None:
            sim_fs = fs_manager.getCurrentFileSystem()
            target_fs = fs_manager.getFileSystem(
                self._simulation_arguments["target_case"])
            itr = 0
            mask = self._simulation_arguments["active_realizations"]
        else:
            itr = 1
            sim_fs = prior_context.get_target_fs()
            target_fs = None
            mask = sim_fs.getStateMap().createMask(
                RealizationStateEnum.STATE_HAS_DATA
                | RealizationStateEnum.STATE_INITIALIZED)

        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()
        return run_context
Beispiel #8
0
    def create_context(self, arguments, prior_context=None):

        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()
        if prior_context is None:
            sim_fs = fs_manager.getCurrentFileSystem()
            target_fs = fs_manager.getFileSystem(arguments["target_case"])
            itr = 0
            mask = arguments["active_realizations"]
        else:
            itr = 1
            sim_fs = prior_context.get_target_fs()
            target_fs = None
            state = RealizationStateEnum.STATE_HAS_DATA | RealizationStateEnum.STATE_INITIALIZED
            mask = sim_fs.getStateMap().createMask(state)

        # 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()
        return run_context
Beispiel #9
0
    def update(self, line):
        arguments = splitArguments(line)

        if len(arguments) == 1:
            case_name = arguments[0]
            ert = self.ert()
            fs_manager = ert.getEnkfFsManager()

            ert.getEnkfSimulationRunner().runWorkflows(HookRuntime.PRE_UPDATE)

            es_update = ESUpdate(ert)
            target_fs = fs_manager.getFileSystem(case_name)
            source_fs = fs_manager.getCurrentFileSystem()
            model_config = ert.getModelConfig()
            runpath_fmt = model_config.getRunpathFormat()
            subst_list = ert.getDataKW()
            mask = BoolVector(default_value=True,
                              initial_size=ert.getEnsembleSize())

            run_context = ErtRunContext.ensemble_smoother(
                source_fs, target_fs, mask, runpath_fmt, subst_list, 0)
            success = es_update.smootherUpdate(run_context)

            if not success:
                self.lastCommandFailed("Unable to perform update")

            ert.getEnkfSimulationRunner().runWorkflows(HookRuntime.POST_UPDATE)

        else:
            self.lastCommandFailed(
                "Expected one argument: <target_fs> received: '%s'" % line)
    def create_context(
        self,
        itr: int,
        prior_context: Optional[ErtRunContext] = None,
        rerun: bool = False,
    ) -> ErtRunContext:
        model_config = self.ert().getModelConfig()
        runpath_fmt = model_config.getRunpathFormat()
        jobname_fmt = model_config.getJobnameFormat()
        subst_list = self.ert().getDataKW()
        target_case_format = self._simulation_arguments["target_case"]

        sim_fs = self.createTargetCaseFileSystem(itr, target_case_format)

        if prior_context is None:
            mask = self._simulation_arguments["active_realizations"]
        else:
            state: RealizationStateEnum = (
                RealizationStateEnum.STATE_HAS_DATA  # type: ignore
                | RealizationStateEnum.STATE_INITIALIZED
            )
            mask = sim_fs.getStateMap().createMask(state)

        if rerun:
            target_fs = None
        else:
            target_fs = self.createTargetCaseFileSystem(itr + 1, target_case_format)

        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 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
Beispiel #12
0
    def test_assert_export(self):
        with ErtTestContext(
                "create_runpath1",
                self.createTestPath(
                    "local/snake_oil_no_data/snake_oil.ert")) as tc:
            ert = tc.getErt()
            runpath_list = ert.getRunpathList()
            self.assertFalse(path.isfile(runpath_list.getExportFile()))

            ens_size = ert.getEnsembleSize()
            runner = ert.getEnkfSimulationRunner()
            fs_manager = ert.getEnkfFsManager()

            init_fs = fs_manager.getFileSystem("init_fs")
            mask = BoolVector(initial_size=100, default_value=True)
            runpath_fmt = ert.getModelConfig().getRunpathFormat()
            subst_list = SubstitutionList()
            itr = 0
            run_context1 = ErtRunContext(EnkfRunType.INIT_ONLY, init_fs, None,
                                         None, mask, runpath_fmt, subst_list,
                                         itr)

            runner.createRunPath(run_context1)

            self.assertTrue(path.isfile(runpath_list.getExportFile()))
            self.assertEqual("test_runpath_list.txt",
                             path.basename(runpath_list.getExportFile()))
Beispiel #13
0
    def test_update_code1(self):
        with ErtTestContext("row_scaling", self.config_file) as tc:
            main = tc.getErt()

            local_config = main.getLocalConfig()
            local_config.clear()
            obs = local_config.createObsdata("OBSSET_LOCAL")
            obs.addNode("WBHP0")
            obs.addNode("WWCT0")
            ministep = local_config.createMinistep("MINISTEP_LOCAL")
            ministep.addActiveData("PORO")
            ministep.attachObsset(obs)
            updatestep = local_config.getUpdatestep()
            updatestep.attachMinistep(ministep)

            row_scaling = ministep.row_scaling("PORO")
            ens_config = main.ensembleConfig()
            poro_config = ens_config["PORO"]
            field_config = poro_config.getFieldModelConfig()

            # -------------------------------------------------------------------------------------
            grid = main.eclConfig().getGrid()
            obs_pos = grid.get_xyz(ijk=(5, 5, 1))
            length_scale = (2, 1, 0.50)
            row_scaling.assign(field_config.get_data_size(),
                               GaussianDecay(obs_pos, length_scale, grid))
            # -------------------------------------------------------------------------------------

            init_fs = init_data(main)
            target_fs = main.getEnkfFsManager().getFileSystem("target")
            es_update = ESUpdate(main)
            run_context = ErtRunContext.ensemble_smoother_update(
                init_fs, target_fs)
            es_update.smootherUpdate(run_context)
    def create_context(self, arguments, itr, prior_context = None, 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()

        sim_fs = fs_manager.getFileSystem(target_case_format % itr)
        if update:
            target_fs = fs_manager.getFileSystem(target_case_format % (itr + 1))
        else:
            target_fs = None

        if prior_context is None:
            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

        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()
        return run_context
    def create_context(self, arguments, itr, prior_context=None, rerun=False):
        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()
        target_case_format = arguments["target_case"]

        if prior_context is None:
            mask = arguments["active_realizations"]
        else:
            mask = prior_context.get_mask()

        sim_fs = self.createTargetCaseFileSystem(itr, target_case_format)
        if rerun:
            target_fs = None
        else:
            target_fs = self.createTargetCaseFileSystem(
                itr + 1, target_case_format)

        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()
        return run_context
    def create_context(self, arguments, itr, prior_context=None, rerun=False):
        model_config = self.ert().getModelConfig()
        runpath_fmt = model_config.getRunpathFormat()
        jobname_fmt = model_config.getJobnameFormat()
        subst_list = self.ert().getDataKW()
        target_case_format = arguments["target_case"]

        sim_fs = self.createTargetCaseFileSystem(itr, target_case_format)

        if prior_context is None:
            mask = arguments["active_realizations"]
        else:
            state = (RealizationStateEnum.STATE_HAS_DATA
                     | RealizationStateEnum.STATE_INITIALIZED)
            mask = sim_fs.getStateMap().createMask(state)

        if rerun:
            target_fs = None
        else:
            target_fs = self.createTargetCaseFileSystem(
                itr + 1, target_case_format)

        # 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
Beispiel #17
0
    def test_assert_symlink_deleted(self):
        with ErtTestContext("create_runpath_symlink_deleted" , self.createTestPath("local/snake_oil_field/snake_oil.ert")) as tc:
            ert = tc.getErt( )
            runpath_list = ert.getRunpathList( )

            ens_size = ert.getEnsembleSize()
            runner = ert.getEnkfSimulationRunner()
            mask = BoolVector( initial_size = ens_size , default_value = True )
            fs_manager = ert.getEnkfFsManager()
            init_fs = fs_manager.getFileSystem("init_fs")

            # create directory structure
            runpath_fmt = ert.getModelConfig().getRunpathFormat( )
            subst_list = SubstitutionList( )
            itr = 0
            jobname_fmt = ert.getModelConfig().getJobnameFormat()
            run_context = ErtRunContext( EnkfRunType.INIT_ONLY , init_fs, None , mask , runpath_fmt, jobname_fmt, subst_list , itr )
            runner.createRunPath( run_context )


            # replace field file with symlink
            linkpath = '%s/permx.grdcel' % str(runpath_list[0].runpath)
            targetpath = '%s/permx.grdcel.target' % str(runpath_list[0].runpath)
            open(targetpath, 'a').close()
            os.remove(linkpath)
            os.symlink(targetpath, linkpath)

            # recreate directory structure
            runner.createRunPath( run_context )

            # ensure field symlink is replaced by file
            self.assertFalse( os.path.islink(linkpath) )
Beispiel #18
0
    def create_context(self, arguments, itr, prior_context=None, 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()

        sim_fs = fs_manager.getFileSystem(target_case_format % itr)
        if update:
            target_fs = fs_manager.getFileSystem(target_case_format %
                                                 (itr + 1))
        else:
            target_fs = None

        if prior_context is None:
            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

        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()
        return run_context
Beispiel #19
0
    def update(self, line):
        arguments = splitArguments(line)

        if len(arguments) == 1:
            case_name = arguments[0]
            ert = self.ert()
            fs_manager = ert.getEnkfFsManager() 

            ert.getEnkfSimulationRunner().runWorkflows(HookRuntime.PRE_UPDATE)

            es_update = ESUpdate( ert )
            target_fs = fs_manager.getFileSystem(case_name)
            source_fs = fs_manager.getCurrentFileSystem( )
            model_config = ert.getModelConfig( )
            runpath_fmt = model_config.getRunpathFormat( )
            subst_list = ert.getDataKW( )
            mask = BoolVector( default_value = True, initial_size = ert.getEnsembleSize( ) )
            
            run_context = ErtRunContext.ensemble_smoother( source_fs , target_fs , mask, runpath_fmt, subst_list, 0 )
            success = es_update.smootherUpdate( run_context  )

            if not success:
                self.lastCommandFailed("Unable to perform update")

            ert.getEnkfSimulationRunner().runWorkflows(HookRuntime.POST_UPDATE)

        else:
            self.lastCommandFailed("Expected one argument: <target_fs> received: '%s'" % line)
Beispiel #20
0
    def test_update(self):
        config = self.createTestPath("local/snake_oil/snake_oil.ert")
        with ErtTestContext("update_test", config) as context:
            ert = context.getErt()
            es_update = ESUpdate(ert)
            fsm = ert.getEnkfFsManager()

            sim_fs = fsm.getFileSystem("default_0")
            target_fs = fsm.getFileSystem("target")
            mask = BoolVector(initial_size=ert.getEnsembleSize(), default_value=True)
            run_context = ErtRunContext.ensemble_smoother_update(sim_fs, target_fs)
            es_update.smootherUpdate(run_context)

            conf = ert.ensembleConfig()["SNAKE_OIL_PARAM"]
            sim_node = EnkfNode(conf)
            target_node = EnkfNode(conf)

            node_id = NodeId(0, 0)
            sim_node.load(sim_fs, node_id)
            target_node.load(target_fs, node_id)

            sim_gen_kw = sim_node.asGenKw()
            target_gen_kw = target_node.asGenKw()

            # Test that an update has actually taken place
            for index in range(len(sim_gen_kw)):
                self.assertNotEqual(sim_gen_kw[index], target_gen_kw[index])
Beispiel #21
0
def _run_ministep(ert, obs_group, data_parameters, prior_name, target_name,
                  output_path):
    local_config = ert.getLocalConfig()
    # Reset internal local config structure, in order to make your own
    local_config.clear()

    # A ministep is used to link betwen data and observations.
    # Make more ministeps to condition different groups together
    ministep = local_config.createMinistep("MINISTEP")
    # Add all dataset to localize
    data_all = local_config.createDataset("DATASET")
    for data in data_parameters:
        data_all.addNode(data)
    # Add all obs to be used in this updating scheme
    obsdata = local_config.createObsdata("OBS")
    for obs in obs_group:
        obsdata.addNode(obs)
    # Attach the created dataset and obsset to the ministep
    ministep.attachDataset(data_all)
    ministep.attachObsset(obsdata)
    # Then attach the ministep to the update step
    local_config.getUpdatestep().attachMinistep(ministep)

    # Perform update analysis
    ert.analysisConfig().set_log_path(output_path)
    run_context = ErtRunContext.ensemble_smoother_update(
        ert.getEnkfFsManager().getFileSystem(prior_name),
        ert.getEnkfFsManager().getFileSystem(target_name),
    )
    ESUpdate(ert).smootherUpdate(run_context)
Beispiel #22
0
def init_data(main):
    fsm = main.getEnkfFsManager()
    init_fs = fsm.getFileSystem("init")
    grid = main.eclConfig().getGrid()

    # Model: bhp = poro * 1000
    poro_mean = 0.15
    poro_std = 0.10
    bhp_std = 125

    # Model: wct = poro * 4
    wct_std = 0.30

    bhp = []
    wct = []
    num_realisations = main.getEnsembleSize()

    # The path fields/poro{}.grdecl must be consistent with the INIT_FILES:
    # argument in the PORO configuration in the configuration file used for the
    # testcase.
    os.mkdir("fields")
    random.seed(12345)
    for i in range(num_realisations):
        with open("fields/poro{}.grdecl".format(i), "w") as f:
            poro = random.gauss(poro_mean, poro_std)
            f.write("PORO")
            for i in range(grid.get_num_active()):
                if i % 10 == 0:
                    f.write("\n")

                f.write("{:<7.5} ".format(poro))
            f.write("\n/\n")
        bhp.append(poro * 1000 + random.gauss(0, bhp_std))
        wct.append(poro * 4 + random.gauss(0, wct_std))

    mask = [True] * main.getEnsembleSize()
    init_context = ErtRunContext.case_init(init_fs, mask)
    main.initRun(init_context)

    ens_config = main.ensembleConfig()
    bhp_config = ens_config["WBHP"]
    wct_config = ens_config["WWCT"]
    state_map = init_fs.getStateMap()
    for iens in range(main.getEnsembleSize()):
        bhp_node = EnkfNode(bhp_config)
        bhp_summary = bhp_node.as_summary()
        bhp_summary[1] = bhp[iens]

        wct_node = EnkfNode(wct_config)
        wct_summary = wct_node.as_summary()
        wct_summary[1] = wct[iens]

        node_id = NodeId(1, iens)
        bhp_node.save(init_fs, node_id)
        wct_node.save(init_fs, node_id)
        state_map[iens] = RealizationStateEnum.STATE_HAS_DATA

    return init_fs
Beispiel #23
0
    def test_localization(self):
        config = self.createTestPath("local/snake_oil/snake_oil.ert")
        with ErtTestContext("localization_test", config) as context:
            ert = context.getErt()
            es_update = ESUpdate(ert)
            fsm = ert.getEnkfFsManager()
            sim_fs = fsm.getFileSystem("default_0")
            target_fs = fsm.getFileSystem("target")

            # perform localization
            localized_idxs = (1, 2)
            local_config = ert.getLocalConfig()
            local_config.clear()
            dataset = local_config.createDataset("DATASET_SCALAR_LOCA")
            dataset.addNode("SNAKE_OIL_PARAM")
            active_list = dataset.getActiveList("SNAKE_OIL_PARAM")
            for i in localized_idxs:
                active_list.addActiveIndex(i)
            obs = local_config.createObsdata("OBSSET_LOCA")
            obs.addNode("WOPR_OP1_72")
            ministep = local_config.createMinistep("MINISTEP_LOCA")
            ministep.attachDataset(dataset)
            ministep.attachObsset(obs)
            updatestep = local_config.getUpdatestep()
            updatestep.attachMinistep(ministep)

            # Run enseble smoother
            mask = BoolVector(initial_size=ert.getEnsembleSize(), default_value=True)
            model_config = ert.getModelConfig()
            path_fmt = model_config.getRunpathFormat()
            jobname_fmt = model_config.getJobnameFormat()
            subst_list = None
            run_context = ErtRunContext.ensemble_smoother(
                sim_fs, target_fs, mask, path_fmt, jobname_fmt, subst_list, 0
            )
            es_update.smootherUpdate(run_context)

            conf = ert.ensembleConfig()["SNAKE_OIL_PARAM"]
            sim_node = EnkfNode(conf)
            target_node = EnkfNode(conf)

            node_id = NodeId(0, 0)
            sim_node.load(sim_fs, node_id)
            target_node.load(target_fs, node_id)

            sim_gen_kw = sim_node.asGenKw()
            target_gen_kw = target_node.asGenKw()

            # Test that the localized values has been updated
            for i in localized_idxs:
                self.assertNotEqual(sim_gen_kw[i], target_gen_kw[i])

            # test that all the other values are left unchanged
            non_localized_idxs = (
                x for x in range(len(sim_gen_kw)) if x not in localized_idxs
            )
            for i in non_localized_idxs:
                self.assertEqual(sim_gen_kw[i], target_gen_kw[i])
Beispiel #24
0
def test_localization(setup_case, expected_target_gen_kw):
    """
    Note that this is now a snapshot test, so there is no guarantee that the
    snapshots are correct, they are just documenting the current behavior.
    """
    res_config = setup_case("local/snake_oil", "snake_oil.ert")
    ert = EnKFMain(res_config)
    es_update = ESUpdate(ert)
    fsm = ert.getEnkfFsManager()
    sim_fs = fsm.getFileSystem("default_0")
    target_fs = fsm.getFileSystem("target")

    # perform localization
    localized_idxs = (1, 2)
    local_config = ert.getLocalConfig()
    local_config.clear()
    obs = local_config.createObsdata("OBSSET_LOCA")
    obs.addNode("WOPR_OP1_72")
    ministep = local_config.createMinistep("MINISTEP_LOCA")
    ministep.addActiveData("SNAKE_OIL_PARAM")  # replace dataset.addNode()
    active_list = ministep.getActiveList("SNAKE_OIL_PARAM")
    for i in localized_idxs:
        active_list.addActiveIndex(i)
    ministep.attachObsset(obs)
    updatestep = local_config.getUpdatestep()
    updatestep.attachMinistep(ministep)

    # Run ensemble smoother
    mask = [True] * ert.getEnsembleSize()
    model_config = ert.getModelConfig()
    path_fmt = model_config.getRunpathFormat()
    jobname_fmt = model_config.getJobnameFormat()
    subst_list = None
    run_context = ErtRunContext.ensemble_smoother(
        sim_fs, target_fs, mask, path_fmt, jobname_fmt, subst_list, 0
    )
    es_update.smootherUpdate(run_context)

    conf = ert.ensembleConfig()["SNAKE_OIL_PARAM"]
    sim_node = EnkfNode(conf)
    target_node = EnkfNode(conf)

    node_id = NodeId(0, 0)
    sim_node.load(sim_fs, node_id)
    target_node.load(target_fs, node_id)

    sim_gen_kw = list(sim_node.asGenKw())
    target_gen_kw = list(target_node.asGenKw())

    # Test that the localized values has been updated
    assert sim_gen_kw[1:3] != target_gen_kw[1:3]

    # test that all the other values are left unchanged
    assert sim_gen_kw[3:] == target_gen_kw[3:]
    assert sim_gen_kw[0] == target_gen_kw[0]

    assert target_gen_kw == pytest.approx(expected_target_gen_kw)
Beispiel #25
0
def _ensemble_smoother_run(ert, target_case):
    source_fs, target_fs = setup_fs(ert, target_case)

    model_config = ert.getModelConfig()
    subst_list = ert.getDataKW()

    mask = BoolVector(default_value=True, initial_size=ert.getEnsembleSize())

    prior_context = ErtRunContext.ensemble_smoother(
        sim_fs=source_fs,
        target_fs=target_fs,
        mask=mask,
        path_fmt=model_config.getRunpathFormat(),
        jobname_fmt=model_config.getJobnameFormat(),
        subst_list=subst_list,
        itr=0)

    sim_runner = ert.getEnkfSimulationRunner()
    _run_ensemble_experiment(ert, prior_context, sim_runner)
    sim_runner.runWorkflows(HookRuntime.PRE_UPDATE)

    es_update = ESUpdate(ert)
    success = es_update.smootherUpdate(prior_context)
    if not success:
        raise AssertionError("Analysis of simulation failed!")

    sim_runner.runWorkflows(HookRuntime.POST_UPDATE)

    ert.getEnkfFsManager().switchFileSystem(prior_context.get_target_fs())

    sim_fs = prior_context.get_target_fs()
    state = RealizationStateEnum.STATE_HAS_DATA | RealizationStateEnum.STATE_INITIALIZED
    mask = sim_fs.getStateMap().createMask(state)

    rerun_context = ErtRunContext.ensemble_smoother(
        sim_fs=sim_fs,
        target_fs=None,
        mask=mask,
        path_fmt=model_config.getRunpathFormat(),
        jobname_fmt=model_config.getJobnameFormat(),
        subst_list=subst_list,
        itr=1)

    _run_ensemble_experiment(ert, rerun_context, sim_runner)
Beispiel #26
0
def test_fs_init_from_scratch_deprecated():
    res_config = ResConfig("snake_oil.ert")
    ert = EnKFMain(res_config)
    sim_fs = ert.getEnkfFsManager().getFileSystem("new_case")  # new case
    mask = BoolVector.createFromList(25, [0, 1, 2, 3, 4, 5])
    run_context = ErtRunContext.case_init(sim_fs, mask)
    with pytest.warns(DeprecationWarning):
        ert.getEnkfFsManager().initializeFromScratch(
            StringList(["SNAKE_OIL_PARAM"]), run_context)
    assert len(ert.getEnkfFsManager().getStateMapForCase("new_case")) == 6
Beispiel #27
0
def test_fs_init_from_scratch():
    res_config = ResConfig("snake_oil.ert")
    ert = EnKFMain(res_config)
    sim_fs = ert.getEnkfFsManager().getFileSystem("new_case")
    mask = [True] * 6 + [False] * 19
    run_context = ErtRunContext.case_init(sim_fs, mask)

    ert.getEnkfFsManager().initializeFromScratch(
        StringList(["SNAKE_OIL_PARAM"]), run_context)
    assert len(ert.getEnkfFsManager().getStateMapForCase("new_case")) == 6
Beispiel #28
0
def initializeCurrentCaseFromScratch(parameters, members):
    selected_parameters = StringList(parameters)
    mask = BoolVector(initial_size = getRealizationCount(), default_value = False)
    for member in members:
        member = int(member.strip())
        mask[member] = True

    sim_fs = ERT.ert.getEnkfFsManager().getCurrentFileSystem()
    run_context = ErtRunContext.case_init(sim_fs, mask)
    ERT.ert.getEnkfFsManager().initializeFromScratch(selected_parameters, run_context)
    ERT.emitErtChange()
Beispiel #29
0
def initializeCurrentCaseFromScratch(
    parameters: List[str], members: List[str], ert: EnKFMain
):
    selected_parameters = StringList(parameters)
    mask = [False] * ert.getEnsembleSize()
    for member in members:
        member = int(member.strip())
        mask[member] = True

    sim_fs = ert.getEnkfFsManager().getCurrentFileSystem()
    run_context = ErtRunContext.case_init(sim_fs, mask)
    ert.getEnkfFsManager().initializeFromScratch(selected_parameters, run_context)
Beispiel #30
0
    def create_context(self, arguments):
        fs_manager = self.ert().getEnkfFsManager()
        init_fs = fs_manager.getCurrentFileSystem( )
        result_fs = fs_manager.getCurrentFileSystem( )

        model_config = self.ert().getModelConfig( )
        runpath_fmt = model_config.getRunpathFormat( )
        subst_list = self.ert().getDataKW( )
        itr = 0
        mask = arguments["active_realizations"]
        run_context = ErtRunContext.ensemble_experiment( init_fs, result_fs, mask, runpath_fmt, subst_list, itr)
        return run_context
Beispiel #31
0
    def test_create(self):
        with TestAreaContext("run_context"):
            arg = None
            sim_fs = EnkfFs.createFileSystem("sim_fs",
                                             EnKFFSType.BLOCK_FS_DRIVER_ID,
                                             arg)
            target_fs = None

            mask = BoolVector(initial_size=100, default_value=True)
            runpath_fmt = PathFormat("path/to/sim%d")
            subst_list = SubstitutionList()
            itr = 0
            run_context1 = ErtRunContext(EnkfRunType.ENSEMBLE_EXPERIMENT,
                                         sim_fs, target_fs, mask, runpath_fmt,
                                         subst_list, itr)
            run_id1 = run_context1.get_id()
            run_arg0 = run_context1[0]
            self.assertEqual(run_id1, run_arg0.get_run_id())

            run_context2 = ErtRunContext(EnkfRunType.ENSEMBLE_EXPERIMENT,
                                         sim_fs, target_fs, mask, runpath_fmt,
                                         subst_list, itr)
            run_id2 = run_context2.get_id()

            self.assertFalse(run_id1 == run_id2)
Beispiel #32
0
    def test_update_workflow(self):
        with ErtTestContext("row_scaling", self.config_file) as tc:
            main = tc.getErt()
            workflow_list = main.getWorkflowList()
            workflow = workflow_list["ROW_SCALING_WORKFLOW1"]
            self.assertTrue(workflow.run(main))

            init_fs = init_data(main)
            target_fs = main.getEnkfFsManager().getFileSystem("target")

            es_update = ESUpdate(main)
            run_context = ErtRunContext.ensemble_smoother_update(init_fs, target_fs)
            es_update.smootherUpdate(run_context)
Beispiel #33
0
    def create_context(self, arguments):
        fs_manager = self.ert().getEnkfFsManager()
        init_fs = fs_manager.getCurrentFileSystem( )
        result_fs = fs_manager.getCurrentFileSystem( )

        model_config = self.ert().getModelConfig( )
        runpath_fmt = model_config.getRunpathFormat( )
        subst_list = self.ert().getDataKW( )
        itr = 0

        mask = BoolVector(  default_value = False )
        mask[0] = True
        run_context = ErtRunContext.ensemble_experiment( init_fs, result_fs, mask, runpath_fmt, subst_list, itr)
        return run_context
Beispiel #34
0
 def create_context(self, arguments, prior_context = None):
     model_config = self.ert().getModelConfig( )
     runpath_fmt = model_config.getRunpathFormat( )
     subst_list = self.ert().getDataKW( )
     fs_manager = self.ert().getEnkfFsManager()
     if prior_context is None:
         sim_fs = fs_manager.getCurrentFileSystem( )
         target_fs = fs_manager.getFileSystem("smoother-update")
         itr = 0
         mask = arguments["active_realizations"]
     else:
         itr = 1
         mask = prior_context.get_mask( )
         sim_fs = prior_context.get_target_fs( )
         target_fs = None
         
     run_context = ErtRunContext.ensemble_smoother( sim_fs, target_fs, mask, runpath_fmt, subst_list, itr)
     return run_context
    def create_context(self, arguments, itr, prior_context = None, rerun = False):
        model_config = self.ert().getModelConfig( )
        runpath_fmt = model_config.getRunpathFormat( )
        subst_list = self.ert().getDataKW( )
        fs_manager = self.ert().getEnkfFsManager()
        target_case_format = arguments["target_case"]

        if prior_context is None:
            mask = arguments["active_realizations"]
        else:
            mask = prior_context.get_mask( )
        
        sim_fs = self.createTargetCaseFileSystem(itr, target_case_format)
        if rerun:
            target_fs = None
        else:
            target_fs = self.createTargetCaseFileSystem(itr + 1 , target_case_format)

        run_context = ErtRunContext.ensemble_smoother( sim_fs, target_fs, mask, runpath_fmt, subst_list, itr)
        return run_context
    def create_context(self, arguments, itr, prior_context = None, 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()

        sim_fs = fs_manager.getFileSystem(target_case_format % itr)
        if update:
            target_fs = fs_manager.getFileSystem(target_case_format % (itr + 1))
        else:
            target_fs = None

        if prior_context is None:
            mask = arguments["active_realizations"]
        else:
            mask = prior_context.get_mask( )

        run_context = ErtRunContext.ensemble_smoother( sim_fs, target_fs, mask, runpath_fmt, jobname_fmt, subst_list, itr)
        return run_context
Beispiel #37
0
    def create_context(self, arguments, prior_context = None):

        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()
        if prior_context is None:
            sim_fs = fs_manager.getCurrentFileSystem( )
            target_fs = fs_manager.getFileSystem(arguments["target_case"])
            itr = 0
            mask = arguments["active_realizations"]
        else:
            itr = 1
            sim_fs = prior_context.get_target_fs( )
            target_fs = None
            state = RealizationStateEnum.STATE_HAS_DATA | RealizationStateEnum.STATE_INITIALIZED
            mask = sim_fs.getStateMap().createMask(state)

        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()
        return run_context
Beispiel #38
0
    def run(self):
        target = self._run_widget.target_case()
        source = self._run_widget.source_case()

        ert = ERT.ert
        fs_manager = ert.getEnkfFsManager()
        es_update = ESUpdate(ert)

        target_fs = fs_manager.getFileSystem(target)
        source_fs = fs_manager.getFileSystem(source)
        run_context = ErtRunContext.ensemble_smoother_update( source_fs, target_fs, )
        success = es_update.smootherUpdate( run_context )

        if not success:
            msg = QMessageBox()
            msg.setIcon(QMessageBox.Warning)
            msg.setWindowTitle("Run Analysis")
            msg.setText("Unable to run analysis for case '%s'." % source)
            msg.setStandardButtons(QMessageBox.Ok)
            msg.exec_()
            return

        ERT.ertChanged.emit()
        self._dialog.accept()