!= 髯ｰ隗｣豕輔↓繧医ｋ譎る俣遨榊�縺ｮ縺溘ａ縺ｮ繝ｫ繝ｼ繝√Φ
!
!= Routines for time integration with implicit scheme
!
! Authors::   Yoshiyuki O. Takahashi, Yasuhiro MORIKAWA, Yukiko YAMADA
! Version::   $Id: phy_implicit_utils.f90,v 1.3 2014/05/07 09:39:20 murashin Exp $
! Tag Name::  $Name:  $
! Copyright:: Copyright (C) GFD Dennou Club, 2008-2009. All rights reserved.
! License::   See COPYRIGHT[link:../../../COPYRIGHT]
!

module phy_implicit_utils
  !
  != 髯ｰ隗｣豕輔↓繧医ｋ譎る俣遨榊�縺ｮ縺溘ａ縺ｮ繝ｫ繝ｼ繝√Φ
  !
  != Routines for time integration with implicit scheme
  !
  ! <b>Note that Japanese and English are described in parallel.</b>
  !
  !== Procedures List
  !
  ! PhyImplEvalRadLFluxA :: 髟ｷ豕｢繝輔Λ繝�け繧ｹ陬懈ｭ｣
  ! ------------         :: ------------
  ! PhyImplEvalRadLFluxA :: Longwave flux correction
  !
  !--
  !== NAMELIST
  !
  ! NAMELIST#phy_implicit_utils_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 :: PhyImplEvalRadLFluxA, PhyImplLUDecomp3, PhyImplLUSolve3
  public :: PhyImplUtilsInit

  ! 蜈ｬ髢句､画焚
  ! Public variables
  !

  ! 髱槫�髢句､画焚
  ! Private variables
  !
  logical, save :: phy_implicit_utils_inited = .false.
                              ! 蛻晄悄險ｭ螳壹ヵ繝ｩ繧ｰ.
                              ! Initialization flag

  character(*), parameter:: module_name = 'phy_implicit_utils'
                              ! 繝｢繧ｸ繝･繝ｼ繝ｫ縺ｮ蜷咲ｧｰ.
                              ! Module name
  character(*), parameter:: version = &
    & '$Name:  $' // &
    & '$Id: phy_implicit_utils.f90,v 1.3 2014/05/07 09:39:20 murashin Exp $'
                              ! 繝｢繧ｸ繝･繝ｼ繝ｫ縺ｮ繝舌�繧ｸ繝ｧ繝ｳ
                              ! Module version

