-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod_utils.f90
1662 lines (1471 loc) · 56.8 KB
/
mod_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 utils
implicit none
private
integer, parameter, public:: dp=kind(0.d0)
integer, parameter, public:: sp=kind(0.)
!constants
real(dp), parameter, public:: pi=3.141592653589793
!physical constants in cgs units
real(dp), parameter, public:: light_velocity=3.e10, stefan_boltzmann=5.6704e-5, elementary_charge=4.8e-10, &
&electron_mass=9.10938291e-28, electron_radius=2.8179e-13, sigma_t=6.65245873498e-25, &
&AU_to_cm=1.49598e+13, K_to_mcc=0.1686e-9,&
&erg_to_mcc=1.221433e+06,erg_to_eV=6.24150934326018e+11,&
&TeV_to_mcc=1.956947e+06,GeV_to_mcc=1.956947e+03,&
&keV_to_mcc=1.956947e-03,eV_to_mcc=1.956947e-06,Hz_to_mcc=8.09329978572e-21,&
&cm_per_pc=3.085680e+18,cm_per_Mpc=3.085680e+24,&
&s_per_yr=3.155760e+07,s_per_day=8.64e4,&
&hbar=1.054e-27,hPlanck=6.62607015e-27,&
&gravitational_contant=6.673e-08,sun_mass=1.989100e+33
public &
&make_latex_string,&
&logspace,&
&linspace,&
&linspace_middle_point,&
&simple_integration,&
&pl_integration,&
&simple_approximation,&
&spline_coeficients,&
&newunit,&
&read_data_files
contains
function value_index_nonstrict(A,aa) result(na) ! if aa<=A(1) then reterns 1, otherwise na>1
implicit none
real(dp), intent(in) :: A(:),aa
integer :: na
na=1
do while ( aa > A(na) .and. na<size(A) )
na=na+1
end do
return
end function value_index_nonstrict
function make_latex_string(x,n,e) result(out_string)
implicit none
real(dp), intent(in) :: x
integer, optional :: n
logical, optional :: e
character(len=30) :: out_string
integer :: nd
logical :: ei
character(len=1) :: s_nd
character(len=6) :: s_print
! return zero
if ( x == 0.) then
out_string = "0"
return
end if
! check exponential output
if (present(e)) then
ei = e
else
ei = .False.
endif
if ( .not. ei) then
!number of digits for the
if (present(n)) then
nd = n
else
nd = 2
endif
write(s_nd,'(I1)') nd
s_print = "(F5."//s_nd//")"
if ( x > 0.01 .and. x < 10) then
write(out_string,s_print) x
return
end if
if ( x > -10 .and. x < -0.01) then
write(out_string,s_print) x
return
end if
if ( x > -100 .and. x <= -10) then
write(out_string,'(F5.1)') x
return
end if
if ( x >= 10 .and. x < 100) then
write(out_string,'(F5.1)') x
return
end if
if ( x > -1000 .and. x <= -100) then
write(out_string,'(F5.0)') x
return
end if
if ( x >= 100 .and. x < 1000) then
write(out_string,'(F5.0)') x
return
end if
endif
block
character(3) :: s_coef, s_power, s_sign
real(dp) :: coef, logg
integer :: power
! number of digits
if (present(n)) then
nd = n
else
nd = 1
endif
write(s_nd,'(I1)') nd
s_print = "(F3."//s_nd//")"
if (x > 0) then
s_sign = ""
else
s_sign = "-"
endif
logg = log10(abs(x))
power = nint(logg)
coef = abs(x) / (10.** power)
if ( coef > 0.95 .and. coef < 1.05 ) then
write(s_power,'(I2)') power
out_string = trim(s_sign)//"10^{"//trim(s_power)//"}"
else
if (power <= 0) power = power - 1
coef = abs(x) / (10.** power)
write(s_coef,s_print) coef
write(s_power,'(I2)') power
if ( power == 0 ) then
out_string = trim(s_sign)//trim(s_coef)
else
out_string = trim(s_sign)//trim(s_coef)//"\times10^{"//trim(s_power)//"}"
endif
endif
end block
return
end function make_latex_string
subroutine linspace(E,Emin,Emax)
real(dp) E(:)
real(dp) Emin,Emax
integer i
E=[(Emin+(Emax-Emin)*(i-1._dp)/(size(E)-1._dp), i=1,size(E))]
return
end subroutine linspace
subroutine linspace_middle_point(E,Emin,Emax)
real(dp) E(:)
real(dp) Emin,Emax
integer i
E=[(Emin+(Emax-Emin)*(i-0.5_dp)/size(E), i=1,size(E))]
return
end subroutine linspace_middle_point
function simple_integration(x,y) result(sum)
real(dp), intent(in)::x(:),y(:)
real(dp) sum
integer k
if (size(x) /= size(y)) stop "inconsistent size of arrays in simple_integration"
sum=0._dp
do k=1,size(x)-1
sum=sum+(y(k)+y(k+1))/2._dp*(x(k+1)-x(k))
enddo
return
end function simple_integration
function pl_integration(x,y) result(sum)
! function reterns an integral assuming a power-law interpolation between the nods
real(dp), intent(in)::x(:),y(:)
real(dp) alpha,coef,sum
integer k
if (size(x) /= size(y)) stop "inconsistent size of arrays in pl_integration"
if (size(x) < 2) stop "inconsistent size of arrays in pl_integration"
if (minval(x) <= 0) stop "inconsistent values in pl_integration, x"
if (minval(y) < 0) stop "inconsistent values in pl_integration, y"
sum=0._dp
do k=1,size(x)-1
if (y(k+1) > 0 .and. y(k) > 0) then
alpha = log(y(k+1)/y(k)) / log(x(k+1)/x(k))
if (alpha > -1.01 .and. alpha < -0.99 ) alpha = -1
coef = y(k) / x(k)**alpha
if (alpha == -1) then
sum=sum + coef * log(x(k+1)/x(k))
else
sum=sum + coef / (alpha+1) * (x(k+1)**(alpha+1) - x(k)**(alpha+1))
endif
endif
enddo
return
end function pl_integration
function simple_approximation(x,x_array,y_array) result(y)
implicit none
real(dp), intent(in) :: x,x_array(:),y_array(:)
real(dp) :: y
integer :: i
if( size(x_array) /= size(y_array) .or. size(x_array) < 2 ) stop "Incorrect size of x,y arrays in simple arrpoximation"
i=value_index_nonstrict(x_array,x)
if ( i == 1 ) i=2
y=y_array(i-1)+(x-x_array(i-1))/(x_array(i)-x_array(i-1))*(y_array(i)-y_array(i-1))
return
end function simple_approximation
integer function newunit(unit) result(n)
! returns lowest i/o unit number not in use
integer, intent(out), optional :: unit
logical inuse
integer, parameter :: nmin=10 ! avoid lower numbers which are sometimes reserved
integer, parameter :: nmax=999 ! may be system-dependent
do n = nmin, nmax
inquire(unit=n, opened=inuse)
if (.not. inuse) then
if (present(unit)) unit=n
return
end if
end do
stop "newunit ERROR: available unit not found."
end function newunit
subroutine read_data_files(array_to_fill,data_file,number_of_raws,&
&optional_max_length,optional_comment_char)
! array_to_fill == double precision array, allocatable
! string with the file name
! number of raws in the data file
! max length of the file, if not given set to 100
! comment character, if not given set to #
! use utils, only : newunit
implicit none
real(dp), dimension(:,:), intent(out), allocatable :: array_to_fill
character(len=*), intent(in) :: data_file
integer, intent(in) :: number_of_raws
integer, intent(in), optional :: optional_max_length
character(1), intent(in), optional :: optional_comment_char
character(1) :: comment_char
real(dp), dimension(:,:), allocatable :: read_data
integer :: max_length
logical :: was_read=.false.
integer :: u,stat,number_data_points,row
character (len=1000) :: line_from_file
if ( present(optional_max_length)) then
max_length=optional_max_length
else
max_length=100
endif
if ( present(optional_comment_char)) then
comment_char=optional_comment_char
else
comment_char="#"
endif
allocate(read_data(number_of_raws,max_length))
print *,"reading data from ",data_file
open(newunit(u), file=data_file)
row=0
do while (row < max_length)
read (u,'(A)',iostat=stat) line_from_file
if ( stat > 0 ) then
stop 'An error occured while reading file'
elseif ( stat < 0 ) then
number_data_points = row
print *, 'EOF of ',data_file,' reached. Found a total of ', number_data_points, 'rows.'
exit
endif
if ( index(line_from_file, comment_char) == 0 ) then
row=row + 1
read (line_from_file, *) read_data(:,row)
endif
end do
if ( .not. stat < 0 ) then
number_data_points = row
print *, 'EOF of ',data_file,' NOT reached. Read a total of ', number_data_points, 'rows.'
endif
close(u)
allocate(array_to_fill(number_of_raws,number_data_points))
array_to_fill=read_data(:,:number_data_points)
end subroutine read_data_files
subroutine logspace(E,Emin,Emax)
real(dp) E(:)
real(dp) Emin,Emax
integer i
E=[(Emin*exp(log(Emax/Emin)*(i-1._dp)/(size(E)-1._dp)),i=1,size(E))]
return
end subroutine logspace
function logspace_f(Ar,N,Amin,Amax) result(A_out)
real(dp), optional :: Ar(:)
integer, optional :: N
real(dp) Amin,Amax
real(dp), dimension(:), allocatable :: A_out
integer N_size,i
if ( .not. (present(Ar) .or. present(N)) ) stop "logspace_f error"
if (present(Ar)) then
N_size=size(Ar)
else
N_size=N
endif
allocate(A_out(N_size))
A_out=[(Amin*exp(log(Amax/Amin)*(i-1._dp)/(N_size-1._dp)),i=1,N_size)]
return
end function logspace_f
subroutine spline_coeficients(x,x_array,y_array,a,b,c,middle_array_index) ! this is not a spline, the overall line can contain breaks, which might be reflected in the subroutine
! calculating distribution evolution
! x_array -- array of variable values
! y_array -- array of function values
! x -- variable value
! middle_array_index -- the previous to x node of array
! a,b,c quadratic spline coefficients
implicit none
real(dp), intent(in) :: x,x_array(:),y_array(:)
real(dp), intent(out) :: a,b,c !constant, linear, and quadratic spline coefficints
integer, intent(out) :: middle_array_index !location in the array
integer n1,n2,n3 ! for quadratic spline
if( size(x_array) /= size(y_array) .or. size(x_array) < 2 ) stop "Incorrect size of x,y arrays in spline_coeficients"
if ( x < x_array(1) .or. x>x_array(size(x_array)) ) then
write(*,*) x,x_array(1)/x,x_array(size(x_array))/x,"from 'spline_coeffients'"
stop "x is out of range in spline_coeficients"
endif
select case (size(x_array))
case (2)
if ( x_array(1) == x_array(2) ) stop "Incorrect x-array in spline_coeficients"
a=(y_array(1)*x_array(2)-y_array(2)*x_array(1))/(x_array(2)-x_array(1))
b=(y_array(2)-y_array(1))/(x_array(2)-x_array(1))
c=0._dp
middle_array_index=1
case default
n1=1
do while (x >= x_array(n1+1) ) ! x should be close to the central point
n1=n1+1
enddo
if (n1==1 .or. &
&( x > sqrt(x_array(n1)*x_array(n1+1)) .and. n1 < size(x_array)-1 ) ) then
n2=n1+1
n3=n1+2
else
n2=n1+1
n3=n1-1
endif
a=&
&y_array(n1)*x_array(n2)*x_array(n3)/(x_array(n1)-x_array(n2))/(x_array(n1)-x_array(n3))+&
&y_array(n2)*x_array(n1)*x_array(n3)/(x_array(n2)-x_array(n1))/(x_array(n2)-x_array(n3))+&
&y_array(n3)*x_array(n2)*x_array(n1)/(x_array(n3)-x_array(n2))/(x_array(n3)-x_array(n1))
b=-1._dp*(&
&y_array(n1)*(x_array(n2)+x_array(n3))/(x_array(n1)-x_array(n2))/(x_array(n1)-x_array(n3))+&
&y_array(n2)*(x_array(n1)+x_array(n3))/(x_array(n2)-x_array(n1))/(x_array(n2)-x_array(n3))+&
&y_array(n3)*(x_array(n2)+x_array(n1))/(x_array(n3)-x_array(n2))/(x_array(n3)-x_array(n1)))
c=&
&y_array(n1)/(x_array(n1)-x_array(n2))/(x_array(n1)-x_array(n3))+&
&y_array(n2)/(x_array(n2)-x_array(n1))/(x_array(n2)-x_array(n3))+&
&y_array(n3)/(x_array(n3)-x_array(n2))/(x_array(n3)-x_array(n1))
middle_array_index=n1
end select
return
end subroutine spline_coeficients
!! Array manipulation
! ! needed for PGI fortran only requires additionally -Kieee
! ! logical function isnan(a) result(check)
! ! real(dp), intent(in) :: a
! ! logical :: check
! ! if (a.ne.a) then
! ! check = .true.
! ! else
! ! check = .false.
! ! endif
! ! return
! ! end function isnan
! subroutine logspace_middle_point(E,Emin,Emax)
! real(dp) E(:)
! real(dp) Emin,Emax
! integer i
! E=[(Emin*exp(log(Emax/Emin)*(i-0.5_dp)/size(E)),i=1,size(E))]
! return
! end subroutine logspace_middle_point
! function logspace_function(Emin,Emax,N) result(E)
! integer, intent(in) :: N
! real(dp), intent(in) :: Emin,Emax
! real(dp) :: E(N)
! integer i
! E=[(Emin*exp(log(Emax/Emin)*(i-1._dp)/(N-1._dp)),i=1,N)]
! return
! end function logspace_function
! function logspace_middle_point_function(Emin,Emax,N) result(E)
! integer, intent(in) :: N
! real(dp), intent(in) :: Emin,Emax
! real(dp) :: E(N)
! integer i
! E=[(Emin*exp(log(Emax/Emin)*(i-0.5_dp)/N),i=1,N)]
! return
! end function logspace_middle_point_function
! function linspace_function(Emin,Emax,N) result(E)
! integer, intent(in) :: N
! real(dp), intent(in) :: Emin,Emax
! real(dp) :: E(N)
! integer i
! E=[(Emin+(Emax-Emin)*(i-1._dp)/(N-1._dp), i=1,N)]
! return
! end function linspace_function
! function Heaviside_function_ar(A) result(B)
! implicit none
! real(dp), intent(in) :: A(:)
! real(dp) :: B(size(A))
! integer :: i
! B=0.e0_dp
! do i=1,size(A)
! if ( A(i) > 0.e0_dp ) B(i)=1.e0_dp
! enddo
! return
! end function Heaviside_function_ar
! function Heaviside_function_non_ar(A) result(B)
! implicit none
! real(dp), intent(in) :: A
! real(dp) :: B
! B=0.e0_dp
! if ( A > 0.e0_dp ) B=1.e0_dp
! return
! end function Heaviside_function_non_ar
! subroutine array_merge(A,B,C,Range)
! !subroutine declaration variables
! real(dp), intent(in) :: A(:,:),B(:,:)
! real(dp), intent(in out) :: C(:,:)
! integer, optional :: Range !1 means C(1,:) is defined, 2 means C(1,1) and C(1,-1) are defined the rest is to be log-spaced, others log-spacing other entire interval
! !internal variables
! integer Na,Nb,Nc
! integer :: ir,i,j,k
! real(dp) :: Emin,Emax,E
! if (size(A(:,1)) /= 2 .or. size(B(:,1)) /= 2 .or. size(C(:,1)) /= 2) stop "Incorrect size of 'A' or 'B' or 'C' &
! &in array merge subroutine"
! Na=size(A(1,:))
! Nb=size(B(1,:))
! Nc=size(C(1,:))
! ! energy interval selection
! if (present(Range)) then
! ir=Range
! else
! ir=0
! endif
! select case (ir)
! case (1)
! case (2) !boundary values are defined
! Emin=C(1,1) !
! Emax=C(1,Nc) !
! call logspace(C(1,:),Emin,Emax)
! case default
! Emin=min(A(1,1),B(1,1))!the lower end
! Emax=max(A(1,Na),B(1,Nb))!the higher end
! call logspace(C(1,:),Emin,Emax)
! end select
! ! end of energy interval selection
! do i=1,Nc
! E=C(1,i)
! C(2,i)=0.d0
! if (E>=A(1,1).and.E<A(1,Na)) then
! j=1
! do while (E>A(1,j+1))
! j=j+1
! enddo
! C(2,i)=A(2,j)+(E-A(1,j))/(A(1,j+1)-A(1,j))*(A(2,j+1)-A(2,j))
! endif
! if (E>=B(1,1).and.E<B(1,Nb)) then
! j=1
! do while (E>B(1,j+1))
! j=j+1
! enddo
! C(2,i)=C(2,i)+B(2,j)+(E-B(1,j))/(B(1,j+1)-B(1,j))*(B(2,j+1)-B(2,j))
! endif
! enddo
! return
! end subroutine array_merge
! subroutine chi_squar(y_data,dy_data,y,chi2,norm,x_data,x,log_interpolation)
! implicit none
! real(dp), intent(in) :: y_data(:),dy_data(:),y(:)
! real(dp), intent(inout) :: norm
! real(dp), intent(out) :: chi2
! real(dp), intent(in), optional :: x_data(:),x(:)
! logical, intent(in), optional :: log_interpolation
! real(dp), dimension(size(y_data)) :: y0,err,y0_data,dy0_data
! logical :: log_int
! integer :: k
! real(dp) :: sum1,sum2
! if (present(log_interpolation)) then
! log_int=log_interpolation
! else
! log_int=.true.
! endif
! if ( present(x) .and. present(x_data) ) then
! if (size(y_data) /= size(x_data) ) stop "inconsistent size of arrays in chi_squar: y_data and x_data"
! if (size(x) /= size(y) ) stop "inconsistent size of arrays in chi_squar: y and x"
! if ( log_int ) then
! !y0 = exp( (/ (polynomial_interpolotation(x_data(k),x,log(y),inter_power=2,error=err(k)),k=1,size(x_data) ) /) )
! y0 = exp( (/ (simple_approximation(x_data(k),x,log(y)),k=1,size(x_data) ) /) )
! else
! !y0 = (/ (polynomial_interpolotation(x_data(k),x,y,inter_power=2,error=err(k)),k=1,size(x_data) ) /)
! y0 = (/ (simple_approximation(x_data(k),x,y),k=1,size(x_data) ) /)
! endif
! ! print *,y0 / x_data**2,err
! else
! if (size(y_data) /= size(y) ) stop "inconsistent size of arrays in chi_squar: y_data and y"
! y0=y
! endif
! where ( dy_data == 0 )
! y0 = 0
! y0_data = 0
! dy0_data = 1.
! elsewhere
! dy0_data = dy_data
! y0_data = y_data
! endwhere
! ! do k=1,size(dy_data)
! ! if (dy_data(k) /= dy0_data(k) ) then
! ! print *,"some points excluded"
! ! end if
! ! enddo
! sum1=sum(y0**2 / dy0_data ** 2 )
! sum2=sum(y0*y0_data / dy0_data ** 2 )
! if ( norm == 0 ) then
! norm = sum2 / sum1
! endif
! chi2 = sum( (y0_data - y0*norm) ** 2 / dy0_data ** 2 )
! end subroutine chi_squar
! subroutine powerlaw_fit(x,y,norm,index,x0,x1)
! ! y=norm * x ** (-index)
! ! Numerical recipes p. 780
! implicit none
! real(dp), intent(in) :: x(:),y(:)
! real(dp), intent(out) :: norm,index
! real(dp), intent(in), optional :: x0,x1
! real(dp) :: e1,e2
! integer :: i1,i2,n
! real(dp), dimension(:), allocatable :: e0,d0
! real(dp) :: S,Sx,Sy,Sxx,Syy,Sxy,Delta
! if (size(x) /= size(y) .or. size(x) == 1 ) stop "inconsistent size of arrays in powerlaw_fit"
! if (present(x0)) then
! i1=value_index(x,x0)
! else
! i1=1
! endif
! if (present(x1)) then
! i2=value_index(x,x1)
! else
! i2=size(x)
! end if
! if ( i1 > i2 ) then
! i1=i1+i2
! i2=i1-i2
! i1=i1-i2
! end if
! if (i1 > 1) i1=i1-1
! if (i2 == i1 .and. i2 < size(x) ) i2=i2+1
! if ( (i2-i1) == 1 ) then
! index=-log(y(i2)/y(i1))/log(x(i2)/x(i1))
! norm=y(i2) * x(i2) ** index
! else
! n=i2-i1+1
! allocate(e0(n))
! allocate(d0(n))
! e0=log(x(i1:i2))
! d0=log(y(i1:i2))
! ! d0=log(energy(i1:i2) ** -1.3 * exp(-energy(i1:i2)/energy(i2)))
! S=n
! Sx=sum(e0)
! Sy=sum(d0)
! Sxx=sum(e0 ** 2)
! Syy=sum(d0 ** 2)
! Sxy=sum(e0 * d0)
! Delta=S * Sxx - Sx ** 2
! norm=exp((Sxx * Sy - Sx * Sxy) / Delta)
! index=(-S * Sxy + Sx * Sy) / Delta
! !fit(3)=sqrt(See/Delta) ! here is not clear
! !fit(4)=sqrt(S/Delta)
! endif
! end subroutine powerlaw_fit
! function array_has_nan_element1D(A) result(check)
! real(dp), intent(in) :: A(:)
! logical check
! integer :: k
! check=.false.
! do k=1,size(A)
! if(isnan(A(k))) check=.true.
! enddo
! return
! end function array_has_nan_element1D
! function array_has_nan_element2D(A) result(check)
! real(dp), intent(in) :: A(:,:)
! logical check
! integer :: k,i
! check=.false.
! do k=1,size(A(1,:))
! do i=1,size(A(:,1))
! if(isnan(A(i,k))) check=.true.
! enddo
! enddo
! return
! end function array_has_nan_element2D
! function array_has_nan_element3D(A) result(check)
! real(dp), intent(in) :: A(:,:,:)
! logical check
! integer :: k,i,j
! check=.false.
! do k=1,size(A(1,1,:))
! do i=1,size(A(1,:,1))
! do j=1,size(A(:,1,1))
! if(isnan(A(j,i,k))) check=.true.
! enddo
! enddo
! enddo
! return
! end function array_has_nan_element3D
! logical function isinf(a)
! real(dp), intent(in) :: a
! if ((a*0).ne.0) then
! isinf = .true.
! else
! isinf = .false.
! end if
! return
! end function isinf
! function array_compare1D(A,B) result(check)
! real(dp), intent(in) :: A(:),B(:)
! logical check
! integer :: k
! check=.false.
! if (size(A) == size(B)) then
! check=.true.
! do k=1,size(A)
! if(A(k) /= B(k) ) check=.false.
! enddo
! endif
! return
! end function array_compare1D
! function array_compare1D0(A,B) result(check)
! real(dp), intent(in) :: A(:),B
! logical check
! integer :: k
! check=.true.
! do k=1,size(A)
! if(A(k) /= B ) check=.false.
! enddo
! return
! end function array_compare1D0
! function array_compare2D(A,B) result(check)
! real(dp), intent(in) :: A(:,:),B(:,:)
! logical check
! integer :: k,i
! check=.false.
! if (size(A,1) == size(B,1) .and. size(A,2) == size(B,2) ) then
! check=.true.
! do k=1,size(A(1,:))
! do i=1,size(A(:,1))
! if(A(i,k) /= B(i,k) ) check=.false.
! enddo
! enddo
! endif
! return
! end function array_compare2D
! function array_compare2D0(A,B) result(check)
! real(dp), intent(in) :: A(:,:),B
! logical check
! integer :: k,i
! check=.true.
! do k=1,size(A(1,:))
! do i=1,size(A(:,1))
! if(A(i,k) /= B ) check=.false.
! enddo
! enddo
! return
! end function array_compare2D0
! function polynomial_interpolotation(x,x_array,y_array,inter_power,error) result(y)
! ! needs a careful check...
! ! NR 3rd edition p 118
! ! Neville's algorithm
! implicit none
! real(dp), intent(in) :: x,x_array(:),y_array(:)
! real(dp), intent(out), optional :: error
! integer, intent(in), optional :: inter_power
! real(dp) :: y
! real(dp) :: dy,diff
! real(dp), dimension(:), allocatable :: c,d,xi,yi
! integer :: n,ns,i,j
! if( size(x_array) /= size(y_array) ) stop "Incorrect size of x,y arrays in polynomial_interpolation"
! ! select the closes point
! diff=abs(x-x_array(1))
! ns=1
! do i=2,size(x_array)
! if ( diff > abs(x-x_array(i)) ) then
! diff = abs(x-x_array(i))
! ns = i
! endif
! enddo
! if (present(inter_power) ) then
! n=min(inter_power,size(x_array))
! else
! n=size(x_array)
! endif
! allocate(xi(n),yi(n),c(n),d(n))
! !print *,"n=",n
! range_define: block
! integer :: i_shift,i_start,i_stop,ns_tmp
! i_shift=Heaviside_function(x-x_array(ns))
! i_start=ns-n/2 + (1-2*(n/2.-n/2))*((i_shift+1)/2)
! i_stop=ns+n/2 + (1-2*(n/2.-n/2))*((i_shift-2)/2)
! ns_tmp= (n+1)/2 + (1-2*(n/2.-n/2))*((2-i_shift)/2.)
! if ( i_start < 1 ) then
! i_start = 1
! i_stop = n
! ns_tmp=ns
! endif
! if (i_stop > size(x_array)) then
! i_start = size(x_array)-n+1
! i_stop = size(x_array)
! ns_tmp=ns+n-size(x_array)
! endif
! xi=x_array(i_start:i_stop)
! yi=y_array(i_start:i_stop)
! ns=ns_tmp
! ! print *,"star at ",i_start,"stop at ",i_stop,"closer point ",ns,"array of x: ",(x-xi)
! ! seems correct... oct 2017
! end block range_define
! ! neville: block ! this is consisten with my note, but probably there is some typo
! ! c=yi
! ! do i=2,n
! ! do j=1,n-i+1
! ! d(j)=((x-xi(j+i-1))*c(j)+(xi(j)-x)*c(j+1))/(xi(j)-xi(j+i-1))
! ! enddo
! ! c=d
! ! enddo
! ! y=c(1)
! ! end block neville
! nevile: block ! this is block that tracks error, consisten with my note
! real(dp) :: ho, hp, den, w
! integer :: k,l
! y=yi(ns)
! c=yi
! d=yi
! do k=1,n-1
! do l=1,n-k
! w=(c(l+1)-d(l))
! den=xi(k+l)-xi(l)
! ho=(x-xi(l))
! hp=(x-xi(k+l))
! if ( den == 0. ) then
! stop "error in nevile2"
! endif
! den= w / den
! c(l)=ho*den
! d(l)=hp*den
! enddo
! if ( 2*ns <= n-l ) then
! dy=c(ns)
! else
! ns=ns-1
! dy=d(ns)
! endif
! y=y+dy
! enddo
! if (present(error) ) then
! if ( n /= 1) then
! error=abs(dy)
! else
! error=abs(y)
! endif
! endif
! end block nevile
! end function polynomial_interpolotation
! function array_interpolation(x,x_array,y_array,debug) result(y) ! quadratic interpolation
! implicit none
! real(dp), intent(in) :: x,x_array(:),y_array(:)
! real(dp) :: y
! logical, optional :: debug
! integer n1,n2,n3 ! for quadratic spline
! if( size(x_array) /= size(y_array) .or. size(x_array) < 2 ) stop "Incorrect size of x,y arrays in array_interpolation"
! if ( x < x_array(1) .or. x>x_array(size(x_array)) ) stop "x is out of range in array_interpolation"
! select case (size(x_array))
! case (2)
! if ( x_array(1) == x_array(2) ) stop "Incorrect x-array in array_interpolation"
! y=y_array(1)+(y_array(2)-y_array(1))*(x-x_array(1))/(x_array(2)-x_array(1))
! case default
! n1=1
! do while (x >= x_array(n1+1) )
! n1=n1+1
! enddo
! if (n1 > 1 ) then
! n2=n1+1
! n3=n1-1
! else
! n2=n1+1 ! why these points exist???? XXXXX
! n3=n1+2
! endif
! ! this can be improved NR p 118
! if (present(debug) ) then
! print *,"ineterpolation", x,x_array(n1),x_array(n2),x_array(n3),y_array(n1),y_array(n2),y_array(n3)
! endif
! y=y_array(n1)*(x-x_array(n2))*(x-x_array(n3))/(x_array(n1)-x_array(n2))/(x_array(n1)-x_array(n3)) + &
! &y_array(n2)*(x-x_array(n3))*(x-x_array(n1))/(x_array(n2)-x_array(n1))/(x_array(n2)-x_array(n3)) + &
! &y_array(n3)*(x-x_array(n1))*(x-x_array(n2))/(x_array(n3)-x_array(n2))/(x_array(n3)-x_array(n1))
! end select
! return
! end function array_interpolation
! function array_interpolation_positive(x,x_array,y_array,debug) result(y) ! quadratic interpolation
! implicit none
! real(dp), intent(in) :: x,x_array(:),y_array(:)
! real(dp) :: ln_x,ln_x_array(size(x_array)),ln_y_array(size(x_array))
! real(dp) :: y,ln_y
! logical, optional :: debug
! integer n1,n2
! if( size(x_array) /= size(y_array) .or. size(x_array) < 2 ) stop "Incorrect size of x,y arrays in array_interpolation_pos"
! if ( x < x_array(1) .or. x > x_array(size(x_array)) ) stop "x is out of range in array_interpolation_pos"
! ln_x = log(x)
! ln_x_array = log(x_array)
! ln_y_array = log(y_array)
! n1 = nearest_element_scalar(ln_x_array, ln_x, -1, .true.)
! n2 = n1 + 1
! if ( x_array(n1) == x_array(n2) ) stop "Incorrect x-array in array_interpolation_pos"
! ln_y = ln_y_array(n1) + &
! & ( ln_y_array(n2) - ln_y_array(n1) ) * &
! & ( ln_x - ln_x_array(n1) ) / (ln_x_array(n2)-ln_x_array(n1))
! y = exp(ln_y)
! return
! end function array_interpolation_positive
! function array_linear_interpolation1D(x,x_array,y_array) result(y)
! implicit none
! real(dp), intent(in) :: x,x_array(:),y_array(:)
! real(dp) :: y
! integer :: i
! if( size(x_array) /= size(y_array) .or. size(x_array) < 2 ) stop "Incorrect size of x,y arrays in array_interpolation"
! if ( x < x_array(1) .or. x>x_array(size(x_array)) ) stop "x is out of range in array_interpolation"
! i=value_index(x_array,x)
! if ( i == 1 ) i=2
! y=y_array(i-1)+(x-x_array(i-1))/(x_array(i)-x_array(i-1))*(y_array(i)-y_array(i-1))
! return
! end function array_linear_interpolation1D
! function array_linear_interpolation2D(x,y,x_array,y_array,array) result(xy_value)
! implicit none
! real(dp), intent(in) :: x,y,x_array(:),y_array(:),&
! &array(size(x_array),size(y_array))
! real(dp) :: xy_value
! integer :: i,j
! real(dp) :: u,t
! i=value_index(x_array,x)
! j=value_index(y_array,y)
! if ( i == 1 ) i=2
! if ( j == 1 ) j=2
! u=(x-x_array(i-1))/(x_array(i)-x_array(i-1))
! t=(y-y_array(j-1))/(y_array(j)-y_array(j-1))
! xy_value=&
! &(1._dp-u)*(1._dp-t)*array(i-1,j-1)+&
! &u*(1._dp-t)*array(i,j-1)+&
! &(1._dp-u)*t*array(i-1,j)+&
! &u*t*array(i,j)
! return
! end function array_linear_interpolation2D
! ! function array_linear_interpolation2D(x,x_array,y_array) result(y)
! ! implicit none
! ! real(dp), intent(in) :: x(2),x_array(:,:),y_array(:,:)
! ! real(dp) :: y
! ! integer :: i
! ! if( size(x_array,1) /= size(y_array,1) .or.&
! ! & size(x_array,2) /= size(y_array,2) .or.&
! ! & size(x_array,1) < 2 .or. &
! ! & size(x_array,2) < 2) &
! ! & stop "Incorrect size of x,y arrays in array_interpolation"
! ! if ( x(1) < x_array(1,1) .or. x>x_array(size(x_array)) ) stop "x is out of range in array_interpolation"
! ! i=value_index(x_array,x)
! ! y=y_array(i)+(x-x_array(i))/(x_array(i+1)-x_array(i))*(y_array(i+1)-y_array(i))
! ! return
! ! end function array_linear_interpolation2D
! subroutine spline_coeficients_v2(x,x_array,y_array,a,b,c,middle_array_index) ! diff to other: using real spline ! very time inefficient: consider using save atribute
! ! doesn't solve the problem as compared to the fake spline (give IDENTICAL spectra)
! ! x_array -- array of variable values
! ! y_array -- array of function values
! ! x -- variable value
! ! middle_array_index -- the previous to x node of array
! ! a,b,c quadratic spline coefficients
! implicit none
! real(dp), intent(in) :: x,x_array(:),y_array(:)
! real(dp), intent(out) :: a,b,c !constant, linear, and quadratic spline coefficints
! integer, intent(out) :: middle_array_index !location in the array
! real(dp) :: spline(3,size(x_array)-1)
! integer n1
! if( size(x_array) /= size(y_array) .or. size(x_array) < 2 ) stop "Incorrect size of x,y arrays in splice_coeficients v2"
! if ( x < x_array(1) .or. x>x_array(size(x_array)) ) stop "x is out of range in splice_coeficients v2"
! select case (size(x_array))
! case (2)
! if ( x_array(1) == x_array(2) ) stop "Incorrect x-array in splice_coeficients v2"
! a=(y_array(1)*x_array(2)-y_array(2)*x_array(1))/(x_array(2)-x_array(1))
! b=(y_array(2)-y_array(1))/(x_array(2)-x_array(1))
! c=0._dp
! middle_array_index=1
! case default
! spline=spline_coeficient_array(x_array,y_array)
! n1=1
! do while (x >= x_array(n1+1) .and. n1 < size(x_array)-2 )
! n1=n1+1
! enddo
! a=spline(1,n1)
! b=spline(2,n1)
! c=spline(3,n1)
! end select
! middle_array_index=n1
! return
! end subroutine spline_coeficients_v2
! function spline_coeficient_array(x_array,y_array) result(spline)
! implicit none
! real(dp), intent(in) :: x_array(:),y_array(:)
! real(dp) :: spline(3,size(x_array)-1)
! integer :: i
! real(dp) :: tmp1,tmp2
! if (size(x_array) < 3 .or. size(x_array) /=size(y_array) ) &
! &stop "size of arrays in not sufficient for spline extrapolation spline_coeficient_array"
! spline(1,1)=&
! &y_array(1)*x_array(2)*x_array(3)/(x_array(1)-x_array(2))/(x_array(1)-x_array(3))+&
! &y_array(2)*x_array(1)*x_array(3)/(x_array(2)-x_array(1))/(x_array(2)-x_array(3))+&