!= Module StoreMixRt
!
! Authors::   SUGIYAMA Ko-ichiro
! Version::   $Id: storemixrt.f90,v 1.1 2009-03-05 05:39:43 yamasita Exp $ 
! Tag Name::  $Name: arare4-20120511 $
! Copyright:: Copyright (C) GFD Dennou Club, 2006. All rights reserved.
! License::   See COPYRIGHT[link:../../COPYRIGHT]
!
!== Overview 
!
!混合比に関する積算値を保管するための変数型モジュール
!
!== Error Handling
!
!== Known Bugs
!
!== Note
!
!== Future Plans
!

module StoreMixRt
  !
  !積算値を保管するための変数型モジュール.
  !

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

  !暗黙の型宣言禁止
  implicit none

  !属性の指定
  private

  !公開要素
  public StoreMixRt_Init, StoreMixRtMeanX, StoreMixRtClean
  public za_Adv,  za_Turb,  za_Diff, za_Flux, za_Rain, &
    &    za_Cond, za_Fill1, za_Fill2
  public StoreMixRtAdv, StoreMixRtTurb, StoreMixRtDiff, StoreMixRtFlux, StoreMixRtRain, &
    &    StoreMixRtCond, StoreMixRtFill1, StoreMixRtFill2

  !公開変数
  real(8), allocatable :: za_Adv(:,:)
  real(8), allocatable :: za_Turb(:,:)
  real(8), allocatable :: za_Diff(:,:)
  real(8), allocatable :: za_Flux(:,:)
  real(8), allocatable :: za_Rain(:,:)
  real(8), allocatable :: za_Fill1(:,:)
  real(8), allocatable :: za_Fill2(:,:)
  real(8), allocatable :: za_Cond(:,:)
!  real(8), allocatable :: za_Asln(:,:)
  real(8), allocatable :: xza_Adv(:,:,:)
  real(8), allocatable :: xza_Turb(:,:,:)
  real(8), allocatable :: xza_Diff(:,:,:)
  real(8), allocatable :: xza_Flux(:,:,:)
  real(8), allocatable :: xza_Rain(:,:,:)
  real(8), allocatable :: xza_Fill1(:,:,:)
  real(8), allocatable :: xza_Fill2(:,:,:)
  real(8), allocatable :: xza_Cond(:,:,:)
!  real(8), allocatable :: xza_Asln(:,:,:)
!  real(8)              :: CalNum

  save xza_Adv,  xza_Turb,  xza_Diff, xza_Flux, xza_Rain, &
    &  xza_Cond, xza_Fill1, xza_Fill2
  save za_Adv,  za_Turb,  za_Diff, za_Flux, za_Rain, &
    &  za_Cond, za_Fill1, za_Fill2
  
contains

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

    allocate(     &
      & za_Adv(DimZMin:DimZMax, SpcNum),   & 
      & za_Turb(DimZMin:DimZMax, SpcNum),  &
      & za_Diff(DimZMin:DimZMax, SpcNum),  &
      & za_Flux(DimZMin:DimZMax, SpcNum),  &
      & za_Rain(DimZMin:DimZMax, SpcNum),  &
      & za_Fill1(DimZMin:DimZMax, SpcNum), &
      & za_Fill2(DimZMin:DimZMax, SpcNum), &
!      & za_Asln(DimZMin:DimZMax, SpcNum), &
      & za_Cond(DimZMin:DimZMax, SpcNum), &
      & xza_Adv(DimXMin:DimXMax, DimZMin:DimZMax, SpcNum),   & 
      & xza_Turb(DimXMin:DimXMax, DimZMin:DimZMax, SpcNum),  &
      & xza_Diff(DimXMin:DimXMax, DimZMin:DimZMax, SpcNum),  &
      & xza_Flux(DimXMin:DimXMax, DimZMin:DimZMax, SpcNum),  &
      & xza_Rain(DimXMin:DimXMax, DimZMin:DimZMax, SpcNum),  &
      & xza_Fill1(DimXMin:DimXMax, DimZMin:DimZMax, SpcNum), &
      & xza_Fill2(DimXMin:DimXMax, DimZMin:DimZMax, SpcNum), &
!      & xza_Asln(DimXMin:DimXMax, DimZMin:DimZMax, SpcNum), &
      & xza_Cond(DimXMin:DimXMax, DimZMin:DimZMax, SpcNum) &
      &  )
    
    call StoreMixRtClean()

  end subroutine StoreMixRt_Init


  subroutine StoreMixRtClean( )
    !保管した値のクリアー
    
    za_Adv  = 0.0d0
    za_Turb = 0.0d0
    za_Diff = 0.0d0
    za_Flux = 0.0d0
    za_Rain = 0.0d0
    za_Fill1 = 0.0d0
    za_Fill2 = 0.0d0
    za_Cond = 0.0d0
!    za_Asln = 0.0d0

    xza_Adv  = 0.0d0
    xza_Turb = 0.0d0
    xza_Diff = 0.0d0
    xza_Flux = 0.0d0
    xza_Rain = 0.0d0
    xza_Fill1 = 0.0d0
    xza_Fill2 = 0.0d0
    xza_Cond = 0.0d0
