CFPRIME HOOK                                                            HOOK        1
        SUBROUTINE HOOK(IHOOK)                                          HOOK        2
CD                                                                      HOOK        3
CD   This routine executes an interface to ETMSP - providing            HOOK        4
CD   "hooks" for to input signals into system and retrieve outputs.     HOOK        5
CD   The purpose is to allow simulation with nonconventional            HOOK        6
CD   or digital controllers.  It also allows access to signals          HOOK        7
CD   during the run for the purposes of signal analysis, controls       HOOK        8
CD   or special outputs. Originaly developed at Montana State UniversityHOOK        9
CD   in cooperation with the Bonneville Power Administration.           HOOK       10
CD                                                                      HOOK       11
CD                                                                      HOOK       12
CD   IHOOK is a numerical flag in common labelled flags                 HOOK       13
CD                                                                      HOOK       14
CD   The manner of which this flag is defined and used are              HOOK       15
CD   as follows:                                                        HOOK       16
CD                                                                      HOOK       17
CD   1. IHOOK is initially set to 99 in routine PARSET                  HOOK       18
CD                                                                      HOOK       19
CD   2. If HOOK routine is to be used the user should initialize IHOOK  HOOK       20
CD      to zero by activate the HOOK routine in the CASE activity       HOOK       21
CD                                                                      HOOK       22
CD   3. When IHOOK is set zero, routine INITIA will call HOOK to performHOOK       23
CD      the initialization and IHOOK is reset to 2. Prior to return to  HOOK       24
CD      INITIA, IHOOK is reset to 3 if output is required.              HOOK       25
CD      IHOOK is reset to 4 if output of the initial conditions is requiHOOK       26
CD                                                                      HOOK       27
CD   4. If IHOOK is 3 or 4 INITIA will prompt the user to enter output fHOOK       28
CD      name and call routine filemg to open the file with units IUHK.  HOOK       29
CD      When the file is opened if IHOOK is 4 HOOK routine will be calleHOOK       30
CD      once again to output the required initial conditions.           HOOK       31
CD      IHOOK is reset to 1.                                            HOOK       32
CD                                                                      HOOK       33
CD   5. If IHOOK IS 1 routine SIMULA will call HOOK to excute the user  HOOK       34
CD      entered control actions statements and output statements of     HOOK       35
CD      BLOCKS 4 and 5.                                                 HOOK       36
CD                                                                      HOOK       37
CD                                                                      HOOK       38
CD  --------------------------------------------------------------------HOOK       39
CD  --------------------------  START OF BLOCK 1  ----------------------HOOK       40
CD  -------------------  COMMONS TO ACCESS ETMSP VARIABLES -------------HOOK       41
CD  --------------------------------------------------------------------HOOK       42
CD                                                                      HOOK       43
C                                                                       HOOK       44
      INCLUDE 'PARX.INS'                                                HOOK       45
      INCLUDE 'CNB8.INS'                                                HOOK       46
      INCLUDE 'CNYVAR.INS'                                              HOOK       47
      INCLUDE 'CNB4.INS'                                                HOOK       48
      INCLUDE 'CNUDM.INS'                                               HOOK       49
      INCLUDE 'CNVOLT.INS'                                              HOOK       50
      INCLUDE 'CNMYVA.INS'                                              HOOK       51
      INCLUDE 'CNNNEW.INS'                                              HOOK       52
      INCLUDE 'CNSVS.INS'                                               HOOK       53
      INCLUDE 'CNGEAR.INS'                                              HOOK       54
      INCLUDE 'CNIPTR.INS'                                              HOOK       55
      INCLUDE 'CNXSTA.INS'                                              HOOK       56
      INCLUDE 'CNMSTA.INS'                                              HOOK       57
      INCLUDE 'CNMDAT.INS'                                              HOOK       58
      INCLUDE 'CNUNIT.INS'                                              HOOK       59
      INCLUDE 'CNB5.INS'                                                HOOK       60
      INCLUDE 'CNB9.INS'                                                HOOK       61
