Build FMUs from Dymola model and run the FMUs in Dymola and Simulink

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

Figure 1: An engine model with a controller model

Figure 1: An engine model with a controller model

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.

Figure 2: Solver setup in “General” tab for exporting an FMU

Figure 2: Solver setup in “General” tab for exporting an FMU

Figure 3: Solver setup in “Realtime” tab for exporting an FMU

Figure 3: Solver setup in “Realtime” tab for exporting an FMU

3) While in the simulation window, click on the “translate” button and choose FMU, see figure 4.

Figure 4: Choose FMU in the “translate” menu

Figure 4: Choose FMU in the “translate” menu

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.

Figure 5: FMU export set up

Figure 5: FMU export set up

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.

Figure 6: Tick “Evaluate parameters” to remove the warning message at the bottom of Figure 5

Figure 6: Tick “Evaluate parameters” to remove the warning message at the bottom of Figure 5

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.

Figure 7: Click on the fmu icon to import an FMU into Dymola

Figure 7: Click on the fmu icon to import an FMU into Dymola

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.

Figure 8: Import an FMU into Dymola

Figure 8: Import an FMU into Dymola

3) Create a new experiment using the FMUs as shown below. Make sure the related inputs and outputs are connected correctly.

Figure 9: Run the FMUs in Dymola

Figure 9: Run the FMUs in Dymola

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.

Figure 10: Setting the stop time and the number of steps in the FMU

Figure 10: Setting the stop time and the number of steps in the 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.

Figure 11: Cylinder pressure and plenum pressure from Dymola engine model experiment and its FMU model experiment at 1.45s in Dymola

Figure 11: Cylinder pressure and plenum pressure from Dymola engine model experiment and its FMU model experiment at 1.45s in Dymola

Figure 12: Cylinder pressure and plenum pressure from Dymola engine model experiment and its FMU model experiment from 0 to 1.5 s in Dymola

Figure 12: Cylinder pressure and plenum pressure from Dymola engine model experiment and its FMU model experiment from 0 to 1.5 s in Dymola

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:

Figure 13: Open FMI Kit for Simulink – Users Guide to learn how to install the FMI Kit in Simulink

Figure 13: Open FMI Kit for Simulink – Users Guide to learn how to install the FMI Kit in Simulink

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.

Figure 14: Add the FMIKit_for_Simulink directory to MATLAB path

Figure 14: Add the FMIKit_for_Simulink directory to MATLAB path

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.

Figure 15: Go to FMI Kit in the library package and drag an FMU block into your model

Figure 15: Go to FMI Kit in the library package and drag an FMU block into your 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.

Figure 16: Double click on the FMU block and load an FMU model

Figure 16: Double click on the FMU block and load an FMU model

Connect the appropriate inputs/outputs between the controller and the engine.

Figure 17: Simulink model of the imported FMUs

Figure 17: Simulink model of the imported FMUs

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.

Figure 18: Simulink setting for running FMUs

Figure 18: Simulink setting for running FMUs

The model is now ready for simulation. The plots below show cylinder pressure and plenum pressure profiles in Simulink.

Figure 19: cylinder pressure at 1.45s in Simulink

Figure 19: cylinder pressure at 1.45s in Simulink

Figure 20: plenum pressure at 1.45s in Simulink

Figure 20: plenum pressure at 1.45s in Simulink

Figure 21: Cylinder pressure from 0 to 1.5 s in Simulink

Figure 21: Cylinder pressure from 0 to 1.5 s in Simulink

Figure 22: Plenum pressure from 0 to 1.5 s in Simulink

Figure 22: Plenum pressure from 0 to 1.5 s 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

  1. Export Dymola models as FMUs.
  2. Import FMUs in Dymola and test the FMUs.
  3. Import FMUs in Simulink and run the FMUs.

Written by: Xiaoran Han – Project Engineer

 

Please get in touch if you have any questions or have got a topic in mind that you would like us to write about. You can submit your questions / topics via: Tech Blog Questions / Topic Suggestion.

CONTACT US

Got a question? Just fill in this form and send it to us and we'll get back to you shortly.

Sending

© Copyright 2010-2019 Claytex Services Ltd All Rights Reserved. | Designed by CMA Marketing

Log in with your credentials

Forgot your details?