Tutorial: FMU Import and Export

Learn how to import the FMU (Functional Mock-up Unit) into your model for data exchange, and export the FMU for co-simulation.

Files for This Tutorial

Controller.scm; Controller_FMU.scm; sb_controller_.fmu

A finished version of the models you build in the tutorials along with any files required to complete the tutorials are available at this location: <installation_directory>/tutorial_models/.

About FMI and Requirements

The Functional Mock-up Interface (FMI) is a tool-independent standard that supports both model exchange and co-simulation of dynamic models using a combination of XML files and compiled C-code.

  • Activate supports FMI 1.0 import and FMI 2.0 import and export processes.
  • A Functional Mock-Up Unit (FMU) can be imported into an Activate model as a single block. Additionally, an Activate super block can be exported as an FMU in order to be exchanged and co-simulated with other tools supporting the FMI standard.
  • The use of the Functional Mock-up Interface with Activate requires the selection of a solver and installation of a compiler.

Compiler Requirements

Activate requires a C-compiler to compile super blocks into C-code and the FMU. Activate detects if Microsoft Visual Studio or GCC is installed on your system and automatically invokes the compiler toolchain without any manual intervention.

Note: The TCC compiler has some limitations and cannot be used for FMU export.

If you do not have one of the supported compilers installed, you can complete the tutorial by loading the FMU that is provided with the tutorial model files: <installation_directory>\tutorial_models\sb_controller.fmu. If loading this file, skip the section, Exporting a Super Block to the FMU, and proceed to Importing the FMU through Model Exchange.

Supported Compilers on Windows Platforms

TCC is the default compiler provided for Windows. Some use cases require the installation of a third-party compiler. The following are the supported compilers for the Functional Mock-Up Interface (FMI), Modelica components, and C-code generation.

Compiler FMI Export Modelica Support C-Code Generation
Microsoft® Visual Studio 2019: Community, Professional Yes Yes Yes
Microsoft® Visual Studio 2017: Express, Community, Professional Yes Yes Yes
Microsoft® Visual Studio C++ Build Tools 2019; 2017; 2015
Note: The option for Windows 10 SDK must be selected.
Yes Yes Yes
Microsoft® Visual Studio C++ 2015 (VC 14.0 Community and Professional) Yes Yes Yes
Microsoft® Visual Studio C++ 2013 (VC EXPRESS/VC Pro 12.0) Yes Yes Yes
Microsoft® Visual Studio C++ 2012 (VC EXPRESS/VC Pro 11.0) Yes No Yes
Microsoft® Visual Studio C++ VC 10.0 (VC Pro 10.0) Yes Yes Yes
Microsoft® Visual Studio C++ SDK 7.1 (VC EXPRESS 10.0) Yes No Yes
TCC (default provided with Activate installation) No No Yes

Supported Compilers on Linux Platforms

GCC is the default compiler provided for Linux. Some use cases require the installation of a third-party compiler. The following are the supported compilers for the Functional Mock-Up Interface (FMI), Modelica components, and C-code generation.

Compiler FMI Export Modelica Support C-Code Generation
GCC Yes Yes Yes

Required Development Tools for Linux Platforms

The installation of development tools is required for Linux platforms to compile C code. The proposed commands will ensure that the required development and build tools are installed for GCC and G++.

Linux Platform Installation Command
SUSE 15 SP1
zypper install -t pattern “devel_basis”
CentOS 8.2 / RHEL 8.2
dnf group install "Development Tools"
or
yum group install "Development Tools"

FMU Export on Linux Platforms

FMU exporting on Linux requires ld version 2.27 or greater.

To export an FMU in Activate on Linux platforms, the compilation and linking of the C code requires ld version 2.27 or greater. You can ensure that you are working with a compatible version of ld by installing or updating Binutils with the command: yum install binutils.

Selecting a Solver

  1. On the ribbon, select Setup.


  2. On the Simulation Parameters dialog that appears, select the Solvers tab.
  3. For solver, select: LSODA, and then click OK.
    Note: Before exporting a super block as an FMU, keep in mind that not all models can be exported as an FMU for Model Exchange. For example, implicit models containing implicit blocks, such as Constraint or Automaton, cannot be exported as an FMU for Model Exchange. All models, on the other hand, can be exported as an FMU for Co-Simulation.

Importing the FMU through Model Exchange

