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

!                      Code_Saturne version 3.2.1
!                      --------------------------
! 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.

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

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

subroutine cs_user_extra_operations &
 ( nvar   , nscal  ,                                              &
   dt     , rtpa   , rtp    , propce )

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

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

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

implicit none

! Arguments

integer          nvar   , nscal

double precision dt(ncelet), rtp(ncelet,*), rtpa(ncelet,*)
double precision propce(ncelet,*)

! Local variables
double precision xfluxf_inlet_1, xfluxf_inlet_2, xfluxf_outlet_1, xfluxf_outlet_2
double precision xfluxf_vol_inlet_1, xfluxf_vol_inlet_2, xfluxf_vol_outlet_1, xfluxf_vol_outlet_2
double precision flumab
integer          ifac,  ilelt  , nlelt, iflmab, keylog

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


  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, iflmab) ! boundary mass flux
  call field_get_val_s(iflmab, bmasfl)

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

  xfluxf_inlet_1 = 0.d0
  xfluxf_inlet_2 = 0.d0
  xfluxf_outlet_1 = 0.d0
  xfluxf_outlet_2 = 0.d0

  xfluxf_vol_inlet_1 = 0.d0
  xfluxf_vol_inlet_2 = 0.d0
  xfluxf_vol_outlet_1 = 0.d0
  xfluxf_vol_outlet_2 = 0.d0

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

  do ilelt = 1, nlelt

    ifac = lstelt(ilelt)

    flumab = bmasfl(ifac)

    xfluxf_inlet_1 = xfluxf_inlet_1+flumab

    flumab = bmasfl(ifac)/brom(ifac)

    xfluxf_vol_inlet_1 = xfluxf_vol_inlet_1+flumab

  enddo

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

  do ilelt = 1, nlelt

    ifac = lstelt(ilelt)

    flumab = bmasfl(ifac)

    xfluxf_inlet_2 = xfluxf_inlet_2+flumab

    flumab = bmasfl(ifac)/brom(ifac)

    xfluxf_vol_inlet_2 = xfluxf_vol_inlet_2+flumab

  enddo

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

  do ilelt = 1, nlelt

    ifac = lstelt(ilelt)

    flumab = bmasfl(ifac)

    xfluxf_outlet_1 = xfluxf_outlet_1+flumab

    flumab = bmasfl(ifac)/brom(ifac)

    xfluxf_vol_outlet_1 = xfluxf_vol_outlet_1+flumab

  enddo

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

  do ilelt = 1, nlelt

    ifac = lstelt(ilelt)

    flumab = bmasfl(ifac)

    xfluxf_outlet_2 = xfluxf_outlet_2+flumab

    flumab = bmasfl(ifac)/brom(ifac)

    xfluxf_vol_outlet_2 = xfluxf_vol_outlet_2+flumab

  enddo

  if (irangp.ge.0) then
    call parsom(xfluxf_inlet_1)
    call parsom(xfluxf_inlet_2)
    call parsom(xfluxf_outlet_1)
    call parsom(xfluxf_outlet_2)

    call parsom(xfluxf_vol_inlet_1)
    call parsom(xfluxf_vol_inlet_2)
    call parsom(xfluxf_vol_outlet_1)
    call parsom(xfluxf_vol_outlet_2)
  endif

  if (irangp.le.0) then
  write (10, '(i6, 4e12.4)')                                               &
    ntcabs, xfluxf_inlet_1, xfluxf_inlet_2, xfluxf_outlet_1, xfluxf_outlet_2
  write (11, '(i6, 4e12.4)')                                               &
    ntcabs, xfluxf_vol_inlet_1, xfluxf_vol_inlet_2, xfluxf_vol_outlet_1, xfluxf_vol_outlet_2
  endif

  write (nfecra, 2000)                                               &
    ntcabs, xfluxf_inlet_1, xfluxf_inlet_2, xfluxf_outlet_1, xfluxf_outlet_2

  write (nfecra, 2001)                                               &
    ntcabs, xfluxf_vol_inlet_1, xfluxf_vol_inlet_2, xfluxf_vol_outlet_1, xfluxf_vol_outlet_2

2000 format                                                           &
  (/,                                                                 &
   3X,'** Mass flow **', /,                                     &
   3X,'   ---------------', /,                                        &
   '---', '------',                                                   &
   '------------------------------------------------------------', /, &
   '  Iter',                                                    &
   ' In_Fumees In_Air Out_Air_Fumees Out_Fumees', /,      &
   i6, 4e12.4, /,                                             &
   '---','------',                                                    &
   '------------------------------------------------------------')

2001 format                                                           &
  (/,                                                                 &
   3X,'** Volumic flow **', /,                                     &
   3X,'   ---------------', /,                                        &
   '---', '------',                                                   &
   '------------------------------------------------------------', /, &
   '  Iter',                                                    &
   ' In_Fumees In_Air Out_Air_Fumees Out_Fumees', /,      &
   i6, 4e12.4, /,                                             &
   '---','------',                                                    &
   '------------------------------------------------------------')


return
end subroutine cs_user_extra_operations
