anchor.diff:  SUBROUTINE ModName_JacobianPConstrState( t, u, p, x, xd, z, OtherState, m, dYdz, dXdz, dXddz, dZdz, ErrStat, ErrMsg )
   !..................................................................................................................................
   
  -      REAL(DbKi),                                INTENT(IN   )           :: t          !< Current simulation time in seconds
  -      TYPE(ModName_InputType),                   INTENT(IN   )           :: u          !< Inputs at t
  -      TYPE(ModName_ParameterType),               INTENT(IN   )           :: p          !< Parameters
  -      TYPE(ModName_ContinuousStateType),         INTENT(IN   )           :: x          !< Continuous states at t
  -      TYPE(ModName_DiscreteStateType),           INTENT(IN   )           :: xd         !< Discrete states at t
  -      TYPE(ModName_ConstraintStateType),         INTENT(IN   )           :: z          !< Constraint states at t
  -      TYPE(ModName_OtherStateType),              INTENT(IN   )           :: OtherState !< Other states at t
  -      TYPE(ModName_MiscVarType),                 INTENT(INOUT)           :: m          !< Misc/optimization variables
  -      REAL(ReKi), ALLOCATABLE, OPTIONAL,         INTENT(  OUT)           :: dYdz       !< Partial derivatives of output
  -                                                                                       !!  functions (Y) with respect to the
  -                                                                                       !!  constraint states (z)
  -      REAL(ReKi), ALLOCATABLE, OPTIONAL,         INTENT(  OUT)           :: dXdz       !< Partial derivatives of continuous
  -                                                                                       !!  state functions (X) with respect to
  -                                                                                       !!  the constraint states (z)
  -      REAL(ReKi), ALLOCATABLE, OPTIONAL,         INTENT(  OUT)           :: dXddz      !< Partial derivatives of discrete state
  -                                                                                       !!  functions (Xd) with respect to the
  -                                                                                       !!  constraint states (z)
  -      REAL(ReKi), ALLOCATABLE, OPTIONAL,         INTENT(  OUT)           :: dZdz       !< Partial derivatives of constraint
  -                                                                                       !! state functions (Z) with respect to
  -                                                                                       !!  the constraint states (z)
  -      INTEGER(IntKi),                            INTENT(  OUT)           :: ErrStat    !< Error status of the operation
  -      CHARACTER(*),                              INTENT(  OUT)           :: ErrMsg     !< Error message if ErrStat /= ErrID_None
  -
  -
  -         ! Initialize ErrStat
  -
  -      ErrStat = ErrID_None
  -      ErrMsg  = ""
  -
  -      IF ( PRESENT( dYdz ) ) THEN
  -
  -            ! Calculate the partial derivative of the output functions (Y) with respect to the constraint states (z) here:
  -
  -         ! allocate and set dYdz
  -
  -      END IF
  -
  -      IF ( PRESENT( dXdz ) ) THEN
  -
  -            ! Calculate the partial derivative of the continuous state functions (X) with respect to the constraint states (z) here:
  -
  -         ! allocate and set dXdz
  -
  -      END IF
  -
  -      IF ( PRESENT( dXddz ) ) THEN
  -
  -            ! Calculate the partial derivative of the discrete state functions (Xd) with respect to the constraint states (z) here:
  -
  -         ! allocate and set dXddz
  -
  -      END IF
  -
  -      IF ( PRESENT( dZdz ) ) THEN
  -
  -            ! Calculate the partial derivative of the constraint state functions (Z) with respect to the constraint states (z) here:
  -
  -         ! allocate and set dZdz
  -
  -      END IF
  +   REAL(DbKi),                                INTENT(IN   )           :: t          !< Current simulation time in seconds
  +   TYPE(ModName_InputType),                   INTENT(IN   )           :: u          !< Inputs at t
  +   TYPE(ModName_ParameterType),               INTENT(IN   )           :: p          !< Parameters
  +   TYPE(ModName_ContinuousStateType),         INTENT(IN   )           :: x          !< Continuous states at t
  +   TYPE(ModName_DiscreteStateType),           INTENT(IN   )           :: xd         !< Discrete states at t
  +   TYPE(ModName_ConstraintStateType),         INTENT(IN   )           :: z          !< Constraint states at t
  +   TYPE(ModName_OtherStateType),              INTENT(IN   )           :: OtherState !< Other states at t
  +   TYPE(ModName_MiscVarType),                 INTENT(INOUT)           :: m          !< Misc/optimization variables
  +   REAL(ReKi), ALLOCATABLE, OPTIONAL,         INTENT(INOUT)           :: dYdz       !< Partial derivatives of output
  +                                                                                    !!  functions (Y) with respect to the
  +                                                                                    !!  constraint states (z)
  +   REAL(ReKi), ALLOCATABLE, OPTIONAL,         INTENT(INOUT)           :: dXdz       !< Partial derivatives of continuous
  +                                                                                    !!  state functions (X) with respect to
  +                                                                                    !!  the constraint states (z)
  +   REAL(ReKi), ALLOCATABLE, OPTIONAL,         INTENT(INOUT)           :: dXddz      !< Partial derivatives of discrete state
  +                                                                                    !!  functions (Xd) with respect to the
  +                                                                                    !!  constraint states (z)
  +   REAL(ReKi), ALLOCATABLE, OPTIONAL,         INTENT(INOUT)           :: dZdz       !< Partial derivatives of constraint
  +                                                                                    !! state functions (Z) with respect to
  +                                                                                    !!  the constraint states (z)
  +   INTEGER(IntKi),                            INTENT(  OUT)           :: ErrStat    !< Error status of the operation
  +   CHARACTER(*),                              INTENT(  OUT)           :: ErrMsg     !< Error message if ErrStat /= ErrID_None
  +
  +
  +      ! Initialize ErrStat
  +
  +   ErrStat = ErrID_None
  +   ErrMsg  = ""
  +
  +   IF ( PRESENT( dYdz ) ) THEN
  +
  +         ! Calculate the partial derivative of the output functions (Y) with respect to the constraint states (z) here:
  +
  +      ! allocate and set dYdz
  +
  +   END IF
  +
  +   IF ( PRESENT( dXdz ) ) THEN
  +
  +         ! Calculate the partial derivative of the continuous state functions (X) with respect to the constraint states (z) here:
  +
  +      ! allocate and set dXdz
  +
  +   END IF
  +
  +   IF ( PRESENT( dXddz ) ) THEN
  +
  +         ! Calculate the partial derivative of the discrete state functions (Xd) with respect to the constraint states (z) here:
  +
  +      ! allocate and set dXddz
  +
  +   END IF
  +
  +   IF ( PRESENT( dZdz ) ) THEN
  +
  +         ! Calculate the partial derivative of the constraint state functions (Z) with respect to the constraint states (z) here:
  +
  +      ! allocate and set dZdz
  +
  +   END IF
   
   
   END SUBROUTINE ModName_JacobianPConstrState
  
