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

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

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

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

!> \file cs_user_initialization-spg.f90
!>
!> \brief Compute streamwise pressure gradient
!>
!> See \subpage cs_user_initialization for examples.
!>
!
!-------------------------------------------------------------------------------

!-------------------------------------------------------------------------------
! Arguments
!______________________________________________________________________________.
!  mode           name          role                                           !
!______________________________________________________________________________!
!> \param[in]     nvar          total number of variables
!> \param[in]     nscal         total number of scalars
!> \param[in]     dt            time step (per cell)
!_______________________________________________________________________________


subroutine cs_user_f_initialization &
 ( nvar   , nscal  ,                                              &
   dt     )

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

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

use paramx
use pointe
use numvar
use optcal
use cstphy
use cstnum
use entsor
use parall
use period
use ppppar
use ppthch
use coincl
use cpincl
use ppincl
use atincl
use ctincl
use ppcpfu
use cs_coal_incl
use cs_fuel_incl
use mesh
use field
use field_operator ! Give access to cell gradient

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

implicit none

! Arguments

integer          nvar, nscal
double precision dt(ncelet)

! Local variables

!< [loc_var_dec]
integer ilelt, nlelt, ifac
integer f_id_p, use_previous_t, inc, recompute_cocg ! For gradient calculation
integer, allocatable, dimension(:) :: lstelt


double precision, dimension(:,:), pointer :: cvar_vel, uvdf

double precision, allocatable, dimension(:) :: mag_vel, spg
double precision, allocatable, dimension(:,:) :: grad_p
!< [loc_var_dec]


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

!< [alloc]
allocate(lstelt(nfabor)) ! temporary array for cells selection
allocate(grad_p(3,ncel)) ! Allocate array for pressure gradient

allocate(mag_vel(ncel)) ! Allocate array for velocity gradient
allocate(uvdf(3,ncel)) ! Allocate array for unit vector in flow direction
allocate(spg(ncel)) ! Allocate array for streamwise pressure gradient
!< [alloc]


!< [init]
!-------------------------------------------------------------------------------
!	Calculate the pressure gradient
	!> \brief  Compute cell gradient of scalar field or component of vector or
	!>         tensor field.

	!> \param[in]   f_id             field id
	!> \param[in]   use_previous_t   1 if values at previous time step should
	!>                               be used, 0 otherwise
	!> \param[in]   imrgra           gradient computation mode: 
	!																 			0 iterative gradient; 
	!																 			1 least square gradient
	!> \param[in]   inc              0: increment; 1: do not increment
	!> \param[in]   recompute_cocg   1 or 0: recompute COCG or not
	!> \param[out]  grad             gradient
  
  f_id_p = ivarfl(ipr) ! Field id for pressure variable
  use_previous_t = 0 ! Values at previous time step are not used
  imrgra = 1 ! Least square gradient
  inc = 1 ! Do not increment
  recompute_cocg = 0 ! Do not recompute COCG
  
  call field_gradient_scalar (f_id_p, use_previous_t,               &
                              imrgra, inc, recompute_cocg, grad_p)
!-------------------------------------------------------------------------------


!-------------------------------------------------------------------------------
! ! Return pointer to the values array of a given scalar field.
! ! subroutine 	field_get_val_s (field_id, p)
    ! ! [in]	field_id	id of given field (which must be scalar)
    ! ! [out]	p	pointer to scalar field values

! Access the velocity field
  call field_get_val_v(ivarfl(iu), cvar_vel)
!-------------------------------------------------------------------------------

!-------------------------------------------------------------------------------	
! Build the list of boundary faces matching a criteria string.
! subroutine 	getfbr (fstr, facnb, faces)
    ! [in]	fstr	criteria string
    ! [out]	facnb	number of selected faces
    ! [out]	faces	selected faces

  call getfbr('PLATE', nlelt, lstelt)
!-------------------------------------------------------------------------------

!-------------------------------------------------------------------------------
! Calculate the streamwise pressure gradient
  do ilelt = 1, nlelt
    ! Access the boundary faces index
    ifac = lstelt(ilelt)

		! Calculate the magnitude (norm) of velocity vector
    mag_vel(ifac) = sqrt(cvar_vel(1,ifac)**2.d0                      &
                        +cvar_vel(2,ifac)**2.d0                      &
                        +cvar_vel(3,ifac)**2.d0)

    ! Calculate the unit vector in the direction of the flow
    uvdf(1,ifac) = cvar_vel(1,ifac)/mag_vel(ifac)
    uvdf(2,ifac) = cvar_vel(2,ifac)/mag_vel(ifac)
    uvdf(3,ifac) = cvar_vel(3,ifac)/mag_vel(ifac)

    ! Calculate the streamwise pressure gradient
    spg(ifac) = (grad_p(1,ifac)*uvdf(1,ifac)                     &
                +grad_p(2,ifac)*uvdf(2,ifac)                     &
                +grad_p(3,ifac)*uvdf(3,ifac))                    &
                                             /mag_vel(ifac)
  enddo
!-------------------------------------------------------------------------------

!< [init]

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

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

!< [finalize]
deallocate(lstelt) ! Deallocate array for cells selection
deallocate(grad_p)  ! Deallocate array for pressure gradient

deallocate(mag_vel) ! Deallocate array for velocity gradient
!< [finalize]

return
end subroutine cs_user_f_initialization
