!= 物理過程のみの計算のための力学過程
!
!= A dynamics for calculation with physical processes only
!
! Authors::   Yoshiyuki O. Takahashi
! Version::   $Id: dynamics_physicsonly.f90,v 1.2 2010-09-12 01:57:40 yot Exp $ 
! Tag Name::  $Name: dcpam5-20110221-2 $
! Copyright:: Copyright (C) GFD Dennou Club, 2008-2009. All rights reserved.
! License::   See COPYRIGHT[link:../../../COPYRIGHT]
!

module dynamics_physicsonly
  !
  != 物理過程のみの計算のための力学過程
  !
  != A dynamics for calculation with physical processes only
  !
  ! <b>Note that Japanese and English are described in parallel.</b>
  !
  !== Procedures List
  !
  !== NAMELIST
  !
  !

  ! モジュール引用 ; USE statements
  !

  ! 格子点設定
  ! Grid points settings
  !
  use gridset, only: imax, & ! 経度格子点数. 
                             ! Number of grid points in longitude
    &                jmax, & ! 緯度格子点数. 
                             ! Number of grid points in latitude
    &                kmax    ! 鉛直層数. 
                             ! Number of vertical level
  ! 組成に関わる配列の設定
  ! Settings of array for atmospheric composition
  !
  use composition, only: &
    &                    ncmax
                              ! 成分の数
                              ! Number of composition

  ! 種別型パラメタ
  ! Kind type parameter
  !
  use dc_types, only: DP, &      ! 倍精度実数型. Double precision. 
    &                 STRING     ! 文字列.       Strings. 

  ! メッセージ出力
  ! Message output
  !
  use dc_message, only: MessageNotify

  ! 宣言文 ; Declaration statements
  !
  implicit none
  private

  ! 公開手続き
  ! Public procedure
  !
  public:: DynamicsPhysicsOnly

  ! 公開変数
  ! Public variables
  !
  logical, save, public:: dynamics_physicsonly_inited = .false.
                              ! 初期設定フラグ. 
                              ! Initialization flag


  ! 非公開変数
  ! Private variables
  !


  character(*), parameter:: module_name = 'dynamics_physicsonly'
                              ! モジュールの名称. 
                              ! Module name
  character(*), parameter:: version = &
    & '$Name: dcpam5-20110221-2 $' // &
    & '$Id: dynamics_physicsonly.f90,v 1.2 2010-09-12 01:57:40 yot Exp $'
                              ! モジュールのバージョン
                              ! Module version

  ! INTERFACE 文 ; INTERFACE statements
  !

