CFPRIME DE4                                                             DE4         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 DE4(H)                                                 DE4         3
CD                                                                      DE4         4
CD    TO CALCULATE THE DERIVATIVES OF THE STATES OF TYPE 4 EXCITER,     DE4         5
CD    USED BY BOTH RUNGE-KUTTA AND TRAPEZOIDAL METHOD.                  DE4         6
CD                                                                      DE4         7
CD    THE DATA STRUCTURE FOR THIS TYPE OF EXCITER IS GIVEN BELOW.       DE4         8
CD                                                                      DE4         9
CD   3.3 Exciter type 4                                                 DE4        10
CD                                                                      DE4        11
CD     Exciter data                                                     DE4        12
CD                                                                      DE4        13
CD     IDDAT(JE+ 1)   = IVTN         : sensing bus number               DE4        14
CD     DDAT(JE+ 2)    = KA                                              DE4        15
CD     DDAT(JE+ 3)    = TA                                              DE4        16
CD     DDAT(JE+ 4)    = KE                                              DE4        17
CD     DDAT(JE+ 5)    = TE                                              DE4        18
CD     DDAT(JE+ 6)    = SE(VSAT) or AEX                                 DE4        19
CD     DDAT(JE+ 7)    = SE(0.75VSAT) or BEX                             DE4        20
CD     DDAT(JE+ 8)    = VSAT                                            DE4        21
CD     DDAT(JE+ 9)    = KC                                              DE4        22
CD     DDAT(JE+10)    = KF                                              DE4        23
CD     DDAT(JE+11)    = TF                                              DE4        24
CD     DDAT(JE+12)    = TC                                              DE4        25
CD     DDAT(JE+13)    = TB                                              DE4        26
CD     DDAT(JE+14)    = VAMAX                                           DE4        27
CD     DDAT(JE+15)    = VAMIN                                           DE4        28
CD     DDAT(JE+16)    = VEMIN                                           DE4        29
CD     DDAT(JE+17)    = KR                                              DE4        30
CD     DDAT(JE+18)    = TR                                              DE4        31
CD     DDAT(JE+19)    = RC                                              DE4        32
CD     DDAT(JE+20)    = XC                                              DE4        33
CD     DDAT(JE+21)    = XDE                                             DE4        34
CD     DDAT(JE+22)    = X'DE                                            DE4        35
CD     DDAT(JE+23)    = KN                                              DE4        36
CD     DDAT(JE+24)    = EFDN                                            DE4        37
CD     DDAT(JE+25)    = KLN                                             DE4        38
CD     DDAT(JE+26)    = VLN                                             DE4        39
CD     DDAT(JE+27)    = VFEMAX                                          DE4        40
CD     DDAT(JE+28)    = LVF                                             DE4        41
CD                                                                      DE4        42
CD     Exciter states                                                   DE4        43
CD                                                                      DE4        44
CD     XSTATE(JES+1)  = TR block     : compulsory                       DE4        45
CD     XSTATE(JES+2)  = TF block     : compulsory                       DE4        46
CD     XSTATE(JES+3)  = TB block     : compulsory                       DE4        47
CD     XSTATE(JES+4)  = TA block     : compulsory                       DE4        48
CD     XSTATE(JES+5)  = TE block     : compulsory                       DE4        49
CD                                                                      DE4        50
CD     Exciter Y-variables                                              DE4        51
CD                                                                      DE4        52
CD     VREG(JYE+1)    = VC (input to the TR block)                      DE4        53
CD     VREF(JYE+2)    = reference value                                 DE4        54
CD                                                                      DE4        55
      INCLUDE 'PARX.INS'                                                DE4        56
      INCLUDE 'CNPT.INS'                                                DE4        57
      INCLUDE 'CNVOLT.INS'                                              DE4        58
      INCLUDE 'CNIPTR.INS'                                              DE4        59
      INCLUDE 'CNMDAT.INS'                                              DE4        60
      INCLUDE 'CNB3.INS'                                                DE4        61
      INCLUDE 'CNB5.INS'                                                DE4        62
      INCLUDE 'CNB8.INS'                                                DE4        63
      INCLUDE 'CNB9.INS'                                                DE4        64
      INCLUDE 'CNB10.INS'                                               DE4        65
      INCLUDE 'CNB19.INS'                                               DE4        66
      INCLUDE 'CNEXC.INS'                                               DE4        67
      INCLUDE 'CNXSTA.INS'                                              DE4        68
      INCLUDE 'CNQSTA.INS'                                              DE4        69
      INCLUDE 'CNMYVA.INS'                                              DE4        70
      INCLUDE 'CNYVAR.INS'                                              DE4        71
      INCLUDE 'CNPREV.INS'                                              DE4        72
      INCLUDE 'CNIMPR.INS'                                              DE4        73
