!---------------------------------------------------------------------
!     Copyright (C) GFD Dennou Club, 2004. All rights reserved.
!---------------------------------------------------------------------
                                                                 !=begin
!= Module Average
!
!   * Developer: SUGIYAMA Ko-ichiro (sugiyama@gfd-dennou.org)
!   * Version: $Id: average.f90,v 1.2.2.1 2005/08/05 01:59:25 kitamo Exp $ 
!   * Tag Name: $Name: arare3m $
!   * Change History: 
!
!== Overview 
!
!物理量の平均操作を行うための一連の関数をまとめたパッケージ型モジュール. 
!水平 Arakawa-C, 鉛直 Lorentz グリッドとする. 
!
!== Error Handling
!
!== Known Bugs
!
!== Note
!
!== Future Plans
!
                                                                 !=end

module average

  interface ss_avr_ss
     function ss_avr_ss(var)
       use gridset
       real(8), intent(in)  :: var(DimXMin:DimXMax, DimZMin:DimZMax)
       real(8)              :: ss_avr_ss(DimXMin:DimXMax, DimZMin:DimZMax)
     end function ss_avr_ss
  end interface

  interface ss_avr_fs
     function ss_avr_fs(var) 
       use gridset
       real(8), intent(in)  :: var(DimXMin:DimXMax, DimZMin:DimZMax)
       real(8)              :: ss_avr_fs(DimXMin:DimXMax, DimZMin:DimZMax)
     end function ss_avr_fs
  end interface
  
  interface ss_avr_sf
     function ss_avr_sf(var)
       use gridset
       real(8), intent(in)  :: var(DimXMin:DimXMax, DimZMin:DimZMax)
       real(8)              :: ss_avr_sf(DimXMin:DimXMax, DimZMin:DimZMax)
     end function ss_avr_sf
  end interface
  
  interface ss_avr_ff
     function ss_avr_ff(var)
       use gridset
       real(8), intent(in)  :: var(DimXMin:DimXMax, DimZMin:DimZMax)
       real(8)              :: ss_avr_ff(DimXMin:DimXMax, DimZMin:DimZMax)
     end function ss_avr_ff
  end interface
  
  interface fs_avr_ss
     function fs_avr_ss(var) 
       use gridset
       real(8), intent(in)  :: var(DimXMin:DimXMax, DimZMin:DimZMax)
       real(8)              :: fs_avr_ss(DimXMin:DimXMax, DimZMin:DimZMax)
     end function fs_avr_ss
  end interface

  interface fs_avr_fs
     function fs_avr_fs(var) 
       use gridset
       real(8), intent(in)  :: var(DimXMin:DimXMax, DimZMin:DimZMax)
       real(8)              :: fs_avr_fs(DimXMin:DimXMax, DimZMin:DimZMax)
     end function fs_avr_fs
  end interface

  interface fs_avr_sf
     function fs_avr_sf(var) 
       use gridset
       real(8), intent(in)  :: var(DimXMin:DimXMax, DimZMin:DimZMax)
       real(8)              :: fs_avr_sf(DimXMin:DimXMax, DimZMin:DimZMax)
     end function fs_avr_sf
  end interface

  interface fs_avr_ff
     function fs_avr_ff(var) 
       use gridset
       real(8), intent(in)  :: var(DimXMin:DimXMax, DimZMin:DimZMax)
       real(8)              :: fs_avr_ff(DimXMin:DimXMax, DimZMin:DimZMax)
     end function fs_avr_ff
  end interface

  interface sf_avr_ss
     function sf_avr_ss(var) 
       use gridset
       real(8), intent(in)  :: var(DimXMin:DimXMax, DimZMin:DimZMax)
       real(8)              :: sf_avr_ss(DimXMin:DimXMax, DimZMin:DimZMax)
     end function sf_avr_ss
  end interface

  interface sf_avr_fs
     function sf_avr_fs(var) 
       use gridset
       real(8), intent(in)  :: var(DimXMin:DimXMax, DimZMin:DimZMax)
       real(8)              :: sf_avr_fs(DimXMin:DimXMax, DimZMin:DimZMax)
     end function sf_avr_fs
  end interface

  interface sf_avr_sf
     function sf_avr_sf(var) 
       use gridset
       real(8), intent(in)  :: var(DimXMin:DimXMax, DimZMin:DimZMax)
       real(8)              :: sf_avr_sf(DimXMin:DimXMax, DimZMin:DimZMax)
     end function sf_avr_sf
  end interface

  interface sf_avr_ff
     function sf_avr_ff(var) 
       use gridset
       real(8), intent(in)  :: var(DimXMin:DimXMax, DimZMin:DimZMax)
       real(8)              :: sf_avr_ff(DimXMin:DimXMax, DimZMin:DimZMax)
     end function sf_avr_ff
  end interface

  interface ff_avr_ss
     function ff_avr_ss(var) 
       use gridset
       real(8), intent(in)  :: var(DimXMin:DimXMax, DimZMin:DimZMax)
       real(8)              :: ff_avr_ss(DimXMin:DimXMax, DimZMin:DimZMax)
     end function ff_avr_ss
  end interface

  interface ff_avr_sf
     function ff_avr_sf(var) 
       use gridset
       real(8), intent(in)  :: var(DimXMin:DimXMax, DimZMin:DimZMax)
       real(8)              :: ff_avr_sf(DimXMin:DimXMax, DimZMin:DimZMax)
     end function ff_avr_sf
  end interface

  interface ff_avr_fs
     function ff_avr_fs(var) 
       use gridset
       real(8), intent(in)  :: var(DimXMin:DimXMax, DimZMin:DimZMax)
       real(8)              :: ff_avr_fs(DimXMin:DimXMax, DimZMin:DimZMax)
     end function ff_avr_fs
  end interface

