Example #1
0
    def test_vector_operations_with_exceptions(self):
        iv1 = IntVector()
        iv1.append(1)
        iv1.append(2)
        iv1.append(3)

        iv2 = IntVector()
        iv2.append(4)
        iv2.append(5)

        dv1 = DoubleVector()
        dv1.append(0.5)
        dv1.append(0.75)
        dv1.append(0.25)

        # Size mismatch
        with self.assertRaises(ValueError):
            iv3 = iv1 + iv2

        # Size mismatch
        with self.assertRaises(ValueError):
            iv3 = iv1 * iv2

        # Type mismatch
        with self.assertRaises(TypeError):
            iv1 += dv1

        # Type mismatch
        with self.assertRaises(TypeError):
            iv1 *= dv1
Example #2
0
File: export.py Project: pgdr/ert
    def exportFIELD(self, line):
        arguments = splitArguments(line)

        if len(arguments) >= 1:
            ens_config = self.ert().ensembleConfig()
            key = arguments[0]
            if key in self.supportedFIELDKeys():
                config_node = ens_config[key]
                if len(arguments) >= 2:
                    path_fmt = arguments[1]
                else:
                    path_fmt = Export.DEFAULT_EXPORT_PATH % (key, key) + ".grdecl"

                if len(arguments) >= 3:
                    range_string = "".join(arguments[2:])
                    iens_list = IntVector.active_list(range_string)
                else:
                    ens_size = self.ert().getEnsembleSize()
                    iens_list = IntVector.createRange(0, ens_size, 1)

                fs_manager = self.ert().getEnkfFsManager()
                fs = fs_manager.getCurrentFileSystem()
                mc = self.ert().getModelConfig()
                init_file = config_node.getInitFile(mc.getRunpathFormat())
                if init_file:
                    print('Using init file: %s' % init_file)

                EnkfNode.exportMany(config_node, path_fmt, fs, iens_list, arg=init_file)
            else:
                self.lastCommandFailed("No such FIELD node: %s" % key)
        else:
            self.lastCommandFailed("Expected at least one argument: <keyword> received: '%s'" % line)
Example #3
0
    def test_grdecl_load(self):
        with self.assertRaises(IOError):
            grid = EclGrid.loadFromGrdecl("/file/does/not/exists")

        with TestAreaContext("python/grid-test/grdeclLoad"):
            with open("grid.grdecl","w") as f:
                f.write("Hei ...")
                
            with self.assertRaises(ValueError):
                grid = EclGrid.loadFromGrdecl("grid.grdecl")
        
            actnum = IntVector(default_value = 1 , initial_size = 1000)
            actnum[0] = 0
            g1 = EclGrid.createRectangular((10,10,10) , (1,1,1) , actnum = actnum )
            self.assertEqual( g1.getNumActive() , actnum.elementSum() )
            g1.save_EGRID("G.EGRID")

            with openEclFile("G.EGRID") as f:
                with open("grid.grdecl" , "w") as f2:
                    f2.write("SPECGRID\n")
                    f2.write("  10  10  10  \'F\' /\n")

                    coord_kw = f["COORD"][0]
                    coord_kw.write_grdecl( f2 )
                    
                    zcorn_kw = f["ZCORN"][0]
                    zcorn_kw.write_grdecl( f2 )
                
                    actnum_kw = f["ACTNUM"][0]
                    actnum_kw.write_grdecl( f2 )
            
            g2 = EclGrid.loadFromGrdecl("grid.grdecl")
            self.assertTrue( g1.equal( g2 ))
Example #4
0
    def test_asList(self):
        v = IntVector()
        v[0] = 100
        v[1] = 10
        v[2] = 1

        l = v.asList()
        self.assertListEqual(l, [100, 10, 1])
Example #5
0
    def test_equal(self):
        v1 = IntVector()
        v1[3] = 99

        v2 = IntVector()
        self.assertNotEqual(v1, v2)
        v2[3] = 99
        self.assertEqual(v1, v2)
Example #6
0
    def test_element_sum(self):
        dv = DoubleVector()
        iv = IntVector()
        for i in range(10):
            dv.append(i + 1)
            iv.append(i + 1)

        self.assertEqual(dv.elementSum(), 55)
        self.assertEqual(iv.elementSum(), 55)
Example #7
0
    def create_gen_data(cls, key, file_fmt, report_steps=(1, )):
        active_steps = IntVector()
        for step in report_steps:
            active_steps.append(step)

        config_node = cls._alloc_gen_data_everest(key, file_fmt, active_steps)
        if config_node is None:
            raise ValueError("Failed to create GEN_DATA node for:%s" % key)

        return config_node
