!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_ChkEquiv
  interface
     
     subroutine ChkEquiv(mu0, phase, name, ssw, sw)
       real(8), intent(in)               :: mu0(:)
       integer, intent(in)               :: phase(:)
       logical, intent(out), allocatable :: ssw(:) 
       logical, intent(out)              :: sw
       character(*), intent(in)          :: name(:)
     end subroutine ChkEquiv
     
  end interface
end module interface_ChkEquiv




subroutine ChkEquiv(mu0, phase, name, ssw, sw)
  use interface_name_cnd2gas
  use interface_name2num
  
  implicit none
  real(8), intent(in)               :: mu0(:)
  integer, intent(in)               :: phase(:)
  logical, intent(out), allocatable :: ssw(:) 
  logical, intent(out)              :: sw
  character(*), intent(in)          :: name(:)
  
  integer, allocatable              :: num(:)    
  integer                           :: N
  integer                           :: s, t
  character(80)                     :: gasname
  
  
!!!
!!!初期化
!!!
  N = size(mu0, 1)
  if (allocated(ssw)) deallocate(ssw)
  allocate(ssw(N), num(N))
  ssw = .true.
  sw = .false.
  num = 0 

  
!!!
!!!液相, 固相に対応する気相の番号を準備
!!!  
  do s = 1, N
     if (phase(s) == 1) cycle
     call name_cnd2gas(name(s), gasname)
     call name2num(gasname, name, num(s))
  end do
  
  
!!!
!!!液相と固相が存在する場合の判定
!!!
  cut: do s = 1, N - 1
     if (phase(s) /= 4) cycle cut
     
     do t = s + 1, N
        if(num(s) == num(t)) then 
           if (mu0(s) > mu0(t)) then  
              ssw(s) = .false. 
              sw = .true.
           else
              ssw(t) = .false. 
              sw = .true. 
           end if
        end if
     end do
  end do cut
    
!!!
!!!後かたづけ
!!!
  deallocate(num)

end subroutine ChkEquiv
