!= åœ°ä¸‹ã«ãŠã‘ã‚‹ç†±ã®é‰›ç›´æ‹¡æ•£
!
!= Vertical diffusion of heat under the ground
!
! Authors::   Yoshiyuki O. Takahashi
! Version::   $Id: subsurface_diffusion_heat.f90,v 1.10 2015/01/29 12:07:59 yot Exp $
! Tag Name::  $Name:  $
! Copyright:: Copyright (C) GFD Dennou Club, 2008-2009. All rights reserved.
! License::   See COPYRIGHT[link:../../../COPYRIGHT]
!

module subsurface_diffusion_heat
  !
  != åœ°ä¸‹ã®é‰›ç›´æ‹¡æ•£
  !
  != Vertical diffusion under the ground
  !
  ! <b>Note that Japanese and English are described in parallel.</b>
  !
  !== Procedures List
  !
  ! SubsurfaceDiffusion     :: ç†±ä¼å°Žãƒ•ãƒ©ãƒƒã‚¯ã‚¹ã®è¨ˆç®—
  ! SubsurfaceDiffusionInit :: åˆæœŸåŒ–ã‚µãƒ–ãƒ«ãƒ¼ãƒãƒ³
  ! ----------------------- :: ------------
  ! SubsurfaceDiffusion     :: Calculate thermal conduction flux
  ! SubsurfaceDiffusionInit :: Initialization
  !
  !--
  !== NAMELIST
  !
  ! NAMELIST#subsurface_diffusion_heat_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
    &                  kslmax  ! åœ°ä¸‹ã®é‰›ç›´å±¤æ•°.
                               ! Number of subsurface vertical level

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

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

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

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

contains

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

