def startSimulation(self): self._run_model.reset() self._snapshot_model.reset() self._tab_widget.clear() evaluator_server_config = EvaluatorServerConfig() def run(): asyncio.set_event_loop(asyncio.new_event_loop()) self._run_model.startSimulations( evaluator_server_config=evaluator_server_config, ) simulation_thread = Thread(name="ert_gui_simulation_thread") simulation_thread.setDaemon(True) simulation_thread.run = run simulation_thread.start() self._ticker.start(1000) tracker = EvaluatorTracker( self._run_model, ee_con_info=evaluator_server_config.get_connection_info(), ) worker = TrackerWorker(tracker) worker_thread = QThread() worker.done.connect(worker_thread.quit) worker.consumed_event.connect(self._on_tracker_event) worker.moveToThread(worker_thread) self.simulation_done.connect(worker.stop) self._worker = worker self._worker_thread = worker_thread worker_thread.started.connect(worker.consume_and_emit) self._worker_thread.start()
def test_tracking_progress( run_model: Union[BaseRunModel, ERT3RunModel], monitor_events: List[CloudEvent], brm_mutations: List[Tuple[str, Any]], expected_progress: float, make_mock_ee_monitor, ): """Tests progress by providing a list of CloudEvent and a list of arguments to apply to setattr(brm) where brm is an actual BaseRunModel instance. The CloudEvent are provided to the tracker via mocking an Ensemble Evaluator Monitor. PartialSnapshots allow realizations to progress, while iterating "iter" in CloudEvents allows phases to progress. Such progress should happen when events are yielded by the tracker. This combined progress is tested. The final update event and end event is also tested.""" if issubclass(run_model, ERT3RunModel): brm = run_model() else: brm = run_model(None, None, None) ee_config = EvaluatorServerConfig( custom_port_range=range(1024, 65535), custom_host="127.0.0.1" ) with patch( "ert.ensemble_evaluator.tracker.evaluator_tracker.create_ee_monitor" ) as mock_ee: mock_ee.return_value.__enter__.return_value = make_mock_ee_monitor( monitor_events.copy() ) tracker = EvaluatorTracker( brm, ee_config.get_connection_info(), next_ensemble_evaluator_wait_time=0.1 ) for attr, val in brm_mutations: setattr(brm, attr, val) tracker_gen = tracker.track() update_event = None for i in range(len(monitor_events)): update_event = next(tracker_gen) assert update_event.progress == expected_progress[i] assert isinstance(update_event, SnapshotUpdateEvent) brm._phase = brm._phase_count assert isinstance(next(tracker_gen), EndEvent)
def evaluate( ensemble: ert.ensemble_evaluator.Ensemble, custom_port_range: Optional[range] = None, ) -> Dict[int, Dict[str, ert.data.RecordTransmitter]]: config = EvaluatorServerConfig(custom_port_range=custom_port_range) run_model = ERT3RunModel() tracker = ert.ensemble_evaluator.EvaluatorTracker( run_model, config.get_connection_info(), ) monitor = Monitor(out=sys.stderr, color_always=False) # type: ignore ee = EnsembleEvaluator(ensemble=ensemble, config=config, iter_=0) executor = futures.ThreadPoolExecutor() future = executor.submit(_run, ee, run_model) monitor.monitor(tracker) # type: ignore result: Dict[int, Dict[str, ert.data.RecordTransmitter]] = future.result() return result
def test_tracking( extra_config, extra_poly_eval, cmd_line_arguments, num_successful, num_iters, progress, assert_present_in_snapshot, tmpdir, source_root, ): experiment_folder = "poly_example" shutil.copytree( os.path.join(source_root, "test-data", "local", f"{experiment_folder}"), os.path.join(str(tmpdir), f"{experiment_folder}"), ) config_lines = [ "INSTALL_JOB poly_eval2 POLY_EVAL\n" "SIMULATION_JOB poly_eval2\n", extra_config, ] with tmpdir.as_cwd(): with open(f"{experiment_folder}/poly.ert", "a") as fh: fh.writelines(config_lines) with fileinput.input(f"{experiment_folder}/poly_eval.py", inplace=True) as fin: for line in fin: if line.strip().startswith("coeffs"): print(extra_poly_eval) print(line, end="") parser = ArgumentParser(prog="test_main") parsed = ert_parser( parser, cmd_line_arguments, ) FeatureToggling.update_from_args(parsed) res_config = ResConfig(parsed.config) os.chdir(res_config.config_path) ert = EnKFMain(res_config, strict=True, verbose=parsed.verbose) facade = LibresFacade(ert) model = create_model( ert, facade.get_ensemble_size(), facade.get_current_case_name(), parsed, ) evaluator_server_config = EvaluatorServerConfig( custom_port_range=range(1024, 65535), custom_host="127.0.0.1" ) thread = threading.Thread( name="ert_cli_simulation_thread", target=model.start_simulations_thread, args=(evaluator_server_config,), ) thread.start() tracker = EvaluatorTracker( model, ee_con_info=evaluator_server_config.get_connection_info(), ) snapshots = {} for event in tracker.track(): if isinstance(event, FullSnapshotEvent): snapshots[event.iteration] = event.snapshot if isinstance(event, SnapshotUpdateEvent): if event.partial_snapshot is not None: snapshots[event.iteration].merge(event.partial_snapshot.data()) if isinstance(event, EndEvent): pass assert tracker._progress() == progress assert len(snapshots) == num_iters for iter_, snapshot in snapshots.items(): successful_reals = list( filter( lambda item: item[1].status == REALIZATION_STATE_FINISHED, snapshot.reals.items(), ) ) assert len(successful_reals) == num_successful for ( iter_expression, snapshot_expression, expected, ) in assert_present_in_snapshot: for i, snapshot in snapshots.items(): if re.match(iter_expression, str(i)): check_expression( snapshot.to_dict(), snapshot_expression, expected, f"Snapshot {i} did not match:\n", ) thread.join() FeatureToggling.reset()
def run_cli(args): res_config = ResConfig(args.config) # Create logger inside function to make sure all handlers have been added to # the root-logger. logger = logging.getLogger(__name__) logger.info( "Logging forward model jobs", extra={ "workflow_jobs": str(res_config.model_config.getForwardModel().joblist()) }, ) os.chdir(res_config.config_path) ert = EnKFMain(res_config, strict=True, verbose=args.verbose) facade = LibresFacade(ert) if args.mode == WORKFLOW_MODE: execute_workflow(ert, args.name) return model = create_model( ert, facade.get_ensemble_size(), facade.get_current_case_name(), args, ) # Test run does not have a current_case if "current_case" in args and args.current_case: facade.select_or_create_new_case(args.current_case) if (args.mode in [ ENSEMBLE_SMOOTHER_MODE, ITERATIVE_ENSEMBLE_SMOOTHER_MODE, ES_MDA_MODE ] and args.target_case == facade.get_current_case_name()): msg = ( "ERROR: Target file system and source file system can not be the same. " "They were both: {}.".format(args.target_case)) raise ErtCliError(msg) evaluator_server_config = EvaluatorServerConfig( custom_port_range=args.port_range) thread = threading.Thread( name="ert_cli_simulation_thread", target=model.start_simulations_thread, args=(evaluator_server_config, ), ) thread.start() tracker = EvaluatorTracker( model, ee_con_info=evaluator_server_config.get_connection_info()) out = open(os.devnull, "w") if args.disable_monitoring else sys.stderr monitor = Monitor(out=out, color_always=args.color_always) try: monitor.monitor(tracker) except (SystemExit, KeyboardInterrupt): print("\nKilling simulations...") tracker.request_termination() if args.disable_monitoring: out.close() thread.join() if model.hasRunFailed(): raise ErtCliError(model.getFailMessage())