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
362 changes: 301 additions & 61 deletions OMPython/ModelicaSystem.py

Large diffs are not rendered by default.

74 changes: 51 additions & 23 deletions OMPython/OMCSession.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
OMSessionException,
)
from OMPython.om_session_omc import (
DockerPopen,
OMCSessionABC,
OMCSessionDocker,
OMCSessionDockerContainer,
Expand All @@ -26,30 +25,28 @@
OMCSessionWSL,
)

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 OMSessionException instead!")
class OMCSessionException(OMSessionException):
"""
Just a compatibility layer ...
"""


@depreciated_class(msg="Please use OMCSession*.sendExpression(...) instead!")
class OMCSessionCmd:
"""
Implementation of Open Modelica Compiler API functions. Depreciated!
"""

def __init__(self, session: OMSessionABC, readonly: bool = False):
warnings.warn(
message="The class OMCSessionCMD is depreciated and will be removed in future versions; "
"please use OMCSession*.sendExpression(...) instead!",
category=DeprecationWarning,
stacklevel=2,
)

if not isinstance(session, OMSessionABC):
raise OMCSessionException("Invalid OMC process definition!")
self._session = session
Expand Down Expand Up @@ -228,6 +225,7 @@ def getClassNames(self, className=None, recursive=False, qualified=False, sort=F
return self._ask(question='getClassNames', opt=opt)


@depreciated_class(msg="Please use OMCSession* classes instead!")
class OMCSessionZMQ(OMSessionABC):
"""
This class is a compatibility layer for the new schema using OMCSession* classes.
Expand All @@ -242,11 +240,6 @@ def __init__(
"""
Initialisation for OMCSessionZMQ
"""
warnings.warn(message="The class OMCSessionZMQ is depreciated and will be removed in future versions; "
"please use OMCProcess* classes instead!",
category=DeprecationWarning,
stacklevel=2)

if omc_process is None:
omc_process = OMCSessionLocal(omhome=omhome, timeout=timeout)
elif not isinstance(omc_process, OMCSessionABC):
Expand Down Expand Up @@ -280,14 +273,22 @@ def omcpath_tempdir(self, tempdir_base: Optional[OMPathABC] = None) -> OMPathABC
return self.omc_process.omcpath_tempdir(tempdir_base=tempdir_base)

def execute(self, command: str):
return self.omc_process.execute(command=command)
warnings.warn(
message="This function is depreciated and will be removed in future versions; "
"please use sendExpression() instead",
category=DeprecationWarning,
stacklevel=2,
)
return self.omc_process.sendExpression(expr=command, parsed=False)

def sendExpression(self, command: str, parsed: bool = True) -> Any:
def sendExpression(self, command: str, parsed: bool = True) -> Any: # pylint: disable=W0237
"""
Send an expression to the OMC server and return the result.

The complete error handling of the OMC result is done within this method using '"getMessagesStringInternal()'.
Caller should only check for OMSessionException.
The complete error handling of the OMC result is done within this method using 'getMessagesStringInternal()'.
Caller should only check for OMCSessionException.

Compatibility: 'command' was renamed to 'expr'
"""
return self.omc_process.sendExpression(expr=command, parsed=parsed)

Expand All @@ -301,9 +302,36 @@ def set_workdir(self, workdir: OMPathABC) -> None:
return self.omc_process.set_workdir(workdir=workdir)


DummyPopen = DockerPopen
OMCProcessLocal = OMCSessionLocal
OMCProcessPort = OMCSessionPort
OMCProcessDocker = OMCSessionDocker
OMCProcessDockerContainer = OMCSessionDockerContainer
OMCProcessWSL = OMCSessionWSL
@depreciated_class(msg="Please use class OMCSessionLocal instead!")
class OMCProcessLocal(OMCSessionLocal):
"""
Just a wrapper class; OMCProcessLocal => OMCSessionLocal
"""


@depreciated_class(msg="Please use class OMCSessionPort instead!")
class OMCProcessPort(OMCSessionPort):
"""
Just a wrapper class; OMCProcessPort => OMCSessionPort
"""


@depreciated_class(msg="Please use class OMCSessionDocker instead!")
class OMCProcessDocker(OMCSessionDocker):
"""
Just a wrapper class; OMCProcessDocker => OMCSessionDocker
"""


@depreciated_class(msg="Please use class OMCSessionDockerContainer instead!")
class OMCProcessDockerContainer(OMCSessionDockerContainer):
"""
Just a wrapper class; OMCProcessDockerContainer => OMCSessionDockerContainer
"""


@depreciated_class(msg="Please use class OMCSessionWSL instead!")
class OMCProcessWSL(OMCSessionWSL):
"""
Just a wrapper class; OMCProcessWSL => OMCSessionWSL
"""
20 changes: 10 additions & 10 deletions OMPython/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
```
import OMPython
omc = OMPython.OMCSessionLocal()
omc.sendExpression("command")
omc.sendExpression("getVersion()")
```

"""

from OMPython.model_execution import (
ModelExecutionCmd,
ModelExecutionData,
ModelExecutionConfig,
ModelExecutionRun,
ModelExecutionException,
)
from OMPython.om_session_abc import (
Expand Down Expand Up @@ -58,15 +58,16 @@
ModelicaDoERunner,
)

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

OMCProcessLocal,
OMCProcessPort,
Expand All @@ -80,15 +81,14 @@

'LinearizationResult',

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

'ModelicaDoEABC',
'ModelicaDoEOMC',
'ModelicaDoERunner',
'ModelicaSystemABC',
'ModelicaSystemDoE',
'ModelicaSystemError',
'ModelicaSystemOMC',
'ModelicaSystemRunner',
Expand All @@ -109,10 +109,10 @@
'OMPathRunnerLocal',
'OMSessionRunner',

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

'OMCSessionABC',
'OMCSessionCmd',

'OMCSessionException',
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
13 changes: 7 additions & 6 deletions OMPython/modelica_doe_abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
from typing import Any, cast, Optional, Tuple

from OMPython.model_execution import (
ModelExecutionData,
ModelExecutionRun,
ModelExecutionException,
)
from OMPython.om_session_abc import (
OMPathABC,
Expand Down Expand Up @@ -138,7 +139,7 @@ def __init__(
self._parameters = {}

self._doe_def: Optional[dict[str, dict[str, Any]]] = None
self._doe_cmd: Optional[dict[str, ModelExecutionData]] = None
self._doe_cmd: Optional[dict[str, ModelExecutionRun]] = None

def get_session(self) -> OMSessionABC:
"""
Expand Down Expand Up @@ -209,7 +210,7 @@ def prepare(self) -> int:
}
)

self._mod.setParameters(sim_param_non_structural)
self._mod.setParameters(**sim_param_non_structural)
mscmd = self._mod.simulate_cmd(
result_file=resultfile,
)
Expand Down Expand Up @@ -255,7 +256,7 @@ def get_doe_definition(self) -> Optional[dict[str, dict[str, Any]]]:
"""
return self._doe_def

def get_doe_command(self) -> Optional[dict[str, ModelExecutionData]]:
def get_doe_command(self) -> Optional[dict[str, ModelExecutionRun]]:
"""
Get the definitions of simulations commands to run for this DoE.
"""
Expand Down Expand Up @@ -310,8 +311,8 @@ def worker(worker_id, task_queue):
returncode = cmd_definition.run()
logger.info(f"[Worker {worker_id}] Simulation {resultpath.name} "
f"finished with return code: {returncode}")
except ModelicaSystemError as ex:
logger.warning(f"Simulation error for {resultpath.name}: {ex}")
except ModelExecutionException as exc:
logger.warning(f"Simulation error for {resultpath.name}: {exc}")

# Mark the task as done
task_queue.task_done()
Expand Down
Loading
Loading