Products & Services Solutions Academia Support User Community Company

Learn more about Simulink   

mdlInitializeConditions - Initialize the state vectors of this S-function

Required

No

C Syntax

#define MDL_INITIALIZE_CONDITIONS
void mdlInitializeConditions(SimStruct *S)

C Arguments

S

SimStruct representing an S-Function block.

M Syntax

InitializeConditions(s)

M Arguments

s

Instance of Simulink.MSFcnRunTimeBlock class representing the Level-2 M-File S-Function block.

Description

The Simulink engine invokes this optional method at the beginning of a simulation. It should initialize the continuous and discrete states, if any, of this S-Function block. In a C MEX S-function, use ssGetContStates and/or ssGetDiscStates to access the states. In a Level-2 M-file S-function, use the ContStates or Dwork run-time object methods to access the continuous and discrete states. This method can also perform any other initialization activities that this S-function requires.

If this S-function resides in an enabled subsystem configured to reset states, the Simulink engine also calls this method when the enabled subsystem restarts execution. C MEX S-functions can use the ssIsFirstInitCond macro to determine whether the time at which mdlInitializeCondition is called is equal to the simulation start time.

The Simulink engine calls mdlInitializeConditions prior to calculating the S-function's input signals. Therefore, since the input signal values are not yet available, mdlInitializeConditions should not use the input signal values to set initial conditions. If your S-function needs to initialize internal values using the block's input signals, perform the initialization in mdlOutputs.

For example, in a C MEX S-function, initializes an IWork vector with one element in the mdlInitializeSizes method.

ssSetNumIWork(S, 1);

The IWork vector holds a flag indicating if initial values have been specified. Initialize the flag's value in the mdlInitializeCondition method.

static void mdlInitializeConditions(SimStruct *S)
{
  /* The mdlInitializeConditions method is called when the simulation
     start and every time an enabled subsystem is re-enabled.
     
     Reset the IWork flag to 1 when values need to be reinitialized.*/

  ssSetIWorkValue(S, 0, 1);
}

Check the value of the IWork vector flag in the mdlOutputs method, to determine if initial values need to be set. Since the engine has calculated input values at this point in the simulation, the mdlOutputs method can use them to initialize internal values.

static void mdlOutputs(SimStruct *S, int_T tid)
{
    // Initialize values if the IWork vector flag is true. //
    if (ssGetIWorkValue(S, 0) == 1) {
             // Enter initialization code here //
    }

    // Remainder of mdlOutputs function //
}

For a Level-2 M-file S-function, use a DWork vector instead of an IWork vector in the previous example.

C Example

This example initializes both a continuous and discrete state to 1.0.

#define MDL_INITIALIZE_CONDITIONS   /*Change to #undef to remove */
                                    /*function*/
#if defined(MDL_INITIALIZE_CONDITIONS)

static void mdlInitializeConditions(SimStruct *S)
{
  int i;
  real_T *xcont    = ssGetContStates(S);
  int_T   nCStates = ssGetNumContStates(S);
  real_T *xdisc    = ssGetRealDiscStates(S);
  int_T   nDStates = ssGetNumDiscStates(S);

  for (i = 0; i < nCStates; i++) {
    *xcont++ = 1.0;
  }

  for (i = 0; i < nDStates; i++) {
    *xdisc++ = 1.0;
  }

}
#endif /* MDL_INITIALIZE_CONDITIONS */

For another example that initializes only the continuous states, see resetint.c.

M Example

This example initializes both a continuous and discrete state to 1.0. Level-2 M-file S-functions store discrete states in their DWork vectors.

function InitializeConditions(s)

s.ContStates.Data(1) = 1;
s.Dwork(1).Data      = 1;

% endfunction

Languages

C, C++, M

See Also

mdlStart, mdlOutputs, ssIsFirstInitCond, ssGetContStates, ssGetDiscStates, ssGetTStart, ssGetT

  


Related Products & Applications

Learn more about Simulink through this collection of videos, articles, technical literature and the Getting Started with Simulink Guide.

 © 1984-2009- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS