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

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

  !モジュールの読み込み
  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 StoreNudging_Init, StoreNudgingMeanX, StoreNudgingClean
  public za_Nudging, xza_Nudging
  public StoreNudgingMixRt

  !公開変数
  real(8), allocatable :: za_Nudging(:,:)
  real(8), allocatable :: xza_Nudging(:,:,:)

  save xza_Nudging
  save za_Nudging
  
contains

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

    allocate(     &
      & za_Nudging(DimZMin:DimZMax, SpcNum),   & 
      & xza_Nudging(DimXMin:DimXMax, DimZMin:DimZMax, SpcNum)   & 
      &  )
    
    call StoreNudgingClean()

  end subroutine StoreNudging_Init


  subroutine StoreNudgingClean( )
    !保管した値のクリアー
    
    za_Nudging  = 0.0d0

    xza_Nudging  = 0.0d0

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


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

    CalNum = TimeDisp / DelTimeLong
   
    za_Nudging  = aa_MeanX_aaa( xza_Nudging ) / CalNum

  end subroutine StoreNudgingMeanX


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

    implicit none

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

    Work2  = xza_Nudging + Work
    xza_Nudging = Work2

!    CalNum = CalNum + 1

  end subroutine StoreNudgingMixRt

  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 StoreNudging
