Skip to content

Commit 51c2096

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 dd9f162 commit 51c2096

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
@@ -952,7 +952,7 @@ def getSimulationOptions(
952952
def getLinearizationOptions(
953953
self,
954954
names: Optional[str | list[str]] = None,
955-
) -> dict[str, str | float] | list[str | float]:
955+
) -> dict[str, str] | list[str]:
956956
"""Get simulation options used for linearization.
957957
958958
Args:
@@ -966,31 +966,30 @@ def getLinearizationOptions(
966966
returned.
967967
If `names` is a list, a list with one value for each option name
968968
in names is returned: [option1_value, option2_value, ...].
969-
Some option values are returned as float when first initialized,
970-
but always as strings after setLinearizationOptions is used to
971-
change them.
969+
970+
The option values are always returned as strings.
972971
973972
Examples:
974973
>>> mod.getLinearizationOptions()
975-
{'startTime': 0.0, 'stopTime': 1.0, 'stepSize': 0.002, 'tolerance': 1e-08}
974+
{'startTime': '0.0', 'stopTime': '1.0', 'stepSize': '0.002', 'tolerance': '1e-08'}
976975
>>> mod.getLinearizationOptions("stopTime")
977-
[1.0]
976+
['1.0']
978977
>>> mod.getLinearizationOptions(["tolerance", "stopTime"])
979-
[1e-08, 1.0]
978+
['1e-08', '1.0']
980979
"""
981980
if names is None:
982-
return self._linearization_options
981+
return {key: str(val) for key, val in self._linearization_options.items()}
983982
if isinstance(names, str):
984983
return [self._linearization_options[names]]
985984
if isinstance(names, list):
986-
return [self._linearization_options[x] for x in names]
985+
return [str(self._linearization_options[x]) for x in names]
987986

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

990989
def getOptimizationOptions(
991990
self,
992991
names: Optional[str | list[str]] = None,
993-
) -> dict[str, str | float] | list[str | float]:
992+
) -> dict[str, str] | list[str]:
994993
"""Get simulation options used for optimization.
995994
996995
Args:
@@ -1004,9 +1003,8 @@ def getOptimizationOptions(
10041003
returned.
10051004
If `names` is a list, a list with one value for each option name
10061005
in names is returned: [option1_value, option2_value, ...].
1007-
Some option values are returned as float when first initialized,
1008-
but always as strings after setOptimizationOptions is used to
1009-
change them.
1006+
1007+
The option values are always returned as string.
10101008
10111009
Examples:
10121010
>>> mod.getOptimizationOptions()
@@ -1017,11 +1015,11 @@ def getOptimizationOptions(
10171015
[1e-08, 1.0]
10181016
"""
10191017
if names is None:
1020-
return self._optimization_options
1018+
return {key: str(val) for key, val in self._optimization_options.items()}
10211019
if isinstance(names, str):
1022-
return [self._optimization_options[names]]
1020+
return [str(self._optimization_options[names])]
10231021
if isinstance(names, list):
1024-
return [self._optimization_options[x] for x in names]
1022+
return [str(self._optimization_options[x]) for x in names]
10251023

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

0 commit comments

Comments
 (0)