*DECK THMRES
      SUBROUTINE THMRES(NFD,NDTOT,MAXIT1,MAXIT2,ERMAXT,DTINV,RC,RIG,
     1 RGG,HCONV,HGAP,RO00,TCOOL,FRO,QFUEL,BURN,POROS,FRACPU,ICONDF,
     2 NCONDF,KCONDF,UCONDF,ICONDC,NCONDC,KCONDC,UCONDC,IFRCDI,ALPHA,
     3 TEMP,TC0)
*
*-----------------------------------------------------------------------
*
*Purpose:
* solution of the discretized thermal conduction equations in a single
* fuel rod.
*
*Copyright:
* Copyright (C) 2012 Ecole Polytechnique de Montreal.
*
*Author(s): A. Hebert, P. Gallet
*
*Parameters: input
* NFD     number of fuel discretized points in the cladded fuel rod. The
*         last point of the discretization (i=NFD) is taken at the surface
*         of the fuel pellet.
* NDTOT   total number of discretized points in the cladded fuel rod with
*         the radial zones in the cladding. The points which are located at
*         i=NFD+1 and i=NDTOT are respectively taken at the inner and
*         the outer surface of the cladding.
* MAXIT1  maximum number of conduction iterations.
* MAXIT2  maximum number of center-pellet iterations.
* ERMAXT  convergence criterion.
* DTINV   inverse time step. Equal to 1/DT in transient cases. Equal to
*         0 in steady-state cases.
* RC      fuel pellet radius in meter.
* RIG     clad internal radius in meter.
* RGG     clad external radius in meter.
* HCONV   clad-coolant thermal exchange coefficient.
* HGAP    gap thermal exchange coefficient.
* RO00    center-mesh coordinate of the first mesh. Set to 0.0 for a
*         no-hole pellet.
* TCOOL   coolant temperature in K.
* FRO     flux depression factor.
* QFUEL   volumic power in fuel.
* BURN    fuel burnup in MWday/tonne.
* POROS   fuel porosity.
* FRACPU  plutonium percent fraction.
* ICONDF  fuel conductivity flag (0=Stora-Chenebault or COMETHE/
*         1=user-provided polynomial + inverse term).
* NCONDF  degree of user-provided fuel conductivity polynomial.
* KCONDF  polynomial coefficients for fuel conductivity in W/m/K^(k+1)
*         (except for the two last coefficients which belongs to the
*         inverse term).
* UCONDF  required unit of temperature in polynomial for fuel
*         conductivity (KELVIN or CELSIUS).
* ICONDC  clad conductivity flag (0=default/1=user-provided
*         polynomial).
* NCONDC  degree of user-provided clad conductivity polynomial.
* KCONDC  polynomial coefficients for clad conductivity in W/m/K^(k+1).
* UCONDC  required unit of temperature in polynomial for clad
*         conductivity (KELVIN or CELSIUS).
* IFRCDI  flag indicating if average approximation is forced during
*         fuel conductivity evaluation (0=default/1=average
*         approximation forced).
* ALPHA   implicit parameter
*
*Parameters: input/output
* TEMP    initial and final meshwise temperatures in K.
* TC0     initial and final center-pellet temperature in K.
*
*-----------------------------------------------------------------------
*
*----
*  SUBROUTINE ARGUMENTS
*----
      INTEGER NFD,NDTOT,MAXIT1,MAXIT2
      REAL ERMAXT,DTINV,RC,RIG,RGG,HCONV,HGAP,RO00,TEMP(NDTOT),TCOOL,
     1 FRO(NFD),QFUEL,BURN,POROS,FRACPU,KCONDF(NCONDF+3),
     2 KCONDC(NCONDC+1),ALPHA,TC0
      CHARACTER UCONDF*12,UCONDC*12
*----
*  LOCAL VARIABLES
*----
      PARAMETER (MAXNPO=20,ZKELV=273.15)
      INTEGER NM
      REAL ASM(MAXNPO,MAXNPO+1),XMMM0(MAXNPO),RO(MAXNPO),XX(MAXNPO)
      REAL HGGAP,PARA,PARAP,DRO,DROG,DROGIN,CONDXA(MAXNPO)
*
      IF(NDTOT.GT.MAXNPO) CALL XABORT('THMRES: MAXNPO OVERFLOW.')
      NGD=NDTOT-NFD
      N=NFD-1
      NG=NGD-2
      TC0=TC0-ZKELV
      DO I=1,NDTOT
         TEMP(I)=TEMP(I)-ZKELV
         XX(I)=TEMP(I)
      ENDDO
      DO J=1,NDTOT
         CALL XDRSET(ASM(1,J),NDTOT,0.0)
      ENDDO
      DRO=0.5*RC*RC/N
      DROG=0.5*(RGG*RGG-RIG*RIG)/NG
      DROGIN=1.0/DROG
      RO(1)=DRO/2.0
      DO J=2,N
         RO(J)=RO(J-1)+DRO
      ENDDO
      RO(N+1)=REAL(N)*DRO
      RO(N+2)=0.5*RIG*RIG
      DO J=(N+3),(NDTOT-1)
         RO(J)=0.5*RIG*RIG+(J-N-3)*DROG+0.5*DROG
      ENDDO
      RO(NDTOT)=RO(NDTOT-1)+0.5*DROG
