!
! ======================= ADPAK_MOD =======================
!
! This module replaces the common blocks in the adpak routines
!
module adpak_mod
  use iso_c_binding, only: fp => c_double
  implicit NONE
  save    ! save between subroutine calls

  !
  ! --- switches for controling the adpak package ---
  ! used in ADSET(), adp_reset() subroutines
  !
  integer :: LADEN  = 1    ! switch for energy level calculation
                           ! 0 -> use H. MAYER SHIELDING CONSTANTS
                           ! 1 -> use R. MORE REVISED SHIELDING CONSTANTS
  integer :: LADTIP = 1    ! switch to use tabulated ionization potentials (0 off, 1 on)
  integer :: LECI   = 2    ! switch for selecting formula to use for electron collisional ionization rate
                           ! 1 -> USE XSNQ DERIVED IONIZATION RATE CALCULATION
                           ! 2 -> USE BELFAST GROUP LIGHT ELEMENT RATE FITS
                           ! 3 -> USE YOUNGER'S RATES FOR AVAILABLE ELEMENTS AND SHELLS
                           !      XSNQ RATES OTHERWISE ON A CHARGE STATE-BY-CHARGE STATE BASIS
  integer :: LDRMLT = 1    ! setup DIELECTRONIC RECOMBINATION RATE COEFFICIENT ARRAYS CDNN,CDNM
                           ! 0 -> USE CDNN, CDNM ARRAYS AS GIVEN
                           ! 1 -> CDNN, CDNM ARRAYS SET TO 1.0
                           ! 2 -> USE Y. HAHN FACTORS TO SETUP CDNN, CDNM
  integer :: LADPOUT = 6   ! logical unit number for error messages

  !
  ! --- results of switch settings ---
  ! set by ADSET(), adp_reset() subroutines
  !
  integer :: LTIPOK        ! set to 1 if found and used tabulated ionization potential
  integer :: LECIOK        ! set to LECI actually used
  
  !
  ! --- DIELECTRONIC RECOMBINATION RATE COEFFICIENT ARRAYS ---
  ! see LDRMLT -- used in ADSET(), adp_reset() subroutines
  !
  real(fp)    :: CDNN(100)     ! input for LDRMLT=0
  real(fp)    :: CDNM(100)     ! input for LDRMLT=0
  real(fp)    :: YHNNA         ! input for LDRMLT=2, Y. HAHN FACTORS
  real(fp)    :: YHNNB
  real(fp)    :: YHNNC
  real(fp)    :: YHNMA
  real(fp)    :: YHNMB
  real(fp)    :: YHNMC
  
  !
  ! --- Internal ---
  ! data stored in the usual way, fortran index I = IONIZATION_STATE+1
  !
  integer :: NUCZ  = 0     ! atomic number of element = Z (0->uninitialized module)
  integer :: NSPC          ! number of species = Z+1
  real(fp)    :: APN(100,10)   ! NUMBER OF ELECTRONS IN SHELL JN FOR SPECIES WITH CHARGE JQ-1
  real(fp)    :: AQN(100,10)   ! (2.*JN**2 - APN(JQ,JN))/(2.*JN**2) IS THE FRACTIONAL VACANCY
  integer :: NVALNC(100)   ! PRINCIPAL QUANTUM NUMBER OF THE HIGHEST OCCUPIED ('VALENCE') SHELL
  real(fp)    :: SIGMA(10,10)  ! R. MORE SHIELDING CONSTANTS
  real(fp)    :: QN(100,10)    ! EFFECTIVE NUCLEAR CHARGE AT SHELL JN
  real(fp)    :: EN(100,10)    ! ENERGY LEVELS FOR RECOMBINATION OF THE GROUND STATE ION
  real(fp)    :: EIN(100,5)    ! IONIZATION POTENTIAL FOR ELECTRON IN SHELL JN
  real(fp)    :: ENM(100,5,10) ! EXCITATION ENERGY FROM SHELL JN UP TO SHELL JM
  real(fp)    :: FNM(100,5,10) ! ABSORPTION OSCILLATOR STRENGTHS 
  real(fp)    :: FNN(100)      ! ABSORPTION OSCILLATOR STRENGTHS FOR N-N TRANSITIONS
  real(fp)    :: ENN(100)      ! EQUIVALENT ENERGY FOR N-N (DELTA N EQUAL ZERO) TRANSITIONS
  
  !
  ! --- electron rates ---
  ! set by ADERC(), adp_ioniz(), adp_recom(), adp_rad() ... 
  ! see ADERC() for adpak routines which directly set these rates
  !
  real(fp)    :: RCLION(100)   ! COLLISIONAL IONIZATION RATES (SEC-1)
  real(fp)    :: RRAREC(100)   ! RADIATIVE RECOMBINATION RATES (SEC-1)
  real(fp)    :: RDIREC(100)   ! DIELECTRONIC RECOMBINATION RATES (SEC-1)
  real(fp)    :: CIZLOS(100)   ! IONIZATION ENERGY LOSS RATE
  real(fp)    :: RADRRC(100)   ! RADIATION DUE TO RADIATIVE RRECOMBINATION (JOUES/SEC)
  real(fp)    :: RADDRC(100)   ! RADIATION DUE TO DIELECTRONIC RECOMBINATION (JOULES/SEC)
  real(fp)    :: RADCLX(100)   ! RADIATION RATES DUE TO COLLISIONAL EXCITATION (JOULES/SEC)
  real(fp)    :: RADBRM(100)   ! BREMSSTRAHLUNG RADIATION (JOULE/SEC)
  
  !
  ! --- neutral rates ---
  ! set by ADBCXR()
  !
  real(fp)    :: RCXREC(100)   ! CHARGE EXCHANGE RECOMBINATION RATES (SEC-1)
  real(fp)    :: RADBCX(100)   ! RADIATION DUE TO CASCADE AFTER CHARGE EXCHANGE (JOULES/SEC)
   
  !
  ! --- coronal equilibrium ---
  ! set by ADCE(),ADCE2() call 
  !
  real(fp)    :: CEFRAC(100)   ! CORONAL EQUILIBRIUM FRACTION IN EACH IONIZATION STATE
  real(fp)    :: CEAVGZ        ! AVERAGE CHARGE IN CORONAL EQUILIBRIUM
  real(fp)    :: CERAD         ! RADIATION RATE IN CORONAL EQUILIBRIUM
end module adpak_mod
