Skip to content

Commit 6c44d13

Browse files
committed
[ModelicaSystem] split __init__()
new: * __init__() - initialisation * model_definition() - model related definitions
1 parent bfeb673 commit 6c44d13

1 file changed

Lines changed: 64 additions & 51 deletions

File tree

OMPython/ModelicaSystem.py

Lines changed: 64 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -349,60 +349,28 @@ def parse_simflags(simflags: str) -> dict[str, Optional[str | dict[str, Any] | n
349349
class ModelicaSystem:
350350
def __init__(
351351
self,
352-
fileName: Optional[str | os.PathLike | pathlib.Path] = None,
353-
modelName: Optional[str] = None,
354-
lmodel: Optional[list[str | tuple[str, str]]] = None,
355352
commandLineOptions: Optional[list[str]] = None,
356-
variableFilter: Optional[str] = None,
357353
customBuildDirectory: Optional[str | os.PathLike] = None,
358354
omhome: Optional[str] = None,
359355
omc_process: Optional[OMCProcessLocal] = None,
360-
build: bool = True,
361356
) -> None:
362357
"""Initialize, load and build a model.
363358
364359
The constructor loads the model file and builds it, generating exe and
365360
xml files, etc.
366361
367362
Args:
368-
fileName: Path to the model file. Either absolute or relative to
369-
the current working directory.
370-
modelName: The name of the model class. If it is contained within
371-
a package, "PackageName.ModelName" should be used.
372-
lmodel: List of libraries to be loaded before the model itself is
373-
loaded. Two formats are supported for the list elements:
374-
lmodel=["Modelica"] for just the library name
375-
and lmodel=[("Modelica","3.2.3")] for specifying both the name
376-
and the version.
377363
commandLineOptions: List with extra command line options as elements. The list elements are
378364
provided to omc via setCommandLineOptions(). If set, the default values will be overridden.
379365
To disable any command line options, use an empty list.
380-
variableFilter: A regular expression. Only variables fully
381-
matching the regexp will be stored in the result file.
382-
Leaving it unspecified is equivalent to ".*".
383366
customBuildDirectory: Path to a directory to be used for temporary
384367
files like the model executable. If left unspecified, a tmp
385368
directory will be created.
386-
omhome: OPENMODELICAHOME value to be used when creating the OMC
387-
session.
369+
omhome: path to OMC to be used when creating the OMC session (see OMCSessionZMQ).
388370
omc_process: definition of a (local) OMC process to be used. If
389371
unspecified, a new local session will be created.
390-
build: Boolean controlling whether or not the model should be
391-
built when constructor is called. If False, the constructor
392-
simply loads the model without compiling.
393-
394-
Examples:
395-
mod = ModelicaSystem("ModelicaModel.mo", "modelName")
396-
mod = ModelicaSystem("ModelicaModel.mo", "modelName", ["Modelica"])
397-
mod = ModelicaSystem("ModelicaModel.mo", "modelName", [("Modelica","3.2.3"), "PowerSystems"])
398372
"""
399373

400-
if fileName is None and modelName is None and not lmodel: # all None
401-
raise ModelicaSystemError("Cannot create ModelicaSystem object without any arguments")
402-
403-
if modelName is None:
404-
raise ModelicaSystemError("A modelname must be provided (argument modelName)!")
405-
406374
self._quantities: list[dict[str, Any]] = []
407375
self._params: dict[str, str] = {} # even numerical values are stored as str
408376
self._inputs: dict[str, list | None] = {}
@@ -438,34 +406,79 @@ def __init__(
438406
for opt in commandLineOptions:
439407
self.setCommandLineOptions(commandLineOptions=opt)
440408

441-
if lmodel is None:
442-
lmodel = []
443-
444-
if not isinstance(lmodel, list):
445-
raise ModelicaSystemError(f"Invalid input type for lmodel: {type(lmodel)} - list expected!")
446-
447-
self._lmodel = lmodel # may be needed if model is derived from other model
448-
self._model_name = modelName # Model class name
449-
self._file_name = pathlib.Path(fileName).resolve() if fileName is not None else None # Model file/package name
450409
self._simulated = False # True if the model has already been simulated
451410
self._result_file: Optional[pathlib.Path] = None # for storing result file
452-
self._variable_filter = variableFilter
411+
412+
self._work_dir: pathlib.Path = self.setWorkDirectory(customBuildDirectory)
413+
414+
self._model_name: Optional[str] = None
415+
self._lmodel: Optional[list[str | tuple[str, str]]] = None
416+
self._file_name: Optional[os.PathLike]
417+
self._variable_filter: Optional[str] = None
418+
419+
def model_definition(
420+
self,
421+
model: str,
422+
file: Optional[str | os.PathLike | pathlib.Path] = None,
423+
libraries: Optional[list[str | tuple[str, str]]] = None,
424+
variable_filter: Optional[str] = None,
425+
build: bool = True,
426+
) -> None:
427+
"""Initialize, load and build a model.
428+
429+
The constructor loads the model file and builds it, generating exe and
430+
xml files, etc.
431+
432+
Args:
433+
file: Path to the model file. Either absolute or relative to
434+
the current working directory.
435+
model: The name of the model class. If it is contained within
436+
a package, "PackageName.ModelName" should be used.
437+
libraries: List of libraries to be loaded before the model itself is
438+
loaded. Two formats are supported for the list elements:
439+
lmodel=["Modelica"] for just the library name
440+
and lmodel=[("Modelica","3.2.3")] for specifying both the name
441+
and the version.
442+
variable_filter: A regular expression. Only variables fully
443+
matching the regexp will be stored in the result file.
444+
Leaving it unspecified is equivalent to ".*".
445+
build: Boolean controlling whether the model should be
446+
built when constructor is called. If False, the constructor
447+
simply loads the model without compiling.
448+
449+
Examples:
450+
mod = ModelicaSystem()
451+
# and then one of the lines below
452+
mod.setup_model(model="modelName", file="ModelicaModel.mo", )
453+
mod.setup_model(model="modelName", file="ModelicaModel.mo", libraries=["Modelica"])
454+
mod.setup_model(model="modelName", file="ModelicaModel.mo", libraries=[("Modelica","3.2.3"), "PowerSystems"])
455+
"""
456+
457+
if not isinstance(model, str):
458+
raise ModelicaSystemError("A model name must be provided (argument modelName)!")
459+
460+
if libraries is None:
461+
libraries = []
462+
463+
if not isinstance(libraries, list):
464+
raise ModelicaSystemError(f"Invalid input type for lmodel: {type(libraries)} - list expected!")
465+
466+
# set variables
467+
self._model_name = model # Model class name
468+
self._lmodel = libraries # may be needed if model is derived from other model
469+
self._file_name = pathlib.Path(file).resolve() if file is not None else None # Model file/package name
470+
self._variable_filter = variable_filter
453471

454472
if self._file_name is not None and not self._file_name.is_file(): # if file does not exist
455473
raise IOError(f"{self._file_name} does not exist!")
456474

457-
self._work_dir: pathlib.Path = self.setWorkDirectory(customBuildDirectory)
458-
459-
if self._file_name is not None:
475+
if self._lmodel:
460476
self._loadLibrary(lmodel=self._lmodel)
477+
if self._file_name is not None:
461478
self._loadFile(fileName=self._file_name)
462479

463-
# allow directly loading models from MSL without fileName
464-
elif fileName is None and modelName is not None:
465-
self._loadLibrary(lmodel=self._lmodel)
466-
467480
if build:
468-
self.buildModel(variableFilter)
481+
self.buildModel(variable_filter)
469482

470483
def setCommandLineOptions(self, commandLineOptions: str):
471484
"""

0 commit comments

Comments
 (0)