Main Content

Override Model Reference Simulation Modes

To override model reference simulation modes without dirtying any models:

  1. Confirm that the top model in the model hierarchy simulates in normal mode.

  2. Specify the override type by using the ModelReferenceSimulationModeOverride parameter of the top model.

  3. When the override affects only a subset of model references, specify the scope of the override by using the ModelReferenceSimulationModeOverrideScope parameter of the top model.

  4. Simulate the model hierarchy.

Supported ModelReferenceSimulationModeOverride override types include:

  • none (default) — Use the simulation modes specified by the Model blocks.

  • all-normal — Use normal mode for all Model blocks.

  • all-accelerator — Use accelerator mode for all Model blocks.

  • specified-models-to-normal — Use normal mode for the Model blocks that reference the models specified by the ModelReferenceSimulationModeOverrideScope model parameter.

  • specified-blocks-to-normal — Use normal mode for the Model blocks specified by the ModelReferenceSimulationModeOverrideScope model parameter.

Only one override type is supported at a time. When you set ModelReferenceSimulationModeOverride to none, all-normal, or all-accelerator, the simulation ignores the ModelReferenceSimulationModeOverrideScope parameter.

Model blocks that reference protected models do not support simulation mode overrides.

For a comparison of model reference simulation modes, see Choose Simulation Modes for Model Hierarchies.

Open Example

This example uses a model hierarchy that has multiple referenced models. Some referenced models simulate in normal mode, while others simulate in accelerator mode.

Open the project named ModelReferenceHierarchy.

openProject("ModelReferenceHierarchy");

A project startup task opens the sldemo_mdlref_depgraph model, which is the top model in the model hierarchy.

sldemo_mdlref_depgraph

Check Simulation Mode of Top Model

Confirm that the top model simulates in normal mode. When a top model simulates in normal mode, its children can simulate in normal or accelerator mode. When a top model simulates in accelerator or rapid accelerator mode, its children simulate in accelerator mode.

The ModelReferenceSimulationModeOverride parameter does not affect the simulation mode of the top model. When the top model simulates in accelerator or rapid accelerator mode, the ModelReferenceSimulationModeOverride parameter cannot override the model reference simulation modes.

Examine Model Reference Simulation Modes

To view the model hierarchy and the simulation mode of each model reference instance, use the Dependency Analyzer.

  1. In the Simulink Toolstrip, on the Modeling tab, in the Design gallery, click Dependency Analyzer.

  2. In the Dependency Analyzer toolstrip, in the Views gallery, select Model Instances.

The Dependency Analyzer displays the model hierarchy and the simulation mode of each model reference instance. While the model hierarchy references most of the models only once, the model hierarchy references the sldemo_mdlref_F2C model twice.

Dependency graph that displays model hierarchy

One instance of sldemo_mdlref_F2C simulates in normal mode. The other instance simulates in accelerator mode.

Determine How to Override Model Reference Simulation Modes

How you specify the ModelReferenceSimulationModeOverride and ModelReferenceSimulationModeOverrideScope model parameters depends on your goal. This example uses two strategies to specify these parameters for the top model in the model hierarchy:

  • To temporarily set the parameters during simulation, this example uses a Simulink.SimulationInput object.

  • To set the parameters during edit time and support the debugging programmatic interface, this example uses the set_param function.

Regardless of how you specify the ModelReferenceSimulationModeOverride and ModelReferenceSimulationModeOverrideScope parameters, these parameters do not dirty the model.

The override takes effect only during simulation. During edit time, the Model blocks do not display the overridden simulation modes.

Override Simulation Mode of Specified Model

Suppose you want all instances of sldemo_mdlref_F2C to simulate in normal mode. To override the simulation mode of sldemo_mdlref_F2C, you must also override the simulation modes of its parent models.

To get the models between the sldemo_mdlref_depgraph top model and the sldemo_mdlref_F2C referenced model, use the pathsToReferencedModel function.

topmodel = "sldemo_mdlref_depgraph";
refmodel = "sldemo_mdlref_F2C";
models = pathsToReferencedModel(topmodel,refmodel)
models = 4x1 cell
    {'sldemo_mdlref_depgraph'    }
    {'sldemo_mdlref_outdoor_temp'}
    {'sldemo_mdlref_heater'      }
    {'sldemo_mdlref_F2C'         }

Create a Simulink.SimulationInput object that overrides the simulation mode of sldemo_mdlref_F2C and its parent models to normal mode.

