Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
202 changes: 164 additions & 38 deletions OMPython/ModelicaSystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import numpy as np

from OMPython.model_execution import (
ModelExecutionCmd,
ModelExecutionConfig,
ModelExecutionException,
)
from OMPython.om_session_omc import (
Expand All @@ -28,10 +28,15 @@
ModelicaDoEOMC,
)

from OMPython.compatibility_v400 import (
depreciated_class,
)

# define logger using the current module name as ID
logger = logging.getLogger(__name__)


@depreciated_class(msg="Please use class ModelicaSystemOMC instead!")
class ModelicaSystem(ModelicaSystemOMC):
"""
Compatibility class.
Expand Down Expand Up @@ -67,58 +72,167 @@ def __init__(
def setCommandLineOptions(self, commandLineOptions: str):
super().set_command_line_options(command_line_option=commandLineOptions)

def setContinuous( # type: ignore[override]
def _set_compatibility_helper(
self,
pkey: str,
args: Any,
kwargs: dict[str, Any],
) -> Any:
param = None
if len(args) == 1:
param = args[0]
if param is None and pkey in kwargs:
param = kwargs[pkey]

return param

def setContinuous(
self,
cvals: str | list[str] | dict[str, Any],
*args: Any,
**kwargs: dict[str, Any],
) -> bool:
if isinstance(cvals, dict):
return super().setContinuous(**cvals)
raise ModelicaSystemError("Only dict input supported for setContinuous()")
"""
Compatibility wrapper for setContinuous() from OMPython v4.0.0

Original definition:

