-
Notifications
You must be signed in to change notification settings - Fork 0
/
remap.F90
1672 lines (1473 loc) · 57.6 KB
/
remap.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 remap
INTEGER, PARAMETER :: &
int_kind = KIND(1), &
real_kind = SELECTED_REAL_KIND(p=14,r=100),&
dbl_kind = selected_real_kind(13)
INTEGER :: nc,nhe
! LOGICAL, PARAMETER:: ldbgr_r = .FALSE.
LOGICAL :: ldbgr
LOGICAL :: ldbg_global
REAL(kind=real_kind), PARAMETER :: &
one = 1.0 ,&
aa = 1.0 ,&
tiny= 1.0E-9 ,&
bignum = 1.0E20
REAL (KIND=dbl_kind), parameter :: fuzzy_width = 10.0*tiny !CAM-SE add
contains
!*******************************************************************************
!
! Begin calculation of overlap weights
!
!*******************************************************************************
! Concepts:
! source grid ('eul') : Cells on which data (e.g. elevation) is provided.
! Here this grid is the equal-area cubed sphere with
! 6 panels of ncube x ncube cells
!
! target grid ('lgr') : Cells to which data is mapped from 'eul'. Here
! this will normally be a spectral element grid with
! ntarget cells
!
! exchange grid : Cells formed by cutting the source grid and target
! grid through each other. The number of cells in this
! grid is difficult to know a-priori. Will be determined
! in subroutine overlap_weights
!
! nreconstruction : Here set to 1 (a few lines above). Order of subgrid reconstruction
! function in source grid cells. 1 means assumed piecewise constant
! in source cells
!
!********************************************************************************
function remap_field(field,area_target,weights_eul_index_all,weights_lgr_index_all,weights_all,ncube,jall,&
nreconstruction,ntarget) result(f)
use shr_kind_mod, only: r8 => shr_kind_r8
implicit none
real(r8), intent(in) :: weights_all(jall,nreconstruction)
integer , intent(in) :: weights_eul_index_all(jall,3),weights_lgr_index_all(jall)
integer , intent(in) :: ncube,jall,nreconstruction,ntarget
real(r8), intent(in) :: field(6*ncube*ncube),area_target(ntarget)
real(r8):: f(ntarget)
integer :: i,ix,iy,ip,ii,counti
real(r8):: wt
real(r8):: ftarget(ntarget)
f=0.0D0
do counti=1,jall
i = weights_lgr_index_all(counti)
ix = weights_eul_index_all(counti,1)
iy = weights_eul_index_all(counti,2)
ip = weights_eul_index_all(counti,3)
!
! convert to 1D indexing of cubed-sphere
!
ii = (ip-1)*ncube*ncube+(iy-1)*ncube+ix
wt = weights_all(counti,1)
! Note: Factor wt/area_target(i) is fractional overlap of target and source grid
! cells
!
f(i) = f(i) + wt*field(ii)/area_target(i)
end do
end function remap_field
!==============================================================================================================
function select_sg_field(field,weights_eul_index_all,weights_lgr_index_all,ncube,jall,&
ntarget,itarget) result(ird)
use shr_kind_mod, only: r8 => shr_kind_r8
implicit none
integer , intent(in) :: weights_eul_index_all(jall,3),weights_lgr_index_all(jall)
integer , intent(in) :: ncube,jall,ntarget,itarget
real(r8), intent(in) :: field(6*ncube*ncube)
! real(r8):: fsg(6*ncube*ncube)
integer :: i,ix,iy,ip,ii,counti
real(r8):: wt
real(r8):: ftarget(ntarget)
integer :: ijp3(10000,3),ird
ird=1
!!fsg=0.0D0
do counti=1,jall
i = weights_lgr_index_all(counti)
if (itarget == i) then
ix = weights_eul_index_all(counti,1)
iy = weights_eul_index_all(counti,2)
ip = weights_eul_index_all(counti,3)
!
! convert to 1D indexing of cubed-sphere
!
ii = (ip-1)*ncube*ncube+(iy-1)*ncube+ix
! Note: Factor wt/area_target(i) is fractional overlap of target and source grid
! cells
!
ijp3(ird,1) = ix
ijp3(ird,2) = iy
ijp3(ird,3) = ip
ird = ird+1
!fsg(ii) = field(ii)
end if
end do
!write(311) ird,ijp3
!write(311) fsg
end function select_sg_field
!==============================================================================================================
function paint_sg_field(field,weights_eul_index_all,weights_lgr_index_all,ncube,jall,&
ntarget,itarget) result(isg)
use shr_kind_mod, only: r8 => shr_kind_r8
implicit none
integer , intent(in) :: weights_eul_index_all(jall,3),weights_lgr_index_all(jall)
integer , intent(in) :: ncube,jall,ntarget,itarget
real(r8), intent(in) :: field(6*ncube*ncube)
integer :: isg(6*ncube*ncube)
integer :: i,ix,iy,ip,ii,counti
real(r8):: wt
real(r8):: ftarget(ntarget)
integer :: ijp3(10000,3),ird
ird=1
isg=-1
do counti=1,jall
i = weights_lgr_index_all(counti)
ix = weights_eul_index_all(counti,1)
iy = weights_eul_index_all(counti,2)
ip = weights_eul_index_all(counti,3)
!
! convert to 1D indexing of cubed-sphere
!
ii = (ip-1)*ncube*ncube+(iy-1)*ncube+ix
isg(ii) = i
end do
!write(311) ncube
!write(311) isg
end function paint_sg_field
!=====================================================================
subroutine compute_weights_cell(xcell_in,ycell_in,jx,jy,nreconstruction,xgno,ygno,&
jx_min, jx_max, jy_min, jy_max,tmp,&
ngauss,gauss_weights,abscissae,weights,weights_eul_index,jcollect,jmax_segments,&
nc_in,nhe_in,nvertex,ldbg)
implicit none
integer (kind=int_kind), intent(in) :: nc_in,nhe_in,nvertex
integer (kind=int_kind) , intent(in):: nreconstruction, jx,jy,ngauss,jmax_segments
real (kind=real_kind) , dimension(0:nvertex+1) :: xcell_in,ycell_in
! real (kind=real_kind) , dimension(0:5), intent(in):: xcell_in,ycell_in
logical, intent(in) :: ldbg
!
! ipanel is just for debugging
!
integer (kind=int_kind), intent(in) :: jx_min, jy_min, jx_max, jy_max
real (kind=real_kind), dimension(-nhe_in:nc_in+2+nhe_in), intent(in) :: xgno
real (kind=real_kind), dimension(-nhe_in:nc_in+2+nhe_in), intent(in) :: ygno
!
! for Gaussian quadrature
!
real (kind=real_kind), dimension(ngauss), intent(in) :: gauss_weights, abscissae
!
! boundaries of domain
!
real (kind=real_kind):: tmp
!
! Number of Eulerian sub-cell integrals for the cell in question
!
integer (kind=int_kind), intent(out) :: jcollect
!
! local workspace
!
!
! max number of line segments is:
!
! (number of longitudes)*(max average number of crossings per line segment = 3)*ncube*2
!
real (kind=real_kind) , &
dimension(jmax_segments,nreconstruction), intent(out) :: weights
integer (kind=int_kind), &
dimension(jmax_segments,2), intent(out) :: weights_eul_index
integer (kind=int_kind) :: jsegment,i
!
! variables for registering crossings with Eulerian latitudes and longitudes
!
integer (kind=int_kind) :: jcross_lat
!
! max. crossings per side is 2*nhe
!
real (kind=real_kind), &
dimension(jmax_segments,2) :: r_cross_lat
integer (kind=int_kind), &
dimension(jmax_segments,2) :: cross_lat_eul_index
real (kind=real_kind) , dimension(1:nvertex) :: xcell,ycell
real (kind=real_kind) :: eps
ldbg_global = ldbg
ldbgr = ldbg
nc = nc_in
nhe = nhe_in
xcell = xcell_in(1:nvertex)
ycell = ycell_in(1:nvertex)
!
! this is to avoid ill-conditioning problems
!
eps = 1.0E-9
jsegment = 0
weights = 0.0D0
jcross_lat = 0
!
!**********************
!
! Integrate cell sides
!
!**********************
IF (jx<-nhe.OR.jx>nc+1+nhe.OR.jy<-nhe.OR.jy>nc+1+nhe) THEN
WRITE(*,*) "jx,jy,-nhe,nc+1+nhe",jx,jy,-nhe,nc+1+nhe
STOP
END IF
call side_integral(xcell,ycell,nvertex,jsegment,jmax_segments,&
weights,weights_eul_index,nreconstruction,jx,jy,xgno,ygno,jx_min, jx_max, jy_min, jy_max,&
ngauss,gauss_weights,abscissae,&
jcross_lat,r_cross_lat,cross_lat_eul_index)
!
!**********************
!
! Do inner integrals
!
!**********************
!
call compute_inner_line_integrals_lat_nonconvex(r_cross_lat,cross_lat_eul_index,&
jcross_lat,jsegment,jmax_segments,xgno,jx_min, jx_max, jy_min, jy_max,&
weights,weights_eul_index,&
nreconstruction,ngauss,gauss_weights,abscissae)
!
! collect line-segment that reside in the same Eulerian cell
!
if (jsegment>0) then
call collect(weights,weights_eul_index,nreconstruction,jcollect,jsegment,jmax_segments)
!
! DBG
!
tmp=0.0
do i=1,jcollect
tmp=tmp+weights(i,1)
enddo
IF (abs(tmp)>0.01) THEN
WRITE(*,*) "sum of weights large",tmp
! stop
END IF
IF (tmp<-1.0E-9) THEN
WRITE(*,*) "sum of weights is negative - negative area?",tmp,jx,jy
! ldbgr=.TRUE.
stop
END IF
else
jcollect = 0
end if
end subroutine compute_weights_cell
!
!****************************************************************************
!
! organize data and store it
!
!****************************************************************************
!
subroutine collect(weights,weights_eul_index,nreconstruction,jcollect,jsegment,jmax_segments)
implicit none
integer (kind=int_kind), INTENT(IN ) :: jsegment,jmax_segments
integer (kind=int_kind) , intent(in) :: nreconstruction
real (kind=real_kind) , dimension(jmax_segments,nreconstruction), intent(inout) :: weights
integer (kind=int_kind), dimension(jmax_segments,2 ), intent(inout) :: weights_eul_index
integer (kind=int_kind), INTENT(OUT ) :: jcollect
!
! local workspace
!
integer (kind=int_kind) :: imin, imax, jmin, jmax, i,j,k,h
logical :: ltmp
real (kind=real_kind) , dimension(jmax_segments,nreconstruction) :: weights_out
integer (kind=int_kind), dimension(jmax_segments,2 ) :: weights_eul_index_out
weights_out = 0.0D0
weights_eul_index_out = -100
imin = MINVAL(weights_eul_index(1:jsegment,1))
imax = MAXVAL(weights_eul_index(1:jsegment,1))
jmin = MINVAL(weights_eul_index(1:jsegment,2))
jmax = MAXVAL(weights_eul_index(1:jsegment,2))
ltmp = .FALSE.
jcollect = 1
do j=jmin,jmax
do i=imin,imax
do k=1,jsegment
if (weights_eul_index(k,1)==i.AND.weights_eul_index(k,2)==j) then
weights_out(jcollect,1:nreconstruction) = &
weights_out(jcollect,1:nreconstruction) + weights(k,1:nreconstruction)
ltmp = .TRUE.
h = k
endif
enddo
if (ltmp) then
weights_eul_index_out(jcollect,:) = weights_eul_index(h,:)
jcollect = jcollect+1
endif
ltmp = .FALSE.
enddo
enddo
jcollect = jcollect-1
weights = weights_out
weights_eul_index = weights_eul_index_out
end subroutine collect
!
!*****************************************************************************************
!
!
!
!*****************************************************************************************
!
subroutine compute_inner_line_integrals_lat(r_cross_lat,cross_lat_eul_index,&
jcross_lat,jsegment,jmax_segments,xgno,jx_min,jx_max,jy_min, jy_max,weights,weights_eul_index,&
nreconstruction,ngauss,gauss_weights,abscissae)!phl add jx_min etc.
implicit none
!
! variables for registering crossings with Eulerian latitudes and longitudes
!
integer (kind=int_kind), intent(in):: jcross_lat, jmax_segments,nreconstruction,ngauss
integer (kind=int_kind), intent(inout):: jsegment
!
! for Gaussian quadrature
!
real (kind=real_kind), dimension(ngauss), intent(in) :: gauss_weights, abscissae
!
! max. crossings per side is 2*nhe
!
real (kind=real_kind), &
dimension(jmax_segments,2), intent(in):: r_cross_lat
integer (kind=int_kind), &
dimension(jmax_segments,2), intent(in):: cross_lat_eul_index
integer (kind=int_kind), intent(in) ::jx_min, jx_max, jy_min, jy_max
real (kind=real_kind), dimension(-nhe:nc+2+nhe), intent(in) :: xgno
real (kind=real_kind) , &
dimension(jmax_segments,nreconstruction), intent(inout) :: weights
integer (kind=int_kind), &
dimension(jmax_segments,2), intent(inout) :: weights_eul_index
real (kind=real_kind) , dimension(nreconstruction) :: weights_tmp
integer (kind=int_kind) :: imin, imax, i,j,k, h
real (kind=real_kind), dimension(2) :: rstart,rend,rend_tmp
real (kind=real_kind), dimension(2) :: xseg, yseg
if (jcross_lat>0) then
do i=MINVAL(cross_lat_eul_index(1:jcross_lat,2)),MAXVAL(cross_lat_eul_index(1:jcross_lat,2))
!
! find "first" crossing with Eulerian cell i
!
do k=1,jcross_lat
if (cross_lat_eul_index(k,2)==i) exit
enddo
do j=k+1,jcross_lat
!
! find "second" crossing with Eulerian cell i
!
if (cross_lat_eul_index(j,2)==i) then
if (r_cross_lat(k,1)<r_cross_lat(j,1)) then
rstart = r_cross_lat(k,1:2)
rend = r_cross_lat(j,1:2)
imin = cross_lat_eul_index(k,1)
imax = cross_lat_eul_index(j,1)
else
rstart = r_cross_lat(j,1:2)
rend = r_cross_lat(k,1:2)
imin = cross_lat_eul_index(j,1)
imax = cross_lat_eul_index(k,1)
endif
do h=imin,imax
if (h==imax) then
rend_tmp = rend
else
rend_tmp(1) = xgno(h+1)
rend_tmp(2) = r_cross_lat(k,2)
endif
xseg(1) = rstart(1)
xseg(2) = rend_tmp(1)
yseg(1) = rstart(2)
yseg(2) = rend_tmp(2)
! call get_weights_exact(weights_tmp,xseg,yseg,nreconstruction)
call get_weights_gauss(weights_tmp,&
xseg,yseg,nreconstruction,ngauss,gauss_weights,abscissae)
if (i.LE.jy_max-1.AND.i.GE.jy_min.AND.h.LE.jx_max-1.AND.h.GE.jx_min) then
jsegment=jsegment+1
if (jsegment>jmax_segments) then
!
! array bound chekc for "user-friendlyness"
!
write(*,*) "Allocated arrays for number of segments in overlap between"
write(*,*) "target grid cell and intermediate cubed-sphere grid are too small!"
write(*,*) "Increase jmax_segments"
write(*,*) "Current value: jmax_segments=",jmax_segments
write(*,*) "ABORTING"
stop
end if
weights_eul_index(jsegment,1) = h
weights_eul_index(jsegment,2) = i
weights(jsegment,1:nreconstruction) = -weights_tmp
endif
!
! subtract the same weights on the "south" side of the line
!
if (i.LE.jy_max.AND.i.GE.jy_min+1.AND.h.LE.jx_max-1.AND.h.GE.jx_min) then
!phl if (i.GE.2.AND.i.LE.nc+1.AND.h.LE.nc.AND.h.GE.1) then
jsegment = jsegment+1
if (jsegment>jmax_segments) then
!
! array bound chekc for "user-friendlyness"
!
write(*,*) "Allocated arrays for number of segments in overlap between"
write(*,*) "target grid cell and intermediate cubed-sphere grid are too small!"
write(*,*) "Increase jmax_segments"
write(*,*) "Current value: jmax_segments=",jmax_segments
write(*,*) "ABORTING"
stop
end if
weights_eul_index(jsegment,1) = h
weights_eul_index(jsegment,2) = i-1
weights(jsegment,1:nreconstruction) = weights_tmp
endif
!
! prepare for next iteration
!
! if (abs(rend_tmp(1)-rend(1))<tiny) then
! EXIT !are we done already?
! else
rstart = rend_tmp
! endif
enddo
endif
enddo
enddo
endif
end subroutine compute_inner_line_integrals_lat
subroutine compute_inner_line_integrals_lat_nonconvex(r_cross_lat,cross_lat_eul_index,&
jcross_lat,jsegment,jmax_segments,xgno,jx_min,jx_max,jy_min, jy_max,weights,weights_eul_index,&
nreconstruction,ngauss,gauss_weights,abscissae)!phl add jx_min etc.
implicit none
!
! variables for registering crossings with Eulerian latitudes and longitudes
!
integer (kind=int_kind), intent(in):: jcross_lat, jmax_segments,nreconstruction,ngauss
integer (kind=int_kind), intent(inout):: jsegment
!
! for Gaussian quadrature
!
real (kind=real_kind), dimension(ngauss), intent(in) :: gauss_weights, abscissae
!
! max. crossings per side is 2*nhe
!
real (kind=real_kind), &
dimension(jmax_segments,2), intent(in):: r_cross_lat
integer (kind=int_kind), &
dimension(jmax_segments,2), intent(in):: cross_lat_eul_index
integer (kind=int_kind), intent(in) ::jx_min, jx_max, jy_min, jy_max
real (kind=real_kind), dimension(-nhe:nc+2+nhe), intent(in) :: xgno
real (kind=real_kind) , &
dimension(jmax_segments,nreconstruction), intent(inout) :: weights
integer (kind=int_kind), &
dimension(jmax_segments,2), intent(inout) :: weights_eul_index
integer (kind=int_kind) :: i,k, h
real (kind=real_kind), dimension(jmax_segments,2) :: r_cross_lat_seg
integer (kind=int_kind), dimension(jmax_segments,2):: cross_lat_eul_index_seg
real (kind=real_kind), dimension(jmax_segments,2) :: r_cross_lat_seg2
integer (kind=int_kind), dimension(jmax_segments,2):: cross_lat_eul_index_seg2
integer (kind=int_kind) :: count,js,is
real (kind=real_kind) :: a,a2!,b,b2
integer :: b,b2
if (jcross_lat>0) then
do i=MINVAL(cross_lat_eul_index(1:jcross_lat,2)),MAXVAL(cross_lat_eul_index(1:jcross_lat,2))
count = 1
!
! find all crossings with Eulerian latitude i
!
do k=1,jcross_lat
if (cross_lat_eul_index(k,2)==i) then
r_cross_lat_seg (count,:) = r_cross_lat (k,:)
cross_lat_eul_index_seg(count,:) = cross_lat_eul_index(k,:)
count = count+1
end if
enddo
count = count-1
IF (ABS((count/2)-DBLE(count)/2.0)<tiny) then
IF (count.NE.2) THEN
WRITE(*,*) "non-convex cell", count
!
! sort array from min to max
!
do js=2, count
a =r_cross_lat_seg(js,1)
a2=r_cross_lat_seg(js,2)
b =cross_lat_eul_index_seg(js,1)
b2=cross_lat_eul_index_seg(js,2)
do is=js-1,1,-1
if (r_cross_lat_seg(is,1)<=a) goto 10
r_cross_lat_seg(is+1,:)=r_cross_lat_seg(is,:)
cross_lat_eul_index_seg(is+1,:) = cross_lat_eul_index_seg(is,:)
end do
is=0
10 r_cross_lat_seg(is+1,1)=a
r_cross_lat_seg(is+1,2)=a2
cross_lat_eul_index_seg(is+1,1) = b
cross_lat_eul_index_seg(is+1,2) = b2
end do
r_cross_lat_seg2 (1:count,:) = r_cross_lat_seg (1:count,:)
cross_lat_eul_index_seg2(1:count,:) = cross_lat_eul_index_seg(1:count,:)
end if
else
WRITE(*,*) "INCONSISTENCY in number of crossings!", count
STOP
END IF
!
! only do every other segment
!
do h=1,count-1,2
r_cross_lat_seg2 (1:2,:) = r_cross_lat_seg (h:h+1,:)
cross_lat_eul_index_seg2(1:2,:) = cross_lat_eul_index_seg(h:h+1,:)
call compute_inner_line_integrals_lat(r_cross_lat_seg2,cross_lat_eul_index_seg2,&
2,jsegment,jmax_segments,xgno,jx_min,jx_max,jy_min, jy_max,weights,weights_eul_index,&
nreconstruction,ngauss,gauss_weights,abscissae)!phl add jx_min etc.
end do
enddo
endif
end subroutine compute_inner_line_integrals_lat_nonconvex
!
! line integral from (a1_in,a2_in) to (b1_in,b2_in)
! If line is coniciding with an Eulerian longitude or latitude the routine
! needs to know where an adjacent side is located to determine which
! reconstruction must be used. therefore (c1,c2) is passed to the routine
!
!
subroutine side_integral(&
x_in,y_in,nvertex,jsegment,jmax_segments,&
weights,weights_eul_index,nreconstruction,jx,jy,xgno,ygno,jx_min,jx_max,jy_min,jy_max,&
ngauss,gauss_weights,abscissae,&!)!phl add jx_min etc.
jcross_lat,r_cross_lat,cross_lat_eul_index)
implicit none
!
! for Gaussian quadrature
!
integer (kind=int_kind), intent(in) :: nvertex
integer (kind=int_kind), intent(in) :: nreconstruction,jx,jy,jmax_segments,ngauss
real (kind=real_kind), dimension(ngauss), intent(in) :: gauss_weights, abscissae
real (kind=real_kind), dimension(1:nvertex) , intent(in) :: x_in,y_in
integer (kind=int_kind), intent(in) :: jx_min, jy_min, jx_max, jy_max
real (kind=real_kind), dimension(-nhe:nc+2+nhe), intent(in) :: xgno
real (kind=real_kind), dimension(-nhe:nc+2+nhe), intent(in) :: ygno
integer (kind=int_kind), intent(inout) :: jsegment
real (kind=real_kind) , &
dimension(jmax_segments,nreconstruction), intent(out) :: weights
integer (kind=int_kind), &
dimension(jmax_segments,2), intent(out) :: weights_eul_index
!
! variables for registering crossings with Eulerian latitudes and longitudes
!
integer (kind=int_kind), intent(inout):: jcross_lat
!
! max. crossings per side is 2*nhe
!
real (kind=real_kind), &
dimension(jmax_segments,2), intent(inout):: r_cross_lat
integer (kind=int_kind), &
dimension(jmax_segments,2), intent(inout):: cross_lat_eul_index
!
! local variables
!
! real (kind=real_kind) :: dist_lon,dist_lat, tmp_a1, tmp_a2, tmp_x(1), tmp_b2
real (kind=real_kind), dimension(2) :: xseg,yseg
real (kind=real_kind), dimension(0:3) :: x,y
real (kind=real_kind) :: xeul,yeul,xcross,ycross,slope
integer (kind=int_kind) :: jx_eul_tmp,jy_eul_tmp
integer (kind=int_kind) :: xsgn1,ysgn1,xsgn2,ysgn2
integer (kind=int_kind) :: iter
logical :: lcontinue, lsame_cell_x, lsame_cell_y
integer (kind=int_kind) :: jx_eul, jy_eul, side_count,jdbg
real (kind=real_kind), dimension(0:nvertex+2) :: xcell,ycell
! if (ldbg_global) then
! OPEN(unit=40, file='side_integral.dat',status='replace')
! WRITE(40,*) " "
! CLOSE(40)
! end if
!
!***********************************************
!
! find jx_eul and jy_eul for (x(1),y(1))
!
!***********************************************
!
jx_eul = jx; jy_eul = jy
xcell(1:nvertex)=x_in; ycell(1:nvertex)=y_in
DO iter=1,nvertex
CALL truncate_vertex(xcell(iter),jx_eul,xgno)
CALL truncate_vertex(ycell(iter),jy_eul,ygno)
END DO
xcell(0) = xcell(nvertex); xcell(nvertex+1)=xcell(1); xcell(nvertex+2)=xcell(2);
ycell(0) = ycell(nvertex); ycell(nvertex+1)=ycell(1); ycell(nvertex+2)=ycell(2);
IF (MAXVAL(xcell).LE.xgno(jx_min).OR.MINVAL(xcell).GE.xgno(jx_max).OR.&
MAXVAL(ycell).LE.ygno(jy_min).OR.MINVAL(ycell).GE.ygno(jy_max)) THEN
ELSE
jx_eul = jx
jy_eul = jy
CALL which_eul_cell(xcell(1:3),jx_eul,xgno)
CALL which_eul_cell(ycell(1:3),jy_eul,ygno)
side_count = 1
DO WHILE (side_count<nvertex+1)
jdbg = 0
iter = 0
lcontinue = .TRUE.
x(0:3) = xcell(side_count-1:side_count+2); y(0:3) = ycell(side_count-1:side_count+2);
DO while (lcontinue)
iter = iter+1
IF (iter>1000) THEN
WRITE(*,*) "search not converging",iter
WRITE(*,*) " "
WRITE(*,*) "This could be due to errors in the grid descriptor file"
WRITE(*,*) "A possible problem could be for grids where the cells have"
WRITE(*,*) "variable number of vertices. For cells that have less than max"
WRITE(*,*) "number of vertices, the auxiliary vertices should have the"
WRITE(*,*) "value of the last vertex (all specified counter-clockwise)"
WRITE(*,*) " "
WRITE(*,*) "ABORTING",iter
STOP
END IF
lsame_cell_x = (x(2).GE.xgno(jx_eul).AND.x(2).LE.xgno(jx_eul+1))
lsame_cell_y = (y(2).GE.ygno(jy_eul).AND.y(2).LE.ygno(jy_eul+1))
IF (lsame_cell_x.AND.lsame_cell_y) THEN
!
!****************************
!
! same cell integral
!
!****************************
!
! IF (ldbgr) WRITE(*,*) "same cell integral",jx_eul,jy_eul
xseg(1) = x(1); yseg(1) = y(1); xseg(2) = x(2); yseg(2) = y(2)
jx_eul_tmp = jx_eul; jy_eul_tmp = jy_eul;
lcontinue = .FALSE.
!
! prepare for next side if (x(2),y(2)) is on a grid line
!
IF (x(2).EQ.xgno(jx_eul+1).AND.x(3)>xgno(jx_eul+1)) THEN
!
! cross longitude jx_eul+1
!
! IF (ldbgr) WRITE(*,*) "cross longitude",jx_eul+1
jx_eul=jx_eul+1
ELSE IF (x(2).EQ.xgno(jx_eul ).AND.x(3)<xgno(jx_eul)) THEN
!
! cross longitude jx_eul
!
! IF (ldbgr) WRITE(*,*) "cross longitude",jx_eul
jx_eul=jx_eul-1
END IF
IF (y(2).EQ.ygno(jy_eul+1).AND.y(3)>ygno(jy_eul+1)) THEN
!
! register crossing with latitude: line-segments point Northward
!
jcross_lat = jcross_lat + 1
jy_eul = jy_eul + 1
! IF (ldbgr) WRITE(*,*) "cross latitude",jy_eul
! IF (ldbgr) WRITE(*,*) "jcross_lat",jcross_lat
cross_lat_eul_index(jcross_lat,1) = jx_eul
cross_lat_eul_index(jcross_lat,2) = jy_eul
r_cross_lat(jcross_lat,1) = x(2)
r_cross_lat(jcross_lat,2) = y(2)
ELSE IF (y(2).EQ.ygno(jy_eul ).AND.y(3)<ygno(jy_eul)) THEN
!
! register crossing with latitude: line-segments point Southward
!
! IF (ldbgr) WRITE(*,*) "cross latitude",jy_eul
! IF (ldbgr) WRITE(*,*) "jcross_lat",jcross_lat
jcross_lat = jcross_lat+1
cross_lat_eul_index(jcross_lat,1) = jx_eul
cross_lat_eul_index(jcross_lat,2) = jy_eul
r_cross_lat(jcross_lat,1) = x(2)
r_cross_lat(jcross_lat,2) = y(2)
jy_eul=jy_eul-1
END IF
lcontinue=.FALSE.
ELSE
!
!****************************
!
! not same cell integral
!
!****************************
!
IF (lsame_cell_x) THEN
! IF (ldbgr) WRITE(*,*) "same cell x"
ysgn1 = (1+INT(SIGN(1.0D0,y(2)-y(1))))/2 !"1" if y(2)>y(1) else "0"
ysgn2 = INT(SIGN(1.0D0,y(2)-y(1))) !"1" if y(2)>y(1) else "-1"
!
!*******************************************************************************
!
! there is at least one crossing with latitudes but no crossing with longitudes
!
!*******************************************************************************
!
yeul = ygno(jy_eul+ysgn1)
IF (x(1).EQ.x(2)) THEN
!
! line segment is parallel to longitude (infinite slope)
!
! IF (ldbgr) WRITE(*,*) "line segment parallel to longitude"
xcross = x(1)
ELSE
slope = (y(2)-y(1))/(x(2)-x(1))
xcross = x_cross_eul_lat(x(1),y(1),yeul,slope)
!
! constrain crossing to be "physically" possible
!
xcross = MIN(MAX(xcross,xgno(jx_eul)),xgno(jx_eul+1))
! IF (ldbgr) WRITE(*,*) "cross latitude"
!
! debugging
!
IF (xcross.GT.xgno(jx_eul+1).OR.xcross.LT.xgno(jx_eul)) THEN
WRITE(*,*) "xcross is out of range",jx,jy
WRITE(*,*) "xcross-xgno(jx_eul+1), xcross-xgno(jx_eul))",&
xcross-xgno(jx_eul+1), xcross-ygno(jx_eul)
STOP
END IF
END IF
xseg(1) = x(1); yseg(1) = y(1); xseg(2) = xcross; yseg(2) = yeul
jx_eul_tmp = jx_eul; jy_eul_tmp = jy_eul;
!
! prepare for next iteration
!
x(0) = x(1); y(0) = y(1); x(1) = xcross; y(1) = yeul; jy_eul = jy_eul+ysgn2
!
! register crossing with latitude
!
jcross_lat = jcross_lat+1
cross_lat_eul_index(jcross_lat,1) = jx_eul
if (ysgn2>0) then
cross_lat_eul_index(jcross_lat,2) = jy_eul
! IF (ldbgr) WRITE(*,*) "cross latitude",jy_eul
! IF (ldbgr) WRITE(*,*) "jcross_lat",jcross_lat
else
cross_lat_eul_index(jcross_lat,2) = jy_eul+1
! IF (ldbgr) WRITE(*,*) "cross latitude",jy_eul+1
! IF (ldbgr) WRITE(*,*) "jcross_lat",jcross_lat
end if
r_cross_lat(jcross_lat,1) = xcross
r_cross_lat(jcross_lat,2) = yeul
ELSE IF (lsame_cell_y) THEN
! IF (ldbgr) WRITE(*,*) "same cell y"
!
!*******************************************************************************
!
! there is at least one crossing with longitudes but no crossing with latitudes
!
!*******************************************************************************
!
xsgn1 = (1+INT(SIGN(1.0D0,x(2)-x(1))))/2 !"1" if x(2)>x(1) else "0"
xsgn2 = INT(SIGN(1.0D0,x(2)-x(1))) !"1" if x(2)>x(1) else "-1"
xeul = xgno(jx_eul+xsgn1)
! IF (ldbgr) WRITE(*,*) " crossing longitude",jx_eul+xsgn1
IF (ABS(x(2)-x(1))<fuzzy_width) THEN
ycross = 0.5*(y(2)-y(1))
! IF (ldbgr) WRITE(*,*) "fuzzy crossing"
ELSE
slope = (y(2)-y(1))/(x(2)-x(1))
ycross = y_cross_eul_lon(x(1),y(1),xeul,slope)
END IF
!
! constrain crossing to be "physically" possible
!
ycross = MIN(MAX(ycross,ygno(jy_eul)),ygno(jy_eul+1))
!
! debugging
!
IF (ycross.GT.ygno(jy_eul+1).OR.ycross.LT.ygno(jy_eul)) THEN
WRITE(*,*) "ycross is out of range"
WRITE(*,*) "jx,jy,jx_eul,jy_eul",jx,jy,jx_eul,jy_eul
WRITE(*,*) "ycross-ygno(jy_eul+1), ycross-ygno(jy_eul))",&
ycross-ygno(jy_eul+1), ycross-ygno(jy_eul)
STOP
END IF
xseg(1) = x(1); yseg(1) = y(1); xseg(2) = xeul; yseg(2) = ycross
jx_eul_tmp = jx_eul; jy_eul_tmp = jy_eul;
!
! prepare for next iteration
!
x(0) = x(1); y(0) = y(1); x(1) = xeul; y(1) = ycross; jx_eul = jx_eul+xsgn2
ELSE
! IF (ldbgr) WRITE(*,*) "not same cell x; not same cell y"
!
!*******************************************************************************
!
! there are crossings with longitude(s) and latitude(s)
!
!*******************************************************************************
!
xsgn1 = (1+INT(SIGN(1.0D0,x(2)-x(1))))/2 !"1" if x(2)>x(1) else "0"
xsgn2 = (INT(SIGN(1.0D0,x(2)-x(1)))) !"1" if x(2)>x(1) else "0"
xeul = xgno(jx_eul+xsgn1)
ysgn1 = (1+INT(SIGN(1.0D0,y(2)-y(1))))/2 !"1" if y(2)>y(1) else "0"
ysgn2 = INT(SIGN(1.0D0,y(2)-y(1))) !"1" if y(2)>y(1) else "-1"
yeul = ygno(jy_eul+ysgn1)
slope = (y(2)-y(1))/(x(2)-x(1))
IF (ABS(x(2)-x(1))<fuzzy_width) THEN
ycross = 0.5*(y(2)-y(1))
ELSE
ycross = y_cross_eul_lon(x(1),y(1),xeul,slope)
END IF
xcross = x_cross_eul_lat(x(1),y(1),yeul,slope)
IF ((xsgn2>0.AND.xcross.LE.xeul).OR.(xsgn2<0.AND.xcross.GE.xeul)) THEN
!
! cross latitude
!
! IF (ldbgr) WRITE(*,*) "crossing latitude",jy_eul+ysgn1
xseg(1) = x(1); yseg(1) = y(1); xseg(2) = xcross; yseg(2) = yeul
jx_eul_tmp = jx_eul; jy_eul_tmp = jy_eul;
!
! prepare for next iteration
!
x(0) = x(1); y(0) = y(1); x(1) = xcross; y(1) = yeul; jy_eul = jy_eul+ysgn2
!
! register crossing with latitude
!
jcross_lat = jcross_lat+1
cross_lat_eul_index(jcross_lat,1) = jx_eul
if (ysgn2>0) then
cross_lat_eul_index(jcross_lat,2) = jy_eul
! IF (ldbgr) WRITE(*,*) "cross latitude",jy_eul
else
cross_lat_eul_index(jcross_lat,2) = jy_eul+1
! IF (ldbgr) WRITE(*,*) "cross latitude",jy_eul+1
end if
r_cross_lat(jcross_lat,1) = xcross
r_cross_lat(jcross_lat,2) = yeul
ELSE
!
! cross longitude
!
! IF (ldbgr) WRITE(*,*) "crossing longitude",jx_eul+xsgn1
xseg(1) = x(1); yseg(1) = y(1); xseg(2) = xeul; yseg(2) = ycross
jx_eul_tmp = jx_eul; jy_eul_tmp = jy_eul;
!
! prepare for next iteration
!
x(0) = x(1); y(0) = y(1); x(1) = xeul; y(1) = ycross; jx_eul = jx_eul+xsgn2
END IF
END IF
END IF
!
! register line-segment (don't register line-segment if outside of panel)
!
if (jx_eul_tmp>=jx_min.AND.jy_eul_tmp>=jy_min.AND.&
jx_eul_tmp<=jx_max-1.AND.jy_eul_tmp<=jy_max-1) then
! jx_eul_tmp<=jx_max-1.AND.jy_eul_tmp<=jy_max-1.AND.side_count<3) then
jsegment=jsegment+1
weights_eul_index(jsegment,1) = jx_eul_tmp
weights_eul_index(jsegment,2) = jy_eul_tmp
call get_weights_gauss(weights(jsegment,1:nreconstruction),&
xseg,yseg,nreconstruction,ngauss,gauss_weights,abscissae)
! if (ldbg_global) then
! OPEN(unit=40, file='side_integral.dat',status='old',POSITION='APPEND')
! WRITE(40,*) xseg(1),yseg(1)
! WRITE(40,*) xseg(2),yseg(2)
! WRITE(40,*) " "
! CLOSE(40)
! end if
jdbg=jdbg+1
if (xseg(1).EQ.xseg(2))then
slope = bignum
else if (abs(yseg(1) -yseg(2))<fuzzy_width) then
slope = 0.0
else
slope = (yseg(2)-yseg(1))/(xseg(2)-xseg(1))
end if
ELSE
! IF (ldbgr) WRITE(*,*) "segment outside of panel"
END IF
END DO
side_count = side_count+1
END DO
END IF
end subroutine side_integral
real (kind=real_kind) function compute_slope(x,y)
implicit none
real (kind=real_kind), dimension(2), intent(in) :: x,y
if (fuzzy(ABS(x(2)-x(1)),fuzzy_width)>0) THEN
compute_slope = (y(2)-y(1))/(x(2)-x(1))
else
compute_slope = bignum
end if
end function compute_slope
real (kind=real_kind) function y_cross_eul_lon(x,y,xeul,slope)
implicit none
real (kind=real_kind), intent(in) :: x,y
real (kind=real_kind) , intent(in) :: xeul,slope
! line: y=a*x+b
real (kind=real_kind) :: b
b = y-slope*x
y_cross_eul_lon = slope*xeul+b
end function y_cross_eul_lon
real (kind=real_kind) function x_cross_eul_lat(x,y,yeul,slope)
implicit none
real (kind=real_kind), intent(in) :: x,y