!= Module StorePotTemp_3d
!
! Authors::   SUGIYAMA Ko-ichiro, ODAKA Masatsugu
! Version::   $Id: storepottemp_3d.f90,v 1.1 2007/10/01 09:00:32 odakker Exp $ 
! Tag Name::  $Name:  $
! Copyright:: Copyright (C) GFD Dennou Club, 2006. All rights reserved.
! License::   See COPYRIGHT[link:../../COPYRIGHT]
!
!== Overview 
!
!温位に関する積算値を保管するための変数型モジュール
!
!== Error Handling
!
!== Known Bugs
!
!== Note
!
!== Future Plans
!

module StorePotTemp_3d
  !
  !温位に関する積算値を保管するための変数型モジュール.
  !
  use dc_types,  only : DP
  use dc_message,only : MessageNotify

  !モジュールの読み込み
  use gridset_3d, only:   DimXMin,     & ! x 方向の配列の下限
    &                  DimXMax,     & ! x 方向の配列の上限
    &                  DimYMin,     & ! y 方向の配列の下限
    &                  DimYMax,     & ! y 方向の配列の上限
    &                  DimZMin,     & ! z 方向の配列の下限
    &                  DimZMax,     & ! z 方向の配列の上限
    &                  SpcNum,      & ! number of species
    &                  RegXMin,     & ! x 方向の物理領域の下限
    &                  RegXMax,     & ! x 方向の物理領域の上限
    &                  RegYMin,     & ! y 方向の物理領域の下限
    &                  RegYMax        ! y 方向の物理領域の上限
  use TimeSet, only:   TimeDisp,    & ! 出力時間間隔
    &                  DelTimeLong    ! 長い時間ステップ

  use xyz_base_module, only: z_AvrXY_xyz

  !暗黙の型宣言禁止
  implicit none

  !属性の指定
  private

  !公開要素
  public StorePotTemp_Init, StorePotTempMeanXY, StorePotTempClean
  public z_Adv,  z_Turb, z_Disp, z_Diff, z_Rad, &
    &    z_Cond, z_Flux, z_Damp
  public StorePotTempAdv,  StorePotTempTurb, StorePotTempDisp, &
    &    StorePotTempDiff, StorePotTempRad,                    &
    &    StorePotTempCond, StorePotTempFlux, StorePotTempDamp

  !変数
  real(DP),allocatable :: z_Adv(:)
  real(DP),allocatable :: z_Turb(:)
  real(DP),allocatable :: z_Disp(:)
  real(DP),allocatable :: z_Diff(:)
  real(DP),allocatable :: z_Rad(:)
  real(DP),allocatable :: z_Damp(:)
  real(DP),allocatable :: z_Cond(:)
  real(DP),allocatable :: z_Flux(:)
  real(DP),allocatable :: xyz_Adv(:,:,:)
  real(DP),allocatable :: xyz_Turb(:,:,:)
  real(DP),allocatable :: xyz_Disp(:,:,:)
  real(DP),allocatable :: xyz_Diff(:,:,:)
  real(DP),allocatable :: xyz_Rad(:,:,:)
  real(DP),allocatable :: xyz_Damp(:,:,:)
  real(DP),allocatable :: xyz_Cond(:,:,:)
  real(DP),allocatable :: xyz_Flux(:,:,:)

  !save 属性
  save xyz_Adv,  xyz_Turb, xyz_Disp, xyz_Diff, xyz_Rad, &
    &  xyz_Cond, xyz_Flux, xyz_Damp
  save z_Adv,  z_Turb, z_Disp, z_Diff, z_Rad, &
    &  z_Cond, z_Flux, z_Damp

