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_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=(1, )): 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 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_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_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_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_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 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_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_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])