Main Content

Component-Based Modeling Using Model Reference

This example walks you through simulation and code generation of a model that references another model multiple times. In this example, Simulink® software generates code for accelerated simulation, and Simulink® Coder™ generates code that you can deploy in standalone applications.

Model reference has several advantages over subsystems.

  • You can independently develop two models: the model referenced and the top model that references it.

  • Multiple models can reference a single model, so you can reference a model in another model without making redundant copies.

  • The referenced model is not loaded until it is needed. Incrementally loading models speeds up model load times.

  • If a model is referenced in accelerator mode, the software creates special binaries to use in simulations. If the referenced model is not changed after the binaries are created, and the binaries are up to date, no code generation occurs when you simulate or compile models that use the binaries. This process is called incremental code generation. Using binaries increases model update and simulation performance, and improves modularity in the generated code. Generating code for a model with Model blocks also takes advantage of incremental code generation.

Model Reference Project

To open the project, click Open Project. Enter this command.

openProject('ComponentBasedModelingUsingModelReference');

On the MATLAB® Toolstrip, on the Project Shortcuts tab, the Model and Reference Models sections provide the models and referenced models in the project. For this example, the project contains the top model sldemo_mdlref_basic and model referenced sldemo_mdlref_counter.

Incremental Loading

Open the model sldemo_mdlref_basic. On the Toolstrip, click sldemo_mdlref_basic or enter this command.

open_system('sldemo_mdlref_basic')

The sldemo_mdlref_basic model contains three Model blocks: CounterA , CounterB and CounterC. The Model blocks reference the same model reference, sldemo_mdlref_counter. The model referenced is a separate model and not a subsystem of sldemo_mdlref_basic.

To determine what models with blocks are loaded in memory, use the find_system function. By default, the project has SignalEditorBlockLib loaded in memory.

find_system('type','block_diagram')
ans =

  2x1 cell array

    {'SignalEditorBlockLib'}
    {'sldemo_mdlref_basic' }

The model referenced is not listed because it is not loaded.

Open the model referenced by clicking on any Model block or entering this command.

open_system('sldemo_mdlref_counter')

Query the models loaded in memory again.

find_system('type','block_diagram')
ans =

  3x1 cell array

    {'sldemo_mdlref_counter'}
    {'SignalEditorBlockLib' }
    {'sldemo_mdlref_basic'  }

The model referenced is listed, indicating that models are loaded incrementally as they are needed.

Inherited Sample Times

Navigate back to the parent model sldemo_mdlref_basic.

The sldemo_mdlref_basic model is configured to display sample time colors when it is compiled. On the Debug tab, click Update Model.

The Model blocks inherit different sample times because the referenced model, sldemo_mdlref_counter, does not explicitly specify a sample time.

Simulation Through Code Generation (Does Not Require Simulink Coder)

Model blocks have a Simulation mode parameter that controls how the referenced model is simulated. If the parameter is set to Normal, the referenced model is simulated in interpreted mode. If the parameter is set to Accelerator, the referenced model is simulated through code generation. This process uses a binary file called a simulation target for each unique model referenced in accelerator mode. Generating a simulation target does not require a Simulink Coder license.

In this model, CounterA and CounterB reference sldemo_mdlref_counter in normal mode, which is indicated by the hollow corners on the Model block icons. The other instance, CounterC, references sldemo_mdlref_counter in accelerator mode, which is indicated by the filled corners on the Model block icon.

You can create the simulation target for the sldemo_mdlref_counter model by performing any of these actions:

  • Updating sldemo_mdlref_basic

  • Simulating sldemo_mdlref_basic

To build the simulation target programmatically, use this command.

slbuild('sldemo_mdlref_counter','ModelReferenceSimTarget')

Once the simulation target is built, subsequently simulating or updating sldemo_mdlref_basic does not trigger a rebuild of the simulation target, unless sldemo_mdlref_counter has changed.

If all three instances of the referenced model are set to simulate in normal mode, the simulation target will not build.

Code Generation for Standalone Applications (Requires Simulink Coder)

When creating a standalone executable for sldemo_mdlref_basic, the build first generates the code and binaries for the model reference coder target of sldemo_mdlref_counter. Generating a model reference coder target requires a Simulink Coder license.

You can build the model reference coder target for sldemo_mdlref_counter and the standalone executable for sldemo_mdlref_basic by performing any of these actions:

  • Building the standalone executable for sldemo_mdlref_basic.

  • Building the model reference coder target of sldemo_mdlref_counter, then building the standalone executable for sldemo_mdlref_basic.

To build the standalone executable programmatically, use this command.

slbuild('sldemo_mdlref_basic','StandaloneCoderTarget')

Once the model reference coder target is built, subsequently building sldemo_mdlref_basic does not trigger a rebuild of the model reference coder target unless sldemo_mdlref_counter has changed. The code generated for the referenced model sldemo_mdlref_counter is reused.

The code generation report for sldemo_mdlref_basic links to the report for sldemo_mdlref_counter in the Referenced Models section.

See Also

|

Related Topics