!---------------------------------------------------------------------
!     Copyright (C) GFD Dennou Club, 2005. All rights reserved.
!---------------------------------------------------------------------
! physics.f90 - physics process module
!
! History
!   2004/11/25 Morikawa Yasuhiro     create
!

module physics_mod

  use type_mod,    only : REKIND, DBKIND, INTKIND, TOKEN, STRING

  implicit none

  private
  public :: physics_init, physics_end,                       &
       &    R0, Omega, Grav, Cp, RAir, EL, CpVap, RVap,      &
       &    DH2O, EpsV, ES0, StB, FKarm, EpsVT, Nu, TempAve, &
       &    TimeFilter, ViscosOrder

  !----------------------------------------------------------------
  !   物理定数、拡散係数、フィルター係数。physics_init で取得。
  !----------------------------------------------------------------
  real(DBKIND), save  ::  &
       &
       & R0             , & ! 球の半径
       & Omega          , & ! 回転角速度
       & Grav           , & ! 重力加速度
       &
       & Cp             , & ! 大気定圧比熱
       & RAir           , & ! 大気気体定数
       & EL             , & ! 水の凝結の潜熱
       & CpVap          , & ! 水蒸気定圧比熱
       & RVap           , & ! 水蒸気気体定数
       & DH2O           , & ! 水の密度
       & EpsV           , & ! 水と大気の分子量比ε
       & ES0            , & ! ０℃飽和蒸気圧：Ｐａ
       & StB            , & ! Stefan-Boltzman
       & FKarm          , & ! Karman 定数
       & EpsVT          , & ! 1/ε-1
       &
       & Nu             , & ! 超粘性係数
       & TempAve        , & ! 平均温度
       & TimeFilter         ! 時間フィルター係数

  integer(INTKIND), save ::  ViscosOrder  ! 超粘性の次数

  logical, save :: physics_initialized = .false.

contains

  subroutine physics_init
    use type_mod,    only : REKIND, DBKIND, INTKIND, TOKEN, STRING
    use dc_trace,    only : SetDebug, BeginSub, EndSub, DbgMessage
    implicit none

    !----------------------------------------------------------------
    !   出力ファイル設定  (gtool4 netCDF データ用)
    !----------------------------------------------------------------
    namelist /physics_nml/ &
         &
         & R0            , & ! 球の半径
         & Omega         , & ! 回転角速度
         & Grav          , & ! 重力加速度
         &
         & Cp            , & ! 大気定圧比熱
         & RAir          , & ! 大気気体定数
         & EL            , & ! 水の凝結の潜熱
         & CpVap         , & ! 水蒸気定圧比熱
         & RVap          , & ! 水蒸気気体定数
         & DH2O          , & ! 水の密度
         & EpsV          , & ! 水と大気の分子量比ε
         & ES0           , & ! ０℃飽和蒸気圧：Ｐａ
         & StB           , & ! Stefan-Boltzman
         & FKarm         , & ! Karman 定数
         & EpsVT         , & ! 1/ε-1
         &
         & Nu            , & ! 超粘性係数
         & TempAve       , & ! 平均温度
         & TimeFilter    , & ! 時間フィルター係数
         & ViscosOrder       ! 超粘性の次数

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

  continue

    !----------------------------------------------------------------
    !   初期化確認
    !----------------------------------------------------------------
    call BeginSub(subname)
    if (physics_initialized) then
       call EndSub( subname, '%c is already called', c1=trim(subname) )
       return
    else
       physics_initialized = .true.
    endif

    !----------------------------------------------------------------
    !   NAMELIST 読み込み
    !----------------------------------------------------------------
    read(5,nml=physics_nml)   ; write(6,nml=physics_nml)

    call EndSub(subname)
  end subroutine physics_init

  subroutine physics_end
    use type_mod,    only : REKIND, DBKIND, INTKIND, TOKEN, STRING
    use dc_trace,    only : SetDebug, BeginSub, EndSub, DbgMessage
    implicit none

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

    !-----------------------------------------------------------------
    !   初期化確認
    !-----------------------------------------------------------------
    call BeginSub(subname)
    if ( .not. physics_initialized) then
       call EndSub( subname, 'physics_init was not called', &
            &       c1=trim(subname) )
       return
    else
       physics_initialized = .false.
    endif

    call EndSub(subname)
  end subroutine physics_end

end module physics_mod