Example #8
0
 def report_index_list(self):
     """
     Internal function for working with report_steps.
     """
     first_report = self.first_report
     last_report = self.last_report
     index_list = IntVector()
     for report_step in range(first_report, last_report + 1):
         time_index = self._get_report_end(report_step)
         index_list.append(time_index)
     return index_list
Example #9
0
 def cells_equal(self, value):
     """
     Will return a list [(i1,j1),(i2,j2), ...(in,jn)] of all cells with value @value.
     """
     i_list = IntVector()
     j_list = IntVector()
     self._cells_equal(value, i_list, j_list)
     ij_list= []
     for (i,j) in zip(i_list, j_list):
         ij_list.append((i,j))
     return ij_list
Example #10
0
 def test_create(self):
     with self.assertRaises(ValueError):
         grid = GridGen.createRectangular( (10,20,30) , (1,1,1) , actnum = [0,1,1,2])
         
     with self.assertRaises(ValueError):
         grid = GridGen.createRectangular( (10,20,30) , (1,1,1) , actnum = IntVector(initial_size = 10))
 
     actnum = IntVector(default_value = 1 , initial_size = 6000)
     actnum[0] = 0
     actnum[1] = 0
     grid = GridGen.createRectangular( (10,20,30) , (1,1,1) , actnum = actnum)
     self.assertEqual( grid.getNumActive( ) , 30*20*10 - 2)
Example #11
0
    def test_count_equal(self):
        v = IntVector(default_value=77)
        v[0] = 1
        v[10] = 1
        v[20] = 1
        self.assertEqual(v.countEqual(1), 3)

        v = DoubleVector(default_value=77)
        v[0] = 1
        v[10] = 1
        v[20] = 1
        self.assertEqual(v.countEqual(1), 3)
Example #12
0
    def test_activeList(self):
        active_list = IntVector.active_list("1,10,100-105")
        self.assertTrue(len(active_list) == 8)
        self.assertTrue(active_list[0] == 1)
        self.assertTrue(active_list[2] == 100)
        self.assertTrue(active_list[7] == 105)
        self.assertEqual(active_list.count(100), 1)
        active_list.append(100)
        active_list.append(100)
        self.assertEqual(active_list.count(100), 3)

        active_list = IntVector.active_list("1,10,100-105X")
        self.assertFalse(active_list)
Example #13
0
    def test_range(self):
        v = IntVector()
        v[10] = 99

        with self.assertRaises(ValueError):
            v.initRange(1, 2, 0)

        self.range_test(v, 0, 5, 1)
        self.range_test(v, 0, 100, 3)
        self.range_test(v, 0, 100, -3)

        self.create_range_test(v, 0, 5, 1)
        self.create_range_test(v, 0, 100, 3)
        self.create_range_test(v, 0, 100, -3)
Example #14
0
    def test_shift(self):
        a = IntVector()
        a.append(1)
        a.append(2)
        a.append(3)
        a.append(4)
        a.append(5)

        with self.assertRaises(ValueError):
            a >> -1

        with self.assertRaises(ValueError):
            a << -1

        with self.assertRaises(ValueError):
            a << -6

        b = a << 2
        self.assertEqual(list(b), [3, 4, 5])

        print(a)
        a <<= 2
        print(a)
        self.assertEqual(list(a), [3, 4, 5])

        b = a >> 2
        self.assertEqual(list(b), [0, 0, 3, 4, 5])

        a >>= 2
        self.assertEqual(list(a), [0, 0, 3, 4, 5])
Example #15
0
    def test_init_linear(self):
        with self.assertRaises(ValueError):
            v = IntVector.create_linear(0, 10, 1)

        v = IntVector.create_linear(0, 10, 11)
        for i in range(len(v)):
            self.assertEqual(v[i], i)

        v = IntVector.create_linear(10, 0, 11)
        for i in range(len(v)):
            self.assertEqual(v[i], 10 - i)

        d = DoubleVector.create_linear(0, 1, 11)
        for i in range(len(d)):
            self.assertEqual(d[i], i * 0.10)
Example #16
0
    def create_range_test(self, v, a, b, d):
        v = IntVector.createRange(a, b, d)
        r = range(a, b, d)

        self.assertEqual(len(v), len(r))
        for a, b in zip(v, r):
            self.assertEqual(a, b)
