C
C     Thick-walled cylinder error calculations
C
      subroutine uvarm(uvar,direct,t,time,dtime,cmname,orname,
     1                 nuvarm,noel,npt,nlayer,nspt,kstep,kinc,
     2                 ndi,nshr, coord, jmac, jmatyp,
     $                 matlayo, laccflg)
C
      INCLUDE 'ABA_PARAM.INC'
C
      common /norms/ errormax,totalerror,totalstress,lelements,kiter
C
      character*80 cmname,orname
      character*3 flgray(15)
      dimension uvar(*),time(2),direct(3,3),t(3,3),
     *     jmac(*), coord(*), jmatyp(*)
      dimension array(15),jarray(15)

      call getvrm('IVOL',array,jarray,flgray,jrcd,
     $     jmac, jmatyp, matlayo, laccflg)
      vol = array(1)
C
      if (noel.eq.1.and.npt.eq.1) then
        kiter = kiter + 1
      end if
C
      call getvrm('S',array,jarray,flgray,jrcd,
     $     jmac, jmatyp, matlayo, laccflg)
      RADIUSSQ = coord(1)**2+coord(2)**2
      exactStress = (-80.d0/(3.d0*radiussq) + 1.d0/15.d0)
      UVAR(1) = ARRAY(1) - exactStress
      if (kiter.eq.1) then
        totalerror = totalerror + vol * uvar(1)**2
        totalstress = totalstress + vol * exactStress**2
        if (abs(uvar(1)).gt.errormax) errormax = abs(uvar(1))
        if (npt.eq.1) lelements = lelements + 1
      end if
      UVAR(2) = ARRAY(2) - (80.d0/(3.d0*radiussq) + 1.d0/15.d0)
      UVAR(3) = ARRAY(1) + ARRAY(2) - 2.d0/15.d0
c
      return
      end
C
C  User defined routine to close external databases

      subroutine uexternaldb(lop,lrestart,time,dtime,kstep,kinc)
C
      INCLUDE 'ABA_PARAM.INC'
C
      common /norms/ errormax,totalerror,totalstress,lelements,kiter
      dimension time(2)
      character*256 OUTDIR,name
C
      if (lop.eq.0.or.lop.eq.1) then
        totalerror = 0.0d0
        totalstress = 0.0d0
        lelements = 0
        errormax = -1.d36
        kiter = -1
      end if
      if (lop.eq.3) then
        CALL GETOUTDIR(OUTDIR,LENOUTDIR)
        write (name,"(A,A)") OUTDIR(1:LENOUTDIR),'/CYLINDER_QA.dat'
        OPEN (unit=25,FILE=name,status='OLD',position='APPEND')
        write (25,100) lelements,sqrt(totalerror/totalstress),errormax
        CLOSE(unit=25)
 100    format (1x,I10,5(2x,1pg16.8))
      end if
C
      return
      end