-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathfv_surf_map.F90
1638 lines (1438 loc) · 54.4 KB
/
fv_surf_map.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
!***********************************************************************
!* GNU Lesser General Public License
!*
!* This file is part of the FV3 dynamical core.
!*
!* The FV3 dynamical core is free software: you can redistribute it
!* and/or modify it under the terms of the
!* GNU Lesser General Public License as published by the
!* Free Software Foundation, either version 3 of the License, or
!* (at your option) any later version.
!*
!* The FV3 dynamical core is distributed in the hope that it will be
!* useful, but WITHOUT ANYWARRANTY; 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 Lesser General Public
!* License along with the FV3 dynamical core.
!* If not, see <http://www.gnu.org/licenses/>.
!***********************************************************************
module fv_surf_map_mod
! <table>
! <tr>
! <th>Module Name</th>
! <th>Functions Included</th>
! </tr>
! <tr>
! <td>constants_mod</td>
! <td>grav, radius, pi=>pi_8</td>
! </tr>
! <tr>
! <td>fms_mod</td>
! <td>file_exist, check_nml_error,open_namelist_file, close_file,
! stdlog, mpp_pe, mpp_root_pe, FATAL, error_mesg</td>
! </tr>
! <tr>
! <td>fv_arrays_mod</td>
! <td>fv_grid_bounds_type, R_GRID</td>
! </tr>
! <tr>
! <td>fv_grid_utils_mod</td>
! <td>great_circle_dist, latlon2xyz, v_prod, normalize_vect,
! g_sum, global_mx, vect_cross</td>
! </tr>
! <tr>
! <td>fv_mp_mod</td>
! <td>ng,mp_stop, mp_reduce_min, mp_reduce_max, is_master</td>
! </tr>
! <tr>
! <td>fv_timing_mod</td>
! <td>timing_on, timing_off</td>
! </tr>
! <tr>
! <td>mpp_mod</td>
! <td>get_unit, input_nml_file, mpp_error,
! mpp_pe, mpp_chksum, stdout</td>
! </tr>
! <tr>
! <td>mpp_domains_mod</td>
! <td>mpp_update_domains, domain2d</td>
! </tr>
! </table>
use fms_mod, only: check_nml_error, stdlog, &
mpp_pe, mpp_root_pe, FATAL, error_mesg
use fms2_io_mod, only: file_exists
use mpp_mod, only: get_unit, input_nml_file, mpp_error
use mpp_domains_mod, only: mpp_update_domains, domain2d
use constants_mod, only: grav, radius, pi=>pi_8
use fv_grid_utils_mod, only: great_circle_dist, latlon2xyz, v_prod, normalize_vect
use fv_grid_utils_mod, only: g_sum, global_mx, vect_cross
use fv_mp_mod, only: mp_stop, mp_reduce_min, mp_reduce_max, is_master
use fv_timing_mod, only: timing_on, timing_off
use fv_arrays_mod, only: fv_grid_bounds_type, R_GRID
implicit none
private
!-----------------------------------------------------------------------
! NAMELIST
! Name, resolution, and format of XXmin USGS datafile
! 1min ---------> 1.85 km
! nlon = 10800 * 2
! nlat = 5400 * 2
! 2min ---------> 3.7 km
! nlon = 10800
! nlat = 5400
! 5min
! nlon = 4320
! nlat = 2160
! surf_format: netcdf (default)
! binary
! New NASA SRTM30 data: SRTM30.nc
! nlon = 43200
! nlat = 21600
logical:: zs_filter = .true.
logical:: zero_ocean = .true. ! if true, no diffusive flux into water/ocean area
integer :: nlon = 21600
integer :: nlat = 10800
real:: cd4 = 0.15 !< Dimensionless coeff for del-4 diffusion (with FCT)
real:: cd2 = -1. !< Dimensionless coeff for del-2 diffusion (-1 gives resolution-determined value)
real:: peak_fac = 1.05 !< overshoot factor for the mountain peak
real:: max_slope = 0.15 !< max allowable terrain slope: 1 --> 45 deg
!! 0.15 for C768 or lower; 0.25 C1536; 0.3 for C3072
integer:: n_del2_weak = 12
integer:: n_del2_strong = -1
integer:: n_del4 = -1
character(len=128):: surf_file = "INPUT/topo1min.nc"
character(len=6) :: surf_format = 'netcdf'
logical :: namelist_read = .false.
real(kind=R_GRID) da_min
real cos_grid
character(len=3) :: grid_string = ''
namelist /surf_map_nml/ surf_file,surf_format,nlon,nlat, zero_ocean, zs_filter, &
cd4, peak_fac, max_slope, n_del2_weak, n_del2_strong, cd2, n_del4
!
real, allocatable:: zs_g(:,:), sgh_g(:,:), oro_g(:,:)
public sgh_g, oro_g, zs_g
public surfdrv
public del2_cubed_sphere, del4_cubed_sphere, FV3_zs_filter
contains
subroutine surfdrv(npx, npy, grid, agrid, area, dx, dy, dxa, dya, dxc, dyc, sin_sg, phis, &
stretch_fac, nested, bounded_domain, npx_global, domain,grid_number, bd)
implicit none
#include <netcdf.inc>
integer, intent(in):: npx, npy
! INPUT arrays
type(fv_grid_bounds_type), intent(IN) :: bd
real(kind=R_GRID), intent(in)::area(bd%isd:bd%ied, bd%jsd:bd%jed)
real, intent(in):: dx(bd%isd:bd%ied, bd%jsd:bd%jed+1)
real, intent(in):: dy(bd%isd:bd%ied+1, bd%jsd:bd%jed)
real, intent(in), dimension(bd%isd:bd%ied, bd%jsd:bd%jed)::dxa, dya
real, intent(in)::dxc(bd%isd:bd%ied+1, bd%jsd:bd%jed)
real, intent(in)::dyc(bd%isd:bd%ied, bd%jsd:bd%jed+1)
real(kind=R_GRID), intent(in):: grid(bd%isd:bd%ied+1, bd%jsd:bd%jed+1,2)
real(kind=R_GRID), intent(in):: agrid(bd%isd:bd%ied, bd%jsd:bd%jed,2)
real, intent(IN):: sin_sg(bd%isd:bd%ied,bd%jsd:bd%jed,9)
real(kind=R_GRID), intent(IN):: stretch_fac
logical, intent(IN) :: nested, bounded_domain
integer, intent(IN) :: npx_global
type(domain2d), intent(INOUT) :: domain
integer, intent(IN) :: grid_number
! OUTPUT arrays
real, intent(out):: phis(bd%isd:bd%ied, bd%jsd:bd%jed)
! Local:
real, allocatable :: z2(:,:)
! Position of edges of the box containing the original data point:
integer londim
integer latdim
character(len=80) :: topoflnm
real(kind=4), allocatable :: ft(:,:), zs(:,:)
real, allocatable :: lon1(:), lat1(:)
real dx1, dx2, dy1, dy2, lats, latn, r2d
real(kind=R_GRID) da_max
real zmean, z2mean, delg, rgrav
! real z_sp, f_sp, z_np, f_np
integer i, j, n, mdim
integer igh, jt
integer ncid, lonid, latid, ftopoid, htopoid
integer jstart, jend, start(4), nread(4)
integer status
integer :: is, ie, js, je
integer :: isd, ied, jsd, jed, ng
real phis_coarse(bd%isd:bd%ied, bd%jsd:bd%jed)
real wt
is = bd%is
ie = bd%ie
js = bd%js
je = bd%je
isd = bd%isd
ied = bd%ied
jsd = bd%jsd
jed = bd%jed
ng = bd%ng
if (nested) then
!Divide all by grav
rgrav = 1./grav
do j=jsd,jed
do i=isd,ied
phis(i,j) = phis(i,j)*rgrav
enddo
enddo
!Save interpolated coarse-grid data for blending
do j=jsd,jed
do i=isd,ied
phis_coarse(i,j) = phis(i,j)
enddo
enddo
endif
do j=js,je
do i=is,ie
phis(i,j) = 0.0
enddo
enddo
call read_namelist
if (grid_number > 1) write(grid_string, '(A, I1)') ' g', grid_number
!
! surface file must be in NetCDF format
!
if ( file_exists(surf_file) ) then
if (surf_format == "netcdf") then
status = nf_open (surf_file, NF_NOWRITE, ncid)
if (status .ne. NF_NOERR) call handle_err(status)
status = nf_inq_dimid (ncid, 'lon', lonid)
if (status .ne. NF_NOERR) call handle_err(status)
status = nf_inq_dimlen (ncid, lonid, londim)
if (status .ne. NF_NOERR) call handle_err(status)
nlon = londim
status = nf_inq_dimid (ncid, 'lat', latid)
if (status .ne. NF_NOERR) call handle_err(status)
status = nf_inq_dimlen (ncid, latid, latdim)
if (status .ne. NF_NOERR) call handle_err(status)
nlat = latdim
if ( is_master() ) then
if ( nlon==43200 ) then
write(*,*) 'Opening NASA datset file:', surf_file, surf_format, nlon, nlat
else
write(*,*) 'Opening USGS datset file:', surf_file, surf_format, nlon, nlat
endif
endif
else
call error_mesg ( 'surfdrv','Raw IEEE data format no longer supported !!!', FATAL )
endif
else
call error_mesg ( 'surfdrv','surface file '//trim(surf_file)//' not found !', FATAL )
endif
allocate ( lat1(nlat+1) )
allocate ( lon1(nlon+1) )
r2d = 180./pi
cos_grid = cos( 2.*pi/real(nlat) ) ! two-data_grid distance
dx1 = 2.*pi/real(nlon)
dy1 = pi/real(nlat)
do i=1,nlon+1
lon1(i) = dx1 * real(i-1) ! between 0 2pi
enddo
lat1(1) = - 0.5*pi
lat1(nlat+1) = 0.5*pi
do j=2,nlat
lat1(j) = -0.5*pi + dy1*(j-1)
enddo
!-------------------------------------
! Compute raw phis and oro
!-------------------------------------
call timing_on('map_to_cubed')
if (surf_format == "netcdf") then
! Find latitude strips reading data
lats = pi/2.
latn = -pi/2.
do j=js,je
do i=is,ie
lats = min( lats, grid(i,j,2), grid(i+1,j,2), grid(i,j+1,2), grid(i+1,j+1,2), agrid(i,j,2) )
latn = max( latn, grid(i,j,2), grid(i+1,j,2), grid(i,j+1,2), grid(i+1,j+1,2), agrid(i,j,2) )
enddo
enddo
! Enlarge the search zone:
! To account for the curvature of the coordinates:
!I have had trouble running c90 with 600 pes unless the search region is expanded
! due to failures in finding latlon points in the source data.
!This sets a larger search region if the number of cells on a PE is too small.
!(Alternately you can just cold start the topography using a smaller number of PEs)
if (min(je-js+1,ie-is+1) < 15) then
delg = max( 0.4*(latn-lats), pi/real(npx_global-1), 2.*pi/real(nlat) )
else
delg = max( 0.2*(latn-lats), pi/real(npx_global-1), 2.*pi/real(nlat) )
endif
lats = max( -0.5*pi, lats - delg )
latn = min( 0.5*pi, latn + delg )
jstart = 1
do j=2,nlat
if ( lats < lat1(j) ) then
jstart = j-1
exit
endif
enddo
jstart = max(jstart-1, 1)
jend = nlat
do j=2,nlat
if ( latn < lat1(j+1) ) then
jend = j+1
exit
endif
enddo
jend = min(jend+1, nlat)
jt = jend - jstart + 1
igh = nlon/8 + nlon/(2*(npx_global-1))
if (is_master()) write(*,*) 'Terrain dataset =', nlon, 'jt=', jt
if (is_master()) write(*,*) 'igh (terrain ghosting)=', igh
status = nf_inq_varid (ncid, 'ftopo', ftopoid)
if (status .ne. NF_NOERR) call handle_err(status)
nread = 1; start = 1
nread(1) = nlon
start(2) = jstart; nread(2) = jend - jstart + 1
allocate ( ft(-igh:nlon+igh,jt) )
status = nf_get_vara_real (ncid, ftopoid, start, nread, ft(1:nlon,1:jt))
if (status .ne. NF_NOERR) call handle_err(status)
do j=1,jt
do i=-igh,0
ft(i,j) = ft(i+nlon,j)
enddo
do i=nlon+1,nlon+igh
ft(i,j) = ft(i-nlon,j)
enddo
enddo
status = nf_inq_varid (ncid, 'htopo', htopoid)
if (status .ne. NF_NOERR) call handle_err(status)
allocate ( zs(-igh:nlon+igh,jt) )
status = nf_get_vara_real (ncid, htopoid, start, nread, zs(1:nlon,1:jt))
if (status .ne. NF_NOERR) call handle_err(status)
status = nf_close (ncid)
if (status .ne. NF_NOERR) call handle_err(status)
! Ghost Data
do j=1,jt
do i=-igh,0
zs(i,j) = zs(i+nlon,j)
enddo
do i=nlon+1,nlon+igh
zs(i,j) = zs(i-nlon,j)
enddo
enddo
endif
! special SP treatment:
! if ( jstart == 1 ) then
! call zonal_mean(nlon, zs(1,1), z_sp)
! call zonal_mean(nlon, ft(1,1), f_sp)
! endif
allocate ( oro_g(isd:ied, jsd:jed) )
allocate ( sgh_g(isd:ied, jsd:jed) )
call timing_on('map_to_cubed')
call map_to_cubed_raw(igh, nlon, jt, lat1(jstart:jend+1), lon1, zs, ft, grid, agrid, &
phis, oro_g, sgh_g, npx, npy, jstart, jend, stretch_fac, bounded_domain, npx_global, bd)
if (is_master()) write(*,*) 'map_to_cubed_raw: master PE done'
call timing_off('map_to_cubed')
deallocate ( zs )
deallocate ( ft )
deallocate ( lon1 )
deallocate ( lat1 )
allocate ( zs_g(is:ie, js:je) )
allocate ( z2(is:ie,js:je) )
do j=js,je
do i=is,ie
zs_g(i,j) = phis(i,j)
z2(i,j) = phis(i,j)**2
enddo
enddo
!--------
! Filter:
!--------
call global_mx(real(phis,kind=R_GRID), ng, da_min, da_max, bd)
zmean = g_sum(domain, zs_g(is:ie,js:je), is, ie, js, je, ng, area, 1)
z2mean = g_sum(domain, z2(is:ie,js:je) , is, ie, js, je, ng, area, 1)
if ( is_master() ) then
write(*,*) 'Before filter ZS', trim(grid_string), ' min=', da_min, ' Max=', da_max,' Mean=',zmean
write(*,*) '*** Mean variance', trim(grid_string), ' *** =', z2mean
endif
!On a nested grid blend coarse-grid and nested-grid
! orography near boundary.
! This works only on the height of topography; assume
! land fraction and sub-grid variance unchanged
! Here, we blend in the four cells nearest to the boundary.
! In the halo we set the value to that interpolated from the coarse
! grid. (Previously this was erroneously not being done, which was causing
! the halo to be filled with unfiltered nested-grid terrain, creating
! ugly edge artifacts.)
if (nested) then
if (is_master()) write(*,*) 'Blending nested and coarse grid topography'
do j=jsd,jed
do i=isd,ied
wt = max(0.,min(1.,real(5 - min(i,j,npx-i,npy-j,5))/5. ))
phis(i,j) = (1.-wt)*phis(i,j) + wt*phis_coarse(i,j)
enddo
enddo
endif
call global_mx(real(oro_g,kind=R_GRID), ng, da_min, da_max, bd)
if ( is_master() ) write(*,*) 'ORO', trim(grid_string), ' min=', da_min, ' Max=', da_max
call global_mx(area, ng, da_min, da_max, bd)
call timing_on('Terrain_filter')
! Del-2: high resolution only
if ( zs_filter ) then
if(is_master()) then
write(*,*) 'Applying terrain filters. zero_ocean is', zero_ocean
endif
call FV3_zs_filter (bd, isd, ied, jsd, jed, npx, npy, npx_global, &
stretch_fac, bounded_domain, domain, area, dxa, dya, dx, dy, dxc, dyc, grid, &
agrid, sin_sg, phis, oro_g)
call mpp_update_domains(phis, domain)
endif ! end terrain filter
call timing_off('Terrain_filter')
do j=js,je
do i=is,ie
z2(i,j) = phis(i,j)**2
end do
end do
call global_mx(real(phis,kind=R_GRID), ng, da_min, da_max, bd)
zmean = g_sum(domain, phis(is:ie,js:je), is, ie, js, je, ng, area, 1)
z2mean = g_sum(domain, z2, is, ie, js, je, ng, area, 1)
deallocate ( z2 )
if ( is_master() ) then
write(*,*) 'After filter Phis', trim(grid_string), ' min=', da_min, ' Max=', da_max, 'Mean=', zmean
write(*,*) '*** Mean variance', trim(grid_string), ' *** =', z2mean
endif
!FOR NESTING: Unless we fill the outermost halo with topography
! interpolated from the coarse grid, the values of phi there
! will be wrong because they are just z instead of g*z; they
! have not had gravity properly multiplied in so we can get phi
! For now we compute phis and sgh_g on the full data domain; for
! nested grids this allows us to do the smoothing near the boundary
! without having to fill the boundary halo from the coarse grid
!ALSO for nesting: note that we are smoothing the terrain using
! the nested-grid's outer halo filled with the terrain computed
! directly from the input file computed here, and then
! replacing it with interpolated topography in fv_restart, so
! as to be consistent when doing the boundary condition
! interpolation. We would ideally replace the nested-grid
! halo topography BEFORE smoothing, which could be more
! consistent, but this would require moving calls to
! nested_grid_BC in this routine.
do j=jsd,jed
do i=isd,ied
phis(i,j) = grav * phis(i,j)
if ( sgh_g(i,j) <= 0. ) then
sgh_g(i,j) = 0.
else
sgh_g(i,j) = sqrt(sgh_g(i,j))
endif
end do
end do
call global_mx(real(sgh_g,kind=R_GRID), ng, da_min, da_max, bd)
if ( is_master() ) write(*,*) 'Before filter SGH', trim(grid_string), ' min=', da_min, ' Max=', da_max
!-----------------------------------------------
! Filter the standard deviation of mean terrain:
!-----------------------------------------------
call global_mx(area, ng, da_min, da_max, bd)
if(zs_filter) call del4_cubed_sphere(npx, npy, sgh_g, area, dx, dy, dxc, dyc, sin_sg, 1, zero_ocean, oro_g, bounded_domain, domain, bd)
call global_mx(real(sgh_g,kind=R_GRID), ng, da_min, da_max, bd)
if ( is_master() ) write(*,*) 'After filter SGH', trim(grid_string), ' min=', da_min, ' Max=', da_max
do j=js,je
do i=is,ie
sgh_g(i,j) = max(0., sgh_g(i,j))
enddo
enddo
end subroutine surfdrv
subroutine FV3_zs_filter (bd, isd, ied, jsd, jed, npx, npy, npx_global, &
stretch_fac, bounded_domain, domain, area, dxa, dya, dx, dy, dxc, dyc, grid, &
agrid, sin_sg, phis, oro )
integer, intent(in):: isd, ied, jsd, jed, npx, npy, npx_global
type(fv_grid_bounds_type), intent(IN) :: bd
real(kind=R_GRID), intent(in), dimension(isd:ied,jsd:jed)::area
real, intent(in), dimension(isd:ied,jsd:jed)::dxa, dya
real, intent(in), dimension(isd:ied, jsd:jed+1):: dx, dyc
real, intent(in), dimension(isd:ied+1,jsd:jed):: dy, dxc
real(kind=R_GRID), intent(in):: grid(isd:ied+1, jsd:jed+1,2)
real(kind=R_GRID), intent(in):: agrid(isd:ied, jsd:jed, 2)
real, intent(IN):: sin_sg(isd:ied,jsd:jed,9)
real(kind=R_GRID), intent(IN):: stretch_fac
logical, intent(IN) :: bounded_domain
real, intent(inout):: phis(isd:ied,jsd,jed)
real, intent(in):: oro(isd:ied,jsd,jed)
type(domain2d), intent(INOUT) :: domain
integer mdim
real(kind=R_GRID) da_max
if (is_master()) print*, ' Calling FV3_zs_filter...'
if (.not. namelist_read) call read_namelist !when calling from external_ic
call global_mx(area, bd%ng, da_min, da_max, bd)
mdim = nint( real(npx_global) * min(10., stretch_fac) )
! Del-2: high resolution only
! call del2_cubed_sphere(npx, npy, phis, area, dx, dy, dxc, dyc, sin_sg, n_del2, cd2, zero_ocean, oro, bounded_domain, domain, bd)
if (n_del2_strong < 0) then
if ( npx_global<=97) then
n_del2_strong = 0
elseif ( npx_global<=193 ) then
n_del2_strong = 1
else
n_del2_strong = 2
endif
endif
if (cd2 < 0.) cd2 = 0.16*da_min
! Applying strong 2-delta-filter:
if ( n_del2_strong > 0 ) &
call two_delta_filter(npx, npy, phis, area, dx, dy, dxa, dya, dxc, dyc, sin_sg, cd2, zero_ocean, &
.true., 0, oro, bounded_domain, domain, bd, n_del2_strong)
! MFCT Del-4:
if (n_del4 < 0) then
if ( mdim<=193 ) then
n_del4 = 1
elseif ( mdim<=1537 ) then
n_del4 = 2
else
n_del4 = 3
endif
endif
call del4_cubed_sphere(npx, npy, phis, area, dx, dy, dxc, dyc, sin_sg, n_del4, zero_ocean, oro, bounded_domain, domain, bd)
! Applying weak 2-delta-filter:
cd2 = 0.12*da_min
call two_delta_filter(npx, npy, phis, area, dx, dy, dxa, dya, dxc, dyc, sin_sg, cd2, zero_ocean, &
.true., 1, oro, bounded_domain, domain, bd, n_del2_weak)
end subroutine FV3_zs_filter
subroutine two_delta_filter(npx, npy, q, area, dx, dy, dxa, dya, dxc, dyc, sin_sg, cd, zero_ocean, &
check_slope, filter_type, oro, bounded_domain, domain, bd, ntmax)
type(fv_grid_bounds_type), intent(IN) :: bd
integer, intent(in):: npx, npy
integer, intent(in):: ntmax
integer, intent(in):: filter_type !< 0: strong, 1: weak
real, intent(in):: cd
! INPUT arrays
real(kind=R_GRID), intent(in)::area(bd%isd:bd%ied, bd%jsd:bd%jed)
real, intent(in):: dx(bd%isd:bd%ied, bd%jsd:bd%jed+1)
real, intent(in):: dy(bd%isd:bd%ied+1,bd%jsd:bd%jed)
real, intent(in):: dxa(bd%isd:bd%ied, bd%jsd:bd%jed)
real, intent(in):: dya(bd%isd:bd%ied, bd%jsd:bd%jed)
real, intent(in):: dxc(bd%isd:bd%ied+1,bd%jsd:bd%jed)
real, intent(in):: dyc(bd%isd:bd%ied, bd%jsd:bd%jed+1)
real, intent(in):: sin_sg(bd%isd:bd%ied,bd%jsd:bd%jed,9)
real, intent(in):: oro(bd%isd:bd%ied, bd%jsd:bd%jed) !< 0==water, 1==land
logical, intent(in):: zero_ocean, check_slope
logical, intent(in):: bounded_domain
type(domain2d), intent(inout) :: domain
! OUTPUT arrays
real, intent(inout):: q(bd%isd:bd%ied, bd%jsd:bd%jed)
! Local:
real, parameter:: p1 = 7./12.
real, parameter:: p2 = -1./12.
real, parameter:: c1 = -2./14.
real, parameter:: c2 = 11./14.
real, parameter:: c3 = 5./14.
real:: ddx(bd%is:bd%ie+1,bd%js:bd%je), ddy(bd%is:bd%ie,bd%js:bd%je+1)
logical:: extm(bd%is-1:bd%ie+1)
logical:: ext2(bd%is:bd%ie,bd%js-1:bd%je+1)
real:: a1(bd%is-1:bd%ie+2)
real:: a2(bd%is:bd%ie,bd%js-1:bd%je+2)
real(kind=R_GRID):: a3(bd%is:bd%ie,bd%js:bd%je)
real(kind=R_GRID):: smax, smin
real:: m_slope, fac
integer:: i,j, nt
integer:: is, ie, js, je
integer:: isd, ied, jsd, jed
integer:: is1, ie2, js1, je2
is = bd%is
ie = bd%ie
js = bd%js
je = bd%je
isd = bd%isd
ied = bd%ied
jsd = bd%jsd
jed = bd%jed
if ( bounded_domain ) then
is1 = is-1; ie2 = ie+2
js1 = js-1; je2 = je+2
else
is1 = max(3,is-1); ie2 = min(npx-2,ie+2)
js1 = max(3,js-1); je2 = min(npy-2,je+2)
end if
if ( check_slope ) then
m_slope = max_slope
else
m_slope = 10.
endif
do 777 nt=1, ntmax
call mpp_update_domains(q, domain)
! Check slope
if ( nt==1 .and. check_slope ) then
do j=js,je
do i=is,ie+1
ddx(i,j) = (q(i,j) - q(i-1,j))/dxc(i,j)
ddx(i,j) = abs(ddx(i,j))
enddo
enddo
do j=js,je+1
do i=is,ie
ddy(i,j) = (q(i,j) - q(i,j-1))/dyc(i,j)
ddy(i,j) = abs(ddy(i,j))
enddo
enddo
do j=js,je
do i=is,ie
a3(i,j) = max( ddx(i,j), ddx(i+1,j), ddy(i,j), ddy(i,j+1) )
enddo
enddo
call global_mx(a3, 0, smin, smax, bd)
if ( is_master() ) write(*,*) 'Before filter: Max_slope=', smax
endif
! First step: average the corners:
if ( .not. bounded_domain .and. nt==1 ) then
if ( is==1 .and. js==1 ) then
q(1,1) = (q(1,1)*area(1,1)+q(0,1)*area(0,1)+q(1,0)*area(1,0)) &
/ ( area(1,1)+ area(0,1)+ area(1,0) )
q(0,1) = q(1,1)
q(1,0) = q(1,1)
endif
if ( (ie+1)==npx .and. js==1 ) then
q(ie, 1) = (q(ie,1)*area(ie,1)+q(npx,1)*area(npx,1)+q(ie,0)*area(ie,0)) &
/ ( area(ie,1)+ area(npx,1)+ area(ie,0))
q(npx,1) = q(ie,1)
q(ie, 0) = q(ie,1)
endif
if ( is==1 .and. (je+1)==npy ) then
q(1, je) = (q(1,je)*area(1,je)+q(0,je)*area(0,je)+q(1,npy)*area(1,npy)) &
/ ( area(1,je)+ area(0,je)+ area(1,npy))
q(0, je) = q(1,je)
q(1,npy) = q(1,je)
endif
if ( (ie+1)==npx .and. (je+1)==npy ) then
q(ie, je) = (q(ie,je)*area(ie,je)+q(npx,je)*area(npx,je)+q(ie,npy)*area(ie,npy)) &
/ ( area(ie,je)+ area(npx,je)+ area(ie,npy))
q(npx,je) = q(ie,je)
q(ie,npy) = q(ie,je)
endif
call mpp_update_domains(q, domain)
endif
! x-diffusive flux:
do 333 j=js,je
do i=is1, ie2
a1(i) = p1*(q(i-1,j)+q(i,j)) + p2*(q(i-2,j)+q(i+1,j))
enddo
if ( .not. bounded_domain ) then
if ( is==1 ) then
a1(0) = c1*q(-2,j) + c2*q(-1,j) + c3*q(0,j)
a1(1) = 0.5*(((2.*dxa(0,j)+dxa(-1,j))*q(0,j)-dxa(0,j)*q(-1,j))/(dxa(-1,j)+dxa(0,j)) &
+ ((2.*dxa(1,j)+dxa( 2,j))*q(1,j)-dxa(1,j)*q( 2,j))/(dxa(1, j)+dxa(2,j)))
a1(2) = c3*q(1,j) + c2*q(2,j) +c1*q(3,j)
endif
if ( (ie+1)==npx ) then
a1(npx-1) = c1*q(npx-3,j) + c2*q(npx-2,j) + c3*q(npx-1,j)
a1(npx) = 0.5*(((2.*dxa(npx-1,j)+dxa(npx-2,j))*q(npx-1,j)-dxa(npx-1,j)*q(npx-2,j))/(dxa(npx-2,j)+dxa(npx-1,j)) &
+ ((2.*dxa(npx, j)+dxa(npx+1,j))*q(npx, j)-dxa(npx, j)*q(npx+1,j))/(dxa(npx, j)+dxa(npx+1,j)))
a1(npx+1) = c3*q(npx,j) + c2*q(npx+1,j) + c1*q(npx+2,j)
endif
endif
if ( filter_type == 0 ) then
do i=is-1, ie+1
if( abs(3.*(a1(i)+a1(i+1)-2.*q(i,j))) > abs(a1(i)-a1(i+1)) ) then
extm(i) = .true.
else
extm(i) = .false.
endif
enddo
else
do i=is-1, ie+1
if ( (a1(i)-q(i,j))*(a1(i+1)-q(i,j)) > 0. ) then
extm(i) = .true.
else
extm(i) = .false.
endif
enddo
endif
do i=is,ie+1
ddx(i,j) = (q(i-1,j)-q(i,j))/dxc(i,j)
if ( extm(i-1).and.extm(i) ) then
ddx(i,j) = 0.5*(sin_sg(i-1,j,3)+sin_sg(i,j,1))*dy(i,j)*ddx(i,j)
elseif ( abs(ddx(i,j)) > m_slope ) then
fac = min(1., max(0.1,(abs(ddx(i,j))-m_slope)/m_slope ) )
ddx(i,j) = fac*0.5*(sin_sg(i-1,j,3)+sin_sg(i,j,1))*dy(i,j)*ddx(i,j)
else
ddx(i,j) = 0.
endif
enddo
333 continue
! y-diffusive flux:
do j=js1,je2
do i=is,ie
a2(i,j) = p1*(q(i,j-1)+q(i,j)) + p2*(q(i,j-2)+q(i,j+1))
enddo
enddo
if ( .not. bounded_domain ) then
if( js==1 ) then
do i=is,ie
a2(i,0) = c1*q(i,-2) + c2*q(i,-1) + c3*q(i,0)
a2(i,1) = 0.5*(((2.*dya(i,0)+dya(i,-1))*q(i,0)-dya(i,0)*q(i,-1))/(dya(i,-1)+dya(i,0)) &
+ ((2.*dya(i,1)+dya(i, 2))*q(i,1)-dya(i,1)*q(i, 2))/(dya(i, 1)+dya(i,2)))
a2(i,2) = c3*q(i,1) + c2*q(i,2) + c1*q(i,3)
enddo
endif
if( (je+1)==npy ) then
do i=is,ie
a2(i,npy-1) = c1*q(i,npy-3) + c2*q(i,npy-2) + c3*q(i,npy-1)
a2(i,npy) = 0.5*(((2.*dya(i,npy-1)+dya(i,npy-2))*q(i,npy-1)-dya(i,npy-1)*q(i,npy-2))/(dya(i,npy-2)+dya(i,npy-1)) &
+ ((2.*dya(i,npy)+dya(i,npy+1))*q(i,npy)-dya(i,npy)*q(i,npy+1))/(dya(i,npy)+dya(i,npy+1)))
a2(i,npy+1) = c3*q(i,npy) + c2*q(i,npy+1) + c1*q(i,npy+2)
enddo
endif
endif
if ( filter_type == 0 ) then
do j=js-1,je+1
do i=is,ie
if( abs(3.*(a2(i,j)+a2(i,j+1)-2.*q(i,j))) > abs(a2(i,j)-a2(i,j+1)) ) then
ext2(i,j) = .true.
else
ext2(i,j) = .false.
endif
enddo
enddo
else
do j=js-1,je+1
do i=is,ie
if ( (a2(i,j)-q(i,j))*(a2(i,j+1)-q(i,j)) > 0. ) then
ext2(i,j) = .true.
else
ext2(i,j) = .false.
endif
enddo
enddo
endif
do j=js,je+1
do i=is,ie
ddy(i,j) = (q(i,j-1)-q(i,j))/dyc(i,j)
if ( ext2(i,j-1) .and. ext2(i,j) ) then
ddy(i,j) = 0.5*(sin_sg(i,j-1,4)+sin_sg(i,j,2))*dx(i,j)*ddy(i,j)
elseif ( abs(ddy(i,j))>m_slope ) then
fac = min(1., max(0.1,(abs(ddy(i,j))-m_slope)/m_slope))
ddy(i,j) = fac*0.5*(sin_sg(i,j-1,4)+sin_sg(i,j,2))*dx(i,j)*ddy(i,j)
else
ddy(i,j) = 0.
endif
enddo
enddo
if ( zero_ocean ) then
! Limit diffusive flux over water cells:
do j=js,je
do i=is,ie+1
ddx(i,j) = max(0., min(oro(i-1,j), oro(i,j))) * ddx(i,j)
enddo
enddo
do j=js,je+1
do i=is,ie
ddy(i,j) = max(0., min(oro(i,j-1), oro(i,j))) * ddy(i,j)
enddo
enddo
endif
do j=js,je
do i=is,ie
q(i,j) = q(i,j) + cd/area(i,j)*(ddx(i,j)-ddx(i+1,j)+ddy(i,j)-ddy(i,j+1))
enddo
enddo
777 continue
! Check slope
if ( check_slope ) then
call mpp_update_domains(q, domain)
do j=js,je
do i=is,ie+1
ddx(i,j) = (q(i,j) - q(i-1,j))/dxc(i,j)
ddx(i,j) = abs(ddx(i,j))
enddo
enddo
do j=js,je+1
do i=is,ie
ddy(i,j) = (q(i,j) - q(i,j-1))/dyc(i,j)
ddy(i,j) = abs(ddy(i,j))
enddo
enddo
do j=js,je
do i=is,ie
a3(i,j) = max( ddx(i,j), ddx(i+1,j), ddy(i,j), ddy(i,j+1) )
enddo
enddo
call global_mx(a3, 0, smin, smax, bd)
if ( is_master() ) write(*,*) 'After filter: Max_slope=', smax
endif
end subroutine two_delta_filter
subroutine del2_cubed_sphere(npx, npy, q, area, dx, dy, dxc, dyc, sin_sg, nmax, cd, zero_ocean, oro, bounded_domain, domain, bd)
type(fv_grid_bounds_type), intent(IN) :: bd
integer, intent(in):: npx, npy
integer, intent(in):: nmax
real(kind=R_GRID), intent(in):: cd
logical, intent(in):: zero_ocean
! INPUT arrays
real(kind=R_GRID), intent(in)::area(bd%isd:bd%ied, bd%jsd:bd%jed)
real, intent(in):: dx(bd%isd:bd%ied, bd%jsd:bd%jed+1)
real, intent(in):: dy(bd%isd:bd%ied+1,bd%jsd:bd%jed)
real, intent(in):: dxc(bd%isd:bd%ied+1,bd%jsd:bd%jed)
real, intent(in):: dyc(bd%isd:bd%ied, bd%jsd:bd%jed+1)
real, intent(IN):: sin_sg(bd%isd:bd%ied,bd%jsd:bd%jed,9)
real, intent(in):: oro(bd%isd:bd%ied, bd%jsd:bd%jed) !< 0==water, 1==land
logical, intent(IN) :: bounded_domain
type(domain2d), intent(INOUT) :: domain
! OUTPUT arrays
real, intent(inout):: q(bd%isd:bd%ied, bd%jsd:bd%jed)
! Local:
real ddx(bd%is:bd%ie+1,bd%js:bd%je), ddy(bd%is:bd%ie,bd%js:bd%je+1)
integer i,j,n
integer :: is, ie, js, je
integer :: isd, ied, jsd, jed
integer :: ng
is = bd%is
ie = bd%ie
js = bd%js
je = bd%je
isd = bd%isd
ied = bd%ied
jsd = bd%jsd
jed = bd%jed
ng = bd%ng
call mpp_update_domains(q,domain,whalo=ng,ehalo=ng,shalo=ng,nhalo=ng)
! First step: average the corners:
if ( is==1 .and. js==1 .and. .not. bounded_domain) then
q(1,1) = (q(1,1)*area(1,1)+q(0,1)*area(0,1)+q(1,0)*area(1,0)) &
/ ( area(1,1)+ area(0,1)+ area(1,0) )
q(0,1) = q(1,1)
q(1,0) = q(1,1)
endif
if ( (ie+1)==npx .and. js==1 .and. .not. bounded_domain) then
q(ie, 1) = (q(ie,1)*area(ie,1)+q(npx,1)*area(npx,1)+q(ie,0)*area(ie,0)) &
/ ( area(ie,1)+ area(npx,1)+ area(ie,0))
q(npx,1) = q(ie,1)
q(ie, 0) = q(ie,1)
endif
if ( (ie+1)==npx .and. (je+1)==npy .and. .not. bounded_domain ) then
q(ie, je) = (q(ie,je)*area(ie,je)+q(npx,je)*area(npx,je)+q(ie,npy)*area(ie,npy)) &
/ ( area(ie,je)+ area(npx,je)+ area(ie,npy))
q(npx,je) = q(ie,je)
q(ie,npy) = q(ie,je)
endif
if ( is==1 .and. (je+1)==npy .and. .not. bounded_domain) then
q(1, je) = (q(1,je)*area(1,je)+q(0,je)*area(0,je)+q(1,npy)*area(1,npy)) &
/ ( area(1,je)+ area(0,je)+ area(1,npy))
q(0, je) = q(1,je)
q(1,npy) = q(1,je)
endif
do n=1,nmax
if( n>1 ) call mpp_update_domains(q,domain,whalo=ng,ehalo=ng,shalo=ng,nhalo=ng)
do j=js,je
do i=is,ie+1
ddx(i,j) = 0.5*(sin_sg(i-1,j,3)+sin_sg(i,j,1))*dy(i,j)*(q(i-1,j)-q(i,j))/dxc(i,j)
enddo
enddo
do j=js,je+1
do i=is,ie
ddy(i,j) = dx(i,j)*(q(i,j-1)-q(i,j))/dyc(i,j) &
*0.5*(sin_sg(i,j-1,4)+sin_sg(i,j,2))
enddo
enddo
if ( zero_ocean ) then
! Limit diffusive flux over ater cells:
do j=js,je
do i=is,ie+1
ddx(i,j) = max(0., min(oro(i-1,j), oro(i,j))) * ddx(i,j)
enddo
enddo
do j=js,je+1
do i=is,ie
ddy(i,j) = max(0., min(oro(i,j-1), oro(i,j))) * ddy(i,j)
enddo
enddo
endif
do j=js,je
do i=is,ie
q(i,j) = q(i,j) + cd/area(i,j)*(ddx(i,j)-ddx(i+1,j)+ddy(i,j)-ddy(i,j+1))
enddo
enddo
enddo
end subroutine del2_cubed_sphere
subroutine del4_cubed_sphere(npx, npy, q, area, dx, dy, dxc, dyc, sin_sg, nmax, zero_ocean, oro, bounded_domain, domain, bd)
type(fv_grid_bounds_type), intent(IN) :: bd
integer, intent(in):: npx, npy, nmax
logical, intent(in):: zero_ocean
real, intent(in):: oro(bd%isd:bd%ied, bd%jsd:bd%jed) !< 0==water, 1==land
real(kind=R_GRID), intent(in)::area(bd%isd:bd%ied, bd%jsd:bd%jed)
real, intent(in):: dx(bd%isd:bd%ied, bd%jsd:bd%jed+1)
real, intent(in):: dy(bd%isd:bd%ied+1,bd%jsd:bd%jed)
real, intent(in):: dxc(bd%isd:bd%ied+1,bd%jsd:bd%jed)
real, intent(in):: dyc(bd%isd:bd%ied, bd%jsd:bd%jed+1)
real, intent(IN):: sin_sg(bd%isd:bd%ied,bd%jsd:bd%jed,9)
real, intent(inout):: q(bd%isd:bd%ied, bd%jsd:bd%jed)
logical, intent(IN) :: bounded_domain
type(domain2d), intent(INOUT) :: domain
! diffusivity
real :: diff(bd%is-3:bd%ie+2,bd%js-3:bd%je+2)
! diffusive fluxes:
real :: fx1(bd%is:bd%ie+1,bd%js:bd%je), fy1(bd%is:bd%ie,bd%js:bd%je+1)
real :: fx2(bd%is:bd%ie+1,bd%js:bd%je), fy2(bd%is:bd%ie,bd%js:bd%je+1)
real :: fx4(bd%is:bd%ie+1,bd%js:bd%je), fy4(bd%is:bd%ie,bd%js:bd%je+1)
real, dimension(bd%isd:bd%ied,bd%jsd:bd%jed):: d2, win, wou
real, dimension(bd%is:bd%ie,bd%js:bd%je):: qlow, qmin, qmax, q0
real, parameter:: esl = 1.E-20
integer i,j, n
integer :: is, ie, js, je
integer :: isd, ied, jsd, jed
is = bd%is
ie = bd%ie
js = bd%js
je = bd%je
isd = bd%isd
ied = bd%ied
jsd = bd%jsd
jed = bd%jed
!On a bounded_domain grid the haloes are not filled. Set to zero.
d2 = 0.
win = 0.