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

!VERS

! 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.

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

!===============================================================================
! Function:
! ---------

!> \file cs_user_boundary_conditions.f90
!>
!> \brief User subroutine which fills boundary conditions arrays
!> (\c icodcl, \c rcodcl) for unknown variables.
!-------------------------------------------------------------------------------

!-------------------------------------------------------------------------------
! Arguments
!______________________________________________________________________________.
!  mode           name          role                                           !
!______________________________________________________________________________!
!> \param[in]     nvar          total number of variables
!> \param[in]     nscal         total number of scalars
!> \param[out]    icodcl        boundary condition code:
!>                               - 1 Dirichlet
!>                               - 2 Radiative outlet
!>                               - 3 Neumann
!>                               - 4 sliding and
!>                                 \f$ \vect{u} \cdot \vect{n} = 0 \f$
!>                               - 5 smooth wall and
!>                                 \f$ \vect{u} \cdot \vect{n} = 0 \f$
!>                               - 6 rought wall and
!>                                 \f$ \vect{u} \cdot \vect{n} = 0 \f$
!>                               - 9 free inlet/outlet
!>                                 (input mass flux blocked to 0)
!> \param[in]     itrifb        indirection for boundary faces ordering
!> \param[in,out] itypfb        boundary face types
!> \param[out]    izfppp        boundary face zone number
!> \param[in]     dt            time step (per cell)
!> \param[in,out] rcodcl        boundary condition values:
!>                               - rcodcl(1) value of the dirichlet
!>                               - rcodcl(2) value of the exterior exchange
!>                                 coefficient (infinite if no exchange)
!>                               - rcodcl(3) value flux density
!>                                 (negative if gain) in w/m2 or roughtness
!>                                 in m if icodcl=6
!>                                 -# for the velocity \f$ (\mu+\mu_T)
!>                                    \gradt \, \vect{u} \cdot \vect{n}  \f$
!>                                 -# for the pressure \f$ \Delta t
!>                                    \grad P \cdot \vect{n}  \f$
!>                                 -# for a scalar \f$ cp \left( K +
!>                                     \dfrac{K_T}{\sigma_T} \right)
!>                                     \grad T \cdot \vect{n} \f$
!_______________________________________________________________________________

subroutine cs_f_user_boundary_conditions &
 ( nvar   , nscal  ,                                              &
   icodcl , itrifb , itypfb , izfppp ,                            &
   dt     ,                                                       &
   rcodcl )

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

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

use paramx
use numvar
use optcal
use cstphy
use cstnum
use entsor
use parall
use period
use ihmpre
use ppppar
use ppthch
use coincl
use cpincl
use ppincl
use ppcpfu
use atincl
use atsoil
use ctincl
use cs_fuel_incl
use mesh
use field

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

implicit none

! Arguments

integer          nvar   , nscal

integer          icodcl(nfabor,nvarcl)
integer          itrifb(nfabor), itypfb(nfabor)
integer          izfppp(nfabor)

double precision dt(ncelet)
double precision rcodcl(nfabor,nvarcl,3)

! Local variables

! INSERT_VARIABLE_DEFINITIONS_HERE
integer          ifac, ii, iel
integer          idim
integer          izone,iesp
integer          ilelt, nlelt

integer, allocatable, dimension(:) :: lstelt

double precision, dimension(:,:), pointer :: cscaa

double precision rayo, a0, r0, enth, temp
double precision coefe(ngazem)
integer          iscal, mun, f_id
integer :: keyvar

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


!===============================================================================
! Initialization
!===============================================================================

allocate(lstelt(nfabor))  ! temporary array for boundary faces selection

call field_get_key_id("variable_id", keyvar)

!
! --- On impose en couleur 5 une cathode puis une entree/sortie ;
!     ==========================================================
!
!
CALL GETFBR('5',NLELT,LSTELT)
!==========

if ( ippmod(ielarc).ge.2 ) then
  call field_get_id('vec_potential', f_id)
  call field_get_key_int(f_id, keyvar, iscal)

  r0 = 1.5d-3
  a0 = 1.d-3

  write(nfecra,*) "elcou", elcou

  do ilelt = 1, nlelt

    ifac = lstelt(ilelt)
!
    iel =  ifabor(ifac)
    rayo = sqrt(xyzcen(1,iel)*xyzcen(1,iel)+xyzcen(2,iel)*xyzcen(2,iel))
