CFPRIME DUDB29                                                          DUDB29      1
C***********************************************************************RIGHTE      1
C*                                                                     *RIGHTE      2
C*        Extended Transient-Midterm Stability Program (ETMSP)         *RIGHTE      3
C*        ====================================================         *RIGHTE      4
C*                                                                     *RIGHTE      5
C*       Copyright (C) (1993) Electric Power Research Institute        *RIGHTE      6
C*                                                                     *RIGHTE      7
C* EPRI reserves all rights in the software and associated             *RIGHTE      8
C* documentation as delivered. The software and its associated         *RIGHTE      9
C* documention or any portion thereof may not be reproduced in any form*RIGHTE     10
C* whatsoever without the consent of EPRI, except as explicity provided*RIGHTE     11
C* for by license. Licensee shall submit a detailed description to EPRI*RIGHTE     12
C* of any changes or modifications to the software within two (2)      *RIGHTE     13
C* months after such changes or modifications are made. At EPRI's sole *RIGHTE     14
C* discretion, the software, as changed or modified, shall be given a  *RIGHTE     15
C* new designation sufficiently different from its then current        *RIGHTE     16
C* designation as to prevent mistake,confusion, or deception as between*RIGHTE     17
C* the current software as changed or modified.                        *RIGHTE     18
C*                                                                     *RIGHTE     19
C* A license under EPRI's rights in the software can be obtained       *RIGHTE     20
C* directly from EPRI.                                                 *RIGHTE     21
C*                                                                     *RIGHTE     22
C* This program and supporting materials may be independently available*RIGHTE     23
C* from Electric Power Software centre (EPSC) for an appropriate       *RIGHTE     24
C* distribution fee. The EPSC is a service of                          *RIGHTE     25
C*                                                                     *RIGHTE     26
C*                    Power Computing Company                          *RIGHTE     27
C*                    1930 Hi Line                                     *RIGHTE     28
C*                    Dallas, Tx 75207                                 *RIGHTE     29
C*                                                                     *RIGHTE     30
C* NEITHER EPRI, ANY MEMBER OF EPRI, NOR ANY PERSON OR  ORGANIZATION   *RIGHTE     31
C* ACTING ON BEHAVE OF THEM:                                           *RIGHTE     32
C*                                                                     *RIGHTE     33
C* 1. MAKE ANY WARRANTY OR REPRESENTATION WHATSOEVER, EXPRESS OR       *RIGHTE     34
C*    IMPLIED, INCLUDING ANY WARRANTY OF MERCHANTABILITY OR FITNESS OR *RIGHTE     35
C*    ANY PURPOSE WITH RESPECT TO THE SOFTWARE OR ITS ASSOCIATED       *RIGHTE     36
C*    DOCUMENTATION; OR                                                *RIGHTE     37
C*                                                                     *RIGHTE     38
C* 2. ASSUMES ANY LIABILITY WHATSOEVER WITH RESPECT TO ANY USE OF THE  *RIGHTE     39
C*    SOFTWARE OR ITS ASSOCIATED DOCUMENTATION OR ANY PORTION THEREOF  *RIGHTE     40
C*    OR WITH RESPECT TO ANY DAMAGES WHICH MAY RESULT FROM SUCH USE.   *RIGHTE     41
C*                                                                     *RIGHTE     42
C* THIS NOTICE MAY NOT BE REMOVED FROM THE SOFTWARE BY ANY USER        *RIGHTE     43
C* THEREOF.                                                            *RIGHTE     44
C*                                                                     *RIGHTE     45
C***********************************************************************RIGHTE     46
      SUBROUTINE DUDB29(LORD)                                           DUDB29      3
CD                                                                      DUDB29      4
CD    TO CALCULATE THE DERIVATIVES OF THE STATES OF USER DEFINED BLOCK  DUDB29      5
CD    TYPE 29 (HYST BLOCK) IN ANY USER DEFINED MODEL, USED BY BOTH      DUDB29      6
CD    RUNGE-KUTTA AND TRAPEZOIDAL METHOD.                               DUDB29      7
CD                                                                      DUDB29      8
      INCLUDE 'PARX.INS'                                                DUDB29      9
      INCLUDE 'CNMDAT.INS'                                              DUDB29     10
      INCLUDE 'CNMSTA.INS'                                              DUDB29     11
      INCLUDE 'CNXSTA.INS'                                              DUDB29     12
      INCLUDE 'CNSTN.INS'                                               DUDB29     13
      INCLUDE 'CNYVAR.INS'                                              DUDB29     14
      INCLUDE 'CNUNIT.INS'                                              DUDB29     15
      INCLUDE 'CNFLAG.INS'                                              DUDB29     16
      INCLUDE 'CNUDM.INS'                                               DUDB29     17
C                                                                       DUDB29     18
      DIMENSION XX(4),YY(4)                                             DUDB29     19
C                                                                       DUDB29     20
      LDEB=(JDEB(264).NE.0)                                             DUDB29     21
C                                                                       DUDB29     22
      VALI=0.0                                                          DUDB29     23
      IORD=IDDAT(JE4+LORD)                                              DUDB29     24
      JEI=JEB+2                                                         DUDB29     25
      DO 100 I=1,4                                                      DUDB29     26
      XX(I)=DDAT(JEI+I)                                                 DUDB29     27
      YY(I)=DDAT(JEI+I+6)                                               DUDB29     28
  100 CONTINUE                                                          DUDB29     29
