Main Content

Types of DWork Vectors

All DWork vectors are S-function memory that the Simulink® engine manages. The Simulink software supports four types of DWork vectors:

  • General DWork vectors contain information of any data type.

  • DState vectors contain discrete state information. Information stored in a DState vector appears as a state in the linearized model and is available during state logging.

  • Scratch vectors contain values that do not need to persist from one time step to the next.

  • Mode vectors contain mode information, usually stored as Boolean or integer data.

S-functions register the DWork vector type using the ssSetDWorkUsageType macro. This macro accepts one of the four usage types described in the following table.

DWorkUsage TypeFunctionality
General SS_DWORK_USED_AS_DWORKStore instance specific persistent data. General DWork vectors can also be used to store discrete state and mode data, however the Simulink engine will not treat this information specially. You might choose to use a general DWork vector to store state information if you want to avoid data logging.
DStateSS_DWORK_USED_AS_DSTATEStore discrete state information. Using the DState vector instead of ssSetNumDiscStates to store discrete states provides more flexibility for naming and data typing the states. The engine marks blocks with discrete states as special during sample time propagation. In addition, the engine makes the data stored in the DState vector available during data logging.
ModeSS_DWORK_USED_AS_MODEIndicate to the Simulink engine that the S-function contains modes. The engine handles blocks with modes specially when solving algebraic loops. In addition, the engine updates an S-function with modes only at major time steps. DWork mode vectors are more efficient than standard mode work vectors (see Elementary Work Vectors) because they can store mode information as Boolean data. In addition, while an S-function has only one mode work vectors, it can have multiple DWork vectors configured to store modes.
ScratchSS_DWORK_USED_AS_SCRATCHStore memory that is not persistent, for example, a large variable that you do not want to mark on the stack. Scratch vectors are scoped to a particular S-function method (for example, mdlOutputs) and exist across a single time step. Scratch memory can be shared across S-function blocks. The Simulink engine attempts to minimize the amount of memory used by scratch variables and reuses scratch memory whenever possible.