program adas_test
!
! June 2009.
! Program to be used to open up and fill in all tables for available ADAS atomic data.
! Before runnig "adas_test" program, all tables have to be initialized. Call "adasinit" first.
!
  use iso_c_binding, only: fp => c_double
  use adas_mod
  implicit NONE
  real(fp) energy(1) ! [KeV/amu]
  real(fp) temp(1) ![KeV/amu]
  real(fp) density_e(1) ! electron density [1/cm^3]
  real(fp) sigv1(1)    ![m^3/sec]
  real(fp) sigv2(1,1)  ![m^3/sec]
  real(fp) sigv3(1,1,1)![m^3/sec]
  integer, parameter  :: jZimp_lbound = 3 , jZimp_ubound = 74 !3 <= Z impurity <=74  and  58,59,60 has some  
                                                              !problems with neutral radiation numerics,
                                                              !so they are forbidden.
  integer, dimension(2) ::  izneut = (/1,2/)  ! only H, and He are supported
  real(fp) zimp !Z imp
  integer i,ii
  integer, dimension(2,10) :: izchrg 
  integer fadas_type,istat
  real(fp) :: conc(1) ! impurity concentration from 0 to 1
  real(fp) zion(1)  !Z ion
  integer, parameter :: NEUTRAL=1, ION=2 ! beam type: =neutral or =ion
!
  !available reaction for H CX,II see adas_ion 
  !available reaction for He CX,II see adas_ion
  ! atomic charge of secondary particle {1, ..., 10}
  izchrg(1,:)=(/1,2,3,4,5,6,7,8,9,10/)
  izchrg(2,:)=(/1,2,0,4,0,6,0,8,0,0/)
  energy=100.00_fp 
  temp=10.00_fp 
  density_e=5.0e+12_fp ![1/cm^3]
  
  write(6,*) ' '
  write(6,*) ' -->adas_test:  atomic reaction tests...............'
  write(6,*) 'IMPURITY STOPPING CROSS_SECTION'
  !-------------------
  ! test: SV  impurity stopping cross-section
  !
  do i=1,size(izneut)
     do ii=jZimp_lbound, jZimp_ubound
        call adas_zstop_sigvz(izneut(i),dble(ii),energy,1,sigv1,istat)
        if(istat.eq.0) write(6,1001) izneut(i),ii,energy,sigv1
     enddo
  enddo
1001 format(/ &
          ' ** impurity stopping sigma*v ** '/ &
          ' Z(neutral atom) = ',i1,' Z(impurity) = ',i2,' E/A = ',f10.2, &
          ' KeV/AMU'/ &
          ' -->real(fp) sigma*v = ',1pe20.13)  

  !-------------------
  ! test: SV  impurity stopping coefficients from ADAS
  !
  write(6,*) ' '
  write(6,*) ' -->adas_test:  atomic reaction tests...............'
  write(6,*) 'IMPURITY STOPPING COEFFICIENT (EXCITED)'
  conc=1._fp !
  do i=1,size(izneut)
     do ii=1,size(izchrg,2)
        zion=dble(ii)
        call adas_bms(energy,1, i, conc,temp,1,&
             &              zion,density_e,1,&
             &              sigv3,istat)
        if(istat.eq.0) write(6,1014) ' ** impurity stopping coefficient (EXCITED) sigma*v (m**3/sec) **', &
             &i,ii,energy,temp,density_e,sigv3
        
     enddo
  enddo
 1014 format(/,A,/ &
          ' Z(neutral beam) = ',i1,'  Z(ion species) = ',i2/ &
          ' E(beam) = ',f10.2,' KeV/AMU'/ &
          ' temperature = ',f10.2,' KeV/AMU'/&
          ' electron density = ',1pe20.13,' 1/cm**3'/ &
          ' -->real(fp) result = ',1pe20.13)
   
  !-------------------
  ! test:  electron impact ionization
  !
  write(6,*) ' '
  write(6,*) ' '
  write(6,*) 'ELECTRON IMPACT IONOZATION'
  do i=1,size(izneut)
     call adas_sigvte_ioniz(izneut(i),temp,1,sigv1,istat)
     write(6,1002) izneut(i),temp,sigv1
  enddo
