Skip to content

Test #275

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 61 commits into from
Closed

Test #275

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
61 commits
Select commit Hold shift + click to select a range
ba103d7
[ModelicaSystem] simplify subprocess.Popen() => use subprocess.run()
syntron Apr 24, 2025
78e7ae2
[ModelicaSystem] add timeout to subprocess.run() in _run_cmd()
syntron Apr 24, 2025
b93f121
[ModelicaSystem._run_cmd()] differentiate between OM error and nonzer…
syntron Apr 25, 2025
2b92216
[ModelicaSystem.linearize()] fix docstring / add description for timeout
syntron Apr 25, 2025
64b72f3
[ModelicaSystem] simplify code in xmlparse()
syntron Apr 23, 2025
d9643e5
[ModelicaSystem] add ModelicaSystemError() for unhandled final else c…
syntron Apr 23, 2025
41f9876
[ModelicaSystem.getSolution()] do not try to continue on error but fail
syntron Apr 23, 2025
d066548
[ModelicaSystem] remove redundant parentheses (type hint by PyCharm)
syntron Apr 23, 2025
87a58b7
[OMCSessionException] add exception handling for OMCSession*
syntron Apr 24, 2025
e609860
[OMCSessionZMQ] use specific exceptions instead of generic Exception …
syntron Apr 24, 2025
0293214
[OMCSessionBase] use OMCSessionException instead of generic Exception
syntron Apr 25, 2025
0135cdc
[OMParser] use a single entry point for OMParser
syntron Apr 24, 2025
e0ff3ed
[OMTypedParser] use a single entry point for OMTypedParser
syntron Apr 24, 2025
8d642d5
[OMCSessionBase] fix exception handling for pyparsing.ParseException
syntron Apr 24, 2025
f16e486
[OMCSessionBase] simplify the two use cases of OMParser.om_parse_basic()
syntron Apr 24, 2025
7f423be
Merge branch 'fix_ModelicaSystem' into merge
syntron Apr 29, 2025
b9a70c0
Merge branch 'parser' into merge
syntron Apr 29, 2025
9b81f3d
Merge branch 'use_subprocess.run' into merge
syntron Apr 29, 2025
4c66612
[DummyPopen] remove redundant parentheses
syntron Apr 24, 2025
30dbf35
Fix two spaces in simflags causing error
ondras12345 Apr 12, 2025
ebe6961
[ModelicaSystem] remove not needed variables
syntron Apr 26, 2025
882b127
[ModelicaSystem] csvFile
syntron Apr 26, 2025
3662fd5
[ModelicaSystem] tempdir
syntron Apr 26, 2025
52df1d8
[ModelicaSystem] batFilePath
syntron Apr 26, 2025
3878503
[ModelicaSystem] fileName / lmodel
syntron Apr 26, 2025
1ee22a6
[tests] fix definition of lmodel - should be a list
syntron Apr 28, 2025
9a50b88
[ModelicaSystem] resultfile
syntron Apr 26, 2025
498eebe
[ModelicaSystem] check session
syntron Apr 26, 2025
be1fa57
[ModelicaSystem] remove _check_error()
syntron Apr 24, 2025
d1747ca
[ModelicaSystem] static strip_space()
syntron Apr 26, 2025
27a1499
[ModelicaSystem] exception handling
syntron Apr 26, 2025
2bc107a
[ModelicaSystem] remove _raise_error()
syntron Apr 28, 2025
6864a46
[tests] remove raiseerror=True
syntron Apr 28, 2025
a4a62e5
[ModelicaSystem] remove verbose - use logger.debug
syntron Apr 26, 2025
9363b77
[ModelicaSystem] check that lmodel is defined as list
syntron Apr 28, 2025
07ed38b
[OMCSessionZMQ] use pathlib.Path()
syntron Apr 28, 2025
f07fa7f
[OMCSessionBase.ask()] define opt as list
syntron Apr 25, 2025
2b51501
rename [OMCSessionBase] => [OMCSessionCmd]; remove dependencies
syntron Apr 26, 2025
822013b
[OMCSessionZMQ] remove unused argument readonly
syntron Apr 28, 2025
8b20545
[OMCSessionCmd] restore test_ZMQ - move execute() back into OMCSessio…
syntron Apr 28, 2025
ca41560
[OMCSessionCmd] make sendExpression() available
syntron Apr 28, 2025
b06d28a
[tests] new test for OMCSessionCmd
syntron Apr 28, 2025
e4d95ef
Merge branch 'update_DummyPopen' into merge2
syntron Apr 29, 2025
621b7aa
Merge branch 'update_OMCSession' into merge2
syntron Apr 29, 2025
7c1575b
[OMCSessionZMQ] allways check for errors if using sendExpression()
syntron Apr 26, 2025
fb239c2
[OMCSession*] move logging into OMCSessionZMQ.sendExpression()
syntron Apr 29, 2025
02a7989
[ModelicaSystem] exception handling for sendExpression()
syntron Apr 28, 2025
3dd1ba8
[ModelicaSystem] remove last call to getErrorString()
syntron Apr 29, 2025
6547f14
[ModelicaSystemCmd] draft
syntron Apr 26, 2025
b9c04b2
[ModelicaSystemCmd] define and use it - needs cleanup!
syntron Apr 28, 2025
5cc7fa5
[ModelicaSystemCmd] update handling of simargs
syntron Apr 28, 2025
e680f1f
[ModelicaSystemCmd] move handling of simflags info this class
syntron Apr 28, 2025
066fc03
[ModelicaSystemCmd] cleanup / docstrings
syntron Apr 28, 2025
e23dfec
[ModelicaSystemCmd] simplify
syntron Apr 28, 2025
4b0faba
[__init__] make ModelicaSystemCmd available
syntron Apr 28, 2025
2bd6754
[ModelicaSystemCmd] special handling for override in simflags / simargs
syntron Apr 28, 2025
308eb3b
[ModelicaSystemCmd] split run() - create command in get_cmd() and get…
syntron Apr 28, 2025
6bfecef
[ModelicaSystem] use pathlib.Path() / simplify
syntron Apr 28, 2025
2d0c3d0
[ModelicaSystemCmd] do *NOT* raise error if returncode != 0
syntron Apr 28, 2025
d6fd226
[tests] for ModelicaSystemCmd
syntron Apr 28, 2025
ae5d6c4
[ModelicaSystem] fix flake8 error
syntron Apr 29, 2025
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
614 changes: 374 additions & 240 deletions OMPython/ModelicaSystem.py

