Skip to content

Commit 2f1f3b4

Browse files
authored
Fix ModelicaSystem cannot load from relative path (#247)
This fixes #245
1 parent 478a0e6 commit 2f1f3b4

File tree

2 files changed

+29
-5
lines changed

2 files changed

+29
-5
lines changed

OMPython/__init__.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import pyparsing
3030
import importlib
3131
import zmq
32+
import pathlib
3233

3334

3435
if sys.platform == 'darwin':
@@ -700,7 +701,7 @@ def __init__(self, fileName=None, modelName=None, lmodel=None, commandLineOption
700701
self.xmlFile = None
701702
self.lmodel = lmodel # may be needed if model is derived from other model
702703
self.modelName = modelName # Model class name
703-
self.fileName = fileName # Model file/package name
704+
self.fileName = pathlib.Path(fileName).resolve() if fileName is not None else None # Model file/package name
704705
self.inputFlag = False # for model with input quantity
705706
self.simulationFlag = False # if the model is simulated?
706707
self.outputFlag = False
@@ -710,8 +711,8 @@ def __init__(self, fileName=None, modelName=None, lmodel=None, commandLineOption
710711

711712
self._raiseerrors = raiseerrors
712713

713-
if fileName is not None and not os.path.exists(self.fileName): # if file does not exist
714-
raise IOError("File Error:" + os.path.abspath(self.fileName) + " does not exist!!!")
714+
if fileName is not None and not self.fileName.is_file(): # if file does not exist
715+
raise IOError(f"File Error: {self.fileName} does not exist!!!")
715716

716717
# set default command Line Options for linearization as
717718
# linearize() will use the simulation executable and runtime
@@ -741,8 +742,7 @@ def setCommandLineOptions(self, commandLineOptions: str):
741742

742743
def loadFile(self):
743744
# load file
744-
loadFileExp = "".join(["loadFile(", "\"", self.fileName, "\"", ")"]).replace("\\", "/")
745-
loadMsg = self.sendExpression(loadFileExp)
745+
loadMsg = self.sendExpression(f'loadFile("{self.fileName.as_posix()}")')
746746
# Show notification or warnings to the user when verbose=True OR if some error occurred i.e., not result
747747
if self._verbose or not loadMsg:
748748
self._check_error()

tests/test_ModelicaSystem.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import tempfile
44
import shutil
55
import os
6+
import pathlib
67

78

89
class ModelicaSystemTester(unittest.TestCase):
@@ -76,6 +77,29 @@ def test_setSimulationOptions(self):
7677
assert d["stopTime"] == "2.1"
7778
assert d["tolerance"] == "1.2e-08"
7879

80+
def test_relative_path(self):
81+
cwd = pathlib.Path.cwd()
82+
(fd, name) = tempfile.mkstemp(dir=cwd, text=True)
83+
try:
84+
with os.fdopen(fd, 'w') as f:
85+
f.write("""model M
86+
Real x(start = 1, fixed=true);
87+
parameter Real a = -1;
88+
equation
89+
der(x) = x*a;
90+
end M;
91+
""")
92+
93+
model_file = pathlib.Path(name).relative_to(cwd)
94+
model_relative = str(model_file)
95+
assert "/" not in model_relative
96+
97+
mod = OMPython.ModelicaSystem(model_relative, "M", raiseerrors=True)
98+
assert float(mod.getParameters("a")[0]) == -1
99+
finally:
100+
# clean up the temporary file
101+
model_file.unlink()
102+
79103

80104
if __name__ == '__main__':
81105
unittest.main()

0 commit comments

Comments
 (0)