subroutine frantic_lun_set(ilun)

  !  set unit number for messages.  Default is lun6 = 6.

  use frantic_module
  implicit none

  integer, intent(in) :: ilun    ! unit number for messages

  lun6=ilun

  return
end subroutine frantic_lun_set

subroutine frantic_species_za(ing,imp,zzplas,zaplas,insfast,zzbeams)

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

  !  Z's are atomic charge, e.g. 1.0 for H
  !  A's are atomic mass, e.g. 2.0 for D

  integer, intent(in) :: ing     ! no. of non-impurity ion thermal species
  integer, intent(in) :: imp     ! no. of impurity thermal species
  integer, intent(in) :: insfast ! no. of fast species (0 OK)
  real(fp), intent(in) :: zzplas(ing+imp)
                                 ! (1): Zimp, (2:ingp1): Z of therm. species
  real(fp), intent(in) :: zaplas(ing+imp)
                                 ! (1): Aimp, (2:ingp1): A of therm. species
  real(fp), intent(in) :: zzbeams(insfast)

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

  call frantic_species_alloc(ing,imp,insfast)

  if(insfast.gt.0) xzbeams(1:insfast)=zzbeams(1:insfast)
  nsbeam=insfast

  xzplas(1)=-1.0_fp                 ! electrons
  aplas(1)=5.4858026e-4_fp

  xzplas(2)=7.7777777777_fp         ! model impurity -- to be set up later
  aplas(2)=2*xzplas(2)              ! as a profile, if impurity density > 0

  xzplas(3:ing+2)=zzplas(1:ing)     ! non-impurity thermal species
  aplas(3:ing+2)=zaplas(1:ing)

  xzplas(ing+3:ing+imp+2)=zzplas(ing+1:ing+imp)
  aplas(ing+3:ing+imp+2)=zaplas(ing+1:ing+imp)

  nplas=ing+2
  nimp=imp
  
  rmass(1:ing)=aplas(3:ing+2)*1.6726e-24_fp

  epslon=1.0e-34_fp

  lzpflag = .FALSE.                 ! until impurity Z & A profile data given

  return
end subroutine frantic_species_za

subroutine frantic_species_alloc(ing,imp,inbs)

  !  allocate species arrays.  Call this **before** frantic_profile_alloc

  use frantic_module
  implicit none

  integer, intent(in) :: ing    ! no. of non-impurity thermal ion species
  integer, intent(in) :: imp    ! no. of impurity thermal ion species
  integer, intent(in) :: inbs   ! no. of fast ion species

  mig=ing
  mii=mig+1
  mic=mig+2
  mix=ing+imp+2
 
  mibs=max(1,inbs)
 
  allocate(APLAS(mix),XZPLAS(mix)); APLAS=0; XZPLAS=0
  allocate(NSPEC(mig),TWALL(mig),WWALL(mig)); NSPEC=0; TWALL=0; WWALL=0
  allocate(EESCAV(mig),OUTFLX(mig)); EESCAV=0; OUTFLX=0
  allocate(LISPEC(mii)); LISPEC=0
  allocate(FLUXI0(mig),RMASS(mig)); FLUXI0=0; RMASS=0

  allocate(vphi_wall(mig)); vphi_wall=0

  allocate(XZBEAMS(mibs)); XZBEAMS=0
  allocate(npro_set(mix)); npro_set=.FALSE.

  !  set some defaults...

  rcflag = .FALSE.   ! no internally computed recombination

  if(inbs.gt.0) then
    lbflag=.TRUE.
  else
    lbflag=.FALSE.
  end if

  lnflag = .TRUE.

  nneut_s0 = 0       ! no prior neutral source specie...
  nneut_s0p = 0
  nneut_z0p = 0

  msflag = .FALSE.   ! initially...

  return
end subroutine frantic_species_alloc

