*DECK THMRES
      SUBROUTINE THMRES(NFD,MAXIT1,MAXIT2,ERMAX,ALPHA,DTINV,RC,RIG,RGG,
     1 HCONV,HGAP,RO00,TCOOL,CONDCO,FRO,QFUEL,BURN,POROS,FRACPU,TEMP,
     2 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
*
*Parameters: input
* NFD     number of radial zones in the cgadded fuel rod. One point is
*         taken in gap and one point is taken in clad.
* MAXIT1  maximum number of conduction iterations
* MAXIT2  maximum number of center-pellet iterations
* ERMAX   convergence criterion
* ALPHA   implicit parameter
* 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
* CONDCO  conduction coefficient
* FRO     flux depression factor
* QFUEL   volumic power in fuel
* BURN    fuel burnup in MWday/tonne
* POROS   fuel porosity
* FRACPU  Plutonium percent fraction
*
*Parameters: input/output
* TEMP    initial and final meshwise temperatures in K
*
*Parameters: output
* TC0     center-pellet temperature in K
*
*-----------------------------------------------------------------------
*
*----
*  SUBROUTINE ARGUMENTS
*----
      INTEGER NFD,MAXIT1,MAXIT2
      REAL ERMAX,ALPHA,DTINV,RC,RIG,RGG,HCONV,HGAP,RO00,TEMP(NFD),TCOOL,
     1 CONDCO(NFD),FRO(NFD),QFUEL,BURN,POROS,FRACPU,TC0
*----
*  LOCAL VARIABLES
*----
      PARAMETER (MAXNPO=15,ZKELV=273.15)
      REAL ASM(MAXNPO,MAXNPO+1),XMMM0(MAXNPO),RO(MAXNPO),XX(MAXNPO)
      DOUBLE PRECISION DDD,HGGAP
*
      IF(NFD.GT.MAXNPO) CALL XABORT('THMRES: MAXNPO OVERFLOW.')
      N=NFD-2
      DO I=1,NFD
         TEMP(I)=TEMP(I)-ZKELV
         XX(I)=TEMP(I)
      ENDDO
      DO J=1,NFD+1
         CALL XDRSET(ASM(1,J),NFD,0.0)
      ENDDO
      DRO=0.5*RC*RC/N
      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*RGG*RGG
*----
*  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+XMMM0(K)*TEMP(K)
         ENDDO
         ASM(I,NFD+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)/
     1           DRO/(RO(I+1)-RO(I))
            ASM(I,I)=ASM(I,I)+PARA
            ASM(I,I+1)=-PARA
            ASM(I,NFD+1)=ASM(I,NFD+1)+CONDCO(I)
         ENDIF
      ENDDO
*----
*  POINT N IN FUEL. CONDXX IS THE CONDUCTION INTEGRAL IN THE FIRST MESH.
*----
      CONDXX=2.0*THMCDI(XX(N),XX(N+1),BURN,POROS,FRACPU)/DRO
      PARA=2.0*ALPHA*CONDXX*RO(N+1)/DRO
      ASM(N,N)=ASM(N,N)+PARA
      ASM(N,N+1)=-PARA
      ASM(N,NFD+1)=ASM(N,NFD+1)+CONDCO(N)
*----
*  GAP (N+1) AND CLAD (N+2). HGGAP IS THE EQUIVALENT THERMAL EXCHANGE
*  COEFFICIENT FOR THE GAP AND CLAD. XKG IS THE THERMAL CONDUCTIVITY
*  OF ZIRCALOY IN WATT/METER/C.
*----
      ECLAD=RGG-RIG
      RN1RG=RC/RGG
      PARA=CONDXX*RC
      RGGAP=(RGG+RC)/2.0
      ASM(N+1,N)=-PARA
      XKG1=12.0+1.25E-2*XX(N+2)
      DDD=ECLAD/XKG1
      HGGAP=1.0D0/(1.0D0/HGAP+ECLAD*RC/XKG1/RGGAP)
      ASM(N+1,N+1)=PARA+REAL(HGGAP)
      ASM(N+1,N+2)=-REAL(HGGAP)
      ASM(N+1,NFD+1)=0.0
      ASM(N+2,N+1)=-REAL(HGGAP)*RN1RG
      ASM(N+2,N+2)=REAL(HGGAP)*RN1RG+HCONV
      ASM(N+2,NFD+1)=HCONV*(TCOOL-ZKELV)
      CALL ALSB(NFD,1,ASM,IER,MAXNPO)
      IF(IER.NE.0) CALL XABORT('THMRES: SINGULAR MATRIX.')
*----
*  CONVERGENCE TEST
*----
      ERR=0.0
      DO J=1,NFD
         ERR=MAX(ERR,ABS(XX(J)-ASM(J,NFD+1)))
         XX(J)=ASM(J,NFD+1)
      ENDDO
      IF((ERR.LT.ERMAX).AND.(ITERT.NE.1)) GO TO 20
      GO TO 10
*----
*  COMPUTE THE CENTER-PELLET AND AVERAGED PELLET ERATURES
*----
   20 TC=XX(1)
      TCAD=0.0
      ITERC=0
   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)+XMM*(TEMP(1)-XX(1))
      TCA=4.*ALPHA*THMCDI(TC1,XX(1),BURN,POROS,FRACPU)/DRO
      TC=TC+TCAD+TCA*XX(1)
      TC=TC/(XMM+TCA)
      DELTAA=ABS(TC-TC1)
      IF((DELTAA.LT.ERMAX).AND.(ITERC.NE.1)) GO TO 40
      GO TO 30
   40 TC0=TC+ZKELV
      DO J=1,NFD
         TEMP(J)=XX(J)+ZKELV
      ENDDO
      RETURN
      END
