Main Content

Export Simulink Model to FMU with Variable Step Solver

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

Export Simulink Model to FMU

Open the 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.

openExample('simscape/MechanicalSystemWithTranslationalFrictionExample','SupportingFile', 'MechanicalSystemWithTranslationalFriction.slx')

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

ModelImage.PNG

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

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

exportdialog.PNG

Export Simulink Model to FMU via Command Line

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

% This function will generate the FMU and a harness model in the current directory.
exportToFMU('mechanical_system_local', 'FMIVersion', '2.0', 'FMUType', 'CS', 'CreateModelAfterGeneratingFMU', 'on');

Simulate FMU with Variable Step Solver

Simulate FMU Within Simulink

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

open_system('mechanical_system_local_harness');

ExportModelToFMUWithVariableStepSolverExample_03.png

Simulate FMU with external tools

To run the deployed FMU, you need an appropriate runtime environment. To install and configure the MATLAB Runtime, see Download and Install MATLAB Runtime (MATLAB Compiler SDK).

Use the following command to download MATLAB Runtime. Alternatively, use the link: https://www.mathworks.com/products/compiler/matlab-runtime.html.

compiler.runtime.download

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:

Windows     
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 use 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 the 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 windows
FMUCheck.win64 mechanical_system.fmu

% on linux
./FMUCheck.linux64 mechanical_system.fmu

Capture2_modified.PNG