Chapter 5
Generated Code Architecture
©
National Instruments Corporation
5-25
Init, Output, and State Phases
A subsystem has phases because the blocks within the subsystem need
phases of computation. The three phases are intended to be used in a
consistent way, just like the standard blocks. There are environment
variables that indicate if a particular phase is active. Therefore, the
canonical usage is that the appropriate environment variable is used as the
guard in an IF statement. This clearly indicates what code is associated with
each phase. The intended usages are listed below.
•
Init
—The intent is to initialize any local, output, state, or parameter
variable.
•
Output
—The intent is to only compute the outputs of this block as a
function of some combination of inputs, parameters, or current state.
•
State
—The intent is to only compute the next state (or state derivative)
as a function of some combination of current state, inputs, outputs, or
parameters.
Caution
No validation is performed to ensure that you write code that conforms to the
intended usage of the phases.
In a discrete subsystem, the phases of a block are set in the following way.
The first time the block is executed, the Init, Output, and State phases are
all active at the same time. For subsequent executions, only Output and
State phases are active.
Note
Regardless of the order within the script, code for the Output phase always will
execute before code for the State phase.
If the BlockScript block has states, place the Init phase within the Output phase to prevent
a repeat of the initialization during the State phase.
In a continuous subsystem, the Init and Output phases are active for the first
execution. The block can then be executed repeatedly for the State phase,
as the integrator integrates the state data for all blocks. On subsequent time
points, the cycle repeats except the Init phase is no longer active.
Caution
Be careful about any side-effects to persistent data within the Init or Output
phases. This can cause mismatches between SystemBuild Simulator and the AutoCode
simulation, because the SystemBuild Simulator might execute the block more than once
at time = 0.0 for the Init/Output phase, whereas the AutoCode simulation only executes the
time = 0.0 Init/Output phase once. Refer to the INITMODE option for the SystemBuild
Simulator for more information about its initialization behavior.