sofirpy.fmu_export.dymola_fmu_export module

This module allows to export a Dymola model as a fmu.

class sofirpy.fmu_export.dymola_fmu_export.DymolaFmuExport(model_path: Path, model_name: str, fmu_name: str | None = None, parameters: dict[str, ParameterValue] | None = None, model_modifiers: list[str] | None = None, packages: list[str | Path] | None = None, output_directory: Path | None = None, fmi_version: Literal[1, 2] = 2, fmi_type: Literal['me', 'cs', 'all', 'csSolver'] = 'all', include_source: bool = False, include_image: Literal[0, 1, 2] = 2)[source]

Bases: FmuExport

Object that performs the Dymola fmu export.

Parameters:
  • model_path (Path) – Path to the modelica model that should be exported.

  • model_name (str) – Name of the model that should be exported. If the model that should be exported is inside a package, separate the package name and the model name with a ‘.’.

  • fmu_name (str | None, optional) – Name the exported fmu should have. If not specified the fmu will have the same name as the model. Defaults to None.

  • parameters (dict[str, ParameterValue], optional) –

    Dictionary of parameter names and values. Example:

    >>> parameters = {"Resistor.R" : "1",
    ...     "Resistor.useHeatPort": True}
    

    Defaults to None.

  • model_modifiers (list[str]], optional) –

    List of model modifiers. Example:

    >>> model_modifiers = [
    ...     "redeclare package Medium = "
    ...     "Modelica.Media.Water.ConstantPropertyLiquidWater"
    ... ]
    

    Defaults to None.

  • packages (list[str], optional) – List of model/package paths that need to be loaded as dependencies for the model. Defaults to None.

  • output_directory (Path | None, optional) – Output directory for the fmu, the log and the mos script. Defaults to None.

  • fmi_version (Literal[1, 2], optional) – FMI version, 1 or 2. Defaults to 2.

  • fmi_type (Literal["me", "cs", "all", "csSolver"], optional) – FMI type, me (model exchange), cs (co-simulation), all or csSolver (using Dymola solver). Defaults to “all”.

  • include_source (bool, optional) – Whether to include source code in FMU. Defaults to False.

  • include_image (Literal[0, 1, 2], optional) – Whether to include the model image (0 - no image, 1 icon, 2 diagram). Defaults to 2.

create_mos_file(mos_script: str) None[source]

Create the mos file with the specified content.

Parameters:

mos_script (str) – The content for the mos file.

export_fmu(dymola_exe_path: Path) bool[source]

Execute commands to export a fmu.

Parameters:
  • dymola_exe_path (Path) – Path to the dymola executable.

  • export_simulator_log (bool, optional) – If True a simulator log file will be generated. Defaults to True.

  • export_error_log (bool, optional) – If True a error log file will be generated. Defaults to True.

Returns:

True if export is successful else False

Return type:

bool

format_parameters() list[str][source]

Format parameter values.

Format parameter values to adjust to dymola scripting syntax and stores the parameter names and values in a list in the following format.

>>> parameter_declaration = ['Resistor.R =  1',
...                          'Resistor.useHeatPort = true']
Returns:

List of parameters.

Return type:

list[str]

property model_modifiers: list[str]

List of model modifiers.

Returns:

List of model modifiers.

Return type:

list[str]

property model_name: str

Name of the model.

Returns:

Name of the model.

Return type:

str

move_files_to_output_directory(export_successful: bool, keep_mos: bool, keep_log: bool) None[source]

Move the fmu, the mos script and the log to the output directory.

Parameters:
  • export_successful (bool) – If True fmu will be moved to the output directory.

  • keep_mos (bool) – If True the mos script is moved to the output directory.

  • keep_log (bool) – If True the simulator log is moved to the output directory.

move_log_file() None[source]

Move the log file to the output directory.

move_mos_script() None[source]

Move the mos script to the output directory.

property parameters: dict[str, str | int | float | list[int | float | str | bool] | bool]

Dictionary of parameter names and values.

Returns:

Dictionary of parameter names and values

Return type:

dict[str, ParameterValue]

read_dymola_error() str[source]

Read the Dymola error message.

Returns:

Dymola error message

Return type:

str

write_mos_script(export_simulator_log: bool = True) str[source]

Write the content for the mos file/script.

The script contains the necessary instructions to import the specified parameters and model modifiers and export the model as a fmu.

Parameters:
  • export_simulator_log (bool, optional) – If True a simulator log file will be generated. Defaults to True.

  • export_error_log (bool, optional) – If True a error log file will be generated. Defaults to True.

Returns:

content for the mos script

Return type:

str

sofirpy.fmu_export.dymola_fmu_export.export_dymola_model(*, dymola_exe_path: Path | str, model_path: Path | str, model_name: str, fmu_name: str | None = None, output_directory: Path | str | None = None, parameters: dict[str, ParameterValue] | None = None, model_modifiers: list[str] | None = None, packages: list[str | Path] | None = None, fmi_version: Literal[1, 2] = 2, fmi_type: Literal['me', 'cs', 'all', 'csSolver'] = 'all', include_source: bool = False, include_image: Literal[0, 1, 2] = 2, keep_log: bool = False, keep_mos: bool = False) Path[source]

Export a dymola model as a fmu.

Parameters:
  • dymola_exe_path (Path | str) – Path to the dymola executable.

  • model_path (Path | str) – Path to the dymola model that should be exported.

  • model_name (str) – Name of the model that should be exported. If the model that should be exported is inside a package, separate the package name and the model name with a ‘.’.

  • fmu_name (str | None, optional) – Name the exported fmu should have. If not specified the fmu will have the same name as the model. Defaults to None.

  • output_directory (Path | str) – Output directory for the fmu, the log and the mos script. Defaults to None.

  • parameters (dict[str, ParameterValue], optional) –

    Dictionary of parameter names and values. Example:

    >>> parameters = {"Resistor.R" : "1", "Resistor.useHeatPort": True}
    

    Defaults to None.

  • model_modifiers (list[str]], optional) –

    List of model modifiers. Example:

    >>> model_modifiers = ["redeclare package Medium ="
    ...     "Modelica.Media.Water.ConstantPropertyLiquidWater"]
    

    Defaults to None.

  • packages (list[str | Path] | None, optional) – List of model/package paths that need to be loaded as dependencies for the model. Defaults to None.

  • fmi_version (Literal[1, 2], optional) – FMI version, 1 or 2. Defaults to 2.

  • fmi_type (Literal["me", "cs", "all", "csSolver"], optional) – FMI type, me (model exchange), cs (co-simulation), all or csSolver (using Dymola solver). Defaults to “all”.

  • include_source (bool, optional) – Whether to include source code in FMU. Defaults to False.

  • include_image (Literal[0, 1, 2], optional) – Whether to include the model image (0 - no image, 1 icon, 2 diagram). Defaults to 2.

  • keep_log (bool, optional) – If True the simulator log is kept else it will be deleted. Defaults to False.

  • keep_mos (bool, optional) – If True the mos script is kept else it will be deleted. Defaults to False.

Returns:

Path to the exported FMU.

Return type:

Path