Skip to content

Commit b477a94

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 5b670ae commit b477a94

5 files changed

Lines changed: 204 additions & 1 deletion

File tree

OMPython/ModelicaSystem.py

Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2120,6 +2120,138 @@ class ModelicaSystem(ModelicaSystemOMC):
21202120
Compatibility class.
21212121
"""
21222122

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

21242256
class ModelicaDoEABC(metaclass=abc.ABCMeta):
21252257
"""
@@ -2691,3 +2823,50 @@ def _prepare_structure_parameters(
26912823
"pre-compiled binary of model.")
26922824

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

OMPython/OMCSession.py

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

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