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

!                      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_extra_operations.f90
!>
!> \brief This function is called at the end of each time step, and has a very
!>  general purpose
!>  (i.e. anything that does not have another dedicated user subroutine)
!>
!> See \subpage cs_user_extra_operations_examples and
!> \subpage cs_user_extra_operations-nusselt_calculation 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_f_user_extra_operations &
 ( nvar   , nscal  ,                                              &
   dt     )

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

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

use paramx
use dimens, only: ndimfb
use pointe
use numvar
use optcal
use cstphy
use cstnum
use entsor
use lagran
use parall
use period
use ppppar
use ppthch
use ppincl
use mesh
use field
use field_operator ! Give access to cell gradient
use turbomachinery
use cs_c_bindings

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

implicit none

! Arguments

integer          nvar   , nscal

double precision dt(ncelet)

! Local variables

! INSERT_VARIABLE_DEFINITIONS_HERE

integer, allocatable, dimension(:) :: lstelt


!integer impout

integer ilelt, nlelt, ifac, iel
integer use_previous_t, inc, recompute_cocg ! For gradient calculation

double precision, pointer, dimension(:) :: spg, cpro_prtot

double precision, allocatable, dimension(:,:) :: grad_p

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


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

allocate(lstelt(nfabor))  ! temporary array for boundary faces selection
allocate(grad_p(3,ncelet)) ! Allocate array for pressure gradient

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

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

!	Calculate the pressure gradient

  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(ivarfl(iprtot), use_previous_t,            &
                             imrgra, inc, recompute_cocg, grad_p)
!-------------------------------------------------------------------------------

call field_get_val_s_by_name("SPG", spg)

do iel = 1, ncelet

spg(iel) = sqrt(grad_p(1,iel)**2.d0                    &
               +grad_p(2,iel)**2.d0                    &
               +grad_p(3,iel)**2.d0)
enddo

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


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

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

deallocate(lstelt)  ! temporary array for boundary faces selection

deallocate(grad_p)  ! Deallocate array for pressure gradient


return
end subroutine cs_f_user_extra_operations
