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

!                      Code_Saturne version 3.0.0-rc1
!                      --------------------------
! 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-base.f90
!>
!> \brief Basic example of cs_user_boundary_conditions subroutine.f90
!>
!>
!> \section loc_var Local variables to be added
!>
!> \snippet cs_user_boundary_conditions-base.f90 loc_var_dec
!>
!>
!> \subsection ex_1 Example 1
!>
!> Set a a Dirichlet value on the three components of \f$ \vect{u} \f$
!> on the faces with the selection criterium '2 and x < 0.01' and set a Dirichlet
!> to all the scalars \f$ \varia \f$.
!>
!> Turbulence example computed using equations valid for a pipe.
!>
!> We will be careful to specify a hydraulic diameter adapted
!> to the current inlet.
!>
!> We will also be careful if necessary to use a more precise
!> formula for the dynamic viscosity use in the calculation of
!> the Reynolds number (especially if it is variable, it may be
!> useful to take the law from 'usphyv'. Here, we use by default
!> the 'viscl0" value.
!>
!> Regarding the density, we have access to its value at boundary
!> faces (romb) so this value is the one used here (specifically,
!> it is consistent with the processing in 'usphyv', in case of
!> variable density)
!>
!> \snippet src/user_examples/cs_user_boundary_conditions-base.f90 example_1
!>
!>
!> \subsection ex_2 Example 2
!>
!> Set a a Dirichlet value on the three components of \f$ \vect{u} \f$
!> on the faces with the selection criterium '3' and set a Dirichlet
!> to all the scalars \f$ \varia \f$.
!>
!> Turbulence example computed using turbulence intensity data.
!>
!> We will be careful to specify a hydraulic diameter adapted
!> to the current inlet.
!>
!> Calculation of \f$ k \f$ and \f$ \varepsilon \f$
!> at the inlet (xkent and xeent) using
!> the turbulence intensity and standard laws for a circular pipe
!> (their initialization is not needed here but is good practice)
!>
!> \snippet src/user_examples/cs_user_boundary_conditions-base.f90 example_2
!>
!>
!> \subsection ex_3 Example 3
!>
!> Outlet:
!>  - zero flux for velocity and temperature, prescribed pressure
!>  - Note that the pressure will be set to P0 at the first
!>  - free outlet face (isolib)
!>
!> \snippet src/user_examples/cs_user_boundary_conditions-base.f90 example_3
!>
!>
!> \subsection ex_4 Example 4
!>
!> Wall:
!> - zero flow (zero flux for pressure)
!> - friction for velocities (+ turbulent variables)
!> - zero flux for scalars
!>
!> \snippet src/user_examples/cs_user_boundary_conditions-base.f90 example_4
!>
!>
!> \subsection ex_5 Example 5
!>
!> Assign a rough wall to boundary faces of group '7'
!> Wall:
!> - zero flow (zero flux for pressure)
!> - rough friction for velocities (+ turbulent variables)
!> - zero flux for scalars
!>
!> \snippet src/user_examples/cs_user_boundary_conditions-base.f90 example_5
!>
!>
!> \subsection ex_6 Example 6
!>
!> Assign a symmetry to boundary faces of group '4'
!> \snippet src/user_examples/cs_user_boundary_conditions-base.f90 example_6
!>
!>
!-------------------------------------------------------------------------------

!-------------------------------------------------------------------------------
! 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]     rtp, rtpa     calculated variables at cell centers
!> \param[in]                    (at current and previous time steps)
!> \param[in]     propce        physical properties at cell centers
!> \param[in]     propfa        physical properties at interior face centers
!> \param[in]     propfb        physical properties at boundary face centers
!> \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_user_boundary_conditions &
!=====================================

 ( nvar   , nscal  ,                                              &
   icodcl , itrifb , itypfb , izfppp ,                            &
   dt     , rtp    , rtpa   , propce , propfa , propfb ,          &
   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 ctincl
use elincl
use cs_fuel_incl
use mesh

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

implicit none

! Arguments

integer          nvar   , nscal

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

double precision dt(ncelet), rtp(ncelet,*), rtpa(ncelet,*)
double precision propce(ncelet,*)
double precision propfa(nfac,*), propfb(nfabor,*)
double precision rcodcl(nfabor,nvarcl,3)

! Local variables

!< [loc_var_dec]
integer          ifac, iel, ii, ivar
integer          izone
integer          ilelt, nlelt
double precision uref2, d2s3
double precision rhomoy, xdh, xustar2
double precision xitur
double precision xkent, xeent
double precision Sface, moutlet, mjet, minlet, voutlet,vinlet, vjet, Qvoutlet

integer, allocatable, dimension(:) :: lstelt
!< [loc_var_dec]

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

! BEGIN_C_DECLS
! 
! inline static cs_real_t
! _get_dot_prod(cs_real_t   v1[],
!               cs_real_t   v2[])
! {
!   return (v1[X] * v2[X] + v1[Y] * v2[Y] + v1[Z] * v2[Z]);
! }
! 
! END_C_DECLS

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

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

d2s3 = 2.d0/3.d0

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

! Assign an inlet to boundary faces of group '2' and x < 0.01,

!< [example_1]
! call getfbr('outlet',nlelt,lstelt)
! 
! 
! 
! do ilelt = 1, nlelt
!   ifac = lstelt(ilelt)
!   
! enddo
! 
! do ilelt = 1, nlelt
! 
!   ifac = lstelt(ilelt)
! 
! 
! 
!     itypfb(ifac) = isolib
! ! definition condition entrée en isolib, voir page 71 du user guide. ceci peut être une entrée (vérifier que le débit est bien négatif lors du calcul) les conditions icodcl(ifac,iu(iphas) )  = 9 sont expliquées page 73
! 
! 
!   icodcl(ifac,iu )  = 9
!   icodcl(ifac,iv )  = 9
!   icodcl(ifac,iw )  = 9
! 
! 
! 
! 
! enddo






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



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




! ============================================================================================================================================
! Condition sortie
! ============================================================================================================================================

!   entrée de la valeur de débit massique de sortie en entrant un débit volumique en m3/h a 20°C


Qvoutlet=1000 !Nm3/h

moutlet=1.2*Qvoutlet/3600

! mjet=moutlet*0.01

! minlet=moutlet-mjet



call getfbr('outlet', nlelt, lstelt)

Sface=0

  do ilelt = 1, nlelt
  ifac = lstelt(ilelt) 
  Sface=sqrt(surfbo(1,ifac)**2 + surfbo(2,ifac)**2 + surfbo(3,ifac)**2)+Sface
  enddo
  
  if (irangp.ge.0) then
      call parsom(Sface)
  endif

!   calcul de la vitese constante sur la totalité de la surface calulée en diviant le débit souhaité par la surface totale de la face de sortie
voutlet=(Qvoutlet/3600)/Sface


do ilelt = 1, nlelt

  ifac = lstelt(ilelt)

    itypfb(ifac) = iindef
! definition condition entrée en isolib, voir page 71 du user guide. ceci peut être une entrée (vérifier que le débit est bien négatif lors du calcul) les conditions icodcl(ifac,iu(iphas) )  = 9 sont expliquées page 73
 
  
   do ii = 1, nvar
    icodcl(ifac,ii )  = 3
    rcodcl(ifac,ii,3) = 0.d0
  enddo
 
  icodcl(ifac,iu )  = 1
  rcodcl(ifac,iu,1) = voutlet*surfbo(1,ifac)/surfbn(ifac)


  icodcl(ifac,iv )  = 1
  rcodcl(ifac,iv,1) = voutlet*surfbo(2,ifac)/surfbn(ifac)


  icodcl(ifac,iw )  = 1
  rcodcl(ifac,iw,1) = voutlet*surfbo(3,ifac)/surfbn(ifac)


!   if(nscal.gt.0) then
!         ii = 1
!         icodcl(ifac, isca(ii))= 1
!         rcodcl(ifac, isca(ii), 1)=20.d0
!   endif

enddo



 
! ============================================================================================================================================
! Condition Jet
! ============================================================================================================================================

!  
!  call getfbr('jet', nlelt, lstelt)
! 
! Sface=0
! 
!   do ilelt = 1, nlelt
!   ifac = lstelt(ilelt) 
!   Sface=sqrt(surfbo(1,ifac)**2 + surfbo(2,ifac)**2 + surfbo(3,ifac)**2)+Sface
!   enddo
! 
!   
! 
! !   calcul de la vitese constante sur la totalité de la surface calulée en diviant le débit souhaité par la surface totale de la face de sortie
! vjet=(mjet/1.2)/Sface
! 
! 
! do ilelt = 1, nlelt
! 
!   ifac = lstelt(ilelt)
! 
!     itypfb(ifac) = ientre
! 
!   icodcl(ifac,iu )  = 1
!   rcodcl(ifac,iu,1) = -1*vjet*surfbo(1,ifac)/surfbn(ifac)
! 
! 
!   icodcl(ifac,iv )  = 1
!   rcodcl(ifac,iv,1) = -1*vjet*surfbo(2,ifac)/surfbn(ifac)
! 
! 
!   icodcl(ifac,iw )  =1
!   rcodcl(ifac,iw,1) = -1*vjet*surfbo(3,ifac)/surfbn(ifac)
! 
! 
! 
! 
! 
!   if(nscal.gt.0) then
!         ii = 1
!         icodcl(ifac, isca(ii))= 1
!         rcodcl(ifac, isca(ii), 1)=20.d0
!   endif
! 
! enddo
 
 
 




! ============================================================================================================================================
! Condition entrée
! ============================================================================================================================================



call getfbr('inlet', nlelt, lstelt)

! Sface=0
!
!   do ilelt = 1, nlelt
!   ifac = lstelt(ilelt) 
!   Sface=sqrt(surfbo(1,ifac)**2 + surfbo(2,ifac)**2 + surfbo(3,ifac)**2)+Sface
!   enddo
!   if (irangp.ge.0) then
!       call parsom(Sface)
!       !==========
!   endif
!
!
! !   entrée de la valeur de débit massique de sortie en entrant un débit volumique en m3/h a 20°C
!
!
!   
!
! !   calcul de la vitese constante sur la totalité de la surface calulée en diviant le débit souhaité par la surface totale de la face de sortie
! vinlet=(minlet/1.2)/Sface
!

do ilelt = 1, nlelt

  ifac = lstelt(ilelt)

    itypfb(ifac) = isolib

!  
!    do ii = 1, nvar
!     icodcl(ifac,ii )  = 3
!     rcodcl(ifac,ii,3) = 0.d0
!   enddo
!  
!   icodcl(ifac,iu )  = 1
!   rcodcl(ifac,iu,1) =vinlet*surfbo(1,ifac)/surfbn(ifac)
! 
! 
!   icodcl(ifac,iv )  =1
!   rcodcl(ifac,iv,1) =  vinlet*surfbo(2,ifac)/surfbn(ifac)
! 
! 
!   icodcl(ifac,iw )  = 1
!   rcodcl(ifac,iw,1) = vinlet*surfbo(3,ifac)/surfbn(ifac)


  if(nscal.gt.0) then
        ii = 1
        icodcl(ifac, isca(ii))= 1
        rcodcl(ifac, isca(ii), 1)=20.d0
  endif

enddo
 

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

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

deallocate(lstelt)  ! temporary array for boundary faces selection

return
end subroutine cs_user_boundary_conditions
