subroutine getsigs_adas310(e_vec,ie_vec,z_beam,a_beam,zte,zti,it_vec,zne,zni,zzi,zza,nspecies,sigv,ing,ier)
  use iso_c_binding, only: fp => c_double
  use logmod
  use mpi_env_mod, only :get_loc_myid,loc_myid,num_procs,mpi_env_comm
  !    use adas310_mod 
  use adas310_mrun310, only : mrun310
  implicit none
  real(fp),intent(in) :: e_vec(ie_vec,nspecies) !interaction energy [ev/amu] beam -> target
  real(fp),intent(in) :: z_beam !beam charge
  real(fp),intent(in) :: a_beam !beam amu
  integer, intent(in) :: ie_vec,it_vec !vector size
  real(fp),intent(in) :: zte(it_vec) !electron temperature [ev]
  real(fp),intent(inout) :: zti(nspecies,it_vec) ! ion temperature in:  [ev]/amu 
  !                 out: [ev]
  real(fp),intent(in) :: zne(it_vec) !electron density [1/cm3]
  real(fp),intent(in) :: zni(nspecies,it_vec) !proton density [1/cm3]
  real(fp),intent(in) :: zzi(nspecies) ! plasma species charge
  real(fp),intent(in) :: zza(nspecies) ! plasma species atomic mass
  integer, intent(in) ::nspecies ! number of all plasma species incl. impurities
  integer,intent(in) :: ing ! number of h isotops ng + 3
  real(fp),intent(out) :: sigv(ie_vec,it_vec,ing)
  integer, intent(out):: ier
  integer, dimension(ie_vec*it_vec) :: map
  integer :: i,myidd,numprocs,mpierr
#ifdef __MPI
  real(fp), allocatable, dimension(:,:,:) :: sigv_wrk
  include 'mpif.h'
#endif
  
  ier=0
  if(nspecies.le.0) then
     ier=1
     call errorlog(' getsigs_adas310: there is no plasma species: nspecies=',nspecies)
     return
  endif
  
  !only diagonal elements will be calculated
  !    nte_vec=nti_vec=nn_vec=it_vec
  if(it_vec.le.0.or.ie_vec.le.0) then
     ier=1
     call errorlog(' getsigs_adas310: # of zones = ',it_vec)
     call errorlog(' getsigs_adas310: # of e points = ',ie_vec)
     return
  endif
  
  !find myid and # of processes
  call get_loc_myid
  myidd=loc_myid
  numprocs=num_procs
  call warnlog('getsigs_adas310: numprocs= ',numprocs)
  !map 
  do i=1,ie_vec*it_vec
     map(i)=mod(i-1,numprocs)
  enddo
  
  !e_vec(:,:) is a beam energy vector[ev/amu]
  call adas310_init(z_beam,a_beam,e_vec(1:ie_vec,1:nspecies),ie_vec,zte,it_vec,&
       & zti(1:nspecies,1:it_vec),it_vec,zne,zni(1:nspecies,1:it_vec),it_vec,&
       ! t_i impurities, [ev]  
       & zzi(1:nspecies), zza(1:nspecies),nspecies, &! all impurities z> = 1 (h-like included)
       & .false.,map,ie_vec*it_vec,ier) !debug=.false.
  
  if(ier.ne.0) then
     call errorlog(' adas310_init in getsigs_adas310: initialization error')
     return
  endif
  
  call mrun310(sigv(1:ie_vec,1:it_vec,1:ing),ing,ier)   !  only diagonal elements will be calculated
  
  call adas310_dalloc
  
#ifdef __MPI
  ! reduce # of splittings
  mpierr=0
  if(allocated(sigv_wrk)) deallocate(sigv_wrk)
  allocate(sigv_wrk(1:ie_vec,1:it_vec,1:ing))
  call mpi_allreduce(sigv,sigv_wrk,ie_vec*it_vec*ing,MPI_REAL8,  &
       MPI_SUM,mpi_env_comm,mpierr)
  if (mpierr.ne.0) then
     call errorlog(' ??getsigs_adas310: mpi_allreduce error - sigv')
     
     call errset_mpi(myidd,mpierr)
     !       call mpi_finalize(mpierr)
     call bad_exit
  endif
  sigv=sigv_wrk
  if(allocated(sigv_wrk)) deallocate(sigv_wrk)
  
#else
#endif
  
  
  return
  
  
end subroutine getsigs_adas310

