Ejemplo n.º 1
0
    def load(self, fs, report_step, input_mask=None):
        """
        @type fs: EnkfFs
        @type report_step: int
        @type input_mask: BoolVector
        @rtype: PlotBlockData
        """

        state_map = fs.getStateMap()
        ensemble_size = len(state_map)

        if not input_mask is None:
            mask = BoolVector.copy(input_mask)
        else:
            mask = BoolVector(False, ensemble_size)

        state_map.selectMatching(mask, RealizationStateEnum.STATE_HAS_DATA)

        depth = self.getDepthValues(report_step)

        self.__permutation_vector = depth.permutationSort()
        depth.permute(self.__permutation_vector)

        plot_block_data = PlotBlockData(depth)

        thread_pool = ThreadPool()
        for index in range(ensemble_size):
            if mask[index]:
                thread_pool.addTask(self.loadVector, plot_block_data, fs,
                                    report_step, index)

        thread_pool.nonBlockingStart()
        thread_pool.join()

        return plot_block_data
Ejemplo n.º 2
0
    def test_it(self):
        state_size = 10
        with ErtTestContext("update", self.config_file) as tc:
            analysis = self.createAnalysisModule()
            ert = tc.getErt()
            obs = ert.getObservations()
            local_obsdata = obs.getAllActiveLocalObsdata()

            fs = ert.getEnkfFsManager().getCurrentFileSystem()

            mask = BoolVector(initial_size=ert.getEnsembleSize(), default_value=True)
            meas_data = MeasData(mask)
            obs_data = ObsData()
            obs.getObservationAndMeasureData(
                fs, local_obsdata, mask.createActiveList(), meas_data, obs_data
            )
            update(self.rng, mask, analysis, ert, meas_data, obs_data, state_size)

            mask[0] = False
            mask[4] = False
            meas_data = MeasData(mask)
            obs_data = ObsData()
            obs.getObservationAndMeasureData(
                fs, local_obsdata, mask.createActiveList(), meas_data, obs_data
            )
            update(self.rng, mask, analysis, ert, meas_data, obs_data, state_size)
Ejemplo n.º 3
0
    def test_bool_vector(self):
        b = BoolVector()
        b.setDefault(True)

        b[4] = False

        self.assertEqual(list(b), [True, True, True, True, False])
Ejemplo n.º 4
0
    def test_analysis_module(self):
        rng = RandomNumberGenerator()
        module = self.createAnalysisModule()
        ens_size = 12
        obs_size = 1
        state_size = 2

        true_params = [1.25, 0.75]
        true_state = forward_model(true_params)
        obs = [(measure(true_state), 0.75)]
        A = Matrix(state_size, ens_size)

        ens = []
        for iens in range(ens_size):
            param = [random.gauss(1.00, 1.00), random.gauss(1.00, 1.00)]
            ens.append(param)

        mask = BoolVector(default_value=True, initial_size=ens_size)
        mask[2] = False
        (A, S, E, D, R, dObs) = init_matrices(ens, mask, obs, rng)

        module.initUpdate(mask, S, R, dObs, E, D, rng)
        module.updateA(A, S, R, dObs, E, D, rng)

        mask[10] = False
        mask[5] = False
        (A, S, E, D, R, dObs) = init_matrices(ens, mask, obs, rng)
        self.assertEqual(S.dims(), (obs_size, mask.countEqual(True)))
        self.assertEqual(E.dims(), (obs_size, mask.countEqual(True)))
        self.assertEqual(D.dims(), (obs_size, mask.countEqual(True)))

        module.initUpdate(mask, S, R, dObs, E, D, rng)
        module.updateA(A, S, R, dObs, E, D, rng)
Ejemplo n.º 5
0
    def createActiveList(ert, fs):
        state_map = fs.getStateMap()
        ens_mask = BoolVector(False, ert.getEnsembleSize())
        state_map.selectMatching(ens_mask, RealizationStateEnum.STATE_HAS_DATA)
        active_list = BoolVector.createActiveList(ens_mask)

        return [iens for iens in active_list]