C                                                                       DE4        74
      LOGICAL SLOGIC                                                    DE4        75
C                                                                       DE4        76
C     DECIDE IF SMALLER TIME STEP SHOULD BE TAKEN                       DE4        77
C                                                                       DE4        78
      DELT=0.0                                                          DE4        79
      IF (IDDAT(JE+30).NE.0) THEN                                       DE4        80
         IF (H.LE.DDAT(JE+31)) THEN                                     DE4        81
            SLOGIC=.FALSE.                                              DE4        82
            KTM=1                                                       DE4        83
         ELSE                                                           DE4        84
            SLOGIC=.TRUE.                                               DE4        85
            KTM=INT(H/DDAT(JE+31)/KSBINT)+1                             DE4        86
            DELT=H/KTM/KSBINT                                           DE4        87
         END IF                                                         DE4        88
      ELSE                                                              DE4        89
         SLOGIC=.FALSE.                                                 DE4        90
         KTM=1                                                          DE4        91
      END IF                                                            DE4        92
C                                                                       DE4        93
      NXS=5                                                             DE4        94
      VRMAX=DDAT(JE+14)                                                 DE4        95
      VRMIN=DDAT(JE+15)                                                 DE4        96
      VEMIN=DDAT(JE+16)                                                 DE4        97
C                                                                       DE4        98
      DO 500 ITM=1,KTM                                                  DE4        99
C                                                                       DE4       100
      VE=XSTATE(JSTATE+5)                                               DE4       101
      SEKX=DDAT(JE+4)*VE                                                DE4       102
      SEKX=SEKX+DDAT(JE+6)*EXP(DDAT(JE+7)*VE)                           DE4       103
      VEMAX=(DDAT(JE+27)-(DDAT(JE+21)-DDAT(JE+22))*CFDN)*VE/SEKX        DE4       104
      SEKX=SEKX+CFDN*(DDAT(JE+21)-DDAT(JE+22))                          DE4       105
C                                                                       DE4       106
C     RATE FEEDBACK BLOCK                                               DE4       107
C                                                                       DE4       108
      XRATE=EFDN                                                        DE4       109
      IF(XRATE.GT.DDAT(JE+24)) THEN                                     DE4       110
      XRATE=DDAT(JE+24)*DDAT(JE+10)+(XRATE-DDAT(JE+24))*                DE4       111
     +      DDAT(JE+23)                                                 DE4       112
      ELSE                                                              DE4       113
      XRATE=XRATE*DDAT(JE+10)                                           DE4       114
      END IF                                                            DE4       115
      IF(IDDAT(JE+11).NE.0)THEN                                         DE4       116
      DSTATE(LSTATE+2)=(XRATE-XSTATE(JSTATE+2))/DDAT(JE+11)             DE4       117
      V2=DSTATE(LSTATE+2)                                               DE4       118
      ELSE                                                              DE4       119
      V2=0.                                                             DE4       120
      DSTATE(LSTATE+2)=0.0                                              DE4       121
      XSTATE(JSTATE+2)=XRATE                                            DE4       122
      ENDIF                                                             DE4       123
