Skip to content
Draft
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
2,755 changes: 293 additions & 2,462 deletions OMPython/ModelicaSystem.py

Large diffs are not rendered by default.

1,853 changes: 48 additions & 1,805 deletions OMPython/OMCSession.py

Large diffs are not rendered by default.

109 changes: 65 additions & 44 deletions OMPython/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,50 +6,67 @@
```
import OMPython
omc = OMPython.OMCSessionLocal()
omc.sendExpression("command")
omc.sendExpression("getVersion()")
```

"""

from OMPython.model_execution import (
ModelExecutionCmd,
ModelExecutionData,
ModelExecutionConfig,
ModelExecutionRun,
ModelExecutionException,
)

from OMPython.ModelicaSystem import (
LinearizationResult,
ModelicaSystem,
ModelicaSystemOMC,
ModelicaSystemDoE,
ModelicaDoEOMC,
ModelicaSystemError,
ModelicaSystemRunner,
ModelicaDoERunner,

doe_get_solutions,

ModelicaSystemCmd,
)
from OMPython.OMCSession import (
from OMPython.om_session_abc import (
OMPathABC,
OMCPath,

OMSessionABC,
OMSessionRunner,

OMSessionException,
)
from OMPython.om_session_omc import (
OMCPath,
OMCSessionABC,
OMCSessionCmd,
OMCSessionDocker,
OMCSessionDockerContainer,
OMCSessionException,
OMCSessionLocal,
OMCSessionPort,

OMCSessionWSL,
)
from OMPython.om_session_runner import (
OMPathRunnerBash,
OMPathRunnerLocal,
OMSessionRunner,
)
from OMPython.modelica_system_abc import (
LinearizationResult,
ModelicaSystemABC,
ModelicaSystemError,
)
from OMPython.modelica_system_omc import (
ModelicaSystemOMC,
)
from OMPython.modelica_system_runner import (
ModelicaSystemRunner,
)
from OMPython.modelica_doe_abc import (
ModelicaDoEABC,
)
from OMPython.modelica_doe_omc import (
doe_get_solutions,

OMCSessionWSL,
ModelicaDoEOMC,
)
from OMPython.modelica_doe_runner import (
ModelicaDoERunner,
)

# the imports below are compatibility functionality (OMPython v4.0.0)
from OMPython.ModelicaSystem import (
ModelicaSystem,
ModelicaSystemDoE,
parse_simflags,
)
from OMPython.OMCSession import (
OMCSessionCmd,
OMCSessionException,
OMCSessionZMQ,

OMCProcessLocal,
Expand All @@ -60,42 +77,46 @@

# global names imported if import 'from OMPython import *' is used
__all__ = [
'doe_get_solutions',

'LinearizationResult',

'ModelExecutionCmd',
'ModelExecutionData',
'ModelExecutionConfig',
'ModelExecutionRun',
'ModelExecutionException',

'ModelicaSystem',
'ModelicaSystemOMC',
'ModelicaSystemCmd',
'ModelicaSystemDoE',
'ModelicaDoEABC',
'ModelicaDoEOMC',
'ModelicaDoERunner',
'ModelicaSystemABC',
'ModelicaSystemError',

'ModelicaSystemOMC',
'ModelicaSystemRunner',
'ModelicaDoERunner',

'OMPathABC',
'OMCPath',

'OMSessionABC',
'OMSessionRunner',

'doe_get_solutions',
'OMSessionException',

'OMCPath',
'OMCSessionABC',
'OMCSessionCmd',
'OMCSessionDocker',
'OMCSessionDockerContainer',
'OMCSessionException',
'OMCSessionPort',
'OMCSessionLocal',
'OMCSessionPort',
'OMCSessionWSL',

'OMPathRunnerBash',
'OMPathRunnerLocal',
'OMSessionRunner',

'ModelicaSystem',
'ModelicaSystemDoE',
'parse_simflags',

'OMCSessionCmd',

'OMCSessionException',

'OMCSessionWSL',
'OMCSessionZMQ',

'OMCProcessLocal',
Expand Down
39 changes: 39 additions & 0 deletions OMPython/compatibility_v400.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# -*- coding: utf-8 -*-
"""
Helper functions for compatibility with OMPython v4.0.0
"""
import warnings
from typing import Optional


def depreciated_class(msg: Optional[str] = None):
"""
Decorator for depreciated / compatibility classes.
"""

def depreciated(cls):
"""
Helper functions to do the decoration part.
"""

class Wrapper(cls):
"""
Wrapper to define the depreciation message.
"""

def __init__(self, *args, **kwargs):
message = f"The class {cls.__name__} is depreciated and will be removed in future versions!"
if msg is not None:
message += f" {msg}"

warnings.warn(
message=message,
category=DeprecationWarning,
stacklevel=3,
)

super().__init__(*args, **kwargs)

return Wrapper

return depreciated
67 changes: 12 additions & 55 deletions OMPython/model_execution.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import re
import subprocess
from typing import Any, Optional
import warnings

# define logger using the current module name as ID
logger = logging.getLogger(__name__)
Expand All @@ -27,14 +26,13 @@ class ModelExecutionException(Exception):


@dataclasses.dataclass
class ModelExecutionData:
class ModelExecutionRun:
"""
Data class to store the command line data for running a model executable in the OMC environment.
Data class to store the command line data for running a model executable. This definition is independent of the OMC
environment as only the executable is needed.