def setParameters( # type: ignore[override]
```
def setContinuous(
self,
cvals: str | list[str] | dict[str, Any],
) -> bool:
```
"""
param = self._set_compatibility_helper(pkey='cvals', args=args, kwargs=kwargs)
if param is None:
raise ModelicaSystemError("Invalid input for setContinuous() (v4.0.0 compatibility mode).")

return super().setContinuous(param)

def setParameters(
self,
pvals: str | list[str] | dict[str, Any],
*args: Any,
**kwargs: dict[str, Any],
) -> bool:
if isinstance(pvals, dict):
return super().setParameters(**pvals)
raise ModelicaSystemError("Only dict input supported for setParameters()")
"""
Compatibility wrapper for setParameters() from OMPython v4.0.0

Original definition:

def setOptimizationOptions( # type: ignore[override]
```
def setParameters(
self,
pvals: str | list[str] | dict[str, Any],
) -> bool:
```
"""
param = self._set_compatibility_helper(pkey='pvals', args=args, kwargs=kwargs)
if param is None:
raise ModelicaSystemError("Invalid input for setParameters() (v4.0.0 compatibility mode).")

return super().setParameters(param)

def setOptimizationOptions(
self,
optimizationOptions: str | list[str] | dict[str, Any],
*args: Any,
**kwargs: dict[str, Any],
) -> bool:
if isinstance(optimizationOptions, dict):
return super().setOptimizationOptions(**optimizationOptions)
raise ModelicaSystemError("Only dict input supported for setOptimizationOptions()")
"""
Compatibility wrapper for setOptimizationOptions() from OMPython v4.0.0

Original definition:

def setInputs( # type: ignore[override]
```
def setOptimizationOptions(
self,
optimizationOptions: str | list[str] | dict[str, Any],
) -> bool:
```
"""
param = self._set_compatibility_helper(pkey='optimizationOptions', args=args, kwargs=kwargs)
if param is None:
raise ModelicaSystemError("Invalid input for setOptimizationOptions() (v4.0.0 compatibility mode).")

return super().setOptimizationOptions(param)

def setInputs(
self,
name: str | list[str] | dict[str, Any],
*args: Any,
**kwargs: dict[str, Any],
) -> bool:
if isinstance(name, dict):
return super().setInputs(**name)
raise ModelicaSystemError("Only dict input supported for setInputs()")
"""
Compatibility wrapper for setInputs() from OMPython v4.0.0

Original definition:

def setSimulationOptions( # type: ignore[override]
```
def setInputs(
self,
name: str | list[str] | dict[str, Any],
) -> bool:
```
"""
param = self._set_compatibility_helper(pkey='name', args=args, kwargs=kwargs)
if param is None:
raise ModelicaSystemError("Invalid input for setInputs() (v4.0.0 compatibility mode).")

return super().setInputs(param)

def setSimulationOptions(
self,
simOptions: str | list[str] | dict[str, Any],
*args: Any,
**kwargs: dict[str, Any],
) -> bool:
if isinstance(simOptions, dict):
return super().setSimulationOptions(**simOptions)
raise ModelicaSystemError("Only dict input supported for setSimulationOptions()")
"""
Compatibility wrapper for setSimulationOptions() from OMPython v4.0.0

Original definition:

def setLinearizationOptions( # type: ignore[override]
```
def setSimulationOptions(
self,
simOptions: str | list[str] | dict[str, Any],
) -> bool:
```
"""
param = self._set_compatibility_helper(pkey='simOptions', args=args, kwargs=kwargs)
if param is None:
raise ModelicaSystemError("Invalid input for setSimulationOptions() (v4.0.0 compatibility mode).")

return super().setSimulationOptions(param)

def setLinearizationOptions(
self,
linearizationOptions: str | list[str] | dict[str, Any],
*args: Any,
**kwargs: dict[str, Any],
) -> bool:
if isinstance(linearizationOptions, dict):
return super().setLinearizationOptions(**linearizationOptions)
raise ModelicaSystemError("Only dict input supported for setLinearizationOptions()")
"""
Compatibility wrapper for setLinearizationOptions() from OMPython v4.0.0

Original definition:

```
def setLinearizationOptions(
self,
linearizationOptions: str | list[str] | dict[str, Any],
) -> bool:
```
"""
param = self._set_compatibility_helper(pkey='linearizationOptions', args=args, kwargs=kwargs)
if param is None:
raise ModelicaSystemError("Invalid input for setLinearizationOptions() (v4.0.0 compatibility mode).")

return super().setLinearizationOptions(param)

def getContinuous(
self,
names: Optional[str | list[str]] = None,
):
"""
Compatibility wrapper for getContinuous() from OMPython v4.0.0

If no model simulation was run (self._simulated == False), the return value should be converted to str.
"""
retval = super().getContinuous(names=names)
if self._simulated:
return retval
Expand All @@ -140,12 +254,17 @@ def getContinuous(
retval3.append(str(val))
return retval3

raise ModelExecutionException("Invalid data!")
raise ModelicaSystemError("Invalid data!")

def getOutputs(
self,
names: Optional[str | list[str]] = None,
):
"""
Compatibility wrapper for getOutputs() from OMPython v4.0.0

If no model simulation was run (self._simulated == False), the return value should be converted to str.
"""
retval = super().getOutputs(names=names)
if self._simulated:
return retval
Expand All @@ -167,18 +286,20 @@ def getOutputs(
retval3.append(str(val))
return retval3

raise ModelExecutionException("Invalid data!")
raise ModelicaSystemError("Invalid data!")


@depreciated_class(msg="Please use class ModelicaDoEOMC instead!")
class ModelicaSystemDoE(ModelicaDoEOMC):
"""
Compatibility class.
"""


class ModelicaSystemCmd(ModelExecutionCmd):
@depreciated_class(msg="Please use class ModelExecutionConfig instead!")
class ModelicaSystemCmd(ModelExecutionConfig):
"""
Compatibility class; in the new version it is renamed as ModelExecutionCmd.
Compatibility class; in the new version it is renamed as ModelExecutionConfig.
"""

def __init__(
Expand Down Expand Up @@ -209,7 +330,8 @@ def get_exe(self) -> pathlib.Path:
return path_exe

def get_cmd(self) -> list:
"""Get a list with the path to the executable and all command line args.
"""
Get a list with the path to the executable and all command line args.

This can later be used as an argument for subprocess.run().
"""
Expand All @@ -218,6 +340,10 @@ def get_cmd(self) -> list:

return cmdl

def run(self):
def run(self) -> int:
cmd_definition = self.definition()
return cmd_definition.run()
try:
returncode = cmd_definition.run()
except ModelExecutionException as exc:
raise ModelicaSystemError(f"Cannot execute model: {exc}") from exc
return returncode
Loading
Loading