!======================================================================!
!                                                                      !
!    Software Name : FrontCOMP_cure   Ver. 3.1                         !
!                                                                      !
!      Module Name : m_heat_mat_ass_capacity                           !
!      Category    : Heat Analysis                                     !
!                                                                      !
!      Developed based on "FrontSTR" of RSS21 project                  !
!                                                                      !
!                     Written by Yasuji Fukahori,    2007/07/04        !
!                                Tomotaka Ogasawara, 2013/03/26        !
!                                                                      !
!     Contact address :  IIS,The University of Tokyo, CISS             !
!                                                                      !
!    "Composite Material Strength & Reliability Evaluation Simulator"  !
!                                                                      !
!======================================================================!

module m_heat_mat_ass_capacity
   contains
!C***
!C*** MAT_ASS_CAPACITY
!C***
   subroutine heat_mat_ass_capacity ( hecMESH,hecMAT,fstrHEAT,DTIME )

      use m_fstr
      use m_heat_LIB_CAPACITY

      implicit none
      integer(kind=kint) itype,iS,iE,ic_type,icel,isect,IMAT,in0,nn,i,nodLOCAL,ip,inod
      integer(kind=kint) ic,jp,jnod,isU,ieU,ik,isL,ieL  
      real(kind=kreal)   DTIME,XX,YY,ZZ,TT,T0,SS,ASECT,S0,THICK


      type (fstr_heat         ) :: fstrHEAT
      type (hecmwST_matrix    ) :: hecMAT
      type (hecmwST_local_mesh) :: hecMESH

      dimension nodLOCAL(20),XX(20),YY(20),ZZ(20),TT(20),T0(20),S0(20),SS(400)


!C +-------------------------------+
!C | ELEMENT-by-ELEMENT ASSEMBLING | 
!C | according to ELEMENT TYPE     |
!C +-------------------------------+

      do itype= 1, hecMESH%n_elem_type
        iS= hecMESH%elem_type_index(itype-1) + 1
        iE= hecMESH%elem_type_index(itype  )
        ic_type= hecMESH%elem_type_item(itype)
   
        do icel = iS, iE
          isect = hecMESH%section_ID(icel)
          IMAT = hecMESH%section%sect_mat_ID_item(isect)
          in0 = hecMESH%elem_node_index(icel-1)

          nn = hecmw_get_max_node(ic_type)
          do i = 1, nn
            nodLOCAL(i) = hecMESH%elem_node_item(in0+i)
            XX(i) = hecMESH%node ( 3*nodLOCAL(i)-2 )
            YY(i) = hecMESH%node ( 3*nodLOCAL(i)-1 )
            ZZ(i) = hecMESH%node ( 3*nodLOCAL(i)   )
            TT(i) = fstrHEAT%TEMP (   nodLOCAL(i)   )
            T0(i) = fstrHEAT%TEMP0(   nodLOCAL(i)   )
          enddo
