def test_EclFile_name_property(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(DeprecationWarning): name = f.name
def test_write(self): with TestAreaContext("surface/write"): s0 = Surface( self.surface_valid ) s0.write( "new_surface.irap") s1 = Surface( "new_surface.irap") self.assertTrue( s1 == s0 ) s0[0] = 99 self.assertFalse( s1 == s0 )
def test_enkf_create_config_file(self): config_file = "test_new_config" dbase_type = "BLOCK_FS" num_realizations = 42 with TestAreaContext("python/ens_condif/create_config", store_area=True) as ta: EnKFMain.createNewConfig(config_file, "storage", dbase_type, num_realizations) main = EnKFMain(config_file) self.assertEqual(main.getEnsembleSize(), num_realizations)
def test_truncated_smspec(self): with TestAreaContext("EclSum/truncated_smspec") as ta: ta.copy_file( self.test_file ) ta.copy_file( self.createTestPath( "Statoil/ECLIPSE/Gurbat/ECLIPSE.UNSMRY" )) file_size = os.path.getsize( "ECLIPSE.SMSPEC") with open("ECLIPSE.SMSPEC","r+") as f: f.truncate( file_size / 2 ) with self.assertRaises(IOError): EclSum( "ECLIPSE" )
def test_truncated_file(self): grid = EclGrid.createRectangular((10, 20, 30), (1, 1, 1)) with TestAreaContext("python/ecl_grid/truncated"): grid.save_EGRID("TEST.EGRID") size = os.path.getsize("TEST.EGRID") with open("TEST.EGRID", "r+") as f: f.truncate(size / 2) with self.assertRaises(IOError): EclGrid("TEST.EGRID")
def test_is_fortran_file(self): with TestAreaContext("python/fortio/guess"): kw1 = EclKW.create("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_fprintf(self): with TestAreaContext("python.ecl_kw"): self.kw_test(EclTypeEnum.ECL_INT_TYPE, [0, 1, 2, 3, 4, 5], "%4d\n") self.kw_test(EclTypeEnum.ECL_FLOAT_TYPE, [0.0, 1.1, 2.2, 3.3, 4.4, 5.5], "%12.6f\n") self.kw_test(EclTypeEnum.ECL_DOUBLE_TYPE, [0.0, 1.1, 2.2, 3.3, 4.4, 5.5], "%12.6f\n") self.kw_test(EclTypeEnum.ECL_BOOL_TYPE, [True, True, True, False, True], "%4d\n") self.kw_test(EclTypeEnum.ECL_CHAR_TYPE, ["1", "22", "4444", "666666", "88888888"], "%-8s\n")
def __init__(self, test_name, config_file, load_config=True, prefix=None, store_area=False): self.config_file = config_file self.load_config = load_config self.test_area_context = TestAreaContext(test_name, prefix=prefix, store_area=store_area)
def test_enkf_state(self): with TestAreaContext("enkf_library_test") as work_area: work_area.copy_directory(self.case_directory) main = EnKFMain("simple_config/minimum_config", self.site_config) state = main.getRealisation(0) with self.assertRaises(TypeError): state.addSubstKeyword("GEO_ID", 45) state.addSubstKeyword("GEO_ID", "45")
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_run_internal_script(self): with TestAreaContext("python/job_queue/workflow_job") as work_area: WorkflowCommon.createErtScriptsJob() config = self._alloc_config() job = self._alloc_from_file("SUBTRACT", config, "subtract_script_job") result = job.run(None, ["1", "2"]) self.assertEqual(result, -1)
def test_load_forward_model(self): with self.assertRaises(IOError): job = ExtJob("CONFIG_FILE" , True) with TestAreaContext("python/job_queue/forward_model1"): create_valid_config("CONFIG") job = ExtJob("CONFIG" , True) self.assertEqual( job.name() , "CONFIG") self.assertEqual( job.get_executable() , os.path.join( os.getcwd() , "script.sh")) self.assertTrue( os.access( job.get_executable() , os.X_OK )) job = ExtJob("CONFIG" , True , name = "Job") self.assertEqual( job.name() , "Job") pfx = 'ExtJob(' self.assertEqual(pfx, repr(job)[:len(pfx)]) with TestAreaContext("python/job_queue/forward_model2"): create_config_missing_executable( "CONFIG" ) with self.assertRaises(ValueError): job = ExtJob("CONFIG" , True) with TestAreaContext("python/job_queue/forward_model3"): create_config_missing_EXECUTABLE( "CONFIG" ) with self.assertRaises(ValueError): job = ExtJob("CONFIG" , True) with TestAreaContext("python/job_queue/forward_model4"): create_config_executable_directory( "CONFIG" ) with self.assertRaises(ValueError): job = ExtJob("CONFIG" , True) with TestAreaContext("python/job_queue/forward_model5"): create_config_foreign_file( "CONFIG" ) with self.assertRaises(ValueError): job = ExtJob("CONFIG" , True)
def test_fscanf(self): tm = TimeMap() with self.assertRaises(IOError): tm.fload( "Does/not/exist" ) with TestAreaContext("timemap/fload1") as work_area: with open("map.txt","w") as fileH: fileH.write("10/10/2000\n") fileH.write("12/10/2000\n") fileH.write("14/10/2000\n") fileH.write("16/10/2000\n") tm.fload("map.txt") self.assertEqual( 4 , len(tm) ) self.assertEqual( datetime.date(2000,10,10) , tm[0]) self.assertEqual( datetime.date(2000,10,16) , tm[3]) with TestAreaContext("timemap/fload2") as work_area: with open("map.txt","w") as fileH: fileH.write("10/10/200X\n") with self.assertRaises(Exception): tm.fload("map.txt") self.assertEqual( 4 , len(tm) ) self.assertEqual( datetime.date(2000,10,10) , tm[0]) self.assertEqual( datetime.date(2000,10,16) , tm[3]) with TestAreaContext("timemap/fload2") as work_area: with open("map.txt","w") as fileH: fileH.write("12/10/2000\n") fileH.write("10/10/2000\n") with self.assertRaises(Exception): tm.fload("map.txt") self.assertEqual( 4 , len(tm) ) self.assertEqual( datetime.date(2000,10,10) , tm[0]) self.assertEqual( datetime.date(2000,10,16) , tm[3])
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_time_map(self): port = self.base_port + 1 config_path = self.createTestPath("Statoil/config/with_data") with TestAreaContext("server/socket2") as work_area: work_area.copy_directory_content(config_path) self.startServer("config", port) data = self.runCommand(["TIME_STEP"], port) self.assertTrue(isinstance(data[0], datetime.datetime)) self.assertEqual(data[0], datetime.datetime(2000, 1, 1, 0, 0, 0)) self.runCommand(["QUIT"], port, expected=["QUIT"])
def test_one_job(self): with TestAreaContext("python/job_queue/forward_model_one_job"): for i in range(len(joblist)): forward_model = self.set_up_forward_model([i]) umask = 11 global_args = SubstitutionList() forward_model.formatted_fprintf( os.getcwd(), global_args, umask) self.verify_json_dump([i], global_args, umask)
def test_neighbours(self): with TestAreaContext( "python/fault_block_layer/neighbour") as work_area: with open("kw.grdecl", "w") as fileH: fileH.write("FAULTBLK \n") fileH.write("1 1 1 0 0\n") fileH.write("1 2 2 0 3\n") fileH.write("4 2 2 3 3\n") fileH.write("4 4 4 0 0\n") fileH.write("4 4 4 0 5\n") fileH.write("/\n") kw = EclKW.read_grdecl(open("kw.grdecl"), "FAULTBLK", ecl_type=EclTypeEnum.ECL_INT_TYPE) grid = EclGrid.create_rectangular((5, 5, 1), (1, 1, 1)) layer = FaultBlockLayer(grid, 0) layer.loadKeyword(kw) block1 = layer.getBlock(1) block2 = layer.getBlock(2) block3 = layer.getBlock(3) block4 = layer.getBlock(4) block5 = layer.getBlock(5) self.assertEqual(block1.getParentLayer(), layer) #Expected: 1 -> {2,4}, 2 -> {1,3,4}, 3 -> {2}, 4 -> {1,2}, 5-> {} neighbours = block1.getNeighbours() self.assertEqual(len(neighbours), 2) self.assertTrue(block2 in neighbours) self.assertTrue(block4 in neighbours) neighbours = block2.getNeighbours() self.assertEqual(len(neighbours), 3) self.assertTrue(block1 in neighbours) self.assertTrue(block3 in neighbours) self.assertTrue(block4 in neighbours) neighbours = block3.getNeighbours() self.assertEqual(len(neighbours), 1) self.assertTrue(block2 in neighbours) neighbours = block4.getNeighbours() self.assertEqual(len(neighbours), 2) self.assertTrue(block1 in neighbours) self.assertTrue(block2 in neighbours) neighbours = block5.getNeighbours() self.assertEqual(len(neighbours), 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.create("TEST", element_count, EclTypeEnum.ECL_INT_TYPE) 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)) freadIndexedData(fortio, 24, EclTypeEnum.ECL_INT_TYPE, 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_truncated(self): with TestAreaContext("python/ecl_file/truncated") as work_area: work_area.copy_file(self.test_file) size = os.path.getsize("ECLIPSE.UNRST") with open("ECLIPSE.UNRST", "r+") as f: f.truncate(size / 2) with self.assertRaises(IOError): rst_file = EclFile("ECLIPSE.UNRST") with self.assertRaises(IOError): rst_file = EclFile("ECLIPSE.UNRST", flags=EclFileFlagEnum.ECL_FILE_WRITABLE)
def test_load_save(self): rng = RandomNumberGenerator() with self.assertRaises(IOError): rng.loadState("does/not/exist") with TestAreaContext("rng_state") as t: rng.saveState("rng.txt") t.sync() val1 = rng.getInt() val2 = rng.getInt() rng.loadState("rng.txt") self.assertEqual(rng.getInt(), val1) self.assertEqual(rng.getInt(), val2)
def test_context(self): with TestAreaContext("python/fortio/context"): kw1 = EclKW.create("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 ) with openFortIO("file") as f: kw2 = EclKW.fread( f ) self.assertTrue( kw1 == kw2 )
def test_parse_invalid(self): conf = ConfigParser() conf.add("INT", value_type=ContentTypeEnum.CONFIG_INT) with TestAreaContext("config/parse2"): with open("config", "w") as fileH: fileH.write("INT xx\n") with self.assertRaises(ValueError): conf.parse("config") content = conf.parse("config", validate=False) self.assertFalse(content.isValid()) self.assertEqual(len(content.getErrors()), 1)
def test_dump_csv_line(self): ecl_sum_vector = EclSumKeyWordVector(self.ecl_sum) ecl_sum_vector.addKeywords("F*") with self.assertRaises(KeyError): ecl_sum_vector.addKeyword("MISSING") dtime = datetime.datetime( 2002 , 01 , 01 , 0 , 0 , 0 ) with TestAreaContext("EclSum/csv_dump"): test_file_name = self.createTestPath("dump.csv") outputH = open(test_file_name , "w") self.ecl_sum.dumpCSVLine( dtime, ecl_sum_vector, outputH) assert os.path.isfile(test_file_name)
def test_write_log(self): with TestAreaContext("python/ert_log/log") as work_area: test_log_filename = "test_log" ErtLog.cnamespace.init(1, test_log_filename, "unittester", True) message = "This is fun" ErtLog.log(1, message) self.assertTrue(os.path.isfile(test_log_filename)) with open(test_log_filename, "r") as f: text = f.readlines() self.assertTrue(len(text) > 0) self.assertTrue(message in text[-1])
def test_config(self): parser = ConfigParser() ps = PlotSettings() with TestAreaContext("plot_config"): with open("config_file", "w") as f: f.write("PLOT_SETTING PATH abc\n") f.write("PLOT_SETTING SHOW_REFCASE False\n") f.write("PLOT_SETTING UnknownKey Value\n") #_plot_settings_config( parser ) content = parser.parse("config_file") ps.apply(content)
def test_compare(self): with TestAreaContext("python/job_queue/workflow_job") as work_area: WorkflowCommon.createInternalFunctionJob() config = alloc_config() workflow_job = alloc_from_file("COMPARE", config, "compare_job") result = workflow_job.run(None, ["String", "string"]) self.assertNotEqual(result, 0) result = workflow_job.run(None, ["String", "String"]) # result is returned as c_void_p -> automatic conversion to None if value is 0 self.assertIsNone(result)
def test_dual(self): with TestAreaContext("python/grid-test/testDual"): grid = EclGrid(self.egrid_file()) self.assertFalse(grid.dualGrid()) self.assertTrue(grid.getNumActiveFracture() == 0) grid2 = EclGrid(self.grid_file()) self.assertFalse(grid.dualGrid()) self.assertTrue(grid.getNumActiveFracture() == 0) dgrid = EclGrid( self.createTestPath( "Statoil/ECLIPSE/DualPoro/DUALPOR_MSW.EGRID")) self.assertTrue( dgrid.getNumActive() == dgrid.getNumActiveFracture()) self.assertTrue(dgrid.getNumActive() == 46118) dgrid2 = EclGrid( self.createTestPath( "Statoil/ECLIPSE/DualPoro/DUALPOR_MSW.GRID")) self.assertTrue( dgrid.getNumActive() == dgrid.getNumActiveFracture()) self.assertTrue(dgrid.getNumActive() == 46118) self.assertTrue(dgrid.equal(dgrid2)) # The DUAL_DIFF grid has been manipulated to create a # situation where some cells are only matrix active, and some # cells are only fracture active. dgrid = EclGrid( self.createTestPath( "Statoil/ECLIPSE/DualPoro/DUAL_DIFF.EGRID")) self.assertTrue(dgrid.getNumActive() == 106) self.assertTrue(dgrid.getNumActiveFracture() == 105) self.assertTrue( dgrid.get_active_fracture_index(global_index=0) == -1) self.assertTrue( dgrid.get_active_fracture_index(global_index=2) == -1) self.assertTrue( dgrid.get_active_fracture_index(global_index=3) == 0) self.assertTrue( dgrid.get_active_fracture_index(global_index=107) == 104) self.assertTrue(dgrid.get_active_index(global_index=1) == 1) self.assertTrue(dgrid.get_active_index(global_index=105) == 105) self.assertTrue(dgrid.get_active_index(global_index=106) == -1) self.assertTrue(dgrid.get_global_index1F(2) == 5) dgrid.save_EGRID("DUAL_DIFF.EGRID") dgrid2 = EclGrid("DUAL_DIFF.EGRID") self.assertTrue(dgrid.equal(dgrid2, verbose=True))
def test_observations(self): with TestAreaContext("enkf_test") as work_area: work_area.copy_directory(self.case_directory) main = EnKFMain("simple_config/minimum_config") count = 10 summary_key = "test_key" observation_key = "test_obs_key" summary_observation_node = EnkfConfigNode.createSummaryConfigNode( summary_key, LoadFailTypeEnum.LOAD_FAIL_EXIT) observation_vector = ObsVector( EnkfObservationImplementationType.SUMMARY_OBS, observation_key, summary_observation_node, count) main.getObservations().addObservationVector(observation_vector) values = [] for index in range(0, count): value = index * 10.5 std = index / 10.0 summary_observation_node = SummaryObservation( summary_key, observation_key, value, std) observation_vector.installNode(index, summary_observation_node) self.assertEqual(observation_vector.getNode(index), summary_observation_node) self.assertEqual(value, summary_observation_node.getValue()) values.append((index, value, std)) observations = main.getObservations() test_vector = observations[observation_key] index = 0 for node in test_vector: self.assertTrue(isinstance(node, SummaryObservation)) self.assertEqual(node.getValue(), index * 10.5) index += 1 self.assertEqual(observation_vector, test_vector) for index, value, std in values: self.assertTrue(test_vector.isActive(index)) summary_observation_node = test_vector.getNode(index) """@type: SummaryObservation""" self.assertEqual(value, summary_observation_node.getValue()) self.assertEqual( std, summary_observation_node.getStandardDeviation()) self.assertEqual(summary_key, summary_observation_node.getSummaryKey()) main.free()
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_run_external_job(self): with TestAreaContext("python/job_queue/workflow_job") as work_area: WorkflowCommon.createExternalDumpJob() config = alloc_config() job = alloc_from_file("DUMP", config, "dump_job") self.assertFalse(job.isInternal()) self.assertIsNone(job.run(None, ["test", "text"])) with open("test", "r") as f: self.assertEqual(f.read(), "text")