end module average



!!!
!!!スカラーの位置での平均操作
!!!  
function ss_avr_ss(var)
  use gridset
  implicit none
  
  real(8), intent(in)  :: var(DimXMin:DimXMax, DimZMin:DimZMax)
  real(8)              :: ss_avr_ss(DimXMin:DimXMax, DimZMin:DimZMax)
  
  ss_avr_ss = var

end function ss_avr_ss


function ss_avr_fs(var) 
  use gridset
  implicit none
  
  real(8), intent(in)  :: var(DimXMin:DimXMax, DimZMin:DimZMax)
  real(8)              :: ss_avr_fs(DimXMin:DimXMax, DimZMin:DimZMax)
  
  ss_avr_fs = epsilon(0.0d0) 
  ss_avr_fs(DimXMin+1 : DimXMax, DimZMin : DimZMax) =          &
       &  (                                                    &
       &     var(DimXMin+1 : DimXMax,   DimZMin : DimZMax)     &
       &   + var(DimXMin   : DimXMax-1, DimZMin : DimZMax)     &
       &  ) * 5.0d-1
end function ss_avr_fs
  
  
function ss_avr_sf(var)
  use gridset
  implicit none
  
  real(8), intent(in)  :: var(DimXMin:DimXMax, DimZMin:DimZMax)
  real(8)              :: ss_avr_sf(DimXMin:DimXMax, DimZMin:DimZMax)
  
  ss_avr_sf = epsilon(0.0d0) 
  ss_avr_sf(DimXMin : DimXMax, DimZMin+1 : DimZMax) =          &
       &  (                                                    &
       &     var(DimXMin : DimXMax, DimZMin+1 : DimZMax   )    &
       &   + var(DimXMin : DimXMax, DimZMin   : DimZMax-1 )    &
       &  ) * 5.0d-1
end function ss_avr_sf
  
  
function ss_avr_ff(var)
  use gridset
  implicit none
  
  real(8), intent(in)  :: var(DimXMin:DimXMax, DimZMin:DimZMax)
  real(8)              :: ss_avr_ff(DimXMin:DimXMax, DimZMin:DimZMax)
  
  ss_avr_ff = epsilon(0.0d0) 
  ss_avr_ff(DimXMin+1 : DimXMax, DimZMin+1 : DimZMax) =           &
       &  (                                                       & 
       &     var(DimXMin+1 : DimXMax,   DimZMin+1 : DimZMax   )   &
       &   + var(DimXMin+1 : DimXMax,   DimZMin   : DimZMax-1 )   &
       &   + var(DimXMin   : DimXMax-1, DimZMin+1 : DimZMax   )   &
       &   + var(DimXMin   : DimXMax-1, DimZMin   : DimZMax-1 )   &
       &  ) * 2.5d-1
