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

!                      Code_Saturne version 3.2.2
!                      --------------------------
! This file is part of Code_Saturne, a general-purpose CFD tool.
!
! Copyright (C) 1998-2013 EDF S.A.
!
! This program is free software; you can redistribute it and/or modify it under
! the terms of the GNU General Public License as published by the Free Software
! Foundation; either version 2 of the License, or (at your option) any later
! version.
!
! This program is distributed in the hope that it will be useful, but WITHOUT
! ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
! FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
! details.
!
! You should have received a copy of the GNU General Public License along with
! this program; if not, write to the Free Software Foundation, Inc., 51 Franklin
! Street, Fifth Floor, Boston, MA 02110-1301, USA.

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

subroutine uslag2 &
!================

 ( nvar   , nscal  ,                                              &
   nbpmax , nvp    , nvp1   , nvep   , nivep  ,                   &
   ntersl , nvlsta , nvisbr ,                                     &
   itypfb , itrifb , itepa  , ifrlag ,                            &
   dt     , rtpa   , propce ,                                     &
   ettp   , tepa   )

!===============================================================================
! Purpose:
! ----------

!   Subroutine of the Lagrangian particle-tracking module:
!   -------------------------------------

!    User subroutine (Mandatory intervention)

!    User subroutine for the boundary conditions associated to the particles
!    (inlet and treatment of the other boundaries)


! Boundary faces identification
! =============================

! Boundary faces may be identified using the 'getfbr' subroutine.
! The syntax of this subroutine is described in the
! 'cs_user_boundary_conditions' subroutine,
! but a more thorough description can be found in the user guide.


