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_truncate(self): kw1 = EclKW("KW1", 2, EclTypeEnum.ECL_INT_TYPE) kw2 = EclKW("KW2", 2, EclTypeEnum.ECL_INT_TYPE) 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_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_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_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_EclFile_name_property(self): with TestAreaContext("name"): kw = EclKW("TEST", 3, EclTypeEnum.ECL_INT_TYPE) with openFortIO("TEST" , mode = FortIO.WRITE_MODE) as f: kw.fwrite( f ) f = EclFile( "TEST" ) with self.assertRaises(DeprecationWarning): name = f.name
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 create_init(grid, case): poro = EclKW("PORO", grid.getNumActive(), EclTypeEnum.ECL_FLOAT_TYPE) 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_is_fortran_file(self): with TestAreaContext("python/fortio/guess"): kw1 = EclKW("KW", 12345, EclTypeEnum.ECL_FLOAT_TYPE) with openFortIO("fortran_file", mode=FortIO.WRITE_MODE) as f: kw1.fwrite(f) with 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() , EclTypeEnum.ECL_FLOAT_TYPE ) 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_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 open("text_file" , "w") as f: kw1.write_grdecl( f ) self.assertTrue( FortIO.isFortranFile( "fortran_file" )) self.assertFalse( FortIO.isFortranFile( "text_file" ))
def test_context(self): with TestAreaContext("python/ecl_file/context"): kw1 = EclKW("KW1", 100, EclTypeEnum.ECL_INT_TYPE) kw2 = EclKW("KW2", 100, EclTypeEnum.ECL_INT_TYPE) with openFortIO("TEST", mode=FortIO.WRITE_MODE) as f: kw1.fwrite(f) kw2.fwrite(f) with openEclFile("TEST") as ecl_file: self.assertEqual(len(ecl_file), 2) self.assertTrue(ecl_file.has_kw("KW1")) self.assertTrue(ecl_file.has_kw("KW2"))
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_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_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_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_context(self): with TestAreaContext("python/fortio/context") as t: kw1 = EclKW("KW", 2456, EclTypeEnum.ECL_FLOAT_TYPE) 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_kw_write(self): with TestAreaContext("python/ecl_kw/writing"): data = [random.random() for i in range(10000)] kw = EclKW("TEST", len(data), EclTypeEnum.ECL_DOUBLE_TYPE) i = 0 for d in data: kw[i] = d i += 1 pfx = 'EclKW(' self.assertEqual(pfx, repr(kw)[:len(pfx)]) fortio = FortIO("ECL_KW_TEST", FortIO.WRITE_MODE) kw.fwrite(fortio) fortio.close() fortio = FortIO("ECL_KW_TEST") kw2 = EclKW.fread(fortio) self.assertTrue(kw.equal(kw2)) ecl_file = EclFile("ECL_KW_TEST", flags=EclFileFlagEnum.ECL_FILE_WRITABLE) kw3 = ecl_file["TEST"][0] self.assertTrue(kw.equal(kw3)) ecl_file.save_kw(kw3) ecl_file.close() fortio = FortIO("ECL_KW_TEST", FortIO.READ_AND_WRITE_MODE) kw4 = EclKW.fread(fortio) self.assertTrue(kw.equal(kw4)) fortio.seek(0) kw4.fwrite(fortio) fortio.close() ecl_file = EclFile("ECL_KW_TEST") kw5 = ecl_file["TEST"][0] self.assertTrue(kw.equal(kw5))
def test_kw(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/write-kw"): f = FortIO("test", FortIO.WRITE_MODE, fmt_file=False) kw1.fwrite(f) f = FortIO("test", FortIO.APPEND_MODE) kw2.fwrite(f) f = FortIO("test", fmt_file=False) k1 = EclKW.fread(f) k2 = EclKW.fread(f) self.assertTrue(k1.equal(kw1)) self.assertTrue(k2.equal(kw2))
def test_kw(self): kw1 = EclKW("KW1", 2, EclTypeEnum.ECL_INT_TYPE) kw2 = EclKW("KW2", 2, EclTypeEnum.ECL_INT_TYPE) kw1[0] = 99 kw1[1] = 77 kw2[0] = 113 kw2[1] = 335 with TestAreaContext("python/fortio/write-kw"): f = FortIO("test", FortIO.WRITE_MODE, fmt_file=False) kw1.fwrite(f) f = FortIO("test", FortIO.APPEND_MODE) kw2.fwrite(f) f = FortIO("test", fmt_file=False) k1 = EclKW.fread(f) k2 = EclKW.fread(f) self.assertTrue(k1.equal(kw1)) self.assertTrue(k2.equal(kw2))
def test_fmt(self): kw1 = EclKW( "NAME1" , 100 , EclTypeEnum.ECL_INT_TYPE) kw2 = EclKW( "NAME2" , 100 , EclTypeEnum.ECL_INT_TYPE) for i in range(len(kw1)): kw1[i] = i + 1 kw2[i] = len(kw1) - kw1[i] with TestAreaContext("ecl_kw/fmt") as ta: with openFortIO( "TEST.FINIT" , FortIO.WRITE_MODE , fmt_file = True ) as f: kw1.fwrite( f ) kw2.fwrite( f ) with openFortIO( "TEST.FINIT" , fmt_file = True ) as f: kw1b = EclKW.fread( f ) kw2b = EclKW.fread( f ) self.assertTrue( kw1 == kw1b ) self.assertTrue( kw2 == kw2b ) f = EclFile( "TEST.FINIT" ) self.assertTrue( kw1 == f[0] ) self.assertTrue( kw2 == f[1] )
def test_fmt(self): kw1 = EclKW( "NAME1" , 100 , EclDataType.ECL_INT) kw2 = EclKW( "NAME2" , 100 , EclDataType.ECL_INT) for i in range(len(kw1)): kw1[i] = i + 1 kw2[i] = len(kw1) - kw1[i] with TestAreaContext("ecl_kw/fmt") as ta: with openFortIO( "TEST.FINIT" , FortIO.WRITE_MODE , fmt_file = True ) as f: kw1.fwrite( f ) kw2.fwrite( f ) with openFortIO( "TEST.FINIT" , fmt_file = True ) as f: kw1b = EclKW.fread( f ) kw2b = EclKW.fread( f ) self.assertTrue( kw1 == kw1b ) self.assertTrue( kw2 == kw2b ) f = EclFile( "TEST.FINIT" ) self.assertTrue( kw1 == f[0] ) self.assertTrue( kw2 == f[1] )
def test_kw_write(self): with TestAreaContext("python/ecl_kw/writing"): data = [random.random() for i in range(10000)] kw = EclKW("TEST", len(data), EclTypeEnum.ECL_DOUBLE_TYPE) i = 0 for d in data: kw[i] = d i += 1 fortio = FortIO("ECL_KW_TEST", FortIO.WRITE_MODE) kw.fwrite(fortio) fortio.close() fortio = FortIO("ECL_KW_TEST") kw2 = EclKW.fread(fortio) self.assertTrue(kw.equal(kw2)) ecl_file = EclFile("ECL_KW_TEST", flags=EclFileFlagEnum.ECL_FILE_WRITABLE) kw3 = ecl_file["TEST"][0] self.assertTrue(kw.equal(kw3)) ecl_file.save_kw(kw3) ecl_file.close() fortio = FortIO("ECL_KW_TEST", FortIO.READ_AND_WRITE_MODE) kw4 = EclKW.fread(fortio) self.assertTrue(kw.equal(kw4)) fortio.seek(0) kw4.fwrite(fortio) fortio.close() ecl_file = EclFile("ECL_KW_TEST") kw5 = ecl_file["TEST"][0] self.assertTrue(kw.equal(kw5))
def test_ecl_file_block(self): with TestAreaContext("name") as t: kw = EclKW("TEST", 3, EclTypeEnum.ECL_INT_TYPE) 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_block_view(self): with TestAreaContext("python/ecl_file/view"): with openFortIO("TEST" , mode = FortIO.WRITE_MODE) as f: for i in range(5): header = EclKW("HEADER" , 1 , EclTypeEnum.ECL_INT_TYPE ) header[0] = i data1 = EclKW("DATA1" , 100 , EclTypeEnum.ECL_INT_TYPE ) data1.assign( i ) data2 = EclKW("DATA2" , 100 , EclTypeEnum.ECL_INT_TYPE ) data2.assign( i*10 ) header.fwrite( f ) data1.fwrite( f ) data2.fwrite( f ) ecl_file = EclFile("TEST") with self.assertRaises(KeyError): ecl_file.blockView("NO" , 1) with self.assertRaises(IndexError): ecl_file.blockView("HEADER" , 100) for i in range(5): view = ecl_file.blockView("HEADER" , i) self.assertEqual( len(view) , 3) header = view["HEADER"][0] data1 = view["DATA1"][0] data2 = view["DATA2"][0] self.assertEqual( header[0] , i ) self.assertEqual( data1[99] , i ) self.assertEqual( data2[99] , i*10 ) for i in range(5): view = ecl_file.blockView2("HEADER" , "DATA2", i ) self.assertEqual( len(view) , 2) header = view["HEADER"][0] data1 = view["DATA1"][0] self.assertEqual( header[0] , i ) self.assertEqual( data1[99] , i ) self.assertFalse( "DATA2" in view ) view = ecl_file.blockView2("HEADER" , None, 0 ) self.assertEqual( len(view) , len(ecl_file)) view = ecl_file.blockView2(None , "DATA2", 0 )
def test_block_view(self): with TestAreaContext("python/ecl_file/view"): with openFortIO("TEST", mode=FortIO.WRITE_MODE) as f: for i in range(5): header = EclKW("HEADER", 1, EclTypeEnum.ECL_INT_TYPE) header[0] = i data1 = EclKW("DATA1", 100, EclTypeEnum.ECL_INT_TYPE) data1.assign(i) data2 = EclKW("DATA2", 100, EclTypeEnum.ECL_INT_TYPE) data2.assign(i * 10) header.fwrite(f) data1.fwrite(f) data2.fwrite(f) ecl_file = EclFile("TEST") with self.assertRaises(KeyError): ecl_file.blockView("NO", 1) with self.assertRaises(IndexError): ecl_file.blockView("HEADER", 100) for i in range(5): view = ecl_file.blockView("HEADER", i) self.assertEqual(len(view), 3) header = view["HEADER"][0] data1 = view["DATA1"][0] data2 = view["DATA2"][0] self.assertEqual(header[0], i) self.assertEqual(data1[99], i) self.assertEqual(data2[99], i * 10) for i in range(5): view = ecl_file.blockView2("HEADER", "DATA2", i) self.assertEqual(len(view), 2) header = view["HEADER"][0] data1 = view["DATA1"][0] self.assertEqual(header[0], i) self.assertEqual(data1[99], i) self.assertFalse("DATA2" in view) view = ecl_file.blockView2("HEADER", None, 0) self.assertEqual(len(view), len(ecl_file)) view = ecl_file.blockView2(None, "DATA2", 0)
def create_restart(grid, case, p1, p2=None): with openFortIO("%s.UNRST" % case, mode=FortIO.WRITE_MODE) as f: seq_hdr = EclKW("SEQNUM", 1, EclTypeEnum.ECL_FLOAT_TYPE) seq_hdr[0] = 10 p = EclKW("PRESSURE", grid.getNumActive(), EclTypeEnum.ECL_FLOAT_TYPE) for i in range(len(p1)): p[i] = p1[i] header = EclKW("INTEHEAD", 67, EclTypeEnum.ECL_INT_TYPE) header[64] = 1 header[65] = 1 header[66] = 2000 seq_hdr.fwrite(f) header.fwrite(f) p.fwrite(f) if p2: seq_hdr[0] = 20 header[66] = 2010 for i in range(len(p2)): p[i] = p2[i] seq_hdr.fwrite(f) header.fwrite(f) p.fwrite(f) class GeertsmaTest(ExtendedTestCase):
def create_restart(grid, case, p1, p2=None): with openFortIO("%s.UNRST" % case, mode=FortIO.WRITE_MODE) as f: seq_hdr = EclKW("SEQNUM", 1, EclTypeEnum.ECL_FLOAT_TYPE) seq_hdr[0] = 10 p = EclKW("PRESSURE", grid.getNumActive(), EclTypeEnum.ECL_FLOAT_TYPE) for i in range(len(p1)): p[i] = p1[i] header = EclKW("INTEHEAD", 67, EclTypeEnum.ECL_INT_TYPE) header[64] = 1 header[65] = 1 header[66] = 2000 seq_hdr.fwrite(f) header.fwrite(f) p.fwrite(f) if p2: seq_hdr[0] = 20 header[66] = 2010 for i in range(len(p2)): p[i] = p2[i] seq_hdr.fwrite(f) header.fwrite(f) p.fwrite(f)