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

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

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

! INSERT_VARIABLE_DEFINITIONS_HERE
double precision xfluxf_inlet, xfluxf_outlet
double precision xfluxf_vol_inlet, xfluxf_vol_outlet
double precision flumab
integer          ifacx,  ileltx  , nleltx, iflmax, keylox

integer, allocatable, dimension(:) :: lstelt
double precision, dimension(:), pointer :: bmasfl
double precision, dimension(:), pointer :: brom
logical lopen

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

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

! INSERT_ADDITIONAL_INITIALIZATION_CODE_HERE

!===============================================================================
! User operations
!===============================================================================

! INSERT_MAIN_CODE_HERE

  if (irangp.le.0) then
    inquire(file='Mass_flow.dat', opened=lopen)
    if(.NOT.lopen) open(10,file='Mass_flow.dat')
    inquire(file='Volumic_flow.dat', opened=lopen)
    if(.NOT.lopen) open(11,file='Volumic_flow.dat')
  endif

  allocate(lstelt(max(ncel,nfac,nfabor)))

  call field_get_key_int(ivarfl(iu), kbmasf, iflmax) ! boundary mass flux
  call field_get_val_s(iflmax, bmasfl)

  call field_get_key_id('log', keylox)
  call field_set_key_int(ibrom, keylox, 1)
  call field_get_val_s(ibrom, brom)

  xfluxf_inlet = 0.d0
  xfluxf_outlet = 0.d0
  xfluxf_vol_inlet = 0.d0
  xfluxf_vol_outlet = 0.d0

  call getfbr('inlet', nleltx, lstelt)
  !==========

  do ileltx = 1, nleltx
    ifacx = lstelt(ileltx)
    flumab = bmasfl(ifacx)
    xfluxf_inlet = xfluxf_inlet+flumab
    flumab = bmasfl(ifacx)/brom(ifacx)
    xfluxf_vol_inlet = xfluxf_vol_inlet+flumab
  enddo

  call getfbr('outlet', nleltx, lstelt)
  !==========

  do ileltx = 1, nleltx
    ifacx = lstelt(ileltx)
    flumab = bmasfl(ifacx)
    xfluxf_outlet = xfluxf_outlet+flumab
    flumab = bmasfl(ifacx)/brom(ifacx)
    xfluxf_vol_outlet = xfluxf_vol_outlet+flumab
  enddo

  if (irangp.ge.0) then
    call parsom(xfluxf_inlet)
    call parsom(xfluxf_outlet)

    call parsom(xfluxf_vol_inlet)
    call parsom(xfluxf_vol_outlet)
  endif

  if (irangp.le.0) then
  write (10, '(i6, 4e12.4)')                                               &
    ntcabs, xfluxf_inlet, xfluxf_outlet
  write (11, '(i6, 4e12.4)')                                               &
    ntcabs, xfluxf_vol_inlet, xfluxf_vol_outlet
  endif

  write (nfecra, 2000)                                               &
    ntcabs, xfluxf_inlet, xfluxf_outlet

  write (nfecra, 2001)                                               &
    ntcabs, xfluxf_vol_inlet, xfluxf_vol_outlet

2000 format                                                           &
  (/,                                                                 &
   3X,'** Mass flow **', /,                                     &
   3X,'   ---------------', /,                                        &
   '---', '------',                                                   &
   '------------------------------------------------------------', /, &
   '  Iter',                                                    &
   ' inlet outlet', /,      &
   i6, 4e12.4, /,                                             &
   '---','------',                                                    &
   '------------------------------------------------------------')

2001 format                                                           &
  (/,                                                                 &
   3X,'** Volumic flow **', /,                                     &
   3X,'   ---------------', /,                                        &
   '---', '------',                                                   &
   '------------------------------------------------------------', /, &
   '  Iter',                                                    &
   ' inlet outlet', /,      &
   i6, 4e12.4, /,                                             &
   '---','------',                                                    &
   '------------------------------------------------------------')

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

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


return
end subroutine cs_f_user_extra_operations