C                                                                       DE4       124
      VSUM=VREF(JYE+2)-V1                                               DE4       125
      IF (DDAT(JE+28).GT.0.5) VSUM=VREF(JYE+2)-V1-V2                    DE4       126
      IF (ISTABN.GT.0) VSUM=VSUM+VSS(JYS+1)                             DE4       127
      IF (IUALN.NE.0.AND.LVUEL.EQ.1) VSUM=VSUM+VUAL(JYU+1)              DE4       128
      IF (ITEBN.NE.0) VSUM=VSUM+TEB(JYTB+1)                             DE4       129
      IF (IMXLN.NE.0) VSUM=VSUM-VMXL(JYMX+1)                            DE4       130
C                                                                       DE4       131
C     LEAD/LAG BLOCK                                                    DE4       132
C                                                                       DE4       133
      IF (IDDAT(JE+13).NE.0)THEN                                        DE4       134
C                                                                       DE4       135
C     TB NE 0.0                                                         DE4       136
C                                                                       DE4       137
      DSTATE(LSTATE+3)=(VSUM-XSTATE(JSTATE+3))/DDAT(JE+13)              DE4       138
      V3=XSTATE(JSTATE+3)+DDAT(JE+12)*DSTATE(LSTATE+3)                  DE4       139
C                                                                       DE4       140
C     TB = 0                                                            DE4       141
C                                                                       DE4       142
      ELSE                                                              DE4       143
      V3=VSUM                                                           DE4       144
      DSTATE(LSTATE+3)=0.0                                              DE4       145
      XSTATE(JSTATE+3)=V3                                               DE4       146
      ENDIF                                                             DE4       147
C                                                                       DE4       148
C     ALTERNATE VUEL CONNECTION POINT                                   DE4       149
C                                                                       DE4       150
      IF (IUALN.NE.0.AND.LVUEL.EQ.2) THEN                               DE4       151
         VKLV=VUAL(JYU+1)                                               DE4       152
      ELSE                                                              DE4       153
         VKLV=(DDAT(JE+26)-EFDN)*DDAT(JE+25)                            DE4       154
      END IF                                                            DE4       155
C                                                                       DE4       156
      IF (V3.GE.VKLV) THEN                                              DE4       157
         IDDAT(JE+29)=1                                                 DE4       158
      ELSE                                                              DE4       159
         V3=VKLV                                                        DE4       160
         IDDAT(JE+29)=2                                                 DE4       161
      END IF                                                            DE4       162
C                                                                       DE4       163
C     CONNECTION FOR IEEE TYPE AC3A                                     DE4       164
C                                                                       DE4       165
      IF (DDAT(JE+28).LT.0.5) V3=V3-V2                                  DE4       166
C                                                                       DE4       167
C     AMPILIFIER BLOCK                                                  DE4       168
C                                                                       DE4       169
      IF(IDDAT(JE+3).NE.0)THEN                                          DE4       170
C                                                                       DE4       171
C     TA.NE.0                                                           DE4       172
C                                                                       DE4       173
      DSTATE(LSTATE+4)=(DDAT(JE+2)*V3-XSTATE(JSTATE+4))/DDAT(JE+3)      DE4       174
      VR=XSTATE(JSTATE+4)                                               DE4       175
C                                                                       DE4       176
C     TA=0                                                              DE4       177
C                                                                       DE4       178
      ELSE                                                              DE4       179
C                                                                       DE4       180
      VR=DDAT(JE+2)*V3                                                  DE4       181
      DSTATE(LSTATE+4)=0.                                               DE4       182
      XSTATE(JSTATE+4)=VR                                               DE4       183
C                                                                       DE4       184
      ENDIF                                                             DE4       185
