-
Notifications
You must be signed in to change notification settings - Fork 11
/
utils.F90
2925 lines (2369 loc) · 76.1 KB
/
utils.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 of generally useful routines and definitions
!Antony Lewis, http://cosmologist.info/
!April 2006: fix to TList_RealArr_Thin
!March 2008: fix to Ranges
!June 2010: fixed bug in DeleteFile gradually using up file units
module Ranges
!A collection of ranges, consisting of sections of minimum step size
implicit none
integer, parameter :: Max_Ranges = 100
double precision, parameter :: RangeTol = 0.1d0
!fraction of bin width we are prepared for merged bin widths to increase by
Type Region
integer start_index
integer steps
logical :: IsLog
double precision Low, High
double precision delta
double precision delta_max, delta_min !for log spacing, the non-log max and min step size
end Type Region
Type Regions
integer count
integer npoints
double precision Lowest, Highest
Type(Region) :: R(Max_ranges)
logical :: has_dpoints
double precision, dimension(:), pointer :: points, dpoints
!dpoints is (points(i+1)-points(i-1))/2
end Type Regions
contains
subroutine Ranges_Init(R)
Type(Regions) R
call Ranges_Free(R)
end subroutine Ranges_Init
subroutine Ranges_Free(R)
Type(Regions) R
integer status
deallocate(R%points,stat = status)
deallocate(R%dpoints,stat = status)
call Ranges_Nullify(R)
end subroutine Ranges_Free
subroutine Ranges_Nullify(R)
Type(Regions) R
nullify(R%points)
nullify(R%dpoints)
R%count = 0
R%npoints = 0
R%has_dpoints = .false.
end subroutine Ranges_Nullify
subroutine Ranges_Assign(R,Rin)
Type(Regions) R, Rin
call Ranges_Init(R)
R = Rin
nullify(R%points,R%dpoints)
if (associated(Rin%points)) then
call Ranges_GetArray(R, associated(Rin%dpoints))
end if
end subroutine Ranges_Assign
function Ranges_IndexOf(Reg, tau) result(pointstep)
Type(Regions), intent(in), target :: Reg
Type(Region), pointer :: AReg
double precision :: tau
integer pointstep
integer i
pointstep=0
do i=1,Reg%count
AReg => Reg%R(i)
if (tau < AReg%High .and. tau >= AReg%Low) then
if (AReg%IsLog) then
pointstep = AReg%start_index + int( log(tau/AReg%Low)/AReg%delta)
else
pointstep = AReg%start_index + int(( tau - AReg%Low)/AReg%delta)
end if
return
end if
end do
if (tau >= Reg%Highest) then
pointstep = Reg%npoints
else
write (*,*) 'Ranges_IndexOf: value out of range'
stop
end if
end function Ranges_IndexOf
subroutine Ranges_GetArray(Reg, want_dpoints)
Type(Regions), target :: Reg
Type(Region), pointer :: AReg
logical, intent(in), optional :: want_dpoints
integer status,i,j,ix
if (present(want_dpoints)) then
Reg%has_dpoints = want_dpoints
else
Reg%has_dpoints = .true.
end if
deallocate(Reg%points,stat = status)
allocate(Reg%points(Reg%npoints))
ix=0
do i=1, Reg%count
AReg => Reg%R(i)
do j = 0, AReg%steps-1
ix=ix+1
if (AReg%IsLog) then
Reg%points(ix) = AReg%Low*exp(j*AReg%delta)
else
Reg%points(ix) = AReg%Low + AReg%delta*j
end if
end do
end do
ix =ix+1
Reg%points(ix) = Reg%Highest
if (ix /= Reg%npoints) stop 'Ranges_GetArray: ERROR'
if (Reg%has_dpoints) call Ranges_Getdpoints(Reg)
end subroutine Ranges_GetArray
subroutine Ranges_Getdpoints(Reg, half_ends)
Type(Regions), target :: Reg
logical, intent(in), optional :: half_ends
integer i, status
logical halfs
if (present(half_ends)) then
halfs = half_ends
else
halfs = .true.
end if
deallocate(Reg%dpoints,stat = status)
allocate(Reg%dpoints(Reg%npoints))
do i=2, Reg%npoints-1
Reg%dpoints(i) = (Reg%points(i+1) - Reg%points(i-1))/2
end do
if (halfs) then
Reg%dpoints(1) = (Reg%points(2) - Reg%points(1))/2
Reg%dpoints(Reg%npoints) = (Reg%points(Reg%npoints) - Reg%points(Reg%npoints-1))/2
else
Reg%dpoints(1) = (Reg%points(2) - Reg%points(1))
Reg%dpoints(Reg%npoints) = (Reg%points(Reg%npoints) - Reg%points(Reg%npoints-1))
end if
end subroutine Ranges_Getdpoints
subroutine Ranges_Add_delta(Reg, t_start, t_end, t_approx_delta, IsLog)
Type(Regions), target :: Reg
logical, intent(in), optional :: IsLog
double precision, intent(in) :: t_start, t_end, t_approx_delta
integer n
logical :: WantLog
if (present(IsLog)) then
WantLog = IsLog
else
WantLog = .false.
end if
if (t_end <= t_start) &
stop 'Ranges_Add_delta: end must be larger than start'
if (t_approx_delta <=0) stop 'Ranges_Add_delta: delta must be > 0'
if (WantLog) then
n = max(1,int(log(t_end/t_start)/t_approx_delta + 1.d0 - RangeTol))
else
n = max(1,int((t_end-t_start)/t_approx_delta + 1.d0 - RangeTol))
end if
call Ranges_Add(Reg,t_start, t_end, n, WantLog)
end subroutine Ranges_Add_delta
subroutine Ranges_Add(Reg, t_start, t_end, nstep, IsLog)
Type(Regions), target :: Reg
logical, intent(in), optional :: IsLog
double precision, intent(in) :: t_start, t_end
integer, intent(in) :: nstep
Type(Region), pointer :: AReg, LastReg
Type(Region), target :: NewRegions(Max_Ranges)
double precision EndPoints(0:Max_Ranges*2)
integer ixin, nreg, ix, i,j, nsteps
double precision delta
logical WantLog
double precision min_request, max_request, min_log_step, max_log_step, diff, max_delta
double precision RequestDelta(Max_Ranges)
if (present(IsLog)) then
WantLog = IsLog
else
WantLog = .false.
end if
if (WantLog) then
delta = log(t_end/t_start) / nstep
else
delta = (t_end - t_start) / nstep
end if
if (t_end <= t_start) stop 'Ranges_Add: end must be larger than start'
if (nstep <=0) stop 'Ranges_Add: nstep must be > 0'
if (Reg%Count>= Max_Ranges) stop 'Ranges_Add: Increase Max_Ranges'
!avoid IBM compiler bug, from Angel de Vicente
! if (Reg%count > 0) NewRegions(1:Reg%count) = Reg%R(1:Reg%count)
if (Reg%count > 0) THEN
DO i=1,Reg%count
NewRegions(i) = Reg%R(i)
END DO
END IF
nreg = Reg%count + 1
AReg=> NewRegions(nreg)
AReg%Low = t_start
AReg%High = t_end
AReg%delta = delta
AReg%steps = nstep
AReg%IsLog = WantLog
!Get end point in order
ix = 0
do i=1, nreg
AReg => NewRegions(i)
if (ix==0) then
ix = 1
EndPoints(ix) = AReg%Low
ix = 2
EndPoints(ix) = AReg%High
else
ixin = ix
do j=1,ixin
if (AReg%Low < EndPoints(j)) then
EndPoints(j+1:ix+1) = EndPoints(j:ix)
EndPoints(j) = AReg%Low
ix=ix+1
exit
end if
end do
if (ixin == ix) then
ix = ix+1
EndPoints(ix) = AReg%Low
ix = ix+1
EndPoints(ix) = AReg%High
else
ixin = ix
do j=1,ixin
if (AReg%High < EndPoints(j)) then
EndPoints(j+1:ix+1) = EndPoints(j:ix)
EndPoints(j) = AReg%High
ix=ix+1
exit
end if
end do
if (ixin == ix) then
ix = ix+1
EndPoints(ix) = AReg%High
end if
end if
end if
end do
!remove duplicate points
ixin = ix
ix = 1
do i=2, ixin
if (EndPoints(i) /= EndPoints(ix)) then
ix=ix+1
EndPoints(ix) = EndPoints(i)
end if
end do
!ix is the number of end points
Reg%Lowest = EndPoints(1)
Reg%Highest = EndPoints(ix)
Reg%count = 0
max_delta = Reg%Highest - Reg%Lowest
do i=1, ix - 1
AReg => Reg%R(i)
AReg%Low = EndPoints(i)
AReg%High = EndPoints(i+1)
! max_delta = EndPoints(i+1) - EndPoints(i)
delta = max_delta
AReg%IsLog = .false.
do j=1, nreg
if (AReg%Low >= NewRegions(j)%Low .and. Areg%Low < NewRegions(j)%High) then
if (NewRegions(j)%IsLog) then
if (AReg%IsLog) then
delta = min(delta,NewRegions(j)%delta)
else
min_log_step = AReg%Low*(exp(NewRegions(j)%delta)-1)
if (min_log_step < delta) then
max_log_step = AReg%High*(1-exp(-NewRegions(j)%delta))
if (delta < max_log_step) then
delta = min_log_step
else
AReg%IsLog = .true.
delta = NewRegions(j)%delta
end if
end if
end if
else !NewRegion is not log
if (AReg%IsLog) then
max_log_step = AReg%High*(1-exp(-delta))
if (NewRegions(j)%delta < max_log_step) then
min_log_step = AReg%Low*(exp(delta)-1)
if (min_log_step < NewRegions(j)%delta) then
AReg%IsLog = .false.
delta = min_log_step
else
delta = - log(1- NewRegions(j)%delta/AReg%High)
end if
end if
else
delta = min(delta, NewRegions(j)%delta)
end if
end if
end if
end do
if (AReg%IsLog) then
Diff = log(AReg%High/AReg%Low)
else
Diff = AReg%High - AReg%Low
endif
if (delta >= Diff) then
AReg%delta = Diff
AReg%steps = 1
else
AReg%steps = max(1,int(Diff/delta + 1.d0 - RangeTol))
AReg%delta = Diff / AReg%steps
end if
Reg%count = Reg%count + 1
RequestDelta(Reg%Count) = delta
if (AReg%IsLog) then
if (AReg%steps ==1) then
AReg%Delta_min = AReg%High - AReg%Low
AReg%Delta_max = AReg%Delta_min
else
AReg%Delta_min = AReg%Low*(exp(AReg%delta)-1)
AReg%Delta_max = AReg%High*(1-exp(-AReg%delta))
end if
else
AReg%Delta_max = AReg%delta
AReg%Delta_min = AReg%delta
end if
end do
!Get rid of tiny regions
ix = Reg%Count
do i=ix, 1, -1
AReg => Reg%R(i)
if (AReg%steps ==1) then
Diff = AReg%High - AReg%Low
if (AReg%IsLog) then
min_request = AReg%Low*(exp(RequestDelta(i))-1)
max_request = AReg%High*(1-exp(-RequestDelta(i)))
else
min_request = RequestDelta(i)
max_request = min_request
end if
if (i/= Reg%Count) then !from i/= ix Mar08
LastReg => Reg%R(i+1)
if (RequestDelta(i) >= AReg%delta .and. Diff <= LastReg%Delta_min &
.and. LastReg%Delta_min <= max_request) then
LastReg%Low = AReg%Low
if (Diff > LastReg%Delta_min*RangeTol) then
LastReg%steps = LastReg%steps + 1
end if
if (LastReg%IsLog) then
LastReg%delta = log(LastReg%High/LastReg%Low) / LastReg%steps
else
LastReg%delta = (LastReg%High -LastReg%Low) / LastReg%steps
end if
Reg%R(i:Reg%Count-1) = Reg%R(i+1:Reg%Count)
Reg%Count = Reg%Count -1
cycle
end if
end if
if (i/=1) then
LastReg => Reg%R(i-1)
if (RequestDelta(i) >= AReg%delta .and. Diff <= LastReg%Delta_max &
.and. LastReg%Delta_max <= min_request) then
LastReg%High = AReg%High
!AlMat08 LastReg%Low = AReg%Low
if (Diff > LastReg%Delta_max*RangeTol) then
LastReg%steps = LastReg%steps + 1
end if
if (LastReg%IsLog) then
LastReg%delta = log(LastReg%High/LastReg%Low) / LastReg%steps
else
LastReg%delta = (LastReg%High -LastReg%Low) / LastReg%steps
end if
Reg%R(i:Reg%Count-1) = Reg%R(i+1:Reg%Count)
Reg%Count = Reg%Count -1
end if
end if
end if
end do
!Set up start indices and get total number of steps
nsteps = 1
do i = 1, Reg%Count
AReg => Reg%R(i)
AReg%Start_index = nsteps
nsteps = nsteps + AReg%steps
if (AReg%IsLog) then
if (AReg%steps ==1) then
AReg%Delta_min = AReg%High - AReg%Low
AReg%Delta_max = AReg%Delta_min
else
AReg%Delta_min = AReg%Low*(exp(AReg%delta)-1)
AReg%Delta_max = AReg%High*(1-exp(-AReg%delta))
end if
else
AReg%Delta_max = AReg%delta
AReg%Delta_min = AReg%delta
end if
end do
Reg%npoints = nsteps
end subroutine Ranges_Add
subroutine Ranges_Write(Reg)
Type(Regions), intent(in), target :: Reg
Type(Region), pointer :: AReg
integer i
do i=1,Reg%count
AReg => Reg%R(i)
if (AReg%IsLog) then
Write (*,'("Range ",I3,":", 3E14.4," log")') i, AReg%Low, AReg%High, AReg%delta
else
Write (*,'("Range ",I3,":", 3E14.4," linear")') i, AReg%Low, AReg%High, AReg%delta
end if
end do
end subroutine Ranges_Write
end module Ranges
module Lists
!Currently implements lists of strings and lists of arrays of reals
implicit none
type real_pointer
real, dimension(:), pointer :: p
end type real_pointer
type double_pointer
double precision, dimension(:), pointer :: p
end type double_pointer
type String_pointer
character, dimension(:), pointer :: p
end type String_pointer
Type TList_RealArr
integer Count
integer Delta
integer Capacity
type(Real_Pointer), dimension(:), pointer :: Items
end Type TList_RealArr
Type TStringList
integer Count
integer Delta
integer Capacity
type(String_Pointer), dimension(:), pointer :: Items
end Type TStringList
contains
subroutine TList_RealArr_Init(L)
Type (TList_RealArr) :: L
L%Count = 0
L%Capacity = 0
L%Delta = 1024
nullify(L%items)
end subroutine TList_RealArr_Init
subroutine TList_RealArr_Clear(L)
Type (TList_RealArr) :: L
integer i, status
do i=L%Count,1,-1
deallocate (L%Items(i)%P, stat = status)
end do
deallocate (L%Items, stat = status)
nullify(L%Items)
L%Count = 0
L%Capacity = 0
end subroutine TList_RealArr_Clear
subroutine TList_RealArr_Add(L, P)
Type (TList_RealArr) :: L
real, intent(in) :: P(:)
integer s
if (L%Count == L%Capacity) call TList_RealArr_SetCapacity(L, L%Capacity + L%Delta)
s = size(P)
L%Count = L%Count + 1
allocate(L%Items(L%Count)%P(s))
L%Items(L%Count)%P = P
end subroutine TList_RealArr_Add
subroutine TList_RealArr_SetCapacity(L, C)
Type (TList_RealArr) :: L
integer C
type(Real_Pointer), dimension(:), pointer :: TmpItems
if (L%Count > 0) then
if (C < L%Count) stop 'TList_RealArr_SetCapacity: smaller than Count'
allocate(TmpItems(L%Count))
TmpItems = L%Items(1:L%Count)
deallocate(L%Items)
allocate(L%Items(C))
L%Items(1:L%Count) = TmpItems
deallocate(TmpItems)
else
allocate(L%Items(C))
end if
L%Capacity = C
end subroutine TList_RealArr_SetCapacity
subroutine TList_RealArr_Delete(L, i)
Type (TList_RealArr) :: L
integer, intent(in) :: i
integer status
deallocate(L%items(i)%P, stat = status)
if (L%Count > 1) L%Items(i:L%Count-1) = L%Items(i+1:L%Count)
L%Count = L%Count -1
end subroutine TList_RealArr_Delete
subroutine TList_RealArr_SaveBinary(L,fid)
Type (TList_RealArr) :: L
integer, intent(in) :: fid
integer i
write (fid) L%Count
do i=1,L%Count
write(fid) size(L%Items(i)%P)
write(fid) L%Items(i)%P
end do
end subroutine TList_RealArr_SaveBinary
subroutine TList_RealArr_ReadBinary(L,fid)
Type (TList_RealArr) :: L
integer, intent(in) :: fid
integer num,i,sz
call TList_RealArr_Clear(L)
read (fid) num
call TList_RealArr_SetCapacity(L, num)
do i=1,num
read(fid) sz
allocate(L%Items(i)%P(sz))
read(fid) L%Items(i)%P
end do
L%Count = num
end subroutine TList_RealArr_ReadBinary
subroutine TList_RealArr_Thin(L, i)
Type (TList_RealArr) :: L
integer, intent(in) :: i
integer newCount
type(Real_Pointer), dimension(:), pointer :: TmpItems
if (L%Count > 1) then
newCount = (L%Count-1)/i+1
allocate(TmpItems(newCount))
TmpItems = L%Items(1:L%Count:i)
deallocate(L%Items)
L%Capacity = newCount
allocate(L%Items(L%Capacity))
L%Items = TmpItems
L%Count = newCount
deallocate(TmpItems)
end if
end subroutine TList_RealArr_Thin
subroutine TList_RealArr_ConfidVal(L, ix, limfrac, ix1, ix2, Lower, Upper)
!Taking the ix'th entry in each array to be a sample, value for which
!limfrac of the items between ix1 and ix2 (inc) are above or below
!e.g. if limfrac = 0.05 get two tail 90% confidence limits
Type (TList_RealArr) :: L
integer, intent(IN) :: ix
real, intent(IN) :: limfrac
real, intent(OUT), optional :: Lower, Upper
integer, intent(IN), optional :: ix1,ix2
integer b,t,samps
real pos, d
type(Real_Pointer), dimension(:), pointer :: SortItems
b=1
t=L%Count
if (present(ix1)) b = ix1
if (present(ix2)) t = ix2
samps = t - b + 1
allocate(SortItems(samps))
SortItems = L%Items(b:t)
call QuickSortArr_Real(SortItems, 1, samps, ix)
if (present(Lower)) then
pos = (samps-1)*limfrac + 1
b = max(int(pos),1)
Lower = SortItems(b)%P(ix)
if (b < samps .and. pos>b) then
d = pos - b
Lower = Lower*(1 - d) + d * SortItems(b+1)%P(ix)
end if
end if
if (present(Upper)) then
pos = (samps-1)*(1.-limfrac) + 1
b = max(int(pos),1)
Upper = SortItems(b)%P(ix)
if (b < samps .and. pos>b) then
d = pos - b
Upper = Upper*(1 - d) + d * SortItems(b+1)%P(ix)
end if
end if
deallocate(SortItems)
end subroutine TList_RealArr_ConfidVal
subroutine TStringList_Init(L)
Type (TStringList) :: L
L%Count = 0
L%Capacity = 0
L%Delta = 128
nullify(L%items)
end subroutine TStringList_Init
subroutine TStringList_Clear(L)
Type (TStringList) :: L
integer i, status
do i=L%Count,1,-1
deallocate (L%Items(i)%P, stat = status)
end do
deallocate (L%Items, stat = status)
call TStringList_Init(L)
end subroutine TStringList_Clear
subroutine TStringList_SetFromString(L, S, valid_chars_in)
Type (TStringList) :: L
character(Len=*), intent(in) :: S
character(Len=*), intent(in), optional :: valid_chars_in
character(LEN=1024) item
integer i,j
character(LEN=256) valid_chars
if (present(valid_chars_in)) then
valid_chars = valid_chars_in
else
valid_chars='abcdefghijklmopqrstuvwxyzABCDEFGHIJKLMOPQRSTUVWXYZ0123456789_-.'
endif
call TStringList_Clear(L)
item =''
j=0
do i=1, len_trim(S)
if (verify(S(i:i),trim(valid_chars)) == 0) then
j=j+1
item(j:j) = S(i:i)
else
if (trim(S(i:i))/='') then
write (*,*) 'Invalid character in: '//trim(S)
end if
if (j>0) call TStringList_Add(L, item(1:j))
j=0
end if
end do
if (j>0) call TStringList_Add(L, item(1:j))
end subroutine TStringList_SetFromString
subroutine TStringList_Add(L, P)
Type (TStringList) :: L
character(LEN=*), intent(in) :: P
integer s,i
if (L%Count == L%Capacity) call TStringList_SetCapacity(L, L%Capacity + L%Delta)
s = len_trim(P)
L%Count = L%Count + 1
allocate(L%Items(L%Count)%P(s))
do i=1,s
L%Items(L%Count)%P(i) = P(i:i)
end do
end subroutine TStringList_Add
function TStringList_Item(L, i) result(S)
Type (TStringList) :: L
integer, intent(in) :: i
integer j
character(LEN=1024) S
S=''
if (i<=L%Count .and. i>0) then
do j=1,size(L%Items(i)%P)
S(j:j)=L%Items(i)%P(j)
end do
end if
end function TStringList_Item
subroutine TStringList_SetCapacity(L, C)
Type (TStringList) :: L
integer C
type(String_Pointer), dimension(:), pointer :: TmpItems
if (L%Count > 0) then
if (C < L%Count) stop 'TStringList_SetCapacity: smaller than Count'
allocate(TmpItems(L%Count))
TmpItems = L%Items(1:L%Count)
deallocate(L%Items)
allocate(L%Items(C))
L%Items(1:L%Count) = TmpItems
deallocate(TmpItems)
else
allocate(L%Items(C))
end if
L%Capacity = C
end subroutine TStringList_SetCapacity
subroutine TStringList_Delete(L, i)
Type (TStringList) :: L
integer, intent(in) :: i
integer status
deallocate(L%items(i)%P, stat = status)
if (L%Count > 1) L%Items(i:L%Count-1) = L%Items(i+1:L%Count)
L%Count = L%Count -1
end subroutine TStringList_Delete
function TStringList_IndexOf(L, S)
Type (TStringList) :: L
character(LEN=*), intent(in) :: S
integer TStringList_IndexOf, i, j,slen
slen = len_trim(S)
do i=1,L%Count
if ( size(L%Items(i)%P)==slen) then
!Yes, comparing strings and pointer strings really is this horrible...
j=1
do while (L%Items(i)%P(j)==S(j:j))
j=j+1
if (j>slen) then
TStringList_IndexOf = i
return
end if
end do
end if
end do
TStringList_IndexOf=-1
end function TStringList_IndexOf
recursive subroutine QuickSortArr_Real(Arr, Lin, R, index)
!Sorts an array of pointers to arrays of reals by the value of the index'th entry
integer, intent(in) :: Lin, R, index
#ifdef __GFORTRAN__
type(real_pointer), dimension(:) :: Arr
#else
type(real_pointer), dimension(*) :: Arr
#endif
integer I, J, L
real P
type(real_pointer) :: temp
L = Lin
do
I = L
J = R
P = Arr((L + R)/2)%p(index)
do
do while (Arr(I)%p(index) < P)
I = I + 1
end do
do while (Arr(J)%p(index) > P)
J = J - 1
end do
if (I <= J) then
Temp%p => Arr(I)%p
Arr(I)%p => Arr(J)%p
Arr(J)%p => Temp%p
I = I + 1
J = J - 1
end if
if (I > J) exit
end do
if (L < J) call QuickSortArr_Real(Arr, L, J, index);
L = I
if (I >= R) exit
end do
end subroutine QuickSortArr_Real
recursive subroutine QuickSortArr(Arr, Lin, R, index)
!Sorts an array of pointers to arrays of reals by the value of the index'th entry
integer, intent(in) :: Lin, R, index
#ifdef __GFORTRAN__
type(double_pointer), dimension(:) :: Arr
#else
type(double_pointer), dimension(*) :: Arr
#endif
integer I, J, L
double precision P
type(double_pointer) :: temp
L = Lin
do
I = L
J = R
P = Arr((L + R)/2)%p(index)
do
do while (Arr(I)%p(index) < P)
I = I + 1
end do
do while (Arr(J)%p(index) > P)
J = J - 1
end do
if (I <= J) then
Temp%p => Arr(I)%p
Arr(I)%p => Arr(J)%p
Arr(J)%p => Temp%p
I = I + 1
J = J - 1
end if
if (I > J) exit
end do
if (L < J) call QuickSortArr(Arr, L, J, index);
L = I
if (I >= R) exit
end do
end subroutine QuickSortArr
end module Lists
module AMLutils
use Lists
#ifdef DECONLY
!Comment out if linking to LAPACK/MKL separetly
!CXML only has LAPACK 2.0
include 'CXML_INCLUDE.F90'
#endif
#ifdef NAGF95
use F90_UNIX
#endif
implicit none
#ifndef NAGF95
#ifndef GFC
#ifndef __INTEL_COMPILER_BUILD_DATE
#ifndef __GFORTRAN__
integer iargc
external iargc
#endif
#endif
#endif
#endif
#ifdef MPI
include "mpif.h"
#endif
integer :: Feedback = 1
integer, parameter :: tmp_file_unit = 50
double precision, parameter :: pi=3.14159265358979323846264338328d0, &
twopi=2*pi, fourpi=4*pi
double precision, parameter :: root2 = 1.41421356237309504880168872421d0, sqrt2 = root2
double precision, parameter :: log2 = 0.693147180559945309417232121458d0
real, parameter :: pi_r = 3.141592653, twopi_r = 2*pi_r, fourpi_r = twopi_r*2
logical :: flush_write = .true.
!True means no data lost on crashes, but may make it slower
integer, parameter :: file_units_start = 20
integer, parameter :: file_units_end = 100
logical file_units(file_units_start:file_units_end)
INTERFACE CONCAT
module procedure concat_s, concat_s_n
END INTERFACE
INTERFACE RealToStr
module procedure SingleToStr, DoubleToStr
END INTERFACE RealToStr
contains
function new_file_unit()
integer i, new_file_unit
logical, save :: file_units_inited = .false.
logical notfree
if (.not. file_units_inited) then
file_units = .false.
file_units_inited = .true.
end if
do i=file_units_start, file_units_end
if (.not. file_units(i) .and. i/=tmp_file_unit) then
inquire(i,opened=notfree)
if (notfree) cycle
file_units(i)=.true.
new_file_unit = i
return
end if
end do