CFPRIME TEFMAR                                                          TEFMAR      1
      SUBROUTINE TEFMAR                                                 TEFMAR      2
CD                                                                      TEFMAR      3
CD    THIS IS THE MAIN DRIVER FOR TEF MARGIN CALCULATION                TEFMAR      4
CD                                                                      TEFMAR      5
      INCLUDE 'PARX.INS'                                                TEFMAR      6
      INCLUDE 'CNREC1.INS'                                              TEFMAR      7
      INCLUDE 'CNFLAG.INS'                                              TEFMAR      8
      INCLUDE 'CNPT.INS'                                                TEFMAR      9
      INCLUDE 'CNB0.INS'                                                TEFMAR     10
      INCLUDE 'CNB4.INS'                                                TEFMAR     11
      INCLUDE 'CNB9.INS'                                                TEFMAR     12
      INCLUDE 'CNB19.INS'                                               TEFMAR     13
      INCLUDE 'CNMON.INS'                                               TEFMAR     14
      INCLUDE 'CNXSTA.INS'                                              TEFMAR     15
      INCLUDE 'CNLSTA.INS'                                              TEFMAR     16
      INCLUDE 'CNMSTA.INS'                                              TEFMAR     17
      INCLUDE 'CNQSTA.INS'                                              TEFMAR     18
      INCLUDE 'CNUNIT.INS'                                              TEFMAR     19
      INCLUDE 'CNLUN.INS'                                               TEFMAR     20
      INCLUDE 'CNPARX.INS'                                              TEFMAR     21
      INCLUDE 'UDCA.INS'                                                TEFMAR     22
      INCLUDE 'UDCG.INS'                                                TEFMAR     23
      INCLUDE 'CNIMPR.INS'                                              TEFMAR     24
      INCLUDE 'CNPREV.INS'                                              TEFMAR     25
      INCLUDE 'CNGEAR.INS'                                              TEFMAR     26
      INCLUDE 'CNENG1.INS'                                              TEFMAR     27
      INCLUDE 'CNULTC.INS'                                              TEFMAR     28
      INCLUDE 'CNMSG.INS'                                               TEFMAR     29
C                                                                       TEFMAR     30
      LOGICAL KRESUL,IPREF,LASCHM,LRCLSE,IFIRST                         TEFMAR     31
      COMMON /CHGDC/ICHGDC                                              TEFMAR     32
      INTEGER IH                                                        TEFMAR     33
      REAL*4  H                                                         TEFMAR     34
      EQUIVALENCE(H,IH)                                                 TEFMAR     35
C                                                                       TEFMAR     36
      DATA NRSTEP/0/,IRSTEP/0/                                          TEFMAR     37
C                                                                       TEFMAR     38
      LDEB=(JDEB(1).NE.0)                                               TEFMAR     39
      LASCHM=(NAUT.NE.0)                                                TEFMAR     40
      LRCLSE=.FALSE.                                                    TEFMAR     41
      LOPUTC=0                                                          TEFMAR     42
C                                                                       TEFMAR     43
C     INITIALIZE IKE WITH ZERO VALUE AND OPEN NETWORK FACTOR            TEFMAR     44
C     FILE IU59                                                         TEFMAR     45
C                                                                       TEFMAR     46
      IKE=0                                                             TEFMAR     47
      XKLAST=0.0                                                        TEFMAR     48
      XPLAST=0.0                                                        TEFMAR     49
      TOTALP=0.0                                                        TEFMAR     50
C                                                                       TEFMAR     51
      OPEN(IU59,STATUS='SCRATCH',FORM='UNFORMATTED')                    TEFMAR     52
      LUNITS(IU59)=.TRUE.                                               TEFMAR     53
C                                                                       TEFMAR     54
      WRITE(LOUT,1200)                                                  TEFMAR     55
C                                                                       TEFMAR     56
C  STORE THE TOTAL NUMBER OF STATES AND INITIALIZE 'QSTATE' TO ZERO     TEFMAR     57
C                                                                       TEFMAR     58
      CALL FMOVE (0,QSTATE,QSTATE,JSTATE)                               TEFMAR     59
C                                                                       TEFMAR     60
C  READ ISWTEX FROM USER DEFINED SWITCHING FILE ISWF                    TEFMAR     61
C                                                                       TEFMAR     62
      REWIND ISWF                                                       TEFMAR     63
C                                                                       TEFMAR     64
      READ(ISWF,1030) (ISWTEX(L),L=1,20)                                TEFMAR     65
C                                                                       TEFMAR     66
C  READ MAX SPEED DEVIATION, STUDY PERIOD,TIME STEP,PRINT RATIO         TEFMAR     67
C  FROM USER DEFINED SWITCHING FILE ISWF                                TEFMAR     68
C                                                                       TEFMAR     69
      READ(ISWF,1040) XCMM,TL,HN,INDIVX,KNDIVX,IPLTK,IPPK,LOBUS         TEFMAR     70
C                                                                       TEFMAR     71
C     XMAXSD=MAX SPEED DEVTN(IN P.U.) TO TERMINATE                      TEFMAR     72
C                                                                       TEFMAR     73
      IF(XMAXSD.LE.0.02) XMAXSD=0.02                                    TEFMAR     74
C                                                                       TEFMAR     75
C      CALL PAGE6                                                       TEFMAR     76
C      CALL PAGE17                                                      TEFMAR     77
      WRITE(IU6,1050) (ISWTEX(LL),LL=1,20)                              TEFMAR     78
      WRITE(IU6,1060) XMAXSD,TL,HN,INDIVX,KNDIVX                        TEFMAR     79
C      WRITE(IU17,1050) (ISWTEX(LL),LL=1,20)                            TEFMAR     80
C      WRITE(IU17,1060) XMAXSD,TL,HN,INDIVX,KNDIVX                      TEFMAR     81
C                                                                       TEFMAR     82
C     CONVERT XMAXSD TO RADS/SEC.                                       TEFMAR     83
C                                                                       TEFMAR     84
      XMAXSD=XMAXSD*WOBASE                                              TEFMAR     85
C                                                                       TEFMAR     86
C     SET INTEGRATION METHOD FLAG:                                      TEFMAR     87
C                                                                       TEFMAR     88
C      LINTEG = .FALSE.  -- EXPLICIT METHOD IS IN USE                   TEFMAR     89
C             = .TRUE.  -- IMPLICIT METHOD IS IN USE                    TEFMAR     90
C                                                                       TEFMAR     91
      LINTEG=.FALSE.                                                    TEFMAR     92