subroutine getsigs_adas310_fast(e_vec,ie_vec,z_beam,a_beam,zte,zti,zne,zni,zzi,zza,nspecies,sigv,ing,ier)
  use iso_c_binding, only: fp => c_double
  use logmod
  use mpi_env_mod, only :get_loc_myid,loc_myid,num_procs,mpi_env_comm
  !    use adas310_mod 
  use adas310_mrun310, only : mrun310
  implicit none
  real(fp),intent(in) :: e_vec(ie_vec,nspecies) !interaction energy [ev/amu] beam -> target
  real(fp),intent(in) :: z_beam !beam charge
  real(fp),intent(in) :: a_beam !beam amu
  integer, intent(in) :: ie_vec !vector size
  real(fp),intent(in) :: zte(ie_vec) !electron temperature [ev]
  real(fp),intent(inout) :: zti(nspecies,ie_vec) ! ion temperature in:  [ev]/amu 
  !                 out: [ev]
  real(fp),intent(in) :: zne(ie_vec) !electron density [1/cm3]
  real(fp),intent(in) :: zni(nspecies,ie_vec) !proton density [1/cm3]
  real(fp),intent(in) :: zzi(nspecies) ! plasma species charge
  real(fp),intent(in) :: zza(nspecies) ! plasma species atomic mass
  integer, intent(in) ::nspecies ! number of all plasma species incl. impurities
  integer,intent(in) :: ing ! number of h isotops ng + 3
  real(fp),intent(out) :: sigv(ie_vec,ing)
  integer, intent(out):: ier
  integer, dimension(ie_vec) :: map
  integer :: i,myidd,numprocs,mpierr
#ifdef __MPI
  real(fp), allocatable, dimension(:,:) :: sigv_wrk
  include 'mpif.h'
#endif
  
  ier=0
  if(nspecies.le.0) then
     ier=1
     call errorlog(' getsigs_adas310: there is no plasma species: nspecies=',nspecies)
     return
  endif
  
  !only diagonal elements will be calculated
  !    nte_vec=nti_vec=nn_vec=ie_vec
  if(ie_vec.le.0) then
     ier=1
     call errorlog(' getsigs_adas310: # of e points = ',ie_vec)
     return
  endif
  
  !find myid and # of processes
  call get_loc_myid
  myidd=loc_myid
  numprocs=num_procs
  call warnlog('getsigs_adas310_fast: numprocs= ',numprocs)    
  !map 
  do i=1,ie_vec
     map(i)=mod(i-1,numprocs)
  enddo
  
  !e_vec(:,:) is a beam energy vector[ev/amu]
  call adas310_init(z_beam,a_beam,e_vec(1:ie_vec,1:nspecies),ie_vec,zte,ie_vec,&
       & zti(1:nspecies,1:ie_vec),ie_vec,zne,zni(1:nspecies,1:ie_vec),ie_vec,&
       ! t_i impurities, [ev] 
       & zzi(1:nspecies), zza(1:nspecies),nspecies, &! all impurities z> = 1 (h-like included)
       & .false.,map,ie_vec,ier) !debug=.false.
  
  if(ier.ne.0) then
     call errorlog(' adas310_init in getsigs_adas310: initialization error')
     return
  endif
  
  call mrun310(sigv(1:ie_vec,1:ing),ing,ier)   !  only diagonal elements will be calculated
  
  call adas310_dalloc
  
#ifdef __MPI
  ! reduce # of splittings
  mpierr=0
  if(allocated(sigv_wrk)) deallocate(sigv_wrk)
  allocate(sigv_wrk(1:ie_vec,1:ing))
  call mpi_allreduce(sigv,sigv_wrk,ie_vec*ing,MPI_REAL8,  &
       &  MPI_SUM,mpi_env_comm,mpierr)
  if (mpierr.ne.0) then
     call errorlog(' ??getsigs_adas310_fast: mpi_allreduce error - sigv')
     
     call errset_mpi(myidd,mpierr)
     !       call mpi_finalize(mpierr)
     call bad_exit
  endif
  sigv=sigv_wrk
  if(allocated(sigv_wrk)) deallocate(sigv_wrk)
  
#endif    
  return
  
  
end subroutine getsigs_adas310_fast