positive.code: SUBROUTINE ModName_UpdateStates( t, n, Inputs, InputTimes, p, x, xd, z, OtherState, m, ErrStat, ErrMsg )
  !..................................................................................................................................
  
        REAL(DbKi),                         INTENT(IN   ) :: t               !< Current simulation time in seconds
        INTEGER(IntKi),                     INTENT(IN   ) :: n               !< Current step of the simulation: t = n*Interval
        TYPE(ModName_InputType),            INTENT(INOUT) :: Inputs(:)       !< Inputs at InputTimes (output from this routine only
                                                                             !!  because of record keeping in routines that copy meshes)
        REAL(DbKi),                         INTENT(IN   ) :: InputTimes(:)   !< Times in seconds associated with Inputs
        TYPE(ModName_ParameterType),        INTENT(IN   ) :: p               !< Parameters
        TYPE(ModName_ContinuousStateType),  INTENT(INOUT) :: x               !< Input: Continuous states at t;
                                                                             !!   Output: Continuous states at t + Interval
        TYPE(ModName_DiscreteStateType),    INTENT(INOUT) :: xd              !< Input: Discrete states at t;
                                                                             !!   Output: Discrete states at t + Interval
        TYPE(ModName_ConstraintStateType),  INTENT(INOUT) :: z               !< Input: Constraint states at t;
                                                                             !!   Output: Constraint states at t + Interval
        TYPE(ModName_OtherStateType),       INTENT(INOUT) :: OtherState      !< Other states: Other states at t;
                                                                             !!   Output: Other states at t + Interval
        TYPE(ModName_MiscVarType),          INTENT(INOUT) :: m               !<  Misc variables for optimization (not copied in glue code)
        INTEGER(IntKi),                     INTENT(  OUT) :: ErrStat         !< Error status of the operation
        CHARACTER(*),                       INTENT(  OUT) :: ErrMsg          !< Error message if ErrStat /= ErrID_None
  
           ! Local variables
  
        TYPE(ModName_ContinuousStateType)                 :: dxdt            ! Continuous state derivatives at t
        TYPE(ModName_DiscreteStateType)                   :: xd_t            ! Discrete states at t (copy)
        TYPE(ModName_ConstraintStateType)                 :: z_Residual      ! Residual of the constraint state functions (Z)
        TYPE(ModName_InputType)                           :: u               ! Instantaneous inputs
  
        INTEGER(IntKi)                                    :: ErrStat2        ! local error status
        CHARACTER(ErrMsgLen)                              :: ErrMsg2         ! local error message
        CHARACTER(*), PARAMETER                           :: RoutineName = 'ModName_UpdateStates'
  
  
           ! Initialize variables
  
        ErrStat   = ErrID_None           ! no error has occurred
        ErrMsg    = ""
  
  
        ! This subroutine contains an example of how the states could be updated. Developers will
        ! want to adjust the logic as necessary for their own situations.
  
  
  
           ! Get the inputs at time t, based on the array of values sent by the glue code:
  
        ! before calling ExtrapInterp routine, memory in u must be allocated; we can do that with a copy:
        call ModName_CopyInput( Inputs(1), u, MESH_NEWCOPY, ErrStat2, ErrMsg2 )
           call SetErrStat(ErrStat2,ErrMsg2,ErrStat,ErrMsg,RoutineName)
           if ( ErrStat >= AbortErrLev ) then
              call cleanup()       ! to avoid memory leaks, we have to destroy the local variables that may have allocatable arrays or meshes
              return
           end if
  
        call ModName_Input_ExtrapInterp( Inputs, InputTimes, u, t, ErrStat2, ErrMsg2 )
           call SetErrStat(ErrStat2,ErrMsg2,ErrStat,ErrMsg,RoutineName)
           if ( ErrStat >= AbortErrLev ) then
              call cleanup()
              return
           end if
  
  
  
           ! Get first time derivatives of continuous states (dxdt):
  
        call ModName_CalcContStateDeriv( t, u, p, x, xd, z, OtherState, m, dxdt, ErrStat2, ErrMsg2 )
           call SetErrStat(ErrStat2,ErrMsg2,ErrStat,ErrMsg,RoutineName)
           if ( ErrStat >= AbortErrLev ) then
              call cleanup()
              return
           end if
  
  
           ! Update discrete states:
           !   Note that xd [discrete state] is changed in ModName_UpdateDiscState() so xd will now contain values at t+Interval
           !   We'll first make a copy that contains xd at time t, which will be used in computing the constraint states
        call ModName_CopyDiscState( xd, xd_t, MESH_NEWCOPY, ErrStat2, ErrMsg2 )
           call SetErrStat(ErrStat2,ErrMsg2,ErrStat,ErrMsg,RoutineName)
           if ( ErrStat >= AbortErrLev ) then
              call cleanup()
              return
           end if
  
        call ModName_UpdateDiscState( t, n, u, p, x, xd, z, OtherState, m, ErrStat2, ErrMsg2 )
           call SetErrStat(ErrStat2,ErrMsg2,ErrStat,ErrMsg,RoutineName)
           if ( ErrStat >= AbortErrLev ) then
              call cleanup()
              return
           end if
  
  
           ! Solve for the constraint states (z) here:
  
           ! Iterate until the value is within a given tolerance.
  
        ! DO
  
           call ModName_CalcConstrStateResidual( t, u, p, x, xd_t, z, OtherState, m, Z_Residual, ErrStat2, ErrMsg2 )
              call SetErrStat(ErrStat2,ErrMsg2,ErrStat,ErrMsg,RoutineName)
              if ( ErrStat >= AbortErrLev ) then
                 call cleanup()
                 return
              end if
  
           !  z =
  
        ! END DO
  
  
  
           ! Integrate (update) continuous states (x) here:
  
        !x = function of dxdt and x
  
  
           ! Destroy local variables before returning
        call cleanup()
  
  
  CONTAINS
  
