!---------------------------------------------------------------------
!     Copyright (C) GFD Dennou Club, 2004. All rights reserved.
!---------------------------------------------------------------------
                                                                 !=begin
!= Subroutine TurbulenceScalar
!
!   * Developer: SUGIYAMA Ko-ichiro (sugiyama@gfd-dennou.org)
!   * Version: $Id: turbulencescalar.f90,v 1.5 2005/02/02 10:19:17 odakker Exp $ 
!   * Tag Name: $Name:  $
!   * Change History: 
!
!== Overview 
!
!スカラー量に対する乱流拡散項の評価
!
!== Error Handling
!
!== Known Bugs
!
!== Note
!
!== Future Plans
!
                                                                 !=end

subroutine TurbulenceScalar(ss_PotTemp_n, ss_Kh_n, ss_TurbScalar_n)
                                                                 !=begin
  !== Dependency
  use dc_trace, only: BeginSub, EndSub
  use dc_message, only: MessageNotify
  use gridset,   only: DimXMin, DimXMax, DimZMin, DimZMax
  use average,   only: sf_avr_ss, fs_avr_ss, ss_avr_sf, ss_avr_fs
  use differentiate,  only: sf_dz_ss, fs_dx_ss, ss_dz_sf, ss_dx_fs
                                                                 !=end  
  !--- 暗黙の型宣言禁止
  implicit none
                                                                 !=begin
  !== Input
  real(8), intent(in)   :: ss_PotTemp_n(DimXMin:DimXMax, DimZMin:DimZMax)
  real(8), intent(in)   :: ss_Kh_n(DimXMin:DimXMax, DimZMin:DimZMax)
  
  !== Output
  real(8), intent(out)  :: ss_TurbScalar_n(DimXMin:DimXMax, DimZMin:DimZMax) 
                                                                 !=end

  call BeginSub("TurbulenceScalar", &
&          fmt="%c",       &
&          c1="Calculate turbulent diffusion term for scalar (ss_TurbScalar).")

  !--- スカラー量の粘性拡散
  ss_TurbScalar_n = &
       & - ss_dx_fs( fs_avr_ss(ss_Kh_n) * fs_dx_ss( ss_PotTemp_n ) ) &
       & - ss_dz_sf( sf_avr_ss(ss_Kh_n) * sf_dz_ss( ss_PotTemp_n ) ) 
  
  write(*,*) "Dt ", maxval( abs( ss_TurbScalar_n ) )

  if ( maxval( abs( ss_TurbScalar_n ) ) > 1.0d1 ) then 
    call MessageNotify("Error", "TurbulenceScalar", &
&                      "Dt is over 10.0 ")
  end if

  call EndSub("TurbulenceScalar")
  
end subroutine TurbulenceScalar
