-
Notifications
You must be signed in to change notification settings - Fork 23
/
mod_esmf_atm_rcm.F90
2927 lines (2926 loc) · 117 KB
/
mod_esmf_atm_rcm.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
!=======================================================================
! Regional Earth System Model (RegESM)
! Copyright (c) 2013-2019 Ufuk Turuncoglu
! Licensed under the MIT License.
!=======================================================================
#define FILENAME "mod_esmf_atm_rcm.F90"
!
!-----------------------------------------------------------------------
! ATM gridded component code
!-----------------------------------------------------------------------
!
module mod_esmf_atm
!
!-----------------------------------------------------------------------
! Used module declarations
!-----------------------------------------------------------------------
!
use ESMF
use NUOPC
use NUOPC_Model, &
NUOPC_SetServices => SetServices, &
NUOPC_Label_Advance => label_Advance, &
NUOPC_Label_DataInitialize => label_DataInitialize, &
NUOPC_Label_SetClock => label_SetClock, &
NUOPC_Label_CheckImport => label_CheckImport
!
use mod_types
use mod_shared
!
use mod_regcm_interface, only : &
ATM_Initialize => RCM_initialize, &
ATM_Run => RCM_run, &
ATM_Finalize => RCM_finalize
!
implicit none
private
!
!-----------------------------------------------------------------------
! Public subroutines
!-----------------------------------------------------------------------
!
public :: ATM_SetServices
!
contains
!
subroutine ATM_SetServices(gcomp, rc)
implicit none
!
!-----------------------------------------------------------------------
! Imported variable declarations
!-----------------------------------------------------------------------
!
type(ESMF_GridComp) :: gcomp
integer, intent(out) :: rc
!
rc = ESMF_SUCCESS
!
!-----------------------------------------------------------------------
! Register NUOPC generic routines
!-----------------------------------------------------------------------
!
call NUOPC_CompDerive(gcomp, NUOPC_SetServices, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=FILENAME)) return
!
!-----------------------------------------------------------------------
! Register initialize routines (Phase 1 and Phase 2)
!-----------------------------------------------------------------------
!
call NUOPC_CompSetEntryPoint(gcomp, &
methodflag=ESMF_METHOD_INITIALIZE, &
phaseLabelList=(/"IPDv00p1"/), &
userRoutine=ATM_SetInitializeP1, &
rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=FILENAME)) return
!
call NUOPC_CompSetEntryPoint(gcomp, &
methodflag=ESMF_METHOD_INITIALIZE, &
phaseLabelList=(/"IPDv00p2"/), &
userRoutine=ATM_SetInitializeP2, &
rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=FILENAME)) return
!
!-----------------------------------------------------------------------
! Attach phase independent specializing methods
! Setting the slow and fast model clocks
!-----------------------------------------------------------------------
!
call NUOPC_CompSpecialize(gcomp, &
specLabel=NUOPC_Label_DataInitialize, &
specRoutine=ATM_DataInit, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=FILENAME)) return
!
call NUOPC_CompSpecialize(gcomp, specLabel=NUOPC_Label_SetClock, &
specRoutine=ATM_SetClock, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=FILENAME)) return
!
call NUOPC_CompSpecialize(gcomp, &
specLabel=NUOPC_Label_CheckImport, &
specPhaseLabel="RunPhase1", &
specRoutine=ATM_CheckImport, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=FILENAME)) return
!
call NUOPC_CompSpecialize(gcomp, specLabel=NUOPC_Label_Advance, &
specRoutine=ATM_ModelAdvance, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=FILENAME)) return
!
!-----------------------------------------------------------------------
! Register finalize routine
!-----------------------------------------------------------------------
!
call ESMF_GridCompSetEntryPoint(gcomp, &
methodflag=ESMF_METHOD_FINALIZE, &
userRoutine=ATM_SetFinalize, &
rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=FILENAME)) return
!
end subroutine ATM_SetServices
!
subroutine ATM_SetInitializeP1(gcomp, importState, exportState, &
clock, rc)
implicit none
!
!-----------------------------------------------------------------------
! Imported variable declarations
!-----------------------------------------------------------------------
!
type(ESMF_GridComp) :: gcomp
type(ESMF_State) :: importState
type(ESMF_State) :: exportState
type(ESMF_Clock) :: clock
integer, intent(out) :: rc
!
!-----------------------------------------------------------------------
! Local variable declarations
!-----------------------------------------------------------------------
!
integer :: i
!
rc = ESMF_SUCCESS
!
!-----------------------------------------------------------------------
! Set import fields
!-----------------------------------------------------------------------
!
do i = 1, ubound(models(Iatmos)%importField, dim=1)
call NUOPC_Advertise(importState, &
StandardName=trim(models(Iatmos)%importField(i)%long_name),&
name=trim(models(Iatmos)%importField(i)%short_name), rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=FILENAME)) return
end do
!
!-----------------------------------------------------------------------
! Set export fields
!-----------------------------------------------------------------------
!
do i = 1, ubound(models(Iatmos)%exportField, dim=1)
call NUOPC_Advertise(exportState, &
StandardName=trim(models(Iatmos)%exportField(i)%long_name),&
name=trim(models(Iatmos)%exportField(i)%short_name), rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=FILENAME)) return
end do
!
end subroutine ATM_SetInitializeP1
!
subroutine ATM_SetInitializeP2(gcomp, importState, exportState, &
clock, rc)
!
!-----------------------------------------------------------------------
! Used module declarations
!-----------------------------------------------------------------------
!
use mod_update, only : ATM_Allocate => RCM_Allocate
use mod_runparams, only : dtsrf
!
implicit none
!
!-----------------------------------------------------------------------
! Imported variable declarations
!-----------------------------------------------------------------------
!
type(ESMF_GridComp) :: gcomp
type(ESMF_State) :: importState
type(ESMF_State) :: exportState
type(ESMF_Clock) :: clock
integer, intent(out) :: rc
!
!-----------------------------------------------------------------------
! Local variable declarations
!-----------------------------------------------------------------------
!
real*8 :: tstr
integer :: comm, localPet, petCount
type(ESMF_VM) :: vm
type(ESMF_Time) :: startTime, currTime
!
rc = ESMF_SUCCESS
!
!-----------------------------------------------------------------------
! Get gridded component
!-----------------------------------------------------------------------
!
call ESMF_GridCompGet(gcomp, vm=vm, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=FILENAME)) return
!
call ESMF_VMGet(vm, localPet=localPet, petCount=petCount, &
mpiCommunicator=comm, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=FILENAME)) return
!
!-----------------------------------------------------------------------
! Initialize the gridded component
!-----------------------------------------------------------------------
!
call ATM_Initialize(mpiCommunicator=comm)
call ATM_Allocate()
!
!-----------------------------------------------------------------------
! Set-up grid and load coordinate data
!-----------------------------------------------------------------------
!
call ATM_SetGridArrays2d(gcomp, localPet, rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=FILENAME)) return
!
if (models(Icopro)%modActive) then
call ATM_SetGridArrays3d(gcomp, localPet, rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=FILENAME)) return
end if
!
!-----------------------------------------------------------------------
! Set-up fields and register to import/export states
!-----------------------------------------------------------------------
!
call ATM_SetStates2d(gcomp, rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=FILENAME)) return
!
if (models(Icopro)%modActive) then
call ATM_SetStates3d(gcomp, rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=FILENAME)) return
end if
!
end subroutine ATM_SetInitializeP2
!
subroutine ATM_DataInit(gcomp, rc)
!
!-----------------------------------------------------------------------
! Used module declarations
!-----------------------------------------------------------------------
!
use mod_runparams, only : dtsrf
!
implicit none
!
!-----------------------------------------------------------------------
! Imported variable declarations
!-----------------------------------------------------------------------
!
type(ESMF_GridComp) :: gcomp
integer, intent(out) :: rc
!
!-----------------------------------------------------------------------
! Local variable declarations
!-----------------------------------------------------------------------
!
integer :: localPet, petCount, phase
real(ESMF_KIND_R8) :: tstr
character(ESMF_MAXSTR) :: str1, str2
!
type(ESMF_VM) :: vm
type(ESMF_Clock) :: clock
type(ESMF_Time) :: currTime
type(ESMF_TimeInterval) :: timeStep
!
!-----------------------------------------------------------------------
! Get gridded component clock
!-----------------------------------------------------------------------
!
call ESMF_GridCompGet(gcomp, vm=vm, clock=clock, &
currentPhase=phase, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=FILENAME)) return
!
call ESMF_VMGet(vm, localPet=localPet, petCount=petCount, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=FILENAME)) return
!
call ESMF_ClockGet(clock, currTime=currTime, &
timeStep=timeStep, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=FILENAME)) return
!
!-----------------------------------------------------------------------
! Put export fields (only for initial and restart run)
!-----------------------------------------------------------------------
!
if (restarted .and. currTime == esmRestartTime) then
!
!-----------------------------------------------------------------------
! Debug: write time information
!-----------------------------------------------------------------------
!
if (debugLevel >= 0 .and. localPet == 0) then
call ESMF_TimeGet(currTime, &
timeStringISOFrac=str1, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=FILENAME)) return
!
call ESMF_TimeGet(currTime+timeStep, &
timeStringISOFrac=str2, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=FILENAME)) return
!
if (debugLevel == 0) then
write(*,10) trim(str1), trim(str2), phase
else
write(*,20) trim(str1), trim(str2), phase, 0.0d0, dtsrf
end if
end if
!
!-----------------------------------------------------------------------
! Run ATM component (run only one time step to fill variables)
!-----------------------------------------------------------------------
!
call ATM_Run(0.0d0, dtsrf)
!
!-----------------------------------------------------------------------
! Put export fields
!-----------------------------------------------------------------------
!
call ATM_Put(gcomp, rc=rc)
!
end if
!
!-----------------------------------------------------------------------
! Format definition
!-----------------------------------------------------------------------
!
10 format(' Running ATM Component: ',A,' --> ',A,' Phase: ',I1,' +')
20 format(' Running ATM Component: ',A,' --> ',A,' Phase: ',I1, &
' [',F15.2, '-', F15.2, '] +')
!
end subroutine ATM_DataInit
!
subroutine ATM_SetClock(gcomp, rc)
!
!-----------------------------------------------------------------------
! Used module declarations
!-----------------------------------------------------------------------
!
use mod_dynparam , only : calendar
use mod_runparams, only : idate0, idate1, idate2, dtsec
use mod_date, only : split_idate
!
implicit none
!
!-----------------------------------------------------------------------
! Imported variable declarations
!-----------------------------------------------------------------------
!
type(ESMF_GridComp) :: gcomp
integer, intent(out) :: rc
!
!-----------------------------------------------------------------------
! Local variable declarations
!-----------------------------------------------------------------------
!
integer :: fac1, fac2, maxdiv
integer :: ref_year, str_year, end_year
integer :: ref_month, str_month, end_month
integer :: ref_day, str_day, end_day
integer :: ref_hour, str_hour, end_hour
integer :: ref_minute, str_minute, end_minute
integer :: ref_second, str_second, end_second
real*8 :: tstr
!
type(ESMF_Clock) :: cmpClock
type(ESMF_TimeInterval) :: timeStep
type(ESMF_Time) :: currTime, startTime
type(ESMF_Time) :: cmpRefTime, cmpStartTime, cmpStopTime
type(ESMF_Calendar) :: cal
!
rc = ESMF_SUCCESS
!
!-----------------------------------------------------------------------
! Create gridded component clock
!-----------------------------------------------------------------------
!
if (calendar == 'gregorian') then
cal = ESMF_CalendarCreate(ESMF_CALKIND_GREGORIAN, &
name=trim(calendar), &
rc=rc)
else if (calendar == 'noleap' .or. calendar == '365_day') then
cal = ESMF_CalendarCreate(ESMF_CALKIND_NOLEAP, &
name=trim(calendar), &
rc=rc)
else if (calendar == '360_day') then
cal = ESMF_CalendarCreate(ESMF_CALKIND_360DAY, &
name=trim(calendar), &
rc=rc)
else
cal = ESMF_CalendarCreate(ESMF_CALKIND_GREGORIAN, &
name=trim(calendar), &
rc=rc)
end if
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=FILENAME)) return
!
!-----------------------------------------------------------------------
! Set reference time
!-----------------------------------------------------------------------
!
call split_idate(idate0, ref_year, ref_month, ref_day, ref_hour)
ref_minute = 0
ref_second = 0
!
call ESMF_TimeSet(cmpRefTime, &
yy=ref_year, &
mm=ref_month, &
dd=ref_day, &
h=ref_hour, &
m=ref_minute, &
s=ref_second, &
calendar=cal, &
rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=FILENAME)) return
!
!-----------------------------------------------------------------------
! Set start time
!-----------------------------------------------------------------------
!
call split_idate(idate1, str_year, str_month, str_day, str_hour)
str_minute = 0
str_second = 0
!
call ESMF_TimeSet (cmpStartTime, &
yy=str_year, &
mm=str_month, &
dd=str_day, &
h=str_hour, &
m=str_minute, &
s=str_second, &
calendar=cal, &
rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=FILENAME)) return
!
!-----------------------------------------------------------------------
! Set stop time
!-----------------------------------------------------------------------
!
call split_idate(idate2, end_year, end_month, end_day, end_hour)
end_minute = 0
end_second = 0
!
call ESMF_TimeSet(cmpStopTime, &
yy=end_year, &
mm=end_month, &
dd=end_day, &
h=end_hour, &
m=end_minute, &
s=end_second, &
calendar=cal, &
rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=FILENAME)) return
!
!-----------------------------------------------------------------------
! Get component clock
!-----------------------------------------------------------------------
!
call ESMF_GridCompGet(gcomp, clock=cmpClock, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=FILENAME)) return
!
call ESMF_ClockGet(cmpClock, timeStep=timeStep, &
currTime=currTime, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=FILENAME)) return
!
!-----------------------------------------------------------------------
! Compare driver time vs. component time
!-----------------------------------------------------------------------
!
if (restarted) then
startTime = esmRestartTime
else
startTime = esmStartTime
end if
!
if (cmpStartTime /= startTime) then
call ESMF_TimePrint(cmpStartTime, options="string", rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=FILENAME)) return
!
call ESMF_TimePrint(startTime, options="string", rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=FILENAME)) return
!
call ESMF_LogSetError(ESMF_FAILURE, rcToReturn=rc, &
msg='ESM and ATM start times do not match: '// &
'please check the config files')
return
end if
!
if (cmpStopTime /= esmStopTime) then
call ESMF_TimePrint(cmpStopTime, options="string", rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=FILENAME)) return
!
call ESMF_TimePrint(esmStopTime, options="string", rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=FILENAME)) return
!
call ESMF_LogSetError(ESMF_FAILURE, rcToReturn=rc, &
msg='ESM and ATM stop times do not match: '// &
'please check the config files')
return
end if
!
if (cal /= esmCal) then
call ESMF_CalendarPrint(cal, options="calkindflag", rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=FILENAME)) return
!
call ESMF_CalendarPrint(esmCal, options="calkindflag", rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=FILENAME)) return
!
call ESMF_LogSetError(ESMF_FAILURE, rcToReturn=rc, &
msg='ESM and ATM calendars do not match: '// &
'please check the config files')
return
end if
!
!-----------------------------------------------------------------------
! Modify component clock time step
!-----------------------------------------------------------------------
!
fac1 = maxval(connectors(Iatmos,:)%divDT, &
mask=models(:)%modActive)
fac2 = maxval(connectors(:,Iatmos)%divDT, &
mask=models(:)%modActive)
maxdiv = max(fac1, fac2)
!
call ESMF_ClockSet(cmpClock, name='atm_clock', &
timeStep=timeStep/maxdiv, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=FILENAME)) return
!
end subroutine ATM_SetClock
!
subroutine ATM_CheckImport(gcomp, rc)
implicit none
!
!-----------------------------------------------------------------------
! Imported variable declarations
!-----------------------------------------------------------------------
!
type(ESMF_GridComp) :: gcomp
integer, intent(out) :: rc
!
!-----------------------------------------------------------------------
! Local variable declarations
!-----------------------------------------------------------------------
!
integer :: i, itemCount, localPet, div, rsec
logical :: atCorrectTime
character(ESMF_MAXSTR), allocatable :: itemNameList(:)
!
type(ESMF_VM) :: vm
type(ESMF_Time) :: currTimeCmp, currTimeDrv
type(ESMF_Time) :: strTimeCmp
type(ESMF_TimeInterval) :: timeStepCmp, timeStepDrv
type(ESMF_Clock) :: modelClock, driverClock
type(ESMF_Field) :: field
type(ESMF_State) :: importState
!
rc = ESMF_SUCCESS
!
!-----------------------------------------------------------------------
! Query component
!-----------------------------------------------------------------------
!
call ESMF_GridCompGet(gcomp, vm=vm, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=FILENAME)) return
!
call ESMF_VMGet(vm, localPet=localPet, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=FILENAME)) return
!
!-----------------------------------------------------------------------
! Query driver and model clocks
!-----------------------------------------------------------------------
!
call NUOPC_ModelGet(gcomp, driverClock=driverClock, &
modelClock=modelClock, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=FILENAME)) return
!
!-----------------------------------------------------------------------
! Get current time and time step out of the clock
!-----------------------------------------------------------------------
!
call ESMF_ClockGet(driverClock, currTime=currTimeDrv, &
timeStep=timeStepDrv, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=FILENAME)) return
!
call ESMF_ClockGet(modelClock, currTime=currTimeCmp, &
startTime=strTimeCmp, timeStep=timeStepCmp, &
rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=FILENAME)) return
!
!-----------------------------------------------------------------------
! Check import field or not?
!-----------------------------------------------------------------------
!
div = maxval(connectors(:,Iatmos)%divDT, mask=models(:)%modActive)
!
call ESMF_TimeIntervalGet(mod((currTimeCmp-strTimeCmp), &
esmTimeStep/div), &
s=rsec, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=FILENAME)) return
!
if (rsec == 0) then
!
!-----------------------------------------------------------------------
! Query component for its clock and importState
!-----------------------------------------------------------------------
!
call ESMF_GridCompGet(gcomp, importState=importState, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=FILENAME)) return
!
!-----------------------------------------------------------------------
! Get list of import fields
!-----------------------------------------------------------------------
!
call ESMF_StateGet(importState, itemCount=itemCount, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=FILENAME)) return
!
if (.not. allocated(itemNameList)) then
allocate(itemNameList(itemCount))
end if
!
call ESMF_StateGet(importState, itemNameList=itemNameList, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=FILENAME)) return
!
!-----------------------------------------------------------------------
! Check fields in the importState
!-----------------------------------------------------------------------
!
if (itemCount > 0) then
!
do i = 1, itemCount
!
call ESMF_StateGet(importState, itemName=trim(itemNameList(i)), &
field=field, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=FILENAME)) return
!
if (cplType == 1) then
atCorrectTime = NUOPC_IsAtTime(field, currTimeCmp, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=FILENAME)) return
!
call print_timestamp(field, currTimeCmp, localPet, "ATM", rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=FILENAME)) return
else
atCorrectTime = NUOPC_IsAtTime(field, currTimeCmp+timeStepCmp, &
rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=FILENAME)) return
!
call print_timestamp(field, currTimeCmp+timeStepCmp, &
localPet, "ATM", rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=FILENAME)) return
end if
!
if (.not. atCorrectTime) then
call ESMF_LogSetError(ESMF_RC_ARG_BAD, &
msg="NUOPC INCOMPATIBILITY DETECTED: "// &
"Import Fields not at correct time", &
line=__LINE__, file=FILENAME, &
rcToReturn=rc)
return
end if
!
end do
end if
!
end if
!
end subroutine ATM_CheckImport
!
subroutine ATM_SetGridArrays2d(gcomp, localPet, rc)
!
!-----------------------------------------------------------------------
! Used module declarations
!-----------------------------------------------------------------------
!
use mod_mppparam, only : ma
use mod_runparams, only : dxsq
use mod_atm_interface, only : mddom
use mod_dynparam, only : iy, jx, nproc, ide1, ide2, jde1, jde2, &
idi1, idi2, jdi1, jdi2, &
ice1, ice2, jce1, jce2
!
implicit none
!
!-----------------------------------------------------------------------
! Imported variable declarations
!-----------------------------------------------------------------------
!
type(ESMF_GridComp), intent(inout) :: gcomp
integer :: localPet
integer :: rc
!
!-----------------------------------------------------------------------
! Local variable declarations
!-----------------------------------------------------------------------
!
integer :: i, j, ii, jj, i0, j0, localDECount
integer :: cpus_per_dim(2)
!
type(ESMF_DistGrid) :: distGrid
type(ESMF_StaggerLoc) :: staggerLoc
real(ESMF_KIND_R8), pointer :: ptrX(:,:), ptrY(:,:), ptrA(:,:)
integer, pointer :: ptrM(:,:)
character (len=40) :: name
!
rc = ESMF_SUCCESS
!
!-----------------------------------------------------------------------
! Calculate number of CPUs in each direction
!-----------------------------------------------------------------------
!
if ( nproc < 4 ) then
cpus_per_dim(2) = 1
cpus_per_dim(1) = nproc
else if ( nproc >= 4 ) then
cpus_per_dim(2) = (nint(sqrt(dble(nproc)))/2)*2
if ( iy > int(1.5*dble(jx)) ) then
cpus_per_dim(2) = cpus_per_dim(2) - 1
do while ( mod(nproc,cpus_per_dim(2)) /= 0 )
cpus_per_dim(2) = cpus_per_dim(2) - 1
end do
else if ( jx > int(1.5*dble(iy)) ) then
cpus_per_dim(2) = cpus_per_dim(2) + 1
do while ( mod(nproc,cpus_per_dim(2)) /= 0 )
cpus_per_dim(2) = cpus_per_dim(2) + 1
end do
else
do while ( mod(nproc,cpus_per_dim(2)) /= 0 )
cpus_per_dim(2) = cpus_per_dim(2) + 1
end do
end if
cpus_per_dim(1) = nproc/cpus_per_dim(2)
end if
!
!-----------------------------------------------------------------------
! Create DistGrid based on model domain decomposition
!
! ESMF is basically using a right handed coordinate system, and
! using the Fortran way of using the smallest stride to the first
! dimension but RegCM not. The order of dimension is reversed
! because of this limitation.
!-----------------------------------------------------------------------
!
distGrid = ESMF_DistGridCreate(minIndex=(/ 1, 1 /), &
maxIndex=(/ iy, jx /), &
regDecomp=cpus_per_dim, &
rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=FILENAME)) return
!
!-----------------------------------------------------------------------
! Define type of stenciles used by grid (dot and cross points)
!-----------------------------------------------------------------------
!
if (.not. allocated(models(Iatmos)%mesh)) then
allocate(models(Iatmos)%mesh(2))
models(Iatmos)%mesh(1)%gtype = Icross
models(Iatmos)%mesh(2)%gtype = Idot
end if
!
do i = 1, 2
!
!-----------------------------------------------------------------------
! Set staggering type
!-----------------------------------------------------------------------
!
if (models(Iatmos)%mesh(i)%gtype == Icross) then
staggerLoc = ESMF_STAGGERLOC_CENTER
else if (models(Iatmos)%mesh(i)%gtype == Idot) then
staggerLoc = ESMF_STAGGERLOC_CORNER
end if
!
!-----------------------------------------------------------------------
! Create grid (2d)
!-----------------------------------------------------------------------
!
if (i == 1) then
models(Iatmos)%grid = ESMF_GridCreate(distgrid=distGrid, &
indexflag=ESMF_INDEX_GLOBAL,&
name="atm_grid2d", &
rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=FILENAME)) return
end if
!
!-----------------------------------------------------------------------
! Allocate coordinates
!-----------------------------------------------------------------------
!
call ESMF_GridAddCoord(models(Iatmos)%grid, &
staggerLoc=staggerLoc, &
rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=FILENAME)) return
!
!-----------------------------------------------------------------------
! Allocate items for masking
!-----------------------------------------------------------------------
!
call ESMF_GridAddItem(models(Iatmos)%grid, &
staggerLoc=staggerLoc, &
itemflag=ESMF_GRIDITEM_MASK, &
rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=FILENAME)) return
!
!-----------------------------------------------------------------------
! Set mask value for land and ocean
!-----------------------------------------------------------------------
!
models(Iatmos)%isLand = 2
models(Iatmos)%isOcean = 0
!
!-----------------------------------------------------------------------
! Allocate items for grid area
!-----------------------------------------------------------------------
!
call ESMF_GridAddItem(models(Iatmos)%grid, &
staggerLoc=staggerLoc, &
itemflag=ESMF_GRIDITEM_AREA, &
rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=FILENAME)) return
!
!-----------------------------------------------------------------------
! Get number of local DEs
!-----------------------------------------------------------------------
!
call ESMF_GridGet(models(Iatmos)%grid, &
localDECount=localDECount, &
rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=FILENAME)) return
!
!-----------------------------------------------------------------------
! Get pointers and set coordinates for the grid
!-----------------------------------------------------------------------
!
do j = 0, localDECount-1
call ESMF_GridGetCoord(models(Iatmos)%grid, &
localDE=j, &
staggerLoc=staggerLoc, &
coordDim=1, &
farrayPtr=ptrX, &
rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=FILENAME)) return
!
call ESMF_GridGetCoord(models(Iatmos)%grid, &
localDE=j, &
staggerLoc=staggerLoc, &
coordDim=2, &
farrayPtr=ptrY, &
rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=FILENAME)) return
!
call ESMF_GridGetItem (models(Iatmos)%grid, &
localDE=j, &
staggerLoc=staggerLoc, &
itemflag=ESMF_GRIDITEM_MASK, &
farrayPtr=ptrM, &
rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=FILENAME)) return
!
call ESMF_GridGetItem (models(Iatmos)%grid, &
localDE=j, &
staggerLoc=staggerLoc, &
itemflag=ESMF_GRIDITEM_AREA, &
farrayPtr=ptrA, &
rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=FILENAME)) return
!
!-----------------------------------------------------------------------
! Debug: write size of pointers
!-----------------------------------------------------------------------
!
name = GRIDDES(models(Iatmos)%mesh(i)%gtype)
!
if (debugLevel > 0) then
write(*,30) localPet, j, adjustl("PTR/ATM/GRD/"//name), &
lbound(ptrX, dim=1), ubound(ptrX, dim=1), &
lbound(ptrX, dim=2), ubound(ptrX, dim=2), &
ma%has_bdybottom, ma%has_bdyright, &
ma%has_bdytop, ma%has_bdyleft
end if
!
!-----------------------------------------------------------------------
! Fill the pointers
!-----------------------------------------------------------------------
!
if (models(Iatmos)%mesh(i)%gtype == Idot) then
if (debugLevel > 0) then
write(*,30) localPet, j, adjustl("DAT/ATM/GRD/"//name), &
ide1, ide2, jde1, jde2, &
ma%has_bdybottom, ma%has_bdyright, &
ma%has_bdytop, ma%has_bdyleft
end if
!
do i0 = ide1, ide2
do j0 = jde1, jde2
ptrX(i0,j0) = mddom%dlon(j0,i0)
ptrY(i0,j0) = mddom%dlat(j0,i0)
end do
end do
!
if (ma%has_bdyright) then
ptrX(:,jde2+1) = ptrX(:,jde2)+(ptrX(:,jde2)-ptrX(:,jde2-1))
ptrY(:,jde2+1) = ptrY(:,jde2)+(ptrY(:,jde2)-ptrY(:,jde2-1))
end if
!
if (ma%has_bdytop) then
ptrX(ide2+1,:) = ptrX(ide2,:)+(ptrX(ide2,:)-ptrX(ide2-1,:))
ptrY(ide2+1,:) = ptrY(ide2,:)+(ptrY(ide2,:)-ptrY(ide2-1,:))
end if
!
ptrA = dxsq
else if (models(Iatmos)%mesh(i)%gtype == Icross) then
if (debugLevel > 0) then
write(*,30) localPet, j, adjustl("DAT/ATM/GRD/"//name), &
ice1, ice2, jce1, jce2, &
ma%has_bdybottom, ma%has_bdyright, &
ma%has_bdytop, ma%has_bdyleft
end if
!
do i0 = ice1, ice2
do j0 = jce1, jce2
ptrX(i0,j0) = mddom%xlon(j0,i0)
ptrY(i0,j0) = mddom%xlat(j0,i0)
ptrM(i0,j0) = int(mddom%mask(j0,i0))
end do
end do
!
ptrA = dxsq
!
if (ma%has_bdyright) then
ptrX(:,jce2+1) = ptrX(:,jce2)+(ptrX(:,jce2)-ptrX(:,jce2-1))
ptrY(:,jce2+1) = ptrY(:,jce2)+(ptrY(:,jce2)-ptrY(:,jce2-1))
end if
!
if (ma%has_bdytop) then
ptrX(ice2+1,:) = ptrX(ice2,:)+(ptrX(ice2,:)-ptrX(ice2-1,:))
ptrY(ice2+1,:) = ptrY(ice2,:)+(ptrY(ice2,:)-ptrY(ice2-1,:))
end if
end if
!
!-----------------------------------------------------------------------
! Nullify pointer to make sure that it does not point on a random
! part in the memory
!-----------------------------------------------------------------------