negative.code: SUBROUTINE ModName_CalcContStateDeriv( t, u, p, x, xd, z, OtherState, m, dxdt, ErrStat, ErrMsg )
  !..................................................................................................................................
  
     REAL(DbKi),                        INTENT(IN   )  :: t           !< Current simulation time in seconds
     TYPE(ModName_InputType),           INTENT(IN   )  :: u           !< Inputs at t
     TYPE(ModName_ParameterType),       INTENT(IN   )  :: p           !< Parameters
     TYPE(ModName_ContinuousStateType), INTENT(IN   )  :: x           !< Continuous states at t
     TYPE(ModName_DiscreteStateType),   INTENT(IN   )  :: xd          !< Discrete states at t
     TYPE(ModName_ConstraintStateType), INTENT(IN   )  :: z           !< Constraint states at t
     TYPE(ModName_OtherStateType),      INTENT(IN   )  :: OtherState  !< Other states at t
     TYPE(ModName_MiscVarType),         INTENT(INOUT)  :: m           !< Misc variables for optimization (not copied in glue code)
     TYPE(ModName_ContinuousStateType), INTENT(  OUT)  :: dxdt        !< Continuous state derivatives at t
     INTEGER(IntKi),                    INTENT(  OUT)  :: ErrStat     !< Error status of the operation
     CHARACTER(*),                      INTENT(  OUT)  :: ErrMsg      !< Error message if ErrStat /= ErrID_None
  
  
        ! Initialize ErrStat
  
     ErrStat = ErrID_None
     ErrMsg  = ""
  
  
        ! Compute the first time derivatives of the continuous states here:
  
     dxdt%DummyContState = 0.0_ReKi
  
  END SUBROUTINE ModName_CalcContStateDeriv
  !----------------------------------------------------------------------------------------------------------------------------------
  