	subroutine temphy( ns, h, y, iwk, rwk, t )

!  Routine to determine temperature, given enthalpy and mass fractions.
!  Robust version using regula falsi.   S.B. Pope

!  input:
!	ns - number of species
!	h  - specific enthalpy of gas mixture
!	y  - species mass fractions
!	iwk - Chemkin integer work array
!	rwk - Chemkin real work array
!  output:
!	t   - temperature (K)
!  notes:
!	program stops if computed temperature is outside range
!	(tbadlo, tbadhi)

	implicit double precision (a-h,o-z), integer (i-n) 
	dimension y(ns), iwk(*), rwk(*)

	data tbadlo, tbadhi/ 280.d0, 5000.d0 /
	data temtol/ 1.d-6 /
	data nitmax/ 100 /

!  evaluate enthalpy at tbadlo and tbadhi

	tlo = tbadlo
	thi = tbadhi
	call ckhbms( tlo, y, iwk, rwk, hlo )
	call ckhbms( thi, y, iwk, rwk, hhi )

!  give up if h outside range

	if( h .lt. hlo  .or.  h .gt. hhi ) then
	   write(0,*)'temphy: temperature out of range'
	   write(0,*)'temphy: tbadlo, tbadhi', tbadlo, tbadhi
	   write(0,*)'temphy: h, hlo, hhi', h, hlo, hhi
	   stop
	endif

!  find temperature using regula falsi

	iter = 0
100	iter = iter + 1

	cpinv = (thi-tlo) / (hhi-hlo)
	t     = tlo + (h - hlo) * cpinv
	call ckhbms( t, y, iwk, rwk, hm )

!  test for convergence

	terr = abs(hm-h) * cpinv
	if( terr .lt. temtol ) return

!  test for too many iterations

	if( iter .gt. nitmax ) then
	   write(0,*)'temphy: too many iterations', iter, terr, t
	   stop
	endif

!  replace hi or low

	if( hm .gt. h ) then
	   hhi = hm
	   thi = t
	else
	   hlo = hm
	   tlo = t
	endif

	go to 100

	end
