!vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv!
!                                                                      !
!  Module name: USR_RATES                                              !
!  Purpose: Hook for user defined reaction rates                       !
!  Author: E.Ramirez                                  Date: 07-Sep-16  !
!               Biomass fast pyrolysis kinetics based on 
!               Blasi 1993, Chan 1985, and Liden 1988 
!  Author: E.Ramirez                                  Date: 8-JUN-17  
!               Isothermal conditions kimetics always at 773K (500C)
!                                                                      !
!^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^!
      SUBROUTINE USR_RATES(IJK, RATES)

      USE param
      USE param1
      USE parallel
      USE fldvar
      USE rxns
      USE energy
      USE geometry
      USE run
      USE indices
      USE physprop
      USE constant
      USE funits
      USE compar
      USE sendrecv
      USE fun_avg
      USE toleranc
      USE usr
      USE functions

      IMPLICIT NONE


      INTEGER, INTENT(IN) :: IJK

      DOUBLE PRECISION, DIMENSION(NO_OF_RXNS), INTENT(OUT) :: RATES

! Index used for looping over all reactions.
      INTEGER L

!-----------------------------------------------
      INCLUDE 'species.inc'
      INCLUDE 'usrnlst.inc'

! Calculation variables:
!`````````````````````````````````````````````````````````````````````//
! Bounded phase temperatures (K)
      DOUBLE PRECISION :: TgX   ! gas phase
      DOUBLE PRECISION :: TsX   ! solids phase 1
      DOUBLE PRECISION :: TgsX  ! Average of gas/solids 1     

      DOUBLE PRECISION :: X_gTar
      DOUBLE PRECISION :: X_sWood

      ! Prevent reactions when mass fraction falls below c_Limiter.
      DOUBLE PRECISION, parameter :: c_Limiter = 1.0d-6
! Reaction specific variables:
!`````````````````````````````````````````````````````````````````````//
!A = pre-factor (1/s) and E = activation energy (J/mol)
      DOUBLE PRECISION, PARAMETER :: A1 = 1.08e11,   E1 = 148.0e3
      DOUBLE PRECISION, PARAMETER :: A2 = 4.38e9,    E2 = 152.7e3
      DOUBLE PRECISION, PARAMETER :: A3 = 3.75e6,    E3 = 111.7e3
      DOUBLE PRECISION, PARAMETER :: A4 = 1.00e5,    E4 = 108.0e3
      DOUBLE PRECISION, PARAMETER :: A5 = 4.28e6,    E5 = 108.0e3

      DOUBLE PRECISION :: K1, K2, K3, K4, K5
      DOUBLE PRECISION, PARAMETER :: RGAS_LOCAL = 8.314472 !J/(K.mol)
 
! calculate variables for reactions:
!`````````````````````````````````````````````````````````````````````//
! Bound the gas and solids phase temperatures.
      TgX  = min(TMAX, T_g(IJK))

      ! Alias the wood/biooil phase volume fractions
      X_gTar = X_g(IJK,biooil)

      IF( X_gTar .LE. c_Limiter) THEN
        Rates(biooil2gas)   = ZERO
      ELSE
        ! Tar to gas conversion
        !K4 = A4 * Exp(-E4/(RGAS_LOCAL*T_g(IJK)))
        K5 = A5 * Exp(-E5/(RGAS_LOCAL*TgX))
        Rates(biooil2gas) = K5 * Rop_g(IJK)*X_gTar/MW_g(biooil)
      END IF

!  Write the reaction rate data into .SPA file and for visualizing.
      ReactionRates(IJK,biooil2gas) = RATES(biooil2gas)


      RETURN

      END SUBROUTINE USR_RATES