C                                                                       HOOK       62
CD  --------------------------------------------------------------------HOOK       63
CD  ----------------------------  END OF BLOCK 1  ----------------------HOOK       64
CD  --------------------------------------------------------------------HOOK       65
CD                                                                      HOOK       66
CD                                                                      HOOK       67
CD  --------------------------------------------------------------------HOOK       68
CD  ------------------------  START OF BLOCK 2  ------------------------HOOK       69
CD       -  USER DEFINED VARIABLE DEFINITIONS AND COMMON STATEMENTS  -  HOOK       70
CD                                                                      HOOK       71
CD  NOTE: ETMSP has adopted the program convention that throughout the  HOOK       72
CD        program, integer variable names begin with leters I-N,while   HOOK       73
CD        other variable names begin with leters in the ranges          HOOK       74
CD        of A-H and O-Z.                                               HOOK       75
CD        For portability it is recommended that variable name should beHOOK       76
CD        restricted to no more than six characters and data statements HOOK       77
CD        must be entered at the end of the BLOCK 2 statements.         HOOK       78
CD                                                                      HOOK       79
CD Example:                                                             HOOK       80
CD                                                                      HOOK       81
CD     (these statements can be deleted and replaced at your discretion)HOOK       82
CD                                                                      HOOK       83
        COMMON/HKCN0/ DIFANG,BCGE,IBYPAS,IREAD,PRINTO,TS0               HOOK       84
        COMMON/HKCN1/ NMDE,NMDE1,IRANIT,OSCIL                           HOOK       85
        COMMON/HKCN3/ GEZS1R,GEZS1X,GEZS2R,GEZS2X,GER,GEX               HOOK       86
CD                                                                      HOOK       87
CD                                                                      HOOK       88
        CHARACTER*12 LFBNA1(20)                                         HOOK       89
        CHARACTER*4  DEVID1(20)                                         HOOK       90
        INTEGER      LFBNO1(20),NPROG1(20)                              HOOK       91
        INTEGER        IPT1(20)                                         HOOK       92
        INTEGER I,NGT1,IPOUT,IPRNT                                      HOOK       93
        LOGICAL IPNOW                                                   HOOK       94
        REAL*4  VOLT1R,VOLT1I,VOLT2R,VOLT2I,ANGLE1,ANGLE2,DIFANG        HOOK       95
CD                                                                      HOOK       96
CD In the following examples it is assumed that:                        HOOK       97
CD                                                                      HOOK       98
CD CHARACTER*12 LFBNA1 ARRAY stores bus names of type 1 dynamice devicesHOOK       99
CD CHARACTER*4  DEVID1 ARRAY stores device id of type 1 dynamice devicesHOOK      100
CD INTEGER      LFBNO1 ARRAY stores loadflow bus number of type 1 deviceHOOK      101
CD INTEGER      NPROG1 ARRAY stores internal bus number of type 1 deviceHOOK      102
CD INTEGER      IPT1   ARRAY stores device number of type 1 devices     HOOK      103
CD                                                                      HOOK      104
                                                                        HOOK      105
                                                                        HOOK      106
                                                                        HOOK      107
                                                                        HOOK      108
                                                                        HOOK      109
CD  --------------------------------------------------------------------HOOK      110
CD  ----------------------  END OF USER BLOCK 2  -----------------------HOOK      111
CD  --------------------------------------------------------------------HOOK      112
CD                                                                      HOOK      113
CD                                                                      HOOK      114
        IF(IHOOK.EQ.99)RETURN                                           HOOK      115
CD                                                                      HOOK      116
CD *********************  INITIALIALIZATION BLOCK  *********************HOOK      117
CD                                                                      HOOK      118
CD      IHOOK = 0 > FIRST CALL FROM ROUTINE INITIA                      HOOK      119
CD                                                                      HOOK      120
        IF(IHOOK.EQ.0)THEN                                              HOOK      121
CD                                                                      HOOK      122
CD      SET IHOOK TO 2 FOR SUBSEQUENT ENTRY OF THE ROUTINE              HOOK      123
CD                                                                      HOOK      124
        IHOOK=2                                                         HOOK      125
CD                                                                      HOOK      126
CD  --------------------------------------------------------------------HOOK      127
CD  -----------------------  START OF BLOCK 3  -------------------------HOOK      128
cd  - ASSIGN NGT1, IPOUT AND EACH LFBNA1(I) or LFBNO1(I) UP TO I = NGT1.HOOK      129
CD                                                                      HOOK      130
        IPOUT = 10                                                      HOOK      131
