CFPRIME IUDB29                                                          IUDB29      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 IUDB29(IORD,INDEX,NLF,KN,CID)                          IUDB29      3
CD                                                                      IUDB29      4
CD    TO PERFORM INITIALIZATION FOR USER DEFINED BLOCK TYPE 23 (HYST    IUDB29      5
CD    BLOCK) IN ANY USER DEFINED MODEL.                                 IUDB29      6
CD                                                                      IUDB29      7
      INCLUDE 'PARX.INS'                                                IUDB29      8
      INCLUDE 'CNMDAT.INS'                                              IUDB29      9
      INCLUDE 'CNMSTA.INS'                                              IUDB29     10
      INCLUDE 'CNXSTA.INS'                                              IUDB29     11
      INCLUDE 'CNSTN.INS'                                               IUDB29     12
      INCLUDE 'CNYVAR.INS'                                              IUDB29     13
      INCLUDE 'CNUNIT.INS'                                              IUDB29     14
      INCLUDE 'CNFLAG.INS'                                              IUDB29     15
      INCLUDE 'CNUDM.INS'                                               IUDB29     16
      INCLUDE 'CNMSG.INS'                                               IUDB29     17
C                                                                       IUDB29     18
      CHARACTER*2 CID                                                   IUDB29     19
      DIMENSION XX(4),YY(4)                                             IUDB29     20
C                                                                       IUDB29     21
      LDEB=(JDEB(262).NE.0)                                             IUDB29     22
C                                                                       IUDB29     23
      VALI=0.0                                                          IUDB29     24
      NEXTB=0                                                           IUDB29     25
      JORD=JUDE(IORD)                                                   IUDB29     26
      JEI=JEB+2                                                         IUDB29     27
      DO 100 I=1,4                                                      IUDB29     28
      XX(I)=DDAT(JEI+I)                                                 IUDB29     29
      YY(I)=DDAT(JEI+I+6)                                               IUDB29     30
  100 CONTINUE                                                          IUDB29     31
      IF(XX(2).LE.XX(1).OR.XX(3).LE.XX(1).OR.XX(4).LE.XX(2).OR.         IUDB29     32
     &   XX(4).LE.XX(3))THEN                                            IUDB29     33
         WRITE(MSG(1),1010) NLF,INAME(KN-2),INAME(KN-1),INAME(KN),CID   IUDB29     34
         WRITE(MSG(2),1020) IORD,BLNAM1,BLNAM2                          IUDB29     35
         WRITE(MSG(3),1030)                                             IUDB29     36
         WRITE(MSG(4),1040) (XX(J),J=1,4)                               IUDB29     37
         CALL PRTMSG(4)                                                 IUDB29     38
         MSG(1)='IUDB29: UDM ERROR'                                     IUDB29     39
         CALL ASTOP(MSG(1),17)                                          IUDB29     40
      ENDIF                                                             IUDB29     41
      IF(YY(2).LE.YY(1).OR.YY(3).LE.YY(1).OR.YY(4).LE.YY(2).OR.         IUDB29     42
     &   YY(4).LE.YY(3))THEN                                            IUDB29     43
         WRITE(MSG(1),1010) NLF,INAME(KN-2),INAME(KN-1),INAME(KN),CID   IUDB29     44
         WRITE(MSG(2),1070) IORD,BLNAM1,BLNAM2                          IUDB29     45
         WRITE(MSG(3),1080)                                             IUDB29     46
         WRITE(MSG(4),1060) (YY(J),J=1,4)                               IUDB29     47
         CALL PRTMSG(4)                                                 IUDB29     48
         MSG(1)='IUDB29: UDM ERROR'                                     IUDB29     49
         CALL ASTOP(MSG(1),17)                                          IUDB29     50
      ENDIF                                                             IUDB29     51
