!-----------------------------------------------------------------------
!  riplos -- ripple loss model for monte carlo fast ions
!
!   dmc december 1989
!
!
subroutine riplos(ztslmg,iripl,ierr)
  !
  use nbi_com
  use nbi_random_pll
  !
  !
  !  passed input:  (goosed) time since last midplane crossing or
  !    start of orbit track
  !
  !  passed output:  iripl=0 if ion orbit continues -- possibly with
  !                      reduced weight
  !                  iripl=1 if ion orbit terminates due to ripple loss
  !
  !  this routine should only be called if namelist control nrip.gt.1
  !
  !  nrip=1:  crude model
  !
  !  use xjadep to determine if ion is in a trapped orbit.  if so, it is
  !  subject to instantaneous loss of a fraction of its weight, leaving
  !  only exp(-ztslmg/taurip) behind unaffected.  lost weight is thrown
  !  completely out of the system.  if the particle weight is too low,
  !  weight is reduced by russian roulette.
  !
  !============
  implicit none
  integer :: iripl,itrap,iuncon,jiav,ierr
  !============
  real(fp) :: ztslmg,zwfmin,zxiav,xjadep,zvpov,zeiav,zphiav,zxhw
  real(fp) :: zwghtp,zwmin,zfrac,zwnew,zfrac2,zz
  !============
  data zwfmin/0.05_fp/
  !
  ierr=0
  !
  iripl=0
  !
  if(ztslmg.le.0.0_fp) return
  !
  if(nrip.eq.1) then
     !
     !  crude ripple loss model in effect -- call xjadep to check for
     !   trapping.  note also ions beyond the last closed surface are
     !   considered "trapped" for ripple loss purposes.
     !
     if(xion.ge.1.0_fp) then
        !
        itrap=1
        !
     else
        !
        zxiav=xjadep(xion,th,rmjion,yion,vion,vpll, &
             itrap,iuncon,zvpov,zeiav,zphiav,jiav,zxhw)
        !
     end if
     !
     if(itrap.eq.1) then
        !
        !  trapped ion ... save wght; compute survival fraction
        !
        zwghtp=wght
        zwmin=zwfmin*wbav_kill(jiav,lsbeam)
        !
        if(ztslmg.gt.50.0_fp*taurip) then
           zfrac=0.0_fp
        else
           zfrac=exp(-ztslmg/taurip)
           zwnew=wght*zfrac
           if(zwnew.lt.zwmin) then
              !  weight too small -- use russian roulette
              zfrac2=zwnew/zwmin
              zz=nbrandm_pll(0)
              if(zz.le.zfrac2) then
                 zfrac=zwmin/zwghtp
              else
                 zfrac=0.0_fp
              end if
           end if
        end if
        !
        !  if zfrac=0.0, ion is lost; otherwise it is saved but continues
        !  orbitting with reduced weight.
        !
        if(zfrac.eq.0.0_fp) then
           iripl=1
        else
           wght=zwghtp*zfrac
           bprpls(lsbeam)=bprpls(lsbeam)+ep*v2toev*(zwghtp-wght)
           bxrpls(lsbeam)=bxrpls(lsbeam)+(zwghtp-wght)
           bphrpls(lsbeam)=bphrpls(lsbeam)+bphion*(zwghtp-wght)
        end if
        !
     end if
     !-----------------------
     !
  else

     write(nonlin,9001) nrip
9001 format(' ?riplos -- unknown model code, nrip=',i10)
     !
     ierr=1
     !
  end if
  !
  return
end subroutine riplos