Example #17
0
 def test_true(self):
     iv = IntVector()
     self.assertFalse(
         iv
     )  # Will invoke the __len__ function; could override with __nonzero__
     iv[0] = 1
     self.assertTrue(iv)
Example #18
0
    def test_truth_and_size(self):
        actnum = IntVector(initial_size=100, default_value=0)
        actnum[0:50] = 1
        grid = EclGrid.createRectangular((10, 10, 1), (1, 1, 1), actnum=actnum)
        region = EclRegion(grid, False)

        self.assertFalse(region)
        self.assertEqual(0, region.active_size())
        self.assertEqual(0, region.global_size())

        region.select_all()
        self.assertTrue(region)
        self.assertEqual(50, region.active_size())
        self.assertEqual(100, region.global_size())

        region.deselect_all()
        self.assertFalse(region)
        self.assertEqual(0, region.active_size())
        self.assertEqual(0, region.global_size())

        region = EclRegion(grid, False)
        region.select_inactive()
        self.assertTrue(region)
        self.assertEqual(0, region.active_size())
        self.assertEqual(50, region.global_size())
Example #19
0
    def create_rectangular(cls, dims, dV, actnum=None):
        """
        Will create a new rectangular grid. @dims = (nx,ny,nz)  @dVg = (dx,dy,dz)

        With the default value @actnum == None all cells will be active,
        """
        if actnum is None:
            ecl_grid = cls._alloc_rectangular(dims[0], dims[1], dims[2], dV[0],
                                              dV[1], dV[2], None)
        else:
            if not isinstance(actnum, IntVector):
                tmp = IntVector(initial_size=len(actnum))
                for (index, value) in enumerate(actnum):
                    tmp[index] = value
                actnum = tmp

            if not len(actnum) == dims[0] * dims[1] * dims[2]:
                raise ValueError(
                    "ACTNUM size mismatch: len(ACTNUM):%d  Expected:%d" %
                    (len(actnum), dims[0] * dims[1] * dims[2]))

            ecl_grid = cls._alloc_rectangular(dims[0], dims[1], dims[2],
                                              dV[0], dV[1], dV[2],
                                              actnum.getDataPtr())

        # If we have not succeeded in creatin the grid we *assume* the
        # error is due to a failed malloc.
        if ecl_grid is None:
            raise MemoryError("Failed to allocated regualar grid")

        return ecl_grid
Example #20
0
    def test_setitem(self):
        actnum = IntVector(default_value=1, initial_size=1000)
        for i in range(100):
            actnum[i] = 0

        grid = EclGrid.createRectangular((10, 10, 10), (1, 1, 1),
                                         actnum=actnum)
        kw = Ecl3DKW("KW", grid, EclDataType.ECL_FLOAT, default_value=77)

        with self.assertRaises(IndexError):
            kw[1000]

        with self.assertRaises(IndexError):
            kw[0, 10, 100]

        with self.assertRaises(ValueError):
            kw[1, 1]

        with self.assertRaises(ValueError):
            kw[1, 1, 1, 1]

        kw.assign(99)
        self.assertEqual(kw[0, 0, 0], 77)
        self.assertEqual(kw[0, 0, 1], 99)

        with self.assertRaises(ValueError):
            kw[0, 0, 0] = 88

        kw[0, 0, 1] = 100
        self.assertEqual(kw[0, 0, 1], 100)
Example #21
0
    def test_cast(self):
        actnum = IntVector(default_value=1, initial_size=1000)
        for i in range(100):
            actnum[i] = 0

        grid = EclGrid.createRectangular((10, 10, 10), (1, 1, 1),
                                         actnum=actnum)
        kw_wrong_size = EclKW("KW", 27, EclDataType.ECL_FLOAT)
        kw_global_size = EclKW("KW", grid.getGlobalSize(),
                               EclDataType.ECL_FLOAT)
        kw_active_size = EclKW("KW", grid.getNumActive(),
                               EclDataType.ECL_FLOAT)

        with self.assertRaises(ValueError):
            Ecl3DKW.castFromKW(kw_wrong_size, grid)

        Ecl3DKW.castFromKW(kw_global_size, grid)
        self.assertTrue(isinstance(kw_global_size, Ecl3DKW))

        Ecl3DKW.castFromKW(kw_active_size, grid, default_value=66)
        self.assertTrue(isinstance(kw_active_size, Ecl3DKW))

        self.assertEqual(kw_active_size[0, 0, 0], 66)
        with self.assertRaises(ValueError):
            kw_active_size[0, 0, 0] = 88
