CFPRIME IUDB22                                                          IUDB22      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 IUDB22(IORD,INDEX,NLF,KN,CID)                          IUDB22      3
CD                                                                      IUDB22      4
CD    TO PERFORM INITIALIZATION FOR USER DEFINED BLOCK TYPE 22 (STATE   IUDB22      5
CD    SPACE BLOCK) IN ANY USER DEFINED MODEL.                           IUDB22      6
CD                                                                      IUDB22      7
      INCLUDE 'PARX.INS'                                                IUDB22      8
      INCLUDE 'CNMDAT.INS'                                              IUDB22      9
      INCLUDE 'CNMSTA.INS'                                              IUDB22     10
      INCLUDE 'CNXSTA.INS'                                              IUDB22     11
      INCLUDE 'CNSTN.INS'                                               IUDB22     12
      INCLUDE 'CNYVAR.INS'                                              IUDB22     13
      INCLUDE 'CNUNIT.INS'                                              IUDB22     14
      INCLUDE 'CNFLAG.INS'                                              IUDB22     15
      INCLUDE 'CNUDM.INS'                                               IUDB22     16
      INCLUDE 'CNMSG.INS'                                               IUDB22     17
C                                                                       IUDB22     18
      CHARACTER*2 CID                                                   IUDB22     19
      DIMENSION VALI(8),AA(8,8),BB(8,8),CC(8),DD(8),DX(8,8)             IUDB22     20
     &          ,DD1(8),IR(8),IC(8)                                     IUDB22     21
      LDEB=(JDEB(262).NE.0)                                             IUDB22     22
