!---------------------------------------------------------------------
!     Copyright (C) GFD Dennou Club, 2005. All rights reserved.
!---------------------------------------------------------------------
                                                                 !=begin
!= Module constants_mod
!
!   * Developers: Morikawa Yasuhiro
!   * Version: $Id: constants.f90,v 1.1.1.1 2005/11/08 14:10:23 morikawa Exp $
!   * Tag Name: $Name:  $
!   * Change History: 
!
!== Overview
!
!This module set physical constants.
!Default value is standard condition on the earth.
!These values could be changed by NAMELIST.
!
!物理定数を設定するためのモジュールである。
!デフォルトでは地球上の標準状態の値を持っているが、
!NAMELIST により変更することも可能である。
!
!== Error Handling
!
!== Known Bugs
!
!== Note
!
!== Future Plans
!
!
                                                                 !=end

module constants_mod
                                                                 !=begin
  !== Dependency
  use type_mod, only : REKIND, DBKIND, INTKIND, TOKEN, STRING
                                                                 !=end
  implicit none
                                                                 !=begin
  !== Public Interface
  private
  public :: constants_init, constants_end  ! subroutines
  public :: & ! variables
       &    PI, R0, Omega, Grav, Cp, RAir, EL, CpVap, RVap,  &
       &    DH2O, EpsV, ES0, StB, FKarm, EpsVT, SecPerDay,   &
       &    EFoldTime, TempAve, TimeFilter, VisOrder,        &
       &    TimeFilterStepInt

  ! follow data is default values.

  real(DBKIND), save  :: PI        = 3.141592653589793 ! 円周率
  real(DBKIND), save  :: R0        = 6.371d6           ! 球の半径
  real(DBKIND), save  :: Omega     = 7.292d-5          ! 回転角速度
  real(DBKIND), save  :: Grav      = 9.8d0             ! 重力加速度

  real(DBKIND), save  :: Cp        = 1004.6d0          ! 大気定圧比熱
  real(DBKIND), save  :: RAir      = 287.04d0          ! 大気気体定数
  real(DBKIND), save  :: EL        = 2.5d6             ! 水の凝結の潜熱
  real(DBKIND), save  :: CpVap     = 1810.0d0          ! 水蒸気定圧比熱
  real(DBKIND), save  :: RVap      = 461.0d0           ! 水蒸気気体定数
  real(DBKIND), save  :: DH2O      = 1000.0d0          ! 水の密度
  real(DBKIND), save  :: EpsV      = 0.6226464208      ! 水と大気の分子量比ε
  real(DBKIND), save  :: ES0       = 611.0d0           ! ０℃飽和蒸気圧：Ｐａ
  real(DBKIND), save  :: StB       = 5.67d-8           ! Stefan-Boltzman
  real(DBKIND), save  :: FKarm     = 0.4d0             ! Karman 定数
  real(DBKIND), save  :: EpsVT     = 0.6060479376      ! 1/ε-1

  real(DBKIND), save  :: SecPerDay = 86400.0d0         ! 1日あたりの秒数 

  real(DBKIND), save  :: EFoldTime = 8640.0d0          ! 最大波数に対する e-folding time
  real(DBKIND), save  :: TempAve   = 300.0             ! 平均温度
  real(DBKIND), save  :: TimeFilter= 0.05              ! 時間フィルター係数

  integer(INTKIND), save ::  VisOrder  = 4             ! 超粘性の次数
  integer(INTKIND), save :: TimeFilterStepInt = 1     ! 時間フィルターするステップ間隔
                                                                 !=end

  logical           ,save:: constants_initialized = .false.
  character(STRING),parameter:: version = &
       & '$Id: constants.f90,v 1.1.1.1 2005/11/08 14:10:23 morikawa Exp $'
  character(STRING),parameter:: tagname = '$Name:  $'