contains

  subroutine StorePotTemp_Init( )
    !初期化ルーチン

    call MessageNotify( "M", "StorePotTemp_Init", "Initialize" )

    allocate(     &
      & z_Adv (DimZMin:DimZMax), & 
      & z_Turb(DimZMin:DimZMax), &
      & z_Disp(DimZMin:DimZMax), &
      & z_Diff(DimZMin:DimZMax), &
      & z_Rad (DimZMin:DimZMax), &
      & z_Damp(DimZMin:DimZMax), &
      & z_Cond(DimZMin:DimZMax), &
      & z_Flux(DimZMin:DimZMax), &
      & xyz_Adv (DimXMin:DimXMax,DimYMin:DimYMax,DimZMin:DimZMax), & 
      & xyz_Turb(DimXMin:DimXMax,DimYMin:DimYMax,DimZMin:DimZMax), &
      & xyz_Disp(DimXMin:DimXMax,DimYMin:DimYMax,DimZMin:DimZMax), &
      & xyz_Diff(DimXMin:DimXMax,DimYMin:DimYMax,DimZMin:DimZMax), &
      & xyz_Rad (DimXMin:DimXMax,DimYMin:DimYMax,DimZMin:DimZMax), &
      & xyz_Damp(DimXMin:DimXMax,DimYMin:DimYMax,DimZMin:DimZMax), &
      & xyz_Cond(DimXMin:DimXMax,DimYMin:DimYMax,DimZMin:DimZMax), &
      & xyz_Flux(DimXMin:DimXMax,DimYMin:DimYMax,DimZMin:DimZMax)  &
      &  )

    call StorePotTempClean

  end subroutine StorePotTemp_Init


  subroutine StorePotTempClean( )
    !保管した値のクリアー
    
    z_Adv  = 0.0d0
    z_Turb = 0.0d0
    z_Diff = 0.0d0
    z_Disp = 0.0d0
    z_Rad  = 0.0d0
    z_Damp = 0.0d0
    z_Cond = 0.0d0
    z_Flux = 0.0d0

    xyz_Adv  = 0.0d0
    xyz_Turb = 0.0d0
    xyz_Diff = 0.0d0
    xyz_Disp = 0.0d0
    xyz_Rad  = 0.0d0
    xyz_Damp = 0.0d0
    xyz_Cond = 0.0d0
    xyz_Flux = 0.0d0
   
