C=======================================================================
C     SIMOMEX.F
C
C     This is a FORTRAN representation of the SIMO system.
C     It works in conjunction with the the file SIMULINK.F.
C     To compile this system, use the syntax:
C          fmex simomex.f simulink.f
C
C=======================================================================
C  Written:
C      Ned Gulley        Apr 28, 1992
C      Copyright (c) 1990-93 by The MathWorks, Inc.
C      All Rights Reserved
C=======================================================================
C 
C=======================================================================
      SUBROUTINE SIZES(SIZE)
C     .. Array arguments ..
      INTEGER*4        SIZE(*)
C=======================================================================
C     Purpose:
C       Function to set size vector
C     Input arguments:
C       None
C     Output arguments:
C       SIZE     a vector of model sizes
C     Description:
C       Sizes returns a vector which determines model characteristics.
C       This vector contains the sizes of the state vector and other
C       parameters. More precisely,
C       SIZE(1)  number of continuous states
C       SIZE(2)  number of discrete states
C       SIZE(3)  number of outputs
C       SIZE(4)  number of inputs
C       SIZE(5)  number of discontinuous roots in the system
C       SIZE(6)  set to 1 if the system has direct feedthrough of its
C                inputs, otherwise 0
C=======================================================================
C     .. Parameters ..
      INTEGER*4       NSIZES
      PARAMETER       (NSIZES=6)
C     .. Local scalars ..
C     .. Local arrays ..
C     .. Executable statements ..
      SIZE(1) = 3
      SIZE(2) = 0
      SIZE(3) = 2
      SIZE(4) = 1
      SIZE(5) = 0
      SIZE(6) = 1

      RETURN
      END

      SUBROUTINE INITCOND(X0)
C     .. Array arguments ..
      REAL*8           X0(*)
C=======================================================================
C     Purpose:
C       Function to set initial condition vector
C     Input arguments:
C       None
C     Output arguments:
C       X0       a vector of initial conditions
C=======================================================================
C     .. Local scalars ..
C     .. Local arrays ..
C     .. Executable statements ..
      X0(1) = 1.0
      X0(2) = 1.0
      X0(3) = 1.0

      RETURN
      END

      SUBROUTINE DERIVS(T, X, U, DX)
C     .. Scalar arguments ..
      REAL*8           T
C     .. Array arguments ..
      REAL*8           X(*), U(*), DX(*)
C=======================================================================
C     Purpose:
C       Function to return derivatives
C     Input arguments:
C       T        time
C       X        state vector
C       U        input vector
C     Output arguments:
C       DX       state vector derivatives
C     Remark:
C       The state vector is partitioned into continuous and discrete
C       states. The first states contain the continuous states, and
C       the last states contain the discrete states.
C=======================================================================
C     .. Local scalars ..
C     .. Local arrays ..
C     .. Executable statements ..
      DX(1) = -0.3*X(1) + U(1)
      DX(2) =  2.9*X(1) - 0.62*X(2) - 2.3*X(3)
      DX(3) =  2.3*X(2) 


      RETURN
      END

      SUBROUTINE DSTATES(T, X, U, XNEW)
C     .. Scalar arguments ..
      REAL*8           T
C     .. Array arguments ..
      REAL*8           X(*), U(*), XNEW(*)
C=======================================================================
C     Purpose:
C       Function to perform discrete state update
C     Input arguments:
C       T        time
C       X        state vector
C       U        input vector
C     Output arguments:
C       XNEW     next state values
C     Remark:
C       The state vector is partitioned into continuous and discrete
C       states. The first states contain the continuous states, and
C       the last states contain the discrete states.
C=======================================================================
C     .. Local scalars ..
C     .. Local arrays ..
C     .. Executable statements ..
      
      RETURN
      END

      SUBROUTINE OUTPUT(T, X, U, Y)
C     .. Scalar arguments ..
      REAL*8           T
C     .. Array arguments ..
      REAL*8           X(*), U(*), Y(*)
C=======================================================================
C     Purpose:
C       Function to return continuous outputs
C     Input arguments:
C       T        time
C       X        state vector
C       U        input vector
C     Output arguments:
C       Y        output vector
C     Remark:
C       The state vector is partitioned into continuous and discrete
C       states. The first states contain the continuous states, and
C       the last states contain the discrete states.
C=======================================================================
C     .. Local scalars ..
C     .. Local arrays ..
C     .. Executable statements ..
      Y(1) = X(1) + X(2)
      Y(2) = X(1) - 3.0*X(2) + X(3) + U(1)

      RETURN
      END
      
      SUBROUTINE DOUTPUT(T, X, U, Y)
C     .. Scalar arguments ..
      REAL*8           T
C     .. Array arguments ..
      REAL*8           X(*), U(*), Y(*)
C=======================================================================
C     Purpose:
C       Function to return discrete outputs
C     Input arguments:
C       T        time
C       X        state vector
C       U        input vector
C     Output arguments:
C       Y        output vector
C     Remark:
C       The state vector is partitioned into continuous and discrete
C       states. The first states contain the continuous states, and
C       the last states contain the discrete states.
C       This procedure is called only if it is a sample hit.
C=======================================================================
C     .. Local scalars ..
C     .. Local arrays ..
C     .. Executable statements ..
      
      RETURN
      END
      
      SUBROUTINE TSAMPL(T, X, U, TS, OFFSET)
C     .. Scalar arguments ..
      REAL*8           T,TS,OFFSET
C     .. Array arguments ..
      REAL*8           X(*), U(*)
C=======================================================================
C     Purpose:
C       Function to return the sample and offset times
C     Input arguments:
C       T        time
C       X        state vector
C       U        input vector
C     Output arguments:
C       TS       sample time
C       OFFSET   offset time
C=======================================================================
C     .. Local scalars ..
C     .. Local arrays ..
C     .. Executable statements ..
      
      RETURN
      END
      
      
      SUBROUTINE SINGUL(T, X, U, SING)
C     .. Scalar arguments ..
      REAL*8           T
C     .. Array arguments ..
      REAL*8           X(*), U(*), SING(*)
C=======================================================================
C     Purpose:
C       Function to return singularities
C     Input arguments:
C       T        time
C       X        state vector
C       U        input vector
C     Output arguments:
C       SING     singularities
C=======================================================================
C     .. Local scalars ..
C     .. Local arrays ..
C     .. Executable statements ..

      RETURN
      END
