October 31st, 2016 by Mahdieh Mehrabi
Written by: Garron Fish – Chief Engineer
Function Mock-up Interface (FMI) is a standard used for model exchange and co-simulation of dynamic models. FMI is useful because it is tool independent (i.e. the standard is not controlled by one tool vendor) and is well supported by many popular simulation tools (91 tools so far). So the FMI standard provides a way to interface models from different simulation tools.
Figure 1: Data flow between the environment and an FMU
Models are exported as Functional Mock-up Units (FMUs). An example of how the FMU data flow is in Figure 1. Models can be exported for model exchange or for co-simulation.
With the model exchange method, solver integration is performed outside of the FMU.
With the co-simulation method the model contains a solver and the solver integration is performed inside the FMU.
These FMUs can be simulated in most of the simulation tools that generate them. They can also be simulated using C-code, the FMI standard describes the C-code functions for simulating these FMUs (see FMI for ModelExchange and CoSimulation Specification v2.0).
QTronic provide a free software development kit which can be used to simulate FMUs from C-code. This code also contains a number of demos and is a good way to get introduced simulating an FMU from C-code. The SDK development kit can be obtained from here: SDK Development Kit
Throughout the rest of the article I make use of the Modelica.Blocks.Examples.PID_Controller (referred to as PID_Controller) model. This model is split into two models, one containing the reference speed generation blocks (referred to as ReferenceSpeedGeneration) and the other contains the PI and plant models (referred to as PIAndPlant). The full vehicle model is in Figure 2.
Figure 2: Full PID_Controller model.
This model is split into two models, one containing the reference speed generation blocks and the other containing the PI and plant models.
These models are exported by selecting the model and switch to the Simulation page and export the model using Simulation->Translate->FMU. The settings for the FMU export are given below:
Figure 3: FMU export options.
When the model is exported by default all the variables in the model can be viewed in the results. If the Black-box option is selected only the input and output signals can be viewed.
If an input or output is not an SI unit then during the conversion process they are scaled to SI units and the suffix “_inBaseUnits” is added to the signal name.
The FMU models are imported, by using Goto File->Import->FMU. The importing options are below:
Figure 4: FMU import options
Both Model exchange and Co-simulation are supported.
Using Model Exchange
ReferenceSpeedGeneration and PIAndPlant models were imported as Model exchange; these models were joined and this model was simulated.
Figure 5: FMU model of PID_controller model
There are some differences in how the initialization is performed and initial start values are now provided for input values.
Figure 6: FMI models parameter dialog with Input Start Values
There is also an FMI tab for selecting logging and the location where the FMU is located.
Figure 7: FMI tab for Model Exchange model
The Model Exchange model was simulated and the results were compared with the PID_controller model. Both gave exactly the same results.
Figure 8: Comparison of the PID controller model vs split Model Exchange model (uses Dymola solver)
With Co-Simulation the solver is part of the FMU model.
To enable Co-Simulation the model needs to be exported with Co-Simulation enabled (see Figure 3). There are three options that support Co-simulation, two that allow Co-Simulation using Cvode and one that uses the solver selected in Dymola. Note: Real-time Inline solvers are also supported.
The FMU model will need to be imported with Co-simulation selected in the FMI import dialog (see Figure 4).
When simulating Co-Simulation models the input to the model is sampled, using Zero order hold, and the FMU’s solver is used to solve the simulation to the next communication step.
FMU’s parameter dialog boxes include settings to control the embedded solver (as in Figure 9).
Figure 9: FMI tab for FMI parameter settings.
Settings control the step-size and number of sub-steps between communication steps.
The fmi_CommunicationStepSize is the communication time step (i.e. when the inputs and outputs of the FMU model are updated). The stepSizeScaleFactor is the number of solver steps run between communication steps.
ReferenceSpeedGeneration and PIAndPlant models were imported as Co-Simulation, these models were joined and this model was simulated. The results of the simulation follow:
Figure 10: Comparison of FMU Co-Simulation split and full PID_Controller model.
Figure 11: Close up of the comparison of the FMU Co-Simulation split and the full PID_Controller model.
The Co-Simulation split model results are sampled with a zero order hold. The sampling introduces a delay between the ReferenceSpeedGeneration and PIAndPlant.
Written by: Garron Fish – Chief Engineer