Skip to content

Commit 26ac406

Browse files
authored
Merge pull request #1687 from jiandewang/DEV-EMC-candidate-20260128
dev/emc candidate 20260128
2 parents f5ae5cf + 0141ab6 commit 26ac406

5 files changed

Lines changed: 993 additions & 34 deletions

File tree

config_src/drivers/nuopc_cap/mom_cap.F90

Lines changed: 95 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,13 @@ module MOM_cap_mod
3131
use MOM_ensemble_manager, only: ensemble_manager_init
3232
use MOM_coms, only: sum_across_PEs
3333

34+
! stub routines for CESMCOUPLED
35+
use mom_cap_outputlog, only: outputlog_init, outputlog_run, outputlog_restart
3436
#ifdef CESMCOUPLED
3537
use shr_log_mod, only: shr_log_setLogUnit
3638
use nuopc_shr_methods, only: get_component_instance
3739
#endif
38-
use time_utils_mod, only: esmf2fms_time
40+
use time_utils_mod, only: esmf2fms_time
3941

4042
use, intrinsic :: iso_fortran_env, only: output_unit
4143

@@ -91,9 +93,11 @@ module MOM_cap_mod
9193
use NUOPC_Model, only: model_label_Finalize => label_Finalize
9294
use NUOPC_Model, only: SetVM
9395

96+
use mom_inline_mod, only : mom_inline_init, mom_inline_run
9497
#ifndef CESMCOUPLED
95-
use shr_is_restart_fh_mod, only : init_is_restart_fh, is_restart_fh, is_restart_fh_type
98+
use shr_is_restart_fh_mod, only : init_is_restart_fh, is_restart_fh, is_restart_fh_type
9699
#endif
100+
use mom_cap_profiling, only: cap_profiling_init, cap_profiling
97101

98102
implicit none; private
99103

@@ -142,6 +146,7 @@ module MOM_cap_mod
142146
logical :: use_mommesh = .true.
143147
logical :: set_missing_stks_to_zero = .false.
144148
logical :: restart_eor = .false.
149+
logical :: use_cdeps_inline = .false.
145150
character(len=128) :: scalar_field_name = ''
146151
integer :: scalar_field_count = 0
147152
integer :: scalar_field_idx_grid_nx = 0
@@ -161,6 +166,7 @@ module MOM_cap_mod
161166
character(len=16) :: inst_suffix = ''
162167
logical :: pointer_date = .true. ! append date to rpointer
163168
real(8) :: timere
169+
integer :: localPet = -1
164170

165171
contains
166172

@@ -178,8 +184,18 @@ subroutine SetServices(gcomp, rc)
178184
! local variables
179185
character(len=*),parameter :: subname='(MOM_cap:SetServices)'
180186

187+
type(ESMF_VM) :: vm
188+
181189
rc = ESMF_SUCCESS
182190

191+
call ESMF_GridCompGet(gcomp, vm=vm, rc=rc)
192+
if (ChkErr(rc,__LINE__,u_FILE_u)) return
193+
call ESMF_VMGet(vm, localpet=localPet, rc=rc)
194+
if (ChkErr(rc,__LINE__,u_FILE_u)) return
195+
196+
if (localPet == 0) call cap_profiling_init()
197+
if (localPet == 0) call cap_profiling("mom", "SetServices", "B")
198+
183199
! the NUOPC model component will register the generic methods
184200
call NUOPC_CompDerive(gcomp, model_routine_SS, rc=rc)
185201
if (ChkErr(rc,__LINE__,u_FILE_u)) return
@@ -219,6 +235,8 @@ subroutine SetServices(gcomp, rc)
219235
specRoutine=ocean_model_finalize, rc=rc)
220236
if (ChkErr(rc,__LINE__,u_FILE_u)) return
221237

238+
if (localPet == 0) call cap_profiling("mom", "SetServices", "E")
239+
222240
end subroutine SetServices
223241

224242
!> First initialize subroutine called by NUOPC. The purpose
@@ -245,10 +263,11 @@ subroutine InitializeP0(gcomp, importState, exportState, clock, rc)
245263
character(len=64) :: value, logmsg
246264
character(len=*),parameter :: subname='(MOM_cap:InitializeP0)'
247265
type(ESMF_VM) :: vm
248-
integer :: mype
249266

250267
rc = ESMF_SUCCESS
251268

