-
Notifications
You must be signed in to change notification settings - Fork 0
/
dyn_grid.F90
1572 lines (1234 loc) · 52.7 KB
/
dyn_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
module dyn_grid
!-------------------------------------------------------------------------------
!
! Define SE computational grids on the dynamics decomposition.
!
! The grid used by the SE dynamics is called the GLL grid. It is
! decomposed into elements which correspond to "blocks" in the
! physics/dynamics coupler terminology. The columns in this grid are
! located at the Gauss-Lobatto-Legendre (GLL) quadrature points. The GLL
! grid will also be used by the physics if the CSLAM advection is not used.
! If CSLAM is used for tracer advection then it uses an FVM grid and the
! physics will either use the same FVM grid or an FVM grid with a different
! number of equal area subcells. The FVM grid used by the physics is
! referred to as the "physgrid".
!
! Module responsibilities:
!
! . Provide the physics/dynamics coupler (in module phys_grid) with data for the
! physics grid on the dynamics decomposition.
!
! . Create CAM grid objects that are used by the I/O functionality to read
! data from an unstructured grid format to the dynamics data structures, and
! to write from the dynamics data structures to unstructured grid format. The
! global column ordering for the unstructured grid is determined by the SE dycore.
!
!-------------------------------------------------------------------------------
use shr_kind_mod, only: r8 => shr_kind_r8, shr_kind_cl
use spmd_utils, only: masterproc, iam, mpicom, mstrid=>masterprocid, &
npes, mpi_integer, mpi_real8, mpi_success
use constituents, only: pcnst
use physconst, only: pi
use cam_initfiles, only: initial_file_get_id
use cam_grid_support, only: iMap
use dp_mapping, only: dp_reoorder
use cam_logfile, only: iulog
use cam_abortutils, only: endrun
use shr_sys_mod, only: shr_sys_flush
use pio, only: file_desc_t, pio_seterrorhandling, pio_bcast_error, &
pio_internal_error, pio_noerr, pio_inq_dimid, &
pio_inq_dimlen
use dimensions_mod, only: globaluniquecols, nelem, nelemd, nelemdmax, &
ne, np, npsq, fv_nphys, nlev, nc, ntrac, &
qsize_condensate_loading
use element_mod, only: element_t
use fvm_control_volume_mod, only: fvm_struct
use hybvcoord_mod, only: hvcoord_t
use prim_init, only: prim_init1
use edge_mod, only: initEdgeBuffer
use edgetype_mod, only: EdgeBuffer_t
use time_mod, only: TimeLevel_t
use dof_mod, only: UniqueCoords, UniquePoints
implicit none
private
save
integer, parameter :: dyn_decomp = 101 ! The SE dynamics grid
integer, parameter :: fvm_decomp = 102 ! The FVM (CSLAM) grid
integer, parameter :: physgrid_d = 103 ! physics grid on dynamics decomp
integer, parameter :: ptimelevels = 2
type (TimeLevel_t) :: TimeLevel ! main time level struct (used by tracers)
type (hvcoord_t) :: hvcoord
type(element_t), pointer :: elem(:) => null() ! local GLL elements for this task
type(fvm_struct), pointer :: fvm(:) => null() ! local FVM elements for this task
public :: &
dyn_decomp, &
ptimelevels, &
TimeLevel, &
hvcoord, &
elem, &
fvm, &
edgebuf
public :: &
dyn_grid_init, &
get_block_bounds_d, & ! get first and last indices in global block ordering
get_block_gcol_d, & ! get column indices for given block
get_block_gcol_cnt_d, & ! get number of columns in given block
get_block_lvl_cnt_d, & ! get number of vertical levels in column
get_block_levels_d, & ! get vertical levels in column
get_block_owner_d, & ! get process "owning" given block
get_gcol_block_d, & ! get global block indices and local columns
! index for given global column index
get_gcol_block_cnt_d, & ! get number of blocks containing data
! from a given global column index
get_horiz_grid_dim_d, &
get_horiz_grid_d, & ! get horizontal grid coordinates
get_dyn_grid_parm, &
get_dyn_grid_parm_real1d, &
dyn_grid_get_elem_coords, & ! get coordinates of a specified block element
dyn_grid_get_colndx, & ! get element block/column and MPI process indices
! corresponding to a specified global column index
physgrid_copy_attributes_d
! Namelist variables controlling grid writing.
! Read in dyn_readnl from dyn_se_inparm group.
character(len=16), public :: se_write_grid_file = 'no'
character(len=shr_kind_cl), public :: se_grid_filename = ''
logical, public :: se_write_gll_corners = .false.
type block_global_data
integer :: UniquePtOffset ! global index of first column in element
integer :: NumUniqueP ! number of unique columns in element
integer :: LocalID ! local index of element in a task
integer :: Owner ! task id of element owner
end type block_global_data
! Only need this global data for the GLL grid if it is also the physics grid.
type(block_global_data), allocatable :: gblocks(:)
! number of global dynamics columns. Set by SE dycore init.
integer :: ngcols_d = 0
! number of global elements. Set by SE dycore init.
integer :: nelem_d = 0
real(r8), parameter :: rad2deg = 180.0_r8/pi
type(EdgeBuffer_t) :: edgebuf
!=============================================================================
contains
!=============================================================================
subroutine dyn_grid_init()
! Initialize SE grid, and decomposition.
use hycoef, only: hycoef_init, hypi, hypm, nprlev, &
hyam, hybm, hyai, hybi, ps0
use ref_pres, only: ref_pres_init
use spmd_utils, only: MPI_MAX, MPI_INTEGER, mpicom
use time_manager, only: get_nstep, get_step_size
use dp_mapping, only: dp_init, dp_write
use native_mapping, only: do_native_mapping, create_native_mapping_files
use parallel_mod, only: par
use hybrid_mod, only: hybrid_t, init_loop_ranges, &
get_loop_ranges, config_thread_region
use thread_mod , only: horz_num_threads
use control_mod, only: qsplit, rsplit
use time_mod, only: tstep, nsplit
use fvm_mod, only: fvm_init2, fvm_init3, fvm_pg_init
use dimensions_mod, only: irecons_tracer
use comp_gll_ctr_vol, only: gll_grid_write
! Local variables
type(file_desc_t), pointer :: fh_ini
integer :: qsize_local
integer :: k
type(hybrid_t) :: hybrid
integer :: ierr
integer :: neltmp(3)
integer :: dtime
real(r8), allocatable ::clat(:), clon(:), areaa(:)
integer :: nets, nete
character(len=*), parameter :: sub = 'dyn_grid_init'
!----------------------------------------------------------------------------
! Get file handle for initial file and first consistency check
fh_ini => initial_file_get_id()
! Initialize hybrid coordinate arrays
call hycoef_init(fh_ini, psdry=.true.)
hvcoord%hyam = hyam
hvcoord%hyai = hyai
hvcoord%hybm = hybm
hvcoord%hybi = hybi
hvcoord%ps0 = ps0
do k = 1, nlev
hvcoord%hybd(k) = hvcoord%hybi(k+1) - hvcoord%hybi(k)
end do
! Initialize reference pressures
call ref_pres_init(hypi, hypm, nprlev)
if (iam < par%nprocs) then
call prim_init1(elem, fvm, par, TimeLevel)
if (fv_nphys > 0) then
call dp_init(elem, fvm)
end if
if (fv_nphys > 0) then
qsize_local = qsize_condensate_loading + 3
else
qsize_local = pcnst + 3
end if
call initEdgeBuffer(par, edgebuf, elem, qsize_local*nlev, nthreads=1)
else ! auxiliary processes
globaluniquecols = 0
nelem = 0
nelemd = 0
nelemdmax = 0
endif
! nelemdmax is computed on the dycore comm, we need it globally.
ngcols_d = nelemdmax
call MPI_Allreduce(ngcols_d, nelemdmax, 1, MPI_INTEGER, MPI_MAX, mpicom, ierr)
! All pes might not have the correct global grid size
call MPI_Allreduce(globaluniquecols, ngcols_d, 1, MPI_INTEGER, MPI_MAX, mpicom, ierr)
! All pes might not have the correct number of elements
call MPI_Allreduce(nelem, nelem_d, 1, MPI_INTEGER, MPI_MAX, mpicom, ierr)
! nelemd (# of elements on this task) is set by prim_init1
call init_loop_ranges(nelemd)
! Dynamics timestep
!
! Note: dtime = timestep for physics/dynamics coupling
! tstep = the dynamics timestep:
dtime = get_step_size()
tstep = dtime / real(nsplit*qsplit*rsplit, r8)
TimeLevel%nstep = get_nstep()*nsplit*qsplit*rsplit
! initial SE (subcycled) nstep
TimeLevel%nstep0 = 0
! Define the dynamics and physics grids on the dynamics decompostion.
! Physics grid on the physics decomposition is defined in phys_grid_init.
call define_cam_grids()
if (fv_nphys > 0) then
! ================================================
! finish fvm initialization
! ================================================
if (iam < par%nprocs) then
!$OMP PARALLEL NUM_THREADS(horz_num_threads), DEFAULT(SHARED), PRIVATE(hybrid,nets,nete)
hybrid = config_thread_region(par,'serial')
call get_loop_ranges(hybrid, ibeg=nets, iend=nete)
! initialize halo coordinate variables for cslam and physgrid
call fvm_init2(elem, fvm, hybrid, nets, nete)
call fvm_pg_init(elem, fvm, hybrid, nets, nete, irecons_tracer)
call fvm_init3(elem, fvm, hybrid, nets, nete, irecons_tracer)
!$OMP END PARALLEL
end if
else
! construct global arrays needed when GLL grid used by physics
call gblocks_init()
end if
! write grid and mapping files
if (se_write_gll_corners) then
call write_grid_mapping(par, elem)
end if
if (trim(se_write_grid_file) /= "no") then
if (fv_nphys > 0) then
call dp_write(elem, fvm, trim(se_write_grid_file), trim(se_grid_filename))
else
call gll_grid_write(elem, trim(se_write_grid_file), trim(se_grid_filename))
end if
end if
if (do_native_mapping) then
allocate(areaA(ngcols_d))
allocate(clat(ngcols_d),clon(ngcols_d))
call get_horiz_grid_d(ngcols_d, clat_d_out=clat, clon_d_out=clon, area_d_out=areaA)
! Create mapping files using SE basis functions
call create_native_mapping_files(par, elem, 'native', ngcols_d, clat, clon, areaa)
call create_native_mapping_files(par, elem, 'bilin', ngcols_d, clat, clon, areaa)
deallocate(areaa, clat, clon)
end if
call mpi_barrier(mpicom, ierr)
end subroutine dyn_grid_init
!=========================================================================================
subroutine get_block_bounds_d(block_first, block_last)
! Return first and last indices used in global block ordering
integer, intent(out) :: block_first ! first (global) index used for blocks
integer, intent(out) :: block_last ! last (global) index used for blocks
!----------------------------------------------------------------------------
block_first = 1
block_last = nelem_d
end subroutine get_block_bounds_d
!=========================================================================================
subroutine get_block_gcol_d(blockid, asize, cdex)
! Return list of global column indices in given block
!------------------------------Arguments--------------------------------
integer, intent(in) :: blockid ! global block id
integer, intent(in) :: asize ! array size
integer, intent(out):: cdex(asize) ! global column indices
integer :: ic
!----------------------------------------------------------------------------
if (fv_nphys > 0) then
cdex(1) = (blockid-1)*fv_nphys*fv_nphys + 1
do ic = 2, asize
cdex(ic) = cdex(1) + ic - 1
end do
else
do ic = 1, asize
cdex(ic) = gblocks(blockid)%UniquePtOffset + ic - 1
end do
end if
end subroutine get_block_gcol_d
!=========================================================================================
integer function get_block_gcol_cnt_d(blockid)
! Return number of dynamics columns in indicated block
integer, intent(in) :: blockid
integer :: ie
!----------------------------------------------------------------------------
if (fv_nphys > 0) then
get_block_gcol_cnt_d = fv_nphys*fv_nphys
else
get_block_gcol_cnt_d = gblocks(blockid)%NumUniqueP
end if
end function get_block_gcol_cnt_d
!=========================================================================================
integer function get_block_lvl_cnt_d(blockid, bcid)
! Return number of levels in indicated column. If column
! includes surface fields, then it is defined to also
! include level 0.
use pmgrid, only: plevp
integer, intent(in) :: blockid ! global block id
integer, intent(in) :: bcid ! column index within block
!-----------------------------------------------------------------------
get_block_lvl_cnt_d = plevp
end function get_block_lvl_cnt_d
!=========================================================================================
subroutine get_block_levels_d(blockid, bcid, lvlsiz, levels)
use pmgrid, only: plev
! Return level indices in indicated column. If column
! includes surface fields, then it is defined to also
! include level 0.
! arguments
integer, intent(in) :: blockid ! global block id
integer, intent(in) :: bcid ! column index within block
integer, intent(in) :: lvlsiz ! dimension of levels array
integer, intent(out) :: levels(lvlsiz) ! levels indices for block
! local variables
integer :: k
character(len=128) :: errmsg
!---------------------------------------------------------------------------
if (lvlsiz < plev + 1) then
write(errmsg,*) 'levels array not large enough (', lvlsiz,' < ',plev + 1,')'
call endrun('GET_BLOCK_LEVELS_D: '//trim(errmsg))
else
do k = 0, plev
levels(k+1) = k
end do
do k = plev+2, lvlsiz
levels(k) = -1
end do
end if
end subroutine get_block_levels_d
!=========================================================================================
integer function get_gcol_block_cnt_d(gcol)
! Return number of blocks containg data for the vertical column with the
! given global column index.
!
! For SE dycore each column is "owned" by a single element, so this routine
! always returns 1.
integer, intent(in) :: gcol ! global column index
!----------------------------------------------------------------------------
get_gcol_block_cnt_d = 1
end function get_gcol_block_cnt_d
!=========================================================================================
subroutine get_gcol_block_d(gcol, cnt, blockid, bcid, localblockid)
use dp_mapping, only: dp_owner
! Return global block index and local column index for given global column index.
!
! The SE dycore assigns each global column to a singe element. So cnt is assumed
! to be 1.
! arguments
integer, intent(in) :: gcol ! global column index
integer, intent(in) :: cnt ! size of blockid and bcid arrays
integer, intent(out) :: blockid(cnt) ! block index
integer, intent(out) :: bcid(cnt) ! column index within block
integer, intent(out), optional :: localblockid(cnt)
! local variables
integer :: sb, eb, ie, high, low
logical :: found
integer, save :: iedex_save = 1
character(len=*), parameter :: subname='get_gcol_block_d'
!----------------------------------------------------------------------------
if (fv_nphys > 0) then
blockid(1) = 1 + ((gcol-1) / (fv_nphys*fv_nphys))
bcid(1) = 1 + mod(gcol-1, fv_nphys*fv_nphys)
if (present(localblockid)) then
localblockid = -1
if (iam == dp_owner(blockid(1))) then
if (blockid(1) == elem(iedex_save)%globalid) then
localblockid = iedex_save
else
do ie = 1,nelemd
if (blockid(1) == elem(ie)%globalid) then
localblockid = ie
iedex_save = ie
exit
end if
end do
end if
end if
end if
else
found = .false.
low = 1
high = nelem_d
! check whether previous found element is the same here
if (.not. found) then
ie = iedex_save
sb = gblocks(ie)%UniquePtOffset
if (gcol >= sb) then
eb = sb + gblocks(ie)%NumUniqueP
if (gcol < eb) then
found = .true.
else
low = ie
endif
else
high = ie
endif
endif
! check whether next element is the one wanted
if ((.not. found) .and. &
((low .eq. iedex_save) .or. (iedex_save .eq. nelem_d))) then
ie = iedex_save + 1
if (ie > nelem_d) ie = 1
sb = gblocks(ie)%UniquePtOffset
if (gcol >= sb) then
eb = sb + gblocks(ie)%NumUniqueP
if (gcol < eb) then
found = .true.
else
low = ie
endif
else
high = ie
endif
endif
! otherwise, use a binary search to find element
if (.not. found) then
! (start with a sanity check)
ie = low
sb = gblocks(ie)%UniquePtOffset
ie = high
eb = gblocks(ie)%UniquePtOffset + gblocks(ie)%NumUniqueP
if ((gcol < sb) .or. (gcol >= eb)) then
do ie=1,nelemd
write(iulog,*) __LINE__,ie,elem(ie)%idxP%UniquePtOffset,elem(ie)%idxP%NumUniquePts
end do
call endrun(subname//': binary search to find element')
end if
do while (.not. found)
ie = low + (high-low)/2;
sb = gblocks(ie)%UniquePtOffset
if (gcol >= sb) then
eb = sb + gblocks(ie)%NumUniqueP
if (gcol < eb) then
found = .true.
else
low = ie+1
end if
else
high = ie-1
end if
end do
end if
blockid(1) = ie
bcid(1) = gcol - sb + 1
iedex_save = ie
if (present(localblockid)) localblockid(1) = gblocks(ie)%LocalID
end if
end subroutine get_gcol_block_d
!=========================================================================================
integer function get_block_owner_d(blockid)
! Return id of processor that "owns" the indicated block
use dp_mapping, only: dp_owner
integer, intent(in) :: blockid ! global block id
character(len=*), parameter :: name = 'get_block_owner_d'
!----------------------------------------------------------------------------
if (fv_nphys > 0) then
if (dp_owner(blockid) > -1) then
get_block_owner_d = dp_owner(blockid)
else
call endrun(name//': Block owner not assigned in gblocks_init')
end if
else
if (gblocks(blockid)%Owner > -1) then
get_block_owner_d = gblocks(blockid)%Owner
else
call endrun(name//': Block owner not assigned in gblocks_init')
end if
end if
end function get_block_owner_d
!=========================================================================================
subroutine get_horiz_grid_dim_d(hdim1_d,hdim2_d)
! Returns declared horizontal dimensions of computational grid.
! For non-lon/lat grids, declare grid to be one-dimensional,
! i.e., (ngcols_d x 1)
!------------------------------Arguments--------------------------------
integer, intent(out) :: hdim1_d ! first horizontal dimension
integer, intent(out), optional :: hdim2_d ! second horizontal dimension
!-----------------------------------------------------------------------
if (fv_nphys > 0) then
hdim1_d = fv_nphys*fv_nphys*nelem_d
else
hdim1_d = ngcols_d
end if
if (present(hdim2_d)) then
hdim2_d = 1
end if
end subroutine get_horiz_grid_dim_d
!=========================================================================================
subroutine get_horiz_grid_d(nxy, clat_d_out, clon_d_out, area_d_out, &
wght_d_out, lat_d_out, lon_d_out)
! Return global arrays of latitude and longitude (in radians), column
! surface area (in radians squared) and surface integration weights for
! global column indices that will be passed to/from physics
! arguments
integer, intent(in) :: nxy ! array sizes
real(r8), intent(out), optional :: clat_d_out(:) ! column latitudes
real(r8), intent(out), optional :: clon_d_out(:) ! column longitudes
real(r8), intent(out), target, optional :: area_d_out(:) ! column surface
real(r8), intent(out), target, optional :: wght_d_out(:) ! column integration weight
real(r8), intent(out), optional :: lat_d_out(:) ! column degree latitudes
real(r8), intent(out), optional :: lon_d_out(:) ! column degree longitudes
! local variables
real(r8), pointer :: area_d(:)
real(r8), pointer :: temp(:)
character(len=256) :: errormsg
character(len=*), parameter :: sub = 'get_horiz_grid_d'
!----------------------------------------------------------------------------
! check that nxy is set to correct size for global arrays
if (fv_nphys > 0) then
if (nxy < fv_nphys*fv_nphys*nelem_d) then
write(errormsg, *) sub//': arrays too small; Passed', &
nxy, ', needs to be at least', fv_nphys*fv_nphys*nelem_d
call endrun(errormsg)
end if
else
if (nxy < ngcols_d) then
write(errormsg,*) sub//': arrays not large enough; ', &
'Passed', nxy, ', needs to be at least', ngcols_d
call endrun(errormsg)
end if
end if
if ( present(area_d_out) ) then
if (size(area_d_out) /= nxy) then
call endrun(sub//': bad area_d_out array size')
end if
area_d => area_d_out
call create_global_area(area_d)
else if ( present(wght_d_out) ) then
if (size(wght_d_out) /= nxy) then
call endrun(sub//': bad wght_d_out array size')
end if
area_d => wght_d_out
call create_global_area(area_d)
end if
! If one of area_d_out or wght_d_out was present, then it was computed
! above. If they were *both* present, then do this:
if ( present(area_d_out) .and. present(wght_d_out) ) then
wght_d_out(:) = area_d_out(:)
end if
if (present(clon_d_out)) then
if (size(clon_d_out) /= nxy) then
call endrun(sub//': bad clon_d_out array size in dyn_grid')
end if
end if
if (present(clat_d_out)) then
if (size(clat_d_out) /= nxy) then
call endrun('bad clat_d_out array size in dyn_grid')
end if
if (present(clon_d_out)) then
call create_global_coords(clat_d_out, clon_d_out, lat_d_out, lon_d_out)
else
allocate(temp(nxy))
call create_global_coords(clat_d_out, temp, lat_d_out, lon_d_out)
deallocate(temp)
end if
else if (present(clon_d_out)) then
allocate(temp(nxy))
call create_global_coords(temp, clon_d_out, lat_d_out, lon_d_out)
deallocate(temp)
end if
end subroutine get_horiz_grid_d
!=========================================================================================
subroutine physgrid_copy_attributes_d(gridname, grid_attribute_names)
! create list of attributes for the physics grid that should be copied
! from the corresponding grid object on the dynamics decomposition
use cam_grid_support, only: max_hcoordname_len
! Dummy arguments
character(len=max_hcoordname_len), intent(out) :: gridname
character(len=max_hcoordname_len), pointer, intent(out) :: grid_attribute_names(:)
if (fv_nphys > 0) then
gridname = 'physgrid_d'
allocate(grid_attribute_names(2))
grid_attribute_names(1) = 'fv_nphys'
grid_attribute_names(2) = 'ne'
else
gridname = 'GLL'
allocate(grid_attribute_names(3))
! For standard CAM-SE, we need to copy the area attribute.
! For physgrid, the physics grid will create area (GLL has area_d)
grid_attribute_names(1) = 'area'
grid_attribute_names(2) = 'np'
grid_attribute_names(3) = 'ne'
end if
end subroutine physgrid_copy_attributes_d
!=========================================================================================
function get_dyn_grid_parm_real1d(name) result(rval)
! This routine is not used for SE, but still needed as a dummy interface to satisfy
! references from mo_synoz.F90 and phys_gmean.F90
character(len=*), intent(in) :: name
real(r8), pointer :: rval(:)
if(name.eq.'w') then
call endrun('get_dyn_grid_parm_real1d: w not defined')
else if(name.eq.'clat') then
call endrun('get_dyn_grid_parm_real1d: clat not supported, use get_horiz_grid_d')
else if(name.eq.'latdeg') then
call endrun('get_dyn_grid_parm_real1d: latdeg not defined')
else
nullify(rval)
end if
end function get_dyn_grid_parm_real1d
!=========================================================================================
integer function get_dyn_grid_parm(name) result(ival)
! This function is in the process of being deprecated, but is still needed
! as a dummy interface to satisfy external references from some chemistry routines.
use pmgrid, only: plat, plon, plev, plevp
character(len=*), intent(in) :: name
!----------------------------------------------------------------------------
if (name.eq.'plat') then
ival = plat
else if(name.eq.'plon') then
if (fv_nphys>0) then
ival = fv_nphys*fv_nphys*nelem_d
else
ival = ngcols_d
end if
else if(name.eq.'plev') then
ival = plev
else
ival = -1
end if
end function get_dyn_grid_parm
!=========================================================================================
subroutine dyn_grid_get_colndx(igcol, ncols, owners, col, lbk)
! For each global column index return the owning task. If the column is owned
! by this task, then also return the local block number and column index in that
! block.
!
! NOTE: this routine needs to be updated for the physgrid
integer, intent(in) :: ncols
integer, intent(in) :: igcol(ncols)
integer, intent(out) :: owners(ncols)
integer, intent(out) :: col(ncols)
integer, intent(out) :: lbk(ncols)
integer :: i, j, k, ii
integer :: blockid(1), bcid(1), lclblockid(1)
!----------------------------------------------------------------------------
if (fv_nphys > 0) then
call endrun('dyn_grid_get_colndx: not implemented for the FVM physics grid')
end if
do i = 1, ncols
call get_gcol_block_d(igcol(i), 1, blockid, bcid, lclblockid)
owners(i) = get_block_owner_d(blockid(1))
if (owners(i) == iam) then
lbk(i) = lclblockid(1)
ii = igcol(i) - elem(lbk(i))%idxp%UniquePtoffset + 1
k = elem(lbk(i))%idxp%ia(ii)
j = elem(lbk(i))%idxp%ja(ii)
col(i) = k + (j - 1)*np
else
lbk(i) = -1
col(i) = -1
end if
end do
end subroutine dyn_grid_get_colndx
!=========================================================================================
subroutine dyn_grid_get_elem_coords(ie, rlon, rlat, cdex)
! Returns coordinates of a specified block element of the dyn grid
!
! NB: This routine only uses the GLL points (i.e, it ignores the physics
! grid). This is probably OK as current use is only for dyn_decomp
! variables in history.
integer, intent(in) :: ie ! block element index
real(r8),optional, intent(out) :: rlon(:) ! longitudes of the columns in the element
real(r8),optional, intent(out) :: rlat(:) ! latitudes of the columns in the element
integer, optional, intent(out) :: cdex(:) ! global column index
integer :: sb,eb, ii, i,j, icol, igcol
real(r8), allocatable :: clat(:), clon(:)
!----------------------------------------------------------------------------
if (fv_nphys > 0) then
call endrun('dyn_grid_get_colndx: not implemented for the FVM physics grid')
end if
sb = elem(ie)%idxp%UniquePtOffset
eb = sb + elem(ie)%idxp%NumUniquePts-1
allocate( clat(sb:eb), clon(sb:eb) )
call UniqueCoords( elem(ie)%idxP, elem(ie)%spherep, clat(sb:eb), clon(sb:eb) )
if (present(cdex)) cdex(:) = -1
if (present(rlat)) rlat(:) = -999._r8
if (present(rlon)) rlon(:) = -999._r8
do ii=1,elem(ie)%idxp%NumUniquePts
i=elem(ie)%idxp%ia(ii)
j=elem(ie)%idxp%ja(ii)
icol = i+(j-1)*np
igcol = elem(ie)%idxp%UniquePtoffset+ii-1
if (present(cdex)) cdex(icol) = igcol
if (present(rlat)) rlat(icol) = clat( igcol )
if (present(rlon)) rlon(icol) = clon( igcol )
end do
deallocate( clat, clon )
end subroutine dyn_grid_get_elem_coords
!=========================================================================================
! Private routines.
!=========================================================================================
subroutine define_cam_grids()
! Create grid objects on the dynamics decomposition for grids used by
! the dycore. The decomposed grid object contains data for the elements
! in each task and information to map that data to the global grid.
!
! Notes on dynamic memory management:
!
! . Coordinate values and the map passed to the horiz_coord_create
! method are copied to the object. The memory may be deallocated
! after the object is created.
!
! . The area values passed to cam_grid_attribute_register are only pointed
! to by the attribute object, so that memory cannot be deallocated. But the
! map is copied.
!
! . The grid_map passed to cam_grid_register is just pointed to.
! Cannot be deallocated.
use cam_grid_support, only: horiz_coord_t, horiz_coord_create
use cam_grid_support, only: cam_grid_register, cam_grid_attribute_register
use spmd_utils, only: MPI_MAX, MPI_INTEGER, mpicom
! Local variables
integer :: i, ii, j, k, ie, mapind
character(len=8) :: latname, lonname, ncolname, areaname
type(horiz_coord_t), pointer :: lat_coord
type(horiz_coord_t), pointer :: lon_coord
integer(iMap), pointer :: grid_map(:,:)
real(r8), allocatable :: pelat_deg(:) ! pe-local latitudes (degrees)
real(r8), allocatable :: pelon_deg(:) ! pe-local longitudes (degrees)
real(r8), pointer :: pearea(:) => null() ! pe-local areas
real(r8) :: areaw(np,np)
integer(iMap) :: fdofP_local(npsq,nelemd)! pe-local map for dynamics decomp
integer(iMap), allocatable :: pemap(:) ! pe-local map for PIO decomp
integer :: ncols_fvm, ngcols_fvm
real(r8), allocatable :: fvm_coord(:)
real(r8), pointer :: fvm_area(:)
integer(iMap), pointer :: fvm_map(:)
integer :: ncols_physgrid, ngcols_physgrid
real(r8), allocatable :: physgrid_coord(:)
real(r8), pointer :: physgrid_area(:)
integer(iMap), pointer :: physgrid_map(:)
!----------------------------------------------------------------------------
!-----------------------
! Create GLL grid object
!-----------------------
! Calculate the mapping between element GLL points and file order
fdofp_local = 0
do ie = 1, nelemd
do ii = 1, elem(ie)%idxP%NumUniquePts
i = elem(ie)%idxP%ia(ii)
j = elem(ie)%idxP%ja(ii)
fdofp_local((np*(j-1))+i,ie) = elem(ie)%idxP%UniquePtoffset + ii - 1
end do
end do
allocate(pelat_deg(np*np*nelemd))
allocate(pelon_deg(np*np*nelemd))
allocate(pearea(np*np*nelemd))
allocate(pemap(np*np*nelemd))
pemap = 0
ii = 1
do ie = 1, nelemd
areaw = 1.0_r8 / elem(ie)%rspheremp(:,:)
pearea(ii:ii+npsq-1) = reshape(areaw, (/ np*np /))
pemap(ii:ii+npsq-1) = fdofp_local(:,ie)
do j = 1, np
do i = 1, np
pelat_deg(ii) = elem(ie)%spherep(i,j)%lat * rad2deg
pelon_deg(ii) = elem(ie)%spherep(i,j)%lon * rad2deg
ii = ii + 1
end do
end do
end do
! If using the physics grid then the GLL grid will use the names with
! '_d' suffixes and the physics grid will use the unadorned names.
! This allows fields on both the GLL and physics grids to be written to history
! output files.
if (fv_nphys > 0) then
latname = 'lat_d'
lonname = 'lon_d'
ncolname = 'ncol_d'
areaname = 'area_d'
else
latname = 'lat'
lonname = 'lon'
ncolname = 'ncol'
areaname = 'area'
end if
lat_coord => horiz_coord_create(trim(latname), trim(ncolname), ngcols_d, &
'latitude', 'degrees_north', 1, size(pelat_deg), pelat_deg, map=pemap)
lon_coord => horiz_coord_create(trim(lonname), trim(ncolname), ngcols_d, &
'longitude', 'degrees_east', 1, size(pelon_deg), pelon_deg, map=pemap)
! Map for GLL grid
allocate(grid_map(3,npsq*nelemd))
grid_map = 0
mapind = 1
do j = 1, nelemd
do i = 1, npsq
grid_map(1, mapind) = i
grid_map(2, mapind) = j
grid_map(3, mapind) = pemap(mapind)
mapind = mapind + 1
end do
end do
! The native SE GLL grid