!c Description: 
!c   境界条件
!c
!c Current Code Owner: 
!c   sugiyama@gfd-dennou.org
!c
!c Histry: 
!c   Version    Date          Comment
!c   -------    ----------    --------
!c   1.0        2003-11-19    杉山耕一朗 
!c   1.0        2003-11-21    杉山耕一朗 
!c
!c Copyright (C) SUGIYAMA Ko-ichiro, 2003, All rights reserved

module if_boundary
  interface boundary

     subroutine boundary1d(type, var)
       use gridset
       integer, intent(in)        :: type
       real(8), intent(inout)     :: var(kmin:kmax)
     end subroutine boundary1d

     subroutine boundary2d(type, var)
       use gridset
       integer, intent(in)        :: type
       real(8), intent(inout)     :: var(imin:imax, kmin:kmax)
     end subroutine boundary2d
     
  end interface
end module if_boundary



subroutine boundary1d(type, var)  
  !--- モジュールの読み込み
  use gridset
  
  !--- 暗黙の型宣言禁止
  implicit none
  
  !--- 入出力変数
  integer, intent(in)        :: type
  real(8), intent(inout)     :: var(kmin:kmax)
  
  !--- 内部変数
  integer   :: i
  
  !--- 境界条件の条件分岐
  if (type == 1 .or. type == 4 .or. type == 5) then 
     !--- z 方向の周期境界条件
     do i = 1, bm
        var(kmb - i) = var(kme + 1 - i)
        var(kme + i) = var(kmb - 1 + i)
     end do
     
  elseif (type == 2 .or. type == 6) then
     !--- z 方向の固定壁 (対称)
     !--- 変数はスカラー
     do i = 1, bm
        var(kmb - i) = var(kmb + 1 + i)
        var(kme + i) = var(kme + 1 - i)
     end do
     
   elseif (type == 3 .or. type == 7) then
     !--- z 方向の固定壁 (反対称)
     !--- 変数はベクトル
     var(kmb) = 0.0d0
     var(kme + 1) = 0.0d0
     do i = 1, bm
        var(kmb - i) = - var(kmb + i)
     end do
     do i = 1, bm - 1
        var(kme + 1 + i) = - var(kme + 1  - i)
     end do
  end if

end subroutine boundary1d




subroutine boundary2d(type, var)
  !--- モジュールの読み込み
  use gridset
  
  !--- 暗黙の型宣言禁止
  implicit none
  
  !--- 入出力変数
  integer, intent(in)        :: type
  real(8), intent(inout)     :: var(imin:imax, kmin:kmax)
  
  !--- 内部変数
  integer   :: i

  select case (type)
     
  case (1)
     !--- x 方向は周期境界条件
     !--- z 方向は周期境界条件
     !--- 変数はスカラー量でもベクトル量でも OK
     do i = 1, bm
        var(imb - i, kmb:kme) = var(ime + 1 - i, kmb:kme)
        var(ime + i, kmb:kme) = var(imb - 1 + i, kmb:kme)
     end do
     
     do i = 1, bm
        var(:, kmb - i) = var(:, kme + 1 - i)
        var(:, kme + i) = var(:, kmb - 1 + i)
     end do


  case (2)
     !--- x 方向は周期境界条件
     !--- z 方向は固定端
     !--- 変数はスカラー量
     do i = 1, bm
        var(imb - i, kmb:kme) = var(ime + 1 - i, kmb:kme)
        var(ime + i, kmb:kme) = var(imb - 1 + i, kmb:kme)
     end do
     
     do i = 1, bm
        var(:, kmb - i) = var(:, kmb + 1 + i)
        var(:, kme + i) = var(:, kme + 1 - i)
     end do
     
  case (3)
     !--- x 方向は周期境界条件
     !--- z 方向は固定端
     !--- 変数はベクトル
     do i = 1, bm
        var(imb - i, kmb:kme) = var(ime + 1 - i, kmb:kme)
        var(ime + i, kmb:kme) = var(imb - 1 + i, kmb:kme)
     end do
     
     var(:, kmb) = 0.0d0
     var(:, kme + 1) = 0.0d0
     do i = 1, bm
        var(:, kmb - i) = - var(:, kmb + i)
     end do
     do i = 1, bm - 1
        var(:, kme + 1 + i) = - var(:, kme + 1 - i)
     end do
     
  case (4)
     !--- x 方向は固定端
     !--- z 方向は周期境界条件
     !--- 変数はスカラー量
     do i = 1, bm
        var(imb - i, kmb:kme) = var(imb + 1 + i, kmb:kme)
        var(ime + i, kmb:kme) = var(ime + 1 - i, kmb:kme)
     end do
     
     do i = 1, bm
        var(:, kmb - i) = var(:, kme + 1 - i)
        var(:, kme + i) = var(:, kmb - 1 + i)
     end do
     
  case (5)
     !--- x 方向は固定端
     !--- z 方向は周期境界条件
     !--- 変数はベクトル
     var(imb,:) = 0.0d0
     var(ime + 1,:) = 0.0d0
     do i = 1, bm
        var(imb - i, kmb:kme) = - var(imb + i, kmb:kme)
     end do
     do i = 1, bm - 1
        var(ime + 1 + i, kmb:kme) = - var(ime + 1 - i, kmb:kme)
     end do

     do i = 1, bm
        var(:, kmb - i) = var(:, kme + 1 - i)
        var(:, kme + i) = var(:, kmb - 1 + i)
     end do

  case (6)
     !--- x 方向は固定端
     !--- z 方向は固定端
     !--- 変数はスカラー量
     do i = 1, bm
        var(imb - i, kmb:kme) = var(imb + 1 + i, kmb:kme)
        var(ime + i, kmb:kme) = var(ime + 1 - i, kmb:kme)
     end do
     
     do i = 1, bm
        var(:, kmb - i) = var(:, kmb + 1 + i)
        var(:, kme + i) = var(:, kme + 1 - i)
     end do
     
  case (7)
     !--- x 方向は固定端
     !--- z 方向は固定端
     !--- 変数はベクトル
     var(imb, :) = 0.0d0
     var(ime + 1, :) = 0.0d0
     do i = 1, bm
        var(imb - i, kmb:kme) = - var(imb + i, kmb:kme)
     end do
     do i = 1, bm - 1
        var(ime + 1 + i, kmb:kme) = - var(ime + 1 - i, kmb:kme)
     end do

     var(:, kmb) = 0.0d0
     var(:, kme + 1) = 0.0d0
     do i = 1, bm
        var(:, kmb - i) = - var(:, kmb + i)
     end do
     do i = 1, bm - 1
        var(:, kme + 1 + i) = - var(:, kme + 1 - i)
     end do

  end select

end subroutine boundary2d
