Generate Model Exchange FMU from Simulink Model
This example shows how to generate a standalone model exchange FMU, compatible with FMI 2.0 standards, from a Simulink® model. A model exchange FMU only contains the equations for the mathematical model of a system. It uses the solver of the FMU importer for solving its system equations during simulation.
Use a model exchange FMU when you want to control and specify simulation solver details in the FMU importer.
This example first generates a standalone model exchange FMU from a Simulink model. The generated FMU is then imported back into Simulink using the FMU Import block and simulated with different Simulink solvers.
Export Model to Standalone Model Exchange FMU
Open the f14_flightDynamics_forMEExport
model. This model represents the flight dynamics of aircraft.
flightDynamics_model = 'f14_flightDynamics_forMEExport.slx'; open_system('f14_flightDynamics_forMEExport.slx');
Use the exportToFMU
function to export the model to a standalone model exchange FMU. Specify the FMUTYpe
as ME
and FMI version
as 2.0
. Set a required name for the FMU file using the FMUName
argument.
exportToFMU('f14_flightDynamics_forMEExport', 'FMIVersion', '2.0', 'FMUType', 'ME', 'FMUName', 'f14_flightDynamics_ME', 'ExportedParameters',{''});
Setting System Target to FMU 'Model Exchange' for model 'f14_flightDynamics_forMEExport'. Setting Hardware Implementation > Device Type to 'MATLAB Host' for model 'f14_flightDynamics_forMEExport'. ### 'GenerateComments' is disabled for 'Model Exchange' FMU Export. Build Summary Top model targets: Model Build Reason Status Build Duration ================================================================================================================================= f14_flightDynamics_forMEExport Information cache folder or artifacts were missing. Code generated and compiled. 0h 0m 12.177s 1 of 1 models built (0 models already up to date) Build duration: 0h 0m 12.748s ### Model was successfully exported to 'Model Exchange' FMU: '/tmp/Bdoc24b_2704207_649653/tpe0d556fd/simulinkcompiler-ex50621501/f14_flightDynamics_ME.fmu'.
You can also use the FMU Export dialog to generate the model exchange FMU. In to the Simulation tab on the Simulink toolstrip, click drop-down button for Save. Select Standalone FMU..... This opens the FMU Export dialog. Open the Advanced tab within the dialog and select the Enable FMUState capability check box. Click Create to generate the FMU.
Import and Simulate FMU in Simulink
Open the f14_sim_model
Simulink model that imports the generated FMU into Simulink. This model integrates the flight dynamics, modeled by the FMU, with pilot inputs, wind model, and a controller.
model_for_sim = 'f14_sim_model.slx';
open_system(model_for_sim);
You can simulate the model with different Simulink solvers. The model exchange FMU is solved using the specified Simulink solver.
sim_fixedstep_euler = Simulink.SimulationInput(model_for_sim); sim_fixedstep_euler = setModelParameter(sim_fixedstep_euler, 'SolverType', 'fixed-step', 'SolverName', 'ode1','FixedStep','0.001', 'StopTime', '40'); sim_fixedstep_eluer_outdata = sim(sim_fixedstep_euler); sim_varstep = Simulink.SimulationInput(model_for_sim); sim_varstep = setModelParameter(sim_varstep, 'SolverType', 'variable-step', 'SolverName', 'ode113', 'StopTime', '40'); sim_varstep_outdata = sim(sim_varstep);
Observe that the FMU output for each of the specified solver.
plotData_angleofAttack = figure(1); plot(sim_fixedstep_eluer_outdata.yout{1}.Values, '-r', 'LineWidth', 1.5); hold on; plot(sim_varstep_outdata.yout{1}.Values, '--g', 'LineWidth', 1.5); hold on legend('Fixed-Step Solver', 'Variable Step Solver'); xlabel('Time (sec)') ylabel('Angle of Attack (rad)') grid minor;
plotData_pilotGForce = figure(2); plot(sim_fixedstep_eluer_outdata.yout{2}.Values, '-r', 'LineWidth', 1.5); hold on; plot(sim_varstep_outdata.yout{2}.Values, '--g', 'LineWidth', 1.5); hold on; legend('Fixed-Step Solver', 'Variable Step Solver'); xlabel('Time (sec)') ylabel('Pilot g Force (g)') grid minor;
Limitations
The following limitations apply for model exchange export.
Multitasking execution is not supported. You must set
EnableMultiTasking
configuration parameter for your model asoff
.Non-inlined S-Functions are not supported.
Nested FMUs are not supported. You must not have a model exchange FMU in the model or within a model reference while exporting a model as model exchange FMU.