Skip to content

Commit af45662

Browse files
committed
[ModelicaSystem] split __init__()
new: * __init__() - initialisation * model_definition() - model related definitions
1 parent 7620bcc commit af45662

1 file changed

Lines changed: 66 additions & 59 deletions

File tree

OMPython/ModelicaSystem.py

Lines changed: 66 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -347,60 +347,28 @@ def parse_simflags(simflags: str) -> dict[str, Optional[str | dict[str, Any] | n
347347
class ModelicaSystem:
348348
def __init__(
349349
self,
350-
fileName: Optional[str | os.PathLike] = None,
351-
modelName: Optional[str] = None,
352-
lmodel: Optional[list[str | tuple[str, str]]] = None,
353350
commandLineOptions: Optional[list[str]] = None,
354-
variableFilter: Optional[str] = None,
355351
customBuildDirectory: Optional[str | os.PathLike] = None,
356352
omhome: Optional[str] = None,
357353
omc_process: Optional[OMCProcessLocal] = None,
358-
build: bool = True,
359354
) -> None:
360355
"""Initialize, load and build a model.
361356
362357
The constructor loads the model file and builds it, generating exe and
363358
xml files, etc.
364359
365360
Args:
366-
fileName: Path to the model file. Either absolute or relative to
367-
the current working directory.
368-
modelName: The name of the model class. If it is contained within
369-
a package, "PackageName.ModelName" should be used.
370-
lmodel: List of libraries to be loaded before the model itself is
371-
loaded. Two formats are supported for the list elements:
372-
lmodel=["Modelica"] for just the library name
373-
and lmodel=[("Modelica","3.2.3")] for specifying both the name
374-
and the version.
375361
commandLineOptions: List with extra command line options as elements. The list elements are
376362
provided to omc via setCommandLineOptions(). If set, the default values will be overridden.
377363
To disable any command line options, use an empty list.
378-
variableFilter: A regular expression. Only variables fully
379-
matching the regexp will be stored in the result file.
380-
Leaving it unspecified is equivalent to ".*".
381364
customBuildDirectory: Path to a directory to be used for temporary
382365
files like the model executable. If left unspecified, a tmp
383366
directory will be created.
384-
omhome: OPENMODELICAHOME value to be used when creating the OMC
385-
session.
367+
omhome: path to OMC to be used when creating the OMC session (see OMCSessionZMQ).
386368
omc_process: definition of a (local) OMC process to be used. If
387369
unspecified, a new local session will be created.
388-
build: Boolean controlling whether or not the model should be
389-
built when constructor is called. If False, the constructor
390-
simply loads the model without compiling.
391-
392-
Examples:
393-
mod = ModelicaSystem("ModelicaModel.mo", "modelName")
394-
mod = ModelicaSystem("ModelicaModel.mo", "modelName", ["Modelica"])
395-
mod = ModelicaSystem("ModelicaModel.mo", "modelName", [("Modelica","3.2.3"), "PowerSystems"])
396370
"""
397371

398-
if fileName is None and modelName is None and not lmodel: # all None
399-
raise ModelicaSystemError("Cannot create ModelicaSystem object without any arguments")
400-
401-
if modelName is None:
402-
raise ModelicaSystemError("A modelname must be provided (argument modelName)!")
403-
404372
self._quantities: list[dict[str, Any]] = []
405373
self._params: dict[str, str] = {} # even numerical values are stored as str
406374
self._inputs: dict[str, list | None] = {}
@@ -436,44 +404,83 @@ def __init__(
436404
for opt in commandLineOptions:
437405
self.setCommandLineOptions(commandLineOptions=opt)
438406

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

445-
self._lmodel = lmodel # may be needed if model is derived from other model
446-
self._model_name = modelName # Model class name
447-
if fileName is not None:
448-
file_name = self._getconn.omcpath(fileName).resolve()
464+
# set variables
465+
self._model_name = model # Model class name
466+
self._lmodel = libraries # may be needed if model is derived from other model
467+
if file is not None:
468+
file_name = self._getconn.omcpath(file).resolve()
449469
else:
450470
file_name = None
451-
self._file_name: Optional[OMCPath] = file_name # Model file/package name
452-
self._simulated = False # True if the model has already been simulated
453-
self._result_file: Optional[OMCPath] = None # for storing result file
454-
self._variable_filter = variableFilter
471+
self._file_name = file_name # Model file/package name
472+
self._variable_filter = variable_filter
455473

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

459-
# set default command Line Options for linearization as
460-
# linearize() will use the simulation executable and runtime
461-
# flag -l to perform linearization
462-
self.setCommandLineOptions("--linearizationDumpLanguage=python")
463-
self.setCommandLineOptions("--generateSymbolicLinearization")
464-
465-
self._work_dir: OMCPath = self.setWorkDirectory(customBuildDirectory)
466-
467-
if self._file_name is not None:
477+
if self._lmodel:
468478
self._loadLibrary(lmodel=self._lmodel)
479+
if self._file_name is not None:
469480
self._loadFile(fileName=self._file_name)
470481

471-
# allow directly loading models from MSL without fileName
472-
elif fileName is None and modelName is not None:
473-
self._loadLibrary(lmodel=self._lmodel)
474-
475482
if build:
476-
self.buildModel(variableFilter)
483+
self.buildModel(variable_filter)
477484

478485
def setCommandLineOptions(self, commandLineOptions: str):
479486
"""

0 commit comments

Comments
 (0)