Ejemplo n.º 6
0
    def realizationList(self, state_value):
        """
        Will create a list of all realisations with state equal to state_value.

        @type state_value: RealizationStateEnum
        @rtype: ecl.util.IntVector
        """
        mask = BoolVector(False, len(self))
        self.selectMatching(mask, state_value)
        return BoolVector.createActiveList(mask)
Ejemplo n.º 7
0
    def test_update_active_mask(self):
        vec = BoolVector(False, 10)

        self.assertTrue(vec.updateActiveMask("1-2,5"))
        self.assertTrue(vec[1])
        self.assertTrue(vec[2])
        self.assertTrue(vec[5])
        self.assertFalse(vec[4])

        vec = BoolVector(False, 10)

        self.assertTrue(vec.updateActiveMask("1-5,2,3"))
        self.assertTrue(vec[1])
        self.assertTrue(vec[2])
        self.assertTrue(vec[3])
        self.assertTrue(vec[4])
        self.assertTrue(vec[5])
        self.assertFalse(vec[0])
        self.assertFalse(vec[6])

        vec = BoolVector(False, 10)

        self.assertTrue(vec.updateActiveMask("5,6,7,15"))
        self.assertTrue(vec[5])
        self.assertTrue(vec[6])
        self.assertTrue(vec[7])
        self.assertFalse(vec[4])
        self.assertFalse(vec[8])
        self.assertEqual(len(vec), 16)
Ejemplo n.º 8
0
    def test_create_active_list(self):
        vec = BoolVector(False, 10)
        vec[0] = True
        vec[3] = True
        vec[7] = True

        idxs = vec.createActiveList()
        self.assertTrue(len(idxs) == 3, "Should get 3 indices")
        self.assertEqual(idxs[0], 0)
        self.assertEqual(idxs[1], 3)
        self.assertEqual(idxs[2], 7)
Ejemplo n.º 9
0
    def getActiveRealizationsMask(self):
        count = getRealizationCount()

        mask = BoolVector(default_value=False, initial_size = count)
        if not mask.updateActiveMask(self.getValue()):
            raise ValueError('Error while parsing range string "%s"!' % self.getValue())

        if len(mask) != count:
            raise ValueError("Mask size changed %d != %d!" % (count, len(mask)))

        return mask
Ejemplo n.º 10
0
    def test_custom_realizations(self):
        config_file = self.createTestPath('local/poly_example/poly.ert')
        with ErtTestContext('test_custom_realizations', config_file) as work_area:
            ert = work_area.getErt()
            notifier = ErtCliNotifier(ert, config_file)
            ERT.adapt(notifier)

            args = Namespace(realizations="0-4,7,8")
            res = model_factory._realizations(args)
            ensemble_size = ERT.ert.getEnsembleSize()
            mask = BoolVector(default_value=False, initial_size=ensemble_size)
            mask.updateActiveMask("0-4,7,8")
            self.assertEqual(mask, res)
Ejemplo n.º 11
0
    def test_activeMask(self):
        active_list = BoolVector.createActiveMask("1 , 4 - 7 , 10")
        self.assertTrue(len(active_list) == 11)
        self.assertTrue(active_list[1])
        self.assertTrue(active_list[4])
        self.assertTrue(active_list[10])
        self.assertFalse(active_list[9])
        self.assertFalse(active_list[8])

        self.assertEqual(6, active_list.count(True))

        active_list = BoolVector.createActiveMask("1,4-7,10X")
        self.assertFalse(active_list)
Ejemplo n.º 12
0
    def test_activeMask(self):
        active_list = BoolVector.createActiveMask("1 , 4 - 7 , 10")
        self.assertTrue(len(active_list) == 11)
        self.assertTrue(active_list[1])
        self.assertTrue(active_list[4])
        self.assertTrue(active_list[10])
        self.assertFalse(active_list[9])
        self.assertFalse(active_list[8])

        self.assertEqual(6, active_list.count(True))

        active_list = BoolVector.createActiveMask("1,4-7,10X")
        self.assertFalse(active_list)