C                                                                       HOOK      132
        NGT1 = 0                                                        HOOK      133
CD                                                                      HOOK      134
CD  IPOUT > HOOK WILL WRITE TO OUTPUT FILE EVERY IPOUT TIME STEPS       HOOK      135
CD                                                                      HOOK      136
CD  NGT1 = NUMBER OF DYNAMIC DEVICES TO ACCESS                          HOOK      137
CD                                                                      HOOK      138
CD  EXAMPLE OF DYNAMIC DEVICE ASSIGNMENTS:                              HOOK      139
CD                                                                      HOOK      140
CD  1. Bus name interface mode                                          HOOK      141
CD                                                                      HOOK      142
CD        Use bus name and device ID to assign                          HOOK      143
CD                                                                      HOOK      144
CD        LFBNA1(1) = 'SLATT2   500'                                    HOOK      145
CD        DEVID1(1)= '  1 '                                             HOOK      146
CD        LFBNA1(2) = 'SLATT    500'                                    HOOK      147
CD        DEVID1(2)= '  A '                                             HOOK      148
CD        LFBNA1(3) = 'MCNARY 213.8'                                    HOOK      149
CD        DEVID1(3)= '  1 '                                             HOOK      150
CD                                                                      HOOK      151
CD                                                                      HOOK      152
CD  2. BUS NUMBER INTERFACE MODE                                        HOOK      153
CD                                                                      HOOK      154
CD        Use bus number and device ID to assign                        HOOK      155
CD                                                                      HOOK      156
CD        LFBNO1(1) = 731                                               HOOK      157
CD        DEVID1(1)= '  1 '                                             HOOK      158
CD        LFBNO1(2) = 567                                               HOOK      159
CD        DEVID1(2)= '  A '                                             HOOK      160
CD        LFBNO1(3) = 679                                               HOOK      161
CD        DEVID1(3)= '  1 '                                             HOOK      162
CD                                                                      HOOK      163
CD                                                                      HOOK      164
CD   NOTE:                                                              HOOK      165
CD                                                                      HOOK      166
CD   To allow the user of liberal bus numbers in the input data and to  HOOK      167
CD   perform the simulation in a CPU-efficient way, ETMSP has adopted anHOOK      168
CD   internal bus numbering scheme. details of which can be found in    HOOK      169
CD   section 4 of the Programmer's guide.                               HOOK      170
CD                                                                      HOOK      171
CD                                                                      HOOK      172
CD   For efficient computation, schemes with large vector arrays and offHOOK      173
CD   have been widely used in ETMSP. An example of these schemes has beeHOOK      174
CD   described in section 3.1.1 of the Programmer's guide.              HOOK      175
CD                                                                      HOOK      176
CD                                                                      HOOK      177
CD   Based on the internal number the manner of which to access the modeHOOK      178
CD   data, states, and y variables of an ac dynamic model can be found iHOOK      179
CD   tables 3-17,3-18,and 3-19 of the Programmer's guide.               HOOK      180
CD                                                                      HOOK      181
CD  --------------------------------------------------------------------HOOK      182
CD                                                                      HOOK      183
        IPRNT = IPOUT                                                   HOOK      184
CD                                                                      HOOK      185
CD                                                                      HOOK      186
CD  ------------------------  END OF BLOCK 3  --------------------------HOOK      187
CD  --------------------------------------------------------------------HOOK      188
CD                                                                      HOOK      189
CD                                                                      HOOK      190
cd  --------------------------------------------------------------------HOOK      191
CD  -----------------------  START OF BLOCK 4 --------------------------HOOK      192
Cd  ----- CODE TO GET NUMBERS AND ACCESS POINTERS OF DYNAMICE DEVICES---HOOK      193
CD  --------------------------------------------------------------------HOOK      194
CD                                                                      HOOK      195
C                                                                       HOOK      196
C       GET NUMBERS AND ACCESS POINTERS FOR BUSES OF TYPE 1 DEVICES     HOOK      197
C                                                                       HOOK      198
        IF(NGT1.LE.0) GOTO 20                                           HOOK      199
