forked from ESCOMP/CMEPS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathesm.F90
1549 lines (1282 loc) · 63.4 KB
/
esm.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
module ESM
!-----------------------------------------------------------------------------
! Code that specializes generic ESM Component code.
!-----------------------------------------------------------------------------
use shr_kind_mod , only : r8=>shr_kind_r8, cl=>shr_kind_cl, cs=>shr_kind_cs
use shr_sys_mod , only : shr_sys_abort
use shr_mpi_mod , only : shr_mpi_bcast
use shr_mem_mod , only : shr_mem_init
use shr_log_mod , only : shr_log_setLogunit
use esm_utils_mod, only : logunit, maintask, dbug_flag, chkerr
implicit none
private
public :: SetServices
public :: ReadAttributes ! used in ensemble_driver
private :: SetModelServices
private :: SetRunSequence
private :: ModifyCplLists
private :: InitAttributes
private :: CheckAttributes
private :: AddAttributes
private :: InitAdvertize
private :: esm_init_pelayout
private :: esm_finalize
private :: pretty_print_nuopc_freeformat
real(r8) :: scol_spval = -999._r8
character(*), parameter :: u_FILE_u = &
__FILE__
!================================================================================
contains
!================================================================================
subroutine SetServices(driver, rc)
use NUOPC , only : NUOPC_CompDerive, NUOPC_CompSpecialize, NUOPC_CompSetInternalEntryPoint
use NUOPC_Driver , only : driver_routine_SS => SetServices
use NUOPC_Driver , only : driver_label_SetModelServices => label_SetModelServices
use NUOPC_Driver , only : driver_label_SetRunSequence => label_SetRunSequence
use NUOPC_Driver , only : driver_label_Finalize => label_Finalize
use ESMF , only : ESMF_GridComp, ESMF_Config, ESMF_GridCompSet, ESMF_ConfigLoadFile
use ESMF , only : ESMF_METHOD_INITIALIZE
use ESMF , only : ESMF_SUCCESS, ESMF_LogWrite, ESMF_LOGMSG_INFO
! input/output variables
type(ESMF_GridComp) :: driver
integer, intent(out) :: rc
! local variables
character(len=*), parameter :: subname = "(esm.F90:SetServices)"
!---------------------------------------
rc = ESMF_SUCCESS
call ESMF_LogWrite(trim(subname)//": called", ESMF_LOGMSG_INFO)
! NUOPC_Driver registers the generic methods
call NUOPC_CompDerive(driver, driver_routine_SS, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
! attach specializing method(s)
call NUOPC_CompSpecialize(driver, specLabel=driver_label_SetModelServices, &
specRoutine=SetModelServices, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
call NUOPC_CompSpecialize(driver, specLabel=driver_label_SetRunSequence, &
specRoutine=SetRunSequence, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
! register an internal initialization method
call NUOPC_CompSetInternalEntryPoint(driver, ESMF_METHOD_INITIALIZE, &
phaseLabelList=(/"IPDv03p2"/), userRoutine=ModifyCplLists, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
!
! This prevents the driver trying to "auto" connect to the ensemble_driver
! by default the FieldTransferPolicy is "transferall" and we need "transfernone"
!
call NUOPC_CompSetInternalEntryPoint(driver, ESMF_METHOD_INITIALIZE, &
phaseLabelList=(/"IPDv05p1"/), userRoutine=InitAdvertize, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
! Set a finalize method
call NUOPC_CompSpecialize(driver, specLabel=driver_label_Finalize, &
specRoutine=esm_finalize, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
! Create, open and set the config
call ESMF_GridCompSet(driver, configFile="nuopc.runconfig", rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
call ESMF_LogWrite(trim(subname)//": done", ESMF_LOGMSG_INFO)
end subroutine SetServices
!================================================================================
subroutine SetModelServices(driver, rc)
use ESMF , only : ESMF_GridComp, ESMF_VM, ESMF_Config, ESMF_VMBarrier
use ESMF , only : ESMF_GridCompGet, ESMF_VMGet, ESMF_ConfigGetAttribute
use ESMF , only : ESMF_ConfigGetLen, ESMF_RC_NOT_VALID, ESMF_LogFoundAllocError
use ESMF , only : ESMF_LogSetError, ESMF_LogWrite, ESMF_LOGMSG_INFO
use ESMF , only : ESMF_GridCompSet, ESMF_SUCCESS, ESMF_METHOD_INITIALIZE
use ESMF , only : ESMF_VMisCreated, ESMF_GridCompIsPetLocal
use ESMF , only : ESMF_RC_FILE_OPEN, ESMF_RC_FILE_READ
use ESMF , only : ESMF_AttributeUpdate, ESMF_VMBroadcast
use ESMF , only : ESMF_MethodAdd
use NUOPC , only : NUOPC_CompSetInternalEntryPoint, NUOPC_CompAttributeGet
use NUOPC , only : NUOPC_CompAttributeAdd, NUOPC_CompAttributeSet
use NUOPC_Driver , only : NUOPC_DriverAddComp, NUOPC_DriverGetComp
! input/output variables
type(ESMF_GridComp) :: driver
integer, intent(out) :: rc
! local variables
type(ESMF_VM) :: vm
type(ESMF_Config) :: config
integer :: localPet
character(len=CL) :: meminitStr
integer :: global_comm
integer :: maxthreads
character(len=CL) :: msgstr
integer :: componentcount
character(len=*), parameter :: subname = "(esm.F90:SetModelServices)"
!-------------------------------------------
rc = ESMF_SUCCESS
call ESMF_LogWrite(trim(subname)//": called", ESMF_LOGMSG_INFO)
!-------------------------------------------
! Set the io logunit to the value defined in ensemble_driver
!-------------------------------------------
call shr_log_setLogunit(logunit)
!-------------------------------------------
! Get the config and vm objects from the driver
!-------------------------------------------
call ESMF_GridCompGet(driver, vm=vm, config=config, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
call ESMF_VMGet(vm, localPet=localPet, mpiCommunicator=global_comm, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
if (localPet == 0) then
maintask=.true.
else
maintask = .false.
end if
!-------------------------------------------
! determine the generic component labels
!-------------------------------------------
componentCount = ESMF_ConfigGetLen(config,label="component_list:", rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
if (componentCount == 0) then
write (msgstr, *) "No models were specified in component_list "
call ESMF_LogSetError(ESMF_RC_NOT_VALID, msg=msgstr, line=__LINE__, file=__FILE__, rcToReturn=rc)
return ! bail out
endif
!-------------------------------------------
! Obtain driver attributes
!-------------------------------------------
call ReadAttributes(driver, config, "DRIVER_attributes::", formatprint=.true., rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
call ReadAttributes(driver, config, "CLOCK_attributes::", formatprint=.true., rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
call ReadAttributes(driver, config, "ALLCOMP_attributes::", formatprint=.true., rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
call ReadAttributes(driver, config, "PELAYOUT_attributes::", formatprint=.true., rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
call CheckAttributes(driver, rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
!-------------------------------------------
! Initialize other attributes (after initializing driver clock)
!-------------------------------------------
call InitAttributes(driver, rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
!-------------------------------------------
! Initialize component pe layouts
!-------------------------------------------
call esm_init_pelayout(driver, maxthreads, rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
! Memory test
if (maintask) then
call shr_mem_init(strbuf=meminitstr)
write(logunit,*) trim(meminitstr)
end if
call ESMF_LogWrite(trim(subname)//": done", ESMF_LOGMSG_INFO)
end subroutine SetModelServices
!================================================================================
subroutine SetRunSequence(driver, rc)
use ESMF , only : ESMF_GridComp, ESMF_LogWrite, ESMF_SUCCESS, ESMF_LOGMSG_INFO
use ESMF , only : ESMF_Config
use ESMF , only : ESMF_GridCompGet, ESMF_ConfigCreate
use ESMF , only : ESMF_ConfigLoadFile
use NUOPC , only : NUOPC_FreeFormat, NUOPC_FreeFormatDestroy
use NUOPC , only : NUOPC_FreeFormatCreate
use NUOPC_Driver , only : NUOPC_DriverIngestRunSequence, NUOPC_DriverSetRunSequence
use NUOPC_Driver , only : NUOPC_DriverPrint
! input/output variables
type(ESMF_GridComp) :: driver
integer, intent(out) :: rc
! local variables
type(ESMF_Config) :: runSeq
type(NUOPC_FreeFormat) :: runSeqFF
character(len=*), parameter :: subname = "(esm.F90:SetRunSequence)"
!---------------------------------------
rc = ESMF_SUCCESS
call ESMF_LogWrite(trim(subname)//": called", ESMF_LOGMSG_INFO)
call shr_log_setLogunit(logunit)
!--------
! Run Sequence and Connectors
!--------
! read free format run sequence
runSeq = ESMF_ConfigCreate(rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
call ESMF_ConfigLoadFile(runSeq, "nuopc.runseq", rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
runSeqFF = NUOPC_FreeFormatCreate(runSeq, label="runSeq::", rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
call NUOPC_DriverIngestRunSequence(driver, runSeqFF, autoAddConnectors=.true., rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
#ifdef DEBUG
! Uncomment these to add debugging information for driver
! call NUOPC_DriverPrint(driver, orderflag=.true.)
! if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
! line=__LINE__, &
! file=__FILE__)) &
! return ! bail out
! call pretty_print_nuopc_freeformat(runSeqFF, 'run sequence', rc=rc)
! if (chkerr(rc,__LINE__,u_FILE_u)) return
#endif
call NUOPC_FreeFormatDestroy(runSeqFF, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
call ESMF_LogWrite(trim(subname)//": done", ESMF_LOGMSG_INFO)
end subroutine SetRunSequence
!================================================================================
subroutine pretty_print_nuopc_freeformat(ffstuff, label, rc)
use NUOPC, only : NUOPC_FreeFormat, NUOPC_FreeFormatGet, NUOPC_FreeFormatLen
use ESMF, only : ESMF_SUCCESS
! input/output variables
type(NUOPC_FreeFormat) , intent(in) :: ffstuff
character(len=*) , intent(in) :: label
integer , intent(out) :: rc
! local variables
integer :: i
integer :: linecnt
character(len=NUOPC_FreeFormatLen), pointer :: outstr(:)
!---------------------------------------
rc = ESMF_SUCCESS
if (maintask .or. dbug_flag > 3) then
write(logunit, *) 'BEGIN: ', trim(label)
call NUOPC_FreeFormatGet(ffstuff, linecount=linecnt, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
allocate(outstr(linecnt))
call NUOPC_FreeFormatGet(ffstuff, stringList=outstr, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
do i=1,linecnt
if(len_trim(outstr(i)) > 0) then
write(logunit, *) trim(outstr(i))
endif
enddo
write(logunit, *) 'END: ', trim(label)
deallocate(outstr)
endif
end subroutine pretty_print_nuopc_freeformat
!================================================================================
recursive subroutine ModifyCplLists(driver, importState, exportState, clock, rc)
use ESMF , only : ESMF_GridComp, ESMF_State, ESMF_Clock, ESMF_LogWrite
use ESMF , only : ESMF_LOGMSG_INFO, ESMF_CplComp, ESMF_SUCCESS
use NUOPC , only : NUOPC_CompAttributeGet, NUOPC_CompAttributeSet
use NUOPC_Driver , only : NUOPC_DriverGetComp
type(ESMF_GridComp) :: driver
type(ESMF_State) :: importState, exportState
type(ESMF_Clock) :: clock
integer, intent(out) :: rc
type(ESMF_CplComp), pointer :: connectorList(:)
integer :: i, j, cplListSize
character(len=CL), allocatable :: cplList(:)
character(len=CL) :: tempString
character(len=CL) :: msgstr
character(len=*), parameter :: subname = "(esm.F90:ModifyCplLists)"
!---------------------------------------
rc = ESMF_SUCCESS
call ESMF_LogWrite(trim(subname)//": called", ESMF_LOGMSG_INFO)
call shr_log_setLogunit(logunit)
call ESMF_LogWrite("Driver is in ModifyCplLists()", ESMF_LOGMSG_INFO, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
nullify(connectorList)
call NUOPC_DriverGetComp(driver, compList=connectorList, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
write (msgstr,*) "Found ", size(connectorList), " Connectors."// " Modifying CplList Attribute...."
call ESMF_LogWrite(trim(msgstr), ESMF_LOGMSG_INFO, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
do i=1, size(connectorList)
! query the cplList for connector i
call NUOPC_CompAttributeGet(connectorList(i), name="CplList", itemCount=cplListSize, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
if (cplListSize>0) then
allocate(cplList(cplListSize))
call NUOPC_CompAttributeGet(connectorList(i), name="CplList", valueList=cplList, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
! go through all of the entries in the cplList and set the mapping method to "redist"
do j=1, cplListSize
!tempString = trim(cplList(j))//":REMAPMETHOD=bilinear"//&
!":SrcTermProcessing=1:DUMPWEIGHTS=true:TermOrder=SrcSeq"
tempString = trim(cplList(j))//":remapmethod=redist"
cplList(j) = trim(tempString)
enddo
! store the modified cplList in CplList attribute of connector i
call NUOPC_CompAttributeSet(connectorList(i), name="CplList", valueList=cplList, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
deallocate(cplList)
endif
enddo
deallocate(connectorList)
call ESMF_LogWrite(trim(subname)//": done", ESMF_LOGMSG_INFO)
end subroutine ModifyCplLists
!================================================================================
subroutine InitAttributes(driver, rc)
use ESMF , only : ESMF_GridComp, ESMF_GridCompGet
use ESMF , only : ESMF_Clock, ESMF_ClockGet, ESMF_Time, ESMF_TimeGet
use ESMF , only : ESMF_SUCCESS, ESMF_LogWrite, ESMF_LogSetError, ESMF_LOGMSG_INFO
use ESMF , only : ESMF_RC_NOT_VALID
use ESMF , only : ESMF_GridCompIsPetLocal, ESMF_VMBroadcast
use NUOPC , only : NUOPC_CompAttributeGet, NUOPC_CompAttributeSet, NUOPC_CompAttributeAdd
use shr_assert_mod , only : shr_assert_in_domain
use shr_const_mod , only : shr_const_tkfrz, shr_const_tktrip
use shr_const_mod , only : shr_const_mwwv, shr_const_mwdair
use shr_frz_mod , only : shr_frz_freezetemp_init
use shr_reprosum_mod , only : shr_reprosum_setopts
use shr_wv_sat_mod , only : shr_wv_sat_set_default, shr_wv_sat_init
use shr_wv_sat_mod , only : shr_wv_sat_make_tables, ShrWVSatTableSpec
use shr_wv_sat_mod , only : shr_wv_sat_get_scheme_idx, shr_wv_sat_valid_idx
use glc_elevclass_mod, only : glc_elevclass_init
!use shr_scam_mod , only : shr_scam_checkSurface
! input/output variables
type(ESMF_GridComp) , intent(inout) :: driver
integer , intent(out) :: rc ! return code
! local variables
character(len=CL) :: errstring
character(len=CL) :: cvalue
logical :: reprosum_use_ddpdd ! setup reprosum, use ddpdd
real(R8) :: reprosum_diffmax ! setup reprosum, set rel_diff_max
logical :: reprosum_recompute ! setup reprosum, recompute if tolerance exceeded
character(LEN=CS) :: tfreeze_option ! Freezing point calculation
integer :: glc_nec ! number of elevation classes in the land component for lnd->glc
character(LEN=CS) :: wv_sat_scheme
real(R8) :: wv_sat_transition_start
logical :: wv_sat_use_tables
real(R8) :: wv_sat_table_spacing
type(ShrWVSatTableSpec) :: liquid_spec
type(ShrWVSatTableSpec) :: ice_spec
type(ShrWVSatTableSpec) :: mixed_spec
integer :: localPet, rootpe_med
integer , parameter :: ens1=1 ! use first instance of ensemble only
integer , parameter :: fix1=1 ! temporary hard-coding to first ensemble, needs to be fixed
real(R8) , parameter :: epsilo = shr_const_mwwv/shr_const_mwdair
character(len=*) , parameter :: subname = '(InitAttributes)'
!----------------------------------------------------------
rc = ESMF_SUCCESS
call ESMF_LogWrite(trim(subname)//": called", ESMF_LOGMSG_INFO)
call shr_log_setLogunit(logunit)
!----------------------------------------------------------
! Initialize options for reproducible sums
! TODO: this needs to be moved out of here
!----------------------------------------------------------
call NUOPC_CompAttributeGet(driver, name="reprosum_use_ddpdd", value=cvalue, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
read(cvalue,*) reprosum_use_ddpdd
call NUOPC_CompAttributeGet(driver, name="reprosum_diffmax", value=cvalue, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
read(cvalue,*) reprosum_diffmax
call NUOPC_CompAttributeGet(driver, name="reprosum_recompute", value=cvalue, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
read(cvalue,*) reprosum_recompute
call shr_reprosum_setopts(repro_sum_use_ddpdd_in=reprosum_use_ddpdd, &
repro_sum_rel_diff_max_in=reprosum_diffmax, repro_sum_recompute_in=reprosum_recompute)
!----------------------------------------------------------
! Initialize freezing point calculation for all components
! TODO: this needs to be moved out of here
!----------------------------------------------------------
call NUOPC_CompAttributeGet(driver, name="tfreeze_option", value=tfreeze_option, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
call shr_frz_freezetemp_init(tfreeze_option, maintask)
call NUOPC_CompAttributeGet(driver, name='cpl_rootpe', value=cvalue, rc=rc)
read(cvalue, *) rootpe_med
if (chkerr(rc,__LINE__,u_FILE_u)) return
call ESMF_GridCompGet(driver, localPet=localPet, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
!----------------------------------------------------------
! Initialize glc_elevclass_mod module variables
!----------------------------------------------------------
! This must be called on all processors of the driver
call NUOPC_CompAttributeGet(driver, name='glc_nec', value=cvalue, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
read(cvalue,*) glc_nec
call glc_elevclass_init(glc_nec, logunit=logunit)
!----------------------------------------------------------
! Initialize water vapor info
!----------------------------------------------------------
! TODO: this does not seem to belong here - where should it go?
call NUOPC_CompAttributeGet(driver, name="wv_sat_scheme", value=wv_sat_scheme, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
if (.not. shr_wv_sat_valid_idx(shr_wv_sat_get_scheme_idx(trim(wv_sat_scheme)))) then
call shr_sys_abort(subname//': "'//trim(wv_sat_scheme)//'" is not a recognized saturation vapor pressure scheme name')
end if
if (.not. shr_wv_sat_set_default(wv_sat_scheme)) then
call shr_sys_abort('Invalid wv_sat_scheme.')
end if
call NUOPC_CompAttributeGet(driver, name="wv_sat_transition_start", value=cvalue, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
read(cvalue,*) wv_sat_transition_start
call shr_assert_in_domain(wv_sat_transition_start, &
ge=0._R8, le=40._R8, &
varname="wv_sat_transition_start", msg="Invalid transition temperature range.")
call NUOPC_CompAttributeGet(driver, name="wv_sat_use_tables", value=cvalue, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
read(cvalue,*) wv_sat_use_tables
call NUOPC_CompAttributeGet(driver, name="wv_sat_table_spacing", value=cvalue, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
read(cvalue,*) wv_sat_table_spacing
! A transition range averaging method in CAM is only valid for:
! -40 deg C <= T <= 0 deg C
! shr_wv_sat_mod itself checks for values with the wrong sign, but we
! have to check that the range is no more than 40 deg C here. Even
! though this is a CAM-specific restriction, it's not really likely
! that any other parameterization will be dealing with mixed-phase
! water below 40 deg C anyway.
call shr_wv_sat_init(shr_const_tkfrz, shr_const_tktrip, wv_sat_transition_start, epsilo, errstring)
if (errstring /= "") then
call shr_sys_abort('shr_wv_sat_init: '//trim(errstring))
end if
! The below produces internal lookup tables in the range 175-374K for
! liquid water, and 125-274K for ice, with a resolution set by the
! option wv_sat_table_spacing.
! In theory these ranges could be specified in the namelist, but in
! practice users will want to change them *very* rarely if ever, which
! is why only the spacing is in the namelist.
if (wv_sat_use_tables) then
liquid_spec = ShrWVSatTableSpec(ceiling(200._R8/wv_sat_table_spacing), 175._R8, wv_sat_table_spacing)
ice_spec = ShrWVSatTableSpec(ceiling(150._R8/wv_sat_table_spacing), 125._R8, wv_sat_table_spacing)
mixed_spec = ShrWVSatTableSpec(ceiling(250._R8/wv_sat_table_spacing), 125._R8, wv_sat_table_spacing)
call shr_wv_sat_make_tables(liquid_spec, ice_spec, mixed_spec)
end if
end subroutine InitAttributes
!================================================================================
subroutine CheckAttributes( driver, rc )
! !DESCRIPTION: Check that input driver config values have reasonable values
use ESMF , only : ESMF_GridComp, ESMF_SUCCESS, ESMF_LogWrite, ESMF_LOGMSG_INFO
use NUOPC , only : NUOPC_CompAttributeGet
! !INPUT/OUTPUT PARAMETERS:
type(ESMF_GridComp) , intent(inout) :: driver
integer , intent(out) :: rc
!----- local -----
character(len=CS) :: logFilePostFix ! postfix for output log files
character(len=CL) :: outPathRoot ! root for output log files
character(len=CS) :: cime_model
character(len=*), parameter :: subname = '(driver_attributes_check) '
!-------------------------------------------------------------------------------
rc = ESMF_SUCCESS
call ESMF_LogWrite(trim(subname)//": called", ESMF_LOGMSG_INFO)
call NUOPC_CompAttributeGet(driver, name="cime_model", value=cime_model, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
if ( trim(cime_model) /= 'cesm' .and. trim(cime_model) /= 'ufs') then
call shr_sys_abort( subname//': cime_model must be set to cesm or ufs, aborting')
end if
! --- LogFile ending name -----
call NUOPC_CompAttributeGet(driver, name="logFilePostFix", value=logFilePostFix, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
if ( len_trim(logFilePostFix) == 0 ) then
call shr_sys_abort( subname//': logFilePostFix must be set to something not blank' )
end if
! --- Output path root directory -----
call NUOPC_CompAttributeGet(driver, name="outPathRoot", value=outPathRoot, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
if ( len_trim(outPathRoot) == 0 ) then
call shr_sys_abort( subname//': outPathRoot must be set' )
end if
if ( index(outPathRoot, "/", back=.true.) /= len_trim(outPathRoot) ) then
call shr_sys_abort( subname//': outPathRoot must end with a slash' )
end if
end subroutine CheckAttributes
!===============================================================================
subroutine AddAttributes(gcomp, driver, config, compid, compname, inst_suffix, nthrds, rc)
! Add specific set of attributes to components from driver attributes
use ESMF , only : ESMF_GridComp, ESMF_Config, ESMF_LogWrite, ESMF_LOGMSG_INFO, ESMF_SUCCESS
use ESMF , only : ESMF_LogFoundAllocError, ESMF_ConfigGetLen, ESMF_ConfigGetAttribute
use NUOPC , only : NUOPC_CompAttributeAdd, NUOPC_CompAttributeGet, NUOPC_CompAttributeSet
! input/output parameters
type(ESMF_GridComp) , intent(inout) :: gcomp
type(ESMF_GridComp) , intent(in) :: driver
type(ESMF_Config) , intent(inout) :: config
integer , intent(in) :: compid
character(len=*) , intent(in) :: compname
character(len=*) , intent(in) :: inst_suffix
integer , intent(in) :: nthrds
integer , intent(inout) :: rc
! local variables
integer :: inst_index
logical :: computetask
character(len=CL) :: cvalue
character(len=CS) :: attribute
character(len=*), parameter :: subname = "(esm.F90:AddAttributes)"
!-------------------------------------------
computetask = .false.
rc = ESMF_Success
call ESMF_LogWrite(trim(subname)//": called", ESMF_LOGMSG_INFO)
call shr_log_setLogunit(logunit)
!------
! Add compid to gcomp attributes
!------
write(cvalue,*) compid
call NUOPC_CompAttributeAdd(gcomp, attrList=(/'MCTID'/), rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
call NUOPC_CompAttributeSet(gcomp, name='MCTID', value=trim(cvalue), rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
!------
! Add driver restart flag to gcomp attributes
!------
attribute = 'read_restart'
call NUOPC_CompAttributeGet(driver, name=trim(attribute), isPresent=computetask, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
if(.not. computetask) return
call NUOPC_CompAttributeGet(driver, name=trim(attribute), value=cvalue, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
call NUOPC_CompAttributeAdd(gcomp, (/trim(attribute)/), rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
call NUOPC_CompAttributeSet(gcomp, name=trim(attribute), value=trim(cvalue), rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
!------
! Add component specific attributes
!------
call ReadAttributes(gcomp, config, trim(compname)//"_attributes::", rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
call ReadAttributes(gcomp, config, "ALLCOMP_attributes::", rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
call ESMF_LogWrite(trim(subname)//": call Readattributes for"//trim(compname), ESMF_LOGMSG_INFO)
call ReadAttributes(gcomp, config, trim(compname)//"_modelio::", rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) then
print *,__FILE__,__LINE__,"ERROR reading ",trim(compname)," modelio from runconfig"
return
endif
call ReadAttributes(gcomp, config, "CLOCK_attributes::", rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
!------
! Add mediator specific attributes - if component is mediator
!------
if (compname == 'MED') then
call ReadAttributes(gcomp, config, "MED_attributes::", rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
endif
!------
! Add multi-instance specific attributes
!------
call NUOPC_CompAttributeAdd(gcomp, attrList=(/'inst_index'/), rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
! add inst_index attribute (inst_index is not required for cime internal components)
! for now hard-wire inst_index to 1
inst_index = 1
write(cvalue,*) inst_index
call NUOPC_CompAttributeSet(gcomp, name='inst_index', value=trim(cvalue), rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
! add inst_suffix attribute
if (len_trim(inst_suffix) > 0) then
call NUOPC_CompAttributeAdd(gcomp, attrList=(/'inst_suffix'/), rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
call NUOPC_CompAttributeSet(gcomp, name='inst_suffix', value=inst_suffix, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
end if
! Add the nthreads attribute
call NUOPC_CompAttributeAdd(gcomp, attrList=(/'nthreads'/), rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
write(cvalue, *) nthrds
call NUOPC_CompAttributeSet(gcomp, name='nthreads', value=cvalue, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
!------
! Add single column and single point attributes
!------
call esm_set_single_column_attributes(compname, gcomp, rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
end subroutine AddAttributes
!================================================================================
subroutine ReadAttributes(gcomp, config, label, relaxedflag, formatprint, rc)
use ESMF , only : ESMF_GridComp, ESMF_Config, ESMF_LogWrite, ESMF_LOGMSG_INFO, ESMF_SUCCESS
use NUOPC , only : NUOPC_FreeFormatCreate, NUOPC_CompAttributeIngest
use NUOPC , only : NUOPC_FreeFormatDestroy, NUOPC_FreeFormat
! input/output arguments
type(ESMF_GridComp) , intent(inout) :: gcomp
type(ESMF_Config) , intent(in) :: config
character(len=*) , intent(in) :: label
logical , intent(in), optional :: relaxedflag
logical , intent(in), optional :: formatprint
integer , intent(inout) :: rc
! local variables
type(NUOPC_FreeFormat) :: attrFF
character(len=*), parameter :: subname = "(esm.F90:ReadAttributes)"
!-------------------------------------------
rc = ESMF_SUCCESS
call shr_log_setLogunit(logunit)
if (present(relaxedflag)) then
attrFF = NUOPC_FreeFormatCreate(config, label=trim(label), relaxedflag=.true., rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
else
attrFF = NUOPC_FreeFormatCreate(config, label=trim(label), rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
end if
call NUOPC_CompAttributeIngest(gcomp, attrFF, addFlag=.true., rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
#ifdef DEBUG
! if (present (formatprint)) then
! call pretty_print_nuopc_freeformat(attrFF, trim(label)//' attributes', rc=rc)
! if (chkerr(rc,__LINE__,u_FILE_u)) return
! end if
#endif
call NUOPC_FreeFormatDestroy(attrFF, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
end subroutine ReadAttributes
!================================================================================
subroutine InitAdvertize(driver, importState, exportState, clock, rc)
! This empty InitAdvertise is needed because it overrides the behavior
! of the default InitAdvertise inside the generic NUOPC_Driver.F90. The
! default behavior tries to mirror the fields up the hierarchy (i.e., up
! to the ensemble driver). This would be used if we needed to
! communicate between the ensemble members. Since we do not need that
! right now, we turn it off with this empty subroutine.
use ESMF, only : ESMF_GridComp, ESMF_State, ESMF_Clock
use ESMF, only : ESMF_LogWrite, ESMF_SUCCESS, ESMF_LOGMSG_INFO
! input/output variables
type(ESMF_GridComp) :: driver
type(ESMF_State) :: importState, exportState
type(ESMF_Clock) :: clock
integer, intent(out) :: rc
! local variables
character(len=*), parameter :: subname = "(esm.F90:InitAdvertize)"
!---------------------------------------
rc = ESMF_SUCCESS
call ESMF_LogWrite(trim(subname)//": called", ESMF_LOGMSG_INFO)
end subroutine InitAdvertize
!================================================================================
subroutine esm_init_pelayout(driver, maxthreads, rc)
use ESMF , only : ESMF_GridComp, ESMF_GridCompGet, ESMF_VM, ESMF_VMGet
use ESMF , only : ESMF_LogWrite, ESMF_SUCCESS, ESMF_LOGMSG_INFO, ESMF_Config
use ESMF , only : ESMF_ConfigGetLen, ESMF_LogFoundAllocError, ESMF_ConfigGetAttribute
use ESMF , only : ESMF_RC_NOT_VALID, ESMF_LogSetError, ESMF_Info, ESMF_InfoSet
use ESMF , only : ESMF_GridCompIsPetLocal, ESMF_MethodAdd, ESMF_UtilStringLowerCase
use ESMF , only : ESMF_InfoCreate, ESMF_InfoDestroy
use NUOPC , only : NUOPC_CompAttributeGet
use NUOPC_Driver , only : NUOPC_DriverAddComp
#ifndef NO_MPI2
use mpi , only : MPI_COMM_NULL, mpi_comm_size
#endif
use m_MCTWorld , only : mct_world_init => init
#ifdef MED_PRESENT
use med_internalstate_mod , only : med_id
use med , only : MedSetServices => SetServices
#ifdef ESMF_AWARE_THREADING
use med , only : MEDSetVM => SetVM
#endif
#endif
#ifdef ATM_PRESENT
use atm_comp_nuopc , only : ATMSetServices => SetServices
#ifdef ESMF_AWARE_THREADING
use atm_comp_nuopc , only : ATMSetVM => SetVM
#endif
#endif
#ifdef ICE_PRESENT
use ice_comp_nuopc , only : ICESetServices => SetServices
#ifdef ESMF_AWARE_THREADING
use ice_comp_nuopc , only : ICESetVM => SetVM
#endif
#endif
#ifdef LND_PRESENT
use lnd_comp_nuopc , only : LNDSetServices => SetServices
#ifdef ESMF_AWARE_THREADING
use lnd_comp_nuopc , only : LNDSetVM => SetVM
#endif
#endif
#ifdef OCN_PRESENT
use ocn_comp_nuopc , only : OCNSetServices => SetServices
#ifdef ESMF_AWARE_THREADING
use ocn_comp_nuopc , only : OCNSetVM => SetVM
#endif
#endif
#ifdef WAV_PRESENT
use wav_comp_nuopc , only : WAVSetServices => SetServices
#ifdef ESMF_AWARE_THREADING
use wav_comp_nuopc , only : WAVSetVM => SetVM
#endif
#endif
#ifdef ROF_PRESENT
use rof_comp_nuopc , only : ROFSetServices => SetServices
#ifdef ESMF_AWARE_THREADING
use rof_comp_nuopc , only : ROFSetVM => SetVM
#endif
#endif
#ifdef GLC_PRESENT
use glc_comp_nuopc , only : GLCSetServices => SetServices
#ifdef ESMF_AWARE_THREADING
use glc_comp_nuopc , only : GLCSetVM => SetVM
#endif
#endif
#ifdef NO_MPI2
include 'mpif.h'
#endif
! input/output variables
type(ESMF_GridComp) :: driver
integer, intent(out) :: maxthreads ! maximum number of threads any component
integer, intent(out) :: rc
! local variables
type(ESMF_GridComp) :: child
type(ESMF_VM) :: vm
type(ESMF_Config) :: config
type(ESMF_Info) :: info
integer :: PetCount
integer :: ComponentCount
integer :: ntasks, rootpe, nthrds, stride
integer :: ntask
integer :: i
integer :: stat
character(len=32), allocatable :: compLabels(:)
character(CS) :: namestr
character(CL) :: msgstr
integer, allocatable :: petlist(:)
integer, pointer :: comms(:), comps(:)
integer :: Global_Comm
logical :: isPresent
integer, allocatable :: comp_comm_iam(:)
logical, allocatable :: comp_iamin(:)
character(len=5) :: inst_suffix
character(CL) :: cvalue
logical :: found_comp
integer :: rank, nprocs, ierr
character(len=*), parameter :: subname = "(esm_pelayout.F90:esm_init_pelayout)"
!---------------------------------------
call shr_log_setLogunit(logunit)
rc = ESMF_SUCCESS
call ESMF_LogWrite(trim(subname)//": called", ESMF_LOGMSG_INFO)
call ESMF_GridCompGet(driver, vm=vm, config=config, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
call ReadAttributes(driver, config, "PELAYOUT_attributes::", rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
call ESMF_VMGet(vm, petCount=petCount, mpiCommunicator=Global_Comm, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
componentCount = ESMF_ConfigGetLen(config,label="component_list:", rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
allocate(compLabels(componentCount), stat=stat)
if (ESMF_LogFoundAllocError(statusToCheck=stat, msg="Allocation of compLabels failed.", &
line=__LINE__, file=u_FILE_u, rcToReturn=rc)) return
allocate(comp_iamin(componentCount), stat=stat)
if (ESMF_LogFoundAllocError(statusToCheck=stat, msg="Allocation of compLabels failed.", &
line=__LINE__, file=u_FILE_u, rcToReturn=rc)) return
allocate(comp_comm_iam(componentCount), stat=stat)
if (ESMF_LogFoundAllocError(statusToCheck=stat, msg="Allocation of compLabels failed.", &
line=__LINE__, file=u_FILE_u, rcToReturn=rc)) return
call ESMF_ConfigGetAttribute(config, valueList=compLabels, label="component_list:", count=componentCount, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
call NUOPC_CompAttributeGet(driver, name="inst_suffix", isPresent=isPresent, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
if (isPresent) then
call NUOPC_CompAttributeGet(driver, name="inst_suffix", value=inst_suffix, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
else
inst_suffix = ""
endif
allocate(comms(componentCount+1), comps(componentCount+1))
comps(1) = 1
comms = MPI_COMM_NULL
comms(1) = Global_Comm
maxthreads = 1
do i=1,componentCount
namestr = ESMF_UtilStringLowerCase(compLabels(i))
if (namestr == 'med') namestr = 'cpl'
call NUOPC_CompAttributeGet(driver, name=trim(namestr)//'_nthreads', value=cvalue, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
read(cvalue,*) nthrds
if(nthrds > maxthreads) maxthreads = nthrds
enddo
do i=1,componentCount
namestr = ESMF_UtilStringLowerCase(compLabels(i))
if (namestr == 'med') namestr = 'cpl'
call NUOPC_CompAttributeGet(driver, name=trim(namestr)//'_ntasks', value=cvalue, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
read(cvalue,*) ntasks
if (ntasks < 0 .or. ntasks > PetCount) then
write (msgstr, *) "Invalid NTASKS value specified for component: ",namestr, ' ntasks: ',ntasks, petcount
call ESMF_LogSetError(ESMF_RC_NOT_VALID, msg=msgstr, line=__LINE__, file=__FILE__, rcToReturn=rc)
return
endif
call NUOPC_CompAttributeGet(driver, name=trim(namestr)//'_nthreads', value=cvalue, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
read(cvalue,*) nthrds
info = ESMF_InfoCreate(rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
call ESMF_InfoSet(info, key="/NUOPC/Hint/PePerPet/MaxCount", value=nthrds, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
call NUOPC_CompAttributeGet(driver, name=trim(namestr)//'_rootpe', value=cvalue, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
read(cvalue,*) rootpe
if (rootpe < 0 .or. rootpe > PetCount) then
write (msgstr, *) "Invalid Rootpe value specified for component: ",namestr, ' rootpe: ',rootpe
call ESMF_LogSetError(ESMF_RC_NOT_VALID, msg=msgstr, line=__LINE__, file=__FILE__, rcToReturn=rc)
return
endif
if(rootpe+ntasks > PetCount) then
write (msgstr, *) "Invalid pelayout value specified for component: ",namestr, ' rootpe+ntasks: ',rootpe+ntasks
call ESMF_LogSetError(ESMF_RC_NOT_VALID, msg=msgstr, line=__LINE__, file=__FILE__, rcToReturn=rc)
return
endif
call NUOPC_CompAttributeGet(driver, name=trim(namestr)//'_pestride', value=cvalue, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
read(cvalue,*) stride
if (stride < 1 .or. rootpe+(ntasks-1)*stride > PetCount) then
write (msgstr, *) "Invalid pestride value specified for component: ",namestr,&
' rootpe: ',rootpe, ' pestride: ', stride, ' ntasks: ',ntasks, ' PetCount: ', PetCount
call ESMF_LogSetError(ESMF_RC_NOT_VALID, msg=msgstr, line=__LINE__, file=__FILE__, rcToReturn=rc)
return
endif
if (allocated(petlist)) then
#ifdef ESMF_AWARE_THREADING
if(size(petlist) .ne. ntasks*nthrds) then
#else
if(size(petlist) .ne. ntasks) then
#endif
deallocate(petlist)
endif
endif
if(.not. allocated(petlist)) then
#ifdef ESMF_AWARE_THREADING
allocate(petlist(ntasks*nthrds))
#else
allocate(petlist(ntasks))
#endif