!---------------------------------------------------------------------
!     Copyright (C) GFD Dennou Club, 2004. All rights reserved.
!---------------------------------------------------------------------
                                                                 !=begin
!= Subroutine AdvectDiffuseX
!
!   * Developer: SUGIYAMA Ko-ichiro
!   * Version: $Id: advectdiffusex.f90,v 1.2.4.1 2006/01/13 06:55:04 sugiyama Exp $ 
!   * Tag Name: $Name:  $
!   * Change History: 
!
!== Overview 
!
!X 方向の運動方程式中に現れる以下の項を
!長い時間ステップ DelTimeLong において評価する. 
!
!   * 移流項
!   * 乱流拡散項
!   * 数値粘性項
!
!== Error Handling
!
!== Known Bugs
!
!== Note
!
!  * 移流項の計算には ***_adv となる変数を利用する. 
!  * 乱流拡散項, 数値粘性項の計算には ***_dif となる変数を利用する. 
!
!== Future Plans
!
                                                                 !=end
subroutine AdvectDiffuseX(                  &
     & fs_VelX_adv, sf_VelZ_adv,            & !移流
     & fs_VelX_dif, sf_VelZ_dif, ss_Km_dif, & !乱流拡散, 数値粘性
     & fs_AdvDiffX_out )
                                                                 !=begin
  !== Dependency
  use dc_trace, only: BeginSub, EndSub
  use gridset,  only: DimXMin, DimXMax, DimZMin, DimZMax, MixLen
  use arareset, only: Cm, NuH, NuV
  use average,  only: fs_avr_ss, ss_avr_fs, ff_avr_ss, fs_avr_ff, ff_avr_sf
  use differentiate_center4, only: ss_dx_fs, fs_dx_ss, fs_dz_ff, ff_dx_sf, &
       &                   ff_dz_fs, fs_dz_ff, ff_dz_fs, fs_dz_ff
                                                                 !=end  
  !== 暗黙の型宣言禁止 
  implicit none
                                                                 !=begin
  !== Input
  real(8), intent(in)  :: fs_VelX_adv(DimXMin:DimXMax, DimZMin:DimZMax)
  real(8), intent(in)  :: sf_VelZ_adv(DimXMin:DimXMax, DimZMin:DimZMax)
  real(8), intent(in)  :: fs_VelX_dif(DimXMin:DimXMax, DimZMin:DimZMax)
  real(8), intent(in)  :: sf_VelZ_dif(DimXMin:DimXMax, DimZMin:DimZMax)
  real(8), intent(in)  :: ss_Km_dif(DimXMin:DimXMax, DimZMin:DimZMax)

  !== Output
  real(8), intent(out) :: fs_AdvDiffX_out(DimXMin:DimXMax, DimZMin:DimZMax)
                                                                 !=end    
  !=== Work
  real(8)  :: fs_AdvVelX(DimXMin:DimXMax, DimZMin:DimZMax)
  real(8)  :: fs_TurbVelX(DimXMin:DimXMax, DimZMin:DimZMax)
  real(8)  :: fs_NumDiffVelX(DimXMin:DimXMax, DimZMin:DimZMax)
  
  call BeginSub("AdvectDiffuseX", &
       &        fmt="%c",    &
       &        c1="calculation of extra force term in x-direction.")


  !=== 移流項
  fs_AdvVelX = &
       &    fs_avr_ss( ss_avr_fs( fs_VelX_adv ) * ss_dx_fs( fs_VelX_adv ) ) &
       & +  fs_avr_ff( ff_avr_sf( sf_VelZ_adv ) * ff_dz_fs( fs_VelX_adv ) )


  !=== 乱流拡散項 (時刻は t-Δt で評価)
  fs_TurbVelX = &
       &   2.0d0 * fs_dx_ss( ss_Km_dif * ss_dx_fs( fs_VelX_dif ) ) &
       & + fs_dz_ff( ff_avr_ss( ss_Km_dif ) * ff_dx_sf( sf_VelZ_dif ) )  &
       & + fs_dz_ff( ff_avr_ss( ss_Km_dif ) * ff_dz_fs( fs_VelX_dif ) )  &
       & - 2.0d0 * fs_dx_ss( ( ss_Km_dif ** 2.0d0 ) ) &
       &   / ( 3.0d0 * ( Cm ** 2.0d0 ) * ( MixLen ** 2.0d0 ) )   
  

  !=== 数値粘性項 (時刻は t-Δt で評価)
  fs_NumDiffVelX = &
       &   NuH * ( fs_dx_ss( ss_dx_fs( fs_VelX_dif) ) ) &
       & + NuV * ( fs_dz_ff( ff_dz_fs( fs_VelX_dif) ) ) 

  !=== 外力項として出力
  fs_AdvDiffX_out = - fs_AdvVelX + fs_TurbVelX + fs_NumDiffVelX 
  
  call EndSub("AdvectDiffuseX")
  
end subroutine AdvectDiffuseX
