Main Content

Generate Code That Responds to Initialize, Reset, and Terminate Events

To generate code from a modeling component that responds to initialize, reset, and terminate events during execution, use the blocks Initialize Function and Terminate Function. For information on how to use these blocks, see Using Initialize, Reset, and Terminate Functions. You can use the blocks anywhere in a model hierarchy.

Examples of when to generate code that responds to initialize, reset, or terminate events include:

  • Starting and stopping a component.

  • Calculating initial conditions.

  • Saving and restoring state from nonvolatile memory.

  • Generating reset entry-point functions that respond to external events.

Each nonvirtual subsystem and referenced model can have its own set of initialize, reset, and terminate functions.

The code generator produces initialization and termination code differently than reset code. For initialization and termination code, the code generator includes your component initialization and termination code in the default entry-point functions, model_initialize and model_terminate. The code generator produces reset code only if you model reset behavior.

Generate Code for Initialize and Terminate Events

When you generate code for a component that includes Initialize Function and Terminate Function blocks, the code generator:

  • Includes initialize event code with default initialize code in entry-point function model_initialize.

  • Includes terminate event code with default terminate code in entry-point function model_terminate.

Consider the model rtwdemo_irt_base.

For this model, the code generator produces initialize and terminate entry-point functions that other code can interface with.

void rtwdemo_irt_base_initialize(void)
void rtwdemo_irt_base_terminate(void)

This code appears in the generated file rtwdemo_irt_base.c. The initialize function, rtwdemo_irt_base_initialize:

  • Initializes an error status.

  • Allocates memory for block I/O and state parameters.

  • Sets the output value

  • Sets the initial condition for the discrete integrator.

The terminate function, ,rtwdemo_irt_base_terminate, requires no code.

This code assumes that support for nonfinite numbers and MAT-file logging is disabled.

void rtwdemo_irt_base_initialize(void)
{
  rtmSetErrorStatus(rtwdemo_irt_base_M, (NULL));

  (void) memset((void *)&rtwdemo_irt_base_DW, 0,
                 sizeof(DW_rtwdemo_irt_base_T));   
      
  rtwdemo_irt_base_Y.Out1 = 0.0;

  rtwdemo_irt_base_DW.DiscreteIntegrator_DSTATE = 0.0;
}
	
void rtwdemo_irt_base_terminate(void)
{
  /* (no terminate code required) */
}

Add Initialize Function and Terminate Function blocks to the model (see rtwdemo_irt_initterm). The Initialize Function block uses the State Writer block to set the initial condition of a Discrete Integrator block. The Terminate Function block includes a State Reader block, which reads the state of the Discrete Integrator block.

Parameter Event type for the Event Listener block for the initialize and terminate functions is set to Initialize and Terminate, respectively. The initialize function uses the State Writer block to initialize the input value for the Discrete Integrator block to 10. The terminate function uses the State Reader block to read the state of the Discrete Integrator block.

The code generator includes the event code that it produces for the Initialize Function and Terminate Function blocks with standard initialize and terminate code in entry-point functions rtwdemo_irt_initterm_initialize and rtwdemo_irt_initterm_terminate. This code assumes that support for nonfinite numbers and MAT-file logging is disabled.

void rtwdemo_irt_initterm_initialize(void)
{
  rtmSetErrorStatus(rtwdemo_irt_initterm_M, (NULL));
		
  (void) memset((void *)&rtwdemo_irt__initterm_DW, 0,
	          sizeof(DW_rtwdemo_irt__initterm_T));
	
  rtwdemo_irt_initterm_Y.Out1 = 0.0;

  rtwdemo_irt_initterm_DW.DiscreteIntegrator_DSTATE = 10.0;
}

void rtwdemo_irt__initterm_terminate(void)
{
  /* (no terminate code required) */
} 

Generate Code for Reset Events

Generate code that responds to a reset event by including an Initialize Function or Terminate Function block in a modeling component. Configure the block for a reset by setting the Event type parameter of its Event Listener block to Reset. Also set the Event name parameter. The default name is reset.