Ejemplo n.º 13
0
 def loadFromForwardModel(self, realization: List[bool], iteration: int,
                          fs):
     """Returns the number of loaded realizations"""
     true_indices = [idx for idx, value in enumerate(realization) if value]
     bool_vector = BoolVector.createFromList(size=len(realization),
                                             source_list=true_indices)
     return self._load_from_forward_model(iteration, bool_vector, fs)
Ejemplo n.º 14
0
    def test_assert_export(self):
        with ErtTestContext(
                "create_runpath_export",
                self.createTestPath("local/snake_oil_no_data/snake_oil.ert"),
        ) as tc:
            ert = tc.getErt()
            runpath_list = ert.getRunpathList()
            self.assertFalse(os.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=25, default_value=True)
            runpath_fmt = ert.getModelConfig().getRunpathFormat()
            subst_list = SubstitutionList()
            itr = 0
            jobname_fmt = ert.getModelConfig().getJobnameFormat()
            run_context1 = ErtRunContext(
                EnkfRunType.INIT_ONLY,
                init_fs,
                None,
                mask,
                runpath_fmt,
                jobname_fmt,
                subst_list,
                itr,
            )

            runner.createRunPath(run_context1)

            self.assertTrue(os.path.isfile(runpath_list.getExportFile()))
            self.assertEqual("test_runpath_list.txt",
                             os.path.basename(runpath_list.getExportFile()))
Ejemplo n.º 15
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))
Ejemplo n.º 16
0
    def test_with_gen_kw(self):
        case_directory = self.createTestPath('local/snake_oil_no_data/')
        with TestAreaContext('test_enkf_runpath',
                             store_area=True) as work_area:
            work_area.copy_directory(case_directory)
            res_config = ResConfig('snake_oil_no_data/snake_oil.ert')
            main = EnKFMain(res_config)
            iactive = BoolVector(initial_size=main.getEnsembleSize(),
                                 default_value=False)
            iactive[0] = True
            fs = main.getEnkfFsManager().getCurrentFileSystem()
            run_context = main.getRunContextENSEMPLE_EXPERIMENT(fs, iactive)
            main.createRunpath(run_context)
            self.assertFileExists(
                'snake_oil_no_data/storage/snake_oil/runpath/realisation-0/iter-0/parameters.txt'
            )
            self.assertEqual(
                len(os.listdir('snake_oil_no_data/storage/snake_oil/runpath')),
                1)
            self.assertEqual(
                len(
                    os.listdir(
                        'snake_oil_no_data/storage/snake_oil/runpath/realisation-0'
                    )), 1)

            rp = main.create_runpath_list()
            self.assertEqual(len(rp), 0)
            rp.load()
            self.assertEqual(len(rp), 1)
