!= é™°è§£æ³•ã«ã‚ˆã‚‹æ™‚é–“ç©åˆ†
!
!= Time integration with implicit scheme
!
! Authors::   Yoshiyuki O. Takahashi, Yasuhiro MORIKAWA, Yukiko YAMADA
! Version::   $Id: phy_implicit.f90,v 1.24 2015/01/29 12:05:01 yot Exp $
! Tag Name::  $Name:  $
! Copyright:: Copyright (C) GFD Dennou Club, 2008-2009. All rights reserved.
! License::   See COPYRIGHT[link:../../../COPYRIGHT]
!

module phy_implicit
  !
  != é™°è§£æ³•ã«ã‚ˆã‚‹æ™‚é–“ç©åˆ†
  !
  != Time integration with implicit scheme
  !
  ! <b>Note that Japanese and English are described in parallel.</b>
  !
  !== Procedures List
  !
  ! PhyImplTendency      :: æ™‚é–“å¤‰åŒ–çŽ‡ã®è¨ˆç®—
  ! PhyImplEvalRadLFluxA :: é•·æ³¢ãƒ•ãƒ©ãƒƒã‚¯ã‚¹è£œæ£
  ! ------------         :: ------------
  ! PhyImplTendency      :: Calculate tendency
  ! PhyImplEvalRadLFluxA :: Longwave flux correction
  !
  !--
  !== NAMELIST
  !
  ! NAMELIST#phy_implicit_nml
  !++

  ! ãƒ¢ã‚¸ãƒ¥ãƒ¼ãƒ«å¼•ç”¨ ; 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, IndexH2OVap

  ! ç¨®åˆ¥åž‹ãƒ‘ãƒ©ãƒ¡ã‚¿
  ! 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 :: PhyImplTendency
  public :: PhyImplInit

  ! å…¬é–‹å¤‰æ•°
  ! Public variables
  !

  ! éžå…¬é–‹å¤‰æ•°
  ! Private variables
  !
  logical, save :: FlagBucketModel
  logical, save :: FlagSnow
  logical, save :: phy_implicit_inited = .false.
                              ! åˆæœŸè¨å®šãƒ•ãƒ©ã‚°.
                              ! Initialization flag

  character(*), parameter:: module_name = 'phy_implicit'
                              ! ãƒ¢ã‚¸ãƒ¥ãƒ¼ãƒ«ã®åç§°.
                              ! Module name
  character(*), parameter:: version = &
    & '$Name:  $' // &
    & '$Id: phy_implicit.f90,v 1.24 2015/01/29 12:05:01 yot Exp $'
                              ! ãƒ¢ã‚¸ãƒ¥ãƒ¼ãƒ«ã®ãƒãƒ¼ã‚¸ãƒ§ãƒ³
                              ! Module version