contains

  subroutine DynamicsPhysicsOnly(                                &
    & xyz_DUDtPhy, xyz_DVDtPhy, xyz_DTempDtPhy, xyzf_DQMixDtPhy, & ! (in)
    & xy_PsB, xyz_UB, xyz_VB, xyz_TempB, xyzf_QMixB,             & ! (in)
    & xy_PsA, xyz_UA, xyz_VA, xyz_TempA, xyzf_QMixA              & ! (out)
    & )

    ! モジュール引用 ; USE statements
    !

    ! 時刻管理
    ! Time control
    !
    use timeset, only: &
      & DelTime, &            ! $ \Delta t $ [s]
      & TimeN, &              ! ステップ $ t $ の時刻. Time of step $ t $. 
      & TimesetClockStart, TimesetClockStop

    ! デバッグ用ユーティリティ
    ! Utilities for debug
    !
    use dc_trace, only: DbgMessage, BeginSub, EndSub

    ! 宣言文 ; Declaration statements
    !
    implicit none

    real(DP), intent(in):: xyz_DUDtPhy    (0:imax-1, 1:jmax, 1:kmax)
                              ! $ \left(\DP{u}{t}\right)^{phy} $ .
                              ! 外力項 (物理過程) による東西風速変化.
                              ! Eastward wind tendency by external force terms (physical processes)
    real(DP), intent(in):: xyz_DVDtPhy    (0:imax-1, 1:jmax, 1:kmax)
                              ! $ \left(\DP{v}{t}\right)^{phy} $ .
                              ! 外力項 (物理過程) による南北風速変化.
                              ! Northward wind tendency by external force terms (physicalprocesses)
    real(DP), intent(in):: xyz_DTempDtPhy (0:imax-1, 1:jmax, 1:kmax)
                              ! $ \left(\DP{T}{t}\right)^{phy} $ .
                              ! 外力項 (物理過程) による温度変化.
                              ! Temperature tendency by external force terms (physical processes)
    real(DP), intent(in):: xyzf_DQMixDtPhy (0:imax-1, 1:jmax, 1:kmax, 1:ncmax)
                              ! $ \left(\DP{q}{t}\right)^{phy} $ .
                              ! 外力項 (物理過程) による比湿変化.
                              ! Temperature tendency by external force terms (physical processes)
    real(DP), intent(in ):: xy_PsB(0:imax-1, 1:jmax)
                              ! $ p_s $ . 地表面気圧 (半整数レベル). 
                              ! Surface pressure (half level)
    real(DP), intent(in ):: xyz_UB(0:imax-1, 1:jmax, 1:kmax)
                              ! $ u $ . 東西風速. Eastward wind
    real(DP), intent(in ):: xyz_VB(0:imax-1, 1:jmax, 1:kmax)
                              ! $ v $ . 南北風速. Northward wind
    real(DP), intent(in ):: xyz_TempB(0:imax-1, 1:jmax, 1:kmax)
                              ! $ T $ . 温度 (整数レベル). 
                              ! Temperature (full level)
    real(DP), intent(in ):: xyzf_QMixB(0:imax-1, 1:jmax, 1:kmax, 1:ncmax)
                              ! $ q $ .     比湿. Specific humidity
    real(DP), intent(out):: xy_PsA(0:imax-1, 1:jmax)
                              ! $ p_s $ . 地表面気圧 (半整数レベル). 
                              ! Surface pressure (half level)
    real(DP), intent(out):: xyz_UA(0:imax-1, 1:jmax, 1:kmax)
                              ! $ u $ . 東西風速. Eastward wind
    real(DP), intent(out):: xyz_VA(0:imax-1, 1:jmax, 1:kmax)
                              ! $ v $ . 南北風速. Northward wind
    real(DP), intent(out):: xyz_TempA(0:imax-1, 1:jmax, 1:kmax)
                              ! $ T $ . 温度 (整数レベル). 
                              ! Temperature (full level)
    real(DP), intent(out):: xyzf_QMixA(0:imax-1, 1:jmax, 1:kmax, 1:ncmax)
                              ! $ q $ .     比湿. Specific humidity


    ! 作業変数
    ! Work variables
    !

!!$    integer:: i               ! 経度方向に回る DO ループ用作業変数
!!$                              ! Work variables for DO loop in longitude
!!$    integer:: j               ! 緯度方向に回る DO ループ用作業変数
!!$                              ! Work variables for DO loop in latitude
!!$    integer:: k

    ! 実行文 ; Executable statement
    !

    ! 計算時間計測開始
    ! Start measurement of computation time
    !
    call TimesetClockStart( module_name )

    ! 初期化
    ! Initialization
    !
    if ( .not. dynamics_physicsonly_inited ) call DynamicsPhysicsOnlyInit


    xy_PsA     = xy_PsB
    xyz_UA     = xyz_UB     + xyz_DUDtPhy     * 2.0d0 * DelTime
    xyz_VA     = xyz_VB     + xyz_DVDtPhy     * 2.0d0 * DelTime
    xyz_TempA  = xyz_TempB  + xyz_DTempDtPhy  * 2.0d0 * DelTime
    xyzf_QMixA = xyzf_QMixB + xyzf_DQMixDtPhy * 2.0d0 * DelTime


  end subroutine DynamicsPhysicsOnly

  !-------------------------------------------------------------------

  subroutine DynamicsPhysicsOnlyInit

    ! モジュール引用 ; USE statements
    !


    ! 宣言文 ; Declaration statements
    !
    implicit none