simConfigModels = Simulink.SimulationInput(topmodel);
simConfigModels = setModelParameter(simConfigModels,...
    ModelReferenceSimulationModeOverride="specified-models-to-normal",...
    ModelReferenceSimulationModeOverrideScope=models)
simConfigModels = 
  SimulationInput with properties:

               ModelName: "sldemo_mdlref_depgraph"
            InitialState: [0x0 Simulink.op.ModelOperatingPoint]
           ExternalInput: []
         ModelParameters: [1x2 Simulink.Simulation.ModelParameter]
         BlockParameters: [0x0 Simulink.Simulation.BlockParameter]
               Variables: [0x0 Simulink.Simulation.Variable]
               PreSimFcn: []
              PostSimFcn: []
              UserString: ''
    VariantConfiguration: ''

In the Simulink.SimulationInput object, the ModelParameters property stores the parameter settings.

simConfigModels.ModelParameters(1)
ans = 
  ModelParameter with properties:

     Name: "ModelReferenceSimulationModeOverride"
    Value: "specified-models-to-normal"

simConfigModels.ModelParameters(2)
ans = 
  ModelParameter with properties:

     Name: "ModelReferenceSimulationModeOverrideScope"
    Value: {4x1 cell}

Simulate the model hierarchy using the configuration stored in the Simulink.SimulationInput object.

sim(simConfigModels);
### Searching for referenced models in model 'sldemo_mdlref_depgraph'.
### Found 3 model reference targets to update.
### Starting serial model reference simulation build.
### Successfully updated the model reference simulation target for: sldemo_mdlref_heat2cost
### Successfully updated the model reference simulation target for: sldemo_mdlref_thermostat
### Successfully updated the model reference simulation target for: sldemo_mdlref_house

Build Summary

Model reference simulation targets:

Model                     Build Reason                                                 Status                        Build Duration
===================================================================================================================================
sldemo_mdlref_heat2cost   Target (sldemo_mdlref_heat2cost_msf.mexa64) did not exist.   Code generated and compiled.  0h 0m 21.276s 
sldemo_mdlref_thermostat  Target (sldemo_mdlref_thermostat_msf.mexa64) did not exist.  Code generated and compiled.  0h 0m 10.027s 
sldemo_mdlref_house       Target (sldemo_mdlref_house_msf.mexa64) did not exist.       Code generated and compiled.  0h 0m 10.271s 

3 of 3 models built (0 models already up to date)
Build duration: 0h 0m 55.853s

sldemo_mdlref_F2C and its parent models simulate in normal mode. Simulation does not check, update, or use the model reference simulation target for the models that simulate in normal mode.

Overriding the model reference simulation modes does not dirty any of the models.

Override Simulation Mode of Specified Block

Suppose all the referenced models in the model hierarchy are configured to simulate in accelerator mode. You want only the instance of sldemo_mdlref_F2C in sldemo_mdlref_heater to simulate in normal mode. To override the simulation mode of sldemo_mdlref_F2C in sldemo_mdlref_heater, you must also override the simulation mode of sldemo_mdlref_heater.

To get the Model blocks between the sldemo_mdlref_depgraph top model and the sldemo_mdlref_F2C referenced model, use the pathsToReferencedModel function.

topmodel = "sldemo_mdlref_depgraph";
refmodel = "sldemo_mdlref_F2C";
[models,blocks] = pathsToReferencedModel(topmodel,refmodel)
models = 4x1 cell
    {'sldemo_mdlref_depgraph'    }
    {'sldemo_mdlref_outdoor_temp'}
    {'sldemo_mdlref_heater'      }
    {'sldemo_mdlref_F2C'         }

blocks = 
  1x2 BlockPath array with properties:

    SubPath
    isLoadingModel
    isSavingModel

The function returns one Simulink.BlockPath object for each instance of the referenced model in the model hierarchy.

Get the Simulink.BlockPath object that corresponds to the instance of sldemo_mdlref_F2C in sldemo_mdlref_heater. Assign the Simulink.BlockPath object to a variable.

block1 = blocks(1)
block1 = 
  Simulink.BlockPath
  Package: Simulink

  Block Path:
    'sldemo_mdlref_depgraph/outdoor temp'
      'sldemo_mdlref_outdoor_temp/Fahrenheit to Celsius'

  Use the getBlock method to access block path character vectors from this object.
