Main Content

Using Initialize, Reinitialize, Reset, and Terminate Functions

Some blocks maintain state information that they use during a simulation. For example, theUnit Delayblock uses the current state of the block to calculate the output signal value for the next simulation time step.

Subsystemblocks have default initialize and termination routines. You can add custom routines to the default routines usingInitialize Function,Reinitialize Function, andTerminate Functionblocks to change or read block states. You can also implement custom routines usingReset Functionblocks. These function blocks contain:

  • An事件监听器block that executes the combined routine when receiving an initialize, reinitialize, or terminate function-call event, or the custom routine when receiving a reset function-call event.

  • AState Writerblock to initialize or set a block state or aState Readerblock to read the state.

To define the custom routine, edit the contents of the block. For example:

  • Set theState owner blockparameter of theState WriterorState Readerblock.

  • Add additionalState WriterorState Readerblocks.

  • AddParameter Writerblocks to set the value of block parameters. SeeInitialize and Reset Parameter Values.

  • AddData Store Writeblocks to write orData Store Readblocks to read data in a data store. SeeModel Global Data by Creating Data Stores.

  • Add blocks and signal lines to supply the values forState Writer,Parameter Writer, andData Store Writeblocks to write, or to save the values read byState ReaderorData Store Readblocks.

Create Model Component with State

You can define model algorithms using Simulink®blocks. In this example, a singleDiscrete-Time Integratorblock defines the algorithm for integrating an input signal.

  1. Open a new Simulink model. Save this model with the nameModel01.

  2. Add aDiscrete-Time Integratorblock. Verify the default parameter values are1.0forGain value,0forInitial condition,AutoforInitial condition setting, and-1forSample time.

  3. Connect aConstantblock to the input of theDiscrete-Time Integratorblock to model an input signal. Connect aScopeblock to the output signal.

  4. Open the Configuration Parameters dialog box. Set the simulation parameters for the SolverTypetoFixed-step,Solvertoauto, andFixed-step sizeto1.

  5. Open theScopeblock, and then run simulation. The output signal increases by1at each time step.

Initialize Block State

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 aState Writerblock. To control when initialization occurs, use anInitialize Functionblock that includes theState Writerblock.

  1. Add anInitialize Functionblock.

    通过default, theInitialize Functionblock includes an事件监听器block with theEvent typeparameter set toInitialize. The block also includes aState Writerblock, and aConstantblock as a placeholder for the source of the initial state value.

  2. Model initial conditions. In this example, set theConstant value参数的Constantblock to4.

  3. Connect the state writer with the state owner. Open the State Writer dialog box. Expand the State Owner Selector Tree, selectDiscrete-Time Integrator,然后单击Apply.

    TheState Writerblock displays the name of the state owner block. The state owner block displays a tag indicating a link to aState Writerblock. If you click the label above the tag, a list opens with a link for navigating to theState Writerblock.

  4. 运行仿真to confirm that your model simulates without errors.

    TheInitialize Functionblock executes at the beginning of a simulation. The output signal starts with and initial value of4and then increases by1until the end of the simulation.

Reset Block State

During a simulation, you can reset the state of a block using aState Writerblock. To control when reset occurs, use aReset Functionblock.

Note

