From 120718c38e3061c79659f6d24cb0457bfa339f9c Mon Sep 17 00:00:00 2001 From: arun3688 Date: Tue, 31 Oct 2023 16:24:27 +0100 Subject: [PATCH 01/13] allow modelicaSystem() to directly load MSL without filename --- OMPython/__init__.py | 38 +++++++++++++++++++++++++++++--------- tests/__init__.py | 2 +- tests/test_FMIExport.py | 30 ++++++++++++++++++++++++++++++ 3 files changed, 60 insertions(+), 10 deletions(-) create mode 100644 tests/test_FMIExport.py diff --git a/OMPython/__init__.py b/OMPython/__init__.py index f8ba88710..37d2df6a0 100755 --- a/OMPython/__init__.py +++ b/OMPython/__init__.py @@ -798,7 +798,6 @@ def __init__(self, fileName=None, modelName=None, lmodel=[], useCorba=False, com Note: If the model file is not in the current working directory, then the path where file is located must be included together with file name. Besides, if the Modelica model contains several different models within the same package, then in order to build the specific model, in second argument, user must put the package name with dot(.) followed by specific model name. ex: myModel = ModelicaSystem("ModelicaModel.mo", "modelName") """ - if fileName is None and modelName is None and not lmodel: # all None if useCorba: self.getconn = OMCSession() @@ -806,10 +805,7 @@ def __init__(self, fileName=None, modelName=None, lmodel=[], useCorba=False, com self.getconn = OMCSessionZMQ() return - if fileName is None: - return "File does not exist" self.tree = None - self.quantitiesList=[] self.paramlist={} self.inputlist={} @@ -830,6 +826,10 @@ def __init__(self, fileName=None, modelName=None, lmodel=[], useCorba=False, com else: self.getconn = OMCSessionZMQ() + ## needed for properly deleting the OMCSessionZMQ + self._omc_log_file = self.getconn._omc_log_file + self._omc_process = self.getconn._omc_process + ## set commandLineOptions if provided by users if commandLineOptions is not None: exp="".join(["setCommandLineOptions(","\"",commandLineOptions,"\"",")"]) @@ -846,7 +846,7 @@ def __init__(self, fileName=None, modelName=None, lmodel=[], useCorba=False, com self.resultfile="" # for storing result file self.variableFilter = variableFilter - if not os.path.exists(self.fileName): # if file does not eixt + if fileName is not None and not os.path.exists(self.fileName): # if file does not eixt print("File Error:" + os.path.abspath(self.fileName) + " does not exist!!!") return @@ -856,19 +856,37 @@ def __init__(self, fileName=None, modelName=None, lmodel=[], useCorba=False, com self.getconn.sendExpression("setCommandLineOptions(\"--linearizationDumpLanguage=python\")") self.getconn.sendExpression("setCommandLineOptions(\"--generateSymbolicLinearization\")") - self.loadingModel() + self.setTempDirectory() + + if fileName is not None: + self.loadFile() + + ## allow directly loading models from MSL without fileName + if fileName is None and modelName is not None: + self.loadLibrary() + + self.buildModel() def __del__(self): OMCSessionBase.__del__(self) - # for loading file/package, loading model and building model - def loadingModel(self): + def setCommandLineOptions(self): + ## set commandLineOptions if provided by users + if commandLineOptions is not None: + exp="".join(["setCommandLineOptions(","\"",commandLineOptions,"\"",")"]) + cmdexp = self.getconn.sendExpression(exp) + if not cmdexp: + return print(self.getconn.sendExpression("getErrorString()")) + + def loadFile(self): # load file loadFileExp="".join(["loadFile(","\"",self.fileName,"\"",")"]).replace("\\","/") loadMsg = self.getconn.sendExpression(loadFileExp) if not loadMsg: return print(self.getconn.sendExpression("getErrorString()")) + # for loading file/package, loading model and building model + def loadLibrary(self): # load Modelica standard libraries or Modelica files if needed for element in self.lmodel: if element is not None: @@ -892,6 +910,7 @@ def loadingModel(self): if loadmodelError: print(loadmodelError) + def setTempDirectory(self): # create a unique temp directory for each session and build the model in that directory self.tempdir = tempfile.mkdtemp() if not os.path.exists(self.tempdir): @@ -900,7 +919,8 @@ def loadingModel(self): exp="".join(["cd(","\"",self.tempdir,"\"",")"]).replace("\\","/") self.getconn.sendExpression(exp) - self.buildModel() + def getWorkDirectory(self): + return self.tempdir def buildModel(self, variableFilter=None): if variableFilter is not None: diff --git a/tests/__init__.py b/tests/__init__.py index df2f51748..38842a093 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -1 +1 @@ -__all__ = ['tests.test_OMParser', 'tests.test_ZMQ', 'tests.test_ModelicaSystem'] +__all__ = ['tests.test_OMParser', 'tests.test_ZMQ', 'tests.test_ModelicaSystem', 'tests.test_FMIExport'] diff --git a/tests/test_FMIExport.py b/tests/test_FMIExport.py new file mode 100644 index 000000000..a864a2b7f --- /dev/null +++ b/tests/test_FMIExport.py @@ -0,0 +1,30 @@ +import OMPython +import unittest +import tempfile, shutil, os + +class testFMIExport(unittest.TestCase): + def __init__(self, *args, **kwargs): + super(testFMIExport, self).__init__(*args, **kwargs) + self.tmp = "" + + def __del__(self): + shutil.rmtree(self.tmp, ignore_errors=True) + + def testCauerLowPassAnalog(self): + print("testing Cauer") + mod = OMPython.ModelicaSystem(modelName="Modelica.Electrical.Analog.Examples.CauerLowPassAnalog") + 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") + self.tmp = mod.getWorkDirectory() + + fmu = mod.convertMo2Fmu(fileNamePrefix="DrumBoiler") + self.assertEqual(True, os.path.exists(fmu)) + +if __name__ == '__main__': + unittest.main() From dbe73d84e3199781f80e3b630768c41e7f359581 Mon Sep 17 00:00:00 2001 From: arun3688 Date: Tue, 31 Oct 2023 16:42:56 +0100 Subject: [PATCH 02/13] load Modelica --- tests/test_FMIExport.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_FMIExport.py b/tests/test_FMIExport.py index a864a2b7f..2837a01ab 100644 --- a/tests/test_FMIExport.py +++ b/tests/test_FMIExport.py @@ -12,7 +12,7 @@ def __del__(self): def testCauerLowPassAnalog(self): print("testing Cauer") - mod = OMPython.ModelicaSystem(modelName="Modelica.Electrical.Analog.Examples.CauerLowPassAnalog") + mod = OMPython.ModelicaSystem(modelName="Modelica.Electrical.Analog.Examples.CauerLowPassAnalog", lmodel="Modelica") self.tmp = mod.getWorkDirectory() fmu = mod.convertMo2Fmu(fileNamePrefix="CauerLowPassAnalog") @@ -20,7 +20,7 @@ def testCauerLowPassAnalog(self): def testDrumBoiler(self): print("testing DrumBoiler") - mod = OMPython.ModelicaSystem(modelName="Modelica.Fluid.Examples.DrumBoiler.DrumBoiler") + mod = OMPython.ModelicaSystem(modelName="Modelica.Fluid.Examples.DrumBoiler.DrumBoiler", lmodel="Modelica") self.tmp = mod.getWorkDirectory() fmu = mod.convertMo2Fmu(fileNamePrefix="DrumBoiler") From 9eb6fa2da5c3876c293407750d8ba6ef2d3bf22c Mon Sep 17 00:00:00 2001 From: arun3688 Date: Thu, 2 Nov 2023 12:06:56 +0100 Subject: [PATCH 03/13] remove fmi_export test --- tests/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/__init__.py b/tests/__init__.py index 38842a093..df2f51748 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -1 +1 @@ -__all__ = ['tests.test_OMParser', 'tests.test_ZMQ', 'tests.test_ModelicaSystem', 'tests.test_FMIExport'] +__all__ = ['tests.test_OMParser', 'tests.test_ZMQ', 'tests.test_ModelicaSystem'] From f647ac6b21989d7c88b76764cf4f29b3b7ce664a Mon Sep 17 00:00:00 2001 From: arun3688 Date: Thu, 2 Nov 2023 12:20:26 +0100 Subject: [PATCH 04/13] use full path of the model --- tests/test_ModelicaSystem.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/tests/test_ModelicaSystem.py b/tests/test_ModelicaSystem.py index c0480eb3f..8c9678c95 100644 --- a/tests/test_ModelicaSystem.py +++ b/tests/test_ModelicaSystem.py @@ -19,12 +19,10 @@ def __del__(self): def testModelicaSystemLoop(self): def worker(): - origDir = os.getcwd() - os.chdir(self.tmp) - m = OMPython.ModelicaSystem("M.mo", "M") + filePath = os.path.join(self.tmp,"M.mo").replace("\\", "/") + m = OMPython.ModelicaSystem(filePath, "M") m.simulate() m.convertMo2Fmu(fmuType="me") - os.chdir(origDir) for _ in range(10): worker() From c4aaa7b6d1996e261274aa87146560d9a5b2b20a Mon Sep 17 00:00:00 2001 From: arun3688 Date: Thu, 2 Nov 2023 12:31:02 +0100 Subject: [PATCH 05/13] test fmi export --- .github/workflows/Test.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/Test.yml b/.github/workflows/Test.yml index fa4065a95..0885db161 100644 --- a/.github/workflows/Test.yml +++ b/.github/workflows/Test.yml @@ -44,3 +44,4 @@ jobs: python -m unittest tests/test_ModelicaSystem.py python -m unittest tests/test_OMParser.py python -m unittest tests/test_ZMQ.py + python -m unittest tests/test_FMIExport.py From b791f35c8a0b1dac9410350388d3f078b0503642 Mon Sep 17 00:00:00 2001 From: arun3688 Date: Thu, 2 Nov 2023 12:37:08 +0100 Subject: [PATCH 06/13] install libraries --- .github/workflows/Test.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/Test.yml b/.github/workflows/Test.yml index 0885db161..130c0cb65 100644 --- a/.github/workflows/Test.yml +++ b/.github/workflows/Test.yml @@ -24,6 +24,8 @@ jobs: version: ${{ matrix.omc-version }} packages: | omc + libraries: | + 'Modelica 4.0.0' - run: "omc --version" From fb834529d649b4ef447182f7b59574e6bc0ee28c Mon Sep 17 00:00:00 2001 From: arun3688 Date: Thu, 2 Nov 2023 14:02:43 +0100 Subject: [PATCH 07/13] use pytest --- .github/workflows/Test.yml | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/.github/workflows/Test.yml b/.github/workflows/Test.yml index 130c0cb65..6fbf94e48 100644 --- a/.github/workflows/Test.yml +++ b/.github/workflows/Test.yml @@ -39,11 +39,21 @@ jobs: - name: Install dependencies run: | python -m pip install --upgrade pip - pip install future pyparsing numpy psutil pyzmq + pip install future pyparsing numpy psutil pyzmq pytest pytest-md pytest-emoji - - name: Test OMPython + - uses: pavelzw/pytest-action@v2 + with: + verbose: true + emoji: true + job-summary: true + custom-arguments: '-q' + click-to-expand: true + report-title: 'Test Report' + + - name: Run Pytest run: | - python -m unittest tests/test_ModelicaSystem.py - python -m unittest tests/test_OMParser.py - python -m unittest tests/test_ZMQ.py - python -m unittest tests/test_FMIExport.py + pytest tests/test_ModelicaSystem.py + # python -m unittest tests/test_ModelicaSystem.py + # python -m unittest tests/test_OMParser.py + # python -m unittest tests/test_ZMQ.py + # python -m unittest tests/test_FMIExport.py From c8a75efce5996b07aae8f6e28c0a4e9e9447c840 Mon Sep 17 00:00:00 2001 From: arun3688 Date: Thu, 2 Nov 2023 14:34:49 +0100 Subject: [PATCH 08/13] skip testDocker using pytest --- .github/workflows/Test.yml | 10 +--------- tests/test_docker.py | 2 ++ 2 files changed, 3 insertions(+), 9 deletions(-) diff --git a/.github/workflows/Test.yml b/.github/workflows/Test.yml index 6fbf94e48..8d4520d69 100644 --- a/.github/workflows/Test.yml +++ b/.github/workflows/Test.yml @@ -48,12 +48,4 @@ jobs: job-summary: true custom-arguments: '-q' click-to-expand: true - report-title: 'Test Report' - - - name: Run Pytest - run: | - pytest tests/test_ModelicaSystem.py - # python -m unittest tests/test_ModelicaSystem.py - # python -m unittest tests/test_OMParser.py - # python -m unittest tests/test_ZMQ.py - # python -m unittest tests/test_FMIExport.py + report-title: 'Test Report' \ No newline at end of file diff --git a/tests/test_docker.py b/tests/test_docker.py index 4ab305bd7..1db6deaf3 100644 --- a/tests/test_docker.py +++ b/tests/test_docker.py @@ -1,8 +1,10 @@ import OMPython import unittest import tempfile, shutil, os +import pytest class DockerTester(unittest.TestCase): + @pytest.mark.skip(reason="This test would fail") def testDocker(self): om = OMPython.OMCSessionZMQ(docker="openmodelica/openmodelica:v1.16.1-minimal") assert(om.sendExpression("getVersion()") == "OpenModelica 1.16.1") From 7e2807c3563226d5f3cd97a9b7c25774a5bee8d7 Mon Sep 17 00:00:00 2001 From: arun3688 Date: Thu, 2 Nov 2023 14:50:40 +0100 Subject: [PATCH 09/13] use verbose --- .github/workflows/Test.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/Test.yml b/.github/workflows/Test.yml index 8d4520d69..b0bc7c812 100644 --- a/.github/workflows/Test.yml +++ b/.github/workflows/Test.yml @@ -46,6 +46,6 @@ jobs: verbose: true emoji: true job-summary: true - custom-arguments: '-q' + custom-arguments: '-v' click-to-expand: true report-title: 'Test Report' \ No newline at end of file From 30d045a4a6c72d9f121a39265c00db785e3a56fc Mon Sep 17 00:00:00 2001 From: arun3688 Date: Thu, 2 Nov 2023 15:31:38 +0100 Subject: [PATCH 10/13] rename header --- .github/workflows/Test.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/Test.yml b/.github/workflows/Test.yml index b0bc7c812..8f9449a9a 100644 --- a/.github/workflows/Test.yml +++ b/.github/workflows/Test.yml @@ -41,7 +41,8 @@ jobs: python -m pip install --upgrade pip pip install future pyparsing numpy psutil pyzmq pytest pytest-md pytest-emoji - - uses: pavelzw/pytest-action@v2 + - name: Run pytest + uses: pavelzw/pytest-action@v2 with: verbose: true emoji: true From 2a53367aad8c55a5aa2bc8224e8aa92f7e347ceb Mon Sep 17 00:00:00 2001 From: arun3688 Date: Thu, 2 Nov 2023 15:38:58 +0100 Subject: [PATCH 11/13] add timezone for the test report --- .github/workflows/Test.yml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/.github/workflows/Test.yml b/.github/workflows/Test.yml index 8f9449a9a..33b002572 100644 --- a/.github/workflows/Test.yml +++ b/.github/workflows/Test.yml @@ -41,6 +41,11 @@ jobs: python -m pip install --upgrade pip pip install future pyparsing numpy psutil pyzmq pytest pytest-md pytest-emoji + - name: Set timezone + uses: szenius/set-timezone@v1.2 + with: + timezone: 'Europe/Berlin' + - name: Run pytest uses: pavelzw/pytest-action@v2 with: From b8cf171331c8f07c40bd2324d3b353fd5c355259 Mon Sep 17 00:00:00 2001 From: arun3688 Date: Fri, 3 Nov 2023 11:12:48 +0100 Subject: [PATCH 12/13] fix timezone --- .github/workflows/Test.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/Test.yml b/.github/workflows/Test.yml index 33b002572..dd2a81096 100644 --- a/.github/workflows/Test.yml +++ b/.github/workflows/Test.yml @@ -44,7 +44,7 @@ jobs: - name: Set timezone uses: szenius/set-timezone@v1.2 with: - timezone: 'Europe/Berlin' + timezoneLinux: 'Europe/Berlin' - name: Run pytest uses: pavelzw/pytest-action@v2 From d2ca2f471533b146867f864f47e1f09b33913aba Mon Sep 17 00:00:00 2001 From: arun3688 Date: Fri, 3 Nov 2023 11:26:49 +0100 Subject: [PATCH 13/13] remove Jenkinsfile --- .jenkins/python2/Dockerfile | 10 -------- .jenkins/python3/Dockerfile | 10 -------- Jenkinsfile | 49 ------------------------------------- 3 files changed, 69 deletions(-) delete mode 100644 .jenkins/python2/Dockerfile delete mode 100644 .jenkins/python3/Dockerfile delete mode 100644 Jenkinsfile diff --git a/.jenkins/python2/Dockerfile b/.jenkins/python2/Dockerfile deleted file mode 100644 index 61e689458..000000000 --- a/.jenkins/python2/Dockerfile +++ /dev/null @@ -1,10 +0,0 @@ -FROM docker.openmodelica.org/build-deps:v1.16.2 - -RUN apt-get update \ - && apt-get install -qy gnupg wget ca-certificates apt-transport-https sudo \ - && echo "deb https://build.openmodelica.org/omc/builds/linux/releases/1.14.2/ `lsb_release -sc` release" > /etc/apt/sources.list.d/openmodelica.list \ - && wget https://build.openmodelica.org/apt/openmodelica.asc -O- | apt-key add - \ - && apt-get update \ - && apt-get install -qy --no-install-recommends omc \ - && rm -rf /var/lib/apt/lists/* -RUN pip2 install --no-cache pytest psutil diff --git a/.jenkins/python3/Dockerfile b/.jenkins/python3/Dockerfile deleted file mode 100644 index 59e38afd0..000000000 --- a/.jenkins/python3/Dockerfile +++ /dev/null @@ -1,10 +0,0 @@ -FROM docker.openmodelica.org/build-deps:v1.16.2 - -RUN apt-get update \ - && apt-get install -qy gnupg wget ca-certificates apt-transport-https sudo \ - && echo "deb https://build.openmodelica.org/omc/builds/linux/releases/1.14.2/ `lsb_release -sc` release" > /etc/apt/sources.list.d/openmodelica.list \ - && wget https://build.openmodelica.org/apt/openmodelica.asc -O- | apt-key add - \ - && apt-get update \ - && apt-get install -qy --no-install-recommends omc \ - && rm -rf /var/lib/apt/lists/* -RUN pip3 install --no-cache pytest psutil diff --git a/Jenkinsfile b/Jenkinsfile deleted file mode 100644 index 998672f98..000000000 --- a/Jenkinsfile +++ /dev/null @@ -1,49 +0,0 @@ -pipeline { - agent none - stages { - stage('test') { - parallel { - stage('python2') { - agent { - label 'linux' - } - steps { - script { - def deps = docker.build('ompython-jenkins-python2', '--pull .jenkins/python2') - def dockergid = sh (script: 'stat -c %g /var/run/docker.sock', returnStdout: true).trim() - sh "docker pull openmodelica/openmodelica:v1.16.1-minimal" // Avoid timeout - deps.inside("-v /var/run/docker.sock:/var/run/docker.sock --network=host --pid=host --group-add '${dockergid}'") { - sh 'python2 setup.py build' - timeout(3) { - sh 'python2 /usr/local/bin/py.test -v --junitxml py2.xml tests' - } - sh 'HOME="$PWD" python2 setup.py install --user' - } - junit 'py2.xml' - } - } - } - stage('python3') { - agent { - label 'linux' - } - steps { - script { - def deps = docker.build('ompython-jenkins-python3', '--pull .jenkins/python3') - def dockergid = sh (script: 'stat -c %g /var/run/docker.sock', returnStdout: true).trim() - sh "docker pull openmodelica/openmodelica:v1.16.1-minimal" // Avoid timeout - deps.inside("-v /var/run/docker.sock:/var/run/docker.sock --network=host --pid=host --group-add '${dockergid}'") { - sh 'python3 setup.py build' - timeout(3) { - sh 'python3 /usr/local/bin/py.test -v --junitxml py3.xml tests' - } - sh 'HOME="$PWD" python3 setup.py install --user' - } - junit 'py3.xml' - } - } - } - } - } - } -}