Large diffs are not rendered by default.

276 changes: 139 additions & 137 deletions OMPython/OMCSession.py

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions OMPython/OMParser.py
Original file line number Diff line number Diff line change
Expand Up @@ -892,3 +892,15 @@ def check_for_values(string):
check_for_values(next_set)

return result


# TODO: hack to be able to use one entry point wich also resets the (global) variable results
# this should be checked such that the content of this file can be used as class with correct handling of
# variable usage
def om_parser_basic(string: str):
result_return = check_for_values(string=string)

global result
result = {}

return result_return
8 changes: 5 additions & 3 deletions OMPython/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,17 @@
CONDITIONS OF OSMC-PL.
"""

from OMPython.OMCSession import OMCSessionBase, OMCSessionZMQ
from OMPython.ModelicaSystem import ModelicaSystem, ModelicaSystemError, LinearizationResult
from OMPython.OMCSession import OMCSessionCmd, OMCSessionZMQ, OMCSessionException
from OMPython.ModelicaSystem import ModelicaSystem, ModelicaSystemCmd, ModelicaSystemError, LinearizationResult

# global names imported if import 'from OMPython import *' is used
__all__ = [
'ModelicaSystem',
'ModelicaSystemCmd',
'ModelicaSystemError',
'LinearizationResult',

'OMCSessionException',
'OMCSessionZMQ',
'OMCSessionBase',
'OMCSessionCmd',
]
5 changes: 3 additions & 2 deletions tests/test_FMIExport.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,16 @@ def __del__(self):

def testCauerLowPassAnalog(self):
print("testing Cauer")
mod = OMPython.ModelicaSystem(modelName="Modelica.Electrical.Analog.Examples.CauerLowPassAnalog", lmodel="Modelica")
mod = OMPython.ModelicaSystem(modelName="Modelica.Electrical.Analog.Examples.CauerLowPassAnalog",
lmodel=["Modelica"])
self.tmp = mod.getWorkDirectory()

fmu = mod.convertMo2Fmu(fileNamePrefix="CauerLowPassAnalog")
self.assertEqual(True, os.path.exists(fmu))

def testDrumBoiler(self):
print("testing DrumBoiler")
mod = OMPython.ModelicaSystem(modelName="Modelica.Fluid.Examples.DrumBoiler.DrumBoiler", lmodel="Modelica")
mod = OMPython.ModelicaSystem(modelName="Modelica.Fluid.Examples.DrumBoiler.DrumBoiler", lmodel=["Modelica"])
self.tmp = mod.getWorkDirectory()

fmu = mod.convertMo2Fmu(fileNamePrefix="DrumBoiler")
Expand Down
17 changes: 8 additions & 9 deletions tests/test_ModelicaSystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def worker():
def test_setParameters(self):
omc = OMPython.OMCSessionZMQ()
model_path = omc.sendExpression("getInstallationDirectoryPath()") + "/share/doc/omc/testmodels/"
mod = OMPython.ModelicaSystem(model_path + "BouncingBall.mo", "BouncingBall", raiseerrors=True)
mod = OMPython.ModelicaSystem(model_path + "BouncingBall.mo", "BouncingBall")

# method 1
mod.setParameters("e=1.234")
Expand All @@ -59,7 +59,7 @@ def test_setParameters(self):
def test_setSimulationOptions(self):
omc = OMPython.OMCSessionZMQ()
model_path = omc.sendExpression("getInstallationDirectoryPath()") + "/share/doc/omc/testmodels/"
mod = OMPython.ModelicaSystem(model_path + "BouncingBall.mo", "BouncingBall", raiseerrors=True)
mod = OMPython.ModelicaSystem(model_path + "BouncingBall.mo", "BouncingBall")

# method 1
mod.setSimulationOptions("stopTime=1.234")
Expand Down Expand Up @@ -89,7 +89,7 @@ def test_relative_path(self):
model_relative = str(model_file)
assert "/" not in model_relative

mod = OMPython.ModelicaSystem(model_relative, "M", raiseerrors=True)
mod = OMPython.ModelicaSystem(model_relative, "M")
assert float(mod.getParameters("a")[0]) == -1
finally:
# clean up the temporary file
Expand All @@ -99,17 +99,16 @@ def test_customBuildDirectory(self):
filePath = (self.tmp / "M.mo").as_posix()
tmpdir = self.tmp / "tmpdir1"
tmpdir.mkdir()
m = OMPython.ModelicaSystem(filePath, "M", raiseerrors=True,
customBuildDirectory=tmpdir)
assert pathlib.Path(m.getWorkDirectory()).resolve() == tmpdir.resolve()
m = OMPython.ModelicaSystem(filePath, "M", customBuildDirectory=tmpdir)
assert m.getWorkDirectory().resolve() == tmpdir.resolve()
result_file = tmpdir / "a.mat"
assert not result_file.exists()
m.simulate(resultfile="a.mat")
assert result_file.is_file()

def test_getSolutions(self):
filePath = (self.tmp / "M.mo").as_posix()
mod = OMPython.ModelicaSystem(filePath, "M", raiseerrors=True)
mod = OMPython.ModelicaSystem(filePath, "M")
x0 = 1
a = -1
tau = -1 / a
Expand Down Expand Up @@ -145,7 +144,7 @@ def test_getters(self):
y = der(x);
end M_getters;
""")
mod = OMPython.ModelicaSystem(model_file.as_posix(), "M_getters", raiseerrors=True)
mod = OMPython.ModelicaSystem(model_file.as_posix(), "M_getters")