Example #22
0
    def test_div(self):
        v = IntVector()
        v[0] = 100
        v[1] = 10
        v[2] = 1
        v /= 10

        self.assertEqual(list(v), [10, 1, 0])
Example #23
0
    def test_obs_block_scale_std(self):
        with ErtTestContext("obs_test_scale",
                            self.config_file) as test_context:
            ert = test_context.getErt()
            fs = ert.getEnkfFsManager().getCurrentFileSystem()
            active_list = IntVector()
            active_list.initRange(0, ert.getEnsembleSize(), 1)

            obs = ert.getObservations()
            obs_data = LocalObsdata("OBSxx", obs)
            obs_vector = obs["WWCT:OP_1"]
            obs_data.addObsVector(obs_vector)
            scale_factor = obs.scaleCorrelatedStd(fs, obs_data, active_list)

            for obs_node in obs_vector:
                for index in range(len(obs_node)):
                    self.assertEqual(scale_factor,
                                     obs_node.getStdScaling(index))
Example #24
0
    def test_pop(self):
        a = IntVector()
        a.append(1)
        a.append(2)

        self.assertEqual(a.pop(), 2)
        self.assertEqual(len(a), 1)
        self.assertEqual(a.pop(), 1)
        self.assertEqual(len(a), 0)
        with self.assertRaises(ValueError):
            a.pop()
Example #25
0
    def test_create( self ):
        actnum = IntVector(default_value = 1 , initial_size = 1000)
        for i in range(100):
            actnum[i] = 0

        grid = EclGrid.createRectangular( (10,10,10) , (1,1,1) , actnum = actnum)
        kw = Ecl3DKW( "KW" , grid , EclDataType.ECL_FLOAT )
        self.assertEqual( len(kw) , grid.getNumActive())

        self.assertEqual( (10,10,10) , kw.dims() )
Example #26
0
    def getEdgePolygon(self):
        x_list = DoubleVector()
        y_list = DoubleVector()
        cell_list = IntVector()

        self._trace_edge(x_list, y_list, cell_list)
        p = Polyline()
        for (x, y) in zip(x_list, y_list):
            p.addPoint(x, y)
        return p
Example #27
0
    def test_true_false(self):
        v = IntVector(default_value=77)
        self.assertFalse(v)
        v[10] = 77
        self.assertTrue(v)

        v = DoubleVector(default_value=77)
        self.assertFalse(v)
        v[10] = 77
        self.assertTrue(v)
Example #28
0
    def test_contains_int(self):
        iv = IntVector()
        iv[0] = 1
        iv[1] = 10
        iv[2] = 100
        iv[3] = 1000

        self.assertTrue(1 in iv)
        self.assertTrue(10 in iv)
        self.assertTrue(88 not in iv)
        self.assertTrue(99 not in iv)
Example #29
0
    def test_field_export_many(self):
        with ErtTestContext("export_test", self.config_file) as test_context:
            ert = test_context.getErt()
            fs_manager = ert.getEnkfFsManager()
            ens_config = ert.ensembleConfig()
            config_node = ens_config["PERMX"]
            iens_list = IntVector()
            iens_list.append(0)
            iens_list.append(2)
            iens_list.append(4)

            fs = fs_manager.getCurrentFileSystem()

            # Filename without embedded %d - TypeError
            with self.assertRaises(TypeError):
                EnkfNode.exportMany(config_node,
                                    "export/with/path/PERMX.grdecl", fs,
                                    iens_list)

            EnkfNode.exportMany(config_node,
                                "export/with/path/PERMX_%d.grdecl", fs,
                                iens_list)
            self.assertTrue(os.path.isfile("export/with/path/PERMX_0.grdecl"))
            self.assertTrue(os.path.isfile("export/with/path/PERMX_2.grdecl"))
            self.assertTrue(os.path.isfile("export/with/path/PERMX_4.grdecl"))
Example #30
0
    def test_create_global_size( self ):
        actnum = IntVector(default_value = 1 , initial_size = 1000)
        for i in range(100):
            actnum[i] = 0

        grid = EclGrid.createRectangular( (10,10,10) , (1,1,1) , actnum = actnum)
        kw = Ecl3DKW( "KW" , grid , EclDataType.ECL_FLOAT , global_active = True)
        self.assertEqual( len(kw) , grid.getGlobalSize())

        kw.assign(50)
        self.assertEqual( kw[0,0,0] , 50 )

        kw[0,0,0] = 45
        self.assertEqual( kw[0,0,0] , 45 )