!         do i = 1, nn 
!           S0(i) = 0.0
!         enddo
          do i = 1, nn*nn
            SS(i) = 0.0
          enddo

          if( ic_type.eq.111 ) then
            is = hecMesh%section%sect_R_index(isect)
            ASECT = hecMESH%section%sect_R_item(is)
            call heat_CAPACITY_111 ( nn,XX,YY,ZZ,TT,IMAT,ASECT,S0                        &
                          ,fstrHEAT%CPtab(IMAT)     ,fstrHEAT%CPtemp(IMAT,:)          &
                          ,fstrHEAT%CPfuncA(IMAT,:) ,fstrHEAT%CPfuncB(IMAT,:)      &
                          ,fstrHEAT%RHOtab(IMAT)    ,fstrHEAT%RHOtemp(IMAT,:)         &
                          ,fstrHEAT%RHOfuncA(IMAT,:),fstrHEAT%RHOfuncB(IMAT,:) )
          elseif( ic_type.eq.231 ) then
            is = hecMesh%section%sect_R_index(isect)
            THICK = hecMESH%section%sect_R_item(is)
            call heat_CAPACITY_231 ( nn,XX,YY,ZZ,TT,IMAT,THICK,S0                        &
                          ,fstrHEAT%CPtab(IMAT)     ,fstrHEAT%CPtemp(IMAT,:)          &
                          ,fstrHEAT%CPfuncA(IMAT,:) ,fstrHEAT%CPfuncB(IMAT,:)      &
                          ,fstrHEAT%RHOtab(IMAT)    ,fstrHEAT%RHOtemp(IMAT,:)         &
                          ,fstrHEAT%RHOfuncA(IMAT,:),fstrHEAT%RHOfuncB(IMAT,:) )
          elseif( ic_type.eq.232 ) then
            is = hecMesh%section%sect_R_index(isect)
            THICK = hecMESH%section%sect_R_item(is)
            call heat_CAPACITY_232 ( nn,XX,YY,ZZ,TT,IMAT,THICK,S0                        &       
                          ,fstrHEAT%CPtab(IMAT)     ,fstrHEAT%CPtemp(IMAT,:)          &
                          ,fstrHEAT%CPfuncA(IMAT,:) ,fstrHEAT%CPfuncB(IMAT,:)      &
                          ,fstrHEAT%RHOtab(IMAT)    ,fstrHEAT%RHOtemp(IMAT,:)         &
                          ,fstrHEAT%RHOfuncA(IMAT,:),fstrHEAT%RHOfuncB(IMAT,:) )
          elseif( ic_type.eq.241 ) then
            is = hecMesh%section%sect_R_index(isect)
            THICK = hecMESH%section%sect_R_item(is)
            call heat_CAPACITY_241 ( nn,XX,YY,ZZ,TT,IMAT,THICK,S0                        &
                          ,fstrHEAT%CPtab(IMAT)     ,fstrHEAT%CPtemp(IMAT,:)          &
                          ,fstrHEAT%CPfuncA(IMAT,:) ,fstrHEAT%CPfuncB(IMAT,:)      &
                          ,fstrHEAT%RHOtab(IMAT)    ,fstrHEAT%RHOtemp(IMAT,:)         &
                          ,fstrHEAT%RHOfuncA(IMAT,:),fstrHEAT%RHOfuncB(IMAT,:) )
          elseif( ic_type.eq.242 ) then
            is = hecMesh%section%sect_R_index(isect)
            THICK = hecMESH%section%sect_R_item(is)
            call heat_CAPACITY_242 ( nn,XX,YY,ZZ,TT,IMAT,THICK,S0                        & 
                          ,fstrHEAT%CPtab(IMAT)     ,fstrHEAT%CPtemp(IMAT,:)          &
                          ,fstrHEAT%CPfuncA(IMAT,:) ,fstrHEAT%CPfuncB(IMAT,:)      &
                          ,fstrHEAT%RHOtab(IMAT)    ,fstrHEAT%RHOtemp(IMAT,:)         &
                          ,fstrHEAT%RHOfuncA(IMAT,:),fstrHEAT%RHOfuncB(IMAT,:) )
          elseif( ic_type.eq.341 ) then
            call heat_CAPACITY_341 ( nn,XX,YY,ZZ,TT,IMAT,S0                              & 
                          ,fstrHEAT%CPtab(IMAT)     ,fstrHEAT%CPtemp(IMAT,:)          &
                          ,fstrHEAT%CPfuncA(IMAT,:) ,fstrHEAT%CPfuncB(IMAT,:)      &
                          ,fstrHEAT%RHOtab(IMAT)    ,fstrHEAT%RHOtemp(IMAT,:)         &
                          ,fstrHEAT%RHOfuncA(IMAT,:),fstrHEAT%RHOfuncB(IMAT,:) )
          elseif( ic_type.eq.342 ) then
            call heat_CAPACITY_342 ( nn,XX,YY,ZZ,TT,IMAT,S0,SS                           & 
                          ,fstrHEAT%CPtab(IMAT)     ,fstrHEAT%CPtemp(IMAT,:)          &
                          ,fstrHEAT%CPfuncA(IMAT,:) ,fstrHEAT%CPfuncB(IMAT,:)      &
                          ,fstrHEAT%RHOtab(IMAT)    ,fstrHEAT%RHOtemp(IMAT,:)         &
                          ,fstrHEAT%RHOfuncA(IMAT,:),fstrHEAT%RHOfuncB(IMAT,:) )
          elseif( ic_type.eq.351 ) then
            call heat_CAPACITY_351 ( nn,XX,YY,ZZ,TT,IMAT,S0                              & 
                          ,fstrHEAT%CPtab(IMAT)     ,fstrHEAT%CPtemp(IMAT,:)          &
                          ,fstrHEAT%CPfuncA(IMAT,:) ,fstrHEAT%CPfuncB(IMAT,:)      &
                          ,fstrHEAT%RHOtab(IMAT)    ,fstrHEAT%RHOtemp(IMAT,:)         &
                          ,fstrHEAT%RHOfuncA(IMAT,:),fstrHEAT%RHOfuncB(IMAT,:) )
          elseif( ic_type.eq.352 ) then
            call heat_CAPACITY_352 ( nn,XX,YY,ZZ,TT,IMAT,S0                              & 
                          ,fstrHEAT%CPtab(IMAT)     ,fstrHEAT%CPtemp(IMAT,:)          &
                          ,fstrHEAT%CPfuncA(IMAT,:) ,fstrHEAT%CPfuncB(IMAT,:)      &
                          ,fstrHEAT%RHOtab(IMAT)    ,fstrHEAT%RHOtemp(IMAT,:)         &
                          ,fstrHEAT%RHOfuncA(IMAT,:),fstrHEAT%RHOfuncB(IMAT,:) )
          elseif( ic_type.eq.361 ) then
            call heat_CAPACITY_361 ( nn,XX,YY,ZZ,TT,IMAT,S0                              & 
                          ,fstrHEAT%CPtab(IMAT)     ,fstrHEAT%CPtemp(IMAT,:)          &
                          ,fstrHEAT%CPfuncA(IMAT,:) ,fstrHEAT%CPfuncB(IMAT,:)      &
                          ,fstrHEAT%RHOtab(IMAT)    ,fstrHEAT%RHOtemp(IMAT,:)         &
                          ,fstrHEAT%RHOfuncA(IMAT,:),fstrHEAT%RHOfuncB(IMAT,:) )
          elseif( ic_type.eq.362 ) then
            call heat_CAPACITY_362 ( nn,XX,YY,ZZ,TT,IMAT,S0                              & 
                          ,fstrHEAT%CPtab(IMAT)     ,fstrHEAT%CPtemp(IMAT,:)          &
                          ,fstrHEAT%CPfuncA(IMAT,:) ,fstrHEAT%CPfuncB(IMAT,:)      &
                          ,fstrHEAT%RHOtab(IMAT)    ,fstrHEAT%RHOtemp(IMAT,:)         &
                          ,fstrHEAT%RHOfuncA(IMAT,:),fstrHEAT%RHOfuncB(IMAT,:) )
          elseif( ic_type.eq.731 ) then
            is = hecMesh%section%sect_R_index(isect)
            THICK = hecMESH%section%sect_R_item(is)
            call heat_CAPACITY_731 ( nn,XX,YY,ZZ,TT,IMAT,THICK,S0                        & 
                          ,fstrHEAT%CPtab(IMAT)     ,fstrHEAT%CPtemp(IMAT,:)          &
                          ,fstrHEAT%CPfuncA(IMAT,:) ,fstrHEAT%CPfuncB(IMAT,:)      &
                          ,fstrHEAT%RHOtab(IMAT)    ,fstrHEAT%RHOtemp(IMAT,:)         &
                          ,fstrHEAT%RHOfuncA(IMAT,:),fstrHEAT%RHOfuncB(IMAT,:) )
          elseif( ic_type.eq.741 ) then
            is = hecMESH%section%sect_R_index(isect)
            THICK = hecMESH%section%sect_R_item(is)
            call heat_CAPACITY_741 ( nn,XX,YY,ZZ,TT,IMAT,THICK,S0                        & 
                          ,fstrHEAT%CPtab(IMAT)     ,fstrHEAT%CPtemp(IMAT,:)          &
                          ,fstrHEAT%CPfuncA(IMAT,:) ,fstrHEAT%CPfuncB(IMAT,:)      &
                          ,fstrHEAT%RHOtab(IMAT)    ,fstrHEAT%RHOtemp(IMAT,:)         &
                          ,fstrHEAT%RHOfuncA(IMAT,:),fstrHEAT%RHOfuncB(IMAT,:) )
          endif
