Skip to content

Commit 8cc8b70

Browse files
Flexible restart write times (restart_fh) (NOAA-EMC#139)
Modify mom_cap.F90 so that restart writes can be triggered at any forecast time via UFS configuration as for other components
1 parent 5e0c21f commit 8cc8b70

File tree

1 file changed

+21
-68
lines changed

1 file changed

+21
-68
lines changed

config_src/drivers/nuopc_cap/mom_cap.F90

+21-68
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,10 @@ module MOM_cap_mod
9191
use NUOPC_Model, only: model_label_Finalize => label_Finalize
9292
use NUOPC_Model, only: SetVM
9393

94+
#ifndef CESMCOUPLED
95+
use shr_is_restart_fh_mod, only : init_is_restart_fh, is_restart_fh, is_restart_fh_type
96+
#endif
97+
9498
implicit none; private
9599

96100
public SetServices
@@ -150,13 +154,12 @@ module MOM_cap_mod
150154
#else
151155
logical :: cesm_coupled = .false.
152156
type(ESMF_GeomType_Flag) :: geomtype
157+
type(is_restart_fh_type) :: restartfh_info ! For flexible restarts in UFS
153158
#endif
154159
character(len=8) :: restart_mode = 'alarms'
155160
character(len=16) :: inst_suffix = ''
156161
real(8) :: timere
157162

158-
type(ESMF_Time), allocatable :: restartFhTimes(:)
159-
160163
contains
161164

162165
!> NUOPC SetService method is the only public entry point.
@@ -625,7 +628,7 @@ subroutine InitializeAdvertise(gcomp, importState, exportState, clock, rc)
625628
open(newunit=readunit, file=rpointer_filename, form='formatted', status='old', iostat=iostat)
626629
if (iostat /= 0) then
627630
call ESMF_LogSetError(ESMF_RC_FILE_OPEN, msg=subname//' ERROR opening '//rpointer_filename, &
628-
line=__LINE__, file=u_FILE_u, rcToReturn=rc)
631+
line=__LINE__, file=u_FILE_u, rcToReturn=rc)
629632
return
630633
endif
631634
do
@@ -1643,11 +1646,9 @@ subroutine ModelAdvance(gcomp, rc)
16431646
character(len=:), allocatable :: rpointer_filename
16441647
integer :: num_rest_files
16451648
real(8) :: MPI_Wtime, timers
1646-
logical :: write_restart
1647-
logical :: write_restartfh
1649+
logical :: write_restart, write_restartfh
16481650
logical :: write_restart_eor
16491651

1650-
16511652
rc = ESMF_SUCCESS
16521653
if(profile_memory) call ESMF_VMLogMemInfo("Entering MOM Model_ADVANCE: ")
16531654
if(write_runtimelog) then
@@ -1797,16 +1798,6 @@ subroutine ModelAdvance(gcomp, rc)
17971798
call ESMF_ClockGetAlarm(clock, alarmname='restart_alarm', alarm=restart_alarm, rc=rc)
17981799
if (ChkErr(rc,__LINE__,u_FILE_u)) return
17991800

1800-
write_restartfh = .false.
1801-
! check if next time is == to any restartfhtime
1802-
if (allocated(RestartFhTimes)) then
1803-
do n = 1,size(RestartFhTimes)
1804-
call ESMF_ClockGetNextTime(clock, MyTime, rc=rc)
1805-
if (ChkErr(rc,__LINE__,u_FILE_u)) return
1806-
if (MyTime == RestartFhTimes(n)) write_restartfh = .true.
1807-
end do
1808-
end if
1809-
18101801
write_restart = .false.
18111802
if (ESMF_AlarmIsRinging(restart_alarm, rc=rc)) then
18121803
if (ChkErr(rc,__LINE__,u_FILE_u)) return
@@ -1827,7 +1818,12 @@ subroutine ModelAdvance(gcomp, rc)
18271818
end if
18281819
end if
18291820