You can use either aReinitialize Functionblock or aReset Functionblock to reset the state of a block. Both theReinitialize Functionblock and theReset Functionblocks can write to block states or perform whatever actions you specify when you configure the blocks. The difference between the blocks is that aReinitialize Functionfirst implicitly resets the states of all blocks in the model back to their initial conditions and then carries out whatever other actions, including writing to block states, you specify when you configure the block. By contrast, aReset Functionblock, carries out only the actions that you specify when you configure the block. In this example, since the model contains no other blocks with state besides theDiscrete-Time Integratorblock, aReinitialize Functionblock could be used in place of theReset Functionblock with no difference in results.

  1. Add aReset Functionblock.

  2. Open theReset Functionblock.

  3. Model reset conditions. In this example, set theConstant value参数的Constantblock to2.

  4. Connect state writer with the state owner. Open theState Writerblock dialog box. Expand the State Owner Selector Tree, selectDiscrete-Time Integrator,然后单击Apply.

  5. The event name for theReset Functionblock,reset, is displayed on the face of the block.

    If you click above the tag, a list opens with a link for navigating to theState Writerblocks located in theInitialize Functionblock and theReset Functionblock.

    Note

    If theReset Functionblock were replaced by aReinitialize Functionblock, then theReinitialize Functionblock would first set the state of theDiscrete-Time Integratorblock back to its initial condition of 0, as specified in theDiscrete-Time Integratorblock dialog. TheReinitialize Functionblock would then use theState Writerblock to set the state of theDiscrete-Time Integratorblock to 2, which would override the implicit reinitialization to 0. This sequence of actions has the same effect as theReset Functionblock, which simply sets the state of theDiscrete-Time Integratorblock to 2.

    If aReinitialize Functionblock were used without aState Writerblock, then it would set the state of theDiscrete-Time Integratorblock to its initial condition of 0. If aReset Functionblock were used without aState Writerblock, it would have no effect on the state of any other block.

  6. Run a simulation to confirm that your model simulates without errors.

    TheReset Functionblock does not execute during the simulation. The block needs a function-call event signal.

    To create a function-call event signal for theReset Functionblock, seeCreate Test Harness to Generate Function Calls.

Read and Save Block State

At the end of a simulation, you can read the state of a block, and save that state.

  1. Add aTerminate Functionblock.

    通过default, theTerminate Functionblock includes an事件监听器block with the parameterEvent typeset toTerminate. The block also includes aState Readerblock, and aTerminatorblock as a placeholder for saving the state value.

  2. Connect the state reader with the state owner. Open theState Readerblock dialog box. From the State Owner Selector Tree, selectDiscrete-Time Integrator,然后单击Apply.

  3. Run a simulation to confirm that your model simulates without errors. TheTerminate Functionblock executes at the end of a simulation.

  4. Delete the blocks that you added for testing. Replace theConstantblock with anInportblock and theScopeblock with anOutportblock.

Prepare Model Component for Testing

Make the following changes to avoid simulation errors when the component model is placed in an export-function model for simulation testing.

  1. Open the Block Parameters dialog box for theDiscrete-Time Integratorblock. SetIntegrator methodtoAccumulation:Forward Euler.

  2. Open the Model Configuration Parameters dialog box. Confirm the solverTypeis set toFixed-stepandSolveris set toauto. Change theFixed-step sizefrom1toauto.

    This change avoids a simulation error caused by having multiple sample times in a Function-Call Subsystem.

Create an Export-Function Model

Placing a model component in a test harness for testing the initialize, reset, and terminate functions requires the model to follow export-function rules. SeeExport-Function Models OverviewandCreate Test Harness to Generate Function Calls.

To create an export-function model, place the model component in aFunction-Call Subsystemblock using aModelblock. Connect input and output ports from the model to the subsystem input and output ports.

  1. Create a Simulink model. Save this model with the nameModel02.

  2. Open the Configuration Parameters dialog box. Set the simulation parameter for the SolverTypetoFixed-step. ConfirmSolveris set toautoandFixed-step sizeis set toauto.

  3. Add aFunction-Call Subsystemblock. Open the subsystem by double-clicking the block.

  4. Add aModelblock to the subsystem and setModel nametoModel01. AddInportandOutportblocks.

  5. Navigate to the top level of the model.

  6. Add anInportblock. This block is the control signal for executing the subsystem. Change the block name toRunand connect it to thefunction()port.

    Open theInportblock dialog box and on theSignal Attributestab, select theOutput function callcheck box.

  7. Add a secondInportblock and rename it toSignal In. Connect it to theIn1port of the subsystem. This block is the signal for the integration algorithm.

    Add anOutportblock, rename it toSignal Out, and then connect it to the着干活port of the subsystem. This block is the integrated signal.

  8. Designate the model as an export-function model by going to the Property Inspector and setting the execution domain of the model toExport function. SeeDesignating an Export-Function Model.

  9. Open the Configuration Parameters dialog box. On theModel Referencingpane, set theTotal number of instances allowed per top modeltoone.

  10. Update your model and confirm that there are no errors by pressingCtrl-D.

The next step is create a test harness. SeeCreate Test Harness to Generate Function Calls.

See Also

Blocks

Related Topics