C                                                                       DE4       186
      IF (VR.GE.VRMAX)THEN                                              DE4       187
         VR=VRMAX                                                       DE4       188
         IF (DSTATE(LSTATE+4).GT.0.0) THEN                              DE4       189
            DSTATE(LSTATE+4)=0.0                                        DE4       190
            QSTATE(JSTATE+4)=0.0                                        DE4       191
         END IF                                                         DE4       192
      ELSE IF (VR.LE.VRMIN) THEN                                        DE4       193
         VR=VRMIN                                                       DE4       194
         IF (DSTATE(LSTATE+4).LT.0.0) THEN                              DE4       195
            DSTATE(LSTATE+4)=0.0                                        DE4       196
            QSTATE(JSTATE+4)=0.0                                        DE4       197
         END IF                                                         DE4       198
      END IF                                                            DE4       199
C                                                                       DE4       200
      VEIN=DDAT(JE+17)*EFDN*VR-SEKX                                     DE4       201
C                                                                       DE4       202
      DSTATE(LSTATE+5)=VEIN/DDAT(JE+5)                                  DE4       203
C                                                                       DE4       204
C  NON-WINDUP LIMITS ON 'VE'                                            DE4       205
C                                                                       DE4       206
      IF (VE.GE.VEMAX) THEN                                             DE4       207
         VE=VEMAX                                                       DE4       208
         IF (DSTATE(LSTATE+5).GT.0.0) THEN                              DE4       209
            DSTATE(LSTATE+5)=0.0                                        DE4       210
            QSTATE(JSTATE+5)=0.0                                        DE4       211
         END IF                                                         DE4       212
      ELSE IF (VE.LE.VEMIN) THEN                                        DE4       213
         VE=VEMIN                                                       DE4       214
         IF (DSTATE(LSTATE+5).LT.0.0) THEN                              DE4       215
            DSTATE(LSTATE+5)=0.0                                        DE4       216
            QSTATE(JSTATE+5)=0.0                                        DE4       217
         END IF                                                         DE4       218
      END IF                                                            DE4       219
C                                                                       DE4       220
C     EULER FORWARD METHOD                                              DE4       221
C                                                                       DE4       222
      IF (SLOGIC) THEN                                                  DE4       223
         DO 310 JX=2,5                                                  DE4       224
            XSTATE(JSTATE+JX)=XSTATE(JSTATE+JX)+DELT*DSTATE(LSTATE+JX)  DE4       225
  310    CONTINUE                                                       DE4       226
         IF (XSTATE(JSTATE+4).GT.VRMAX) THEN                            DE4       227
            XSTATE(JSTATE+4)=VRMAX                                      DE4       228
         ELSE IF (XSTATE(JSTATE+4).LT.VRMIN) THEN                       DE4       229
            XSTATE(JSTATE+4)=VRMIN                                      DE4       230
         END IF                                                         DE4       231
         IF (XSTATE(JSTATE+5).GT.VEMAX) THEN                            DE4       232
            XSTATE(JSTATE+5)=VEMAX                                      DE4       233
         ELSE IF (XSTATE(JSTATE+5).LT.VEMIN) THEN                       DE4       234
            XSTATE(JSTATE+5)=VEMIN                                      DE4       235
         END IF                                                         DE4       236
         VE=XSTATE(JSTATE+5)                                            DE4       237
C                                                                       DE4       238
C     RECTIFIER LOADING EFFECTS                                         DE4       239
C                                                                       DE4       240
         VTH=VE-CFDN*DDAT(JE+22)                                        DE4       241
         EFDN=VTH                                                       DE4       242
         IF (IDDAT(JE+9).NE.0) THEN                                     DE4       243
            IF (VTH.GT.0.0) THEN                                        DE4       244
               FACT=DDAT(JE+9)*CFDN/VTH                                 DE4       245
               IF (FACT.GE.0.75) THEN                                   DE4       246
                  EFDN=VTH*1.732*(1.0-FACT)                             DE4       247
               ELSE IF (FACT.GE.0.433) THEN                             DE4       248
                  EFDN=VTH*SQRT(0.75-FACT*FACT)                         DE4       249
               ELSE                                                     DE4       250
                  EFDN=VTH*(1.0-0.577*FACT)                             DE4       251
               END IF                                                   DE4       252
            END IF                                                      DE4       253
         END IF                                                         DE4       254
         IF (EFDN.LT.0.0) EFDN=0.0                                      DE4       255
      END IF                                                            DE4       256
