Skip to content

Commit e3984e7

Browse files
committed
(D008) add v4.0.0 compatibility layer
[OMTypedParser] compatibility layer [__init__/OMCSession] prepare compatibility layer [ModelicaSystem] define as compatibility layer [ModelicaSystemCmd] define as compatibility layer
1 parent 97c3e4b commit e3984e7

File tree

5 files changed

+204
-1
lines changed

5 files changed

+204
-1
lines changed

OMPython/ModelicaSystem.py

Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2116,6 +2116,138 @@ class ModelicaSystem(ModelicaSystemOMC):
21162116
Compatibility class.
21172117
"""
21182118

2119+
def __init__(
2120+
self,
2121+
fileName: Optional[str | os.PathLike | pathlib.Path] = None,
2122+
modelName: Optional[str] = None,
2123+
lmodel: Optional[list[str | tuple[str, str]]] = None,
2124+
commandLineOptions: Optional[list[str]] = None,
2125+
variableFilter: Optional[str] = None,
2126+
customBuildDirectory: Optional[str | os.PathLike] = None,
2127+
omhome: Optional[str] = None,
2128+
omc_process: Optional[OMCSessionLocal] = None,
2129+
build: bool = True,
2130+
) -> None:
2131+
super().__init__(
2132+
command_line_options=commandLineOptions,
2133+
work_directory=customBuildDirectory,
2134+
omhome=omhome,
2135+
session=omc_process,
2136+
)
2137+
self.model(
2138+
model_name=modelName,
2139+
model_file=fileName,
2140+
libraries=lmodel,
2141+
variable_filter=variableFilter,
2142+
build=build,
2143+
)
2144+
self._getconn = self._session
2145+
2146+
def setCommandLineOptions(self, commandLineOptions: str):
2147+
super().set_command_line_options(command_line_option=commandLineOptions)
2148+
2149+
def setContinuous( # type: ignore[override]
2150+
self,
2151+
cvals: str | list[str] | dict[str, Any],
2152+
) -> bool:
2153+
if isinstance(cvals, dict):
2154+
return super().setContinuous(**cvals)
2155+
raise ModelicaSystemError("Only dict input supported for setContinuous()")
2156+
2157+
def setParameters( # type: ignore[override]
2158+
self,
2159+
pvals: str | list[str] | dict[str, Any],
2160+
) -> bool:
2161+
if isinstance(pvals, dict):
2162+
return super().setParameters(**pvals)
2163+
raise ModelicaSystemError("Only dict input supported for setParameters()")
2164+
2165+
def setOptimizationOptions( # type: ignore[override]
2166+
self,
2167+
optimizationOptions: str | list[str] | dict[str, Any],
2168+
) -> bool:
2169+
if isinstance(optimizationOptions, dict):
2170+
return super().setOptimizationOptions(**optimizationOptions)
2171+
raise ModelicaSystemError("Only dict input supported for setOptimizationOptions()")
2172+
2173+
def setInputs( # type: ignore[override]
2174+
self,
2175+
name: str | list[str] | dict[str, Any],
2176+
) -> bool:
2177+
if isinstance(name, dict):
2178+
return super().setInputs(**name)
2179+
raise ModelicaSystemError("Only dict input supported for setInputs()")
2180+
2181+
def setSimulationOptions( # type: ignore[override]
2182+
self,
2183+
simOptions: str | list[str] | dict[str, Any],
2184+
) -> bool:
2185+
if isinstance(simOptions, dict):
2186+
return super().setSimulationOptions(**simOptions)
2187+
raise ModelicaSystemError("Only dict input supported for setSimulationOptions()")
2188+
2189+
def setLinearizationOptions( # type: ignore[override]
2190+
self,
2191+
linearizationOptions: str | list[str] | dict[str, Any],
2192+
) -> bool:
2193+
if isinstance(linearizationOptions, dict):
2194+
return super().setLinearizationOptions(**linearizationOptions)
2195+
raise ModelicaSystemError("Only dict input supported for setLinearizationOptions()")
2196+
2197+
def getContinuous(
2198+
self,
2199+
names: Optional[str | list[str]] = None,
2200+
):
2201+
retval = super().getContinuous(names=names)
2202+
if self._simulated:
2203+
return retval
2204+
2205+
if isinstance(retval, dict):
2206+
retval2: dict = {}
2207+
for key, val in retval.items():
2208+
if np.isnan(val):
2209+
retval2[key] = None
2210+
else:
2211+
retval2[key] = str(val)
2212+
return retval2
2213+
if isinstance(retval, list):
2214+
retval3: list[str | None] = []
2215+
for val in retval:
2216+
if np.isnan(val):
2217+
retval3.append(None)
2218+
else:
2219+
retval3.append(str(val))
2220+
return retval3
2221+
2222+
raise ModelExecutionException("Invalid data!")
2223+
2224+
def getOutputs(
2225+
self,
2226+
names: Optional[str | list[str]] = None,
2227+
):
2228+
retval = super().getOutputs(names=names)
2229+
if self._simulated:
2230+
return retval
2231+
2232+
if isinstance(retval, dict):
2233+
retval2: dict = {}
2234+
for key, val in retval.items():
2235+
if np.isnan(val):
2236+
retval2[key] = None
2237+
else:
2238+
retval2[key] = str(val)
2239+
return retval2
2240+
if isinstance(retval, list):
2241+
retval3: list[str | None] = []
2242+
for val in retval:
2243+
if np.isnan(val):
2244+
retval3.append(None)
2245+
else:
2246+
retval3.append(str(val))
2247+
return retval3
2248+
2249+
raise ModelExecutionException("Invalid data!")
2250+
21192251

21202252
class ModelicaDoEABC(metaclass=abc.ABCMeta):
21212253
"""
@@ -2687,3 +2819,50 @@ def _prepare_structure_parameters(
26872819
"pre-compiled binary of model.")
26882820