C                                                                       HOOK      200
           DO 10 I=1,NGT1                                               HOOK      201
C                                                                       HOOK      202
C   Case 1: Assuming you are in bus name interface mode and generator   HOOK      203
C           is the interested dynamic device                            HOOK      204
C                                                                       HOOK      205
C   STEP 1. Call CHKBUS to find the loadflow bus number                 HOOK      206
C                                                                       HOOK      207
                                                                        HOOK      208
             CALL CHKBUS(LFBNA1(I),LFBNO1(I))                           HOOK      209
C                                                                       HOOK      210
C       check for bus # = 0, which indicates a problem.                 HOOK      211
C       if ok, then get generator numbers in internal order.            HOOK      212
C                                                                       HOOK      213
             IF( LFBNO1(I) .NE. 0) THEN                                 HOOK      214
C                                                                       HOOK      215
C   STEP 2. Get the internal bus number                                 HOOK      216
C                                                                       HOOK      217
               NPROG1(I) = NNEW(LFBNO1(I))                              HOOK      218
C                                                                       HOOK      219
C   STEP 3. Call CHKGEN to find the generator number by matching the devHOOK      220
C                                                                       HOOK      221
            CALL CHKGEN(NPROG1(I),DEVID1(I),NTHDEV)                     HOOK      222
C                                                                       HOOK      223
C       check device # = 0, which indicates a problem.                  HOOK      224
C       if ok, then store to array IPT1(I).                             HOOK      225
C                                                                       HOOK      226
                  IF(NTHDEV.NE.0)THEN                                   HOOK      227
C                                                                       HOOK      228
                  IPT1(I)=NTHDEV                                        HOOK      229
C                                                                       HOOK      230
                  ELSE                                                  HOOK      231
C                                                                       HOOK      232
                  WRITE(IU16,*)' ERROR - PROBLEM WITH DEVICE ID IN HOOK'HOOK      233
                  CALL ASTOP('HOOK ERROR',16)                           HOOK      234
C                                                                       HOOK      235
                  ENDIF                                                 HOOK      236
C                                                                       HOOK      237
             ELSE                                                       HOOK      238
               WRITE(IU16,*) ' ERROR - PROBLEM WITH BUS NAME IN HOOK '  HOOK      239
               CALL ASTOP('HOOK ERROR',16)                              HOOK      240
             ENDIF                                                      HOOK      241
C                                                                       HOOK      242
 10        CONTINUE                                                     HOOK      243
C                                                                       HOOK      244
C   CASE 2: Assuming that you are in bus number interface mode and SVC  HOOK      245
C           in the interested dynamic device                            HOOK      246
C                                                                       HOOK      247
C                                                                       HOOK      248
C   STEP 1. Get the internal bus number                                 HOOK      249
C                                                                       HOOK      250
CF               NPROG1(I) = NNEW(LFBNO1(I))                            HOOK      251
C                                                                       HOOK      252
CF       IF(NPROG1(I).EQ.0)THEN                                         HOOK      253
C                                                                       HOOK      254
C                                                                       HOOK      255
C               CHECK BUS # = 0, WHICH INDICATES A PROBLEM.             HOOK      256
C                  IF OK, PROCEED TO FIND THE DEVICE NUMBER.            HOOK      257
C                                                                       HOOK      258
CF               WRITE(IU16,*) ' ERROR - PROBLEM WITH BUS NAME IN HOOK 'HOOK      259
CF               CALL ASTOP('HOOK ERROR',16)                            HOOK      260
C                                                                       HOOK      261
CF       ELSE                                                           HOOK      262
C                                                                       HOOK      263
C   STEP 2. Call chksvc to find the device number by matching the deviceHOOK      264
C                                                                       HOOK      265
CF            CALL CHKSVC(NPROG1(I),DEVID(I),NTHDEV)                    HOOK      266
C                                                                       HOOK      267
C                  check device # = 0, which indicates a problem.       HOOK      268
c                  if ok, then store to array ipt1(i).                  HOOK      269
C                                                                       HOOK      270
CF                  IF(NTHDEV.NE.0)THEN                                 HOOK      271
C                                                                       HOOK      272
CF                  IPT1(I)=NTHDEV                                      HOOK      273
C                                                                       HOOK      274
CF                  ELSE                                                HOOK      275
C                                                                       HOOK      276
CF               WRITE(IU16,*) ' ERROR - PROBLEM WITH DEVICE ID IN HOOk'HOOK      277
CF               CALL ASTOP('HOOK ERROR',16)                            HOOK      278
C                                                                       HOOK      279
CF                  ENDIF                                               HOOK      280
C                                                                       HOOK      281
CF           ENDIF                                                      HOOK      282
C                                                                       HOOK      283
 20        CONTINUE                                                     HOOK      284
