Skip to content
Open
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
18 changes: 17 additions & 1 deletion OMPython/OMCSession.py
Original file line number Diff line number Diff line change
Expand Up @@ -1022,11 +1022,27 @@ 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 OMCSession implementation.
"""
raise OMCSessionException("OMCSessionPort does not support omc_run_data_update()!")
raise OMCSessionException(f"({self.__class__.__name__}) does not support omc_run_data_update()!")


class OMCSessionLocal(OMCSession):
Expand Down
16 changes: 8 additions & 8 deletions tests/test_ArrayDimension.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
12 changes: 6 additions & 6 deletions tests/test_FMIRegression.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand Down
19 changes: 9 additions & 10 deletions tests/test_ModelicaSystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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.OMCSessionDocker(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(
session=omc.omc_process,
session=omcs,
)
mod.model(
model_file=model_firstorder,
Expand Down
7 changes: 3 additions & 4 deletions tests/test_ModelicaSystemDoE.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.OMCSessionDocker(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,
session=omcp,
session=omcs,
simargs={"override": {'stopTime': 1.0}},
)

Expand Down
43 changes: 15 additions & 28 deletions tests/test_OMCPath.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,54 +15,41 @@
)


def test_OMCPath_OMCSessionZMQ():
om = OMPython.OMCSessionZMQ()

_run_OMCPath_checks(om)

del om


def test_OMCPath_OMCProcessLocal():
omp = OMPython.OMCSessionLocal()
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.OMCSessionDocker(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.OMCSessionWSL(
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()
Expand All @@ -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
4 changes: 2 additions & 2 deletions tests/test_OMSessionCmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@


def test_isPackage():
omczmq = OMPython.OMCSessionZMQ()
omccmd = OMPython.OMCSessionCmd(session=omczmq.omc_process)
omcs = OMPython.OMCSessionLocal()
omccmd = OMPython.OMCSessionCmd(session=omcs)
assert not omccmd.isPackage('Modelica')


Expand Down
63 changes: 30 additions & 33 deletions tests/test_ZMQ.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.OMCSessionPort(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.OMCSessionPort(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
24 changes: 9 additions & 15 deletions tests/test_docker.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,17 @@

@skip_on_windows
def test_docker():
omcp = OMPython.OMCSessionDocker(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.OMCSessionDockerContainer(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.OMCSessionDocker(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
Loading