C                                                                       DE4       257
  500 CONTINUE                                                          DE4       258
C                                                                       DE4       259
C     IF STATES ARE UPDATED BY INNER LOOP, FOOL THE RKG                 DE4       260
C                                                                       DE4       261
      IF (SLOGIC) THEN                                                  DE4       262
         DO 320 JX=2,5                                                  DE4       263
            DSTATE(LSTATE+JX)=0.0                                       DE4       264
  320    CONTINUE                                                       DE4       265
         EFD(JYG+4)=EFDN                                                DE4       266
      ELSE                                                              DE4       267
C                                                                       DE4       268
C     RECTIFIER LOADING EFFECTS                                         DE4       269
C                                                                       DE4       270
         VTH=VE-CFDN*DDAT(JE+22)                                        DE4       271
         EFD(JYG+4)=VTH                                                 DE4       272
         IF (IDDAT(JE+9).NE.0) THEN                                     DE4       273
            IF (VTH.GT.0.0) THEN                                        DE4       274
               FACT=DDAT(JE+9)*CFDN/VTH                                 DE4       275
               IF (FACT.GE.0.75) THEN                                   DE4       276
                  EFD(JYG+4)=VTH*1.732*(1.0-FACT)                       DE4       277
               ELSE IF (FACT.GE.0.433) THEN                             DE4       278
                  EFD(JYG+4)=VTH*SQRT(0.75-FACT*FACT)                   DE4       279
               ELSE                                                     DE4       280
                  EFD(JYG+4)=VTH*(1.0-0.577*FACT)                       DE4       281
               END IF                                                   DE4       282
            END IF                                                      DE4       283
            IF (EFD(JYG+4).LT.0.0) EFD(JYG+4)=0.0                       DE4       284
         END IF                                                         DE4       285
      END IF                                                            DE4       286
C                                                                       DE4       287
      LSTATE=LSTATE+NXS                                                 DE4       288
C                                                                       DE4       289
      RETURN                                                            DE4       290
C                                                                       DE4       291
      ENTRY DEI4(H)                                                     DE4       292
CD                                                                      DE4       293
CD    TO CALCULATE THE DERIVATIVES OF THE STATES OF TYPE 4 EXCITER,     DE4       294
CD    USED BY TRAPEZOIDAL METHOD.                                       DE4       295
CD                                                                      DE4       296
C                                                                       DE4       297
C     DECIDE IF SMALLER TIME STEP SHOULD BE TAKEN                       DE4       298
C                                                                       DE4       299
      DELT=0.0                                                          DE4       300
      IF (IDDAT(JE+30).NE.0) THEN                                       DE4       301
         IF (H.LE.DDAT(JE+31)) THEN                                     DE4       302
            SLOGIC=.FALSE.                                              DE4       303
            KTM=1                                                       DE4       304
         ELSE                                                           DE4       305
            SLOGIC=.TRUE.                                               DE4       306
            KTM=INT(H/DDAT(JE+31))+1                                    DE4       307
            DELT=H/KTM                                                  DE4       308
            IF (NITER.EQ.0) THEN                                        DE4       309
               DO 90 JX=2,5                                             DE4       310
                  XSTATE(JSTATE+JX)=XPREV(1,JSTATE+JX)                  DE4       311
   90          CONTINUE                                                 DE4       312
            END IF                                                      DE4       313
         END IF                                                         DE4       314
      ELSE                                                              DE4       315
         SLOGIC=.FALSE.                                                 DE4       316
         KTM=1                                                          DE4       317
      END IF                                                            DE4       318
C                                                                       DE4       319
      NXS=5                                                             DE4       320
