Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 19 additions & 19 deletions OMPython/ModelicaSystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand Down Expand Up @@ -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):
"""
Expand Down Expand Up @@ -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")

Expand Down Expand Up @@ -534,24 +534,24 @@ 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,
)
# ... 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

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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

Expand All @@ -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
Expand All @@ -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:
Expand Down Expand Up @@ -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():
Expand Down Expand Up @@ -1709,7 +1709,7 @@ def linearize(
)

om_cmd = ModelicaSystemCmd(
session=self._getconn,
session=self._session,
runpath=self.getWorkDirectory(),
modelname=self._model_name,
timeout=timeout,
Expand Down Expand Up @@ -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():
Expand Down Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion tests/test_ModelicaSystemCmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand Down
2 changes: 1 addition & 1 deletion tests/test_OMSessionCmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')


Expand Down
2 changes: 1 addition & 1 deletion tests/test_optimization.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down