All data should be defined for the environment, where OMC is running (local, docker or WSL)

To use this as a definition of an OMC simulation run, it has to be processed within
OMCProcess*.self_update(). This defines the attribute cmd_model_executable.
All data should be defined for the environment, where the executable was defined / is located. This is especially
important if OMPython and the executable are defined in different environments (docker or WSL).
"""
# cmd_path is the expected working directory
cmd_path: str
Expand Down Expand Up @@ -105,11 +103,12 @@ def run(self) -> int:
return returncode


class ModelExecutionCmd:
class ModelExecutionConfig:
"""
All information about a compiled model executable. This should include data about all structured parameters, i.e.
parameters which need a recompilation of the model. All non-structured parameters can be easily changed without
the need for recompilation.
This class collects all information about a compiled model executable. This includes data about all structured
parameters, i.e. parameters which need a recompilation of the model. All non-structured parameters can be easily
changed without the need for recompilation. The final result is an instance of class ModelExecutionRun - a
definition to run one simulation based on the compiled model executable.
"""

def __init__(
Expand Down Expand Up @@ -261,7 +260,7 @@ def get_cmd_args(self) -> list[str]:

return cmdl

def definition(self) -> ModelExecutionData:
def definition(self) -> ModelExecutionRun:
"""
Define all needed data to run the model executable. The data is stored in an OMCSessionRunData object.
"""
Expand Down Expand Up @@ -301,7 +300,7 @@ def definition(self) -> ModelExecutionData:
if self._cmd_local:
cmd_cwd_local = cmd_path.as_posix()

omc_run_data = ModelExecutionData(
omc_run_data = ModelExecutionRun(
cmd_path=cmd_path.as_posix(),
cmd_model_name=self._model_name,
cmd_args=self.get_cmd_args(),
Expand All @@ -314,45 +313,3 @@ def definition(self) -> ModelExecutionData:
)

return omc_run_data

@staticmethod
def parse_simflags(simflags: str) -> dict[str, Optional[str | dict[str, Any] | numbers.Number]]:
"""
Parse a simflag definition; this is deprecated!

The return data can be used as input for self.args_set().
"""
warnings.warn(
message="The argument 'simflags' is depreciated and will be removed in future versions; "
"please use 'simargs' instead",
category=DeprecationWarning,
stacklevel=2,
)

simargs: dict[str, Optional[str | dict[str, Any] | numbers.Number]] = {}

args = [s for s in simflags.split(' ') if s]
for arg in args:
if arg[0] != '-':
raise ModelExecutionException(f"Invalid simulation flag: {arg}")
arg = arg[1:]
parts = arg.split('=')
if len(parts) == 1:
simargs[parts[0]] = None
elif parts[0] == 'override':
override = '='.join(parts[1:])

override_dict = {}
for item in override.split(','):
kv = item.split('=')
if not 0 < len(kv) < 3:
raise ModelExecutionException(f"Invalid value for '-override': {override}")
if kv[0]:
try:
override_dict[kv[0]] = kv[1]
except (KeyError, IndexError) as ex:
raise ModelExecutionException(f"Invalid value for '-override': {override}") from ex

simargs[parts[0]] = override_dict

return simargs
Loading
Loading