C                                                                       DE4       321
C     BYPASS IF SUBDIVISION IS ON AND NITER > 0                         DE4       322
C                                                                       DE4       323
      IF (SLOGIC.AND.NITER.GT.0) THEN                                   DE4       324
         EFDN=EFD(JYG+4)                                                DE4       325
         GOTO 130                                                       DE4       326
      END IF                                                            DE4       327
C                                                                       DE4       328
      VRMAX=DDAT(JE+14)                                                 DE4       329
      VRMIN=DDAT(JE+15)                                                 DE4       330
      VEMIN=DDAT(JE+16)                                                 DE4       331
C                                                                       DE4       332
      DO 100 ITM=1,KTM                                                  DE4       333
C                                                                       DE4       334
      VE=XSTATE(JSTATE+5)                                               DE4       335
      SEKX=DDAT(JE+4)*VE                                                DE4       336
      SEKX=SEKX+DDAT(JE+6)*EXP(DDAT(JE+7)*VE)                           DE4       337
      VEMAX=(DDAT(JE+27)-(DDAT(JE+21)-DDAT(JE+22))*CFDN)*VE/SEKX        DE4       338
      SEKX=SEKX+CFDN*(DDAT(JE+21)-DDAT(JE+22))                          DE4       339
C                                                                       DE4       340
C     RATE FEEDBACK BLOCK                                               DE4       341
C                                                                       DE4       342
      XRATE=EFDN                                                        DE4       343
      IF(XRATE.GT.DDAT(JE+24)) THEN                                     DE4       344
      XRATE=DDAT(JE+24)*DDAT(JE+10)+(XRATE-DDAT(JE+24))*                DE4       345
     +      DDAT(JE+23)                                                 DE4       346
      ELSE                                                              DE4       347
      XRATE=XRATE*DDAT(JE+10)                                           DE4       348
      END IF                                                            DE4       349
      IF(IDDAT(JE+11).NE.0)THEN                                         DE4       350
      DSTATE(LSTATE+2)=(XRATE-XSTATE(JSTATE+2))/DDAT(JE+11)             DE4       351
      V2=DSTATE(LSTATE+2)                                               DE4       352
      ELSE                                                              DE4       353
      V2=0.                                                             DE4       354
      DSTATE(LSTATE+2)=0.0                                              DE4       355
      XSTATE(JSTATE+2)=XRATE                                            DE4       356
      ENDIF                                                             DE4       357
C                                                                       DE4       358
      VSUM=VREF(JYE+2)-V1                                               DE4       359
      IF (DDAT(JE+28).GT.0.5) VSUM=VREF(JYE+2)-V1-V2                    DE4       360
      IF (ISTABN.GT.0) VSUM=VSUM+VSS(JYS+1)                             DE4       361
      IF (IUALN.NE.0.AND.LVUEL.EQ.1) VSUM=VSUM+VUAL(JYU+1)              DE4       362
      IF (ITEBN.NE.0) VSUM=VSUM+TEB(JYTB+1)                             DE4       363
      IF (IMXLN.NE.0) VSUM=VSUM-VMXL(JYMX+1)                            DE4       364
C                                                                       DE4       365
C     LEAD/LAG BLOCK                                                    DE4       366
C                                                                       DE4       367
      IF (IDDAT(JE+13).NE.0)THEN                                        DE4       368
C                                                                       DE4       369
C     TB NE 0.0                                                         DE4       370
C                                                                       DE4       371
      DSTATE(LSTATE+3)=(VSUM-XSTATE(JSTATE+3))/DDAT(JE+13)              DE4       372
      V3=XSTATE(JSTATE+3)+DDAT(JE+12)*DSTATE(LSTATE+3)                  DE4       373
C                                                                       DE4       374
C     TB = 0                                                            DE4       375
C                                                                       DE4       376
      ELSE                                                              DE4       377
      V3=VSUM                                                           DE4       378
      DSTATE(LSTATE+3)=0.0                                              DE4       379
      XSTATE(JSTATE+3)=V3                                               DE4       380
      ENDIF                                                             DE4       381
