!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-03-01    作成
!c
!c Copyright (C) SUGIYAMA Ko-ichiro, 2003, All rights reserved

module interface_ChkElmExist
  interface
     
     subroutine ChkElmExist(emol, struct, ssw, esw, esw_store, sw)
       real(8), intent(in)               :: emol(:)
       real(8), intent(in)               :: struct(:,:)
       logical, intent(out), allocatable :: ssw(:)
       logical, intent(out), allocatable :: esw(:)
       logical, intent(out)              :: sw
       logical, intent(in)               :: esw_store(:)
     end subroutine ChkElmExist
     
  end interface
end module interface_ChkElmExist




subroutine ChkElmExist(emol, struct, ssw, esw, esw_store, sw)
  implicit none
  real(8), intent(in)               :: emol(:)  
  real(8), intent(in)               :: struct(:,:)
  logical, intent(out), allocatable :: ssw(:)
  logical, intent(out), allocatable :: esw(:)
  logical, intent(out)              :: sw
  logical, intent(in)               :: esw_store(:)
  
  integer                           :: e, s
  integer                           :: N, M


!!!
!!!初期値作成
!!!
  N = size(struct, 1)
  M = size(struct, 2)

  if (allocated(ssw)) deallocate(ssw)
  if (allocated(esw)) deallocate(esw)
  allocate(ssw(N), esw(M))
  ssw = .true.; esw = .true.; sw = .false. 
 
!!!
!!!チェックしなくて済む場合
!!!
  if (count(esw_store) == M)  return     !全部真ならばサブルーチン終了
  

!!!
!!!前のステップで元素が系から除去された場合
!!!
  if (size(esw_store) == M) then 
     esw = esw_store

     do e = 1, M
        if (.NOT. esw(e)) then 
           sw = .true.
           do s = 1, N
              if (struct(s, e) > 0.0d0)  ssw(s) = .false.
           end do
        end if
     end do
     
  end if
  

end subroutine ChkElmExist
