!-----------------------------------------------------------------
!
SUBROUTINE KDSAWMF2(J,IX1,IX2,ZX1,ZX1P,ZX2,ZX2P,ZF,ZFP,ZFPS,ZFPSP)
!
  use kdsaw_module
  implicit NONE
!
!  kdsaw internal routine
!
  integer j,ix1,ix2
  real(fp) :: zx1,zx1p,zx2,zx2p,zf,zfp,zfps,zfpsp
!
!  CALCULATE FRACTION OF PRE-SAWTOOTH ZONES MIXED INTO A POST SAWTOOTH
!  ZONE AS PER KADOMTSEV.
!
!  INPUT:  PASSED
!
!   J  POST SAWTOOTH ZONE NUMBER
!   IX1  1ST PRESAWTOOTH ZONE MIXED IN
!   IX2  2ND PRESAWTOOTH ZONE MIXED IN
!    ZX1,ZX1P-- XI LIMITS OF PART OF 1ST PRESAWTOOTH ZONE GETTING MIXED
!    ZX2,ZX2P-- DITTO FOR THE 2ND PRESAWTOOTH ZONE PIECE
!   ZF,ZFP  TOROIDAL FLUX SPANNED BY BOTH THE PRESAWTOOTH ZONE PIECES
!   ZFPS,ZFPSP  TOROIDAL FLUX SPANNED BY THE POST SAWTOOTH ZONE
!
!  INPUT:  COMMON
!   DVOL  ZONE VOLUMES
!   XI    ZONE CTR AND BDY XI VALUES (NORMALIZED TOROIDAL FLUX)
!
!  OUTPUT:  SCRATCH COMMON
!   INZMIX(J)  NUMBER OF PRE-SAWTOOTH ZONE PIECES MIXED IN TO FORM THE
!              POST SAWTOOTH ZONE-- INCREASED BY TWO WITH ERROR CHECK
!   IZMIX(--,J)  PRESAWTOOTH ZONE NUMBERS CONTAINING THE MIXED IN ZONE
!              PIECES
!   ZZMIX(--,J)  FRACTION OF MATERIAL (OR ENERGY) OF PRESAWTOOTH ZONE
!              GETTING MAPPED INTO POST-SAWTOOTH ZONE DUE TO TRANSFER
!              OF THESE TWO PIECES.  NOTE THAT PRE-SAWTOOTH ZONE PIECES
!              MAY NOT CONSTITUTE COMPLETE PRE-SAWTOOTH ZONES, **AND**
!              YET MAY MAP TO COVER MORE THAN ONE COMPLETE POST SAWTOOTH
!              ZONE.  THE KADOMTSEV PRINCIPAL (GENERALIZED FOR TRANSP)
!              IS TO MAP TOGETHER REGIONS OF THE SAME PRE-SAWTOOTH
!              HELICAL FLUX, WHILE PRESERVING THE ASSOCIATED TOROIDAL
!              FLUX.  THUS ZONES TOWARDS THE OUTSIDE OF THE MIXING
!              REGION COVER A LOT MORE FLUX AND PIECES FROM SUCH ZONES
!              MIGHT SUFFICE TO COVER TWO OR THREE POST SAWTOOTH FLUX
!              ZONES NEAR THE MAGNETIC AXIS.
!
!-------------------------------------------------------------------
  integer iz1,iz2,ix1p1,ix2p1
  real(fp) :: zx1a,zx1b,zx2a,zx2b,zfrac0,zfrac1,zfrac2
!-------------------------------------------------------------------
!  DMC 12 AUG 1991 --
!  LOGIC TO CHECK INPUT ARGUMENTS MOVED HERE
!  -- CIRCUMVENT DECSTATION COMPILER BUG
!
  IF(ZF.LE.ZFPSP) RETURN
!
!  INCREMENT INDEXING
!
  IZ1=INZMIX(J)+1
  IZ2=IZ1+1
  INZMIX(J)=IZ2
!
  IZMIX(IZ1,J)=IX1
  IZMIX(IZ2,J)=IX2
!
!  FRACTIONS DUE TO ZONE PIECES NOT BEING COMPLETE PRE-SAWTOOTH ZONES
!
  ZX1A=MIN(ZX1,ZX1P)
  ZX1B=MAX(ZX1,ZX1P)
!
  ZX2A=MIN(ZX2,ZX2P)
  ZX2B=MAX(ZX2,ZX2P)
!
  IX1P1=IX1+1
  ZFRAC1=(ZX1B**two-ZX1A**two)/(XI(IX1P1,1)**two-XI(IX1,1)**two)
!
  IX2P1=IX2+1
  ZFRAC2=(ZX2B**two-ZX2A**two)/(XI(IX2P1,1)**two-XI(IX2,1)**two)
!
!  FRACTION DUE TO PRE-S.T. ZONE PIECES' TOROIDAL FLUX EXCEEDING
!   TOROIDAL FLUX FOR POST-SAWTOOTH ZONE
!
  ZFRAC0=MIN(1.0d0,(MIN(ZF,ZFPS)-MAX(ZFP,ZFPSP))/(ZF-ZFP))
!
!  ZONE MAPPING FRACTIONS
!
  ZZMIX(IZ1,J)=ZFRAC0*ZFRAC1
  ZZMIX(IZ2,J)=ZFRAC0*ZFRAC2
!
  RETURN
!
end subroutine kdsawmf2