Ejemplo n.º 17
0
    def test_simulation_model(self):

        with TestAreaContext('enkf_test_sim_model_kw') as work_area:
            base_path = os.getcwd()
            source_path = self.createTestPath('local/simulation_model')

            work_area.copy_directory(source_path)
            dir_ert = os.path.join(base_path, 'simulation_model')
            assert (os.path.isdir(dir_ert))

            file_ert = os.path.join(dir_ert, 'sim_kw.ert')
            assert (os.path.isfile(file_ert))

            with ErtTestContext("sim_kw",
                                model_config=file_ert,
                                store_area=True) as ctx:
                ert = ctx.getErt()
                fs_manager = ert.getEnkfFsManager()
                result_fs = fs_manager.getCurrentFileSystem()

                model_config = ert.getModelConfig()
                forward_model = model_config.getForwardModel()
                self.assertEqual(forward_model.get_size(), 4)
                self.assertEqual(
                    forward_model.iget_job(3).get_arglist(), ['WORD_A'])
                self.assertEqual(
                    forward_model.iget_job(0).get_arglist(), ['<ARGUMENT>'])
                self.assertEqual(
                    forward_model.iget_job(1).get_arglist(),
                    ['Hello', 'True', '3.14', '4'])
                self.assertEqual(
                    forward_model.iget_job(2).get_arglist(),
                    ['word', '<ECLBASE>'])

                runpath_fmt = model_config.getRunpathFormat()
                jobname_fmt = model_config.getJobnameFormat()

                subst_list = ert.getDataKW()
                itr = 0
                mask = BoolVector(default_value=True, initial_size=1)

                run_context = ErtRunContext.ensemble_experiment(
                    result_fs, mask, runpath_fmt, jobname_fmt, subst_list, itr)
                ert.getEnkfSimulationRunner().createRunPath(run_context)
                queue_config = ert.get_queue_config()
                self.assertEqual(queue_config.num_cpu, 5)
                os.chdir('storage/sim_kw/runpath/realisation-0/iter-0')
                assert (os.path.isfile('jobs.json'))
                with open("jobs.json", "r") as f:
                    data = json.load(f)
                    jobList = data["jobList"]
                    old_job_A = jobList[3]
                    self.assertEqual(old_job_A["argList"], ['WORD_A'])
                    old_job_B = jobList[0]
                    self.assertEqual(old_job_B["argList"], ['yy'])
                    new_job_A = jobList[1]
                    self.assertEqual(new_job_A["argList"],
                                     ['Hello', 'True', '3.14', '4'])
                    new_job_B = jobList[2]
                    self.assertEqual(new_job_B["argList"], ['word', 'SIM_KW'])
Ejemplo n.º 18
0
    def test_update(self):
        key = "OBS"
        obs_size = 4
        ens_size = 10
        ens_mask = BoolVector(default_value=True, initial_size=ens_size)
        block = MeasBlock(key, obs_size, ens_mask)

        with self.assertRaises(TypeError):
            block["String"] = 10

        with self.assertRaises(TypeError):
            block[10] = 10

        with self.assertRaises(IndexError):
            block[obs_size, 0] = 10

        with self.assertRaises(IndexError):
            block[0, ens_size] = 10

        #-----------------------------------------------------------------

        with self.assertRaises(TypeError):
            a = block["String"]

        with self.assertRaises(TypeError):
            a = block[10]

        with self.assertRaises(IndexError):
            val = block[obs_size, 0]

        with self.assertRaises(IndexError):
            val = block[0, ens_size]

        block[1, 2] = 3
        self.assertEqual(3, block[1, 2])
Ejemplo n.º 19
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"])
Ejemplo n.º 20
0
    def createActiveList(self, fs):
        state_map = fs.getStateMap()
        ens_mask = BoolVector(False, self.ert().getEnsembleSize())
        state_map.selectMatching(ens_mask, RealizationStateEnum.STATE_HAS_DATA)
        active_list = BoolVector.createActiveList(ens_mask)

        return active_list
Ejemplo n.º 21
0
 def getSimulationArguments(self):
     return {
         "active_realizations": BoolVector(
             default_value=True, initial_size=self.number_of_realizations
         ),
         "config_file": self.config_file,
     }
Ejemplo n.º 22
0
    def test_run_context(self):
        with TestAreaContext("enkf_test") as work_area:
            work_area.copy_directory(self.case_directory)
            res_config = ResConfig("simple_config/minimum_config")
            main = EnKFMain(res_config)
            fs_manager = main.getEnkfFsManager()
            fs = fs_manager.getCurrentFileSystem()
            iactive = BoolVector(initial_size=10, default_value=True)
            iactive[0] = False
            iactive[1] = False
            run_context = main.getRunContextENSEMPLE_EXPERIMENT(fs, iactive)

            self.assertEqual(len(run_context), 10)

            with self.assertRaises(IndexError):
                run_context[10]

            with self.assertRaises(TypeError):
                run_context["String"]

            self.assertIsNone(run_context[0])
            run_arg = run_context[2]
            self.assertTrue(isinstance(run_arg, RunArg))

            rng1 = main.rng()
            rng1.setState("ABCDEFGHIJ012345")
            d1 = rng1.getDouble()
            rng1.setState("ABCDEFGHIJ012345")
            rng2 = main.rng()
            d2 = rng2.getDouble()
            self.assertEqual(d1, d2)
