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

!                      Code_Saturne version 4.0.4
!                      --------------------------
! This file is part of Code_Saturne, a general-purpose CFD tool.
!
! Copyright (C) 1998-2015 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-global_efforts.f90
! This is an example of cs_user_extra_operations.f90 which
! performs global efforts

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

!-------------------------------------------------------------------------------
! 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 lagpar
use lagran
use lagdim
use parall
use period
use ppppar
use ppthch
use ppincl
use mesh
use field
use field_operator
use turbomachinery

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

implicit none

! Arguments

integer          nvar   , nscal

double precision dt(ncelet)

! Local variables

!< [loc_var_dec]
integer          ifac , ivar , iel
integer          ii
integer          ilelt, nlelt, iflmas, iflmab

double precision xfor(3)
double precision massin , massout 
double precision massjin , massjout
double precision flumab, flumas

double precision, dimension(:,:), pointer :: bfprp_for
double precision, dimension(:), pointer :: imasfl, bmasfl


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

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

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

if (ineedf.eq.1) call field_get_val_v(iforbr, bfprp_for)

! Allocate a temporary array for cells or interior/boundary faces selection
allocate(lstelt(max(ncel,nfac,nfabor)))


!===============================================================================
! Compute global efforts on a subset of faces
!===============================================================================

! If efforts have been calculated correctly:
if (ineedf.eq.1) then
   open(file='forces.dat',unit= impusr(1))
! write heading in .dat file
   write(impusr(1), * )'TIME-STEP','   FX    ','   FY    ','   FZ    '
   
   !close(impusr(1))
endif

!Efforts sur le paroi inferieur
if (ineedf.eq.1) then

  do ii = 1, ndim
    xfor(ii) = 0.d0
  enddo

  call getfbr('bottom', nlelt, lstelt)
  !==========

  do ilelt = 1, nlelt

    ifac = lstelt(ilelt)

    do ii = 1, ndim
      xfor(ii) = xfor(ii) + bfprp_for(ii, ifac)
    enddo

  enddo

  if (irangp.ge.0) then
    call parrsm(ndim,xfor)
  endif

!=======Output Forces, ecriture des forces dans un fichier======
     write(impusr(1),*)ntcabs,xfor(1),xfor(2),xfor(3)
     close(impusr(1))
endif

   
!==================================================================================
!Compute global mass fluw rate on faces
!==================================================================================
!=======creer output fichier debit=======
if (ineedf.eq.1) then
   open(file='debit.dat',unit= impusr(2))
! write heading in .dat file
   write(impusr(2), * )'TIME-STEP','   MASSIN   ','   MASSOUT    ','   MASSJIN    ','   MASSJOUT   '
   
   !close(impusr(2))
endif



!recupere la valeur du debit massique
  call field_get_key_int(ivarfl(ivar), kimasf, iflmas)
  call field_get_key_int(ivarfl(ivar), kbmasf, iflmab)
  call field_get_val_s(iflmas, imasfl)
  call field_get_val_s(iflmab, bmasfl)

if(ineedf.eq.1) then 
!initialisation des variables
  
    massin   = 0.0d0
    massout  = 0.0d0
    massjin  = 0.0d0
    massjout = 0.0d0
  
! appelle de la section inlet
  call getfbr('inlet', nlelt, lstelt) 

!boucle sur la surface 
  do ilelt = 1, nlelt 
! temp value for surface identity
    ifac = lstelt(ilelt) 
    iel  = ifabor(ifac)

    flumab = bmasfl(ifac)
! somme la valeur de flux massique de chaque surface 
    massin = massin + flumab 
  enddo

!appelle de la section outlet
  call getfbr('outlet', nlelt,lstelt) 

  do ilelt = 1, nlelt
     ifac = lstelt(ilelt)

     flumab = bmasfl(ifac)
     massout = massout + flumab
  enddo
!appelle une section à z=0
  call getfac('normal[0, 0, 1, 0.0001]', nlelt, lstelt) 
 
  do ilelt = 1, nlelt
     ifac = lstelt(ilelt)

     flumas = imasfl(ifac)
     massjin = massjin + flumas

  enddo
  
  call getfac('normal[0, 0.0075, 0.07073, 0.0001]' , nlelt, lstelt)
 
  do ilelt = 1, nlelt
     ifac = lstelt(ilelt)

     flumas = imasfl(ifac)
     massjout = massjout + flumas
  enddo

  if (irangp.ge.0) then
     call parsom(massin)
     call parsom(massout)
     call parsom(massjin)
     call parsom(massjout)
  endif
  
!=======Output Forces, ecriture des forces dans un fichier======
     write(impusr(2),*)ntcabs, massin, massout, massjin, massjout
     close(impusr(2))

endif
! Deallocate the temporary array
deallocate(lstelt)

return
end subroutine cs_f_user_extra_operations
