C
C
      SUBROUTINE ITR (MARKX,MARKN,NSFLOW,NSNVP,MADJC,ITCT)
C
C     ------------------------------------------------------------------
C
C       SUBROUTINE PURPOSE:
C       APPLICATION OF HARDY CROSS METHOD TO NETWORK BALANCING.
C
C     ------------------------------------------------------------------
C
      INCLUDE 'CMMNW.DAT'
C
C  ** APPLICATION OF HARDY CROSS METHOD.
C
5     IT=0
10    DQSUM=0.
      MBEGW=1
      DO 50 K=1,MNO
         MENDW=MEND(K)
         DPSUM=0.
         RQSUM=0.
         N=MSL(MBEGW)
         IF (N.LT.0) N=-N
         IF (NWTYP(N).EQ.(-1)) THEN
            MBEGW=MENDW+1
            GO TO 50
         ENDIF
         DO 30 J=MBEGW,MENDW
            N=MSL(J)
            FACT=1.
            IF (N.LT.0) THEN
               N=-N
               FACT=-1.
            ENDIF
            IF(NWTYP(N).EQ.1) THEN
               IF (NFNUM.GT.0) THEN
                  DO 20 L=1,NFNUM
                     IF (NFREG(L).EQ.N) THEN
                        RQSUM=RQSUM-(RGRAD(L)*100000)
C                                             ******
                        GO TO 25
                     ENDIF
20                CONTINUE
               ENDIF
25             IF (IABS(JS(N)).NE.JSB(N)) FACT=-FACT
               DPSUM=DPSUM-FACT*R(N)
            ELSE
               RQ2=R(N)*ABS(Q(N))*2.E-5
               RQSUM=RQSUM+RQ2
               DP=R(N)*Q(N)*ABS(Q(N))*1.E-10
               DPSUM=DPSUM+FACT*DP
            ENDIF
30       CONTINUE
         IF (ABS(RQSUM).GT.1.E-10) THEN
            DQ=(DPSUM-FNVP(K))*1.E5/RQSUM
         ELSE
            DQ=0.0
         ENDIF
          DO 40 J=MBEGW,MENDW
            N=MSL(J)
            FACT=1.
            IF (N.LT.0) THEN
               N=-N
               FACT=-1.
            ENDIF
            Q(N)=Q(N)-(DQ*FACT)
40       CONTINUE
         DQSUM=DQSUM+ABS(DQ)
         MBEGW=MENDW+1
50    CONTINUE
C
      DO 100 KI=1,NB
         IF (NWTYP(KI).EQ.1.AND.NFNUM.GT.0) THEN
C            ***************
            DO 90 J=1,NFNUM
               IF (NOF(J).EQ.NO(KI)) THEN
                  NFCW(J)=0
                  NEGQ(J)=0
                  IF (Q(KI).LT.0.0.AND.IABS(JS(KI)).EQ.JSB(KI))
     .               NEGQ(J)=1
                  IF (Q(KI).GT.0.0.AND.IABS(JS(KI)).NE.JSB(KI))
     .               NEGQ(J)=1
                  IF (NVPN.GE.1.AND.MADJC.LE.0) THEN
                     TABF=TR
                  ELSE
                     NABF=JS(KI)
                     DO 70 L=1,NJ
                        IF (NABF.EQ.JNO(L)) THEN
                           TABF=T(L)
                           GO TO 80
                        ENDIF
70                   CONTINUE
                  ENDIF
80                IF (NEGQ(J).EQ.0) THEN
C                 ************************
                     IF (NSWT(J).EQ.0) NSWT(J)=2
                     IF (NSKP(J).EQ.0) THEN
                        IF (NSWT(J).EQ.2) THEN
C                       ======================
                           CALL SPLINE (J,Q(KI),R(KI),TABF,0)
                        ELSEIF (NSWT(J).EQ.1) THEN
C                        ==========================
                           CALL LSFAN (J,Q(KI),R(KI),TABF,0,0)
C                          ===================================
                        ENDIF
C                       =====
                        NSKP(J)=1
                     ELSE
                        IF (NSWT(J).EQ.2) THEN
C                       =======================
                           CALL SPLINE (J,Q(KI),R(KI),TABF,1)
                        ELSEIF (NSWT(J).EQ.1) THEN
C                       ===========================
                           CALL LSFAN (J,Q(KI),R(KI),TABF,1,0)
C                          ===================================
                        ENDIF
C                       =====
                     ENDIF
                     GO TO 100
C                *************************************
                  ELSE
                     R(KI)=PF(J,1)
                     R(KI)=R(KI)*(TR+460.)/(TABF+460.)
                     RGRAD(J)=0.0
C                 ************************************
                  ENDIF
              ENDIF  
90          CONTINUE
         ENDIF
100   CONTINUE
C
C  ** CONTINUE COMPUTING IN PROPER PATH.
C     MARKX: 0: THE SUB. IS CALLED FROM NETWORK OR TEMP. PART OF
C            THE MAIN PROG..  1: CALLED FROM DYNAMIC SIMULATION PART.
C     MARKN: 0: NORMAL CONDITION.  1: SUB. IS TERMINATED DUE TO USER-
C
      IT=IT+1
         ITCT=ITCT+1
      IF (MARKX.LE.0) THEN
         IF (IT.LE.1) THEN
            GO TO 10
         ELSE IF ((DQSUM/FLOAT(MNO)).LT.10.) THEN
            RETURN
C         ELSE IF (ITCT.GT.80.AND.ITCT.LE.500) THEN
C            WRITE(8,*) 'DQSUM: ',DQSUM
         ELSE IF (ITCT.GT.500) THEN
C                         ***
             MARKN=1
             RETURN
         ELSE IF (IT.GT.50) THEN
C                      ****
            CALL ARR (MARKX)
            CALL BASE (NSTOP)
            CALL MSLIST (NSTOP)
            CALL MBLNC
C
C     NSNVP: 0: SUB. IS CALLED FROM NETWORK PART.  1: SUB. IS
C            CALLED FROM TEMP. OR DYNAMIC SIMULATION PART.
C
            IF (NSNVP.LE.0) THEN
               IF (NVPN.LE.0) CALL NVP1
            ELSE
               CALL NVP2
            ENDIF
            GO TO 5
         ENDIF
      ELSE
         IF (DQSUM/FLOAT(NB-NJ+1).LT.10.) RETURN
         IF (IT.GT.200) THEN
            WRITE (8,200)
            RETURN
         ENDIF
CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
         IF (INIFAN.EQ.1.AND.IT.GT.50) THEN
            CALL ARR (MARKX)
            CALL BASE (NSTOP)
            CALL MSLIST (NSTOP)
            CALL MBLNC
            CALL NVP1
C           *********
            CALL NVP2
            GO TO 10  
         ENDIF
CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
      ENDIF
      GO TO 10
C
200   FORMAT (//,1X,'* ATTN * THE ACCURACY IN NETWORK BALANCING ',
     .        'WAS NOT SATISFIED',/,' DUE TO ITERATIONS BECAME ',
     .        'EXCESIVE.')
C
      END
