!-----------------------------------------------------------------------
!  beamiz -- promote ionization level of orbitting heavy beam ion
!
subroutine beamiz(isw,ievent,iflri)
  !
  use nbi_com
  !
  !-----------------------------------------------------------------------
  !
  !  this routine is called from subroutine orbit from the same place as,
  !  and functions in a way similar to, beamcx.
  !
  !  when a heavy ion has an electron stripped and has its ionization
  !  level promoted, this happens out at a gyro position.  the guiding
  !  center of the orbit moves to a position between the old guiding
  !  center position and the current flr gyro position.  there is an
  !  associated change in guiding center energy and jxb torque if
  !  toroidal rotation and a non-zero radial electrostatic potential
  !  are present.
  !
  !  if the ion charge changes, the switch isw is set to signal a new
  !  orbit.  the call sequence cxline/nb0jxb/depsub/nb0jxb, similar to
  !  what is done in nutrav, takes us from the old guiding center out
  !  to the gyro back to the new guiding center, while keeping track
  !  of associated momentum/energy changes due to the presence of
  !  plasma rotation and electrostatic field potential.
  !
  !  c% plotting code allows the change in guiding center to be plotted
  !  (analogous to nutrav, but less dramatic, as there is no long neutral
  !  track).
  !
  !  if the stripping rate is rapid enough, the ion's ionization level may
  !  be promoted more than once within the "atomic physics" timestep
  !  modeled in this routine.
  !
  !  this routine is mutually exclusive with beamcx.  the beam code common
  !  variables ttcx and tscx are reused in deciding when stripping events
  !  occur.  in the caller (subroutine orbit) numerical goosing is done
  !  as if the effects modeled here were "charge exchange".  for light
  !  fast ions (isotopes of h or he), beamcx is to be used; for heavy
  !  fast ions (neon,argon,krypton,xenon) this routine is to be used.
  !
  !  for newly deposited heavy ions with only one electron stripped, it
  !  is possible that the gyro travel time is *not* short compared to the
  !  stripping time for one more electron.  in this case, an explicit
  !  integration around the gyro is coded!
  !
  !-----------------------------------------------------------------------
  !
  !============
  implicit none
  integer :: ievent,iflri,isw,int,iz,izp1,igc
  !============
  real(fp) :: ztleft,ztiflr,ztinex,zangph,zfac,zdtiz
  !============
  if(.not.nlheavy(lsbeam)) then
     call nbi_message( &
          'nubeam internal error, beamiz call for light beam ion')
     return
  end if
  !
  nbicx=0
  isw=0
  ievent=0
  !
  !  current ionization level
  !
  int=0
  ztleft=dtgcx
  do
     if(ztleft.le.0.0_fp) then
        if(isw.eq.1) then
           call jbshld
        end if
        return
     endif

  !
     iz=xzbeami+0.5_fp
     izp1=iz+1
     if(iflri.eq.1) then
        !  explicit gyro integration is flagged -- is this necessary?
        if(rlar.le.epslon) then
           iflri=0
        else
           igc=min(ledge,ngc)
           ztiflr=vperp/(twopi*rlar)
           ztinex=hvsigi(igc,izp1,lsbeam)
           !  turn off if tau(gyro) < 1/10 * tau(next-electron-stripped)
           if(0.1_fp*ztiflr.ge.ztinex) iflri=0
        end if
     end if
     !
     if(iflri.eq.1) then
        !
        !  explicit integration around the gyro
        !
        int=int+1
        if(int.eq.1) zangph=atan2(sinpho,cospho)
        !
        zfac=min(4,iz)*0.05_fp
        zdtiz=min(ztleft, zfac*twopi*rlar/vperp )
        ztleft=ztleft-zdtiz
        !
     else
        !
        zdtiz=ztleft
        ztleft = 0.0_fp
        !
     end if
     !
     !----------------------------------
     !
     !  actually compute the electron stripping and move the g.c. if
     !  a stripping event takes place.
     !
     call beamiz0(isw,ievent,iflri,zdtiz,zangph)
     !
     !  ievent=2 signals the orbit gyro struck a limiter
     !
     if(ievent.eq.2) return
     !
     !----------------------------------
     !
  enddo
  !
  if(isw.eq.1) then
     call jbshld
  end if
  return
end subroutine beamiz