C                                                                       IUDB29     52
      IF(INDEX.EQ.1.OR.INDEX.EQ.2) THEN                                 IUDB29     53
         J2=JE5-2                                                       IUDB29     54
         DO 110 J=1,INTUD                                               IUDB29     55
         J2=J2+2                                                        IUDB29     56
         IDD=IDDAT(J2+2)                                                IUDB29     57
         IDD=ABS(IDD)                                                   IUDB29     58
         IF(IDD.EQ.IORD) THEN                                           IUDB29     59
            XDAT=1.0                                                    IUDB29     60
            IF(IDDAT(J2+2).LT.0) XDAT=-1.0                              IUDB29     61
            IOUT=IDDAT(J2+1)                                            IUDB29     62
            LTYP1=IDDAT(JE3+17*IOUT-15)                                 IUDB29     63
            IF(JUDE(IOUT).EQ.0.AND.                                     IUDB29     64
     &      (LTYP1.NE.4.AND.LTYP1.NE.6)) THEN                           IUDB29     65
               NEXTB=IOUT                                               IUDB29     66
               XDATN=XDAT                                               IUDB29     67
            ELSE                                                        IUDB29     68
               VALI=VALI+VALOUT(IOUT)*XDAT                              IUDB29     69
            ENDIF                                                       IUDB29     70
         ENDIF                                                          IUDB29     71
  110    CONTINUE                                                       IUDB29     72
      ENDIF                                                             IUDB29     73
      J3=JE6-3                                                          IUDB29     74
      DO 120 J=1,INPUD                                                  IUDB29     75
      J3=J3+3                                                           IUDB29     76
      IF(IDDAT(J3+1).EQ.IORD) THEN                                      IUDB29     77
         IIN=IDDAT(J3+2)                                                IUDB29     78
         IF(IIN.EQ.20) THEN                                             IUDB29     79
            XDATN=DDAT(J3+3)                                            IUDB29     80
         ELSE                                                           IUDB29     81
            VALI=VALI+VALIN(IIN)*DDAT(J3+3)                             IUDB29     82
         ENDIF                                                          IUDB29     83
      ENDIF                                                             IUDB29     84
  120 CONTINUE                                                          IUDB29     85
      IF(INDEX.EQ.2) THEN                                               IUDB29     86
         VALO=VALOUT(IORD)                                              IUDB29     87
         IF(VALO.GE.YY(4)) THEN                                         IUDB29     88
            SLOPE=(YY(4)-YY(2))/(XX(4)-XX(2))                           IUDB29     89
            VALI1=XX(2)+(VALO-YY(2))/SLOPE                              IUDB29     90
         ELSEIF(VALO.LE.YY(1)) THEN                                     IUDB29     91
            SLOPE=(YY(3)-YY(1))/(XX(3)-XX(1))                           IUDB29     92
            VALI1=XX(1)+(VALO-YY(1))/SLOPE                              IUDB29     93
         ELSE                                                           IUDB29     94
            VALI1=DDAT(JEI+5)                                           IUDB29     95
            S1=(YY(4)-YY(2))/(XX(4)-XX(2))                              IUDB29     96
            S2=(YY(3)-YY(1))/(XX(3)-XX(1))                              IUDB29     97
            S3=(YY(2)-YY(1))/(XX(2)-XX(1))                              IUDB29     98
            S4=(YY(4)-YY(3))/(XX(4)-XX(3))                              IUDB29     99
            SLOPE=S1                                                    IUDB29    100
            VALIX1=XX(2)+(VALO-YY(2))/S1                                IUDB29    101
            VALIX2=XX(1)+(VALO-YY(1))/S2                                IUDB29    102
            VALIX3=XX(1)+(VALO-YY(1))/S3                                IUDB29    103
            VALIX4=XX(3)+(VALO-YY(3))/S4                                IUDB29    104
            IF(VALI1.GT.XX(4)) THEN                                     IUDB29    105
               VALI1=XX(4)                                              IUDB29    106
               SLOPE=S1                                                 IUDB29    107
            ENDIF                                                       IUDB29    108
            IF(VALI1.LT.XX(1)) THEN                                     IUDB29    109
               VALI1=XX(1)                                              IUDB29    110
               SLOPE=S2                                                 IUDB29    111
            ENDIF                                                       IUDB29    112
            IF(VALI1.LT.VALIX1) THEN                                    IUDB29    113
               VALI1=VALIX1                                             IUDB29    114
               SLOPE=S1                                                 IUDB29    115
            ENDIF                                                       IUDB29    116
            IF(VALI1.GT.VALIX2) THEN                                    IUDB29    117
               VALI1=VALIX2                                             IUDB29    118
               SLOPE=S2                                                 IUDB29    119
            ENDIF                                                       IUDB29    120
            IF(VALI1.LT.VALIX3) THEN                                    IUDB29    121
               VALI1=VALIX3                                             IUDB29    122
               SLOPE=S3                                                 IUDB29    123
            ENDIF                                                       IUDB29    124
            IF(VALI1.GT.VALIX4) THEN                                    IUDB29    125
               VALI1=VALIX4                                             IUDB29    126
               SLOPE=S4                                                 IUDB29    127
            ENDIF                                                       IUDB29    128
         ENDIF                                                          IUDB29    129
         IF(JORD.LT.NUDB) THEN                                          IUDB29    130
            IF(NEXTB.EQ.0)THEN                                          IUDB29    131
            MSG(1)='IUDB29: 1'                                          IUDB29    132
            CALL ASTOP(MSG(1),9)                                        IUDB29    133
            ENDIF                                                       IUDB29    134
            VALOUT(NEXTB)=(VALI1-VALI)/XDATN                            IUDB29    135
         ELSE                                                           IUDB29    136
            VALIN(20)=(VALI1-VALI)/XDATN                                IUDB29    137
         ENDIF                                                          IUDB29    138
         DDAT(JEI+5)=VALI1                                              IUDB29    139
      ELSE                                                              IUDB29    140
         IF(VALI.GE.XX(4)) THEN                                         IUDB29    141
            SLOPE=(YY(4)-YY(2))/(XX(4)-XX(2))                           IUDB29    142
            VALO=YY(2)+SLOPE*(VALI-XX(2))                               IUDB29    143
         ELSEIF(VALI.LE.XX(1)) THEN                                     IUDB29    144
            SLOPE=(YY(3)-YY(1))/(XX(3)-XX(1))                           IUDB29    145
            VALO=YY(1)+SLOPE*(VALI-XX(1))                               IUDB29    146
         ELSE                                                           IUDB29    147
            VALO=DDAT(JEI+11)                                           IUDB29    148
            S1=(YY(4)-YY(2))/(XX(4)-XX(2))                              IUDB29    149
            S2=(YY(3)-YY(1))/(XX(3)-XX(1))                              IUDB29    150
            S3=(YY(2)-YY(1))/(XX(2)-XX(1))                              IUDB29    151
            S4=(YY(4)-YY(3))/(XX(4)-XX(3))                              IUDB29    152
            SLOPE=S1                                                    IUDB29    153
            VALO1=YY(2)+S1*(VALI-XX(2))                                 IUDB29    154
            VALO2=YY(1)+S2*(VALI-XX(1))                                 IUDB29    155
            VALO3=YY(1)+S3*(VALI-XX(1))                                 IUDB29    156
            VALO4=YY(3)+S4*(VALI-XX(3))                                 IUDB29    157
            IF(VALO.GT.YY(4)) THEN                                      IUDB29    158
               VALO=YY(4)                                               IUDB29    159
               SLOPE=S1                                                 IUDB29    160
            ENDIF                                                       IUDB29    161
            IF(VALO.LT.YY(1)) THEN                                      IUDB29    162
               VALO=YY(1)                                               IUDB29    163
               SLOPE=S2                                                 IUDB29    164
            ENDIF                                                       IUDB29    165
            IF(VALO.GT.VALO1) THEN                                      IUDB29    166
               VALO=VALO1                                               IUDB29    167
               SLOPE=S1                                                 IUDB29    168
            ENDIF                                                       IUDB29    169
            IF(VALO.LT.VALO2) THEN                                      IUDB29    170
               VALO=VALO2                                               IUDB29    171
               SLOPE=S2                                                 IUDB29    172
            ENDIF                                                       IUDB29    173
            IF(VALO.GT.VALO3) THEN                                      IUDB29    174
               VALO=VALO3                                               IUDB29    175
               SLOPE=S3                                                 IUDB29    176
            ENDIF                                                       IUDB29    177
            IF(VALO.LT.VALO4) THEN                                      IUDB29    178
               VALO=VALO4                                               IUDB29    179
               SLOPE=S4                                                 IUDB29    180
            ENDIF                                                       IUDB29    181
         ENDIF                                                          IUDB29    182
         VALOUT(IORD)=VALO                                              IUDB29    183
         DDAT(JEI+5)=VALI                                               IUDB29    184
      ENDIF                                                             IUDB29    185
      DDAT(JEI+6)=SLOPE                                                 IUDB29    186
      DDAT(JEI+11)=VALOUT(IORD)                                         IUDB29    187
