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

!                      Code_Saturne version 5.2.2
!                      --------------------------
! This file is part of Code_Saturne, a general-purpose CFD tool.
!
! Copyright (C) 1998-2018 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_postprocess_var.f90
!> \brief Output additional variables on a postprocessing mesh.
!>
!> Several "automatic" postprocessing meshes may be defined:
!> - The volume mesh (ipart=-1)
!> - The boundary mesh (ipart=-2)
!> - SYRTHES coupling surface (ipart < -2)
!>
!> Additional meshes (cells or faces) may also be defined through the GUI or
!> using the \ref cs_user_postprocess_meshes function from the
!> cs_user_postprocess.c file.
!>
!> This subroutine is called once for each post-processing mesh
!> (with a different value of 'ipart') for each time step at which output
!> on this mesh is active.
!
!-------------------------------------------------------------------------------

!-------------------------------------------------------------------------------
! Arguments
!______________________________________________________________________________.
!  mode           name          role                                           !
!______________________________________________________________________________!
!> \param[in]     ipart         number of the post-processing mesh (< 0 or > 0)
!> \param[in]     nvar          total number of variables
!> \param[in]     nscal         total number of scalars
!> \param[in]     nignor        ignored (set to 0, kept for argument list
!                               compatibility)
!> \param[in]     ncelps        number of cells in post-processing mesh
!> \param[in]     nfacps        number of interior faces in post-process. mesh
!> \param[in]     nfbrps        number of boundary faces in post-process. mesh
!> \param[in]     itypps        global presence flag (0 or 1) for cells (1),
!>                              interior faces (2), or boundary faces (3) in
!>                              post-processing mesh
!> \param[in]     lstcel        list of cells in post-processing mesh
!> \param[in]     lstfac        list of interior faces in post-processing mesh
!> \param[in]     lstfbr        list of boundary faces in post-processing mesh
!_______________________________________________________________________________

subroutine usvpst &
 ( ipart  ,                                                       &
   nvar   , nscal  , nignor ,                                     &
   ncelps , nfacps , nfbrps ,                                     &
   itypps ,                                                       &
   lstcel , lstfac , lstfbr )

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

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

!===============================================================================
! Module files
!===============================================================================

use paramx
use cstnum
use pointe
use entsor
use optcal
use numvar
use parall
use period
use mesh
use field
use post
use cs_c_bindings

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

implicit none

! Arguments

integer          ipart
integer          nvar,   nscal , nignor
integer          ncelps, nfacps, nfbrps

integer          itypps(3)
integer          lstcel(ncelps), lstfac(nfacps), lstfbr(nfbrps)

! Local variables

double precision, dimension(:), pointer :: smomu, smomv, smomw
double precision xx, yy, zz
double precision pas, dpas, rmin, rmax, rr(24)
double precision u_tul(288), u_esp(288), u_pdl(288)
double precision v_tul(288), v_esp(288), v_pdl(288)
double precision w_tul(288), w_esp(288), w_pdl(288)
double precision tul_vol(288), esp_vol(288), pdl_vol(288)
double precision xref, yref, zref
integer          ii, ia, ir, jju, jjv,jjw, kk
integer          iel, ifac, iloc, ivar
integer          imomu, imomv, imomw

!===============================================================================
if (ipart .eq. -1) then
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    ! Moment numbers (choose the moment number from the xml file)
    ! ici le moment pour la vitesse u
    imomu = 1
    imomv = 2
    imomw = 3
    call field_get_val_s(time_moment_field_id(imomu), smomu)
    call field_get_val_s(time_moment_field_id(imomv), smomv)
    call field_get_val_s(time_moment_field_id(imomw), smomw)
 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
! le pas pour un volume de controle
    pas = 0.008d0
   ! demi pas
    dpas = 0.5d0*pas
    rmin = 0.023d0
    rmax = 0.215d0
!    
    do ii = 1, 288
    ! un tableau pour la position y
      rr(ii) = (rmax-rmin)/288*ii+rmin
      tul_vol(ii) = 0.d0
      u_tul(ii)   = 0.d0
      v_tul(ii)   = 0.d0
      w_tul(ii)   = 0.d0
      esp_vol(ii) = 0.d0
      u_esp(ii)   = 0.d0
      v_esp(ii)   = 0.d0
      w_esp(ii)   = 0.d0
      pdl_vol(ii) = 0.d0
      u_pdl(ii)   = 0.d0
      v_pdl(ii)   = 0.d0
      w_pdl(ii)   = 0.d0
    enddo
!
    do iloc = 1, ncelps
      iel = lstcel(iloc)
      xx = xyzcen(1,iel)
      yy = xyzcen(2,iel)
      zz = xyzcen(3,iel)
!
! profil au niveau de la tulipe
      zref=1.26d0
      if (zz.gt.zref-dpas.and.zz.lt.zref+dpas) then
        do ia = 1,12
           do ir =6,24  ! on commence à 6 car les points précédents sont entre la tige et la tulipe et ne nous intéressent donc pas
             xref=rr(ir)*cos(ia*30.)
             yref=rr(ir)*sin(ia*30.)
             if (sqrt((xx-xref)**2 + (yy-yref)**2).lt.dpas) then !cylindre de contrôle
               tul_vol((ia-1)*24 +ir) = tul_vol((ia-1)*24 +ir) + volume(iel)
               u_tul((ia-1)*24 +ir)   = u_tul((ia-1)*24 +ir)   + smomu(iel)*volume(iel)
               v_tul((ia-1)*24 +ir)   = v_tul((ia-1)*24 +ir)   + smomv(iel)*volume(iel)
               w_tul((ia-1)*24 +ir)   = w_tul((ia-1)*24 +ir)   + smomw(iel)*volume(iel)
             endif
           enddo
        enddo
      endif
