!c Description:
!c  相に番号を与える. 気体は 1, 溶液は 2, 固溶体は 3, それ以外は 4
!c
!c Current Code Owner: 
!c   sugiyama@gfd-dennou.org
!c
!c Histry: 
!c   Version    Date          Comment
!c   -------    ----------    --------
!c   1.0        2004-08-01    作成
!c
!c Copyright (C) SUGIYAMA Ko-ichiro, 2004, All rights reserved

module interface_def_phase
  interface

     subroutine def_phase(property)
       use prm_type, only: type_property
       type(type_property), intent(inout) :: property(:)
     end subroutine def_phase
     
  end interface
end module interface_def_phase



subroutine def_phase(property)
  use prm_type, only: type_property
  use interface_name2num

  implicit none
  type(type_property), intent(inout) :: property(:)

  integer                            :: s, N, M
  integer                            :: sol
  integer                            :: liq
  integer                            :: num
  logical                            :: sw1, sw2
  character(20)                      :: name

  N = size(property, 1)
  property%phase = 0           !初期化
  sw1 = .false.; sw2 = .false.
  sol = 0; liq = 0
  
  !溶液を作るためには複数の物質が必要
  do s = 1, N
     M = len_trim(property(s)%name)
     if (trim(property(s)%name(M:M)) == 'l' &
          & .AND. property(s)%solsw) liq = liq + 1
     if (trim(property(s)%name(M:M)) == 's' & 
          & .AND. property(s)%solsw) sol = sol + 1
  end do
  
  !複数の化学種があればスイッチを入れる
  sw1 = liq >= 2
  sw2 = sol >= 2

  !実際に番号付けを行う
  do s = 1, N
     M = len_trim(property(s)%name)

     !気相なら 1     
     if(property(s)%name(M:M) == 'g') then 
        property(s)%phase = 1 
        
     !溶液
     elseif(property(s)%solsw) then 
        if (property(s)%name(M:M) == 'l' .AND. sw1) then 
           property(s)%phase = 2
        elseif(property(s)%name(M:M) == 's' .AND. sw2) then 
           property(s)%phase = 3
        else
           property(s)%phase = 4
        end if
        
     else
        property(s)%phase = 4
     end if
     
     if (property(s)%phase == 0) then 
        write(*,*) "phase is not defined"
        stop
     end if
  end do
  
  
end subroutine def_phase
