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

module interface_nonzeromol  
  interface 

     subroutine nonzeromol(mol, mol_pre)
       real(8), intent(inout)     :: mol(:)
       real(8), intent(in)        :: mol_pre(:)
     end subroutine nonzeromol
     
  end interface
end module interface_nonzeromol



subroutine NonZeroMol(mol, mol_pre)
  use prm_common, only: SVect

  implicit none
  real(8), intent(inout)     :: mol(:)
  real(8), intent(in)        :: mol_pre(:)
  real(8), allocatable       :: lambda(:)       !作業用係数
  real(8)                    :: lambda_min
  integer                    :: smax
  

  !配列の大きさを得る
  smax = size(mol, 1)
  
  !初期値を代入
  allocate(lambda(smax))
  lambda = 1.0d0

  !大きさを決める
  lambda = merge(mol_pre / (mol_pre - mol), lambda, mol <= 0.0D0)
  lambda_min = minval(lambda)
  mol = mol_pre - lambda_min  * SVect * (mol_pre - mol)

  !配列の開放
  deallocate(lambda)
  
end subroutine NonZeroMol


