CFPRIME DERAN                                                           DERAN       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 DERAN (H)                                              DERAN       3
CD                                                                      DERAN       4
CD    TO CALCULATE THE DERIVATIVES OF THE STATES OF RANI FOR USE IN     DERAN       5
CD    THE RUNGE-KUTTA METHOD.                                           DERAN       6
CD                                                                      DERAN       7
CD    UPDATE THE STATES FOR THE CURRENT TIME STEP BY THE RUNGE-KUTTA    DERAN       8
CD    FORMULA.                                                          DERAN       9
CD                                                                      DERAN      10
      INCLUDE 'PARX.INS'                                                DERAN      11
      INCLUDE 'CNPT.INS'                                                DERAN      12
      INCLUDE 'CNIPTR.INS'                                              DERAN      13
      INCLUDE 'CNB4.INS'                                                DERAN      14
      INCLUDE 'CNB9.INS'                                                DERAN      15
      INCLUDE 'CNB91.INS'                                               DERAN      16
      INCLUDE 'CNRAN.INS'                                               DERAN      17
      INCLUDE 'CNUDM.INS'                                               DERAN      18
C                                                                       DERAN      19
      INCLUDE 'CNXSTA.INS'                                              DERAN      20
      INCLUDE 'CNQSTA.INS'                                              DERAN      21
      INCLUDE 'CNZSTA.INS'                                              DERAN      22
      INCLUDE 'CNMDAT.INS'                                              DERAN      23
      INCLUDE 'CNVOLT.INS'                                              DERAN      24
      INCLUDE 'CNMYVA.INS'                                              DERAN      25
      INCLUDE 'CNYVAR.INS'                                              DERAN      26
C                                                                       DERAN      27
      INTEGER*4 JCKID                                                   DERAN      28
      CHARACTER*2 LCKT                                                  DERAN      29
      CHARACTER*4 CKID                                                  DERAN      30
      EQUIVALENCE (CKID,JCKID)                                          DERAN      31
C                                                                       DERAN      32
      DO 500 N=1,NRANI                                                  DERAN      33
      JR=JRDAT(N)                                                       DERAN      34
      IF(H.LT.0.001) THEN                                               DERAN      35
         NSUBD=1                                                        DERAN      36
      ELSE                                                              DERAN      37
         NSUBD=IDDAT(JR+7)                                              DERAN      38
      ENDIF                                                             DERAN      39
      IF(NSUBD.GT.1.AND..NOT.LIR1) GO TO 500                            DERAN      40
      HSUB=H/NSUBD                                                      DERAN      41
      JSTATE=JXRAN(N)                                                   DERAN      42
      JY=JVRAN(N)                                                       DERAN      43
      NRTYPN=IDDAT(JR+4)                                                DERAN      44
      IRANF=IDDAT(JR+6)                                                 DERAN      45
      IF(NRTYPN.NE.50) GO TO 180                                        DERAN      46
C                                                                       DERAN      47
C     GET THE INPUT VALUES                                              DERAN      48
C                                                                       DERAN      49
      LSTATE=0                                                          DERAN      50
      JE0=JR+7                                                          DERAN      51
      JE1=JE0+5                                                         DERAN      52
      CALL RINUDM                                                       DERAN      53
C                                                                       DERAN      54
      K1=IDDAT(JR+1)                                                    DERAN      55
      K2=IDDAT(JR+2)                                                    DERAN      56
      K13=K1*3                                                          DERAN      57
      NLF1=NOLD(K1)                                                     DERAN      58
      JCKID=IDDAT(JR+3)                                                 DERAN      59
      LCKT(1:2)=CKID(3:4)                                               DERAN      60
