Skip to content

Commit c91c0e3

Browse files
committed
[ModelicaSystem] define _linearization_options and _optimization_options as dict[str, str]
* after OMC is run, the values will be string anyway * simplify code / align on one common definition for these dicts
1 parent 206ba19 commit c91c0e3

1 file changed

Lines changed: 20 additions & 22 deletions

File tree

OMPython/ModelicaSystem.py

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -331,14 +331,14 @@ def __init__(
331331
self._simulate_options: dict[str, str] = {}
332332
self._override_variables: dict[str, str] = {}
333333
self._simulate_options_override: dict[str, str] = {}
334-
self._linearization_options: dict[str, str | float] = {
335-
'startTime': 0.0,
336-
'stopTime': 1.0,
337-
'stepSize': 0.002,
338-
'tolerance': 1e-8,
334+
self._linearization_options: dict[str, str] = {
335+
'startTime': str(0.0),
336+
'stopTime': str(1.0),
337+
'stepSize': str(0.002),
338+
'tolerance': str(1e-8),
339339
}
340340
self._optimization_options = self._linearization_options | {
341-
'numberOfIntervals': 500,
341+
'numberOfIntervals': str(500),
342342
}
343343
self._linearized_inputs: list[str] = [] # linearization input list
344344
self._linearized_outputs: list[str] = [] # linearization output list
@@ -951,7 +951,7 @@ def getSimulationOptions(
951951
def getLinearizationOptions(
952952
self,
953953
names: Optional[str | list[str]] = None,
954-
) -> dict[str, str | float] | list[str | float]:
954+
) -> dict[str, str] | list[str]:
955955
"""Get simulation options used for linearization.
956956
957957
Args:
@@ -965,31 +965,30 @@ def getLinearizationOptions(
965965
returned.
966966
If `names` is a list, a list with one value for each option name
967967
in names is returned: [option1_value, option2_value, ...].
968-
Some option values are returned as float when first initialized,
969-
but always as strings after setLinearizationOptions is used to
970-
change them.
968+
969+
The option values are always returned as strings.
971970
972971
Examples:
973972
>>> mod.getLinearizationOptions()
974-
{'startTime': 0.0, 'stopTime': 1.0, 'stepSize': 0.002, 'tolerance': 1e-08}
973+
{'startTime': '0.0', 'stopTime': '1.0', 'stepSize': '0.002', 'tolerance': '1e-08'}
975974
>>> mod.getLinearizationOptions("stopTime")
976-
[1.0]
975+
['1.0']
977976
>>> mod.getLinearizationOptions(["tolerance", "stopTime"])
978-
[1e-08, 1.0]
977+
['1e-08', '1.0']
979978
"""
980979
if names is None:
981-
return self._linearization_options
980+
return {key: str(val) for key, val in self._linearization_options.items()}
982981
if isinstance(names, str):
983982
return [self._linearization_options[names]]
984983
if isinstance(names, list):
985-
return [self._linearization_options[x] for x in names]
984+
return [str(self._linearization_options[x]) for x in names]
986985

987986
raise ModelicaSystemError("Unhandled input for getLinearizationOptions()")
988987

989988
def getOptimizationOptions(
990989
self,
991990
names: Optional[str | list[str]] = None,
992-
) -> dict[str, str | float] | list[str | float]:
991+
) -> dict[str, str] | list[str]:
993992
"""Get simulation options used for optimization.
994993
995994
Args:
@@ -1003,9 +1002,8 @@ def getOptimizationOptions(
10031002
returned.
10041003
If `names` is a list, a list with one value for each option name
10051004
in names is returned: [option1_value, option2_value, ...].
1006-
Some option values are returned as float when first initialized,
1007-
but always as strings after setOptimizationOptions is used to
1008-
change them.
1005+
1006+
The option values are always returned as string.
10091007
10101008
Examples:
10111009
>>> mod.getOptimizationOptions()
@@ -1016,11 +1014,11 @@ def getOptimizationOptions(
10161014
[1e-08, 1.0]
10171015
"""
10181016
if names is None:
1019-
return self._optimization_options
1017+
return {key: str(val) for key, val in self._optimization_options.items()}
10201018
if isinstance(names, str):
1021-
return [self._optimization_options[names]]
1019+
return [str(self._optimization_options[names])]
10221020
if isinstance(names, list):
1023-
return [self._optimization_options[x] for x in names]
1021+
return [str(self._optimization_options[x]) for x in names]
10241022

10251023
raise ModelicaSystemError("Unhandled input for getOptimizationOptions()")
10261024

0 commit comments

Comments
 (0)