Main Content

Conditionally Execute Custom Initialize, Reinitialize, Reset, and Terminate Routines

The custom routine blocks(Initialize Function, Reinitialize Function, Reset Function, and Terminate Function) blocks are preconfigured subsystem blocks that enable you to add custom routines to the default initialize, reset, and terminate routines to change or read block states. You can execute the custom routine conditionally by specifying variant conditions on the Event Listener block within custom routine blocks.

For example, consider a model algorithm that reads a sensor value. The Initialize Function block enables you to perform a computation to set the initial sensor state. To control when to reset the state of the sensor, use a Reset Function block. To save the state of the sensor at the end of the simulation, use a Terminate block. You can conditionally initialize, reset, or save the state of the sensor by specifying a variant condition for the Event Listener block within the Initialize, Reset, and Terminate function blocks. For more information, see Using Initialize, Reinitialize, Reset, and Terminate Functions.

Explore the Model

Consider the slexVariantIRTVariantCondProp model.

open_system("slexVariantIRTVariantCondProp")

The model contains a Model block ResetRefModel. The ResetRefModel block contains Initialize Function, Reset Function, and Terminate Function blocks to set the state of the Discrete-Time Integrator block. The Event Listener block within the Initialize, Reset, and Terminate Function blocks have the Enable variant condition option selected. The Variant control parameter of the Event Listener block is specified as V == 1. During simulation, the Function-Call Generator blocks send function-call signals to initialize, reset, and terminate ports at time step 2, 5, and 7 respectively.

When you simulate the model with V = 1, the ResetRefModel block propagates the variant condition, V == 1, that originates from the Reset Function blocks to the reset port. The block does not propagate the variant conditions from the Initialize and Terminate blocks. This makes the initialize and terminate ports unconditional because they control both the model default and block-specific initialize and terminate events of the referenced model. The Scope output shows that the signal is initialized to a custom value 5 at time step 2, reset to a custom value 10 at time step 5, and terminated at a custom time step 7.

V = 1;
sim("slexVariantIRTVariantCondProp");
open_system("slexVariantIRTVariantCondProp/Scope");

When you simulate the model with V = 2, the ResetRefModel block propagates the variant condition, V == 1, from the Reset Function block, thus making the blocks connected to the reset port inactive. The initialize and terminate ports are unconditional. The Scope output shows that the signal is initialized to a default value 0 at time step 2 and continues to increase by 1 until the simulation terminates at step 7.

V = 2;
sim("slexVariantIRTVariantCondProp");
open_system("slexVariantIRTVariantCondProp/Scope");

See Also

Propagate Variant Conditions to Define Variant Regions with Variant Blocks