C                                                                       DERAN      61
      VRE1=REAL(VOLT(K1))                                               DERAN      62
      VIM1=AIMAG(VOLT(K1))                                              DERAN      63
      VMG1=SQRT(VRE1*VRE1+VIM1*VIM1)                                    DERAN      64
      VRE2=REAL(VOLT(K2))                                               DERAN      65
      VIM2=AIMAG(VOLT(K2))                                              DERAN      66
      VMG2=SQRT(VRE2*VRE2+VIM2*VIM2)                                    DERAN      67
      VRE=VRE1-VRE2                                                     DERAN      68
      VIM=VIM1-VIM2                                                     DERAN      69
      VMG=SQRT(VRE*VRE+VIM*VIM)                                         DERAN      70
      VALIN(7)=VMG1                                                     DERAN      71
      VALIN(8)=VMG2                                                     DERAN      72
      VALIN(20)=RVREF(JY+1)                                             DERAN      73
      IF(VMG1.GT.0.05)THEN                                              DERAN      74
         VA1=ATAN2(VIM1,VRE1)                                           DERAN      75
         VAGD=DDAT(JE0+2)-VA1                                           DERAN      76
         DDAT(JE0+2)=VA1                                                DERAN      77
         IF(VAGD.GT.3.15) THEN                                          DERAN      78
            IDDAT(JE0+1)=IDDAT(JE0+1)+1                                 DERAN      79
         ELSEIF(VAGD.LT.-3.15) THEN                                     DERAN      80
            IDDAT(JE0+1)=IDDAT(JE0+1)-1                                 DERAN      81
         ENDIF                                                          DERAN      82
         VALIN(9)=VA1+6.2831853*IDDAT(JE0+1)                            DERAN      83
      ELSE                                                              DERAN      84
         VALIN(9)=DDAT(JE0+2)+6.2831853*IDDAT(JE0+1)                    DERAN      85
      ENDIF                                                             DERAN      86
      IF(VMG2.GT.0.05)THEN                                              DERAN      87
         VA2=ATAN2(VIM2,VRE2)                                           DERAN      88
         VAGD=DDAT(JE0+4)-VA2                                           DERAN      89
         DDAT(JE0+4)=VA2                                                DERAN      90
         IF(VAGD.GT.3.15) THEN                                          DERAN      91
            IDDAT(JE0+3)=IDDAT(JE0+3)+1                                 DERAN      92
         ELSEIF(VAGD.LT.-3.15) THEN                                     DERAN      93
            IDDAT(JE0+3)=IDDAT(JE0+3)-1                                 DERAN      94
         ENDIF                                                          DERAN      95
         VALIN(10)=VA2+6.2831853*IDDAT(JE0+3)                           DERAN      96
      ELSE                                                              DERAN      97
         VALIN(10)=DDAT(JE0+4)+6.2831853*IDDAT(JE0+3)                   DERAN      98
      ENDIF                                                             DERAN      99
      IF(IRANF.EQ.0) THEN                                               DERAN     100
         VALIN(1)=VMG*BDEV(JY+5)                                        DERAN     101
         VALIN(2)=VMG*BTCR(JY+6)                                        DERAN     102
         VALIN(3)=VMG*VALIN(1)                                          DERAN     103
         VALIN(4)=VMG*VALIN(2)                                          DERAN     104
      ELSEIF(IRANF.EQ.1) THEN                                           DERAN     105
         VALIN(1)=VMG*BDEV(JY+5)                                        DERAN     106
         VALIN(16)=VMG*VALIN(1)                                         DERAN     107
      ELSEIF(IRANF.EQ.2) THEN                                           DERAN     108
         Y02=BRANO(JY+9)**2+BRANO(JY+3)**2                              DERAN     109
         R0=BRANO(JY+9)/Y02                                             DERAN     110
         X0=BRANO(JY+3)/Y02                                             DERAN     111
         DX=BDEV(JY+5)*BASER(JY+4)-BRANO(JY+10)                         DERAN     112
         XT=X0+DX                                                       DERAN     113
         ZT=SQRT(R0**2+XT**2)                                           DERAN     114
         VALIN(1)=BASER(JY+4)*VMG/ZT                                    DERAN     115
         VALIN(3)=BDEV(JY+5)*VALIN(1)**2                                DERAN     116
      ELSEIF(IRANF.EQ.3) THEN                                           DERAN     117
         VALIN(3)=VMG*BDEV(JY+5)                                        DERAN     118
         IF(VMG.GT.0.0001) THEN                                         DERAN     119
            VALIN(5)=BDEV(JY+5)/VMG                                     DERAN     120
         ELSE                                                           DERAN     121
            VALIN(5)=0.0                                                DERAN     122
         ENDIF                                                          DERAN     123
      ENDIF                                                             DERAN     124
C                                                                       DERAN     125
      DO 170 JJ=1,NSUBD                                                 DERAN     126
      IF(LIR1) THEN                                                     DERAN     127
         JE0=0                                                          DERAN     128
      ELSE                                                              DERAN     129
         JE0=1                                                          DERAN     130
      ENDIF                                                             DERAN     131
      GOTO ( 110, 120, 130, 130, 130, 140), INTMTH                      DERAN     132
  110 CONTINUE                                                          DERAN     133
      IR1=0                                                             DERAN     134
      GOTO 140                                                          DERAN     135
  120 CONTINUE                                                          DERAN     136
      IR1=1                                                             DERAN     137
      GOTO 140                                                          DERAN     138
  130 CONTINUE                                                          DERAN     139
      IR1=2                                                             DERAN     140
  140 IR1=IR1+1                                                         DERAN     141
      NXS=0                                                             DERAN     142
