# Load Data to Represent Variable-Step Input from Model Hierarchy

A model hierarchy can include discrete and continuous components that operate with different dynamics. As you design such a system, typically you simulate the model hierarchy using a variable-step solver to capture both the discrete and continuous dynamics in the hybrid system.

As you develop each component, you can focus on the design for a single component by simulating the referenced model that implements the component as a top model. Instead of the input data coming from the parent model, the input ports on the model interface load external input data you specify using the **Input** model configuration parameter.

This example shows how you can use the **Output times** model configuration parameter to ensure such a simulation captures dynamics of continuous input signals logged from a variable-step simulation.

### Open Top Model

The model for this example contains a continuous plant and a discrete algorithm that counts the number of oscillations in the plant step response that reach a value of `1.5`

or higher. The top model uses a variable-step solver to capture the continuous dynamics of the plant.

A Step block provides the step input for the continuous plant, which is implemented using a Transfer Fcn block. The continuous plant output signal connects to an Outport block and to the referenced model that implements the discrete counting algorithm. The oscillation count output from the discrete algorithm connects to a second Outport block in the top model.

Open the model `ContinuousPlant`

.

```
mdl = "ContinuousPlant";
open_system(mdl)
```

### Simulate Top Model

Suppose you want to test and refine the model `CountOscillations`

in isolation. You can use data logged from a simulation of the top model as input data when you simulate the referenced model as a top model.

Simulate the top model. On the Simulink® Toolstrip, on the **Simulation** tab, click **Run**. Alternatively, use the `sim`

function.

out = sim(mdl);

Get the logged data for the two outputs `Step Response`

and `Oscillation Count`

.

yout = out.yout; stepResp = getElement(yout,"Step Response"); oscCount = getElement(yout,"Oscillation Count");

Plot the plant step response. The plot shows that the step response is underdamped and the algorithm counts `7`

oscillations that go above a value of `1.5`

.

figure(1) subplot(2,1,1) plot(stepResp.Values) subplot(2,1,2) plot(oscCount.Values)

### Simulate Referenced Model as Top Model

Open the model `CountOscillations`

as a top model.

```
mdl2 = "CountOscillations";
open_system(mdl2)
```

Configure the model to load the data in the variable `stepResp`

as simulation input.

In the Simulink Toolstrip, on the

**Modeling**tab, click**Model Settings**.Select the

**Data Import/Export**pane.Select

**Input**.In the

**Input**field, enter`stepResp`

.Click

**OK**.

Alternatively, create a `Simulink.SimulationInput`

object for the referenced model. Then, use the `setExternalInput`

function to specify the external input for the simulation as the variable `stepResp`

.

```
simIn = Simulink.SimulationInput(mdl2);
simIn = setExternalInput(simIn,"stepResp");
```

To see how the model loads the input data, mark the input signal for logging. Select the signal `Step Response`

. Then, in the **Simulation** tab, click **Log Signals**.

Alternatively, you can mark the signal for logging using the `Simulink.sdi.markSignalsForStreaming`

function.

blkPth = strcat(mdl2,"/Signal"); Simulink.sdi.markSignalForStreaming(blkPth,1,"on")

Simulate the model.

out2 = sim(simIn);

Get the data for the logged input signal and the oscillation count.

logsout = out2.logsout; stepRespInput = getElement(logsout,"Step Response"); yout2 = out2.yout; oscCount2 = getElement(yout2,"Oscillation Count");

Plot the input signal and the oscillation count. The response appears no longer underdamped, and the algorithm counts zero oscillations due to the way the simulation loaded the step response data.

figure(2) subplot(2,1,1) plot(stepRespInput.Values) subplot(2,1,2) plot(oscCount2.Values)

When you simulate the referenced model as a top model, the solver is aware of the step response dynamics only after the Inport block loads the input data into the model. Because the signal had continuous sample time in a variable-step simulation, the samples are not evenly spaced. To load the data in a way that accurately reflects the step response, the Inport block needs to load more input values from the step response data.

### Specify Additional Output Times for Simulation

You can use the **Output times** model configuration parameter to force the variable-step solver to take time steps at specified values in addition to those the solver determines. You can use the time values in the logged data from the first simulation to specify the value of the **Output times** parameter.

For this example, because the oscillations in the step response continue throughout the simulation, specify the value of the **Output times** parameter as the entire time vector for the input data.

Get the vector of time values.

outTimes = stepResp.Values.Time;

Specify the **Output times** parameter value as the variable `outTimes`

.

In the Simulink Toolstrip, on the

**Modeling**tab, click**Model Settings**.Select the

**Data Import/Export**tab.Expand

**Additional parameters**.Set

**Output options**to`Produce additional output`

.In the

**Output times**field, type`outTimes`

.Click

**OK**.

Alternatively, you can configure the parameter values for the simulation using the `SimulationInput`

object.

simIn = setModelParameter(simIn,... "OutputOption","AdditionalOutputTimes",... "OutputTimes","outTimes");

Simulate the model again.

out3 = sim(simIn);

Get the data for the logged input signal and the oscillation count.

logsout2 = out3.logsout; stepRespInput2 = getElement(logsout2,"Step Response"); yout3 = out3.yout; oscCount3 = getElement(yout3,"Oscillation Count");

Plot the logged input signal and the oscillation count. Now, the plots look the same as those from the first simulation of the entire model hierarchy.

figure(3) subplot(2,1,1) plot(stepRespInput2.Values) subplot(2,1,2) plot(oscCount3.Values)

Depending on the requirements for your system, you can specify the value for the **Output times** parameter using the time vector for the entire input signal or using one or more relevant portions of the input signal time data.

## See Also

### Blocks

### Model Settings

### Functions

`Simulink.sdi.markSignalForStreaming`

|`timeseries`

|`tsdata.event`

|`gettsbeforeevent`

|`gettsafterevent`