Ejemplo n.º 23
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
Ejemplo n.º 24
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) )
Ejemplo n.º 25
0
    def test_load_results_from_run_context(self):
        with ErtTestContext("manual_load_test", self.config_file) as test_context:
            ert = test_context.getErt()
            load_into_case = "A1"
            load_from_case = "default"

            load_into = ert.getEnkfFsManager().getFileSystem(load_into_case)
            load_from = ert.getEnkfFsManager().getFileSystem(load_from_case)

            ert.getEnkfFsManager().switchFileSystem(load_from)
            realisations = BoolVector(default_value=True, initial_size=25)
            realisations[7] = False
            iteration = 0

            run_context = ert.getRunContextENSEMPLE_EXPERIMENT(load_into,
                                                               realisations)

            loaded = ert.loadFromRunContext(run_context, load_into)

            load_into_case_state_map = load_into.getStateMap()
            load_into_states = [state for state in load_into_case_state_map]

            expected = [RealizationStateEnum.STATE_HAS_DATA] * 25
            expected[7] = RealizationStateEnum.STATE_UNDEFINED

            self.assertListEqual(load_into_states, expected)
            self.assertEqual(24, loaded)
            self.assertEqual(25, len(expected))
            self.assertEqual(25, len(realisations))
Ejemplo n.º 26
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])
Ejemplo n.º 27
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( )
            jobname_fmt = model_config.getJobnameFormat( )
            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, jobname_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)
Ejemplo n.º 28
0
    def testObs(self):
        with ErtTestContext("obs_test", self.config_file) as test_context:
            ert = test_context.getErt()
            obs = ert.getObservations()

            self.assertEqual(32, len(obs))
            for v in obs:
                self.assertTrue(isinstance(v, ObsVector))

            self.assertEqual(obs[-1].getKey(), "RFT_TEST")
            self.assertEqual(obs[-1].getDataKey(), "4289383")
            self.assertEqual(obs[-1].getObsKey(), "RFT_TEST")

            with self.assertRaises(IndexError):
                v = obs[-40]
            with self.assertRaises(IndexError):
                v = obs[40]

            with self.assertRaises(KeyError):
                v = obs["No-this-does-not-exist"]

            v1 = obs["WWCT:OP_3"]
            v2 = obs["GOPT:OP"]
            mask = BoolVector(True, ert.getEnsembleSize())
            current_fs = ert.getEnkfFsManager().getCurrentFileSystem()

            self.assertTrue(v1.hasData(mask, current_fs))
            self.assertFalse(v2.hasData(mask, current_fs))

            local_node = v1.createLocalObs()
            for t in v1.getStepList():
                self.assertTrue(local_node.tstepActive(t))
Ejemplo n.º 29
0
 def test_without_gen_kw(self):
     case_directory = self.createTestPath("local/snake_oil_no_data/")
     with TestAreaContext("test_enkf_runpath",
                          store_area=False) as work_area:
         work_area.copy_directory(case_directory)
         res_config = ResConfig("snake_oil_no_data/snake_oil_no_gen_kw.ert")
         main = EnKFMain(res_config)
         iactive = BoolVector(initial_size=main.getEnsembleSize(),
                              default_value=False)
         iactive[0] = True
         fs = main.getEnkfFsManager().getCurrentFileSystem()
         run_context = main.getRunContextENSEMPLE_EXPERIMENT(fs, iactive)
         main.createRunpath(run_context)
         self.assertDirectoryExists(
             "snake_oil_no_data/storage/snake_oil_no_gen_kw/runpath/realisation-0/iter-0"
         )
         self.assertFileDoesNotExist(
             "snake_oil_no_data/storage/snake_oil_no_gen_kw/runpath/realisation-0/iter-0/parameters.txt"
         )
         self.assertEqual(
             len(
                 os.listdir(
                     "snake_oil_no_data/storage/snake_oil_no_gen_kw/runpath"
                 )),
             1,
         )
         self.assertEqual(
             len(
                 os.listdir(
                     "snake_oil_no_data/storage/snake_oil_no_gen_kw/runpath/realisation-0"
                 )),
             1,
         )
