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 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() init_file = self.ert().fieldInitFile(config_node) 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_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 openEclFile("G.EGRID") as f: with open("grid.grdecl" , "w") as f2: f2.write("SPECGRID\n") f2.write(" 10 10 10 \'F\' /\n") 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_scale_obs(self): with ErtTestContext("obs_test", self.config_file) as test_context: ert = test_context.getErt() obs = ert.getObservations() obs1 = obs["WWCT:OP_1"].getNode( 50 ) obs2 = obs["WWCT:OP_1_50"].getNode( 50 ) self.assertEqual( obs1.getStandardDeviation( ) , obs2.getStandardDeviation( )) std0 = obs1.getStandardDeviation( ) local_obsdata = LocalObsdata("obs" , obs) node1 = LocalObsdataNode( "WWCT:OP_1" ) node1.addRange( 50 , 50 ) node2 = LocalObsdataNode( "WWCT:OP_1_50" ) node2.addRange( 50 , 50 ) local_obsdata.addNode( node1 ) local_obsdata.addNode( node2 ) mask = BoolVector( default_value = True ) mask[2] = True meas_data = MeasData(mask) obs_data = ObsData( ) fs = ert.getEnkfFsManager().getCurrentFileSystem() active_list = IntVector() active_list.initRange(0,2,1) obs.getObservationAndMeasureData( fs , local_obsdata , EnkfStateType.FORECAST , active_list , meas_data , obs_data ) self.assertEqual( 2 , len(obs_data) ) v1 = obs_data[0] v2 = obs_data[1] self.assertEqual( v1[1] , std0 ) self.assertEqual( v2[1] , std0 ) meas_data = MeasData(mask) obs_data = ObsData( 10 ) obs.getObservationAndMeasureData( fs , local_obsdata , EnkfStateType.FORECAST , active_list , meas_data , obs_data ) self.assertEqual( 2 , len(obs_data) ) v1 = obs_data[0] v2 = obs_data[1] self.assertEqual( v1[1] , std0*10) self.assertEqual( v2[1] , std0*10 ) actl = ActiveList() obs1.updateStdScaling( 10 , actl) obs2.updateStdScaling( 20 , actl) meas_data = MeasData(mask) obs_data = ObsData( ) obs.getObservationAndMeasureData( fs , local_obsdata , EnkfStateType.FORECAST , active_list , meas_data , obs_data ) self.assertEqual( 2 , len(obs_data) ) v1 = obs_data[0] v2 = obs_data[1] self.assertEqual( v1[1] , std0*10) self.assertEqual( v2[1] , std0*20)
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_asList(self): v = IntVector() v[0] = 100 v[1] = 10 v[2] = 1 l = v.asList() self.assertListEqual( l , [100,10,1] )
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 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 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) 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)
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)
def cellsEqual(self, value): """ Will return a list [(i1,j1),(i2,j2) , ...(in,jn)] of all cells with value @value. """ i_list = IntVector() j_list = IntVector() Layer.cNamespace().cells_equal(self, 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 = EclSum.cNamespace().get_report_end( self , report_step ) index_list.append( time_index ) return index_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_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)) 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_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_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_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]) print a a <<= 2 print a 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 __init__(self , obs_key , data_config , scalar_value = None , obs_file = None , data_index = None): c_pointer = GenObservation.cNamespace().alloc( data_config , obs_key ) super(GenObservation, self).__init__(c_pointer) 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: GenObservation.cNamespace().load( self , obs_file ) else: obs_value , obs_std = scalar_value GenObservation.cNamespace().scalar_set( self , obs_value , obs_std ) if not data_index is None: if os.path.isfile( data_index ): GenObservation.cNamespace().load_data_index( self , data_index ) else: index_list = IntVector.active_list( data_index ) GenObservation.cNamespace().add_data_index( self , index_list )
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 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_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 , EclTypeEnum.ECL_INT_TYPE , global_active = True ) kw.assign( 0 ) kw[0: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 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_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 createRectangular(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, """ obj = object.__new__(cls) if actnum is None: c_ptr = cfunc.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])) c_ptr = cfunc.alloc_rectangular(dims[0], dims[1], dims[2], dV[0], dV[1], dV[2], actnum.getDataPtr()) obj.init_cobj(c_ptr, cfunc.free) return obj
def active_list(self): """ IntVector instance with active indices in the region. """ c_ptr = cfunc.get_active_list( self ) active_list = IntVector.asPythonReference( c_ptr , self ) return active_list
def createRectangular(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 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_true(self): iv = IntVector() self.assertFalse( iv ) # Will invoke the __len__ function; could override with __nonzero__ iv[0] = 1 self.assertTrue(iv)
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_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]) print a a <<= 2 print a 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_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 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 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_create( 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 , EclTypeEnum.ECL_FLOAT_TYPE ) self.assertEqual( len(kw) , grid.getNumActive()) self.assertEqual( (10,10,10) , kw.dims() )
def getEdgePolygon(self): x_list = DoubleVector() y_list = DoubleVector() cell_list = IntVector() self.cNamespace().trace_edge(self, 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_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_field_export_many(self): with ErtTestContext("export_test", self.config_file) as test_context: ert = test_context.getErt() fs_manager = ert.getEnkfFsManager( ) ens_config = ert.ensembleConfig() config_node = ens_config["PERMX"] iens_list = IntVector( ) iens_list.append(0) iens_list.append(2) iens_list.append(4) fs = fs_manager.getCurrentFileSystem( ) # Filename without embedded %d - TypeError with self.assertRaises(TypeError): EnkfNode.exportMany( config_node , "export/with/path/PERMX.grdecl" , fs , iens_list ) EnkfNode.exportMany( config_node , "export/with/path/PERMX_%d.grdecl" , fs , iens_list ) self.assertTrue( os.path.isfile("export/with/path/PERMX_0.grdecl") ) self.assertTrue( os.path.isfile("export/with/path/PERMX_2.grdecl") ) self.assertTrue( os.path.isfile("export/with/path/PERMX_4.grdecl") )
def test_field_export_many(self): with ErtTestContext("export_test", self.config_file) as test_context: ert = test_context.getErt() fs_manager = ert.getEnkfFsManager() ens_config = ert.ensembleConfig() config_node = ens_config["PERMX"] iens_list = IntVector() iens_list.append(0) iens_list.append(2) iens_list.append(4) fs = fs_manager.getCurrentFileSystem() # Filename without embedded %d - TypeError with self.assertRaises(TypeError): EnkfNode.exportMany(config_node, "export/with/path/PERMX.grdecl", fs, iens_list) EnkfNode.exportMany(config_node, "export/with/path/PERMX_%d.grdecl", fs, iens_list) self.assertTrue(os.path.isfile("export/with/path/PERMX_0.grdecl")) self.assertTrue(os.path.isfile("export/with/path/PERMX_2.grdecl")) self.assertTrue(os.path.isfile("export/with/path/PERMX_4.grdecl"))
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 getNeighbours(self): """ Will return a list of FaultBlock instances which are in direct contact with this block. """ neighbour_id_list = IntVector() self.cNamespace().get_neighbours( self , neighbour_id_list ) parent_layer = self.getParentLayer() neighbour_list = [] for id in neighbour_id_list: neighbour_list.append( parent_layer.getBlock( id )) return neighbour_list
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_create_global_size( 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 , EclTypeEnum.ECL_FLOAT_TYPE , global_active = True) self.assertEqual( len(kw) , grid.getGlobalSize()) kw.assign(50) self.assertEqual( kw[0,0,0] , 50 ) kw[0,0,0] = 45 self.assertEqual( kw[0,0,0] , 45 )
def exportACTNUM(self): actnum = IntVector( initial_size = self.getGlobalSize() ) cfunc.init_actnum( self , actnum.getDataPtr() ) return actnum
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_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_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 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_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 = copy.deepcopy(a) self.assertEqual(list(a), list(copy_of_a)) another_copy_of_a = copy.copy(a) self.assertEqual(list(a), list(another_copy_of_a))
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_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 kw_index_list(self , ecl_kw , force_active): c_ptr = cfunc.get_kw_index_list( self , ecl_kw , force_active) index_list = IntVector.asPythonReference( c_ptr , self ) return index_list