!-------------------------------------------------------------------
!  sawnbi - apply effect of kadomtsev sawtooth on fast ion
!  population that was previously computed by pre_sawnbi.
!
subroutine sawnbi_exec
  !
  !  dmc nov 2005 -- parallelized version; executes inside parallel
  !  section of orball
  !
  !-------------------------------------------------------------------
  !  f90 modules...
  !
  use nbi_com
  use nbi_mpi_com

  !-------------------------------------------------------------------
  !  see pre_sawnbi subroutine...
  !
  !  the data to be copied is:
  !
  !  modified copies of the monte carlo fast ion list arrays
  !  (cf nbspec.dat):
  !     njavay <--   njavay_saw
  !     rmjionay <-- rmjionay_saw
  !     xzionay <--  xzionay_saw
  !     tfluxay <--  tfluxay_saw
  !     xmuay <--    xmuay_saw
  !     thay <--     thay_saw
  !     pphiay <--   pphiay_saw
  !     pmechay <--  pmechay_saw
  !     vay <--      vay_saw
  !     xiay <--     xiay_saw
  !     xistay <--   xistay_saw
  !     xksidy <--   xksidy_saw
  !
  !   (some mc list arrays are unaffected and some are reset to constants).
  !
  !---------------------------------------------------------------

  implicit none
  integer :: iptcl,ind
  integer :: find_ptcl_ind
  real(fp) :: tdepay_ind

  !---------------------------------------------------------------
  !
#ifndef __MPI
  integer :: myid_debug, numproc_debug
  if(debug_mpi.and.nltest_output) then
     myid_debug=myidd
     numproc_debug=numprocd
  end if
#endif

  call nbi_mpi_com_init

#ifndef __MPI
  if(debug_mpi.and.nltest_output) then
     myidd=myid_debug       ! do not redefine  myidd and nuprocd for
     numprocd=numproc_debug ! debugging of mpi version by serial code
  end if
#endif

  if(nuse(lsbeam).eq.0) return     ! no particles...
  !
  !  sawtooth applied to current species...
  !
  do iptcl=1,nuse(lsbeam)
     ind=nbndex(iptcl,lsbeam)
     ind=find_ptcl_ind(numprocd,myidd,ind)
     tdepay_ind=max(0.0_fp,tdepay(ind,lsbeam)-tbm1) !mg 11/2013
     if(tdepay_ind.eq.0.0_fp) then
        njavay(ind,lsbeam) =   njavay_saw(ind,lsbeam)
        rmjionay(ind,lsbeam) = rmjionay_saw(ind,lsbeam)
        xzionay(ind,lsbeam) =  xzionay_saw(ind,lsbeam)
        tfluxay(ind,lsbeam) =  tfluxay_saw(ind,lsbeam)
        xmuay(ind,lsbeam) =    xmuay_saw(ind,lsbeam)
        thay(ind,lsbeam) =     thay_saw(ind,lsbeam)
        pphiay(ind,lsbeam) =   pphiay_saw(ind,lsbeam)
        pmechay(ind,lsbeam) =  pmechay_saw(ind,lsbeam)
        vay(ind,lsbeam) =      vay_saw(ind,lsbeam)
        xiay(ind,lsbeam) =     xiay_saw(ind,lsbeam)
        xistay(ind,lsbeam) =   xistay_saw(ind,lsbeam)
        xksidy(ind,lsbeam) =   xksidy_saw(ind,lsbeam)
        !
        !  reinitialize numeric controls:  new orbit
        !
        goosay(ind,lsbeam)=1.00001_fp
        fppray(ind,lsbeam)=1.0_fp
        cxpray(ind,lsbeam)=1.0_fp
     end if

  end do
  !
  return
end subroutine sawnbi_exec