C                                                                       TEFMAR     93
C   INITIALIZE XPREV ARRAY FOR USE OF IMPLICIT INTEGRATION METHOD       TEFMAR     94
C                                                                       TEFMAR     95
      DO 90 I=1,NSTATE                                                  TEFMAR     96
       XPREV(1,I)=XSTATE(I)                                             TEFMAR     97
       XPREV(2,I)=XSTATE(I)                                             TEFMAR     98
  90  CONTINUE                                                          TEFMAR     99
C                                                                       TEFMAR    100
C   NOTE:                                                               TEFMAR    101
C                                                                       TEFMAR    102
C   NSTEP = TOTAL NO. OF TIME STEPS IN THE SIMULATION WITH              TEFMAR    103
C           IMPLICIT METHOD                                             TEFMAR    104
C   NTITER = TOTAL NO. OF ITERATIONS IN THE SIMULATION WITH             TEFMAR    105
C            IMPLICIT METHOD                                            TEFMAR    106
C                                                                       TEFMAR    107
C                                                                       TEFMAR    108
      NTSTEP=0                                                          TEFMAR    109
      NTITER=0                                                          TEFMAR    110
      LONEXT=0                                                          TEFMAR    111
      ILSUM=0                                                           TEFMAR    112
      ILSUM1=0                                                          TEFMAR    113
C                                                                       TEFMAR    114
C     LSDAMP = .FALSE.  : NO SYSTEM DAMPING ADDED                       TEFMAR    115
C            = .TRUE.   : SYSTEM DAMPING ADDED                          TEFMAR    116
C                                                                       TEFMAR    117
      LSDAMP=.FALSE.                                                    TEFMAR    118
C                                                                       TEFMAR    119
      LDISC=.TRUE.                                                      TEFMAR    120
      IPREF=.FALSE.                                                     TEFMAR    121
      TS=0.0                                                            TEFMAR    122
      IASWX=0                                                           TEFMAR    123
      TSOUTC=0.                                                         TEFMAR    124
C                                                                       TEFMAR    125
C   NOTE:                                                               TEFMAR    126
C                                                                       TEFMAR    127
C  ITSTRN IS THE TERMINATION FLAG                                       TEFMAR    128
C  ITSTRN=1 SET IN RESULT ROUTINE WHEN SPEED OF GENERATOR EXCEEDS LIMIT TEFMAR    129
C  ITSTRN=2 SET IN NETSOL WHEN VOLTAGE ITERATION EXCEEDED LIMIT         TEFMAR    130
C  ITSTRN=3 SET IN INITIA WHEN PRE-FAULT NETWORK ITERATION              TEFMAR    131
C           EXCEEDED LIMIT                                              TEFMAR    132
C                                                                       TEFMAR    133
C                                                                       TEFMAR    134
      IPP=0                                                             TEFMAR    135
      ITSTRN=0                                                          TEFMAR    136
      KVOLT=1                                                           TEFMAR    137
      IPLT=1                                                            TEFMAR    138
      IASPRI=0                                                          TEFMAR    139
      IRPTK=1                                                           TEFMAR    140
C                                                                       TEFMAR    141
C   NOTE:                                                               TEFMAR    142
C                                                                       TEFMAR    143
C  KVOLT=0 NETSOL CALCULATE CURRENT INJECTED BUS VOLTAGES ONLY          TEFMAR    144
C  KVOLT=1 NETSOL WILL COMPUTE ALL REQUESTED VOLTAGES                   TEFMAR    145
C                                                                       TEFMAR    146
C                                                                       TEFMAR    147
      IDTS=0                                                            TEFMAR    148
      ITCFLG=0                                                          TEFMAR    149
  100 CONTINUE                                                          TEFMAR    150
C                                                                       TEFMAR    151
      IBFLAG=0                                                          TEFMAR    152
      CALL MODIFY (H,IPREF,IBFLAG)                                      TEFMAR    153
      IF(NDLR.NE.0)LRCLSE=.TRUE.                                        TEFMAR    154
      H=0.0                                                             TEFMAR    155
      IR=0                                                              TEFMAR    156
      IFIRST=.TRUE.                                                     TEFMAR    157
C                                                                       TEFMAR    158
      IF(IKE.EQ.1)THEN                                                  TEFMAR    159
C                                                                       TEFMAR    160
C     STEP 2. WHEN TS = TNOFA(+) FAULT IS NOW CLEARED                   TEFMAR    161
C                                                                       TEFMAR    162
C     TASK 1. WRITE THE NO FAULT NETWORK FACTOR                         TEFMAR    163
C                                                                       TEFMAR    164
C     SET IMODE  = 0 FOR WRITE                                          TEFMAR    165
C     SET IREWIN = 1 TO REWIND FILE WHEN WRITING IS COMPLETED           TEFMAR    166
C                                                                       TEFMAR    167
      IMODE=0                                                           TEFMAR    168
      IREWIN=1                                                          TEFMAR    169
      CALL RWFACT(IREWIN,IMODE)                                         TEFMAR    170
C                                                                       TEFMAR    171
C     TASK 2. CALCULATE THE KINETIC ENERGY                              TEFMAR    172
C                                                                       TEFMAR    173
      CALL SYSCEN                                                       TEFMAR    174
      CALL KENERG                                                       TEFMAR    175
      TKEMAX=TS                                                         TEFMAR    176
      XKEMAX=TOTALK                                                     TEFMAR    177
      XKLAST=TOTALK                                                     TEFMAR    178
C                                                                       TEFMAR    179
C     SET IKE = 2 TO FIND POST FAULT MAXIMUM AND MINIMUM KINETIC ENERGY TEFMAR    180
C                                                                       TEFMAR    181
      IKE=2                                                             TEFMAR    182
C                                                                       TEFMAR    183
      ENDIF                                                             TEFMAR    184
