!---------------------------------------------------------------------
!     Copyright (C) GFD Dennou Club, 2004. All rights reserved.
!---------------------------------------------------------------------
                                                                 !=begin
!= Subroutine CflCheck
!
!   * Developer: SUGIYAMA Ko-ichiro (sugiyama@gfd-dennou.org)
!   * Version: $Id: cflcheck.f90,v 1.2 2005/04/22 15:01:52 sugiyama Exp $ 
!   * Tag Name: $Name:  $
!   * Change History: 
!
!== Overview 
!
!CFL 条件のチェック. 
!現在は X 方向の格子点間隔から CFL 条件をチェック
!
!== Error Handling
!
!== Known Bugs
!
!== Note
!
!== Future Plans
!
!Error Handling を gt4f90io を利用するように変更
!
                                                                 !=end

subroutine cflcheck
                                                                 !=begin
  !== Dependency
  use dc_trace, only: BeginSub, EndSub
  use dc_message ,only: MessageNotify
  use gridset,  only: DelX
  use timeset,  only: DelTimeShort
  use basicset, only: ss_VelSoundBasicZ
                                                                 !=end
  !--- 暗黙の型宣言禁止
  implicit none
  
  !--- 内部変数
  real(8)                :: ss_VelSoundMax
  real(8)                :: Cfl

  call BeginSub("cflcheck", &
&              fmt="%c", &
&              c1="CFL condition check.")
  
  !--- 音速と CFL 条件を求める
  ss_VelSoundMax = maxval(ss_VelSoundBasicZ)
  Cfl = DelX / ss_VelSoundMax 
  
  !--- 警告メッセージ
  if (DelTimeShort >= Cfl) then 
     write(*,*) "CFL Condition is broken, DelTimeShort * VelSound > DelX"
     write(*,*) "sound wave velocity", ss_VelSoundMax
     write(*,*) "DelX", DelX
     write(*,*) "DelTimeShort", DelTimeShort
     stop
  end if
  
  !--- 警告メッセージ
  if (DelTimeShort >= Cfl * 9.0d-1) then 
     write(*,*) "CFL Condition is nearly broken"
     write(*,*) "sound wave velocity", ss_VelSoundMax
     write(*,*) "DelX", DelX
     write(*,*) "DelTimeShort", DelTimeShort
  end if
  
  !--- 警告メッセージ
  if (ss_VelSoundMax <= 0.0d0) then 
     call MessageNotify("Error", "cflcheck", &
&                  "sound wave velocity <= 0.0d0")
     stop
  end if

  call EndSub("cflcheck")
  
end subroutine cflcheck
