def test_scatter_copy(self): source = EclKW("SOURCE", 4 , EclDataType.ECL_INT) with self.assertRaises(TypeError): copy = source.scatter_copy([1,1,1,1]) actnum = EclKW("ACTNUM", 6 , EclDataType.ECL_FLOAT) with self.assertRaises(ValueError): copy = source.scatter_copy(actnum) actnum = EclKW("ACTNUM", 8, EclDataType.ECL_INT) actnum[0] = 1 actnum[1] = 1 with self.assertRaises(ValueError): copy = source.scatter_copy(actnum) actnum.assign(1) with self.assertRaises(ValueError): copy = source.scatter_copy(actnum) for i in range(4): source[i] = i+1 actnum[2*i] = 0 # src = [1,2,3,4] # actnum = [0,1,0,1,0,1,0,1] # copy = [0,1,0,2,0,3,0,4] copy = source.scatter_copy(actnum) for i in range(4): self.assertEqual(copy[2*i + 1], i+1)
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_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) f = EclFile("TEST")
def test_broken_file(self): with TestAreaContext("test_broken_file"): with open("CASE.FINIT", "w") as f: f.write("This - is not a ECLISPE file\nsdlcblhcdbjlwhc\naschscbasjhcasc\nascasck c s s aiasic asc") f = EclFile("CASE.FINIT") self.assertEqual(len(f), 0) kw = EclKW("HEADER", 100, EclDataType.ECL_INT) with openFortIO("FILE",mode=FortIO.WRITE_MODE) as f: kw.fwrite(f) kw.fwrite(f) with open("FILE", "a+") as f: f.write("Seom random gibberish") f = EclFile("FILE") self.assertEqual(len(f), 2) with openFortIO("FILE",mode=FortIO.WRITE_MODE) as f: kw.fwrite(f) kw.fwrite(f) file_size = os.path.getsize("FILE") with open("FILE", "a+") as f: f.truncate(file_size * 0.75) f = EclFile("FILE") self.assertEqual(len(f), 1)
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_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_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_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 loadGrid(self): grid_file = self.createTestPath("Statoil/ECLIPSE/Faults/grid.grdecl") fileH = copen(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_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 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 test_dataframe_actnum(self): grid = EclGrid.create_rectangular((2, 3, 1), (1, 1, 1), actnum=[1, 1, 0, 0, 1, 1]) df = grid.export_index(True) index_matrix = np.array([[0, 0, 0, 0], [1, 0, 0, 1], [0, 2, 0, 2], [1, 2, 0, 3]]) assert (np.array_equal(df.values, index_matrix)) kw_int_active = EclKW('int_act', 4, EclTypeEnum.ECL_INT_TYPE) kw_int_active[0] = 9 kw_int_active[1] = 8 kw_int_active[2] = 7 kw_int_active[3] = 6 data = grid.export_data(df, kw_int_active) assert (len(data) == 4) assert (np.array_equal(data, np.array([9, 8, 7, 6]))) kw_float_active = EclKW('float_at', 4, EclTypeEnum.ECL_FLOAT_TYPE) kw_float_active[0] = 10.5 kw_float_active[1] = 9.25 kw_float_active[2] = 2.0 kw_float_active[3] = 1.625 data = grid.export_data(df, kw_float_active) assert (len(data) == 4) assert (np.array_equal(data, np.array([10.5, 9.25, 2.0, 1.625]))) kw_int_global = EclKW('int_glob', 6, EclTypeEnum.ECL_INT_TYPE) kw_int_global[0] = 0 kw_int_global[1] = 2 kw_int_global[2] = 4 kw_int_global[3] = 6 kw_int_global[4] = 8 kw_int_global[5] = 9 data = grid.export_data(df, kw_int_global) assert (len(data) == 4) assert (np.array_equal(data, np.array([0, 2, 8, 9]))) kw_double_global = EclKW('double_g', 6, EclTypeEnum.ECL_DOUBLE_TYPE) kw_double_global[0] = 1.1 kw_double_global[1] = 2.2 kw_double_global[2] = 3.3 kw_double_global[3] = 4.4 kw_double_global[4] = 5.5 kw_double_global[5] = 6.6 data = grid.export_data(df, kw_double_global) assert (np.array_equal(data, np.array([1.1, 2.2, 5.5, 6.6]))) df = grid.export_index() #DataFrame has now 6 rows global_index = df.index assert (np.array_equal(global_index, np.array([0, 1, 2, 3, 4, 5]))) data = grid.export_data(df, kw_int_active, 9999) assert (np.array_equal(data, np.array([9, 8, 9999, 9999, 7, 6]))) data = grid.export_data(df, kw_float_active, 2222.0) assert (np.array_equal( data, np.array([10.5, 9.25, 2222.0, 2222.0, 2.0, 1.625])))
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 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 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 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_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 = copen(self.src_file, "r") self.assertTrue(EclKW.fseek_grdecl(file, "PERMX")) self.assertFalse(EclKW.fseek_grdecl(file, "PERMY")) file.close() file = copen(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_fseek(self): file = copen(self.src_file, "r") self.assertTrue(EclKW.fseek_grdecl(file, "PERMX")) self.assertFalse(EclKW.fseek_grdecl(file, "PERMY")) file.close() file = copen(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_is_fortran_file(self): with TestAreaContext("python/fortio/guess"): kw1 = EclKW("KW" , 12345 , EclDataType.ECL_FLOAT) with openFortIO("fortran_file" , mode = FortIO.WRITE_MODE) as f: kw1.fwrite( f ) with cwrap.open("text_file" , "w") as f: kw1.write_grdecl( f ) self.assertTrue( FortIO.isFortranFile( "fortran_file" )) self.assertFalse( FortIO.isFortranFile( "text_file" ))
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 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 create(self, filename, load_actnum=True): fileH = copen(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 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 test_context(self): with TestAreaContext("python/fortio/context") as t: kw1 = EclKW("KW", 2456, EclDataType.ECL_FLOAT) for i in range(len(kw1)): kw1[i] = randint(0, 1000) with openFortIO("file", mode=FortIO.WRITE_MODE) as f: kw1.fwrite(f) self.assertEqual(f.filename(), "file") with openFortIO("file") as f: kw2 = EclKW.fread(f) self.assertTrue(kw1 == kw2)
def create_init(grid, case): poro = EclKW("PORO", grid.getNumActive(), EclDataType.ECL_FLOAT) porv = EclKW("PORV", grid.getGlobalSize(), EclDataType.ECL_FLOAT) with openFortIO("%s.INIT" % case, mode=FortIO.WRITE_MODE) as f: poro.fwrite(f) porv.fwrite(f)
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_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_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 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) == 1)
def test_resize(self): N = 4 kw = EclKW("KW", N, EclDataType.ECL_INT) for i in range(N): kw[i] = i kw.resize(2*N) self.assertEqual(len(kw), 2*N) for i in range(N): self.assertEqual(kw[i], i) kw.resize(N/2) self.assertEqual(len(kw), N/2) for i in range(int(N / 2)): self.assertEqual(kw[i], i)
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 )
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 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_context(self): with TestAreaContext("python/fortio/context") as t: kw1 = EclKW("KW" , 2456 , EclDataType.ECL_FLOAT) for i in range(len(kw1)): kw1[i] = randint(0,1000) with openFortIO("file" , mode = FortIO.WRITE_MODE) as f: kw1.fwrite( f ) self.assertEqual( f.filename() , "file") t.sync( ) with openFortIO("file") as f: kw2 = EclKW.fread( f ) self.assertTrue( kw1 == kw2 )
def test_Load( self ): kw = EclKW.read_grdecl(copen(self.src_file, "r"), "PERMX") self.assertTrue(kw) grid = EclGrid( self.createTestPath("Statoil/ECLIPSE/Gurbat/ECLIPSE" )) kw = Ecl3DKW.read_grdecl(grid , copen(self.src_file, "r"), "PERMX") self.assertTrue( isinstance( kw , Ecl3DKW ))
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") with cwrap.open("kw.grdecl") as f: kw = EclKW.read_grdecl( f, "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_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", six.functools.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 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 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 kw_test(self, data_type, data, fmt): name1 = "file1.txt" name2 = "file2.txt" kw = EclKW("TEST", len(data), data_type) for (i,d) in enumerate(data): kw[i] = d file1 = cwrap.open(name1, "w") kw.fprintf_data(file1, fmt) file1.close() file2 = open(name2, "w") for d in data: file2.write(fmt % d) file2.close() self.assertFilesAreEqual(name1, name2) self.assertEqual(kw.data_type, data_type)
def test_reload( self ): kw = EclKW.read_grdecl(copen(self.src_file, "r"), "PERMX") tmp_file1 = "/tmp/permx1.grdecl" tmp_file2 = "/tmp/permx2.grdecl" self.addFile(tmp_file1) self.addFile(tmp_file2) fileH = copen(tmp_file1, "w") kw.write_grdecl(fileH) fileH.close() kw1 = EclKW.read_grdecl(copen(tmp_file1, "r"), "PERMX") fileH = copen(tmp_file2, "w") kw1.write_grdecl(fileH) fileH.close() self.assertFilesAreEqual(tmp_file1, tmp_file2)
def test_fprintf_data(self): with TestAreaContext("kw_no_header"): kw = EclKW("REGIONS", 10, EclDataType.ECL_INT) for i in range(len(kw)): kw[i] = i fileH = cwrap.open("test", "w") kw.fprintf_data(fileH) fileH.close() fileH = open("test", "r") data = [] for line in fileH.readlines(): tmp = line.split() for elm in tmp: data.append(int(elm)) for (v1,v2) in zip(data,kw): self.assertEqual(v1,v2)
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 test_invalid(self): case = create_case() with TestAreaContext("sum_invalid"): case.fwrite( ) with open("CASE.txt", "w") as f: f.write("No - this is not EclKW file ....") with self.assertRaises( IOError ): case2 = EclSum.load( "CSV.SMSPEC" , "CASE.txt" ) with self.assertRaises( IOError ): case2 = EclSum.load( "CASE.txt" , "CSV.UNSMRY" ) kw1 = EclKW("TEST1", 30, EclDataType.ECL_INT) kw2 = EclKW("TEST2", 30, EclDataType.ECL_INT) with openFortIO( "CASE.KW" , FortIO.WRITE_MODE) as f: kw1.fwrite( f ) kw2.fwrite( f ) with self.assertRaises( IOError ): case2 = EclSum.load( "CSV.SMSPEC" , "CASE.KW") with self.assertRaises( IOError ): case2 = EclSum.load( "CASE.KW" , "CSV.UNSMRY" )
def test_internal_blocks(self): nx = 8 ny = 8 nz = 1 grid = EclGrid.createRectangular( (nx , ny , nz) , (1,1,1) ) layer = FaultBlockLayer( grid , 0 ) with TestAreaContext("python/FaultBlocks/internal_blocks"): with open("faultblock.grdecl","w") as fileH: fileH.write("FAULTBLK \n") fileH.write("1 1 1 1 2 2 2 2 \n") fileH.write("1 4 4 1 2 5 5 2 \n") fileH.write("1 4 4 1 2 5 5 2 \n") fileH.write("1 1 1 1 2 2 2 2 \n") fileH.write("1 1 1 1 1 2 2 2 \n") fileH.write("1 1 3 1 1 2 2 2 \n") fileH.write("1 1 1 1 1 2 2 2 \n") fileH.write("1 1 1 1 1 2 2 2 \n") fileH.write("/\n") with cwrap.open("faultblock.grdecl") as f: kw = EclKW.read_grdecl( f, "FAULTBLK", ecl_type=EclDataType.ECL_INT) with open("faults.grdecl", "w") as f: f.write("FAULTS\n") f.write("\'FX\' 4 4 1 4 1 1 'X' /\n") f.write("\'FX\' 5 5 4 4 1 1 'Y' /\n") f.write("\'FX\' 5 5 5 8 1 1 'X' /\n") f.write("/") faults = FaultCollection( grid , "faults.grdecl") layer.loadKeyword( kw ) layer.addFaultBarrier( faults["FX"] ) b1 = layer.getBlock( 1 ) b2 = layer.getBlock( 2 ) b3 = layer.getBlock( 3 ) b4 = layer.getBlock( 4 ) b5 = layer.getBlock( 5 ) nb = b1.getNeighbours() for b in nb: print('Block:%d' % b.getBlockID()) self.assertTrue( len(nb) == 2 ) self.assertTrue( b3 in nb ) self.assertTrue( b4 in nb ) nb = b2.getNeighbours() self.assertTrue( len(nb) == 1 ) self.assertTrue( b5 in nb )