-
Notifications
You must be signed in to change notification settings - Fork 0
/
ynogk.f90
7438 lines (7277 loc) · 348 KB
/
ynogk.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 constants
!********************************************************************
!* This module defines many constants often uesd in our code.
!* One can use these constants through a command "use constants" in their
!* own subroutines or functions.
!********************************************************************
implicit none
Double precision infinity,pi,dtors,sixteen,twopi,zero,one,two,three,four,six,half,&
half2,mh,hbar,pho_v,plankc,five,dtor,eight
parameter(infinity=1.D40,dtors=asin(1.D0)*2.D0/180.D0, &
sixteen=16.D0, twopi=4.D0*dasin(1.D0), pi = dasin(1.D0)*2.D0)!3.141592653589793D0
PARAMETER(zero=0.D0, one=1.D0, two=2.D0, three=3.D0, four=4.D0, six=6.D0, half=0.5D0, half2=0.25D0, &
mh=1.6726231D-24, hbar = 1.0545887D-27, plankc=6.626178D-27, pho_v=2.99792458D10, five=5.D0,&
dtor=asin(1.D0)*2.D0/180.D0, eight=8.D0)
!********************************************************************************************
end module constants
!********************************************************************************************
!********************************************************************************************
module rootsfinding
!********************************************************************
!* This module aim on solve cubic and quartic polynomial equations.
!* One can use these subroutine root3 and root4 to find roots of cubic and
!* quartic equations respectively.
!********************************************************************
use constants
implicit none
contains
!*********************************************************************************************
subroutine root3(b,c,d,r1,r2,r3,del)
!********************************************************************
!* PURPOSE: This subroutine aim on solving cubic equations: x^3+b*x^2+c*x+d=0.
!* INPUTS: b, c, d-----they are the coefficients of equation.
!* OUTPUTS: r1,r2,r3----roots of the equation, with complex number form.
!* del---------the number of real roots among r1,r2,r3.
!* ROUTINES CALLED: sort
!* This code comes from internet.
!********************************************************************
implicit none
Double precision a,b,c,d,p,q,delta,DD,e1,e2,e3,realp,imagep,temp1,temp2,phi,y1,y2,&
y3,y2r,y2i,u,v
complex*16 r1,r2,r3
integer del
a=1.D0
! Step 1: Calculate p and q --------------------------------------------
p = c/a - b*b/a/a/3.D0
q = (two*b*b*b/a/a/a - 9.D0*b*c/a/a + 27.D0*d/a) / 27.D0
! Step 2: Calculate DD (discriminant) ----------------------------------
DD = p*p*p/27.D0 + q*q/4.D0
! Step 3: Branch to different algorithms based on DD -------------------
if(DD .lt. 0.D0)then
! Step 3b:
! 3 real unequal roots -- use the trigonometric formulation
phi = acos(-q/two/sqrt(abs(p*p*p)/27.D0))
temp1=two*sqrt(abs(p)/3.D0)
y1 = temp1*cos(phi/3.D0)
y2 = -temp1*cos((phi+pi)/3.D0)
y3 = -temp1*cos((phi-pi)/3.D0)
else
! Step 3a:
! 1 real root & 2 conjugate complex roots OR 3 real roots (some are equal)
temp1 = -q/two + sqrt(DD)
temp2 = -q/two - sqrt(DD)
u = abs(temp1)**(1.D0/3.D0)
v = abs(temp2)**(1.D0/3.D0)
if(temp1 .lt. 0.D0) u=-u
if(temp2 .lt. 0.D0) v=-v
y1 = u + v
y2r = -(u+v)/two
y2i = (u-v)*sqrt(3.D0)/two
endif
! Step 4: Final transformation -----------------------------------------
temp1 = b/a/3.D0
y1 = y1-temp1
y2 = y2-temp1
y3 = y3-temp1
y2r=y2r-temp1
! Assign answers -------------------------------------------------------
if(DD .lt. 0.D0)then
call sort(y1,y2,y3,y1,y2,y3)
r1 = dcmplx( y1, 0.D0)
r2 = dcmplx( y2, 0.D0)
r3 = dcmplx( y3, 0.D0)
del=3
elseif(DD .eq. 0.D0)then
call sort(y1,y2r,y2r,y1,y2r,y2r)
r1 = dcmplx( y1, 0.D0)
r2 = dcmplx(y2r, 0.D0)
r3 = dcmplx(y2r, 0.D0)
del=3
else
r1 = dcmplx( y1, 0.D0)
r2 = dcmplx(y2r, y2i)
r3 = dcmplx(y2r,-y2i)
del=1
endif
return
end subroutine root3
!*********************************************************************************************
subroutine root4(b,c,d,e,r1,r2,r3,r4,reals)
!*********************************************************************************************
!* PURPOSE: This subroutine aim on solving quartic equations: x^4+b*x^3+c*x^2+d*x+e=0.
!* INPUTS: b, c, d, e-----they are the coefficients of equation.
!* OUTPUTS: r1,r2,r3,r4----roots of the equation, with complex number form.
!* reals------------the number of real roots among r1,r2,r3,r4.
!* ROUTINES CALLED: root3
!* AUTHOR: Yang, Xiao-lin & Wang, Jian-cheng (2012)
!* DATE WRITTEN: 1 Jan 2012
!********************************************************************
implicit none
Double precision b,c,d,e,q,r,s,realp,imagep,two
parameter(two=2.D0)
complex*16 r1,r2,r3,r4,s1,s2,s3,temp(1:4),temp1
integer i,j,del,reals
reals=0
q=c-3.D0*b**2/8.D0
r=d-b*c/two+b**3/8.D0
s=e-b*d/4.D0+b**2*c/16.D0-3.D0*b**4/256.D0
call root3(two*q,q**2-4.D0*s,-r**2,s1,s2,s3,del)
If(del.eq.3)then
If(real(s3).ge.0.D0)then
reals=4
s1=dcmplx(real(sqrt(s1)),0.D0)
s2=dcmplx(real(sqrt(s2)),0.D0)
s3=dcmplx(real(sqrt(s3)),0.D0)
else
reals=0
s1=sqrt(s1)
s2=sqrt(s2)
s3=sqrt(s3)
endif
else
If(real(s1).ge.0.D0)then
reals=2
s1=dcmplx(real(sqrt(s1)),0.D0)
s2=sqrt(s2)
s3=dcmplx(real(s2),-aimag(s2))
else
reals=0
s1=sqrt(s1)
s2=sqrt(s2)
s3=sqrt(s3)
endif
endif
if(real(s1*s2*s3)*(-r) .lt. 0.D0)then
s1=-s1
end if
temp(1)=(s1+s2+s3)/two-b/4.D0
temp(2)=(s1-s2-s3)/two-b/4.D0
temp(3)=(s2-s1-s3)/two-b/4.D0
temp(4)=(s3-s2-s1)/two-b/4.D0
Do i=1,4
Do j=1+i,4
If(real(temp(i)).gt.real(temp(j)))then
temp1=temp(i)
temp(i)=temp(j)
temp(j)=temp1
endif
enddo
enddo
r1=temp(1)
r2=temp(2)
r3=temp(3)
r4=temp(4)
return
end subroutine root4
!*********************************************************************************************
subroutine sort(a1,a2,a3,s1,s2,s3)
!********************************************************************
!* PURPOSE: This subroutine aim on sorting a1, a2, a3 by decreasing way.
!* INPUTS: a1,a2,a3----they are the number list required to bo sorted.
!* OUTPUTS: s1,s2,s3----sorted number list with decreasing way.
!*
!* AUTHOR: Yang, Xiao-lin & Wang, Jian-cheng (2012)
!* DATE WRITTEN: 1 Jan 2012
!********************************************************************
implicit none
Double precision s1,s2,s3,s4,temp,arr(1:3),a1,a2,a3
integer i,j
arr(1)=a1
arr(2)=a2
arr(3)=a3
Do i=1,3
Do j=i+1,3
If(arr(i)<arr(j))then
temp=arr(i)
arr(i)=arr(j)
arr(j)=temp
end if
end do
end do
s1=arr(1)
s2=arr(2)
s3=arr(3)
end subroutine sort
end module rootsfinding
!*******************************************************************************
module ellfunction
!********************************************************************
!* PURPOSE: This module includes supporting functions and subroutines to compute
!* Weierstrass' and Jacobi's elliptical integrals and functions by Carlson's
!* integral method. Those codes mainly come from Press (2007) and geokerr.f of
!* Dexter & Agol (2009).
!* AUTHOR: Yang, Xiao-lin & Wang, Jian-cheng (2012)
!* DATE WRITTEN: 1 Jan 2012
!********************************************************************
use constants
use rootsfinding
implicit none
contains
!***************************************************************************
Double precision function weierstrassP(z,g2,g3,r1,del)
!***************************************************************************
!* PURPOSE: to compute Weierstrass' elliptical function \wp(z;g_2,g_3) and all of
!* this function involved are real numbers.
!* INPUTS: z----------the independent variable value.
!* g_2, g_3---two parameters.
!* r1(1:3)----an array which is the roots of equation W(t)=4t^3-g_2t-g_3=0.
!* del--------number of real roots among r1(1:3).
!* RETURN: weierstrassP----the value of function \wp(z;g_2,g_3).
!* ROUTINES CALLED: sncndn
!* AUTHOR: Yang, Xiao-lin & Wang, Jian-cheng (2012)
!* DATE WRITTEN: 1 Jan 2012
!********************************************************************
implicit none
Double precisionz,g2,g3,g1,e1,e2,e3,k2,u,sn,alp,bet,sig,lamb,cn,dn,realp,&
imagep,rfx,rfy,rfz,EK,two,four,zero
parameter(two=2.D0,four=4.D0,zero=0.D0)
complex*16 r1(1:3)
integer i,del
If(z.eq.zero)then
weierstrassP=infinity
else
z=abs(z)
if(del.eq.3)then
e1=real(r1(1))
e2=real(r1(2))
e3=real(r1(3))
k2=(e2-e3)/(e1-e3)
u=z*sqrt(e1-e3)
call sncndn(u,1.D0-k2,sn,cn,dn)
weierstrassP=e3+(e1-e3)/sn**2
else
alp=-real(r1(1))/two
bet=abs(aimag(r1(2)))
sig=(9.D0*alp**2+bet**2)**(one/four)
lamb=(sig**2-three*alp)/bet
k2=0.5D0+1.5D0*alp/sig**2
u=two*sig*z
call sncndn(u,1.D0-k2,sn,cn,dn)
if(cn.gt.zero)then
weierstrassP=two*sig**two*(1.D0+sqrt(1.D0-sn**2))/sn**2-two*alp-sig**2
else
If(abs(sn).gt.1.D-7)then
weierstrassP=two*sig**two*(1.D0-sqrt(1.D0-sn**2))/sn**2-two*alp-sig**2
else
weierstrassP=-two*alp
endif
endif
end if
endif
return
end function weierstrassP
!*************************************************************************************************
Function halfperiodwp(g2,g3,r1,del)
!*************************************************************************************************
!* PURPOSE: to compute the semi period of Weierstrass' elliptical function \wp(z;g_2,g_3) and all of
!* this function involved are real numbers.
!* INPUTS: g_2, g_3---two parameters.
!* r1(1:3)----an array which is the roots of equation W(t)=4t^3-g_2t-g_3=0.
!* del--------number of real roots among r1(1:3).
!* RETURN: halfperiodwp----the semi period of function \wp(z;g_2,g_3).
!* ROUTINES CALLED: rf
!* AUTHOR: Yang, Xiao-lin & Wang, Jian-cheng (2012)
!* DATE WRITTEN: 1 Jan 2012
!********************************************************************
implicit none
Double precision halfperiodwp,g2,g3,g1,e1,e2,e3,zero,one,two,&
three,four,EK,alp,bet,sig,lamb,k2
parameter(zero=0.D0,one=1.D0,two=2.D0,three=3.D0,four=4.D0)
complex*16 r1(3)
integer i,del
if(del.eq.3)then
e1=real(r1(1))
e2=real(r1(2))
e3=real(r1(3))
k2=(e2-e3)/(e1-e3)
EK=rf(zero,one-k2,one)
halfperiodwp=EK/sqrt(e1-e3)
else
alp=-real(r1(1))/two
bet=abs(aimag(r1(2)))
sig=(9*alp**two+bet**two)**(one/four)
k2=one/two+three/two*alp/sig**two
EK=rf(zero,one-k2,one)
halfperiodwp=EK/sig
endif
return
End Function halfperiodwp
!*************************************************************************************************
subroutine sncndn(uu,emmc,sn,cn,dn)
!*************************************************************************************************
!* PURPOSE: Returns the Jacobian elliptic functions sn(u|k^2), cn(u|k^2),
!* and dn(u|k^2). Here uu=u, while emmc=1-k^2.
!* RETURN: sn, cn, dn----Jacobian elliptic functions sn(u|k^2), cn(u|k^2), dn(u|k^2).
!* AUTHOR: Press et al. (2007)
!********************************************************************
implicit none
Double precision uu,emmc,sn,CA
Double precision ,optional :: cn,dn
parameter (CA=3.D0-8)
integer i,ii,l
Double precisiona,b,c,d,emc,u,em(13),en(13)
logical bo
emc=emmc
u=uu
if(emc.ne.0.D0)then
bo=(emc.lt.0.D0)
if(bo)then
d=1.D0-emc !t'=t*k, u'=k*u, k'^2=1./k^2,
emc=-emc/d
d=sqrt(d)
u=d*u
end if
a=1.D0
dn=1.D0
l1: do i=1,13
l=i
em(i)=a
emc=sqrt(emc)
en(i)=emc
c=0.5D0*(a+emc)
if(abs(a-emc).le.CA*a) exit
emc=a*emc
a=c
end do l1
u=c*u
sn=sin(u)
cn=cos(u)
if(sn.eq.0.D0)then
if(bo)then
a=dn
dn=cn
cn=a
sn=sn/d
end if
return
endif
a=cn/sn
c=a*c
l2: do ii=l,1,-1
b=em(ii)
a=c*a
c=dn*c
dn=(en(ii)+a)/(b+a)
a=c/b
enddo l2
a=1.D0/sqrt(c**2+1.D0)
if(sn.lt.0.D0)then
sn=-a
else
sn=a
endif
cn=c*sn
return
else
cn=1.D0/cosh(u)
dn=cn
sn=tanh(u)
return
end if
end subroutine sncndn
!*************************************************************************************************
Double precision FUNCTION rf(x,y,z)
!*************************************************************************************************
!* PURPOSE: Compute Carlson fundamental integral RF
!* R_F=1/2 \int_0^\infty dt (t+x)^(-1/2) (t+y)^(-1/2) (t+z)^(-1/2)
!* ARGUMENTS: Symmetric arguments x,y,z
!* ROUTINES CALLED: None.
!* ALGORITHM: Due to B.C. Carlson.
!* ACCURACY: The parameter ERRTOL sets the desired accuracy.
!* REMARKS:
!* AUTHOR: Press et al (2007).
!* DATE WRITTEN: 25 Mar 91.
!* REVISIONS:
!***********************************************************************
implicit none
Double precision x,y,z,ERRTOL,TINY1,BIG,THIRD,C1,C2,C3,C4,delta,zero
parameter (ERRTOL=0.0025D0,TINY1=1.5D-38,BIG=3.D37,THIRD=1.D0/3.D0,C1=1.D0/24.D0,&
C2=0.1D0,C3=3.D0/44.D0,C4=1.D0/14.D0,zero=0.D0)
Double precision alamb,ave,delx,dely,delz,e2,e3,sqrtx,sqrty,sqrtz,xt,yt,zt
!if(min(x,y,z).lt.0..or.min(x+y,x+z,y+z).lt.TINY1.or.max(x,y,z).gt.BIG)then
! rf=0.D0
! return
!endif
IF(x.lt.zero)x=zero
IF(y.lt.zero)y=zero
IF(z.lt.zero)z=zero
xt=x
yt=y
zt=z
sqrtx=sqrt(xt)
sqrty=sqrt(yt)
sqrtz=sqrt(zt)
alamb=sqrtx*(sqrty+sqrtz)+sqrty*sqrtz
xt=0.25D0*(xt+alamb)
yt=0.25D0*(yt+alamb)
zt=0.25D0*(zt+alamb)
ave=THIRD*(xt+yt+zt)
delx=(ave-xt)/ave
dely=(ave-yt)/ave
delz=(ave-zt)/ave
delta=max(abs(delx),abs(dely),abs(delz))
Do while(delta.gt.ERRTOL)
sqrtx=sqrt(xt)
sqrty=sqrt(yt)
sqrtz=sqrt(zt)
alamb=sqrtx*(sqrty+sqrtz)+sqrty*sqrtz
xt=0.25D0*(xt+alamb)
yt=0.25D0*(yt+alamb)
zt=0.25D0*(zt+alamb)
ave=THIRD*(xt+yt+zt)
delx=(ave-xt)/ave
dely=(ave-yt)/ave
delz=(ave-zt)/ave
delta=max(abs(delx),abs(dely),abs(delz))
enddo
e2=delx*dely-delz**2
e3=delx*dely*delz
rf=(1.D0+(C1*e2-C2-C3*e3)*e2+C4*e3)/sqrt(ave)
return
end Function rf
!************************************************************************
Double precision FUNCTION rj(x,y,z,p)
!************************************************************************
!* PURPOSE: Compute Carlson fundamental integral RJ
!* RJ(x,y,z,p) = 3/2 \int_0^\infty dt
!* (t+x)^(-1/2) (t+y)^(-1/2) (t+z)^(-1/2) (t+p)^(-1)
!* ARGUMENTS: x,y,z,p
!* ROUTINES CALLED: RF, RC.
!* ALGORITHM: Due to B.C. Carlson.
!* ACCURACY: The parameter ERRTOL sets the desired accuracy.
!* REMARKS:
!* AUTHOR: Press et al (1992)
!* DATE WRITTEN: 25 Mar 91.
!* REVISIONS:
!***********************************************************************
implicit none
Double precision x,y,z,p,ERRTOL,TINY1,BIG,C1,C2,C3,C4,C5,C6,C7,C8,delta,zero
Double precision a,alamb,alpha,ave,b,beta,delp,delx,dely,delz,ea,eb,ec,ed,ee,fac,pt,&
rcx,rho,sqrtx,sqrty,sqrtz,sum1,tau,xt,yt,zt
Parameter(ERRTOL=0.0015D0,TINY1=2.5D-13,BIG=9.D11,C1=3.D0/14.D0,&
C2=1.D0/3.D0,C3=3.D0/22.D0,C4=3.D0/26.D0,C5=0.75D0*C3,&
C6=1.5D0*C4,C7=0.5D0*C2,C8=C3+C3,zero=0.D0)
!if(min(x,y,z).lt.0..or.min(x+y,x+z,y+z,abs(p)).lt.TINY1.or.max(x,y,z,abs(p)).gt.BIG)then
! rj=0.D0
! write(*,*)'ffsdfa'
! return
!endif
IF(x.lt.zero)x=zero
IF(y.lt.zero)y=zero
IF(z.lt.zero)z=zero
sum1=0.D0
fac=1.D0
If(p.gt.0.D0)then
xt=x
yt=y
zt=z
pt=p
else
xt=min(x,y,z)
zt=max(x,y,z)
yt=x+y+z-xt-zt
a=1.D0/(yt-p)
b=a*(zt-yt)*(yt-xt)
pt=yt+b
rho=xt*zt/yt
tau=p*pt/yt
rcx=rc(rho,tau)
endif
sqrtx=sqrt(xt)
sqrty=sqrt(yt)
sqrtz=sqrt(zt)
alamb=sqrtx*(sqrty+sqrtz)+sqrty*sqrtz
alpha=(pt*(sqrtx+sqrty+sqrtz)+sqrtx*sqrty*sqrtz)**2
beta=pt*(pt+alamb)**2
sum1=sum1+fac*rc(alpha,beta)
fac=0.25D0*fac
xt=0.25D0*(xt+alamb)
yt=0.25D0*(yt+alamb)
zt=0.25D0*(zt+alamb)
pt=0.25D0*(pt+alamb)
ave=0.2D0*(xt+yt+zt+pt+pt)
delx=(ave-xt)/ave
dely=(ave-yt)/ave
delz=(ave-zt)/ave
delp=(ave-pt)/ave
delta=max(abs(delx),abs(dely),abs(delz),abs(delp))
Do while(delta.gt.ERRTOL)
sqrtx=sqrt(xt)
sqrty=sqrt(yt)
sqrtz=sqrt(zt)
alamb=sqrtx*(sqrty+sqrtz)+sqrty*sqrtz
alpha=(pt*(sqrtx+sqrty+sqrtz)+sqrtx*sqrty*sqrtz)**2
beta=pt*(pt+alamb)**2
sum1=sum1+fac*rc(alpha,beta)
fac=0.25D0*fac
xt=0.25D0*(xt+alamb)
yt=0.25D0*(yt+alamb)
zt=0.25D0*(zt+alamb)
pt=0.25D0*(pt+alamb)
ave=0.2D0*(xt+yt+zt+pt+pt)
delx=(ave-xt)/ave
dely=(ave-yt)/ave
delz=(ave-zt)/ave
delp=(ave-pt)/ave
delta=max(abs(delx),abs(dely),abs(delz),abs(delp))
enddo
ea=delx*(dely+delz)+dely*delz
eb=delx*dely*delz
ec=delp**2
ed=ea-3.D0*ec
ee=eb+2.D0*delp*(ea-ec)
rj=3.D0*sum1+fac*(1.D0+ed*(-C1+C5*ed-C6*ee)+eb*(C7+&
delp*(-C8+delp*C4))+delp*ea*(C2-delp*C3)-&
C2*delp*ec)/(ave*sqrt(ave))
If(p.le.0.D0)rj=a*(b*rj+3.D0*(rcx-rf(xt,yt,zt)))
return
end Function rj
!************************************************************************
FUNCTION rc(x,y)
!************************************************************************
!* PURPOSE: Compute Carlson degenerate integral RC
!* R_C(x,y)=1/2 \int_0^\infty dt (t+x)^(-1/2) (t+y)^(-1)
!* ARGUMENTS: x,y
!* ROUTINES CALLED: None.
!* ALGORITHM: Due to B.C. Carlson.
!* ACCURACY: The parameter ERRTOL sets the desired accuracy.
!* REMARKS:
!* AUTHOR: Press et al (1992)
!* DATE WRITTEN: 25 Mar 91.
!* REVISIONS:
!***********************************************************************
implicit none
Double precision rc,x,y,ERRTOL,TINY1,sqrtNY,BIG,TNBG,COMP1,COMP2,THIRD,C1,C2,C3,C4
PARAMETER(ERRTOL=0.0012D0,TINY1=1.69D-38,sqrtNY=1.3D-19,BIG=3.D37,&
TNBG=TINY1*BIG,COMP1=2.236D0/sqrtNY,COMP2=TNBG*TNBG/25.D0,&
THIRD=1.D0/3.D0,C1=0.3D0,C2=1.D0/7.D0,C3=0.375D0,C4=9.D0/22.D0)
Double precisionalamb,ave,s,w,xt,yt
!if((x.lt.0.D0).or.(y.eq.0.D0).or.((x+abs(y)).lt.TINY1).or.((x+abs(y)).gt.BIG).or.&
! ((y.lt.-COMP1).and.(x.gt.0.D0).and.(x.lt.COMP2)))then
! rc=0.D0
! return
!endif
if(y.gt.0.D0)then
xt=x
yt=y
w=1.D0
else
xt=x-y
yt=-y
w=sqrt(x)/sqrt(xt)
endif
alamb=2.D0*sqrt(xt)*sqrt(yt)+yt
xt=0.25D0*(xt+alamb)
yt=0.25D0*(yt+alamb)
ave=THIRD*(xt+yt+yt)
s=(yt-ave)/ave
Do While(abs(s).gt.ERRTOL)
alamb=2.D0*sqrt(xt)*sqrt(yt)+yt
xt=0.25D0*(xt+alamb)
yt=0.25D0*(yt+alamb)
ave=THIRD*(xt+yt+yt)
s=(yt-ave)/ave
ENDdo
rc=w*(1.D0+s*s*(C1+s*(C2+s*(C3+s*C4))))/sqrt(ave)
return
END FUNCTION rc
!**********************************************************************
FUNCTION rd(x,y,z)
!**********************************************************************
!* PURPOSE: Compute Carlson degenerate integral RD
!* R_D(x,y,z)=3/2 \int_0^\infty dt (t+x)^(-1/2) (t+y)^(-1/2) (t+z)^(-3/2)
!* ARGUMENTS: x,y,z
!* ROUTINES CALLED: None.
!* ALGORITHM: Due to B.C. Carlson.
!* ACCURACY: The parameter ERRTOL sets the desired accuracy.
!* REMARKS:
!* AUTHOR: Press et al (1992)
!* DATE WRITTEN: 25 Mar 91.
!* REVISIONS:
!***********************************************************************
implicit none
Double precision rd,x,y,z,ERRTOL,TINY,BIG,C1,C2,C3,C4,C5,C6,zero
PARAMETER (ERRTOL=0.0015D0,TINY=1.D-25,BIG=4.5D21,C1=3.D0/14.D0,C2=1.D0/6.D0,&
C3=9.D0/22.D0,C4=3.D0/26.D0,C5=0.25D0*C3,C6=1.5D0*C4,zero=0.D0)
Double precision alamb,ave,delx,dely,delz,ea,eb,ec,ed,ee,fac,sqrtx,sqrty,sqrtz,sum,xt,yt,zt
!if(min(x,y).lt.0.D0.or.min(x+y,z).lt.TINY.or. max(x,y,z).gt.BIG)then
! rd=0.D0
! return
!endif
IF(x.lt.zero)x=zero
IF(y.lt.zero)y=zero
xt=x
yt=y
zt=z
sum=0.D0
fac=1.D0
sqrtx=sqrt(xt)
sqrty=sqrt(yt)
sqrtz=sqrt(zt)
alamb=sqrtx*(sqrty+sqrtz)+sqrty*sqrtz
sum=sum+fac/(sqrtz*(zt+alamb))
fac=0.25D0*fac
xt=0.25D0*(xt+alamb)
yt=0.25D0*(yt+alamb)
zt=0.25D0*(zt+alamb)
ave=0.2D0*(xt+yt+3.D0*zt)
delx=(ave-xt)/ave
dely=(ave-yt)/ave
delz=(ave-zt)/ave
DO While(max(abs(delx),abs(dely),abs(delz)).gt.ERRTOL)
sqrtx=sqrt(xt)
sqrty=sqrt(yt)
sqrtz=sqrt(zt)
alamb=sqrtx*(sqrty+sqrtz)+sqrty*sqrtz
sum=sum+fac/(sqrtz*(zt+alamb))
fac=0.25D0*fac
xt=0.25D0*(xt+alamb)
yt=0.25D0*(yt+alamb)
zt=0.25D0*(zt+alamb)
ave=0.2D0*(xt+yt+3.D0*zt)
delx=(ave-xt)/ave
dely=(ave-yt)/ave
delz=(ave-zt)/ave
End DO
ea=delx*dely
eb=delz*delz
ec=ea-eb
ed=ea-6.D0*eb
ee=ed+ec+ec
rd=3.D0*sum+fac*(1.D0+ed*(-C1+C5*ed-C6*delz*ee)+delz*(C2*ee+&
delz*(-C3*ec+delz*C4*ea)))/(ave*sqrt(ave))
return
END function rd
!*******************************************************************
Function EllipticF(t,k2)
!*******************************************************************
!* PURPOSE: calculate Legendre's first kind elliptic integral:
!* F(t,k2)=\int_0^t dt/sqrt{(1-t^2)*(1-k2*t^2)}.
!* ARGUMENTS: t, k2
!* ROUTINES CALLED: RF
!* AUTHOR: Press et al (1992)
!* DATE WRITTEN: 25 Mar 91.
!* REVISIONS:
!***********************************************************************
implicit none
Double precision t,k2,EllipticF,x1,y1,z1
x1=1.D0-t*t
y1=1.D0-k2*t*t
z1=1.D0
!Press et al. 2007 (6.12.19)
EllipticF=t*rf(x1,y1,z1)
return
end function EllipticF
!*********************************************************************
Function EllipticE(t,k2)
!*********************************************************************
!* PURPOSE: calculate Legendre's second kind elliptic integrals:
!* E(t,k2)=\int_0^t sqrt{1-k2*t^2}/sqrt{(1-t^2)}dt.
!* ARGUMENTS: t, k2
!* ROUTINES CALLED: RF, RD
!* AUTHOR: Press et al (1992)
!* DATE WRITTEN: 25 Mar 91.
!* REVISIONS:
!**********************************************************************
implicit none
Double precision t,k2,EllipticE,x1,y1,z1
x1=1.D0-t*T
y1=1.D0-k2*t*t
z1=1.D0
!Press et al. 2007 (6.12.20)
EllipticE=t*rf(x1,y1,z1)-1.D0/3.D0*k2*t**3*rd(x1,y1,z1)
return
end function EllipticE
!***********************************************************************
Function EllipticPI(t,n,k2)
!***********************************************************************
!* PURPOSE: calculate Legendre's third kind elliptic integrals:
!* PI(t,n,k2)=\int_0^t /(1+nt^2)/sqrt{(1-k2*t^2)(1-t^2)}dt.
!* ARGUMENTS: t, k2
!* ROUTINES CALLED: RF, RJ
!* AUTHOR: Press et al (1992)
!* DATE WRITTEN: 25 Mar 91.
!* REVISIONS:
!**********************************************************************
implicit none
Double precision t,k2,EllipticPI,x1,y1,z1,w1,n
x1=1.D0-t*t
y1=1.D0-k2*t*t
z1=1.D0
w1=1.D0+n*t*t
!Press et al. 2007 (6.12.20)
EllipticPI=t*rf(x1,y1,z1)-1.D0/3.D0*n*t*t*t*rj(x1,y1,z1,w1)
return
end function EllipticPI
!*************************************************************************************************
subroutine weierstrass_int_J3(y,x,bb,del,a4,b4,p4,rff_p,integ,cases)
!*************************************************************************************************
!* PURPOSE: Computes integrals: J_k(h)=\int^x_y (b4*t+a4)^(k/2)*(4*t^3-g_2*t-g_3)^(-1/2)dt.
!* Where integer index k can be 0, -2, -4 and 2. (75) and (76) of Yang & Wang (2012).
!* INPUTS: x,y -- limits of integral.
!* bb(1:3) -- Roots of equation 4*t^3-g_2*t-g_3=0 solved by routine root3.
!* del -- Number of real roots in bb(1:3).
!* p4,rff_p,integ -- p4(1:4) is an array which specifies the value of index k of J_k(h).
!* If p4(1)=0, then J_0 was computed and sent to integ(1).
!* If p4(1)!=0, then J_0 was replaced by parameter p, and rff_p=p.
!* If p4(2)=-2, then J_{-2} was computed and sent to integ(2).
!* If p4(3)=2, then J_{2} was computed and sent to integ(3).
!* If p4(4)=-4, then J_{-4} was computed and sent to integ(4).
!* cases -- If cases=1, then only J_0 was computed.
!* If cases=2, then only J_0 and J_{-2} are computed.
!* If cases=3, then only J_0, J_{-2} and J_{2} are computed.
!* If cases=4, then J_0, J_{-2}, J_{2} and J_{-4} are computed.
!* OUTPUTS: integ -- is an array saved the results of J_k(h).
!* ROUTINES CALLED: ellcubicreals, ellcubiccomplexs
!* ACCURACY: Machine.
!* REMARKS: Based on Yang & Wang (2012).
!* AUTHOR: Yang & Wang (2012).
!* DATE WRITTEN: 4 Jan 2012
!***********************************************************************
implicit none
Double precision y,x,yt,xt,h4,g2,g3,a1,b1,a2,b2,a3,b3,a4,b4,rff_p,integ(4),b,three,two,one,&
tempt,f,g,h,a44,b44,sign_h
parameter (three=3.0D0,two=2.0D0,one=1.D0)
integer del,p4(4),i,cases
complex*16 bb(1:3)
logical :: inverse,neg
xt=x
yt=y
a44=a4
b44=b4
inverse=.false.
neg=.false.
If(abs(xt-yt).le.1.D-9)then
integ=0.D0
return
endif
if(yt.gt.xt)then
tempt=xt
xt=yt
yt=tempt
inverse=.true.
endif
b=0.D0
sign_h=sign(one,b44*xt+a44)
If(del.eq.3)then
! equation (75) of Yang & Wang (2012).
a44=sign_h*a44
b44=sign_h*b44
a1=-real(bb(1))
a2=-real(bb(2))
a3=-real(bb(3))
b1=1.D0
b2=1.D0
b3=1.D0
call ellcubicreals(p4,a1,b1,a2,b2,a3,b3,a44,b44,yt,xt,rff_p*two,integ,cases)
if(inverse)then
integ(1)=-integ(1)
integ(2)=-integ(2)
integ(3)=-integ(3)
integ(4)=-integ(4)
endif
Do i=1,4
integ(i)=integ(i)/two
integ(i)=integ(i)*(sign_h)**(-p4(i)/2)
Enddo
else
! equation (76) of Yang & Wang (2012).
a44=sign_h*a44
b44=sign_h*b44
a1=-real(bb(1))
b1=one
f=real(bb(2))**2+aimag(bb(2))**2
g=-two*real(bb(2))
h=one
call ellcubiccomplexs(p4,a1,b1,a44,b44,f,g,h,yt,xt,rff_p*two,integ,cases)
if(inverse)then
integ(1)=-integ(1)
integ(2)=-integ(2)
integ(3)=-integ(3)
integ(4)=-integ(4)
endif
Do i=1,4
integ(i)=integ(i)/two
integ(i)=integ(i)*(sign_h)**(-p4(i)/2)
Enddo
endif
return
end subroutine weierstrass_int_J3
!**********************************************************************************************
subroutine carlson_doublecomplex5(y,x,f1,g1,h1,f2,g2,h2,a5,b5,p5,rff_p,integ,cases)
!**********************************************************************************************
!* PURPOSE: Computes integrals: J_k(h)=\int^x_y (b5*r+a5)^(k/2)*[(h1*r^2+g1*r+f1)(h2*r^2+g2*r+f2)]^(-1/2)dr.
!* Where integer index k can be 0, -2, -4, 2 and 4. (77) of Yang & Wang (2012).
!* INPUTS: x,y -- limits of integral.
!* p5,rff_p,integ -- p5(1:5) is an array which specifies the value of index k of J_k(h).
!* If p5(1)=0, then J_0 was computed and sent to integ(1).
!* If p5(1)!=0, then J_0 was replaced by parameter p, and rff_p=p.
!* If p5(2)=-2, then J_{-2} was computed and sent to integ(2).
!* If p5(3)=2, then J_{2} was computed and sent to integ(3).
!* If p5(4)=-4, then J_{-4} was computed and sent to integ(4).
!* If p5(4)=4, then J_{4} was computed and sent to integ(5).
!* cases -- If cases=1, then only J_0 will be computed.
!* If cases=2, then only J_0 and J_{-2} will be computed.
!* If cases=3, then only J_0, J_{-2} and J_{2} will be computed.
!* If cases=4, then J_0, J_{-2}, J_{2} and J_{-4} will be computed.
!* If cases=5, then J_0, J_{-2}, J_{2} and J_{4} will be computed.
!* OUTPUTS: integ -- is an array saved the results of J_k(h).
!* ROUTINES CALLED: elldoublecomplexs
!* ACCURACY: Machine.
!* REMARKS: Based on Yang & Wang (2012).
!* AUTHOR: Yang & Wang (2012).
!* DATE WRITTEN: 4 Jan 2012
!***********************************************************************
implicit none
Double precision y,x,xt,yt,f1,g1,h1,f2,g2,h2,a5,b5,rff,integ(1:5),b,tempt,f,g,h,a55,&
b55,sign_h,one,zero,rff_p
parameter(one=1.D0,zero=0.D0)
integer reals,p5(1:5),cases,i
logical :: inverse,neg
xt=x
yt=y
a55=a5
b55=b5
inverse=.false.
neg=.false.
If(abs(xt-yt).le.1.D-9)then
integ=zero
return
endif
if(yt.gt.xt)then
tempt=xt
xt=yt
yt=tempt
inverse=.true.
endif
sign_h=sign(one,b55*xt+a55)
a55=sign_h*a55
b55=sign_h*b55
! equation (77) of Yang & Wang (2012).
call elldoublecomplexs(p5,f1,g1,h1,f2,g2,h2,a55,b55,yt,xt,rff_p,integ,cases)
if(inverse)then
integ(1)=-integ(1)
integ(2)=-integ(2)
integ(3)=-integ(3)
integ(4)=-integ(4)
integ(5)=-integ(5)
endif
Do i=1,5
integ(i)=integ(i)*(sign_h)**(-p5(i)/2)
Enddo
return
end subroutine carlson_doublecomplex5
!*******************************************************************************
subroutine ellcubicreals(index_p4,a1,b1,a2,b2,a3,b3,a4,b4,y,x,rff_p,integ,cases)
!*******************************************************************************
!* PURPOSE: Computes J_k(h)=\int_y^x dt (b4*t+a4)^(k/2)[(b1*t+a1)*(b2*t+a2)*(b3*t+a3)]^{-1/2}.
!* It is the case of equation W(t)=4*t^3-g_2*t-g_3=0 has three real roots.
!* Equation (61) of Yang & Wang (2012).
!* INPUTS: Arguments for above integral. If index_p4(1)=0, then J_0 will be computed,
!* else J_0 will be replaced by parameter p, and rff_p=p.
!* OUTPUTS: Value of integral J_k(h).
!* ROUTINES CALLED: RF,RJ,RC,RD
!* ACCURACY: Machine.
!* AUTHOR: Dexter & Agol (2009)
!* MODIFIED: Yang & Wang (2012)
!* DATE WRITTEN: 4 Mar 2009
!* REVISIONS:
!***********************************************************************
implicit NONE
Double precision zero,one,half,two,three,ellcubic,d12,d13,d14,d24,d34,X1,X2,X3,X4,&
Y1,Y2,Y3,Y4,U1c,U32,U22,W22,U12,Q22,P22,I1c,I3c,r12,r13,r24i,r34i,&
I2c,J2c,K2c,a1,b1,a2,b2,a3,b3,a4,b4,y,x,rff_p,r14,r24,r34,rff,integ(4)
integer index_p4(4),cases
PARAMETER ( ZERO=0.D0, ONE=1.D0, TWO=2.D0, HALF=0.5d0, THREE=3.D0 )
ellcubic=0.d0
!c (2.1) Carlson (1989)
d12=a1*b2-a2*b1
d13=a1*b3-a3*b1
d14=a1*b4-a4*b1
d24=a2*b4-a4*b2
d34=a3*b4-a4*b3
r14=a1/b1-a4/b4
r24=a2/b2-a4/b4
r34=a3/b3-a4/b4
!c (2.2) Carlson (1989)
X1=dsqrt(abs(a1+b1*x))
X2=dsqrt(abs(a2+b2*x))
X3=dsqrt(abs(a3+b3*x))
X4=dsqrt(abs(a4+b4*x))
Y1=dsqrt(abs(a1+b1*y))
Y2=dsqrt(abs(a2+b2*y))
Y3=dsqrt(abs(a3+b3*y))
Y4=dsqrt(abs(a4+b4*y))
!c! (2.3) Carlson (1989)
If(x.lt.infinity)then
U1c=(X1*Y2*Y3+Y1*X2*X3)/(x-y)
U12=U1c**2
U22=((X2*Y1*Y3+Y2*X1*X3)/(x-y))**2
U32=((X3*Y1*Y2+Y3*X1*X2)/(x-y))**2
else
U1c=dsqrt(abs(b2*b3))*Y1
U12=U1c**2
U22=b1*b3*Y2**2
U32=b2*b1*Y3**2
endif
!c (2.4) Carlson (1989)
W22=U12
W22=U12-b4*d12*d13/d14
! (2.5) Carlson (1989)
If(x.lt.infinity)then
Q22=(X4*Y4/X1/Y1)**2*W22
else
Q22=b4/b1*(Y4/Y1)**2*W22
endif
P22=Q22+b4*d24*d34/d14
!c Now, compute the three integrals we need [-1,-1,-1],[-1,-1,-1,-2], and
!c [-1,-1,-1,-4]:we need to calculate the [-1,-1,-1,2] integral,we add it in this part.
if(index_p4(1).eq.0) then
!c (2.21) Carlson (1989)
rff=rf(U32,U22,U12)
integ(1)=two*rff
if(cases.eq.1)return
else
rff=rff_p/two
endif
!c (2.12) Carlson (1989)
I1c=rff_p!two*rff
If(index_p4(3).eq.2) then
!c (2.13) Carlson (1989)
I2c=two/three*d12*d13*rd(U32,U22,U12)+two*X1*Y1/U1c
! (2.39) Carlson (1989)
integ(3)=(b4*I2c-d14*I1c)/b1
if(cases.eq.3)return
endif
If(X1*Y1.ne.zero) then
!c (2.14) Carlson (1989)
I3c=two*rc(P22,Q22)-two*b1*d12*d13/three/d14*rj(U32,U22,U12,W22)
Else
! One can read the paragraph between (2.19) and (2.20) of Carlson (1989).
I3c=-two*b1*d12*d13/three/d14*rj(U32,U22,U12,W22)
Endif
if(index_p4(2).eq.-2) then
!c (2.49) Carlson (1989)
integ(2)=(b4*I3c-b1*I1c)/d14
if(cases.eq.2)return
endif
If(index_p4(4).eq.-4)then
!c (2.1) Carlson (1989)
r12=a1/b1-a2/b2
r13=a1/b1-a3/b3
r24i=b2*b4/(a2*b4-a4*b2)
r34i=b3*b4/(a3*b4-a4*b3)
If(x.lt.infinity)then
!c (2.17) Carlson (1989)
J2c=two/three*d12*d13*rd(U32,U22,U12)+two*d13*X2*Y2/X3/Y3/U1c
!c (2.59) & (2.6) Carlson (1989)
K2c=b3*J2c-two*d34*(X1*X2/X3/X4**2-Y1*Y2/Y3/Y4**2)