!-----------------------------------------------------------------------
!  rplsrd -- ripple loss model for monte carlo fast ions -- srd calculation
!
!
!   mhr july 1994
!
!     jfs  25 apr 95: inverted zdsbpt to avoid problem when rlar=0.
!
!
subroutine rplsrd
  !
  use nbi_com
  !
  !
  !  passed input:  none
  !
  !  passed output:  srd - gwb criterion
  !
  !  this routine should only be called if namelist control nrip=2
  !
  !  nrip=2:  srd model
  !
  !  calculate srd
  !
  !  calculate delta_s
  !
  !============
  implicit none
  integer :: ier
  !============
  real(fp) :: zdi,zgradxi,zqprime,zeps,zdsbpti,zdbpt
  !============
  zdi=1.0_fp/(xbjaco(1,1)*xbjaco(2,2)-xbjaco(1,2)*xbjaco(2,1))
  zgradxi=sqrt(xbjaco(1,2)**2+xbjaco(2,2)**2)*zdi
  zqprime=max(epslon, abs(bidqdx(1)*zgradxi) )
  zeps=aratii(ngc,1)+xingc*(aratii(ngc+1,1)-aratii(ngc,1))
  zdsbpti=(rlar*zqprime)/((zeps/(ncoils*xpi*biq(1)))**1.5_fp)
  !
  !  interpolate toroidal ripple field
  !
  call eq_frz(1,1.0e-2_fp*rmjion,1.0e-2_fp*yion,1,id_ripple, &
       1,zdbpt,ier)
  if(ier.ne.0) &
       call nbi_message('?rplsrd:  error getting ripple data!')
  !
  zdbpt=exp(zdbpt)

  !ccccccc      srd = zdbpt*zdsbpti

  if ( nlfprod(lsbeam) ) then
     if(asrd.eq.0._fp) srd = 2.0_fp*zdbpt*zdsbpti
     if(asrd.ne.0._fp) srd=asrd*zdbpt*zdsbpti
  else
     if(bsrd.eq.0._fp) srd = 0.5_fp*zdbpt*zdsbpti
     if(bsrd.ne.0._fp) srd=bsrd*zdbpt*zdsbpti
  end if
  !
  return
end subroutine rplsrd