contains
                                                                 !=begin
  !== Procedure Interface
  !
  !=== Initialize module and acquire NAMELIST
  !
  !モジュールを初期化し、NAMELIST から値を取得する。
  !NAMELIST から値が取得できないものに関しては上記のデフォルト値が
  !用いられる。
  !
  !NAMELIST ファイルは、メインプログラムにて ((< nmlfile_mod >)) の
  !((< nmlfile_init >)) で指定されることが想定されているが、
  !もしもこの初期化ルーチンより以前に指定されていなければ、
  !((< nmlfile_init >)) のデフォルトで指定される NAMELIST ファイルを
  !読む。
  !
  subroutine constants_init
  !==== Dependency
    use type_mod   , only : REKIND, DBKIND, INTKIND, TOKEN, STRING
    use nmlfile_mod, only : nmlfile_init, nmlfile_open, nmlfile_close
    use dc_trace   , only : BeginSub, EndSub, DbgMessage
    use dc_message , only : MessageNotify
                                                                 !=end
    implicit none
    !
    ! This namelist is require physical constants
    namelist /constants_nml/ &

         & PI                , & ! 円周率
         & R0                , & ! 球の半径
         & Omega             , & ! 回転角速度
         & Grav              , & ! 重力加速度

         & Cp                , & ! 大気定圧比熱
         & RAir              , & ! 大気気体定数
         & EL                , & ! 水の凝結の潜熱
         & CpVap             , & ! 水蒸気定圧比熱
         & RVap              , & ! 水蒸気気体定数
         & DH2O              , & ! 水の密度
         & EpsV              , & ! 水と大気の分子量比ε
         & ES0               , & ! ０℃飽和蒸気圧：Ｐａ
         & StB               , & ! Stefan-Boltzman
         & FKarm             , & ! Karman 定数
         & EpsVT             , & ! 1/ε-1

         & SecPerDay         , & ! 1日あたりの秒数

         & EFoldTime         , & ! 最大波数に対する e-folding time
         & TempAve           , & ! 平均温度
         & TimeFilter        , & ! 時間フィルター係数
         & TimeFilterStepInt , & ! 時間フィルターするステップ間隔
         & VisOrder              ! 超粘性の次数
                                                                 !=end

    integer(INTKIND)            :: nmlstat, nmlunit
    logical                     :: nmlreadable
    character(STRING), parameter:: subname = "constants_init"
  continue

    !----------------------------------------------------------------
    !   Check Initialization
    !----------------------------------------------------------------
    call BeginSub(subname)
    if (constants_initialized) then
       call EndSub( subname, '%c is already called', c1=trim(subname) )
       return
    else
       constants_initialized = .true.
    endif

    !----------------------------------------------------------------
    !   Version identifier
    !----------------------------------------------------------------
    call DbgMessage('%c :: %c', c1=trim(version), c2=trim(tagname))

    !----------------------------------------------------------------
    !   read constants_nml
    !----------------------------------------------------------------
    call nmlfile_init
    call nmlfile_open(nmlunit, nmlreadable)
    if (nmlreadable) then
       read(nmlunit, nml=constants_nml, iostat=nmlstat)
       call DbgMessage('Stat of NAMELIST constants_nml Input is <%d>', &
            &           i=(/nmlstat/))
       write(0, nml=constants_nml)
    else
       call DbgMessage('Not Read NAMELIST constants_nml')
       call MessageNotify('W', subname, &
            & 'Can not Read NAMELIST constants_nml. Force Use Default Value.')
    end if
    call nmlfile_close

    call EndSub( subname )
  end subroutine constants_init


                                                                 !=begin
  !=== Terminate module
  !
  !NAMELIST から読み込んだ値を破棄し、各定数をデフォルトの値に戻す。
  !
  subroutine constants_end
  !==== Dependency
    use dc_trace,    only : BeginSub, EndSub, DbgMessage
                                                                 !=end
    implicit none

    !-----------------------------------------------------------------
    !   変数定義
    !-----------------------------------------------------------------
    !----- 作業用内部変数 -----
    character(len = *),  parameter:: subname = "constants_end"
  continue

    !----------------------------------------------------------------
    !   Check Initialization
    !----------------------------------------------------------------
    call BeginSub(subname)
    if ( .not. constants_initialized) then
       call EndSub( subname, 'constants_init was not called', &
            &       c1=trim(subname) )
       return
    else
       constants_initialized = .false.
    endif

    !----------------------------------------------------------------
    !   Reset to default value
    !----------------------------------------------------------------
    PI        = 3.141592653589793  ! 円周率
    R0        = 6.371d6            ! 球の半径
    Omega     = 7.292d-5           ! 回転角速度
    Grav      = 9.8d0              ! 重力加速度

    Cp        = 1004.6d0           ! 大気定圧比熱
    RAir      = 287.04d0           ! 大気気体定数
    EL        = 2.5d6              ! 水の凝結の潜熱
    CpVap     = 1810.0d0           ! 水蒸気定圧比熱
    RVap      = 461.0d0            ! 水蒸気気体定数
    DH2O      = 1000.0d0           ! 水の密度
    EpsV      = 0.6226464208       ! 水と大気の分子量比ε
    ES0       = 611.0d0            ! ０℃飽和蒸気圧：Ｐａ
    StB       = 5.67d-8            ! Stefan-Boltzman
    FKarm     = 0.4d0              ! Karman 定数
    EpsVT     = 0.6060479376       ! 1/ε-1

    SecPerDay = 86400.0d0          ! 1日あたりの秒数

    EFoldTime = 8640.0d0           ! 最大波数に対する e-folding time
    TempAve   = 300.0              ! 平均温度
    TimeFilter= 0.05               ! 時間フィルター係数
    TimeFilterStepInt = 1          ! 時間フィルターするステップ間隔

    call EndSub(subname)
  end subroutine constants_end


end module constants_mod
