|
3 | 3 | Definition of main class to run Modelica simulations - ModelicaSystem. |
4 | 4 | """ |
5 | 5 |
|
| 6 | +from __future__ import annotations |
| 7 | + |
6 | 8 | import logging |
| 9 | +import numbers |
7 | 10 | import os |
8 | 11 | import pathlib |
9 | 12 | import platform |
10 | 13 | from typing import Any, Optional |
| 14 | +import warnings |
11 | 15 |
|
12 | 16 | import numpy as np |
13 | 17 |
|
14 | 18 | from OMPython.model_execution import ( |
15 | 19 | ModelExecutionCmd, |
16 | 20 | ModelExecutionException, |
17 | 21 | ) |
| 22 | +from OMPython.om_session_abc import ( |
| 23 | + OMPathABC, |
| 24 | +) |
18 | 25 | from OMPython.om_session_omc import ( |
19 | 26 | OMCSessionLocal, |
20 | 27 | ) |
21 | 28 | from OMPython.modelica_system_abc import ( |
| 29 | + LinearizationResult, |
22 | 30 | ModelicaSystemError, |
23 | 31 | ) |
24 | 32 | from OMPython.modelica_system_omc import ( |
@@ -72,6 +80,71 @@ def __init__( |
72 | 80 | def setCommandLineOptions(self, commandLineOptions: str): |
73 | 81 | super().set_command_line_options(command_line_option=commandLineOptions) |
74 | 82 |
|
| 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 | + |
75 | 148 | @staticmethod |
76 | 149 | def _set_compatibility_helper( |
77 | 150 | pkey: str, |
@@ -300,6 +373,8 @@ class ModelicaSystemDoE(ModelicaDoEOMC): |
300 | 373 | class ModelicaSystemCmd(ModelExecutionCmd): |
301 | 374 | """ |
302 | 375 | 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! |
303 | 378 | """ |
304 | 379 |
|
305 | 380 | def __init__( |
@@ -347,3 +422,45 @@ def run(self) -> int: |
347 | 422 | except ModelExecutionException as exc: |
348 | 423 | raise ModelicaSystemError(f"Cannot execute model: {exc}") from exc |
349 | 424 | 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 |
0 commit comments