C                                                                       TEFMAR    185
C                                                                       TEFMAR    186
C   NOTE:                                                               TEFMAR    187
C  IPRTSW IS THE PROGRAM PRINT OUT CONTROL FLAG                         TEFMAR    188
C  IPRTSW=1 RESULT ROUTNIE WILL CALCULATE,WRITE AND PRINT               TEFMAR    189
C  IPRTSW=2 RESULT ROUTINE WILL CALCULATE AND WRITE                     TEFMAR    190
C  IPRTSW=3 RESULT ROUTINE WILL JUST WRITE END OF SIMULATION RECORDS    TEFMAR    191
C                                                                       TEFMAR    192
C  READ NEXT SWITCHING TIME RECORD FROM ISWF FILE                       TEFMAR    193
C                                                                       TEFMAR    194
      READ(ISWF,1010) TNEXT,HN,INDIVY,KNDIVY,IPPKN,IPLKN,INTMTH,IRPTKN  TEFMAR    195
     *               ,IGMINS,EPSGR,LONEXT                               TEFMAR    196
      IF(INTMTH.EQ.0) INTMTH=1                                          TEFMAR    197
      INDIV=INDIVX                                                      TEFMAR    198
      IF(INDIVY.NE.0) INDIV=INDIVY                                      TEFMAR    199
      KNDIV=KNDIVX                                                      TEFMAR    200
      IF(KNDIVY.NE.0) KNDIV=KNDIVY                                      TEFMAR    201
C                                                                       TEFMAR    202
                                                                        TEFMAR    203
      IF(IKE.EQ.0.AND.ABS(TNEXT-TNOFA).LE.0.00001)THEN                  TEFMAR    204
C                                                                       TEFMAR    205
C     STEP 1. WHEN TS = TNOFA" PRIOR TO READ THE NEXT SET OF SWITCHING  TEFMAR    206
C             DATA                                                      TEFMAR    207
C                                                                       TEFMAR    208
C             WRITE NETWORK FACTORS FOR FAULT SYSTEM BY SETTING         TEFMAR    209
C                                                                       TEFMAR    210
C             IMODE  = 0 TO WRITE                                       TEFMAR    211
C             IREWIN = 0 NO REWIND OF FILE WHEN WRITING IS COMPLETED    TEFMAR    212
C             IKE    = 1 TO WRITE THE NO FAULT NETWORK FACTORS          TEFMAR    213
C                        AND CALCULATE THE KINETIC ENERGY WHEN FAULT    TEFMAR    214
C                        IS CLEARED (MAXIMUM KINETIC ENERGY)            TEFMAR    215
      REWIND IU59                                                       TEFMAR    216
      IREWIN=0                                                          TEFMAR    217
      IMODE=0                                                           TEFMAR    218
      IKE=1                                                             TEFMAR    219
      CALL RWFACT(IREWIN,IMODE)                                         TEFMAR    220
      ENDIF                                                             TEFMAR    221
C                                                                       TEFMAR    222
      IF (INTMTH.LE.5) THEN                                             TEFMAR    223
         LINTEG=.FALSE.                                                 TEFMAR    224
      ELSE IF (INTMTH.GE.11) THEN                                       TEFMAR    225
         LINTEG=.TRUE.                                                  TEFMAR    226
         IGEARS=.FALSE.                                                 TEFMAR    227
         IF (INTMTH.EQ.12) THEN                                         TEFMAR    228
            IGEARS=.TRUE.                                               TEFMAR    229
            NGMINS=0                                                    TEFMAR    230
            IF (IGMINS.EQ.0) IGMINS=5                                   TEFMAR    231
            IF (EPSGR.EQ.0.0) EPSGR=0.001                               TEFMAR    232
         END IF                                                         TEFMAR    233
      END IF                                                            TEFMAR    234
C                                                                       TEFMAR    235
C  DETERMINE THE ITERATION CONTROL                                      TEFMAR    236
C                                                                       TEFMAR    237
      IF(IFAUTO.EQ.0)THEN                                               TEFMAR    238
       LITTER=0                                                         TEFMAR    239
       LITER=0                                                          TEFMAR    240
      ELSE                                                              TEFMAR    241
       LITTER=1                                                         TEFMAR    242
       LITER=1                                                          TEFMAR    243
      ENDIF                                                             TEFMAR    244
C                                                                       TEFMAR    245
C                                                                       TEFMAR    246
C  CHANGE HN AND IPPR ONLY IF NEW VALUES ARE READ                       TEFMAR    247
C                                                                       TEFMAR    248
      IF (HN.GT.1.0E-5) THEN                                            TEFMAR    249
         HSTEP=HN                                                       TEFMAR    250
      ELSE IF (INTMTH.EQ.12) THEN                                       TEFMAR    251
         HSTEP=0.005                                                    TEFMAR    252
      END IF                                                            TEFMAR    253
      IF(IRPTKN.NE.0)IRPTK=IRPTKN                                       TEFMAR    254
      IF(IPPKN.NE.0)IPPK=IPPKN                                          TEFMAR    255
      IF(IPLKN.NE.0)IPLTK=IPLKN                                         TEFMAR    256
      IF(NMOTL.EQ.0)KNDIV=0                                             TEFMAR    257
      IF(KCDC.EQ.0)INDIV=0                                              TEFMAR    258
      XNDIV=INDIV                                                       TEFMAR    259
      JNDIV=INDIV-1                                                     TEFMAR    260
      YNDIV=KNDIV                                                       TEFMAR    261
      LNDIV=KNDIV-1                                                     TEFMAR    262
C                                                                       TEFMAR    263
 110  CONTINUE                                                          TEFMAR    264
C                                                                       TEFMAR    265
C  CALCULATE THE EVENED STEP SIZE                                       TEFMAR    266
C                                                                       TEFMAR    267
      IF(TNEXT.GE.TL)THEN                                               TEFMAR    268
         TNEXT=TL                                                       TEFMAR    269
         ITERMI=1                                                       TEFMAR    270
      ELSE                                                              TEFMAR    271
         ITERMI=0                                                       TEFMAR    272
      ENDIF                                                             TEFMAR    273
C                                                                       TEFMAR    274
      XSTEP=(TNEXT-TS)/HSTEP                                            TEFMAR    275
C                                                                       TEFMAR    276
      XSTEP=XSTEP+0.5                                                   TEFMAR    277
      NSTEP=INT(XSTEP)                                                  TEFMAR    278
      XSTEP=REAL(NSTEP)                                                 TEFMAR    279
      HXSTEP=(TNEXT-TS)/XSTEP                                           TEFMAR    280
      IF(ABS(HXSTEP-HSTEP).GT.1.E-5)THEN                                TEFMAR    281
         NSTEP=NSTEP+1                                                  TEFMAR    282
         XSTEP=REAL(NSTEP)                                              TEFMAR    283
         HSTEP=(TNEXT-TS)/XSTEP                                         TEFMAR    284
      ELSE                                                              TEFMAR    285
         HSTEP=HXSTEP                                                   TEFMAR    286
      ENDIF                                                             TEFMAR    287
      KSTEP=NSTEP-1                                                     TEFMAR    288
      IF(IASPRI.EQ.0)THEN                                               TEFMAR    289
      WRITE(IU6,1000) TNEXT,HSTEP,NSTEP,IPLTK,IPPK,IRPTK,INDIV,KNDIV    TEFMAR    290
     *               ,INTMTH,NSLMTH,LOBUS                               TEFMAR    291
