!===============================================================================
! User source terms definition.
!
! 1) Momentum equation (segegrated solver)
! 2) Momentum equation (coupled solver)
! 3) Species transport
! 4) Turbulence (k-epsilon, k-omega, Rij-epsilon, v2-f, Spalart-Allmaras)
!===============================================================================

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

!VERS

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

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

subroutine ustsnv &
!================

 ( nvar   , nscal  , ncepdp , ncesmp ,                            &
   ivar   ,                                                       &
   icepdc , icetsm , itypsm ,                                     &
   dt     ,                                                       &
   ckupdc , smacel ,                                              &
   crvexp , crvimp )

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

use paramx
use numvar
use entsor
use optcal
use cstphy
use parall
use period
use mesh
use field

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

implicit none

! Arguments

integer          nvar   , nscal
integer          ncepdp , ncesmp
integer          ivar

integer          icepdc(ncepdp)
integer          icetsm(ncesmp), itypsm(ncesmp,nvar)

double precision dt(ncelet)
double precision ckupdc(ncepdp,6), smacel(ncesmp,nvar)
double precision crvexp(3,ncelet), crvimp(3,3,ncelet)

! Local variables

character*80     chaine
integer          iel
double precision ckp, qdm

!===============================================================================
! 1. Initialization
!===============================================================================

if (iwarni(ivar).ge.1) then
  call field_get_label(ivarfl(ivar), chaine)
  write(nfecra,1000) chaine(1:8)
endif

qdm  = 1.d0

do iel = 1, ncel
  crvexp(1,iel) =   volume(iel)*qdm
enddo

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

 1000 format(' User source termes for variable ',A8,/)

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

return
end subroutine ustsnv

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

subroutine ustssc &
!================

 ( nvar   , nscal  , ncepdp , ncesmp ,                            &
   iscal  ,                                                       &
   icepdc , icetsm , itypsm ,                                     &
   dt     ,                                                       &
   ckupdc , smacel ,                                              &
   crvexp , crvimp )

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

use paramx
use numvar
use entsor
use optcal
use cstphy
use parall
use period
use mesh
use field

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

implicit none

! Arguments

integer          nvar   , nscal
integer          ncepdp , ncesmp
integer          iscal

integer          icepdc(ncepdp)
integer          icetsm(ncesmp), itypsm(ncesmp,nvar)

double precision dt(ncelet)
double precision ckupdc(ncepdp,6), smacel(ncesmp,nvar)
double precision crvexp(ncelet), crvimp(ncelet)

! Local variables

character*80     chaine
integer          ivar, iel
integer          ilelt, nlelt, ifac, iflmas

double precision tauf, prodf, volf, pwatt
double precision surf, xx, ubulk

double precision, dimension(:), pointer :: imasfl
double precision, dimension(:,:), pointer :: cvar_vel

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

if (iscal.ne.iscalt) return

!===============================================================================
! 1. Initialization
!===============================================================================

! --- Index number of the variable associated to scalar iscal
ivar = isca(iscal)

! --- Name of the the variable associated to scalar iscal
call field_get_label(ivarfl(ivar), chaine)

if (iwarni(ivar).ge.1) then
  write(nfecra,1000) chaine(1:8)
endif

ubulk = 0.d0
surf  = 0.d0

call field_get_key_int(ivarfl(iu), kimasf, iflmas)
call field_get_val_s(iflmas, imasfl)
call field_get_val_v(ivarfl(iu), cvar_vel)

do ifac = 1, nfac
   xx = cdgfac(1,ifac)
   if (abs(xx).lt.1.d-5) then
      ubulk = ubulk + imasfl(ifac)
      surf  = surf + surfac(1,ifac)
   endif
enddo

if (irangp.ge.0) then
   call parsom(ubulk)
   call parsom(surf)
endif

ubulk = abs(ubulk)/abs(surf)
ubulk = max(ubulk,1d-12)

do iel = 1, ncel
   crvexp(iel) = volume(iel)*cvar_vel(1,iel)/ubulk
enddo

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

 1000 format(' User source terms for variable ',A8,/)

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

return
end subroutine ustssc