The code generator produces a reset entry-point function only if you model reset behavior. If a component contains multiple reset specifications, the code that the code generator produces depends on whether reset functions share an event name. For a given component hierarchy:

  • For reset functions with unique event names, the code generator produces a separate entry-point function for each named event. The name of each function is the name of the corresponding event.

  • For reset functions that share an event name, the code generator aggregates the reset code into one entry-point function. The code for the reset functions appears in order, starting with the lowest level (innermost) of the component hierarchy and ending with the root (outermost). The name of the function is model_reset. For more information, see Event Names and Code Aggregation.

Consider the model rtwdemo_irt_reset, which includes a Reset Function block derived from an Initialize Function block.

The Event type and Event name parameters of the Event Listener block are set to Reset and reset, respectively. The function uses the State Writer block to reset the input value for the Discrete Integrator block to 5.

The code generator produces reset function rtwdemo_irt_reset_reset.

void rtwdemo_irt_reset_reset(void)

{
  rtwdemo_irt_reset_DW.DiscreteIntegrator_DSTATE = 5.0;
}

Event Names and Code Aggregation

Use the Initialize Function and Terminate Function blocks to define multiple initialize, reset, and terminate functions for a component hierarchy. Define only one initialize function and one terminate function per hierarchy level. You can define multiple reset functions for a hierarchy level. The event names that you configure for the functions at a given level must be unique.

When producing code, the code generator aggregates code for functions that have a given event name across the entire component hierarchy into one entry-point function. The code for reset functions appears in order, starting with the lowest level (innermost) of the component hierarchy and ending with the root (outermost). The code generator uses the event name to name the function.

For example, the model rtwdemo_irt_shared includes a subsystem that replicates the initialize, reset, and terminate functions that are in the parent model.

Although the model includes multiple copies of the initialize, reset, and terminate functions, the code generator produces one entry-point function for reset (rtwdemo_irt_shared_reset), one for initialize (rtwdemo_irt_shared_initialize), and one for terminate (rtwdemo_irt_shared_terminate). Within each entry-point function, after listing code for blocks configured with an initial condition (model_P.block_IC), the code generator orders code for components, starting with the lowest level of the hierarchy and ending with the root.

.
.
.
void rtwdemo_irt_shared_reset(void)
{
  rtwdemo_irt_shared_DW.SubIntegrator2_DSTATE = 5.0;

  rtwdemo_irt_shared_DW.Integrator2_DSTATE = 5.0;
} 
.
.
.
void rtwdemo_irt_shared_initialize(void)
{
  rtmSetErrorStatus(rtwdemo_irt_shared_M, (NULL));

  (void) memset(((void *)&rtwdemo_irt_shared_DW), 0,
                sizeof(DW_rtwdemo_irt_shared_T));

  rtwdemo_irt_shared_Y.Out1 = 0.0;

  rtwdemo_irt_shared_DW.Integrator1_DSTATE = 0.0;

  rtwdemo_irt_shared_DW.SubIntegrator2_DSTATE = 2.0;

  rtwdemo_irt_shared_DW.Integrator2_DSTATE = 10.0;
.
.
.
void rtwdemo_irt_shared_terminate(void)
{
   /* (no terminate code required) */
   }

If you rename the event configured for the subsystem reset function to reset_02, the code generator produces two reset entry-point functions, rtwdemo_irt_shared_reset and rtwdemo_irt_shared_reset_02.

void rtwdemo_irt_shared_reset(void)
{
	  rtwdemo_irt_shared_DW.SubIntegrator2_DSTATE = 5.0;
}
 
void rtwdemo_irt_shared_reset_02(void)
{
   rtwdemo_irt_shared_DW.Integrator2_DSTATE = 5.0;
}

Limitation

You cannot generate code from a harness model—a root model that contains a Model block, which exposes initialize, reset, or terminate function ports.

Related Topics