subroutine frantic_profile_alloc(inzonec)

  !  allocate species arrays.  Call this **after** frantic_species_alloc

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

  integer, intent(in) :: inzonec  ! no. of radial zones

  integer i

  if(mig.eq.0) then
    write(lun6,*) ' ?frantic_profile_alloc: call out of order.'
    write(lun6,*) '  call  frantic_species_alloc(...) first.'
    call bad_exit
  end if

  nzonec=inzonec
  mj = nzonec+2   ! if this changes see also frantic_module_io (read routine)
  mitrc = nzonec*5

  allocate(RBOUND(mj),RMJRMP(mj),VOL(mj)); RBOUND=0; RMJRMP=0; VOL=0
  allocate(TVOL(mj)); TVOL=0

  allocate(ALPHAX(mj,mj,mig,mig),ALPHAT(mj,mj,mig)); ALPHAX=0; ALPHAT=0
  allocate(BETAX(mj,mig,mig),BETAT(mj,mig)); BETAX=0; BETAT=0
  allocate(PRFLUX(mj,mj,mig,2),PRWFLX(mj,mig,2)); PRFLUX=0; PRWFLX=0
  allocate(TSTOR1(mj,mig)); TSTOR1=0
  allocate(GFUN(mj,mj,mig,mig),dist(mitrc),nzcros(mitrc)); GFUN=0; dist=0; nzcros=0
  allocate(YLOC(mj),WALL(mj,mig)); YLOC=0; WALL=0
  allocate(S0COLD(mj,mig,mig)); S0COLD=0

  allocate(DEN(mj,mic),TEMP(mj,mic),VPHIZ(mj,mic)); DEN=0; TEMP=0; VPHIZ=0
  allocate(BII(mj,mig),BCX(mj,mig),BCXS(mj,mig,mibs)); BII=0; BCX=0; BCXS=0
  allocate(SVRC(mj),VOLSOR(mj,mig)); SVRC=0; VOLSOR=0
  allocate(vsor_mo(mj),vsor_en(mj),vsor_zw(mj)); vsor_mo=0; vsor_en=0; vsor_zw=0
  allocate(XZIMPJ(mj),AIMPJ(mj)); XZIMPJ=0; AIMPJ=0

  allocate(DCOLD(mj,mig),DNEUT(mj,mig)); DCOLD=0; DNEUT=0
  allocate(TNEUT(mj,mig),VPHI0F(mj,mig)); TNEUT=0; VPHI0F=0
  allocate(PRC(mj),PII(mj),PCX(mj),SCX(mj)); PRC=0; PII=0; PCX=0; SCX=0
  allocate(SORI(mj,mig),SORN(mj,mig),SORE(mj)); SORI=0; SORN=0; SORE=0
  allocate(PCXSF(mj),PIISF(mj)); PCXSF=0; PIISF=0
  allocate(TQCX(mj),TQII(mj)); TQCX=0; TQII=0
  allocate(PFLX0M(2,mj),SFLX0M(2,mj),TQFLX0(mj)); PFLX0M=0; SFLX0M=0; TQFLX0=0
  allocate(TQFLX0M(2,mj)); TQFLX0M=0

  allocate(x(nzonec),xb(nzonec+1))
  xb(1)=0
  do i=1,nzonec
    xb(i+1)=(1.0_fp*i)/nzonec
    x(i)=(xb(i)+xb(i+1))/2
  enddo

  return
end subroutine frantic_profile_alloc

subroutine frantic_free
  use frantic_module
  implicit none

  ! deallocate all...

  if(allocated(aplas)) call frantic_free_species
  if(allocated(alphax)) call frantic_free_profiles
  call frantic_free_x_user

  return
end subroutine frantic_free

subroutine frantic_free_species
  use frantic_module
  implicit none

  deallocate(APLAS,XZPLAS,NSPEC,TWALL,WWALL,vphi_wall,npro_set)
  deallocate(EESCAV,OUTFLX)
  deallocate(LISPEC,FLUXI0,RMASS,XZBEAMS)
  mig=0

  return
end subroutine frantic_free_species

subroutine frantic_free_profiles
  use frantic_module
  implicit none

  deallocate(ALPHAX,ALPHAT,BETAX,BETAT)
  deallocate(PRFLUX,PRWFLX,TSTOR1)
  deallocate(GFUN,DIST,NZCROS)
  deallocate(RBOUND,RMJRMP,VOL,TVOL)
  deallocate(YLOC,WALL,S0COLD)
  deallocate(DEN,TEMP,VPHIZ)
  deallocate(BII,BCX,BCXS)
  deallocate(SVRC,VOLSOR,XZIMPJ,AIMPJ)
  deallocate(vsor_mo,vsor_en,vsor_zw)

  deallocate(DCOLD,DNEUT,TNEUT)
  deallocate(VPHI0F,PRC,PII,PCX,SCX)
  deallocate(SORI,SORN,SORE)
  deallocate(PCXSF,PIISF)
  deallocate(TQCX,TQII)
  deallocate(PFLX0M,SFLX0M,TQFLX0,TQFLX0M)
  deallocate(x,xb)

  call frantic_free_x_user

  mj=0

  if(allocated(npro_set)) npro_set = .FALSE.

  nneut_s0 = 0       ! now, no prior neutral source specie...
  nneut_s0p = 0
  nneut_z0p = 0

  msflag = .FALSE.   ! initially...

  return
end subroutine frantic_free_profiles

subroutine frantic_free_x_user
  use frantic_module
  implicit none

  if(allocated(x_user)) then
    deallocate(x_user,xb_user,volxb_user,Rmajxb_user)
  end if

  return
end subroutine frantic_free_x_user