Ejemplo n.º 30
0
    def calculatePrincipalComponent(self,
                                    fs,
                                    local_obsdata,
                                    truncation_or_ncomp=3):
        pc = Matrix(1, 1)
        pc_obs = Matrix(1, 1)
        singular_values = DoubleVector()

        state_map = fs.getStateMap()
        ens_mask = BoolVector(False, self.ert().getEnsembleSize())
        state_map.selectMatching(ens_mask, RealizationStateEnum.STATE_HAS_DATA)
        active_list = ens_mask.createActiveList()

        if len(ens_mask) > 0:
            meas_data = MeasData(ens_mask)
            obs_data = ObsData()

            self.ert().getObservations().getObservationAndMeasureData(
                fs, local_obsdata, active_list, meas_data, obs_data)

            meas_data.deactivateZeroStdSamples(obs_data)

            active_size = len(obs_data)

            if active_size > 0:
                S = meas_data.createS()
                D_obs = obs_data.createDObs()

                truncation, ncomp = self.truncationOrNumberOfComponents(
                    truncation_or_ncomp)

                obs_data.scale(S, D_obs=D_obs)
                EnkfLinalg.calculatePrincipalComponents(
                    S, D_obs, truncation, ncomp, pc, pc_obs, singular_values)
                if self.__prior_singular_values is None:
                    self.__prior_singular_values = singular_values
                else:
                    for row in range(pc.rows()):
                        factor = singular_values[
                            row] / self.__prior_singular_values[row]
                        pc.scaleRow(row, factor)
                        pc_obs.scaleRow(row, factor)

                return PcaPlotData(local_obsdata.getName(), pc, pc_obs,
                                   singular_values)
        return None
Ejemplo n.º 31
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])
Ejemplo n.º 32
0
    def load(self, args):
        arguments = splitArguments(args)

        if len(arguments) < 1:
            self.lastCommandFailed("Loading requires a realization mask.")
            return False

        realization_count = self.ert().getEnsembleSize()

        mask = BoolVector(False, realization_count)
        mask_success = mask.updateActiveMask(arguments[0])

        if not mask_success:
            self.lastCommandFailed("The realization mask: '%s' is not valid." % arguments[0])
            return False

        fs = self.ert().getEnkfFsManager().getCurrentFileSystem()
        self.ert().loadFromForwardModel(mask, 0, fs)
Ejemplo n.º 33
0
 def getRunContextENSEMPLE_EXPERIMENT(self,
                                      fs,
                                      iactive: List[bool],
                                      iteration: int = 0):
     true_indices = [idx for idx, value in enumerate(iactive) if value]
     bool_vector = BoolVector.createFromList(size=len(iactive),
                                             source_list=true_indices)
     return self._alloc_run_context_ENSEMBLE_EXPERIMENT(
         fs, bool_vector, iteration)
Ejemplo n.º 34
0
    def test_update_active_mask(self):
        vec = BoolVector(False, 10)

        self.assertTrue(vec.updateActiveMask("1-2,5"))
        self.assertTrue(vec[1])
        self.assertTrue(vec[2])
        self.assertTrue(vec[5])
        self.assertFalse(vec[4])

        vec = BoolVector(False, 10)

        self.assertTrue(vec.updateActiveMask("1-5,2,3"))
        self.assertTrue(vec[1])
        self.assertTrue(vec[2])
        self.assertTrue(vec[3])
        self.assertTrue(vec[4])
        self.assertTrue(vec[5])
        self.assertFalse(vec[0])
        self.assertFalse(vec[6])

        vec = BoolVector(False, 10)

        self.assertTrue(vec.updateActiveMask("5,6,7,15"))
        self.assertTrue(vec[5])
        self.assertTrue(vec[6])
        self.assertTrue(vec[7])
        self.assertFalse(vec[4])
        self.assertFalse(vec[8])
        self.assertEqual(len(vec), 16)