! $BI=Bj(B: $BI=LL$N6-3&>r7o$K4X$9$k%b%8%e!<%k(B
! $BMzNr(B: 2003/11/15


module Tbound
  use prm, only : IMAX, JMAX, KMAX, pi, dt
  use prmphys, only : s_a, Rel, grad_Ms, Mcycle
  use com_var, only : x 

  real(8), save   :: Ms(IMAX, JMAX)
  real(8), save   :: Ts(IMAX, JMAX)
contains

subroutine get_Ms_con
  implicit none
  integer   :: i 
!  Ms = 0.3D0 /s_a
  Ms(:, 1) = 0.15D0 /s_a
  Ms(:, 2) = 0.3D0 /s_a

! $B6-3&>r7o(B
  Ms(1, :) = 0.0D0
  Ms(IMAX, :) = 0.0D0

  ! get_Ms_cont $B$r8F$S=P$9EY$K0J2<$N%k!<%W$r@x$k$N$O%@%5%$(B
  ! $BJQ99$9$Y$7(B

  do i = 1, IMAX
     ! EISMINT $B$N<B83$G$O(B 0.5D0 $B$H$J$C$F$$$k$,<B:]$O(B 0.25D0
     Ms(i, 3) = MIN(0.5D0, grad_Ms*(Rel - abs(x(IMAX/2+1) - x(i))))/s_a
  end do
  !  Ms(:, 3) = 0.6D0 /s_a
!  write(6, *)'Ms = ', Ms(:, 3)

!  Ms = 0.0D0

end subroutine get_Ms_con


subroutine get_Ts_con
  implicit none

  Ts = 200.0D0
  
end subroutine get_Ts_con

subroutine get_Ts_test1(Hsfc)
  implicit none
  
  real(8)  :: dist
  real(8), intent(IN)  :: Hsfc(1:IMAX, 1:JMAX)
  integer   :: i 

  Ts = 200.0D0


  do i = 1, IMAX
     dist = abs(x(i)-x(IMAX/2+1))/1.0D3
     Ts(i, 1:2) = 239.0D0 + 8 * 1.0D-8 * dist*dist*dist
  end do

  Ts(:, 3) = 270.0D0 - 0.01D0 * Hsfc(:, 3)


end subroutine get_Ts_test1

subroutine get_Ts_test2(it, Hsfc)
  implicit none
  
  real(8)  :: dist
  real(8), intent(IN)  :: Hsfc(1:IMAX, 1:JMAX)
  real(8)    :: dltTS
  integer   :: i 
  integer, intent(IN)  :: it


  do i = 1, IMAX
     dist = abs(x(i)-x(IMAX/2+1))/1.0D3
     Ts(i, 1:2) = 239.0D0 + 8 * 1.0D-8 * dist*dist*dist
  end do

  Ts(:, 3) = 270.0D0 - 0.01D0 * Hsfc(:, 3)

  dltTs = 10.0D0 * sin(2.0D0*pi*it*(dt/s_a)/Mcycle)
  Ts = Ts + dltTS

 
end subroutine get_Ts_test2

subroutine get_Ms_test2(it)
  implicit none
  integer :: i 
  real(8)   :: Rel2
  real(8)   :: dltMs
  integer, intent(IN) :: it
!  Ms = 0.3D0 /s_a

  Ms(:, 1) = 0.15D0 /s_a
  Ms(:, 2) = 0.3D0 /s_a

! $B6-3&>r7o(B
  Ms(1, :) = 0.0D0
  Ms(IMAX, :) = 0.0D0

  Rel2 = Rel + 100.0D3 *sin(2.0D0*pi*it*(dt/s_a)/Mcycle)
  
  do i = 1, IMAX
     Ms(i, 3) = MIN(0.5D0, grad_Ms*(Rel2 - abs(x(IMAX/2+1) - x(i))))/s_a
  end do

  dltMs = 0.2D0 * sin(2.0D0*pi*it/Mcycle)/s_a

  Ms(:, 1:2) = Ms(:, 1:2) + dltMs

end subroutine get_Ms_test2




end module Tbound
