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

!                      Code_Saturne version 4.0.2
!                      --------------------------
! 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
integer          ii
integer          ilelt  , nlelt


double precision xfor(3), tfor(3)!, area(1)
double precision, dimension(:,:), pointer :: bfprp_for

double precision xnod(3)
integer, allocatable, dimension(:) :: lstelt
!< [loc_var_dec]

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

!===============================================================================
! Initialization
!===============================================================================
!write (*,*)  "Global_Effors.dat"

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:

! Open file 
! ntcabs: Current absolute time step number
if(ntcabs.eq.1) then
        open(file="Forces.dat",unit=impusr(1))
! write headings to the dat file FX, FY, FZ are the x,y,z efforts
        write(impusr(1),"(5(a,1x))") " TIME STEP  ","  TIME  ","     FX     ","     FY     ","     FZ     "!,"   Area"
        close(unit=impusr(1))
        open(file="Moments.dat",unit=impusr(2))
! write headings to the dat file TX, TY, TZ are the x,y,z efforts
        write(impusr(2),"(5(a,1x))") " TIME STEP  ","  TIME  ","     TX     ","     TY     ","     TZ     "
        close(unit=impusr(2))
endif
! open the file
      open(file="Forces.dat",unit=impusr(1),position="append")
      open(file="Moments.dat",unit=impusr(2),position="append")

if (ineedf.eq.1) then

  do ii = 1, ndim
    xfor(ii) = 0.d0
    tfor(ii) = 0.d0
 enddo
  !  area(1) = 0.d0
  !==========
  call getfbr('Object', nlelt, lstelt)
  !==========

  do ilelt = 1, nlelt

    ifac = lstelt(ilelt)
! Face center
    xnod(1) = cdgfbo(1, ifac)
    xnod(2) = cdgfbo(2, ifac)
    xnod(3) = cdgfbo(3, ifac)
! Boundary Area
 !   area(1) = area(1) + surfbn(ifac)
! Global Efforts
    do ii = 1, ndim
      xfor(ii) = xfor(ii) + bfprp_for(ii, ifac)
    enddo
!Global Moments

    tfor(1)= tfor(1)+ (bfprp_for(3, ifac)* xnod(2) - bfprp_for(2, ifac)*xnod(3))
    tfor(2)= tfor(2)+ (bfprp_for(1, ifac)* xnod(3) - bfprp_for(3, ifac)*xnod(1))
    tfor(3)= tfor(3)+ (bfprp_for(2, ifac)* xnod(1) - bfprp_for(1, ifac)*xnod(2))

  enddo
! Compute the global sums of an array of real numbers in case of parellism.
  if (irangp.ge.0) then
    call parrsm(ndim,xfor)
    !call parrsm(1,area)
    call parrsm(ndim,tfor)
  
! print  efforts for current timestep
    write(impusr(1),"(i12,1x,4(e12.5,1x))") ntcabs,ttcabs,xfor(1),xfor(2),xfor(3)!,area(1)
    write(impusr(2),"(i12,1x,4(e12.5,1x))") ntcabs,ttcabs,tfor(1),tfor(2),tfor(3)
    
! close
    close(unit=impusr(1))
    close(unit=impusr(2))
 
 endif
     
endif
!< [End Global Efforts]

! Deallocate the temporary array
deallocate(lstelt)

return
end subroutine cs_f_user_extra_operations
