-
Notifications
You must be signed in to change notification settings - Fork 0
/
reconstruct.F90
2883 lines (2337 loc) · 105 KB
/
reconstruct.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 GECORE_Interp_ABP
!
! Purpose:
! Provides functions for performing conservative interpolation between
! cubed sphere and lat lon grids.
!
! Date Programmer Affiliation Description of change
! ==== ========== =========== =====================
! 06/25/08 P.A.Ullrich CGD,NCAR Original Code
! 07/11/08 P.A.Ullrich CGD,NCAR First order interp.
! 07/18/08 P.A.Ullrich CGD,NCAR Renamed to GECORE
!
!-----------------------------------------------------------------------------
MODULE reconstruct
USE remap
! INTEGER, PARAMETER :: &
! int_kind = KIND(1), &
! real_kind = SELECTED_REAL_KIND(p=14,r=100),&
! dbl_kind = selected_real_kind(13)
! LOGICAL, PARAMETER:: ldbg_r = .FALSE.
INTEGER, PRIVATE :: ncube_reconstruct
REAL(kind=real_kind), PARAMETER, PRIVATE :: &
! one = 1.0 ,&
! aa = 1.0 ,&
! tiny= 1.0E-10 ,&
pi = 3.14159265358979323846264338327 ,&
piq = 0.25*pi ,&
pih = 0.50*pi ,&
pi2 = 2.0*pi
REAL (KIND=dbl_kind), DIMENSION(:,:,:), ALLOCATABLE, PRIVATE :: abp_centroid
REAL (KIND=dbl_kind), DIMENSION(:), ALLOCATABLE, PRIVATE :: gp
REAL (KIND=dbl_kind), PARAMETER, PRIVATE :: lammax = 60_dbl_kind !for selective limiting
! REAL (KIND=dbl_kind),PARAMETER,PRIVATE :: bignum = 1.e+20_dbl_kind
CONTAINS
!
! wrapper subroutine to use the CSLAM code
!
! note that ncube_reconstruct is ncube_reconstruct+1 in topo software
!
! SUBROUTINE get_halo(fcube_in,fcube_out,order, kpd,DAcube,ncube)
! IMPLICIT NONE
!
! REAL (KIND=dbl_kind), &
! DIMENSION(6*(ncube-1)*(ncube-1)), INTENT(IN) :: fcube_in
! REAL (KIND=dbl_kind), &
! DIMENSION(1-xxxxncube-1,ncube-1,6), INTENT(OUT) :: fcube_out
!
! INTEGER (KIND=int_kind), INTENT(IN) :: order, ncube
!
! REAL (KIND=dbl_kind), DIMENSION(ncube-1,ncube-1), INTENT(IN) :: dAcube
! REAL (KIND=dbl_kind), DIMENSION(nreconstruction,6*(ncube-1)*(ncube-1)) :: centroids
! ! Local variables
! REAL (KIND=dbl_kind), &
! DIMENSION(1:ncube-1, 1:ncube-1, 6) :: fcube
! INTEGER (KIND=int_kind) :: status,k,ip,ii,ix,iy,nreconstruction,kpd,kmono
!
! REAL (KIND=dbl_kind), DIMENSION(nreconstruction,ncube-1,ncube-1,6) :: recons_4D
!
! IF (order<2) THEN
! WRITE(*,*) "order out of range",order
! STOP
!! END IF
!
! ncube_reconstruct = ncube
!
! DO ip=1,6
! DO iy=1,ncube-1
! DO ix=1,ncube-1
! ii = (ip-1)*(ncube-1)*(ncube-1)+(iy-1)*(ncube-1)+ix
! fcube(ix,iy,ip) = fcube_in(ii)
! END DO
! END DO
! END DO
!
!
! ALLOCATE(gp (ncube_reconstruct), STAT=status)
! ! Equi-angular grid in [-piq, piq]
! DO k = 1, ncube_reconstruct
! gp(k) = -piq + (pi / DBLE(2 * (ncube_reconstruct-1))) * DBLE(k-1)
! ENDDO
!
! WRITE(*,*) "Compute centroids"
! CALL ComputeABPElementCentroids(order,DAcube,ncube_reconstruct)
!! WRITE(*,*) "abp_centroid(1,1,1)",abp_centroid(1,1,1)
! WRITE(*,*) "Compute reconstruction coefficients"
! CALL ReconstructABPGradient(fcube, 3, 2, order, kmono, recons_4D, kpd, 2)
! DEALLOCATE(gp)
! recons = 0.0
! DEALLOCATE(abp_centroid)
! DEALLOCATE(gp)
! END SUBROUTINE get_halo
!
! wrapper subroutine to use the CSLAM code
!
! note that ncube_reconstruct is ncube_reconstruct+1 in topo software
!
SUBROUTINE get_reconstruction(fcube_in,order, kmono, recons, kpd,DAcube,ncube,nreconstruction,centroids)
IMPLICIT NONE
INTEGER (KIND=int_kind), INTENT(IN) :: order, kmono, kpd,ncube,nreconstruction
REAL (KIND=dbl_kind), &
DIMENSION(6*(ncube-1)*(ncube-1)), INTENT(IN) :: fcube_in
REAL (KIND=dbl_kind), DIMENSION(nreconstruction,6*(ncube-1)*(ncube-1)), INTENT(OUT) :: recons
REAL (KIND=dbl_kind), DIMENSION(ncube-1,ncube-1), INTENT(IN) :: dAcube
REAL (KIND=dbl_kind), DIMENSION(nreconstruction,6*(ncube-1)*(ncube-1)), INTENT(OUT) :: centroids
! Local variables
REAL (KIND=dbl_kind), &
DIMENSION(1:ncube-1, 1:ncube-1, 6) :: fcube
INTEGER (KIND=int_kind) :: status,k,ip,ii,ix,iy
REAL (KIND=dbl_kind), DIMENSION(nreconstruction,ncube-1,ncube-1,6) :: recons_4D
IF (order<2) THEN
WRITE(*,*) "order out of range",order
STOP
END IF
ncube_reconstruct = ncube
DO ip=1,6
DO iy=1,ncube-1
DO ix=1,ncube-1
ii = (ip-1)*(ncube-1)*(ncube-1)+(iy-1)*(ncube-1)+ix
fcube(ix,iy,ip) = fcube_in(ii)
END DO
END DO
END DO
ALLOCATE(gp (ncube_reconstruct), STAT=status)
! Equi-angular grid in [-piq, piq]
DO k = 1, ncube_reconstruct
gp(k) = -piq + (pi / DBLE(2 * (ncube_reconstruct-1))) * DBLE(k-1)
ENDDO
WRITE(*,*) "Compute centroids"
CALL ComputeABPElementCentroids(order,DAcube,ncube_reconstruct)
! WRITE(*,*) "abp_centroid(1,1,1)",abp_centroid(1,1,1)
WRITE(*,*) "Compute reconstruction coefficients"
CALL ReconstructABPGradient(fcube, 3, 2, order, kmono, recons_4D, kpd, 2)
DEALLOCATE(gp)
WRITE(*,*) "map to 1D arrays"
recons = 0.0
DO ip=1,6
DO iy=1,ncube-1
DO ix=1,ncube-1
ii = (ip-1)*(ncube-1)*(ncube-1)+(iy-1)*(ncube-1)+ix
recons(:,ii) = recons_4D(:,ix,iy,ip)
centroids(:,ii) = abp_centroid(:,ix,iy)
END DO
END DO
END DO
DEALLOCATE(abp_centroid)
END SUBROUTINE get_reconstruction
!------------------------------------------------------------------------------
! SUBROUTINE ComputeABPElementCentroids
!
! Description:
! Compute the centroid coordinates of ABP elements.
!
! Note:
! ComputeABPW8.0s_1 must be called prior to calling this function.
!
! Parameters:
! order - Order of the method being applied
!------------------------------------------------------------------------------
SUBROUTINE ComputeABPElementCentroids(order,DAcube,ncube_reconstruct)
IMPLICIT NONE
INTEGER (KIND=int_kind) , INTENT(IN) :: order,ncube_reconstruct
REAL (KIND=dbl_kind), DIMENSION(ncube_reconstruct-1,ncube_reconstruct-1), INTENT(IN) :: dAcube
INTEGER (KIND=int_kind) :: i, j, k
REAL (KIND=dbl_kind) :: alpha, beta, alpha_next, beta_next, area
! Equi-angular grid in [-piq, piq]
DO k = 1, ncube_reconstruct
gp(k) = -piq + (pi / DBLE(2 * (ncube_reconstruct-1))) * DBLE(k-1)
ENDDO
! Allocate space for centroid calculations
IF (order == 1) THEN
RETURN
ELSEIF (order == 2) THEN
WRITE(*,*) "allocate abp_centroid"
ALLOCATE(abp_centroid(2, -1:ncube_reconstruct+1, -1:ncube_reconstruct+1))
ELSEIF (order == 3) THEN
WRITE(*,*) "allocate abp_centroid"
ALLOCATE(abp_centroid(5, -1:ncube_reconstruct+1, -1:ncube_reconstruct+1))
ELSE
WRITE(*,*) 'Fatal Error: In ComputeABPElementCentroids'
WRITE(*,*) 'order out of range [1-3], given: ', order
STOP
ENDIF
WRITE(*,*) 'Begin computing ABP element centroids'
! Compute centroids via line integrals
abp_centroid = 0.0
DO j = -1, ncube_reconstruct+1
IF ((j > 0) .AND. (j < ncube_reconstruct)) THEN
beta = gp(j)
beta_next = gp(j+1)
ELSEIF (j == -1) THEN
beta = -piq - (gp(3) + piq)
beta_next = -piq - (gp(2) + piq)
ELSEIF (j == 0) THEN
beta = -piq - (gp(2) + piq)
beta_next = -piq
ELSEIF (j == ncube_reconstruct) THEN
beta = piq
beta_next = piq + (piq - gp(ncube_reconstruct-1))
ELSEIF (j == ncube_reconstruct+1) THEN
beta = piq + (piq - gp(ncube_reconstruct-1))
beta_next = piq + (piq - gp(ncube_reconstruct-2))
ENDIF
DO i = -1, ncube_reconstruct+1
IF ((i > 0) .AND. (i < ncube_reconstruct)) THEN
alpha = gp(i)
alpha_next = gp(i+1)
ELSEIF (i == -1) THEN
alpha = -piq - (gp(3) + piq)
alpha_next = -piq - (gp(2) + piq)
ELSEIF (i == 0) THEN
alpha = -piq - (gp(2) + piq)
alpha_next = -piq
ELSEIF (i == ncube_reconstruct) THEN
alpha = piq
alpha_next = piq + (piq - gp(ncube_reconstruct-1))
ELSEIF (i == ncube_reconstruct+1) THEN
alpha = piq + (piq - gp(ncube_reconstruct-1))
alpha_next = piq + (piq - gp(ncube_reconstruct-2))
ENDIF
abp_centroid(1,i,j) = &
I_10_ab(alpha_next,beta_next)-I_10_ab(alpha ,beta_next)+&
I_10_ab(alpha ,beta )-I_10_ab(alpha_next,beta )
! - ASINH(COS(alpha_next) * TAN(beta_next)) &
! + ASINH(COS(alpha_next) * TAN(beta)) &
! + ASINH(COS(alpha) * TAN(beta_next)) &
! - ASINH(COS(alpha) * TAN(beta))
abp_centroid(2,i,j) = &
I_01_ab(alpha_next,beta_next)-I_01_ab(alpha ,beta_next)+&
I_01_ab(alpha ,beta )-I_01_ab(alpha_next,beta )
! - ASINH(TAN(alpha_next) * COS(beta_next)) &
! + ASINH(TAN(alpha_next) * COS(beta)) &
! + ASINH(TAN(alpha) * COS(beta_next)) &
! - ASINH(TAN(alpha) * COS(beta))
!ADD PHL START
IF (order>2) THEN
! TAN(alpha)^2 component
abp_centroid(3,i,j) =&
I_20_ab(alpha_next,beta_next)-I_20_ab(alpha ,beta_next)+&
I_20_ab(alpha ,beta )-I_20_ab(alpha_next,beta )
! TAN(beta)^2 component
abp_centroid(4,i,j) = &
I_02_ab(alpha_next,beta_next)-I_02_ab(alpha ,beta_next)+&
I_02_ab(alpha ,beta )-I_02_ab(alpha_next,beta )
! TAN(alpha) TAN(beta) component
abp_centroid(5,i,j) = &
I_11_ab(alpha_next,beta_next)-I_11_ab(alpha ,beta_next)+&
I_11_ab(alpha ,beta )-I_11_ab(alpha_next,beta )
ENDIF
!ADD PHL END
ENDDO
ENDDO
!
! PHL outcommented below
!
! High order calculations
! IF (order > 2) THEN
! DO k = 1, nlon
! DO i = 1, int_nx(nlat,k)-1
! IF ((int_itype(i,k) > 4) .AND. (int_np(1,i,k) == 1)) THEN
! abp_centroid(3, int_a(i,k), int_b(i,k)) = &
! abp_centroid(3, int_a(i,k), int_b(i,k)) + int_wt_2a(i,k)
! abp_centroid(4, int_a(i,k), int_b(i,k)) = &
! abp_centroid(4, int_a(i,k), int_b(i,k)) + int_wt_2b(i,k)
! abp_centroid(5, int_a(i,k), int_b(i,k)) = &
! abp_centroid(5, int_a(i,k), int_b(i,k)) + int_wt_2c(i,k)
! ENDIF
! ENDDO
! ENDDO
! ENDIF
! Normalize with element areas
DO j = -1, ncube_reconstruct+1
IF ((j > 0) .AND. (j < ncube_reconstruct)) THEN
beta = gp(j)
beta_next = gp(j+1)
ELSEIF (j == -1) THEN
beta = -piq - (gp(3) + piq)
beta_next = -piq - (gp(2) + piq)
ELSEIF (j == 0) THEN
beta = -piq - (gp(2) + piq)
beta_next = -piq
ELSEIF (j == ncube_reconstruct) THEN
beta = piq
beta_next = piq + (piq - gp(ncube_reconstruct-1))
ELSEIF (j == ncube_reconstruct+1) THEN
beta = piq + (piq - gp(ncube_reconstruct-1))
beta_next = piq + (piq - gp(ncube_reconstruct-2))
ENDIF
DO i = -1, ncube_reconstruct+1
IF ((i > 0) .AND. (i < ncube_reconstruct)) THEN
alpha = gp(i)
alpha_next = gp(i+1)
ELSEIF (i == -1) THEN
alpha = -piq - (gp(3) + piq)
alpha_next = -piq - (gp(2) + piq)
ELSEIF (i == 0) THEN
alpha = -piq - (gp(2) + piq)
alpha_next = -piq
ELSEIF (i == ncube_reconstruct) THEN
alpha = piq
alpha_next = piq + (piq - gp(ncube_reconstruct-1))
ELSEIF (i == ncube_reconstruct+1) THEN
alpha = piq + (piq - gp(ncube_reconstruct-1))
alpha_next = piq + (piq - gp(ncube_reconstruct-2))
ENDIF
IF ((i > 0) .AND. (i < ncube_reconstruct) .AND. (j > 0) .AND. (j < ncube_reconstruct)) THEN
area = DAcube(i,j)
ELSE
area = EquiangularElementArea(alpha, alpha_next - alpha, &
beta, beta_next - beta)
ENDIF
abp_centroid(1,i,j) = abp_centroid(1,i,j) / area
abp_centroid(2,i,j) = abp_centroid(2,i,j) / area
IF (order > 2) THEN
IF ((i > 0) .AND. (i < ncube_reconstruct) .AND. (j > 0) .AND. (j < ncube_reconstruct)) THEN
abp_centroid(3,i,j) = abp_centroid(3,i,j) / area
abp_centroid(4,i,j) = abp_centroid(4,i,j) / area
abp_centroid(5,i,j) = abp_centroid(5,i,j) / area
ENDIF
ENDIF
ENDDO
ENDDO
WRITE(*,*) '...Done computing ABP element centroids'
END SUBROUTINE ComputeABPElementCentroids
!------------------------------------------------------------------------------
! FUNCTION EvaluateABPReconstruction
!
! Description:
! Evaluate the sub-grid scale reconstruction at the given point.
!
! Parameters:
! fcubehalo - Array of element values
! recons - Array of reconstruction coefficients
! a - Index of element in alpha direction (1 <= a <= ncube_reconstruct-1)
! b - Index of element in beta direction (1 <= b <= ncube_reconstruct-1)
! p - Panel index of element
! alpha - Alpha coordinate of evaluation point
! beta - Beta coordinate of evaluation point
! order - Order of the reconstruction
! value (OUT) - Result of function evaluation at given point
!------------------------------------------------------------------------------
SUBROUTINE EvaluateABPReconstruction( &
fcubehalo, recons, a, b, p, alpha, beta, order, value)
IMPLICIT NONE
! Dummy variables
REAL (KIND=dbl_kind), DIMENSION(-1:ncube_reconstruct+1, -1:ncube_reconstruct+1, 6), &
INTENT(IN) :: fcubehalo
REAL (KIND=dbl_kind), DIMENSION(:,:,:,:), INTENT(IN) :: recons
INTEGER (KIND=int_kind), INTENT(IN) :: a, b, p
REAL (KIND=dbl_kind), INTENT(IN) :: alpha, beta
INTEGER (KIND=int_kind), INTENT(IN) :: order
REAL (KIND=dbl_kind), INTENT(OUT) :: value
! Evaluate constant order terms
value = fcubehalo(a,b,p)
! Evaluate linear order terms
IF (order > 1) THEN
value = value + recons(1,a,b,p) * (TAN(alpha) - abp_centroid(1,a,b))
value = value + recons(2,a,b,p) * (TAN(beta) - abp_centroid(2,a,b))
ENDIF
! Evaluate second order terms
IF (order > 2) THEN
value = value + recons(3,a,b,p) * &
(abp_centroid(1,a,b)**2 - abp_centroid(3,a,b))
value = value + recons(4,a,b,p) * &
(abp_centroid(2,a,b)**2 - abp_centroid(4,a,b))
value = value + recons(5,a,b,p) * &
(abp_centroid(1,a,b) * abp_centroid(2,a,b) - &
abp_centroid(5,a,b))
value = value + recons(3,a,b,p) * (TAN(alpha) - abp_centroid(1,a,b))**2
value = value + recons(4,a,b,p) * (TAN(beta) - abp_centroid(2,a,b))**2
value = value + recons(5,a,b,p) * (TAN(alpha) - abp_centroid(1,a,b)) &
* (TAN(beta) - abp_centroid(2,a,b))
ENDIF
END SUBROUTINE
!------------------------------------------------------------------------------
! SUBROUTINE ABPHaloMinMax
!
! Description:
! Calculate the minimum and maximum values of the cell-averaged function
! around the given element.
!
! Parameters:
! fcubehalo - Cell-averages for the cubed sphere
! a - Local element alpha index
! b - Local element beta index
! p - Local element panel index
! min_val (OUT) - Minimum value in the halo
! max_val (OUT) - Maximum value in the halo
! nomiddle - whether to not include the middle cell (index a,b) in the search.
!
! NOTE: Since this routine is not vectorized, it will likely be called MANY times.
! To speed things up, make sure to pass the first argument as the ENTIRE original
! array, not as a subset of it, since repeatedly cutting up that array and creating
! an array temporary (on some compilers) is VERY slow.
! ex:
! CALL APBHaloMinMax(zarg, a, ...) !YES
! CALL ABPHaloMinMax(zarg(-1:ncube_reconstruct+1,-1:ncube_reconstruct+1,:)) !NO -- slow
!------------------------------------------------------------------------------
SUBROUTINE ABPHaloMinMax(fcubehalo, a, b, p, min_val, max_val, nomiddle)
IMPLICIT NONE
REAL (KIND=dbl_kind), DIMENSION(-1:ncube_reconstruct+1, -1:ncube_reconstruct+1, 6), &
INTENT(IN) :: fcubehalo
INTEGER (KIND=int_kind), INTENT(IN) :: a, b, p
REAL (KIND=dbl_kind), INTENT(OUT) :: min_val, max_val
LOGICAL, INTENT(IN) :: nomiddle
! Local variables
INTEGER (KIND=int_kind) :: i, j, il, jl, inew, jnew
REAL (KIND=dbl_kind) :: value
min_val = fcubehalo(a,b,p)
max_val = fcubehalo(a,b,p)
value = fcubehalo(a,b,p)
DO il = a-1,a+1
DO jl = b-1,b+1
i = il
j = jl
inew = i
jnew = j
IF (nomiddle .AND. i==a .AND. j==b) CYCLE
!Interior
IF ((i > 0) .AND. (i < ncube_reconstruct) .AND. (j > 0) .AND. (j < ncube_reconstruct)) THEN
value = fcubehalo(i,j,p)
ELSE
!The next 4.0 regions are cases in which a,b themselves lie in the panel's halo, and the cell's "halo" (in this usage the 8.0 cells surrounding it)
!might wrap around into another part of the halo. This happens for (a,b) = {(1,:0),(ncube_reconstruct-1,:0),(1,ncube_reconstruct:),(ncube_reconstruct-1,ncube_reconstruct:)}
!and for the transposes thereof ({(:0,1), etc.}). In these cases (i,j) could lie in the "Corners" where nothing should lie.
!We correct this by moving i,j to its appropriate position on the "facing" halo, and then the remainder of the routine then moves it onto the correct face.
!NOTE: we need the general case to be able to properly handle (0,0), (ncube_reconstruct,0), etc.
!Note that we don't need to bother with (0,0), etc. when a, b lie in the interior, since both sides of the (0,0) cell are already accounted for by this routine.
!LOWER LEFT
IF (i < 1 .AND. j < 1) THEN
IF (a < 1) THEN !(a,b) centered on left halo, cross to lower halo
inew = 1-j
jnew = i
ELSE IF (b < 1) THEN !(a,b) centered on lower halo, cross to left halo
jnew = 1-i
inew = j
END IF
! WRITE(*,102) i, j, p, inew, jnew, 1
!LOWER RIGHT
ELSE IF (i > ncube_reconstruct-1 .AND. j < 1) THEN
IF (a > ncube_reconstruct-1) THEN !(a,b) centered on right halo, cross to lower halo
inew = ncube_reconstruct-1+j
jnew = ncube_reconstruct-i
ELSE IF (b < 1) THEN !(a,b) centered on lower halo, cross to right halo
jnew = 1+(i-ncube_reconstruct)
inew = ncube_reconstruct-j
END IF
! WRITE(*,102) i, j, p, inew, jnew, 2
!UPPER LEFT
ELSE IF (i < 1 .AND. j > ncube_reconstruct-1) THEN
IF (a < 1) THEN! (a,b) centered on left halo, cross to upper halo
inew = 1-(j-ncube_reconstruct)
jnew = ncube_reconstruct-i
ELSE IF (b > ncube_reconstruct-1) THEN !(a,b) centered on upper halo, cross to left halo
inew = ncube_reconstruct-j
jnew = ncube_reconstruct-1-i
END IF
! WRITE(*,102) i, j, p, inew, jnew, 3
!UPPER RIGHT
ELSE IF (i > ncube_reconstruct-1 .AND. j > ncube_reconstruct-1) THEN
IF (a > ncube_reconstruct-1) THEN !(a,b) centered on right halo, cross to upper halo
inew = ncube_reconstruct-1-(ncube_reconstruct-j)
jnew = i
ELSE IF (b > ncube_reconstruct-1) THEN !(a,b) centered on upper halo, cross to right halo
inew = j
jnew = ncube_reconstruct-1-(ncube_reconstruct-i)
END IF
! WRITE(*,102) i, j, p, inew, jnew, 4
END IF
i = inew
j = jnew
!Lower halo ("halo" meaning the panel's halo, not the nine-cell halo
IF ((i < 1) .AND. (j > 0) .AND. (j < ncube_reconstruct)) THEN
IF (p == 1) THEN
value = fcubehalo(ncube_reconstruct-1+i,j,4)
ELSEIF (p == 2) THEN
value = fcubehalo(ncube_reconstruct-1+i,j,1)
ELSEIF (p == 3) THEN
value = fcubehalo(ncube_reconstruct-1+i,j,2)
ELSEIF (p == 4) THEN
value = fcubehalo(ncube_reconstruct-1+i,j,3)
ELSEIF (p == 5) THEN
value = fcubehalo(j,1-i,4)
ELSEIF (p == 6) THEN
value = fcubehalo(ncube_reconstruct-j,ncube_reconstruct-1+i,4)
ENDIF
!Upper halo
ELSEIF ((i > ncube_reconstruct-1) .AND. (j > 0) .AND. (j < ncube_reconstruct)) THEN
IF (p == 1) THEN
value = fcubehalo(i-ncube_reconstruct+1,j,2)
ELSEIF (p == 2) THEN
value = fcubehalo(i-ncube_reconstruct+1,j,3)
ELSEIF (p == 3) THEN
value = fcubehalo(i-ncube_reconstruct+1,j,4)
ELSEIF (p == 4) THEN
value = fcubehalo(i-ncube_reconstruct+1,j,1)
ELSEIF (p == 5) THEN
value = fcubehalo(ncube_reconstruct-j,i-ncube_reconstruct+1,2)
ELSEIF (p == 6) THEN
value = fcubehalo(j,2*ncube_reconstruct-i-1,2)
ENDIF
!Left halo
ELSEIF ((j < 1) .AND. (i > 0) .AND. (i < ncube_reconstruct)) THEN
IF (p == 1) THEN
value = fcubehalo(i,ncube_reconstruct-1+j,5)
ELSEIF (p == 2) THEN
value = fcubehalo(ncube_reconstruct-1+j,ncube_reconstruct-i,5)
ELSEIF (p == 3) THEN
value = fcubehalo(ncube_reconstruct-i,1-j,5)
ELSEIF (p == 4) THEN
value = fcubehalo(1-j,i,5)
ELSEIF (p == 5) THEN
value = fcubehalo(ncube_reconstruct-i,1-j,3)
ELSEIF (p == 6) THEN
value = fcubehalo(i,ncube_reconstruct-1+j,1)
ENDIF
!Right halo
ELSEIF ((j > ncube_reconstruct-1) .AND. (i > 0) .AND. (i < ncube_reconstruct)) THEN
IF (p == 1) THEN
value = fcubehalo(i,j-ncube_reconstruct+1,6)
ELSEIF (p == 2) THEN
value = fcubehalo(2*ncube_reconstruct-j-1,i,6)
ELSEIF (p == 3) THEN
value = fcubehalo(ncube_reconstruct-i, 2*ncube_reconstruct-j-1,6)
ELSEIF (p == 4) THEN
value = fcubehalo(j-ncube_reconstruct+1,ncube_reconstruct-i,6)
ELSEIF (p == 5) THEN
value = fcubehalo(i,j-ncube_reconstruct+1,1)
ELSEIF (p == 6) THEN
value = fcubehalo(ncube_reconstruct-i, 2*ncube_reconstruct-j-1,3)
ENDIF
ENDIF
END IF
min_val = MIN(min_val, value)
max_val = MAX(max_val, value)
ENDDO
ENDDO
END SUBROUTINE
!------------------------------------------------------------------------------
! SUBROUTINE MonotonizeABPGradient
!
! Description:
! Apply a monotonic filter to the calculated ABP gradient.
!
! Parameters:
! fcubehalo - Scalar field on the cubed sphere to use in reconstruction
! order - Order of the reconstruction
! recons (INOUT) - Array of reconstructed coefficients
! selective - whether to apply a simple form of selective limiting,
!which assumes that if a point is larger/smaller than ALL of its
!surrounding points, that the extremum is physical, and that
!filtering should not be applied to it.
!
! Remarks:
! This monotonizing scheme is based on the monotone scheme for unstructured
! grids of Barth and Jespersen (1989).
!------------------------------------------------------------------------------
SUBROUTINE MonotonizeABPGradient(fcubehalo, order, recons, selective)
! USE selective_limiting
IMPLICIT NONE
REAL (KIND=dbl_kind), DIMENSION(-1:ncube_reconstruct+1, -1:ncube_reconstruct+1, 6), &
INTENT(IN) :: fcubehalo
INTEGER (KIND=int_kind), INTENT(IN) :: order
LOGICAL, INTENT(IN) :: selective
REAL (KIND=dbl_kind), DIMENSION(:,:,:,:), INTENT(INOUT) :: recons
! Local variables
INTEGER (KIND=int_kind) :: i, j, k, m, n, skip
REAL (KIND=dbl_kind) :: local_min, local_max, value, min_phi
REAL (KIND=dbl_kind) :: disc, mx, my, gamma_min, gamma_max
REAL (KIND=dbl_kind), DIMENSION(-1:ncube_reconstruct+1, -1:ncube_reconstruct+1, 6) :: &
gamma
! The first-order piecewise constant scheme is monotone by construction
IF (order == 1) THEN
RETURN
ENDIF
!
! xxxxx
!
! IF (selective) THEN
! CALL smoothness2D(fcubehalo, gamma, 2)
! WRITE(*,*) 'gamma range: max ', MAXVAL(gamma), " min ", MINVAL(gamma)
! DO i=1,ncube_reconstruct-1
! WRITE(*,*) gamma(i, i, 3)
! ENDDO
! skip = 0
! END IF
! Apply monotone limiting
DO k = 1, 6
DO j = 1, ncube_reconstruct-1
DO i = 1, ncube_reconstruct-1
IF (selective) THEN
CALL ABPHaloMinMax(gamma, i, j, k, gamma_min, gamma_max, .FALSE.)
IF (gamma_max/(gamma_min + tiny) < lammax) THEN
skip = skip + 1
CYCLE
END IF
END IF
CALL ABPHaloMinMax(fcubehalo, i, j, k, local_min, local_max,.FALSE.)
! Initialize the limiter
min_phi = one
! For the second-order calculation, the minima and maxima will occur
! at the corner points of the element
DO m = i, i+1
DO n = j, j+1
! Evaluate the function at each corner point
CALL EvaluateABPReconstruction( &
fcubehalo, recons, i, j, k, gp(m), gp(n), order, value)
CALL AdjustLimiter( &
value, fcubehalo(i,j,k), local_min, local_max, min_phi)
ENDDO
ENDDO
! For the third order method, the minima and maxima may occur along
! the line segments given by du/dx = 0 and du/dy = 0. Also check
! for the presence of a maxima / minima of the quadratic within
! the domain.
IF (order == 3) THEN
disc = recons(5,i,j,k)**2 - 4.0 * recons(4,i,j,k) * recons(3,i,j,k)
! Check if the quadratic is minimized within the element
IF (ABS(disc) > tiny) THEN
mx = - recons(5,i,j,k) * recons(2,i,j,k) &
+ 2.0 * recons(4,i,j,k) * recons(1,i,j,k)
my = - recons(5,i,j,k) * recons(1,i,j,k) &
+ 2.0 * recons(3,i,j,k) * recons(2,i,j,k)
mx = mx / disc + abp_centroid(1,i,j)
my = my / disc + abp_centroid(2,i,j)
IF ((mx - TAN(gp(i)) > -tiny) .AND. &
(mx - TAN(gp(i+1)) < tiny) .AND. &
(my - TAN(gp(j)) > -tiny) .AND. &
(my - TAN(gp(j+1)) < tiny) &
) THEN
CALL EvaluateABPReconstruction( &
fcubehalo, recons, i, j, k, ATAN(mx), ATAN(my), &
order, value)
CALL AdjustLimiter( &
value, fcubehalo(i,j,k), &
local_min, local_max, min_phi)
ENDIF
ENDIF
! Check all potential minimizer points along element boundaries
IF (ABS(recons(5,i,j,k)) > tiny) THEN
! Left/right edge, intercept with du/dx = 0
DO m = i, i+1
my = - recons(1,i,j,k) - 2.0 * recons(3,i,j,k) * &
(TAN(gp(m)) - abp_centroid(1,i,j))
my = my / recons(5,i,j,k) + abp_centroid(2,i,j)
IF ((my < TAN(gp(j))) .OR. (my > TAN(gp(j+1)))) THEN
CYCLE
ENDIF
CALL EvaluateABPReconstruction( &
fcubehalo, recons, i, j, k, gp(m), ATAN(my), &
order, value)
CALL AdjustLimiter( &
value, fcubehalo(i,j,k), &
local_min, local_max, min_phi)
ENDDO
! Top/bottom edge, intercept with du/dy = 0
DO n = j, j+1
mx = - recons(2,i,j,k) - 2.0 * recons(4,i,j,k) * &
(TAN(gp(n)) - abp_centroid(2,i,j))
mx = mx / recons(5,i,j,k) + abp_centroid(1,i,j)
IF ((mx < TAN(gp(i))) .OR. (mx > TAN(gp(i+1)))) THEN
CYCLE
ENDIF
CALL EvaluateABPReconstruction( &
fcubehalo, recons, i, j, k, ATAN(mx), gp(n), &
order, value)
CALL AdjustLimiter( &
value, fcubehalo(i,j,k), &
local_min, local_max, min_phi)
ENDDO
ENDIF
! Top/bottom edge, intercept with du/dx = 0
IF (ABS(recons(3,i,j,k)) > tiny) THEN
DO n = j, j+1
mx = - recons(1,i,j,k) - recons(5,i,j,k) * &
(TAN(gp(n)) - abp_centroid(2,i,j))
mx = mx / (2.0 * recons(3,i,j,k)) + abp_centroid(1,i,j)
IF ((mx < TAN(gp(i))) .OR. (mx > TAN(gp(i+1)))) THEN
CYCLE
ENDIF
CALL EvaluateABPReconstruction( &
fcubehalo, recons, i, j, k, ATAN(mx), gp(n), &
order, value)
CALL AdjustLimiter( &
value, fcubehalo(i,j,k), &
local_min, local_max, min_phi)
ENDDO
ENDIF
! Left/right edge, intercept with du/dy = 0
IF (ABS(recons(4,i,j,k)) > tiny) THEN
DO m = i, i+1
my = - recons(2,i,j,k) - recons(5,i,j,k) * &
(TAN(gp(m)) - abp_centroid(1,i,j))
my = my / (2.0 * recons(4,i,j,k)) + abp_centroid(2,i,j)
IF ((my < TAN(gp(j))) .OR. (my > TAN(gp(j+1)))) THEN
CYCLE
ENDIF
CALL EvaluateABPReconstruction( &
fcubehalo, recons, i, j, k, gp(m), ATAN(my), &
order, value)
CALL AdjustLimiter( &
value, fcubehalo(i,j,k), &
local_min, local_max, min_phi)
ENDDO
ENDIF
ENDIF
IF ((min_phi < -tiny) .OR. (min_phi > one + tiny)) THEN
WRITE (*,*) 'Fatal Error: In MonotonizeABPGradient'
WRITE (*,*) 'Slope limiter out of range: ', min_phi
STOP
ENDIF
! Apply monotone limiter to all reconstruction coefficients
recons(1,i,j,k) = min_phi * recons(1,i,j,k)
recons(2,i,j,k) = min_phi * recons(2,i,j,k)
IF (order > 2) THEN
recons(3,i,j,k) = min_phi * recons(3,i,j,k)
recons(4,i,j,k) = min_phi * recons(4,i,j,k)
recons(5,i,j,k) = min_phi * recons(5,i,j,k)
ENDIF
ENDDO
ENDDO
ENDDO
IF (selective) WRITE(*,*) 'skipped ', skip, ' points out of ', 6*(ncube_reconstruct-1)**2
END SUBROUTINE
!------------------------------------------------------------------------------
! SUBROUTINE PosDefABPGradient
!
! Description:
! Scale the reconstructions so they are positive definite
!
! Parameters:
! fcubehalo - Scalar field on the cubed sphere to use in reconstruction
! order - Order of the reconstruction
! recons (INOUT) - Array of reconstructed coefficients
!
! Remarks:
! This monotonizing scheme is based on the monotone scheme for unstructured
! grids of Barth and Jespersen (1989), but simpler. This simply finds the
! minimum and then scales the reconstruction so that it is 0.
!------------------------------------------------------------------------------
SUBROUTINE PosDefABPGradient(fcubehalo, order, recons)
IMPLICIT NONE
REAL (KIND=dbl_kind), DIMENSION(-1:ncube_reconstruct+1, -1:ncube_reconstruct+1, 6), &
INTENT(IN) :: fcubehalo
INTEGER (KIND=int_kind), INTENT(IN) :: order
REAL (KIND=dbl_kind), DIMENSION(:,:,:,:), INTENT(INOUT) :: recons
! Local variables
INTEGER (KIND=int_kind) :: i, j, k, m, n
REAL (KIND=dbl_kind) :: local_min, local_max, value, min_phi
REAL (KIND=dbl_kind) :: disc, mx, my
! The first-order piecewise constant scheme is monotone by construction
IF (order == 1) THEN
RETURN
ENDIF
! Apply monotone limiting
DO k = 1, 6
DO j = 1, ncube_reconstruct-1
DO i = 1, ncube_reconstruct-1
!If the average value in the cell is 0.0, then we should skip
!all of the scaling and just set the reconstruction to 0.0
! IF (ABS(fcubehalo(i,j,k)) < tiny) THEN
! recons(:,i,j,k) = 0.0
! CYCLE
! END IF
CALL ABPHaloMinMax(fcubehalo, i, j, k, local_min, local_max,.FALSE.)
!This allowance for miniscule negative values appearing around the cell being
!filtered/limited. Before this, negative values would be caught in adjust_limiter
!and would stop the model. Doing this only causes minor negative values; no blowing
!up is observed. The rationale is the same as for the monotone filter, which does
!allow miniscule negative values due to roundoff error --- of the order E-10 ---
!in flux-form methods (and E-17 in the s-L method, indicating that roundoff error
!is more severe in the flux-form method, as we expect since we are often subtracting
!2.0 values which are very close together.
local_min = MIN(0.0_dbl_kind,local_min)
local_max = bignum !prevents scaling upward; for positive
!definite limiting we don't care about the upper bound
! Initialize the limiter
min_phi = one
! For the second-order calculation, the minima and maxima will occur
! at the corner points of the element
DO m = i, i+1
DO n = j, j+1
! Evaluate the function at each corner point
CALL EvaluateABPReconstruction( &
fcubehalo, recons, i, j, k, gp(m), gp(n), order, value)
CALL AdjustLimiter( &
value, fcubehalo(i,j,k), local_min, local_max, min_phi)
ENDDO
ENDDO
! For the third order method, the minima and maxima may occur along
! the line segments given by du/dx = 0 and du/dy = 0. Also check
! for the presence of a maxima / minima of the quadratic within
! the domain.
IF (order == 3) THEN
disc = recons(5,i,j,k)**2 - 4.0 * recons(4,i,j,k) * recons(3,i,j,k)
! Check if the quadratic is minimized within the element
IF (ABS(disc) > tiny) THEN
mx = - recons(5,i,j,k) * recons(2,i,j,k) &
+ 2.0 * recons(4,i,j,k) * recons(1,i,j,k)
my = - recons(5,i,j,k) * recons(1,i,j,k) &
+ 2.0 * recons(3,i,j,k) * recons(2,i,j,k)
mx = mx / disc + abp_centroid(1,i,j)
my = my / disc + abp_centroid(2,i,j)
IF ((mx - TAN(gp(i)) > -tiny) .AND. &
(mx - TAN(gp(i+1)) < tiny) .AND. &
(my - TAN(gp(j)) > -tiny) .AND. &
(my - TAN(gp(j+1)) < tiny) &
) THEN
CALL EvaluateABPReconstruction( &
fcubehalo, recons, i, j, k, ATAN(mx), ATAN(my), &
order, value)
CALL AdjustLimiter( &
value, fcubehalo(i,j,k), &
local_min, local_max, min_phi)
ENDIF
ENDIF
! Check all potential minimizer points along element boundaries
IF (ABS(recons(5,i,j,k)) > tiny) THEN
! Left/right edge, intercept with du/dx = 0
DO m = i, i+1
my = - recons(1,i,j,k) - 2.0 * recons(3,i,j,k) * &
(TAN(gp(m)) - abp_centroid(1,i,j))
my = my / recons(5,i,j,k) + abp_centroid(2,i,j)
IF ((my < TAN(gp(j))) .OR. (my > TAN(gp(j+1)))) THEN
CYCLE
ENDIF
CALL EvaluateABPReconstruction( &
fcubehalo, recons, i, j, k, gp(m), ATAN(my), &
order, value)
CALL AdjustLimiter( &
value, fcubehalo(i,j,k), &
local_min, local_max, min_phi)
ENDDO
! Top/bottom edge, intercept with du/dy = 0
DO n = j, j+1
mx = - recons(2,i,j,k) - 2.0 * recons(4,i,j,k) * &
(TAN(gp(n)) - abp_centroid(2,i,j))
mx = mx / recons(5,i,j,k) + abp_centroid(1,i,j)
IF ((mx < TAN(gp(i))) .OR. (mx > TAN(gp(i+1)))) THEN
CYCLE
ENDIF