Skip to content

Commit 9fed44e

Browse files
syntronadeas31
andauthored
[ModelicaSystem] prepare OMCPath (#330)
* [ModelicaSystem] do not use package csv background: if OMCPath will be used, it is not available * [ModelicaSystem] create override file using pathlib.Path.write_text() background: open() is not available if OMCPath is used * [ModelicaSystem] update handling of override file * define file name based on result file name & Path * simplify code --------- Co-authored-by: Adeel Asghar <[email protected]>
1 parent 2e69f3c commit 9fed44e

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed

OMPython/ModelicaSystem.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
"""
3434

3535
import ast
36-
import csv
3736
from dataclasses import dataclass
3837
import logging
3938
import numbers
@@ -965,16 +964,17 @@ def simulate_cmd(
965964
if simargs:
966965
om_cmd.args_set(args=simargs)
967966

968-
overrideFile = self._tempdir / f"{self._model_name}_override.txt"
969967
if self._override_variables or self._simulate_options_override:
970-
tmpdict = self._override_variables.copy()
971-
tmpdict.update(self._simulate_options_override)
972-
# write to override file
973-
with open(file=overrideFile, mode="w", encoding="utf-8") as fh:
974-
for key, value in tmpdict.items():
975-
fh.write(f"{key}={value}\n")
968+
override_file = result_file.parent / f"{result_file.stem}_override.txt"
976969

977-
om_cmd.arg_set(key="overrideFile", val=overrideFile.as_posix())
970+
override_content = (
971+
"\n".join([f"{key}={value}" for key, value in self._override_variables.items()])
972+
+ "\n".join([f"{key}={value}" for key, value in self._simulate_options_override.items()])
973+
+ "\n"
974+
)
975+
976+
override_file.write_text(override_content)
977+
om_cmd.arg_set(key="overrideFile", val=override_file.as_posix())
978978

979979
if self._inputs: # if model has input quantities
980980
for key in self._inputs:
@@ -1432,9 +1432,10 @@ def _createCSVData(self, csvfile: Optional[pathlib.Path] = None) -> pathlib.Path
14321432
if csvfile is None:
14331433
csvfile = self._tempdir / f'{self._model_name}.csv'
14341434

1435-
with open(file=csvfile, mode="w", encoding="utf-8", newline="") as fh:
1436-
writer = csv.writer(fh)
1437-
writer.writerows(csv_rows)
1435+
# basic definition of a CSV file using csv_rows as input
1436+
csv_content = "\n".join([",".join(map(str, row)) for row in csv_rows]) + "\n"
1437+
1438+
csvfile.write_text(csv_content)
14381439

14391440
return csvfile
14401441

0 commit comments

Comments
 (0)