!---------------------------------------------------------------------
!     Copyright (C) GFD Dennou Club, 2004. All rights reserved.
!---------------------------------------------------------------------
                                                                 !=begin
!= Subroutine SubGridBuoy
!
!   * Developer: SUGIYAMA Ko-ichiro (sugiyama@gfd-dennou.org)
!   * Version: $Id: subgridbuoy.f90,v 1.1 2005/02/07 10:31:21 sugiyama Exp $ 
!   * Tag Name: $Name:  $
!   * Change History: 
!
!== Overview 
!
!CReSS, Klemp and Wilhelmson (1978) で用いられている 1.5 次の
!クロージャーによって, 浮力によるサブグリッドスケールの
!乱流エネルギー生成を評価する. 
!
!== Error Handling
!
!== Known Bugs
!
!== Note
!
!== Future Plans
!
                                                                 !=end


subroutine SubGridBuoy( ss_PotTemp_n, ss_BuoyKm_n )
                                                                 !=begin  
  !== Dependency 
  use dc_trace,       only: BeginSub, EndSub
  use gridset,        only: DimXMin, DimXMax, DimZMin, DimZMax, DelXZ
  use differentiate,  only: sf_dz_ss 
  use average,        only: ss_avr_sf
  use arareprm,       only: Grav, Cm
  use basicset,       only: ss_PotTempBasicZ

                                                                 !=end  
  !--- 暗黙の型宣言禁止
  implicit none
                                                                 !=begin    
  !== Input 
  real(8), intent(in) :: ss_PotTemp_n(DimXMin:DimXMax, DimZMin:DimZMax) !温位
  
  !== Output
  real(8), intent(out) :: ss_BuoyKm_n(DimXMin:DimXMax, DimZMin:DimZMax) !浮力による乱流エネルギー生成
                                                                 !=end
  call BeginSub("SubGridBuoy", &
&              fmt="%c",       &
&              c1="Calculate subgrid terbulent energy for buoyancy (ss_BuoyKm).")


  !-- 浮力による乱流エネルギー生成
  ss_BuoyKm_n = &
       &    3.0d0 * Grav * ( Cm ** 2.0d0 ) * ( DelXZ ** 2.0d0 ) &
       &       * ss_avr_sf( sf_dz_ss( ss_PotTemp_n ) ) &
       &       / ( 2.0d0 * ss_PotTempBasicZ )

  call EndSub("SubGridBuoy")
  
end subroutine SubGridBuoy
