forked from uturuncoglu/RegESM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod_esmf_cpl.F90
1155 lines (1155 loc) · 49.5 KB
/
mod_esmf_cpl.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_cpl.F90"
!
!-----------------------------------------------------------------------
! CPL gridded component code
!-----------------------------------------------------------------------
!
module mod_esmf_cpl
!
!-----------------------------------------------------------------------
! Used module declarations
!-----------------------------------------------------------------------
!
use ESMF
use NUOPC
use NUOPC_Connector, only : &
NUOPC_SetServices => SetServices, &
NUOPC_Label_ComputeRH => label_ComputeRouteHandle, &
NUOPC_Label_ExecuteRH => label_ExecuteRouteHandle, &
NUOPC_Label_ReleaseRH => label_ReleaseRouteHandle, &
NUOPC_ConnectorGet, NUOPC_ConnectorSet
!
use mod_types
use mod_utils
!
implicit none
private
!
!-----------------------------------------------------------------------
! Public subroutines
!-----------------------------------------------------------------------
!
public :: CPL_SetServices
!
contains
!
subroutine CPL_SetServices(ccomp, rc)
implicit none
!
!-----------------------------------------------------------------------
! Imported variable declarations
!-----------------------------------------------------------------------
!
type(ESMF_CplComp) :: ccomp
integer, intent(out) :: rc
!
rc = ESMF_SUCCESS
!
!-----------------------------------------------------------------------
! Register generic methods
!-----------------------------------------------------------------------
!
call NUOPC_CompDerive(ccomp, NUOPC_SetServices, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=FILENAME)) return
!
!-----------------------------------------------------------------------
! Attach specializing methods
!-----------------------------------------------------------------------
!
call NUOPC_CompSpecialize(ccomp, specLabel=NUOPC_Label_ComputeRH, &
specRoutine=CPL_ComputeRH, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=FILENAME)) return
!
call NUOPC_CompSpecialize(ccomp, specLabel=NUOPC_Label_ExecuteRH, &
specRoutine=CPL_ExecuteRH, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=FILENAME)) return
!
call NUOPC_CompSpecialize(ccomp, specLabel=NUOPC_Label_ReleaseRH, &
specRoutine=CPL_ReleaseRH, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=FILENAME)) return
!
end subroutine CPL_SetServices
!
subroutine CPL_ComputeRH(ccomp, rc)
implicit none
!
!-----------------------------------------------------------------------
! Imported variable declarations
!-----------------------------------------------------------------------
!
type(ESMF_CplComp) :: ccomp
integer, intent(out) :: rc
!
!-----------------------------------------------------------------------
! Local variable declarations
!-----------------------------------------------------------------------
!
logical :: enableExtp, rh1Exist, rh2Exist
integer :: i, j, localPet, petCount, localDECount
integer :: iSrc, iDst, idSrc, idDst, itSrc, itDst, grSrc, grDst
integer :: srcCount, dstCount, itemCount, srcTermProcessing
integer :: srcMaskVal, dstMaskVal
integer(ESMF_KIND_I4), allocatable, dimension(:,:) :: tlw, tuw
character(ESMF_MAXSTR) :: ofile
character(ESMF_MAXSTR) :: cname, fname, rname, msgString
character(ESMF_MAXSTR), pointer :: cplList(:)
character(ESMF_MAXSTR), pointer :: srcList(:), dstList(:)
!
type(ESMF_VM) :: vm
type(ESMF_State) :: state
type(ESMF_DistGrid) :: distgrid
type(ESMF_Grid) :: srcGrid, dstGrid
type(ESMF_StaggerLoc) :: srcSLoc, dstSLoc
type(ESMF_UnmappedAction_Flag) :: unmap
type(ESMF_RegridMethod_Flag) :: regridMethod
type(ESMF_RouteHandle) :: routeHandle
type(ESMF_Field) :: srcField, dstField, tmpField
type(ESMF_FieldBundle) :: dstFields, srcFields
!
rc = ESMF_SUCCESS
!
!-----------------------------------------------------------------------
! Query coupler component
!-----------------------------------------------------------------------
!
call ESMF_CplCompGet(ccomp, name=cname, 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
!
do i = 1, nModels
do j = 1, nModels
if (connectors(i,j)%modActive .and. &
trim(connectors(i,j)%name) == trim(cname)) then
iSrc = i
iDst = j
end if
end do
end do
!
enableExtp = connectors(iSrc,iDst)%modExtrapolation
!
!-----------------------------------------------------------------------
! Exchange land-sea mask information
!-----------------------------------------------------------------------
!
call UTIL_VMGlobalBroadcast(models(iSrc)%isLand, &
models(iSrc)%petList(1), rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=FILENAME)) return
!
call UTIL_VMGlobalBroadcast(models(iDst)%isLand, &
models(iDst)%petList(1), rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=FILENAME)) return
!
call UTIL_VMGlobalBroadcast(models(iSrc)%isOcean, &
models(iSrc)%petList(1), rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=FILENAME)) return
!
call UTIL_VMGlobalBroadcast(models(iDst)%isOcean, &
models(iDst)%petList(1), rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=FILENAME)) return
!
!-----------------------------------------------------------------------
! Set source and destination masks for connector
!-----------------------------------------------------------------------
!
if (connectors(iSrc,iDst)%modInteraction == Ioverocn) then
srcMaskVal = models(iSrc)%isLand
dstMaskVal = models(iDst)%isLand
else if (connectors(iSrc,iDst)%modInteraction == Ioverlnd) then
srcMaskVal = models(iSrc)%isOcean
dstMaskVal = models(iDst)%isOcean
end if
!
!-----------------------------------------------------------------------
! Get size of source and destination field list
!-----------------------------------------------------------------------
!
call NUOPC_ConnectorGet(ccomp, srcFields=srcFields, &
dstFields=dstFields, state=state, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=FILENAME)) return
!
call ESMF_FieldBundleGet(srcFields, fieldCount=srcCount, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=FILENAME)) return
!
call ESMF_FieldBundleGet(dstFields, fieldCount=dstCount, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=FILENAME)) return
!
!-----------------------------------------------------------------------
! Get source and destination fields
!-----------------------------------------------------------------------
!
if (srcCount == dstCount .and. dstCount > 0) then
!
!-----------------------------------------------------------------------
! Allocate list arrays and routehandle
!-----------------------------------------------------------------------
!
allocate(srcList(srcCount))
allocate(dstList(dstCount))
!
!-----------------------------------------------------------------------
! Query field lists
!-----------------------------------------------------------------------
!
call ESMF_FieldBundleGet(srcFields, fieldNameList=srcList, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=FILENAME)) return
!
call ESMF_FieldBundleGet(dstFields, fieldNameList=dstList, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=FILENAME)) return
!
!-----------------------------------------------------------------------
! Loop over exchange fields
!-----------------------------------------------------------------------
!
do i = 1, srcCount
!
!-----------------------------------------------------------------------
! Get source and destination field index
!-----------------------------------------------------------------------
!
idSrc = get_varid(models(iSrc)%exportField, srcList(i))
idDst = get_varid(models(iDst)%importField, dstList(i))
!
!-----------------------------------------------------------------------
! Get interpolation type
!-----------------------------------------------------------------------
!
itSrc = models(iSrc)%exportField(idSrc)%itype
itDst = models(iDst)%importField(idDst)%itype
!
if (itSrc /= itDst) then
write(msgString,'(a)') trim(cname)// &
': src and dst field interpolation type does not match!'
call ESMF_LogWrite(trim(msgString), ESMF_LOGMSG_ERROR)
return
end if
!
!-----------------------------------------------------------------------
! Get grid type
!-----------------------------------------------------------------------
!
grSrc = models(iSrc)%exportField(idSrc)%gtype
grDst = models(iDst)%importField(idDst)%gtype
!
!-----------------------------------------------------------------------
! Get source and destination field bundle
!-----------------------------------------------------------------------
!
fname = trim(models(iSrc)%exportField(idSrc)%short_name)
!
call ESMF_FieldBundleGet(srcFields, trim(fname), &
field=srcField, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=FILENAME)) return
!
call ESMF_FieldBundleGet(dstFields, trim(fname), &
field=dstField, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=FILENAME)) return
!
!-----------------------------------------------------------------------
! Get source and destination field
!-----------------------------------------------------------------------
!
call ESMF_FieldGet(srcField, grid=srcGrid, &
staggerloc=srcSLoc, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=FILENAME)) return
!
call ESMF_FieldGet(dstField, grid=dstGrid, &
staggerloc=dstSLoc, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=FILENAME)) return
!
!-----------------------------------------------------------------------
! Check for extrapolation option for field?
!-----------------------------------------------------------------------
!
if (enableExtp) then
!
!-----------------------------------------------------------------------
! Create routehandles for two step interpolation
!-----------------------------------------------------------------------
!-----------------------------------------------------------------------
! Check 1st routehandle (i.e. rh_CROSS_DOT_BLIN_ATM-OCN)
!-----------------------------------------------------------------------
!
rname = trim(GRIDDES(grSrc))//'_'//trim(GRIDDES(grDst))//'_'// &
trim(INTPDES(Ibilin))//'_'//trim(cname)//'_ext'
!
call ESMF_StateGet(state, itemSearch='rh_'//trim(rname), &
itemCount=itemCount, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=FILENAME)) return
!
rh1Exist = .true.
if (itemCount <= 0) rh1Exist = .false.
!
if (.not. rh1Exist) then
!
!-----------------------------------------------------------------------
! Create 1st routehandle
!-----------------------------------------------------------------------
!
unmap = ESMF_UNMAPPEDACTION_IGNORE
regridMethod = ESMF_REGRIDMETHOD_BILINEAR
!
srcTermProcessing = 0
!
call ESMF_FieldRegridStore(srcField=srcField, &
dstField=dstField, &
srcMaskValues=(/srcMaskVal/), &
dstMaskValues=(/dstMaskVal/), &
unmappedaction=unmap, &
routeHandle=routeHandle, &
regridmethod=regridMethod, &
ignoreDegenerate=.true., &
srcTermProcessing=srcTermProcessing, &
rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=FILENAME)) return
!
!-----------------------------------------------------------------------
! Add name to 1st routehandle
!-----------------------------------------------------------------------
!
call ESMF_RouteHandleSet(routeHandle, &
name='rh_'//trim(rname), rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=FILENAME)) return
!
!-----------------------------------------------------------------------
! Add 1st routehandle to the state
!-----------------------------------------------------------------------
!
call ESMF_StateAdd(state, (/ routeHandle /), rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=FILENAME)) return
!
end if
!
!-----------------------------------------------------------------------
! Check 2nd routehandle (i.e. rh_CROSS_DOT_NS2D_ATM-OCN)
!-----------------------------------------------------------------------
!
rname = trim(GRIDDES(grSrc))//'_'//trim(GRIDDES(grDst))//'_'// &
trim(INTPDES(Instod))//'_'//trim(cname)//'_ext'
!
call ESMF_StateGet(state, itemSearch='rh_'//trim(rname), &
itemCount=itemCount, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=FILENAME)) return
!
rh2Exist = .true.
if (itemCount <= 0) rh2Exist = .false.
!
if (.not. rh2Exist) then
!
!-----------------------------------------------------------------------
! Create temporary field in destination grid
!-----------------------------------------------------------------------
!
tmpField = UTIL_FieldCreate(dstField, 'temp_field', ONE_R8, -1,rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=FILENAME)) return
!
!-----------------------------------------------------------------------
! Modify grid mask to split masked and unmasked grid cells
!-----------------------------------------------------------------------
!
call UTIL_FindUnmapped(srcField, dstField, srcMaskVal, &
dstMaskVal, iSrc, iDst, rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=FILENAME)) return
!
!-----------------------------------------------------------------------
! Create 2nd routehandle
!-----------------------------------------------------------------------
!
unmap = ESMF_UNMAPPEDACTION_IGNORE
regridMethod = ESMF_REGRIDMETHOD_NEAREST_STOD
!
srcTermProcessing = 0
!
call ESMF_FieldRegridStore(srcField=tmpField, &
dstField=dstField, &
srcMaskValues=(/ dstMaskVal, &
UNMAPPED_MASK /), &
dstMaskValues=(/ dstMaskVal, &
MAPPED_MASK /), &
unmappedaction=unmap, &
routeHandle=routeHandle, &
regridmethod=regridMethod, &
srcTermProcessing=srcTermProcessing, &
ignoreDegenerate=.true., &
rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=FILENAME)) return
!
!-----------------------------------------------------------------------
! Add name to 2nd routehandle
!-----------------------------------------------------------------------
!
call ESMF_RouteHandleSet(routeHandle, &
name='rh_'//trim(rname), rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=FILENAME)) return
!
!-----------------------------------------------------------------------
! Add 2nd routehandle to the state
!-----------------------------------------------------------------------
!
call ESMF_StateAdd(state, (/ routeHandle /), rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=FILENAME)) return
!
!-----------------------------------------------------------------------
! Delete temporary field
!-----------------------------------------------------------------------
!
call ESMF_FieldDestroy(tmpField, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=FILENAME)) return
!
end if
!
else
!
!-----------------------------------------------------------------------
! Create routehandle for one step interpolation
!-----------------------------------------------------------------------
!
rname = trim(GRIDDES(grSrc))//'_'//trim(GRIDDES(grDst))//'_'// &
trim(INTPDES(itSrc))//'_'//trim(cname)
!
call ESMF_StateGet(state, itemSearch='rh_'//trim(rname), &
itemCount=itemCount, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=FILENAME)) return
!
rh1Exist = .true.
rh2Exist = .false.
if (itemCount <= 0) rh1Exist = .false.
!
if (.not. rh1Exist) then
unmap = ESMF_UNMAPPEDACTION_IGNORE
!
if (itSrc == Ibilin) then
regridMethod = ESMF_REGRIDMETHOD_BILINEAR
else if (itSrc == Instod) then
regridMethod = ESMF_REGRIDMETHOD_NEAREST_STOD
else if (itSrc == Indtos) then
regridMethod = ESMF_REGRIDMETHOD_NEAREST_DTOS
else
write(msgString,'(a)') trim(cname)//': selected '// &
'interpolation type is not supported! '//INTPDES(itSrc)
call ESMF_LogWrite(trim(msgString), ESMF_LOGMSG_ERROR)
call ESMF_Finalize(endflag=ESMF_END_ABORT)
end if
!
srcTermProcessing = 0
!
if (iSrc == Iocean) then
call ESMF_FieldRegridStore(srcField=srcField, &
dstField=dstField, &
srcMaskValues=(/models(iSrc)%isLand/), &
unmappedaction=unmap, &
routeHandle=routeHandle, &
regridmethod=regridMethod, &
srcTermProcessing=srcTermProcessing, &
ignoreDegenerate=.true., &
rc=rc)
else
call ESMF_FieldRegridStore(srcField=srcField, &
dstField=dstField, &
unmappedaction=unmap, &
routeHandle=routeHandle, &
regridmethod=regridMethod, &
srcTermProcessing=srcTermProcessing, &
ignoreDegenerate=.true., &
rc=rc)
end if
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=FILENAME)) return
!
!-----------------------------------------------------------------------
! Add name to routehandle
!-----------------------------------------------------------------------
!
call ESMF_RouteHandleSet(routeHandle, &
name='rh_'//trim(rname), rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=FILENAME)) return
!
!-----------------------------------------------------------------------
! Add routehandle to the state
!-----------------------------------------------------------------------
!
call ESMF_StateAdd(state, (/ routeHandle /), rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=FILENAME)) return
!
end if
!
end if
!
!-----------------------------------------------------------------------
! Debug: print out exchange fields
!-----------------------------------------------------------------------
!
if ((debugLevel > 0) .and. (localPet == 0)) then
write(*,40) trim(cname), &
trim(models(iSrc)%exportField(idSrc)%short_name), &
trim(GRIDDES(models(iSrc)%exportField(idSrc)%gtype)), &
trim(models(iDst)%importField(idDst)%short_name), &
trim(GRIDDES(models(iDst)%importField(idDst)%gtype)), &
trim(INTPDES(models(iSrc)%exportField(idSrc)%itype)), &
rh1Exist, rh2Exist
end if
!
end do
end if
!
!-----------------------------------------------------------------------
! Formats
!-----------------------------------------------------------------------
!
40 format(A10,': routehandle ',A4,'[',A,'] to ',A4,'[',A,']', &
' >> ',A, ' - ',L1,' - ',L1)
!
end subroutine CPL_ComputeRH
!
subroutine CPL_ExecuteRH(ccomp, rc)
implicit none
!
!-----------------------------------------------------------------------
! Imported variable declarations
!-----------------------------------------------------------------------
!
type(ESMF_CplComp) :: ccomp
integer, intent(out) :: rc
!
!-----------------------------------------------------------------------
! Local variable declarations
!-----------------------------------------------------------------------
!
logical :: enableExtp
real*8 :: src_total, dst_total, rel_error
integer :: srcValueList(9), dstValueList(9)
integer :: localPet, petCount, localDECount
integer :: i, j, srcCount, dstCount
integer :: iSrc, iDst, idSrc, idDst, itSrc, itDst, grSrc, grDst
character(ESMF_MAXSTR), pointer :: srcList(:), dstList(:)
character(ESMF_MAXSTR) :: msgString, cname, fname, rname
!
type(ESMF_VM) :: vm
type(ESMF_State) :: state
type(ESMF_Grid) :: srcGrid, dstGrid
type(ESMF_RouteHandle) :: routeHandle
type(ESMF_StaggerLoc) :: srcSLoc, dstSLoc
type(ESMF_Field) :: srcField, dstField, tmpField
type(ESMF_FieldBundle) :: dstFields, srcFields
real(ESMF_KIND_R8), dimension(:,:), pointer :: ptr2d
!
rc = ESMF_SUCCESS
!
!-----------------------------------------------------------------------
! Query coupler component
!-----------------------------------------------------------------------
!
call ESMF_CplCompGet(ccomp, name=cname, 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
!
do i = 1, nModels
do j = 1, nModels
if (connectors(i,j)%modActive .and. &
trim(connectors(i,j)%name) == trim(cname)) then
iSrc = i
iDst = j
end if
end do
end do
!
enableExtp = connectors(iSrc,iDst)%modExtrapolation
!
!-----------------------------------------------------------------------
! Get size of field list
!-----------------------------------------------------------------------
!
call NUOPC_ConnectorGet(ccomp, srcFields=srcFields, &
dstFields=dstFields, state=state, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=FILENAME)) return
!
call ESMF_FieldBundleGet(srcFields, fieldCount=srcCount, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=FILENAME)) return
!
call ESMF_FieldBundleGet(dstFields, fieldCount=dstCount, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=FILENAME)) return
!
!-----------------------------------------------------------------------
! Get name of fields
!-----------------------------------------------------------------------
!
allocate(srcList(srcCount))
call ESMF_FieldBundleGet(srcFields, fieldNameList=srcList, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=FILENAME)) return
!
allocate(dstList(dstCount))
call ESMF_FieldBundleGet(dstFields, fieldNameList=dstList, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=FILENAME)) return
!
!-----------------------------------------------------------------------
! Loop over exchange fields
!-----------------------------------------------------------------------
!
do i = 1, srcCount
!
!-----------------------------------------------------------------------
! Get source and destination field index
!-----------------------------------------------------------------------
!
idSrc = get_varid(models(iSrc)%exportField, srcList(i))
idDst = get_varid(models(iDst)%importField, dstList(i))
!
!-----------------------------------------------------------------------
! Get interpolation type
!-----------------------------------------------------------------------
!
itSrc = models(iSrc)%exportField(idSrc)%itype
itDst = models(iDst)%importField(idDst)%itype
!
if (itSrc /= itDst) then
write(msgString,'(a)') trim(cname)// &
': src and dst field interpolation type does not match!'
call ESMF_LogWrite(trim(msgString), ESMF_LOGMSG_ERROR)
return
end if
!
!-----------------------------------------------------------------------
! Get grid type
!-----------------------------------------------------------------------
!
grSrc = models(iSrc)%exportField(idSrc)%gtype
grDst = models(iDst)%importField(idDst)%gtype
!
!-----------------------------------------------------------------------
! Get source and destination field
!-----------------------------------------------------------------------
!
fname = trim(models(iSrc)%exportField(idSrc)%short_name)
!
call ESMF_FieldBundleGet(srcFields, trim(fname), &
field=srcField, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=FILENAME)) return
!
call ESMF_FieldBundleGet(dstFields, trim(fname), &
field=dstField, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=FILENAME)) return
!
!-----------------------------------------------------------------------
! Perform regrid
!-----------------------------------------------------------------------
!
if (enableExtp) then
!
!-----------------------------------------------------------------------
! Perform regrid with extrapolation support
!-----------------------------------------------------------------------
!-----------------------------------------------------------------------
! Get 1st routehandle from state
!-----------------------------------------------------------------------
!
rname = trim(GRIDDES(grSrc))//'_'//trim(GRIDDES(grDst))//'_'// &
trim(INTPDES(itSrc))//'_'//trim(cname)//'_ext'
!
call ESMF_StateGet(state, 'rh_'//trim(rname), routeHandle, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=FILENAME)) return
!
!-----------------------------------------------------------------------
! Create temporary field in destination grid
!-----------------------------------------------------------------------
!
tmpField = UTIL_FieldCreate(dstField, fname, MISSING_R8, -1, rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=FILENAME)) return
!
!-----------------------------------------------------------------------
! Perform 1st regrid operation
!-----------------------------------------------------------------------
!
call ESMF_FieldRegrid(srcField, tmpField, routeHandle, &
zeroregion=ESMF_REGION_SELECT, &
termorderflag=ESMF_TERMORDER_SRCSEQ, &
rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=FILENAME)) return
!
!-----------------------------------------------------------------------
! Copy content from temporary field to destination field
!-----------------------------------------------------------------------
!
call ESMF_FieldCopy(dstField, tmpField, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=FILENAME)) return
!
!-----------------------------------------------------------------------
! Get 2nd routehandle from state
!-----------------------------------------------------------------------
!
rname = trim(GRIDDES(grSrc))//'_'//trim(GRIDDES(grDst))//'_'// &
'NS2D_'//trim(cname)//'_ext'
!
call ESMF_StateGet(state, 'rh_'//trim(rname), routeHandle, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=FILENAME)) return
!
!-----------------------------------------------------------------------
! Perform 2nd regrid operation to fill unmapped grid points
!-----------------------------------------------------------------------
!
call ESMF_FieldRegrid(tmpField, dstField, routeHandle, &
zeroregion=ESMF_REGION_SELECT, &
termorderflag=ESMF_TERMORDER_SRCSEQ, &
rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=FILENAME)) return
!
call ESMF_FieldDestroy(tmpField, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=FILENAME)) return
!
!-----------------------------------------------------------------------
! Check: integral adjustment is activated or not for the field
!-----------------------------------------------------------------------
!
if (models(iSrc)%exportField(idSrc)%enable_integral_adj) then
!
!-----------------------------------------------------------------------
! Calculate integral
!-----------------------------------------------------------------------
!
src_total = ZERO_R8
src_total = UTIL_CalcIntegral(vm, srcField, &
(/ UNMAPPED_MASK, MAPPED_MASK /), rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=FILENAME)) return
!
if (localPet == 0) then
write(*,70) localPet, 'SRC. INTEGRAL', src_total, &
trim(models(iSrc)%exportField(idSrc)%short_name)
end if
!
dst_total = ZERO_R8
dst_total = UTIL_CalcIntegral(vm, dstField, &
(/ UNMAPPED_MASK, MAPPED_MASK /), rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=FILENAME)) return
!
if (localPet == 0) then
write(*,70) localPet, 'DST. INTEGRAL', dst_total, &
trim(models(iSrc)%exportField(idSrc)%short_name)
rel_error = 0.0d0
if (src_total /= 0.0d0) then
rel_error = (dst_total-src_total)/src_total
end if
write(*,70) localPet, 'RELATIVE ERROR 1', rel_error, &
trim(models(iSrc)%exportField(idSrc)%short_name)
end if
!
!-----------------------------------------------------------------------
! Adjust destination field based on calculated integral
!-----------------------------------------------------------------------
!
call UTIL_AdjustField(vm, dstField, &
(/ UNMAPPED_MASK, MAPPED_MASK /), &
dst_total-src_total, rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=FILENAME)) return
!
dst_total = ZERO_R8
dst_total = UTIL_CalcIntegral(vm, dstField, &
(/ UNMAPPED_MASK, MAPPED_MASK /), rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=FILENAME)) return
!
if (localPet == 0) then
write(*,70) localPet, 'DST. INTEGRAL (CORR)', dst_total, &
trim(models(iSrc)%exportField(idSrc)%short_name)
rel_error = 0.0d0
if (src_total /= 0.0d0) then
rel_error = (dst_total-src_total)/src_total
end if
write(*,70) localPet, 'RELATIVE ERROR 2', rel_error, &
trim(models(iSrc)%exportField(idSrc)%short_name)
end if
end if
!
else
!
!-----------------------------------------------------------------------
! Perform regrid without extrapolation support
!-----------------------------------------------------------------------
!
rname = trim(GRIDDES(grSrc))//'_'//trim(GRIDDES(grDst))//'_'// &
trim(INTPDES(itSrc))//'_'//trim(cname)
!
call ESMF_StateGet(state, 'rh_'//trim(rname), routeHandle, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=FILENAME)) return
!
call ESMF_FieldRegrid(srcField, dstField, routeHandle, &
zeroregion=ESMF_REGION_SELECT, &
termorderflag=ESMF_TERMORDER_SRCSEQ, &
rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=FILENAME)) return
!
end if
!
!-----------------------------------------------------------------------
! Debug: write data to disk
!-----------------------------------------------------------------------
!
if (debugLevel > 4) then
call ESMF_FieldWrite(srcField, 'src_data_'//trim(cname)//'.nc', &
variableName='src_data', overwrite=.true., &
rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=FILENAME)) return
!
call ESMF_FieldWrite(dstField, 'dst_data_'//trim(cname)//'.nc', &
variableName='dst_data', overwrite=.true., &
rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=FILENAME)) return
end if
!
!-----------------------------------------------------------------------
! Debug: print out exchange fields
!-----------------------------------------------------------------------
!
if ((debugLevel > 0) .and. (localPet == 0)) then
write(*,60) trim(cname), &
trim(models(iSrc)%exportField(idSrc)%short_name), &
trim(GRIDDES(models(iSrc)%exportField(idSrc)%gtype)), &
trim(models(iDst)%importField(idDst)%short_name), &
trim(GRIDDES(models(iDst)%importField(idDst)%gtype)), &
trim(INTPDES(models(iSrc)%exportField(idSrc)%itype))
end if
!
!-----------------------------------------------------------------------
! Debug: print out import/export fields time stamp info
!-----------------------------------------------------------------------
!
if ((debugLevel > 0) .and. (localPet == 0)) then
call ESMF_AttributeGet(srcField, name="TimeStamp", &
valueList=srcValueList, &
convention="NUOPC", purpose="Instance", &
rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=FILENAME)) return
!
call ESMF_AttributeGet(dstField, name="TimeStamp", &
valueList=dstValueList, &
convention="NUOPC", purpose="Instance", &
rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=FILENAME)) return
!
write(*,50) trim(cname), &
trim(models(iSrc)%exportField(idSrc)%short_name), &
srcValueList(1), srcValueList(2), srcValueList(3), &
srcValueList(4), srcValueList(5), &
trim(models(iDst)%importField(idDst)%short_name), &
dstValueList(1), dstValueList(2), dstValueList(3), &
dstValueList(4), dstValueList(5)
end if
!
end do
!
!-----------------------------------------------------------------------
! Deallocate temporary arrays
!-----------------------------------------------------------------------
!
deallocate(srcList)
deallocate(dstList)
!
!-----------------------------------------------------------------------
! Formats
!-----------------------------------------------------------------------
!
50 format(A10,': tstamp ',A4,' [',I4,'-',I2.2,'-', &
I2.2,'_',I2.2,'_',I2.2,'] to ',A4,' [',I4,'-',I2.2,'-', &
I2.2,'_',I2.2,'_',I2.2,']')
60 format(A10,': regrid ',A4,' [',A,'] to ',A4,' [',A,']',' >> ',A)
70 format(" PET(",I3.3,") - ",A," = ",E14.5," (",A,")")
!
end subroutine CPL_ExecuteRH
!
subroutine CPL_ReleaseRH(ccomp, rc)
implicit none
!
!-----------------------------------------------------------------------
! Imported variable declarations
!-----------------------------------------------------------------------
!
type(ESMF_CplComp) :: ccomp
integer, intent(out) :: rc
!
!-----------------------------------------------------------------------
! Local variable declarations
!-----------------------------------------------------------------------
!
logical :: enableExtp, rhExist, rh1Exist, rh2Exist
integer :: i, j, localPet, petCount
integer :: itemCount, srcCount, dstCount
integer :: iSrc, iDst, idSrc, idDst, itSrc, itDst, grSrc, grDst
character(ESMF_MAXSTR), pointer :: srcList(:), dstList(:)
character(ESMF_MAXSTR) :: cname, rname
!
type(ESMF_VM) :: vm
type(ESMF_State) :: state
type(ESMF_FieldBundle) :: srcFields, dstFields
type(ESMF_RouteHandle) :: routeHandle
!
rc = ESMF_SUCCESS
!
!-----------------------------------------------------------------------
! Query coupler component
!-----------------------------------------------------------------------
!
call ESMF_CplCompGet(ccomp, name=cname, 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
!
do i = 1, nModels
do j = 1, nModels
if (connectors(i,j)%modActive .and. &
trim(connectors(i,j)%name) == trim(cname)) then
iSrc = i
iDst = j
end if
end do
end do
!
enableExtp = connectors(iSrc,iDst)%modExtrapolation
!
!-----------------------------------------------------------------------
! Get size of field list
!-----------------------------------------------------------------------
!
call NUOPC_ConnectorGet(ccomp, srcFields=srcFields, &
dstFields=dstFields, state=state, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=FILENAME)) return
!
call ESMF_FieldBundleGet(srcFields, fieldCount=srcCount, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, file=FILENAME)) return