!-----------------------------------------------------------------------
!  riplos2 -- ripple loss model for monte carlo fast ions
!
!   mhr july 1994
!
!
subroutine riplos2(zksid,iripl,ierr)
  !
  use nbi_com
  !
  !
  !  passed input:  zksid=vpll/v - check for change sign; toroidally trapped
  !
  !  passed output:  iripl=0 if ion orbit continues -- possibly with
  !                      reduced weight
  !                  iripl=1 if ion orbit terminates due to ripple loss
  !                  srd=stochastic ripple threshold criterion
  !
  !  this routine should only be called if namelist control nrip.eq.2
  !
  !  nrip=2:  srd model of goldston, white, boozer
  !
  !
  !============
  implicit none
  integer :: iripl,ierr
  !============
  real(fp) :: zksid
  !============
  ierr=0
  !
  if(nrip.eq.2) then
     !
     !  dmc -- i have rewritten this section, it was not correct.
     !  the problem was that once an orbit bounced, ntrap=1 was set
     !  and stayed set.  then, if anywhere on the future orbit trajectory
     !  (not just a bounce point) the orbit passed through an srd.gt.0
     !  region, then, the orbit was declared lost.  (note feb 2007 -- ntrap
     !  was an nbi_com variable; it no longer exists).
     !
     !  actually, rplsrd should not be called unless at a bouncepoint
     !  or an accel = 0 point when we do the goosing upgrade.
     !
     iripl=0
     if(zksid*xksid.le.0.0_fp) then
        ! vpll changed sign
        call rplsrd
        if(srd.ge.1.0_fp) then
           iripl=1
           call rpltq
        end if
     end if
     !
     !-----------------------
     !
  else

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