Some blocks maintain state information that they use during a simulation. For example, the Unit Delay block uses the current state of the block to calculate the output signal value for the next simulation time step.
Subsystem blocks have default initialize and termination routines. You can add custom routines to the default routines using Initialize Function and Terminate Function blocks to change or read block states. These function blocks contain:
Event Listener blocks that execute the combined routines when receiving an initialize or terminate function-call event.
State Writer blocks to initialize the block state and State Reader blocks to read the state.
You can define model algorithms using Simulink® blocks. In this example, a single Discrete-Time Integrator block defines the algorithm for integrating an input signal.
Open a new Simulink model. Save this model with the name
Model01
.
Add a Discrete-Time Integrator block. Verify the default parameter
values are 1.0
for Gain value,
0
for Initial condition, State
(most efficient)
for Initial condition setting, and
-1
for Sample time.
Connect a Constant block to the input of the Discrete-Time Integrator block to model an input signal. Connect a Scope block to the output signal.
Open the Configuration Parameters dialog box. Set the simulation parameters for the
Solver Type to Fixed-step
,
Solver to auto
, and
Fixed-step size to 1
.
Open the Scope block, and then run simulation. The output signal
increases by 1
at each time step.
Some model algorithms contain states that you can initialize. For example, with an algorithm that reads a sensor value, you can perform a computation to set the initial sensor state.
At the beginning of a simulation, initialize the state of a block using a State Writer block. To control when initialization occurs, use an Initialize Function block that includes the State Writer block.
Add an Initialize Function block.
By default, the Initialize Function block includes an Event
Listener block with the Event type parameter set to
Initialize
. The block also includes a State
Writer block, and a Constant block as a placeholder for the
source of the initial state value.
Model initial conditions. In this example, set the Constant
value parameter for the Constant block to
4
.
Connect the state writer with the state owner. Open the State Writer dialog box.
Expand the State Owner Selector Tree, select Discrete-Time
Integrator
, and then click Apply.
The State Writer block displays the name of the state owner block. The state owner block displays a tag indicating a link to a State Writer block. If you click the label above the tag, a list opens with a link for navigating to the State Writer block.
Run simulation to confirm that your model simulates without errors.
The Initialize Function block executes at the beginning of a
simulation. The output signal starts with and initial value of 4
and
then increases by 1
until the end of the simulation.
During a simulation, you can reset the state of a block using a State Writer block. To control when reset occurs, use an Initialize Function block that you reconfigure to a Reset Function block.
Add an Initialize Function block.
Open the new Initialize Function block.
Configure block for reset. Open the Block Parameter dialog box for the Event
Listener block. From the Event type drop-down list,
select Reset
. In the Event name box,
enter an event name. For example, enter reset
. Close the dialog
box.
Model reset conditions. In this example, set the Constant value
parameter for the Constant block to 2
.
Connect state writer with the state owner. Open the State Writer dialog box. Expand
the State Owner Selector Tree, select Discrete-Time
Integrator
, and then click Apply.
Navigate to the top level of Model01
. Rename the block from
Initialize Function1
to Reset Function
.
After updating your model, the event name for the Reset Function block is displayed on the face of the block.
If you click above the tag, a list opens with a link for navigating to the State Writer blocks located in the Initialize Function block and the Reset Function block.
Run a simulation to confirm that your model simulates without errors.
The Reset Function block does not execute during the simulation. It needs an function-call event signal.
To create an function-call event signal for the Reset Function block, see Create Test Harness to Generate Function Calls.
At the end of a simulation, you can read the state of a block, and save that state.
Add a Terminate Function block.
By default, the Terminate Function block includes an Event
Listener block with the parameter Event type set to
Terminate
. The block also includes a State
Reader block, and a Terminator block as a placeholder for
saving the state value.
Connect the state reader with the state owner. Open the State Reader dialog box.
From the State Owner Selector Tree, select Discrete-Time
Integrator
, and then click Apply.
Run a simulation to confirm that your model simulates without errors. The Terminate Function block executes at the end of a simulation.
Delete the blocks that you added for testing. Replace the Constant block with an Inport block and the Scope block with an Outport block.
Make the following changes to avoid simulation errors when the component model is placed in an export-function model for simulation testing.
Open the Block Parameters dialog box for the Discrete-Time Integrator
block. Set Integrator method to
Accumulation:Forward Euler
.
Open the Model Configuration Parameters dialog box. Confirm the solver
Type is set to Fixed-step
and
Solver is set to auto
. Change the
Fixed-step size from 1
to
auto
.
This change avoids a simulation error caused by having multiple sample times in a Function-Call Subsystem.
Placing a model component in a test harness for testing the initialize, reset, and terminate functions requires the model to follow export-function rules. See Export-Function Models Overview and Create Test Harness to Generate Function Calls.
To create an export-function model, place the model component in a Function-Call Subsystem block using a Model block. Connect input and output ports from the model to the subsystem input and output ports.
Create a Simulink model. Save this model with the name Model02
.
Open the Configuration Parameters dialog box. Set the simulation parameter for the
Solver Type to Fixed-step
. Confirm
Solver is set to auto
and
Fixed-step size is set to auto
.
Add a Function-Call Subsystem block. Open the subsystem by double-clicking the block.
Add a Model block to the subsystem and set Model
name to Model01
. Add Inport and
Outport blocks.
Navigate to the top level of the model.
Add an Inport block. This block is the control signal for executing
the subsystem. Change the block name to Run
and connect it to the
function()
port.
Open the Inport block dialog box and on the Signal Attributes tab, select the Output function call check box.
Add a second Inport block and rename it to Signal
In
. Connect it to the In1
port of the subsystem. This
block is the signal for the integration algorithm.
Add an Outport block, rename it to Signal Out
, and
then connect it to the Out1
port of the subsystem. This block is the
integrated signal.
Open the Configuration Parameters dialog box. On the Model Referencing pane, set the
Total number of instances allowed per top model to
one
.
Update your model and confirm that there are no errors by pressing Ctrl-D.
The next step is create a test harness. See Create Test Harness to Generate Function Calls.