forked from uturuncoglu/RegESM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod_esmf_ocn_roms.F90
2560 lines (2558 loc) · 103 KB
/
mod_esmf_ocn_roms.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
!-----------------------------------------------------------------------
!
! This file is part of ITU RegESM.
!
! ITU RegESM is free software: you can redistribute it and/or modify
! it under the terms of the GNU General Public License as published by
! the Free Software Foundation, either version 3 of the License, or
! (at your option) any later version.
!
! ITU RegESM is distributed in the hope that it will be useful,
! but WITHOUT ANY WARRANTY; without even the implied warranty of
! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
! GNU General Public License for more details.
!
! You should have received a copy of the GNU General Public License
! along with ITU RegESM. If not, see <http://www.gnu.org/licenses/>.
!
!-----------------------------------------------------------------------
#define FILENAME "mod_esmf_ocn.F90"
!
!-----------------------------------------------------------------------
! OCN gridded component code
!-----------------------------------------------------------------------
!
module mod_esmf_ocn
!
!-----------------------------------------------------------------------
! 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 ocean_control_mod, only : &
OCN_Initialize => ROMS_initialize, &
OCN_Run => ROMS_run, &
OCN_Finalize => ROMS_finalize
!
implicit none
private
!
!-----------------------------------------------------------------------
! Public subroutines
!-----------------------------------------------------------------------
!
public :: OCN_SetServices
!
contains
!
subroutine OCN_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 routine (P 1/2) for specific implementation
!-----------------------------------------------------------------------
!
call NUOPC_CompSetEntryPoint(gcomp, &
methodflag=ESMF_METHOD_INITIALIZE, &
phaseLabelList=(/"IPDv00p1"/), &
userRoutine=OCN_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=OCN_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=OCN_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=OCN_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=OCN_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=OCN_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=OCN_SetFinalize, &
rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=FILENAME)) return
!
end subroutine OCN_SetServices
!
subroutine OCN_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(Iocean)%importField, dim=1)
call NUOPC_Advertise(importState, &
StandardName=trim(models(Iocean)%importField(i)%long_name),&
name=trim(models(Iocean)%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(Iocean)%exportField, dim=1)
call NUOPC_Advertise(exportState, &
StandardName=trim(models(Iocean)%exportField(i)%long_name),&
name=trim(models(Iocean)%exportField(i)%short_name), rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=FILENAME)) return
end do
!
end subroutine OCN_SetInitializeP1
!
subroutine OCN_SetInitializeP2(gcomp, importState, exportState, &
clock, rc)
!
!-----------------------------------------------------------------------
! Used module declarations
!-----------------------------------------------------------------------
!
use mod_scalars, only : exit_flag, NoError
use mod_param, only : BOUNDS, Ngrids
use ocean_coupler_mod, only : allocate_atm2ocn, &
initialize_atm2ocn
!
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
!-----------------------------------------------------------------------
!
logical :: flag
integer :: LBi, UBi, LBj, UBj
integer :: ng, comm, localPet, petCount
character(ESMF_MAXSTR) :: gname
!
type(ESMF_VM) :: vm
type(ESMF_Time) :: startTime, currTime
!
rc = ESMF_SUCCESS
!
!-----------------------------------------------------------------------
! Get gridded component
!-----------------------------------------------------------------------
!
call ESMF_GridCompGet(gcomp, name=gname, 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
!-----------------------------------------------------------------------
!
flag = .true.
call OCN_Initialize(flag, mpiCOMM=comm)
!
!-----------------------------------------------------------------------
! Allocate exchange arrays
!-----------------------------------------------------------------------
!
do ng = 1, Ngrids
LBi = BOUNDS(ng)%LBi(localPet)
UBi = BOUNDS(ng)%UBi(localPet)
LBj = BOUNDS(ng)%LBj(localPet)
UBj = BOUNDS(ng)%UBj(localPet)
call allocate_atm2ocn(ng, LBi, UBi, LBj, UBj)
call initialize_atm2ocn(ng, localPet)
end do
!
!-----------------------------------------------------------------------
! Set-up grid and load coordinate data
!-----------------------------------------------------------------------
!
call OCN_SetGridArrays(gcomp, localPet, rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=FILENAME)) return
!
!-----------------------------------------------------------------------
! Set-up fields and register to import/export states
!-----------------------------------------------------------------------
!
call OCN_SetStates(gcomp, rc)
!
end subroutine OCN_SetInitializeP2
!
subroutine OCN_DataInit(gcomp, rc)
implicit none
!
!-----------------------------------------------------------------------
! Imported variable declarations
!-----------------------------------------------------------------------
!
type(ESMF_GridComp) :: gcomp
integer, intent(out) :: rc
!
!-----------------------------------------------------------------------
! Local variable declarations
!-----------------------------------------------------------------------
!
integer :: localPet, petCount
!
type(ESMF_Clock) :: clock
type(ESMF_Time) :: currTime
!
!-----------------------------------------------------------------------
! Get gridded component clock
!-----------------------------------------------------------------------
!
call ESMF_GridCompGet(gcomp, clock=clock, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=FILENAME)) return
!
call ESMF_ClockGet(clock, currTime=currTime, 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
call OCN_Put(gcomp, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=FILENAME)) return
end if
!
end subroutine OCN_DataInit
!
subroutine OCN_SetClock(gcomp, rc)
!
!-----------------------------------------------------------------------
! Used module declarations
!-----------------------------------------------------------------------
!
use mod_param
use mod_scalars
!
implicit none
!
!-----------------------------------------------------------------------
! Imported variable declarations
!-----------------------------------------------------------------------
!
type(ESMF_GridComp) :: gcomp
integer :: rc
!
!-----------------------------------------------------------------------
! Local variable declarations
!-----------------------------------------------------------------------
!
integer :: ng, 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
integer :: localPet, petCount
real*8 :: stime, etime, hour, minute, yday
character (len=80) :: calendar
!
type(ESMF_VM) :: vm
type(ESMF_Clock) :: cmpClock
type(ESMF_TimeInterval) :: timeStep
type(ESMF_Time) :: cmpRefTime, cmpStartTime, cmpStopTime
type(ESMF_Time) :: startTime, currTime
type(ESMF_Calendar) :: cal
!
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, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=FILENAME)) return
!
!-----------------------------------------------------------------------
! Create gridded component clock
!-----------------------------------------------------------------------
!
if (int(time_ref) == -2) then
ref_year=1968
ref_month=5
ref_day=23
ref_hour=0
ref_minute=0
ref_second=0
calendar='gregorian'
cal = ESMF_CalendarCreate(ESMF_CALKIND_GREGORIAN, &
name=trim(calendar), rc=rc)
else if (int(time_ref) == -1) then
ref_year=0
ref_month=1
ref_day=1
ref_hour=0
ref_minute=0
ref_second=0
calendar='360_day'
cal = ESMF_CalendarCreate(ESMF_CALKIND_360DAY, &
name=trim(calendar), rc=rc)
else if (int(time_ref) == 0) then
ref_year=1
ref_month=1
ref_day=1
ref_hour=0
ref_minute=0
ref_second=0
calendar='julian'
cal = ESMF_CalendarCreate(ESMF_CALKIND_JULIAN, &
name=trim(calendar), rc=rc)
else if (time_ref > 0.0_r8) then
ref_year=int(r_date(2))
ref_month=int(r_date(4))
ref_day=int(r_date(5))
ref_hour=int(r_date(6))
ref_minute=int(r_date(7))
ref_second=int(r_date(8))
calendar='gregorian'
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 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
!-----------------------------------------------------------------------
!
stime = minval(tdays)
call caldate(r_date, stime, str_year, yday, str_month, &
str_day, hour)
minute = (hour-aint(hour))*60.0_r8
str_hour = int(hour)
str_minute = int(minute)
str_second = int((minute-aint(minute))*60.0_r8)
!
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
!-----------------------------------------------------------------------
!
etime = 0.0_r8
do ng = 1, Ngrids
if (iic(ng) /= ntstart(ng)) then
etime = max(etime, (real(ntimes(ng),r8)*dt(ng))*sec2day)
else
etime = max(etime, tdays(ng)+(real(ntimes(ng),r8)*dt(ng))*sec2day)
end if
end do
call caldate(r_date, etime, end_year, yday, end_month, &
end_day, hour)
minute = (hour-aint(hour))*60.0_r8
end_hour = int(hour)
end_minute = int(minute)
end_second = int((minute-aint(minute))*60.0_r8)
if (localPet == 0) then
print*, etime, r_date, end_year, end_month, end_day, end_hour, end_minute, end_second
end if
!
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 OCN 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 OCN 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 OCN calendars do not match: '// &
'please check the config files')
return
end if
!
!-----------------------------------------------------------------------
! Modify component clock time step
!-----------------------------------------------------------------------
!
fac1 = maxval(connectors(Iocean,:)%divDT,mask=models(:)%modActive)
fac2 = maxval(connectors(:,Iocean)%divDT,mask=models(:)%modActive)
maxdiv = max(fac1, fac2)
!
call ESMF_ClockSet(cmpClock, name='ocn_clock', &
refTime=cmpRefTime, timeStep=timeStep/maxdiv, &
startTime=cmpStartTime, stopTime=cmpStopTime, &
rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=FILENAME)) return
!
end subroutine OCN_SetClock
!
subroutine OCN_CheckImport(gcomp, rc)
implicit none
!
!-----------------------------------------------------------------------
! Imported variable declarations
!-----------------------------------------------------------------------
!
type(ESMF_GridComp) :: gcomp
integer, intent(out) :: rc
!
!-----------------------------------------------------------------------
! Local variable declarations
!-----------------------------------------------------------------------
!
integer :: itemCount, localPet
logical :: atCorrectTime
character(ESMF_MAXSTR), allocatable :: itemNameList(:)
!
type(ESMF_VM) :: vm
type(ESMF_Time) :: startTime, currTime
type(ESMF_TimeInterval) :: timeStep
type(ESMF_Clock) :: driverClock
type(ESMF_Field) :: field
type(ESMF_State) :: importState
!
rc = ESMF_SUCCESS
!
!-----------------------------------------------------------------------
! Query component for the driverClock
!-----------------------------------------------------------------------
!
call NUOPC_ModelGet(gcomp, driverClock=driverClock, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=FILENAME)) return
!
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
!
!-----------------------------------------------------------------------
! Get the start time and current time out of the clock
!-----------------------------------------------------------------------
!
call ESMF_ClockGet(driverClock, startTime=startTime, &
currTime=currTime, timeStep=timeStep, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=FILENAME)) return
!
!-----------------------------------------------------------------------
! 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 (fast time step)
!-----------------------------------------------------------------------
!
if (itemCount > 0) then
call ESMF_StateGet(importState, itemName=trim(itemNameList(1)), &
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, currTime, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=FILENAME)) return
!
call print_timestamp(field, currTime, localPet, "OCN", rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=FILENAME)) return
else
atCorrectTime = NUOPC_IsAtTime(field, currTime+timeStep, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=FILENAME)) return
!
call print_timestamp(field, currTime+timeStep, localPet, "OCN", 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 if
!
!-----------------------------------------------------------------------
! Check fields in the importState (slow time step)
!-----------------------------------------------------------------------
!
if (models(Iriver)%modActive) then
!
call ESMF_StateGet(importState, itemName="rdis", &
field=field, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=FILENAME)) return
!
atCorrectTime = NUOPC_IsAtTime(field, startTime, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=FILENAME)) return
!
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 if
!
end subroutine OCN_CheckImport
!
subroutine OCN_SetGridArrays(gcomp, localPet, rc)
!
!-----------------------------------------------------------------------
! Used module declarations
!-----------------------------------------------------------------------
!
use mod_grid , only : GRID
use mod_param, only : NtileI, NtileJ, BOUNDS, Lm, Mm, Ngrids
!
implicit none
!
!-----------------------------------------------------------------------
! Imported variable declarations
!-----------------------------------------------------------------------
!
type(ESMF_GridComp), intent(inout) :: gcomp
integer :: localPet
integer :: rc
!
!-----------------------------------------------------------------------
! Local variable declarations
!-----------------------------------------------------------------------
!
integer :: i, j, ii, jj, ng, nr, tile, localDECount
integer :: IstrR, IendR, JstrR, JendR
integer :: IstrU, IendU, JstrU, JendU
integer :: IstrV, IendV, JstrV, JendV
integer :: LBi, UBi, LBj, UBj
integer :: staggerEdgeLWidth(2)
integer :: staggerEdgeUWidth(2)
integer, allocatable :: deBlockList(:,:,:)
real(ESMF_KIND_R8), pointer :: ptrX(:,:), ptrY(:,:), ptrA(:,:)
integer(ESMF_KIND_I4), pointer :: ptrM(:,:)
character(ESMF_MAXSTR) :: cname, name, msgString
!
type(ESMF_Array) :: arrX, arrY, arrM, arrA
type(ESMF_StaggerLoc) :: staggerLoc
type(ESMF_DistGrid) :: distGrid
type(ESMF_VM) :: vm
!
rc = ESMF_SUCCESS
!
!-----------------------------------------------------------------------
! Check number of nested grids
!-----------------------------------------------------------------------
!
call ESMF_GridCompGet(gcomp, vm=vm, name=cname, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=FILENAME)) return
!
if (Ngrids > 1) then
write(msgString,'(A,I3)') trim(cname)// &
': number of nested grid is', Ngrids, &
'coupled model only interacts with outermost one!'
call ESMF_LogWrite(trim(msgString), ESMF_LOGMSG_ERROR)
ng = 1
else
ng = Ngrids
end if
!
!-----------------------------------------------------------------------
! Get limits of the grid arrays (based on PET and nest level)
!-----------------------------------------------------------------------
!
IstrR = BOUNDS(ng)%IstrR(localPet)
IendR = BOUNDS(ng)%IendR(localPet)
JstrR = BOUNDS(ng)%JstrR(localPet)
JendR = BOUNDS(ng)%JendR(localPet)
!
IstrU = BOUNDS(ng)%Istr(localPet)
IendU = BOUNDS(ng)%IendR(localPet)
JstrU = BOUNDS(ng)%JstrR(localPet)
JendU = BOUNDS(ng)%JendR(localPet)
!
IstrV = BOUNDS(ng)%IstrR(localPet)
IendV = BOUNDS(ng)%IendR(localPet)
JstrV = BOUNDS(ng)%Jstr(localPet)
JendV = BOUNDS(ng)%JendR(localPet)
!
LBi = BOUNDS(ng)%LBi(localPet)
UBi = BOUNDS(ng)%UBi(localPet)
LBj = BOUNDS(ng)%LBj(localPet)
UBj = BOUNDS(ng)%UBj(localPet)
!
if (.not.allocated(deBlockList)) then
allocate(deBlockList(2,2,NtileI(ng)*NtileJ(ng)))
end if
do tile=0,NtileI(ng)*NtileJ(ng)-1
deBlockList(1,1,tile+1)=BOUNDS(ng)%Istr(tile)
deBlockList(1,2,tile+1)=BOUNDS(ng)%Iend(tile)
deBlockList(2,1,tile+1)=BOUNDS(ng)%Jstr(tile)
deBlockList(2,2,tile+1)=BOUNDS(ng)%Jend(tile)
end do
!
!-----------------------------------------------------------------------
! Create ESMF DistGrid based on model domain decomposition
!-----------------------------------------------------------------------
!
distGrid = ESMF_DistGridCreate(minIndex=(/ 1, 1 /), &
maxIndex=(/ Lm(ng), Mm(ng) /), &
deBlockList=deBlockList, &
rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=FILENAME)) return
!
!-----------------------------------------------------------------------
! Deallocate arrays
!-----------------------------------------------------------------------
!
if (allocated(deBlockList)) deallocate(deBlockList)
!
!-----------------------------------------------------------------------
! Define component grid (dot and cross points)
!-----------------------------------------------------------------------
!
if (.not. allocated(models(Iocean)%mesh)) then
allocate(models(Iocean)%mesh(4))
models(Iocean)%mesh(1)%gtype = Icross
models(Iocean)%mesh(2)%gtype = Idot
models(Iocean)%mesh(3)%gtype = Iupoint
models(Iocean)%mesh(4)%gtype = Ivpoint
end if
!
!-----------------------------------------------------------------------
! Set staggering type
!-----------------------------------------------------------------------
!
do i = 1, 4
if (models(Iocean)%mesh(i)%gtype == Iupoint) then
staggerLoc = ESMF_STAGGERLOC_EDGE1
staggerEdgeLWidth = (/0,1/) !(/0,1/)
staggerEdgeUWidth = (/0,1/) !(/1,1/)
else if (models(Iocean)%mesh(i)%gtype == Ivpoint) then
staggerLoc = ESMF_STAGGERLOC_EDGE2
staggerEdgeLWidth = (/0,0/) !(/1,0/)
staggerEdgeUWidth = (/0,1/) !(/1,1/)
else if (models(Iocean)%mesh(i)%gtype == Icross) then
staggerLoc = ESMF_STAGGERLOC_CENTER
staggerEdgeLWidth = (/0,1/) !(/1,1/)
staggerEdgeUWidth = (/0,1/) !(/1,1/)
else if (models(Iocean)%mesh(i)%gtype == Idot) then
staggerLoc = ESMF_STAGGERLOC_CORNER
staggerEdgeLWidth = (/0,0/) !(/0,0/)
staggerEdgeUWidth = (/0,1/) !(/1,1/)
end if
!
!-----------------------------------------------------------------------
! Create ESMF Grid
!-----------------------------------------------------------------------
!
if (i == 1) then
models(Iocean)%grid = ESMF_GridCreate(distgrid=distGrid, &
gridEdgeLWidth=(/1,1/), &
gridEdgeUWidth=(/1,1/), &
indexflag=ESMF_INDEX_GLOBAL,&
name="ocn_grid", &
rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=FILENAME)) return
end if
!
!-----------------------------------------------------------------------
! Allocate coordinates
!-----------------------------------------------------------------------
!
call ESMF_GridAddCoord(models(Iocean)%grid, &
staggerLoc=staggerLoc, &
staggerEdgeLWidth=staggerEdgeLWidth, &
staggerEdgeUWidth=staggerEdgeUWidth, &
rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=FILENAME)) return
!
!-----------------------------------------------------------------------
! Allocate items for masking
!-----------------------------------------------------------------------
!
call ESMF_GridAddItem(models(Iocean)%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(Iocean)%isLand = 0
models(Iocean)%isOcean = 1
!
!-----------------------------------------------------------------------
! Allocate items for grid area
!-----------------------------------------------------------------------
!
call ESMF_GridAddItem(models(Iocean)%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(Iocean)%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(Iocean)%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(Iocean)%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(Iocean)%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(Iocean)%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(Iocean)%mesh(i)%gtype)
!
if (debugLevel > 0) then
write(*,30) localPet, j, adjustl("PTR/OCN/GRD/"//name), &
lbound(ptrX, dim=1), ubound(ptrX, dim=1), &
lbound(ptrX, dim=2), ubound(ptrX, dim=2)
end if
!
!-----------------------------------------------------------------------
! Fill the pointers
!-----------------------------------------------------------------------
!
if (models(Iocean)%mesh(i)%gtype == Idot) then
if (debugLevel > 0) then
write(*,30) localPet, j, adjustl("DAT/OCN/GRD/"//name), &
IstrU, IendR, JstrV, JendR
end if
!
do jj = JstrV, JendR
do ii = IstrU, IendR
ptrX(ii,jj) = GRID(ng)%lonp(ii,jj)
ptrY(ii,jj) = GRID(ng)%latp(ii,jj)
ptrM(ii,jj) = int(GRID(ng)%pmask(ii,jj))