! subroutine used to calculate trajectories for SAL
! and generate object for Pij or Characteristic calculation
subroutine SALT(nentry,hentry,ientry,jentry,kentry)

  use precision_and_kinds, only : epsilon_pdb,small
  use files_1 , only : f_geo , ftrack2 ,ftrack3 , fname
  use geo_types_list
  use geometry_types
  use tracking_types
  use quadrature_types
  use objects_1,  only : gg,tt,qq
  use geometry_1, only :typgeo,nbfold,anggeo,lengthx,lengthy
  use sal_geometrie_mod, only : sal100
  use sal_tracking_mod, only : sal200
  use tracking_1, only : nphi,npsix,mphi,mpsi,ndiv,ngauss, &
         nmax2,option,nsn,delr,delmin, nbcurr,nd_turn, &
         prtind,index,lprt,flash,verbos,meth_teta,meth_phi,type_14,type_15, &
         lgnotcycl,nangle
  use tracking_2, only : record, n_axis
  use salutil, only : give_unit
  use library_1, only : nbmed
  use my_lcmput_mod
  use my_lcmget_mod

  implicit none

! input/output parameters:
!  nentry : number of linked lists or files used by the module.
!  hentry : character*12 name of each linked list or file.
!  ientry : =1 memory-resident LCM object; =2 xsm file; =3 sequential binary file;
!           =4 sequential ascii file.
!  jentry : =0 the LCM object or file is created.
!           =1 the LCM object or file is open for modifications;
!           =2 the LCM object or file is open in read-only mode.
!  kentry : =file unit number; =LCM object address otherwise.
!           dimension hentry(nentry),ientry(nentry),jentry(nentry),
!           kentry(nentry)

  integer      nentry,ientry(nentry),jentry(nentry)
  type(c_ptr)  kentry(nentry)
  character    hentry(nentry)*12

  ! local variable
  integer :: ityp,nitma,ok,i,ierr,j,k,i_cont,itot,igig,max_reg,nb_unk,upn
  type(c_ptr) :: kentry_4,kentry_5
  integer :: ilong,itylcm,iqua10,nanis,ibihet
  type(c_ptr) :: ksal,kfse
  integer :: ifse,jfse
  real    :: flott
  character(len=4 ) :: text4
  character(len=80 ) :: text80
  character(len=12 ) :: local_track
  character(len=12) :: name_geom
  real(8) :: dflott
  real    :: x
  character(len=12) :: text12
  integer, dimension(40) :: i_state
  integer, dimension(:) , allocatable :: itab ! local array
  real, dimension(:), allocatable :: volume ! local volume single precision
  integer, dimension(:), allocatable :: gig  ! tableau des profondeurs de gigogne pour chaque region
  integer :: icreate ! =0: l'objet tracking existe deja; =1: il est clone; =2: il est cree
  logical :: lginf,lbihet
  integer, external :: KDROPN,KDRCLS

  if (nentry < 3) call xabort('SALT : A BAD NUMBER OF OBJETS IN ARGUMENT WAS GIVEN')

  ! allocate a sequential binary file to hold SAL tracks
  ftrack3=KDROPN('trackFIL_sal',0,2,0)

  kfse=kentry(3)
  ifse=ientry(3)
  jfse=jentry(3)
  ! SAL surfacic elements ascii file 
  ! check file is on read mode only and ascii file
  if (( ifse /= 4).or.(jfse/= 2)) call xabort('SALT : A READ ONLY ASCII FILE IS EXPECTED ')
  f_geo = FILUNIT(kfse)
  rewind(f_geo)

  icreate=2
  kentry_5=c_null_ptr
  do k=4,nentry
    if ((ientry(k) > 2).or.(jentry(k) /= 2)) then
       call xabort('SALT : A READ-ONLY TRACK OR GEOMETRY LCM OBJECT IS EXPECTED ')
    endif
    call my_lcmget(kentry(4),'SIGNATURE',text12)
    if (text12 == 'L_TRACK') then
       if (icreate == 0) call xabort('SALT : NOT EXPECTING TWO RHS TRACK LCM OBJECTS ')
       icreate=1
       kentry_4=kentry(k)
    else if (text12 == 'L_GEOM') then
       kentry_5=kentry(k)
    endif
  enddo

  ! create or recover the SAL tracking LCM object
  if (icreate == 1) then
    ksal=kentry_4
  else if(icreate == 2) then
    call LCMOP(ksal,'trackLCM_sal',0,1,0)
    kentry_4=ksal
  endif

  ! value of epsilon for tracking
  epsilon_pdb=sqrt(epsilon(x))
  
  ! text recuperation for tracking option
  ! *************************************
  call redget(ityp,nitma,flott,text4,dflott)

  prtind = 0 ; index = 0 ; lprt =0 ; flash =0  ! for printing tracks
  nphi =0 ; npsix =0  ! for product quadrature
  nanis =1 ! anisotropy level for EXCELT:-like tracking
  option = 0 ; nsn =0  ! quadrature type and SN order
  verbos = 0  ! for printing quadrature
  ndiv =0  ! for division of the discretization step
  nmax2 = 1 ! size of the buffer for printing the tracking
  ngauss = -2 ! two points of gauss integration (normal one)
  meth_phi = 0 ; meth_teta =0  ! for circular quadrature
  mphi =1 ; mpsi = 1  ! number of cones (horizontal and vertical)
  delr = 0. ! constant step value for horizontal quadratures
  delmin = 0. ! minimum value of DELR
  upn = -1 ! default order of flux expansion on boundaries
  iqua10 = 0 ! quadrature parameter for the double heterogeneity option
  do
     select case (text4)
     case ('EDIT')
        call redget(ityp,prtind,flott,text4,dflott)
        if ((ityp /= 1).or.(prtind <0))  call xabort('SALT : A POSITIVE INTEGER WAS EXPECTED  ' &
             //'FOR PRINTING INDICATION : prtind')
        ! same number for all printing indications
        index = max(0,prtind-5) ! print tracking summary
        lprt = max(0,prtind-5)  ! print first lprt traj per angle
        flash =  max(0,prtind-5)  ! print summary for all tracks
     case ('AN2D')
        call redget(ityp,nphi,flott,text4,dflott)
        if ((ityp /= 1).or.(nphi<=0)) call xabort('SALT : A POSITIVE INTEGER WAS EXPECTED ' &
             //'FOR THE NUMBER OF ANGLES  in [0,pi] (HORIZONTAL 2D): NPHI')
     case ('AN3D')
        call redget(ityp,npsix,flott,text4,dflott)
        if ((ityp /= 1).or.(npsix<=0))  call xabort('SALT : A POSITIVE INTEGER WAS EXPECTED' &
             // 'FOR THE NUMBER OF ANGLES in [0,pi] (VERTICAL 3D) : NPSI')
     case ('QUAD')
        call redget(ityp,option,flott,text4,dflott)
        if ((ityp /= 1).or.(option<=0).or.(option>10))  call xabort('SALT : AN INTEGER in 1,10 ' &
             // 'WAS EXPECTED FOR THE OPTION QUADRATURE : OPTION')
     case ('NSN')
        call redget(ityp,nsn,flott,text4,dflott)
        if ((ityp /= 1).or.(2*(nsn/2)/=nsn )) call xabort('SALT : A POSITIVE EVEN INTEGER WAS EXPECTED ' &
             // ' FOR THE ORDER QUADRATURE : NSN')
     case('PSN')
        call redget(ityp,verbos,flott,text4,dflott)
        if ((ityp /= 1).or.(verbos<0)) call xabort('SALT : A POSITIVE INTEGER WAS EXPECTED ' &
             // ' FOR THE PRINTING QUADRATURE ORDER : VERBOS') 
     case('NDIV')
        call redget(ityp,ndiv,flott,text4,dflott)
        if ((ityp /= 1).or.(ndiv<0)) call xabort('SALT : A POSITIVE INTEGER WAS EXPECTED ' &
             // ' FOR THE NUMBER OF DIVISIONS : NDIV')
     case('NGAU')
        call redget(ityp,ngauss,flott,text4,dflott)
        if (ityp /= 1) call xabort('SALT : AN  INTEGER WAS EXPECTED ' &
             // ' FOR THE GAUSSIAN QUADRATURE USED IN SPACE : NGAUSS') 
     case('NMAX')
        call redget(ityp,nmax2,flott,text4,dflott)
        if ((ityp /= 1).or.(nmax2 <1)) call xabort('SALT : A POSITIVE   INTEGER WAS EXPECTED ' &
             // ' FOR THE SIZE BUFFER in 1000 : NMAX2') 
     case('MPHI')
        call  redget(ityp, meth_phi,flott,text4,dflott)
        if ((ityp /= 1).or.(abs(meth_phi)<1).or.(abs(meth_phi)>4)) call xabort('SALT : A  INTEGER EQUAL TO 1, 2, 3, 4 ' &
             // 'WAS EXPECTED FOR THE METHOD USED TO COMPUTE AZIMUTHAL ANGLES : meth_phi') 
     case('MTET')
        call redget(ityp, meth_teta,flott,text4,dflott)
        if ((ityp /= 1).or.(meth_teta<1).or.(meth_teta>3)) call xabort('SALT : A  INTEGER EQUAL TO 1, 2 or 3' &
             // 'WAS EXPECTED FOR THE METHOD USED TO COMPUTE CIRCULAR ANGLES : meth_teta') 
     case('DELR')
        call  redget(ityp, nitma ,delr ,text4,dflott)
        if ((ityp /= 2).or.(delr <= 0))  call xabort('SALT : A POSITIVE  REAL  WAS EXPECTED FOR THE ' &
             // ' DELTA R DISPLACEMENT FOR NUMERICAL QUADRATURE IF NDIV IS NON ZERO')
     case('DELM')
        call  redget(ityp, nitma ,delmin ,text4,dflott)
        if ((ityp /= 2).or.(delmin <= 0))  call xabort('SALT : A POSITIVE  REAL  WAS EXPECTED FOR THE ' &
             // ' MINIMUM VALUE FOR DEL')
     case('DP00')
        upn=0
     case('DP01')
        upn=1
     case('SPEC')
        upn=999
     case('QUAB')
        call redget(ityp, iqua10 ,flott ,text4,dflott)
        if (ityp /= 1) call xabort('SALT : AN  INTEGER WAS EXPECTED ' &
             // ' FOR THE QUADRATURE PARAMETER USED IN BIHET') 
     case ('ANIS')
        call redget(ityp,nanis,flott,text4,dflott)
        if ((ityp /= 1).or.(nanis <0))  call xabort('SALT : A POSITIVE INTEGER WAS EXPECTED FOR ANISOTROPY LEVEL')
     case (';')
        exit
     case default
        text80 = 'SALT : MAUVAISE OPTION ' // text4
        call xabort(text80)
     end select
     call redget(ityp,nitma,flott,text4,dflott)
  end do

  ! check consistency of data
  if (delr < small) call  xabort('SALT : DELR NOT DEFINED OR TO SMALL')
  if (delmin < small) call  xabort('SALT : DELMIN NOT DEFINED OR TO SMALL')
  ! initialization en fonction des donnees

  ! read geometry and create gg object
  ! **********************************
  ! lecture of the geometry name
  read(f_geo,'(a12)')  name_geom
  allocate(gg, stat= ok)
  if (ok /= 0) call xabort('SALT: FAILURE TO ALLOCATE GG')
  call sal100()

  ! store gg object in geometry directory on LCM
  ! ********************************************
  call lcmsix(ksal,'GEOMETRY    ',1)
  call my_lcmput(ksal,'NB_ELEM     ',gg%nb_elem)
  call my_lcmput(ksal,'NIPAR       ',size(gg%ipar,1))
  call my_lcmput(ksal,'IPAR        ',gg%ipar)
  call my_lcmput(ksal,'ISURF2_ELEM ',gg%isurf2_elem)
  call my_lcmput(ksal,'NB_NODE     ',gg%nb_node)
  call my_lcmput(ksal,'VOL_NODE    ',gg%vol_node)
  call my_lcmput(ksal,'NB_SURF2    ',gg%nb_surf2)
  lginf = .TRUE.
  if (gg%nb_surf2 > 0) then
     call my_lcmput(ksal,'IBC2_SURF2  ',gg%ibc2_surf2)
     call my_lcmput(ksal,'IELEM_SURF2 ',gg%ielem_surf2)
     call my_lcmput(ksal,'SURF2       ',gg%surf2)
     do i = 1,gg%nb_surf2
       lginf = lginf .and. (gg%bcdata(1,gg%idata_bc2(gg%ibc2_surf2(i))) == 1.)
     enddo
  end if
  call my_lcmput(ksal,'NPERIM_MAC2 ',gg%nperim_mac2)
  call my_lcmput(ksal,'PERIM_MAC2  ',gg%perim_mac2)
  call my_lcmput(ksal,'PPERIM_MAC2 ',gg%pperim_mac2)
  call my_lcmput(ksal,'MED         ',gg%med(:))
  call my_lcmput(ksal,'BC_DATA_DIM2',size(gg%bcdata,2))
  call my_lcmput(ksal,'BC_DATA     ',gg%bcdata(:,:))
  call my_lcmput(ksal,'NB_BC2      ',gg%nb_bc2)
  call my_lcmput(ksal,'TYPE_BC2    ',gg%type_bc2)
  call my_lcmput(ksal,'IDATA_BC2   ',gg%idata_bc2)
  call lcmsix(ksal,' ',2) ! come back to father directory

  if (prtind > 0) write(6,'(/" SALT: TYPGEO=",i5)') typgeo
  select case(typgeo)
     case(0)
        if ((option<3).or.(option>=8))  call  xabort('SALT : WRONG OPTION QUADRATURE')
        if ((option==3).and.(nsn==0))  call xabort('SALT : NSN SHOULD BE DEFINED FOR QUAD =3')
     case(1)
        ! symetry case
        type_14 = 1  ! rectangular
        type_15 = 6  ! angle symmetry 
        if (option /= 10)   call  xabort('SALT :  WRONG OPTION QUADRATURE : SHOULD BE 10')
        if ((meth_phi==0).or.(meth_teta==0)) call  xabort('SALT : FOR CYCLIC, ' &
             // 'WE SHOULD DEFINE A PRODUCT QUADRATURE MPHI AND MTETA')
        if ((nphi==0).or.(npsix==0))   call  xabort('SALT : AN2D AND AN3D SHOULD BE DEFINED')
        if ((mod(nphi,2)/=0).or.(mod(npsix,2)/=0)) call  xabort('SALT : AN2D AND AN3D SHOULD BE EVEN')
     case(2)
        ! rotation
        ! cyclic ; type(15) = 3 or 4 : internal rotation with symmetry or translation
        if (n_axis == 4) then
           type_14 = 1 ! rectangular
           if (option /= 8)   call  xabort('SALT :  WRONG OPTION QUADRATURE : SHOULD BE 8 (1)')
           if ((nphi==0).or.(npsix==0))   call  xabort('SALT : AN2D AND AN3D SHOULD BE DEFINED')
           if ((mod(nphi,2)/=0).or.(mod(npsix,2)/=0)) call  xabort('SALT : AN2D AND AN3D SHOULD BE EVEN')
        else
           type_14 = 1 ! rectangular
           type_15 = 5 ! angle rotation
           if (option /= 9)   call  xabort('SALT :  WRONG OPTION QUADRATURE : SHOULD BE 9')
           if ((nphi==0).or.(npsix==0))   call  xabort('SALT : AN2D AND AN3D SHOULD BE DEFINED')
           if (mod(nphi,2)/=0) call  xabort('SALT : AN2D  SHOULD BE EVEN')
        end if
     case(5)
        type_14 = 1 ! rectangular
        type_15 = 1 ! global translation
        if (option /= 8)   call  xabort('SALT :  WRONG OPTION QUADRATURE : SHOULD BE 8 (2)')
        if ((meth_phi==0).or.(meth_teta==0)) call  xabort('SALT : FOR CYCLIC, ' &
                 // 'WE SHOULD DEFINE A PRODUCT QUADRATURE MPHI AND MTETA')
        if ((nphi==0).or.(npsix==0))   call  xabort('SALT : AN2D AND AN3D SHOULD BE DEFINED')
        if ((mod(nphi,2)/=0).or.(mod(npsix,2)/=0)) call  xabort('SALT : AN2D AND AN3D SHOULD BE EVEN')
        if ((mod(nbfold,2) /= 0).and. (mod(nphi,4)/=0)) call  xabort('SALT : AN2D BE A MULTIPLE OF 4')
     case(6)
        type_14 = 1 ! rectangular
        type_15 = 2 ! global symmetry
        if (option /= 8)   call  xabort('SALT :  WRONG OPTION QUADRATURE : SHOULD BE 8 (3)')
        if ((meth_phi==0).or.(meth_teta==0)) call  xabort('SALT : FOR CYCLIC, ' &
                 // 'WE SHOULD DEFINE A PRODUCT QUADRATURE MPHI AND MTETA')
        if ((nphi==0).or.(npsix==0))   call  xabort('SALT : AN2D AND AN3D SHOULD BE DEFINED')
        if ((mod(nphi,2)/=0).or.(mod(npsix,2)/=0)) call  xabort('SALT : AN2D AND AN3D SHOULD BE EVEN')
     case(7)
        type_14 = 3  ! 1/8 symmetry
        type_15 = 2  ! global symmetry
        if (option /= 8)   call  xabort('SALT :  WRONG OPTION QUADRATURE : SHOULD BE 8 (4)')
        if ((meth_phi==0).or.(meth_teta==0)) call  xabort('SALT : FOR CYCLIC, ' &
                 // 'WE SHOULD DEFINE A PRODUCT QUADRATURE MPHI AND MTETA')
        if ((nphi==0).or.(npsix==0))   call  xabort('SALT : AN2D AND AN3D SHOULD BE DEFINED')
        if ((mod(2*nphi,2)/=0).or.(mod(npsix,2)/=0)) call  xabort('SALT : 2*AN2D AND AN3D SHOULD BE EVEN')
     case(8)
        type_14 = 4  ! hexagonal
        type_15 = 2  ! global symetry
        if (option /= 8)   call  xabort('SALT :  WRONG OPTION QUADRATURE : SHOULD BE 8 (5)')
        if ((meth_phi==0).or.(meth_teta==0)) call  xabort('SALT : FOR CYCLIC, ' &
                 // 'WE SHOULD DEFINE A PRODUCT QUADRATURE MPHI AND MTETA')
        if ((nphi==0).or.(npsix==0))   call  xabort('SALT : AN2D AND AN3D SHOULD BE DEFINED')
        if ((mod(3*nphi,2)/=0).or.(mod(npsix,2)/=0)) call  xabort('SALT : 3*AN2D AND AN3D SHOULD BE EVEN')
        type_14 = 4  ! hexagonal
     case(9)
        type_14 = 4  ! hexagonal
        type_15 = 1  ! global translation
        if (option /= 8)   call  xabort('SALT :  WRONG OPTION QUADRATURE : SHOULD BE 8 (6)')
        if ((meth_phi==0).or.(meth_teta==0)) call  xabort('SALT : FOR CYCLIC, ' &
                 // 'WE SHOULD DEFINE A PRODUCT QUADRATURE MPHI AND MTETA')
        if ((nphi==0).or.(npsix==0))   call  xabort('SALT : AN2D AND AN3D SHOULD BE DEFINED')
        if ((mod(3*nphi,2)/=0).or.(mod(npsix,2)/=0)) call  xabort('SALT : 3*AN2D AND AN3D SHOULD BE EVEN')
  end select 
        
  ! create garbage unit for tracks
  local_track = 'tracking_fil'
  fname =local_track
  ftrack2 =  give_unit()
  open(UNIT=ftrack2,FILE=fname,FORM='unformatted',ACCESS='sequential',STATUS='new')
  if (ftrack2 <= 0) call  xabort('SALT : LOCAL UNIT NUMBER FOR TRACKS NEGATIVE')

  ! tracking
  ierr=-1 
  fname =  'tracking_fil'
  call sal200(ierr)

  ! destroy local tracking file
  close(ftrack2, STATUS='delete',IOSTAT=ierr)
  if (ierr /= 0 ) call  xabort('SALT : LOCAL TRACK FILE NOT DESTROYED')

  ! lecture of the corresponding numerotation between SAL and Dragon numerotation
  text12='MERGE       '
  igig = 0
  if (icreate == 2) then
     call lcmlen(kentry_4,'BIHET',ilong,itylcm)
     lbihet=(ilong /= 0)
     if(lbihet) iqua10=5
  else
     call my_lcmget(kentry_4,text12,gg%num_merge)
     ! recuperation du state vector si on a cree un nouvel objet
     text12='STATE-VECTOR'
     call my_lcmget(kentry_4,text12,i_state)
     ! nombre de gigogne max
     igig = i_state(7)
     if (icreate ==1 ) then
        allocate( gig(gg%nb_node*igig))
        text12='GIGOGNE     '
        call my_lcmget(kentry_4,text12,gig)
     end if
     lbihet=(i_state(40) > 0)
     if(lbihet) then
        call lcmsix(kentry_4,'BIHET',1)
        call lcmget(kentry_4,'PARAM',i_state)
        call lcmsix(kentry_4,'BIHET',2)
        ibihet=i_state(6)
        iqua10=i_state(8)
     else
        ibihet=0
        iqua10=0
     end if
  end if
  !
  ! compute the number of unknowns.
  nb_unk=gg%nb_node
  do i=1,maxval(gg%num_merge)
    k=0
    do j=1,gg%nb_node
      if(gg%num_merge(j)==i) k=k+1
    enddo
    if(k>1) nb_unk=nb_unk+1
  enddo
  !
  ! store data in objet
  text12 = 'L_TRACK     '
  call my_lcmput(ksal,'SIGNATURE   ',text12) ! name
  ! store the STATE VECTOR
  max_reg=maxval(gg%num_merge)
  i_state(:) =0
  i_state(1) = max_reg ! number of regions
  if(upn >= 0) then
    i_state(2) = max_reg ! number of regions in DRAGON
  else
    i_state(2) = nb_unk ! number of unknowns in DRAGON
  endif
  i_state(3) = 1 ! 1 = absent leakage, 0 leakage
  i_state(4) = nbmed ! maximum number of mixture
  i_state(5) = gg%nb_surf2 ! number of outer surface
  i_state(6) = nangle ! number of angles in half the sphere
  i_state(7) = igig ! nombre de gigogne max
  i_state(8) = nphi ! nb of horizontal angles
  i_state(9) = npsix ! nb of vertical angles
  i_state(10) = nbcurr ! nb of current to describe a boundary current
  i_state(11) = mphi  ! nb of horizontal cones
  i_state(12) = mpsi   ! nb of vertical cones to describe boundary conditions
  i_state(13) = nmax2 ! size of the buffer to store tracking
  i_state(14) = nd_turn ! for rotations or symetry
  i_state(15) = record  ! number of recors used to write the tracking
  i_state(16) = typgeo  ! type of the geometry
  i_state(17) = nbfold   ! for rotations 
  i_state(18) = gg%nb_node ! number of unknowns in SAL
  i_state(19) = upn ! =-1: characteristic solution; =0: DP-0 ; =1: DP-1; =999: specular bc
  if(lbihet) i_state(40) = 1
  !
  ! si nouvel objet on ecrit 
  if (icreate ==1 ) then
     text12='MERGE       '
     call my_lcmput(ksal,text12,gg%num_merge)
     text12='GIGOGNE     '
     call my_lcmput(ksal,text12,gig)
     deallocate(gig)
  else if (icreate ==2 ) then
     text12='MERGE       '
     call my_lcmput(ksal,text12,gg%num_merge)
  end if
  text12 = 'SAL         '
  call my_lcmput(ksal,'TRACK-TYPE  ',text12) ! TRACKING TYPE
  call my_lcmput(ksal,'LINK.GEOM   ',name_geom) ! Geometry link
  text12 = hentry(2)
  call my_lcmput(ksal,'LINK.FTRACK ',text12) ! Link to the tracking file
  call XDRTA1(ksal,.TRUE.,.FALSE.) ! Compute Bickley function tables

  ! fill-in medium number per region
  allocate(itab(max_reg),volume(max_reg), stat =ok)
  if (ok /= 0) call xabort('SALT : FAILURE TO ALLOCATE INTEGER ITAB')
  ! fill in matcod
  do j=1 , gg%nb_node
     itab(gg%num_merge(j)) = gg%med(j)
  end do
  call my_lcmput(ksal,'MATCOD      ',itab(1:maxval(gg%num_merge)) ) 

  ! fill-in keyflx per region
  itab(:) = 0
  if(upn == -1) then
    ! correction to deal with merge
    do i=1, gg%nb_node
      itab(gg%num_merge(i)) = i
    end do
    i_cont =0
    do i=1 ,size(gg%num_merge)
      j =0
      ! calculate if a volume is merged
      do k=1, gg%nb_node
        if (gg%num_merge(k)==i) then
           j = j+1
           itot=1
        else
           itot=0
        end if
        if ((itot==1).and.(j==2))  then
           i_cont = i_cont+1  ! to mark the number of the flux after nb_node
           itab(i) = gg%nb_node + 2*nbcurr+ i_cont
           exit
        end if
      end do
    end do
  else
    ! pij case
    do i=1, max_reg
      itab(i) = i
    end do
  endif  
  call my_lcmput(ksal,'KEYFLX      ',itab)

  ! fill-in volumes per region
  volume(:) =0.
  do i=1, gg%nb_node
     volume(gg%num_merge(i)) = volume(gg%num_merge(i)) + real(gg%vol_node(i))
  end do
  call my_lcmput(ksal,'VOLUME      ',volume)

  ! useful values in geometry_1.md9
  call my_lcmput(ksal,'ANGGEO      ',anggeo)
  call my_lcmput(ksal,'LENGTHX     ',lengthx)
  call my_lcmput(ksal,'LENGTHY     ',lengthy)

  ! save the STATE-VECTOR record
  if (.not.lginf) i_state(3)=0 ! reset the leakage flag
  call my_lcmput(ksal,'STATE-VECTOR',i_state) 

  ! useful values in tracking_1
  call my_lcmput(ksal,'LGNOTCYCL   ',lgnotcycl)
  
  ! move to tt object
  call lcmsix(ksal,'CHARACTER   ',1)
  ! store tt object
  call my_lcmput(ksal,'PSIVAL      ',tt%psival)
  call my_lcmput(ksal,'ADPHI       ',tt%adphi)
  if ((option==8).and.associated(tt%m)) then
     call my_lcmput(ksal,'M           ',tt%m(:))
  end if
  if ((option==8).and.associated(tt%n)) then
     call my_lcmput(ksal,'N           ',tt%n(:))
  end if
  call my_lcmput(ksal,'AQ2D        ',tt%aq2d(:,:))
  call my_lcmput(ksal,'AQ3D        ',tt%aq3d(:,:))
  call lcmsix(ksal,' ',2)  ! come back to father directory

  ! deallocate memory
  ! deallocate gg
  ! *************
  deallocate(gg%ipar,gg%rpar,gg%ibc2_elem,gg%isurf2_elem,gg%vol_node,gg%pperim_node, &
       gg%type_bc2,gg%idata_bc2,gg%perim_mac2,gg%pperim_mac2,gg%med,  &
       gg%bcdata, gg%perim_node,stat =ok)
  if (ok /= 0) call xabort('SALT: FAILURE TO DEALLOCATE GG MEMBERS')
  if (gg%nb_surf2>0)  then
     deallocate(gg%ibc2_surf2,gg%ielem_surf2,gg%surf2,stat =ok)
     if (ok /= 0) call xabort('SALT: FAILURE TO DEALLOCATE GG SURF MEMBERS')
  end if
!  if (associated(gg%idata_axis)) then
!     deallocate(gg%idata_axis,stat =ok)
!     if (ok /= 0) call xabort('SALT: FAILURE TO DEALLOCATE GG%IDATA_AXIS')
!  end if
!  if (associated(gg%dist_axis)) then
!     deallocate(gg%dist_axis,stat =ok)
!     if (ok /= 0) call xabort('SALT: FAILURE TO DEALLOCATE GG%DIST_AXIS')
!  end if
  if (associated(gg%num_merge)) then
     deallocate(gg%num_merge,stat =ok)
     if (ok /= 0) call xabort('SALT: FAILURE TO DEALLOCATE GG%NUM_MERGE')
  end if
  deallocate(gg, stat= ok)
  if (ok /= 0) call xabort('SALT: FAILURE TO DEALLOCATE GG')
  ! deallocate TT
  !**************
  deallocate(tt%psival, stat =ok) 
  if (ok /= 0) call xabort('SALT: FAILURE TO DEALLOCATE TT%PSIVAL')
  deallocate(tt%phival, stat =ok) 
  if (ok /= 0) call xabort('SALT: FAILURE TO DEALLOCATE TT%PHIVAL')
  deallocate(tt%aq2d, stat =ok) 
  if (ok /= 0) call xabort('SALT: FAILURE TO DEALLOCATE TT%AQ2D')
  deallocate(tt%aq3d, stat =ok) 
  if (ok /= 0) call xabort('SALT: FAILURE TO DEALLOCATE TT%AQ3D')
  deallocate(tt%currn, stat =ok) 
  if (ok /= 0) call xabort('SALT: FAILURE TO DEALLOCATE TT%CURRN')
  deallocate(tt%surfn, stat =ok) 
  if (ok /= 0) call xabort('SALT: FAILURE TO DEALLOCATE TT%SURFN')
  deallocate(tt%voln, stat =ok) 
  if (ok /= 0) call xabort('SALT: FAILURE TO DEALLOCATE TT%VOLN')
  deallocate(tt%adphi, stat =ok) 
  if (ok /= 0) call xabort('SALT: FAILURE TO DEALLOCATE TT%ADPHI')
  if (associated(tt%n)) then
     deallocate(tt%n, stat=ok)
     if (ok /= 0) call xabort('SALT: FAILURE TO DEALLOCATE TT%N')
  end if
  if (associated(tt%m)) then
     deallocate(tt%m, stat=ok)
     if (ok /= 0) call xabort('SALT: FAILURE TO DEALLOCATE TT%M')
  end if
  deallocate(tt,  stat =ok)
  if (ok /= 0) call xabort('SALT: FAILURE TO DEALLOCATE TT')
  ! deallocate quadrature set
  ! *************************
  deallocate(qq%mu, qq%eta, qq%ksi, qq%weight, stat = ok)
  if (ok /= 0) call xabort('SALT: FAILURE TO DEALLOCATE QQ MEMBERS')
  if (associated(qq%m)) then
     deallocate(qq%m, stat=ok) ! bug
     if (ok /= 0) call xabort('SALT: FAILURE TO DEALLOCATE qq%M')
  end if
  if (ok /= 0) call xabort('SALT: FAILURE TO DEALLOCATE QQ MEMBERS')
  if (associated(qq%n)) then
     deallocate(qq%n, stat=ok) ! bug
     if (ok /= 0) call xabort('SALT: FAILURE TO DEALLOCATE qq%n')
  end if
  deallocate(qq,  stat =ok)
  if (ok /= 0) call xabort('SALT: FAILURE TO DEALLOCATE qq')
  ! deallocate local data
  deallocate(itab,volume,stat =ok)
  if (ok /= 0) call xabort('SALT: FAILURE TO DEALLOCATE LOCAL DATA')

  ! Process double heterogeneity (Bihet) data (if available)
  if (c_associated(kentry_5)) call XDRTBH(kentry_5,kentry_4,iqua10,ibihet,prtind)

  ! Convert SAL tracking in EXCELT format
  if (npsix==0) npsix=2
  write(6,*) 'INPUT PARAMETERS : NANIS & IMPX :',nanis,prtind
  CALL TRCDRV(2,hentry,ientry,(/0,0/),kentry,ksal,ftrack3,nanis,prtind)
  if(icreate == 2) call LCMCL(ksal,2)
  ok=KDRCLS(ftrack3,2)
  if (ok /= 0) call XABORT('SALT: KDRCLS failure')
end subroutine SALT
