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
def cellsEqual(self, value): """ Will return a list [(i1,j1),(i2,j2) , ...(in,jn)] of all cells with value @value. """ i_list = IntVector() j_list = IntVector() Layer.cNamespace().cells_equal(self, value, i_list, j_list) ij_list = [] for (i, j) in zip(i_list, j_list): ij_list.append((i, j)) return ij_list
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)
def test_region_filter(self): nx = 10 ny = 10 nz = 1 actnum = IntVector( initial_size = nx*ny*nz , default_value = 1 ) actnum[nx*ny - 1] = 0 grid = EclGrid.createRectangular( (nx,ny,nz) , (1,1,1) , actnum = actnum) self.assertEqual( grid.getNumActive() , nx*ny*nz - 1 ) kw = Ecl3DKW.create( "REGIONS" , grid , EclTypeEnum.ECL_INT_TYPE , global_active = True ) kw.assign( 0 ) kw[0:nx*ny/2] = 1 kw[5,2,0] = 0 kw[0,9,0] = 2 kw.fixUninitialized( grid ) # Not assigned because they are in contact with a '2'; these # two are problem cells. self.assertEqual( kw[0,ny - 2,0] , 0) self.assertEqual( kw[1,ny - 1,0] , 0) # Not assigned because it is inactive self.assertEqual( kw[nx - 1,ny - 1,0] , 0) self.assertEqual( kw[5,2,0] , 1 ) for j in range(5,10): self.assertEqual( kw[5,j,0] , 1 ) for i in range(10): self.assertEqual( kw[i,7,0] , 1 )
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
def createRectangular(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, """ obj = object.__new__(cls) if actnum is None: c_ptr = cfunc.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])) c_ptr = cfunc.alloc_rectangular(dims[0], dims[1], dims[2], dV[0], dV[1], dV[2], actnum.getDataPtr()) obj.init_cobj(c_ptr, cfunc.free) return obj
def createRectangular(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
def test_true(self): iv = IntVector() self.assertFalse( iv ) # Will invoke the __len__ function; could override with __nonzero__ iv[0] = 1 self.assertTrue(iv)
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])
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 ))
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)
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"))
def test_asList(self): v = IntVector() v[0] = 100 v[1] = 10 v[2] = 1 l = v.asList() self.assertListEqual(l, [100, 10, 1])
def test_div(self): v = IntVector() v[0] = 100 v[1] = 10 v[2] = 1 v /= 10 self.assertEqual(list(v), [10, 1, 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)
def test_ecl_file_indexed_read(self): with TestAreaContext("ecl_file_indexed_read") as area: fortio = FortIO("ecl_file_index_test", mode=FortIO.WRITE_MODE) element_count = 100000 ecl_kw_1 = EclKW("TEST1", element_count, EclDataType.ECL_INT) ecl_kw_2 = EclKW("TEST2", element_count, EclDataType.ECL_INT) for index in range(element_count): ecl_kw_1[index] = index ecl_kw_2[index] = index + 3 ecl_kw_1.fwrite(fortio) ecl_kw_2.fwrite(fortio) fortio.close() ecl_file = EclFile("ecl_file_index_test") index_map = IntVector() index_map.append(2) index_map.append(3) index_map.append(5) index_map.append(7) index_map.append(11) index_map.append(13) index_map.append(313) index_map.append(1867) index_map.append(5227) index_map.append(7159) index_map.append(12689) index_map.append(18719) index_map.append(32321) index_map.append(37879) index_map.append(54167) index_map.append(77213) index_map.append(88843) index_map.append(99991) char_buffer_1 = ctypes.create_string_buffer( len(index_map) * ctypes.sizeof(ctypes.c_int)) char_buffer_2 = ctypes.create_string_buffer( len(index_map) * ctypes.sizeof(ctypes.c_int)) self._eclFileIndexedRead(ecl_file, "TEST2", 0, index_map, char_buffer_2) self._eclFileIndexedRead(ecl_file, "TEST1", 0, index_map, char_buffer_1) int_buffer_1 = ctypes.cast(char_buffer_1, ctypes.POINTER(ctypes.c_int)) int_buffer_2 = ctypes.cast(char_buffer_2, ctypes.POINTER(ctypes.c_int)) for index, index_map_value in enumerate(index_map): self.assertEqual(index_map_value, int_buffer_1[index]) self.assertEqual(index_map_value, int_buffer_2[index] - 3)
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)
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)
def getEdgePolygon(self): x_list = DoubleVector() y_list = DoubleVector() cell_list = IntVector() self.cNamespace().trace_edge(self, x_list, y_list, cell_list) p = Polyline() for (x, y) in zip(x_list, y_list): p.addPoint(x, y) return p
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 , EclTypeEnum.ECL_FLOAT_TYPE ) self.assertEqual( len(kw) , grid.getNumActive()) self.assertEqual( (10,10,10) , kw.dims() )
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)
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
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()
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)
def test_ecl_kw_indexed_read(self): with TestAreaContext("ecl_kw_indexed_read") as area: fortio = FortIO("index_test", mode=FortIO.WRITE_MODE) element_count = 100000 ecl_kw = EclKW.create("TEST", element_count, EclTypeEnum.ECL_INT_TYPE) for index in range(element_count): ecl_kw[index] = index ecl_kw.fwrite(fortio) fortio.close() fortio = FortIO("index_test", mode=FortIO.READ_MODE) new_ecl_kw = EclKW.fread(fortio) for index in range(element_count): self.assertEqual(new_ecl_kw[index], index) index_map = IntVector() index_map.append(2) index_map.append(3) index_map.append(5) index_map.append(7) index_map.append(11) index_map.append(13) index_map.append(313) index_map.append(1867) index_map.append(5227) index_map.append(7159) index_map.append(12689) index_map.append(18719) index_map.append(32321) index_map.append(37879) index_map.append(54167) index_map.append(77213) index_map.append(88843) index_map.append(99991) char_buffer = ctypes.create_string_buffer( len(index_map) * ctypes.sizeof(ctypes.c_int)) freadIndexedData(fortio, 24, EclTypeEnum.ECL_INT_TYPE, element_count, index_map, char_buffer) int_buffer = ctypes.cast(char_buffer, ctypes.POINTER(ctypes.c_int)) for index, index_map_value in enumerate(index_map): self.assertEqual(index_map_value, int_buffer[index])
def getNeighbours(self): """ Will return a list of FaultBlock instances which are in direct contact with this block. """ neighbour_id_list = IntVector() self.cNamespace().get_neighbours( self , neighbour_id_list ) parent_layer = self.getParentLayer() neighbour_list = [] for id in neighbour_id_list: neighbour_list.append( parent_layer.getBlock( id )) return neighbour_list
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 , EclTypeEnum.ECL_FLOAT_TYPE , 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 )
def getNeighbours(self, polylines=None, connected_only=True): """ Will return a list of FaultBlock instances which are in direct contact with this block. """ neighbour_id_list = IntVector() if polylines is None: polylines = CPolylineCollection() self._get_neighbours(connected_only, polylines, neighbour_id_list) parent_layer = self.getParentLayer() neighbour_list = [] for id in neighbour_id_list: neighbour_list.append(parent_layer.getBlock(id)) return neighbour_list
def test_compressed_copy(self): actnum = IntVector(default_value = 1 , initial_size = 1000) for i in range(500): actnum[2*i + 1] = 0 grid = EclGrid.createRectangular( (10,10,10) , (1,1,1) , actnum = actnum) kw = Ecl3DKW( "KW" , grid , EclTypeEnum.ECL_INT_TYPE , global_active = True) for i in range(len(kw)): kw[i] = i kw_copy = kw.compressedCopy() self.assertTrue( isinstance( kw_copy , EclKW ) ) self.assertEqual(len(kw_copy) , 500) for i in range(len(kw_copy)): self.assertEqual(kw_copy[i] , 2*i)
def test_unique(self): iv = IntVector() iv.append(1) iv.append(1) iv.append(1) iv.append(0) iv.append(1) iv.append(2) iv.append(2) iv.append(0) iv.append(3) iv.selectUnique() self.assertEqual(len(iv), 4) self.assertEqual(iv[0], 0) self.assertEqual(iv[1], 1) self.assertEqual(iv[2], 2) self.assertEqual(iv[3], 3)