diff --git a/OMPython/ModelicaSystem.py b/OMPython/ModelicaSystem.py index 71432449..bdbebbc9 100644 --- a/OMPython/ModelicaSystem.py +++ b/OMPython/ModelicaSystem.py @@ -393,9 +393,9 @@ def __init__( self._linearized_states: list[str] = [] # linearization states list if omc_process is not None: - self._getconn = OMCSessionZMQ(omc_process=omc_process) + self._session = OMCSessionZMQ(omc_process=omc_process) else: - self._getconn = OMCSessionZMQ(omhome=omhome) + self._session = OMCSessionZMQ(omhome=omhome) # set commandLineOptions using default values or the user defined list if commandLineOptions is None: @@ -417,7 +417,7 @@ def __init__( self._lmodel = lmodel # may be needed if model is derived from other model self._model_name = modelName # Model class name if fileName is not None: - file_name = self._getconn.omcpath(fileName).resolve() + file_name = self._session.omcpath(fileName).resolve() else: file_name = None self._file_name: Optional[OMCPath] = file_name # Model file/package name @@ -451,7 +451,7 @@ def session(self) -> OMCSessionZMQ: """ Return the OMC session used for this class. """ - return self._getconn + return self._session def setCommandLineOptions(self, commandLineOptions: str): """ @@ -494,11 +494,11 @@ def setWorkDirectory(self, customBuildDirectory: Optional[str | os.PathLike] = N directory. If no directory is defined a unique temporary directory is created. """ if customBuildDirectory is not None: - workdir = self._getconn.omcpath(customBuildDirectory).absolute() + workdir = self._session.omcpath(customBuildDirectory).absolute() if not workdir.is_dir(): raise IOError(f"Provided work directory does not exists: {customBuildDirectory}!") else: - workdir = self._getconn.omcpath_tempdir().absolute() + workdir = self._session.omcpath_tempdir().absolute() if not workdir.is_dir(): raise IOError(f"{workdir} could not be created") @@ -534,7 +534,7 @@ def buildModel(self, variableFilter: Optional[str] = None): # check if the executable exists ... om_cmd = ModelicaSystemCmd( - session=self._getconn, + session=self._session, runpath=self.getWorkDirectory(), modelname=self._model_name, timeout=5.0, @@ -542,16 +542,16 @@ def buildModel(self, variableFilter: Optional[str] = None): # ... by running it - output help for command help om_cmd.arg_set(key="help", val="help") cmd_definition = om_cmd.definition() - returncode = self._getconn.run_model_executable(cmd_run_data=cmd_definition) + returncode = self._session.run_model_executable(cmd_run_data=cmd_definition) if returncode != 0: raise ModelicaSystemError("Model executable not working!") - xml_file = self._getconn.omcpath(buildModelResult[0]).parent / buildModelResult[1] + xml_file = self._session.omcpath(buildModelResult[0]).parent / buildModelResult[1] self._xmlparse(xml_file=xml_file) def sendExpression(self, expr: str, parsed: bool = True) -> Any: try: - retval = self._getconn.sendExpression(expr, parsed) + retval = self._session.sendExpression(expr, parsed) except OMCSessionException as ex: raise ModelicaSystemError(f"Error executing {repr(expr)}") from ex @@ -1024,7 +1024,7 @@ def simulate_cmd( """ om_cmd = ModelicaSystemCmd( - session=self._getconn, + session=self._session, runpath=self.getWorkDirectory(), modelname=self._model_name, timeout=timeout, @@ -1105,7 +1105,7 @@ def simulate( elif isinstance(resultfile, OMCPath): self._result_file = resultfile else: - self._result_file = self._getconn.omcpath(resultfile) + self._result_file = self._session.omcpath(resultfile) if not self._result_file.is_absolute(): self._result_file = self.getWorkDirectory() / resultfile @@ -1124,7 +1124,7 @@ def simulate( self._result_file.unlink() # ... run simulation ... cmd_definition = om_cmd.definition() - returncode = self._getconn.run_model_executable(cmd_run_data=cmd_definition) + returncode = self._session.run_model_executable(cmd_run_data=cmd_definition) # and check returncode *AND* resultfile if returncode != 0 and self._result_file.is_file(): # check for an empty (=> 0B) result file which indicates a crash of the model executable @@ -1148,12 +1148,12 @@ 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._getconn.omc_process, OMCProcessLocal): + if not isinstance(self._session.omc_process, OMCProcessLocal): raise ModelicaSystemError("Plot is using the OMC plot functionality; " "thus, it is only working if OMC is running locally!") if resultfile is not None: - plot_result_file = self._getconn.omcpath(resultfile) + plot_result_file = self._session.omcpath(resultfile) elif self._result_file is not None: plot_result_file = self._result_file else: @@ -1207,7 +1207,7 @@ def getSolutions( raise ModelicaSystemError("No result file found. Run simulate() first.") result_file = self._result_file else: - result_file = self._getconn.omcpath(resultfile) + result_file = self._session.omcpath(resultfile) # check if the result file exits if not result_file.is_file(): @@ -1709,7 +1709,7 @@ def linearize( ) om_cmd = ModelicaSystemCmd( - session=self._getconn, + session=self._session, runpath=self.getWorkDirectory(), modelname=self._model_name, timeout=timeout, @@ -1748,7 +1748,7 @@ def linearize( linear_file.unlink(missing_ok=True) cmd_definition = om_cmd.definition() - returncode = self._getconn.run_model_executable(cmd_run_data=cmd_definition) + returncode = self._session.run_model_executable(cmd_run_data=cmd_definition) if returncode != 0: raise ModelicaSystemError(f"Linearize failed with return code: {returncode}") if not linear_file.is_file(): @@ -2104,7 +2104,7 @@ def worker(worker_id, task_queue): logger.info(f"[Worker {worker_id}] Performing task: {resultpath.name}") try: - returncode = self._mod._getconn.run_model_executable(cmd_run_data=cmd_definition) + returncode = self.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/tests/test_ModelicaSystemCmd.py b/tests/test_ModelicaSystemCmd.py index 844bd8d4..3532b82a 100644 --- a/tests/test_ModelicaSystemCmd.py +++ b/tests/test_ModelicaSystemCmd.py @@ -19,7 +19,7 @@ def model_firstorder(tmp_path): def mscmd_firstorder(model_firstorder): mod = OMPython.ModelicaSystem(fileName=model_firstorder.as_posix(), modelName="M") mscmd = OMPython.ModelicaSystemCmd( - session=mod._getconn, + session=mod.session(), runpath=mod.getWorkDirectory(), modelname=mod._model_name, ) diff --git a/tests/test_OMSessionCmd.py b/tests/test_OMSessionCmd.py index 1588fac8..106a6cc7 100644 --- a/tests/test_OMSessionCmd.py +++ b/tests/test_OMSessionCmd.py @@ -10,7 +10,7 @@ def test_isPackage(): def test_isPackage2(): mod = OMPython.ModelicaSystem(modelName="Modelica.Electrical.Analog.Examples.CauerLowPassAnalog", lmodel=["Modelica"]) - omccmd = OMPython.OMCSessionCmd(session=mod._getconn) + omccmd = OMPython.OMCSessionCmd(session=mod.session()) assert omccmd.isPackage('Modelica') diff --git a/tests/test_optimization.py b/tests/test_optimization.py index a6764a6b..bacf0a27 100644 --- a/tests/test_optimization.py +++ b/tests/test_optimization.py @@ -51,7 +51,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._getconn.omcpath(resultfile_str) + resultfile_omcpath = mod.session().omcpath(resultfile_str) time, f, v = mod.getSolutions(["time", "f", "v"], resultfile=resultfile_omcpath.as_posix()) assert np.isclose(f[0], 10) assert np.isclose(f[-1], -10)