module frantic_module

  use iso_c_binding, only: fp => c_double
  implicit none
  SAVE

  ! all stuff used by FRANTIC that used to be in TRCOM is redefined
  ! here, in allocatable real(fp) form.

  ! modifications:  NSRC, constant over a FRANTIC call, was removed;
  !   arrays previously indexed by NSRC had dimensionality reduced by 1.
  ! also: array RMJRMP declared as singly dimensioned, dropping the second
  !   dimension that was always referenced as RMJRMP(...,2).

  ! dmc Oct 2002 -- comments generally copied from port.for (TRANSP).

  ! reviewed dmc Dec 2008 -- added comments; added vphi_wall
  ! Units:  the code is mostly cgs internally but the interface routines
  !         use MKS.  Energies & Temperatures in KeV.

  ! array dimensions:

  integer :: mj = 0    ! number of radial zones + 2
  integer :: mig = 0   ! number of plasma thermal ion species
                       ! not electrons nor impurities

  integer :: mii       ! mig+1
  integer :: mic       ! mig+2
  integer :: mix       ! mig+[no. of impurity species cf NIMP]

  integer :: mibs      ! number of fast ion species
  integer :: mitrc     ! dimension for tracking arrays: 5*(mj-2)

  ! working integer scalars:

  integer :: LUN6 = 6  ! output channel for messages

  integer :: frantic_atomic=1 !
  ! default fratnic_atomic=1 -- legacy of PREACT atomic physics
  ! 2 -- ADAS atomic physics for H and He beams.
  !      He beam -- only ground state model available
  ! 3 -- PREACT ground state atomic physics
  !      ADAS interaction with impurities
  ! 4 -- ADAS ground state atomic physics
  !      PREACT interaction with impurities

  integer :: NZONEC      ! # OF FRANTIC ZONES
  integer :: NBOUND      ! # OF FRANTIC ZONE BOUNDARIES
  integer :: NINLDX      ! INDEX TO LEAST CHARGED ION SPECIES
  integer :: NPLAS       ! CURRENT # OF CHARGED PTCL SPECIES
  integer :: NNEUT       ! NUMBER OF NEUTRAL SPECIES
  integer :: NIMP        ! number of impurity species
  integer :: NNEUT_S0    ! index to initial source neutral specie
  integer :: NNEUT_S0P   ! index to prior initial source neutral specie
  integer :: NNEUT_Z0P   ! Z of prior initial source neutral specie
  integer :: NSBEAM      ! no. of fast ion species.

  integer :: NBCROS      ! NUMBER OF ZONE BOUNDARIES CROSSED
  integer :: MID         ! MIDDLE ZONE ELEMENT OF TRAJECTORY

  ! controls:

  logical :: ESFLAG      ! .TRUE. TO CALCULATE EDGE SOURCE
  logical :: VSFLAG      ! .TRUE. TO CALCULATE VOLUME SOURCE
  logical :: MCFLAG      ! .TRUE. TO CALCULATE TRANSFER MATRIX
  logical :: MSFLAG      ! .TRUE. IF MATRIX HAS BEEN CALCULATED
  logical :: LNFLAG      ! .TRUE. TO INITIALIZE GEOMETRY
  logical :: RCFLAG      ! .TRUE. TO INCLUDE ELECTRON-ION RECOMBINATION
  logical :: LBFLAG      ! .TRUE. IF THERE IS A BEAM SINK OF NEUTRALS

  logical :: LZPFLAG     ! .TRUE. if the impurity Z & A are profiles

  ! scalars:

  real(fp) :: EPSLON     ! a small number
  real(fp) :: AMINOR     ! minor radius, cm

  ! these appear to be superseded, see balance checks in frantic_prinsum.f90...
  real(fp) ::  PLOSS     ! PTCL LOSS A
  real(fp) ::  PLOSS1    ! PTCL LOSS B-- COMPARE WITH PLOSS FOR BAL.
  real(fp) ::  ELOSS     ! ENERGY LOSS A
  real(fp) ::  ELOSS1    ! ENERGY LOSS B- COMPARE WITH ELOSS1, E. BAL.

  ! species information:

  real(fp), dimension(:), allocatable :: APLAS  ! AMU MASSES OF CHARGED SPECIES
  real(fp), dimension(:), allocatable :: XZPLAS ! Species charges (+1 for H+)
  integer,  dimension(:), allocatable :: NSPEC ! neutrals->charged species map
  real(fp), dimension(:), allocatable :: WWALL  ! Species outflux #/cm2/sec
  real(fp), dimension(:), allocatable :: TWALL  ! Edge source temperatures KeV
  real(fp), dimension(:), allocatable :: vphi_wall  ! Edge source v_phi cm/sec
  ! if vphi_wall is never specified by input, assume zero

  real(fp), dimension(:), allocatable :: EESCAV  ! AVG ENERGY/PTCL IN ESCAPE FLUXES, by species, KeV
  real(fp), dimension(:), allocatable :: OUTFLX  ! PTCL ESCAPE FLUX by species

  integer,  dimension(:), allocatable :: LISPEC  ! ion to neutral cross index

  real(fp), dimension(:), allocatable :: FLUXI0  ! wall neutral source #/cm2/sec
  real(fp), dimension(:), allocatable :: RMASS   ! species nucleus mass, g
  real(fp), dimension(:), allocatable :: XZBEAMS ! Z of fast species (1 or 2)

  ! grid:

  real(fp), dimension(:), allocatable :: RBOUND ! boundary radii, cm
  real(fp), dimension(:), allocatable :: RMJRMP ! center of flux surfaces, cm
  real(fp), dimension(:), allocatable :: VOL    ! FRANTIC volume of zones, cm**3
  real(fp), dimension(:), allocatable :: tvol   ! enclosed toroidal volume, cm**3
  real(fp), dimension(:), allocatable :: x, xb  ! radial flux coord label [0:1]

  ! work arrays:

  real(fp), dimension(:,:,:,:), allocatable :: ALPHAX ! CH-EX ABSORBTION COEFFICIENT (1/CM) ARRAY
  real(fp), dimension(:,:,:),   allocatable :: ALPHAT ! CH-EX+IONIZATION COEFFICIENT ARRAY
  real(fp), dimension(:,:,:),   allocatable :: BETAX  ! CH-EX COEFFICIENT FOR WALL-BORN NEUTRALS
  real(fp), dimension(:,:),     allocatable :: BETAT  ! CH-EX+IONIZATION COEFF. FOR 1ST GEN. NEUTS.
  real(fp), dimension(:,:,:,:), allocatable :: PRFLUX ! RADIAL IN/OUTFLUX PROBABLITIES ARRAY
  real(fp), dimension(:,:,:),   allocatable :: PRWFLX ! RADIAL IN/OUTFLUX FROM WALL PROBABILITIES
  real(fp), dimension(:,:),     allocatable :: TSTOR1 ! WORKSPACE

  real(fp), dimension(:,:,:,:), allocatable :: GFUN   ! TRANSFER MATRIX
  real(fp), dimension(:),       allocatable :: DIST   ! TRAJECTORY PATH LENGTHS ARRAY
  integer,  dimension(:),       allocatable :: NZCROS ! INDEX OF ZONES CROSSED BY TRAJECTORY
  real(fp), dimension(:),       allocatable :: YLOC   ! TRAJECTORY LOCATIONS ARRAY
  real(fp), dimension(:,:),     allocatable :: WALL   ! ESCAPE-TO-WALL PROBABILITY
  real(fp), dimension(:,:,:),   allocatable :: S0COLD ! CH-EX SOURCE DRIVEN BY 1ST GEN. NEUTRALS

  ! input profiles:

  real(fp), dimension(:,:),   allocatable :: DEN     ! CHARGED PTCL SPECIES DENSITIES
  real(fp), dimension(:,:),   allocatable :: TEMP    ! CHARGED PTCL SPECIES TEMPERATURES
  real(fp), dimension(:,:),   allocatable :: VPHIZ   ! CHARGED PTCL SPECIES AVG VELOCITIES (PHI)
  real(fp), dimension(:,:),   allocatable :: BII     ! BEAM IMPACT IONIZATION RATE COEFFICIENT
  real(fp), dimension(:,:),   allocatable :: BCX     ! BEAM CH-EX REACTION RATE COEFFICIENT
  real(fp), dimension(:,:,:), allocatable :: BCXS    ! fast ion cx rate coeff., by fast ion species
  real(fp), dimension(:),     allocatable :: SVRC    ! RECOMBINATION COEFFICIENT (1/MFP)
  real(fp), dimension(:,:),   allocatable :: VOLSOR  ! VOLUME SCE OF NEUTRALS

  real(fp), dimension(:), allocatable :: VSOR_ZW   ! Z*[Volume SCE of neutrals]
  real(fp), dimension(:), allocatable :: VSOR_MO   ! momentum VOL SCE OF NEUTRALS
  real(fp), dimension(:), allocatable :: VSOR_EN   ! energy VOL SCE OF NEUTRALS

  real(fp), dimension(:), allocatable :: XZIMPJ    ! impurity Z(r) profile
  real(fp), dimension(:), allocatable :: AIMPJ     ! impurity A(r) profile

  ! output profiles:

  real(fp), dimension(:,:), allocatable :: DCOLD  ! 1ST GEN. NEUTRAL DENSITIES
  real(fp), dimension(:,:), allocatable :: DNEUT  ! NEUTRAL DENSITIES (MULTI-GENERATION)
  real(fp), dimension(:,:), allocatable :: TNEUT  ! NEUTRAL TEMPERATARES (MULTI-GENERATION)
  real(fp), dimension(:,:), allocatable :: VPHI0F ! NEUTRAL SPECIE AVG V'S (CM/SEC ALONG Z, FRANTIC)
  real(fp), dimension(:),   allocatable :: PRC    ! RECOMBINATION POWER (LOSS TO IONS)
  real(fp), dimension(:),   allocatable :: PII    ! IONIZATION POWER (INPUT TO IONS)
  real(fp), dimension(:),   allocatable :: PCX    ! CHARGE EXCHANGE POWER (LOSS TO IONS)
  real(fp), dimension(:,:), allocatable :: SORI   ! NET ION SOURCE FUNCTION
  real(fp), dimension(:,:), allocatable :: SORN   ! MULTI-GENERATION NEUTRAL SOURCE FUNCTION
  real(fp), dimension(:),   allocatable :: SORE   ! NET ELECTRON SOURCE FUNCTION
  real(fp), dimension(:),   allocatable :: PCXSF  ! CX SOURCE FRICTION (INPUT TO IONS)
  real(fp), dimension(:),   allocatable :: PIISF  ! II SOURCE FRICTION (INPUT TO IONS)
  real(fp), dimension(:),   allocatable :: TQCX   ! CX TORQUE (LOSS FROM IONS)
  real(fp), dimension(:),   allocatable :: TQII   ! II TORQUE (INPUT TO IONS)

  ! following have influx and outflux counted separately
  real(fp), dimension(:,:), allocatable :: PFLX0M  ! TOTAL DIVERGENCE OF POWER FLUX IN TH. NEUTRALS
  real(fp), dimension(:,:), allocatable :: SFLX0M  ! TOTAL DIVERGENCE OF PTCL FLUX IN TH. NEUTRALS
  real(fp), dimension(:,:), allocatable :: TQFLX0M ! TOTAL DIVERGENCE OF ANG. MOMENTUM FLUX IN TH.NEUTS

  ! here influx and outflux are combined (backwards compatibility reason)
  real(fp), dimension(:), allocatable :: TQFLX0  ! TOTAL DIVERGENCE OF ANG. MOMENTUM FLUX IN TH.NEUTS

  real(fp), dimension(:), allocatable :: SCX   ! total CX reaction rate

  !-----------------------------------------------
  ! user output grid information

  integer :: nxb_user   ! no. of zone bdys in user grid
  integer :: nx_user    ! no. of zones in user grid, =nxb_user-1

  real(fp), dimension(:), allocatable :: xb_user  ! user grid bdys
  real(fp), dimension(:), allocatable :: x_user   ! user grid zone centers
  real(fp), dimension(:), allocatable :: volxb_user   ! volume inside bdys
  real(fp), dimension(:), allocatable :: Rmajxb_user  ! Rmaj of each bdy

  !  Rmajxb_user(j) gives the midplane center of each bdy flux surface j
  !  volxb_user(j) gives the volume inclused by bdy flux surface j

  !-----------------------------------------------
  ! debug stuff

  logical, dimension(:), allocatable :: npro_set ! profile-set flags

  !-----------------------------------------------

end module frantic_module
