*DECK MCGFCF
      SUBROUTINE MCGFCF(SUBFFI,SUBFFA,SUBSCH,IFTRAK,NBTR,NMAX,NDIM,KPN,
     1                  K,NREG,M,NGEFF,NANGL,NMU,NANI,NFUNL,NMOD,
     2                  KEYFLX,KEYCUR,NZON,NCONV,CAZ,CPO,ZMU,
     3                  WZMU,PHI,S,SIGAL,ISGNR)
*
*-----------------------------------------------------------------------
*
*Purpose:
* flux integration upon the tracking.
*
*Copyright:
* Copyright (C) 2002 Ecole Polytechnique de Montreal
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version
*
*Author(s): R. Le Tellier
*
*Parameters: input
* SUBFFI  isotropic flux integration subroutine.
* SUBFFA  anisotropic flux integration subroutine.
* SUBSCH  track coefficients calculation subroutine.
* IFTRAK  tracking file unit number.
* NBTR    total number of tracking lines.
* NMAX    maximum number of elements in a track.
* NDIM    number of dimensions for the geometry.
* KPN     total number of unknowns in vectors PHI.
* K       total number of volumes for which specific values
*         of the neutron flux and reactions rates are required.
* NREG    number of volumes.
* M       number of material mixtures.
* NGEFF   number of groups to process.
* NANGL   number of tracking angles in the tracking file.
* NMU     order of the polar quadrature in 2D / 1 in 3D.
* NANI    scattering anisotropy (=1 for isotropic scattering).
* NFUNL   number of moments of the flux (in 2D : NFUNL=NANI*(NANI+1)/2).
* NMOD    second dimension of ISGNR.
* KEYFLX  position of flux elements in PHI vector.
* KEYCUR  position of current elements in PHI vector.
* NZON    index-number of the mixture type assigned to each volume.
* NCONV   logical array of convergence status for each group (.TRUE.:
*         not converged)
* CAZ     cosines of the different tracking azimuthal angles.
* CPO     cosines of the different tracking polar angles.
*         (polar quadrature in 2D / tracking angles in 3D).
* ZMU     polar quadrature set in 2D.
* WZMU    polar quadrature set in 2D.
* S       total source vector components.
* SIGAL   total cross-section and albedo array.
*
*Parameters: input/output
* PHI     vector containing the zonal scalar flux
*
*-----------------------------------------------------------------------
*
      IMPLICIT NONE
*----
*  SUBROUTINE ARGUMENTS
*----
      INTEGER NGEFF,K,KPN,M,NMAX,NDIM,NMU,NZON(K),NANI,
     1 NFUNL,NMOD,NREG,KEYFLX(NREG,NFUNL),KEYCUR(K-NREG),IFTRAK,NBTR,
     2 NANGL,ISGNR(NFUNL,NMOD)
      REAL ZMU(NMU),WZMU(NMU),SIGAL(-6:M,NGEFF),CAZ(NANGL),CPO(*)
      DOUBLE PRECISION PHI(KPN,NGEFF),S(KPN,NGEFF)
      LOGICAL NCONV(NGEFF)
      EXTERNAL SUBFFI,SUBFFA,SUBSCH
*----
*  LOCAL VARIABLES
*----
      INTEGER II,ILINE,IANG,NSEG,I,IMU,NL,IANG0,NOMP,INDP,NOMM,INDM,
     1 NOMI,JF,IND
      REAL WEITF,WEIGHT,ZMUI
      DOUBLE PRECISION Q0,Q1
      INTEGER, ALLOCATABLE, DIMENSION(:) :: NOM
      REAL, ALLOCATABLE, DIMENSION(:) :: HTF,T2D
      REAL, ALLOCATABLE, DIMENSION(:,:) :: RHARM
      REAL, ALLOCATABLE, DIMENSION(:,:,:) :: TRHAR
      DOUBLE PRECISION, ALLOCATABLE, DIMENSION(:) :: B
      DOUBLE PRECISION, ALLOCATABLE, DIMENSION(:,:,:,:) :: STOT
*----
*  SCRATCH STORAGE ALLOCATION
*----
      ALLOCATE(NOM(NMAX),HTF(NMAX),B(4*NMAX))
