C CONTHE    SOURCE    BECC      09/11/18    21:15:01     6542
      subroutine conthe(nesp, nordpo, acv, w, h0, runiv,
     &     tmaxcv, uvec, y,
     &     tguess,
     &     acvtot, ux, uy, uz, p, t,
     &     logipg, lognc, logneg, valer, meserr)
C
C     See PRIDE1.ESO
C
C     This subroutine creates some thermodynamical variables and
C     properties starting from the conservative variables.
C
C     INPUT:
C     nesp, nordpo,
C     acv(0:nordpo,nesp)
C     w
C     h0
C     runiv = universal constant of gas
C     tmaxcv = upper limit for cv expansion
C     uvec(1,*) = rho
C     uvec(2,*) = rhou
C     uvec(3,*) = rhov
C     uvec(4,*) = rhow
C     uvec(5,*) = rhoet
C     y
C     tguess = guess value for the temperature in the Newton method
C
C     OUTPUT:
C     acvtot   = coefficients such that
C                cv = \sum_{i=1,nordpo+1} acvtot(i) T^{i-1}
C                ether = \sum_{i=1,nordpo+1} acvtot(i) T^{i} / (i)
C     rho      = density
C     ux,uy,uz = velocity
C     p        = pressure
C     t        = temperature
C
C     ERROR
C     logipg, lognc, logneg, valer, meserr
C
      integer nesp, nordpo
     &     , iesp, icoef
      real*8 acv(0:nordpo,nesp), w(nesp), h0(nesp), runiv, tmaxcv
     &     , uvec(1:5), y(1:nesp)
     &     , tguess, acvtot(1:(nordpo+1))
     &     , rho, ux, uy, uz, p, t
     &     , valer(2)
     &     , hftot, rgas, un2, ether
      character*(40) meserr(2)
      logical lognc, logneg, logipg
C
C     Initialisation of some variables
C
      hftot = 0.0d0
      do icoef = 1 ,(nordpo+1), 1
         acvtot(icoef) = 0.0d0
      enddo
      rgas = 0.0d0
C
C     Density, velocity, internal energy
C
      rho = uvec(1)
      ux = uvec(2) / rho
      uy = uvec(3) / rho
      uz = uvec(4) / rho
      un2 = (ux * ux) + (uy * uy) + (uz * uz)
C      do iesp = 1, 5
C         write(*,*) uvec(iesp)
C      enddo
C
C     rgas, hftot, acvtot
C     In order to avoid one more loop, this could have be done before
C
      do iesp = 1, nesp, 1
C        Rgas = y(iesp) / W_iesp
         Rgas = Rgas + (y(iesp)/w(iesp))
C        hftot = hftot + (y_i * hf0i)
         hftot = hftot + (y(iesp) * h0(iesp))
         do icoef = 0, nordpo, 1
            acvtot(icoef+1) = acvtot(icoef+1) +
     &           (y(iesp) * acv(icoef,iesp))
         enddo
      enddo
      rgas = rgas * runiv
C
C     Computation of temperature T
C     if (T > Tmax) => cv = cv(Tmax)
C     if (T < Tmin) => cv = cv(Tmin)
C
      ether = (uvec(5) / rho) - (0.5d0 * un2)
      ether = ether - hftot
C      write(*,*) hftot, ether
      if(ether .lt. 0.0D0)then
         logneg = .true.
         meserr(1) = 'internal sensible energy               '
         valer(1) = ether
         goto 9999
      endif
      call prite1(nordpo,Tmaxcv,acvtot,ether,tguess,T,lognc,logipg)
      if(logipg)then
         goto 9999
      endif
      if(lognc)then
         goto 9999
      endif
C
      p = rgas * rho * T
 9999 continue
      return
      end