contains

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

  subroutine PhyImplTendency(                                  &
    & xyr_MomFluxX, xyr_MomFluxY, xyr_HeatFlux, xyrf_QMixFlux, & ! (in)
    & xyr_RadSFlux, xyr_RadLFlux,                              & ! (in)
    & xy_GroundTempFlux,                                       & ! (in)
    & xy_SurfTemp, xy_SurfHumidCoef, xy_SurfCond,              & ! (in)
    & xy_SurfHeatCapacity,                                     & ! (in)
    & xyra_DelRadLFlux,                                        & ! (in)
    & xyr_Press, xyz_Exner, xyr_Exner,                         & ! (in)
    & xyr_VirTemp, xyz_Height,                                 & ! (in)
    & xyr_VelDiffCoef, xyr_TempDiffCoef, xyr_QMixDiffCoef,     & ! (in)
    & xy_SurfVelTransCoef, xy_SurfTempTransCoef,               & ! (in)
    & xy_SurfQVapTransCoef,                                    & ! (in)
    & xyz_DUDt, xyz_DVDt, xyz_DTempDt, xyzf_DQMixDt,           & ! (out)
    & xy_DSurfTempDt                                           & ! (out)
    & )
    !
    ! æ™‚é–“å¤‰åŒ–çŽ‡ã®è¨ˆç®—ã‚’è¡Œã„ã¾ã™.
    !
    ! Calculate tendencies.
    !

    ! ãƒ¢ã‚¸ãƒ¥ãƒ¼ãƒ«å¼•ç”¨ ; USE statements
    !

    ! ç‰©ç†å®šæ•°è¨å®š
    ! Physical constants settings
    !
    use constants, only: &
      & Grav, &               ! $ g $ [m s-2].
                              ! é‡åŠ›åŠ é€Ÿåº¦.
                              ! Gravitational acceleration
      & CpDry, &
                              ! $ C_p $ [J kg-1 K-1].
                              ! ä¹¾ç‡¥å¤§æ°—ã®å®šåœ§æ¯”ç†±.
                              ! Specific heat of air at constant pressure
      & LatentHeat, &
                              ! $ L $ [J kg-1] .
                              ! å‡çµã®æ½œç†±.
                              ! Latent heat of condensation
      & GasRDry
                              ! $ R $ [J kg-1 K-1].
                              ! ä¹¾ç‡¥å¤§æ°—ã®æ°—ä½“å®šæ•°.
                              ! Gas constant of air

    ! æ™‚åˆ»ç®¡ç†
    ! Time control
    !
    use timeset, only: &
      & DelTime, &            ! $ \Delta t $ [s]
      & TimeN, &              ! ã‚¹ãƒ†ãƒƒãƒ— $ t $ ã®æ™‚åˆ». Time of step $ t $.
      & TimesetClockStart, TimesetClockStop

    ! ãƒ’ã‚¹ãƒˆãƒªãƒ‡ãƒ¼ã‚¿å‡ºåŠ›
    ! History data output
    !
    use gtool_historyauto, only: HistoryAutoPut

    ! é£½å’Œæ¯”æ¹¿ã®ç®—å‡º
    ! Evaluate saturation specific humidity
    !
    use saturate, only: xy_CalcQVapSat, xy_CalcDQVapSatDTemp

    ! é™°è§£æ³•ã«ã‚ˆã‚‹æ™‚é–“ç©åˆ†ã®ãŸã‚ã®ãƒ«ãƒ¼ãƒãƒ³
    ! Routines for time integration with implicit scheme
    !
    use phy_implicit_utils, only : PhyImplLUDecomp3, PhyImplLUSolve3

    ! å®£è¨€æ–‡ ; Declaration statements
    !
    implicit none

    real(DP), intent(in):: xyr_MomFluxX (0:imax-1, 1:jmax, 0:kmax)
                              ! æ±è¥¿æ–¹å‘é‹å‹•é‡ãƒ•ãƒ©ãƒƒã‚¯ã‚¹.
                              ! Eastward momentum flux
    real(DP), intent(in):: xyr_MomFluxY (0:imax-1, 1:jmax, 0:kmax)
                              ! å—åŒ—æ–¹å‘é‹å‹•é‡ãƒ•ãƒ©ãƒƒã‚¯ã‚¹.
                              ! Northward momentum flux
    real(DP), intent(in):: xyr_HeatFlux (0:imax-1, 1:jmax, 0:kmax)
                              ! ç†±ãƒ•ãƒ©ãƒƒã‚¯ã‚¹.
                              ! Heat flux
    real(DP), intent(in):: xyrf_QMixFlux (0:imax-1, 1:jmax, 0:kmax, 1:ncmax)
                              ! è³ªé‡ãƒ•ãƒ©ãƒƒã‚¯ã‚¹.
                              ! Mass flux of constituents

    real(DP), intent(in):: xyr_RadSFlux (0:imax-1, 1:jmax, 0:kmax)
                              ! çŸæ³¢ (æ—¥å°„) ãƒ•ãƒ©ãƒƒã‚¯ã‚¹.
                              ! Shortwave (insolation) flux
    real(DP), intent(in):: xyr_RadLFlux (0:imax-1, 1:jmax, 0:kmax)
                              ! é•·æ³¢ãƒ•ãƒ©ãƒƒã‚¯ã‚¹.
                              ! Longwave flux

    real(DP), intent(in):: xy_GroundTempFlux (0:imax-1, 1:jmax)
                              ! åœ°ä¸ç†±ãƒ•ãƒ©ãƒƒã‚¯ã‚¹.
                              ! Ground temperature flux
    real(DP), intent(in):: xy_SurfTemp (0:imax-1, 1:jmax)
                              ! åœ°è¡¨é¢æ¸©åº¦.
                              ! Surface temperature
    real(DP), intent(in):: xy_SurfHumidCoef (0:imax-1, 1:jmax)
                              ! åœ°è¡¨æ¹¿æ½¤åº¦.
                              ! Surface humidity coefficient
    integer, intent(in):: xy_SurfCond (0:imax-1, 1:jmax)
                              ! åœ°è¡¨çŠ¶æ…‹.
                              ! Surface condition
    real(DP), intent(in):: xy_SurfHeatCapacity (0:imax-1, 1:jmax)
                              ! åœ°è¡¨ç†±å®¹é‡.
                              ! Surface heat capacity

    real(DP), intent(in):: xyra_DelRadLFlux (0:imax-1, 1:jmax, 0:kmax, 0:1)
                              ! é•·æ³¢åœ°è¡¨æ¸©åº¦å¤‰åŒ–.
                              ! Surface temperature tendency with longwave

    real(DP), intent(in):: xyr_Press (0:imax-1, 1:jmax, 0:kmax)
                              ! $ \hat{p} $ . æ°—åœ§ (åŠæ•´æ•°ãƒ¬ãƒ™ãƒ«).
                              ! Air pressure (half level)
    real(DP), intent(in):: xyz_Exner (0:imax-1, 1:jmax, 1:kmax)
                              ! Exner é–¢æ•° (æ•´æ•°ãƒ¬ãƒ™ãƒ«).
                              ! Exner function (full level)
    real(DP), intent(in):: xyr_Exner (0:imax-1, 1:jmax, 0:kmax)
                              ! Exner é–¢æ•° (åŠæ•´æ•°ãƒ¬ãƒ™ãƒ«).
                              ! Exner function (half level)

    real(DP), intent(in):: xyr_VirTemp (0:imax-1, 1:jmax, 0:kmax)
                              ! $ \hat{T}_v $ . ä»®æ¸©åº¦ (åŠæ•´æ•°ãƒ¬ãƒ™ãƒ«).
                              ! Virtual temperature (half level)
    real(DP), intent(in):: xyz_Height (0:imax-1, 1:jmax, 1:kmax)
                              ! é«˜åº¦ (æ•´æ•°ãƒ¬ãƒ™ãƒ«).
                              ! Height (full level)

    real(DP), intent(in):: xyr_VelDiffCoef (0:imax-1, 1:jmax, 0:kmax)
                              ! æ‹¡æ•£ä¿‚æ•°ï¼šé‹å‹•é‡.
                              ! Diffusion coefficient: velocity
    real(DP), intent(in):: xyr_TempDiffCoef (0:imax-1, 1:jmax, 0:kmax)
                              ! æ‹¡æ•£ä¿‚æ•°ï¼šæ¸©åº¦.
                              ! Transfer coefficient: temperature
    real(DP), intent(in):: xyr_QMixDiffCoef (0:imax-1, 1:jmax, 0:kmax)
                              ! æ‹¡æ•£ä¿‚æ•°ï¼šæ¯”æ¹¿.
                              ! Diffusion coefficient: specific humidity

    real(DP), intent(in):: xy_SurfVelTransCoef (0:imax-1, 1:jmax)
                              ! è¼¸é€ä¿‚æ•°ï¼šé‹å‹•é‡.
                              ! Diffusion coefficient: velocity
    real(DP), intent(in):: xy_SurfTempTransCoef (0:imax-1, 1:jmax)
                              ! è¼¸é€ä¿‚æ•°ï¼šæ¸©åº¦.
                              ! Transfer coefficient: temperature
    real(DP), intent(in):: xy_SurfQVapTransCoef (0:imax-1, 1:jmax)
                              ! è¼¸é€ä¿‚æ•°ï¼šæ¯”æ¹¿.
                              ! Transfer coefficient: specific humidity

    real(DP), intent(out):: xyz_DUDt (0:imax-1, 1:jmax, 1:kmax)
                              ! $ \DP{u}{t} $ . æ±è¥¿é¢¨é€Ÿå¤‰åŒ–.
                              ! Eastward wind tendency
    real(DP), intent(out):: xyz_DVDt (0:imax-1, 1:jmax, 1:kmax)
                              ! $ \DP{v}{t} $ . å—åŒ—é¢¨é€Ÿå¤‰åŒ–.
                              ! Northward wind tendency
    real(DP), intent(out):: xyz_DTempDt (0:imax-1, 1:jmax, 1:kmax)
                              ! $ \DP{T}{t} $ . æ¸©åº¦å¤‰åŒ–.
                              ! Temperature tendency
    real(DP), intent(out):: xyzf_DQMixDt(0:imax-1, 1:jmax, 1:kmax, 1:ncmax)
                              ! $ \DP{q}{t} $ . æ¯”æ¹¿å¤‰åŒ–.
                              ! Temperature tendency
    real(DP), intent(out):: xy_DSurfTempDt (0:imax-1, 1:jmax)
                              ! åœ°è¡¨é¢æ¸©åº¦å¤‰åŒ–çŽ‡.
                              ! Surface temperature tendency

    ! ä½œæ¥å¤‰æ•°
    ! Work variables
    !
    real(DP) :: xyr_VelTransCoef (0:imax-1, 1:jmax, 0:kmax)
                              ! è¼¸é€ä¿‚æ•°ï¼šé‹å‹•é‡.
                              ! Transfer coefficient: velocity
    real(DP) :: xyr_TempTransCoef (0:imax-1, 1:jmax, 0:kmax)
                              ! è¼¸é€ä¿‚æ•°ï¼šæ¸©åº¦.
                              ! Transfer coefficient: temperature
    real(DP) :: xyr_QMixTransCoef(0:imax-1, 1:jmax, 0:kmax)
                              ! è¼¸é€ä¿‚æ•°ï¼šè³ªé‡.
                              ! Transfer coefficient: mass of constituents
    real(DP):: xyza_UVMtx (0:imax-1, 1:jmax, 1:kmax, -1:1)
                              ! é€Ÿåº¦é™°è§£è¡Œåˆ—.
                              ! Implicit matrix about velocity
    real(DP):: xyra_TempMtx (0:imax-1, 1:jmax, 0:kmax, -1:1)
                              ! æ¸©åº¦é™°è§£è¡Œåˆ—.
                              ! Implicit matrix about temperature
    real(DP):: xyza_QVapMtx (0:imax-1, 1:jmax, 1:kmax, -1:1)
                              ! æ¯”æ¹¿é™°è§£è¡Œåˆ—.
                              ! Implicit matrix about specific humidity
    real(DP):: xyza_QMixMtx (0:imax-1, 1:jmax, 1:kmax, -1:1)
                              ! è³ªé‡æ··åˆæ¯”é™°è§£è¡Œåˆ—.
                              ! Implicit matrix about mass mixing ratio
    real(DP):: xyaa_SurfMtx (0:imax-1, 1:jmax, 0:0, -1:1)
                              ! æƒ‘æ˜Ÿè¡¨é¢ã‚¨ãƒãƒ«ã‚®ãƒ¼åŽæ”¯ç”¨é™°è§£è¡Œåˆ—
                              ! Implicit matrix for surface energy balance
    real(DP):: xyz_DelTempQVap (0:imax-1, 1:jmax, -kmax:kmax)
                              ! $ T q $ ã®æ™‚é–“å¤‰åŒ–.
                              ! Tendency of $ T q $
    real(DP):: xyza_TempQVapLUMtx (0:imax-1, 1:jmax, -kmax:kmax, -1:1)
                              ! LU è¡Œåˆ—.
                              ! LU matrix
    real(DP):: xyza_UVLUMtx (0:imax-1, 1:jmax, 1:kmax,-1:1)
                              ! LU è¡Œåˆ—.
                              ! LU matrix
    real(DP):: xyza_QMixLUMtx(0:imax-1, 1:jmax, 1:kmax,-1:1)
                              ! LU è¡Œåˆ—.
                              ! LU matrix
    real(DP):: xy_SurfQVapSat (0:imax-1, 1:jmax)
                              ! åœ°è¡¨é£½å’Œæ¯”æ¹¿.
                              ! Saturated specific humidity on surface
    real(DP):: xy_SurfDQVapSatDTemp (0:imax-1, 1:jmax)
                              ! åœ°è¡¨é£½å’Œæ¯”æ¹¿å¤‰åŒ–.
                              ! Saturated specific humidity tendency on surface

    integer:: i               ! çµŒåº¦æ–¹å‘ã«å›žã‚‹ DO ãƒ«ãƒ¼ãƒ—ç”¨ä½œæ¥å¤‰æ•°
                              ! Work variables for DO loop in longitude
    integer:: j               ! ç·¯åº¦æ–¹å‘ã«å›žã‚‹ DO ãƒ«ãƒ¼ãƒ—ç”¨ä½œæ¥å¤‰æ•°
                              ! Work variables for DO loop in latitude
    integer:: k               ! é‰›ç›´æ–¹å‘ã«å›žã‚‹ DO ãƒ«ãƒ¼ãƒ—ç”¨ä½œæ¥å¤‰æ•°
                              ! Work variables for DO loop in vertical direction
    integer:: l               ! è¡Œåˆ—ç”¨ DO ãƒ«ãƒ¼ãƒ—ç”¨ä½œæ¥å¤‰æ•°
                              ! Work variables for DO loop of matrices
    integer:: n               ! çµ„æˆæ–¹å‘ã«å›žã‚‹ DO ãƒ«ãƒ¼ãƒ—ç”¨ä½œæ¥å¤‰æ•°
                              ! Work variables for DO loop in dimension of constituents

    ! å®Ÿè¡Œæ–‡ ; Executable statement
    !

    ! åˆæœŸåŒ–ç¢ºèª
    ! Initialization check
    !
    if ( .not. phy_implicit_inited ) then
      call MessageNotify( 'E', module_name, 'This module has not been initialized.' )
    end if

    ! è¨ˆç®—æ™‚é–“è¨ˆæ¸¬é–‹å§‹
    ! Start measurement of computation time
    !
    call TimesetClockStart( module_name )

    ! ãƒã‚±ãƒ„ãƒ¢ãƒ‡ãƒ«ã®æ‰±ã„ã®ç¢ºèª
    ! Check use of bucket model
    !
    ! Bucket model with this routine is not supported fully, although some
    ! parts of calculation is implemented.
    !
    if ( FlagBucketModel ) then
      call MessageNotify( 'E', module_name, 'Bucket model cannot be used with this routine.' )
    end if

    ! é›ªã®æ‰±ã„ã®ç¢ºèª
    ! Check about treatment of snow
    !
    if ( FlagSnow ) then
      call MessageNotify( 'E', module_name, 'Snow melt is not treated in this routine.' )
    end if

    ! è¼¸é€ä¿‚æ•°ã®è¨ˆç®—
    ! Calculate transfer coefficient
    !
    xyr_VelTransCoef (:,:,0)    = 0.0_DP
    xyr_VelTransCoef (:,:,kmax) = 0.0_DP
    xyr_TempTransCoef(:,:,0)    = 0.0_DP
    xyr_TempTransCoef(:,:,kmax) = 0.0_DP
    xyr_QMixTransCoef(:,:,0)    = 0.0_DP
    xyr_QMixTransCoef(:,:,kmax) = 0.0_DP

    do k = 1, kmax-1
      xyr_VelTransCoef(:,:,k) =                                     &
        &   xyr_VelDiffCoef(:,:,k)                                  &
        &     * xyr_Press(:,:,k) / ( GasRDry * xyr_VirTemp(:,:,k) ) &
        &     / ( xyz_Height(:,:,k+1) - xyz_Height(:,:,k) )

      xyr_TempTransCoef(:,:,k) =                                    &
        &   xyr_TempDiffCoef(:,:,k)                                 &
        &     * xyr_Press(:,:,k) / ( GasRDry * xyr_VirTemp(:,:,k) ) &
        &     / ( xyz_Height(:,:,k+1) - xyz_Height(:,:,k) )

      xyr_QMixTransCoef(:,:,k) =                                    &
        &   xyr_QMixDiffCoef(:,:,k)                                 &
        &     * xyr_Press(:,:,k) / ( GasRDry * xyr_VirTemp(:,:,k) ) &
        &     / ( xyz_Height(:,:,k+1) - xyz_Height(:,:,k) )
    end do

    ! é™°è§£æ³•ã®ãŸã‚ã®è¡Œåˆ—ä½œæˆ
    ! Create matrices for implicit scheme
    !

    ! é‰›ç›´æ‹¡æ•£ã‚¹ã‚ãƒ¼ãƒ ã®è¼¸é€ä¿‚æ•°ã‹ã‚‰é™°è§£è¡Œåˆ—ã®è¨ˆç®— (é€Ÿåº¦)
    ! Calculate implicit matrices from transfer coefficient of vertical diffusion scheme (velocity)
    !
    k = 1
    xyza_UVMtx  (:,:,k,-1) = 0.0_DP
    xyza_UVMtx  (:,:,k, 0) =                                                  &
      & - ( xyr_Press(:,:,k) - xyr_Press(:,:,k-1) ) / Grav / ( 2.0_DP * DelTime ) &
      & + xy_SurfVelTransCoef(:,:)                                            &
      & + xyr_VelTransCoef(:,:,k  )
    xyza_UVMtx  (:,:,k, 1) = &
      & - xyr_VelTransCoef(:,:,k)

    do k = 2, kmax-1
      xyza_UVMtx  (:,:,k,-1) = &
        & - xyr_VelTransCoef(:,:,k-1)
      xyza_UVMtx  (:,:,k, 0) =                                                  &
        & - ( xyr_Press(:,:,k) - xyr_Press(:,:,k-1) ) / Grav / ( 2.0_DP * DelTime ) &
        & + xyr_VelTransCoef(:,:,k-1)                                           &
        & + xyr_VelTransCoef(:,:,k  )
      xyza_UVMtx  (:,:,k, 1) = &
        & - xyr_VelTransCoef(:,:,k)
    end do

    k = kmax
    xyza_UVMtx  (:,:,k,-1) = &
      & - xyr_VelTransCoef(:,:,k-1)
    xyza_UVMtx  (:,:,k, 0) =                                                  &
      & - ( xyr_Press(:,:,k) - xyr_Press(:,:,k-1) ) / Grav / ( 2.0_DP * DelTime ) &
      & + xyr_VelTransCoef(:,:,k-1)
    xyza_UVMtx  (:,:,k, 1) = 0.0_DP

    ! é‰›ç›´æ‹¡æ•£ã‚¹ã‚ãƒ¼ãƒ ã®è¼¸é€ä¿‚æ•°ã‹ã‚‰é™°è§£è¡Œåˆ—ã®è¨ˆç®— (æ¸©åº¦)
    ! Calculate implicit matrices from transfer coefficient of vertical diffusion scheme (temperature)
    !
    k = 1
    xyra_TempMtx(:,:,k,-1) = &
      & - CpDry * xy_SurfTempTransCoef(:,:)
    xyra_TempMtx(:,:,k, 0) =                                                          &
      & - CpDry * ( xyr_Press(:,:,k) - xyr_Press(:,:,k-1) ) / Grav / ( 2.0_DP * DelTime ) &
      & + CpDry * xyr_Exner(:,:,k-1) / xyz_Exner(:,:,k  ) * xy_SurfTempTransCoef(:,:) &
      & + CpDry * xyr_Exner(:,:,k  ) / xyz_Exner(:,:,k  ) * xyr_TempTransCoef(:,:,k  )
    xyra_TempMtx(:,:,k, 1) = &
      & - CpDry * xyr_Exner(:,:,k  ) / xyz_Exner(:,:,k+1) * xyr_TempTransCoef(:,:,k  )

    do k = 2, kmax-1
      xyra_TempMtx(:,:,k,-1) = &
        & - CpDry * xyr_Exner(:,:,k-1) / xyz_Exner(:,:,k-1) * xyr_TempTransCoef(:,:,k-1)
      xyra_TempMtx(:,:,k, 0) =                                                          &
        & - CpDry * ( xyr_Press(:,:,k) - xyr_Press(:,:,k-1) ) / Grav / ( 2.0_DP * DelTime ) &
        & + CpDry * xyr_Exner(:,:,k-1) / xyz_Exner(:,:,k  ) * xyr_TempTransCoef(:,:,k-1)&
        & + CpDry * xyr_Exner(:,:,k  ) / xyz_Exner(:,:,k  ) * xyr_TempTransCoef(:,:,k  )
      xyra_TempMtx(:,:,k, 1) = &
        & - CpDry * xyr_Exner(:,:,k  ) / xyz_Exner(:,:,k+1) * xyr_TempTransCoef(:,:,k  )
    end do

    k = kmax
    xyra_TempMtx(:,:,k,-1) = &
      & - CpDry * xyr_Exner(:,:,k-1) / xyz_Exner(:,:,k-1) * xyr_TempTransCoef(:,:,k-1)
    xyra_TempMtx(:,:,k, 0) =                                                          &
      & - CpDry * ( xyr_Press(:,:,k) - xyr_Press(:,:,k-1) ) / Grav / ( 2.0_DP * DelTime ) &
      & + CpDry * xyr_Exner(:,:,k-1) / xyz_Exner(:,:,k  ) * xyr_TempTransCoef(:,:,k-1)
    xyra_TempMtx(:,:,k, 1) = 0.0_DP

    ! é‰›ç›´æ‹¡æ•£ã‚¹ã‚ãƒ¼ãƒ ã®è¼¸é€ä¿‚æ•°ã‹ã‚‰é™°è§£è¡Œåˆ—ã®è¨ˆç®— (æ¯”æ¹¿)
    ! Calculate implicit matrices from transfer coefficient of vertical diffusion scheme (specific humidity)
    !
    ! é£½å’Œæ¯”æ¹¿ã®è¨ˆç®—
    ! Calculate saturated specific humidity
    !
    xy_SurfQVapSat       = &
      & xy_CalcQVapSat      ( xy_SurfTemp, xyr_Press(:,:,0) )
    xy_SurfDQVapSatDTemp = &
      & xy_CalcDQVapSatDTemp( xy_SurfTemp, xy_SurfQVapSat   )

    k = 1
    if ( FlagBucketModel ) then
      xyza_QVapMtx(:,:,k,-1) =                                                  &
        & 0.0d0
      xyza_QVapMtx(:,:,k, 0) =                                                  &
        & - ( xyr_Press(:,:,k) - xyr_Press(:,:,k-1) ) / Grav / ( 2.0_DP * DelTime ) &
        & + xyr_QMixTransCoef(:,:,k  )
    else
      xyza_QVapMtx(:,:,k,-1) =                                                  &
        & - xy_SurfHumidCoef(:,:) * xy_SurfQVapTransCoef(:,:)                   &
        &   * xy_SurfDQVapSatDTemp(:,:)
      xyza_QVapMtx(:,:,k, 0) =                                                  &
        & - ( xyr_Press(:,:,k) - xyr_Press(:,:,k-1) ) / Grav / ( 2.0_DP * DelTime ) &
        & + xy_SurfHumidCoef(:,:) * xy_SurfQVapTransCoef(:,:)                   &
        & + xyr_QMixTransCoef(:,:,k  )
    end if
    xyza_QVapMtx(:,:,k, 1) =                                                  &
      & - xyr_QMixTransCoef(:,:,k  )

    do k = 2, kmax-1
      xyza_QVapMtx(:,:,k,-1) = &
        & - xyr_QMixTransCoef(:,:,k-1)
      xyza_QVapMtx(:,:,k, 0) =                                                  &
        & - ( xyr_Press(:,:,k) - xyr_Press(:,:,k-1) ) / Grav / ( 2.0_DP * DelTime ) &
        & + xyr_QMixTransCoef(:,:,k-1)                                          &
        & + xyr_QMixTransCoef(:,:,k  )
      xyza_QVapMtx(:,:,k, 1) = &
        & - xyr_QMixTransCoef(:,:,k  )
    end do

    k = kmax
    xyza_QVapMtx(:,:,k,-1) = &
      & - xyr_QMixTransCoef(:,:,k-1)
    xyza_QVapMtx(:,:,k, 0) =                                                  &
      & - ( xyr_Press(:,:,k) - xyr_Press(:,:,k-1) ) / Grav / ( 2.0_DP * DelTime ) &
      & + xyr_QMixTransCoef(:,:,k-1)
    xyza_QVapMtx(:,:,k, 1) = 0.0_DP

    ! é‰›ç›´æ‹¡æ•£ã‚¹ã‚ãƒ¼ãƒ ã®è¼¸é€ä¿‚æ•°ã‹ã‚‰é™°è§£è¡Œåˆ—ã®è¨ˆç®— (æ¯”æ¹¿ã‚’é™¤ãæ··åˆæ¯”)
    ! Calculate implicit matrices from transfer coefficient of vertical diffusion scheme (mixing ratio except for specific humidity)
    !
    k = 1
    xyza_QMixMtx(:,:,k,-1) =                                                  &
      & 0.0d0
    xyza_QMixMtx(:,:,k, 0) =                                                  &
      & - ( xyr_Press(:,:,k) - xyr_Press(:,:,k-1) ) / Grav / ( 2.0_DP * DelTime ) &
      & + xyr_QMixTransCoef(:,:,k  )
    xyza_QMixMtx(:,:,k, 1) =                                                  &
      & - xyr_QMixTransCoef(:,:,k  )

    do l = -1, 1
      do k = 2, kmax
        xyza_QMixMtx(:,:,k,l) = xyza_QVapMtx(:,:,k,l)
      end do
    end do

    ! åœ°è¡¨é¢éŽç¨‹ã®è¼¸é€ä¿‚æ•°ã‹ã‚‰é™°è§£è¡Œåˆ—ã®è¨ˆç®—
    ! Calculate implicit matrices from transfer coefficient of surface process
    !
    do i = 0, imax-1
      do j = 1, jmax
        if ( xy_SurfCond(i,j) == 1 ) then

          if ( FlagBucketModel ) then
            xyaa_SurfMtx(i,j,0,-1) =                                             &
              & 0.0_DP
            xyaa_SurfMtx(i,j,0, 0) =                                             &
              &   xy_SurfHeatCapacity(i,j) / ( 2.0_DP * DelTime )                    &
              & + CpDry * xy_SurfTempTransCoef(i,j)                              &
              & + xyra_DelRadLFlux(i,j,0,0)
          else
            xyaa_SurfMtx(i,j,0,-1) =                                             &
              & - LatentHeat * xy_SurfHumidCoef(i,j) * xy_SurfQVapTransCoef(i,j)
            xyaa_SurfMtx(i,j,0, 0) =                                             &
              &   xy_SurfHeatCapacity(i,j) / ( 2.0_DP * DelTime )                    &
              & + CpDry * xy_SurfTempTransCoef(i,j)                              &
              & + xyra_DelRadLFlux(i,j,0,0)                                      &
              & + LatentHeat * xy_SurfHumidCoef(i,j) * xy_SurfQVapTransCoef(i,j) &
              &   * xy_SurfDQVapSatDTemp(i,j)
          end if
          xyaa_SurfMtx(i,j,0, 1) =                                             &
            & - CpDry * xyr_Exner(i,j,0) / xyz_Exner(i,j,1)                    &
            &   * xy_SurfTempTransCoef(i,j)                                    &
            & + xyra_DelRadLFlux(i,j,0,1)
        else
          xyaa_SurfMtx(i,j,0,-1) = 0.0_DP
          xyaa_SurfMtx(i,j,0, 0) = 1.0_DP
          xyaa_SurfMtx(i,j,0, 1) = 0.0_DP
        end if
      end do
    end do

    ! æ±è¥¿é¢¨é€Ÿ, å—åŒ—é¢¨é€Ÿã®è¨ˆç®—
    ! Calculate eastward and northward wind
    !
    xyza_UVLUMtx = xyza_UVMtx

    call PhyImplLUDecomp3( &
      & xyza_UVLUMtx, &     ! (inout)
      & imax * jmax, kmax ) ! (in)

    do k = 1, kmax
      xyz_DUDt(:,:,k) = - ( xyr_MomFluxX(:,:,k) - xyr_MomFluxX(:,:,k-1) )
      xyz_DVDt(:,:,k) = - ( xyr_MomFluxY(:,:,k) - xyr_MomFluxY(:,:,k-1) )
    end do

    call PhyImplLUSolve3( &
      & xyz_DUDt, &            ! (inout)
      & xyza_UVLUMtx, &        ! (in)
      & 1, imax * jmax, kmax ) ! (in)

    call PhyImplLUSolve3( &
      & xyz_DVDt, &            ! (inout)
      & xyza_UVLUMtx, &        ! (in)
      & 1, imax * jmax, kmax ) ! (in)

    ! æ¸©åº¦ã¨æ¯”æ¹¿ã®è¨ˆç®—
    ! Calculate temperature and specific humidity
    !
    do l = -1, 1

      do k = 1, kmax
        xyza_TempQVapLUMtx(:,:,-k,-l) = xyza_QVapMtx(:,:,k,l)
      end do

      k = 0
      xyza_TempQVapLUMtx(:,:, k, l) = xyaa_SurfMtx(:,:,0,l)

      do k =  1,  kmax
        xyza_TempQVapLUMtx(:,:, k, l) = xyra_TempMtx(:,:,k,l)
      end do

    end do

    call PhyImplLUDecomp3( &
      & xyza_TempQVapLUMtx, &         ! (inout)
      & imax * jmax, (2 * kmax) + 1 ) ! (in)

    do k = 1, kmax
      xyz_DelTempQVap(:,:,-k) = &
        & - ( xyrf_QMixFlux(:,:,k,IndexH2OVap) - xyrf_QMixFlux(:,:,k-1,IndexH2OVap) )
    end do

    k = 0
    do j = 1, jmax
      do i = 0, imax-1
        if ( xy_SurfCond(i,j) == 1 ) then
          xyz_DelTempQVap(i,j,k) =                            &
            & - xyr_RadSFlux(i,j,0)                           &
            & - xyr_RadLFlux(i,j,0)                           &
            & - xyr_HeatFlux(i,j,0)                           &
            & - LatentHeat * xyrf_QMixFlux(i,j,0,IndexH2OVap) &
            & + xy_GroundTempFlux(i,j)
        else
          xyz_DelTempQVap(i,j,k) = 0.0_DP
        end if
      end do
    end do

    do k = 1, kmax
      xyz_DelTempQVap(:,:,k) = - ( xyr_HeatFlux(:,:, k) - xyr_HeatFlux(:,:,  k-1 ) )
    end do

    call PhyImplLUSolve3( &
      & xyz_DelTempQVap, &                ! (inout)
      & xyza_TempQVapLUMtx, &             ! (in)
      & 1, imax * jmax , (2 * kmax) + 1 ) ! (in)

    ! æ™‚é–“å¤‰åŒ–çŽ‡ã®è¨ˆç®—
    ! Calculate tendency
    !
    do k = 1, kmax
      xyz_DUDt(:,:,k)                 = xyz_DUDt(:,:,k)         / ( 2.0_DP * DelTime )
      xyz_DVDt(:,:,k)                 = xyz_DVDt(:,:,k)         / ( 2.0_DP * DelTime )
      xyz_DTempDt(:,:,k)              = xyz_DelTempQVap(:,:, k) / ( 2.0_DP * DelTime )
      xyzf_DQMixDt(:,:,k,IndexH2OVap) = xyz_DelTempQVap(:,:,-k) / ( 2.0_DP * DelTime )
    end do

    do j = 1, jmax
      do i = 0, imax-1
        if ( xy_SurfCond(i,j) == 1 ) then
          xy_DSurfTempDt(i,j) = xyz_DelTempQVap(i,j,0) / ( 2.0_DP * DelTime )
        else
          xy_DSurfTempDt(i,j) = 0.0_DP
        end if
      end do
    end do

    ! æ¯”æ¹¿ã‚’é™¤ãè³ªé‡æ··åˆæ¯”
    ! Calculate mass mixing ratio except for specific humidity
    !
    xyza_QMixLUMtx = xyza_QMixMtx

    call PhyImplLUDecomp3( &
      & xyza_QMixLUMtx,    & ! (inout)
      & imax * jmax, kmax  & ! (in)
      & )

    do n = 1, ncmax
      if ( n == IndexH2OVap ) cycle

      do k = 1, kmax
        xyzf_DQMixDt(:,:,k,n) = &
          & - ( xyrf_QMixFlux(:,:,k,n) - xyrf_QMixFlux(:,:,k-1,n) )
      end do

      call PhyImplLUSolve3(      &
        & xyzf_DQMixDt(:,:,:,n), & ! (inout)
        & xyza_QMixLUMtx,        & ! (in)
        & 1, imax * jmax, kmax   & ! (in)
        & )
    end do

    !#########################################################
    ! code for debug, this will be removed, (Y. O. Takahashi, 2009/04/07)
