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

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

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

implicit none

! Arguments

integer          nvar   , nscal

double precision dt(ncelet)

! Local variables

!< [loc_var_dec]
integer          ifac
integer          ii
integer          ilelt  , nlelt

integer          ioutput
!double precision filenumber

double precision xfor(3)
double precision a1, a2
double precision, dimension(:,:), pointer :: bfprp_for

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

!===============================================================================
! Example: compute global efforts on a subset of faces
!===============================================================================

! If efforts have been calculated correctly:

if (ineedf.eq.1) then

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

  call getfbr('AIRFOIL', 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
  
  
  if (irangp.le.0) then
    write(nfecra,*)xfor(1),xfor(2),xfor(3)
  endif
  
!Output Forces
  if (irangp.le.0) then

    write(impusr(1),*)ntcabs,xfor(1),xfor(2),xfor(3)

  endif
 
endif



if (ineedf.eq.1) then

  do ioutput = 2, 3      !!!!!!!!!!!!!change1:3!!!!!!!!!!!!!!!!

      do ii = 1, ndim
        xfor(ii) = 0.d0
      enddo
  
      a1 = 0.1d0 * dble(ioutput-2) / 2.0d0
      a2 = 0.1d0 * dble(ioutput-1) / 2.0d0

      write(nfecra,*) a1, a2
      call getfbr('AIRFOIL and z > a1 and z < a2', nlelt, lstelt)           !!!!!!!!!!!!!change2:2.0!!!!!!!!!!!!!!
  !==========

      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
        if (irangp.le.0) then

        write(impusr(ioutput),*)ntcabs,xfor(1),xfor(2),xfor(3)

        endif
  
  enddo
 
endif


! Deallocate the temporary array
deallocate(lstelt)

return
end subroutine cs_f_user_extra_operations
