diff --git a/OMPython/ModelicaSystem.py b/OMPython/ModelicaSystem.py index d6a3d654..4935ac73 100644 --- a/OMPython/ModelicaSystem.py +++ b/OMPython/ModelicaSystem.py @@ -51,9 +51,8 @@ from OMPython.OMCSession import ( OMCSessionException, OMCSessionRunData, - OMCSessionZMQ, - OMCProcess, - OMCProcessLocal, + OMCSession, + OMCSessionLocal, OMCPath, ) @@ -127,7 +126,7 @@ class ModelicaSystemCmd: def __init__( self, - session: OMCSessionZMQ, + session: OMCSession, runpath: OMCPath, modelname: Optional[str] = None, ) -> None: @@ -291,8 +290,10 @@ def parse_simflags(simflags: str) -> dict[str, Optional[str | dict[str, Any] | n The return data can be used as input for self.args_set(). """ - warnings.warn("The argument 'simflags' is depreciated and will be removed in future versions; " - "please use 'simargs' instead", DeprecationWarning, stacklevel=2) + warnings.warn(message="The argument 'simflags' is depreciated and will be removed in future versions; " + "please use 'simargs' instead", + category=DeprecationWarning, + stacklevel=2) simargs: dict[str, Optional[str | dict[str, Any] | numbers.Number]] = {} @@ -325,7 +326,7 @@ def parse_simflags(simflags: str) -> dict[str, Optional[str | dict[str, Any] | n class ModelicaSystem: """ - Class to simulate a Modelica model using OpenModelica via OMCSessionZMQ. + Class to simulate a Modelica model using OpenModelica via OMCSession. """ def __init__( @@ -333,7 +334,7 @@ def __init__( command_line_options: Optional[list[str]] = None, work_directory: Optional[str | os.PathLike] = None, omhome: Optional[str] = None, - omc_process: Optional[OMCProcess] = None, + session: Optional[OMCSession] = None, ) -> None: """Create a ModelicaSystem instance. To define the model use model() or convertFmu2Mo(). @@ -344,8 +345,8 @@ def __init__( work_directory: Path to a directory to be used for temporary files like the model executable. If left unspecified, a tmp directory will be created. - omhome: path to OMC to be used when creating the OMC session (see OMCSessionZMQ). - omc_process: definition of a (local) OMC process to be used. If + omhome: path to OMC to be used when creating the OMC session (see OMCSession). + session: definition of a (local) OMC session to be used. If unspecified, a new local session will be created. """ @@ -373,10 +374,10 @@ def __init__( self._linearized_outputs: list[str] = [] # linearization output list self._linearized_states: list[str] = [] # linearization states list - if omc_process is not None: - self._session = OMCSessionZMQ(omc_process=omc_process) + if session is not None: + self._session = session else: - self._session = OMCSessionZMQ(omhome=omhome) + self._session = OMCSessionLocal(omhome=omhome) # set commandLineOptions using default values or the user defined list if command_line_options is None: @@ -461,13 +462,13 @@ def model( if model_file is not None: file_path = pathlib.Path(model_file) # special handling for OMCProcessLocal - consider a relative path - if isinstance(self._session.omc_process, OMCProcessLocal) and not file_path.is_absolute(): + if isinstance(self._session, OMCSessionLocal) and not file_path.is_absolute(): file_path = pathlib.Path.cwd() / file_path if not file_path.is_file(): raise IOError(f"Model file {file_path} does not exist!") self._file_name = self.getWorkDirectory() / file_path.name - if (isinstance(self._session.omc_process, OMCProcessLocal) + if (isinstance(self._session, OMCSessionLocal) and file_path.as_posix() == self._file_name.as_posix()): pass elif self._file_name.is_file(): @@ -482,7 +483,7 @@ def model( if build: self.buildModel(variable_filter) - def session(self) -> OMCSessionZMQ: + def get_session(self) -> OMCSession: """ Return the OMC session used for this class. """ @@ -585,7 +586,7 @@ def buildModel(self, variableFilter: Optional[str] = None): def sendExpression(self, expr: str, parsed: bool = True) -> Any: try: - retval = self._session.sendExpression(expr, parsed) + retval = self._session.sendExpression(command=expr, parsed=parsed) except OMCSessionException as ex: raise ModelicaSystemError(f"Error executing {repr(expr)}: {ex}") from ex @@ -1197,7 +1198,7 @@ def plot( plot is created by OMC which needs access to the local display. This is not the case for docker and WSL. """ - if not isinstance(self._session.omc_process, OMCProcessLocal): + if not isinstance(self._session, OMCSessionLocal): raise ModelicaSystemError("Plot is using the OMC plot functionality; " "thus, it is only working if OMC is running locally!") @@ -1612,9 +1613,9 @@ def _createCSVData(self, csvfile: Optional[OMCPath] = None) -> OMCPath: for signal_name, signal_values in inputs.items(): signal = np.array(signal_values) interpolated_inputs[signal_name] = np.interp( - all_times, - signal[:, 0], # times - signal[:, 1], # values + x=all_times, + xp=signal[:, 0], # times + fp=signal[:, 1], # values ) # Write CSV file @@ -1954,7 +1955,7 @@ def __init__( variable_filter: Optional[str] = None, work_directory: Optional[str | os.PathLike] = None, omhome: Optional[str] = None, - omc_process: Optional[OMCProcess] = None, + session: Optional[OMCSession] = None, # simulation specific input # TODO: add more settings (simulation options, input options, ...) simargs: Optional[dict[str, Optional[str | dict[str, str] | numbers.Number]]] = None, @@ -1974,7 +1975,7 @@ def __init__( command_line_options=command_line_options, work_directory=work_directory, omhome=omhome, - omc_process=omc_process, + session=session, ) self._mod.model( model_file=model_file, @@ -1988,9 +1989,9 @@ def __init__( self._simargs = simargs if resultpath is None: - self._resultpath = self.session().omcpath_tempdir() + self._resultpath = self.get_session().omcpath_tempdir() else: - self._resultpath = self.session().omcpath(resultpath) + self._resultpath = self.get_session().omcpath(resultpath) if not self._resultpath.is_dir(): raise ModelicaSystemError("Argument resultpath must be set to a valid path within the environment used " f"for the OpenModelica session: {resultpath}!") @@ -2003,11 +2004,11 @@ def __init__( self._doe_def: Optional[dict[str, dict[str, Any]]] = None self._doe_cmd: Optional[dict[str, OMCSessionRunData]] = None - def session(self) -> OMCSessionZMQ: + def get_session(self) -> OMCSession: """ Return the OMC session used for this class. """ - return self._mod.session() + return self._mod.get_session() def prepare(self) -> int: """ @@ -2046,7 +2047,7 @@ def prepare(self) -> int: pk_value = pc_structure[idx_structure] if isinstance(pk_value, str): - pk_value_str = self.session().escape_str(pk_value) + pk_value_str = self.get_session().escape_str(pk_value) expression = f"setParameterValue({self._model_name}, {pk_structure}, \"{pk_value_str}\")" elif isinstance(pk_value, bool): pk_value_bool_str = "true" if pk_value else "false" @@ -2167,12 +2168,12 @@ def worker(worker_id, task_queue): raise ModelicaSystemError("Missing simulation definition!") resultfile = cmd_definition.cmd_result_path - resultpath = self.session().omcpath(resultfile) + resultpath = self.get_session().omcpath(resultfile) logger.info(f"[Worker {worker_id}] Performing task: {resultpath.name}") try: - returncode = self.session().run_model_executable(cmd_run_data=cmd_definition) + returncode = self.get_session().run_model_executable(cmd_run_data=cmd_definition) logger.info(f"[Worker {worker_id}] Simulation {resultpath.name} " f"finished with return code: {returncode}") except ModelicaSystemError as ex: diff --git a/OMPython/OMCSession.py b/OMPython/OMCSession.py index ee92ce9f..2d11b136 100644 --- a/OMPython/OMCSession.py +++ b/OMPython/OMCSession.py @@ -99,9 +99,9 @@ class OMCSessionCmd: Implementation of Open Modelica Compiler API functions. Depreciated! """ - def __init__(self, session: OMCSessionZMQ, readonly: bool = False): - if not isinstance(session, OMCSessionZMQ): - raise OMCSessionException("Invalid session definition!") + def __init__(self, session: OMCSession, readonly: bool = False): + if not isinstance(session, OMCSession): + raise OMCSessionException("Invalid OMC process definition!") self._session = session self._readonly = readonly self._omc_cache: dict[tuple[str, bool], Any] = {} @@ -286,14 +286,14 @@ def getClassNames(self, className=None, recursive=False, qualified=False, sort=F class OMCPathReal(pathlib.PurePosixPath): """ - Implementation of a basic (PurePosix)Path object which uses OMC as backend. The connection to OMC is provided via a - OMCSessionZMQ session object. + Implementation of a basic (PurePosix)Path object which uses OMC as backend. The connection to OMC is provided via an + instances of OMCSession* classes. PurePosixPath is selected to cover usage of OMC in docker or via WSL. Usage of specialised function could result in errors as well as usage on a Windows system due to slightly different definitions (PureWindowsPath). """ - def __init__(self, *path, session: OMCProcess) -> None: + def __init__(self, *path, session: OMCSession) -> None: super().__init__(*path) self._session = session @@ -301,7 +301,7 @@ def with_segments(self, *pathsegments): """ Create a new OMCPath object with the given path segments. - The original definition of Path is overridden to ensure session is set. + The original definition of Path is overridden to ensure the OMC session is set. """ return type(self)(*pathsegments, session=self._session) @@ -322,7 +322,7 @@ def is_absolute(self): Check if the path is an absolute path considering the possibility that we are running locally on Windows. This case needs special handling as the definition of is_absolute() differs. """ - if isinstance(self._session, OMCProcessLocal) and platform.system() == 'Windows': + if isinstance(self._session, OMCSessionLocal) and platform.system() == 'Windows': return pathlib.PureWindowsPath(self.as_posix()).is_absolute() return super().is_absolute() @@ -517,8 +517,6 @@ class OMCSessionRunData: cmd_model_executable: Optional[str] = None # additional library search path; this is mainly needed if OMCProcessLocal is run on Windows cmd_library_path: Optional[str] = None - # command timeout - cmd_timeout: Optional[float] = 10.0 # working directory to be used on the *local* system cmd_cwd_local: Optional[str] = None @@ -537,83 +535,6 @@ def get_cmd(self) -> list[str]: return cmdl -class OMCSessionZMQ: - """ - This class is handling an OMC session. It is a compatibility class for the new schema using OMCProcess* classes. - """ - - def __init__( - self, - timeout: float = 10.00, - omhome: Optional[str] = None, - omc_process: Optional[OMCProcess] = None, - ) -> None: - """ - Initialisation for OMCSessionZMQ - """ - warnings.warn(message="The class OMCSessionZMQ is depreciated and will be removed in future versions; " - "please use OMCProcess* classes instead!", - category=DeprecationWarning, - stacklevel=2) - - if omc_process is None: - omc_process = OMCProcessLocal(omhome=omhome, timeout=timeout) - elif not isinstance(omc_process, OMCProcess): - raise OMCSessionException("Invalid definition of the OMC process!") - self.omc_process = omc_process - - def __del__(self): - del self.omc_process - - @staticmethod - def escape_str(value: str) -> str: - """ - Escape a string such that it can be used as string within OMC expressions, i.e. escape all double quotes. - """ - return OMCProcess.escape_str(value=value) - - def omcpath(self, *path) -> OMCPath: - """ - Create an OMCPath object based on the given path segments and the current OMC session. - """ - return self.omc_process.omcpath(*path) - - def omcpath_tempdir(self, tempdir_base: Optional[OMCPath] = None) -> OMCPath: - """ - Get a temporary directory using OMC. It is our own implementation as non-local usage relies on OMC to run all - filesystem related access. - """ - return self.omc_process.omcpath_tempdir(tempdir_base=tempdir_base) - - def omc_run_data_update(self, omc_run_data: OMCSessionRunData) -> OMCSessionRunData: - """ - Modify data based on the selected OMCProcess implementation. - - Needs to be implemented in the subclasses. - """ - return self.omc_process.omc_run_data_update(omc_run_data=omc_run_data) - - @staticmethod - def run_model_executable(cmd_run_data: OMCSessionRunData) -> int: - """ - Run the command defined in cmd_run_data. This class is defined as static method such that there is no need to - keep instances of over classes around. - """ - return OMCProcess.run_model_executable(cmd_run_data=cmd_run_data) - - def execute(self, command: str): - return self.omc_process.execute(command=command) - - def sendExpression(self, command: str, parsed: bool = True) -> Any: - """ - Send an expression to the OMC server and return the result. - - The complete error handling of the OMC result is done within this method using '"getMessagesStringInternal()'. - Caller should only check for OMCSessionException. - """ - return self.omc_process.sendExpression(command=command, parsed=parsed) - - class PostInitCaller(type): """ Metaclass definition to define a new function __post_init__() which is called after all __init__() functions where @@ -641,7 +562,7 @@ def __call__(cls, *args, **kwargs): return obj -class OMCProcessMeta(abc.ABCMeta, PostInitCaller): +class OMCSessionMeta(abc.ABCMeta, PostInitCaller): """ Helper class to get a combined metaclass of ABCMeta and PostInitCaller. @@ -650,25 +571,24 @@ class OMCProcessMeta(abc.ABCMeta, PostInitCaller): """ -class OMCProcess(metaclass=OMCProcessMeta): +class OMCSession(metaclass=OMCSessionMeta): """ - Base class for an OMC session. This class contains common functionality for all OMC sessions. + Base class for an OMC session started via ZMQ. This class contains common functionality for all variants of an + OMC session definition. The main method is sendExpression() which is used to send commands to the OMC process. - The class expects an OMCProcess* on initialisation. It defines the type of OMC process to use: - - * OMCProcessLocal + The following variants are defined: - * OMCProcessPort + * OMCSessionLocal - * OMCProcessDocker + * OMCSessionPort - * OMCProcessDockerContainer + * OMCSessionDocker - * OMCProcessWSL + * OMCSessionDockerContainer - If no OMC process is defined, a local OMC process is initialized. + * OMCSessionWSL """ def __init__( @@ -677,12 +597,12 @@ def __init__( **kwargs, ) -> None: """ - Initialisation for OMCProcess + Initialisation for OMCSession """ # store variables self._timeout = timeout - # generate a random string for this session + # generate a random string for this instance of OMC self._random_string = uuid.uuid4().hex # get a temporary directory self._temp_dir = pathlib.Path(tempfile.gettempdir()) @@ -715,6 +635,9 @@ def __post_init__(self) -> None: """ Create the connection to the OMC server using ZeroMQ. """ + # set_timeout() is used to define the value of _timeout as it includes additional checks + self.set_timeout(timeout=self._timeout) + port = self.get_port() if not isinstance(port, str): raise OMCSessionException(f"Invalid content for port: {port}") @@ -732,8 +655,8 @@ def __del__(self): if isinstance(self._omc_zmq, zmq.Socket): try: self.sendExpression("quit()") - except OMCSessionException: - pass + except OMCSessionException as exc: + logger.warning(f"Exception on sending 'quit()' to OMC: {exc}! Continue nevertheless ...") finally: self._omc_zmq = None @@ -750,13 +673,51 @@ def __del__(self): self._omc_process.wait(timeout=2.0) except subprocess.TimeoutExpired: if self._omc_process: - logger.warning("OMC did not exit after being sent the quit() command; " + logger.warning("OMC did not exit after being sent the 'quit()' command; " "killing the process with pid=%s", self._omc_process.pid) self._omc_process.kill() self._omc_process.wait() finally: self._omc_process = None + def _timeout_loop( + self, + timeout: Optional[float] = None, + timestep: float = 0.1, + ): + """ + Helper (using yield) for while loops to check OMC startup / response. The loop is executed as long as True is + returned, i.e. the first False will stop the while loop. + """ + + if timeout is None: + timeout = self._timeout + if timeout <= 0: + raise OMCSessionException(f"Invalid timeout: {timeout}") + + timer = 0.0 + yield True + while True: + timer += timestep + if timer > timeout: + break + time.sleep(timestep) + yield True + yield False + + def set_timeout(self, timeout: Optional[float] = None) -> float: + """ + Set the timeout to be used for OMC communication (OMCSession). + + The defined value is set and the current value is returned. If None is provided as argument, nothing is changed. + """ + retval = self._timeout + if timeout is not None: + if timeout <= 0.0: + raise OMCSessionException(f"Invalid timeout value: {timeout}!") + self._timeout = timeout + return retval + @staticmethod def escape_str(value: str) -> str: """ @@ -766,15 +727,15 @@ def escape_str(value: str) -> str: def omcpath(self, *path) -> OMCPath: """ - Create an OMCPath object based on the given path segments and the current OMC session. + Create an OMCPath object based on the given path segments and the current OMCSession* class. """ # fallback solution for Python < 3.12; a modified pathlib.Path object is used as OMCPath replacement if sys.version_info < (3, 12): - if isinstance(self, OMCProcessLocal): + if isinstance(self, OMCSessionLocal): # noinspection PyArgumentList return OMCPath(*path) - raise OMCSessionException("OMCPath is supported for Python < 3.12 only if OMCProcessLocal is used!") + raise OMCSessionException("OMCPath is supported for Python < 3.12 only if OMCSessionLocal is used!") return OMCPath(*path, session=self) def omcpath_tempdir(self, tempdir_base: Optional[OMCPath] = None) -> OMCPath: @@ -808,11 +769,9 @@ def omcpath_tempdir(self, tempdir_base: Optional[OMCPath] = None) -> OMCPath: return tempdir - @staticmethod - def run_model_executable(cmd_run_data: OMCSessionRunData) -> int: + def run_model_executable(self, cmd_run_data: OMCSessionRunData) -> int: """ - Run the command defined in cmd_run_data. This class is defined as static method such that there is no need to - keep instances of over classes around. + Run the command defined in cmd_run_data. """ my_env = os.environ.copy() @@ -829,7 +788,7 @@ def run_model_executable(cmd_run_data: OMCSessionRunData) -> int: text=True, env=my_env, cwd=cmd_run_data.cmd_cwd_local, - timeout=cmd_run_data.cmd_timeout, + timeout=self._timeout, check=True, ) stdout = cmdres.stdout.strip() @@ -848,8 +807,10 @@ def run_model_executable(cmd_run_data: OMCSessionRunData) -> int: return returncode def execute(self, command: str): - warnings.warn("This function is depreciated and will be removed in future versions; " - "please use sendExpression() instead", DeprecationWarning, stacklevel=2) + warnings.warn(message="This function is depreciated and will be removed in future versions; " + "please use sendExpression() instead", + category=DeprecationWarning, + stacklevel=2) return self.sendExpression(command, parsed=False) @@ -861,34 +822,28 @@ def sendExpression(self, command: str, parsed: bool = True) -> Any: Caller should only check for OMCSessionException. """ - # this is needed if the class is not fully initialized or in the process of deletion - if hasattr(self, '_timeout'): - timeout = self._timeout - else: - timeout = 1.0 - if self._omc_zmq is None: - raise OMCSessionException("No OMC running. Please create a new instance of OMCProcess!") + raise OMCSessionException("No OMC running. Please create a new instance of OMCSession!") logger.debug("sendExpression(%r, parsed=%r)", command, parsed) - attempts = 0 - while True: + loop = self._timeout_loop(timestep=0.05) + while next(loop): try: self._omc_zmq.send_string(str(command), flags=zmq.NOBLOCK) break except zmq.error.Again: pass - attempts += 1 - if attempts >= 50: - # in the deletion process, the content is cleared. Thus, any access to a class attribute must be checked - try: - log_content = self.get_log() - except OMCSessionException: - log_content = 'log not available' - raise OMCSessionException(f"No connection with OMC (timeout={timeout}). " - f"Log-file says: \n{log_content}") - time.sleep(timeout / 50.0) + else: + # in the deletion process, the content is cleared. Thus, any access to a class attribute must be checked + try: + log_content = self.get_log() + except OMCSessionException: + log_content = 'log not available' + + logger.error(f"Docker did not start. Log-file says:\n{log_content}") + raise OMCSessionException(f"No connection with OMC (timeout={self._timeout}).") + if command == "quit()": self._omc_zmq.close() self._omc_zmq = None @@ -984,7 +939,7 @@ def sendExpression(self, command: str, parsed: bool = True) -> Any: raise OMCSessionException(f"OMC error occurred for 'sendExpression({command}, {parsed}):\n" f"{msg_long_str}") - if parsed is False: + if not parsed: return result try: @@ -998,7 +953,7 @@ def sendExpression(self, command: str, parsed: bool = True) -> Any: def get_port(self) -> Optional[str]: """ - Get the port to connect to the OMC process. + Get the port to connect to the OMC session. """ if not isinstance(self._omc_port, str): raise OMCSessionException(f"Invalid port to connect to OMC process: {self._omc_port}") @@ -1030,7 +985,7 @@ def _get_portfile_path(self) -> Optional[pathlib.Path]: @abc.abstractmethod def omc_run_data_update(self, omc_run_data: OMCSessionRunData) -> OMCSessionRunData: """ - Update the OMCSessionRunData object based on the selected OMCProcess implementation. + Update the OMCSessionRunData object based on the selected OMCSession implementation. The main point is the definition of OMCSessionRunData.cmd_model_executable which contains the specific command to run depending on the selected system. @@ -1040,9 +995,9 @@ def omc_run_data_update(self, omc_run_data: OMCSessionRunData) -> OMCSessionRunD raise NotImplementedError("This method must be implemented in subclasses!") -class OMCProcessPort(OMCProcess): +class OMCSessionPort(OMCSession): """ - OMCProcess implementation which uses a port to connect to an already running OMC server. + OMCSession implementation which uses a port to connect to an already running OMC server. """ def __init__( @@ -1052,16 +1007,32 @@ def __init__( super().__init__() self._omc_port = omc_port + @staticmethod + def run_model_executable(cmd_run_data: OMCSessionRunData) -> int: + """ + Run the command defined in cmd_run_data. This class is defined as static method such that there is no need to + keep instances of over classes around. + """ + raise OMCSessionException("OMCSessionPort does not support run_model_executable()!") + + def get_log(self) -> str: + """ + Get the log file content of the OMC session. + """ + log = f"No log available if OMC session is defined by port ({self.__class__.__name__})" + + return log + def omc_run_data_update(self, omc_run_data: OMCSessionRunData) -> OMCSessionRunData: """ - Update the OMCSessionRunData object based on the selected OMCProcess implementation. + Update the OMCSessionRunData object based on the selected OMCSession implementation. """ - raise OMCSessionException("OMCProcessPort does not support omc_run_data_update()!") + raise OMCSessionException(f"({self.__class__.__name__}) does not support omc_run_data_update()!") -class OMCProcessLocal(OMCProcess): +class OMCSessionLocal(OMCSession): """ - OMCProcess implementation which runs the OMC server locally on the machine (Linux / Windows). + OMCSession implementation which runs the OMC server locally on the machine (Linux / Windows). """ def __init__( @@ -1117,25 +1088,19 @@ def _omc_port_get(self) -> str: port = None # See if the omc server is running - attempts = 0 - while True: + loop = self._timeout_loop(timestep=0.1) + while next(loop): omc_portfile_path = self._get_portfile_path() - if omc_portfile_path is not None and omc_portfile_path.is_file(): # Read the port file with open(file=omc_portfile_path, mode='r', encoding="utf-8") as f_p: port = f_p.readline() break - if port is not None: break - - attempts += 1 - if attempts == 80.0: - raise OMCSessionException(f"OMC Server did not start (timeout={self._timeout}). " - f"Could not open file {omc_portfile_path}. " - f"Log-file says:\n{self.get_log()}") - time.sleep(self._timeout / 80.0) + else: + logger.error(f"Docker did not start. Log-file says:\n{self.get_log()}") + raise OMCSessionException(f"OMC Server did not start (timeout={self._timeout}).") logger.info(f"Local OMC Server is up and running at ZMQ port {port} " f"pid={self._omc_process.pid if isinstance(self._omc_process, subprocess.Popen) else '?'}") @@ -1144,7 +1109,7 @@ def _omc_port_get(self) -> str: def omc_run_data_update(self, omc_run_data: OMCSessionRunData) -> OMCSessionRunData: """ - Update the OMCSessionRunData object based on the selected OMCProcess implementation. + Update the OMCSessionRunData object based on the selected OMCSession implementation. """ # create a copy of the data omc_run_data_copy = dataclasses.replace(omc_run_data) @@ -1185,9 +1150,9 @@ def omc_run_data_update(self, omc_run_data: OMCSessionRunData) -> OMCSessionRunD return omc_run_data_copy -class OMCProcessDockerHelper(OMCProcess): +class OMCSessionDockerHelper(OMCSession): """ - Base class for OMCProcess implementations which run the OMC server in a Docker container. + Base class for OMCSession implementations which run the OMC server in a Docker container. """ def __init__( @@ -1216,8 +1181,8 @@ def _docker_process_get(self, docker_cid: str) -> Optional[DockerPopen]: if sys.platform == 'win32': raise NotImplementedError("Docker not supported on win32!") - docker_process = None - for _ in range(0, 40): + loop = self._timeout_loop(timestep=0.2) + while next(loop): docker_top = subprocess.check_output(["docker", "top", docker_cid]).decode().strip() docker_process = None for line in docker_top.split("\n"): @@ -1228,10 +1193,11 @@ def _docker_process_get(self, docker_cid: str) -> Optional[DockerPopen]: except psutil.NoSuchProcess as ex: raise OMCSessionException(f"Could not find PID {docker_top} - " "is this a docker instance spawned without --pid=host?") from ex - if docker_process is not None: break - time.sleep(self._timeout / 40.0) + else: + logger.error(f"Docker did not start. Log-file says:\n{self.get_log()}") + raise OMCSessionException(f"Docker based OMC Server did not start (timeout={self._timeout}).") return docker_process @@ -1253,8 +1219,8 @@ def _omc_port_get(self) -> str: raise OMCSessionException(f"Invalid docker container ID: {self._docker_container_id}") # See if the omc server is running - attempts = 0 - while True: + loop = self._timeout_loop(timestep=0.1) + while next(loop): omc_portfile_path = self._get_portfile_path() if omc_portfile_path is not None: try: @@ -1265,16 +1231,11 @@ def _omc_port_get(self) -> str: port = output.decode().strip() except subprocess.CalledProcessError: pass - if port is not None: break - - attempts += 1 - if attempts == 80.0: - raise OMCSessionException(f"Docker based OMC Server did not start (timeout={self._timeout}). " - f"Could not open port file {omc_portfile_path}. " - f"Log-file says:\n{self.get_log()}") - time.sleep(self._timeout / 80.0) + else: + logger.error(f"Docker did not start. Log-file says:\n{self.get_log()}") + raise OMCSessionException(f"Docker based OMC Server did not start (timeout={self._timeout}).") logger.info(f"Docker based OMC Server is up and running at port {port}") @@ -1301,7 +1262,7 @@ def get_docker_container_id(self) -> str: def omc_run_data_update(self, omc_run_data: OMCSessionRunData) -> OMCSessionRunData: """ - Update the OMCSessionRunData object based on the selected OMCProcess implementation. + Update the OMCSessionRunData object based on the selected OMCSession implementation. """ omc_run_data_copy = dataclasses.replace(omc_run_data) @@ -1322,7 +1283,7 @@ def omc_run_data_update(self, omc_run_data: OMCSessionRunData) -> OMCSessionRunD return omc_run_data_copy -class OMCProcessDocker(OMCProcessDockerHelper): +class OMCSessionDocker(OMCSessionDockerHelper): """ OMC process running in a Docker container. """ @@ -1442,30 +1403,29 @@ def _docker_omc_start(self) -> Tuple[subprocess.Popen, DockerPopen, str]: raise OMCSessionException(f"Invalid content for docker container ID file path: {docker_cid_file}") docker_cid = None - for _ in range(0, 40): + loop = self._timeout_loop(timestep=0.1) + while next(loop): try: with open(file=docker_cid_file, mode="r", encoding="utf-8") as fh: docker_cid = fh.read().strip() except IOError: pass - if docker_cid: + if docker_cid is not None: break - time.sleep(self._timeout / 40.0) - - if docker_cid is None: + else: logger.error(f"Docker did not start. Log-file says:\n{self.get_log()}") raise OMCSessionException(f"Docker did not start (timeout={self._timeout} might be too short " "especially if you did not docker pull the image before this command).") docker_process = self._docker_process_get(docker_cid=docker_cid) if docker_process is None: - raise OMCSessionException(f"Docker top did not contain omc process {self._random_string}. " - f"Log-file says:\n{self.get_log()}") + logger.error(f"Docker did not start. Log-file says:\n{self.get_log()}") + raise OMCSessionException(f"Docker top did not contain omc process {self._random_string}.") return omc_process, docker_process, docker_cid -class OMCProcessDockerContainer(OMCProcessDockerHelper): +class OMCSessionDockerContainer(OMCSessionDockerHelper): """ OMC process running in a Docker container (by container ID). """ @@ -1558,7 +1518,7 @@ def _docker_omc_start(self) -> Tuple[subprocess.Popen, DockerPopen]: return omc_process, docker_process -class OMCProcessWSL(OMCProcess): +class OMCSessionWSL(OMCSession): """ OMC process running in Windows Subsystem for Linux (WSL). """ @@ -1612,12 +1572,11 @@ def _omc_process_get(self) -> subprocess.Popen: return omc_process def _omc_port_get(self) -> str: - omc_portfile_path: Optional[pathlib.Path] = None port = None # See if the omc server is running - attempts = 0 - while True: + loop = self._timeout_loop(timestep=0.1) + while next(loop): try: omc_portfile_path = self._get_portfile_path() if omc_portfile_path is not None: @@ -1628,16 +1587,11 @@ def _omc_port_get(self) -> str: port = output.decode().strip() except subprocess.CalledProcessError: pass - if port is not None: break - - attempts += 1 - if attempts == 80.0: - raise OMCSessionException(f"WSL based OMC Server did not start (timeout={self._timeout}). " - f"Could not open port file {omc_portfile_path}. " - f"Log-file says:\n{self.get_log()}") - time.sleep(self._timeout / 80.0) + else: + logger.error(f"Docker did not start. Log-file says:\n{self.get_log()}") + raise OMCSessionException(f"WSL based OMC Server did not start (timeout={self._timeout}).") logger.info(f"WSL based OMC Server is up and running at ZMQ port {port} " f"pid={self._omc_process.pid if isinstance(self._omc_process, subprocess.Popen) else '?'}") @@ -1646,7 +1600,7 @@ def _omc_port_get(self) -> str: def omc_run_data_update(self, omc_run_data: OMCSessionRunData) -> OMCSessionRunData: """ - Update the OMCSessionRunData object based on the selected OMCProcess implementation. + Update the OMCSessionRunData object based on the selected OMCSession implementation. """ omc_run_data_copy = dataclasses.replace(omc_run_data) diff --git a/OMPython/__init__.py b/OMPython/__init__.py index 7d571a9b..069369bf 100644 --- a/OMPython/__init__.py +++ b/OMPython/__init__.py @@ -44,15 +44,16 @@ ModelicaSystemError, ) from OMPython.OMCSession import ( + OMCPath, OMCSessionCmd, OMCSessionException, OMCSessionRunData, - OMCSessionZMQ, - OMCProcessPort, - OMCProcessLocal, - OMCProcessDocker, - OMCProcessDockerContainer, - OMCProcessWSL, + OMCSession, + OMCSessionPort, + OMCSessionLocal, + OMCSessionDocker, + OMCSessionDockerContainer, + OMCSessionWSL, ) # global names imported if import 'from OMPython import *' is used @@ -63,13 +64,15 @@ 'ModelicaSystemDoE', 'ModelicaSystemError', + 'OMCPath', + 'OMCSessionCmd', 'OMCSessionException', 'OMCSessionRunData', - 'OMCSessionZMQ', - 'OMCProcessPort', - 'OMCProcessLocal', - 'OMCProcessDocker', - 'OMCProcessDockerContainer', - 'OMCProcessWSL', + 'OMCSession', + 'OMCSessionPort', + 'OMCSessionLocal', + 'OMCSessionDocker', + 'OMCSessionDockerContainer', + 'OMCSessionWSL', ] diff --git a/README.md b/README.md index 2fd6baa1..07ff11f5 100644 --- a/README.md +++ b/README.md @@ -40,8 +40,8 @@ help(OMPython) ``` ```python -from OMPython import OMCSessionZMQ -omc = OMCSessionZMQ() +from OMPython import OMCSessionLocal +omc = OMCSessionLocal() omc.sendExpression("getVersion()") ``` diff --git a/tests/test_ArrayDimension.py b/tests/test_ArrayDimension.py index 13b3c11b..6e80d53f 100644 --- a/tests/test_ArrayDimension.py +++ b/tests/test_ArrayDimension.py @@ -2,18 +2,18 @@ def test_ArrayDimension(tmp_path): - omc = OMPython.OMCSessionZMQ() + omcs = OMPython.OMCSessionLocal() - omc.sendExpression(f'cd("{tmp_path.as_posix()}")') + omcs.sendExpression(f'cd("{tmp_path.as_posix()}")') - omc.sendExpression('loadString("model A Integer x[5+1,1+6]; end A;")') - omc.sendExpression("getErrorString()") + omcs.sendExpression('loadString("model A Integer x[5+1,1+6]; end A;")') + omcs.sendExpression("getErrorString()") - result = omc.sendExpression("getComponents(A)") + result = omcs.sendExpression("getComponents(A)") assert result[0][-1] == (6, 7), "array dimension does not match" - omc.sendExpression('loadString("model A Integer y = 5; Integer x[y+1,1+9]; end A;")') - omc.sendExpression("getErrorString()") + omcs.sendExpression('loadString("model A Integer y = 5; Integer x[y+1,1+9]; end A;")') + omcs.sendExpression("getErrorString()") - result = omc.sendExpression("getComponents(A)") + result = omcs.sendExpression("getComponents(A)") assert result[-1][-1] == ('y+1', 10), "array dimension does not match" diff --git a/tests/test_FMIRegression.py b/tests/test_FMIRegression.py index b61b8d49..8a91c514 100644 --- a/tests/test_FMIRegression.py +++ b/tests/test_FMIRegression.py @@ -7,21 +7,21 @@ def buildModelFMU(modelName): - omc = OMPython.OMCSessionZMQ() + omcs = OMPython.OMCSessionLocal() tempdir = pathlib.Path(tempfile.mkdtemp()) try: - omc.sendExpression(f'cd("{tempdir.as_posix()}")') + omcs.sendExpression(f'cd("{tempdir.as_posix()}")') - omc.sendExpression("loadModel(Modelica)") - omc.sendExpression("getErrorString()") + omcs.sendExpression("loadModel(Modelica)") + omcs.sendExpression("getErrorString()") fileNamePrefix = modelName.split(".")[-1] exp = f'buildModelFMU({modelName}, fileNamePrefix="{fileNamePrefix}")' - fmu = omc.sendExpression(exp) + fmu = omcs.sendExpression(exp) assert os.path.exists(fmu) finally: - del omc + del omcs shutil.rmtree(tempdir, ignore_errors=True) diff --git a/tests/test_ModelicaSystem.py b/tests/test_ModelicaSystem.py index 8567c426..dd0321ec 100644 --- a/tests/test_ModelicaSystem.py +++ b/tests/test_ModelicaSystem.py @@ -47,14 +47,15 @@ def worker(): ) mod.simulate() mod.convertMo2Fmu(fmuType="me") + for _ in range(10): worker() def test_setParameters(): - omc = OMPython.OMCSessionZMQ() - model_path_str = omc.sendExpression("getInstallationDirectoryPath()") + "/share/doc/omc/testmodels" - model_path = omc.omcpath(model_path_str) + omcs = OMPython.OMCSessionLocal() + model_path_str = omcs.sendExpression("getInstallationDirectoryPath()") + "/share/doc/omc/testmodels" + model_path = omcs.omcpath(model_path_str) mod = OMPython.ModelicaSystem() mod.model( model_file=model_path / "BouncingBall.mo", @@ -87,9 +88,9 @@ def test_setParameters(): def test_setSimulationOptions(): - omc = OMPython.OMCSessionZMQ() - model_path_str = omc.sendExpression("getInstallationDirectoryPath()") + "/share/doc/omc/testmodels" - model_path = omc.omcpath(model_path_str) + omcs = OMPython.OMCSessionLocal() + model_path_str = omcs.sendExpression("getInstallationDirectoryPath()") + "/share/doc/omc/testmodels" + model_path = omcs.omcpath(model_path_str) mod = OMPython.ModelicaSystem() mod.model( model_file=model_path / "BouncingBall.mo", @@ -155,11 +156,9 @@ def test_customBuildDirectory(tmp_path, model_firstorder): @skip_on_windows @skip_python_older_312 def test_getSolutions_docker(model_firstorder): - omcp = OMPython.OMCProcessDocker(docker="openmodelica/openmodelica:v1.25.0-minimal") - omc = OMPython.OMCSessionZMQ(omc_process=omcp) - + omcs = OMPython.OMCSessionDocker(docker="openmodelica/openmodelica:v1.25.0-minimal") mod = OMPython.ModelicaSystem( - omc_process=omc.omc_process, + session=omcs, ) mod.model( model_file=model_firstorder, diff --git a/tests/test_ModelicaSystemCmd.py b/tests/test_ModelicaSystemCmd.py index 7eaf08ba..2480aad9 100644 --- a/tests/test_ModelicaSystemCmd.py +++ b/tests/test_ModelicaSystemCmd.py @@ -24,7 +24,7 @@ def mscmd_firstorder(model_firstorder): model_name="M", ) mscmd = OMPython.ModelicaSystemCmd( - session=mod.session(), + session=mod.get_session(), runpath=mod.getWorkDirectory(), modelname=mod._model_name, ) diff --git a/tests/test_ModelicaSystemDoE.py b/tests/test_ModelicaSystemDoE.py index f9d70011..0e8d6caa 100644 --- a/tests/test_ModelicaSystemDoE.py +++ b/tests/test_ModelicaSystemDoE.py @@ -69,15 +69,14 @@ def test_ModelicaSystemDoE_local(tmp_path, model_doe, param_doe): @skip_on_windows @skip_python_older_312 def test_ModelicaSystemDoE_docker(tmp_path, model_doe, param_doe): - omcp = OMPython.OMCProcessDocker(docker="openmodelica/openmodelica:v1.25.0-minimal") - omc = OMPython.OMCSessionZMQ(omc_process=omcp) - assert omc.sendExpression("getVersion()") == "OpenModelica 1.25.0" + omcs = OMPython.OMCSessionDocker(docker="openmodelica/openmodelica:v1.25.0-minimal") + assert omcs.sendExpression("getVersion()") == "OpenModelica 1.25.0" doe_mod = OMPython.ModelicaSystemDoE( model_file=model_doe, model_name="M", parameters=param_doe, - omc_process=omcp, + session=omcs, simargs={"override": {'stopTime': 1.0}}, ) diff --git a/tests/test_OMCPath.py b/tests/test_OMCPath.py index 4a053287..2ea8b8c8 100644 --- a/tests/test_OMCPath.py +++ b/tests/test_OMCPath.py @@ -15,54 +15,41 @@ ) -def test_OMCPath_OMCSessionZMQ(): - om = OMPython.OMCSessionZMQ() - - _run_OMCPath_checks(om) - - del om - - def test_OMCPath_OMCProcessLocal(): - omp = OMPython.OMCProcessLocal() - om = OMPython.OMCSessionZMQ(omc_process=omp) + omcs = OMPython.OMCSessionLocal() - _run_OMCPath_checks(om) + _run_OMCPath_checks(omcs) - del om + del omcs @skip_on_windows @skip_python_older_312 def test_OMCPath_OMCProcessDocker(): - omcp = OMPython.OMCProcessDocker(docker="openmodelica/openmodelica:v1.25.0-minimal") - om = OMPython.OMCSessionZMQ(omc_process=omcp) - assert om.sendExpression("getVersion()") == "OpenModelica 1.25.0" + omcs = OMPython.OMCSessionDocker(docker="openmodelica/openmodelica:v1.25.0-minimal") + assert omcs.sendExpression("getVersion()") == "OpenModelica 1.25.0" - _run_OMCPath_checks(om) + _run_OMCPath_checks(omcs) - del omcp - del om + del omcs @pytest.mark.skip(reason="Not able to run WSL on github") @skip_python_older_312 def test_OMCPath_OMCProcessWSL(): - omcp = OMPython.OMCProcessWSL( + omcs = OMPython.OMCSessionWSL( wsl_omc='omc', wsl_user='omc', timeout=30.0, ) - om = OMPython.OMCSessionZMQ(omc_process=omcp) - _run_OMCPath_checks(om) + _run_OMCPath_checks(omcs) - del omcp - del om + del omcs -def _run_OMCPath_checks(om: OMPython.OMCSessionZMQ): - p1 = om.omcpath_tempdir() +def _run_OMCPath_checks(omcs: OMPython.OMCSession): + p1 = omcs.omcpath_tempdir() p2 = p1 / 'test' p2.mkdir() assert p2.is_dir() @@ -81,14 +68,14 @@ def _run_OMCPath_checks(om: OMPython.OMCSessionZMQ): def test_OMCPath_write_file(tmpdir): - om = OMPython.OMCSessionZMQ() + omcs = OMPython.OMCSessionLocal() data = "abc # \\t # \" # \\n # xyz" - p1 = om.omcpath_tempdir() + p1 = omcs.omcpath_tempdir() p2 = p1 / 'test.txt' p2.write_text(data=data) assert data == p2.read_text() - del om + del omcs diff --git a/tests/test_OMSessionCmd.py b/tests/test_OMSessionCmd.py index be02136a..d3997ecf 100644 --- a/tests/test_OMSessionCmd.py +++ b/tests/test_OMSessionCmd.py @@ -2,8 +2,8 @@ def test_isPackage(): - omczmq = OMPython.OMCSessionZMQ() - omccmd = OMPython.OMCSessionCmd(session=omczmq) + omcs = OMPython.OMCSessionLocal() + omccmd = OMPython.OMCSessionCmd(session=omcs) assert not omccmd.isPackage('Modelica') @@ -13,7 +13,7 @@ def test_isPackage2(): model_name="Modelica.Electrical.Analog.Examples.CauerLowPassAnalog", libraries=["Modelica"], ) - omccmd = OMPython.OMCSessionCmd(session=mod.session()) + omccmd = OMPython.OMCSessionCmd(session=mod.get_session()) assert omccmd.isPackage('Modelica') diff --git a/tests/test_ZMQ.py b/tests/test_ZMQ.py index 45d517cd..1302a79d 100644 --- a/tests/test_ZMQ.py +++ b/tests/test_ZMQ.py @@ -14,58 +14,55 @@ def model_time_str(): @pytest.fixture -def om(tmp_path): +def omcs(tmp_path): origDir = pathlib.Path.cwd() os.chdir(tmp_path) - om = OMPython.OMCSessionZMQ() + omcs = OMPython.OMCSessionLocal() os.chdir(origDir) - return om + return omcs -def testHelloWorld(om): - assert om.sendExpression('"HelloWorld!"') == "HelloWorld!" +def testHelloWorld(omcs): + assert omcs.sendExpression('"HelloWorld!"') == "HelloWorld!" -def test_Translate(om, model_time_str): - assert om.sendExpression(model_time_str) == ("M",) - assert om.sendExpression('translateModel(M)') is True +def test_Translate(omcs, model_time_str): + assert omcs.sendExpression(model_time_str) == ("M",) + assert omcs.sendExpression('translateModel(M)') is True -def test_Simulate(om, model_time_str): - assert om.sendExpression(f'loadString("{model_time_str}")') is True - om.sendExpression('res:=simulate(M, stopTime=2.0)') - assert om.sendExpression('res.resultFile') +def test_Simulate(omcs, model_time_str): + assert omcs.sendExpression(f'loadString("{model_time_str}")') is True + omcs.sendExpression('res:=simulate(M, stopTime=2.0)') + assert omcs.sendExpression('res.resultFile') -def test_execute(om): +def test_execute(omcs): with pytest.deprecated_call(): - assert om.execute('"HelloWorld!"') == '"HelloWorld!"\n' - assert om.sendExpression('"HelloWorld!"', parsed=False) == '"HelloWorld!"\n' - assert om.sendExpression('"HelloWorld!"', parsed=True) == 'HelloWorld!' + assert omcs.execute('"HelloWorld!"') == '"HelloWorld!"\n' + assert omcs.sendExpression('"HelloWorld!"', parsed=False) == '"HelloWorld!"\n' + assert omcs.sendExpression('"HelloWorld!"', parsed=True) == 'HelloWorld!' -def test_omcprocessport_execute(om): - port = om.omc_process.get_port() - omcp = OMPython.OMCProcessPort(omc_port=port) +def test_omcprocessport_execute(omcs): + port = omcs.get_port() + omcs2 = OMPython.OMCSessionPort(omc_port=port) # run 1 - om1 = OMPython.OMCSessionZMQ(omc_process=omcp) - assert om1.sendExpression('"HelloWorld!"', parsed=False) == '"HelloWorld!"\n' + assert omcs.sendExpression('"HelloWorld!"', parsed=False) == '"HelloWorld!"\n' # run 2 - om2 = OMPython.OMCSessionZMQ(omc_process=omcp) - assert om2.sendExpression('"HelloWorld!"', parsed=False) == '"HelloWorld!"\n' + assert omcs2.sendExpression('"HelloWorld!"', parsed=False) == '"HelloWorld!"\n' - del om1 - del om2 + del omcs2 -def test_omcprocessport_simulate(om, model_time_str): - port = om.omc_process.get_port() - omcp = OMPython.OMCProcessPort(omc_port=port) +def test_omcprocessport_simulate(omcs, model_time_str): + port = omcs.get_port() + omcs2 = OMPython.OMCSessionPort(omc_port=port) - om = OMPython.OMCSessionZMQ(omc_process=omcp) - assert om.sendExpression(f'loadString("{model_time_str}")') is True - om.sendExpression('res:=simulate(M, stopTime=2.0)') - assert om.sendExpression('res.resultFile') != "" - del om + assert omcs2.sendExpression(f'loadString("{model_time_str}")') is True + omcs2.sendExpression('res:=simulate(M, stopTime=2.0)') + assert omcs2.sendExpression('res.resultFile') != "" + + del omcs2 diff --git a/tests/test_docker.py b/tests/test_docker.py index 8d68f11f..f1973599 100644 --- a/tests/test_docker.py +++ b/tests/test_docker.py @@ -10,23 +10,17 @@ @skip_on_windows def test_docker(): - omcp = OMPython.OMCProcessDocker(docker="openmodelica/openmodelica:v1.25.0-minimal") - om = OMPython.OMCSessionZMQ(omc_process=omcp) - assert om.sendExpression("getVersion()") == "OpenModelica 1.25.0" + omcs = OMPython.OMCSessionDocker(docker="openmodelica/openmodelica:v1.25.0-minimal") + assert omcs.sendExpression("getVersion()") == "OpenModelica 1.25.0" - omcpInner = OMPython.OMCProcessDockerContainer(dockerContainer=omcp.get_docker_container_id()) - omInner = OMPython.OMCSessionZMQ(omc_process=omcpInner) - assert omInner.sendExpression("getVersion()") == "OpenModelica 1.25.0" + omcsInner = OMPython.OMCSessionDockerContainer(dockerContainer=omcs.get_docker_container_id()) + assert omcsInner.sendExpression("getVersion()") == "OpenModelica 1.25.0" - omcp2 = OMPython.OMCProcessDocker(docker="openmodelica/openmodelica:v1.25.0-minimal", port=11111) - om2 = OMPython.OMCSessionZMQ(omc_process=omcp2) - assert om2.sendExpression("getVersion()") == "OpenModelica 1.25.0" + omcs2 = OMPython.OMCSessionDocker(docker="openmodelica/openmodelica:v1.25.0-minimal", port=11111) + assert omcs2.sendExpression("getVersion()") == "OpenModelica 1.25.0" - del omcp2 - del om2 + del omcs2 - del omcpInner - del omInner + del omcsInner - del omcp - del om + del omcs diff --git a/tests/test_optimization.py b/tests/test_optimization.py index be6945f3..d7494281 100644 --- a/tests/test_optimization.py +++ b/tests/test_optimization.py @@ -56,7 +56,7 @@ def test_optimization_example(tmp_path): r = mod.optimize() # it is necessary to specify resultfile, otherwise it wouldn't find it. resultfile_str = r["resultFile"] - resultfile_omcpath = mod.session().omcpath(resultfile_str) + resultfile_omcpath = mod.get_session().omcpath(resultfile_str) time, f, v = mod.getSolutions( varList=["time", "f", "v"], resultfile=resultfile_omcpath,