*----
* CALCULATION OF CONDXA VECTOR FOR RELAXATION METHOD.
*----
*     in the fuel regions
      DO 71 J=1,N-1
          CONDXA(J)=2.0*(1.0-ALPHA)*(RO00+J*DRO)*
     >    THMCDI(TEMP(J),TEMP(J+1),BURN,POROS,FRACPU,ICONDF,NCONDF,
     >    KCONDF,UCONDF,IFRCDI)*(TEMP(J+1)-TEMP(J))/DRO/(RO(J+1)-RO(J))
   71  CONTINUE
       CONDXA(N)=4.0*(1.0-ALPHA)*THMCDI(TEMP(N),TEMP(N+1),BURN,
     > POROS,FRACPU,ICONDF,NCONDF,KCONDF,UCONDF,IFRCDI)
     > *(TEMP(N+1)-TEMP(N))*RO(N+1)/DRO/DRO
*     in the cladding
      DO 72 J=(N+3),(NDTOT-2)
         CONDXA(J)=2.0*(1.0-ALPHA)*(RO(J)+0.5*DROG)*
     >           THMGDI(TEMP(J+1),TEMP(J),ICONDC,NCONDC,KCONDC,UCONDC)*
     >           (TEMP(J+1)-TEMP(J))*DROGIN**2.0
   72 CONTINUE
      NM=NDTOT-1
      CONDXA(NM)=4.0*(1.0-ALPHA)*THMGDI(TEMP(NM),TEMP(NDTOT),ICONDC,
     > NCONDC,KCONDC,UCONDC)*(TEMP(NDTOT)-TEMP(NM))*RO(NDTOT)
     > *DROGIN**2.0
*     specific term in the cladding     
      CONDXA(NDTOT)=-4.0*(1.0-ALPHA)*THMGDI(TEMP(N+3),TEMP(N+2),ICONDC,
     > NCONDC,KCONDC,UCONDC)*(TEMP(N+3)-TEMP(N+2))*RO(N+2)*DROGIN**2.0
*----
*  ITERATIVE PROCEDURE
*----
      ITERT=0
   10 ITERT=ITERT+1
      IF(ITERT.GT.MAXIT1) CALL XABORT('THMRES: CONVERGENCE FAILURE(1)')
*----
*  POINTS 1 TO N-1 IN FUEL.
*----
      DO I=1,N
         SMI=0.0
         XMMM0(I)=THMCCD(XX(I),POROS,FRACPU)*DTINV
         DO K=1,I
            ASM(I,K)=XMMM0(K)
            SMI=SMI+THMCCD(TEMP(K),POROS,FRACPU)*DTINV*TEMP(K)
         ENDDO
         ASM(I,NDTOT+1)=SMI+QFUEL*FRO(I)
         IF(I.LT.N) THEN
            RPOS=RO00+REAL(I)*DRO
            PARA=2.0*ALPHA*RPOS*THMCDI(XX(I),XX(I+1),BURN,POROS,FRACPU,
     >          ICONDF,NCONDF,KCONDF,UCONDF,IFRCDI)/DRO/(RO(I+1)-RO(I))
            ASM(I,I)=ASM(I,I)+PARA
            ASM(I,I+1)=-PARA
            ASM(I,NDTOT+1)=ASM(I,NDTOT+1)+CONDXA(I)
         ENDIF
      ENDDO
*----
*  POINT N IN FUEL.
*----
      CONDXX=2.0*THMCDI(XX(N),XX(N+1),BURN,POROS,FRACPU,
     >       ICONDF,NCONDF,KCONDF,UCONDF,IFRCDI)/DRO
      PARA=2.0*ALPHA*CONDXX*RO(N+1)/DRO
      ASM(N,N)=ASM(N,N)+PARA
      ASM(N,N+1)=-PARA
      ASM(N,NDTOT+1)=ASM(N,NDTOT+1)+CONDXA(N)
*----
*  POINT N+1 AT THE OUTER SURFACE OF THE FUEL PELLET.
*----
      PARA=CONDXX*RC
      ASM(N+1,N)=-PARA
      ASM(N+1,N+1)=PARA+HGAP
      ASM(N+1,N+2)=-HGAP
      ASM(N+1,NDTOT+1)=0.0