!    CalNum  = 1.0d-40    !ゼロ割を禁止. 
  end subroutine StorePotTempClean


  subroutine StorePotTempMeanXY( )
    !保管した値の水平平均値 [K/s]
    real(DP):: CalNum

    CalNum = TimeDisp / DelTimeLong 
    
    z_Adv  = a_MeanXY_aaa( xyz_Adv )   / CalNum
    z_Turb = a_MeanXY_aaa( xyz_Turb )  / CalNum
    z_Diff = a_MeanXY_aaa( xyz_Diff )  / CalNum
    z_Disp = a_MeanXY_aaa( xyz_Disp )  / CalNum
    z_Rad  = a_MeanXY_aaa( xyz_Rad  )  / CalNum
    z_Damp = a_MeanXY_aaa( xyz_Damp )  / CalNum
    z_Cond = a_MeanXY_aaa( xyz_Cond )  / CalNum
    z_Flux = a_MeanXY_aaa( xyz_Flux )  / CalNum

  end subroutine StorePotTempMeanXY


  subroutine StorePotTempAdv( Work )
    !移流項の保管

    implicit none

    real(DP),intent(in)  :: Work(DimXMin:DimXMax,DimYMin:DimYMax,DimZMin:DimZMax)
    real(DP)             :: Work2(DimXMin:DimXMax,DimYMin:DimYMax,DimZMin:DimZMax)

    Work2  = xyz_Adv + Work
    xyz_Adv = Work2

  end subroutine StorePotTempAdv


  subroutine StorePotTempTurb( Work )
    !乱流項の保管

    implicit none

    real(DP),intent(in)  :: Work(DimXMin:DimXMax,DimYMin:DimYMax,DimZMin:DimZMax)
    real(DP)             :: Work2(DimXMin:DimXMax,DimYMin:DimYMax,DimZMin:DimZMax)

    Work2   = xyz_Turb + Work
    xyz_Turb = Work2

  end subroutine StorePotTempTurb


  subroutine StorePotTempDiff( Work )
    !数値拡散項の保管

    implicit none
    
    real(DP),intent(in)  :: Work(DimXMin:DimXMax,DimYMin:DimYMax,DimZMin:DimZMax)
    real(DP)             :: Work2(DimXMin:DimXMax,DimYMin:DimYMax,DimZMin:DimZMax)

    Work2   = xyz_Diff + Work
    xyz_Diff = Work2

  end subroutine StorePotTempDiff


  subroutine StorePotTempDisp( Work )
    !散逸加熱項の保管

    implicit none
    
    real(DP),intent(in)  :: Work(DimXMin:DimXMax,DimYMin:DimYMax,DimZMin:DimZMax)
    real(DP)             :: Work2(DimXMin:DimXMax,DimYMin:DimYMax,DimZMin:DimZMax)

    Work2   = xyz_Disp + Work
    xyz_Disp = Work2

  end subroutine StorePotTempDisp


  subroutine StorePotTempRad( Work )
    !放射冷却項の保管

    implicit none
    
    real(DP),intent(in)  :: Work(DimXMin:DimXMax,DimYMin:DimYMax,DimZMin:DimZMax)
    real(DP)             :: Work2(DimXMin:DimXMax,DimYMin:DimYMax,DimZMin:DimZMax)

    Work2  = xyz_Rad + Work
    xyz_Rad = Work2

  end subroutine StorePotTempRad


  subroutine StorePotTempDamp( Work )
    !ダンピング項の保管

    implicit none
    
    real(DP),intent(in)  :: Work(DimXMin:DimXMax,DimYMin:DimYMax,DimZMin:DimZMax)
    real(DP)             :: Work2(DimXMin:DimXMax,DimYMin:DimYMax,DimZMin:DimZMax)

    Work2   = xyz_Damp + Work
    xyz_Damp = Work2

  end subroutine StorePotTempDamp


  subroutine StorePotTempCond( Work )
    !潜熱・蒸発熱の保管

    implicit none
    
    real(DP),intent(in)  :: Work(DimXMin:DimXMax,DimYMin:DimYMax,DimZMin:DimZMax)
    real(DP)             :: Work2(DimXMin:DimXMax,DimYMin:DimYMax,DimZMin:DimZMax)

    Work2   = xyz_Cond + Work
    xyz_Cond = Work2

  end subroutine StorePotTempCond


  subroutine StorePotTempFlux( Work )
    !地表面フラックスの保管

    implicit none
    
    real(DP),intent(in)  :: Work(DimXMin:DimXMax,DimYMin:DimYMax,DimZMin:DimZMax)
    real(DP)             :: Work2(DimXMin:DimXMax,DimYMin:DimYMax,DimZMin:DimZMax)

    Work2   = xyz_Flux + Work
    xyz_Flux = Work2

  end subroutine StorePotTempFlux

  function a_MeanXY_aaa( var ) 
    !
    ! 水平平均値の計算
    !
    
    !暗黙の型宣言禁止
    implicit none
    
    !変数定義
    real(DP), intent(in)  :: var(DimXMin:DimXMax,DimYMin:DimYMax,DimZMin:DimZMax)
                                     !平均演算の対象となる変数
    real(DP)              :: a_MeanXY_aaa(DimZMin:DimZMax)
                                     !水平平均値

    real(DP)              :: aa_work(DimYMin:DimYMax,DimZMin:DimZMax)
                                     !水平平均値

    aa_work = aa_MeanX_aaa(var)

    a_MeanXY_aaa = sum( aa_work(RegYMin:RegYMax, :), 1 ) &
      &           / real(RegYMax - RegYMin + 1, 8) 


  end function a_MeanXY_aaa

  function aa_MeanX_aaa( var ) 
    !
    ! 水平平均値の計算
    !
    
    !暗黙の型宣言禁止
    implicit none
    
    !変数定義
    real(DP), intent(in)  :: var(DimXMin:DimXMax,DimYMin:DimYMax,DimZMin:DimZMax)
                                     !平均演算の対象となる変数
    real(DP)             :: aa_MeanX_aaa(DimYMin:DimYMax,DimZMin:DimZMax)
                                     !水平平均値

    aa_MeanX_aaa = sum( var(RegXMin:RegXMax, :, :), 1 ) &
      &           / real(RegXMax - RegXMin + 1 , 8) 

  end function aa_MeanX_aaa
  
  
end module StorePotTemp_3d
