def test_rpc_storage_with_simulation(self): config = self.createTestPath("local/snake_oil_no_data/snake_oil.ert") with RPCServiceContext("ert/server/rpc_storage/simulation_and_storage", config, store_area=True) as server: client = ErtRPCClient("localhost", server.port) realization_count = 2 client.prototypeStorage("SNAKEX", { "SNAKE_ID": float, "SNAKE_RUN": str }) case_names = ["test_run_0", "test_run_1"] for batch_id, case_name in enumerate(case_names): self.runSimulation(client, realization_count, batch_id, case_name) for (batch_id, case_name), iens in product(enumerate(case_names), range(realization_count)): result = client.getCustomKWResult(case_name, iens, "SNAKEX") self.assertEqual(result["SNAKE_RUN"], "batch_%d" % batch_id) snake_id = realization_count * batch_id + iens self.assertEqual(result["SNAKE_ID"], snake_id)
def test_runtime_geoid(self): config_rel_path = "local/snake_oil_no_data/snake_oil_GEO_ID.ert" geoid_config_path = self.createTestPath(config_rel_path) def assert_all_resolved(self, geo_id): runpath_root_fmt = "simulations/%s" resolved_path = runpath_root_fmt % geo_id unresolved_path = runpath_root_fmt % '<GEO_ID>' self.assertTrue(os.path.isdir(resolved_path), "Runpath '%s/..' not created" % resolved_path) self.assertFalse( os.path.isdir(unresolved_path), "Unresolved runpath '%s/..' was created" % unresolved_path) for geo_id in range(3): with RPCServiceContext("ert/server/rpc/geoid", geoid_config_path, store_area=True) as server: pert_id, iens = 0, 0 server.startSimulationBatch("testing_geoid_%d" % geo_id, "results_geoid_%d" % geo_id, 1) keywords = {"SNAKE_OIL_PARAM": 10 * [0.5]} server.addSimulation(geo_id, pert_id, iens, keywords) assert_all_resolved(self, geo_id) while not server.isRunning(): time.sleep(0.5) assert_all_resolved(self, geo_id)
def test_rpc_storage(self): config = self.createTestPath("local/snake_oil_no_data/snake_oil.ert") with RPCServiceContext("ert/server/rpc_storage/storage", config) as server: client = ErtRPCClient("localhost", server.port) group_name = "Test" storage_definition = { "PI": float, "DakotaVersion": str, "Gradient": float, "GradientDirection": str } client.prototypeStorage(group_name, storage_definition) with self.assertRaises(UserWarning): client.prototypeStorage(group_name, {"value": float}) with self.assertRaises(TypeError): client.prototypeStorage(group_name, {"value": bool}) ensemble_config = server.ert.ensembleConfig() self.assertIn( group_name, ensemble_config.getKeylistFromImplType(ErtImplType.CUSTOM_KW)) custom_kw_config = ensemble_config.getNode( group_name).getCustomKeywordModelConfig() self.assertIn("PI", custom_kw_config) self.assertTrue(custom_kw_config.keyIsDouble("PI")) self.assertIn("DakotaVersion", custom_kw_config) self.assertFalse(custom_kw_config.keyIsDouble("DakotaVersion")) self.assertIn("Gradient", custom_kw_config) self.assertTrue(custom_kw_config.keyIsDouble("Gradient")) self.assertIn("GradientDirection", custom_kw_config) self.assertFalse(custom_kw_config.keyIsDouble("GradientDirection")) simulation_count = 10 initializeCase(server.ert, "default", simulation_count) client.storeGlobalData("default", group_name, "PI", 3.1415) client.storeGlobalData("default", group_name, "DakotaVersion", "DAKOTA 6.2.0") gradients = [ random() * 20.0 - 10.0 for _ in range(simulation_count) ] gradient_directions = [ ("POSITIVE" if gradient >= 0.0 else "NEGATIVE") for gradient in gradients ] for sim_id in range(simulation_count): gradient = gradients[sim_id] gradient_direction = gradient_directions[sim_id] client.storeSimulationData("default", group_name, "Gradient", gradient, sim_id) client.storeSimulationData("default", group_name, "GradientDirection", gradient_direction, sim_id) data = CustomKWCollector.loadAllCustomKWData(server.ert, "default") for sim_id in range(simulation_count): self.assertEqual(data["Test:PI"][sim_id], 3.1415) self.assertEqual(data["Test:DakotaVersion"][sim_id], "DAKOTA 6.2.0") self.assertFloatEqual(data["Test:Gradient"][sim_id], gradients[sim_id]) self.assertEqual(data["Test:GradientDirection"][sim_id], gradient_directions[sim_id])
def test_client_interaction(self): target_case_names = ["batch_1", ".batch_1", "batch_1", ".batch_1"] kw = [{ "SNAKE_OIL_PARAM": [0.50, 6, 1.750, 0.250, 0.990, 2, 1.770, 0.330, 0.550, 0.770] }, { "SNAKE_OIL_PARAM": [0.51, 7, 1.751, 0.251, 0.991, 3, 1.771, 0.331, 0.551, 0.771] }, { "SNAKE_OIL_PARAM": [0.52, 8, 1.752, 0.252, 0.992, 4, 1.772, 0.332, 0.552, 0.772] }, { "SNAKE_OIL_PARAM": [0.53, 9, 1.753, 0.253, 0.993, 5, 1.773, 0.333, 0.553, 0.773] }] expected_state_map = { "batch_1": [ RealizationStateEnum.STATE_HAS_DATA, RealizationStateEnum.STATE_UNDEFINED, RealizationStateEnum.STATE_HAS_DATA ], ".batch_1": [ RealizationStateEnum.STATE_UNDEFINED, RealizationStateEnum.STATE_HAS_DATA, RealizationStateEnum.STATE_UNDEFINED, RealizationStateEnum.STATE_HAS_DATA ] } expected_custom_kw = [{ 'RATING': 'EXCELLENT', 'NPV': 125692.534209 }, { 'RATING': 'AVERAGE', 'NPV': 87423.5773042 }, { 'RATING': 'GOOD', 'NPV': 113243.319848 }, { 'RATING': 'AVERAGE', 'NPV': 91781.8557083 }] with RPCServiceContext("ert/server/rpc/client", self.config) as server: client = ErtRPCClient("localhost", server.port) self.assertEqual(Version.currentVersion().versionTuple(), client.ertVersion()) realization_count = len(target_case_names) client.startSimulationBatch("default", realization_count) self.assertTrue(server.isInitializationCaseAvailable()) self.assertTrue(server.isRunning()) for iens in range(realization_count): client.addSimulation(target_case_names[iens], geo_id=0, pert_id=0, sim_id=iens, keywords=kw[iens]) self.assertTrue( realizationIsInitialized(server.ert, target_case_names[iens], iens)) while client.isRunning(): time.sleep(1) for case in expected_state_map: fs = server.ert.getEnkfFsManager().getFileSystem(case) state_map = fs.getStateMap() states = expected_state_map[case] for index, state in enumerate(states): self.assertEqual(state, state_map[index]) time_map = client.getTimeMap("batch_1") self.assertEqual(datetime.datetime(2010, 1, 1), time_map[0]) self.assertEqual(datetime.datetime(2015, 6, 13), time_map[199]) with self.assertRaises(KeyError): client.getGenDataResult(target_case_names[0], 0, 199, "UNKNOWN_KEYWORD") for iens, case in enumerate(target_case_names): self.assertTrue(client.isRealizationFinished(iens)) self.assertTrue(client.didRealizationSucceed(iens)) self.assertFalse(client.didRealizationFail(iens)) self.assertTrue(client.isCustomKWKey("SNAKE_OIL_NPV")) self.assertFalse(client.isGenDataKey("SNAKE_OIL_NPV")) self.assertTrue(client.isGenDataKey("SNAKE_OIL_OPR_DIFF")) result = client.getGenDataResult(case, iens, 199, "SNAKE_OIL_OPR_DIFF") self.assertEqual(len(result), 2000) result = client.getCustomKWResult(case, iens, "SNAKE_OIL_NPV") self.assertTrue("NPV" in result) self.assertTrue("RATING" in result) self.assertEqual(expected_custom_kw[iens]["RATING"], result["RATING"]) self.assertAlmostEqual(expected_custom_kw[iens]["NPV"], result["NPV"])
def test_multiple_threads(self): expected_ckw = { 0: { 'RATING': 'EXCELLENT', 'NPV': 125692.534209 }, 1: { 'RATING': 'AVERAGE', 'NPV': 87384.4316741 }, 2: { 'RATING': 'GOOD', 'NPV': 113181.024141 }, 3: { 'RATING': 'AVERAGE', 'NPV': 91659.8650599 }, 4: { 'RATING': 'EXCELLENT', 'NPV': 134891.570703 }, 5: { 'RATING': 'GOOD', 'NPV': 117270.977546 }, 6: { 'RATING': 'GOOD', 'NPV': 106838.28455 }, 7: { 'RATING': 'EXCELLENT', 'NPV': 144001.339 }, 8: { 'RATING': 'AVERAGE', 'NPV': 95423.9155004 }, 9: { 'RATING': 'AVERAGE', 'NPV': 96123.0227439 } } with RPCServiceContext("ert/server/rpc/multi_client", self.config, store_area=True) as server: client_count = len(expected_ckw) # initializeCase(server.ert, "default", 1) thread_success_state = {} def createClientInteractor(target_case_name, iens): def clientInteraction(): thread_success_state[iens] = False keywords = { "SNAKE_OIL_PARAM": [ 0.50, iens + 2, 1.750, 0.250, 0.990, 2 + client_count - iens, 1.770, 0.330, 0.550, 0.770 ] } client = ErtRPCClient("localhost", server.port) client.startSimulationBatch("default", client_count) self.assertTrue(client.isRunning()) self.assertTrue(client.isInitializationCaseAvailable()) client.addSimulation(target_case_name, 0, 0, iens, keywords) self.assertTrue( realizationIsInitialized(server.ert, target_case_name, iens)) while not client.isRealizationFinished(iens): time.sleep(0.5) self.assertTrue(client.didRealizationSucceed(iens)) result = client.getCustomKWResult(target_case_name, iens, "SNAKE_OIL_NPV") self.assertTrue("NPV" in result) self.assertTrue("RATING" in result) self.assertEqual(expected_ckw[iens]["RATING"], result["RATING"]) self.assertAlmostEqual(expected_ckw[iens]["NPV"], result["NPV"]) thread_success_state[iens] = True return clientInteraction threads = [] for iens in range(client_count): thread = Thread(name="client_%d" % iens) target_case_name = "target" if iens % 2 == 0 else ".target" thread.run = createClientInteractor(target_case_name, iens) threads.append(thread) for thread in threads: thread.start() while server.isRunning(): time.sleep(0.1) for thread in threads: thread.join() self.assertTrue( all(success for success in thread_success_state.values()))