-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathMAPL_CapGridComp.F90
2162 lines (1708 loc) · 73.8 KB
/
MAPL_CapGridComp.F90
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "MAPL_Generic.h"
#include "unused_dummy.H"
module MAPL_CapGridCompMod
use ESMF
use MAPL_ExceptionHandling
use MAPL_BaseMod
use MAPL_Constants
use MAPL_Profiler, only: BaseProfiler, get_global_time_profiler, get_global_memory_profiler
use MAPL_ProfMod
use MAPL_MemUtilsMod
use MAPL_IOMod
use MAPL_CommsMod
use MAPL_GenericMod
use MAPL_LocStreamMod
use ESMFL_Mod
use MAPL_ShmemMod
use MAPL_HistoryGridCompMod, only : Hist_SetServices => SetServices
use MAPL_HistoryGridCompMod, only : HISTORY_ExchangeListWrap
#if defined(BUILD_WITH_EXTDATA2G)
use MAPL_ExtDataGridComp2G, only : ExtData2G_SetServices => SetServices
#endif
use MAPL_ExtDataGridCompMod, only : ExtData1G_SetServices => SetServices
use MAPL_ConfigMod
use MAPL_DirPathMod
use MAPL_KeywordEnforcerMod
use MAPL_ExternalGridFactoryMod
use MAPL_GridManagerMod
use pFIO
use gFTL_StringVector
use pflogger, only: logging, Logger
use MAPL_TimeUtilsMod, only: is_valid_time, is_valid_date
use MAPL_ExternalGCStorage
use iso_fortran_env
implicit none
private
character(*), parameter :: internal_cap_name = "InternalCapGridComp"
public :: MAPL_CapGridComp, MAPL_CapGridCompCreate, MAPL_CapGridComp_Wrapper
type :: ThroughputTimers
real(kind=real64) :: loop_start_timer
real(kind=REAL64) :: start_run_timer
real(kind=REAL64) :: start_timer
end type
type :: MAPL_CapGridComp
private
type (ESMF_GridComp) :: gc
procedure(), pointer, nopass :: root_set_services => null()
character(len=:), allocatable :: root_dso
character(len=:), allocatable :: final_file, name, cap_rc_file
integer :: nsteps, heartbeat_dt, perpetual_year, perpetual_month, perpetual_day
logical :: amiroot, started_loop_timer
logical :: lperp = .false.
integer :: extdata_id, history_id, root_id, printspec
type(ESMF_Clock) :: clock, clock_hist
type(ESMF_Config) :: cf_ext, cf_root, cf_hist, config
type(ESMF_GridComp), allocatable :: gcs(:)
type(ESMF_State), public :: import_state, export_state
type(ESMF_State), allocatable :: child_imports(:), child_exports(:)
type(ESMF_VM) :: vm
type(ESMF_Time) :: cap_restart_time
type(ESMF_Alarm), allocatable :: alarm_list(:)
type(ESMF_Time), allocatable :: AlarmRingTime(:)
logical, allocatable :: ringingState(:)
logical :: compute_throughput
integer :: n_run_phases
type (ThroughputTimers) :: starts
integer :: step_counter
contains
procedure :: set_services
procedure :: initialize
procedure :: initialize_extdata
procedure :: initialize_history
procedure :: run
procedure :: step
procedure :: finalize
procedure :: get_model_duration
procedure :: get_am_i_root
procedure :: get_heartbeat_dt
procedure :: get_current_time
procedure :: rewind_clock
procedure :: record_state
procedure :: refresh_state
procedure :: destroy_state
procedure :: get_field_from_import
procedure :: get_field_from_internal
procedure :: set_grid
procedure :: inject_external_grid
procedure :: set_clock
procedure :: set_step_counter
procedure :: increment_step_counter
procedure :: get_step_counter
end type MAPL_CapGridComp
type :: MAPL_CapGridComp_Wrapper
type(MAPL_CapGridComp), pointer :: ptr => null()
end type MAPL_CapGridComp_Wrapper
include "mpif.h"
character(len=*), parameter :: Iam = __FILE__
contains
subroutine MAPL_CapGridCompCreate(cap, cap_rc, name, final_file, unusable, n_run_phases, root_set_services, root_dso, rc)
use mapl_StubComponent
type(MAPL_CapGridComp), intent(out), target :: cap
character(*), intent(in) :: cap_rc, name
character(len=*), optional, intent(in) :: final_file
class(KeywordEnforcer), optional, intent(in) :: unusable
procedure(), optional :: root_set_services
character(len=*), optional, intent(in) :: root_dso
integer, optional, intent(in) :: n_run_phases
integer, optional, intent(out) :: rc
type(MAPL_CapGridComp_Wrapper) :: cap_wrapper
type(MAPL_MetaComp), pointer :: meta => null()
integer :: status
character(*), parameter :: cap_name = "CAP"
type(StubComponent) :: stub_component
_UNUSED_DUMMY(unusable)
cap%cap_rc_file = cap_rc
if (present(root_set_services)) cap%root_set_services => root_set_services
if (present(root_dso)) cap%root_dso = root_dso
if (present(root_dso) .and. present(root_set_services)) then
_FAIL("can only specify a setservice pointer or a dso to use")
end if
if (present(final_file)) then
allocate(cap%final_file, source=final_file)
end if
cap%n_run_phases = 1
if (present(n_run_phases)) cap%n_run_phases = n_run_phases
cap%config = ESMF_ConfigCreate(_RC)
call ESMF_ConfigLoadFile(cap%config, cap%cap_rc_file,_RC)
allocate(cap%name, source=name)
cap%gc = ESMF_GridCompCreate(name=cap_name, config=cap%config, _RC)
meta => null()
call MAPL_InternalStateCreate(cap%gc, meta, _RC)
call MAPL_Set(meta, CF=cap%config, _RC)
call MAPL_Set(meta, name=cap_name, component=stub_component, _RC)
cap_wrapper%ptr => cap
call ESMF_UserCompSetInternalState(cap%gc, internal_cap_name, cap_wrapper, status)
_VERIFY(status)
_RETURN(_SUCCESS)
end subroutine MAPL_CapGridCompCreate
subroutine initialize_gc(gc, import_state, export_state, clock, rc)
type(ESMF_GridComp) :: gc
type(ESMF_State) :: import_state, export_state
type(ESMF_Clock) :: clock
integer, intent(out) :: rc
type(ESMF_GridComp) :: GCMGC
type (ESMF_VM) :: gcmVM
integer :: comm
integer :: N,NSTEPS
integer :: NPES
integer :: corespernode
logical :: amIRoot_
character(len=ESMF_MAXSTR) :: enableTimers
character(len=ESMF_MAXSTR) :: enableMemUtils
integer :: MemUtilsMode
integer :: useShmem
integer :: status
type (t_extdata_state), pointer :: ExtData_internal_state => null()
type (extdata_wrap) :: wrap
character(len=ESMF_MAXSTR ) :: timerModeStr
integer :: timerMode
type(ESMF_TimeInterval) :: Frequency
character(len=ESMF_MAXSTR) :: ROOT_NAME
! Misc locals
!------------
character(len=ESMF_MAXSTR) :: EXPID
character(len=ESMF_MAXSTR) :: EXPDSC
integer :: RUN_DT
integer :: snglcol
character(len=ESMF_MAXSTR) :: replayMode
integer :: nx
integer :: ny
integer :: HEARTBEAT_DT
type(ESMF_Alarm) :: PERPETUAL
character(len=ESMF_MAXSTR) :: clockname
character(len=ESMF_MAXSTR) :: HIST_CF, ROOT_CF, EXTDATA_CF
character(len=ESMF_MAXSTR ) :: DYCORE
character(len=ESMF_MAXPATHLEN) :: user_dirpath,tempString
logical :: tend,foundPath
logical :: cap_clock_is_present
type (MAPL_MetaComp), pointer :: maplobj, root_obj
character(len=ESMF_MAXSTR) :: sharedObj
type (ESMF_GridComp), pointer :: root_gc
procedure(), pointer :: root_set_services
type(MAPL_CapGridComp), pointer :: cap
class(BaseProfiler), pointer :: t_p
class(Logger), pointer :: lgr
type(ESMF_Clock) :: cap_clock
logical :: use_extdata2g
_UNUSED_DUMMY(import_state)
_UNUSED_DUMMY(export_state)
_UNUSED_DUMMY(clock)
cap => get_CapGridComp_from_gc(gc)
call MAPL_InternalStateRetrieve(gc, maplobj, rc=status)
_VERIFY(status)
t_p => get_global_time_profiler()
call ESMF_GridCompGet(gc, vm = cap%vm, rc = status)
_VERIFY(status)
call ESMF_VMGet(cap%vm, petcount = NPES, mpiCommunicator = comm, rc = status)
_VERIFY(status)
AmIRoot_ = MAPL_Am_I_Root(cap%vm)
call MAPL_GetNodeInfo(comm = comm, rc = status)
_VERIFY(STATUS)
AmIRoot_ = MAPL_Am_I_Root(cap%vm)
cap%AmIRoot = AmIRoot_
! CAP's MAPL MetaComp
!---------------------
! Note the call to GetLogger must be _after_ the call to MAPL_Set().
! That call establishes the name of this component which is used in
! retrieving this component's logger.
call MAPL_GetLogger(gc, lgr, rc=status)
_VERIFY(status)
! Check if user wants to use node shared memory (default is no)
!--------------------------------------------------------------
call MAPL_GetResource(MAPLOBJ, useShmem, label = 'USE_SHMEM:', default = 0, rc = status)
if (useShmem /= 0) then
call MAPL_InitializeShmem (rc = status)
_VERIFY(status)
end if
! Check if a valid clock was provided externally
!-----------------------------------------------
call ESMF_GridCompGet(gc, clockIsPresent=cap_clock_is_present, rc=status)
_VERIFY(status)
if (cap_clock_is_present) then
call ESMF_GridCompGet(gc, clock=cap_clock, rc=status)
_VERIFY(status)
call ESMF_ClockValidate(cap_clock, rc=status)
_VERIFY(status)
cap%clock = ESMF_ClockCreate(cap_clock, rc=status)
_VERIFY(status)
! NOTE: We assume the MAPL components will only advance by
! one time step when driven with an external clock.
!---------------------------------------------------------
cap%nsteps = 1
cap%compute_throughput = .false.
else
! Create Clock. This is a private routine that sets the start and
! end times and the time interval of the clock from the configuration.
! The start time is temporarily set to 1 interval before the time in the
! configuration. Once the Alarms are set in intialize, the clock will
! be advanced to guarantee it and its alarms are in the same state as they
! were after the last advance before the previous Finalize.
!---------------------------------------------------------------------------
call MAPL_ClockInit(MAPLOBJ, cap%clock, nsteps, rc = status)
_VERIFY(status)
cap%nsteps = nsteps
cap%compute_throughput = .true.
end if
call ESMF_ClockGet(cap%clock,currTime=cap%cap_restart_time,rc=status)
_VERIFY(status)
cap%clock_hist = ESMF_ClockCreate(cap%clock, rc = STATUS ) ! Create copy for HISTORY
_VERIFY(STATUS)
CoresPerNode = MAPL_CoresPerNodeGet(comm,rc=status)
_VERIFY(STATUS)
! We check resource for CoresPerNode (no longer needed to be in CAP.rc)
! If it is set in the resource, we issue an warning if the
! value does not agree with the detected CoresPerNode
call ESMF_ConfigGetAttribute(cap%config, value = n, Label = "CoresPerNode:", rc = status)
if (status == ESMF_SUCCESS) then
if (CoresPerNode /= n) then
call lgr%warning("CoresPerNode set (%i0), but does NOT match detected value (%i0)", CoresPerNode, n)
end if
end if
call ESMF_VMGet(cap%vm, petcount=npes, mpicommunicator=comm, rc=status)
_VERIFY(status)
_ASSERT(CoresPerNode <= npes, 'something impossible happened')
if (cap_clock_is_present) then
call ESMF_ClockGet(cap%clock, timeStep=frequency, rc=status)
_VERIFY(status)
call ESMF_TimeIntervalGet(frequency, s=heartbeat_dt, rc=status)
_VERIFY(status)
else
call ESMF_ConfigGetAttribute(cap%config, value = heartbeat_dt, Label = "HEARTBEAT_DT:", rc = status)
_VERIFY(status)
call ESMF_TimeIntervalSet(frequency, s = heartbeat_dt, rc = status)
_VERIFY(status)
end if
cap%heartbeat_dt = heartbeat_dt
perpetual = ESMF_AlarmCreate(clock = cap%clock_hist, name = 'PERPETUAL', ringinterval = frequency, sticky = .false., rc = status)
_VERIFY(status)
call ESMF_AlarmRingerOff(perpetual, rc = status)
_VERIFY(status)
! Set CLOCK for AGCM if not externally provided
! ---------------------------------------------
if (.not.cap_clock_is_present) then
call MAPL_GetResource(MAPLOBJ, cap%perpetual_year, label='PERPETUAL_YEAR:', default = -999, rc = status)
_VERIFY(status)
call MAPL_GetResource(MAPLOBJ, cap%perpetual_month, label='PERPETUAL_MONTH:', default = -999, rc = status)
_VERIFY(status)
call MAPL_GetResource(MAPLOBJ, cap%perpetual_day, label='PERPETUAL_DAY:', default = -999, rc = status)
_VERIFY(status)
cap%lperp = ((cap%perpetual_day /= -999) .or. (cap%perpetual_month /= -999) .or. (cap%perpetual_year /= -999))
if (cap%perpetual_day /= -999) then
_ASSERT(cap%perpetual_month /= -999, 'Must specify a value for PERPETUAL_MONTH in cap.')
_ASSERT(cap%perpetual_year /= -999, 'Must specify a value for PERPETUAL_YEAR in cap.')
endif
if (cap%lperp) then
if (cap%perpetual_year /= -999) call lgr%info('Using Perpetual Year: %i0', cap%perpetual_year)
if (cap%perpetual_month /= -999) call lgr%info('Using Perpetual Month: %i0', cap%perpetual_month)
if (cap%perpetual_day /= -999) call lgr%info('Using Perpetual Day: %i0', cap%perpetual_day)
call ESMF_ClockGet(cap%clock, name = clockname, rc = status)
clockname = trim(clockname) // '_PERPETUAL'
call ESMF_Clockset(cap%clock, name = clockname, rc = status)
call ESMF_ClockGet(cap%clock_hist, name = clockname, rc = status)
clockname = trim(clockname) // '_PERPETUAL'
call ESMF_Clockset(cap%clock_hist, name = clockname, rc = status)
call Perpetual_Clock(cap, rc=status)
_VERIFY(status)
endif
endif
! Get configurable info to create HIST
! and the ROOT of the computational hierarchy
!---------------------------------------------
!BOR
! !RESOURCE_ITEM: string :: Name of ROOT's config file
call MAPL_GetResource(MAPLOBJ, ROOT_CF, "ROOT_CF:", default = "ROOT.rc", rc = status)
_VERIFY(status)
! !RESOURCE_ITEM: string :: Name to assign to the ROOT component
call MAPL_GetResource(MAPLOBJ, ROOT_NAME, "ROOT_NAME:", default = "ROOT", rc = status)
_VERIFY(status)
! !RESOURCE_ITEM: string :: Name of HISTORY's config file
call MAPL_GetResource(MAPLOBJ, HIST_CF, "HIST_CF:", default = "HIST.rc", rc = status)
_VERIFY(status)
! !RESOURCE_ITEM: string :: Name of ExtData's config file
call MAPL_GetResource(MAPLOBJ, EXTDATA_CF, "EXTDATA_CF:", default = 'ExtData.rc', rc = status)
_VERIFY(status)
! !RESOURCE_ITEM: string :: Control Timers
call MAPL_GetResource(MAPLOBJ, enableTimers, "MAPL_ENABLE_TIMERS:", default = 'NO', rc = status)
_VERIFY(status)
! !RESOURCE_ITEM: string :: Control Memory Diagnostic Utility
call MAPL_GetResource(MAPLOBJ, enableMemUtils, "MAPL_ENABLE_MEMUTILS:", default='NO', rc = status)
_VERIFY(status)
call MAPL_GetResource(MAPLOBJ, MemUtilsMode, "MAPL_MEMUTILS_MODE:", default = MAPL_MemUtilsModeBase, rc = status)
_VERIFY(status)
!EOR
enableTimers = ESMF_UtilStringUpperCase(enableTimers, rc = status)
_VERIFY(status)
call MAPL_GetResource(maplobj,use_extdata2g,"USE_EXTDATA2G:",default=.false.,_RC)
if (enableTimers /= 'YES') then
call MAPL_ProfDisable(rc = status)
_VERIFY(status)
else
call MAPL_GetResource(MAPLOBJ, timerModeStr, "MAPL_TIMER_MODE:", &
default='MINMAX', RC=STATUS )
_VERIFY(STATUS)
timerModeStr = ESMF_UtilStringUpperCase(timerModeStr, rc=STATUS)
_VERIFY(STATUS)
end if
cap%started_loop_timer=.false.
enableMemUtils = ESMF_UtilStringUpperCase(enableMemUtils, rc=STATUS)
_VERIFY(STATUS)
if (enableMemUtils /= 'YES') then
call MAPL_MemUtilsDisable( rc=STATUS )
_VERIFY(STATUS)
else
call MAPL_MemUtilsInit( mode=MemUtilsMode, rc=STATUS )
_VERIFY(STATUS)
end if
call MAPL_GetResource( MAPLOBJ, cap%printSpec, label='PRINTSPEC:', default = 0, rc=STATUS )
_VERIFY(STATUS)
call dirpaths%append(".",rc=status)
_VERIFY(status)
call ESMF_ConfigFindLabel(cap%config,Label='USER_DIRPATH:',isPresent=foundPath,rc=status)
if (foundPath) then
tend=.false.
do while (.not.tend)
call ESMF_ConfigGetAttribute(cap%config,value=user_dirpath,default='',rc=status)
if (tempstring /= '') then
call dirpaths%append(user_dirpath,rc=status)
_VERIFY(status)
end if
call ESMF_ConfigNextLine(cap%config,tableEnd=tend,rc=status)
_VERIFY(STATUS)
enddo
end if
! Handle RUN_DT in ROOT_CF
!-------------------------
cap%cf_root = ESMF_ConfigCreate(rc=STATUS )
_VERIFY(STATUS)
call ESMF_ConfigLoadFile(cap%cf_root, ROOT_CF, rc=STATUS )
_VERIFY(STATUS)
call ESMF_ConfigGetAttribute(cap%cf_root, value=RUN_DT, Label="RUN_DT:", rc=status)
if (STATUS == ESMF_SUCCESS) then
if (heartbeat_dt /= run_dt) then
call lgr%error('inconsistent values of HEARTBEAT_DT (%g0) and root RUN_DT (%g0)', heartbeat_dt, run_dt)
_FAIL('inconsistent values of HEARTBEAT_DT and RUN_DT')
end if
else
call MAPL_ConfigSetAttribute(cap%cf_root, value=heartbeat_dt, Label="RUN_DT:", rc=status)
_VERIFY(STATUS)
endif
! Add EXPID and EXPDSC from HISTORY.rc to AGCM.rc
!------------------------------------------------
cap%cf_hist = ESMF_ConfigCreate(rc=STATUS )
_VERIFY(STATUS)
call ESMF_ConfigLoadFile(cap%cf_hist, HIST_CF, rc=STATUS )
_VERIFY(STATUS)
call MAPL_ConfigSetAttribute(cap%cf_hist, value=HIST_CF, Label="HIST_CF:", rc=status)
_VERIFY(STATUS)
call ESMF_ConfigGetAttribute(cap%cf_hist, value=EXPID, Label="EXPID:", default='', rc=status)
_VERIFY(STATUS)
call ESMF_ConfigGetAttribute(cap%cf_hist, value=EXPDSC, Label="EXPDSC:", default='', rc=status)
_VERIFY(STATUS)
call MAPL_ConfigSetAttribute(cap%cf_hist, value=heartbeat_dt, Label="RUN_DT:", rc=status)
_VERIFY(STATUS)
call MAPL_ConfigSetAttribute(cap%cf_root, value=EXPID, Label="EXPID:", rc=status)
_VERIFY(STATUS)
call MAPL_ConfigSetAttribute(cap%cf_root, value=EXPDSC, Label="EXPDSC:", rc=status)
_VERIFY(STATUS)
call ESMF_ConfigGetAttribute(cap%cf_root, value = NX, Label="NX:", rc=status)
_VERIFY(STATUS)
call ESMF_ConfigGetAttribute(cap%cf_root, value = NY, Label="NY:", rc=status)
_VERIFY(STATUS)
call MAPL_ConfigSetAttribute(cap%cf_hist, value=NX, Label="NX:", rc=status)
_VERIFY(STATUS)
call MAPL_ConfigSetAttribute(cap%cf_hist, value=NY, Label="NY:", rc=status)
_VERIFY(STATUS)
! Add CoresPerNode from CAP.rc to HISTORY.rc and AGCM.rc
!-------------------------------------------------------
call MAPL_ConfigSetAttribute(cap%cf_root, value=CoresPerNode, Label="CoresPerNode:", rc=status)
_VERIFY(STATUS)
call MAPL_ConfigSetAttribute(cap%cf_hist, value=CoresPerNode, Label="CoresPerNode:", rc=status)
_VERIFY(STATUS)
! Add a SINGLE_COLUMN flag in HISTORY.rc based on DYCORE value(from AGCM.rc)
!---------------------------------------------------------------------------
call ESMF_ConfigGetAttribute(cap%cf_root, value=DYCORE, Label="DYCORE:", default = 'FV3', rc=status)
_VERIFY(STATUS)
if (DYCORE == 'DATMO') then
snglcol = 1
call MAPL_ConfigSetAttribute(cap%cf_hist, value=snglcol, Label="SINGLE_COLUMN:", rc=status)
_VERIFY(STATUS)
end if
! Detect if this a regular replay in the AGCM.rc
! ----------------------------------------------
call ESMF_ConfigGetAttribute(cap%cf_root, value=ReplayMode, Label="REPLAY_MODE:", default="NoReplay", rc=status)
_VERIFY(STATUS)
! Register the children with MAPL
!--------------------------------
! Create Root child
!-------------------
call MAPL_Set(MAPLOBJ, CF=CAP%CF_ROOT, RC=STATUS)
_VERIFY(STATUS)
root_set_services => cap%root_set_services
call t_p%start('SetService')
if (.not.allocated(cap%root_dso)) then
cap%root_id = MAPL_AddChild(MAPLOBJ, name = root_name, SS = root_set_services, rc = status)
_VERIFY(status)
else
sharedObj = trim(cap%root_dso)
cap%root_id = MAPL_AddChild(MAPLOBJ, root_name, 'setservices_', sharedObj=sharedObj, rc=status)
_VERIFY(status)
end if
root_gc => maplobj%get_child_gridcomp(cap%root_id)
call MAPL_GetObjectFromGC(root_gc, root_obj, rc=status)
_ASSERT(cap%n_run_phases <= SIZE(root_obj%phase_run),"n_run_phases in cap_gc should not exceed n_run_phases in root")
! Create History child
!----------------------
call MAPL_Set(MAPLOBJ, CF=CAP%CF_HIST, RC=STATUS)
_VERIFY(STATUS)
cap%history_id = MAPL_AddChild( MAPLOBJ, name = 'HIST', SS = HIST_SetServices, rc = status)
_VERIFY(status)
! Create ExtData child
!----------------------
cap%cf_ext = ESMF_ConfigCreate(rc=STATUS )
_VERIFY(STATUS)
call ESMF_ConfigLoadFile(cap%cf_ext, EXTDATA_CF, rc=STATUS )
_VERIFY(STATUS)
call ESMF_ConfigGetAttribute(cap%cf_ext, value=RUN_DT, Label="RUN_DT:", rc=status)
if (STATUS == ESMF_SUCCESS) then
if (heartbeat_dt /= run_dt) then
call lgr%error('inconsistent values of HEARTBEAT_DT (%g0) and ExtData RUN_DT (%g0)', heartbeat_dt, run_dt)
_FAIL('inconsistent values of HEARTBEAT_DT and RUN_DT')
end if
else
call MAPL_ConfigSetAttribute(cap%cf_ext, value=heartbeat_dt, Label="RUN_DT:", rc=status)
_VERIFY(STATUS)
endif
call MAPL_Set(MAPLOBJ, CF=CAP%CF_EXT, RC=STATUS)
_VERIFY(STATUS)
if (use_extdata2g) then
#if defined(BUILD_WITH_EXTDATA2G)
cap%extdata_id = MAPL_AddChild (MAPLOBJ, name = 'EXTDATA', SS = ExtData2G_SetServices, _RC)
#else
call lgr%error('ExtData2G requested but not built')
_FAIL('ExtData2G requested but not built')
#endif
else
cap%extdata_id = MAPL_AddChild (MAPLOBJ, name = 'EXTDATA', SS = ExtData1G_SetServices, _RC)
end if
call t_p%stop('SetService')
! Add NX and NY from AGCM.rc to ExtData.rc as well as name of ExtData rc file
call ESMF_ConfigGetAttribute(cap%cf_root, value = NX, Label="NX:", rc=status)
_VERIFY(STATUS)
call ESMF_ConfigGetAttribute(cap%cf_root, value = NY, Label="NY:", rc=status)
_VERIFY(STATUS)
call MAPL_ConfigSetAttribute(cap%cf_ext, value=NX, Label="NX:", rc=status)
_VERIFY(STATUS)
call MAPL_ConfigSetAttribute(cap%cf_ext, value=NY, Label="NY:", rc=status)
_VERIFY(STATUS)
call MAPL_ConfigSetAttribute(cap%cf_ext, value=EXTDATA_CF, Label="CF_EXTDATA:", rc=status)
_VERIFY(STATUS)
call MAPL_ConfigSetAttribute(cap%cf_ext, value=EXPID, Label="EXPID:", rc=status)
_VERIFY(STATUS)
! Query MAPL for the the children's for GCS, IMPORTS, EXPORTS
!-------------------------------------------------------------
call MAPL_Get(MAPLOBJ, childrens_gridcomps = cap%gcs, &
childrens_import_states = cap%child_imports, childrens_export_states = cap%child_exports, rc = status)
_VERIFY(status)
! Inject grid to root child if grid has been set externally
!-----------------------------------------------------------
call cap%inject_external_grid(_RC)
! Run as usual unless PRINTSPEC> 0 as set in CAP.rc. If set then
! model will not run completely and instead it will simply run MAPL_SetServices
! and print out the IM/EX specs. This step uses MAPL_StatePrintSpecCSV found
! in MAPL_Generic.F90.
if (cap%printSpec>0) then
call MAPL_StatePrintSpecCSV(cap%gcs(cap%root_id), cap%printspec, rc = status)
_VERIFY(status)
call ESMF_VMBarrier(cap%vm, rc = status)
_VERIFY(status)
else
! Initialize the Computational Hierarchy
!----------------------------------------
call t_p%start('Initialize')
call ESMF_GridCompInitialize(cap%gcs(cap%root_id), importState = cap%child_imports(cap%root_id), &
exportState = cap%child_exports(cap%root_id), clock = cap%clock, userRC = status)
_VERIFY(status)
call cap%initialize_history(rc=status)
_VERIFY(status)
call cap%initialize_extdata(root_gc,rc=status)
_VERIFY(status)
! Finally check is this is a regular replay
! If so stuff gc and input state for ExtData in GCM internal state
! -----------------------------------------------------------------
if (trim(replayMode)=="Regular") then
call MAPL_GCGet(CAP%GCS(cap%root_id),"GCM",gcmGC,rc=status)
_VERIFY(STATUS)
call ESMF_GridCompGet(gcmGC,vm=gcmVM,rc=status)
_VERIFY(STATUS)
_ASSERT(cap%vm==gcmVM,'CAP and GCM should agree on their VMs.')
call ESMF_UserCompGetInternalState(gcmGC,'ExtData_state',wrap,status)
_VERIFY(STATUS)
ExtData_internal_state => wrap%ptr
ExtData_internal_state%gc = CAP%GCS(cap%extdata_id)
ExtData_internal_state%expState = CAP%CHILD_EXPORTS(cap%extdata_id)
end if
call t_p%stop('Initialize')
end if
_RETURN(ESMF_SUCCESS)
end subroutine initialize_gc
subroutine initialize_history(cap, rc)
class(MAPL_CapGridComp), intent(inout) :: cap
integer, optional, intent(out) :: rc
integer :: status
type(HISTORY_ExchangeListWrap) :: lswrap
integer(kind=INT64), pointer :: LSADDR(:) => null()
if (present(rc)) rc = ESMF_SUCCESS
! All the EXPORTS of the Hierachy are made IMPORTS of History
!------------------------------------------------------------
call ESMF_StateAdd(cap%child_imports(cap%history_id), [cap%child_exports(cap%root_id)], rc = status)
_VERIFY(STATUS)
allocate(lswrap%ptr, stat = status)
_VERIFY(STATUS)
call ESMF_UserCompSetInternalState(cap%gcs(cap%history_id), 'MAPL_LocStreamList', &
lswrap, STATUS)
_VERIFY(STATUS)
call MAPL_GetAllExchangeGrids(CAP%GCS(cap%root_id), LSADDR, RC=STATUS)
_VERIFY(STATUS)
lswrap%ptr%LSADDR_PTR => LSADDR
! Initialize the History
!------------------------
call ESMF_GridCompInitialize (CAP%GCS(cap%history_id), importState=CAP%CHILD_IMPORTS(cap%history_id), &
exportState=CAP%CHILD_EXPORTS(cap%history_id), clock=CAP%CLOCK_HIST, userRC=STATUS )
_VERIFY(STATUS)
_RETURN(ESMF_SUCCESS)
end subroutine initialize_history
subroutine initialize_extdata(cap , root_gc, rc)
class(MAPL_CapGridComp), intent(inout) :: cap
type (ESMF_GridComp), intent(inout), pointer :: root_gc
integer, optional, intent(out) :: rc
integer :: item_count, status
type (ESMF_StateItem_Flag), pointer :: item_types(:)
character(len=ESMF_MAXSTR ), pointer :: item_names(:)
type(ESMF_Field) :: field
type(ESMF_FieldBundle) :: bundle
type(StringVector) :: cap_imports_vec, cap_exports_vec
type(StringVectorIterator) :: iter
integer :: i
type(ESMF_State) :: state, root_imports, component_state
character(len=:), allocatable :: component_name, field_name
! Prepare EXPORTS for ExtData
! ---------------------------
cap_imports_vec = get_vec_from_config(cap%config, "CAP_IMPORTS")
cap_exports_vec = get_vec_from_config(cap%config, "CAP_EXPORTS")
cap%import_state = ESMF_StateCreate(name = "Cap_Imports", stateintent = ESMF_STATEINTENT_IMPORT)
cap%export_state = ESMF_StateCreate(name = "Cap_Exports", stateintent = ESMF_STATEINTENT_EXPORT)
if (cap_exports_vec%size() /= 0) then
iter = cap_exports_vec%begin()
do while(iter /= cap_exports_vec%end())
component_name = iter%get()
component_name = trim(component_name(index(component_name, ",")+1:))
field_name = iter%get()
field_name = trim(field_name(1:index(field_name, ",")-1))
call MAPL_ExportStateGet([cap%child_exports(cap%root_id)], component_name, &
component_state, status)
_VERIFY(status)
call ESMF_StateGet(component_state, trim(field_name), field, rc = status)
_VERIFY(status)
call MAPL_StateAdd(cap%export_state, field, rc = status)
_VERIFY(status)
call iter%next()
end do
end if
call ESMF_StateGet(cap%child_imports(cap%root_id), itemcount = item_count, rc = status)
_VERIFY(status)
allocate(item_names(item_count), stat = status)
_VERIFY(status)
allocate(item_types(item_count), stat = status)
_VERIFY(status)
call ESMF_StateGet(cap%child_imports(cap%root_id), itemnamelist = item_names, &
itemtypelist = item_types, rc = status)
_VERIFY(status)
root_imports = cap%child_imports(cap%root_id)
do i = 1, item_count
if (vector_contains_str(cap_imports_vec, item_names(i))) then
state = cap%import_state
else
state = cap%child_exports(cap%extdata_id)
end if
if (item_types(i) == ESMF_StateItem_Field) then
call ESMF_StateGet(root_imports, item_names(i), field, rc = status)
_VERIFY(status)
call MAPL_AddAttributeToFields(root_gc,trim(item_names(i)),'RESTART',MAPL_RestartSkip,_RC)
call MAPL_StateAdd(state, field, rc = status)
_VERIFY(status)
else if (item_types(i) == ESMF_StateItem_FieldBundle) then
call ESMF_StateGet(root_imports, item_names(i), bundle, rc = status)
_VERIFY(status)
call MAPL_StateAdd(state, bundle, rc = status)
_VERIFY(status)
end if
end do
deallocate(item_types)
deallocate(item_names)
! Initialize the ExtData
!------------------------
call ESMF_GridCompInitialize (cap%gcs(cap%extdata_id), importState = cap%child_imports(cap%extdata_id), &
exportState = cap%child_exports(cap%extdata_id), &
clock = cap%clock, userRc = status)
_VERIFY(status)
_RETURN(ESMF_SUCCESS)
end subroutine initialize_extdata
subroutine run_gc(gc, import, export, clock, rc)
!ARGUMENTS:
type(ESMF_GridComp) :: GC ! Gridded component
type(ESMF_State) :: import ! Import state
type(ESMF_State) :: export ! Export state
type(ESMF_Clock) :: clock ! The clock
integer, intent(out) :: RC ! Error code:
integer :: status, phase
class (BaseProfiler), pointer :: t_p
_UNUSED_DUMMY(import)
_UNUSED_DUMMY(export)
_UNUSED_DUMMY(clock)
t_p => get_global_time_profiler()
call t_p%start('Run')
call ESMF_GridCompGet( gc, currentPhase=phase, RC=status )
VERIFY_(status)
call run_MAPL_GridComp(gc, phase=phase, rc=status)
_VERIFY(status)
call t_p%stop('Run')
_RETURN(ESMF_SUCCESS)
end subroutine run_gc
subroutine finalize_gc(gc, import_state, export_state, clock, rc)
type(ESMF_GridComp) :: gc
type(ESMF_State) :: import_state, export_state
type(ESMF_Clock) :: clock
integer, intent(out) :: rc
integer :: status
type(MAPL_CapGridComp), pointer :: cap
type(MAPL_MetaComp), pointer :: maplobj
class (BaseProfiler), pointer :: t_p
_UNUSED_DUMMY(import_state)
_UNUSED_DUMMY(export_state)
_UNUSED_DUMMY(clock)
cap => get_CapGridComp_from_gc(gc)
call MAPL_GetObjectFromGC(gc, maplobj, rc=status)
_VERIFY(status)
t_p => get_global_time_profiler()
call t_p%start('Finalize')
if (.not. cap%printspec > 0) then
call ESMF_GridCompFinalize(cap%gcs(cap%root_id), importstate = cap%child_imports(cap%root_id), &
exportstate=cap%child_exports(cap%root_id), clock = cap%clock, userrc = status)
_VERIFY(status)
call ESMF_GridCompFinalize(cap%gcs(cap%history_id), importstate = cap%child_imports(cap%history_id), &
exportstate = cap%child_exports(cap%history_id), clock = cap%clock_hist, userrc = status)
_VERIFY(status)
call ESMF_GridCompFinalize(cap%gcs(cap%extdata_id), importstate = cap%child_imports(cap%extdata_id), &
exportstate = cap%child_exports(cap%extdata_id), clock = cap%clock, userrc = status)
_VERIFY(status)
call CAP_Finalize(CAP%CLOCK_HIST, "cap_restart", rc=STATUS)
_VERIFY(status)
call ESMF_ConfigDestroy(cap%cf_ext, rc = status)
_VERIFY(status)
call ESMF_ConfigDestroy(cap%cf_hist, rc = status)
_VERIFY(status)
call ESMF_ConfigDestroy(cap%cf_root, rc = status)
_VERIFY(status)
call ESMF_ConfigDestroy(cap%config, rc = status)
_VERIFY(status)
call MAPL_FinalizeShmem(rc = status)
_VERIFY(STATUS)
! Write EGRESS file
!------------------
call ESMF_VMBarrier(cap%vm)
if(allocated(cap%final_file)) then
if (cap%AmIRoot) then
close(99)
open (99,file=cap%final_file,form='formatted')
close(99)
end if
end if
end if
call t_p%stop('Finalize')
_RETURN(ESMF_SUCCESS)
end subroutine finalize_gc
subroutine set_services_gc(gc, rc)
type (ESMF_GridComp) :: gc
integer, intent(out) :: rc
integer :: status, phase
type(MAPL_CapGridComp), pointer :: cap
cap => get_CapGridComp_from_gc(gc)
call ESMF_GridCompSetEntryPoint(gc, ESMF_METHOD_INITIALIZE, userRoutine = initialize_gc, rc = status)
_VERIFY(status)
do phase = 1, cap%n_run_phases
call ESMF_GridCompSetEntryPoint(gc, ESMF_METHOD_RUN, userRoutine = run_gc, rc = status)
_VERIFY(status)
enddo
call ESMF_GridCompSetEntryPoint(gc, ESMF_METHOD_FINALIZE, userRoutine = finalize_gc, rc = status)
_VERIFY(status)
_RETURN(ESMF_SUCCESS)
end subroutine set_services_gc
subroutine set_services(this, rc)
class(MAPL_CapGridComp), intent(inout) :: this
integer, optional, intent(out) :: rc
integer :: status
call ESMF_GridCompSetServices(this%gc, set_services_gc, rc = status)
_VERIFY(status)
_RETURN(ESMF_SUCCESS)
end subroutine set_services
subroutine initialize(this, rc)
class(MAPL_CapGridComp), intent(inout) :: this
integer, optional, intent(out) :: rc
integer :: status
call ESMF_GridCompInitialize(this%gc, userRC=status)
_VERIFY(status)
_RETURN(ESMF_SUCCESS)
end subroutine initialize
subroutine run(this, phase, rc)
class(MAPL_CapGridComp), intent(inout) :: this
integer, optional, intent(in) :: phase
integer, optional, intent(out) :: rc
integer :: status
integer :: userrc, phase_
phase_ = 1
if (present(phase)) phase_ = phase
call ESMF_GridCompRun(this%gc, phase=phase_, userrc=userrc, rc=status)
_VERIFY(status)
_VERIFY(userrc)
_RETURN(ESMF_SUCCESS)
end subroutine run
subroutine finalize(this, rc)
class(MAPL_CapGridComp), intent(inout) :: this
integer, optional, intent(out) :: rc
integer :: status
call ESMF_GridCompFinalize(this%gc, rc = status)
_VERIFY(status)
_RETURN(ESMF_SUCCESS)
end subroutine finalize
function get_model_duration(this, rc) result (duration)
class (MAPL_CapGridComp) :: this
integer, optional, intent(out) :: rc
integer :: duration
duration = this%nsteps * this%heartbeat_dt
_RETURN(ESMF_SUCCESS)
end function get_model_duration
function get_am_i_root(this, rc) result (amiroot)
class (MAPL_CapGridComp) :: this
integer, optional, intent(out) :: rc