end function ss_avr_ff
  
  



!!!
!!!速度 u の位置での平均操作
!!!  

function fs_avr_ss(var) 
  use gridset
  implicit none
  
  real(8), intent(in)  :: var(DimXMin:DimXMax, DimZMin:DimZMax)
  real(8)              :: fs_avr_ss(DimXMin:DimXMax, DimZMin:DimZMax)
  
  fs_avr_ss = epsilon(0.0d0) 
  fs_avr_ss(DimXMin : DimXMax-1, DimZMin : DimZMax) =         &
       &  (                                                   &
       &     var(DimXMin+1 : DimXMax   , DimZMin : DimZMax)   &
       &   + var(DimXMin   : DimXMax-1 , DimZMin : DimZMax)   &
       &  ) * 5.0d-1
end function fs_avr_ss


function fs_avr_fs(var) 
  use gridset
  implicit none

  real(8), intent(in)  :: var(DimXMin:DimXMax, DimZMin:DimZMax)
  real(8)              :: fs_avr_fs(DimXMin:DimXMax, DimZMin:DimZMax)
  
  fs_avr_fs = var

end function fs_avr_fs


function fs_avr_sf(var) 
  use gridset
  implicit none

  real(8), intent(in)  :: var(DimXMin:DimXMax, DimZMin:DimZMax)
  real(8)              :: fs_avr_sf(DimXMin:DimXMax, DimZMin:DimZMax)
  
  fs_avr_sf = epsilon(0.0d0) 
  fs_avr_sf(DimXMin : DimXMax-1, DimZMin+1 : DimZMax) =         &
       &  (                                                     &
       &     var(DimXMin+1 : DimXMax,   DimZMin+1 : DimZMax  )  &
       &   + var(DimXMin+1 : DimXMax,   DimZMin   : DimZMax-1)  &
       &   + var(DimXMin   : DimXMax-1, DimZMin+1 : DimZMax  )  &
       &   + var(DimXMin   : DimXMax-1, DimZMin   : DimZMax-1)  &
       &  ) * 2.5d-1
end function fs_avr_sf


function fs_avr_ff(var) 
  use gridset
  implicit none
  
  real(8), intent(in)  :: var(DimXMin:DimXMax, DimZMin:DimZMax)
  real(8)              :: fs_avr_ff(DimXMin:DimXMax, DimZMin:DimZMax)
  
  fs_avr_ff = epsilon(0.0d0) 
  fs_avr_ff(DimXMin : DimXMax, DimZMin+1 : DimZMax) =       &
       &  (                                            &
       &     var(DimXMin : DimXMax, DimZMin+1 : DimZMax  )  &
       &   + var(DimXMin : DimXMax, DimZMin   : DimZMax-1)  &
       &  ) * 5.0d-1
end function fs_avr_ff

  
  
!!!
!!!速度 w の位置での平均操作
!!!  
  
function sf_avr_ss(var) 
  use gridset
  implicit none
  
  real(8), intent(in)  :: var(DimXMin:DimXMax, DimZMin:DimZMax)
  real(8)              :: sf_avr_ss(DimXMin:DimXMax, DimZMin:DimZMax)
  
  sf_avr_ss = epsilon(0.0d0) 
  sf_avr_ss(DimXMin : DimXMax, DimZMin : DimZMax-1) =         &
       &  (                                &
       &     var( DimXMin : DimXMax, DimZMin+1 :DimZMax   )    &
       &   + var( DimXMin : DimXMax, DimZMin   :DimZMax-1 )    &
       &  ) * 5.0d-1