C                                                                       DE4       382
C     ALTERNATE VUEL CONNECTION POINT                                   DE4       383
C                                                                       DE4       384
      IF (IUALN.NE.0.AND.LVUEL.EQ.2) THEN                               DE4       385
         VKLV=VUAL(JYU+1)                                               DE4       386
      ELSE                                                              DE4       387
         VKLV=(DDAT(JE+26)-EFDN)*DDAT(JE+25)                            DE4       388
      ENDIF                                                             DE4       389
C                                                                       DE4       390
      IF (V3.GE.VKLV) THEN                                              DE4       391
         IDDAT(JE+29)=1                                                 DE4       392
      ELSE                                                              DE4       393
         V3=VKLV                                                        DE4       394
         IDDAT(JE+29)=2                                                 DE4       395
      END IF                                                            DE4       396
C                                                                       DE4       397
C     CONNECTION FOR IEEE TYPE AC3A                                     DE4       398
C                                                                       DE4       399
      IF (DDAT(JE+28).LT.0.5) V3=V3-V2                                  DE4       400
C                                                                       DE4       401
C   AMPILIFIER BLOCK                                                    DE4       402
C                                                                       DE4       403
      IF(IDDAT(JE+3).NE.0)THEN                                          DE4       404
C                                                                       DE4       405
C   TA.NE.0                                                             DE4       406
C                                                                       DE4       407
      DSTATE(LSTATE+4)=(DDAT(JE+2)*V3-XSTATE(JSTATE+4))/DDAT(JE+3)      DE4       408
      VR=XSTATE(JSTATE+4)                                               DE4       409
C                                                                       DE4       410
C   TA=0                                                                DE4       411
C                                                                       DE4       412
      ELSE                                                              DE4       413
C                                                                       DE4       414
      VR=DDAT(JE+2)*V3                                                  DE4       415
      DSTATE(LSTATE+4)=0.                                               DE4       416
      XSTATE(JSTATE+4)=VR                                               DE4       417
C                                                                       DE4       418
      ENDIF                                                             DE4       419
C                                                                       DE4       420
      IF (VR.GE.VRMAX)THEN                                              DE4       421
         VR=VRMAX                                                       DE4       422
         IF (DSTATE(LSTATE+4).GT.0.0) DSTATE(LSTATE+4)=0.0              DE4       423
      ELSE IF (VR.LE.VRMIN) THEN                                        DE4       424
         VR=VRMIN                                                       DE4       425
         IF (DSTATE(LSTATE+4).LT.0.0) DSTATE(LSTATE+4)=0.0              DE4       426
      END IF                                                            DE4       427
C                                                                       DE4       428
      VEIN=DDAT(JE+17)*EFDN*VR-SEKX                                     DE4       429
C                                                                       DE4       430
      DSTATE(LSTATE+5)=VEIN/DDAT(JE+5)                                  DE4       431
C                                                                       DE4       432
C  NON-WINDUP LIMITS ON 'VE'                                            DE4       433
C                                                                       DE4       434
      IF (VE.GE.VEMAX) THEN                                             DE4       435
         VE=VEMAX                                                       DE4       436
         IF (DSTATE(LSTATE+5).GT.0.0) DSTATE(LSTATE+5)=0.0              DE4       437
      ELSE IF (VE.LE.VEMIN) THEN                                        DE4       438
         VE=VEMIN                                                       DE4       439
         IF (DSTATE(LSTATE+5).LT.0.0) DSTATE(LSTATE+5)=0.0              DE4       440
      END IF                                                            DE4       441
