Skip to content

Commit

Permalink
Fixing issue with relative paths when loading unzipped FMUs (modelon-…
Browse files Browse the repository at this point in the history
  • Loading branch information
PeterMeisrimelModelon authored Aug 1, 2024
1 parent 8ebae0a commit 99dcb5d
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 12 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,5 @@ src/pyfmi/LICENSE
src/pyfmi/CHANGELOG

src/pyfmi/version.txt

__pycache__
1 change: 1 addition & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

--- Future release ---
* Updated the error message displayed when loading FMUs with needsExecutionTool set to True.
* Loading unzipped FMUs now also works with relative paths.

--- PyFMI-2.13.1 ---
* Numpy 2.x support
Expand Down
2 changes: 2 additions & 0 deletions src/pyfmi/fmi.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1378,6 +1378,7 @@ cdef class FMUModelBase(ModelBase):
fmu_temp_dir = encode(fmu)
else:
fmu_temp_dir = encode(create_temp_dir())
fmu_temp_dir = os.path.abspath(fmu_temp_dir)
self._fmu_temp_dir = <char*>FMIL.malloc((FMIL.strlen(fmu_temp_dir)+1)*sizeof(char))
FMIL.strcpy(self._fmu_temp_dir, fmu_temp_dir)

Expand Down Expand Up @@ -4070,6 +4071,7 @@ cdef class FMUModelBase2(ModelBase):
fmu_temp_dir = encode(fmu)
else:
fmu_temp_dir = encode(create_temp_dir())
fmu_temp_dir = os.path.abspath(fmu_temp_dir)
self._fmu_temp_dir = <char*>FMIL.malloc((FMIL.strlen(fmu_temp_dir)+1)*sizeof(char))
FMIL.strcpy(self._fmu_temp_dir, fmu_temp_dir)

Expand Down
44 changes: 32 additions & 12 deletions tests/test_fmi.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,13 +184,16 @@ def test_unzipped_fmu_exception_invalid_dir(self):
""" Verify that we get an exception if unzipped FMU does not contain modelDescription.xml, which it should according to the FMI specification. """
_helper_unzipped_fmu_exception_invalid_dir(FMUModelME1)

def _test_unzipped_bouncing_ball(self, fmu_loader):
def _test_unzipped_bouncing_ball(self, fmu_loader, tmp_dir = None):
""" Simulates the bouncing ball FMU ME1.0 by unzipping the example FMU before loading, 'fmu_loader' is either FMUModelME1 or load_fmu. """
tol = 1e-4
fmu_dir = create_temp_dir()
if tmp_dir is None:
fmu_dir = create_temp_dir()
else:
fmu_dir = tmp_dir
fmu = os.path.join(get_examples_folder(), 'files', 'FMUs', 'ME1.0', 'bouncingBall.fmu')
with ZipFile(fmu, 'r') as fmu_zip:
fmu_zip.extractall(path=fmu_dir)
fmu_zip.extractall(path = fmu_dir)

unzipped_fmu = fmu_loader(fmu_dir, allow_unzipped_fmu = True)
res = unzipped_fmu.simulate(final_time = 2.0)
Expand All @@ -201,11 +204,13 @@ def _test_unzipped_bouncing_ball(self, fmu_loader):
def test_unzipped_fmu1(self):
""" Test load and simulate unzipped ME FMU 1.0 using FMUModelME1 """
self._test_unzipped_bouncing_ball(FMUModelME1)
self._test_unzipped_bouncing_ball(FMUModelME1, tmp_dir = tempfile.TemporaryDirectory(dir = "./").name)

@testattr(stddist = True)
def test_unzipped_fmu2(self):
""" Test load and simulate unzipped ME FMU 1.0 using load_fmu """
self._test_unzipped_bouncing_ball(load_fmu)
self._test_unzipped_bouncing_ball(load_fmu, tmp_dir = tempfile.TemporaryDirectory(dir = "./").name)

@testattr(stddist = True)
def test_invalid_binary(self):
Expand Down Expand Up @@ -324,13 +329,16 @@ def test_unzipped_fmu_exception_invalid_dir(self):
""" Verify that we get an exception if unzipped FMU does not contain modelDescription.xml, which it should according to the FMI specification. """
_helper_unzipped_fmu_exception_invalid_dir(FMUModelCS1)

def _test_unzipped_bouncing_ball(self, fmu_loader):
def _test_unzipped_bouncing_ball(self, fmu_loader, tmp_dir = None):
""" Simulates the bouncing ball FMU CS1.0 by unzipping the example FMU before loading, 'fmu_loader' is either FMUModelCS1 or load_fmu. """
tol = 1e-2
fmu_dir = create_temp_dir()
if tmp_dir is None:
fmu_dir = create_temp_dir()
else:
fmu_dir = tmp_dir
fmu = os.path.join(get_examples_folder(), 'files', 'FMUs', 'CS1.0', 'bouncingBall.fmu')
with ZipFile(fmu, 'r') as fmu_zip:
fmu_zip.extractall(path=fmu_dir)
fmu_zip.extractall(path = fmu_dir)

unzipped_fmu = fmu_loader(fmu_dir, allow_unzipped_fmu = True)
res = unzipped_fmu.simulate(final_time = 2.0)
Expand All @@ -341,11 +349,13 @@ def _test_unzipped_bouncing_ball(self, fmu_loader):
def test_unzipped_fmu1(self):
""" Test load and simulate unzipped CS FMU 1.0 using FMUModelCS1 """
self._test_unzipped_bouncing_ball(FMUModelCS1)
self._test_unzipped_bouncing_ball(FMUModelCS1, tmp_dir = tempfile.TemporaryDirectory(dir = "./").name)

@testattr(stddist = True)
def test_unzipped_fmu2(self):
""" Test load and simulate unzipped CS FMU 1.0 using load_fmu """
self._test_unzipped_bouncing_ball(load_fmu)
self._test_unzipped_bouncing_ball(load_fmu, tmp_dir = tempfile.TemporaryDirectory(dir = "./").name)

@testattr(stddist = True)
def test_invalid_binary(self):
Expand Down Expand Up @@ -577,13 +587,16 @@ def test_unzipped_fmu_exception_invalid_dir(self):
""" Verify that we get an exception if unzipped FMU does not contain modelDescription.xml, which it should according to the FMI specification. """
_helper_unzipped_fmu_exception_invalid_dir(FMUModelCS2)

def _test_unzipped_bouncing_ball(self, fmu_loader):
def _test_unzipped_bouncing_ball(self, fmu_loader, tmp_dir = None):
""" Simulates the bouncing ball FMU CS2.0 by unzipping the example FMU before loading, 'fmu_loader' is either FMUModelCS2 or load_fmu. """
tol = 1e-2
fmu_dir = create_temp_dir()
if tmp_dir is None:
fmu_dir = create_temp_dir()
else:
fmu_dir = tmp_dir
fmu = os.path.join(get_examples_folder(), 'files', 'FMUs', 'CS2.0', 'bouncingBall.fmu')
with ZipFile(fmu, 'r') as fmu_zip:
fmu_zip.extractall(path=fmu_dir)
fmu_zip.extractall(path = fmu_dir)

unzipped_fmu = fmu_loader(fmu_dir, allow_unzipped_fmu = True)
res = unzipped_fmu.simulate(final_time = 2.0)
Expand All @@ -594,11 +607,13 @@ def _test_unzipped_bouncing_ball(self, fmu_loader):
def test_unzipped_fmu1(self):
""" Test load and simulate unzipped CS FMU 2.0 using FMUModelCS2 """
self._test_unzipped_bouncing_ball(FMUModelCS2)
self._test_unzipped_bouncing_ball(FMUModelCS2, tmp_dir = tempfile.TemporaryDirectory(dir = "./").name)

@testattr(stddist = True)
def test_unzipped_fmu2(self):
""" Test load and simulate unzipped CS FMU 2.0 using load_fmu """
self._test_unzipped_bouncing_ball(load_fmu)
self._test_unzipped_bouncing_ball(load_fmu, tmp_dir = tempfile.TemporaryDirectory(dir = "./").name)

@testattr(stddist = True)
def test_log_file_name(self):
Expand Down Expand Up @@ -1169,13 +1184,16 @@ def test_unzipped_fmu_exception_invalid_dir(self):
""" Verify that we get an exception if unzipped FMU does not contain modelDescription.xml, which it should according to the FMI specification. """
_helper_unzipped_fmu_exception_invalid_dir(FMUModelME2)

def _test_unzipped_bouncing_ball(self, fmu_loader):
def _test_unzipped_bouncing_ball(self, fmu_loader, tmp_dir = None):
""" Simulates the bouncing ball FMU ME2.0 by unzipping the example FMU before loading, 'fmu_loader' is either FMUModelME2 or load_fmu. """
tol = 1e-4
fmu_dir = create_temp_dir()
if tmp_dir is None:
fmu_dir = create_temp_dir()
else:
fmu_dir = tmp_dir
fmu = os.path.join(get_examples_folder(), 'files', 'FMUs', 'ME2.0', 'bouncingBall.fmu')
with ZipFile(fmu, 'r') as fmu_zip:
fmu_zip.extractall(path=fmu_dir)
fmu_zip.extractall(path = fmu_dir)

unzipped_fmu = fmu_loader(fmu_dir, allow_unzipped_fmu = True)
res = unzipped_fmu.simulate(final_time = 2.0)
Expand All @@ -1186,11 +1204,13 @@ def _test_unzipped_bouncing_ball(self, fmu_loader):
def test_unzipped_fmu1(self):
""" Test load and simulate unzipped ME FMU 2.0 using FMUModelME2 """
self._test_unzipped_bouncing_ball(FMUModelME2)
self._test_unzipped_bouncing_ball(FMUModelME2, tmp_dir = tempfile.TemporaryDirectory(dir = "./").name)

@testattr(stddist = True)
def test_unzipped_fmu2(self):
""" Test load and simulate unzipped ME FMU 2.0 using load_fmu """
self._test_unzipped_bouncing_ball(load_fmu)
self._test_unzipped_bouncing_ball(load_fmu, tmp_dir = tempfile.TemporaryDirectory(dir = "./").name)

@testattr(stddist = True)
def test_unzipped_fmu_exceptions(self):
Expand Down

0 comments on commit 99dcb5d

Please sign in to comment.