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

module interface_def_cp_cef
  interface

     subroutine def_cp_cef(property)
       use prm_type, only: type_property
       type(type_property), intent(inout)   :: property(:)!物性値
     end subroutine def_cp_cef
     
  end interface
end module interface_def_cp_cef



subroutine def_cp_cef(property)
  use prm_type, only: type_property, type_spl
  use interface_stop
  use interface_list_output
  use interface_list_insert
  use interface_spl

  implicit none
  type(type_property), intent(inout)   :: property(:)!物性値
  
  real(8), allocatable  :: temp(:)
  real(8), allocatable  :: cp(:)
  real(8), allocatable  :: cp_int(:)
  real(8), allocatable  :: cp_dvt_int(:)
  real(8), allocatable  :: C(:)
  real(8), allocatable  :: D(:)
  real(8), allocatable  :: E(:)
  real(8)               :: DY
  integer               :: N, M, s, i
  integer               :: ICON  
  logical               :: sw
  type(type_spl)        :: object
  

  N = size(property, 1)

  spl_cef: do s = 1, N
     !ポインタ準備
     allocate(property(s)%spl)
     allocate(property(s)%spl%next)
     
     !番兵作成
     property(s)%spl%record%temp = huge(property(s)%spl%record%temp)
     property(s)%spl%record%dat = huge(property(s)%spl%record%dat)
     property(s)%spl%record%dat_int = &
          & huge(property(s)%spl%record%dat_int)
     property(s)%spl%record%dat_dvt_int = &
          & huge(property(s)%spl%record%dat_dvt_int)
     property(s)%spl%record%C = huge(property(s)%spl%record%C)
     property(s)%spl%record%D = huge(property(s)%spl%record%D)
     property(s)%spl%record%E = huge(property(s)%spl%record%E)
     
     !初期化
     nullify(property(s)%spl%next)     

     !計算法が 1 (希ガス)以外は終了
     if (property(s)%method /= 2) cycle spl_cef
     
     !値を取得する. 温度, 比熱の値は既に property に含まれている
     call list_output(property(s)%cp, temp, cp)
     
     !スプライン補間係数の作成
     M = size(temp, 1)
     allocate(C(M), D(M), E(M), cp_int(M), cp_dvt_int(M))
     C = 0.0d0; D = 0.0d0; E = 0.0d0; cp_int = 0.0d0; cp_dvt_int = 0.0d0
     
     !スプライン補間
     C = 0.0D0; D = 0.0D0; E = 0.0D0; DY = 0.0D0   
     call DINSPL(temp, cp, DY, M, C, D, E, ICON)
     
     !DINSPL ルーチンのコンディションチェック
     call stop(ICON, 'def_cp_cef.f90')
     
     !スプライン補間
     sw = .true. 
     do i = 1, M - 1
        object = type_spl(temp(i), cp(i), &
             & cp_int(i), cp_dvt_int(i), C(i), D(i), E(i))
        call spl(object, temp(i), temp(i+1), sw, &
             & var_int=cp_int(i), var_dvt_int=cp_dvt_int(i))
     end do
     
     !値をリストに代入
     do i = 1, M
        call list_insert(temp(i), cp(i), cp_int(i), cp_dvt_int(i), &
             & C(i), D(i), E(i), property(s)%spl)
     end do

     
     deallocate(temp, cp, C, D, E, cp_int, cp_dvt_int)
  end do spl_cef
  
end subroutine def_cp_cef