*---
* Compute flux and currents for this tracking line
*---
      IF (NANI.EQ.1) THEN
*     --------------------
*     Isotropic Scattering
*     --------------------
         IF (NDIM.EQ.3) THEN
*        ---
*        3D calculation -> no loop over a polar angle
*        ---
         DO ILINE=1,NBTR
            READ(IFTRAK) IANG,NSEG,WEITF,(NOM(I),I=1,NSEG),
     1                                   (HTF(I),I=1,NSEG)
            DO II=1,NGEFF
            IF (NCONV(II)) THEN
*                   MCGFFI:  'Source Term Isolation' Strategy turned on
*                   MCGFFIS: 'Source Term Isolation' Strategy turned off
*                   MCGFFIT: 'MOCC/MCI' Iterative Strategy
               CALL SUBFFI(SUBSCH,K,KPN,M,NSEG,HTF,NOM,NZON,SIGAL(0,II),
     1              S(1,II),NREG,KEYFLX,KEYCUR,PHI(1,II),B,WEITF)
            ENDIF
            ENDDO
         ENDDO
*        ---
         ELSE
*        ---
*        2D calculation -> loop over the polar angle
*        ---
         ALLOCATE(T2D(NMAX))
         DO ILINE=1,NBTR
            READ(IFTRAK) IANG,NSEG,WEITF,(NOM(I),I=1,NSEG),
     1                                   (HTF(I),I=1,NSEG)
            DO IMU=1,NMU
               ZMUI=ZMU(IMU)
               WEIGHT=WEITF*WZMU(IMU)
               DO I=2,NSEG-1
                  T2D(I)=HTF(I)*ZMUI
               ENDDO
               DO II=1,NGEFF            
               IF (NCONV(II)) THEN
                  CALL SUBFFI(SUBSCH,K,KPN,M,NSEG,T2D,NOM,NZON,
     1                 SIGAL(0,II),S(1,II),NREG,KEYFLX,KEYCUR,PHI(1,II),
     2                 B,WEIGHT)
               ENDIF
               ENDDO
            ENDDO
         ENDDO
         DEALLOCATE(T2D)
         ENDIF
*        ---
      ELSE
*     ----------------------
*     Anisotropic Scattering
*     ----------------------
         ALLOCATE(STOT(NMAX,NMU,NGEFF,2))
         CALL XDDSET(STOT,2*NMU*NMAX*NGEFF,0.0D0)
         ALLOCATE(RHARM(NMU,NANI**2),TRHAR(NMU,NFUNL,NMOD))
         NL=NANI-1
         IANG0=0
         IF (NDIM.EQ.3) THEN
*        ---
*        3D calculation -> no loop over a polar angle
*        ---
         DO ILINE=1,NBTR
            READ(IFTRAK) IANG,NSEG,WEITF,(NOM(I),I=1,NSEG),
     1                                   (HTF(I),I=1,NSEG)
            IF (IANG.NE.IANG0) THEN
               IANG0=IANG
               CALL MOCCHR(3,NL,1,CPO(IANG),CAZ(IANG),RHARM)
               DO 10 JF=1,NFUNL
                  TRHAR(1,JF,1)=ISGNR(JF,1)*RHARM(1,JF)
                  TRHAR(1,JF,NMOD)=ISGNR(JF,NMOD)*RHARM(1,JF)
 10            CONTINUE
            ENDIF
            DO II=1,NGEFF            
            IF (NCONV(II)) THEN
*              incoming flux in + direction
               NOMP=NOM(1)
               INDP=KEYCUR(-NOMP)
*              incoming flux in - direction
               NOMM=NOM(NSEG)
               INDM=KEYCUR(-NOMM)
               STOT(1,1,II,1)=WEITF*S(INDP,II)
               STOT(NSEG,1,II,2)=WEITF*S(INDM,II)
*              regional sources   
               DO I=2,NSEG-1
                  NOMI=NOM(I)
                  Q0=0.0D0
                  Q1=0.0D0
                  DO JF=1,NFUNL
                     IND=KEYFLX(NOMI,JF)         
                     Q0=Q0+S(IND,II)*TRHAR(1,JF,1)
                     Q1=Q1+S(IND,II)*TRHAR(1,JF,NMOD)
                  ENDDO                       
                  STOT(I,1,II,1)=WEITF*REAL(Q0)
                  STOT(I,1,II,2)=WEITF*REAL(Q1)
               ENDDO
            ENDIF
            ENDDO
            DO II=1,NGEFF
            IF (NCONV(II)) THEN
