From e01044c60686abbd7142ba6e9108b56d9a0f82cd Mon Sep 17 00:00:00 2001 From: syntron Date: Mon, 23 Jun 2025 09:07:48 +0200 Subject: [PATCH 1/4] [ModelicaSystem] remove class variable csvFile; define name based on resultfile in simulate() reason: * variable not needed / used as class variable * using name based on resultfile allows to run the same model executable several times --- OMPython/ModelicaSystem.py | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/OMPython/ModelicaSystem.py b/OMPython/ModelicaSystem.py index 2f02b710..add3587c 100644 --- a/OMPython/ModelicaSystem.py +++ b/OMPython/ModelicaSystem.py @@ -389,7 +389,6 @@ def __init__( self._file_name = pathlib.Path(fileName).resolve() if fileName is not None else None # Model file/package name self._has_inputs = False # for model with input quantity self._simulated = False # True if the model has already been simulated - self._csvFile: Optional[pathlib.Path] = None # for storing inputs condition self._result_file: Optional[pathlib.Path] = None # for storing result file self._variable_filter = variableFilter @@ -969,6 +968,9 @@ def simulate(self, om_cmd.arg_set(key="overrideFile", val=overrideFile.as_posix()) if self._has_inputs: # if model has input quantities + # csvfile is based on name used for result file + csvfile = self._result_file.parent / f"{self._result_file.stem}.csv" + for i in self._inputs: val = self._inputs[i] if val is None: @@ -980,9 +982,11 @@ def simulate(self, raise ModelicaSystemError(f"startTime not matched for Input {i}!") if float(self._simulate_options["stopTime"]) != val[-1][0]: raise ModelicaSystemError(f"stopTime not matched for Input {i}!") - self._csvFile = self._createCSVData() # create csv file - om_cmd.arg_set(key="csvInput", val=self._csvFile.as_posix()) + # write csv file and store the name + csvfile = self._createCSVData(csvfile=csvfile) + + om_cmd.arg_set(key="csvInput", val=csvfile.as_posix()) # delete resultfile ... if self._result_file.is_file(): @@ -1231,7 +1235,11 @@ def _checkValidInputs(self, name): else: raise ModelicaSystemError('Error!!! Value must be in tuple format') - def _createCSVData(self) -> pathlib.Path: + def _createCSVData(self, csvfile: Optional[pathlib.Path] = None) -> pathlib.Path: + """ + Create a csv file with inputs for the simulation/optimization of the model. If csvfile is provided as argument, + this file is used; else a generic file name is created. + """ start_time: float = float(self._simulate_options["startTime"]) stop_time: float = float(self._simulate_options["stopTime"]) @@ -1272,13 +1280,14 @@ def _createCSVData(self) -> pathlib.Path: ] csv_rows.append(row) - csvFile = self._tempdir / f'{self._model_name}.csv' + if csvfile is None: + csvFile = self._tempdir / f'{self._model_name}.csv' - with open(file=csvFile, mode="w", encoding="utf-8", newline="") as fh: + with open(file=csvfile, mode="w", encoding="utf-8", newline="") as fh: writer = csv.writer(fh) writer.writerows(csv_rows) - return csvFile + return csvfile def convertMo2Fmu(self, version: str = "2.0", fmuType: str = "me_cs", fileNamePrefix: str = "", @@ -1422,8 +1431,8 @@ def load_module_from_path(module_name, file_path): for l in tupleList: if l[0] < float(self._simulate_options["startTime"]): raise ModelicaSystemError('Input time value is less than simulation startTime') - self._csvFile = self._createCSVData() - om_cmd.arg_set(key="csvInput", val=self._csvFile.as_posix()) + csvfile = self._createCSVData() + om_cmd.arg_set(key="csvInput", val=csvfile.as_posix()) om_cmd.arg_set(key="l", val=str(lintime or self._linearization_options["stopTime"])) From 77cd5473a5e62a691eec0076b267525a9edf45d1 Mon Sep 17 00:00:00 2001 From: syntron Date: Mon, 23 Jun 2025 15:51:48 +0200 Subject: [PATCH 2/4] [ModelicaSystem/test_ModelicaSystem] fix test (csvFile no longer a class variable) --- tests/test_ModelicaSystem.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/test_ModelicaSystem.py b/tests/test_ModelicaSystem.py index b4d328e9..a943d6a7 100644 --- a/tests/test_ModelicaSystem.py +++ b/tests/test_ModelicaSystem.py @@ -396,12 +396,14 @@ def test_simulate_inputs(tmp_path): "u1=[(0.0, 0), (1.0, 1)]", "u2=[(0.0, 0), (0.25, 0.5), (0.5, 1.0), (1.0, 0)]", ]) - mod.simulate() - assert pathlib.Path(mod._csvFile).read_text() == """time,u1,u2,end + csv_file = mod.createCSVData() + assert pathlib.Path(csv_file).read_text() == """time,u1,u2,end 0.0,0.0,0.0,0 0.25,0.25,0.5,0 0.5,0.5,1.0,0 1.0,1.0,0.0,0 """ + + mod.simulate() y = mod.getSolutions("y")[0] assert np.isclose(y[-1], 1.0) From 8ecb14738c7d47fd319ab990421bad7ecae378ce Mon Sep 17 00:00:00 2001 From: syntron Date: Mon, 7 Jul 2025 19:18:19 +0200 Subject: [PATCH 3/4] [ModelicaSystem] fix rebase - use csvfile instead of csvFile --- OMPython/ModelicaSystem.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/OMPython/ModelicaSystem.py b/OMPython/ModelicaSystem.py index add3587c..29929cd9 100644 --- a/OMPython/ModelicaSystem.py +++ b/OMPython/ModelicaSystem.py @@ -1281,7 +1281,7 @@ def _createCSVData(self, csvfile: Optional[pathlib.Path] = None) -> pathlib.Path csv_rows.append(row) if csvfile is None: - csvFile = self._tempdir / f'{self._model_name}.csv' + csvfile = self._tempdir / f'{self._model_name}.csv' with open(file=csvfile, mode="w", encoding="utf-8", newline="") as fh: writer = csv.writer(fh) From fd3953e34f36cb08690892604c987b1c842ad07f Mon Sep 17 00:00:00 2001 From: syntron Date: Mon, 7 Jul 2025 20:21:42 +0200 Subject: [PATCH 4/4] [test_ModelicaSystem] fix rebase fallout --- tests/test_ModelicaSystem.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_ModelicaSystem.py b/tests/test_ModelicaSystem.py index a943d6a7..a7a4b472 100644 --- a/tests/test_ModelicaSystem.py +++ b/tests/test_ModelicaSystem.py @@ -396,7 +396,7 @@ def test_simulate_inputs(tmp_path): "u1=[(0.0, 0), (1.0, 1)]", "u2=[(0.0, 0), (0.25, 0.5), (0.5, 1.0), (1.0, 0)]", ]) - csv_file = mod.createCSVData() + csv_file = mod._createCSVData() assert pathlib.Path(csv_file).read_text() == """time,u1,u2,end 0.0,0.0,0.0,0 0.25,0.25,0.5,0