@@ -21,7 +21,9 @@ module GEOS_GcmGridCompMod
2121 use GEOS_mkiauGridCompMod, only: AIAU_SetServices = > SetServices
2222 use DFI_GridCompMod, only: ADFI_SetServices = > SetServices
2323 use GEOS_OgcmGridCompMod, only: OGCM_SetServices = > SetServices
24-
24+ use MAPL_HistoryGridCompMod, only: Hist_SetServices = > SetServices
25+ use MAPL_HistoryGridCompMod, only: HISTORY_ExchangeListWrap
26+ use iso_fortran_env
2527
2628 implicit none
2729 private
@@ -50,6 +52,7 @@ module GEOS_GcmGridCompMod
5052integer :: OGCM
5153integer :: AIAU
5254integer :: ADFI
55+ integer :: hist
5356
5457integer :: bypass_ogcm
5558integer :: k
@@ -76,6 +79,8 @@ module GEOS_GcmGridCompMod
7679 logical :: checkpointRequested = .false.
7780 character (len= ESMF_MAXSTR) :: checkpointFilename = ' '
7881 character (len= ESMF_MAXSTR) :: checkpointFileType = ' '
82+ type (ESMF_GridComp) :: history_parent
83+ logical :: run_history = .false.
7984end type T_GCM_STATE
8085
8186! Wrapper for extracting internal state
@@ -613,7 +618,9 @@ subroutine SetServices ( GC, RC )
613618 VERIFY_(STATUS)
614619 ExtDataWrap% ptr = > extdata_internal_state
615620 call ESMF_UserCompSetInternalState( GC, ' ExtData_state' ,ExtDataWrap,status)
616- VERIFY_(STATUS)
621+ VERIFY_(STATUS)
622+
623+ call history_setservice(_RC)
617624 end if
618625
619626
@@ -646,6 +653,37 @@ subroutine SetServices ( GC, RC )
646653
647654 contains
648655
656+ subroutine history_setservice (rc )
657+ integer , intent (out ), optional :: rc
658+
659+ integer :: status
660+ type (ESMF_Config) :: hist_cf, gcm_cf
661+ type (MAPL_MetaComp), pointer :: history_metaobj
662+ type (StubComponent) :: stub_component
663+ integer :: run_dt
664+ character (len= ESMF_MAXSTR) :: replay_history
665+ logical :: is_present
666+
667+ call ESMF_GridCompGet(gc,config= gcm_cf,_RC)
668+ call ESMF_ConfigFindLabel(gcm_cf," REPLAY_HISTORY_RC:" ,isPresent= is_present,_RC)
669+
670+ if (is_present) then
671+ gcm_internal_state% run_history = .true.
672+ call MAPL_GetResource(MAPL,replay_history," REPLAY_HISTORY_RC:" ,_RC)
673+ hist_cf = ESMF_ConfigCreate(_RC)
674+ call ESMF_ConfigLoadFile(hist_cf,trim (replay_history),_RC)
675+ call MAPL_GetResource(MAPL,run_dt," RUN_DT:" ,_RC)
676+ call MAPL_ConfigSetAttribute(hist_cf,value= run_dt,label= " RUN_DT:" ,_RC)
677+ call MAPL_ConfigSetAttribute(hist_cf,value= replay_history,label= " HIST_CF:" ,_RC)
678+ gcm_internal_state% history_parent = ESMF_GridCompCreate(name= " History_GCM_parent" ,config= hist_cf,_RC)
679+ history_metaobj = > null ()
680+ call MAPL_InternalStateCreate(gcm_internal_state% history_parent,history_metaobj,_RC)
681+ call MAPL_Set(history_metaobj,cf= hist_cf,name= " History_GCM_parent" ,component= stub_component,_RC)
682+ hist = MAPL_AddChild(history_metaobj,name= " History_GCM" ,ss= hist_setservices,_RC)
683+ end if
684+ _RETURN(_SUCCESS)
685+ end subroutine history_setservice
686+
649687 subroutine OBIO_TerminateImports (DO_DATAATM , RC )
650688
651689 integer , intent (IN ) :: DO_DATAATM
@@ -1297,12 +1335,52 @@ subroutine Initialize ( GC, IMPORT, EXPORT, CLOCK, RC )
12971335 if ( MAPL_am_I_root() ) call ESMF_StatePrint ( EXPORT, rc= STATUS )
12981336#endif
12991337
1338+ if (gcm_internal_state% rplRegular .and. gcm_internal_state% run_history) then
1339+ call initialize_history(_RC)
1340+ end if
1341+
13001342 call MAPL_TimerOff(MAPL," TOTAL" )
13011343 call MAPL_TimerOff(MAPL," INITIALIZE" )
13021344
13031345 RETURN_(ESMF_SUCCESS)
13041346
13051347 contains
1348+
1349+ subroutine initialize_history (rc )
1350+ integer , optional , intent (Out ) :: rc
1351+
1352+ integer :: status,user_status
1353+ type (ESMF_State), allocatable :: gcm_exports(:),hist_imports(:),hist_exports(:)
1354+ type (ESMF_GridComp), allocatable :: hist_gcs(:)
1355+ type (ESMF_GridComp), allocatable :: gcm_gcs(:)
1356+ type (MAPL_MetaComp), pointer :: history_metaobj
1357+ type (HISTORY_ExchangeListWrap) :: lswrap
1358+ integer (kind= INT64), pointer :: LSADDR(:) = > null ()
1359+
1360+ call MAPL_GetObjectFromGC ( gcm_internal_state% history_parent, history_metaobj, _RC)
1361+
1362+ call MAPL_Get(mapl,childrens_export_states = gcm_exports, childrens_gridcomps = gcm_gcs, _RC)
1363+
1364+ call MAPL_Get(history_metaobj, &
1365+ childrens_export_states = hist_exports, &
1366+ childrens_import_states = hist_imports, &
1367+ childrens_gridcomps = hist_gcs, _RC)
1368+
1369+ allocate (lswrap% ptr, stat = status)
1370+ _VERIFY(STATUS)
1371+ call ESMF_UserCompSetInternalState(hist_gcs(hist), ' MAPL_LocStreamList' , &
1372+ lswrap, _RC)
1373+ call MAPL_GetAllExchangeGrids(gcm_gcs(agcm), LSADDR, _RC)
1374+ lswrap% ptr% LSADDR_PTR = > LSADDR
1375+
1376+ call ESMF_StateAdd(hist_imports(hist),[gcm_exports(agcm)],_RC)
1377+ call ESMF_GridCompInitialize(hist_gcs(hist),importState= hist_imports(hist),&
1378+ exportState= hist_exports(hist),&
1379+ clock= clock,userRC= user_status,_RC)
1380+
1381+ _RETURN(_SUCCESS)
1382+ end subroutine initialize_history
1383+
13061384 subroutine AllocateExports (STATE , NAMES , RC )
13071385 type (ESMF_State) , intent (INOUT ) :: STATE
13081386 character (len=* ) , intent (IN ) :: NAMES(:)
@@ -1701,6 +1779,10 @@ subroutine Run ( GC, IMPORT, EXPORT, CLOCK, RC )
17011779 call ESMF_VMBarrier(VM, rc= status)
17021780 VERIFY_(STATUS)
17031781
1782+ if (gcm_internal_state% run_history) then
1783+ call run_history(_RC)
1784+ end if
1785+
17041786 DONE = ESMF_AlarmIsRinging(GCM_INTERNAL_STATE% replayStopAlarm, RC= STATUS)
17051787 VERIFY_(STATUS)
17061788 if ( DONE ) exit
@@ -1826,6 +1908,29 @@ subroutine Run ( GC, IMPORT, EXPORT, CLOCK, RC )
18261908
18271909 RETURN_(ESMF_SUCCESS)
18281910 contains
1911+
1912+ subroutine run_history (rc )
1913+ integer , optional , intent (out ) :: rc
1914+
1915+ integer :: user_status,status
1916+ type (ESMF_State), allocatable :: gcm_exports(:),hist_imports(:),hist_exports(:)
1917+ type (ESMF_GridComp), allocatable :: hist_gcs(:)
1918+ type (MAPL_MetaComp), pointer :: history_metaobj
1919+
1920+ call MAPL_GetObjectFromGC ( gcm_internal_state% history_parent, history_metaobj, _RC)
1921+
1922+ call MAPL_Get(history_metaobj, &
1923+ childrens_export_states = hist_exports, &
1924+ childrens_import_states = hist_imports, &
1925+ childrens_gridcomps = hist_gcs, _RC)
1926+
1927+ call ESMF_GridCompRun(hist_gcs(hist),importState= hist_imports(hist),&
1928+ exportState= hist_exports(hist),&
1929+ clock= clock,userRC= user_status,_RC)
1930+ _RETURN(_SUCCESS)
1931+
1932+ end subroutine run_history
1933+
18291934 subroutine RUN_OCEAN (phase , rc )
18301935 integer , optional , intent (IN ) :: phase
18311936 integer , optional , intent (OUT ) :: rc
0 commit comments