forked from ccht-ncsu/FigureGen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FigureGen.F90
11268 lines (9459 loc) · 504 KB
/
FigureGen.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
!----------------------------------------------------------------------!
!
! FIGUREGEN
! By: Casey Dietrich
!
! FigureGen is a Fortran program that creates images for ADCIRC files.
! It reads mesh files (fort.14, etc.), nodal attributes files
! (fort.13, etc.) and output files (fort.63, fort.64, maxele.63, etc.).
! It plots contours, contour lines, and vectors. Using FigureGen, you
! can go directly from the ADCIRC input and output files to a
! presentation-quality figure, for one or multiple time snaps.
!
! This program started from a script written by Brian Blanton,
! and I converted it to Fortran because I am more familiar with that
! language. It now contains code written by John Atkinson, Zach Cobell,
! Howard Lander, Chris Szpilka, Matthieu Vitse, Matthew Bilskie, and others.
! But, at its core, FigureGen behaves like a script, and it uses system
! calls to tell other software how to generate the figure(s).
!
!----------------------------------------------------------------------!
!----------------------------------------------------------------------!
!...Use the configuration file to compile the code
! instead of using many options on the command line.
! compile with the option -DHAVE_CONFIG to enable this
! feature. Make sure that the file "FigureGen_Options.h"
! is either in the current directory, or its location is
! spcified using the -I/path/to/directory syntax.
#ifdef HAVE_CONFIG
#include 'FigureGen_Options.h'
#endif
!----------------------------------------------------------------------!
!----------------------------------------------------------------------!
! KDTREE2 is (c) Matthew Kennel, Institute for Nonlinear Science (2004)
! Licensed under the Academic Free License version 1.1
!----------------------------------------------------------------------!
MODULE KDTREE2_PRECISION_MODULE
INTEGER, PARAMETER :: SP = KIND(0.0)
INTEGER, PARAMETER :: DP = KIND(0.0D0)
PRIVATE :: SP, DP
!INTEGER, PARAMETER :: KDKIND = SP
INTEGER, PARAMETER :: KDKIND = DP
PUBLIC :: KDKIND
END MODULE KDTREE2_PRECISION_MODULE
MODULE KDTREE2_PRIORITY_QUEUE_MODULE
USE KDTREE2_PRECISION_MODULE
TYPE KDTREE2_RESULT
REAL(KDKIND) :: DIS!=0.0
INTEGER :: IDX!=-1 INITIALIZERS CAUSE SOME BUGS IN COMPILERS.
END TYPE KDTREE2_RESULT
TYPE PQ
INTEGER :: HEAP_SIZE = 0
TYPE(KDTREE2_RESULT), POINTER :: ELEMS(:)
END TYPE PQ
PUBLIC :: KDTREE2_RESULT
PUBLIC :: PQ
PUBLIC :: PQ_CREATE
PUBLIC :: PQ_DELETE, PQ_INSERT
PUBLIC :: PQ_EXTRACT_MAX, PQ_MAX, PQ_REPLACE_MAX, PQ_MAXPRI
PRIVATE
CONTAINS
FUNCTION PQ_CREATE(RESULTS_IN) RESULT(RES)
TYPE(KDTREE2_RESULT), TARGET:: RESULTS_IN(:)
TYPE(PQ) :: RES
INTEGER :: NALLOC
NALLOC = SIZE(RESULTS_IN,1)
IF (NALLOC .LT. 1) THEN
WRITE (*,*) 'PQ_CREATE: ERROR, INPUT ARRAYS MUST BE '// &
'ALLOCATED.'
END IF
RES%ELEMS => RESULTS_IN
RES%HEAP_SIZE = 0
RETURN
END FUNCTION PQ_CREATE
SUBROUTINE HEAPIFY(A,I_IN)
TYPE(PQ),POINTER :: A
INTEGER, INTENT(IN) :: I_IN
!
INTEGER :: I, L, R, LARGEST
REAL(KDKIND) :: PRI_I, PRI_L, PRI_R, PRI_LARGEST
TYPE(KDTREE2_RESULT) :: TEMP
I = I_IN
BIGLOOP: DO
L = 2*I ! LEFT(I)
R = L+1 ! RIGHT(I)
IF (L .GT. A%HEAP_SIZE) THEN
EXIT
ELSE
PRI_I = A%ELEMS(I)%DIS
PRI_L = A%ELEMS(L)%DIS
IF (PRI_L .GT. PRI_I) THEN
LARGEST = L
PRI_LARGEST = PRI_L
ELSE
LARGEST = I
PRI_LARGEST = PRI_I
ENDIF
IF (R .LE. A%HEAP_SIZE) THEN
PRI_R = A%ELEMS(R)%DIS
IF (PRI_R .GT. PRI_LARGEST) THEN
LARGEST = R
ENDIF
ENDIF
ENDIF
IF (LARGEST .NE. I) THEN
TEMP = A%ELEMS(I)
A%ELEMS(I) = A%ELEMS(LARGEST)
A%ELEMS(LARGEST) = TEMP
I = LARGEST
CYCLE BIGLOOP
ELSE
RETURN
END IF
ENDDO BIGLOOP
RETURN
END SUBROUTINE HEAPIFY
SUBROUTINE PQ_MAX(A,E)
TYPE(PQ),POINTER :: A
TYPE(KDTREE2_RESULT),INTENT(OUT) :: E
IF (A%HEAP_SIZE .GT. 0) THEN
E = A%ELEMS(1)
ELSE
WRITE (*,*) 'PQ_MAX: ERROR, HEAP_SIZE < 1'
STOP
ENDIF
RETURN
END SUBROUTINE PQ_MAX
REAL(KDKIND) FUNCTION PQ_MAXPRI(A)
TYPE(PQ), POINTER :: A
IF (A%HEAP_SIZE .GT. 0) THEN
PQ_MAXPRI = A%ELEMS(1)%DIS
ELSE
WRITE (*,*) 'PQ_MAX_PRI: ERROR, HEAPSIZE < 1'
STOP
ENDIF
RETURN
END FUNCTION PQ_MAXPRI
SUBROUTINE PQ_EXTRACT_MAX(A,E)
TYPE(PQ),POINTER :: A
TYPE(KDTREE2_RESULT), INTENT(OUT) :: E
IF (A%HEAP_SIZE .GE. 1) THEN
E = A%ELEMS(1)
A%ELEMS(1) = A%ELEMS(A%HEAP_SIZE)
A%HEAP_SIZE = A%HEAP_SIZE-1
CALL HEAPIFY(A,1)
RETURN
ELSE
WRITE (*,*) 'PQ_EXTRACT_MAX: ERROR,',&
' ATTEMPTED TO POP NON-POSITIVE PQ'
STOP
END IF
END SUBROUTINE PQ_EXTRACT_MAX
REAL(KDKIND) FUNCTION PQ_INSERT(A,DIS,IDX)
TYPE(PQ),POINTER :: A
REAL(KDKIND), INTENT(IN) :: DIS
INTEGER, INTENT(IN) :: IDX
INTEGER :: I, ISPARENT
REAL(KDKIND) :: PARENTDIS
A%HEAP_SIZE = A%HEAP_SIZE + 1
I = A%HEAP_SIZE
DO WHILE (I .GT. 1)
ISPARENT = INT(I/2)
PARENTDIS = A%ELEMS(ISPARENT)%DIS
IF (DIS .GT. PARENTDIS) THEN
A%ELEMS(I)%DIS = PARENTDIS
A%ELEMS(I)%IDX = A%ELEMS(ISPARENT)%IDX
I = ISPARENT
ELSE
EXIT
ENDIF
END DO
A%ELEMS(I)%DIS = DIS
A%ELEMS(I)%IDX = IDX
PQ_INSERT = A%ELEMS(1)%DIS
RETURN
END FUNCTION PQ_INSERT
SUBROUTINE PQ_ADJUST_HEAP(A,I)
TYPE(PQ),POINTER :: A
INTEGER, INTENT(IN) :: I
REAL(KDKIND) :: PRICHILD
INTEGER :: PARENT, CHILD, N
TYPE(KDTREE2_RESULT) :: E
E = A%ELEMS(I)
PARENT = I
CHILD = 2*I
N = A%HEAP_SIZE
DO WHILE (CHILD .LE. N)
IF (CHILD .LT. N) THEN
IF (A%ELEMS(CHILD)%DIS .LT. A%ELEMS(CHILD+1)%DIS) THEN
CHILD = CHILD+1
ENDIF
ENDIF
PRICHILD = A%ELEMS(CHILD)%DIS
IF (E%DIS .GE. PRICHILD) THEN
EXIT
ELSE
A%ELEMS(PARENT) = A%ELEMS(CHILD)
PARENT = CHILD
CHILD = 2*PARENT
END IF
END DO
A%ELEMS(PARENT) = E
RETURN
END SUBROUTINE PQ_ADJUST_HEAP
REAL(KDKIND) FUNCTION PQ_REPLACE_MAX(A,DIS,IDX)
TYPE(PQ),POINTER :: A
REAL(KDKIND), INTENT(IN) :: DIS
INTEGER, INTENT(IN) :: IDX
INTEGER :: PARENT, CHILD, N
REAL(KDKIND) :: PRICHILD, PRICHILDP1
TYPE(KDTREE2_RESULT) :: ETMP
IF (.TRUE.) THEN
N=A%HEAP_SIZE
IF (N .GE. 1) THEN
PARENT =1
CHILD=2
LOOP: DO WHILE (CHILD .LE. N)
PRICHILD = A%ELEMS(CHILD)%DIS
IF (CHILD .LT. N) THEN
PRICHILDP1 = A%ELEMS(CHILD+1)%DIS
IF (PRICHILD .LT. PRICHILDP1) THEN
CHILD = CHILD+1
PRICHILD = PRICHILDP1
ENDIF
ENDIF
IF (DIS .GE. PRICHILD) THEN
EXIT LOOP
ELSE
A%ELEMS(PARENT) = A%ELEMS(CHILD)
PARENT = CHILD
CHILD = 2*PARENT
END IF
END DO LOOP
A%ELEMS(PARENT)%DIS = DIS
A%ELEMS(PARENT)%IDX = IDX
PQ_REPLACE_MAX = A%ELEMS(1)%DIS
ELSE
A%ELEMS(1)%DIS = DIS
A%ELEMS(1)%IDX = IDX
PQ_REPLACE_MAX = DIS
ENDIF
ELSE
CALL PQ_EXTRACT_MAX(A,ETMP)
ETMP%DIS = DIS
ETMP%IDX = IDX
PQ_REPLACE_MAX = PQ_INSERT(A,DIS,IDX)
ENDIF
RETURN
END FUNCTION PQ_REPLACE_MAX
SUBROUTINE PQ_DELETE(A,I)
TYPE(PQ),POINTER :: A
INTEGER :: I
IF ((I .LT. 1) .OR. (I .GT. A%HEAP_SIZE)) THEN
WRITE (*,*) 'PQ_DELETE: ERROR, ATTEMPT TO REMOVE',&
' OUT OF BOUNDS ELEMENT.'
STOP
ENDIF
A%ELEMS(I) = A%ELEMS(A%HEAP_SIZE)
A%HEAP_SIZE = A%HEAP_SIZE - 1
CALL HEAPIFY(A,I)
END SUBROUTINE PQ_DELETE
END MODULE KDTREE2_PRIORITY_QUEUE_MODULE
MODULE KDTREE2_MODULE
USE KDTREE2_PRECISION_MODULE
USE KDTREE2_PRIORITY_QUEUE_MODULE
PUBLIC :: KDKIND
PUBLIC :: KDTREE2, KDTREE2_RESULT, TREE_NODE
PUBLIC :: KDTREE2_CREATE, KDTREE2_DESTROY
PUBLIC :: KDTREE2_N_NEAREST,KDTREE2_N_NEAREST_AROUND_POINT
PUBLIC :: KDTREE2_R_NEAREST, KDTREE2_R_NEAREST_AROUND_POINT
PUBLIC :: KDTREE2_SORT_RESULTS
PUBLIC :: KDTREE2_R_COUNT, KDTREE2_R_COUNT_AROUND_POINT
PUBLIC :: KDTREE2_N_NEAREST_BRUTE_FORCE
PUBLIC :: KDTREE2_R_NEAREST_BRUTE_FORCE
INTEGER, PARAMETER :: BUCKET_SIZE = 12
TYPE INTERVAL
REAL(KDKIND) :: LOWER,UPPER
END TYPE INTERVAL
TYPE :: TREE_NODE
PRIVATE
INTEGER :: CUT_DIM
REAL(KDKIND) :: CUT_VAL
REAL(KDKIND) :: CUT_VAL_LEFT, CUT_VAL_RIGHT
INTEGER :: L, U
TYPE (TREE_NODE), POINTER :: LEFT, RIGHT
TYPE(INTERVAL), POINTER :: BOX(:) => NULL()
END TYPE TREE_NODE
TYPE :: KDTREE2
INTEGER :: DIMEN=0, N=0
REAL(KDKIND), POINTER :: THE_DATA(:,:) => NULL()
INTEGER, POINTER :: IND(:) => NULL()
LOGICAL :: SORT = .FALSE.
LOGICAL :: REARRANGE = .FALSE.
REAL(KDKIND), POINTER :: REARRANGED_DATA(:,:) => NULL()
TYPE (TREE_NODE), POINTER :: ROOT => NULL()
END TYPE KDTREE2
TYPE :: TREE_SEARCH_RECORD
PRIVATE
INTEGER :: DIMEN
INTEGER :: NN, NFOUND
REAL(KDKIND) :: BALLSIZE
INTEGER :: CENTERIDX=999, CORRELTIME=9999
INTEGER :: NALLOC
LOGICAL :: REARRANGE
LOGICAL :: OVERFLOW
REAL(KDKIND), POINTER :: QV(:)
TYPE(KDTREE2_RESULT), POINTER :: RESULTS(:)
TYPE(PQ) :: PQ
REAL(KDKIND), POINTER :: DATA(:,:)
INTEGER, POINTER :: IND(:)
END TYPE TREE_SEARCH_RECORD
PRIVATE
TYPE(TREE_SEARCH_RECORD), SAVE, TARGET :: SR
CONTAINS
FUNCTION KDTREE2_CREATE(INPUT_DATA,IDIM2,SORT,REARRANGE)&
RESULT(MR)
TYPE (KDTREE2), POINTER :: MR
INTEGER, INTENT(IN), OPTIONAL :: IDIM2
LOGICAL, INTENT(IN), OPTIONAL :: SORT
LOGICAL, INTENT(IN), OPTIONAL :: REARRANGE
REAL(KDKIND), TARGET :: INPUT_DATA(:,:)
INTEGER :: I
ALLOCATE (MR)
MR%THE_DATA => INPUT_DATA
IF (PRESENT(IDIM2)) THEN
MR%DIMEN = IDIM2
ELSE
MR%DIMEN = SIZE(INPUT_DATA,1)
END IF
MR%N = SIZE(INPUT_DATA,2)
IF (MR%DIMEN > MR%N) THEN
WRITE (*,*) 'KD_TREE_TRANS: LIKELY USER ERROR.'
WRITE (*,*) 'KD_TREE_TRANS: YOU PASSED IN MATRIX WITH D=',&
MR%DIMEN
WRITE (*,*) 'KD_TREE_TRANS: AND N=',MR%N
WRITE (*,*) 'KD_TREE_TRANS: NOTE, THAT NEW FORMAT IS',&
' DATA(1:D,1:N)'
WRITE (*,*) 'KD_TREE_TRANS: WITH USUALLY N >> D. ',&
'IF N =APPROX= D, THEN A K-D TREE'
WRITE (*,*) 'KD_TREE_TRANS: IS NOT AN APPROPRIATE DATA',&
' STRUCTURE.'
STOP
END IF
CALL BUILD_TREE(MR)
IF (PRESENT(SORT)) THEN
MR%SORT = SORT
ELSE
MR%SORT = .FALSE.
ENDIF
IF (PRESENT(REARRANGE)) THEN
MR%REARRANGE = REARRANGE
ELSE
MR%REARRANGE = .TRUE.
ENDIF
IF (MR%REARRANGE) THEN
ALLOCATE(MR%REARRANGED_DATA(MR%DIMEN,MR%N))
DO I=1,MR%N
MR%REARRANGED_DATA(:,I) = MR%THE_DATA(:,&
MR%IND(I))
ENDDO
ELSE
NULLIFY(MR%REARRANGED_DATA)
ENDIF
END FUNCTION KDTREE2_CREATE
SUBROUTINE BUILD_TREE(TP)
TYPE (KDTREE2), POINTER :: TP
! ..
INTEGER :: J
TYPE(TREE_NODE), POINTER :: DUMMY => NULL()
! ..
ALLOCATE (TP%IND(TP%N))
FORALL (J=1:TP%N)
TP%IND(J) = J
END FORALL
TP%ROOT => BUILD_TREE_FOR_RANGE(TP,1,TP%N, DUMMY)
END SUBROUTINE BUILD_TREE
RECURSIVE FUNCTION BUILD_TREE_FOR_RANGE(TP,L,U,PARENT)&
RESULT (RES)
TYPE (TREE_NODE), POINTER :: RES
TYPE (KDTREE2), POINTER :: TP
TYPE (TREE_NODE),POINTER :: PARENT
INTEGER, INTENT (IN) :: L, U
INTEGER :: I, C, M, DIMEN
LOGICAL :: RECOMPUTE
REAL(KDKIND) :: AVERAGE
DIMEN = TP%DIMEN
ALLOCATE (RES)
ALLOCATE(RES%BOX(DIMEN))
IF ( U < L ) THEN
NULLIFY(RES)
RETURN
END IF
IF ((U-L)<=BUCKET_SIZE) THEN
DO I=1,DIMEN
CALL SPREAD_IN_COORDINATE(TP,I,L,U,RES%BOX(I))
END DO
RES%CUT_DIM = 0
RES%CUT_VAL = 0.0
RES%L = L
RES%U = U
RES%LEFT =>NULL()
RES%RIGHT => NULL()
ELSE
DO I=1,DIMEN
RECOMPUTE=.TRUE.
IF (ASSOCIATED(PARENT)) THEN
IF (I .NE. PARENT%CUT_DIM) THEN
RECOMPUTE=.FALSE.
END IF
ENDIF
IF (RECOMPUTE) THEN
CALL SPREAD_IN_COORDINATE(TP,I,L,U,RES%BOX(I))
ELSE
RES%BOX(I) = PARENT%BOX(I)
ENDIF
END DO
C = MAXLOC(RES%BOX(1:DIMEN)%UPPER-RES%BOX(1:DIMEN)%LOWER,1)
IF (.FALSE.) THEN
M = (L+U)/2
CALL SELECT_ON_COORDINATE(TP%THE_DATA,TP%IND,C,M,L,U)
ELSE
IF (.TRUE.) THEN
AVERAGE = SUM(TP%THE_DATA(C,TP%IND(L:U))) / &
REAL(U-L+1,KDKIND)
ELSE
AVERAGE = (RES%BOX(C)%UPPER + RES%BOX(C)%LOWER)/2.0
ENDIF
RES%CUT_VAL = AVERAGE
M = SELECT_ON_COORDINATE_VALUE(&
TP%THE_DATA,TP%IND,C,AVERAGE,L,U)
ENDIF
RES%CUT_DIM = C
RES%L = L
RES%U = U
RES%LEFT => BUILD_TREE_FOR_RANGE(TP,L,M,RES)
RES%RIGHT => BUILD_TREE_FOR_RANGE(TP,M+1,U,RES)
IF (ASSOCIATED(RES%RIGHT) .EQV. .FALSE.) THEN
RES%BOX = RES%LEFT%BOX
RES%CUT_VAL_LEFT = RES%LEFT%BOX(C)%UPPER
RES%CUT_VAL = RES%CUT_VAL_LEFT
ELSEIF (ASSOCIATED(RES%LEFT) .EQV. .FALSE.) THEN
RES%BOX = RES%RIGHT%BOX
RES%CUT_VAL_RIGHT = RES%RIGHT%BOX(C)%LOWER
RES%CUT_VAL = RES%CUT_VAL_RIGHT
ELSE
RES%CUT_VAL_RIGHT = RES%RIGHT%BOX(C)%LOWER
RES%CUT_VAL_LEFT = RES%LEFT%BOX(C)%UPPER
RES%CUT_VAL = (RES%CUT_VAL_LEFT + RES%CUT_VAL_RIGHT)/2
RES%BOX%UPPER = MAX(RES%LEFT%BOX%UPPER,RES%RIGHT%BOX%UPPER)
RES%BOX%LOWER = MIN(RES%LEFT%BOX%LOWER,RES%RIGHT%BOX%LOWER)
ENDIF
END IF
END FUNCTION BUILD_TREE_FOR_RANGE
INTEGER FUNCTION SELECT_ON_COORDINATE_VALUE(V,IND,C,ALPHA,LI,UI)RESULT(RES)
INTEGER, INTENT (IN) :: C, LI, UI
REAL(KDKIND), INTENT(IN) :: ALPHA
REAL(KDKIND) :: V(1:,1:)
INTEGER :: IND(1:)
INTEGER :: TMP
INTEGER :: LB, RB
LB = LI; RB = UI
DO WHILE (LB < RB)
IF ( V(C,IND(LB)) <= ALPHA ) THEN
LB = LB+1
ELSE
TMP = IND(LB); IND(LB) = IND(RB); IND(RB) = TMP
RB = RB-1
ENDIF
END DO
IF (V(C,IND(LB)) <= ALPHA) THEN
RES = LB
ELSE
RES = LB-1
ENDIF
END FUNCTION SELECT_ON_COORDINATE_VALUE
SUBROUTINE SELECT_ON_COORDINATE(V,IND,C,K,LI,UI)
INTEGER, INTENT (IN) :: C, K, LI, UI
INTEGER :: I, L, M, S, T, U
REAL(KDKIND) :: V(:,:)
INTEGER :: IND(:)
L = LI
U = UI
DO WHILE (L<U)
T = IND(L)
M = L
DO I = L + 1, U
IF (V(C,IND(I))<V(C,T)) THEN
M = M + 1
S = IND(M)
IND(M) = IND(I)
IND(I) = S
END IF
END DO
S = IND(L)
IND(L) = IND(M)
IND(M) = S
IF (M<=K) L = M + 1
IF (M>=K) U = M - 1
END DO
END SUBROUTINE SELECT_ON_COORDINATE
SUBROUTINE SPREAD_IN_COORDINATE(TP,C,L,U,INTERV)
TYPE (KDTREE2), POINTER :: TP
TYPE(INTERVAL), INTENT(OUT) :: INTERV
INTEGER, INTENT (IN) :: C, L, U
REAL(KDKIND) :: LAST, LMAX, LMIN, T, SMIN,SMAX
INTEGER :: I, ULOCAL
REAL(KDKIND), POINTER :: V(:,:)
INTEGER, POINTER :: IND(:)
! ..
V => TP%THE_DATA(1:,1:)
IND => TP%IND(1:)
SMIN = V(C,IND(L))
SMAX = SMIN
ULOCAL = U
DO I = L + 2, ULOCAL, 2
LMIN = V(C,IND(I-1))
LMAX = V(C,IND(I))
IF (LMIN>LMAX) THEN
T = LMIN
LMIN = LMAX
LMAX = T
END IF
IF (SMIN>LMIN) SMIN = LMIN
IF (SMAX<LMAX) SMAX = LMAX
END DO
IF (I==ULOCAL+1) THEN
LAST = V(C,IND(ULOCAL))
IF (SMIN>LAST) SMIN = LAST
IF (SMAX<LAST) SMAX = LAST
END IF
INTERV%LOWER = SMIN
INTERV%UPPER = SMAX
END SUBROUTINE SPREAD_IN_COORDINATE
SUBROUTINE KDTREE2_DESTROY(TP)
TYPE (KDTREE2), POINTER :: TP
! ..
CALL DESTROY_NODE(TP%ROOT)
DEALLOCATE (TP%IND)
NULLIFY (TP%IND)
IF (TP%REARRANGE) THEN
DEALLOCATE(TP%REARRANGED_DATA)
NULLIFY(TP%REARRANGED_DATA)
ENDIF
DEALLOCATE(TP)
RETURN
CONTAINS
RECURSIVE SUBROUTINE DESTROY_NODE(NP)
TYPE (TREE_NODE), POINTER :: NP
INTRINSIC ASSOCIATED
IF (ASSOCIATED(NP%LEFT)) THEN
CALL DESTROY_NODE(NP%LEFT)
NULLIFY (NP%LEFT)
END IF
IF (ASSOCIATED(NP%RIGHT)) THEN
CALL DESTROY_NODE(NP%RIGHT)
NULLIFY (NP%RIGHT)
END IF
IF (ASSOCIATED(NP%BOX)) DEALLOCATE(NP%BOX)
DEALLOCATE(NP)
RETURN
END SUBROUTINE DESTROY_NODE
END SUBROUTINE KDTREE2_DESTROY
SUBROUTINE KDTREE2_N_NEAREST(TP,QV,NN,RESULTS)
TYPE (KDTREE2), POINTER :: TP
REAL(KDKIND), TARGET, INTENT (IN) :: QV(:)
INTEGER, INTENT (IN) :: NN
TYPE(KDTREE2_RESULT), TARGET :: RESULTS(:)
SR%BALLSIZE = HUGE(1.0)
SR%QV => QV
SR%NN = NN
SR%NFOUND = 0
SR%CENTERIDX = -1
SR%CORRELTIME = 0
SR%OVERFLOW = .FALSE.
SR%RESULTS => RESULTS
SR%NALLOC = NN
SR%IND => TP%IND
SR%REARRANGE = TP%REARRANGE
IF (TP%REARRANGE) THEN
SR%DATA => TP%REARRANGED_DATA
ELSE
SR%DATA => TP%THE_DATA
ENDIF
SR%DIMEN = TP%DIMEN
CALL VALIDATE_QUERY_STORAGE(NN)
SR%PQ = PQ_CREATE(RESULTS)
CALL SEARCH(TP%ROOT)
IF (TP%SORT) THEN
CALL KDTREE2_SORT_RESULTS(NN, RESULTS)
ENDIF
RETURN
END SUBROUTINE KDTREE2_N_NEAREST
SUBROUTINE KDTREE2_N_NEAREST_AROUND_POINT(TP,IDXIN,CORRELTIME,&
NN,RESULTS)
TYPE (KDTREE2), POINTER :: TP
INTEGER, INTENT (IN) :: IDXIN, CORRELTIME, NN
TYPE(KDTREE2_RESULT), TARGET :: RESULTS(:)
ALLOCATE (SR%QV(TP%DIMEN))
SR%QV = TP%THE_DATA(:,IDXIN)
SR%BALLSIZE = HUGE(1.0)
SR%CENTERIDX = IDXIN
SR%CORRELTIME = CORRELTIME
SR%NN = NN
SR%NFOUND = 0
SR%DIMEN = TP%DIMEN
SR%NALLOC = NN
SR%RESULTS => RESULTS
SR%IND => TP%IND
SR%REARRANGE = TP%REARRANGE
IF (SR%REARRANGE) THEN
SR%DATA => TP%REARRANGED_DATA
ELSE
SR%DATA => TP%THE_DATA
ENDIF
CALL VALIDATE_QUERY_STORAGE(NN)
SR%PQ = PQ_CREATE(RESULTS)
CALL SEARCH(TP%ROOT)
IF (TP%SORT) THEN
CALL KDTREE2_SORT_RESULTS(NN, RESULTS)
ENDIF
DEALLOCATE (SR%QV)
RETURN
END SUBROUTINE KDTREE2_N_NEAREST_AROUND_POINT
SUBROUTINE KDTREE2_R_NEAREST(TP,QV,R2,NFOUND,NALLOC,RESULTS)
TYPE (KDTREE2), POINTER :: TP
REAL(KDKIND), TARGET, INTENT (IN) :: QV(:)
REAL(KDKIND), INTENT(IN) :: R2
INTEGER, INTENT(OUT) :: NFOUND
INTEGER, INTENT (IN) :: NALLOC
TYPE(KDTREE2_RESULT), TARGET :: RESULTS(:)
SR%QV => QV
SR%BALLSIZE = R2
SR%NN = 0
SR%NFOUND = 0
SR%CENTERIDX = -1
SR%CORRELTIME = 0
SR%RESULTS => RESULTS
CALL VALIDATE_QUERY_STORAGE(NALLOC)
SR%NALLOC = NALLOC
SR%OVERFLOW = .FALSE.
SR%IND => TP%IND
SR%REARRANGE= TP%REARRANGE
IF (TP%REARRANGE) THEN
SR%DATA => TP%REARRANGED_DATA
ELSE
SR%DATA => TP%THE_DATA
ENDIF
SR%DIMEN = TP%DIMEN
CALL SEARCH(TP%ROOT)
NFOUND = SR%NFOUND
IF (TP%SORT) THEN
CALL KDTREE2_SORT_RESULTS(NFOUND, RESULTS)
ENDIF
IF (SR%OVERFLOW) THEN
WRITE (*,*) 'KD_TREE_TRANS: WARNING! RETURN FROM',&
' KDTREE2_R_NEAREST FOUND MORE NEIGHBORS'
WRITE (*,*) 'KD_TREE_TRANS: THAN STORAGE WAS PROVIDED FOR.',&
' ANSWER IS NOT SMALLEST BALL'
WRITE (*,*) 'KD_TREE_TRANS: WITH THAT NUMBER OF NEIGHBORS!',&
' I.E. IT IS WRONG.'
ENDIF
RETURN
END SUBROUTINE KDTREE2_R_NEAREST
SUBROUTINE KDTREE2_R_NEAREST_AROUND_POINT(TP,IDXIN,CORRELTIME,R2,&
NFOUND,NALLOC,RESULTS)
TYPE (KDTREE2), POINTER :: TP
INTEGER, INTENT (IN) :: IDXIN, CORRELTIME, NALLOC
REAL(KDKIND), INTENT(IN) :: R2
INTEGER, INTENT(OUT) :: NFOUND
TYPE(KDTREE2_RESULT), TARGET :: RESULTS(:)
INTRINSIC HUGE
! ..
ALLOCATE (SR%QV(TP%DIMEN))
SR%QV = TP%THE_DATA(:,IDXIN)
SR%BALLSIZE = R2
SR%NN = 0
SR%NFOUND = 0
SR%CENTERIDX = IDXIN
SR%CORRELTIME = CORRELTIME
SR%RESULTS => RESULTS
SR%NALLOC = NALLOC
SR%OVERFLOW = .FALSE.
CALL VALIDATE_QUERY_STORAGE(NALLOC)
SR%IND => TP%IND
SR%REARRANGE = TP%REARRANGE
IF (TP%REARRANGE) THEN
SR%DATA => TP%REARRANGED_DATA
ELSE
SR%DATA => TP%THE_DATA
ENDIF
SR%REARRANGE = TP%REARRANGE
SR%DIMEN = TP%DIMEN
CALL SEARCH(TP%ROOT)
NFOUND = SR%NFOUND
IF (TP%SORT) THEN
CALL KDTREE2_SORT_RESULTS(NFOUND,RESULTS)
ENDIF
IF (SR%OVERFLOW) THEN
WRITE (*,*) 'KD_TREE_TRANS: WARNING! RETURN FROM',&
' KDTREE2_R_NEAREST FOUND MORE NEIGHBORS'
WRITE (*,*) 'KD_TREE_TRANS: THAN STORAGE WAS PROVIDED FOR.',&
' ANSWER IS NOT SMALLEST BALL'
WRITE (*,*) 'KD_TREE_TRANS: WITH THAT NUMBER OF NEIGHBORS!',&
' I.E. IT IS WRONG.'
ENDIF
DEALLOCATE (SR%QV)
RETURN
END SUBROUTINE KDTREE2_R_NEAREST_AROUND_POINT
FUNCTION KDTREE2_R_COUNT(TP,QV,R2) RESULT(NFOUND)
TYPE (KDTREE2), POINTER :: TP
REAL(KDKIND), TARGET, INTENT (IN) :: QV(:)
REAL(KDKIND), INTENT(IN) :: R2
INTEGER :: NFOUND
INTRINSIC HUGE
! ..
SR%QV => QV
SR%BALLSIZE = R2
SR%NN = 0
SR%NFOUND = 0
SR%CENTERIDX = -1
SR%CORRELTIME = 0
NULLIFY(SR%RESULTS)
SR%NALLOC = 0
SR%IND => TP%IND
SR%REARRANGE = TP%REARRANGE
IF (TP%REARRANGE) THEN
SR%DATA => TP%REARRANGED_DATA
ELSE
SR%DATA => TP%THE_DATA
ENDIF
SR%DIMEN = TP%DIMEN
SR%OVERFLOW = .FALSE.
CALL SEARCH(TP%ROOT)
NFOUND = SR%NFOUND
RETURN
END FUNCTION KDTREE2_R_COUNT
FUNCTION KDTREE2_R_COUNT_AROUND_POINT(TP,IDXIN,CORRELTIME,R2)RESULT(NFOUND)
TYPE (KDTREE2), POINTER :: TP
INTEGER, INTENT (IN) :: CORRELTIME, IDXIN
REAL(KDKIND), INTENT(IN) :: R2
INTEGER :: NFOUND
INTRINSIC HUGE
! ..
ALLOCATE (SR%QV(TP%DIMEN))
SR%QV = TP%THE_DATA(:,IDXIN)
SR%BALLSIZE = R2
SR%NN = 0
SR%NFOUND = 0
SR%CENTERIDX = IDXIN
SR%CORRELTIME = CORRELTIME
NULLIFY(SR%RESULTS)
SR%NALLOC = 0
SR%IND => TP%IND
SR%REARRANGE = TP%REARRANGE
IF (SR%REARRANGE) THEN
SR%DATA => TP%REARRANGED_DATA
ELSE
SR%DATA => TP%THE_DATA
ENDIF
SR%DIMEN = TP%DIMEN
SR%OVERFLOW = .FALSE.
CALL SEARCH(TP%ROOT)
NFOUND = SR%NFOUND
RETURN
END FUNCTION KDTREE2_R_COUNT_AROUND_POINT
SUBROUTINE VALIDATE_QUERY_STORAGE(N)
INTEGER, INTENT(IN) :: N
IF (SIZE(SR%RESULTS,1) .LT. N) THEN
WRITE (*,*) 'KD_TREE_TRANS: YOU DID NOT PROVIDE ENOUGH',&
' STORAGE FOR RESULTS(1:N)'
STOP
RETURN
ENDIF
RETURN
END SUBROUTINE VALIDATE_QUERY_STORAGE
FUNCTION SQUARE_DISTANCE(D, IV,QV) RESULT (RES)
REAL(KDKIND) :: RES
INTEGER :: D
REAL(KDKIND) :: IV(:),QV(:)
RES = SUM( (IV(1:D)-QV(1:D))**2 )
END FUNCTION SQUARE_DISTANCE
RECURSIVE SUBROUTINE SEARCH(NODE)
TYPE (TREE_NODE), POINTER :: NODE
! ..
TYPE(TREE_NODE),POINTER :: NCLOSER, NFARTHER
!
INTEGER :: CUT_DIM, I
! ..
REAL(KDKIND) :: QVAL, DIS
REAL(KDKIND) :: BALLSIZE
REAL(KDKIND), POINTER :: QV(:)
TYPE(INTERVAL), POINTER :: BOX(:)
IF ((ASSOCIATED(NODE%LEFT) .AND. ASSOCIATED(NODE%RIGHT))&
.EQV. .FALSE.) THEN
IF (SR%NN .EQ. 0) THEN
CALL PROCESS_TERMINAL_NODE_FIXEDBALL(NODE)
ELSE
CALL PROCESS_TERMINAL_NODE(NODE)
ENDIF
ELSE
QV => SR%QV(1:)
CUT_DIM = NODE%CUT_DIM
QVAL = QV(CUT_DIM)
IF (QVAL < NODE%CUT_VAL) THEN
NCLOSER => NODE%LEFT
NFARTHER => NODE%RIGHT
DIS = (NODE%CUT_VAL_RIGHT - QVAL)**2
ELSE
NCLOSER => NODE%RIGHT
NFARTHER => NODE%LEFT
DIS = (NODE%CUT_VAL_LEFT - QVAL)**2
ENDIF
IF (ASSOCIATED(NCLOSER)) CALL SEARCH(NCLOSER)
IF (ASSOCIATED(NFARTHER)) THEN
BALLSIZE = SR%BALLSIZE
IF (DIS <= BALLSIZE) THEN
BOX => NODE%BOX(1:)
DO I=1,SR%DIMEN
IF (I .NE. CUT_DIM) THEN
DIS = DIS + DIS2_FROM_BND(QV(I),BOX(I)%LOWER,&