!!$    i = 1
!!$    j = jmax / 2
!!$    n = IndexH2OVap
!!$    write( 6, * ) &
!!$      & - xyr_RadSFlux(i,j,0),                                                      &
!!$      & - ( xyr_RadLFlux(i,j,0)                                                     &
!!$      &   + xyra_DelRadLFlux(i,j,0,0) * xy_DSurfTempDt(i,j) * ( 2.0d0 * DelTime )   &
!!$      &   + xyra_DelRadLFlux(i,j,0,1) * xyz_DTempDt(i,j,1) * ( 2.0d0 * DelTime ) ), &
!!$      & - ( xyr_HeatFlux(i,j,0)                                                     &
!!$      &   - CpDry * xyr_Exner(i,j,0) * xy_SurfTempTransCoef(i,j)                    &
!!$      &     * ( xyz_DTempDt(i,j,1) / xyz_Exner(i,j,1)                               &
!!$      &       - xy_DSurfTempDt(i,j) / xyr_Exner(i,j,0) ) * ( 2.0d0 * DelTime ) ),   &
!!$      & - LatentHeat                                                                &
!!$      &   * ( xyrf_QMixFlux(i,j,0,n)                                                &
!!$      &     - xy_SurfQVapTransCoef(i,j)                                             &
!!$      &       * ( xyzf_DQMixDt(i,j,1,n)                                             &
!!$      &         - xy_SurfDQVapSatDTemp(i,j) * xy_DSurfTempDt(i,j) )                 &
!!$      &       * ( 2.0d0 * DelTime ) ) !, &
!!$!      & + xy_GroundTempFlux(i,j)
!!$    write( 6, * ) &
!!$      & - xyr_RadSFlux(i,j,0)                                                       &
!!$      & - ( xyr_RadLFlux(i,j,0)                                                     &
!!$      &   + xyra_DelRadLFlux(i,j,0,0) * xy_DSurfTempDt(i,j) * ( 2.0d0 * DelTime )   &
!!$      &   + xyra_DelRadLFlux(i,j,0,1) * xyz_DTempDt(i,j,1) * ( 2.0d0 * DelTime ) )  &
!!$      & - ( xyr_HeatFlux(i,j,0)                                                     &
!!$      &   - CpDry * xyr_Exner(i,j,0) * xy_SurfTempTransCoef(i,j)                    &
!!$      &     * ( xyz_DTempDt(i,j,1) / xyz_Exner(i,j,1)                               &
!!$      &       - xy_DSurfTempDt(i,j) / xyr_Exner(i,j,0) ) * ( 2.0d0 * DelTime ) )    &
!!$      & - LatentHeat                                                                &
!!$      &   * ( xyrf_QMixFlux(i,j,0,n)                                                &
!!$      &     - xy_SurfQVapTransCoef(i,j)                                             &
!!$      &       * ( xyzf_DQMixDt(i,j,1,n)                                             &
!!$      &         - xy_SurfDQVapSatDTemp(i,j) * xy_DSurfTempDt(i,j) )                 &
!!$      &       * ( 2.0d0 * DelTime ) ) !, &
!!$!      & + xy_GroundTempFlux(i,j)
!!$
!!$    xy_SurfQVapSat(i,j) =                                                           &
!!$      & - xyr_RadSFlux(i,j,0)                                                       &
!!$      & - ( xyr_RadLFlux(i,j,0)                                                     &
!!$      &   + xyra_DelRadLFlux(i,j,0,0) * xy_DSurfTempDt(i,j) * ( 2.0d0 * DelTime )   &
!!$      &   + xyra_DelRadLFlux(i,j,0,1) * xyz_DTempDt(i,j,1) * ( 2.0d0 * DelTime ) )  &
!!$      & - ( xyr_HeatFlux(i,j,0)                                                     &
!!$      &   - CpDry * xyr_Exner(i,j,0) * xy_SurfTempTransCoef(i,j)                    &
!!$      &     * ( xyz_DTempDt(i,j,1) / xyz_Exner(i,j,1)                               &
!!$      &       - xy_DSurfTempDt(i,j) / xyr_Exner(i,j,0) ) * ( 2.0d0 * DelTime ) )    &
!!$      & - LatentHeat                                                                &
!!$!      &   * ( xyr_QVapFlux(i,j,0)                                                   &
!!$!      &     - xy_SurfQVapTransCoef(i,j)                                             &
!!$!      &       * ( xyz_DQVapDt(i,j,1)                                                &
!!$!      &         - xy_SurfDQVapSatDTemp(i,j) * xy_DSurfTempDt(i,j) )                 &
!!$!      &       * ( 2.0d0 * DelTime ) )
!!$      &   * xyrf_QMixFlux(i,j,0,n)
!!$    write( 6, * ) xy_SurfQVapSat(i,j)
    !#########################################################

    ! è¨ˆç®—æ™‚é–“è¨ˆæ¸¬ä¸€æ™‚åœæ¢
    ! Pause measurement of computation time
    !
    call TimesetClockStop( module_name )

  end subroutine PhyImplTendency

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

  subroutine PhyImplInit(              &
    & ArgFlagBucketModel, ArgFlagSnow  & ! (in)
    & )
    !
    ! phy_implicit ãƒ¢ã‚¸ãƒ¥ãƒ¼ãƒ«ã®åˆæœŸåŒ–ã‚’è¡Œã„ã¾ã™.
    ! NAMELIST#phy_implicit_nml ã®èªã¿è¾¼ã¿ã¯ã“ã®æ‰‹ç¶šãã§è¡Œã‚ã‚Œã¾ã™.
    !
    ! "phy_implicit" module is initialized.
    ! "NAMELIST#phy_implicit_nml" is loaded in this procedure.
    !

    ! ãƒ¢ã‚¸ãƒ¥ãƒ¼ãƒ«å¼•ç”¨ ; USE statements
    !

    ! NAMELIST ãƒ•ã‚¡ã‚¤ãƒ«å…¥åŠ›ã«é–¢ã™ã‚‹ãƒ¦ãƒ¼ãƒ†ã‚£ãƒªãƒ†ã‚£
    ! Utilities for NAMELIST file input
    !
    use namelist_util, only: namelist_filename, NmlutilMsg, NmlutilAryValid

    ! ãƒ•ã‚¡ã‚¤ãƒ«å…¥å‡ºåŠ›è£œåŠ©
    ! File I/O support
    !
    use dc_iounit, only: FileOpen

    ! ç¨®åˆ¥åž‹ãƒ‘ãƒ©ãƒ¡ã‚¿
    ! Kind type parameter
    !
    use dc_types, only: STDOUT ! æ¨™æº–å‡ºåŠ›ã®è£…ç½®ç•ªå·. Unit number of standard output

    ! æ–‡å—åˆ—æ“ä½œ
    ! Character handling
    !
    use dc_string, only: StoA

    ! ãƒ’ã‚¹ãƒˆãƒªãƒ‡ãƒ¼ã‚¿å‡ºåŠ›
    ! History data output
    !
    use gtool_historyauto, only: HistoryAutoAddVariable

    ! å®£è¨€æ–‡ ; Declaration statements
    !
    implicit none

    logical, intent(in ) :: ArgFlagBucketModel
    logical, intent(in ) :: ArgFlagSnow

    ! ä½œæ¥å¤‰æ•°
    ! Work variables
    !