C                                                                       IUDB22     23
      NEXTB=0                                                           IUDB22     24
      IBB=0                                                             IUDB22     25
      VALI1=0.0                                                         IUDB22     26
      CALL FMOVE(0,VALI,VALI,8)                                         IUDB22     27
      JORD=JUDE(IORD)                                                   IUDB22     28
      KORD=JSTATE+KUDE(IORD)                                            IUDB22     29
      JEI=JEB+2                                                         IUDB22     30
      NAA=IDDAT(JEI+1)                                                  IUDB22     31
      NBB=IDDAT(JEI+2)                                                  IUDB22     32
      IF(NAA.LT.1.OR.NAA.GT.8) THEN                                     IUDB22     33
         WRITE(MSG(1),1000) NLF,INAME(KN-2),INAME(KN-1),INAME(KN),CID   IUDB22     34
         WRITE(MSG(2),1070) IORD,BLNAM1,BLNAM2,NAA                      IUDB22     35
         CALL PRTMSG(2)                                                 IUDB22     36
         MSG(1)='IUDB22: UDM ERROR'                                     IUDB22     37
         CALL ASTOP(MSG(1),17)                                          IUDB22     38
      ENDIF                                                             IUDB22     39
      IF(NBB.LT.1.OR.NBB.GT.8) THEN                                     IUDB22     40
         WRITE(MSG(1),1000) NLF,INAME(KN-2),INAME(KN-1),INAME(KN),CID   IUDB22     41
         WRITE(MSG(2),1080) IORD,BLNAM1,BLNAM2,NBB                      IUDB22     42
         CALL PRTMSG(2)                                                 IUDB22     43
         MSG(1)='IUDB22: UDM ERROR'                                     IUDB22     44
         CALL ASTOP(MSG(1),17)                                          IUDB22     45
      ENDIF                                                             IUDB22     46
      JLS=IDDAT(JEI+3)                                                  IUDB22     47
      IF(INDEX.EQ.1.OR.INDEX.EQ.2) THEN                                 IUDB22     48
         J2=JE5-2                                                       IUDB22     49
         DO 110 J=1,INTUD                                               IUDB22     50
         J2=J2+2                                                        IUDB22     51
         IDD=IDDAT(J2+2)                                                IUDB22     52
         IDD=ABS(IDD)                                                   IUDB22     53
         IF(IDD.EQ.IORD) THEN                                           IUDB22     54
            IBB=IBB+1                                                   IUDB22     55
            XDAT=1.0                                                    IUDB22     56
            IF(IDDAT(J2+2).LT.0) XDAT=-1.0                              IUDB22     57
            IOUT=IDDAT(J2+1)                                            IUDB22     58
            LTYP1=IDDAT(JE3+17*IOUT-15)                                 IUDB22     59
            IF(JUDE(IOUT).EQ.0.AND.                                     IUDB22     60
     &      (LTYP1.NE.4.AND.LTYP1.NE.6)) THEN                           IUDB22     61
               NEXTB=IOUT                                               IUDB22     62
               XDATN=XDAT                                               IUDB22     63
               NEXTBB=IBB                                               IUDB22     64
            ELSE                                                        IUDB22     65
               VALI(IBB)=VALOUT(IOUT)*XDAT                              IUDB22     66
            ENDIF                                                       IUDB22     67
         ENDIF                                                          IUDB22     68
  110    CONTINUE                                                       IUDB22     69
      ENDIF                                                             IUDB22     70
      J3=JE6-3                                                          IUDB22     71
      DO 120 J=1,INPUD                                                  IUDB22     72
      J3=J3+3                                                           IUDB22     73
      IF(IDDAT(J3+1).EQ.IORD) THEN                                      IUDB22     74
         IBB=IBB+1                                                      IUDB22     75
         IIN=IDDAT(J3+2)                                                IUDB22     76
         IF(IIN.EQ.20) THEN                                             IUDB22     77
            XDATN=DDAT(J3+3)                                            IUDB22     78
            NEXTBB=IBB                                                  IUDB22     79
         ELSE                                                           IUDB22     80
            VALI(IBB)=VALIN(IIN)*DDAT(J3+3)                             IUDB22     81
         ENDIF                                                          IUDB22     82
      ENDIF                                                             IUDB22     83
  120 CONTINUE                                                          IUDB22     84
      IF(IBB.GT.NBB) THEN                                               IUDB22     85
         WRITE(MSG(1),1000) NLF,INAME(KN-2),INAME(KN-1),INAME(KN),CID   IUDB22     86
         WRITE(MSG(2),1060) IORD,BLNAM1,BLNAM2,IBB                      IUDB22     87
         CALL PRTMSG(2)                                                 IUDB22     88
         MSG(1)='IUDB22: UDM ERROR'                                     IUDB22     89
         CALL ASTOP(MSG(1),17)                                          IUDB22     90
      ENDIF                                                             IUDB22     91
      DO 130 L=1,NAA                                                    IUDB22     92
      DO 130 J=1,NAA                                                    IUDB22     93
      JLS=JLS+1                                                         IUDB22     94
      AA(L,J)=SSMAT(JLS)                                                IUDB22     95
  130 CONTINUE                                                          IUDB22     96
      DO 140 L=1,NAA                                                    IUDB22     97
      DO 140 J=1,NBB                                                    IUDB22     98
      JLS=JLS+1                                                         IUDB22     99
      BB(L,J)=SSMAT(JLS)                                                IUDB22    100
  140 CONTINUE                                                          IUDB22    101
      DO 150 L=1,NAA                                                    IUDB22    102
      JLS=JLS+1                                                         IUDB22    103
      CC(L)=SSMAT(JLS)                                                  IUDB22    104
  150 CONTINUE                                                          IUDB22    105
      DO 160 L=1,NBB                                                    IUDB22    106
      JLS=JLS+1                                                         IUDB22    107
      DD(L)=SSMAT(JLS)                                                  IUDB22    108
  160 CONTINUE                                                          IUDB22    109
      IF(NAA.EQ.1)THEN                                                  IUDB22    110
         IF(AA(1,1).LE.0.0.AND.AA(1,1).GE.0.0) THEN                     IUDB22    111
         WRITE(MSG(1),1000) NLF,INAME(KN-2),INAME(KN-1),INAME(KN),CID   IUDB22    112
         WRITE(MSG(2),1090) IORD,BLNAM1,BLNAM2                          IUDB22    113
         CALL PRTMSG(2)                                                 IUDB22    114
         MSG(1)='IUDB22: UDM ERROR'                                     IUDB22    115
         CALL ASTOP(MSG(1),17)                                          IUDB22    116
         ENDIF                                                          IUDB22    117
         AA(1,1)=1.0/AA(1,1)                                            IUDB22    118
      ELSE                                                              IUDB22    119
         CALL MINVRS(AA,8,NAA,DETA,IER,IR,IC)                           IUDB22    120
         IF(IER.EQ.1) THEN                                              IUDB22    121
            WRITE(MSG(1),1000) NLF,INAME(KN-2),INAME(KN-1),INAME(KN)    IUDB22    122
     *                        ,CID                                      IUDB22    123
            WRITE(MSG(2),1090) IORD,BLNAM1,BLNAM2                       IUDB22    124
            CALL PRTMSG(2)                                              IUDB22    125
            MSG(1)='IUDB22: UDM ERROR'                                  IUDB22    126
            CALL ASTOP(MSG(1),17)                                       IUDB22    127
         ENDIF                                                          IUDB22    128
      ENDIF                                                             IUDB22    129
      DO 180 L=1,NBB                                                    IUDB22    130
      DO 180 J=1,NAA                                                    IUDB22    131
      SUM=0.0                                                           IUDB22    132
      DO 170 K=1,NAA                                                    IUDB22    133
      SUM=SUM+AA(J,K)*BB(K,L)                                           IUDB22    134
  170 CONTINUE                                                          IUDB22    135
      DX(J,L)=SUM                                                       IUDB22    136
  180 CONTINUE                                                          IUDB22    137
      DO 200 L=1,NBB                                                    IUDB22    138
      SUM=0.0                                                           IUDB22    139
      DO 190 J=1,NAA                                                    IUDB22    140
      SUM=SUM+CC(J)*DX(J,L)                                             IUDB22    141
  190 CONTINUE                                                          IUDB22    142
      DD1(L)=DD(L)-SUM                                                  IUDB22    143
  200 CONTINUE                                                          IUDB22    144
      DO 210 L=1,IBB                                                    IUDB22    145
      VALI1=VALI1+VALI(L)*DD1(L)                                        IUDB22    146
  210 CONTINUE                                                          IUDB22    147
      IF(INDEX.EQ.2) THEN                                               IUDB22    148
         VALO=VALOUT(IORD)                                              IUDB22    149
         IF(JORD.LT.NUDB) THEN                                          IUDB22    150
            IF(NEXTB.EQ.0)THEN                                          IUDB22    151
            MSG(1)='IUDB22: 1'                                          IUDB22    152
            CALL ASTOP(MSG(1),9)                                        IUDB22    153
            ENDIF                                                       IUDB22    154
            VALOUT(NEXTB)=(VALO-VALI1)/DD1(NEXTBB)/XDATN                IUDB22    155
         ELSE                                                           IUDB22    156
            VALIN(20)=(VALO-VALI1)/DD1(NEXTBB)/XDATN                    IUDB22    157
         ENDIF                                                          IUDB22    158
         VALI(NEXTB)=VALO-VALI1                                         IUDB22    159
      ELSE                                                              IUDB22    160
         VALOUT(IORD)=VALI1                                             IUDB22    161
      ENDIF                                                             IUDB22    162
      DO 230 L=1,NAA                                                    IUDB22    163
      KORD=KORD+1                                                       IUDB22    164
      SUM=0.0                                                           IUDB22    165
      DO 220 J=1,NBB                                                    IUDB22    166
      SUM=DX(L,J)*VALI(J)                                               IUDB22    167
  220 CONTINUE                                                          IUDB22    168
      XSTATE(KORD)=-SUM                                                 IUDB22    169
  230 CONTINUE                                                          IUDB22    170
      IF(LDEB)WRITE(IUD6,1050) KORD,JORD,(XSTATE(I),I=1,NAA)            IUDB22    171
C                                                                       IUDB22    172
      RETURN                                                            IUDB22    173
 1050 FORMAT(1X,'KORD,JORD,XSTATE(KORD): ',2I5,/,1X,8F10.3)             IUDB22    174
 1000 FORMAT(' *** ERROR USER DEFINED MODEL AT BUS:',I5,' STATION: '    IUDB22    175
     *,3A4,' ID:',A4)                                                   IUDB22    176
 1060 FORMAT(11X,'BLOCK#',I3,'(',2A4,') HAS ',I2                        IUDB22    177
     *,' INPUTS. ONLY UP TO NBB IS ALLOWED.')                           IUDB22    178
 1070 FORMAT(11X,'BLOCK#',I3,'(',2A4,') HAS UNACCEPTABLE NAA=',I2)      IUDB22    179
 1080 FORMAT(11X,'BLOCK#',I3,'(',2A4,') HAS UNACCEPTABLE NBB=',I2)      IUDB22    180
 1090 FORMAT(11X,'BLOCK#',I3,'(',2A4,') HAS SINGULAR A MATRIX.')        IUDB22    181
      END                                                               IUDB22    182