Import the FMU into a controller model with the Model Exchange option.

  1. From the Palette Browser > Activate > CoSimulation, drag and drop one FMU block into the modeling window.
    The FMU block provides the interface to import and configure the exported FMU file.
  2. On the FMU block, double-click.
  3. In the FMU block dialog, for the FMU filename, click , and browse to the exported FMU file that you created in the last steps. Alternatively, to use the example FMU that is provided for this tutorial, browse to <installation_directory>\tutorial_models\sb_controller.fmu.
    All other parameters on the General Parameters tab are automatically populated.
  4. In the Input ports table, for the Direct dependency vector for the input, enter 0.
  5. Click OK.
    The Direct dependency vector for the input sets the input to affect the output directly. In this case, the input does not affect the output, therefore the value is 0.
  6. To resize the FMU block, select it and drag it from the corner until you can easily read all of the labels on the block.
    The FMU block has one input and 5 outputs. Note that all inputs and outputs carry a scalar signal and that vector or matrix data is not allowed in the FMI standard.

    You can derive the input and output dimensions of the original super block controller by examining the ContStateSpace and MatrixGain blocks inside. The input size is 1x1; the first output size is 2x1; and the second output size is 3x1.

  7. In the Palette Browser, from System > Activate > Routing, drag and drop two Mux blocks into the diagram.
  8. On one of the Mux blocks, double-click.
  9. In the block dialog, for number of inputs, enter 3.
  10. Assemble the scalar outputs from the FMU block so that the block resembles the original super block from the input/output (I/O) perspective.
  11. Convert the FMU block and Mux blocks into a super block, and then in the Property Editor, Under General, for Name, enter FMU ME.
  12. On the FMU ME super block, double-click.
    The diagram of the super block is exposed.
  13. From the Palette Browser > Activate > Ports, drag and drop one Input block and two Output blocks into the diagram.
  14. Connect the blocks as follows:
  15. To return to the main diagram, in the blank space outside of the current diagram, double-click.
  16. To duplicate the main diagram, box-select the entire main diagram, press Ctrl+C, and then Ctrl+V.
    A duplicate diagram is added to the model:
  17. Select the super block FMU ME containing the FMU block; press Ctrl+X; select the super block controller_1; press Ctrl+V.
    The super block, controller_1, is replaced with FMU ME:
  18. On the ribbon, from the Simulate tool group, click Run .
    As the simulation runs, examine the Scope windows of the two Scope blocks. The windows should display the same curves indicating that the imported FMU is co-simulating successfully with the rest of the model, and that the imported FMU behavior is the same as the original super block from which the FMU was exported.
  19. On the ribbon, click Save .
  20. In the Save Model As dialog, for File name, enter Controller_FMU, and press Save.

Importing the FMU Through Co-Simulation

Import the FMU into a controller model with the Co-Simulation option.

  1. Box-select the diagram that contains the super block, FMU ME, and press Ctrl+C:
  2. Move your cursor to a blank area below the diagrams, click, and press Ctrl+V.
    The diagram with the FMU ME super block is added as a third model for comparison.

    The next steps show you how change the simulation type to co-simulation.

  3. Select the super block, FMU ME_1, and in the Property Editor, enter the name FMU CS.
  4. Configure the FMU block to run the FMU in co-simulation mode: Enter the FMU CS super block, then on the FMU block, double-click.
  5. From the block dialog that appears, select the Advanced tab and clear the check box: Run as Model Exchange if both FMU types are provided. When the check box is cleared, the FMU runs in co-simulation mode; when the check box is selected, the FMU runs in model exchange mode.
    Your model should look something like this:
  6. On the ribbon, click Run .
    The simulation runs and the Scope windows produce the following plots:
  7. Compare the simulation results in the three Scope windows. You should see three identical curves supporting that the FMU exported from the steps in the topic, Exporting a Super Block to the FMU, could be imported back and simulated in both Model Exchange and Co-Simulation modes, and that the FMU results are identical to the original super block from which the FMU was generated.
  8. On the ribbon, click Save .
  9. In the Save Model As dialog, for File name, enter Controller_FMU, and press, Save.

Exporting a Super Block to the FMU

Export a super block from the controller model into the FMU.

  1. From the ribbon, select Open and navigate to Controller.scm.
    The Controller model opens with its main diagram displayed:
  2. From the main diagram, double-click the super block, controller:
  3. Double-click the input block, regular_in_port.
  4. The block dialog opens.
    Note:

    The time dependency setting applied with earlier editions of Activate is no longer required and is currently ignored.

  5. In the blank space just outside of the diagram, right-click, and select Context.
  6. In the Context dialog, enter the following variable definitions, and click OK:
    Tip: The variables are also defined in the root diagram context. You can copy the definitions from there and paste them into the controller diagram context.
    Note: This step is required for the FMU export because all context variables defined in the super block must be defined locally before exporting to the FMU. The super block to be exported searches for context variables defined at the current level only.
  7. To return to the display of the main diagram, in the space outside of the controller super block diagram, double-click.
  8. From the main diagram, select the super block, controller.
  9. From the menu bar, select Tools > FMU 2.0 > Cosimulation and Model Exchange.
  10. In the File Browser that appears, specify a directory where you want to save the generated FMU file, sb_controller.fmu.
    The FMU is generated and can be imported into another Activate model or other software product that supports FMI, and subsequently run in either Cosimulation or Model Exchange.