Skip to content

Commit 5d23fcb

Browse files
committed
Merge branch 'ModelicaSystem_rename_getconn_session' into merge_all
2 parents a3a33a8 + 7bf3310 commit 5d23fcb

4 files changed

Lines changed: 25 additions & 22 deletions

File tree

OMPython/ModelicaSystem.py

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -384,9 +384,9 @@ def __init__(
384384
self._linearized_states: list[str] = [] # linearization states list
385385

386386
if omc_process is not None:
387-
self._getconn = OMCSessionZMQ(omc_process=omc_process)
387+
self._session = OMCSessionZMQ(omc_process=omc_process)
388388
else:
389-
self._getconn = OMCSessionZMQ(omhome=omhome)
389+
self._session = OMCSessionZMQ(omhome=omhome)
390390

391391
# set commandLineOptions if provided by users
392392
self.setCommandLineOptions(commandLineOptions=commandLineOptions)
@@ -400,7 +400,7 @@ def __init__(
400400
self._lmodel = lmodel # may be needed if model is derived from other model
401401
self._model_name = modelName # Model class name
402402
if fileName is not None:
403-
file_name = self._getconn.omcpath(fileName).resolve()
403+
file_name = self._session.omcpath(fileName).resolve()
404404
else:
405405
file_name = None
406406
self._file_name: Optional[OMCPath] = file_name # Model file/package name
@@ -430,6 +430,9 @@ def __init__(
430430
if build:
431431
self.buildModel(variableFilter)
432432

433+
def get_session(self) -> OMCSessionZMQ:
434+
return self._session
435+
433436
def setCommandLineOptions(self, commandLineOptions: Optional[str] = None):
434437
# set commandLineOptions if provided by users
435438
if commandLineOptions is None:
@@ -471,11 +474,11 @@ def setWorkDirectory(self, customBuildDirectory: Optional[str | os.PathLike] = N
471474
directory. If no directory is defined a unique temporary directory is created.
472475
"""
473476
if customBuildDirectory is not None:
474-
workdir = self._getconn.omcpath(customBuildDirectory).absolute()
477+
workdir = self._session.omcpath(customBuildDirectory).absolute()
475478
if not workdir.is_dir():
476479
raise IOError(f"Provided work directory does not exists: {customBuildDirectory}!")
477480
else:
478-
workdir = self._getconn.omcpath_tempdir().absolute()
481+
workdir = self._session.omcpath_tempdir().absolute()
479482
if not workdir.is_dir():
480483
raise IOError(f"{workdir} could not be created")
481484

@@ -511,24 +514,24 @@ def buildModel(self, variableFilter: Optional[str] = None):
511514

512515
# check if the executable exists ...
513516
om_cmd = ModelicaSystemCmd(
514-
session=self._getconn,
517+
session=self._session,
515518
runpath=self.getWorkDirectory(),
516519
modelname=self._model_name,
517520
timeout=5.0,
518521
)
519522
# ... by running it - output help for command help
520523
om_cmd.arg_set(key="help", val="help")
521524
cmd_definition = om_cmd.definition()
522-
returncode = self._getconn.run_model_executable(cmd_run_data=cmd_definition)
525+
returncode = self._session.run_model_executable(cmd_run_data=cmd_definition)
523526
if returncode != 0:
524527
raise ModelicaSystemError("Model executable not working!")
525528

526-
xml_file = self._getconn.omcpath(buildModelResult[0]).parent / buildModelResult[1]
529+
xml_file = self._session.omcpath(buildModelResult[0]).parent / buildModelResult[1]
527530
self._xmlparse(xml_file=xml_file)
528531

529532
def sendExpression(self, expr: str, parsed: bool = True) -> Any:
530533
try:
531-
retval = self._getconn.sendExpression(expr, parsed)
534+
retval = self._session.sendExpression(expr, parsed)
532535
except OMCSessionException as ex:
533536
raise ModelicaSystemError(f"Error executing {repr(expr)}") from ex
534537

@@ -1003,7 +1006,7 @@ def simulate_cmd(
10031006
"""
10041007

10051008
om_cmd = ModelicaSystemCmd(
1006-
session=self._getconn,
1009+
session=self._session,
10071010
runpath=self.getWorkDirectory(),
10081011
modelname=self._model_name,
10091012
timeout=timeout,
@@ -1083,7 +1086,7 @@ def simulate(
10831086
elif isinstance(resultfile, OMCPath):
10841087
self._result_file = resultfile
10851088
else:
1086-
self._result_file = self._getconn.omcpath(resultfile)
1089+
self._result_file = self._session.omcpath(resultfile)
10871090
if not self._result_file.is_absolute():
10881091
self._result_file = self.getWorkDirectory() / resultfile
10891092

@@ -1102,7 +1105,7 @@ def simulate(
11021105
self._result_file.unlink()
11031106
# ... run simulation ...
11041107
cmd_definition = om_cmd.definition()
1105-
returncode = self._getconn.run_model_executable(cmd_run_data=cmd_definition)
1108+
returncode = self._session.run_model_executable(cmd_run_data=cmd_definition)
11061109
# and check returncode *AND* resultfile
11071110
if returncode != 0 and self._result_file.is_file():
11081111
# check for an empty (=> 0B) result file which indicates a crash of the model executable
@@ -1157,7 +1160,7 @@ def getSolutions(
11571160
raise ModelicaSystemError("No result file found. Run simulate() first.")
11581161
result_file = self._result_file
11591162
else:
1160-
result_file = self._getconn.omcpath(resultfile)
1163+
result_file = self._session.omcpath(resultfile)
11611164

11621165
# check if the result file exits
11631166
if not result_file.is_file():
@@ -1652,7 +1655,7 @@ def linearize(
16521655
)
16531656

16541657
om_cmd = ModelicaSystemCmd(
1655-
session=self._getconn,
1658+
session=self._session,
16561659
runpath=self.getWorkDirectory(),
16571660
modelname=self._model_name,
16581661
timeout=timeout,
@@ -1692,7 +1695,7 @@ def linearize(
16921695
linear_file.unlink(missing_ok=True)
16931696

16941697
cmd_definition = om_cmd.definition()
1695-
returncode = self._getconn.run_model_executable(cmd_run_data=cmd_definition)
1698+
returncode = self._session.run_model_executable(cmd_run_data=cmd_definition)
16961699
if returncode != 0:
16971700
raise ModelicaSystemError(f"Linearize failed with return code: {returncode}")
16981701
if not linear_file.is_file():
@@ -1860,9 +1863,9 @@ def __init__(
18601863
self._timeout = timeout
18611864

18621865
if resultpath is None:
1863-
self._resultpath = self._mod._getconn.omcpath_tempdir()
1866+
self._resultpath = self._mod.get_session().omcpath_tempdir()
18641867
else:
1865-
self._resultpath = self._mod._getconn.omcpath(resultpath)
1868+
self._resultpath = self._mod.get_session().omcpath(resultpath)
18661869
if not self._resultpath.is_dir():
18671870
raise ModelicaSystemError("Argument resultpath must be set to a valid path within the environment used "
18681871
f"for the OpenModelica session: {resultpath}!")
@@ -2033,12 +2036,12 @@ def worker(worker_id, task_queue):
20332036
raise ModelicaSystemError("Missing simulation definition!")
20342037

20352038
resultfile = cmd_definition.cmd_result_path
2036-
resultpath = self._mod._getconn.omcpath(resultfile)
2039+
resultpath = self._mod.get_session().omcpath(resultfile)
20372040

20382041
logger.info(f"[Worker {worker_id}] Performing task: {resultpath.name}")
20392042

20402043
try:
2041-
returncode = self._mod._getconn.run_model_executable(cmd_run_data=cmd_definition)
2044+
returncode = self._mod.get_session().run_model_executable(cmd_run_data=cmd_definition)
20422045
logger.info(f"[Worker {worker_id}] Simulation {resultpath.name} "
20432046
f"finished with return code: {returncode}")
20442047
except ModelicaSystemError as ex:

tests/test_ModelicaSystemCmd.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def model_firstorder(tmp_path):
1919
def mscmd_firstorder(model_firstorder):
2020
mod = OMPython.ModelicaSystem(fileName=model_firstorder.as_posix(), modelName="M")
2121
mscmd = OMPython.ModelicaSystemCmd(
22-
session=mod._getconn,
22+
session=mod.get_session(),
2323
runpath=mod.getWorkDirectory(),
2424
modelname=mod._model_name,
2525
)

tests/test_OMSessionCmd.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ def test_isPackage():
1010
def test_isPackage2():
1111
mod = OMPython.ModelicaSystem(modelName="Modelica.Electrical.Analog.Examples.CauerLowPassAnalog",
1212
lmodel=["Modelica"])
13-
omccmd = OMPython.OMCSessionCmd(session=mod._getconn)
13+
omccmd = OMPython.OMCSessionCmd(session=mod.get_session())
1414
assert omccmd.isPackage('Modelica')
1515

1616

tests/test_optimization.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def test_optimization_example(tmp_path):
5151
r = mod.optimize()
5252
# it is necessary to specify resultfile, otherwise it wouldn't find it.
5353
resultfile_str = r["resultFile"]
54-
resultfile_omcpath = mod._getconn.omcpath(resultfile_str)
54+
resultfile_omcpath = mod.get_session().omcpath(resultfile_str)
5555
time, f, v = mod.getSolutions(["time", "f", "v"], resultfile=resultfile_omcpath.as_posix())
5656
assert np.isclose(f[0], 10)
5757
assert np.isclose(f[-1], -10)

0 commit comments

Comments
 (0)