contains

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

  subroutine PhyImplEvalRadLFluxA( &
    & xyr_RadLFlux, &                                  ! (in)
    & xyz_DTempDt, xy_DSurfTempDt, xyra_DelRadLFlux, & ! (in)
    & xyr_RadLFluxA &                                  ! (out)
    & )
    !
    ! $ t-\Delta t $ 縺ｫ縺翫￠繧句､牙喧邇�ｒ蜈�↓,
    ! $ t+\Delta t $ 縺ｮ髟ｷ豕｢繝輔Λ繝�け繧ｹ (xyr_RadLFluxA) 繧堤ｮ怜�縺励∪縺�.
    !
    ! Evaluate longwave flux at $ t+\Delta t $ (xyr_RadLFluxA)
    ! from the tendency at $ t-\Delta t $ .
    !

    ! 繝｢繧ｸ繝･繝ｼ繝ｫ蠑慕畑 ; USE statements
    !

    ! 譎ょ綾邂｡逅�
    ! Time control
    !
    use timeset, only: DelTime, &  ! $ \Delta t $ [s]
      & TimesetClockStart, TimesetClockStop

    ! 螳｣險譁� ; Declaration statements
    !
    implicit none
    real(DP), intent(in):: xyr_RadLFlux (0:imax-1, 1:jmax, 0:kmax)
                              ! 髟ｷ豕｢繝輔Λ繝�け繧ｹ.
                              ! Longwave flux
    real(DP), intent(in):: xyz_DTempDt (0:imax-1, 1:jmax, 1:kmax)
                              ! $ \DP{T}{t} $ . 貂ｩ蠎ｦ螟牙喧.
                              ! Temperature tendency
    real(DP), intent(in):: xy_DSurfTempDt (0:imax-1, 1:jmax)
                              ! 蝨ｰ陦ｨ髱｢貂ｩ蠎ｦ螟牙喧邇�.
                              ! Surface temperature tendency
    real(DP), intent(in):: xyra_DelRadLFlux (0:imax-1, 1:jmax, 0:kmax,  0:1)
                              ! 髟ｷ豕｢蝨ｰ陦ｨ貂ｩ蠎ｦ螟牙喧.
                              ! Surface temperature tendency with longwave
    real(DP), intent(out):: xyr_RadLFluxA (0:imax-1, 1:jmax, 0:kmax)
                              ! $ t-\Delta t $ 縺ｫ縺翫￠繧句､牙喧邇�ｒ蜈�↓
                              ! 邂怜�縺輔ｌ縺� $ t+\Delta t $ 縺ｫ縺翫￠繧�
                              ! 髟ｷ豕｢繝輔Λ繝�け繧ｹ.
                              !
                              ! Longwave flux at $ t+\Delta t $
                              ! calculated from the tendency at
                              ! $ t-\Delta t $ .

    ! 菴懈･ｭ螟画焚
    ! Work variables
    !
    integer:: k               ! 驩帷峩譁ｹ蜷代↓蝗槭ｋ DO 繝ｫ繝ｼ繝礼畑菴懈･ｭ螟画焚
                              ! Work variables for DO loop in vertical direction

    ! 螳溯｡梧枚 ; Executable statement
    !

    ! 蛻晄悄蛹也｢ｺ隱�
    ! Initialization check
    !
    if ( .not. phy_implicit_utils_inited ) then
      call MessageNotify( 'E', module_name, 'This module has not been initialized.' )
    end if

    ! 險育ｮ玲凾髢楢ｨ域ｸｬ髢句ｧ�
    ! Start measurement of computation time
    !
    call TimesetClockStart( module_name )

    ! $ t+\Delta t $ 縺ｮ髟ｷ豕｢繝輔Λ繝�け繧ｹ (xyr_RadLFluxA) 繧堤ｮ怜�
    ! Evaluate longwave flux at $ t+\Delta t $ (xyr_RadLFluxA)
    !
    do k = 0, kmax
      xyr_RadLFluxA(:,:,k) = &
        &   xyr_RadLFlux(:,:,k) &
        & + (   xy_DSurfTempDt     * xyra_DelRadLFlux(:,:,k,0)   &
        &     + xyz_DTempDt(:,:,1) * xyra_DelRadLFlux(:,:,k,1) ) &
        &   * 2.0_DP * DelTime
    end do

    ! 險育ｮ玲凾髢楢ｨ域ｸｬ荳譎ょ●豁｢
    ! Pause measurement of computation time
    !
    call TimesetClockStop( module_name )

  end subroutine PhyImplEvalRadLFluxA

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

  subroutine PhyImplLUDecomp3( &
    & jna_LUMtx, &            ! (inout)
    & JDim, NDim &            ! (in)
    & )
    !
    ! 3 驥榊ｯｾ隗定｡悟�縺ｮ LU 蛻�ｧ｣繧定｡後＞縺ｾ縺�.
    !
    ! LU decomposition of triple diagonal matrix.
    !

    ! 螳｣險譁� ; Declaration statements
    !
    implicit none
    integer, intent(in):: JDim
    integer, intent(in):: NDim
    real(DP), intent(inout):: jna_LUMtx(JDim, NDim, -1:1)
                              ! LU 陦悟�.
                              ! LU matrix

    ! 菴懈･ｭ螟画焚
    ! Work variables
    !
    integer:: j, n            ! DO 繝ｫ繝ｼ繝礼畑菴懈･ｭ螟画焚
                              ! Work variables for DO loop

    ! 螳溯｡梧枚 ; Executable statement
    !

    ! LU 蛻�ｧ｣
    ! LU decomposition
    !
    do j = 1, JDim
      jna_LUMtx(j,1,1) = jna_LUMtx(j,1,1) / jna_LUMtx(j,1,0)
    end do

    do n = 2, NDim-1
      do j = 1, JDim
        jna_LUMtx(j,n,0)  =   jna_LUMtx(j,n,0) &
          &                 - jna_LUMtx(j,n,-1) * jna_LUMtx(j,n-1,1)

        jna_LUMtx(j,n,1)  =   jna_LUMtx(j,n,1) / jna_LUMtx(j,n,0)
      end do
    end do

    do j = 1, JDim
      jna_LUMtx(j,NDim,0) =   jna_LUMtx(j,NDim, 0) &
        &                   - jna_LUMtx(j,NDim,-1) * jna_LUMtx(j,NDim-1,1)
    end do

  end subroutine PhyImplLUDecomp3

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

  subroutine PhyImplLUSolve3( &
    & ijn_Vector, &       ! (inout)
    & jna_LUMtx, &        ! (in)
    & IDim, JDim, NDim &  ! (in)
    & )
    !
    ! LU 蛻�ｧ｣縺ｫ繧医ｋ隗｣縺ｮ險育ｮ� (3驥榊ｯｾ隗定｡悟�逕ｨ) 繧定｡後＞縺ｾ縺�.
    !
    ! Solve with LU decomposition (For triple diagonal matrix).
    !

    ! 螳｣險譁� ; Declaration statements
    !
    implicit none
    integer, intent(in):: IDim
    integer, intent(in):: JDim
    integer, intent(in):: NDim
    real(DP), intent(in):: jna_LUMtx(JDim, NDim, -1:1)
                              ! LU 陦悟�.
                              ! LU matrix
    real(DP), intent(inout):: ijn_Vector(IDim, JDim, NDim)
                              ! 蜿ｳ霎ｺ繝吶け繝医Ν / 隗｣.
                              ! Right-hand side vector / solution

    ! 菴懈･ｭ螟画焚
    ! Work variables
    !
    integer:: i, j, n         ! DO 繝ｫ繝ｼ繝礼畑菴懈･ｭ螟画焚
                              ! Work variables for DO loop

    ! 螳溯｡梧枚 ; Executable statement
    !

    ! 蜑埼ｲ莉｣蜈･
    ! Forward substitution
    !
    do i = 1, IDim
      do j = 1, JDim
        ijn_Vector(i,j,1) = ijn_Vector(i,j,1) / jna_LUMtx(j,1,0)
      end do
    end do

    do n = 2, NDim
      do i = 1, IDim
        do j = 1, JDim
          ijn_Vector(i,j,n) = (   ijn_Vector(i,j,n) &
            &                   - ijn_Vector(i,j,n-1) * jna_LUMtx(j,n,-1) &
            &                  ) / jna_LUMtx(j,n,0)
        end do
      end do
    end do

    ! 蠕碁莉｣蜈･
    ! Backward substitution
    !
    do n = NDim-1, 1, -1
      do i = 1, IDim
        do j = 1, JDim
          ijn_Vector(i,j,n) =   ijn_Vector(i,j,n) &
            &                 - ijn_Vector(i,j,n+1) * jna_LUMtx(j,n,1)
        end do
      end do
    end do

  end subroutine PhyImplLUSolve3

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

  subroutine PhyImplUtilsInit
    !
    ! phy_implicit_utils 繝｢繧ｸ繝･繝ｼ繝ｫ縺ｮ蛻晄悄蛹悶ｒ陦後＞縺ｾ縺�.
    ! NAMELIST#phy_implicit_nml 縺ｮ隱ｭ縺ｿ霎ｼ縺ｿ縺ｯ縺薙�謇狗ｶ壹″縺ｧ陦後ｏ繧後∪縺�.
    !
    ! "phy_implicit_utils" module is initialized.
    ! "NAMELIST#phy_implicit_utils_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

    ! 菴懈･ｭ螟画焚
    ! 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_utils_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

    ! 蜊ｰ蟄� ; Print
    !
    call MessageNotify( 'M', module_name, '----- Initialization Messages -----' )
    call MessageNotify( 'M', module_name, '-- version = %c', c1 = trim(version) )

    phy_implicit_utils_inited = .true.

  end subroutine PhyImplUtilsInit

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

end module phy_implicit_utils