!---------------------------------------------------------------------
!     Copyright (C) GFD Dennou Club, 2004. All rights reserved.
!---------------------------------------------------------------------
                                                                 !=begin
!= Subroutine BasicType
!
!   * Developer: SUGIYAMA Ko-ichiro (sugiyama@gfd-dennou.org)
!   * Version: $Id: basictype.f90,v 1.2 2005/01/28 12:41:21 sugiyama Exp $ 
!   * Tag Name: $Name:  $
!   * Change History: 
!
!== Overview 
!
!典型的な基本場を計算するためのサブルーチン. 
!タイプとしては以下が設定可能. 
!   * 温度一様 or 温位一様
!   * 圧力一様 or 静水圧平衡
!
!== Error Handling
!
!== Known Bugs
!
!== Note
!
!== Future Plans
!
                                                                 !=end

subroutine BasicType(ss_CpBasicZ, ss_DensBasicZ, &
     & ss_ExnerBasicZ, ss_PotTempBasicZ, ss_VelSoundBasicZ)
                                                                 !=begin
  !== Dependency
  use gridset, only:  DimXMin, DimXMax, DimZMin, DimZMax, s_Z
  use physprm, only:  GasRUniv
  use basicprm, only: Grav, TempSfc, PressSfc, MolWtDry, CpDry, &
       &               TempType, PressType
                                                                 !=end

  !--- 暗黙の型宣言禁止
  implicit none
                                                                 !=begin
  !== Output
  real(8), intent(out) :: ss_CpBasicZ(DimXMin:DimXMax, DimZMin:DimZMax)      !比熱
  real(8), intent(out) :: ss_DensBasicZ(DimXMin:DimXMax, DimZMin:DimZMax)    !密度
  real(8), intent(out) :: ss_ExnerBasicZ(DimXMin:DimXMax, DimZMin:DimZMax)   !無次元圧力
  real(8), intent(out) :: ss_PotTempBasicZ(DimXMin:DimXMax, DimZMin:DimZMax)   !温位
  real(8), intent(out) :: ss_VelSoundBasicZ(DimXMin:DimXMax, DimZMin:DimZMax)!音速
                                                                 !=end
  !--- 内部変数
  real(8)  :: ss_TempBasicZ(DimXMin:DimXMax, DimZMin:DimZMax) ! 平均温度場
  real(8)  :: ss_CvBasicZ(DimXMin:DimXMax, DimZMin:DimZMax)
  real(8)  :: ss_Z(DimXMin:DimXMax, DimZMin:DimZMax)     ! 2-D 座標
  real(8)  :: GasR
  integer  :: k


  !--- 座標を初期化
  do k = DimZMin, DimZMax
     ss_Z(:,k) = s_Z(k)
  end do

  !--------------------------------------------
  !--- 気体定数を決める
  !--------------------------------------------
  GasR = GasRUniv / MolWtDry
  
  
  !--------------------------------------------
  !--- 比熱を決める
  !---   比熱は乾燥大気を念頭に一様とする. 
  !--------------------------------------------
  ss_CpBasicZ = CpDry / MolWtDry
  ss_CvBasicZ = ss_CpBasicZ - GasR


  !--------------------------------------------
  !--- 温度/温位, 無次元圧力の基本場
  !--------------------------------------------
  if (TempType == "uniform" .AND. PressType == "uniform") then 

     !--- 温度場は TempSfc で一様
     ss_TempBasicZ = TempSfc
     
     !--- 地表面圧力で一定. 定義より値は 1 となる. 
     ss_ExnerBasicZ = 1.0d0
     
     !--- 温位
     ss_PotTempBasicZ = ss_TempBasicZ / ss_ExnerBasicZ 
     
  elseif (TempType == "uniform" .AND. PressType == "hydrostatic") then 
     
     !--- 温度場は TempSfc で一様
     ss_TempBasicZ = TempSfc
     
     !--- 無次元圧力と温位を連立して解く. 
     ss_ExnerBasicZ = &
          & dexp( - Grav * ss_Z / ( ss_CpBasicZ * ss_TempBasicZ ) )

     !--- 無次元圧力を利用して温位を計算
     ss_PotTempBasicZ = ss_TempBasicZ / ss_ExnerBasicZ
     
  elseif (TempType == "adiabat" .AND. PressType == "uniform") then 

     !--- 地表面での温位(= TempSfc)で一定
     ss_PotTempBasicZ = TempSfc

     !--- 地表面圧力で一定. 定義より値は 1 となる. 
     ss_ExnerBasicZ = 1.0d0

  elseif (TempType == "adiabat" .AND. PressType == "hydrostatic") then 
     
     !--- 地表面での温位(= TempSfc)で一定
     ss_PotTempBasicZ = TempSfc

     !--- 無次元圧力を計算. 温位一定
     ss_ExnerBasicZ = &
          & - Grav * ss_Z / ( ss_CpBasicZ * ss_PotTempBasicZ ) + 1.0d0

  else
     write(*,*) "unknown TempType, or PressType"
     write(*,*) TempType, PressType
        
  end if

    
  !--------------------------------------------
  !---- 密度
  !--------------------------------------------
  ss_DensBasicZ = &
       & PressSfc * ( ss_ExnerBasicZ ** (ss_CvBasicZ / GasR) ) &
       &   / ( GasR * ss_PotTempBasicZ )
  
  
  !--------------------------------------------    
  !--- 平均場の音速
  !--------------------------------------------
  ss_VelSoundBasicZ = &
       & ss_CpBasicZ * GasR * ss_ExnerBasicZ * ss_PotTempBasicZ &
       &   / ss_CvBasicZ  
  ss_VelSoundBasicZ = sqrt( ss_VelSoundBasicZ )
  
  
end subroutine BasicType