!
! DEBUT : ELARGISSEMENT DE LA CATHODE
!
    if(rayo .gt. r0) then
      if ( rayo .lt. 6.1d-2 ) then
        icodcl(ifac,iscal  ) = 14 ! generalized symetric vector B.C.
        icodcl(ifac,iscal+1) = 14
        icodcl(ifac,iscal+2) = 14

        ! Dirichlet on the normal component
        rcodcl(ifac,iscal  ,1) = a0 - 2.d-7 * elcou * (0.5d0+log(rayo/a0))
        rcodcl(ifac,iscal+1,1) = a0 - 2.d-7 * elcou * (0.5d0+log(rayo/a0))
        rcodcl(ifac,iscal+2,1) = a0 - 2.d-7 * elcou * (0.5d0+log(rayo/a0))

        ! Homogeneous Neumann on the tangential component
        rcodcl(ifac,iscal  ,3) = 0.d0
        rcodcl(ifac,iscal+1,3) = 0.d0
        rcodcl(ifac,iscal+2,3) = 0.d0
      else
        icodcl(ifac,iscal  ) = 1 ! Dirichlet on the vector
        icodcl(ifac,iscal+1) = 1
        icodcl(ifac,iscal+2) = 1

        rcodcl(ifac,iscal  ,1) = 0.d0
        rcodcl(ifac,iscal+1,1) = 0.d0
        rcodcl(ifac,iscal+2,1) = a0 - 2.d-7 * elcou * (0.5d0+log(rayo/a0))
      endif
    endif
!
  enddo
endif

! --- On impose en couleur 2 ou 3 une paroi ;
!     ============================== exemple d'Anode en arc electrique
!                                    =================================

CALL GETFBR('2 or 3',NLELT,LSTELT)
!==========

do ilelt = 1, nlelt

  ifac = lstelt(ilelt)

!          SORTIE : FLUX NUL VITESSE ET TEMPERATURE, PRESSION IMPOSEE
!            Noter que la pression sera recalee a P0
!                sur la premiere face de sortie libre (ISOLIB)

! --- On traite les scalaires rattaches a la phase courante

!  Enthalpie en J/kg  (Par defaut flux nul avec ISOLIB)
!
!
  ii = iscalt
!
  iel = ifabor(ifac)
  rayo= sqrt(xyzcen(1,iel)*xyzcen(1,iel)+xyzcen(2,iel)*xyzcen(2,iel))
!
  if(rayo .lt. 4.44d-3) then
    temp = 12500.d0
    enth = 0.14176d8
    icodcl(ifac,isca(ii))  = 1
    rcodcl(ifac,isca(ii),1)  = enth
!
  else if (rayo .ge. 4.44d-3 .and. rayo .le. 7.6d-3 ) then
    temp =  -3644314.9d0*rayo+28696.955d0
    coefe(1) = 1.d0
    do iesp = 2,ngazem
      coefe(iesp) = 0.d0
    enddo
    mun = -1
    call elthht(mun,coefe,enth,temp)
    icodcl(ifac,isca(ii))  = 1
    rcodcl(ifac,isca(ii),1)  = enth
!
  else if (rayo .gt.  7.6d-3 ) then
    temp =  1000.d0
    enth = 0.3652d6
    icodcl(ifac,isca(ii))  = 1
    rcodcl(ifac,isca(ii),1)  = enth
  endif
!
!
enddo

! --- On impose en couleur 6 une paroi
!     ================================

CALL GETFBR('6',NLELT,LSTELT)
!==========

call field_get_id('vec_potential', f_id)
call field_get_key_int(f_id, keyvar, iscal)
call field_get_val_prev_v_by_name('vec_potential', cscaa)

do ilelt = 1, nlelt
  ifac = lstelt(ilelt)
  if ( ippmod(ielarc).ge.2 ) then
    iel = ifabor(ifac)

    icodcl(ifac,iscal  )   = 1 ! Dirichlet on the vector
    icodcl(ifac,iscal+1)   = 1
    icodcl(ifac,iscal+2)   = 1

    rcodcl(ifac,iscal  ,1) = cscaa(1,iel)
    rcodcl(ifac,iscal+1,1) = cscaa(2,iel)
    rcodcl(ifac,iscal+2,1) = cscaa(3,iel)
  endif
enddo

deallocate(lstelt)  ! temporary array for boundary faces selection

return
end subroutine cs_f_user_boundary_conditions