*                   MCGFFA:  'Source Term Isolation' Strategy turned on
*                   MCGFFAS: 'Source Term Isolation' Strategy turned off
*                   MCGFFAT: 'MOCC/MCI' Iterative Strategy
               CALL SUBFFA(SUBSCH,K,KPN,M,NSEG,HTF,NOM,NZON,SIGAL(0,II),
     1              STOT(1,1,II,1),STOT(1,1,II,2),NREG,1,NANI,NFUNL,
     2              NMOD,TRHAR,KEYFLX,KEYCUR,1,PHI(1,II),B,1,NMOD)
            ENDIF
            ENDDO
         ENDDO
*        ---
         ELSE
*        ---
*        2D calculation -> loop over the polar angle
*        ---
         ALLOCATE(T2D(NMAX))
         DO ILINE=1,NBTR
            READ(IFTRAK) IANG,NSEG,WEITF,(NOM(I),I=1,NSEG),
     1                                   (HTF(I),I=1,NSEG)
            IF (IANG.NE.IANG0) THEN
               IANG0=IANG
               CALL MOCCHR(2,NL,NMU,CPO,CAZ(IANG),RHARM)
               DO 20 JF=1,NFUNL
               DO 20 IMU=1,NMU
                  TRHAR(IMU,JF,1)=ISGNR(JF,1)*RHARM(IMU,JF)
                  TRHAR(IMU,JF,NMOD)=ISGNR(JF,NMOD)*RHARM(IMU,JF)
 20            CONTINUE
            ENDIF
            DO II=1,NGEFF            
            IF (NCONV(II)) THEN
*              incoming flux in + direction
               NOMP=NOM(1)
               INDP=KEYCUR(-NOMP)
*              incoming flux in - direction
               NOMM=NOM(NSEG)
               INDM=KEYCUR(-NOMM)
               DO IMU=1,NMU
                  WEIGHT=WEITF*WZMU(IMU)
                  STOT(1,IMU,II,1)=WEIGHT*S(INDP,II)
                  STOT(NSEG,IMU,II,2)=WEIGHT*S(INDM,II)
               ENDDO
*              regional sources               
               DO I=2,NSEG-1
                  NOMI=NOM(I)
                  DO IMU=1,NMU
                     Q0=0.0D0
                     Q1=0.0D0
                     WEIGHT=WEITF*WZMU(IMU)
                     DO JF=1,NFUNL
                        IND=KEYFLX(NOMI,JF)         
                        Q0=Q0+S(IND,II)*TRHAR(IMU,JF,1)
                        Q1=Q1+S(IND,II)*TRHAR(IMU,JF,NMOD)
                     ENDDO                       
                     STOT(I,IMU,II,1)=WEIGHT*REAL(Q0)
                     STOT(I,IMU,II,2)=WEIGHT*REAL(Q1)
                  ENDDO
               ENDDO
            ENDIF
            ENDDO
            DO IMU=1,NMU
               ZMUI=ZMU(IMU)
               WEIGHT=WEITF*WZMU(IMU)
               DO I=2,NSEG-1
                  T2D(I)=HTF(I)*ZMUI
               ENDDO
               DO II=1,NGEFF            
               IF (NCONV(II)) THEN
                  CALL SUBFFA(SUBSCH,K,KPN,M,NSEG,T2D,NOM,NZON,
     1                 SIGAL(0,II),STOT(1,IMU,II,1),STOT(1,IMU,II,2),
     2                 NREG,NMU,NANI,NFUNL,NMOD,TRHAR,KEYFLX,KEYCUR,
     3                 IMU,PHI(1,II),B,1,NMOD)
               ENDIF
               ENDDO
            ENDDO
         ENDDO
*        ---
         DEALLOCATE(T2D)
         ENDIF
         DEALLOCATE(TRHAR,RHARM,STOT)
      ENDIF
*----
*  SCRATCH STORAGE DEALLOCATION
*----
      DEALLOCATE(B,HTF)
      RETURN
      END
