-
Notifications
You must be signed in to change notification settings - Fork 69
Expand file tree
/
Copy pathmodel_execution.py
More file actions
358 lines (296 loc) · 13.4 KB
/
Copy pathmodel_execution.py
File metadata and controls
358 lines (296 loc) · 13.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
# -*- coding: utf-8 -*-
"""
Definition of needed tools to execute a compiled (binary) OpenModelica model.
"""
import ast
import dataclasses
import logging
import numbers
import os
import pathlib
import re
import subprocess
from typing import Any, Optional
import warnings
# define logger using the current module name as ID
logger = logging.getLogger(__name__)
MODEL_EXECUTION_TIMEOUT: float = 300.0
class ModelExecutionException(Exception):
"""
Exception which is raised by ModelException* classes.
"""
@dataclasses.dataclass
class ModelExecutionData:
"""
Data class to store the command line data for running a model executable in the OMC environment.
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.
"""
# cmd_path is the expected working directory
cmd_path: str
cmd_model_name: str
# command prefix data (as list of strings); needed for docker or WSL
cmd_prefix: list[str]
# cmd_model_executable is build out of cmd_path and cmd_model_name; this is mainly needed on Windows (add *.exe)
cmd_model_executable: str
# command line arguments for the model executable
cmd_args: list[str]
# result file with the simulation output
cmd_result_file: str
# command timeout
cmd_timeout: float
# additional library search path; this is mainly needed if OMCProcessLocal is run on Windows
cmd_library_path: Optional[str] = None
# working directory to be used on the *local* system
cmd_cwd_local: Optional[str] = None
def get_cmd(self) -> list[str]:
"""
Get the command line to run the model executable in the environment defined by the OMCProcess definition.
"""
cmdl = self.cmd_prefix
cmdl += [self.cmd_model_executable]
cmdl += self.cmd_args
return cmdl
def run(self) -> int:
"""
Run the model execution defined in this class.
"""
my_env = os.environ.copy()
if isinstance(self.cmd_library_path, str):
my_env["PATH"] = self.cmd_library_path + os.pathsep + my_env["PATH"]
cmdl = self.get_cmd()
logger.debug("Run OM command %s in %s (timeout=%2fs)", repr(cmdl), self.cmd_path, self.cmd_timeout)
try:
cmdres = subprocess.run(
cmdl,
capture_output=True,
text=True,
env=my_env,
cwd=self.cmd_cwd_local,
timeout=self.cmd_timeout,
check=True,
)
stdout = cmdres.stdout.strip()
stderr = cmdres.stderr.strip()
returncode = cmdres.returncode
logger.debug("OM output for command %s:\n%s", repr(cmdl), stdout)
if stderr:
raise ModelExecutionException(f"Error running model executable {repr(cmdl)}: {stderr}")
except subprocess.TimeoutExpired as ex:
raise ModelExecutionException("OMPython timeout running model executable "
f"(timeout={self.cmd_timeout:.2f}s){repr(cmdl)}: {ex}") from ex
except subprocess.CalledProcessError as ex:
raise ModelExecutionException(f"Error running model executable {repr(cmdl)}: {ex}") from ex
return returncode
class ModelExecutionCmd:
"""
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.
"""
def __init__(
self,
runpath: os.PathLike,
cmd_prefix: list[str],
cmd_local: bool = False,
cmd_windows: bool = False,
timeout: Optional[float] = None,
model_name: Optional[str] = None,
) -> None:
if model_name is None:
raise ModelExecutionException("Missing model name!")
self._cmd_local = cmd_local
self._cmd_windows = cmd_windows
self._cmd_prefix = cmd_prefix
self._runpath = pathlib.PurePosixPath(runpath)
self._model_name = model_name
if timeout is None:
# a separate timeout is defined here to allow the use of the class independent of the normal call chain via
# classes derived from OMSession (OMSESSION_TIMEOUT)
self._timeout = MODEL_EXECUTION_TIMEOUT
else:
self._timeout = timeout
# dictionaries of command line arguments for the model executable
self._args: dict[str, str | None] = {}
# 'override' argument needs special handling, as it is a dict on its own saved as dict elements following the
# structure: 'key' => 'key=value'
self._arg_override: dict[str, str] = {}
def arg_set(
self,
key: str,
val: Optional[str | dict[str, Any] | numbers.Number] = None,
) -> None:
"""
Set one argument for the executable model.
Args:
key: identifier / argument name to be used for the call of the model executable.
val: value for the given key; None for no value and for key == 'override' a dictionary can be used which
indicates variables to override
"""
def override2str(
orkey: str,
orval: str | bool | numbers.Number,
) -> str:
"""
Convert a value for 'override' to a string taking into account differences between Modelica and Python.
"""
# check oval for any string representations of numbers (or bool) and convert these to Python representations
if isinstance(orval, str):
try:
val_evaluated = ast.literal_eval(orval)
if isinstance(val_evaluated, (numbers.Number, bool)):
orval = val_evaluated
except (ValueError, SyntaxError):
pass
if isinstance(orval, str):
val_str = orval.strip()
elif isinstance(orval, bool):
val_str = 'true' if orval else 'false'
elif isinstance(orval, numbers.Number):
val_str = str(orval)
else:
raise ModelExecutionException(f"Invalid value for override key {orkey}: {type(orval)}")
return f"{orkey}={val_str}"
if not isinstance(key, str):
raise ModelExecutionException(f"Invalid argument key: {repr(key)} (type: {type(key)})")
key = key.strip()
if isinstance(val, dict):
if key != 'override':
raise ModelExecutionException("Dictionary input only possible for key 'override'!")
for okey, oval in val.items():
if not isinstance(okey, str):
raise ModelExecutionException("Invalid key for argument 'override': "
f"{repr(okey)} (type: {type(okey)})")
if not isinstance(oval, (str, bool, numbers.Number, type(None))):
raise ModelExecutionException(f"Invalid input for 'override'.{repr(okey)}: "
f"{repr(oval)} (type: {type(oval)})")
if okey in self._arg_override:
if oval is None:
logger.info(f"Remove model executable override argument: {repr(self._arg_override[okey])}")
del self._arg_override[okey]
continue
logger.info(f"Update model executable override argument: {repr(okey)} = {repr(oval)} "
f"(was: {repr(self._arg_override[okey])})")
if oval is not None:
self._arg_override[okey] = override2str(orkey=okey, orval=oval)
argval = ','.join(sorted(self._arg_override.values()))
elif val is None:
argval = None
elif isinstance(val, str):
argval = val.strip()
elif isinstance(val, numbers.Number):
argval = str(val)
else:
raise ModelExecutionException(f"Invalid argument value for {repr(key)}: {repr(val)} (type: {type(val)})")
if key in self._args:
logger.warning(f"Override model executable argument: {repr(key)} = {repr(argval)} "
f"(was: {repr(self._args[key])})")
self._args[key] = argval
def arg_get(self, key: str) -> Optional[str | dict[str, str | bool | numbers.Number]]:
"""
Return the value for the given key
"""
if key in self._args:
return self._args[key]
return None
def args_set(
self,
args: dict[str, Optional[str | dict[str, Any] | numbers.Number]],
) -> None:
"""
Define arguments for the model executable.
"""
for arg in args:
self.arg_set(key=arg, val=args[arg])
def get_cmd_args(self) -> list[str]:
"""
Get a list with the command arguments for the model executable.
"""
cmdl = []
for key in sorted(self._args):
if self._args[key] is None:
cmdl.append(f"-{key}")
else:
cmdl.append(f"-{key}={self._args[key]}")
return cmdl
def definition(self) -> ModelExecutionData:
"""
Define all needed data to run the model executable. The data is stored in an OMCSessionRunData object.
"""
# ensure that a result filename is provided
result_file = self.arg_get('r')
if not isinstance(result_file, str):
result_file = (self._runpath / f"{self._model_name}.mat").as_posix()
# as this is the local implementation, pathlib.Path can be used
cmd_path = self._runpath
cmd_library_path = None
if self._cmd_local and self._cmd_windows:
cmd_library_path = ""
# set the process environment from the generated .bat file in windows which should have all the dependencies
# for this pathlib.PurePosixPath() must be converted to a pathlib.Path() object, i.e. WindowsPath
path_bat = pathlib.Path(cmd_path) / f"{self._model_name}.bat"
if not path_bat.is_file():
raise ModelExecutionException("Batch file (*.bat) does not exist " + str(path_bat))
content = path_bat.read_text(encoding='utf-8')
for line in content.splitlines():
match = re.match(pattern=r"^SET PATH=([^%]*)", string=line, flags=re.IGNORECASE)
if match:
cmd_library_path = match.group(1).strip(';') # Remove any trailing semicolons
my_env = os.environ.copy()
my_env["PATH"] = cmd_library_path + os.pathsep + my_env["PATH"]
cmd_model_executable = cmd_path / f"{self._model_name}.exe"
else:
# for Linux the paths to the needed libraries should be included in the executable (using rpath)
cmd_model_executable = cmd_path / self._model_name
# define local(!) working directory
cmd_cwd_local = None
if self._cmd_local:
cmd_cwd_local = cmd_path.as_posix()
omc_run_data = ModelExecutionData(
cmd_path=cmd_path.as_posix(),
cmd_model_name=self._model_name,
cmd_args=self.get_cmd_args(),
cmd_result_file=result_file,
cmd_prefix=self._cmd_prefix,
cmd_library_path=cmd_library_path,
cmd_model_executable=cmd_model_executable.as_posix(),
cmd_cwd_local=cmd_cwd_local,
cmd_timeout=self._timeout,
)
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