Main Content

Export a Model as a Tool-Coupling FMU

To integrate Simulink® components into third-party software, export a Simulink model as a tool-coupling functional mockup unit (FMU). When a third-party tool runs the FMU, it checks out required licenses and starts a local installation of Simulink to start the model. Tool-coupling FMUs support the fixed-step and variable-step solvers that the FMU encapsulates.

Use a project to export an FMU. Open the model and select New > Project > New Project from this Model to create a project from a model. This example uses the vdp model.

You can export a FMU from a project interactively.

  1. In the project, select Share > Tool-Coupling FMU.

  2. Type in the Copyright, Description, and FMU Icon fields. Click Save Settings and Export as and provide a name.

The generated FMU includes model implementation, as well as the metadata provided during export.

<?xml version="1.0" encoding="utf-8"?>
<fmiModelDescription author="" copyright="" description="" fmiVersion="2.0"
                     generationDateAndTime="2018-08-16T15:51:48Z"
                     generationTool="Simulink (R2018b)"
                     guid="5bd096be-a08d-020e-fc96-847aa21def5b"
                     license=""
                     modelName="vdp"
                     variableNamingConvention="structured"
                     version="1.8">

You can also create and export a project to a FMU from the command line as follows:

>> p = slproject.create('vdpProject') 
>> copyfile(which('vdp'), './vdpFMU.slx') 
>> p.addFile('./vdpFMU.slx')
>> Simulink.fmuexport.ExportSimulinkProjectToFMU(p,'vdpFMU.slx','-fmuname','vdpFMU')

For more FMU export options from the command line, type:

help Simulink.fmuexport.ExportSimulinkProjectToFMU

The model must satisfy these conditions for exporting:

  • Model must be in normal or accelerator simulation mode.

  • Root input and output ports must be one of these numerical data types:

    • double

    • int32

    • Boolean

    • Simulink.Bus object, where all elements are double, int32, or Boolean

    • Simulink.AliasType object, in which the base type resolves to double, int32 or Boolean

If the cosimulation component is an FMU exported from Simulink. the local sample time for that FMU is the sample time of the original model.

Include Tunable Parameters for Tool-Coupling FMU

To include tunable parameters:

  1. Open the model from the associated Simulink project.

  2. From the Simulink model, click the Modeling tab and start Model Explorer.

  3. Select Model Workspace and add a MATLAB® variable or Simulink parameter.

  4. For each parameter you add and want tunable, in the Data Properties or Simulink.Parameter pane, select the Argument check box.

  5. Click Apply.

  6. Reference the tunable parameters in the model.

  7. Export the tool-coupling FMU.

Use Protected Model to Conceal Content

The exported tool-coupling FMU is a zip package and contains the original Simulink project and model files in its resources folder. To protect your intellectual property and conceal the contents of your model, use protected models. When you save a model as a protected model, select the functionality for the protected model to support, such as simulation. Create a model with a Model block to load the SLXP file. Then, use this model as the main interface model when exporting to tool-coupling FMU. For more information, refer to Protect Models to Conceal Contents (Embedded Coder).

Use the Exported Tool-Coupling FMU

The exported FMU requires a local installation of Simulink to run. The MATLAB version used for cosimulation must be the same as the MATLAB version where the FMU is exported. On Windows®, the application that runs the FMU can check out the required licenses automatically. For other operating systems, apply these settings:

  • On Linux®:

    setenv LD_LIBRARY_PATH ${LD_LIBRARY_PATH}:<InstallationFolder>/bin/glnxa64:<InstallationFolder>/extern/bin/glnxa64  (csh/tcsh) 
    export LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:<InstallationFolder>/bin/glnxa64:<InstallationFolder>/extern/bin/glnxa64  (bash)

  • On macOS:

    setenv DYLD_LIBRARY_PATH ${DYLD_LIBRARY_PATH}:<InstallationFolder>/bin/maci64:<InstallationFolder>/extern/bin/maci64  (csh/tcsh) 
    export DYLD_LIBRARY_PATH=${DYLD_LIBRARY_PATH}:<InstallationFolder>/bin/maci64:<InstallationFolder>/extern/bin/maci64  (bash)

For macOS, due to System Integrity Protection (SIP), setenv command does not work for applications that starts new processes, such as MATLAB. Follow Append library path to "DYLD_LIBRARY_PATH" in MAC to set DYLD_LIBRARY_PATH to <InstallationFolder>/bin/maci64:<InstallationFolder>/extern/bin/maci64.

Before you can run the FMU, you must set up a MATLAB session from your operating system console. After you set up this session, start the third-party application and import the tool-coupling FMU. Each FMU instance requires a new MATLAB session.

Start a Dedicated Session from MATLAB

If the application that imports the FMU runs a single instance of the FMU, you can use MATLAB to start a session.

>> shareMATLABForFMUCoSim

shareMATLABForFMUCoSim dedicates the current MATLAB session available for requests from the external tool to co-simulate an imported FMU. When an FMU is connected this session, the Simulink editor and Simulink project are loaded, and cosimulation starts automatically. You can use this session to pause, resume cosimulation, tune parameters, and plot signals from the command window while cosimulation is running. If cosimulation is finished, stopped by you, or interrupted by a runtime error, MATLAB closes, unloads the Simulink editor and Simulink project, and discards changes to the model. If an error occurs, it displays in the simulation tool that imports this FMU. Each session can connect to one FMU instance at the same time.

Start a Dedicated Session from the Operating System

If the application that imports the FMU runs multiple FMU instances, you can use the operating system console to start dedicated MATLAB sessions. Run matlabroot in MATLAB to find out <matlabroot>.

Then, use <matlabroot>:

  • On Windows:

    <matlabroot>\toolbox\shared\fmu_share\script\fmu-matlab-setup.cmd [NumberOfMATLABSessions]

  • On Linux and macOS:

    <matlabroot>/toolbox/shared/fmu_share/script/fmu-matlab-setup [NumberOfMATLABSessions]

The input argument NumberOfMATLABSessions is an integer representing the number of MATLAB sessions to be launched when starting this script. If NumberOfMATLABSessions is not present, MATLAB sessions are not launched. Then, the program enters command-line interface mode and waits for a command.

See available commands by typing help:

> help
Command list:                                                                                                                                                                                 
quit - Close all shared MATLABs and exit.                                                                                                                                                     
list - List shared MATLABs.                                                                                                                                                                   
start NUMBER_OF_MATLABS - Start NUMBER_OF_MATLABS more MATLABs.                                                                                                                               
stop NUMBER_OF_MATLABS - Stop NUMBER_OF_MATLABS MATLABs.                                                                                                                                      
ignore - Stop asking about the hardware core count when launching MATLABs.                                                                                                                    
clean MATLAB_NUMBER - Clean up the MATLAB workspace for MATLAB #MATLAB_NUMBER. Use 0 for all MATLABs.                                                                                         
help - Print the command list.

Launch one session for each FMU to run concurrently. If there is a single FMU, type:

> start 1

If there are three FMUs running concurrently, type:

> start 3

Only one MATLAB management tool should be running on a single machine.

See Also

Related Topics