def test_sum(self): grid = EclGrid.createRectangular((10, 10, 1), (1, 1, 1)) kw_mask = EclKW("INT", grid.getGlobalSize(), EclDataType.ECL_INT) int_value = EclKW("INT", grid.getGlobalSize(), EclDataType.ECL_INT) float_value = EclKW("FLOAT", grid.getGlobalSize(), EclDataType.ECL_FLOAT) double_value = EclKW("DOUBLE", grid.getGlobalSize(), EclDataType.ECL_DOUBLE) bool_value = EclKW("BOOL", grid.getGlobalSize(), EclDataType.ECL_BOOL) kw_mask[0:50] = 1 for i in range(len(int_value)): float_value[i] = i double_value[i] = i int_value[i] = i bool_value[i] = True region = EclRegion(grid, False) region.select_equal(kw_mask, 1) self.assertEqual(int_value.sum(), 99 * 100 / 2) self.assertEqual(int_value.sum(mask=region), 49 * 50 / 2) self.assertEqual(double_value.sum(mask=region), 1.0 * 49 * 50 / 2) self.assertEqual(float_value.sum(mask=region), 1.0 * 49 * 50 / 2) self.assertEqual(bool_value.sum(mask=region), 50)
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_sum(self): for ecl_type in [EclDataType.ECL_CHAR, EclDataType.ECL_STRING(42)]: kw_string = EclKW("STRING", 100, ecl_type) with self.assertRaises(ValueError): kw_string.sum() kw_int = EclKW("INT", 4, EclDataType.ECL_INT) kw_int[0] = 1 kw_int[1] = 2 kw_int[2] = 3 kw_int[3] = 4 self.assertEqual(kw_int.sum(), 10) kw_d = EclKW("D", 4, EclDataType.ECL_DOUBLE) kw_d[0] = 1 kw_d[1] = 2 kw_d[2] = 3 kw_d[3] = 4 self.assertEqual(kw_d.sum(), 10) kw_f = EclKW("F", 4, EclDataType.ECL_FLOAT) kw_f[0] = 1 kw_f[1] = 2 kw_f[2] = 3 kw_f[3] = 4 self.assertEqual(kw_f.sum(), 10) kw_b = EclKW("F", 4, EclDataType.ECL_BOOL) kw_b[0] = False kw_b[1] = True kw_b[2] = False kw_b[3] = True self.assertEqual(kw_b.sum(), 2)
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_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_numeric_equal(self): kw1 = EclKW("Name1", 10, EclDataType.ECL_DOUBLE) kw2 = EclKW("Name1", 10, EclDataType.ECL_DOUBLE) shift = 0.0001 value = 1000 abs_diff = shift rel_diff = shift / (shift + 2 * value) kw1.assign(value) kw2.assign(value + shift) self.assertTrue( kw1.equal_numeric(kw2, abs_epsilon=abs_diff * 1.1, rel_epsilon=rel_diff * 1.1)) self.assertFalse( kw1.equal_numeric(kw2, abs_epsilon=abs_diff * 1.1, rel_epsilon=rel_diff * 0.9)) self.assertFalse( kw1.equal_numeric(kw2, abs_epsilon=abs_diff * 0.9, rel_epsilon=rel_diff * 1.1)) self.assertTrue( kw1.equal_numeric(kw2, abs_epsilon=0, rel_epsilon=rel_diff * 1.1)) self.assertTrue( kw1.equal_numeric(kw2, abs_epsilon=abs_diff * 1.1, rel_epsilon=0))
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_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_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_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_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_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_first_different(self): kw1 = EclKW("NAME1", 100, EclDataType.ECL_INT) kw2 = EclKW("NAME2", 100, EclDataType.ECL_INT) kw3 = EclKW("NAME2", 200, EclDataType.ECL_INT) kw4 = EclKW("NAME2", 100, EclDataType.ECL_FLOAT) kw5 = EclKW("NAME2", 100, EclDataType.ECL_FLOAT) with self.assertRaises(IndexError): EclKW.firstDifferent(kw1, kw2, offset=100) with self.assertRaises(ValueError): EclKW.firstDifferent(kw1, kw3) with self.assertRaises(TypeError): EclKW.firstDifferent(kw1, kw4) with self.assertRaises(IndexError): kw1.firstDifferent(kw2, offset=100) with self.assertRaises(ValueError): kw1.firstDifferent(kw3) with self.assertRaises(TypeError): kw1.firstDifferent(kw4) kw1.assign(1) kw2.assign(1) self.assertEqual(kw1.firstDifferent(kw2), len(kw1)) kw1[0] = 100 self.assertEqual(kw1.firstDifferent(kw2), 0) self.assertEqual(kw1.firstDifferent(kw2, offset=1), len(kw1)) kw1[10] = 100 self.assertEqual(kw1.firstDifferent(kw2, offset=1), 10) kw4.assign(1.0) kw5.assign(1.0) self.assertEqual(kw4.firstDifferent(kw5), len(kw4)) kw4[10] *= 1.0001 self.assertEqual(kw4.firstDifferent(kw5), 10) self.assertEqual(kw4.firstDifferent(kw5, epsilon=1.0), len(kw4)) self.assertEqual(kw4.firstDifferent(kw5, epsilon=0.0000001), 10)
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 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_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_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_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_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_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_mul(self): kw1 = EclKW("Name1", 10, EclDataType.ECL_INT) kw1.assign(10) kw2 = EclKW("Name1", 10, EclDataType.ECL_INT) kw2.assign(2) kw3 = kw1 * kw2 kw4 = kw1 + kw2 self.assertEqual(len(kw3), len(kw1)) self.assertEqual(len(kw4), len(kw1)) for v in kw3: self.assertEqual(v, 20) for v in kw4: self.assertEqual(v, 12)
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_compressed_copy(self): nx = 10 ny = 10 nz = 10 grid = GridGen.createRectangular( (nx,ny,nz) , (1,1,1) ) kw1 = EclKW("KW" , 1001 , EclDataType.ECL_INT ) with self.assertRaises(ValueError): cp = grid.compressedKWCopy( 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_string_alloc(self): kw = EclKW("KW", 10, EclDataType.ECL_STRING(30)) for i in range(10): kw[i] = str(i)*30 for i in range(10): self.assertEqual(str(i)*30, kw[i])
def test_abs(self): for ecl_type in [ EclDataType.ECL_CHAR, EclDataType.ECL_BOOL, EclDataType.ECL_STRING(32) ]: kw = EclKW("NAME", 10, ecl_type) with self.assertRaises(TypeError): abs_kw = abs(kw) kw = EclKW("NAME", 10, EclDataType.ECL_INT) for i in range(len(kw)): kw[i] = -i abs_kw = abs(kw) for i in range(len(kw)): self.assertEqual(kw[i], -i) self.assertEqual(abs_kw[i], i)
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 compressedKWCopy(self, kw): if len(kw) == self.getNumActive(): return kw.copy( ) elif len(kw) == self.getGlobalSize(): kw_copy = EclKW( kw.getName() , self.getNumActive() , kw.data_type) self._compressed_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_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")
def test_gc(self): kw1 = EclKW("KW1", 100, EclDataType.ECL_INT) kw2 = EclKW("KW2", 100, EclDataType.ECL_INT) kw3 = EclKW("KW3", 100, EclDataType.ECL_INT) for i in range(len(kw1)): kw1[i] = i kw2[i] = 2 * i kw3[i] = 3 * i kw_list = [kw1, kw2, kw2] with TestAreaContext("context") as ta: createFile("TEST", kw_list) gc.collect() kw_list2 = loadKeywords("TEST") for kw1, kw2 in zip(kw_list, kw_list2): self.assertEqual(kw1, kw2)