!C
          ic = 0
          do ip = 1, nn
            inod = nodLOCAL(ip)
            do jp = 1, nn
              ic = ic + 1
              jnod = nodLOCAL(jp)

              if( jnod.gt.inod ) then
                isU = hecMAT%indexU(inod-1) + 1
                ieU = hecMAT%indexU(inod)
                do ik = isU, ieU
                  if( hecMAT%itemU(ik).eq.jnod ) hecMAT%AU(ik) = hecMAT%AU(ik) + SS(ic)/DTIME
                enddo
              elseif( jnod.lt.inod ) then
                isL = hecMAT%indexL(inod-1) + 1
                ieL = hecMAT%indexL(inod)
                do ik = isL, ieL
                  if( hecMAT%itemL(ik).eq.jnod ) hecMAT%AL(ik) = hecMAT%AL(ik) + SS(ic)/DTIME
                enddo
              else
                hecMAT%D(inod) = hecMAT%D(inod) + SS(ic)/DTIME
              endif

              hecMAT%B(inod) = hecMAT%B(inod) + SS(ic)*T0(jp)/DTIME
            enddo
          enddo
!C
        enddo
      enddo

   end subroutine heat_mat_ass_capacity

!CC-------------------------------------------------------------------------------
!C*** MAT_ASS_CAPACITY  FOR CURE
!CC-------------------------------------------------------------------------------
   subroutine cure_mat_ass_capacity ( hecMESH,hecMAT,fstrHEAT,DTIME )

      use m_fstr
      use m_heat_LIB_CAPACITY

      implicit none
      integer(kind=kint) itype,iS,iE,ic_type,icel,isect,IMAT,in0,nn,i,nodLOCAL,ip,inod
      integer(kind=kint) ic,jp,jnod,isU,ieU,ik,isL,ieL  
      real(kind=kreal)   DTIME,XX,YY,ZZ,TT,T0,CUR_nod_I,ASECT,S0,SS,THICK


      type (fstr_heat         ) :: fstrHEAT
      type (hecmwST_matrix    ) :: hecMAT
      type (hecmwST_local_mesh) :: hecMESH

      dimension nodLOCAL(20),XX(20),YY(20),ZZ(20),TT(20),T0(20),S0(20),CUR_nod_I(20),SS(400)