end function sf_avr_ss

  
function sf_avr_fs(var) 
  use gridset
  implicit none
  
  real(8), intent(in)  :: var(DimXMin:DimXMax, DimZMin:DimZMax)
  real(8)              :: sf_avr_fs(DimXMin:DimXMax, DimZMin:DimZMax)
  
  sf_avr_fs = epsilon(0.0d0) 
  sf_avr_fs(DimXMin+1 : DimXMax, DimZMin : DimZMax-1 ) =       &
       &  (                                &
       &     var(DimXMin+1 :DimXMax,   DimZMin+1 : DimZMax  )  &
       &   + var(DimXMin+1 :DimXMax,   DimZMin   : DimZMax-1)  &
       &   + var(DimXMin   :DimXMax-1, DimZMin+1 : DimZMax  )  &
       &   + var(DimXMin   :DimXMax-1, DimZMin   : DimZMax-1)  &
       &  ) * 2.5d-1
end function sf_avr_fs


function sf_avr_sf(var) 
  use gridset
  implicit none

  real(8), intent(in)  :: var(DimXMin:DimXMax, DimZMin:DimZMax)
  real(8)              :: sf_avr_sf(DimXMin:DimXMax, DimZMin:DimZMax)
  
  sf_avr_sf = var
end function sf_avr_sf


function sf_avr_ff(var) 
  use gridset
  implicit none
  
  real(8), intent(in)  :: var(DimXMin:DimXMax, DimZMin:DimZMax)
  real(8)              :: sf_avr_ff(DimXMin:DimXMax, DimZMin:DimZMax)
  
  sf_avr_ff = epsilon(0.0d0) 
  sf_avr_ff(DimXMin+1 : DimXMax, DimZMin : DimZMax) =       &
       &  (                                            &
       &     var(DimXMin+1 : DimXMax,   DimZMin : DimZMax)  &
       &   + var(DimXMin   : DimXMax-1, DimZMin : DimZMax)  &
       &  ) * 5.0d-1
end function sf_avr_ff




!!!
!!! ff 格子点位置での平均操作
!!!

function ff_avr_ss(var) 
  use gridset
  implicit none
  
  real(8), intent(in)  :: var(DimXMin:DimXMax, DimZMin:DimZMax)
  real(8)              :: ff_avr_ss(DimXMin:DimXMax, DimZMin:DimZMax)
  
  ff_avr_ss = epsilon(0.0d0) 
  ff_avr_ss(DimXMin : DimXMax-1, DimZMin : DimZMax-1) =       &
       &  (                                &
       &     var(DimXMin+1 : DimXMax,   DimZMin+1 : DimZMax   )  &
       &   + var(DimXMin+1 : DimXMax,   DimZMin   : DimZMax-1 )  &
       &   + var(DimXMin   : DimXMax-1, DimZMin+1 : DimZMax   )  &
       &   + var(DimXMin   : DimXMax-1, DimZMin   : DimZMax-1 )  &
       &  ) * 2.5d-1
end function ff_avr_ss


function ff_avr_sf(var) 
  use gridset
  implicit none
  
  real(8), intent(in)  :: var(DimXMin:DimXMax, DimZMin:DimZMax)
  real(8)              :: ff_avr_sf(DimXMin:DimXMax, DimZMin:DimZMax)
  
  ff_avr_sf = epsilon(0.0d0) 
  ff_avr_sf(DimXMin : DimXMax-1 , DimZMin : DimZMax) =       &
       &  (                                            &
       &     var(DimXMin+1 : DimXMax,   DimZMin : DimZMax)  &
       &   + var(DimXMin   : DimXMax-1, DimZMin : DimZMax)  &
       &  ) * 5.0d-1
end function ff_avr_sf


function ff_avr_fs(var) 
  use gridset
  implicit none
  
  real(8), intent(in)  :: var(DimXMin:DimXMax, DimZMin:DimZMax)
  real(8)              :: ff_avr_fs(DimXMin:DimXMax, DimZMin:DimZMax)
  
  ff_avr_fs = epsilon(0.0d0) 
  ff_avr_fs(DimXMin : DimXMax, DimZMin : DimZMax-1) =       &
       &  (                                            &
       &     var(DimXMin : DimXMax, DimZMin+1 : DimZMax)  &
       &   + var(DimXMin : DimXMax, DimZMin   : DimZMax-1)  &
       &  ) * 5.0d-1
end function ff_avr_fs

