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; controller.fmu
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.
- Twin 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 Twin Activate model as a single block. Additionally, an Twin 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 Twin Activate requires the selection of a solver and installation of a compiler.
Compiler Requirements
Twin Activate requires a C-compiler to compile super blocks into C-code and the FMU. Twin Activate detects if Microsoft Visual Studio or GCC is installed on your system and automatically invokes the compiler toolchain without any manual intervention.
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\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 2022: Community, Professional | Yes | Yes | Yes |
Microsoft® Visual Studio 2019: Community, Professional | Yes | Yes | 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 SP 2 |
|
RHEL/Oracle Linux 8.3 | or
|
FMU Export on Linux Platforms
FMU exporting on Linux requires ld version 2.27 or greater.
To export an FMU in Twin 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
-
On the ribbon, select Setup.
- On the Simulation Parameters dialog that appears, select the Solvers tab.
-
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.
Exporting a Super Block to the FMU
Export a super block from the controller model into the FMU.
-
From the ribbon, select Open
and
navigate to Controller.scm.
The Controller model opens with its main diagram displayed:
-
From the main diagram, double-click the super block
controller:
-
Double-click the input block regular_in_port.
-
In the dialog box, select the Time dependency check
box.
Note:
By default, Time dependency is not selected. If you do not select Time dependency, no error occurs if the Force inputs to always active check box is selected in the Code Generation and Export dialog box in step 9.
- To return to the display of the main diagram, double-click in the space outside of the controller super block diagram.
-
To expose parameters in the FMU, right-click the super block, and then select
.If you double-click the super block, you can see the parameters.
- From the main diagram, select the super block controller.
-
Select
from the menu ribbon.You can also right-click on the super block and select Code Generation and Export.
-
In the dialog box, select FMU as the
Target, and then click
Generate.
The FMU is generated and can be imported into another Twin Activate model or other software product that supports FMI, and subsequently run in either Cosimulation or Model Exchange.
Importing the FMU through Model Exchange
Import the FMU into a controller model with the Model Exchange option.
-
From the FMU block into the
modeling window.
, drag and drop one The FMU block provides the interface to import and configure the exported FMU file.
- On the FMU block, double-click.
-
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\FMU import and export\controller.fmu.
All other parameters on the General Parameters tab are automatically populated.
- In the Input ports table, for the Direct dependency vector for the input, enter 0.
-
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.
-
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.
- In the Palette Browser, from , drag and drop two Mux blocks into the diagram.
- On one of the Mux blocks, double-click.
- In the block dialog, for number of inputs, enter 3.
-
Assemble the scalar outputs from the FMU block so that the block resembles the
original super block from the input/output (I/O) perspective.
- Convert the FMU block and Mux blocks into a super block, and then in the Property Editor, Under General, for Name, enter FMU ME.
-
On the FMU ME super block, double-click.
The diagram of the super block is exposed.
- From the Input block and two Output blocks into the diagram. , drag and drop one
-
Connect the blocks as follows:
- To return to the main diagram, in the blank space outside of the current diagram, double-click.
-
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:
-
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:
-
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.
- On the ribbon, click Save .
- 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.
- Box-select the diagram that contains the super block, FMU ME, and press Ctrl+C:
-
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.
- Select the super block, FMU ME_1, and in the Property Editor, enter the name FMU CS.
- 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.
-
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:
-
On the ribbon, click Run
.
The simulation runs and the Scope windows produce the following plots:
- 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.
- On the ribbon, click Save .
- In the Save Model As dialog, for File name, enter Controller_FMU, and press, Save.