C      WRITE(IU17,1000) TNEXT,HSTEP,NSTEP,IPLTK,IPPK,IRPTK,INDIV,KNDIV  TEFMAR    292
C     *                ,INTMTH,NSLMTH,LOBUS                             TEFMAR    293
      ENDIF                                                             TEFMAR    294
C                                                                       TEFMAR    295
      IIL=61                                                            TEFMAR    296
C                                                                       TEFMAR    297
      IF(IFIRST)THEN                                                    TEFMAR    298
      CALL NETSOL (H,KVOLT,0,IPREF,IIL,NRSTEP)                          TEFMAR    299
      IFIRST=.FALSE.                                                    TEFMAR    300
      ENDIF                                                             TEFMAR    301
C  NOTE:                                                                TEFMAR    302
C  DTIME IS THE INTERVAL SIZE FOR THE DC ROUTINES                       TEFMAR    303
C                                                                       TEFMAR    304
      DTIME=HSTEP                                                       TEFMAR    305
C                                                                       TEFMAR    306
C  SANITY CHECK TO SEE WHETHER THE AC TIME STEP IS SMALLER              TEFMAR    307
C  THAN THE DC TIME STEP.                                               TEFMAR    308
C                                                                       TEFMAR    309
      IF(STIME.GT.(HSTEP+0.00001)) THEN                                 TEFMAR    310
       WRITE(MSG(1),1100) STIME                                         TEFMAR    311
       WRITE(MSG(2),1101) HSTEP                                         TEFMAR    312
       CALL PRTMSG(2)                                                   TEFMAR    313
       MSG(1)='TERMINATED DUE TO IMPROPER SIZE OF DC TIME STEP'         TEFMAR    314
       CALL ASTOP(MSG(1),47)                                            TEFMAR    315
      ENDIF                                                             TEFMAR    316
C                                                                       TEFMAR    317
C  SANITY CHECK TO SEE WHETHER THE DC TIME STEP IS SMALLER              TEFMAR    318
C  THAN THE DC CONTROL TIME STEP.                                       TEFMAR    319
C                                                                       TEFMAR    320
      IF(CTIME.GT.STIME) THEN                                           TEFMAR    321
       WRITE(MSG(1),1110) CTIME                                         TEFMAR    322
       WRITE(MSG(2),1111) STIME                                         TEFMAR    323
       CALL PRTMSG(2)                                                   TEFMAR    324
       MSG(1)=                                                          TEFMAR    325
     *  'TERMINATED DUE TO IMPROPER SIZE OF DC CONTROL TIME STEP'       TEFMAR    326
       CALL ASTOP(MSG(1),55)                                            TEFMAR    327
      ENDIF                                                             TEFMAR    328
C                                                                       TEFMAR    329
      ISTEP=-1                                                          TEFMAR    330
C                                                                       TEFMAR    331
  200 CONTINUE                                                          TEFMAR    332
C                                                                       TEFMAR    333
C  NOTE:                                                                TEFMAR    334
C      KRESUL=FALSE NO RESULT IS REQUIRED                               TEFMAR    335
C      KRESUL=TRUE RESULT IS REQUIRED                                   TEFMAR    336
C                                                                       TEFMAR    337
      KRESUL=.FALSE.                                                    TEFMAR    338
C                                                                       TEFMAR    339
      IF(IH.EQ.0.OR.ISTEP.EQ.KSTEP)THEN                                 TEFMAR    340
C                                                                       TEFMAR    341
C     DISCONTINUNITY POINT                                              TEFMAR    342
C                                                                       TEFMAR    343
         IPRTSW=1                                                       TEFMAR    344
         KRESUL=.TRUE.                                                  TEFMAR    345
         LDISC=.TRUE.                                                   TEFMAR    346
         IPP=1                                                          TEFMAR    347
         IPLT=1                                                         TEFMAR    348
C                                                                       TEFMAR    349
C     TIME POINT                                                        TEFMAR    350
C                                                                       TEFMAR    351
      ELSE                                                              TEFMAR    352
C                                                                       TEFMAR    353
         LDISC=.FALSE.                                                  TEFMAR    354
C                                                                       TEFMAR    355
         IF(IPLT.LT.IPLTK)THEN                                          TEFMAR    356
            IPLT=IPLT+1                                                 TEFMAR    357
         ELSE                                                           TEFMAR    358
            IPLT=1                                                      TEFMAR    359
            IPRTSW=2                                                    TEFMAR    360
            KRESUL=.TRUE.                                               TEFMAR    361
            IF(IPP.LT.IPPK)THEN                                         TEFMAR    362
               IPP=IPP+1                                                TEFMAR    363
            ELSE                                                        TEFMAR    364
               IPP=1                                                    TEFMAR    365
               IPRTSW=1                                                 TEFMAR    366
            ENDIF                                                       TEFMAR    367
         ENDIF                                                          TEFMAR    368
      ENDIF                                                             TEFMAR    369
C                                                                       TEFMAR    370
      IF(.NOT.LUNITS(IU17))IPRTSW=2                                     TEFMAR    371
C                                                                       TEFMAR    372
      IF(IH.NE.0) IRSTEP=IRSTEP+1                                       TEFMAR    373
      IF(IRSTEP.EQ.IRPTK)THEN                                           TEFMAR    374
         IRSTEP=0                                                       TEFMAR    375
         NRSTEP=1                                                       TEFMAR    376
      ENDIF                                                             TEFMAR    377
C                                                                       TEFMAR    378
C  NOTE:                                                                TEFMAR    379
C      INTMTH <= 5  FOR EXPLICIT METHOD                                 TEFMAR    380
C      INTMTH >= 11 FOR IMPLICIT INTEGRATION METHOD                     TEFMAR    381
C                                                                       TEFMAR    382
      IF (INTMTH.LE.5.OR.IH.EQ.0) THEN                                  TEFMAR    383
         IF (LINTEG) INTMTH=1                                           TEFMAR    384
         CALL INTRKG(H,KRESUL,IPREF,IIL,NRSTEP)                         TEFMAR    385
      ELSE IF (LINTEG) THEN                                             TEFMAR    386
         CALL IMPINT(KRESUL,IIL,NRSTEP)                                 TEFMAR    387
      END IF                                                            TEFMAR    388