CD  --------------------------------------------------------------------HOOK      285
CD  ------------------------  END OF BLOCK 4  --------------------------HOOK      286
CD  --------------------------------------------------------------------HOOK      287
CD                                                                      HOOK      288
cd  --------------------------------------------------------------------HOOK      289
CD  ---------------------  START OF USER BLOCK 5  ----------------------HOOK      290
CD    -  GET THE INDICES OF ANY RELEVENT STATES USING IPT1(I) AS OFFSET HOOK      291
cd  --------------------------------------------------------------------HOOK      292
CD                                                                      HOOK      293
                                                                        HOOK      294
                                                                        HOOK      295
                                                                        HOOK      296
                                                                        HOOK      297
                                                                        HOOK      298
CD                                                                      HOOK      299
CD  --------------------------------------------------------------------HOOK      300
CD  ------------------------  END OF BLOCK 5  --------------------------HOOK      301
CD  --------------------------------------------------------------------HOOK      302
CD                                                                      HOOK      303
CD                                                                      HOOK      304
CD      SET IHOOK TO 3 IF YOUR NEED OUTPUTS                             HOOK      305
CD                                                                      HOOK      306
CD      SET IHOOK TO 4 IF YOUR NEED OUTPUTS AND INITIAL CONDITIONS      HOOK      307
CD                                                                      HOOK      308
C                                                                       HOOK      309
        RETURN                                                          HOOK      310
C                                                                       HOOK      311
C       ENDIF IHOOK.EQ.0                                                HOOK      312
C                                                                       HOOK      313
        ENDIF                                                           HOOK      314
CD                                                                      HOOK      315
CD      SECOND CALL FROM ROUTINE INITIA                                 HOOK      316
CD                                                                      HOOK      317
        IF(IHOOK.EQ.4)THEN                                              HOOK      318
CD                                                                      HOOK      319
cd  --------------------------------------------------------------------HOOK      320
CD  -----------------------  START OF BLOCK 6  -------------------------HOOK      321
cd  ------------ insert your code to output initial conditions ---------HOOK      322
cd  --------------------------------------------------------------------HOOK      323
CD                                                                      HOOK      324
                                                                        HOOK      325
                                                                        HOOK      326
                                                                        HOOK      327
                                                                        HOOK      328
                                                                        HOOK      329
CD                                                                      HOOK      330
CD  --------------------------------------------------------------------HOOK      331
CD  ------------------------  END OF BLOCK 6  --------------------------HOOK      332
CD  --------------------------------------------------------------------HOOK      333
CD                                                                      HOOK      334
C                                                                       HOOK      335
C                                                                       HOOK      336
C                                                                       HOOK      337
        RETURN                                                          HOOK      338
C                                                                       HOOK      339
        ENDIF                                                           HOOK      340
CD                                                                      HOOK      341
CD  --------------------------------------------------------------------HOOK      342
CD  ***************   END OF INITIALIZATION BLOCKS  ********************HOOK      343
CD  --------------------------------------------------------------------HOOK      344
CD                                                                      HOOK      345
CD                                                                      HOOK      346
CD      CALL MADE FROM ROUTINE SIMULA                                   HOOK      347
CD                                                                      HOOK      348
        IF(IHOOK.EQ.1)THEN                                              HOOK      349