C                                                                       DERAN     143
      IF(IRANF.EQ.0) THEN                                               DERAN     144
         VALIN(5)=BDEV(JY+5)                                            DERAN     145
         VALIN(6)=BTCR(JY+6)                                            DERAN     146
         VALIN(11)=STCR(JY+7)                                           DERAN     147
      ELSEIF(IRANF.EQ.1) THEN                                           DERAN     148
         VALIN(15)=BDEV(JY+5)                                           DERAN     149
         VALIN(12)=SRAN(JY+7)                                           DERAN     150
      ELSEIF(IRANF.EQ.2) THEN                                           DERAN     151
         VALIN(17)=BDEV(JY+5)                                           DERAN     152
         VALIN(12)=STCR(JY+7)                                           DERAN     153
      ELSEIF(IRANF.EQ.3) THEN                                           DERAN     154
         VALIN(1)=BDEV(JY+5)                                            DERAN     155
         VALIN(12)=SRAN(JY+7)                                           DERAN     156
      ENDIF                                                             DERAN     157
C                                                                       DERAN     158
      CALL DEUDM(HSUB,NXS,NLF1,K13,LCKT)                                DERAN     159
      JE0=1                                                             DERAN     160
C                                                                       DERAN     161
      IOUT=IDDAT(JE7+1)                                                 DERAN     162
      BDEV(JY+5)=VALOUT(IOUT)                                           DERAN     163
      IF(IRANF.EQ.0) THEN                                               DERAN     164
         BRANI(JY+2)=VALOUT(IOUT)*BASER(JY+4)                           DERAN     165
         BTCR(JY+6)=VALIN(6)                                            DERAN     166
         STCR(JY+7)=VALIN(11)                                           DERAN     167
      ELSEIF(IRANF.EQ.1) THEN                                           DERAN     168
         BRANI(JY+2)=VALOUT(IOUT)*BASER(JY+4)                           DERAN     169
         SRAN(JY+7)=VALIN(12)                                           DERAN     170
      ELSEIF(IRANF.EQ.2) THEN                                           DERAN     171
         Y02=BRANO(JY+9)**2+BRANO(JY+3)**2                              DERAN     172
         R0=BRANO(JY+9)/Y02                                             DERAN     173
         X0=BRANO(JY+3)/Y02                                             DERAN     174
         DX=VALOUT(IOUT)*BASER(JY+4)-BRANO(JY+10)                       DERAN     175
         Z2=R0**2+(X0+DX)**2                                            DERAN     176
         BRANI(JY+8)=R0/Z2                                              DERAN     177
         BRANI(JY+2)=(X0+DX)/Z2                                         DERAN     178
         SRAN(JY+7)=VALIN(12)                                           DERAN     179
      ELSEIF(IRANF.EQ.3) THEN                                           DERAN     180
         BRANI(JY+2)=VALOUT(IOUT)*BASER(JY+4)                           DERAN     181
         SRAN(JY+7)=VALIN(12)                                           DERAN     182
      ENDIF                                                             DERAN     183
C                                                                       DERAN     184
      IF(NSUBD.EQ.1) THEN                                               DERAN     185
         CALL RKG(NXS,JSTATE,XSTATE,DSTATE,QSTATE,ZSTATE,H)             DERAN     186
         IF(LIR4) THEN                                                  DERAN     187
            JYUDM=JY+10                                                 DERAN     188
            DO 150 J=1,NUDB                                             DERAN     189
            YVAR(JYUDM+J)=VALOUT(J)                                     DERAN     190
  150       CONTINUE                                                    DERAN     191
            CALL UDMNWL(3)                                              DERAN     192
         ENDIF                                                          DERAN     193
      ELSE                                                              DERAN     194
         CALL ICOEF1                                                    DERAN     195
         CALL RKG1(NXS,0,JSTATE,XSTATE,DSTATE,QSTATE,ZSTATE,HSUB)       DERAN     196
         IF(IR1.LT.4) GO TO 140                                         DERAN     197
         IF(JJ.EQ.NSUBD) THEN                                           DERAN     198
            JYUDM=JY+10                                                 DERAN     199
            DO 160 J=1,NUDB                                             DERAN     200
            YVAR(JYUDM+J)=VALOUT(J)                                     DERAN     201
  160       CONTINUE                                                    DERAN     202
         ENDIF                                                          DERAN     203
         CALL UDMNWL(3)                                                 DERAN     204
      ENDIF                                                             DERAN     205
  170 CONTINUE                                                          DERAN     206
      GO TO 500                                                         DERAN     207
  180 CONTINUE                                                          DERAN     208
  500 CONTINUE                                                          DERAN     209
      RETURN                                                            DERAN     210
C                                                                       DERAN     211
      END                                                               DERAN     212
