! ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
! 
! Auxiliary Routines File
! 
! Generated by KPP-2.2.1_rs5 symbolic chemistry Kinetics PreProcessor
!       (http://www.cs.vt.edu/~asandu/Software/KPP)
! KPP is distributed under GPL, the general public licence
!       (http://www.gnu.org/copyleft/gpl.html)
! (C) 1995-1997, V. Damian & A. Sandu, CGRER, Univ. Iowa
! (C) 1997-2005, A. Sandu, Michigan Tech, Virginia Tech
!     With important contributions from:
!        M. Damian, Villanova University, USA
!        R. Sander, Max-Planck Institute for Chemistry, Mainz, Germany
! 
! File                 : SUAQ_messy_mecca_kpp_Util.f90
! Time                 : Tue Jan 23 13:18:01 2018
! Working directory    : /gpfs/projects/bsc32/bsc32771/MN3/NMMB/test/KPP/caaba_3.0gmdd/mecca
! Equation file        : SUAQ_messy_mecca_kpp.kpp
! Output root filename : SUAQ_messy_mecca_kpp
! 
! ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~



MODULE SUAQ_messy_mecca_kpp_Util

  USE SUAQ_messy_mecca_kpp_Parameters
  IMPLICIT NONE

CONTAINS



! User INLINED Utility Functions

! from xmecca:
SUBROUTINE initialize_indexarrays
  USE SUAQ_messy_mecca_kpp_global     ! ind_XYZ_a(:) arrays
  USE SUAQ_messy_mecca_kpp_parameters ! ind_XYZ_a## scalars
  IMPLICIT NONE
  ! from aerosol.awk:
  ind_O2_a(01)        = ind_O2_a01
  ind_O3_a(01)        = ind_O3_a01
  ind_OH_a(01)        = ind_OH_a01
  ind_HO2_a(01)       = ind_HO2_a01
  ind_H2O_a(01)       = ind_H2O_a01
  ind_H2O2_a(01)      = ind_H2O2_a01
  ind_NH3_a(01)       = ind_NH3_a01
  ind_NO_a(01)        = ind_NO_a01
  ind_NO2_a(01)       = ind_NO2_a01
  ind_NO3_a(01)       = ind_NO3_a01
  ind_HONO_a(01)      = ind_HONO_a01
  ind_HNO3_a(01)      = ind_HNO3_a01
  ind_HNO4_a(01)      = ind_HNO4_a01
  ind_N2O5_a(01)      = ind_N2O5_a01
  ind_CH3OH_a(01)     = ind_CH3OH_a01
  ind_HCOOH_a(01)     = ind_HCOOH_a01
  ind_HCHO_a(01)      = ind_HCHO_a01
  ind_CH3O2_a(01)     = ind_CH3O2_a01
  ind_CH3OOH_a(01)    = ind_CH3OOH_a01
  ind_CO2_a(01)       = ind_CO2_a01
  ind_CH3CO2H_a(01)   = ind_CH3CO2H_a01
  ind_PAN_a(01)       = ind_PAN_a01
  ind_C2H5O2_a(01)    = ind_C2H5O2_a01
  ind_CH3CHO_a(01)    = ind_CH3CHO_a01
  ind_CH3COCH3_a(01)  = ind_CH3COCH3_a01
  ind_Cl_a(01)        = ind_Cl_a01
  ind_Cl2_a(01)       = ind_Cl2_a01
  ind_HCl_a(01)       = ind_HCl_a01
  ind_HOCl_a(01)      = ind_HOCl_a01
  ind_Br_a(01)        = ind_Br_a01
  ind_Br2_a(01)       = ind_Br2_a01
  ind_HBr_a(01)       = ind_HBr_a01
  ind_HOBr_a(01)      = ind_HOBr_a01
  ind_BrCl_a(01)      = ind_BrCl_a01
  ind_I2_a(01)        = ind_I2_a01
  ind_IO_a(01)        = ind_IO_a01
  ind_HI_a(01)        = ind_HI_a01
  ind_HOI_a(01)       = ind_HOI_a01
  ind_ICl_a(01)       = ind_ICl_a01
  ind_IBr_a(01)       = ind_IBr_a01
  ind_HIO3_a(01)      = ind_HIO3_a01
  ind_SO2_a(01)       = ind_SO2_a01
  ind_H2SO4_a(01)     = ind_H2SO4_a01
  ind_DMS_a(01)       = ind_DMS_a01
  ind_DMSO_a(01)      = ind_DMSO_a01
  ind_Hg_a(01)        = ind_Hg_a01
  ind_HgO_a(01)       = ind_HgO_a01
  ind_HgOH_a(01)      = ind_HgOH_a01
  ind_HgOHOH_a(01)    = ind_HgOHOH_a01
  ind_HgOHCl_a(01)    = ind_HgOHCl_a01
  ind_HgCl2_a(01)     = ind_HgCl2_a01
  ind_HgBr2_a(01)     = ind_HgBr2_a01
  ind_HgSO3_a(01)     = ind_HgSO3_a01
  ind_ClHgBr_a(01)    = ind_ClHgBr_a01
  ind_BrHgOBr_a(01)   = ind_BrHgOBr_a01
  ind_ClHgOBr_a(01)   = ind_ClHgOBr_a01
  ind_O2m_a(01)       = ind_O2m_a01
  ind_OHm_a(01)       = ind_OHm_a01
  ind_Hp_a(01)        = ind_Hp_a01
  ind_NH4p_a(01)      = ind_NH4p_a01
  ind_NO2m_a(01)      = ind_NO2m_a01
  ind_NO3m_a(01)      = ind_NO3m_a01
  ind_NO4m_a(01)      = ind_NO4m_a01
  ind_CO3m_a(01)      = ind_CO3m_a01
  ind_HCOOm_a(01)     = ind_HCOOm_a01
  ind_HCO3m_a(01)     = ind_HCO3m_a01
  ind_CH3COOm_a(01)   = ind_CH3COOm_a01
  ind_Clm_a(01)       = ind_Clm_a01
  ind_Cl2m_a(01)      = ind_Cl2m_a01
  ind_ClOm_a(01)      = ind_ClOm_a01
  ind_ClOHm_a(01)     = ind_ClOHm_a01
  ind_Brm_a(01)       = ind_Brm_a01
  ind_Br2m_a(01)      = ind_Br2m_a01
  ind_BrOm_a(01)      = ind_BrOm_a01
  ind_BrOHm_a(01)     = ind_BrOHm_a01
  ind_BrCl2m_a(01)    = ind_BrCl2m_a01
  ind_Br2Clm_a(01)    = ind_Br2Clm_a01
  ind_Im_a(01)        = ind_Im_a01
  ind_IO2m_a(01)      = ind_IO2m_a01
  ind_IO3m_a(01)      = ind_IO3m_a01
  ind_ICl2m_a(01)     = ind_ICl2m_a01
  ind_IClBrm_a(01)    = ind_IClBrm_a01
  ind_IBr2m_a(01)     = ind_IBr2m_a01
  ind_SO3m_a(01)      = ind_SO3m_a01
  ind_SO3mm_a(01)     = ind_SO3mm_a01
  ind_SO4m_a(01)      = ind_SO4m_a01
  ind_SO4mm_a(01)     = ind_SO4mm_a01
  ind_SO5m_a(01)      = ind_SO5m_a01
  ind_HSO3m_a(01)     = ind_HSO3m_a01
  ind_HSO4m_a(01)     = ind_HSO4m_a01
  ind_HSO5m_a(01)     = ind_HSO5m_a01
  ind_CH3SO3m_a(01)   = ind_CH3SO3m_a01
  ind_CH2OHSO3m_a(01) = ind_CH2OHSO3m_a01
  ind_Hgp_a(01)       = ind_Hgp_a01
  ind_Hgpp_a(01)      = ind_Hgpp_a01
  ind_HgOHp_a(01)     = ind_HgOHp_a01
  ind_HgClp_a(01)     = ind_HgClp_a01
  ind_HgCl3m_a(01)    = ind_HgCl3m_a01
  ind_HgCl4mm_a(01)   = ind_HgCl4mm_a01
  ind_HgBrp_a(01)     = ind_HgBrp_a01
  ind_HgBr3m_a(01)    = ind_HgBr3m_a01
  ind_HgBr4mm_a(01)   = ind_HgBr4mm_a01
  ind_HgSO32mm_a(01)  = ind_HgSO32mm_a01
  ind_D1O_a(01)       = ind_D1O_a01
  ind_D2O_a(01)       = ind_D2O_a01
  ind_DAHp_a(01)      = ind_DAHp_a01
  ind_DA_a(01)        = ind_DA_a01
  ind_DAm_a(01)       = ind_DAm_a01
  ind_DGtAi_a(01)     = ind_DGtAi_a01
  ind_DGtAs_a(01)     = ind_DGtAs_a01
  ind_PROD1_a(01)     = ind_PROD1_a01
  ind_PROD2_a(01)     = ind_PROD2_a01
  ind_Nap_a(01)       = ind_Nap_a01
END SUBROUTINE initialize_indexarrays

! End INLINED Utility Functions

! Utility Functions from KPP_HOME/util/util
! ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
! 
! UTIL - Utility functions
!   Arguments :
! 
! ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

! ****************************************************************
!                            
! InitSaveData - Opens the data file for writing
!   Parameters :                                                  
!
! ****************************************************************

      SUBROUTINE InitSaveData ()

      USE SUAQ_messy_mecca_kpp_Parameters

      open(10, file='SUAQ_messy_mecca_kpp.dat')

      END SUBROUTINE InitSaveData

! End of InitSaveData function
! ****************************************************************

! ****************************************************************
!                            
! SaveData - Write LOOKAT species in the data file 
!   Parameters :                                                  
!
! ****************************************************************

      SUBROUTINE SaveData ()

      USE SUAQ_messy_mecca_kpp_Global
      USE SUAQ_messy_mecca_kpp_Monitor

      INTEGER i

      WRITE(10,999) (TIME-TSTART)/3600.D0,  &
                   (C(LOOKAT(i))/CFACTOR, i=1,NLOOKAT)
999   FORMAT(E24.16,100(1X,E24.16))

      END SUBROUTINE SaveData

! End of SaveData function
! ****************************************************************

! ****************************************************************
!                            
! CloseSaveData - Close the data file 
!   Parameters :                                                  
!
! ****************************************************************

      SUBROUTINE CloseSaveData ()

      USE SUAQ_messy_mecca_kpp_Parameters

      CLOSE(10)

      END SUBROUTINE CloseSaveData

! End of CloseSaveData function
! ****************************************************************

! ****************************************************************
!                            
! GenerateMatlab - Generates MATLAB file to load the data file 
!   Parameters : 
!                It will have a character string to prefix each 
!                species name with.                                                 
!
! ****************************************************************

      SUBROUTINE GenerateMatlab ( PREFIX )

      USE SUAQ_messy_mecca_kpp_Parameters
      USE SUAQ_messy_mecca_kpp_Global
      USE SUAQ_messy_mecca_kpp_Monitor

      
      CHARACTER(LEN=8) PREFIX 
      INTEGER i

      open(20, file='SUAQ_messy_mecca_kpp.m')
      write(20,*) 'load SUAQ_messy_mecca_kpp.dat;'
      write(20,990) PREFIX
990   FORMAT(A1,'c = SUAQ_messy_mecca_kpp;')
      write(20,*) 'clear SUAQ_messy_mecca_kpp;'
      write(20,991) PREFIX, PREFIX
991   FORMAT(A1,'t=',A1,'c(:,1);')
      write(20,992) PREFIX
992   FORMAT(A1,'c(:,1)=[];')

      do i=1,NLOOKAT
        write(20,993) PREFIX, SPC_NAMES(LOOKAT(i)), PREFIX, i
993     FORMAT(A1,A6,' = ',A1,'c(:,',I2,');')
      end do
      
      CLOSE(20)

      END SUBROUTINE GenerateMatlab

! End of GenerateMatlab function
! ****************************************************************


! ****************************************************************
!                            
! tag2num - convert equation tags to kpp reaction number
!   Arguments :
!      id        - string with the equation tag
!
! ****************************************************************

ELEMENTAL INTEGER FUNCTION tag2num ( id )

  USE SUAQ_messy_mecca_kpp_Monitor, ONLY: EQN_TAGS

  CHARACTER(LEN=*), INTENT(IN) :: id
  INTEGER i

  tag2num = 0 ! mz_rs_20050115
  DO i = 1, SIZE(EQN_TAGS)
    IF (TRIM(EQN_TAGS(i)) == TRIM(id)) THEN
      tag2num = i ! mz_rs_20050115
      EXIT
    ENDIF
  END DO

END FUNCTION tag2num

! End of tag2num function
! ****************************************************************

! End Utility Functions from KPP_HOME/util/util
! End of UTIL function
! ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


! ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
! 
! Shuffle_user2kpp - function to copy concentrations from USER to KPP
!   Arguments :
!      V_USER    - Concentration of variable species in USER's order
!      V         - Concentrations of variable species (local)
! 
! ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

SUBROUTINE Shuffle_user2kpp ( V_USER, V )

! V_USER - Concentration of variable species in USER's order
  REAL(kind=dp) :: V_USER(NVAR)
! V - Concentrations of variable species (local)
  REAL(kind=dp) :: V(NVAR)

  V(7) = V_USER(3)
  V(3) = V_USER(6)
  V(9) = V_USER(8)
      
END SUBROUTINE Shuffle_user2kpp

! End of Shuffle_user2kpp function
! ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


! ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
! 
! Shuffle_kpp2user - function to restore concentrations from KPP to USER
!   Arguments :
!      V         - Concentrations of variable species (local)
!      V_USER    - Concentration of variable species in USER's order
! 
! ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

SUBROUTINE Shuffle_kpp2user ( V, V_USER )

! V - Concentrations of variable species (local)
  REAL(kind=dp) :: V(NVAR)
! V_USER - Concentration of variable species in USER's order
  REAL(kind=dp) :: V_USER(NVAR)

  V_USER(3) = V(7)
  V_USER(6) = V(3)
  V_USER(8) = V(9)
      
END SUBROUTINE Shuffle_kpp2user

! End of Shuffle_kpp2user function
! ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


! ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
! 
! GetMass - compute total mass of selected atoms
!   Arguments :
!      CL        - Concentration of all species (local)
!      Mass      - value of mass balance
! 
! ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

SUBROUTINE GetMass ( CL, Mass )

! CL - Concentration of all species (local)
  REAL(kind=dp) :: CL(NSPEC)
! Mass - value of mass balance
  REAL(kind=dp) :: Mass(1)

      
END SUBROUTINE GetMass

! End of GetMass function
! ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~



END MODULE SUAQ_messy_mecca_kpp_Util

