This blog post describes the procedures required to build FMUs from Dymola models and run the FMUs in both Dymola and Simulink. In this way Dymola models can be tested and run together with other models or controllers in Simulink. An engine model has been used to demonstrate the method as an example. The procedure will remain mostly the same for other applications.
It is sometimes desirable to test a Dymola engine model with an external controller designed in Simulink or run the engine model within hardware-in-the-loop environments, e.g. RedHawk Concurrent test rig or dSPACE. To do that, an engine model can be compiled to a functional Mock-up Unit (“FMU”) according to the Functional Mock-up Interface (“FMI”) open standard and exported into another host environment. This means that models that are compiled by third-party tools such as Dymola, CATIA DBM and many more can be imported and used in Simulink. The FMI standard defines two interfaces: one for model exchange; and one for co-simulation. When using the Co-simulation interface, this means that the solver built in to the FMU is used to simulate that model and the Simulink solver is used to simulate the Simulink part of the system with the two systems exchanging data at discrete times. The advantage of this is that appropriate solvers can be used for both parts of the model improving the overall simulation performance. When using the Model Exchange interface, the Simulink solver is used to handle the complete model i.e. the FMU equations and the Simulink model. This allows variable step solvers to be used and has the advantage that a single solver is used for the entire model.
The existing FMI standard does not support acausal physical connectors, so we need to use real inputs/outputs when creating an engine model in Dymola in order to couple with other FMUs. An engine model with a controller has been built in Dymola with real inputs and outputs. This blog post only provides instructions on how to run an engine and a controller in Dymola and Simulink. For splitting mechanical, electrical, thermal components in Dymola for running the split component in Simulink, refer to Splitting mechanical and fluid devices using real inputs real outputs.
For splitting an engine model into multiple sub-models and running the sub-models on a single or multiple cores of a hardware processor, please refer to Hybridisation and splitting of a crank angle resolved internal combustion engine model using a mean value intake for real-time performance. This blog post explains how to create FMUs using co-simulation. For the option to use model-exchange, the procedure will remain the same as co-simulation.
An engine model and a controller model are given as below
1. Building FMUs in Dymola
1) Open the controller model in Dymola
2) Go to the setup in Simulation window, choose solver and integration step as in figure 2 and figure 3. Make sure the “Fixed Integrator Step” in “general tab” and “Optimize code for step size” in the “Realtime” tab have the same settings, in this case they are both set to 5E-5s. The step size needs to be small enough to produce a correct result. Then click on OK to close simulation setup window.
3) While in the simulation window, click on the “translate” button and choose FMU, see figure 4.
4) Once the “Export FMU” pop-up window has opened, (see figure 5), choose “Co-simulation using Cvode”, “version 2”, “Include source code” “32-bit binaries” and “64-bit binaries” and give a name for the FMU in the space provided at the bottom, in this case it is called “ControllerCAREM”. Then click on OK. Do the same for the engine model. The FMUs will be created in the working directory, specified by the user. If the “Model exchange” option is preferred, then the procedure will remain the same. If a Dymola model contains data files saved as txt files, then make sure “Export with string parameters” is ticked.
Note the warning at the bottom of the window in figure 5, “Evaluate parameters is not set”, can be avoided by ticking the box related to “Evaluate parameters to reduce models” in the translation tab in simulation setup, see figure below.
2. Import FMU in Dymola
Once the FMUs are created, it is useful to check if the FMUs are correct by running them in Dymola and check that they produce the same result as the Dymola models that are used for creating the FMUs. The following procedure shows how to import FMUs into Dymola.
1) In the simulation window, click on the “fmu” button at the top left, as shown in Figure 7.
2) In the pop-up window “Import FMU”, see figure 8, choose “Co-simulation” and choose the FMU to be loaded in the Browser window and then click on the OK button. The FMU will be loaded at the bottom of the package Browser window in Dymola. When using the Co-simulation interface, this means that the solver built in to the FMU is used to simulate that model and the Simulink solver is used to simulate the Simulink part of the system with the two systems exchanging data at discrete times. If choosing “Model exchange” the procedure will remain the same.
3) Create a new experiment using the FMUs as shown below. Make sure the related inputs and outputs are connected correctly.
4) Double click on an FMU block, go to the FMI tab, and set the “fmi_NumberOfSteps” to be equal to (fmi_StopTime-fmi_StartTime)/stepsize, where stepsize was set to be 0.00005s, when creating the FMUs as shown in figure 2 and figure 3. Apply these settings for both the controller and the engine FMU.
5) Go to simulation window and open simulation setup, choose the setup options to be the same as in figure 2 and 3 and then click run.
6) The plots below show simulation result from the Dymola engine experiment in Figure 1 and the FMU model experiment in Figure 9. In figures below, figure 11 and 12, it is seen cylinder pressure and plenum pressure are near-identical between the Dymola engine experiment and the FMU engine experiments.
3. Running FMUs in Simulink
Now the FMUs have been created and tested in Dymola to make sure they run correctly. They are now ready to be imported into another host environment, e.g. Simulink. FMI Kit is developed by Dassault Systemes to support import or export of FMUs from or to MATLAB/Simulink. First, open the user guide for FMIKit in Mfiles folder in Dymola installation, for example in our case it is:
Install the FMIKit as described in the user guide. Once it has been installed, open the MATLAB and add the FMIKit_for_Simulink directory to your MATLAB path as shown below.
Then execute the command FMIKit.initialize() in the command window. Once FMIKit has been initialized, go to Simulink library Browser, go to FMI Kit in the Libraries window and drag the FMU block into a new model.
Double click on the FMU block to load a desired FMU, then close the window. Do the same for both the controller and the engine FMU.
Connect the appropriate inputs/outputs between the controller and the engine.
In Simulink, go to “Model Configuration Parameter” tab and follow the settings as in Figure 18. Choose Fixed-step solver ode1 Euler for example with step-size equal to 0.00005s, which is identical to the settings when creating the FMUs as shown in Figure 2.
The model is now ready for simulation. The plots below show cylinder pressure and plenum pressure profiles in Simulink.
It can be seen that figure 19 to figure 22 show very similar cylinder pressure and plenum pressure profiles as figure 11 and figure 12.
we have shown how to
- Export Dymola models as FMUs.
- Import FMUs in Dymola and test the FMUs.
- Import FMUs in Simulink and run the FMUs.
Written by: Xiaoran Han – Project Engineer