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

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

! Example of cs_user_boundary_conditions subroutine.f90 for inlet
! with inlet profile mapped to profile inside the domain.

! This example assumes the mesh is orthogonal at the inlet.
!
!-------------------------------------------------------------------------------

!-------------------------------------------------------------------------------
! 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_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 elincl
use cs_fuel_incl
use mesh
use field
use turbomachinery
use iso_c_binding
use cs_c_bindings

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

implicit none

! Arguments

integer          nvar   , nscal

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

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

! Local variables

!< [loc_var_dec]
integer          ifac, iel, ii, ivar, iscal, ilelt, nlfac
double precision xkent, xeent

integer          keyvar, keysca
integer          n_fields, f_id, normalize, interpolate
double precision xustar2, xdh, d2s3, rhomoy
double precision fmprsc, uref2
DOUBLE PRECISION tar_Tbin, Tbout, mTb, tot_m, dTb, ifac_massflow

integer, allocatable, dimension(:) :: lstfac, lstcel
double precision, dimension(:), pointer :: brom

double precision, dimension(:,:), pointer :: vel
double precision, dimension(:), pointer ::  imasfl
double precision, dimension(3,1) :: coord_shift
double precision, dimension(1) :: rvoid

type(c_ptr), save :: inlet_l = c_null_ptr
!< [loc_var_dec]

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

!< [set target inlet bulk temperature]




!< [init]
allocate(lstfac(nfabor))  ! temporary array for boundary faces selection

! Map field arrays
call field_get_val_v(ivarfl(iu), vel)

call field_get_val_s(ibrom, brom)


call getfbr('top', nlfac, lstfac)


if (ntcabs.eq.ntpabs+1) then

  allocate(lstcel(ncel))

  do iel = 1, ncel
    lstcel(iel) = iel
  enddo

  coord_shift(1,1) = 0.d0
  coord_shift(2,1) = 0.d0
  coord_shift(3,1) = -0.0162d0

  inlet_l = boundary_conditions_map(MESH_LOCATION_CELLS, ncel,                &
                                    nlfac, lstcel, lstfac,                    &
                                    coord_shift, 0,                           &
                                    0.1d0)

  deallocate(lstcel)

endif


! Subsequent time steps
!----------------------

!< [example_1_map_apply]


if (ntcabs.gt.1) then

  call field_get_n_fields(n_fields)
  call field_get_key_id("variable_id", keyvar)
  call field_get_key_id("scalar_id", keysca)



  interpolate = 0

  do f_id = 0, n_fields-1
    call field_get_key_int(f_id, keyvar, ivar)
    if (ivar.ge.1) then
      call field_get_key_int(f_id, keysca, iscal)
      if (ivar.eq.ik .or. ivar.eq.iomg) then
        normalize = 0
      else 
        normalize = 1
      endif
      
      call boundary_conditions_mapped_set(f_id, inlet_l, MESH_LOCATION_CELLS, &
                                          normalize, interpolate,             &
                                          nlfac, lstfac, rvoid,               &
                                          nvarcl, rcodcl)
      
    endif
  enddo



     
endif
!< [example_1_map_apply]

! Destroy locator at end

!< [example_1_map_free]
if (ntcabs.eq.ntmabs) then
  call locator_destroy(inlet_l)
endif
!< [example_1_map_free]

!--------
! Formats
!--------

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

deallocate(lstfac)  ! temporary array for boundary faces selection

return
end subroutine cs_user_boundary_conditions