C                                                                       DE4       442
C     EULER FORWARD METHOD                                              DE4       443
C                                                                       DE4       444
      IF (SLOGIC) THEN                                                  DE4       445
         DO 110 JX=2,5                                                  DE4       446
            XSTATE(JSTATE+JX)=XSTATE(JSTATE+JX)+DELT*DSTATE(LSTATE+JX)  DE4       447
  110    CONTINUE                                                       DE4       448
         IF (XSTATE(JSTATE+4).GT.VRMAX) THEN                            DE4       449
            XSTATE(JSTATE+4)=VRMAX                                      DE4       450
         ELSE IF (XSTATE(JSTATE+4).LT.VRMIN) THEN                       DE4       451
            XSTATE(JSTATE+4)=VRMIN                                      DE4       452
         END IF                                                         DE4       453
         IF (XSTATE(JSTATE+5).GT.VEMAX) THEN                            DE4       454
            XSTATE(JSTATE+5)=VEMAX                                      DE4       455
         ELSE IF (XSTATE(JSTATE+5).LT.VEMIN) THEN                       DE4       456
            XSTATE(JSTATE+5)=VEMIN                                      DE4       457
         END IF                                                         DE4       458
         VE=XSTATE(JSTATE+5)                                            DE4       459
C                                                                       DE4       460
C     RECTIFIER LOADING EFFECTS                                         DE4       461
C                                                                       DE4       462
         VTH=VE-CFDN*DDAT(JE+22)                                        DE4       463
         EFDN=VTH                                                       DE4       464
         IF (IDDAT(JE+9).NE.0) THEN                                     DE4       465
            IF (VTH.GT.0.0) THEN                                        DE4       466
               FACT=DDAT(JE+9)*CFDN/VTH                                 DE4       467
               IF (FACT.GE.0.75) THEN                                   DE4       468
                  EFDN=VTH*1.732*(1.0-FACT)                             DE4       469
               ELSE IF (FACT.GE.0.433) THEN                             DE4       470
                  EFDN=VTH*SQRT(0.75-FACT*FACT)                         DE4       471
               ELSE                                                     DE4       472
                  EFDN=VTH*(1.0-0.577*FACT)                             DE4       473
               END IF                                                   DE4       474
            END IF                                                      DE4       475
         END IF                                                         DE4       476
         IF (EFDN.LT.0.0) EFDN=0.0                                      DE4       477
      END IF                                                            DE4       478
C                                                                       DE4       479
  100 CONTINUE                                                          DE4       480
C                                                                       DE4       481
C     IF STATES ARE UPDATED BY INNER LOOP, FOOL THE RKG                 DE4       482
C                                                                       DE4       483
  130 IF (SLOGIC) THEN                                                  DE4       484
         DO 140 JX=2,5                                                  DE4       485
            DSTATE(LSTATE+JX)=0.0                                       DE4       486
  140    CONTINUE                                                       DE4       487
         EFD(JYG+4)=EFDN                                                DE4       488
      ELSE                                                              DE4       489
C                                                                       DE4       490
C     RECTIFIER LOADING EFFECTS                                         DE4       491
C                                                                       DE4       492
         VTH=VE-CFDN*DDAT(JE+22)                                        DE4       493
         EFD(JYG+4)=VTH                                                 DE4       494
         IF (IDDAT(JE+9).NE.0) THEN                                     DE4       495
            IF (VTH.GT.0.0) THEN                                        DE4       496
               FACT=DDAT(JE+9)*CFDN/VTH                                 DE4       497
               IF (FACT.GE.0.75) THEN                                   DE4       498
                  EFD(JYG+4)=VTH*1.732*(1.0-FACT)                       DE4       499
               ELSE IF (FACT.GE.0.433) THEN                             DE4       500
                  EFD(JYG+4)=VTH*SQRT(0.75-FACT*FACT)                   DE4       501
               ELSE                                                     DE4       502
                  EFD(JYG+4)=VTH*(1.0-0.577*FACT)                       DE4       503
               END IF                                                   DE4       504
            END IF                                                      DE4       505
            IF (EFD(JYG+4).LT.0.0) EFD(JYG+4)=0.0                       DE4       506
         END IF                                                         DE4       507
      END IF                                                            DE4       508
C                                                                       DE4       509
      LSTATE=LSTATE+NXS                                                 DE4       510
C                                                                       DE4       511
      RETURN                                                            DE4       512
      END                                                               DE4       513