!-------------------------------------------------------------------------------
! Arguments
!__________________.____._____.________________________________________________.
! name             !type!mode ! role                                           !
!__________________!____!_____!________________________________________________!
! nvar             ! i  ! <-- ! total number of variables                      !
! nscal            ! i  ! <-- ! total number of scalars                        !
! nbpmax           ! i  ! <-- ! maximum number of particles allowed            !
! nvp              ! i  ! <-- ! number of particle variables                   !
! nvp1             ! i  ! <-- ! nvp minus position, fluid and part. velocities !
! nvep             ! i  ! <-- ! number of particle properties (integer)        !
! nivep            ! i  ! <-- ! number of particle properties (integer)        !
! ntersl           ! i  ! <-- ! number of source terms of return coupling      !
! nvlsta           ! i  ! <-- ! nb of Lagrangian statistical variables         !
! nvisbr           ! i  ! <-- ! number of boundary statistics                  !
! itrifb(nfabor)   ! ia ! <-- ! indirection for the sorting of the             !
! itypfb(nfabor)   ! ia ! <-- ! type of the boundary faces                     !
! ifrlag(nfabor    ! ia ! --> ! type of the Lagrangian boundary faces          !
! itepa            ! ia ! <-- ! particle information (integers)                !
! (nbpmax,nivep    !    !     !                                                !
! dt(ncelet)       ! ra ! <-- ! time step (per cell)                           !
! rtpa             ! ra ! <-- ! transported variables at the previous          !
! (ncelet,*)       !    !     ! time step                                      !
! propce           ! ra ! <-- ! physical properties at cell centers            !
! (ncelet,*)       !    !     !                                                !
! ettp             ! ra ! <-- ! array of the variables associated to           !
!  (nbpmax,nvp)    !    !     ! the particles at the current time step         !
! tepa             ! ra ! <-- ! particle information (real) (statis. weight..) !
! (nbpmax,nvep)    !    !     !                                                !
!__________________!____!_____!________________________________________________!

!     Type: i (integer), r (real), s (string), a (array), l (logical),
!           and composite types (ex: ra real array)
!     mode: <-- input, --> output, <-> modifies data, --- work array
!===============================================================================

!===============================================================================
! Module files
!===============================================================================

use paramx
use numvar
use optcal
use cstnum
use cstphy
use entsor
use lagpar
use lagran
use ppppar
use ppthch
use cpincl
use ihmpre
use mesh

!===============================================================================

implicit none

! Arguments

integer          nvar   , nscal
integer          nbpmax , nvp    , nvp1   , nvep  , nivep
integer          ntersl , nvlsta , nvisbr

integer          itypfb(nfabor) , itrifb(nfabor)
integer          itepa(nbpmax,nivep) , ifrlag(nfabor)

double precision dt(ncelet) , rtpa(ncelet,*)
double precision propce(ncelet,*)
double precision ettp(nbpmax,nvp) , tepa(nbpmax,nvep)

! Local variables

integer          ifac , izone, nbclas, iclas
integer          icoal , ilayer
integer          ilelt, nlelt


integer, dimension(ndlaim) :: iczpar
double precision, dimension(ndlagm) :: rczpar

integer, allocatable, dimension(:) :: lstelt
!Duc variable
integer it, ii, ii2
double precision :: r, v2, x, y
!real :: ro1, ro2, m_sg, U1_const, U2_const,alp2
double precision, dimension (1 : 15) :: ri2, vi2
!Outlet variables

!===============================================================================



!===============================================================================
! 1.  Memory management
!===============================================================================
! Allocate a temporary array for boundary faces selection
allocate(lstelt(nfabor))

izone = 0

call getfbr('INLET',nlelt,lstelt)

do ilelt=1,nlelt
  ifac = lstelt(ilelt)
  itypfb(ifac) = ientre
  izone        = 1
  ifrlag(ifac) = izone
enddo

call getfbr('Outlet',nlelt,lstelt)

do ilelt = 1, nlelt
  ifac = lstelt(ilelt)

  izone        = 2
  ifrlag(ifac) = izone
enddo

call getfbr('WALL',nlelt,lstelt)

do ilelt = 1, nlelt
  ifac = lstelt(ilelt)

  izone        = 3
  ifrlag(ifac) = izone
enddo


!2 classes de particule injecté dans inlet
izone     = 1
nbclas    = 2
iusncl(izone) = nbclas

izone     = 2
nbclas    = 0
iusncl(izone) = nbclas


izone     = 3
nbclas    = 0
iusncl(izone) = nbclas


! --> For every class associated with a zone,
!     we give the following information.


!     iusncl number of classes per zone
!     iusclb boundary conditions for the particles
!     = ientrl -> zone of particle inlet
!     = isortl -> particle outlet
!     = irebol -> rebound of the particles
!     = idepo1 -> definitive deposition
!     = idepo2 -> definitive deposition, but the particle remains in memory
!                 (useful only if iensi2 = 1)
!     = idepfa -> deposition of the particle with DLVO forces
!     = iencrl -> fouling (coal only iphyla = 2)
!     = isymtl -> symmetry condition for the particles (zero flux)

!     Array iczpar:
!     ============
!        ijnbp : number of particles per class and per zone
!        ijfre : injection frequency. If ijfre = 0, then the injection
!                occurs only at the first absolute iteration.
!        iclst : number of the group to which the particle belongs
!                (only if one wishes to calculate statistics per group)
!        ijuvw : type of condition on the velocity
!                  = -1 imposed flow velocity
!                  =  0 imposed velocity along the normal direction of the
!                      boundary face, with norm equal to rczpar(iuno)
!                  =  1 imposed velocity: we prescribe   rczpar(iupt)
!                                                        rczpar(ivpt)
!                                                        rczpar(iwpt)
!                  =  2 user-defined profile
!        ijprtp : type of temperature condition
!                  =  1 imposed temperature: we prescribe rczpar(itpt)
!                  =  2 user-defined profile
!        ijprdp : type of diameter condition
!                  =  1 imposed diameter: we prescribe  rczpar(idpt)
!                                                       rczpar(ivdpt)
!                  =  2 user-defined profile
!        ijprtp : type of temperature condition
!                  =  1 imposed temperature: we prescribe rczpar(itpt)
!                  =  2 user-defined profile
!        inuchl : number of the coal of the particle (only if iphyla = 2)
!        irawcl : type of coal injection composition (only if iphyla = 2)
!                  = 0 coal injected with an user-defined composition
!                  = 1 raw coal injection

!     Array rczpar:
!     ============
!        iuno  : Norm of the velocity (m/s)
!        iupt  : Velocity along the X axis, for each class and for each zone (m/s)
!        ivpt  : Velocity along the Y axis, for each class and for each zone (m/s)
!        iwpt  : Velocity along the Z axis, for each class and for each zone (m/s
!        ipoit : Statistical weight (number of samples) associated
!                to the particle (automatically computed to respect a mass
!                flow rate if it is defined)
!        idebt : Mass flow rate (kg/s)

!        Physical characteristics:
!          idpt   : diameter (m)
!          ivdpt  : standard deviation of the diameter (m)
!          iropt  : density (kg/m3)
!          itpt   : temperature in Celsius degress (no enthalpy)
!          icpt   : specific heat (J/kg/K)
!          iepsi  : emissivity (if =0 then no radiative effect is taken into account)

!         If coal (iphyla=2)
!            ihpt    : temperature in Kelvin degres (no enthalpy) (layer by layer)
!            ifrmwt  : mass fraction of moisture in the particle
!            ifrmch  : mass fraction of reactive coal in the particle (layer by layer)
!            ifrmck  : mass fraction of coke coal in the particle (layer by layer)
!            irdck   : coke diameter (m)
!            ird0p   : coal particle initial diameter (m)
!            irhock0 : density of coke at the end of the pyrolysis (kg/m³) (layer by layer)


! ---> EXAMPLE : First zone, numbered IZONE = 1 (NBCLAS classes)
!        IUSCLB : adherence of the particle to a boundary face
!        IJNBP  : 10 particles for each class,
!        IJFRE  : injection every other time step
!        IJUVW, IUPT, IVPT, IWPT : imposed velocity on 1.1D0, 0.0D0, 0.0D0
!        ICPT   : cp equal to 10000
!        ITPT   : temperature equal to 25 Celsius degress
!        IDPT   : diameter equal to 50.E-6 m
!        IEPSI  : emissivity equal to 0.7
!        IVDPT  : constant diameter ==> standard deviation null
!        IROPT  : density
!        IPOIT  : statistical weight (number of physical particles
!                 represented by one statistical particle)
!        IDEBT  : mass flow rate

izone         = 1
nbclas        = iusncl(izone)
iusclb(izone) =  ientrl


iclas = 1
  ! Ensure defaults are set

  call lagr_init_zone_class_param(iczpar, rczpar)
  !==============================
  
  ! Now define parameters for this class and zone
  
  !iczpar(ijnbp) = 500
  iczpar(ijnbp) = 250
  iczpar(ijfre) = 1   
  iczpar(ijuvw) = 2
  iczpar(iclst) = 1
  !rczpar(iupt)  = 0.0d0
  !rczpar(ivpt)  = 0.0d0
  !rczpar(iwpt)  = 0.0d0
  !Définir mass flow rate (kg/s)
  iczpar(ijprpd)= 1
  rczpar(ipoit) = 150d0
  !rczpar(ipoit) = 100d0
  rczpar(idebt) = 0.0d0
  
 
  ! if the physics is " simple"

  if (iphyla.eq.0 .or. iphyla.eq.1) then

    ! Mean value and standard deviation of the diameter

    iczpar(ijprdp)= 2
    !rczpar(idpt)  = 25.0d-6
    !rczpar(ivdpt) = 0.d0

   ! Density

    rczpar(iropt) = 2500.d0
  endif

  ! Définir la classe 1 (25micron) appartient au groupe 1 de post-processing.
   

  ! Complete definition of parameters for this class and zone
  call lagr_define_zone_class_param(iclas, izone, iczpar, rczpar)


iclas = 2
  !================================

  call lagr_init_zone_class_param(iczpar, rczpar)
  !==============================

  ! Now define parameters for this class and zone

  !iczpar(ijnbp) = 500
  iczpar(ijnbp) = 250
  iczpar(ijfre) = 1  
  iczpar(ijuvw) = 2
  iczpar(iclst) = 2
  !rczpar(iupt)  = 0.0d0
  !rczpar(ivpt)  = 0.0d0
  !rczpar(iwpt)  = 0.0d0
  !Définir mass flow rate (kg/s)
  iczpar(ijprpd)= 1
  rczpar(ipoit) = 1.5d0
  !rczpar(ipoit) = 250d0
  rczpar(idebt) = 0.0d0
 
  ! if the physics is " simple"

  if (iphyla.eq.0 .or. iphyla.eq.1) then

    ! Mean value and standard deviation of the diameter

    iczpar(ijprdp)= 2
    !rczpar(idpt)  = 70.0d-6
    !rczpar(ivdpt) = 0.d0

   ! Density

    rczpar(iropt) = 2500.d0
  endif
  
  ! Complete definition of parameters for this class and zone
  call lagr_define_zone_class_param(iclas, izone, iczpar, rczpar)
  !================================

!enddo




izone=2
iusclb(izone) =  isortl



izone=3
iusclb(izone) =  irebol



!----

! Deallocate the temporary array
deallocate(lstelt)

return
end subroutine uslag2