*----
*  POINT N+2 IN GAP AND AT THE INNER SURFACE OF THE CLADDING.
*----
      PARA=2*THMGDI(XX(N+2),XX(N+3),ICONDC,NCONDC,KCONDC,UCONDC)*RIG
     >     /DROG
      HGGAP=HGAP*RC/RIG
      ASM(N+2,N+1)=-HGGAP
      ASM(N+2,N+2)=PARA+HGGAP
      ASM(N+2,N+3)=-PARA
      ASM(N+2,NDTOT+1)=0.0
*----
*  POINTS N+3 TO NDTOT-1 IN CLADDING.
*----
      PARAP=4.0*ALPHA*THMGDI(XX(N+2),XX(N+3),ICONDC,NCONDC,KCONDC,
     >      UCONDC)*RO(N+2)*DROGIN**2.0
      DO 15 I=(N+3),(NDTOT-1)
         XMMM0(I)=THMGCD(XX(I))*DTINV
         SMI=XMMM0(I)*TEMP(I)
         ASM(I,N+2)=-PARAP
         ASM(I,N+3)=PARAP+XMMM0(N+3)
         PARA=2.0*ALPHA*(RO(I)+0.5*DROG)*THMGDI(XX(I),XX(I+1),ICONDC,
     >        NCONDC,KCONDC,UCONDC)*DROGIN**2.0
         IF(I.EQ.(NDTOT-1)) THEN
             PARA=4.0*ALPHA*RO(NDTOT)*THMGDI(XX(I),XX(I+1),ICONDC,
     >            NCONDC,KCONDC,UCONDC)*DROGIN**2.0
         ENDIF
         ASM(I,I+1)=-PARA
         ASM(I,I)=XMMM0(I)+PARA
         IF(I.EQ.(N+3)) ASM(I,I)=ASM(I,I)+PARAP
         IF(I.GT.(N+3)) THEN
            SMI=SMI+THMGCD(TEMP(N+3))*DTINV*TEMP(N+3)
         ENDIF
         DO 16 K=(N+4),(I-1)
            ASM(I,K)=XMMM0(K)
            SMI=SMI+THMGCD(TEMP(K))*DTINV*TEMP(K)
   16    CONTINUE
         ASM(I,NDTOT+1)=SMI+CONDXA(I)+CONDXA(NDTOT)
   15 CONTINUE
*----
*  POINT NDTOT AT THE OUTER SURFACE OF THE CLADDING AT THE INTERFACE
*  WITH THE COOLANT.
*----
      PARA=2.0*THMGDI(XX(NDTOT),XX(NDTOT-1),ICONDC,NCONDC,KCONDC,
     >     UCONDC)*RGG/DROG
      ASM(NDTOT,NDTOT-1)=-PARA
      ASM(NDTOT,NDTOT)=PARA+HCONV
      ASM(NDTOT,NDTOT+1)=HCONV*(TCOOL-ZKELV)
*----
*  RESOLUTION OF THE MATRIX LINEAR SYSTEM.
*----
      CALL ALSB(NDTOT,1,ASM,IER,MAXNPO)
      IF(IER.NE.0) CALL XABORT('THMRES: SINGULAR MATRIX')
*----
*  CONVERGENCE TEST.
*----
      ERR=0.0
      DO J=1,NDTOT
         ERR=MAX(ERR,ABS(XX(J)-ASM(J,NDTOT+1)))
         XX(J)=ASM(J,NDTOT+1)
      ENDDO
      IF((ERR.LT.ERMAXT).AND.(ITERT.NE.1)) GO TO 20
      GO TO 10
*----
*  COMPUTE THE CENTER-PELLET AND AVERAGED PELLET TEMPERATURES.
*----
   20 TC=XX(1)
      ITERC=0
      XMM1=THMCCD((TC0+TEMP(1))*0.5,POROS,FRACPU)*0.5*DTINV
   30 ITERC=ITERC+1
      IF(ITERC.GT.MAXIT2) CALL XABORT('THMRES: CONVERGENCE FAILURE(2)')
      TC1=TC
      XMM=THMCCD((TC1+XX(1))*0.5,POROS,FRACPU)*0.5*DTINV
      TC=QFUEL*FRO(1)+XMM1*(TC0+TEMP(1))-XMM*XX(1)
      TCA=4.0*THMCDI(TC1,XX(1),BURN,POROS,FRACPU,ICONDF,NCONDF,KCONDF,
     > UCONDF,IFRCDI)/DRO
      TC=TC+TCA*XX(1)
      TC=TC/(XMM+TCA)
      DELTAA=ABS(TC-TC1)
      IF((DELTAA.LT.ERMAXT).AND.(ITERC.NE.1)) GO TO 40
      GO TO 30
   40 TC0=TC+ZKELV
      DO J=1,NDTOT
         TEMP(J)=XX(J)+ZKELV
      ENDDO
      RETURN
      END
