!!  CDW 1 June 2020

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

!                      Code_Saturne version 
!                      --------------------------
! This file is part of Code_Saturne, a general-purpose CFD tool.
!
! Copyright (C) 1998-2019 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.

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

!-------------------------------------------------------------------------------
! 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 rough wall and
!>                                 \f$ \vect{u} \cdot \vect{n} = 0 \f$
!>                               - 9 free inlet/outlet
!>                                 (input mass flux blocked to 0)
!>                               - 13 Dirichlet for the advection operator and
!>                                    Neumann for the diffusion operator
!> \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 roughness
!>                                 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
use turbomachinery
use iso_c_binding
use cs_c_bindings

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

implicit none

! Arguments

integer          nvar   , nscal, ivar

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

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


! INSERT_VARIABLE_DEFINITIONS_HERE
integer          ifac !, iel, ii
integer          ilelt, nlelt
!double precision uref2
!double precision rhomoy, xdh
!double precision xitur


integer, allocatable, dimension(:) :: lstelt

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


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

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

! INSERT_ADDITIONAL_INITIALIZATION_CODE_HERE

!===============================================================================
! Assign boundary conditions to boundary faces here

! For each subset:
! - use selection criteria to filter boundary faces of a given subset
! - loop on faces from a subset
!   - set the boundary condition for each face
!===============================================================================

! INSERT_MAIN_CODE_HERE


!! Bottom face
call getfbr('F_bottom', nlelt, lstelt) 

do ilelt = 1, nlelt

   ! Velocity field
   ifac = lstelt(ilelt)
   icodcl(ifac,iu)=3 ! Homogeneous Neumann boundary
   icodcl(ifac,iv)=1 ! Dirichlet
   icodcl(ifac,iw)=1

   rcodcl(ifac,iu,3)=0
   rcodcl(ifac,iv,1)=0
   rcodcl(ifac,iw,1)=0

   ! Temperature field
   ivar = isca(1)
   icodcl(ifac,ivar) = 1
   rcodcl(ifac,ivar,1)=293.15
end do


!! Top face
call getfbr('F_top', nlelt, lstelt) 
do ilelt = 1, nlelt
   
   ! Velocity field
   ifac = lstelt(ilelt)
   icodcl(ifac,iu)=3
   icodcl(ifac,iv)=3
   icodcl(ifac,iw)=1

   rcodcl(ifac,iu,3)=0
   rcodcl(ifac,iv,3)=0
   rcodcl(ifac,iw,1)=0

   ! Temperature field
   ivar = isca(1)
   icodcl(ifac,ivar) = 1 
   rcodcl(ifac,ivar,1)=293.15
   rcodcl(ifac,ivar,3)=0
end do

!! Bottom face
call getfbr('F_right', nlelt, lstelt) 

do ilelt = 1, nlelt

   ! Velocity field
   ifac = lstelt(ilelt)
   icodcl(ifac,iu)=3
   icodcl(ifac,iv)=3
   icodcl(ifac,iw)=1

   rcodcl(ifac,iu,3)=0
   rcodcl(ifac,iv,3)=0
   rcodcl(ifac,iw,1)=0
   
   ! Temperature field
   ivar = isca(1)
   icodcl(ifac,ivar) = 1
   rcodcl(ifac,ivar,1) = 293.15
   rcodcl(ifac,ivar,3) = 0
end do

!----
! End
!----

deallocate(lstelt)  ! temporary array for boundary faces selection

return
end subroutine cs_f_user_boundary_conditions

!> \section examples Examples
!>   Several examples are provided
!>   \ref cs_user_boundary_conditions_examples "here".