!!$  subroutine SubsurfaceDiffusionFlagLand(  &
!!$    & xy_SurfType,                         & ! (in)
!!$    & xy_FlagMatthewsLand                  & ! (out)
!!$    & )
!!$    !
!!$    !
!!$    !
!!$    ! Set index for calculation method from Matthews' index
!!$    !
!!$
!!$    ! ãƒ¢ã‚¸ãƒ¥ãƒ¼ãƒ«å¼•ç”¨ ; USE statements
!!$    !
!!$
!!$    ! é›ªã¨æµ·æ°·ã®å®šæ•°ã®è¨å®š
!!$    ! Setting constants of snow and sea ice
!!$    !
!!$    use constants_snowseaice, only: &
!!$      & SeaIceThreshold
!!$
!!$
!!$    ! å®£è¨€æ–‡ ; Declaration statements
!!$    !
!!$
!!$    integer , intent(in ) :: xy_SurfType        (0:imax-1, 1:jmax)
!!$                              ! åœŸåœ°åˆ©ç”¨.
!!$                              ! Surface index
!!$    logical , intent(out) :: xy_FlagMatthewsLand(0:imax-1, 1:jmax)
!!$                              !
!!$                              ! Flag for land grid box
!!$
!!$
!!$    ! ä½œæ¥å¤‰æ•°
!!$    ! Work variables
!!$    !
!!$
!!$    integer:: i               ! çµŒåº¦æ–¹å‘ã«å›žã‚‹ DO ãƒ«ãƒ¼ãƒ—ç”¨ä½œæ¥å¤‰æ•°
!!$                              ! Work variables for DO loop in longitude
!!$    integer:: j               ! ç·¯åº¦æ–¹å‘ã«å›žã‚‹ DO ãƒ«ãƒ¼ãƒ—ç”¨ä½œæ¥å¤‰æ•°
!!$                              ! Work variables for DO loop in latitude
!!$
!!$
!!$    ! å®Ÿè¡Œæ–‡ ; Executable statement
!!$    !
!!$
!!$    ! åˆæœŸåŒ–ç¢ºèª
!!$    ! Initialization check
!!$    !
!!$    if ( .not. subsurface_diffusion_inited ) then
!!$      call MessageNotify( 'E', module_name, 'This module has not been initialized.' )
!!$    end if
!!$
!!$    !
!!$    ! Set flag for land
!!$    !
!!$    do j = 1, jmax
!!$      do i = 0, imax-1
!!$        if ( xy_SurfType(i,j) >= 1 ) then
!!$          ! land
!!$          xy_FlagMatthewsLand(i,j) = .true.
!!$        else
!!$          ! others
!!$          xy_FlagMatthewsLand(i,j) = .false.
!!$        end if
!!$      end do
!!$    end do
!!$
!!$
!!$  end subroutine SubsurfaceDiffusionFlagLand

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

  subroutine SubsurfaceDiffusion(              &
    & xy_DeepSubSurfHeatFlux,                  &          ! (in )
    & xy_SoilHeatCap, xy_SoilHeatDiffCoef,     &          ! (in )
    & xy_SurfTemp, xyz_SoilTemp, xy_SurfSnowB, &          ! (in )
    & xyr_SoilTempTransCoef, xyr_SoilHeatFlux  &          ! (out)
    & )
    !
    ! æ™‚é–“å¤‰åŒ–çŽ‡ã®è¨ˆç®—ã‚’è¡Œã„ã¾ã™.
    !
    ! Calculate tendencies.
    !

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

    ! æ™‚åˆ»ç®¡ç†
    ! Time control
    !
    use timeset, only: &
      & TimesetClockStart, TimesetClockStop

    ! åº§æ¨™ãƒ‡ãƒ¼ã‚¿è¨å®š
    ! Axes data settings
    !
    use axesset, only: &
      r_SSDepth, & ! subsurface grid on interface of layer
      z_SSDepth    ! subsurface grid at midpoint of layer

    ! é›ªã¨æµ·æ°·ã®å®šæ•°ã®è¨å®š
    ! Setting constants of snow and sea ice
    !
    use constants_snowseaice, only: &
      & SnowDens,            &
      & SnowMaxThermDepth,   &
      & SnowThermCondCoef

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

    real(DP), intent(in ):: xy_DeepSubSurfHeatFlux (0:imax-1, 1:jmax)
                              ! åœ°ä¸ç†±ãƒ•ãƒ©ãƒƒã‚¯ã‚¹.
                              ! "Deep subsurface heat flux"
                              ! Heat flux at the bottom of surface/soil layer.
    real(DP), intent(in ):: xy_SoilHeatCap (0:imax-1, 1:jmax)
                              ! åœŸå£Œç†±å®¹é‡ (J K-1 kg-1)
                              ! Specific heat of soil (J K-1 kg-1)
    real(DP), intent(in ):: xy_SoilHeatDiffCoef (0:imax-1, 1:jmax)
                              ! åœŸå£Œç†±ä¼å°Žä¿‚æ•° (J m-3 K-1)
                              ! Heat conduction coefficient of soil (J m-3 K-1)
    real(DP), intent(in ):: xy_SurfTemp (0:imax-1, 1:jmax)
                              ! åœ°è¡¨é¢æ¸©åº¦.
                              ! Surface temperature
    real(DP), intent(in ):: xyz_SoilTemp (0:imax-1, 1:jmax, 1:kslmax)
                              ! åœŸå£Œæ¸©åº¦ (K)
                              ! Soil temperature (K)
    real(DP), intent(in ):: xy_SurfSnowB (0:imax-1, 1:jmax)
                              ! ç©é›ªé‡.
                              ! Surface snow amount.
    real(DP), intent(out):: xyr_SoilTempTransCoef (0:imax-1, 1:jmax, 0:kslmax)
                              ! è¼¸é€ä¿‚æ•°ï¼šåœŸå£Œæ¸©åº¦.
                              ! Transfer coefficient: soil temperature
    real(DP), intent(out):: xyr_SoilHeatFlux (0:imax-1, 1:jmax, 0:kslmax)
                              ! åœŸå£Œä¸ã®ç†±ãƒ•ãƒ©ãƒƒã‚¯ã‚¹.
                              ! Heat flux in sub-surface soil

    ! ä½œæ¥å¤‰æ•°
    ! Work variables
    !
    integer:: k               ! é‰›ç›´æ–¹å‘ã«å›žã‚‹ DO ãƒ«ãƒ¼ãƒ—ç”¨ä½œæ¥å¤‰æ•°
                              ! Work variables for DO loop in vertical direction

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

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

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

    ! åœŸå£Œæ¸©åº¦è¨ˆç®—ç”¨ã®è¼¸é€ä¿‚æ•°ã®è¨ˆç®—
    ! Calculate transfer coefficient for heat diffusion in the soil
    !
    k = 0
    if ( kslmax == 0 ) then
      ! This line is used when kslmax == 0, because z_SSDepth(k+1) does not exist.
      xyr_SoilTempTransCoef(:,:,k) = 0.0_DP
    else