C                                                                       DUDB29     30
      KINT=KINPU                                                        DUDB29     31
      KINPU=KINPU+IDDAT(JEI+13)                                         DUDB29     32
      IF(KINT.LT.KINPU) THEN                                            DUDB29     33
         DO 110 J=KINT+1,KINPU                                          DUDB29     34
         XDAT=GAINS(J)                                                  DUDB29     35
         IOUT=INPUTS(J)                                                 DUDB29     36
         VALI=VALI+VALOUT(IOUT)*XDAT                                    DUDB29     37
  110    CONTINUE                                                       DUDB29     38
      ENDIF                                                             DUDB29     39
      KINP=KINPU                                                        DUDB29     40
      KINPU=KINPU+IDDAT(JEI+14)                                         DUDB29     41
      IF(KINP.LT.KINPU) THEN                                            DUDB29     42
         DO 120 J=KINP+1,KINPU                                          DUDB29     43
         XDAT=GAINS(J)                                                  DUDB29     44
         IIN=INPUTS(J)                                                  DUDB29     45
         VALI=VALI+VALIN(IIN)*XDAT                                      DUDB29     46
  120    CONTINUE                                                       DUDB29     47
      ENDIF                                                             DUDB29     48
C                                                                       DUDB29     49
      IF(VALI.GE.XX(4)) THEN                                            DUDB29     50
         SLOPE=(YY(4)-YY(2))/(XX(4)-XX(2))                              DUDB29     51
         VALO=YY(2)+SLOPE*(VALI-XX(2))                                  DUDB29     52
      ELSEIF(VALI.LE.XX(1)) THEN                                        DUDB29     53
         SLOPE=(YY(3)-YY(1))/(XX(3)-XX(1))                              DUDB29     54
         VALO=YY(1)+SLOPE*(VALI-XX(1))                                  DUDB29     55
      ELSE                                                              DUDB29     56
         X0=DDAT(JEI+5)                                                 DUDB29     57
         Y0=DDAT(JEI+11)                                                DUDB29     58
         S1=(YY(4)-YY(2))/(XX(4)-XX(2))                                 DUDB29     59
         S2=(YY(3)-YY(1))/(XX(3)-XX(1))                                 DUDB29     60
         S3=(YY(2)-YY(1))/(XX(2)-XX(1))                                 DUDB29     61
         S4=(YY(4)-YY(3))/(XX(4)-XX(3))                                 DUDB29     62
         IF(VALI.GT.X0) THEN                                            DUDB29     63
            XLIM=(S2*X0-Y0-S4*XX(3)+YY(3))/(S2-S4)                      DUDB29     64
            IF(XLIM.GT.XX(4)) THEN                                      DUDB29     65
               SLOPE=S1                                                 DUDB29     66
               VALO=YY(2)+SLOPE*(VALI-XX(2))                            DUDB29     67
            ELSEIF(XLIM.GT.VALI) THEN                                   DUDB29     68
               SLOPE=S2                                                 DUDB29     69
               VALO=Y0+SLOPE*(VALI-X0)                                  DUDB29     70
            ELSE                                                        DUDB29     71
               SLOPE=S4                                                 DUDB29     72
               VALO=YY(3)+SLOPE*(VALI-XX(3))                            DUDB29     73
            ENDIF                                                       DUDB29     74
         ELSEIF(VALI.LT.X0) THEN                                        DUDB29     75
            XLIM=(S1*X0-Y0-S3*XX(1)+YY(1))/(S1-S3)                      DUDB29     76
            IF(XLIM.LT.XX(1)) THEN                                      DUDB29     77
               SLOPE=S2                                                 DUDB29     78
               VALO=YY(1)+SLOPE*(VALI-XX(1))                            DUDB29     79
            ELSEIF(XLIM.LT.VALI) THEN                                   DUDB29     80
               SLOPE=S1                                                 DUDB29     81
               VALO=Y0+SLOPE*(VALI-X0)                                  DUDB29     82
            ELSE                                                        DUDB29     83
               SLOPE=S3                                                 DUDB29     84
               VALO=YY(1)+SLOPE*(VALI-XX(1))                            DUDB29     85
            ENDIF                                                       DUDB29     86
         ELSE                                                           DUDB29     87
            SLOPE=DDAT(JEI+6)                                           DUDB29     88
            VALO=Y0                                                     DUDB29     89
         ENDIF                                                          DUDB29     90
      ENDIF                                                             DUDB29     91
      VALOUT(IORD)=VALO                                                 DUDB29     92
      DDAT(JEI+5)=VALI                                                  DUDB29     93
      DDAT(JEI+11)=VALO                                                 DUDB29     94
      DDAT(JEI+6)=SLOPE                                                 DUDB29     95
      IF(LDEB) WRITE(IUD6,1000) LORD,IORD,VALOUT(IORD)                  DUDB29     96
C                                                                       DUDB29     97
      RETURN                                                            DUDB29     98
 1000 FORMAT(1X,'LORD,IORD,VALOUT(IORD): ',2I5,F10.3)                   DUDB29     99
      END                                                               DUDB29    100
