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

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

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

implicit none

! Arguments

integer          nvar   , nscal

double precision dt(ncelet)

! Local variables

! INSERT_VARIABLE_DEFINITIONS_HERE
integer iel, ii, ifac
integer ilelt , nlelt
integer, allocatable, dimension(:) :: lstelt
double precision cyl_area, radius, dia, depth
double precision xcof(3), torque, tcof
double precision xfor(3)
double precision, dimension(:,:), pointer :: bfprp_for

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

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

! INSERT_ADDITIONAL_INITIALIZATION_CODE_HERE
allocate(lstelt(max(ncel,nfac,nfabor)))
call field_get_val_v(iforbr, bfprp_for)

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

! INSERT_MAIN_CODE_HERE
do ii = 1, ndim
xfor(ii) = 0.d0
torque = 0.d0
xcof(ii) = 0.d0
tcof = 0.d0
enddo
dia = 0.246
radius = dia/2.d0
depth = 0.5
call getfbr("wall1", nlelt, lstelt)
do ilelt = 1, nlelt
ifac = lstelt(ilelt)
do ii = 1, ndim
xfor(ii) = xfor(ii) + bfprp_for(ii, ifac)
enddo
torque = xfor(2)*cdgfbo(1,ifac) - xfor(1)*cdgfbo(2,ifac)
enddo

if (irangp >= 0) then
call parrsm(ndim,xfor)
call parsom(torque)
endif

cyl_area = depth*dia
do ii=1,ndim
xcof(ii) = xfor(ii) /(0.5d0*ro0*uref**2*cyl_area)
enddo
tcof = torque/(0.5d0*ro0*uref**2*cyl_area*radius)




if (irangp == 0) then
if (ntcabs == ntpabs+1) then
open(unit=impusr(1), file="coefficients.dat")
write(impusr(1),*) "#File with Cl and Cd"
write(impusr(1),*) "#R", radius
write(impusr(1),*) "#Uinf ", uref
write(impusr(1),*) "#rho ", ro0
write(impusr(1),*) "#Front area ", cyl_area
write(impusr(1),*) "#Structure of the results:"
write(impusr(1),*) "#Col 1: Time"
write(impusr(1),*) "#Col 2-3: Force in x and y"
write(impusr(1),*) "#Col 4: Torque"
write(impusr(1),*) "#Col 5: Cd"
write(impusr(1),*) "#Col 6: Cl"
write(impusr(1),*) "#Col 7: Ct"
endif
write(impusr(1),1001)ttcabs,xfor(1),xfor(2), &
torque,xcof(1),xcof(2),tcof

if (ntcabs == ntmabs) close(impusr(1))
endif
1001 format(7(e18.9,1x))

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

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


return
end subroutine cs_f_user_extra_operations