!!$    integer:: unit_nml        ! NAMELIST ファイルオープン用装置番号. 
!!$                              ! Unit number for NAMELIST file open
!!$    integer:: iostat_nml      ! NAMELIST 読み込み時の IOSTAT. 
!!$                              ! IOSTAT of NAMELIST read

    ! NAMELIST 変数群
    ! NAMELIST group name
    !
!!$    namelist /surface_flux_bulk_nml/ &
!!$      & VelMinForRi, VelMinForVel, VelMinForTemp, VelMinForQVap, &
!!$      & VelMaxForVel, VelMaxForTemp, VelMaxForQVap, &
!!$! 
!!$      & Neutral, ConstBulkCoef, &
!!$      & VelBulkCoefMin, TempBulkCoefMin, QVapBulkCoefMin, &
!!$      & VelBulkCoefMax, TempBulkCoefMax, QVapBulkCoefMax
!!$          !
!!$          ! デフォルト値については初期化手続 "surface_flux_bulk#SurfFluxInit" 
!!$          ! のソースコードを参照のこと. 
!!$          !
!!$          ! Refer to source codes in the initialization procedure
!!$          ! "surface_flux_bulk#SurfFluxInit" for the default values. 
!!$          !

    ! 実行文 ; Executable statement
    !

    if ( dynamics_physicsonly_inited ) return
!!$    call InitCheck



    ! デフォルト値の設定
    ! Default values settings
    !



    ! NAMELIST の読み込み
    ! NAMELIST is input
    !
!!$    if ( trim(namelist_filename) /= '' ) then
!!$      call FileOpen( unit_nml, &          ! (out)
!!$        & namelist_filename, mode = 'r' ) ! (in)
!!$
!!$      rewind( unit_nml )
!!$      read( unit_nml, &                ! (in)
!!$        & nml = surface_flux_bulk_nml, &  ! (out)
!!$        & iostat = iostat_nml )        ! (out)
!!$      close( unit_nml )
!!$
!!$      call NmlutilMsg( iostat_nml, module_name ) ! (in)
!!$    end if
!!$
!!$    ! ヒストリデータ出力のためのへの変数登録
!!$    ! Register of variables for history data output
!!$    !
!!$    call HistoryAutoAddVariable( 'TauX', &
!!$      & (/ 'lon ', 'lat ', 'time' /), &
!!$      & 'surface stress(x)  ', 'N m-2' )
!!$
!!$    call HistoryAutoAddVariable( 'TauY', &
!!$      & (/ 'lon ', 'lat ', 'time' /), &
!!$      & 'surface stress(y)  ', 'N m-2' )
!!$
!!$    call HistoryAutoAddVariable( 'Sens', &
!!$      & (/ 'lon ', 'lat ', 'time' /), &
!!$      & 'sensible heat flux', 'W m-2' )
!!$
!!$    call HistoryAutoAddVariable( 'EVap', &
!!$      & (/ 'lon ', 'lat ', 'time' /), &
!!$      & 'latent heat flux  ', 'W m-2' )
!!$
!!$    ! 印字 ; Print
!!$    !
!!$    call MessageNotify( 'M', module_name, '----- Initialization Messages -----' )
!!$
!!$    call MessageNotify( 'M', module_name, '  VelMinForRi   = %f', d = (/ VelMinForRi   /) )
!!$    call MessageNotify( 'M', module_name, '  VelMinForVel  = %f', d = (/ VelMinForVel  /) )
!!$    call MessageNotify( 'M', module_name, '  VelMinForTemp = %f', d = (/ VelMinForTemp /) )
!!$    call MessageNotify( 'M', module_name, '  VelMinForQVap = %f', d = (/ VelMinForQVap /) )
!!$    call MessageNotify( 'M', module_name, '  VelMaxForVel  = %f', d = (/ VelMaxForVel  /) )
!!$    call MessageNotify( 'M', module_name, '  VelMaxForTemp = %f', d = (/ VelMaxForTemp /) )
!!$    call MessageNotify( 'M', module_name, '  VelMaxForQVap = %f', d = (/ VelMaxForQVap /) )
!!$
!!$    call MessageNotify( 'M', module_name, 'Bulk coefficients:' )
!!$    call MessageNotify( 'M', module_name, '  Neutral         = %b', l = (/ Neutral          /) )
!!$    call MessageNotify( 'M', module_name, '  ConstBulkCoef   = %f', d = (/ ConstBulkCoef   /) )
!!$    call MessageNotify( 'M', module_name, '  VelBulkCoefMin  = %f', d = (/ VelBulkCoefMin  /) )
!!$    call MessageNotify( 'M', module_name, '  TempBulkCoefMin = %f', d = (/ TempBulkCoefMin /) )
!!$    call MessageNotify( 'M', module_name, '  QVapBulkCoefMin = %f', d = (/ QVapBulkCoefMin /) )
!!$    call MessageNotify( 'M', module_name, '  VelBulkCoefMax  = %f', d = (/ VelBulkCoefMax  /) )
!!$    call MessageNotify( 'M', module_name, '  TempBulkCoefMax = %f', d = (/ TempBulkCoefMax /) )
!!$    call MessageNotify( 'M', module_name, '  QVapBulkCoefMax = %f', d = (/ QVapBulkCoefMax /) )
!!$    call MessageNotify( 'M', module_name, '-- version = %c', c1 = trim(version) )

    dynamics_physicsonly_inited = .true.
  end subroutine DynamicsPhysicsOnlyInit

  !-------------------------------------------------------------------
