Main Content

Export Simulink Model to FMU with Variable Step Solver

The following example demostrates how to generate Functional Mockup Unit (FMU) from a Simulink model that uses variable step solver. Export Simulink model to FMU that supports Co-Simulation in FMI version 2.0. Simulating FMU with variable step solver requires MATLAB or MATLAB Runtime to be installed on the deployment machine.

Export Simulink Model to FMU

Open Simulink model MechanicalSystemWithTranslationalFriction. This model uses daessc as the default variable step solver. For a detailed explanation of the model, see: Mechanical System with Translational Friction.

% this model is installed with MATLAB examples and located at matlab/examples/simscape/main/internal

% save system, export to FMU with variable step solver requires model write permission
save_system('MechanicalSystemWithTranslationalFriction', 'mechanical_system_local', 'OverwriteIfChangedOnDisk',true);


From Simulation tab, click drop-down button for Save. In Export Model To section, click Standalone FMU... in FMU Export dialog, configure wrapper model and icon settings, and specify save location for generated FMU.

Configure the settings and Click Create to export to FMU. After the process, the mechanical_system_local.fmu file can be found at specified save location.


Export Simulink Model to FMU via Command Line

Export this model to FMU with command exportToFMU2CS. To learn more about about this function, see exportToFMU2CS.

% This function will generate the FMU and a harness model in the current directory.
exportToFMU2CS('mechanical_system_local', 'CreateModelAfterGeneratingFMU', 'on');

Simulate FMU with Variable Step Solver

Simulate FMU within Simulink

Open harness model mechanical_system_local_harness and run simulation. You can observe the result by adding a Scope block.



Simulate FMU with external tools

To run the deployed FMU, you need an appropriate runtime environment. To install and configure the MATLAB Runtime, see Install and Configure MATLAB Runtime.

Use the following command to download MATLAB Runtime. Alternatively, use the link:

The default MATLAB Runtime Installation Directory is:

Windows   C:\Program Files\MATLAB\MATLAB Runtime\R2023b
Linux     /usr/local/MATLAB/MATLAB_Runtime/R2023b

After installation is complete, use the following command to set environment variables on the deployment machine:

set InstallationFolder='MATLAB runtime Path here'
set PATH=%PATH%;%InstallationFolder%\bin\win64;%InstallationFolder%\extern\bin\win64

% in linux shell terminal
export InstallationFolder='MATLAB runtime Path here'
export LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:${InstallationFolder}/bin/glnxa64:${InstallationFolder}/extern/bin/glnxa64

% in linux csh terminal
setenv InstallationFolder 'MATLAB runtime Path here'
setenv LD_LIBRARY_PATH ${LD_LIBRARY_PATH}:${InstallationFolder}/bin/glnxa64:${InstallationFolder}/extern/bin/glnxa64

The following steps uses FMU Compliance Checker to validate the generated FMU. For information about FMU Compliance checker, see FMI Compliance Checker Git repo.

MATLAB Runtime in Environment Variable

To run FMU with FMU Compliance Checker, open Command Prompt and go to the location where FMU Compliance Checker is installed. Set Environment Variable with the command illustrated in the previous section.

Run FMU with the following command and observe output in terminal.

% on winodws
FMUCheck.win64 mechanical_system.fmu

% on linux
./FMUCheck.linux64 mechanical_system.fmu