def test_long_name(self): with self.assertRaises(ValueError): EclKW("LONGLONGNAME", 10, EclDataType.ECL_INT) kw = EclKW("REGIONS", 10, EclDataType.ECL_INT) with self.assertRaises(ValueError): kw.name = "LONGLONGNAME"
def test_sliced_set(self): kw = EclKW("REGIONS", 10, EclDataType.ECL_INT) kw.assign(99) kw[0:5] = 66 self.assertEqual(kw[0], 66) self.assertEqual(kw[4], 66) self.assertEqual(kw[5], 99)
def test_ecl_file_block(self): with TestAreaContext("name") as t: kw = EclKW("TEST", 3, EclDataType.ECL_INT) with openFortIO("TEST", mode=FortIO.WRITE_MODE) as f: kw.fwrite(f) t.sync() f = EclFile("TEST") with self.assertRaises(NotImplementedError): f.select_block("KW", 100) with self.assertRaises(NotImplementedError): f.select_global() with self.assertRaises(NotImplementedError): f.select_restart_section(index=None, report_step=None, sim_time=None) with self.assertRaises(NotImplementedError): f.select_restart_section() with self.assertRaises(NotImplementedError): EclFile.restart_block("TEST")
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 test_name(self): kw = EclKW('TEST', 3, EclDataType.ECL_INT) self.assertEqual(kw.name, 'TEST') self.assertIn('TEST', repr(kw)) kw.name = 'SCHMEST' self.assertEqual(kw.name, 'SCHMEST') self.assertIn('SCHMEST', repr(kw))
def test_EclFile_name_property(self): with TestAreaContext("name") as t: kw = EclKW("TEST", 3, EclDataType.ECL_INT) with openFortIO("TEST", mode=FortIO.WRITE_MODE) as f: kw.fwrite(f) t.sync() f = EclFile("TEST")
def test_string_padding(self): kw = EclKW("TEST_KW", 1, EclDataType.ECL_STRING(4)) kw[0] = "AB" self.assertEqual(kw[0], "AB ") kw = EclKW("TEST_KW", 1, EclDataType.ECL_CHAR) kw[0] = "ABCD" self.assertEqual(kw[0], "ABCD ")
def test_min_max(self): kw = EclKW("TEST", 3, EclDataType.ECL_INT) kw[0] = 10 kw[1] = 5 kw[2] = 0 self.assertEqual(10, kw.getMax()) self.assertEqual(0 , kw.getMin()) self.assertEqual((0,10) , kw.getMinMax())
def create_init(grid, case): poro = EclKW("PORO", grid.getNumActive(), EclDataType.ECL_FLOAT) porv = poro.copy() porv.setName("PORV") for g in range(grid.getGlobalSize()): porv[g] *= grid.cell_volume(global_index=g) with openFortIO("%s.INIT" % case, mode=FortIO.WRITE_MODE) as f: poro.fwrite(f) porv.fwrite(f)
def test_fault_block_edge(self): grid = EclGrid.createRectangular((5, 5, 1), (1, 1, 1)) kw = EclKW("FAULTBLK", grid.getGlobalSize(), EclDataType.ECL_INT) kw.assign(0) for j in range(1, 4): for i in range(1, 4): g = i + j * grid.getNX() kw[g] = 1 layer = FaultBlockLayer(grid, 0)
def globalKWCopy(self, kw , default_value): if len(kw) == self.getGlobalSize( ): return kw.copy( ) elif len(kw) == self.getNumActive(): kw_copy = EclKW( kw.getName() , self.getGlobalSize() , kw.data_type) kw_copy.assign( default_value ) self._global_kw_copy( kw_copy , kw) return kw_copy else: raise ValueError("The input keyword must have nx*n*nz or nactive elements. Size:%d invalid" % len(kw))
def test_create(self): # The init file created here only contains a PORO field. More # properties must be added to this before it can be used for # any usefull gravity calculations. poro = EclKW("PORO", self.grid.getGlobalSize(), EclDataType.ECL_FLOAT) with TestAreaContext("grav_init"): with openFortIO("TEST.INIT", mode=FortIO.WRITE_MODE) as f: poro.fwrite(f) self.init = EclFile("TEST.INIT") grav = EclGrav(self.grid, self.init)
def test_fseek(self): file = open(self.src_file, "r") self.assertTrue(EclKW.fseek_grdecl(file, "PERMX")) self.assertFalse(EclKW.fseek_grdecl(file, "PERMY")) file.close() file = open(self.src_file, "r") kw1 = EclKW.read_grdecl(file, "PERMX") self.assertFalse(EclKW.fseek_grdecl(file, "PERMX")) self.assertTrue(EclKW.fseek_grdecl(file, "PERMX", rewind=True)) file.close()
def test_fault_block_layer_export(self): layer = FaultBlockLayer(self.grid, 1) kw1 = EclKW("FAULTBLK", self.grid.getGlobalSize() + 1, EclDataType.ECL_INT) with self.assertRaises(ValueError): layer.exportKeyword(kw1) kw2 = EclKW("FAULTBLK", self.grid.getGlobalSize(), EclDataType.ECL_FLOAT) with self.assertRaises(TypeError): layer.exportKeyword(kw2)
def create(self, filename, load_actnum=True): fileH = open(filename, "r") specgrid = EclKW.read_grdecl(fileH, "SPECGRID", ecl_type=EclDataType.ECL_INT, strict=False) zcorn = EclKW.read_grdecl(fileH, "ZCORN") coord = EclKW.read_grdecl(fileH, "COORD") if load_actnum: actnum = EclKW.read_grdecl(fileH, "ACTNUM", ecl_type=EclDataType.ECL_INT) else: actnum = None mapaxes = EclKW.read_grdecl(fileH, "MAPAXES") grid = EclGrid.create(specgrid, zcorn, coord, actnum, mapaxes=mapaxes) return grid
def setUp(self): self.grid = EclGrid.createRectangular((10, 10, 10), (1, 1, 1)) self.kw = EclKW("FAULTBLK", self.grid.getGlobalSize(), EclDataType.ECL_INT) self.kw.assign(1) reg = EclRegion(self.grid, False) for k in range(self.grid.getNZ()): reg.clear() reg.select_kslice(k, k) self.kw.assign(k, mask=reg) self.kw[k * self.grid.getNX() * self.grid.getNY() + 7] = 177
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("TEST", element_count, EclDataType.ECL_INT) 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)) self._freadIndexedData(fortio, 24, EclDataType.ECL_INT, 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 test_equal(self): grid = EclGrid.createRectangular((10, 10, 1), (1, 1, 1)) kw_int = EclKW("INT", grid.getGlobalSize(), EclDataType.ECL_INT) kw_float = EclKW("FLOAT", grid.getGlobalSize(), EclDataType.ECL_FLOAT) kw_int[0:49] = 1 region = EclRegion(grid, False) region.select_equal(kw_int, 1) glist = region.getGlobalList() for g in glist: self.assertEqual(kw_int[g], 1) with self.assertRaises(ValueError): region.select_equal(kw_float, 1)
def test_save_kw(self): with TestAreaContext("python/ecl_file/save_kw"): data = range(1000) kw = EclKW("MY_KEY", len(data), EclDataType.ECL_INT) for index, val in enumerate(data): kw[index] = val clean_dump = "my_clean_file" fortio = FortIO(clean_dump, FortIO.WRITE_MODE) kw.fwrite(fortio) fortio.close() test_file = "my_dump_file" fortio = FortIO(test_file, FortIO.WRITE_MODE) kw.fwrite(fortio) fortio.close() self.assertFilesAreEqual(clean_dump, test_file) ecl_file = EclFile(test_file, flags=EclFileFlagEnum.ECL_FILE_WRITABLE) loaded_kw = ecl_file["MY_KEY"][0] self.assertTrue(kw.equal(loaded_kw)) ecl_file.save_kw(loaded_kw) ecl_file.close() self.assertFilesAreEqual(clean_dump, test_file) ecl_file = EclFile(test_file) loaded_kw = ecl_file["MY_KEY"][0] self.assertTrue(kw.equal(loaded_kw))
def loadGrid(self): grid_file = self.createTestPath("Statoil/ECLIPSE/Faults/grid.grdecl") fileH = open(grid_file, "r") specgrid = EclKW.read_grdecl(fileH, "SPECGRID", ecl_type=EclDataType.ECL_INT, strict=False) zcorn = EclKW.read_grdecl(fileH, "ZCORN") coord = EclKW.read_grdecl(fileH, "COORD") actnum = EclKW.read_grdecl(fileH, "ACTNUM", ecl_type=EclDataType.ECL_INT) return EclGrid.create(specgrid, zcorn, coord, actnum)
def test_truncate(self): kw1 = EclKW("KW1", 2, EclDataType.ECL_INT) kw2 = EclKW("KW2", 2, EclDataType.ECL_INT) kw1[0] = 99 kw1[1] = 77 kw2[0] = 113 kw2[1] = 335 with TestAreaContext("python/fortio/ftruncate") as t: with openFortIO("file" , mode = FortIO.WRITE_MODE) as f: kw1.fwrite(f) pos1 = f.getPosition( ) kw2.fwrite(f) t.sync( ) # Truncate file in read mode; should fail hard. with openFortIO("file") as f: with self.assertRaises(IOError): f.truncate( ) with openFortIO("file" , mode = FortIO.READ_AND_WRITE_MODE) as f: f.seek( pos1 ) f.truncate( ) f = EclFile("file") self.assertEqual( len(f) , 1) kw1_ = f[0] self.assertEqual( kw1 , kw1_)
def make_field(rng , grid , iens): permx = EclKW.create( "PERMX" , grid.getGlobalSize( ) , EclTypeEnum.ECL_FLOAT_TYPE) permx.assign( rng.getDouble( ) ) poro = EclKW.create( "PORO" , grid.getGlobalSize( ) , EclTypeEnum.ECL_FLOAT_TYPE) poro.assign( rng.getDouble( ) ) if not os.path.isdir("fields"): os.makedirs("fields") with open("fields/permx%d.grdecl" % iens,"w") as f: permx.write_grdecl( f ) with open("fields/poro%d.grdecl" % iens ,"w") as f: poro.write_grdecl( f )
def test_deprecated_datatypes(self): with warnings.catch_warnings(record=True) as w: warnings.simplefilter("always") kw = EclKW("Test", 10, EclTypeEnum.ECL_INT_TYPE) self.assertTrue(len(w) > 0) self.assertTrue(issubclass(w[-1].category, DeprecationWarning)) with warnings.catch_warnings(record=True) as w: warnings.simplefilter("always") kw = EclKW("Test", 10, EclDataType.ECL_INT) self.assertTrue(len(w) == 0) self.assertEqual(EclTypeEnum.ECL_INT_TYPE, kw.type) self.assertTrue(len(w) > 0) self.assertTrue(issubclass(w[-1].category, DeprecationWarning))
def test_actnum_extraction(self): dims = (4, 4, 4) coord = GridGen.create_coord(dims, (1, 1, 1)) zcorn = GridGen.create_zcorn(dims, (1, 1, 1), offset=0) actnum = EclKW("ACTNUM", reduce(operator.mul, dims), EclDataType.ECL_INT) random.seed(1337) for i in range(len(actnum)): actnum[i] = random.randint(0, 1) grid = EclGrid.create(dims, zcorn, coord, actnum) ijk_bounds = generate_ijk_bounds(dims) for ijk_bound in ijk_bounds: if not decomposition_preserving(ijk_bound): continue sub = GridGen.extract_subgrid_data(dims, coord, zcorn, ijk_bound, actnum=actnum) sub_coord, sub_zcorn, sub_actnum = sub sub_dims = tuple([u - l + 1 for l, u in ijk_bound]) subgrid = EclGrid.create(sub_dims, sub_zcorn, sub_coord, sub_actnum) self.assertEqual(sub_dims, subgrid.getDims()[:-1:]) self.assertSubgrid(grid, subgrid, ijk_bound)
def test_get_ijk(self): with TestAreaContext( "python/fault_block_layer/neighbour") as work_area: with open("kw.grdecl", "w") as fileH: fileH.write("FAULTBLK \n") fileH.write("1 1 1 0 0\n") fileH.write("1 2 2 0 3\n") fileH.write("4 2 2 3 3\n") fileH.write("4 4 4 0 0\n") fileH.write("4 4 4 0 5\n") fileH.write("/\n") kw = EclKW.read_grdecl(open("kw.grdecl"), "FAULTBLK", ecl_type=EclDataType.ECL_INT) grid = EclGrid.createRectangular((5, 5, 1), (1, 1, 1)) layer = FaultBlockLayer(grid, 0) layer.loadKeyword(kw) block = layer[0, 0] self.assertEqual(block.getBlockID(), 1) block = layer[2, 2] self.assertEqual(block.getBlockID(), 2) with self.assertRaises(ValueError): layer[3, 3] with self.assertRaises(IndexError): layer[5, 5]
def test_fault_block(self): grid = EclGrid.createRectangular((5, 5, 1), (1, 1, 1)) kw = EclKW("FAULTBLK", grid.getGlobalSize(), EclDataType.ECL_INT) kw.assign(0) for j in range(1, 4): for i in range(1, 4): g = i + j * grid.getNX() kw[g] = 1 layer = FaultBlockLayer(grid, 0) layer.scanKeyword(kw) block = layer[1] self.assertEqual((2.50, 2.50), block.getCentroid()) self.assertEqual(len(block), 9) self.assertEqual(layer, block.getParentLayer())
def test_string_write_read_formatted(self): for str_len in range(1000): with TestAreaContext("my_space"): kw = EclKW("TEST_KW", 10, EclDataType.ECL_STRING(str_len)) for i in range(10): kw[i] = str(i)*str_len file_name = "ecl_kw_test" with openFortIO(file_name, mode=FortIO.WRITE_MODE, fmt_file=True) as fortio: kw.fwrite(fortio) with openFortIO(file_name, fmt_file=True) as fortio: loaded_kw = EclKW.fread(fortio) self.assertEqual(kw, loaded_kw)
def extract_actnum(cls, dims, actnum, ijk_bounds): if actnum is None: return None nx, ny, nz = dims (lx, ux), (ly, uy), (lz, uz) = ijk_bounds new_nx, new_ny, new_nz = ux - lx + 1, uy - ly + 1, uz - lz + 1 cls.assert_actnum(nx, ny, nz, actnum) actnum = divide(divide(actnum, nx), ny) new_actnum = [ y_slice[lx:ux + 1:] for z_slice in actnum[lz:uz + 1:] for y_slice in z_slice[ly:uy + 1:] ] new_actnum = flatten(new_actnum) cls.assert_actnum(new_nx, new_ny, new_nz, new_actnum) actnumkw = EclKW("ACTNUM", len(new_actnum), EclDataType.ECL_INT) for i, value in enumerate(new_actnum): actnumkw[i] = value return actnumkw
def replace_kw(self, old_kw, new_kw): """ Will replace @old_kw with @new_kw in current EclFile instance. This method can be used to replace one of the EclKW instances in the current EclFile. The @old_kw reference must be to the actual EclKW instance in the current EclFile instance (the final comparison is based on C pointer equality!), i.e. it must be a reference (not a copy) from one of the ??get_kw?? methods of the EclFile class. In the example below we replace the SWAT keyword from a restart file: swat = file.iget_named_kw( "SWAT" , 0 ) new_swat = swat * 0.25 file.replace_kw( swat , new_swat ) The C-level ecl_file_type structure takes full ownership of all installed ecl_kw instances; mixing the garbage collector into it means that this is quite low level - and potentially dangerous! """ # We ensure that this scope owns the new_kw instance; the # new_kw will be handed over to the ecl_file instance, and we # can not give away something we do not alreeady own. if not new_kw.data_owner: new_kw = EclKW.copy(new_kw) # The ecl_file instance will take responsability for freeing # this ecl_kw instance. new_kw.data_owner = False self._replace_kw(old_kw, new_kw, False)
def test_Load(self): kw = EclKW.read_grdecl(open(self.src_file, "r"), "PERMX") self.assertTrue(kw) grid = EclGrid(self.createTestPath("Statoil/ECLIPSE/Gurbat/ECLIPSE")) kw = Ecl3DKW.read_grdecl(grid, open(self.src_file, "r"), "PERMX") self.assertTrue(isinstance(kw, Ecl3DKW))
def test_numpy3D(self): nx = 10 ny = 7 nz = 5 grid = GridGen.createRectangular((nx,ny,nz) , (1,1,1)) kw = EclKW( "SWAT" , nx*ny*nz , EclDataType.ECL_FLOAT ) numpy_3d = grid.create3D( kw )