1830-
if (write_restart .or. write_restartfh .or. write_restart_eor) then
1821+
#ifndef CESMCOUPLED
1822+
call is_restart_fh(clock, restartfh_info, write_restartfh)
1823+
if (write_restartfh) write_restart = .true.
1824+
#endif
1825+
1826+
if (write_restart .or. write_restart_eor) then
18311827
! determine restart filename
18321828
call ESMF_ClockGetNextTime(clock, MyTime, rc=rc)
18331829
if (ChkErr(rc,__LINE__,u_FILE_u)) return
@@ -1850,7 +1846,7 @@ subroutine ModelAdvance(gcomp, rc)
18501846
! write restart file(s)
18511847
call ocean_model_restart(ocean_state, restartname=restartname, num_rest_files=num_rest_files)
18521848
if (localPet == 0) then
1853-
! Write name of restart file in the rpointer file - this is currently hard-coded for the ocean
1849+
! Write name of restart file in the rpointer file - this is currently hard-coded for the ocean
18541850
open(newunit=writeunit, file=rpointer_filename, form='formatted', status='unknown', iostat=iostat)
18551851
if (iostat /= 0) then
18561852
call ESMF_LogSetError(ESMF_RC_FILE_OPEN, &
@@ -1927,34 +1923,26 @@ end subroutine ModelAdvance
19271923

19281924

19291925
subroutine ModelSetRunClock(gcomp, rc)
1930-
1931-
use ESMF, only : ESMF_TimeIntervalSet
1932-
19331926
type(ESMF_GridComp) :: gcomp
19341927
integer, intent(out) :: rc
19351928

19361929
! local variables
1937-
type(ESMF_VM) :: vm
19381930
type(ESMF_Clock) :: mclock, dclock
19391931
type(ESMF_Time) :: mcurrtime, dcurrtime
19401932
type(ESMF_Time) :: mstoptime, dstoptime
19411933
type(ESMF_TimeInterval) :: mtimestep, dtimestep
1942-
type(ESMF_TimeInterval) :: fhInterval
19431934
character(len=128) :: mtimestring, dtimestring
1944-
character(len=256) :: timestr
19451935
character(len=256) :: cvalue
19461936
character(len=256) :: restart_option ! Restart option units
19471937
integer :: restart_n ! Number until restart interval
19481938
integer :: restart_ymd ! Restart date (YYYYMMDD)
1949-
integer :: dt_cpl ! coupling timestep
1939+
integer :: dt_cpl
19501940
type(ESMF_Alarm) :: restart_alarm
19511941
type(ESMF_Alarm) :: stop_alarm
19521942
logical :: isPresent, isSet
19531943
logical :: first_time = .true.
1954-
integer :: localPet
1955-
integer :: n, nfh
1956-
integer, allocatable :: restart_fh(:)
1957-
character(len=*),parameter :: subname='(MOM_cap:ModelSetRunClock) '
1944+
character(len=*),parameter :: subname='MOM_cap:(ModelSetRunClock) '
1945+
character(len=256) :: timestr
19581946
!--------------------------------
19591947

19601948
rc = ESMF_SUCCESS
@@ -1970,11 +1958,6 @@ subroutine ModelSetRunClock(gcomp, rc)
19701958
call ESMF_ClockGet(mclock, currTime=mcurrtime, timeStep=mtimestep, rc=rc)
19711959
if (ChkErr(rc,__LINE__,u_FILE_u)) return
19721960

1973-
call ESMF_GridCompGet(gcomp, vm=vm, rc=rc)
1974-
if (ChkErr(rc,__LINE__,u_FILE_u)) return
1975-
call ESMF_VMGet(vm, localPet=localPet, rc=rc)
1976-
if (ChkErr(rc,__LINE__,u_FILE_u)) return
1977-
19781961
!--------------------------------
19791962
! check that the current time in the model and driver are the same
19801963
!--------------------------------
@@ -2073,6 +2056,9 @@ subroutine ModelSetRunClock(gcomp, rc)
20732056
call ESMF_LogWrite(subname//" Restarts will be written at finalize only", ESMF_LOGMSG_INFO)
20742057
endif
20752058
endif
2059+
call ESMF_TimeIntervalGet(dtimestep, s=dt_cpl, rc=rc)
2060+
if (ChkErr(rc,__LINE__,u_FILE_u)) return
2061+
call init_is_restart_fh(mcurrTime, dt_cpl, is_root_pe(), restartfh_info)
20762062
endif
20772063

20782064
if (restart_mode == 'alarms') then
@@ -2098,41 +2084,8 @@ subroutine ModelSetRunClock(gcomp, rc)
20982084
call ESMF_TimeGet(dstoptime, timestring=timestr, rc=rc)
20992085
call ESMF_LogWrite("Stop Alarm will ring at : "//trim(timestr), ESMF_LOGMSG_INFO)
21002086

2101-
! set up Times to write non-interval restarts
2102-
call NUOPC_CompAttributeGet(gcomp, name='restart_fh', isPresent=isPresent, isSet=isSet, rc=rc)
2103-
if (ChkErr(rc,__LINE__,u_FILE_u)) return
2104-
if (isPresent .and. isSet) then
2105-
2106-
call ESMF_TimeIntervalGet(dtimestep, s=dt_cpl, rc=rc)
2107-
if (ChkErr(rc,__LINE__,u_FILE_u)) return
2108-
call NUOPC_CompAttributeGet(gcomp, name='restart_fh', value=cvalue, rc=rc)
2109-
if (ChkErr(rc,__LINE__,u_FILE_u)) return
2110-
2111-
! convert string to a list of integer restart_fh values
2112-
nfh = 1 + count(transfer(trim(cvalue), 'a', len(cvalue)) == ",")
2113-
allocate(restart_fh(1:nfh))
2114-
allocate(restartFhTimes(1:nfh))
2115-
read(cvalue,*)restart_fh(1:nfh)
2116-
2117-
! create a list of times at each restart_fh
2118-
do n = 1,nfh
2119-
call ESMF_TimeIntervalSet(fhInterval, h=restart_fh(n), rc=rc)
2120-
if (ChkErr(rc,__LINE__,u_FILE_u)) return
2121-
restartFhTimes(n) = mcurrtime + fhInterval
2122-
call ESMF_TimePrint(restartFhTimes(n), options="string", preString="Restart_Fh at ", unit=timestr, rc=rc)
2123-
if (ChkErr(rc,__LINE__,u_FILE_u)) return
2124-
if (localPet == 0) then
2125-
if (mod(3600*restart_fh(n),dt_cpl) /= 0) then
2126-
write(stdout,'(A)')trim(subname)//trim(timestr)//' will not be written'
2127-
else
2128-
write(stdout,'(A)')trim(subname)//trim(timestr)//' will be written'
2129-
end if
2130-
end if
2131-
end do
2132-
deallocate(restart_fh)
2133-
end if
2134-
21352087
first_time = .false.
2088+
21362089
endif
21372090

21382091
!--------------------------------

0 commit comments

Comments
 (0)