Skip to content

Commit 28ac83c

Browse files
committed
[ModelExecutionCmd] remove depreciated simflags
1 parent 570dc39 commit 28ac83c

3 files changed

Lines changed: 117 additions & 63 deletions

File tree

OMPython/ModelicaSystem.py

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,30 @@
33
Definition of main class to run Modelica simulations - ModelicaSystem.
44
"""
55

6+
from __future__ import annotations
7+
68
import logging
9+
import numbers
710
import os
811
import pathlib
912
import platform
1013
from typing import Any, Optional
14+
import warnings
1115

1216
import numpy as np
1317

1418
from OMPython.model_execution import (
1519
ModelExecutionCmd,
1620
ModelExecutionException,
1721
)
22+
from OMPython.om_session_abc import (
23+
OMPathABC,
24+
)
1825
from OMPython.om_session_omc import (
1926
OMCSessionLocal,
2027
)
2128
from OMPython.modelica_system_abc import (
29+
LinearizationResult,
2230
ModelicaSystemError,
2331
)
2432
from OMPython.modelica_system_omc import (
@@ -72,6 +80,71 @@ def __init__(
7280
def setCommandLineOptions(self, commandLineOptions: str):
7381
super().set_command_line_options(command_line_option=commandLineOptions)
7482

83+
def simulate_cmd( # type: ignore[override]
84+
self,
85+
result_file: OMPathABC,
86+
simflags: Optional[str] = None,
87+
simargs: Optional[dict[str, Optional[str | dict[str, Any] | numbers.Number]]] = None,
88+
) -> ModelExecutionCmd:
89+
"""
90+
Compatibility layer for OMPython v4.0.0 - keep simflags available and use ModelicaSystemCmd!
91+
"""
92+
93+
if simargs is None:
94+
simargs = {}
95+
96+
if simflags is not None:
97+
simargs_extra = ModelicaSystemCmd.parse_simflags(simflags=simflags)
98+
simargs = simargs | simargs_extra
99+
100+
return super().simulate_cmd(
101+
result_file=result_file,
102+
simargs=simargs,
103+
)
104+
105+
def simulate( # type: ignore[override]
106+
self,
107+
resultfile: Optional[str | os.PathLike] = None,
108+
simflags: Optional[str] = None,
109+
simargs: Optional[dict[str, Optional[str | dict[str, Any] | numbers.Number]]] = None,
110+
) -> None:
111+
"""
112+
Compatibility layer for OMPython v4.0.0 - keep simflags available and use ModelicaSystemCmd!
113+
"""
114+
115+
if simargs is None:
116+
simargs = {}
117+
118+
if simflags is not None:
119+
simargs_extra = ModelicaSystemCmd.parse_simflags(simflags=simflags)
120+
simargs = simargs | simargs_extra
121+
122+
return super().simulate(
123+
resultfile=resultfile,
124+
simargs=simargs,
125+
)
126+
127+
def linearize( # type: ignore[override]
128+
self,
129+
lintime: Optional[float] = None,
130+
simflags: Optional[str] = None,
131+
simargs: Optional[dict[str, Optional[str | dict[str, Any] | numbers.Number]]] = None,
132+
) -> LinearizationResult:
133+
"""
134+
Compatibility layer for OMPython v4.0.0 - keep simflags available and use ModelicaSystemCmd!
135+
"""
136+
if simargs is None:
137+
simargs = {}
138+
139+
if simflags is not None:
140+
simargs_extra = ModelicaSystemCmd.parse_simflags(simflags=simflags)
141+
simargs = simargs | simargs_extra
142+
143+
return super().linearize(
144+
lintime=lintime,
145+
simargs=simargs,
146+
)
147+
75148
@staticmethod
76149
def _set_compatibility_helper(
77150
pkey: str,
@@ -300,6 +373,8 @@ class ModelicaSystemDoE(ModelicaDoEOMC):
300373
class ModelicaSystemCmd(ModelExecutionCmd):
301374
"""
302375
Compatibility class; in the new version it is renamed as ModelExecutionCmd.
376+
377+
This class is only defined for the unit tests - it is NOT used within ModelicaSystem of v4.0.0!
303378
"""
304379

305380
def __init__(
@@ -347,3 +422,45 @@ def run(self) -> int:
347422
except ModelExecutionException as exc:
348423
raise ModelicaSystemError(f"Cannot execute model: {exc}") from exc
349424
return returncode
425+
426+
@staticmethod
427+
def parse_simflags(simflags: str) -> dict[str, Optional[str | dict[str, Any] | numbers.Number]]:
428+
"""
429+
Parse a simflag definition; this is deprecated!
430+
431+
The return data can be used as input for self.args_set().
432+
"""
433+
warnings.warn(
434+
message="The argument 'simflags' is depreciated and will be removed in future versions; "
435+
"please use 'simargs' instead",
436+
category=DeprecationWarning,
437+
stacklevel=2,
438+
)
439+
440+
simargs: dict[str, Optional[str | dict[str, Any] | numbers.Number]] = {}
441+
442+
args = [s for s in simflags.split(' ') if s]
443+
for arg in args:
444+
if arg[0] != '-':
445+
raise ModelExecutionException(f"Invalid simulation flag: {arg}")
446+
arg = arg[1:]
447+
parts = arg.split('=')
448+
if len(parts) == 1:
449+
simargs[parts[0]] = None
450+
elif parts[0] == 'override':
451+
override = '='.join(parts[1:])
452+
453+
override_dict = {}
454+
for item in override.split(','):
455+
kv = item.split('=')
456+
if not 0 < len(kv) < 3:
457+
raise ModelExecutionException(f"Invalid value for '-override': {override}")
458+
if kv[0]:
459+
try:
460+
override_dict[kv[0]] = kv[1]
461+
except (KeyError, IndexError) as ex:
462+
raise ModelExecutionException(f"Invalid value for '-override': {override}") from ex
463+
464+
simargs[parts[0]] = override_dict
465+
466+
return simargs

OMPython/model_execution.py

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
import re
1313
import subprocess
1414
from typing import Any, Optional
15-
import warnings
1615

1716
# define logger using the current module name as ID
1817
logger = logging.getLogger(__name__)
@@ -305,45 +304,3 @@ def definition(self) -> ModelExecutionData:
305304
)
306305

307306
return omc_run_data
308-
309-
@staticmethod
310-
def parse_simflags(simflags: str) -> dict[str, Optional[str | dict[str, Any] | numbers.Number]]:
311-
"""
312-
Parse a simflag definition; this is deprecated!
313-
314-
The return data can be used as input for self.args_set().
315-
"""
316-
warnings.warn(
317-
message="The argument 'simflags' is depreciated and will be removed in future versions; "
318-
"please use 'simargs' instead",
319-
category=DeprecationWarning,
320-
stacklevel=2,
321-
)
322-
323-
simargs: dict[str, Optional[str | dict[str, Any] | numbers.Number]] = {}
324-
325-
args = [s for s in simflags.split(' ') if s]
326-
for arg in args:
327-
if arg[0] != '-':
328-
raise ModelExecutionException(f"Invalid simulation flag: {arg}")
329-
arg = arg[1:]
330-
parts = arg.split('=')
331-
if len(parts) == 1:
332-
simargs[parts[0]] = None
333-
elif parts[0] == 'override':
334-
override = '='.join(parts[1:])
335-
336-
override_dict = {}
337-
for item in override.split(','):
338-
kv = item.split('=')
339-
if not 0 < len(kv) < 3:
340-
raise ModelExecutionException(f"Invalid value for '-override': {override}")
341-
if kv[0]:
342-
try:
343-
override_dict[kv[0]] = kv[1]
344-
except (KeyError, IndexError) as ex:
345-
raise ModelExecutionException(f"Invalid value for '-override': {override}") from ex
346-
347-
simargs[parts[0]] = override_dict
348-
349-
return simargs

OMPython/modelica_system_abc.py

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -616,7 +616,6 @@ def _process_override_data(
616616
def simulate_cmd(
617617
self,
618618
result_file: OMPathABC,
619-
simflags: Optional[str] = None,
620619
simargs: Optional[dict[str, Optional[str | dict[str, Any] | numbers.Number]]] = None,
621620
) -> ModelExecutionCmd:
622621
"""
@@ -632,7 +631,6 @@ def simulate_cmd(
632631
Parameters
633632
----------
634633
result_file
635-
simflags
636634
simargs
637635
638636
Returns
@@ -651,10 +649,6 @@ def simulate_cmd(
651649
# always define the result file to use
652650
om_cmd.arg_set(key="r", val=result_file.as_posix())
653651

654-
# allow runtime simulation flags from user input
655-
if simflags is not None:
656-
om_cmd.args_set(args=om_cmd.parse_simflags(simflags=simflags))
657-
658652
if simargs:
659653
om_cmd.args_set(args=simargs)
660654

@@ -688,7 +682,6 @@ def simulate_cmd(
688682
def simulate(
689683
self,
690684
resultfile: Optional[str | os.PathLike] = None,
691-
simflags: Optional[str] = None,
692685
simargs: Optional[dict[str, Optional[str | dict[str, Any] | numbers.Number]]] = None,
693686
) -> None:
694687
"""Simulate the model according to simulation options.
@@ -697,16 +690,11 @@ def simulate(
697690
698691
Args:
699692
resultfile: Path to a custom result file
700-
simflags: String of extra command line flags for the model binary.
701-
This argument is deprecated, use simargs instead.
702693
simargs: Dict with simulation runtime flags.
703694
704695
Examples:
705696
mod.simulate()
706697
mod.simulate(resultfile="a.mat")
707-
# set runtime simulation flags, deprecated
708-
mod.simulate(simflags="-noEventEmit -noRestart -override=e=0.3,g=10")
709-
# using simargs
710698
mod.simulate(simargs={"noEventEmit": None, "noRestart": None, "override": "override": {"e": 0.3, "g": 10}})
711699
"""
712700

@@ -725,7 +713,6 @@ def simulate(
725713

726714
om_cmd = self.simulate_cmd(
727715
result_file=self._result_file,
728-
simflags=simflags,
729716
simargs=simargs,
730717
)
731718

@@ -1123,7 +1110,6 @@ def _createCSVData(self, csvfile: Optional[OMPathABC] = None) -> OMPathABC:
11231110
def linearize(
11241111
self,
11251112
lintime: Optional[float] = None,
1126-
simflags: Optional[str] = None,
11271113
simargs: Optional[dict[str, Optional[str | dict[str, Any] | numbers.Number]]] = None,
11281114
) -> LinearizationResult:
11291115
"""Linearize the model according to linearization options.
@@ -1132,8 +1118,6 @@ def linearize(
11321118
11331119
Args:
11341120
lintime: Override "stopTime" value.
1135-
simflags: String of extra command line flags for the model binary.
1136-
This argument is deprecated, use simargs instead.
11371121
simargs: A dict with command line flags and possible options; example: "simargs={'csvInput': 'a.csv'}"
11381122
11391123
Returns:
@@ -1185,10 +1169,6 @@ def linearize(
11851169
f"<= lintime <= {self._linearization_options['stopTime']}")
11861170
om_cmd.arg_set(key="l", val=str(lintime))
11871171

1188-
# allow runtime simulation flags from user input
1189-
if simflags is not None:
1190-
om_cmd.args_set(args=om_cmd.parse_simflags(simflags=simflags))
1191-
11921172
if simargs:
11931173
om_cmd.args_set(args=simargs)
11941174

0 commit comments

Comments
 (0)