!Models-3/CMAQ code adapted for MONARCH

!***********************************************************************
!   Portions of Models-3/CMAQ software were developed or based on      *
!   information from various groups: Federal Government employees,     *
!   contractors working on a United States Government contract, and    *
!   non-Federal sources (including research institutions).  These      *
!   research institutions have given the Government permission to      *
!   use, prepare derivative works, and distribute copies of their      *
!   work in Models-3/CMAQ to the public and to permit others to do     *
!   so.  EPA therefore grants similar permissions for use of the       *
!   Models-3/CMAQ software, but users are requested to provide copies  *
!   of derivative works to the Government without restrictions as to   *
!   use by others.  Users are responsible for acquiring their own      *
!   copies of commercial software associated with Models-3/CMAQ and    *
!   for complying with vendor requirements.  Software copyrights by    *
!   the MCNC Environmental Modeling Center are used with their         *
!   permissions subject to the above restrictions.                     *
!***********************************************************************

! RCS file, release, date & time of last delta, author, state, [and locker]
! $Header: /project/work/rep/CCTM/src/chem/ebi_cb05cl_ae5/hrg3.F,v 1.1.1.1 2008/02/08 21:09:23 sjr Exp $ 

! what(1) key, module and SID; SCCS file; date and time of last delta:
! %W% %P% %G% %U%
!MONARCH
       SUBROUTINE EXT_HRG3( DTC )
!MONARCH
!**********************************************************************
!
!  FUNCTION:  To solve for the concentration of C2O3 and PAN
!
!  PRECONDITIONS: For the CB05CL family of mechanisms
!
!  KEY SUBROUTINES/FUNCTIONS CALLED: None
!
!  REVISION HISTORY: Created by EBI solver program, Jan. 31, 2008
!
!**********************************************************************
!MONARCH
      USE EXT_HRDATA
!MONARCH
      IMPLICIT NONE

!..INCLUDES:  NONE

!..ARGUMENTS:
      REAL DTC              ! Time step


!..PARAMETERS: NONE


!..EXTERNAL FUNCTIONS: NONE


!..SAVED LOCAL VARIABLES:
      CHARACTER( 16 ), SAVE  :: PNAME = 'HRG3'      ! Program name


!..SCRATCH LOCAL VARIABLES:
      REAL   A, B, C, Q   ! Quadratic equation terms
      REAL   CMN          ! Temp scalar
      REAL   L8           ! Loss of CCO_O2
      REAL   L9           ! Loss of PAN
      REAL   P8           ! Production of CCO_O2

      REAL   K8_8         ! Kc2o3+c2o3 * delta t
      REAL   R8_9         ! Kpan-->c2o3 * delta t
      REAL   R9_8         ! Kc2o3+no2-->pan * [NO2] * delta t

!**********************************************************************


!..Production of C2O3 (except from PAN )
      P8 =           RXRAT(  83 )      ! ALD2+O=C2O3+OH
     &    +          RXRAT(  84 )      ! ALD2+OH=C2O3
     &    +          RXRAT(  85 )      ! ALD2+NO3=C2O3+HNO3
     &    +          RXRAT(  95 )      ! PACD+OH=C2O3
     &    +          RXRAT( 135 )      ! OPEN=C2O3+HO2+CO
     &    +          RXRAT( 136 )      ! OPEN+OH=C2O3+2*CO+2*HO2+...
     &    +  0.620 * RXRAT( 137 )      ! OPEN+O3=0.62*C2O3+...
     &    +          RXRAT( 139 )      ! OH+MGLY=C2O3+XO2
     &    +          RXRAT( 140 )      ! MGLY=C2O3+HO2+CO
     &    +  0.210 * RXRAT( 145 )      ! OH+ISPD=0.21*C2O3+...
     &    +  0.114 * RXRAT( 146 )      ! O3+ISPD=0.114*C2O3+...
     &    +  0.967 * RXRAT( 148 )      ! ISPD=0.967*C2O3+0.067*ALD2+...
     &    +          RXRAT( 173 )      ! CL+ALD2=C2O3+HCL

!..Loss frequency of C2O3 ( not including C2O3 + C2O3 )
      L8    =            RKI(  87 ) * YC( NO      )   ! C2O3+NO=MEO2+NO2
     &        +          RKI(  88 ) * YC( NO2     )   ! C2O3+NO2=PAN
     &        +          RKI(  91 ) * YC( HO2     )   ! C2O3+HO2=...
     &        +          RKI(  92 ) * YC( MEO2    )   ! C2O3+MEO2=...
     &        +          RKI(  93 ) * YC( XO2     )   ! C2O3+XO2=...
     &        +          RKI( 111 ) * YC( CXO3    )   ! C2O3+CXO3=MEO2+...

!..Loss frequency of PAN
      L9    =            RKI(  89 )                   ! PAN=C2O3+NO2
     &        +          RKI(  90 )                   ! PAN=C2O3+NO2

!..K8_8, R8_9, and R9_8 terms
      K8_8  = RKI(  94 ) * DTC

      R8_9  = ( RKI(  89 )
     &      +   RKI(  90 ) ) * DTC 

      R9_8  = ( RKI(  88 ) * YC( NO2 ) ) * DTC 

!..Solution of quadratic equation to get C2O3 & PAN
      CMN = 1.0 + L9 * DTC
      A = 2.0 * K8_8 * CMN
      B = CMN * ( 1.0 + L8 * DTC ) - R8_9 * R9_8
      C = CMN * ( YC0( C2O3 ) + P8 * DTC ) +  R8_9 * YC0( PAN )

      Q = -0.5 * ( B + SIGN( 1.0, B ) * SQRT( B * B + 4.0 * A * C ) )

      YCP( C2O3 ) = MAX( Q / A , -C / Q  )

      YCP( PAN ) = ( YC0( PAN ) +  R9_8 * YCP( C2O3 ) ) / CMN

      RETURN

      END