subroutine getsigs_adas310_all(e_vec,ie_vec,z_beam,a_beam,zte,zti,it_vec,zne,zni,in_vec,zzi,zza,nspecies,sigv,ing,ier)
  use iso_c_binding, only: fp => c_double
  use logmod
  use mpi_env_mod, only :get_loc_myid,loc_myid,num_procs,mpi_env_comm
  !    use adas310_mod 
  use adas310_mrun310, only : mrun310
  implicit none
  real(fp),intent(in) :: e_vec(ie_vec,nspecies) !interaction energy [ev/amu] beam -> target
  real(fp),intent(in) :: z_beam !beam charge
  real(fp),intent(in) :: a_beam !beam amu
  integer, intent(in) :: ie_vec,it_vec,in_vec !vector size
  real(fp),intent(in) :: zte(it_vec) !electron temperature [ev]
  real(fp),intent(inout) :: zti(nspecies,it_vec) ! ion temperature in:  [ev]/amu 
  real(fp),intent(in) :: zne(in_vec) !electron density [1/cm3]
  real(fp),intent(in) :: zni(nspecies,in_vec) !proton density [1/cm3]
  real(fp),intent(in) :: zzi(nspecies) ! plasma species charge
  real(fp),intent(in) :: zza(nspecies) ! plasma species atomic mass
  integer, intent(in) ::nspecies ! number of all plasma species incl. impurities
  integer,intent(in) :: ing ! number of h isotops ng + 3
  real(fp),intent(out) :: sigv(ie_vec,in_vec,it_vec,ing)
  integer, intent(out):: ier
  integer, dimension(ie_vec*it_vec*in_vec) :: map
  integer :: i,myidd,numprocs
  integer :: nte_vec,nti_vec,nn_vec,mpierr
#ifdef __MPI
  real(fp), allocatable, dimension(:,:,:,:) :: sigv_wrk
  include 'mpif.h'
#endif
  
  ! assuming that first elements in zti(nspecies,it_vec),zni(nspecies,it_vec), zzi(nspecies),zza(nspecies)
  ! are hydrogen like
  ier=0
  if(nspecies.le.0) then
     ier=1
     call errorlog(' getsigs_adas310: there is no plasma species: nspecies=',nspecies)
     return
  endif
  
  !only diagonal elements will be calculated
  nte_vec=it_vec
  nti_vec=it_vec
  nn_vec=in_vec
  if(it_vec.le.0.or.ie_vec.le.0.or.in_vec.le.0) then
     ier=1
     call errorlog(' getsigs_adas310: # of zones = ',it_vec)
     call errorlog(' getsigs_adas310: # of e points = ',ie_vec)
     call errorlog(' getsigs_adas310: # of n points = ',in_vec)
     return
  endif
  
  !find myid and # of processes
  call get_loc_myid
  myidd=loc_myid
  numprocs=num_procs
  call warnlog('getsigs_adas310_all: numprocs= ',numprocs)
  !map 
  do i=1,ie_vec*it_vec*in_vec
     map(i)=mod(i-1,numprocs)
  enddo
  
  !e_vec(:,:) is a beam energy vector[ev/amu]
  call adas310_init(z_beam,a_beam,e_vec(1:ie_vec,1:nspecies),ie_vec,zte,nte_vec,&
       & zti(1:nspecies,1:it_vec),nti_vec,zne,zni(1:nspecies,1:nn_vec),nn_vec,&
       ! t_i impurities, [ev] (same for all imp z> = 1)   
       & zzi(1:nspecies), zza(1:nspecies),nspecies, &! all impurities z> = 1 (h-like included)
       & .false.,map,ie_vec*it_vec*in_vec,ier) !debug=.false.
  if(ier.ne.0) then
     call errorlog(' adas310_initin getsigs_adas310_all: initialization error')
     return
  endif
  
  call mrun310(sigv(1:ie_vec,1:in_vec,1:it_vec,1:ing),ing,ier)   !  only diagonal elements will be calculated
  
  call adas310_dalloc
  
#ifdef __MPI
  ! reduce # of splittings
  mpierr=0
  if(allocated(sigv_wrk)) deallocate(sigv_wrk)
  allocate(sigv_wrk(1:ie_vec,1:in_vec,1:it_vec,1:ing))
  call mpi_allreduce(sigv,sigv_wrk,ie_vec*in_vec*it_vec*ing,MPI_REAL8,  &
       &  MPI_SUM,mpi_env_comm,mpierr)
  if (mpierr.ne.0) then
     call errorlog(' ??getsigs_adas310: mpi_allreduce error - sigv')
     
     call errset_mpi(myidd,mpierr)
     !       call mpi_finalize(mpierr)
     call bad_exit       
  endif
  sigv=sigv_wrk
  if(allocated(sigv_wrk)) deallocate(sigv_wrk)
  
#endif
  
  return
  
  
end subroutine getsigs_adas310_all