269+
if (localPet == 0) call cap_profiling("mom", "InitializeP0", "B")
270+
252271
! Switch to IPDv03 by filtering all other phaseMap entries
253272
call NUOPC_CompFilterPhaseMap(gcomp, ESMF_METHOD_INITIALIZE, &
254273
acceptStringList=(/"IPDv03p"/), rc=rc)
@@ -395,6 +414,13 @@ subroutine InitializeP0(gcomp, importState, exportState, clock, rc)
395414
geomtype = ESMF_GEOMTYPE_GRID
396415
endif
397416

417+
call NUOPC_CompAttributeGet(gcomp, name="use_cdeps_inline", value=value, &
418+
isPresent=isPresent, isSet=isSet, rc=rc)
419+
if (ChkErr(rc,__LINE__,u_FILE_u)) return
420+
if (isPresent .and. isSet) use_cdeps_inline=(trim(value)=="true")
421+
write(logmsg,*) use_cdeps_inline
422+
call ESMF_LogWrite('MOM_cap:use_cdeps_inline = '//trim(logmsg), ESMF_LOGMSG_INFO)
423+
398424
! Read end of run restart config option
399425
call NUOPC_CompAttributeGet(gcomp, name="write_restart_at_endofrun", value=value, &
400426
isPresent=isPresent, isSet=isSet, rc=rc)
@@ -403,6 +429,8 @@ subroutine InitializeP0(gcomp, importState, exportState, clock, rc)
403429
if (trim(value) .eq. '.true.') restart_eor = .true.
404430
end if
405431

432+
if (localPet == 0) call cap_profiling("mom", "InitializeP0", "E")
433+
406434
end subroutine
407435

408436
!> Called by NUOPC to advertise import and export fields. "Advertise"
@@ -454,7 +482,6 @@ subroutine InitializeAdvertise(gcomp, importState, exportState, clock, rc)
454482
character(len=40) :: wave_method ! Wave coupling method.
455483
logical :: use_MARBL ! If true, MARBL tracers are being used.
456484
integer :: userRc
457-
integer :: localPet
458485
integer :: localPeCount
459486
integer :: iostat
460487
integer :: readunit
@@ -472,6 +499,9 @@ subroutine InitializeAdvertise(gcomp, importState, exportState, clock, rc)
472499
!--------------------------------
473500

474501
rc = ESMF_SUCCESS
502+
503+
if (localPet == 0) call cap_profiling("mom", "InitializeAdvertise", "B")
504+
475505
if(write_runtimelog) timeiads = MPI_Wtime()
476506

477507
call ESMF_LogWrite(subname//' enter', ESMF_LOGMSG_INFO)
@@ -487,7 +517,7 @@ subroutine InitializeAdvertise(gcomp, importState, exportState, clock, rc)
487517
call ESMF_VMGetCurrent(vm, rc=rc)
488518
if (ChkErr(rc,__LINE__,u_FILE_u)) return
489519

490-
call ESMF_VMGet(VM, mpiCommunicator=mpi_comm_mom, localPet=localPet, rc=rc)
520+
call ESMF_VMGet(VM, mpiCommunicator=mpi_comm_mom, rc=rc)
491521
if (ChkErr(rc,__LINE__,u_FILE_u)) return
492522

493523
call ESMF_ClockGet(CLOCK, currTIME=MyTime, TimeStep=TINT, RC=rc)
@@ -512,7 +542,7 @@ subroutine InitializeAdvertise(gcomp, importState, exportState, clock, rc)
512542

513543
rpointer_filename = 'rpointer.ocn'//trim(inst_suffix)
514544
if (pointer_date) then
515-
write(timestamp,'(".",i4.4,"-",i2.2,"-",i2.2,"-",i5.5)'),year,month,day,hour*3600+minute*60+second
545+
write(timestamp,'(".",i4.4,"-",i2.2,"-",i2.2,"-",i5.5)')year,month,day,hour*3600+minute*60+second
516546
inquire(file=trim(rpointer_filename//timestamp), exist=found)
517547
! for backward compatibility
518548
if (found) then
@@ -677,8 +707,6 @@ subroutine InitializeAdvertise(gcomp, importState, exportState, clock, rc)
677707
call ESMF_LogWrite('MOM_cap: restart requested, using '//trim(rpointer_filename), ESMF_LOGMSG_WARNING)
678708
call ESMF_GridCompGet(gcomp, vm=vm, rc=rc)
679709
if (ChkErr(rc,__LINE__,u_FILE_u)) return
680-
call ESMF_VMGet(vm, localPet=localPet, rc=rc)
681-
if (ChkErr(rc,__LINE__,u_FILE_u)) return
682710

683711
if (localPet == 0) then
684712
open(newunit=readunit, file=rpointer_filename, form='formatted', status='old', iostat=iostat)
@@ -915,6 +943,8 @@ subroutine InitializeAdvertise(gcomp, importState, exportState, clock, rc)
915943
enddo
916944
if(write_runtimelog .and. is_root_pe()) write(stdout,*) 'In ',trim(subname),' time ', MPI_Wtime()-timeiads
917945

946+
if (localPet == 0) call cap_profiling("mom", "InitializeAdvertise", "E")
947+
918948
end subroutine InitializeAdvertise
919949

920950
!> Called by NUOPC to realize import and export fields. "Realizing" a field
@@ -969,7 +999,6 @@ subroutine InitializeRealize(gcomp, importState, exportState, clock, rc)
969999
real(ESMF_KIND_R8), pointer :: dataPtr_xcor(:,:)
9701000
real(ESMF_KIND_R8), pointer :: dataPtr_ycor(:,:)
9711001
integer :: mpicom
972-
integer :: localPet
9731002
integer :: localPeCount
9741003
integer :: lsize
9751004
integer :: ig,jg, ni,nj,k
@@ -1008,6 +1037,9 @@ subroutine InitializeRealize(gcomp, importState, exportState, clock, rc)
10081037
!--------------------------------
10091038

10101039
rc = ESMF_SUCCESS
1040+
1041+
if (localPet == 0) call cap_profiling("mom", "InitializeRealize", "B")
1042+
10111043
if(write_runtimelog) timeirls = MPI_Wtime()
10121044

10131045
call shr_log_setLogUnit (stdout)
@@ -1030,7 +1062,7 @@ subroutine InitializeRealize(gcomp, importState, exportState, clock, rc)
10301062
call ESMF_VMGetCurrent(vm, rc=rc)
10311063
if (ChkErr(rc,__LINE__,u_FILE_u)) return
10321064

1033-
call ESMF_VMGet(vm, petCount=npet, mpiCommunicator=mpicom, localPet=localPet, rc=rc)
1065+
call ESMF_VMGet(vm, petCount=npet, mpiCommunicator=mpicom, rc=rc)
10341066
if (ChkErr(rc,__LINE__,u_FILE_u)) return
10351067

10361068
!---------------------------------
@@ -1582,6 +1614,11 @@ subroutine InitializeRealize(gcomp, importState, exportState, clock, rc)
15821614
!---------------------------------
15831615
call mom_set_geomtype(geomtype)
15841616

1617+
if (use_cdeps_inline) then
1618+
call mom_inline_init(gcomp, clock, eMesh, localPet, rc=rc)
1619+
if (ChkErr(rc,__LINE__,u_FILE_u)) return
1620+
end if
1621+
15851622
!---------------------------------
15861623
! write out diagnostics
15871624
!---------------------------------
@@ -1593,6 +1630,8 @@ subroutine InitializeRealize(gcomp, importState, exportState, clock, rc)
15931630
timere = 0.
15941631
if(write_runtimelog .and. is_root_pe()) write(stdout,*) 'In ',trim(subname),' time ', MPI_Wtime()-timeirls
15951632

1633+
if (localPet == 0) call cap_profiling("mom", "InitializeRealize", "E")
1634+
15961635
end subroutine InitializeRealize
15971636

15981637
!> TODO
@@ -1624,6 +1663,8 @@ subroutine DataInitialize(gcomp, rc)
16241663
real(8) :: MPI_Wtime, timedis
16251664
!--------------------------------
16261665

1666+
if (localPet == 0) call cap_profiling("mom", "DataInitialize", "B")
1667+
16271668
if(write_runtimelog) timedis = MPI_Wtime()
16281669

16291670
! query the Component for its clock, importState and exportState
@@ -1688,6 +1729,8 @@ subroutine DataInitialize(gcomp, rc)
16881729

16891730
if(write_runtimelog .and. is_root_pe()) write(stdout,*) 'In ',trim(subname),' time ', MPI_Wtime()-timedis
16901731

1732+
if (localPet == 0) call cap_profiling("mom", "DataInitialize", "E")
1733+
16911734
end subroutine DataInitialize
16921735

16931736
!> Called by NUOPC to advance the model a single timestep.
@@ -1729,7 +1772,6 @@ subroutine ModelAdvance(gcomp, rc)
17291772
character(ESMF_MAXSTR) :: casename
17301773
integer :: iostat
17311774
integer :: writeunit
1732-
integer :: localPet
17331775
type(ESMF_VM) :: vm
17341776
integer :: n, i
17351777
character(240) :: import_timestr, export_timestr
@@ -1744,6 +1786,9 @@ subroutine ModelAdvance(gcomp, rc)
17441786
logical :: write_restart_eor
17451787

17461788
rc = ESMF_SUCCESS
1789+
1790+
if (localPet == 0) call cap_profiling("mom", "ModelAdvance", "B")
1791+
17471792
if(profile_memory) call ESMF_VMLogMemInfo("Entering MOM Model_ADVANCE: ")
17481793
if(write_runtimelog) then
17491794
timers = MPI_Wtime()
@@ -1774,7 +1819,9 @@ subroutine ModelAdvance(gcomp, rc)
17741819
call ESMF_LogWrite(trim(msgString), ESMF_LOGMSG_INFO)
17751820

17761821
call ESMF_TimeGet(currTime, timestring=import_timestr, rc=rc)
1822+
if (ChkErr(rc,__LINE__,u_FILE_u)) return
17771823
call ESMF_TimeGet(currTime+timestep, timestring=export_timestr, rc=rc)
1824+
if (ChkErr(rc,__LINE__,u_FILE_u)) return
17781825

17791826
Time_step_coupled = esmf2fms_time(timeStep)
17801827
Time = esmf2fms_time(currTime)
@@ -1857,6 +1904,11 @@ subroutine ModelAdvance(gcomp, rc)
18571904
set_missing_stks_to_zero, rc=rc)
18581905
if (ChkErr(rc,__LINE__,u_FILE_u)) return
18591906

1907+
if (use_cdeps_inline) then
1908+
call mom_inline_run(clock, ocean_public, ocean_grid, ice_ocean_boundary, dbug, rc=rc)
1909+
if (ChkErr(rc,__LINE__,u_FILE_u)) return
1910+
end if
1911+
18601912
!---------------
18611913
! Update MOM6
18621914
!---------------
@@ -1877,7 +1929,7 @@ subroutine ModelAdvance(gcomp, rc)
18771929
call state_diagnose(exportState,subname//':ES ',rc=rc)
18781930
if (ChkErr(rc,__LINE__,u_FILE_u)) return
18791931
endif
1880-
endif
1932+
endif ! do_advance
18811933

18821934
!---------------
18831935
! Get the stop alarm
@@ -1905,12 +1957,12 @@ subroutine ModelAdvance(gcomp, rc)
19051957
write_restart_eor = .false.
19061958
if (restart_eor) then
19071959
if (ESMF_AlarmIsRinging(stop_alarm, rc=rc)) then
1908-
if (ChkErr(rc,__LINE__,u_FILE_u)) return
1909-
write_restart_eor = .true.
1910-
! turn off the alarm
1911-
call ESMF_AlarmRingerOff(stop_alarm, rc=rc )
1912-
if (ChkErr(rc,__LINE__,u_FILE_u)) return
1913-
end if
1960+
if (ChkErr(rc,__LINE__,u_FILE_u)) return
1961+
write_restart_eor = .true.
1962+
! turn off the alarm
1963+
call ESMF_AlarmRingerOff(stop_alarm, rc=rc )
1964+
if (ChkErr(rc,__LINE__,u_FILE_u)) return
1965+
end if
19141966
end if
19151967

19161968
#ifndef CESMCOUPLED
@@ -1930,10 +1982,8 @@ subroutine ModelAdvance(gcomp, rc)
19301982
if (ChkErr(rc,__LINE__,u_FILE_u)) return
19311983
call ESMF_GridCompGet(gcomp, vm=vm, rc=rc)
19321984
if (ChkErr(rc,__LINE__,u_FILE_u)) return
1933-
call ESMF_VMGet(vm, localPet=localPet, rc=rc)
1934-
if (ChkErr(rc,__LINE__,u_FILE_u)) return
19351985

1936-
write(timestamp,'(".",i4.4,"-",i2.2,"-",i2.2,"-",i5.5)'),year,month,day,hour*3600+minute*60+seconds
1986+
write(timestamp,'(".",i4.4,"-",i2.2,"-",i2.2,"-",i5.5)')year,month,day,hour*3600+minute*60+seconds
19371987

19381988
rpointer_filename = 'rpointer.ocn'//trim(inst_suffix)
19391989
if (pointer_date) then
@@ -1981,8 +2031,10 @@ subroutine ModelAdvance(gcomp, rc)
19812031

19822032
! write restart file(s)
19832033
call ocean_model_restart(ocean_state, restartname=restartname, &
1984-
stoch_restartname=stoch_restartname)
2034+
stoch_restartname=stoch_restartname, num_rest_files=num_rest_files)
19852035

2036+
call outputlog_restart(clock, num_rest_files, rc=rc)
2037+
if (ChkErr(rc,__LINE__,u_FILE_u)) return
19862038
endif
19872039

19882040
if (is_root_pe()) then
@@ -1991,6 +2043,9 @@ subroutine ModelAdvance(gcomp, rc)
19912043
endif
19922044
endif ! restart_mode
19932045

2046+
call outputlog_run(clock, rc=rc)
2047+
if (ChkErr(rc,__LINE__,u_FILE_u)) return
2048+
19942049
!---------------
19952050
! Write diagnostics
19962051
!---------------
@@ -2019,6 +2074,8 @@ subroutine ModelAdvance(gcomp, rc)
20192074

20202075
if(profile_memory) call ESMF_VMLogMemInfo("Leaving MOM Model_ADVANCE: ")
20212076

2077+
if (localPet == 0) call cap_profiling("mom", "ModelAdvance", "E")
2078+
20222079
end subroutine ModelAdvance
20232080

20242081

@@ -2047,6 +2104,8 @@ subroutine ModelSetRunClock(gcomp, rc)
20472104

20482105
rc = ESMF_SUCCESS
20492106

2107+
if (localPet == 0) call cap_profiling("mom", "ModelSetRunClock", "B")
2108+
20502109
! query the Component for its clock, importState and exportState
20512110
call NUOPC_ModelGet(gcomp, driverClock=dclock, modelClock=mclock, rc=rc)
20522111
if (ChkErr(rc,__LINE__,u_FILE_u)) return
@@ -2186,6 +2245,8 @@ subroutine ModelSetRunClock(gcomp, rc)
21862245
call ESMF_TimeGet(dstoptime, timestring=timestr, rc=rc)
21872246
call ESMF_LogWrite("Stop Alarm will ring at : "//trim(timestr), ESMF_LOGMSG_INFO)
21882247

2248+
call outputlog_init(gcomp, mclock, rc)
2249+
if (ChkErr(rc,__LINE__,u_FILE_u)) return
21892250
first_time = .false.
21902251

21912252
endif
@@ -2200,6 +2261,8 @@ subroutine ModelSetRunClock(gcomp, rc)
22002261
call ESMF_ClockSet(mclock, currTime=dcurrtime, timeStep=dtimestep, stopTime=mstoptime, rc=rc)
22012262
if (ChkErr(rc,__LINE__,u_FILE_u)) return
22022263

2264+
if (localPet == 0) call cap_profiling("mom", "ModelSetRunClock", "E")
2265+
22032266
end subroutine ModelSetRunClock
22042267

22052268
!===============================================================================
@@ -2226,6 +2289,8 @@ subroutine ocean_model_finalize(gcomp, rc)
22262289
character(len=*),parameter :: subname='(MOM_cap:ocean_model_finalize)'
22272290
real(8) :: MPI_Wtime, timefs
22282291

2292+
if (localPet == 0) call cap_profiling("mom", "ocean_model_finalize", "B")
2293+
22292294
if (is_root_pe()) then
22302295
write(stdout,*) 'MOM: --- finalize called ---'
22312296
endif
@@ -2259,8 +2324,16 @@ subroutine ocean_model_finalize(gcomp, rc)
22592324
call io_infra_end()
22602325
call MOM_infra_end()
22612326

2327+
! need to call twice to force logging of last output file
2328+
call outputlog_run(clock, rc=rc)
2329+
if (ChkErr(rc,__LINE__,u_FILE_u)) return
2330+
call outputlog_run(clock, .true., rc=rc)
2331+
if (ChkErr(rc,__LINE__,u_FILE_u)) return
2332+
22622333
if(write_runtimelog .and. is_root_pe()) write(stdout,*) 'In ',trim(subname),' time ', MPI_Wtime()-timefs
22632334

2335+
if (localPet == 0) call cap_profiling("mom", "ocean_model_finalize", "E")
2336+
22642337
end subroutine ocean_model_finalize
22652338

22662339

0 commit comments

Comments
 (0)