!
! profil dans l'epace entre la tulipe et la PdL
      zref=1.20d0
      if (zz.gt.zref-dpas.and.zz.lt.zref+dpas) then
        do ia = 1,12
           do ir =1,24
             xref=rr(ir)*cos(ia*30.)
             yref=rr(ir)*sin(ia*30.)
             if (sqrt((xx-xref)**2 + (yy-yref)**2).lt.dpas) then
               esp_vol((ia-1)*24 +ir) = esp_vol((ia-1)*24 +ir) + volume(iel)
               u_esp((ia-1)*24 +ir)   = u_esp((ia-1)*24 +ir)   + smomu(iel)*volume(iel)
               v_esp((ia-1)*24 +ir)   = v_esp((ia-1)*24 +ir)   + smomv(iel)*volume(iel)
               w_esp((ia-1)*24 +ir)   = w_esp((ia-1)*24 +ir)   + smomw(iel)*volume(iel)
             endif
           enddo
        enddo
      endif
!
! profil après la PdL
      zref=1.05d0
      if (zz.gt.zref-dpas.and.zz.lt.zref+dpas) then
        do ia = 1,12
           do ir =1,10   ! Seuls les 10 premiers points se trouvent dans le guide de grappe
             xref=rr(ir)*cos(ia*30.)
             yref=rr(ir)*sin(ia*30.)
             if (sqrt((xx-xref)**2 + (yy-yref)**2).lt.dpas) then
               pdl_vol((ia-1)*24 +ir) = pdl_vol((ia-1)*24 +ir) + volume(iel)
               u_pdl((ia-1)*24 +ir)   = u_pdl((ia-1)*24 +ir)   + smomu(iel)*volume(iel)
               v_pdl((ia-1)*24 +ir)   = v_pdl((ia-1)*24 +ir)   + smomv(iel)*volume(iel)
               w_pdl((ia-1)*24 +ir)   = w_pdl((ia-1)*24 +ir)   + smomw(iel)*volume(iel)
             endif
           enddo
        enddo
      endif
    enddo
!
    if (irangp.ge.0) then
      call parrsm(288, tul_vol)
      call parrsm(288, u_tul)
      call parrsm(288, v_tul)
      call parrsm(288, w_tul)
      call parrsm(288, esp_vol)
      call parrsm(288, u_esp)
      call parrsm(288, v_esp)
      call parrsm(288, w_esp)
      call parrsm(288, pdl_vol)
      call parrsm(288, u_pdl)
      call parrsm(288, v_pdl)
      call parrsm(288, w_pdl)
    endif
!
   ! Eviter les divisions par 0
   do ia=1,12
     do ir=1,24
       if (tul_vol((ia-1)*24 +ir).eq.0) tul_vol((ia-1)*24 +ir)=1.d0
       if (esp_vol((ia-1)*24 +ir).eq.0) esp_vol((ia-1)*24 +ir)=1.d0
       if (pdl_vol((ia-1)*24 +ir).eq.0) pdl_vol((ia-1)*24 +ir)=1.d0
     enddo
   enddo


   if (ntcabs.eq.ntmabs) then
! profil au niveau de la tulipe
    do ir = 6, 24
      write(nfecra,1011) rr(ir), ttcabs, (u_tul((ia-1)*24 +ir)/tul_vol((ia-1)*24 +ir), ia = 1,12)
      write(nfecra,1012) rr(ir), ttcabs, (v_tul((ia-1)*24 +ir)/tul_vol((ia-1)*24 +ir), ia = 1,12)
      write(nfecra,1013) rr(ir), ttcabs, (w_tul((ia-1)*24 +ir)/tul_vol((ia-1)*24 +ir), ia = 1,12)
    enddo
    write(nfecra,*) " "
! profil dans l'espace entre la tulipe et la PdL
    do ir = 1, 24
      write(nfecra,1021) rr(ir), ttcabs, (u_esp((ia-1)*24 +ir)/esp_vol((ia-1)*24 +ir), ia = 1,12)
      write(nfecra,1022) rr(ir), ttcabs, (v_esp((ia-1)*24 +ir)/esp_vol((ia-1)*24 +ir), ia = 1,12)
      write(nfecra,1023) rr(ir), ttcabs, (w_esp((ia-1)*24 +ir)/esp_vol((ia-1)*24 +ir), ia = 1,12)
    enddo
    write(nfecra,*) " "
! profil après la PdL
    do ir = 1, 10
      write(nfecra,1031) rr(ir), ttcabs, (u_pdl((ia-1)*24 +ir)/pdl_vol((ia-1)*24 +ir), ia = 1,12)
      write(nfecra,1032) rr(ir), ttcabs, (v_pdl((ia-1)*24 +ir)/pdl_vol((ia-1)*24 +ir), ia = 1,12)
      write(nfecra,1033) rr(ir), ttcabs, (w_pdl((ia-1)*24 +ir)/pdl_vol((ia-1)*24 +ir), ia = 1,12)
    enddo
    write(nfecra,*) " "
   endif

!
  1011 format(' U_tulipe ',14F14.4)
  1012 format(' V_tulipe ',14F14.4)
  1013 format(' W_tulipe ',14F14.4)
  1021 format(' U_espace ',14F14.4)
  1022 format(' V_espace ',14F14.4)
  1023 format(' W_espace ',14F14.4)
  1031 format(' U_PdL ',14F14.4)
  1032 format(' V_PdL ',14F14.4)
  1033 format(' W_PdL ',14F14.4)

endif



return

end subroutine usvpst