!C +-------------------------------+
!C | ELEMENT-by-ELEMENT ASSEMBLING | 
!C | according to ELEMENT TYPE     |
!C +-------------------------------+

      do itype= 1, hecMESH%n_elem_type
        iS= hecMESH%elem_type_index(itype-1) + 1
        iE= hecMESH%elem_type_index(itype  )
        ic_type= hecMESH%elem_type_item(itype)
   
        do icel = iS, iE
          isect = hecMESH%section_ID(icel)
          IMAT = hecMESH%section%sect_mat_ID_item(isect)
          in0 = hecMESH%elem_node_index(icel-1)

          nn = hecmw_get_max_node(ic_type)
          do i = 1, nn
            nodLOCAL(i) = hecMESH%elem_node_item(in0+i)
            XX(i) = hecMESH%node ( 3*nodLOCAL(i)-2 )
            YY(i) = hecMESH%node ( 3*nodLOCAL(i)-1 )
            ZZ(i) = hecMESH%node ( 3*nodLOCAL(i)   )
            TT(i) = fstrHEAT%TEMP (   nodLOCAL(i)   )
            T0(i) = fstrHEAT%TEMP0(   nodLOCAL(i)   )
            CUR_nod_I(i) = fstrHEAT%CURE3D_nod_I( nodLOCAL(i)   )
          enddo
!         do i = 1, nn 
!           S0(i) = 0.0
!         enddo
          do i = 1, nn*nn
            SS(i) = 0.0
          enddo

        if( IMAT .ne. 2) then

          do i = 1, nn*nn
!!!!---  20111213  ----------------
            SS(i) = 1.0D-30
!           SS(i) = 1.0D-10
          enddo

        else if( IMAT .eq. 2) then
          if( ic_type.eq.111 ) then
          elseif( ic_type.eq.231 ) then
          elseif( ic_type.eq.232 ) then
          elseif( ic_type.eq.241 ) then
          elseif( ic_type.eq.242 ) then
          elseif( ic_type.eq.341 ) then
          elseif( ic_type.eq.342 ) then
            call cure_CAPACITY_342 ( nn,XX,YY,ZZ,T0,IMAT,S0,SS                        & 
                          ,fstrHEAT%CPtab(IMAT)     ,fstrHEAT%CPtemp(IMAT,:)          &
                          ,fstrHEAT%CPfuncA(IMAT,:) ,fstrHEAT%CPfuncB(IMAT,:)         &
                          ,fstrHEAT%RHOtab(IMAT)    ,fstrHEAT%RHOtemp(IMAT,:)         &
                          ,fstrHEAT%RHOfuncA(IMAT,:),fstrHEAT%RHOfuncB(IMAT,:) )
          elseif( ic_type.eq.351 ) then
          elseif( ic_type.eq.352 ) then
          elseif( ic_type.eq.361 ) then
          elseif( ic_type.eq.362 ) then
          elseif( ic_type.eq.731 ) then
          elseif( ic_type.eq.741 ) then
          endif
        end if
!C
          ic = 0
          do ip = 1, nn
            inod = nodLOCAL(ip)
            do jp = 1, nn
              ic = ic + 1
              jnod = nodLOCAL(jp)

              if( jnod.gt.inod ) then
                isU = hecMAT%indexU(inod-1) + 1
                ieU = hecMAT%indexU(inod)
                do ik = isU, ieU
                  if( hecMAT%itemU(ik).eq.jnod ) hecMAT%AU(ik) = hecMAT%AU(ik) + SS(ic)/DTIME
                enddo
              elseif( jnod.lt.inod ) then
                isL = hecMAT%indexL(inod-1) + 1
                ieL = hecMAT%indexL(inod)
                do ik = isL, ieL
                  if( hecMAT%itemL(ik).eq.jnod ) hecMAT%AL(ik) = hecMAT%AL(ik) + SS(ic)/DTIME
                enddo
              else
                hecMAT%D(inod) = hecMAT%D(inod) + SS(ic)/DTIME
              endif

              hecMAT%B(inod) = hecMAT%B(inod) + SS(ic)*CUR_nod_I(jp)/DTIME
            enddo
          enddo
!C
        enddo
      enddo

   end subroutine cure_mat_ass_capacity 

end module m_heat_mat_ass_capacity
