Export Simulink Models to Functional Mock-up Units

Export Models

Export Simulink® models to Functional Mock-up Unit (FMU) that supports Co-Simulation in FMI version 2.0. To check that the exported block is still a valid Simulink model, you can also direct the software to import the FMU back to a Simulink model as part of the export process.

Requirements include:

  • Simulink Compiler™

  • A writable folder into which to place the exported FMU.

Exported models can have:

  • Input and output data types: double, int32, boolean, string

  • Matrices

  • Bus Signals

  • Tunable Parameters. Parameter must refer to a global workspace variable.

Standalone FMU

Simulink models can be exported to Standalone Co-Simulation FMU in version 2.0. The generated FMU package contains following files:

  • modelDescription.xml

  • model.png (optional)

  • binaries\win64\modelname.dll, or binaries\linux64\modelname.so, or binaries\darwin64\modelname.dylib

You might experience an expected time delay in the exported FMU for Co-Simulation mode.

FMU Variables

FMU modelDescription.xml file contains interfacing variables converted from Simulink model:

  • Variables with causality=’input’: converted from root Inport block

  • Variables with causality=’output’: converted from root Outport block

  • Variables with causality=’parameter’: converted from referenced Runtime Tunable Parameters

  • Independent variable ‘time’

To generate FMU input and output, define root Inport and Outport block in Simulink model. Name of the generated variable is converted from root Inport/Outport block name, by removing special and blank characters and avoiding duplicates.

The following input/output data types are supported:

  • double (Real in FMI)

  • int32 (Integer in FMI)

  • boolean (Boolean in FMI)

  • string (String in FMI)

If model root Inport or Outport block is a non-virtual bus, individual bus elements will be expanded to variables using structured naming convention (‘.’). If model root Inport or Outport block, individual scalar elements will be expanded to variables using array naming convention (‘[]’).

To export referenced variables as FMU parameter, you can

  • define a variable. Use the variable in a Simulink block with tunable parameter, and make sure Code Generation > Optimization > Default parameter behavior is set to ‘Tunable’ (or model parameter ‘InlineParams’ is set to off)

  • define a Simulink Parameter object. Use the object in a Simulink block with tunable parameter, and make sure the Storage Class of the object is ‘ExportedGlobal’.

Name of the generated variable is converted from parameter name, by removing special characters and avoiding duplicates.

The following parameter data types are supported:

  • double (Real in FMI)

  • int32 (Integer in FMI)

  • boolean or logical (Boolean in FMI)

If referenced parameter is a struct, individual struct members will be expanded to variables using structured naming convention (‘.’). If referenced parameter is array or matrix, individual scalar elements will be expanded to variables using array naming convention (‘[]’).

FMU Solver

Fixed-step solvers are supported for standalone FMU export. It is recommended to set a fixed fundamental sample time (Solver > Solver details > Fixed-step size) before exporting the model. When simulating the Standalone FMU in other environment, communication step-size must be an integral multiple of the fundamental sample time.

FMU Dynamic Library

Generated FMU contains dynamic library build for the current platform. Default fmi2TypesPlatform value is used.

All required and optional fmi2 functions defined by FMI standard can be invoked. However, the following functions have no operation and return fmi2OK immediately:

  • Model-Exchange functions

  • Functions accessing or serializing FMUstate

  • Functions setting or getting input or output derivatives

  • Functions querying fmi2DoStep status, or cancelling fmi2DoStep

  • Function computing directional derivative of variables

Limitations

You cannot generate FMU from Simulink model, due to these limitations:

  • Variable-step solvers are not supported

  • Non-zero simulation start time are not supported

  • Simulink model that references external resources (data files, mex or m files) are not supported

Export a Simulink Model

Use the Export Dialog Box

Export the vdp example using Simulink toolstrip: Simulation > Save > Export Model ToStandalone FMU

  1. Open the model, vdp

  2. In Simulink editor, Simulink toolstrip: Simulation > Save > Standalone FMU.

  3. In Simulink editor, select Save> Export Model to> FMU Co-Simulation.

  4. In the export dialog box, specify the path to export the FMU.

  5. Click Create

    Simulink creates the FMU file, modelName.fmu and optionally the Simulink model,modelName_fmu.slx.

Use the Programmatic Interface

  • Export the vdp example to an FMU using the default exportToFMU2CS function. This command creates the FMU file, modelName.fmu. By default, it also creates a Simulink model, modelName_fmu.slx, that contains an FMU Co-Simulation block with the original model. Create this model if you want to check the integrity of the exported FMU.

    load_system('vdp')
    set_param('vdp', 'SolverType', 'Fixed-step')
    exportToFMU2CS('vdp')

  • Export the vdp example to an FMU using the exportToFMU2CS function, but do not create a Simulink model. This command creates the FMU file, modelName.fmu.

    load_system('vdp')
    set_param('vdp', 'SolverType', 'Fixed-step')
    exportToFMU2CS('vdp','CreateModelAfterGeneratingFMU','off')
    
  • Export the vdp example to an FMU using the exportToFMU2CS function. Create a model for the FMU and use an image of the original model as the block icon. This command creates the FMU file, modelName.fmu, and a Simulink model with an FMU Co-Simulation block whose block icon is the original model.

    exportToFMU2CS('vdp','AddIcon','snapshot')

See Also

| | | |

Related Topics