!!$      xyr_SoilTempTransCoef(:,:,k) = &
!!$        & xy_SoilHeatDiffCoef(:,:) / ( z_SSDepth(k+1) - 0.0_DP )
      xyr_SoilTempTransCoef(:,:,k) =                                          &
        & (   ( z_SSDepth(k+1) - 0.0_DP ) / xy_SoilHeatDiffCoef               &
        &   - min( max( xy_SurfSnowB / SnowDens, 0.0_DP ), SnowMaxThermDepth )&
        &     / SnowThermCondCoef )**(-1)

    end if
    do k = 1, kslmax-1
      xyr_SoilTempTransCoef(:,:,k) = &
        & xy_SoilHeatDiffCoef(:,:) / ( z_SSDepth(k+1) - z_SSDepth(k) )
    end do
    k = kslmax
    xyr_SoilTempTransCoef(:,:,k) = 0.0_DP

    ! åœŸå£Œä¸ã®ç†±ãƒ•ãƒ©ãƒƒã‚¯ã‚¹ã®è¨ˆç®—
    ! Calculate heat flux in sub-surface soil
    !
    k = 0
    if ( kslmax == 0 ) then
      ! This line is used when kslmax == 0, because xyz_SoilTemp(:,:,k+1) does not exist.
      xyr_SoilHeatFlux(:,:,k) = 0.0_DP
    else
      xyr_SoilHeatFlux(:,:,k) = &
        & - xyr_SoilTempTransCoef(:,:,k) * ( xyz_SoilTemp(:,:,1) - xy_SurfTemp(:,:) )
    end if
    do k = 1, kslmax-1
      xyr_SoilHeatFlux(:,:,k) =                               &
        & - xyr_SoilTempTransCoef(:,:,k)                      &
        &   * ( xyz_SoilTemp(:,:,k+1) - xyz_SoilTemp(:,:,k) )
    end do
    k = kslmax
    xyr_SoilHeatFlux(:,:,k) = xy_DeepSubSurfHeatFlux

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

  end subroutine SubsurfaceDiffusion

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

  subroutine SubsurfaceDiffusionInit
    !
    ! subsurface_diffusion_heat ãƒ¢ã‚¸ãƒ¥ãƒ¼ãƒ«ã®åˆæœŸåŒ–ã‚’è¡Œã„ã¾ã™.
    ! NAMELIST#subsurface_diffusion_heat_nml ã®èªã¿è¾¼ã¿ã¯ã“ã®æ‰‹ç¶šãã§è¡Œã‚ã‚Œã¾ã™.
    !
    ! "subsurface_diffusion_heat" module is initialized.
    ! "NAMELIST#subsurface_diffusion_heat_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

    ! ãƒ¡ãƒƒã‚»ãƒ¼ã‚¸å‡ºåŠ›
    ! Message output
    !
    use dc_message, only: MessageNotify

    ! å®£è¨€æ–‡ ; 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

!!$    real(DP) :: SoilHeatCap
!!$    real(DP) :: SoilHeatDiffCoef

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

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

    if ( subsurface_diffusion_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 = subsurface_diffusion_heat_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) )

    subsurface_diffusion_inited = .true.

  end subroutine SubsurfaceDiffusionInit

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

end module subsurface_diffusion_heat