*DECK MOCFCF
      SUBROUTINE MOCFCF(SUBFFI,SUBFFA,SUBSCH,IFTRAK,NBTR,MXSEG,NUNKNO,
     1                  NREG,NSOUT,NMAT,NALB,NGEFF,NPHI,NGSS,NLF,NFUNL,
     2                  NMOD,KEYFLX,MATALB,NCONV,SIGANG,PHIANG,XGSS,
     3                  YGSS,WGSS,PHIOUT,SOUR,ISGNR)
*
*-----------------------------------------------------------------------
*
*Purpose:
* flux integration upon the cyclic 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. Roy and 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.
* NGEFF   number of groups to process.
* NMAT    number of mixtures.
* NLF     number of Legendre orders for the flux.
* NREG    number of regions.
* NUNKNO  number of unknowns per energy group including spherical
*         harmonic terms and fundamental currents.
* NGSS    number of polar angles.
* NSOUT   number of surfaces.
* MXSEG   maximun number of segments in a track.
* NBTR    number of tracks.
* NPHI    number of angles in the plan.
* NFUNL   number of moments of the flux (in 2D : NFUNL=NLF*(NLF+1)/2).
* NMOD    second dimension of ISGNR.
* ISGNR   array of spherical harmonics signs.
* PHIANG  array of cosines of angles in the plan.
* KEYFLX  position of flux elements in PHIIN vector.
* MATALB  mixture and albedo indices.
* WGSS    polar weights.
* XGSS    polar angle cosines.
* YGSS    polar angle sines.
* NALB    number of albedos.
* SIGANG  arrays of total cross-sections and albedos.
* SOUR    total source vector components.
* NCONV   logical array of convergence status for each group (.TRUE.:
*         not converged)
*
*Parameters: output
* PHIOUT  vector containing the zonal flux moments.
*
*-----------------------------------------------------------------------
*
      IMPLICIT  NONE
*----
*  SUBROUTINE ARGUMENTS
*----
      INTEGER IFTRAK,NGEFF,NMAT,NLF,NREG,NUNKNO,NGSS,NSOUT,MXSEG,
     1 NBTR,NPHI,NFUNL,NMOD,ISGNR(NFUNL,NMOD),KEYFLX(NREG,NFUNL),
     2 MATALB(-NSOUT:NREG),NALB
      REAL PHIANG(NPHI),WGSS(NGSS),XGSS(NGSS),YGSS(NGSS),
     1 SIGANG(-NALB:NMAT,NGEFF)
      DOUBLE PRECISION SOUR(NUNKNO,NGEFF),PHIOUT(NUNKNO,NGEFF)
      LOGICAL NCONV(NGEFF)
      EXTERNAL SUBFFI,SUBFFA,SUBSCH
      INTEGER, ALLOCATABLE, DIMENSION(:) :: NRSEG
      REAL, ALLOCATABLE, DIMENSION(:) :: SEGLEN
      REAL, ALLOCATABLE, DIMENSION(:,:) :: RHARM
      REAL, ALLOCATABLE, DIMENSION(:,:,:) :: TRHAR
      DOUBLE PRECISION, ALLOCATABLE, DIMENSION(:) :: EXPT,EXP2
      DOUBLE PRECISION, ALLOCATABLE, DIMENSION(:,:) :: FLM,FLP,CYM,CYP
*----
*  LOCAL VARIABLES
*----
      INTEGER MXE
      PARAMETER (MXE=64)
      INTEGER ILINE,IANG,NSEG,I,IE,II,NOMI,NZI,IND,IANG0,NL,IM,JF,
     1 NUMOLD,I0,IFACE
      REAL WEIGHT
      DOUBLE PRECISION DWEIG(MXE),Q0,Q1
      INTEGER NEWMOD(8,3)
      DATA NEWMOD/ 2,1,4,3,6,5,8,7,
     1             3,4,1,2,7,8,5,6,
     2             5,6,7,8,1,2,3,4 /
      SAVE NEWMOD
*----
*  SCRATCH STORAGE ALLOCATION
*----
      ALLOCATE(NRSEG(MXSEG),SEGLEN(MXSEG),FLM(NGSS,MXSEG),
     1 FLP(NGSS,MXSEG),CYM(NGSS,MXSEG),CYP(NGSS,MXSEG))
      ALLOCATE(EXPT(NGSS*MXSEG),EXP2(2*NGSS*MXSEG))
