module xjasub_mod
  use iso_c_binding, only: fp => c_double
  save

! dmc apr 2004 -- all xja routines use bmnmx instead of bmidp -- code
!  now in terms of min and max b on flux surfaces, not midplane values
!----------------------------------------------------------------------
!@      cliche xjasubs
!
!*   zmass  ion mass
!*   zekin  kinetic energy at start point
!*   zepot  potential energy at start point
!*   zmub   conserved quantity:  mu=vperp**2/b
!*   zpsito conserved quantity:  canonical momentum
!
!*  moved to xjasubs common
!
!    zevtv2 converts ev to v**2
!
! (dmc) converted from common to module, 7 mar 2002
!
  real(fp) :: zmass,zekin,zepot,zmub,zpsito,zevtv2
  integer :: init0,icen,inzp1,i2nzp1,i2nzpx,iloc(4)
  real(fp) :: zxidpi(4),zvplli(4),zei(4),zbi(4),zri(4),zvroti(4)
  real(fp) :: zfext1,zfext2

  logical :: xjasubs_init = .false.
  real(fp), dimension(:), allocatable :: zfa,zxia,zvplla,zea,zba,zvrota

  logical :: ilconfine  ! .t if confined
  logical :: iltrap     ! .t if trapped
  logical :: ilinside   ! .t if inside-trapped
!
  integer :: impin      ! inner (small r) orbit midplane intercept
  integer :: impout     ! outer (large r) orbit midplane intercept
!
!    icen=inzp1= nzones+1
!    i2nzp1    = 2*nzones+1  for midplane indexing
!    i2nzpx = i2nzp1 + something, for midplane indexing extended on
!                      the large major radius side
!
!    iloc(j)= midplane index to orbit midplane intercept j
!
!  j=1:  outer co intercept
!  j=2:  outer ctr intercept
!  j=3:  inner ctr intercept
!  j=4:  inner co intercept
!
!     if(istat(j).ge.1)
!       zxidpi(j) gives precise "xi" (r/a) location
!       zvplli(j) gives estimated vpll at the location
!       zei(j) gives ptcl energy at the intercept
!       zbi(j) gives field strength at the intercept
!	zri(j) gives the intercept major radius
!       zvroti(j) gives the plasma rotation velocity
!
!   zfa  f at all flux zone bdy - midplane intercepts
!        f=mu*b-e/2m + vpll**2 --- zeroes of f are orbit intercepts
!          where vpll=(pphi-e/c*psi)/mr estimated from orbit pphi
!          conservation principle
!
!   zxia  xi at zone bdy intercepts
!   zvplla  vpll at zone bdy intercepts-- evaluated by pphi conservation
!   zea   orbit kinetic energy at zone bdy intercepts -- including
!         effect of electrostatic potential change
!   zba   magnetic field strength at midplane zone bdy intercept
!   zvrota  plasma rotation velocity at intercepts
!
!@      endcliche
!----------------------------------------------------------------------
  contains
    subroutine xja_init

      use nbi_dimensions
      implicit none

      xjasubs_init=.true.
      allocate(zfa(mjr),zxia(mjr),zvplla(mjr),zea(mjr),zba(mjr),zvrota(mjr))
      zfa=0.0_fp; zxia=0.0_fp; zvplla=0.0_fp; 
      zea=0.0_fp; zba=0.0_fp; zvrota=0.0_fp
      zxidpi=0.0_fp; zvplli=0.0_fp; zei=0.0_fp; 
      zbi=0.0_fp; zri=0.0_fp; zvroti=0.0_fp
      zmass=0.0_fp; zekin=0.0_fp; zepot=0.0_fp; 
      zmub=0.0_fp; zpsito=0.0_fp; zevtv2=0.0_fp
      zfext1=0.0_fp; zfext2=0.0_fp
      init0=0; icen=0; inzp1=0; i2nzpx=0; iloc=0
      impin=0; impout=0
      ilconfine=.false.; iltrap=.false.; ilinside=.false.

    end subroutine xja_init

end module xjasub_mod