C                                                                       TEFMAR    389
C     ITSTRN = 2 WHEN NETWORK ITERATION COUNT EXCEEDES LIMIT            TEFMAR    390
C                                                                       TEFMAR    391
      IF (ITSTRN.EQ.2) GOTO 300                                         TEFMAR    392
      ISTEP=ISTEP+1                                                     TEFMAR    393
      TS=TS+H                                                           TEFMAR    394
C                                                                       TEFMAR    395
      IF (KRESUL) THEN                                                  TEFMAR    396
         CALL RESULT(H,LOUT)                                            TEFMAR    397
            IF(IFAUTO.NE.0)THEN                                         TEFMAR    398
            CALL SCHECK(H)                                              TEFMAR    399
            CALL CURVHL                                                 TEFMAR    400
            ENDIF                                                       TEFMAR    401
      END IF                                                            TEFMAR    402
C                                                                       TEFMAR    403
C     ITSTRN = 1 WHEN GENERATOR SPEED EXCEEDS LIMIT                     TEFMAR    404
C                                                                       TEFMAR    405
      IF(ITSTRN.EQ.1)GOTO 300                                           TEFMAR    406
C                                                                       TEFMAR    407
      IF(.NOT.LDISC.AND.IKE.EQ.2)THEN                                   TEFMAR    408
C                                                                       TEFMAR    409
C     AT FAULT CLEARING TIME INITIALIZE FOLD                            TEFMAR    410
C                                                                       TEFMAR    411
      CALL ENGFUN(FOLD)                                                 TEFMAR    412
C                                                                       TEFMAR    413
      IKE=3                                                             TEFMAR    414
C                                                                       TEFMAR    415
      ENDIF                                                             TEFMAR    416
C                                                                       TEFMAR    417
C     CALCULATE KINETIC ENERGY AND TRACK FOR MAXIMUM AND MINIMUM VALUE  TEFMAR    418
C                                                                       TEFMAR    419
      IF(IKE.EQ.3.OR.IKE.EQ.4)THEN                                      TEFMAR    420
C                                                                       TEFMAR    421
      CALL SYSCEN                                                       TEFMAR    422
      CALL KENERG                                                       TEFMAR    423
C                                                                       TEFMAR    424
      DKE=TOTALK-XKLAST                                                 TEFMAR    425
C                                                                       TEFMAR    426
      IF(IKE.EQ.3)THEN                                                  TEFMAR    427
C                                                                       TEFMAR    428
C     IKE=3 FIND MAXIMUM POST FAULT KINETIC ENERGY                      TEFMAR    429
C     WHEN FOUND SET IKE = 4 TO FIND MINIMUM KINETIC ENERGY             TEFMAR    430
C                                                                       TEFMAR    431
           IF(DKE.LE.0.0)THEN                                           TEFMAR    432
           IKE=4                                                        TEFMAR    433
           TKEMAX=TS                                                    TEFMAR    434
           XKEMAX=TOTALK                                                TEFMAR    435
           XKLAST=TOTALK                                                TEFMAR    436
           TEMARG=0.0                                                   TEFMAR    437
C                                                                       TEFMAR    438
           ELSE                                                         TEFMAR    439
C                                                                       TEFMAR    440
           XKLAST=TOTALK                                                TEFMAR    441
C                                                                       TEFMAR    442
           ENDIF                                                        TEFMAR    443
C                                                                       TEFMAR    444
      ELSE                                                              TEFMAR    445
C                                                                       TEFMAR    446
C     IKE=4 FIND MINIMUM                                                TEFMAR    447
C                                                                       TEFMAR    448
           IF(DKE.GE.0.0)THEN                                           TEFMAR    449
C                                                                       TEFMAR    450
C          MINIMUM POST FAULT KINETIC ENERGY                            TEFMAR    451
C          WHEN FOUND SET IKE = 5 TO START APPLY INTERMITTEN FAULT      TEFMAR    452
C                                                                       TEFMAR    453
           IKE=5                                                        TEFMAR    454
           TKEMIN=TS                                                    TEFMAR    455
           XKEMIN=TOTALK                                                TEFMAR    456
           TEMARG=0.0                                                   TEFMAR    457
C                                                                       TEFMAR    458
           ELSE                                                         TEFMAR    459
C                                                                       TEFMAR    460
           XKLAST=TOTALK                                                TEFMAR    461
C                                                                       TEFMAR    462
           ENDIF                                                        TEFMAR    463
C                                                                       TEFMAR    464
         ENDIF                                                          TEFMAR    465
C                                                                       TEFMAR    466
      ENDIF                                                             TEFMAR    467
C                                                                       TEFMAR    468
         IF(IKE.EQ.9)THEN                                               TEFMAR    469
C                                                                       TEFMAR    470
C        END OF NO FAULT STEP                                           TEFMAR    471
C                                                                       TEFMAR    472
         CALL SYSCEN                                                    TEFMAR    473
         CALL PENERG                                                    TEFMAR    474
         IKE=5                                                          TEFMAR    475
         DKE=TOTALP-XPLAST                                              TEFMAR    476
C                                                                       TEFMAR    477
           IF(DKE.LE.0.0)THEN                                           TEFMAR    478
C                                                                       TEFMAR    479
C          MAXIMUM POST FAULT POTENTIAL ENERGY                          TEFMAR    480
C          CALCULATE ENERGY MARGIN AND STOP                             TEFMAR    481
C                                                                       TEFMAR    482
           TPEMAX=TS-H                                                  TEFMAR    483
           XPEMAX=XPLAST                                                TEFMAR    484
C                                                                       TEFMAR    485
           ELSE                                                         TEFMAR    486
C                                                                       TEFMAR    487
           XPLAST=TOTALP                                                TEFMAR    488
C                                                                       TEFMAR    489
           ENDIF                                                        TEFMAR    490
C                                                                       TEFMAR    491
         ENDIF                                                          TEFMAR    492
C                                                                       TEFMAR    493
         IF(IKE.EQ.5)THEN                                               TEFMAR    494