*----
*  ISOTROPIC SCATTERING
*----
      IF(NLF.EQ.1) THEN
      DO ILINE=1,NBTR
         READ(IFTRAK) IANG,NSEG,WEIGHT,(NRSEG(I),I=1,NSEG),
     1                                 (SEGLEN(I),I=1,NSEG)
         DO IE=1,NGSS
            DWEIG(IE)=WEIGHT*WGSS(IE)
         ENDDO
         DO II=1,NGEFF
         IF(NCONV(II)) THEN
            DO I=1,NSEG
               NOMI=NRSEG(I)
               NZI=MATALB(NOMI)
               IF(NZI.LT.0) THEN
                  DO IE=1,NGSS
                     FLM(IE,I)=0.D0
                     FLP(IE,I)=0.D0
                  ENDDO
               ELSE
                  IND=KEYFLX(NOMI,1)
                  DO IE=1,NGSS
                     FLM(IE,I)=DWEIG(IE)*SOUR(IND,II)
                     FLP(IE,I)=FLM(IE,I)
                  ENDDO
               ENDIF
            ENDDO
*                MOCFFI:  'Source Term Isolation' Strategy turned on
*                MOCFFIS: 'Source Term Isolation' Strategy turned off
*                MOCFFIT: 'MOCC/MCI' Iterative Strategy
            CALL SUBFFI(SUBSCH,NREG,NSOUT,NUNKNO,NMAT,NSEG,SEGLEN,NRSEG,
     1           NGSS,MATALB,SIGANG(-NALB,II),KEYFLX,YGSS,PHIOUT(1,II),
     2           EXPT,EXP2,FLM,FLP,CYM,CYP)
         ENDIF
         ENDDO
      ENDDO
*----
*  ANISOTROPIC SCATTERING
*----
      ELSE
      ALLOCATE(RHARM(NGSS,NLF**2),TRHAR(NGSS,NFUNL,NMOD))
      IANG0=0
      NL=NLF-1
      DO ILINE=1,NBTR
         READ(IFTRAK) IANG,NSEG,WEIGHT,(NRSEG(I),I=1,NSEG),
     1                                 (SEGLEN(I),I=1,NSEG)
         IF(IANG.NE.IANG0) THEN
            IANG0=IANG
            CALL MOCCHR(2,NL,NGSS,XGSS,PHIANG(IANG),RHARM)
            DO 10 IM=1,NMOD
            DO 10 JF=1,NFUNL
            DO 10 IE=1,NGSS
               TRHAR(IE,JF,IM)=ISGNR(JF,IM)*RHARM(IE,JF)
 10       CONTINUE
         ENDIF
         DO IE=1,NGSS
            DWEIG(IE)=WEIGHT*WGSS(IE)
         ENDDO         
         DO II=1,NGEFF
         IF(NCONV(II)) THEN
            I0=1
            NUMOLD=NRSEG(1)
            DO I=1,NSEG
               NOMI=NRSEG(I)
               NZI=MATALB(NOMI)
               IF(NZI.LT.0) THEN
                  IF(NUMOLD.NE.NOMI) THEN
                     IFACE=(1-NZI)/2
                     I0=NEWMOD(I0,IFACE)
                     DO IE=1,NGSS
                        FLM(IE,I)=0.D0
                        FLP(IE,I)=0.D0
                     ENDDO
                  ELSE
                     DO IE=1,NGSS
                        FLM(IE,I)=0.D0
                        FLP(IE,I)=0.D0
                     ENDDO
                  ENDIF
                  NUMOLD=NOMI
               ELSE
                  DO IE=1,NGSS
                     Q0=0.D0
                     Q1=0.D0
                     DO JF=1,NFUNL
                        IND=KEYFLX(NOMI,JF)
                        Q0=Q0+SOUR(IND,II)*TRHAR(IE,JF,I0)
                        Q1=Q1+SOUR(IND,II)*TRHAR(IE,JF,5-I0)
                     ENDDO
                     FLM(IE,I)=DWEIG(IE)*Q0
                     FLP(IE,I)=DWEIG(IE)*Q1
                  ENDDO
               ENDIF
            ENDDO
*                MOCFFA:  'Source Term Isolation' Strategy turned on
*                MOCFFAS: 'Source Term Isolation' Strategy turned off
*                MOCFFAT: 'MOCC/MCI' Iterative Strategy
            CALL SUBFFA(SUBSCH,NREG,NSOUT,NUNKNO,NMAT,NSEG,SEGLEN,NRSEG,
     1           NGSS,NFUNL,NMOD,MATALB,SIGANG(-NALB,II),KEYFLX,YGSS,
     2           PHIOUT(1,II),EXPT,EXP2,FLM,FLP,CYM,CYP,NEWMOD,TRHAR)
         ENDIF
         ENDDO
      ENDDO
      DEALLOCATE(TRHAR,RHARM)
      ENDIF
*----
*  SCRATCH STORAGE DEALLOCATION
*----
      DEALLOCATE(EXP2,EXPT)
      DEALLOCATE(CYP,CYM,FLP,FLM,SEGLEN,NRSEG)
*
      RETURN
      END