26892821
return {}
2822+
2823+
2824+
class ModelicaSystemCmd(ModelExecutionCmd):
2825+
# TODO: docstring
2826+
2827+
def __init__(
2828+
self,
2829+
runpath: pathlib.Path,
2830+
modelname: str,
2831+
timeout: float = 10.0,
2832+
) -> None:
2833+
super().__init__(
2834+
runpath=runpath,
2835+
timeout=timeout,
2836+
cmd_prefix=[],
2837+
model_name=modelname,
2838+
)
2839+
2840+
def get_exe(self) -> pathlib.Path:
2841+
"""Get the path to the compiled model executable."""
2842+
# TODO: move to the top
2843+
import platform
2844+
2845+
path_run = pathlib.Path(self._runpath)
2846+
if platform.system() == "Windows":
2847+
path_exe = path_run / f"{self._model_name}.exe"
2848+
else:
2849+
path_exe = path_run / self._model_name
2850+
2851+
if not path_exe.exists():
2852+
raise ModelicaSystemError(f"Application file path not found: {path_exe}")
2853+
2854+
return path_exe
2855+
2856+
def get_cmd(self) -> list:
2857+
"""Get a list with the path to the executable and all command line args.
2858+
2859+
This can later be used as an argument for subprocess.run().
2860+
"""
2861+
2862+
cmdl = [self.get_exe().as_posix()] + self.get_cmd_args()
2863+
2864+
return cmdl
2865+
2866+
def run(self):
2867+
cmd_definition = self.definition()
2868+
return cmd_definition.run()

OMPython/OMCSession.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2155,3 +2155,11 @@ def omcpath_tempdir(self, tempdir_base: Optional[OMPathABC] = None) -> OMPathABC
21552155

21562156
def sendExpression(self, expr: str, parsed: bool = True) -> Any:
21572157
raise OMCSessionException(f"{self.__class__.__name__} does not uses an OMC server!")
2158+
2159+
2160+
DummyPopen = DockerPopen
2161+
OMCProcessLocal = OMCSessionLocal
2162+
OMCProcessPort = OMCSessionPort
2163+
OMCProcessDocker = OMCSessionDocker
2164+
OMCProcessDockerContainer = OMCSessionDockerContainer
2165+
OMCProcessWSL = OMCSessionWSL

OMPython/OMTypedParser.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,3 +161,6 @@ def om_parser_typed(string) -> Any:
161161
if len(res) == 0:
162162
return None
163163
return res[0]
164+
165+
166+
parseString = om_parser_typed

OMPython/__init__.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
ModelicaDoERunner,
2424

2525
doe_get_solutions,
26+
27+
ModelicaSystemCmd,
2628
)
2729
from OMPython.OMCSession import (
2830
OMPathABC,
@@ -47,6 +49,11 @@
4749

4850
OMCSessionWSL,
4951
OMCSessionZMQ,
52+
53+
OMCProcessLocal,
54+
OMCProcessPort,
55+
OMCProcessDocker,
56+
OMCProcessDockerContainer,
5057
)
5158

5259
# global names imported if import 'from OMPython import *' is used
@@ -58,6 +65,7 @@
5865

5966
'ModelicaSystem',
6067
'ModelicaSystemOMC',
68+
'ModelicaSystemCmd',
6169
'ModelExecutionCmd',
6270
'ModelicaSystemDoE',
6371
'ModelicaDoEOMC',
@@ -87,4 +95,9 @@
8795

8896
'OMCSessionWSL',
8997
'OMCSessionZMQ',
98+
99+
'OMCProcessLocal',
100+
'OMCProcessPort',
101+
'OMCProcessDocker',
102+
'OMCProcessDockerContainer',
90103
]

tests/test_ModelicaSystemRunner.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def param():
3939

4040
def test_runner(model_firstorder, param):
4141
# create a model using ModelicaSystem
42-
mod = OMPython.ModelicaSystem()
42+
mod = OMPython.ModelicaSystemOMC()
4343
mod.model(
4444
model_file=model_firstorder,
4545
model_name="M",

0 commit comments

Comments
 (0)