C                                                                       TEFMAR    495
C        APPLY FAULT BY                                                 TEFMAR    496
C        SETTING IMODE  = 1 TO READ                                     TEFMAR    497
C        SETTING IREWIN = 0 NOT TO REWIND FILE WHEN READING IS COMPLETEDTEFMAR    498
C                                                                       TEFMAR    499
         IMODE=1                                                        TEFMAR    500
         IREWIN=0                                                       TEFMAR    501
         CALL RWFACT(IREWIN,IMODE)                                      TEFMAR    502
         H=0.0                                                          TEFMAR    503
         IR=0                                                           TEFMAR    504
         CALL NETSOL (H,KVOLT,0,IPREF,IIL,NRSTEP)                       TEFMAR    505
         ISTEP=ISTEP-1                                                  TEFMAR    506
         IKE=6                                                          TEFMAR    507
         GOTO 200                                                       TEFMAR    508
         END IF                                                         TEFMAR    509
C                                                                       TEFMAR    510
         IF(IKE.EQ.6)THEN                                               TEFMAR    511
C                                                                       TEFMAR    512
C        SET IKE = 7 AND ALLOW ONE STEP INTEGRATION                     TEFMAR    513
C                                                                       TEFMAR    514
         IKE=7                                                          TEFMAR    515
         GOTO 250                                                       TEFMAR    516
         ENDIF                                                          TEFMAR    517
C                                                                       TEFMAR    518
         IF(IKE.EQ.7)THEN                                               TEFMAR    519
C                                                                       TEFMAR    520
C        REMOVE FAULT BY                                                TEFMAR    521
C        SETTING IMODE  = 1 TO READ                                     TEFMAR    522
C        SETTING IREWIN = 1 TO REWIND FILE WHEN READING IS COMPLETED    TEFMAR    523
C                                                                       TEFMAR    524
         IMODE=1                                                        TEFMAR    525
         IREWIN=1                                                       TEFMAR    526
         CALL RWFACT(IREWIN,IMODE)                                      TEFMAR    527
         H=0.0                                                          TEFMAR    528
         IR=0                                                           TEFMAR    529
         CALL NETSOL (H,KVOLT,0,IPREF,IIL,NRSTEP)                       TEFMAR    530
         ISTEP=ISTEP-1                                                  TEFMAR    531
         IKE=8                                                          TEFMAR    532
         GOTO 200                                                       TEFMAR    533
         ENDIF                                                          TEFMAR    534
C                                                                       TEFMAR    535
         IF(IKE.EQ.8)THEN                                               TEFMAR    536
C                                                                       TEFMAR    537
C        SET IKE = 9 AND ALLOW ONE STEP INTEGRATION                     TEFMAR    538
C                                                                       TEFMAR    539
         IKE=9                                                          TEFMAR    540
         GOTO 250                                                       TEFMAR    541
         ENDIF                                                          TEFMAR    542
C                                                                       TEFMAR    543
 250  CONTINUE                                                          TEFMAR    544
C                                                                       TEFMAR    545
      H=HSTEP                                                           TEFMAR    546
C                                                                       TEFMAR    547
      IF (LINTEG.AND.INTMTH.EQ.1) THEN                                  TEFMAR    548
         INTMTH=11                                                      TEFMAR    549
         IF (IGEARS) INTMTH=12                                          TEFMAR    550
         CALL PREINT                                                    TEFMAR    551
      ENDIF                                                             TEFMAR    552
C                                                                       TEFMAR    553
      IF (ISTEP.LT.NSTEP) GOTO 200                                      TEFMAR    554
C                                                                       TEFMAR    555
      LOBUS=LONEXT                                                      TEFMAR    556
C                                                                       TEFMAR    557
      IF (ITERMI.EQ.0) GOTO 100                                         TEFMAR    558
C                                                                       TEFMAR    559
C     STOP SIMULATION COMPLETED                                         TEFMAR    560
C                                                                       TEFMAR    561
  300 CONTINUE                                                          TEFMAR    562
      IPRTSW=3                                                          TEFMAR    563
      CALL RESULT(H,LOUT)                                               TEFMAR    564
C                                                                       TEFMAR    565
      IF (NTSTEP.NE.0) THEN                                             TEFMAR    566
         WRITE(IU6,2000) NTSTEP,NTITER,REAL(NTITER)/REAL(NTSTEP)        TEFMAR    567
      END IF                                                            TEFMAR    568
C                                                                       TEFMAR    569
      RETURN                                                            TEFMAR    570
