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 fixed-step and variable-step solvers.
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.
You can export a FMU from a project interactively.
In the project, select Share > Tool-Coupling FMU.
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="vdpSlave" 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'), './vdpSlave.slx') >> p.addFile('./vdpSlave.slx') >> Simulink.fmuexport.ExportSimulinkProjectToFMU(p,'vdpSlave.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 of numerical data type.
If the co-simulation component is an FMU exported from Simulink. the local sample time for that FMU is the sample time of the original model.
To include tunable parameters:
Open the model from the associated Simulink project.
From the Simulink model, click the Modeling tab and start Model Explorer.
Select Model Workspace and add a MATLAB® variable or Simulink parameter.
For each parameter you add and want tunable, in the Data Properties or Simulink.Parameter pane, select the Argument check box.
Click Apply.
Reference the tunable parameters in the model.
Export the tool-coupling FMU.
The exported FMU requires a local installation of Simulink to run. The MATLAB version used for co-simulation 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 Mac OS:
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 Mac OS, 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.
If the application that imports the FMU runs a single instance of the FMU, you can use MATLAB to start a session.
>> shareMATLABForFMUCoSim
This 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, Simulink editor and Simulink project are loaded, and co-simulation starts automatically. You can use this session to pause, resume co-simulation, as well as tune parameters or plot signals from the command window while co-simulation is running. If co-simulation is finished, stopped by user, or interrupted by a runtime error, MATLAB closes, unloads 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.
If the application that imports the FMU runs multiple FMU instances, you can use the operating system console to start dedicated MATLAB sessions.
On Windows:
<matlabroot>\toolbox\shared\fmu_share\script\fmu-matlab-setup.cmd
On Linux and Mac OS:
<matlabroot>/toolbox/shared/fmu_share/script/fmu-matlab-setup
Run matlabroot
in MATLAB to find out <matlabroot>. The setup program starts 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.