FMU Export

May 23, 2022 ยท View on GitHub

FMI Kit provides two Simulink Coder targets to export Simulink models as FMUs:

  • grtfmi.tlc (based on the Generic Real-Time target)
  • rtwsfcnfmi.tlc (based on the S-function target)

The table below gives an overview of the different capabilities:

GRTS-Function
Model Referencesyesno
Source Codeyesno
Co-Simulationyesyes
Model Exchangenoyes
Requires CMakeyesyes

Which target should I use?

Use grtfmi.tlc unless you...

  • can't use a fixed-step solver
  • need a Model Exchange FMU
  • have to use binary S-functions

Supported compilers

Windows: Visual Studio 2015 and later

Linux: GCC

macOS: clang

Exporting an FMU

To export a Simulink model as an FMU

  • open the model to export (e.g. type f14 in the command window)
  • add external inputs and outputs (optional)
  • open Simulation > Model Configuration Parameters (CTRL+E)
  • under Solver > Type select Fixed-step for a Co-Simulation FMU or Variable-step for a Model Exchange FMU
  • under Code Generation > System target file click Browse and select grtfmi.tlc for a Generic Real-Time based FMU or rtwsfcnfmi.tlc for an S-function based FMU
  • under Code Generation > CMake select the CMake generator for your toolchain
  • apply the Model Configuration Parameters dialog
  • select Code > C/C++ Code > Build Model (CTRL+B) to export the FMU to the current working directory

Coder target

Generic Real-Time Target based FMU

The grtfmi.tlc target has the following options under Simulation > Model Configuration Parameters > FMI:

ParameterDescription
FMI versionFMI version of the FMU
Visible parametersParameters to include in the model description (leave empty to include all)
Model authorModel author to be written to the model description
Template directoryTemplate directory with files and folders to be added to the FMU
Add image of Simulink modelAdd an image of the Simulink model to the FMU (model.png)
Include sources in FMUAdd model sources to FMU
Include block outputsInclude global block outputs in the model description

and under Simulation > Model Configuration Parameters > CMake:

ParameterDescription
CMake commandCMake command or path the executable (leave empty for default command)
CMake generatorCMake generator to build the shared library
ToolsetCMake toolset specification (optional)
Build configurationCMake build configuration
Compiler optimization levelCompiler optimization level
Custom compiler optimization flagsCustom compiler optimization flags

Example of a template folder:

+ binaries
  + win64
    - Solver.dll
+ documentation
  - index.html
  + licenses
    - license.txt
+ resources
  - table.dat

Note that only files under binaries, documentation, resources, and sources will be added to the FMU archive.

S-Function based FMU

The rtwsfcnfmi.tlc target has the following options under Simulation > Model Configuration Parameters > FMI:

ParameterDescription
FMI versionFMI version of the FMU
FMI typespecifies FMI type (ModelExchange or CoSimulation)
Visible parametersParameters to include in the model description (leave empty to include all)
Model authorModel author to be written to the model description
Template directoryTemplate directory with files and folders to be added to the FMU
Include global block outputsselects if block outputs should be included in FMU XML description
Add image of Simulink modelAdd an image of the Simulink model to the FMU (model.png)
Load S-functions from binary MEX filesselects that S-functions in the model will be loaded from pre-compiled binary MEX files instead of using stand-alone compilation of S-function sources. This option will create dependencies on MATLAB binaries, which are not included in the exported FMU. On Windows, the FMU will by default try to load these from the bin directory of the exporting MATLAB installation. The environment variable SFCN_FMI_MATLAB_BIN can be used to specify a different directory from where to load the MATLAB binaries (for example a MATLAB run-time installation). On Linux, it is required to use the environment variable LD_LIBRARY_PATH to specify the path to the MATLAB binaries. The S-function MEX files used by the model are copied to /resources/SFunctions of the FMU and are loaded automatically when the FMU is instantiated.

and under Simulation > Model Configuration Parameters > CMake:

ParameterDescription
CMake commandCMake command or path the executable (leave empty for default command)
CMake generatorCMake generator to build the shared library
ToolsetCMake toolset specification (optional)
Build configurationCMake build configuration
Compiler optimization levelCompiler optimization level
Custom compiler optimization flagsCustom compiler optimization flags

It is recommended to set the Tasking mode to SingleTasking.

Limitations and Trouble-Shooting

  • The FMU is compiled with dynamic loading of the C run-time on Windows. This may require installation of corresponding Visual Studio redistributables on the target system.

  • The option Include block hierarchy in variable names could in very rare cases give rise to name conflicts in the XML variable names. For example, any special characters in Simulink block names will be converted to underscore which may lead to name conflicts. It is recommended to avoid using special characters in block names with this option (carriage return and space are safe to use).

  • The variable names for continuous-time states, discrete states, parameters, and block outputs are separated in the top-level categories ContStates, DiscStates, Parameters, and BlockOutputs in the structured view. This is to ensure unique variable names in the FMU XML file, since variable names from different categories are not guaranteed to be unique within a block. In the flat view, the variable names are appended with _xc, _xd, _pm and _wb respectively.

  • For multiple instances of conditionally executed non-virtual subsystems or Stateflow charts, it is required to select Treat as atomic unit and set Function packaging to Inline for the subsystems/charts.

  • S-functions in the exported model are not allowed to call into the MATLAB environment, e.g. using mexCallMATLAB or mexEvalString.

  • The FMU export target is not model reference compliant.

  • The package is subject to the same limitations as the standard S-Function target.