Skip to content

Commit bb7cfd7

Browse files
authored
Merge pull request #319 from DeniseWorthen/feature/add_restartfh
add restart_fh
2 parents baa3024 + 241dfb3 commit bb7cfd7

File tree

1 file changed

+137
-0
lines changed

1 file changed

+137
-0
lines changed

share/shr_is_restart_fh_mod.F90

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
module shr_is_restart_fh_mod
2+
3+
! Common methods for components to check if it's time to write forecast hour-based restarts
4+
5+
!use dshr_methods_mod , only : chkerr
6+
use ESMF, only : ESMF_ConfigCreate, ESMF_ConfigDestroy, ESMF_ConfigLoadFile, &
7+
ESMF_ConfigGetLen, ESMF_ConfigGetAttribute, ESMF_TimePrint, &
8+
ESMF_LOGMSG_INFO, ESMF_LogWrite, ESMF_TimeInterval, &
9+
ESMF_Time, ESMF_KIND_R8, ESMF_Config, ESMF_Clock, &
10+
ESMF_TimeIntervalSet, ESMF_TimePrint, operator(+), operator(==), &
11+
ESMF_LogFoundError, ESMF_LOGERR_PASSTHRU
12+
13+
implicit none
14+
private
15+
16+
type :: is_restart_fh_type
17+
logical :: write_restartfh = .false.
18+
type(ESMF_Time), allocatable :: restartFhTimes(:)
19+
end type is_restart_fh_type
20+
21+
public :: init_is_restart_fh, is_restart_fh, finalize_restart_fh, is_restart_fh_type
22+
23+
contains
24+
25+
!-----------------------------------------------------------------------
26+
subroutine init_is_restart_fh(currentTime, dtime, lLog, restartfh_info)
27+
!
28+
! !DESCRIPTION:
29+
! Process restart_fh attribute from model_configure in UFS
30+
!
31+
! !USES:
32+
!
33+
! !ARGUMENTS:
34+
type(ESMF_Time), intent(in) :: currentTime
35+
integer, intent(in) :: dtime ! time step (s)
36+
logical, intent(in) :: lLog ! If true, this task logs restart_fh info
37+
type(is_restart_fh_type), intent(out) :: restartfh_info !restart_fh info for each task
38+
!
39+
! !LOCAL VARIABLES:
40+
character(len=256) :: timestr
41+
integer :: n, nfh, fh_s, rc
42+
logical :: isPresent
43+
real(kind=ESMF_KIND_R8), allocatable :: restart_fh(:)
44+
type(ESMF_TimeInterval) :: fhInterval
45+
type(ESMF_Config) :: CF_mc
46+
!-----------------------------------------------------------------------
47+
48+
! set up Times to write non-interval restarts
49+
inquire(FILE='model_configure', EXIST=isPresent)
50+
if (isPresent) then !model_configure exists. this is ufs run
51+
CF_mc = ESMF_ConfigCreate(rc=rc)
52+
call ESMF_ConfigLoadFile(config=CF_mc,filename='model_configure' ,rc=rc)
53+
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, line=__LINE__, file=__FILE__)) return
54+
55+
nfh = ESMF_ConfigGetLen(config=CF_mc, label ='restart_fh:',rc=rc)
56+
if (nfh .gt. 0) then
57+
allocate(restart_fh(1:nfh))
58+
allocate(restartfh_info%restartFhTimes(1:nfh)) !not deallocated here
59+
60+
call ESMF_ConfigGetAttribute(CF_mc,valueList=restart_fh,label='restart_fh:', rc=rc)
61+
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, line=__LINE__, file=__FILE__)) return
62+
! create a list of times at each restart_fh
63+
do n = 1,nfh
64+
fh_s = NINT(3600*restart_fh(n))
65+
call ESMF_TimeIntervalSet(fhInterval, s=fh_s, rc=rc)
66+
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, line=__LINE__, file=__FILE__)) return
67+
restartfh_info%restartFhTimes(n) = currentTime + fhInterval
68+
call ESMF_TimePrint(restartfh_info%restartFhTimes(n), options="string", &
69+
preString="restart_fh at ", unit=timestr, rc=rc)
70+
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, line=__LINE__, file=__FILE__)) return
71+
if (lLog) then
72+
if (mod(fh_s,dtime) /= 0) then
73+
call ESMF_LogWrite('restart time NOT to be written for '//trim(timestr), ESMF_LOGMSG_INFO)
74+
else
75+
call ESMF_LogWrite('restart time to be written for '//trim(timestr), ESMF_LOGMSG_INFO)
76+
end if
77+
end if
78+
end do
79+
deallocate(restart_fh)
80+
end if !nfh>0
81+
call ESMF_ConfigDestroy(CF_mc, rc=rc)
82+
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, line=__LINE__, file=__FILE__)) return
83+
end if !model_configure
84+
85+
end subroutine init_is_restart_fh
86+
87+
subroutine is_restart_fh(clock, restartfh_info, lWrite)
88+
!
89+
! !DESCRIPTION:
90+
! True/false if time to write restart
91+
!
92+
! !USES:
93+
use ESMF, only : ESMF_ClockGetNextTime
94+
95+
!
96+
! !ARGUMENTS:
97+
type(ESMF_Clock), intent(in) :: clock
98+
type(is_restart_fh_type), intent(inout) :: restartfh_info
99+
logical, intent(out) :: lWrite ! time to write?
100+
!
101+
! !LOCAL VARIABLES:
102+
integer :: nfh, rc
103+
type(ESMF_Time) :: nextTime
104+
!-----------------------------------------------------------------------
105+
106+
restartfh_info%write_restartfh = .false.
107+
if (allocated(restartfh_info%restartFhTimes)) then
108+
! check if next time is == to any restartfhtime
109+
do nfh = 1,size(restartfh_info%restartFhTimes)
110+
call ESMF_ClockGetNextTime(clock, nextTime=nexttime, rc=rc)
111+
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, line=__LINE__, file=__FILE__)) return
112+
if (nextTime == restartfh_info%restartFhTimes(nfh)) restartfh_info%write_restartfh = .true.
113+
end do
114+
end if
115+
116+
lWrite = restartfh_info%write_restartfh
117+
118+
end subroutine is_restart_fh
119+
120+
subroutine finalize_restart_fh(restartfh_info)
121+
!
122+
! !DESCRIPTION:
123+
! Clean-up...release allocated memory
124+
!
125+
! !USES:
126+
!
127+
! !ARGUMENTS:
128+
type(is_restart_fh_type), intent(inout) :: restartfh_info
129+
!
130+
! !LOCAL VARIABLES:
131+
!-----------------------------------------------------------------------
132+
133+
if (allocated(restartfh_info%restartFhTimes)) deallocate(restartfh_info%restartFhTimes)
134+
135+
end subroutine finalize_restart_fh
136+
137+
end module shr_is_restart_fh_mod

0 commit comments

Comments
 (0)