!c Description: 
!c   $BMpN.%(%M%k%.!<$rDj>o$H$7$?>l9g$N123H;678?t(B, $B3H;65wN%(B
!c
!c Current Code Owner: 
!c   sugiyama@gfd-dennou.org
!c
!c Copyright (C) SUGIYAMA Ko-ichiro, 2004, All rights reserved

subroutine TurbStab(fs_VelX_B, sf_VelZ_B, ss_Km_B)

  !--- $B%b%8%e!<%k$NFI$_9~$_(B
  use gridset,  only: DimXMin, DimXMax, DimZMin, DimZMax, DelXZ
  use if_avr,   only: ss_avr_fs, ss_avr_sf
  use arareprm, only: Cm
  use if_boundary
  
  !--- $B0EL[$N7?@k8@6X;_(B
  implicit none
  
  !--- $BF~=PNOJQ?t(B 
  real(8), intent(in)     :: fs_VelX_B(DimXMin:DimXMax, DimZMin:DimZMax)
  real(8), intent(in)     :: sf_VelZ_B(DimXMin:DimXMax, DimZMin:DimZMax)
  real(8), intent(out)    :: ss_Km_B(DimXMin:DimXMax, DimZMin:DimZMax)
  
  !--- $BFbItJQ?t(B
  real(8)   :: ss_Energy_B(DimXMin:DimXMax, DimZMin:DimZMax)

  !--- $BMpN.%(%M%k%.!<$N7W;;(B
  ss_Energy_B = &
       & (   ss_avr_fs( fs_VelX_B ** 2.0d0 ) &
       &   + ss_avr_sf( sf_VelZ_B ** 2.0d0 ) ) &
       & * 5.0d-1

  !--- $B12G4@-78?t$r7h$a$k(B($BMpN.%(%M%k%.!<(B E $B$ODj>o$J>l9g(B)  
  ss_Km_B = Cm * DelXZ * sqrt( ss_Energy_B )

  write(*,*) "E  ", maxval( abs( ss_Energy_B ) )
  write(*,*) "Km ", maxval( abs( ss_Km_B ) )
  
end subroutine TurbStab