C                                                                       TEFMAR    571
 1000 FORMAT('  NEXT SWITCHING TIME =',F10.4,/                          TEFMAR    572
     &,'  TIME INTERVAL =',F8.4,/                                       TEFMAR    573
     &,'  NUMBER OF STEPS =',I5,/                                       TEFMAR    574
     &,'  PRINT & PLOT CONTROL OPTION :',/                              TEFMAR    575
     &,'  PLOT STEP =',I5,3X,'PRINT STEP =',I5,'   REPORT STEP =',I5,/  TEFMAR    576
     &,'  DC SOLUTION DIVIDER = ',I2,/                                  TEFMAR    577
     &,'  INDUCTION MOTOR SOLUTION DIVIDER = ',I2,/                     TEFMAR    578
     &,'  INTEGRATION OPTION = ',I2,/                                   TEFMAR    579
     &,'  NETWORK SOLUTION OPTION = ',I2,/                              TEFMAR    580
     *,'  LOAD BUS SIMPLIFICATION OPTION = ',I2,/)                      TEFMAR    581
 1010 FORMAT(2F10.5,6I5,5X,I5,F10.5,I5)                                 TEFMAR    582
 1030 FORMAT(20A4)                                                      TEFMAR    583
 1040 FORMAT(3F10.5,6I5)                                                TEFMAR    584
 1050 FORMAT('  SWITCHING: ',20A4)                                      TEFMAR    585
 1060 FORMAT(/,'  MAXIMUM SPEED DEVIATION TO TERMINATE STUDY  =',F8.4   TEFMAR    586
     *,' IN PER UNIT',/                                                 TEFMAR    587
     *,'  INTENDED LENGTH OF SIMULATION TL',12X,'=',F8.4,' SECONDS',/   TEFMAR    588
     *,'  NOMINAL TIME INTERVAL H',21X,'=',F8.4,' SECONDS',/            TEFMAR    589
     *,'  NOMINAL DC SOLUTION DIVIDER IDC',13X,'=',I3,/                 TEFMAR    590
     *,'  NOMINAL INDUCTION MOTOR SOLUTION DIVIDER IM =',I3,//)         TEFMAR    591
 1100 FORMAT(' *** ERROR THE DC TIME STEP = ',F8.4,                     TEFMAR    592
     *' SHOULD BE SMALLER THAN')                                        TEFMAR    593
 1101 FORMAT('     THE AC TIME STEP = ',F8.4)                           TEFMAR    594
 1110 FORMAT(' *** ERROR THE DC CONTROL TIME STEP = ',F8.6,             TEFMAR    595
     &' SHOULD BE SMALLER THAN')                                        TEFMAR    596
 1111 FORMAT('     THE DC TIME STEP = ',F8.6)                           TEFMAR    597
 1200 FORMAT(32X,'SOLUTION REPORT',/,32X,15('='),/                      TEFMAR    598
     *,'                                    MAX. ANG.'                  TEFMAR    599
     *,'                        MAX. CUR.',/                            TEFMAR    600
     *,'     TIME     BUS#  STATION NAME GID   CHANGE'                  TEFMAR    601
     *,'     BUS#    BUS NAME    MISMATCH',/                            TEFMAR    602
     *,'    -----    -----  ------------ --- --------'                  TEFMAR    603
     *,'    -----  ------------ ---------')                             TEFMAR    604
 2000 FORMAT(///,'  TOTAL NUMBER OF TIME STEPS (NTSTEP) =',I7,/         TEFMAR    605
     *,'  TOTAL NUMBER OF ITERATIONS (NTITER) =',I7,/                   TEFMAR    606
     *,'  NTITER/NTSTEP                       =',F7.3,/)                TEFMAR    607
C                                                                       TEFMAR    608
      END                                                               TEFMAR    609
      SUBROUTINE SYSCEN                                                 TEFMAR    610
CD                                                                      TEFMAR    611
CD    THIS ROUTINE CALCULATES THE RLATIVE GENERATOR ANGLE & SPEED       TEFMAR    612
CD    WITH RESPECT TO THE SYSTEM CENTER                                 TEFMAR    613
CD                                                                      TEFMAR    614
      INCLUDE 'PARX.INS'                                                TEFMAR    615
      INCLUDE 'CNPT.INS'                                                TEFMAR    616
      INCLUDE 'CNB3.INS'                                                TEFMAR    617
      INCLUDE 'CNB8.INS'                                                TEFMAR    618
      INCLUDE 'CNMSTA.INS'                                              TEFMAR    619
      INCLUDE 'CNXSTA.INS'                                              TEFMAR    620
      INCLUDE 'CNMDAT.INS'                                              TEFMAR    621
      INCLUDE 'CNENG1.INS'                                              TEFMAR    622
C                                                                       TEFMAR    623
C                                                                       TEFMAR    624
      ANGLEC=0.0                                                        TEFMAR    625
      SPEEDC=0.0                                                        TEFMAR    626
CD                                                                      TEFMAR    627
CD    STEP 1. CALCULATE ANGLE AND SPEED OF THE SYSTEM CENTER            TEFMAR    628
CD                                                                      TEFMAR    629
      DO 100 N=1,NT                                                     TEFMAR    630
      JG=JCONTA(1,N)                                                    TEFMAR    631
      JSTATE=MSTATE(1,N)                                                TEFMAR    632
      IGENS=ICONTA(1,N)                                                 TEFMAR    633
      HCONST=DDAT(JG+1)                                                 TEFMAR    634
      SPEEDN=XSTATE(JSTATE+1)/HCONST                                    TEFMAR    635
      ANGLEN=XSTATE(JSTATE+2)/HCONST                                    TEFMAR    636
      IF(IGENS.GT.1)ANGLEN=ANGLEN+1.57075                               TEFMAR    637
      SPEEDC=SPEEDC+SPEEDN                                              TEFMAR    638
      ANGLEC=ANGLEC+ANGLEN                                              TEFMAR    639
 100  CONTINUE                                                          TEFMAR    640
      SPEEDC=SPEEDC*SYSTH                                               TEFMAR    641
      ANGLEC=ANGLEC*SYSTH                                               TEFMAR    642
C                                                                       TEFMAR    643
CD                                                                      TEFMAR    644
CD    STEP 2. CALCULATE AND STORE RELATIVE ANGLE AND SPEED OF ALL GENERATEFMAR    645
CD                                                                      TEFMAR    646
      DO 150 N=1,NT                                                     TEFMAR    647
      IGENS=ICONTA(1,N)                                                 TEFMAR    648
      JSTATE=MSTATE(1,N)                                                TEFMAR    649
      IF(IGENS.GT.1)THEN                                                TEFMAR    650
      RANGLE(N)=XSTATE(JSTATE+2)+1.57075-ANGLEC                         TEFMAR    651
      ELSE                                                              TEFMAR    652
      RANGLE(N)=XSTATE(JSTATE+2)-ANGLEC                                 TEFMAR    653
      ENDIF                                                             TEFMAR    654
      XSPEED(N)=XSTATE(JSTATE+1)-SPEEDC                                 TEFMAR    655
 150  CONTINUE                                                          TEFMAR    656
C                                                                       TEFMAR    657
      RETURN                                                            TEFMAR    658
      END                                                               TEFMAR    659
C                                                                       TEFMAR    660
C                                                                       TEFMAR    661
      SUBROUTINE ENGFUN(FUNCT)                                          TEFMAR    662
CD                                                                      TEFMAR    663
CD    THIS ROUTINE CALCULATES THE ENERGY FUNCTION OF ALL GENERATORS     TEFMAR    664
CD                                                                      TEFMAR    665
      INCLUDE 'PARX.INS'                                                TEFMAR    666
      INCLUDE 'CNPT.INS'                                                TEFMAR    667
      INCLUDE 'CNB3.INS'                                                TEFMAR    668
      INCLUDE 'CNB8.INS'                                                TEFMAR    669
      INCLUDE 'CNMYVA.INS'                                              TEFMAR    670
      INCLUDE 'CNYVAR.INS'                                              TEFMAR    671
      INCLUDE 'CNMDAT.INS'                                              TEFMAR    672
C                                                                       TEFMAR    673
      DIMENSION FUNCT(*)                                                TEFMAR    674
CD                                                                      TEFMAR    675
CD    STEP 1. CALCULATE THE ACCP TERM OF THE ENERGY FUNCTION OF EACH    TEFMAR    676
CD            GENERATOR AND THE SYSTEM CENTRE                           TEFMAR    677
CD                                                                      TEFMAR    678
      ACCPC=0.0                                                         TEFMAR    679
      DO 100 N=1,NT                                                     TEFMAR    680
      IGENS=ICONTA(1,N)                                                 TEFMAR    681
      JYG=MYVAR(1,N)                                                    TEFMAR    682
      IF(IGENS.EQ.4)THEN                                                TEFMAR    683
      XVALUE=ACCP(JYG+5)                                                TEFMAR    684
      ELSE                                                              TEFMAR    685
      XVALUE=ACCP(JYG+6)                                                TEFMAR    686
      ENDIF                                                             TEFMAR    687
      ACCPC=ACCPC+XVALUE                                                TEFMAR    688
      FUNCT(N)=XVALUE                                                   TEFMAR    689
 100  CONTINUE                                                          TEFMAR    690
C                                                                       TEFMAR    691
CD                                                                      TEFMAR    692
CD    STEP 2. CALCULATE THE SECOND ENERGY FUNCTION TERM                 TEFMAR    693
CD            OF EACH INDIVIDUAL GENERATOR                              TEFMAR    694
CD                                                                      TEFMAR    695
      DO 200 N=1,NT                                                     TEFMAR    696
      JG=JCONTA(1,N)                                                    TEFMAR    697
      FUNCT(N)=FUNCT(N)+ACCPC*SYSTH/DDAT(JG+1)                          TEFMAR    698
 200  CONTINUE                                                          TEFMAR    699
C                                                                       TEFMAR    700
      RETURN                                                            TEFMAR    701
      END                                                               TEFMAR    702
C                                                                       TEFMAR    703
C                                                                       TEFMAR    704
      SUBROUTINE KENERG                                                 TEFMAR    705
CD                                                                      TEFMAR    706
CD    THIS ROUTINE CALCULATES THE GENERATOR KINETIC ENERGY              TEFMAR    707
CD                                                                      TEFMAR    708
      INCLUDE 'PARX.INS'                                                TEFMAR    709
      INCLUDE 'CNENG1.INS'                                              TEFMAR    710
      INCLUDE 'CNPT.INS'                                                TEFMAR    711
      INCLUDE 'CNB3.INS'                                                TEFMAR    712
      INCLUDE 'CNB8.INS'                                                TEFMAR    713
      INCLUDE 'CNMDAT.INS'                                              TEFMAR    714
C                                                                       TEFMAR    715
C                                                                       TEFMAR    716
      TOTALK=0.0                                                        TEFMAR    717
      DO 100 N=1,NT                                                     TEFMAR    718
      JG=JCONTA(1,N)                                                    TEFMAR    719
      XKEN=XSPEED(N)*XSPEED(N)/DDAT(JG+1)                               TEFMAR    720
      XKE(N)=XKEN                                                       TEFMAR    721
      TOTALK=TOTALK+XKEN                                                TEFMAR    722
 100  CONTINUE                                                          TEFMAR    723
CD                                                                      TEFMAR    724
CD    STORE THE KE ENERGIES                                             TEFMAR    725
CD                                                                      TEFMAR    726
      RETURN                                                            TEFMAR    727
      END                                                               TEFMAR    728
C                                                                       TEFMAR    729
C                                                                       TEFMAR    730
      SUBROUTINE PENERG                                                 TEFMAR    731
CD                                                                      TEFMAR    732
CD    THIS ROUTINE CALCULATES THE GENERATOR POTENTIAL ENERGY            TEFMAR    733
CD                                                                      TEFMAR    734
      INCLUDE 'PARX.INS'                                                TEFMAR    735
      INCLUDE 'CNPT.INS'                                                TEFMAR    736
      INCLUDE 'CNB8.INS'                                                TEFMAR    737
      INCLUDE 'CNMSTA.INS'                                              TEFMAR    738
      INCLUDE 'CNXSTA.INS'                                              TEFMAR    739
      INCLUDE 'CNENG1.INS'                                              TEFMAR    740
C                                                                       TEFMAR    741
CD                                                                      TEFMAR    742
CD    CALCULATE ENERGY FUNCTION                                         TEFMAR    743
CD                                                                      TEFMAR    744
      CALL ENGFUN(FUNC)                                                 TEFMAR    745
CD                                                                      TEFMAR    746
CD    CALCULATE PE BY TRAPEZOIDAL INTEGRATION RULE                      TEFMAR    747
CD                                                                      TEFMAR    748
C                                                                       TEFMAR    749
      DO 100 N=1,NT                                                     TEFMAR    750
      JSTATE=MSTATE(1,N)                                                TEFMAR    751
      XANGN=XSTATE(JSTATE+2)                                            TEFMAR    752
      DELTA=XANGN-XANGLO(N)                                             TEFMAR    753
      XINC=(FUNC(N)-FOLD(N))*DELTA*0.5                                  TEFMAR    754
      VPE(N)=VPE(N)+XINC                                                TEFMAR    755
      TOTALP=TOTALP+XINC                                                TEFMAR    756
      FOLD(N)=FUNC(N)                                                   TEFMAR    757
      XANGLO(N)=XANGN                                                   TEFMAR    758
 100  CONTINUE                                                          TEFMAR    759
      RETURN                                                            TEFMAR    760
      END                                                               TEFMAR    761
C                                                                       TEFMAR    762
C                                                                       TEFMAR    763
      SUBROUTINE RWFACT(IKEY,IMODE)                                     TEFMAR    764
CD                                                                      TEFMAR    765
CD    THIS ROUTINE WRITES/READS THE NETWORK FACTORS                     TEFMAR    766
CD                                                                      TEFMAR    767
      INCLUDE 'PARX.INS'                                                TEFMAR    768
      INCLUDE 'CNUNIT.INS'                                              TEFMAR    769
      INCLUDE 'CNPT.INS'                                                TEFMAR    770
      INCLUDE 'CNSPRE.INS'                                              TEFMAR    771
      INCLUDE 'CNSPDI.INS'                                              TEFMAR    772
      INCLUDE 'CNSPC0.INS'                                              TEFMAR    773
C                                                                       TEFMAR    774
C     IMODE = 0 FOR WRITE                                               TEFMAR    775
C     IMODE = 1 FOR READ                                                TEFMAR    776
C                                                                       TEFMAR    777
      CALL CMPWR(IU59,IMODE,NLAST,STAIRS)                               TEFMAR    778
      CALL CMPWR(IU59,IMODE,LNFAC,RELEM)                                TEFMAR    779
      CALL CMPWR(IU59,IMODE,LNFAC,CELEM)                                TEFMAR    780
C                                                                       TEFMAR    781
      IF(IKEY.EQ.1)REWIND IU59                                          TEFMAR    782
C                                                                       TEFMAR    783
      RETURN                                                            TEFMAR    784
      END                                                               TEFMAR    785
