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, 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_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 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_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(), 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_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_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_context(self): with TestAreaContext("python/ecl_file/context"): kw1 = EclKW("KW1", 100, EclDataType.ECL_INT) kw2 = EclKW("KW2", 100, EclDataType.ECL_INT) 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")) self.assertEqual(ecl_file[1], ecl_file[-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_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 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_kw_write(self): with TestAreaContext("python/ecl_kw/writing"): data = [random.random() for i in range(10000)] kw = EclKW("TEST", len(data), EclDataType.ECL_DOUBLE) 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_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_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, EclDataType.ECL_INT) header[0] = i data1 = EclKW("DATA1", 100, EclDataType.ECL_INT) data1.assign(i) data2 = EclKW("DATA2", 100, EclDataType.ECL_INT) data2.assign(i * 10) header.fwrite(f) data1.fwrite(f) data2.fwrite(f) ecl_file = EclFile("TEST") pfx = 'EclFile(' self.assertEqual(pfx, repr(ecl_file)[:len(pfx)]) with self.assertRaises(KeyError): ecl_file.blockView("NO", 1) with self.assertRaises(IndexError): ecl_file.blockView("HEADER", 100) with self.assertRaises(IndexError): ecl_file.blockView("HEADER", 1000) bv = ecl_file.blockView("HEADER", -1) 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_ecl_index(self): with TestAreaContext("python/ecl_file/context"): kw1 = EclKW("KW1", 100, EclDataType.ECL_INT) kw2 = EclKW("KW2", 100, EclDataType.ECL_FLOAT) kw3 = EclKW("KW3", 100, EclDataType.ECL_CHAR) kw4 = EclKW("KW4", 100, EclDataType.ECL_STRING(23)) with openFortIO("TEST", mode=FortIO.WRITE_MODE) as f: kw1.fwrite(f) kw2.fwrite(f) kw3.fwrite(f) kw4.fwrite(f) ecl_file = EclFile("TEST") ecl_file.write_index("INDEX_FILE") ecl_file.close() ecl_file_index = EclFile("TEST", 0, "INDEX_FILE") for kw in ["KW1", "KW2", "KW3", "KW4"]: self.assertIn(kw, ecl_file_index) with self.assertRaises(IOError): ecl_file.write_index("does-not-exist/INDEX") os.mkdir("read-only") os.chmod("read-only", 0o444) with self.assertRaises(IOError): ecl_file.write_index("read-only/INDEX") with self.assertRaises(IOError): ecl_file_index = EclFile("TEST", 0, "index_does_not_exist") shutil.copyfile("INDEX_FILE", "INDEX_perm_denied") os.chmod("INDEX_perm_denied", 0o000) with self.assertRaises(IOError): ecl_file_index = EclFile("TEST", 0, "INDEX_perm_denied") os.mkdir("path") shutil.copyfile("TEST", "path/TEST") ecl_file = EclFile("path/TEST") ecl_file.write_index("path/index") with CWDContext("path"): ecl_file = EclFile("TEST", 0, "index")
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_kw_vector(self): case = createEclSum("CSV", [("FOPT", None, 0), ("FOPR", None, 0), ("FGPT", None, 0)], sim_length_days=100, num_report_step=10, num_mini_step=10, func_table={
def create_restart(grid, case, p1, p2=None): with openFortIO("%s.UNRST" % case, mode=FortIO.WRITE_MODE) as f: seq_hdr = EclKW("SEQNUM", 1, EclDataType.ECL_FLOAT) seq_hdr[0] = 10 p = EclKW("PRESSURE", grid.getNumActive(), EclDataType.ECL_FLOAT) for i in range(len(p1)): p[i] = p1[i] header = EclKW("INTEHEAD", 67, EclDataType.ECL_INT) 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(EclTest):