CD                                                                      HOOK      350
CD  --------------------------------------------------------------------HOOK      351
CD  ----------------------  START OF BLOCK 7  --------------------------HOOK      352
CD  ------------ CODE TO TEST DISCONTINUITY FOR PRINTING ---------------HOOK      353
CD  --------------------------------------------------------------------HOOK      354
C                                                                       HOOK      355
        IF (IPRNT.EQ.IPOUT) THEN                                        HOOK      356
           IPNOW = .TRUE.                                               HOOK      357
           IPRNT = 1                                                    HOOK      358
        ELSEIF (IPRNT.NE.IPOUT) THEN                                    HOOK      359
           IPNOW = .FALSE.                                              HOOK      360
           IPRNT = IPRNT + 1                                            HOOK      361
        ENDIF                                                           HOOK      362
C                                                                       HOOK      363
C       LDISC = .TRUE. AT DISCONTINUITY                                 HOOK      364
C                                                                       HOOK      365
        IF (LDISC) THEN                                                 HOOK      366
C                                                                       HOOK      367
C       Example of adjusting the printing sequence of an output file    HOOK      368
C       due to a discontinuity caused by a multiple HOOK call           HOOK      369
C                                                                       HOOK      370
C                                                                       HOOK      371
        IPNOW = .TRUE.                                                  HOOK      372
        IPRNT = IPRNT - 1                                               HOOK      373
C                                                                       HOOK      374
        ENDIF                                                           HOOK      375
                                                                        HOOK      376
CD                                                                      HOOK      377
CD  --------------------------------------------------------------------HOOK      378
CD  -------------------------  END OF BLOCK 7  -------------------------HOOK      379
CD  --------------------------------------------------------------------HOOK      380
CD                                                                      HOOK      381
CD  --------------------------------------------------------------------HOOK      382
CD  ---------------------  START OF USER BLOCK 8  ----------------------HOOK      383
CD           - INSERT ALL CONTROL ACTIONS STATEMENTS HERE -             HOOK      384
CD                                                                      HOOK      385
                                                                        HOOK      386
                                                                        HOOK      387
                                                                        HOOK      388
                                                                        HOOK      389
                                                                        HOOK      390
CD                                                                      HOOK      391
CD  --------------------------------------------------------------------HOOK      392
CD  ----------------------  END OF USER BLOCK 8  -----------------------HOOK      393
CD  --------------------------------------------------------------------HOOK      394
CD                                                                      HOOK      395
        IF(IPNOW)THEN                                                   HOOK      396
CD                                                                      HOOK      397
CD  --------------------------------------------------------------------HOOK      398
CD  ---------------------  START OF USER BLOCK 9  ----------------------HOOK      399
CD                    - INSERT ALL WRITE STATEMENTS HERE -              HOOK      400
CD           - AND REMEMBER TO WRITE ALL YOUR OUTPUT TO UNIT IUHK -     HOOK      401
CD                                                                      HOOK      402
                                                                        HOOK      403
                                                                        HOOK      404
                                                                        HOOK      405
                                                                        HOOK      406
                                                                        HOOK      407
CD                                                                      HOOK      408
CD  ----------------------  END OF USER BLOCK 9  -----------------------HOOK      409
CD  --------------------------------------------------------------------HOOK      410
CD                                                                      HOOK      411
        ENDIF                                                           HOOK      412
C                                                                       HOOK      413
C       ENDIF IHOOK = 1                                                 HOOK      414
C                                                                       HOOK      415
        ENDIF                                                           HOOK      416
C                                                                       HOOK      417
        RETURN                                                          HOOK      418
CD                                                                      HOOK      419
CD ---------------------------------------------------------------------HOOK      420
CD -------------------------- START OF BLOCK 10 ------------------------HOOK      421
CD --------------- INSERT ALL YOUR FORMAT STATEMENTS HERE --------------HOOK      422
CD --SUGGESTION: LABEL YOUR FORMATS WITH HIGH NUMBERS eg 5000 OR UP ----HOOK      423
CD ---------------------------------------------------------------------HOOK      424
                                                                        HOOK      425
                                                                        HOOK      426
                                                                        HOOK      427
                                                                        HOOK      428
CD ---------------------------------------------------------------------HOOK      429
CD --------------------------- END OF BLOCK 10 -------------------------HOOK      430
CD ---------------------------------------------------------------------HOOK      431
C                                                                       HOOK      432
        END                                                             HOOK      433
                                                                        HOOK      434
