Component-Based Modeling with Model Reference
This example takes 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™ software generates code that you can deploy in standalone applications.
Model reference has several advantages over subsystems.
You can reference a model in another model without making redundant copies.
Multiple models can reference the same model.
The software does not load a referenced model until the model is needed. This process is called incremental loading. Incrementally loading models speeds up model load times.
If a model simulates in accelerator mode, the software creates binaries to use in simulations. If the referenced model is unchanged after the binaries are created, no code generation occurs when you simulate or compile the models that use the binaries. This process is called incremental code generation. Using binaries improves model update and simulation performance and increases modularity in code generation.
Generating code for a model with Model blocks also takes advantage of incremental code generation.
For help defining and choosing among model components, see Component-Based Modeling Guidelines.
Incremental Loading
Open the ModelReference
project. The model named sldemo_mdlref_basic
also opens because the project is configured to open this model at startup.
openProject("ModelReference");
The sldemo_mdlref_basic
model contains Model blocks named CounterA
, CounterB
, and CounterC
. Each Model block references the sldemo_mdlref_counter
model. The referenced model is a standalone model and not a subsystem of sldemo_mdlref_basic
.
To determine what model, library, and subsystem files are loaded in memory, use the find_system
function.
find_system("type","block_diagram")
ans = 2x1 cell
{'SignalEditorBlockLib'}
{'sldemo_mdlref_basic' }
The sldemo_mdlref_basic
model is listed because open models are loaded in memory.
The SignalEditorBlockLib
library is listed because the sldemo_mdlref_basic
model contains Signal Editor blocks that are linked to this library.
The referenced model is not listed because it is not loaded.
To open the referenced model, double-click a Model block.
Alternatively, to load the referenced model without opening it, use the load_system
function.
load_system("sldemo_mdlref_counter")
Query the models loaded in memory again.
find_system("type","block_diagram")
ans = 3x1 cell
{'sldemo_mdlref_counter'}
{'SignalEditorBlockLib' }
{'sldemo_mdlref_basic' }
The referenced model is now listed, indicating that models are loaded incrementally as they are needed.
Inherited Sample Times
Navigate back to the top model.
The sldemo_mdlref_basic
model is configured to display sample time colors when compiled. In the Simulink Toolstrip, on the Debug tab, click Update Model. Alternatively, enter this command.
set_param("sldemo_mdlref_basic",SimulationCommand="Update")
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 License)
When the top model simulates in Normal
mode, the Model block Simulation mode parameter controls how the referenced model simulates.
When the Model block Simulation mode parameter is set to
Normal
, the referenced model simulates in interpreted mode.When the Model block Simulation mode parameter is set to
Accelerator
, the referenced model simulates through code generation. This process uses a binary file called a simulation target for each unique model that simulates 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 white and gray corners on the Model block icons. The other instance, CounterC
, references sldemo_mdlref_counter
in accelerator mode, which is indicated by the black corners on the Model block icon.
To build the simulation target for the sldemo_mdlref_counter
model, update or simulate sldemo_mdlref_basic
. On the Modeling tab, click Update Model or Run. Alternatively, to build a simulation target for a specific referenced model, use the slbuild
function.
slbuild("sldemo_mdlref_counter","ModelReferenceSimTarget")
### Searching for referenced models in model 'sldemo_mdlref_counter'. ### Found 1 model reference targets to update. ### Starting serial model reference simulation build. ### Model reference simulation target for sldemo_mdlref_counter is up to date. Build Summary 0 of 1 models built (1 models already up to date) Build duration: 0h 0m 3.1971s
Once the simulation target builds, subsequently simulating or updating sldemo_mdlref_basic
does not trigger a rebuild of the simulation target for sldemo_mdlref_counter
, unless sldemo_mdlref_counter
has changed.
If all three instances of the referenced model are set to simulate in normal mode, the simulation target does not build.
For help choosing simulation modes, see Choose Simulation Modes for Model Hierarchies.
Code Generation for Standalone Applications (Requires Simulink Coder License)
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.
To build the standalone executable for sldemo_mdlref_basic
and the model reference coder target for sldemo_mdlref_counter
, on the Apps tab, click Simulink Coder. On the C Code tab that opens, click Build. Alternatively, press Ctrl+B or enter this command.
slbuild("sldemo_mdlref_basic")
### Searching for referenced models in model 'sldemo_mdlref_basic'. ### Found 1 model reference targets to update. ### Starting serial model reference code generation build. ### Successfully updated the model reference code generation target for: sldemo_mdlref_counter ### Starting build procedure for: sldemo_mdlref_basic ### Successful completion of build procedure for: sldemo_mdlref_basic Build Summary Model reference code generation targets: Model Build Reason Status Build Duration ==================================================================================================================== sldemo_mdlref_counter Target (sldemo_mdlref_counter.c) did not exist. Code generated and compiled. 0h 0m 13.561s Top model targets: Model Build Reason Status Build Duration ====================================================================================================================== sldemo_mdlref_basic Information cache folder or artifacts were missing. Code generated and compiled. 0h 0m 17.811s 2 of 2 models built (0 models already up to date) Build duration: 0h 0m 34.339s
When the model reference coder target for sldemo_mdlref_counter
already exists, subsequently building sldemo_mdlref_basic
does not trigger a rebuild of the model reference coder target unless sldemo_mdlref_counter
has changed. Instead, the code generated for sldemo_mdlref_counter
is reused.
A code generation report for sldemo_mdlref_basic
opens. To view the code generation report for sldemo_mdlref_counter
, click the arrow next to Current model. Then, select sldemo_mdlref_counter
.