Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/psturm/kpp standalone interface #9

Open
wants to merge 16 commits into
base: geos/develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions GeosCore/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ add_library(GeosCore
vdiff_mod.F90
wetscav_mod.F90
YuIMN_Code.F90
kpp_standalone_interface.F90

# Files only included for special cases
$<$<BOOL:${MODEL_CLASSIC}>:flexgrid_read_mod.F90 get_met_mod.F90 set_boundary_conditions_mod.F90>
Expand Down
60 changes: 59 additions & 1 deletion GeosCore/fullchem_mod.F90
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ SUBROUTINE Do_FullChem( Input_Opt, State_Chm, State_Diag, &
USE UCX_MOD, ONLY : SO4_PHOTFRAC
USE UCX_MOD, ONLY : UCX_NOX
USE UCX_MOD, ONLY : UCX_H2SO4PHOT
USE KPP_Standalone_Interface
#ifdef TOMAS
USE TOMAS_MOD, ONLY : H2SO4_RATE
USE TOMAS_MOD, ONLY : PSO4AQ_RATE
Expand Down Expand Up @@ -178,7 +179,7 @@ SUBROUTINE Do_FullChem( Input_Opt, State_Chm, State_Diag, &
INTEGER :: errorCount, origUnit
REAL(fp) :: SO4_FRAC, T, TIN
REAL(fp) :: TOUT, SR, LWC

REAL(dp) :: KPPH_before_integrate
! Strings
CHARACTER(LEN=255) :: errMsg, thisLoc

Expand Down Expand Up @@ -235,6 +236,8 @@ SUBROUTINE Do_FullChem( Input_Opt, State_Chm, State_Diag, &
! (assuming Rosenbrock solver). Define this locally in order to break
! a compile-time dependency. -- Bob Yantosca (05 May 2022)
INTEGER, PARAMETER :: Nhnew = 3
! Add Nhexit, the last timestep length -- Obin Sturm (30 April 2024)
INTEGER, PARAMETER :: Nhexit = 2

! Suppress printing out KPP error messages after this many errors occur
INTEGER, PARAMETER :: INTEGRATE_FAIL_TOGGLE = 20
Expand Down Expand Up @@ -431,6 +434,13 @@ SUBROUTINE Do_FullChem( Input_Opt, State_Chm, State_Diag, &
mapData => NULL()
ENDIF

!=======================================================================
! Should we print the full chemical state for any grid cell on this CPU?
! for use with the KPP Standalone
! (psturm, 03/22/24)
!=======================================================================
CALL Check_Domain( RC )

!========================================================================
! Set up integration convergence conditions and timesteps
! (cf. M. J. Evans)
Expand Down Expand Up @@ -507,6 +517,7 @@ SUBROUTINE Do_FullChem( Input_Opt, State_Chm, State_Diag, &
!$OMP DEFAULT( SHARED )&
!$OMP PRIVATE( I, J, L, N )&
!$OMP PRIVATE( ICNTRL, C_before_integrate )&
!$OMP PRIVATE( KPPH_before_integrate, local_RCONST )&
!$OMP PRIVATE( SO4_FRAC, IERR, RCNTRL, ISTATUS, RSTATE )&
!$OMP PRIVATE( SpcID, KppID, F, P, Vloc )&
!$OMP PRIVATE( Aout, Thread, RC, S, LCH4 )&
Expand Down Expand Up @@ -562,6 +573,12 @@ SUBROUTINE Do_FullChem( Input_Opt, State_Chm, State_Diag, &
! atmosphere if keepActive option is enabled. (hplin, 2/9/22)
CALL fullchem_AR_SetKeepActive( option=.TRUE. )

! Check if the current grid cell in this loop should have its
! full chemical state printed (concentrations, rates, constants)
! for use with the KPP Standalone
! (psturm, 03/22/24)
CALL Check_ActiveCell( I, J, L, State_Grid )

! Start measuring KPP-related routine timing for this grid box
IF ( State_Diag%Archive_KppTime ) THEN
call cpu_time(TimeStart)
Expand Down Expand Up @@ -983,6 +1000,11 @@ SUBROUTINE Do_FullChem( Input_Opt, State_Chm, State_Diag, &
! let us reset concentrations before calling "Integrate" a 2nd time.
C_before_integrate = C

! Do the same for the KPP initial timestep
! Save local rate constants too
KPPH_before_integrate = State_Chm%KPPHvalue(I,J,L)
local_RCONST = RCONST

! Call the Rosenbrock integrator
! (with optional auto-reduce functionality)
CALL Integrate( TIN, TOUT, ICNTRL, &
Expand Down Expand Up @@ -1253,6 +1275,26 @@ SUBROUTINE Do_FullChem( Input_Opt, State_Chm, State_Diag, &
State_Diag%KppTime(I,J,L) = TimeEnd - TimeStart
ENDIF

! Write chemical state to file for the kpp standalone interface
! No external logic needed, this subroutine exits early if the
! chemical state should not be printed (psturm, 03/23/24)
CALL Write_Samples( I, J, L, C_before_integrate, &
local_RCONST, KPPH_before_integrate, &
RSTATE(Nhexit), &
State_Grid, State_Chm, State_Met, &
Input_Opt, ISTATUS(3), RC )

! test the force write option on the root node
! example use case: printing chemical state under conditions
! without knowing where those conditions will happen
! IF ( Input_Opt%amIRoot .AND. L == 1 ) &
! CALL Write_Samples( I, J, L, C_before_integrate, &
! local_RCONST, KPPH_before_integrate, &
! RSTATE(Nhexit), &
! State_Grid, State_Chm, State_Met, &
! Input_Opt, ISTATUS(3), RC, &
! FORCE_WRITE = .TRUE., CELL_NAME = 'root')

!=====================================================================
! Check we have no negative values and copy the concentrations
! calculated from the C array back into State_Chm%Species%Conc
Expand Down Expand Up @@ -2635,6 +2677,7 @@ SUBROUTINE Init_FullChem( Input_Opt, State_Chm, State_Diag, RC )
USE State_Chm_Mod, ONLY : ChmState
USE State_Chm_Mod, ONLY : Ind_
USE State_Diag_Mod, ONLY : DgnState
USE KPP_Standalone_Interface, ONLY : Config_KPP_Standalone
!
! !INPUT PARAMETERS:
!
Expand Down Expand Up @@ -2915,6 +2958,16 @@ SUBROUTINE Init_FullChem( Input_Opt, State_Chm, State_Diag, RC )
ENDIF
ENDIF

!--------------------------------------------------------------------
! Initialize grid cells for input to KPP Standalone (Obin Sturm)
!--------------------------------------------------------------------
CALL Config_KPP_Standalone( Input_Opt, RC )
IF ( RC /= GC_SUCCESS ) THEN
ErrMsg = 'Error encountered in "KPP_Standalone"!'
CALL GC_Error( ErrMsg, RC, ThisLoc )
RETURN
ENDIF

END SUBROUTINE Init_FullChem
!EOC
!------------------------------------------------------------------------------
Expand All @@ -2934,6 +2987,7 @@ SUBROUTINE Cleanup_FullChem( RC )
! !USES:
!
USE ErrCode_Mod
USE KPP_Standalone_Interface, ONLY : Cleanup_KPP_Standalone
!
! !OUTPUT PARAMETERS:
!
Expand Down Expand Up @@ -2983,6 +3037,10 @@ SUBROUTINE Cleanup_FullChem( RC )
IF ( RC /= GC_SUCCESS ) RETURN
ENDIF

! Deallocate variables from kpp standalone module
! psturm, 03/22/2024
CALL Cleanup_KPP_Standalone( RC )

END SUBROUTINE Cleanup_FullChem
!EOC
END MODULE FullChem_Mod
Loading