From c8d1e0d0c39130d5bea6fbb16e406d179bcc5149 Mon Sep 17 00:00:00 2001 From: PeterMeisrimelModelon <92585725+PeterMeisrimelModelon@users.noreply.github.com> Date: Tue, 17 Dec 2024 16:09:09 +0100 Subject: [PATCH] Fixing bug + changelog + test (#279) --- CHANGELOG | 1 + src/common/io.py | 2 ++ tests/test_io.py | 22 ++++++++++++++++++++-- 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index f32796a7..5dcbf402 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,5 +1,6 @@ --- CHANGELOG --- --- Future --- + * Fixed an issue with `get_variables_data` returning full trajectories when mixed with `get_variable_data` calls. --- PyFMI-2.16.0 --- * Added verification against the available free disk space when storing the result on disk (i.e. a protection diff --git a/src/common/io.py b/src/common/io.py index 64460440..04f8156d 100644 --- a/src/common/io.py +++ b/src/common/io.py @@ -1574,6 +1574,8 @@ def get_variable_data(self, name: str) -> Trajectory: A Trajectory object containing the time vector and the data vector of the variable. """ + # prevent caching interference with 'get_variables_data' + self._last_set_of_indices = (None, None) return self._get_variable_data_as_trajectory(name) def get_variables_data(self, diff --git a/tests/test_io.py b/tests/test_io.py index f55a4f25..121915a9 100644 --- a/tests/test_io.py +++ b/tests/test_io.py @@ -1116,8 +1116,8 @@ def test_exception_simulation_start(self): bouncingBall = ResultHandlerBinaryFile(model) bouncingBall.set_options(opts) - msg = "Unable to start simulation. The following keyword argument\(s\) are empty:" - msg += " 'diagnostics\_params' and 'diagnostics\_vars'." + msg = r"Unable to start simulation. The following keyword argument\(s\) are empty:" + msg += r" 'diagnostics\_params' and 'diagnostics\_vars'." with pytest.raises(FMUException, match = msg): bouncingBall.simulation_start() @@ -1934,6 +1934,24 @@ def test_trajectory_lengths(self): assert rdb.get_variables_data([], start_index = 1)[1] == 1 assert rdb.get_variables_data([], start_index = 5)[1] == 5 + def test_mixes_get_variable_s_data(self): + """Test there are no issues when mixing calls of get_variable_data and get_variables_data.""" + fmu = Dummy_FMUModelME2([], os.path.join(file_path, "files", "FMUs", "XML", "ME2.0", "bouncingBall.fmu"), _connect_dll=False) + ncp = 500 + fmu.simulate(options = {"ncp": ncp}) + rdb = ResultDymolaBinary(fmu.get_last_result_file(), allow_file_updates = True) + + vars = ["time"] + start_index, stop_index = 0, 5 + + partial_1, _ = rdb.get_variables_data(vars, start_index, stop_index) + full_traj = rdb.get_variable_data(vars[0]) + partial_2, _ = rdb.get_variables_data(vars, start_index, stop_index) + + assert len(partial_1[vars[0]].x) == (stop_index - start_index) + assert len(full_traj.x) == (ncp + 1) + assert len(partial_2[vars[0]].x) == (stop_index - start_index) + @pytest.mark.assimulo class TestFileSizeLimit: def _setup(self, result_type, result_file_name="", fmi_type="me"):