def test_grdecl_load(self): with self.assertRaises(IOError): grid = EclGrid.loadFromGrdecl("/file/does/not/exists") with TestAreaContext("python/grid-test/grdeclLoad"): with open("grid.grdecl","w") as f: f.write("Hei ...") with self.assertRaises(ValueError): grid = EclGrid.loadFromGrdecl("grid.grdecl") actnum = IntVector(default_value = 1 , initial_size = 1000) actnum[0] = 0 g1 = EclGrid.createRectangular((10,10,10) , (1,1,1) , actnum = actnum ) self.assertEqual( g1.getNumActive() , actnum.elementSum() ) g1.save_EGRID("G.EGRID") with open("grid.grdecl" , "w") as f2: f2.write("SPECGRID\n") f2.write(" 10 10 10 \'F\' /\n") with openEclFile("G.EGRID") as f: with copen("grid.grdecl" , "a") as f2: coord_kw = f["COORD"][0] coord_kw.write_grdecl( f2 ) zcorn_kw = f["ZCORN"][0] zcorn_kw.write_grdecl( f2 ) actnum_kw = f["ACTNUM"][0] actnum_kw.write_grdecl( f2 ) g2 = EclGrid.loadFromGrdecl("grid.grdecl") self.assertTrue( g1.equal( g2 ))
def test_vector_operations_with_exceptions(self): iv1 = IntVector() iv1.append(1) iv1.append(2) iv1.append(3) iv2 = IntVector() iv2.append(4) iv2.append(5) dv1 = DoubleVector() dv1.append(0.5) dv1.append(0.75) dv1.append(0.25) # Size mismatch with self.assertRaises(ValueError): iv3 = iv1 + iv2 # Size mismatch with self.assertRaises(ValueError): iv3 = iv1 * iv2 # Type mismatch with self.assertRaises(TypeError): iv1 += dv1 # Type mismatch with self.assertRaises(TypeError): iv1 *= dv1
def create_gen_data_full( cls, key, result_file, input_format, report_steps, ecl_file, init_file_fmt, template_file, data_key, ): active_steps = IntVector() for step in report_steps: active_steps.append(step) config_node = cls._alloc_gen_data_full( key, result_file, input_format, active_steps, ecl_file, init_file_fmt, template_file, data_key, ) if config_node is None: raise ValueError( "Failed to create GEN_DATA with FULL specs node for:%s" % key ) return config_node
def exportFIELD(self, line): arguments = splitArguments(line) if len(arguments) >= 1: ens_config = self.ert().ensembleConfig() key = arguments[0] if key in self.supportedFIELDKeys(): config_node = ens_config[key] if len(arguments) >= 2: path_fmt = arguments[1] else: path_fmt = Export.DEFAULT_EXPORT_PATH % (key, key) + ".grdecl" if len(arguments) >= 3: range_string = "".join(arguments[2:]) iens_list = IntVector.active_list(range_string) else: ens_size = self.ert().getEnsembleSize() iens_list = IntVector.createRange(0, ens_size, 1) fs_manager = self.ert().getEnkfFsManager() fs = fs_manager.getCurrentFileSystem() mc = self.ert().getModelConfig() init_file = config_node.getInitFile(mc.getRunpathFormat()) if init_file: print('Using init file: %s' % init_file) EnkfNode.exportMany(config_node, path_fmt, fs, iens_list, arg=init_file) else: self.lastCommandFailed("No such FIELD node: %s" % key) else: self.lastCommandFailed("Expected at least one argument: <keyword> received: '%s'" % line)
def test_asList(self): v = IntVector() v[0] = 100 v[1] = 10 v[2] = 1 l = v.asList() self.assertListEqual(l, [100, 10, 1])
def test_equal(self): v1 = IntVector() v1[3] = 99 v2 = IntVector() self.assertNotEqual(v1, v2) v2[3] = 99 self.assertEqual(v1, v2)
def test_element_sum(self): dv = DoubleVector() iv = IntVector() for i in range(10): dv.append(i + 1) iv.append(i + 1) self.assertEqual(dv.elementSum(), 55) self.assertEqual(iv.elementSum(), 55)
def create_gen_data(cls, key, file_fmt, report_steps=(0,)): active_steps = IntVector() for step in report_steps: active_steps.append(step) config_node = cls._alloc_gen_data_everest(key, file_fmt, active_steps) if config_node is None: raise ValueError("Failed to create GEN_DATA node for:%s" % key) return config_node
def cells_equal(self, value): """ Will return a list [(i1,j1),(i2,j2), ...(in,jn)] of all cells with value @value. """ i_list = IntVector() j_list = IntVector() self._cells_equal(value, i_list, j_list) ij_list= [] for (i,j) in zip(i_list, j_list): ij_list.append((i,j)) return ij_list
def report_index_list(self): """ Internal function for working with report_steps. """ first_report = self.first_report last_report = self.last_report index_list = IntVector() for report_step in range(first_report, last_report + 1): time_index = self._get_report_end(report_step) index_list.append(time_index) return index_list
def test_count_equal(self): v = IntVector(default_value=77) v[0] = 1 v[10] = 1 v[20] = 1 self.assertEqual(v.countEqual(1), 3) v = DoubleVector(default_value=77) v[0] = 1 v[10] = 1 v[20] = 1 self.assertEqual(v.countEqual(1), 3)
def test_activeList(self): active_list = IntVector.active_list("1,10,100-105") self.assertTrue(len(active_list) == 8) self.assertTrue(active_list[0] == 1) self.assertTrue(active_list[2] == 100) self.assertTrue(active_list[7] == 105) self.assertEqual(active_list.count(100), 1) active_list.append(100) active_list.append(100) self.assertEqual(active_list.count(100), 3) active_list = IntVector.active_list("1,10,100-105X") self.assertFalse(active_list)
def test_range(self): v = IntVector() v[10] = 99 with self.assertRaises(ValueError): v.initRange(1, 2, 0) self.range_test(v, 0, 5, 1) self.range_test(v, 0, 100, 3) self.range_test(v, 0, 100, -3) self.create_range_test(v, 0, 5, 1) self.create_range_test(v, 0, 100, 3) self.create_range_test(v, 0, 100, -3)
def test_shift(self): a = IntVector() a.append(1) a.append(2) a.append(3) a.append(4) a.append(5) with self.assertRaises(ValueError): a >> -1 with self.assertRaises(ValueError): a << -1 with self.assertRaises(ValueError): a << -6 b = a << 2 self.assertEqual(list(b), [3, 4, 5]) a <<= 2 self.assertEqual(list(a), [3, 4, 5]) b = a >> 2 self.assertEqual(list(b), [0, 0, 3, 4, 5]) a >>= 2 self.assertEqual(list(a), [0, 0, 3, 4, 5])
def test_init_linear(self): with self.assertRaises(ValueError): v = IntVector.create_linear(0, 10, 1) v = IntVector.create_linear(0, 10, 11) for i in range(len(v)): self.assertEqual(v[i], i) v = IntVector.create_linear(10, 0, 11) for i in range(len(v)): self.assertEqual(v[i], 10 - i) d = DoubleVector.create_linear(0, 1, 11) for i in range(len(d)): self.assertEqual(d[i], i * 0.10)
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 create_range_test(self, v, a, b, d): v = IntVector.createRange(a, b, d) r = range(a, b, d) self.assertEqual(len(v), len(r)) for a, b in zip(v, r): self.assertEqual(a, b)
def test_region_filter(self): nx = 10 ny = 10 nz = 1 actnum = IntVector( initial_size = nx*ny*nz , default_value = 1 ) actnum[nx*ny - 1] = 0 grid = EclGrid.createRectangular( (nx,ny,nz) , (1,1,1) , actnum = actnum) self.assertEqual( grid.getNumActive() , nx*ny*nz - 1 ) kw = Ecl3DKW.create( "REGIONS" , grid , EclDataType.ECL_INT , global_active = True ) kw.assign( 0 ) kw[0:int(nx*ny/2)] = 1 kw[5,2,0] = 0 kw[0,9,0] = 2 kw.fixUninitialized( grid ) # Not assigned because they are in contact with a '2'; these # two are problem cells. self.assertEqual( kw[0,ny - 2,0] , 0) self.assertEqual( kw[1,ny - 1,0] , 0) # Not assigned because it is inactive self.assertEqual( kw[nx - 1,ny - 1,0] , 0) self.assertEqual( kw[5,2,0] , 1 ) for j in range(5,10): self.assertEqual( kw[5,j,0] , 1 ) for i in range(10): self.assertEqual( kw[i,7,0] , 1 )
def loadGenData(ert: EnKFMain, case_name, key, report_step, realization_index=None): """@type ert: EnKFMain @type case_name: str @type key: str @type report_step: int @rtype: DataFrame In the returned dataframe the realisation index runs along the rows, and the gen_data element index runs vertically along the columns. """ fs = ert.getEnkfFsManager().getFileSystem(case_name) realizations = fs.realizationList(RealizationStateEnum.STATE_HAS_DATA) if realization_index: if realization_index not in realizations: raise IndexError(f"No such realization {realization_index}") realizations = IntVector.active_list(str(realization_index)) config_node = ert.ensembleConfig().getNode(key) config_node.getModelConfig() ensemble_data = EnsemblePlotGenData(config_node, fs, report_step) data_array = ensemble_data.getRealizations(realizations) realizations = numpy.array(realizations) return DataFrame(data=data_array, columns=realizations)
def test_truth_and_size(self): actnum = IntVector(initial_size=100, default_value=0) actnum[0:50] = 1 grid = EclGrid.createRectangular((10, 10, 1), (1, 1, 1), actnum=actnum) region = EclRegion(grid, False) self.assertFalse(region) self.assertEqual(0, region.active_size()) self.assertEqual(0, region.global_size()) region.select_all() self.assertTrue(region) self.assertEqual(50, region.active_size()) self.assertEqual(100, region.global_size()) region.deselect_all() self.assertFalse(region) self.assertEqual(0, region.active_size()) self.assertEqual(0, region.global_size()) region = EclRegion(grid, False) region.select_inactive() self.assertTrue(region) self.assertEqual(0, region.active_size()) self.assertEqual(50, region.global_size())
def test_obs_block_scale_std(self): with ErtTestContext("obs_test_scale", self.config_file) as test_context: ert = test_context.getErt() fs = ert.getEnkfFsManager().getCurrentFileSystem() active_list = IntVector() active_list.initRange(0, ert.getEnsembleSize(), 1) obs = ert.getObservations() obs_data = LocalObsdata("OBSxx", obs) obs_vector = obs["WWCT:OP_1"] obs_data.addObsVector(obs_vector) scale_factor = obs.scaleCorrelatedStd(fs, obs_data, active_list) for obs_node in obs_vector: for index in range(len(obs_node)): self.assertEqual(scale_factor, obs_node.getStdScaling(index))
def __init__(self , obs_key , data_config , scalar_value = None , obs_file = None , data_index = None): c_ptr = self._alloc( data_config , obs_key ) if c_ptr: super(GenObservation, self).__init__(c_ptr) else: raise ValueError('Unable to construct GenObservation with given obs_key and data_config!') if scalar_value is None and obs_file is None: raise ValueError("Exactly one the scalar_value and obs_file arguments must be present") if scalar_value is not None and obs_file is not None: raise ValueError("Exactly one the scalar_value and obs_file arguments must be present") if obs_file is not None: if not os.path.isfile( obs_file ): raise IOError("The file with observation data:%s does not exist" % obs_file ) else: self._load( obs_file ) else: obs_value , obs_std = scalar_value self._scalar_set( obs_value , obs_std ) if not data_index is None: if os.path.isfile( data_index ): self._load_data_index( data_index ) else: index_list = IntVector.active_list( data_index ) self._add_data_index( index_list )
def test_setitem(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 = Ecl3DKW("KW", grid, EclDataType.ECL_FLOAT, default_value=77) with self.assertRaises(IndexError): kw[1000] with self.assertRaises(IndexError): kw[0, 10, 100] with self.assertRaises(ValueError): kw[1, 1] with self.assertRaises(ValueError): kw[1, 1, 1, 1] kw.assign(99) self.assertEqual(kw[0, 0, 0], 77) self.assertEqual(kw[0, 0, 1], 99) with self.assertRaises(ValueError): kw[0, 0, 0] = 88 kw[0, 0, 1] = 100 self.assertEqual(kw[0, 0, 1], 100)
def test_true(self): iv = IntVector() self.assertFalse( iv ) # Will invoke the __len__ function; could override with __nonzero__ iv[0] = 1 self.assertTrue(iv)
def create_rectangular(cls, dims, dV, actnum=None): """ Will create a new rectangular grid. @dims = (nx,ny,nz) @dVg = (dx,dy,dz) With the default value @actnum == None all cells will be active, """ if actnum is None: ecl_grid = cls._alloc_rectangular(dims[0], dims[1], dims[2], dV[0], dV[1], dV[2], None) else: if not isinstance(actnum, IntVector): tmp = IntVector(initial_size=len(actnum)) for (index, value) in enumerate(actnum): tmp[index] = value actnum = tmp if not len(actnum) == dims[0] * dims[1] * dims[2]: raise ValueError( "ACTNUM size mismatch: len(ACTNUM):%d Expected:%d" % (len(actnum), dims[0] * dims[1] * dims[2])) ecl_grid = cls._alloc_rectangular(dims[0], dims[1], dims[2], dV[0], dV[1], dV[2], actnum.getDataPtr()) # If we have not succeeded in creatin the grid we *assume* the # error is due to a failed malloc. if ecl_grid is None: raise MemoryError("Failed to allocated regualar grid") return ecl_grid
def test_create(self): with self.assertRaises(ValueError): grid = GridGen.createRectangular((10, 20, 30), (1, 1, 1), actnum=[0, 1, 1, 2]) with self.assertRaises(ValueError): grid = GridGen.createRectangular((10, 20, 30), (1, 1, 1), actnum=IntVector(initial_size=10)) grid = GridGen.createRectangular( (10, 20, 30), (1, 1, 1)) # actnum=None -> all active self.assertEqual(grid.getNumActive(), 30 * 20 * 10) actnum = IntVector(default_value=1, initial_size=6000) actnum[0] = 0 actnum[1] = 0 grid = GridGen.createRectangular((10, 20, 30), (1, 1, 1), actnum=actnum) self.assertEqual(grid.getNumActive(), 30 * 20 * 10 - 2)
def test_init_linear(self): with self.assertRaises(ValueError): v = IntVector.create_linear(0, 10, 1) v = IntVector.create_linear(0,10,11) for i in range(len(v)): self.assertEqual(v[i], i) v = IntVector.create_linear(10,0,11) for i in range(len(v)): self.assertEqual(v[i], 10 - i) d = DoubleVector.create_linear(0,1,11) for i in range(len(d)): self.assertEqual( d[i] , i*0.10)
def test_div(self): v = IntVector() v[0] = 100 v[1] = 10 v[2] = 1 v /= 10 self.assertEqual(list(v), [10, 1, 0])
def test_pop(self): a = IntVector() a.append(1) a.append(2) self.assertEqual(a.pop(), 2) self.assertEqual(len(a), 1) self.assertEqual(a.pop(), 1) self.assertEqual(len(a), 0) with self.assertRaises(ValueError): a.pop()
def get_edge_polygon(self): x_list = DoubleVector() y_list = DoubleVector() cell_list = IntVector() self._trace_edge(x_list, y_list, cell_list) p = Polyline() for (x, y) in zip(x_list, y_list): p.addPoint(x, y) return p
def test_true_false(self): v = IntVector(default_value=77) self.assertFalse(v) v[10] = 77 self.assertTrue(v) v = DoubleVector(default_value=77) self.assertFalse(v) v[10] = 77 self.assertTrue(v)
def test_contains_int(self): iv = IntVector() iv[0] = 1 iv[1] = 10 iv[2] = 100 iv[3] = 1000 self.assertTrue(1 in iv) self.assertTrue(10 in iv) self.assertTrue(88 not in iv) self.assertTrue(99 not in iv)
def test_value_list(self): list2 = IntVector.valueList("3,10-12,0,1") self.assertTrue(len(list2) == 6) expected = [3, 10, 11, 12, 0, 1] for v1, v2 in zip(list2, expected): self.assertEqual(v1, v2)
def test_int_vector(self): a = IntVector() a.append(1) a.append(2) a.append(3) a.append(4) a.append(5) self.assertEqual(list(a), [1, 2, 3, 4, 5]) a.sort(reverse=True) self.assertEqual(list(a), [5, 4, 3, 2, 1]) self.assertTrue(a.max(), 5) self.assertTrue(a.min(), 1) self.assertTrue(a.minIndex(), 4) self.assertEqual(a.maxIndex(reverse=True), 0) self.assertEqual(a.maxIndex(reverse=False), 0) a[4] = 5 self.assertTrue(a[4] == 5) a_plus_one = a + 1 self.assertEqual(list(a_plus_one), [6, 5, 4, 3, 6]) sliced = a[0:3] self.assertEqual(list(sliced), [5, 4, 3]) with self.assertRaises(IndexError): item = a[6] copy_of_a = a.copy() self.assertEqual(list(a), list(copy_of_a)) another_copy_of_a = copy_of_a.copy( ) self.assertEqual(list(a), list(another_copy_of_a))
def test_unique(self): iv = IntVector() iv.append(1) iv.append(1) iv.append(1) iv.append(0) iv.append(1) iv.append(2) iv.append(2) iv.append(0) iv.append(3) iv.selectUnique() self.assertEqual(len(iv), 4) self.assertEqual(iv[0], 0) self.assertEqual(iv[1], 1) self.assertEqual(iv[2], 2) self.assertEqual(iv[3], 3)
def test_perm_vector(self): v = IntVector.createRange( 11 , 0 , -1 ) perm = v.permutationSort( ) self.assertEqual( perm[0] , 10 ) self.assertEqual( perm[5] , 5 ) self.assertEqual( perm[10] , 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 kw_index_list(self , ecl_kw , force_active): c_ptr = self._get_kw_index_list( ecl_kw , force_active) index_list = IntVector.createCReference( c_ptr, self ) return index_list
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])