33Definition of main class to run Modelica simulations - ModelicaSystem.
44"""
55
6- from __future__ import annotations
7-
86import logging
97import numbers
108import os
119import pathlib
12- import platform
1310from typing import Any , Optional
1411import warnings
1512
@@ -94,7 +91,7 @@ def simulate_cmd( # type: ignore[override]
9491 simargs = {}
9592
9693 if simflags is not None :
97- simargs_extra = ModelicaSystemCmd . parse_simflags (simflags = simflags )
94+ simargs_extra = parse_simflags (simflags = simflags )
9895 simargs = simargs | simargs_extra
9996
10097 return super ().simulate_cmd (
@@ -116,7 +113,7 @@ def simulate( # type: ignore[override]
116113 simargs = {}
117114
118115 if simflags is not None :
119- simargs_extra = ModelicaSystemCmd . parse_simflags (simflags = simflags )
116+ simargs_extra = parse_simflags (simflags = simflags )
120117 simargs = simargs | simargs_extra
121118
122119 return super ().simulate (
@@ -137,7 +134,7 @@ def linearize( # type: ignore[override]
137134 simargs = {}
138135
139136 if simflags is not None :
140- simargs_extra = ModelicaSystemCmd . parse_simflags (simflags = simflags )
137+ simargs_extra = parse_simflags (simflags = simflags )
141138 simargs = simargs | simargs_extra
142139
143140 return super ().linearize (
@@ -372,95 +369,52 @@ class ModelicaSystemDoE(ModelicaDoEOMC):
372369@depreciated_class (msg = "Please use class ModelExecutionCmd instead!" )
373370class ModelicaSystemCmd (ModelExecutionCmd ):
374371 """
375- Compatibility class; in the new version it is renamed as ModelExecutionCmd .
372+ Compatibility class; not much content .
376373
377- This class is only defined for the unit tests - it is NOT used within ModelicaSystem of v4.0.0!
374+ Missing definitions:
375+ * get_exe() - see self.definition.cmd_model_executable
376+ * get_cmd() - use self.get_cmd_args() or self.definition().get_cmd()
377+ * run() - use self.definition().run()
378378 """
379379
380- def __init__ (
381- self ,
382- runpath : pathlib .Path ,
383- modelname : str ,
384- timeout : float = 10.0 ,
385- ) -> None :
386- super ().__init__ (
387- runpath = runpath ,
388- timeout = timeout ,
389- cmd_prefix = [],
390- model_name = modelname ,
391- )
392-
393- def get_exe (self ) -> pathlib .Path :
394- """Get the path to the compiled model executable."""
395-
396- path_run = pathlib .Path (self ._runpath )
397- if platform .system () == "Windows" :
398- path_exe = path_run / f"{ self ._model_name } .exe"
399- else :
400- path_exe = path_run / self ._model_name
401-
402- if not path_exe .exists ():
403- raise ModelicaSystemError (f"Application file path not found: { path_exe } " )
404-
405- return path_exe
406-
407- def get_cmd (self ) -> list :
408- """
409- Get a list with the path to the executable and all command line args.
410-
411- This can later be used as an argument for subprocess.run().
412- """
413-
414- cmdl = [self .get_exe ().as_posix ()] + self .get_cmd_args ()
415-
416- return cmdl
417-
418- def run (self ) -> int :
419- cmd_definition = self .definition ()
420- try :
421- returncode = cmd_definition .run ()
422- except ModelExecutionException as exc :
423- raise ModelicaSystemError (f"Cannot execute model: { exc } " ) from exc
424- return returncode
425-
426- @staticmethod
427- def parse_simflags (simflags : str ) -> dict [str , Optional [str | dict [str , Any ] | numbers .Number ]]:
428- """
429- Parse a simflag definition; this is deprecated!
430380
431- The return data can be used as input for self.args_set().
432- """
433- warnings .warn (
434- message = "The argument 'simflags' is depreciated and will be removed in future versions; "
435- "please use 'simargs' instead" ,
436- category = DeprecationWarning ,
437- stacklevel = 2 ,
438- )
381+ def parse_simflags (simflags : str ) -> dict [str , Optional [str | dict [str , Any ] | numbers .Number ]]:
382+ """
383+ Parse a simflag definition; this is deprecated!
439384
440- simargs : dict [str , Optional [str | dict [str , Any ] | numbers .Number ]] = {}
441-
442- args = [s for s in simflags .split (' ' ) if s ]
443- for arg in args :
444- if arg [0 ] != '-' :
445- raise ModelExecutionException (f"Invalid simulation flag: { arg } " )
446- arg = arg [1 :]
447- parts = arg .split ('=' )
448- if len (parts ) == 1 :
449- simargs [parts [0 ]] = None
450- elif parts [0 ] == 'override' :
451- override = '=' .join (parts [1 :])
452-
453- override_dict = {}
454- for item in override .split (',' ):
455- kv = item .split ('=' )
456- if not 0 < len (kv ) < 3 :
457- raise ModelExecutionException (f"Invalid value for '-override': { override } " )
458- if kv [0 ]:
459- try :
460- override_dict [kv [0 ]] = kv [1 ]
461- except (KeyError , IndexError ) as ex :
462- raise ModelExecutionException (f"Invalid value for '-override': { override } " ) from ex
463-
464- simargs [parts [0 ]] = override_dict
465-
466- return simargs
385+ The return data can be used as input for self.args_set().
386+ """
387+ warnings .warn (
388+ message = "The argument 'simflags' is depreciated and will be removed in future versions; "
389+ "please use 'simargs' instead" ,
390+ category = DeprecationWarning ,
391+ stacklevel = 2 ,
392+ )
393+
394+ simargs : dict [str , Optional [str | dict [str , Any ] | numbers .Number ]] = {}
395+
396+ args = [s for s in simflags .split (' ' ) if s ]
397+ for arg in args :
398+ if arg [0 ] != '-' :
399+ raise ModelExecutionException (f"Invalid simulation flag: { arg } " )
400+ arg = arg [1 :]
401+ parts = arg .split ('=' )
402+ if len (parts ) == 1 :
403+ simargs [parts [0 ]] = None
404+ elif parts [0 ] == 'override' :
405+ override = '=' .join (parts [1 :])
406+
407+ override_dict = {}
408+ for item in override .split (',' ):
409+ kv = item .split ('=' )
410+ if not 0 < len (kv ) < 3 :
411+ raise ModelExecutionException (f"Invalid value for '-override': { override } " )
412+ if kv [0 ]:
413+ try :
414+ override_dict [kv [0 ]] = kv [1 ]
415+ except (KeyError , IndexError ) as ex :
416+ raise ModelExecutionException (f"Invalid value for '-override': { override } " ) from ex
417+
418+ simargs [parts [0 ]] = override_dict
419+
420+ return simargs
0 commit comments