C                                                                       IUDB29    188
      IF(LDEB)WRITE(IUD6,1000) JORD                                     IUDB29    189
C                                                                       IUDB29    190
      RETURN                                                            IUDB29    191
 1000 FORMAT(1X,'JORD: ',I5)                                            IUDB29    192
 1010 FORMAT(' *** ERROR USER DEFINED MODEL AT BUS:',I5,' STATION: '    IUDB29    193
     *,3A4,' ID:',A4)                                                   IUDB29    194
 1020 FORMAT(11X,'IN BLOCK#',I3,' (',2A4                                IUDB29    195
     *,') X VALUES MUST SATISFY')                                       IUDB29    196
 1030 FORMAT(11X,'X1<X2<X4 AND X1<X3<X4.')                              IUDB29    197
 1040 FORMAT(11X,'X VALUES: ',4F8.3)                                    IUDB29    198
 1060 FORMAT(11X,'Y VALUES: ',4F8.3)                                    IUDB29    199
 1070 FORMAT(11X,'IN BLOCK#',I3,' (',2A4                                IUDB29    200
     *,') Y VALUES MUST SATISFY')                                       IUDB29    201
 1080 FORMAT(11X,'Y1<Y2<Y4 AND Y1<Y3<Y4.')                              IUDB29    202
      END                                                               IUDB29    203