!    xza_Asln = 0.0d0

!    CalNum  = 1.0d-40   !ゼロ割を禁止
  end subroutine StoreMixRtClean


  subroutine StoreMixRtMeanX( )
    !保管した値の水平平均値
 
    real(8) :: CalNum

    CalNum = TimeDisp / DelTimeLong
   
    za_Adv  = aa_MeanX_aaa( xza_Adv ) / CalNum
    za_Turb = aa_MeanX_aaa( xza_Turb ) / CalNum
    za_Diff = aa_MeanX_aaa( xza_Diff ) / CalNum
    za_Flux = aa_MeanX_aaa( xza_Flux ) / CalNum
    za_Rain = aa_MeanX_aaa( xza_Rain ) / CalNum
    za_Fill1 = aa_MeanX_aaa( xza_Fill1  ) / CalNum
    za_Fill2 = aa_MeanX_aaa( xza_Fill2 ) / CalNum
    za_Cond = aa_MeanX_aaa( xza_Cond ) / CalNum
!    za_Asln = aa_MeanX_aaa( xza_Asln ) / CalNum

  end subroutine StoreMixRtMeanX


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

    implicit none

    real(8), intent(in)  :: Work(DimXMin:DimXMax, DimZMin:DimZMax, SpcNum)
    real(8)              :: Work2(DimXMin:DimXMax, DimZMin:DimZMax, SpcNum)

    Work2  = xza_Adv + Work
    xza_Adv = Work2

!    CalNum = CalNum + 1

  end subroutine StoreMixRtAdv


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

    implicit none

    real(8), intent(in)  :: Work(DimXMin:DimXMax, DimZMin:DimZMax, SpcNum)
    real(8)              :: Work2(DimXMin:DimXMax, DimZMin:DimZMax, SpcNum)

    Work2   = xza_Turb + Work
    xza_Turb = Work2

  end subroutine StoreMixRtTurb


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

    implicit none
    
    real(8), intent(in)  :: Work(DimXMin:DimXMax, DimZMin:DimZMax, SpcNum)
    real(8)              :: Work2(DimXMin:DimXMax, DimZMin:DimZMax, SpcNum)

    Work2   = xza_Diff + Work
    xza_Diff = Work2

  end subroutine StoreMixRtDiff


  subroutine StoreMixRtRain( Work )
    !降雨の重力落下に伴う変化

    implicit none
    
    real(8), intent(in)  :: Work(DimXMin:DimXMax, DimZMin:DimZMax, SpcNum)
    real(8)              :: Work2(DimXMin:DimXMax, DimZMin:DimZMax, SpcNum)

    Work2   = xza_Rain + Work
    xza_Rain = Work2

  end subroutine StoreMixRtRain


  subroutine StoreMixRtFlux( Work )
    !地表面フラックスによる変化

    implicit none
    
    real(8), intent(in)  :: Work(DimXMin:DimXMax, DimZMin:DimZMax, SpcNum)
    real(8)              :: Work2(DimXMin:DimXMax, DimZMin:DimZMax, SpcNum)

    Work2  = xza_Flux + Work
    xza_Flux = Work2

  end subroutine StoreMixRtFlux


  subroutine StoreMixRtCond( Work )
    !凝結に伴う変化

    implicit none
    
    real(8), intent(in)  :: Work(DimXMin:DimXMax, DimZMin:DimZMax, SpcNum)
    real(8)              :: Work2(DimXMin:DimXMax, DimZMin:DimZMax, SpcNum)

    Work2   = xza_Cond + Work
    xza_Cond = Work2

  end subroutine StoreMixRtCond


!  subroutine StoreMixRtAsln( Work )
!    !アッセリン時間フィルタに伴う変化量を保管
!
!    implicit none
!    
!    real(8), intent(in)  :: Work(DimXMin:DimXMax, DimZMin:DimZMax, SpcNum)
!    real(8)              :: Work2(DimXMin:DimXMax, DimZMin:DimZMax, SpcNum)
!
!    Work2   = xza_Asln + Work
!    xza_Asln = Work2
!
!  end subroutine StoreMixRtAsln


  subroutine StoreMixRtFill1( Work1 )
    !穴埋めプログラムによる変化

    implicit none
    
    real(8), intent(in)  :: Work1(DimXMin:DimXMax, DimZMin:DimZMax, SpcNum)
    real(8)              :: WorkA(DimXMin:DimXMax, DimZMin:DimZMax, SpcNum)

    WorkA     = Work1 + xza_Fill1
    xza_Fill1 = WorkA

  end subroutine StoreMixRtFill1


  subroutine StoreMixRtFill2( Work1 )
    !穴埋めプログラム(2)による変化

    implicit none
    
    real(8), intent(in)  :: Work1(DimXMin:DimXMax, DimZMin:DimZMax, SpcNum)
    real(8)              :: WorkA(DimXMin:DimXMax, DimZMin:DimZMax, SpcNum)

    WorkA      = Work1 + xza_Fill2
    xza_Fill2  = WorkA

  end subroutine StoreMixRtFill2


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

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