Skip to content
Merged
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
10 changes: 5 additions & 5 deletions OMPython/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import pyparsing
import importlib
import zmq
import pathlib


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

self._raiseerrors = raiseerrors

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

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

def loadFile(self):
# load file
loadFileExp = "".join(["loadFile(", "\"", self.fileName, "\"", ")"]).replace("\\", "/")
loadMsg = self.sendExpression(loadFileExp)
loadMsg = self.sendExpression(f'loadFile("{self.fileName.as_posix()}")')
# Show notification or warnings to the user when verbose=True OR if some error occurred i.e., not result
if self._verbose or not loadMsg:
self._check_error()
Expand Down
24 changes: 24 additions & 0 deletions tests/test_ModelicaSystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import tempfile
import shutil
import os
import pathlib


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

def test_relative_path(self):
cwd = pathlib.Path.cwd()
(fd, name) = tempfile.mkstemp(dir=cwd, text=True)
try:
with os.fdopen(fd, 'w') as f:
f.write("""model M
Real x(start = 1, fixed=true);
parameter Real a = -1;
equation
der(x) = x*a;
end M;
""")

model_file = pathlib.Path(name).relative_to(cwd)
model_relative = str(model_file)
assert "/" not in model_relative

mod = OMPython.ModelicaSystem(model_relative, "M", raiseerrors=True)
assert float(mod.getParameters("a")[0]) == -1
finally:
# clean up the temporary file
model_file.unlink()


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