q = mod.getQuantities()
assert isinstance(q, list)
Expand Down Expand Up @@ -323,7 +322,7 @@ def test_simulate_inputs(self):
y = x;
end M_input;
""")
mod = OMPython.ModelicaSystem(model_file.as_posix(), "M_input", raiseerrors=True)
mod = OMPython.ModelicaSystem(model_file.as_posix(), "M_input")

mod.setSimulationOptions("stopTime=1.0")

Expand Down
42 changes: 42 additions & 0 deletions tests/test_ModelicaSystemCmd.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import OMPython
import pathlib
import shutil
import tempfile
import unittest


import logging
logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.DEBUG)


class ModelicaSystemCmdTester(unittest.TestCase):
def __init__(self, *args, **kwargs):
super(ModelicaSystemCmdTester, self).__init__(*args, **kwargs)
self.tmp = pathlib.Path(tempfile.mkdtemp(prefix='tmpOMPython.tests'))
self.model = self.tmp / "M.mo"
with open(self.model, "w") as fout:
fout.write("""model M
Real x(start = 1, fixed = true);
parameter Real a = -1;
equation
der(x) = x*a;
end M;
""")
self.mod = OMPython.ModelicaSystem(self.model.as_posix(), "M")

def __del__(self):
shutil.rmtree(self.tmp, ignore_errors=True)

def test_simflags(self):
mscmd = OMPython.ModelicaSystemCmd(runpath=self.mod.tempdir, modelname=self.mod.modelName)
mscmd.args_set(args={"noEventEmit": None, "noRestart": None, "override": {'b': 2}})
mscmd.args_set(args=mscmd.parse_simflags(simflags="-noEventEmit -noRestart -override=a=1,x=3"))

logger.info(mscmd.get_cmd())

assert mscmd.get_cmd() == [mscmd.get_exe().as_posix(), '-noEventEmit', '-noRestart', '-override=b=2,a=1,x=3']


if __name__ == '__main__':
unittest.main()
22 changes: 22 additions & 0 deletions tests/test_OMSessionCmd.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import OMPython
import unittest


class OMCSessionCmdTester(unittest.TestCase):
def __init__(self, *args, **kwargs):
super(OMCSessionCmdTester, self).__init__(*args, **kwargs)

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

def test_isPackage2(self):
mod = OMPython.ModelicaSystem(modelName="Modelica.Electrical.Analog.Examples.CauerLowPassAnalog",
lmodel=["Modelica"])
omccmd = OMPython.OMCSessionCmd(session=mod.getconn)
assert omccmd.isPackage('Modelica')


if __name__ == '__main__':
unittest.main()
2 changes: 1 addition & 1 deletion tests/test_linearization.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def test_getters(self):
y2 = phi + u1;
end Pendulum;
""")
mod = OMPython.ModelicaSystem(model_file.as_posix(), "Pendulum", ["Modelica"], raiseerrors=True)
mod = OMPython.ModelicaSystem(model_file.as_posix(), "Pendulum", ["Modelica"])

d = mod.getLinearizationOptions()
assert isinstance(d, dict)
Expand Down
3 changes: 1 addition & 2 deletions tests/test_optimization.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ def test_example(self):
end BangBang2021;
""")

mod = OMPython.ModelicaSystem(model_file.as_posix(), "BangBang2021",
raiseerrors=True)
mod = OMPython.ModelicaSystem(model_file.as_posix(), "BangBang2021")

mod.setOptimizationOptions(["numberOfIntervals=16", "stopTime=1",
"stepSize=0.001", "tolerance=1e-8"])
Expand Down
Loading