!!$
!!$  subroutine InitCheck
!!$    !
!!$    ! 依存モジュールの初期化チェック
!!$    !
!!$    ! Check initialization of dependency modules
!!$
!!$    ! モジュール引用 ; USE statements
!!$    !
!!$
!!$    ! NAMELIST ファイル入力に関するユーティリティ
!!$    ! Utilities for NAMELIST file input
!!$    !
!!$    use namelist_util, only: namelist_util_inited
!!$
!!$    ! 格子点設定
!!$    ! Grid points settings
!!$    !
!!$    use gridset, only: gridset_inited
!!$
!!$    ! 物理定数設定
!!$    ! Physical constants settings
!!$    !
!!$    use constants, only: constants_inited
!!$
!!$    ! 座標データ設定
!!$    ! Axes data settings
!!$    !
!!$    use axesset, only: axesset_inited
!!$
!!$    ! 時刻管理
!!$    ! Time control
!!$    !
!!$    use timeset, only: timeset_inited
!!$
!!$    ! 実行文 ; Executable statement
!!$    !
!!$
!!$    if ( .not. namelist_util_inited ) &
!!$      & call MessageNotify( 'E', module_name, '"namelist_util" module is not initialized.' )
!!$
!!$    if ( .not. gridset_inited ) &
!!$      & call MessageNotify( 'E', module_name, '"gridset" module is not initialized.' )
!!$
!!$    if ( .not. constants_inited ) &
!!$      & call MessageNotify( 'E', module_name, '"constants" module is not initialized.' )
!!$
!!$    if ( .not. axesset_inited ) &
!!$      & call MessageNotify( 'E', module_name, '"axesset" module is not initialized.' )
!!$
!!$    if ( .not. timeset_inited ) &
!!$      & call MessageNotify( 'E', module_name, '"timeset" module is not initialized.' )
!!$
!!$  end subroutine InitCheck

end module dynamics_physicsonly