1002 format(/ &
          ' ** electron impact ionization <sigma*v> ** '/ &
          ' Z(neutral atom) = ',i1,' electron temperature = ',f10.2,' KeV'/ &
          ' -->real(fp) <sigma*v> = ',1pe20.13)
  energy=100.00_fp ! KeV/amu (beam energy/nucleon)
  temp=10.00_fp    ! KeV/amu (target temperature/nucleon)
  !-------------------
  ! test:  CHARGE EXCHANGE(neutralizing) sigma*v 
  !
  write(6,*)' '
  write(6,*)' '
  write(6,*)'CHARGE EXCHANGE(neutralizing) sigma*v  COEFFICIENT'
  fadas_type=1 !'CX'
  do i=1,size(izneut)
     do ii=1,size(izneut)
        call adas_sigv(fadas_type,energy,1,i,dble(ii),sigv1,istat)
        if(istat.eq.0) write(6,1013) ' ** charge exchange sigma*v (m**3/sec) **', &
             &i,ii,energy,sigv1
     enddo
  enddo
 1013 format(/,A,/ &
          ' Z(neutral beam) = ',i1,'  Z(ion species) = ',i2/ &
          ' E(beam) = ',f10.2,' KeV/AMU'/ &
          ' -->real(fp) result = ',1pe20.13)
   
  !-------------------
  ! test:  impact ionization sigma*v
  !
  write(6,*)' '
  write(6,*)' '
  write(6,*)' IMPACT IONIZATION sigma*v  COEFFICIENT'
  fadas_type=2 !'II'
  do i=1,size(izneut)
     do ii=1,size(izchrg,2)
        call adas_sigv(fadas_type,energy,1,i,dble(ii),sigv1,istat)
        if(istat.eq.0) write(6,1013) ' ** impact ionization  sigma*v (m**3/sec) **', &
             &i,ii,energy,sigv1
     enddo
  enddo

  !-------------------
  ! test:  charge exchange rate coefficient
  write(6,*)' '
  write(6,*)' '
  write(6,*)'CHARGE EXCHANGE RATE COEFFICIENT NEUTRAL_BEAM'
  fadas_type=1 !'CX'
  do i=1,size(izneut)
     do ii=1,size(izneut)
        call adas_btsigv(fadas_type,NEUTRAL,energy,temp,1,i,ii,sigv1,istat)
        if(istat.eq.0) write(6,1003) ' ** charge exchange <sigma*v> ** ', &
             &i,izchrg(i,ii),energy,temp,sigv1
     enddo
  enddo
  !-------------------
  ! test:  CHARGE EXCHANGE  <sigma*v>
  !
  write(6,*)' '
  write(6,*)' '
  write(6,*)'CHARGE EXCHANGE RATE COEFFICIENT ION_BEAM'
  fadas_type=1 !'CX'
  do i=1,size(izneut)
     do ii=1,size(izneut)
        call adas_btsigv(fadas_type,ION,energy,temp,1,i,ii,sigv1,istat)
        if(istat.eq.0) write(6,1004) ' ** charge exchange <sigma*v> ** ', &
             &i,izchrg(i,ii),energy,temp,sigv1
     enddo
  enddo
  !-------------------
  ! test:  impact ionization <sigma*v>
  !
  write(6,*)' '
  write(6,*)' '
  write(6,*)'IMPACT IONIZATION NEUTRAL_BEAM '
  fadas_type=2 !'II'
  do i=1,size(izneut)
     do ii=1,size(izchrg,2)
        if(izchrg(i,ii).eq.0) cycle
        call adas_btsigv(fadas_type,NEUTRAL,energy,temp,1,i,ii,sigv1,istat)
        if(istat.eq.0) write(6,1003) ' ** ion impact ionization <sigma*v> ** ', &
             &i,izchrg(i,ii),energy,temp,sigv1
     enddo
  enddo
 1003 format(/,A,/ &
          ' Z(neutral beam) = ',i1,'  Z(target plasma) = ',i2/ &
          ' E(beam) = ',f10.2,' KeV/AMU;  T(target) = ',f10.2,' KeV/AMU'/ &
          ' -->real(fp) result = ',1pe20.13)
  write(6,*)' '
  write(6,*)' '
  write(6,*)'IMPACT IONIZATION ION_BEAM '
  fadas_type=2 !'II'
  do i=1,size(izneut)
     do ii=1,size(izchrg,2)
        if(izchrg(i,ii).eq.0) cycle
        call adas_btsigv(fadas_type,ION,energy,temp,1,i,ii,sigv1,istat)
        if(istat.eq.0) write(6,1004) ' ** ion impact ionization <sigma*v> ** ', &
             &i,izchrg(i,ii),energy,temp,sigv1
     enddo
  enddo
 1004 format(/,A,/ &
          ' Z(ion beam) = ',i1,'  Z(target plasma) = ',i2/ &
          ' E(beam) = ',f10.2,' KeV/AMU;  T(target) = ',f10.2,' KeV/AMU'/ &
          ' -->real(fp) result = ',1pe20.13)
  stop
end program adas_test