!!$    integer:: unit_nml        ! NAMELIST ãƒ•ã‚¡ã‚¤ãƒ«ã‚ªãƒ¼ãƒ—ãƒ³ç”¨è£…ç½®ç•ªå·.
!!$                              ! Unit number for NAMELIST file open
!!$    integer:: iostat_nml      ! NAMELIST èªã¿è¾¼ã¿æ™‚ã® IOSTAT.
!!$                              ! IOSTAT of NAMELIST read

    ! NAMELIST å¤‰æ•°ç¾¤
    ! NAMELIST group name
    !
!!$    namelist /phy_implicit_nml/
!!$          !
!!$          ! ãƒ‡ãƒ•ã‚©ãƒ«ãƒˆå€¤ã«ã¤ã„ã¦ã¯åˆæœŸåŒ–æ‰‹ç¶š "phy_implicit#PhyImplInit"
!!$          ! ã®ã‚½ãƒ¼ã‚¹ã‚³ãƒ¼ãƒ‰ã‚’å‚ç…§ã®ã“ã¨.
!!$          !
!!$          ! Refer to source codes in the initialization procedure
!!$          ! "phy_implicit#PhyImplInit" for the default values.
!!$          !

    ! å®Ÿè¡Œæ–‡ ; Executable statement
    !

    if ( phy_implicit_inited ) return

    ! ãƒ‡ãƒ•ã‚©ãƒ«ãƒˆå€¤ã®è¨å®š
    ! 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 = phy_implicit_nml, &  ! (out)
!!$        & iostat = iostat_nml )   ! (out)
!!$      close( unit_nml )
!!$
!!$      call NmlutilMsg( iostat_nml, module_name ) ! (in)
!!$    end if

    FlagBucketModel = ArgFlagBucketModel
    FlagSnow        = ArgFlagSnow

    ! å°å— ; Print
    !
    call MessageNotify( 'M', module_name, '----- Initialization Messages -----' )
    call MessageNotify( 'M', module_name, '-- version = %c', c1 = trim(version) )

    phy_implicit_inited = .true.

  end subroutine PhyImplInit

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

end module phy_implicit