block2 = blocks(2)
block2 = 
  Simulink.BlockPath
  Package: Simulink

  Block Path:
    'sldemo_mdlref_depgraph/thermostat'
      'sldemo_mdlref_heater/Fahrenheit to Celsius'

  Use the getBlock method to access block path character vectors from this object.

The second Simulink.BlockPath object represents the Model block that references sldemo_mdlref_F2C in sldemo_mdlref_heater.

Create a Simulink.SimulationInput object that overrides the simulation mode of this Model block.

simConfigBlock = Simulink.SimulationInput(topmodel);
simConfigBlock = setModelParameter(simConfigBlock,...
    ModelReferenceSimulationModeOverride="specified-blocks-to-normal",...
    ModelReferenceSimulationModeOverrideScope=block2)
simConfigBlock = 
  SimulationInput with properties:

               ModelName: "sldemo_mdlref_depgraph"
            InitialState: [0x0 Simulink.op.ModelOperatingPoint]
           ExternalInput: []
         ModelParameters: [1x2 Simulink.Simulation.ModelParameter]
         BlockParameters: [0x0 Simulink.Simulation.BlockParameter]
               Variables: [0x0 Simulink.Simulation.Variable]
               PreSimFcn: []
              PostSimFcn: []
              UserString: ''
    VariantConfiguration: ''

In the Simulink.SimulationInput object, the ModelParameters property stores the parameter settings.

simConfigBlock.ModelParameters(1)
ans = 
  ModelParameter with properties:

     Name: "ModelReferenceSimulationModeOverride"
    Value: "specified-blocks-to-normal"

simConfigBlock.ModelParameters(2)
ans = 
  ModelParameter with properties:

     Name: "ModelReferenceSimulationModeOverrideScope"
    Value: [1x1 Simulink.BlockPath]

Simulate the model hierarchy using the configuration stored in the Simulink.SimulationInput object.

sim(simConfigBlock);
### Searching for referenced models in model 'sldemo_mdlref_depgraph'.
### Found 5 model reference targets to update.
### Starting serial model reference simulation build.
### Model reference simulation target for sldemo_mdlref_heat2cost is up to date.
### Model reference simulation target for sldemo_mdlref_thermostat is up to date.
### Model reference simulation target for sldemo_mdlref_house is up to date.
### Successfully updated the model reference simulation target for: sldemo_mdlref_F2C
### Successfully updated the model reference simulation target for: sldemo_mdlref_outdoor_temp

Build Summary

Model reference simulation targets:

Model                       Build Reason                                                   Status                        Build Duration
=======================================================================================================================================
sldemo_mdlref_F2C           Target (sldemo_mdlref_F2C_msf.mexa64) did not exist.           Code generated and compiled.  0h 0m 10.947s 
sldemo_mdlref_outdoor_temp  Target (sldemo_mdlref_outdoor_temp_msf.mexa64) did not exist.  Code generated and compiled.  0h 0m 12.374s 

2 of 5 models built (3 models already up to date)
Build duration: 0h 0m 26.404s

The specified instance of sldemo_mdlref_F2C simulates in normal mode. Its parent model, sldemo_mdlref_heater, also simulates in normal mode.

The other instance of sldemo_mdlref_F2C continues to simulate in accelerator mode. The simulation checks whether to update the model reference simulation target for sldemo_mdlref_F2C.

Overriding the model reference simulation modes does not dirty any of the models.

Override Simulation Modes of All Model Blocks

Suppose you want to simulate the entire model hierarchy in normal mode for debugging.

The debugging programmatic interface does not support Simulink.SimulationInput objects as input.

To set the override type, use the set_param function.

topmodel = "sldemo_mdlref_depgraph";
set_param(topmodel,...
    ModelReferenceSimulationModeOverride="all-normal");

When you set ModelReferenceSimulationModeOverride to all-normal, you do not specify the scope of the override.

To start the simulation in debug mode, enter this command.

sim(topmodel,Debug="on");

For information about debugging simulations programmatically, see Simulink Debugging Programmatic Interface.

All the referenced models simulate in normal mode. Simulation does not check, update, or use the model reference simulation targets for models that simulate in normal mode.

Overriding the model reference simulation modes does not dirty any of the models.

Optionally, remove the override after you finish debugging.

set_param(topmodel,...
    ModelReferenceSimulationModeOverride="none");

The override settings do not persist when you close the model or change MATLAB® sessions.

See Also

Related Topics