-
Notifications
You must be signed in to change notification settings - Fork 8
/
hco_esmf_grid.F90
2197 lines (1966 loc) · 86.6 KB
/
hco_esmf_grid.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
#define VERIFY_(A) if(.not.HCO_ESMF_VRFY(A,subname,__LINE__)) stop -1
#define ASSERT_(A) if(.not.HCO_ESMF_ASRT(A,subname,__LINE__)) stop -1
!------------------------------------------------------------------------------
! Harmonized Emissions Component (HEMCO) !
!------------------------------------------------------------------------------
!BOP
!
! !MODULE: hco_esmf_grid
!
! !DESCRIPTION: Module HCO\_ESMF\_GRID defines functions for the regridding and
! control of the intermediate HEMCO grid in the HEMCO to CAM interface.
!\\
!\\
! !INTERFACE:
!
module hco_esmf_grid
!
! !USES:
!
! ESMF function wrappers
use hco_esmf_wrappers
! ESMF types
use ESMF, only: ESMF_Mesh, ESMF_DistGrid, ESMF_Grid
use ESMF, only: ESMF_Field
use ESMF, only: ESMF_RouteHandle
use ESMF, only: ESMF_SUCCESS
use ESMF, only: ESMF_LogWrite, ESMF_LOGMSG_INFO
! MPI
use mpi, only: MPI_PROC_NULL, MPI_SUCCESS, MPI_INTEGER
! Floating point type.
! FIXME: May change to HEMCO precision later down the line
use shr_kind_mod, only: r8 => shr_kind_r8
use ESMF, only: ESMF_KIND_I4, ESMF_KIND_R8
implicit none
private
save
!
! !PUBLIC MEMBER FUNCTIONS:
!
public :: HCO_Grid_Init
public :: HCO_Grid_UpdateRegrid
public :: HCO_Grid_HCO2CAM_2D ! Regrid HEMCO to CAM mesh on 2D field
public :: HCO_Grid_HCO2CAM_3D ! ...on 3D field
public :: HCO_Grid_CAM2HCO_2D ! Regrid CAM mesh to HEMCO on 2D field
public :: HCO_Grid_CAM2HCO_3D ! ...on 3D field
!
! !PRIVATE MEMBER FUNCTIONS:
!
private :: HCO_Grid_ESMF_CreateCAM
private :: HCO_Grid_ESMF_CreateCAMField ! Create field on CAM physics mesh
private :: HCO_Grid_ESMF_CreateHCO ! Create HEMCO lat-lon grid in ESMF
private :: HCO_Grid_ESMF_CreateHCOField ! Create field on HEMCO ll grid
private :: HCO_ESMF_Set2DHCO ! Set ESMF field with 2D HEMCO data
private :: HCO_ESMF_Set3DHCO ! Set ESMF field with 3D HEMCO data
private :: HCO_ESMF_Set2DCAM ! Set ESMF field with 2D CAM mesh data
private :: HCO_ESMF_Set3DCAM ! Set ESMF field with 3D CAM mesh data
private :: HCO_ESMF_Get1DField ! Retrieve 1D ESMF field pointer
private :: HCO_ESMF_Get2DField ! Retrieve 2D ESMF field pointer
private :: HCO_ESMF_Get3DField ! Retrieve 3D ESMF field pointer
! !REMARKS:
!
! Horizontal grid specifications are based on GEOS-Chem definitions.
! Vertical grid specifications are copied from hyai, hybi from CAM.
!
! Notes:
! (i) In GEOS-Chem, level 1 is bottom-of-atmos, so 'bottom2top' lev_sequence.
! (ii)
!
! The CreateHCO routine replaces the edyn_esmf routines for create_geo and geo2phys
! grid as they only differ by a grid staggering configuration (center and corner).
! Thus they are merged into one routine and write to two grids, HCO_Grid and HCO2CAM_Grid.
! (hplin, 2/20/20)
!
! The HEMCO grid is initialized in ESMF with 1 periodic dim on the longitude.
! It remains to be seen whether this is necessary and validation of the regridder's
! robustness is necessary (hplin, 2/23/20)
!
! Only 3-D regridding implemented for now. (hplin, 2/24/20)
!
! ---------------------- BELOW ARE SIDE REMARKS ---------------------------------
! This module was written by hplin on a gloomy day (as usual) in Cambridge/Boston
! where I patiently copied the following modules and translated the terminology
! between them:
! GEOS-Chem: state_grid_mod, hco_types_mod, state_met_mod, pressure_mod
! CAM Ionosphere Interface: edyn_mpi, edyn_geogrid, ionosphere_interface, hycoef, ref_pres
! The result is a mash-up of CAM and GEOS-Chem coding terminology, but in the end
! it is mostly GEOS-Chem. Conversions are done at initialization.
!
! The vertical grid specification conversions were hand-written
! with napkin-based calculations, so buyer beware. They need to be carefully
! validated before final shipping.
!
! Horizontal grid specifications were mostly borrowed from GEOS-Chem definitions.
! We use a global ??? grid for testing at this point and they need to be
! un-hardcoded in the future.
!
! Thanks to Thibaud Fritz for supplying CESM-GC dev code which helped in the
! conversions from CAM to GC speak.
!
! -- hplin, 2/11/20
!
! !REVISION HISTORY:
! 11 Feb 2020 - H.P. Lin - First crack.
! 17 Feb 2020 - H.P. Lin - Start of work on regrid routines.
! 15 Apr 2020 - H.P. Lin - Regrid routines mostly finalized
!EOP
!------------------------------------------------------------------------------
!BOC
!
! !PUBLIC TYPES:
!
! Global grid parameters.
! this may be refactored into some other structure that isn't global later.
! for now this will do (hplin, 2/11/20) -- and I am confident this will
! be the way for at least a few more years, because who touches working code? ;)
integer, public, protected :: IM ! # of lons
integer, public, protected :: JM ! # of lats
integer, public, protected :: LM ! # of levs
! Computed parameters for compatibility with GEOS-Chem
real(r8), public, protected:: DX ! Delta X [deg long]
real(r8), public, protected:: DY ! Delta X [deg lat]
! Horizontal Coordinates
real(r8), public, pointer :: &
XMid (:,:), & ! Longitude centers [deg]
XEdge(:,:), & ! Longitude edges [deg]
YMid (:,:), & ! Latitude centers [deg]
YEdge(:,:), & ! Latitude edges [deg]
YEdge_R(:,:), & ! Latitude edges R [rad]
YSin (:,:) ! SIN( lat edges ) [1]
! Shadow variables of geo-"meteorological fields" required by HEMCO
!
! Hybrid Grid Coordinate Definition: (dsa, bmy, 8/27/02, 2/2/12)
! ============================================================================
!
! The pressure at the bottom edge of grid box (I,J,L) is defined as follows:
! Pedge(I,J,L) = Ap(L) + [ Bp(L) * Psurface(I,J) ]
! where
! Psurface(I,J) is the "true" surface pressure at lon,lat (I,J)
! Ap(L) has the same units as surface pressure [hPa]
! Bp(L) is a unitless constant given at level edges
!
! Note: I don't know what to do about PEDGE, surface pressure and the like.
! they likely need a regrid through ESMF from State%PSDry or something.
! This is only available in the GridComp Run, not worrying about it here.
! (hplin, 2/11/20)
real(r8), public, pointer :: &
AREA_M2(:,:), & ! Area of grid box [m^2]
Ap (:), & ! "hyai" Hybrid-sigma Ap value [Pa]
Bp (:) ! "hybi" Hybrid-sigma Bp value [Pa]
! MPI Descriptors.
! Ported mostly from edyn_geogrid and edyn_mpi
! -- What everyone knows --
integer, public, protected :: HCO_mpicom
integer, public, protected :: nPET ! Number of PETs
integer, public, protected :: nPET_lon, nPET_lat ! # of PETs over lon, lat
integer, public, allocatable :: HCO_petTable(:,:)! 2D table of tasks (dim'l nPET_lon+2, ..lat+2)
! extra left and right used for halos
integer, public, allocatable :: HCO_petMap(:,:,:)! PETmap for ESMF
! -- Private to MPI process --
! Note L dimension (levs) not distributed
integer, public, protected :: my_IM, my_JM ! # of lons, levs in this task
integer, public, protected :: my_IS, my_IE ! First and last lons
integer, public, protected :: my_JS, my_JE ! First and last lats
integer, public, protected :: my_ID ! my task ID in HCO_Task
integer, public, protected :: my_ID_I, my_ID_J ! mytidi, mytidj coord for current task
integer, public, protected :: my_CE ! # of CAM ncols in this task
type HCO_Task
integer :: ID ! identifier
integer :: ID_I ! task coord in longitude dim'l of task table
integer :: ID_J ! task coord in latitude dim'l of task table
integer :: IM ! # of lons on this task
integer :: JM ! # of lats on this task
integer :: IS, IE ! start and end longitude dim'l index
integer :: JS, JE ! start and end latitude dim'l index
end type HCO_Task
type(HCO_Task), allocatable:: HCO_Tasks(:) ! HCO_Tasks(nPET) avail to all tasks
!
! !PRIVATE TYPES:
!
! ESMF grid and meshes for regridding
type(ESMF_Grid) :: HCO_Grid
type(ESMF_Mesh) :: CAM_PhysMesh ! Copy of CAM physics mesh decomposition
type(ESMF_DistGrid) :: CAM_DistGrid ! DE-local allocation descriptor DistGrid (2D)
! ESMF fields for mapping between HEMCO to CAM fields
type(ESMF_Field) :: CAM_2DFld, CAM_3DFld
type(ESMF_Field) :: HCO_2DFld, HCO_3DFld
! Used to generate regridding weights
integer :: cam_last_atm_id ! Last CAM atmospheric ID
! Regridding weight route handles
type(ESMF_RouteHandle) :: HCO2CAM_RouteHandle_3D, &
HCO2CAM_RouteHandle_2D, &
CAM2HCO_RouteHandle_3D, &
CAM2HCO_RouteHandle_2D
contains
!EOC
!------------------------------------------------------------------------------
! Harmonized Emissions Component (HEMCO) !
!------------------------------------------------------------------------------
!BOP
!
! !IROUTINE: HCO_Grid_Init
!
! !DESCRIPTION: Subroutine HCO\_Grid\_Init initializes the HEMCO-CAM interface
! grid descriptions and MPI distribution.
!\\
!\\
! !INTERFACE:
!
subroutine HCO_Grid_Init( IM_in, JM_in, nPET_in, RC )
!
! !USES:
!
! MPI Properties from CAM
! Even though CAM's principle is that only spmd_utils uses MPI,
! ionos code uses MPI very liberally. Unfortunately we have to follow
! this example as spmd_utils does not provide many of the relevant
! functionality like the communicator split. But keep this in mind.
use cam_logfile, only: iulog
use spmd_utils, only: CAM_mpicom => mpicom, CAM_npes => npes
use spmd_utils, only: MPI_SUCCESS
use spmd_utils, only: iam
use spmd_utils, only: masterproc
! Physical constants
use shr_const_mod, only: pi => shr_const_pi
use shr_const_mod, only: Re => shr_const_rearth
! Grid specifications and information from CAM
use hycoef, only: ps0, hyai, hybi ! Vertical specs
use ppgrid, only: pver ! # of levs
!
! !INPUT PARAMETERS:
!
integer, intent(in) :: IM_in, JM_in ! # lon, lat, lev global
integer, intent(in) :: nPET_in ! # of PETs to distribute to?
integer, intent(inout) :: RC ! Return code
!
! !REMARKS:
!
! !REVISION HISTORY:
! 11 Feb 2020 - H.P. Lin - Initial version
!EOP
!------------------------------------------------------------------------------
!BOC
!
! !LOCAL VARIABLES:
!
character(len=*), parameter :: subname = 'HCO_Grid_Init'
integer :: I, J, L, N
real(r8) :: SIN_N, SIN_S, PI_180
! MPI stuff
integer :: color
integer :: lons_per_task, lons_overflow
integer :: lats_per_task, lats_overflow
integer :: lon_beg, lon_end, lat_beg, lat_end, task_cnt
! Send and receive buffers
integer, allocatable :: itasks_send(:,:), itasks_recv(:,:)
! Reset CAM atmospheric ID because we know nothing about it (hplin, 2/20/20)
cam_last_atm_id = -999
! Some physical constants...
PI_180 = pi / 180.0_r8
! Accept external dimensions.
IM = IM_in
JM = JM_in
nPET = nPET_in
!-----------------------------------------------------------------------
! Compute vertical grid parameters
!-----------------------------------------------------------------------
! Can be directly retrieved from hyai, hybi
! Although they need to be flipped to be passed from CAM (from tfritz)
!
! Note: In GEOS-Chem, Ap, Bp are defined in hPa, 1
! but in HEMCO, they are defined as Pa, 1 (see hcoi_gc_main_mod.F90 :2813)
! So you have to be especially wary of the units.
! For now, use the CAM vertical grid verbatim
LM = pver
! Ap, Bp has LM+1 edges for LM levels
allocate(Ap(LM + 1), STAT=RC) ! LM levels, LM+1 edges
allocate(Bp(LM + 1), STAT=RC)
ASSERT_(RC==0)
! Allocate PEDGE information
! G-C def: Pedge(I,J,L) = Ap(L) + [ Bp(L) * Psurface(I,J) ]
! CAM def: Pifce( L) = hyai(k)*ps0 + [ hybi(k) * ps ]
! w.r.t. ps0 = base state srfc prs; ps = ref srfc prs.
!
! Note that the vertical has to be flipped and this will need to be done
! everywhere else within HEMCO_CESM, too.
do L = 1, (LM+1)
Ap(L) = hyai(LM+2-L) * ps0
Bp(L) = hybi(LM+2-L)
enddo
!-----------------------------------------------------------------------
! Compute horizontal grid parameters
!-----------------------------------------------------------------------
! Notes: long range (i) goes from -180.0_r8 to +180.0_r8
! lat range (j) goes from - 90.0_r8 to + 90.0_r8
allocate(XMid (IM, JM ), STAT=RC)
allocate(XEdge(IM+1, JM ), STAT=RC)
allocate(YMid (IM, JM ), STAT=RC)
allocate(YEdge(IM, JM+1), STAT=RC)
allocate(YEdge_R(IM, JM+1), STAT=RC)
allocate(YSin (IM, JM+1), STAT=RC)
allocate(AREA_M2(IM, JM ), STAT=RC)
ASSERT_(RC==0)
! Compute DX, DY (lon, lat)
DX = 360.0_r8 / real(IM, r8)
DY = 180.0_r8 / real((JM - 1), r8)
! Loop over horizontal grid
! Note: Might require special handling at poles. FIXME. (hplin, 2/11/20)
do J = 1, JM
do I = 1, IM
! Longitude centers [deg]
XMid(I, J) = (DX * (I-1)) - 180.0_r8
! Latitude centers [deg]
YMid(I, J) = (DY * (J-1)) - 90.0_r8
! Note half-sized polar boxes for global grid, multiply DY by 1/4 at poles
if(J == 1) then
YMid(I, 1) = -90.0_r8 + (0.25_r8 * DY)
endif
if(J == JM) then
YMid(I, JM) = 90.0_r8 - (0.25_r8 * DY)
endif
! Edges [deg] (or called corners in CAM ionos speak)
XEdge(I, J) = XMid(I, J) - DX * 0.5_r8
YEdge(I, J) = YMid(I, J) - DY * 0.5_r8
YEdge_R(I, J) = (PI_180 * YEdge(I, J))
YSin (I, J) = SIN( YEdge_R(I, J) ) ! Needed for MAP_A2A regridding
! Compute the LAST edges
if(I == IM) then
XEdge(I+1,J) = XEdge(I, J) + DX
endif
! Enforce half-sized polar boxes where northern edge of grid boxes
! along the SOUTH POLE to be -90 deg lat.
if(J == 1) then
YEdge(I, 1) = -90.0_r8
endif
if(J == JM) then
! Northern edge of grid boxes along the north pole to be +90 deg lat
YEdge(I,J+1) = 90.0_r8
! Adjust for second-to-last lat edge
YEdge(I,J ) = YEdge(I,J+1) - (DY * 0.5_r8)
YEdge_R(I,J) = YEdge(I,J) * PI_180
YSin(I, J) = SIN( YEdge_R(I, J) )
! Last latitude edge [radians]
YEdge_R(I,J+1) = YEdge(I,J+1) * PI_180
YSin(I,J+1) = SIN( YEdge_R(I,J+1) )
endif
enddo
enddo
! Compute grid box areas after everything is populated...
do J = 1, JM
do I = 1, IM
! Sine of latitudes at N and S edges of grid box (I,J)
SIN_N = SIN( YEdge_R(I,J+1) )
SIN_S = SIN( YEdge_R(I,J ) )
! Grid box surface areas [m2]
AREA_M2(I,J) = ( DX * PI_180 ) * ( Re**2 ) * (SIN_N - SIN_S)
enddo
enddo
! Output debug information on the global grid information
! Copied from gc_grid_mod.F90 and pressure_mod.F
if(masterproc) then
write( iulog, '(a)' )
write( iulog, '(''%%%%%%%%%%%%%%% HEMCO GRID %%%%%%%%%%%%%%%'')' )
write( iulog, '(a)' )
write( iulog, *) 'DX', DX, 'DY', DY
write( iulog, '(''Grid box longitude centers [degrees]: '')' )
write( iulog, * ) size(XMid, 1), size(XMid, 2)
write( iulog, '(8(f8.3,1x))' ) ( XMid(I,1), I=1,IM )
write( iulog, '(a)' )
write( iulog, '(''Grid box longitude edges [degrees]: '')' )
write( iulog, * ) size(XEdge, 1), size(XEdge, 2)
write( iulog, '(8(f8.3,1x))' ) ( XEdge(I,1), I=1,IM+1 )
write( iulog, '(a)' )
write( iulog, '(''Grid box latitude centers [degrees]: '')' )
write( iulog, * ) size(YMid, 1), size(YMid, 2)
write( iulog, '(8(f8.3,1x))' ) ( YMid(1,J), J=1,JM )
write( iulog, '(a)' )
write( iulog, '(''Grid box latitude edges [degrees]: '')' )
write( iulog, * ) size(YEdge, 1), size(YEdge, 2)
write( iulog, '(8(f8.3,1x))' ) ( YEdge(1,J), J=1,JM+1 )
write( iulog, '(a)' )
write( iulog, '(''SIN( grid box latitude edges )'')' )
write( iulog, '(8(f8.3,1x))' ) ( YSin(1,J), J=1,JM+1 )
write( iulog, '(a)' ) REPEAT( '=', 79 )
write( iulog, '(a,/)' ) 'V E R T I C A L G R I D S E T U P'
write( iulog, '( ''Ap '', /, 6(f14.6,1x) )' ) AP(1:LM+1)
write( iulog, '(a)' )
write( iulog, '( ''Bp '', /, 6(f14.6,1x) )' ) BP(1:LM+1)
write( iulog, '(a)' ) REPEAT( '=', 79 )
endif
!-----------------------------------------------------------------------
! Distribute among parallelization in MPI 1: Compute distribution
!-----------------------------------------------------------------------
! edyn_geogrid uses 1-D latitude decomposition, so nPET_lon = 1
! and nPET_lat = JM. From tfritz this may not work well with GEOS-Chem
! so we may need to attempt some other decomposition in the future.
!
! The code below from edyn_geogrid may not be generic enough for that
! need, so we might do nPET_lon = 1 for now. (See code path below)
! (hplin, 2/13/20)
! It seems like ESMF conservative regridding will not work with DE < 2
! so the distribution must assign more than 1 lat and lon per PET.
! The code has been updated accordingly. (hplin, 2/22/20)
do nPET_lon = 2, IM
nPET_lat = nPET / nPET_lon
if( nPET_lon * nPET_lat == nPET .and. nPET_lon .le. IM .and. nPET_lat .le. JM ) then
! Enforce more than 2-width lon and lat...
if(int(IM / nPET_lon) > 1 .and. int(JM / nPET_lat) > 1) then
exit
endif
endif
enddo
! nPET_lon = 1
! nPET_lat = nPET
! Verify we have a correct decomposition
! Can't accept invalid decomp; also cannot accept IM, 1 (for sake of consistency)
if( nPET_lon * nPET_lat /= nPET .or. nPET_lat == 1 ) then
! Fall back to same 1-D latitude decomposition like edyn_geogrid
nPET_lon = 1
nPET_lat = nPET
if(masterproc) then
write(iulog,*) "HEMCO: HCO_Grid_Init failed to find a secondary decomp."
write(iulog,*) "Using 1-d latitude decomp. This may fail with ESMF regrid."
endif
endif
! Verify for the 1-D latitude decomposition case (edge case)
! if the number of CPUs is reasonably set. If nPET_lon = 1, then you cannot
! allow nPET_lat exceed JM or it will blow up.
if(nPET_lon == 1 .and. nPET_lat == nPET) then
if(nPET_lat > JM) then
if(masterproc) then
write(iulog,*) "HEMCO: Warning: Input nPET > JM", nPET, JM
write(iulog,*) "HEMCO: I will use nPET = JM for now."
endif
endif
endif
! Commit to the decomposition at this point
if(masterproc) then
write(iulog,*) "HEMCO: HCO_Grid_Init IM, JM, LM", IM, JM, LM
write(iulog,*) "HEMCO: nPET_lon * nPET_lat = ", nPET_lon, nPET_lat, nPET
endif
! Figure out beginning and ending coordinates for each task
! copied from edyn_geogrid
lons_per_task = IM / nPET_lon
lons_overflow = MOD(IM, nPET_lon)
lats_per_task = JM / nPET_lat
lats_overflow = MOD(JM, nPET_lat)
lon_beg = 1
lon_end = 0
lat_beg = 1
lat_end = 0
task_cnt = 0
jloop: do J = 0, nPET_lat - 1
lat_beg = lat_end + 1
lat_end = lat_beg + lats_per_task - 1
if (J < lats_overflow) then
lat_end = lat_end + 1
endif
lon_end = 0
do I = 0, nPET_lon - 1
lon_beg = lon_end + 1
lon_end = lon_beg + lons_per_task - 1
if (I < lons_overflow) then
lon_end = lon_end + 1
endif
task_cnt = task_cnt+1
if (task_cnt > iam) exit jloop ! This makes this loop CPU specific
enddo
enddo jloop
!-----------------------------------------------------------------------
! Distribute among parallelization in MPI 2: Populate indices and task table
!-----------------------------------------------------------------------
! Create communicator
! Color may be unnecessary if using all CAM processes for CAM_mpicom
! but we will retain this functionality for now incase needed (hplin, 2/12/20)
color = iam / (nPET_lat * nPET_lon)
call MPI_comm_split(CAM_mpicom, color, iam, HCO_mpicom, RC)
ASSERT_(RC==MPI_SUCCESS)
! Distribute among MPI (mp_distribute_geo in edyn_geogrid)
! Merged all into this huge monolithic routine..
! (lon_beg, lon_end, lat_beg, lat_end, 1, LM, nPET_lon, nPET_lat)
! (lonndx0, lonndx1, latndx0, latndx1, levndx0, levndx1, ntaski_in, ntaskj_in)
! Get my indices!
my_IS = lon_beg
my_IE = lon_end
my_JS = lat_beg
my_JE = lat_end
! Allocate task info table
allocate(HCO_Tasks(0:nPET-1), stat=RC)
! Allocate 2D table of TASKS (not i j coordinates)
allocate(HCO_petTable(-1:nPET_lon, -1:nPET_lat), stat=RC)
ASSERT_(RC==0)
! Allocate PET map for ESMF
allocate(HCO_petMap(nPET_lon, nPET_lat, 1))
ASSERT_(RC==0)
! 2D table of tasks communicates to MPI_PROC_NULL by default so talking
! to that PID has no effect in MPI comm
HCO_petTable(:,:) = MPI_PROC_NULL
! Figure out ranks for the petTable, which is a table of I, J PETs
! with halo (hplin, 2/12/20)
my_ID = iam
N = 0
do J = 0, nPET_lat-1
do I = 0, nPET_lon-1
HCO_petTable(I, J) = N
HCO_petMap(I+1, J+1, 1) = N ! PETmap indices based off 1, so +1 here
if(iam == N) then
my_ID_I = I
my_ID_J = J ! Found my place in the PET table
endif
N = N + 1 ! move on to the next rank
enddo
! Tasks are periodic in longitude (from edyn_mpi) for haloing
! FIXME: Check if this is true in HCO distribution. Maybe not
HCO_petTable(-1, J) = HCO_petTable(nPET_lon-1, J)
HCO_petTable(nPET_lon, J) = HCO_petTable(0, J)
enddo
! Print some debug information on the distribution
if( .false. ) then
! FIXME Don't write to 6 once finished debugging, this literally
! writes to cesm.log
write(6, "('HEMCO: MPIGrid mytid=',i4,' my_IM, my_JM=',2i4,' my_ID_I,J=',2i4, &
' lon0,1=',2i4,' lat0,1=',2i4)") &
my_ID,my_IM,my_JM,my_ID_I,my_ID_J,my_IS,my_IE,my_JS,my_JE
! write(iulog,"(/,'nPET=',i3,' nPET_lon=',i2,' nPET_lat=',i2,' Task Table:')") &
! nPET,nPET_lon,nPET_lat
! do J=-1,nPET_lat
! write(iulog,"('J=',i3,' HCO_petTable(:,J)=',100i3)") J,HCO_petTable(:,J)
! enddo
endif
! Each task should know its role now...
my_IM = my_IE - my_IS + 1
my_JM = my_JE - my_JS + 1
! Fill all PET info arrays with our information first
do N = 0, nPET-1
HCO_Tasks(N)%ID = iam ! identifier
HCO_Tasks(N)%ID_I = my_ID_I ! task coord in longitude dim'l of task table
HCO_Tasks(N)%ID_J = my_ID_J ! task coord in latitude dim'l of task table
HCO_Tasks(N)%IM = my_IM ! # of lons on this task
HCO_Tasks(N)%JM = my_JM ! # of lats on this task
HCO_Tasks(N)%IS = my_IS
HCO_Tasks(N)%IE = my_IE ! start and end longitude dim'l index
HCO_Tasks(N)%JS = my_JS
HCO_Tasks(N)%JE = my_JE ! start and end latitude dim'l index
enddo
! For root task write out a debug output to make sure
if(masterproc) then
write(iulog,*) ">> HEMCO: Root task committing to sub-decomp"
write(iulog,*) ">> my_IM,JM,IS,IE,JS,JE", my_IM, my_JM, my_IS, my_IE, my_JS, my_JE
endif
!-----------------------------------------------------------------------
! Distribute among parallelization in MPI 3: Distribute all-to-all task info
!-----------------------------------------------------------------------
! After this, exchange task information between everyone so we are all
! on the same page. This was called from edynamo_init in the ionos code.
! We adapt the whole mp_exchange_tasks code here...
! Note: 9 here is the length of the HCO_Tasks(N) component.
#define HCO_TASKS_ITEM_LENGTH 9
allocate(itasks_send(HCO_TASKS_ITEM_LENGTH, 0:nPET-1), stat=RC)
allocate(itasks_recv(HCO_TASKS_ITEM_LENGTH, 0:nPET-1), stat=RC)
ASSERT_(RC==0)
! Fill my send PET info array
do N = 0, nPET-1
itasks_send(1,N) = iam ! %ID identifier
itasks_send(2,N) = my_ID_I ! %ID_I task coord in longitude dim'l of task table
itasks_send(3,N) = my_ID_J ! %ID_J task coord in latitude dim'l of task table
itasks_send(4,N) = my_IM ! %IM # of lons on this task
itasks_send(5,N) = my_JM ! %JM # of lats on this task
itasks_send(6,N) = my_IS ! %IS
itasks_send(7,N) = my_IE ! %IE start and end longitude dim'l index
itasks_send(8,N) = my_JS ! %JS
itasks_send(9,N) = my_JE ! %JE start and end latitude dim'l index
enddo
! Send MPI all-to-all
call mpi_alltoall(itasks_send, HCO_TASKS_ITEM_LENGTH, MPI_INTEGER, &
itasks_recv, HCO_TASKS_ITEM_LENGTH, MPI_INTEGER, &
CAM_mpicom, RC)
ASSERT_(RC==MPI_SUCCESS)
! Unpack receiving data back
do N = 0, nPET-1
HCO_Tasks(N)%ID = itasks_recv(1,N)
HCO_Tasks(N)%ID_I = itasks_recv(2,N)
HCO_Tasks(N)%ID_J = itasks_recv(3,N)
HCO_Tasks(N)%IM = itasks_recv(4,N)
HCO_Tasks(N)%JM = itasks_recv(5,N)
HCO_Tasks(N)%IS = itasks_recv(6,N)
HCO_Tasks(N)%IE = itasks_recv(7,N)
HCO_Tasks(N)%JS = itasks_recv(8,N)
HCO_Tasks(N)%JE = itasks_recv(9,N)
! Debug output for masterproc
! if(masterproc) then
! write(iulog,*) "(mp) Task ", N
! write(iulog,*) "%ID ", HCO_Tasks(N)%ID
! write(iulog,*) "%ID_I", HCO_Tasks(N)%ID_I
! write(iulog,*) "%ID_J", HCO_Tasks(N)%ID_J
! write(iulog,*) "%IM ", HCO_Tasks(N)%IM
! write(iulog,*) "%JM ", HCO_Tasks(N)%JM
! write(iulog,*) "%IS ", HCO_Tasks(N)%IS
! write(iulog,*) "%IE ", HCO_Tasks(N)%IE
! write(iulog,*) "%JS ", HCO_Tasks(N)%JS
! write(iulog,*) "%JE ", HCO_Tasks(N)%JE
! endif
enddo
! Reclaim space
deallocate(itasks_send)
deallocate(itasks_recv)
if(masterproc) then
! Output information on the decomposition
write(iulog,*) "Committed HCO_Tasks decomposition:"
write(iulog,*) "nPET = ", nPET, ", nPET_lon = ", nPET_lon, ", nPET_lat = ", nPET_lat
do N = 0, nPET-1
! more info to print if needed
!write(iulog,*) "PET", N, " ID", HCO_Tasks(N)%ID, " ID_I", HCO_Tasks(N)%ID_I, " ID_J", HCO_Tasks(N)%ID_J, &
! "IM (IS,IE)", HCO_Tasks(N)%IM, HCO_Tasks(N)%IS, HCO_Tasks(N)%IE, " // JM (JS, JE)", HCO_Tasks(N)%JM, HCO_Tasks(N)%JS, HCO_Tasks(N)%JE
enddo
endif
!
! Just remember that my_IM, my_JM ... are your keys to generating
! the relevant met fields and passing to HEMCO.
!
! Only HCO_ESMF_Grid should be aware of the entire grid.
! Everyone else should be just doing work on its subset indices.
!
RC = ESMF_SUCCESS
end subroutine HCO_Grid_Init
!EOC
!------------------------------------------------------------------------------
! Harmonized Emissions Component (HEMCO) !
!------------------------------------------------------------------------------
!BOP
!
! !IROUTINE: HCO_Grid_UpdateRegrid
!
! !DESCRIPTION: Subroutine HCO\_Grid\_UpdateRegrid initializes or updates the
! regridding information used in the HEMCO_CESM interface.
!\\
!\\
! !INTERFACE:
!
subroutine HCO_Grid_UpdateRegrid( RC )
!
! !USES:
!
! MPI Properties from CAM
! Even though CAM's principle is that only spmd_utils uses MPI,
! ionos code uses MPI very liberally. Unfortunately we have to follow
! this example as spmd_utils does not provide many of the relevant
! functionality like the communicator split. But keep this in mind.
use cam_logfile, only: iulog
use spmd_utils, only: CAM_mpicom => mpicom, CAM_npes => npes
use spmd_utils, only: MPI_SUCCESS
use spmd_utils, only: iam
use spmd_utils, only: masterproc
use phys_grid, only: get_ncols_p
use ppgrid, only: begchunk, endchunk
use cam_instance, only: atm_id
! ESMF
use ESMF, only: ESMF_FieldRegridStore
use ESMF, only: ESMF_REGRIDMETHOD_BILINEAR, ESMF_REGRIDMETHOD_CONSERVE
use ESMF, only: ESMF_POLEMETHOD_ALLAVG, ESMF_POLEMETHOD_NONE
use ESMF, only: ESMF_EXTRAPMETHOD_NEAREST_IDAVG
use ESMF, only: ESMF_FieldGet
!
! !OUTPUT PARAMETERS:
!
integer, intent(out) :: RC
!
! !REMARKS:
! This field will ONLY update if it recognizes a change in the CAM instance
! information, as determined by cam_instance::atm_id which is saved in the
! module's cam_last_atm_id field.
!
! This allows the function HCO_Grid_UpdateRegrid be called both in init and run
! without performance / memory repercussions (hopefully...)
!
! Maybe we can use CONSERVE_2ND order for better accuracy in the future. To be tested.
!
! !REVISION HISTORY:
! 20 Feb 2020 - H.P. Lin - Initial version
! 23 Feb 2020 - H.P. Lin - Finalized regridding handles, bilinear for CAM-HCO and
! conservative (1st order) for HCO-CAM.
! 29 Oct 2020 - H.P. Lin - Changed to 1st-order conservative based on feedback from T. Fritz
! about negative numerical artifacts coming out of ESMF.
!EOP
!------------------------------------------------------------------------------
!BOC
!
! !LOCAL VARIABLES:
!
character(len=*), parameter :: subname = 'HCO_Grid_UpdateRegrid'
integer :: smm_srctermproc, smm_pipelinedep
integer :: chnk
! Debug only
integer :: lbnd_hco(3), ubnd_hco(3) ! 3-d bounds of HCO field
integer :: lbnd_cam(2), ubnd_cam(2) ! 3-d bounds of CAM field
real(r8), pointer :: fptr(:,:,:) ! debug
real(r8), pointer :: fptr_cam(:,:) ! debug
! Assume success
RC = ESMF_SUCCESS
! Parameters for ESMF RouteHandle (taken from ionos interface)
smm_srctermproc = 0
! smm_pipelinedep = -1 ! Accept auto-tuning of the pipeline depth
smm_pipelinedep = 16 ! Prescribe pipeline depth for BFB consistency (hplin, 6/7/23)
! Check if we need to update
if(cam_last_atm_id == atm_id) then
if(masterproc) then
write(iulog,'(A,I2)') "HEMCO_CESM: UpdateRegrid is already in atmospheric grid #", atm_id
endif
return
else
if(masterproc) then
write(iulog,'(A,I2)') "HEMCO_CESM: UpdateRegrid now updating for atmospheric grid #", atm_id
endif
endif
cam_last_atm_id = atm_id
! Create CAM physics mesh...
call HCO_Grid_ESMF_CreateCAM(RC)
ASSERT_(RC==ESMF_SUCCESS)
! How many columns in this task? my_CE
! Note that my_CE is LOCAL column index (1:my_CE) and is equivalent to "blksize"
! in the ionos coupling code. This was 144 in my testing.
! But this is NOT ppgrid::pcols, which was 16.
my_CE = 0
do chnk = begchunk, endchunk
my_CE = my_CE + get_ncols_p(chnk)
enddo
! Create HEMCO grid in ESMF format
call HCO_Grid_ESMF_CreateHCO(RC)
ASSERT_(RC==ESMF_SUCCESS)
! Create empty fields on the HEMCO grid and CAM physics mesh
! used for later regridding
! FIXME: Destroy fields before creating to prevent memory leak? ESMF_Destroy
! requires the field to be present (so no silent destruction) (hplin, 2/21/20)
call HCO_Grid_ESMF_CreateCAMField(CAM_2DFld, CAM_PhysMesh, 'HCO_PHYS_2DFLD', 0, RC)
call HCO_Grid_ESMF_CreateCAMField(CAM_3DFld, CAM_PhysMesh, 'HCO_PHYS_3DFLD', LM, RC)
call HCO_Grid_ESMF_CreateHCOField(HCO_2DFld, HCO_Grid, 'HCO_2DFLD', 0, RC)
call HCO_Grid_ESMF_CreateHCOField(HCO_3DFld, HCO_Grid, 'HCO_3DFLD', LM, RC)
ASSERT_(RC==ESMF_SUCCESS)
! if(masterproc) then
! write(iulog,*) ">> HEMCO: HCO_PHYS and HCO_ fields initialized successfully"
! call ESMF_FieldGet(HCO_3DFld, localDE=0, farrayPtr=fptr, &
! computationalLBound=lbnd_hco, &
! computationalUBound=ubnd_hco, rc=RC)
! write(iulog,*) ">> HEMCO: Debug HCO Field: lbnd = (", lbnd_hco, "), ", my_IS, my_IE, "ubnd = (", ubnd_hco, ")", my_JS, my_JE
! call ESMF_FieldGet(CAM_3DFld, localDE=0, farrayPtr=fptr_cam, &
! computationalLBound=lbnd_cam, &
! computationalUBound=ubnd_cam, rc=RC)
! write(iulog,*) ">> HEMCO: Debug CAM Field: lbnd = (", lbnd_cam, "), ubnd = (", ubnd_cam, ")", my_CE
! endif
! CAM -> HCO 2-D
call ESMF_FieldRegridStore( &
srcField=CAM_2DFld, dstField=HCO_2DFld, &
regridMethod=ESMF_REGRIDMETHOD_BILINEAR, &
poleMethod=ESMF_POLEMETHOD_ALLAVG, &
extrapMethod=ESMF_EXTRAPMETHOD_NEAREST_IDAVG, &
routeHandle=CAM2HCO_RouteHandle_2D, &
!srcTermProcessing=smm_srctermproc, &
pipelineDepth=smm_pipelinedep, rc=RC)
ASSERT_(RC==ESMF_SUCCESS)
if(masterproc) then
write(iulog,*) ">> After FieldRegridStore, CAM2D->HCO2D, pipeline", smm_pipelinedep
endif
! smm_pipelinedep = -1 ! Only replace with -1 to accept auto-tuning of smm pipeline depth.
! Create and store ESMF route handles for regridding CAM <-> HCO
! CAM -> HCO 3-D
call ESMF_FieldRegridStore( &
srcField=CAM_3DFld, dstField=HCO_3DFld, &
regridMethod=ESMF_REGRIDMETHOD_BILINEAR, &
poleMethod=ESMF_POLEMETHOD_ALLAVG, &
extrapMethod=ESMF_EXTRAPMETHOD_NEAREST_IDAVG, &
routeHandle=CAM2HCO_RouteHandle_3D, &
!srcTermProcessing=smm_srctermproc, &
pipelineDepth=smm_pipelinedep, rc=RC)
ASSERT_(RC==ESMF_SUCCESS)
if(masterproc) then
write(iulog,*) ">> After FieldRegridStore, CAM3D->HCO3D, pipeline", smm_pipelinedep
endif
! smm_pipelinedep = -1 ! Only replace with -1 to accept auto-tuning of smm pipeline depth.
! HCO -> CAM 2-D
call ESMF_FieldRegridStore( &
srcField=HCO_2DFld, dstField=CAM_2DFld, &
regridMethod=ESMF_REGRIDMETHOD_CONSERVE , &
poleMethod=ESMF_POLEMETHOD_NONE, &
routeHandle=HCO2CAM_RouteHandle_2D, &
!srcTermProcessing=smm_srctermproc, &
pipelineDepth=smm_pipelinedep, rc=RC)
ASSERT_(RC==ESMF_SUCCESS)
if(masterproc) then
write(iulog,*) ">> After FieldRegridStore, HCO2D->CAM2D, pipeline", smm_pipelinedep
endif
! smm_pipelinedep = -1 ! Only replace with -1 to accept auto-tuning of smm pipeline depth.
! HCO -> CAM 3-D
! 3-D conserv. regridding cannot be done on a stagger other than center
! (as of ESMF 8.0.0 in ESMF_FieldRegrid.F90::993)
call ESMF_FieldRegridStore( &
srcField=HCO_3DFld, dstField=CAM_3DFld, &
regridMethod=ESMF_REGRIDMETHOD_CONSERVE , &
poleMethod=ESMF_POLEMETHOD_NONE, &
routeHandle=HCO2CAM_RouteHandle_3D, &
!srcTermProcessing=smm_srctermproc, &
pipelineDepth=smm_pipelinedep, rc=RC)
ASSERT_(RC==ESMF_SUCCESS)
if(masterproc) then
write(iulog,*) ">> After FieldRegridStore, HCO3D->CAM3D, pipeline", smm_pipelinedep
endif
if(masterproc) then
write(iulog,*) "HEMCO_CESM: FieldRegridStore four-way complete", atm_id
endif
! Finished updating regrid routines
end subroutine HCO_Grid_UpdateRegrid
!EOC
!------------------------------------------------------------------------------
! Harmonized Emissions Component (HEMCO) !
!------------------------------------------------------------------------------
!BOP
!
! !IROUTINE: HCO_Grid_ESMF_CreateCAM
!
! !DESCRIPTION: Subroutine HCO\_Grid\_ESMF\_CreateCAM creates the physics mesh
! from CAM and stores in the ESMF state for regridding.
!\\
!\\
! !INTERFACE:
!
subroutine HCO_Grid_ESMF_CreateCAM( RC )
!
! !USES:
!
! MPI Properties from CAM
use cam_logfile, only: iulog
use spmd_utils, only: masterproc
! Phys constants
use shr_const_mod, only: pi => shr_const_pi
! Grid properties in CAM
use cam_instance, only: inst_name
use phys_control, only: phys_getopts
use phys_grid, only: get_ncols_p, get_gcol_p, get_rlon_all_p, get_rlat_all_p
use ppgrid, only: begchunk, endchunk
use ppgrid, only: pcols ! # of col max in chunks
! ESMF
use ESMF, only: ESMF_DistGridCreate, ESMF_MeshCreate
use ESMF, only: ESMF_MeshGet
use ESMF, only: ESMF_FILEFORMAT_ESMFMESH
use ESMF, only: ESMF_MeshIsCreated, ESMF_MeshDestroy
!
! !OUTPUT PARAMETERS:
!
integer, intent(out) :: RC
!
! !REMARKS:
!
! !REVISION HISTORY:
! 11 Feb 2020 - H.P. Lin - Initial version
!EOP
!------------------------------------------------------------------------------
!BOC
!
! !LOCAL VARIABLES:
!
character(len=*), parameter :: subname = 'HCO_Grid_ESMF_CreateCAM'
! For allocation of the distGrid and Mesh
integer :: ncols
integer :: chnk, col, dindex
integer, allocatable :: decomp(:)
integer :: col_total
character(len=256) :: grid_file
! For verification of the mesh
integer :: spatialDim
integer :: numOwnedElements
real(r8), pointer :: ownedElemCoords(:)
real(r8), pointer :: latCAM(:), latMesh(:)
real(r8), pointer :: lonCAM(:), lonMesh(:)
real(r8) :: latCAM_R(pcols) ! array of chunk lat
real(r8) :: lonCAM_R(pcols) ! array of chunk long
integer :: i, c, n
real(r8), parameter :: radtodeg = 180.0_r8/pi
! Assume success
RC = ESMF_SUCCESS