-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkernel_cspline_utils.f90
386 lines (353 loc) · 11.1 KB
/
kernel_cspline_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
!-------------------------------------------------------
!--module to compute csplines for n>=4
!--uses the Hankel transform of order 0 of a nice kernel
!--rlim, eps and tol finely tuned to have beautiful results !
!-------------------------------------------------------
module csplinekernels
implicit none
public :: getcsplinekernel,getcsplinekernelder
private
contains
!------------------------------------------------------
! Subroutine to compute the kernel and its derivatives
!------------------------------------------------------
subroutine getcsplinekernel(nc,radkern,q,w,gw,ggw)
implicit none
integer, intent(in) :: nc
real, intent(in) :: q,radkern
real, intent(out) :: w
real, intent(out) :: gw
real, intent(out) :: ggw
real :: absq
real, parameter :: rlim = 2.d2
real, external :: besselstuff,dbesselstuff,ddbesselstuff,ddzbesselstuff
absq = abs(q)
if (abs(q).lt.radkern) then
call qromb_twoparam(besselstuff,nc,q,0.,rlim,w)
if (w.lt.0.) w = 0.
call qromb_twoparam(dbesselstuff,nc,q,0.,rlim,gw)
gw = -gw
if (gw.gt.0.) gw = 0.
if (q.gt.tiny(0.)) then
call qromb_twoparam(ddbesselstuff,nc,q,0.,rlim,ggw)
ggw = - ggw - gw/q
else
call qromb_twoparam(ddzbesselstuff,nc,q,0.,rlim,ggw)
ggw = -0.5*ggw
endif
else
w = 0.
gw = 0.
ggw = 0.
endif
end subroutine getcsplinekernel
subroutine getcsplinekernelder(nc1,nc2,secondz,radkern,q,w,gw,ggw)
implicit none
integer, intent(in) :: nc1,nc2
real, intent(in) :: q,radkern,secondz
real, intent(out) :: w
real, intent(out) :: gw
real, intent(out) :: ggw
real :: absq
real, parameter :: rlim = 2.d2
real, external :: besselstuffder,dbesselstuffder
real, external :: ddbesselstuffder,ddzbesselstuffder
absq = abs(q)
if (abs(q).lt.radkern) then
call qromb_twoparamder(besselstuffder,nc1,nc2,secondz,q,0.,rlim,w)
if (w.lt.0.) w = 0.
call qromb_twoparamder(dbesselstuffder,nc1,nc2,secondz,q,0.,rlim,gw)
gw = -gw
if (gw.gt.0.) gw = 0.
if (q.gt.tiny(0.)) then
call qromb_twoparamder(ddbesselstuffder,nc1,nc2,secondz,q,0.,rlim,ggw)
ggw = - ggw - gw/q
else
call qromb_twoparamder(ddzbesselstuffder,nc1,nc2,secondz,q,0.,rlim,ggw)
ggw = -0.5*ggw
endif
else
w = 0.
gw = 0.
ggw = 0.
endif
end subroutine getcsplinekernelder
end module
!------------------------------------------------------------------
!--utilities for the standard csplines
!------------------------------------------------------------------
!------------------------------------------------------------------
SUBROUTINE qromb_twoparam(func,nc,q,a,b,ss)
implicit none
INTEGER JMAX,JMAXP,K,KM,nc
REAL a,b,func,q,ss,EPS
EXTERNAL func
PARAMETER (EPS=1.d-8, JMAX=30, JMAXP=JMAX+1, K=5, KM=K-1)
INTEGER j
REAL dss,h(JMAXP),s(JMAXP)
real, parameter :: tol = 1.d-9
h(1)=1.
do 11 j=1,JMAX
call trapzd_twoparam(func,nc,q,a,b,s(j),j)
if (j.ge.K) then
call polint_two(h(j-KM),s(j-KM),K,0.,ss,dss)
!--lines added to hack some spurious breaks------------
if (j.ne.K .and. j.ne.K+1) then !added
if (abs(dss).le.EPS*abs(ss)) return
if (abs(dss).lt.tol) return !added
! print*,abs(dss),abs(ss)
endif
!-------------------------------------------------------
endif
s(j+1)=s(j)
h(j+1)=0.25*h(j)
11 continue
print*,'too many steps in qromb_twoparam'
end subroutine qromb_twoparam
!------------------------------------------------------------------
SUBROUTINE polint_two(xa,ya,n,x,y,dy)
implicit none
INTEGER n,NMAX
REAL dy,x,y,xa(n),ya(n)
PARAMETER (NMAX=10)
INTEGER i,m,ns
REAL den,dif,dift,ho,hp,w,c(NMAX),d(NMAX)
ns=1
dif=abs(x-xa(1))
do i=1,n
dift=abs(x-xa(i))
if (dift.lt.dif) then
ns=i
dif=dift
endif
c(i)=ya(i)
d(i)=ya(i)
enddo
y=ya(ns)
ns=ns-1
do m=1,n-1
do i=1,n-m
ho=xa(i)-x
hp=xa(i+m)-x
w=c(i+1)-d(i)
den=ho-hp
if(den.eq.0.) print*,'failure in polint_two'
den=w/den
d(i)=hp*den
c(i)=ho*den
enddo
if (2*ns.lt.n-m)then
dy=c(ns+1)
else
dy=d(ns)
ns=ns-1
endif
y=y+dy
enddo
return
end subroutine polint_two
!------------------------------------------------------------------
SUBROUTINE trapzd_twoparam(func,nc,q,a,b,s,n)
implicit none
INTEGER n,nc
REAL a,b,s,func,q
INTEGER it,j
REAL del,sum,tnm,x
if (n.eq.1) then
s=0.5*(b-a)*(func(a,q,nc)+func(b,q,nc))
else
it=2**(n-2)
tnm=it
del=(b-a)/tnm
x=a+0.5*del
sum=0.
do j=1,it
sum=sum+func(x,q,nc)
x=x+del
enddo
s=0.5*(s+(b-a)*sum/tnm)
endif
return
END subroutine trapzd_twoparam
!------------------------------------------------------------------
real function besselstuff(x,q,nc)
implicit none
integer, intent(in) :: nc
real, intent(in) :: x,q
real :: ker,rinter1
real, parameter :: pi = 3.141592653589
if (x .le. tiny(0.)) then
ker = 1.
else
ker = 2.*bessel_j1(0.5*x)/(0.5*x)
endif
rinter1 = 1./(2.*pi)*ker**nc
besselstuff = rinter1*x*bessel_j0(q*x)
end function besselstuff
!------------------------------------------------------------------
real function dbesselstuff(x,q,nc)
implicit none
integer, intent(in) :: nc
real, intent(in) :: x,q
real :: ker,rinter1
real, parameter :: pi = 3.141592653589
if (x .le. tiny(0.)) then
ker = 1.
else
ker = 2.*bessel_j1(0.5*x)/(0.5*x)
endif
rinter1 = 1./(2.*pi)*ker**nc
dbesselstuff = rinter1*x*x*bessel_j1(q*x)
end function dbesselstuff
!------------------------------------------------------------------
real function ddbesselstuff(x,q,nc)
implicit none
integer, intent(in) :: nc
real, intent(in) :: x,q
real :: ker,rinter1
real, parameter :: pi = 3.141592653589
if (x .le. tiny(0.)) then
ker = 1.
else
ker = 2.*bessel_j1(0.5*x)/(0.5*x)
endif
rinter1 = 1./(2.*pi)*ker**nc
ddbesselstuff = rinter1*x*x*x*bessel_j0(q*x)
end function ddbesselstuff
!------------------------------------------------------------------
real function ddzbesselstuff(x,q,nc)
implicit none
integer, intent(in) :: nc
real, intent(in) :: x,q
real :: ker,rinter1
real, parameter :: pi = 3.141592653589
if (x .le. tiny(0.)) then
ker = 1.
else
ker = 2.*bessel_j1(0.5*x)/(0.5*x)
endif
rinter1 = 1./(2.*pi)*ker**nc
ddzbesselstuff = rinter1*x*x*x
end function ddzbesselstuff
!------------------------------------------------------------------
!--utilities for the csplines derivatives
!------------------------------------------------------------------
!------------------------------------------------------------------
SUBROUTINE qromb_twoparamder(func,nc1,nc2,secondz,q,a,b,ss)
implicit none
INTEGER JMAX,JMAXP,K,KM,nc1,nc2
REAL a,b,func,q,ss,EPS,secondz
EXTERNAL func
PARAMETER (EPS=1.d-8, JMAX=30, JMAXP=JMAX+1, K=5, KM=K-1)
INTEGER j
REAL dss,h(JMAXP),s(JMAXP)
real, parameter :: tol = 1.d-9
h(1)=1.
do 11 j=1,JMAX
call trapzd_twoparamder(func,nc1,nc2,secondz,q,a,b,s(j),j)
if (j.ge.K) then
call polint_two(h(j-KM),s(j-KM),K,0.,ss,dss)
!--lines added to hack some spurious breaks------------
if (j.ne.K .and. j.ne.K+1) then !added
if (abs(dss).le.EPS*abs(ss)) return
if (abs(dss).lt.tol) return !added
! print*,abs(dss),abs(ss)
endif
!-------------------------------------------------------
endif
s(j+1)=s(j)
h(j+1)=0.25*h(j)
11 continue
print*,'too many steps in qromb_twoparam'
end subroutine qromb_twoparamder
!------------------------------------------------------------------
SUBROUTINE trapzd_twoparamder(func,nc1,nc2,secondz,q,a,b,s,n)
implicit none
INTEGER n,nc1,nc2
REAL a,b,s,func,q,secondz
INTEGER it,j
REAL del,sum,tnm,x
if (n.eq.1) then
s=0.5*(b-a)*(func(a,q,nc1,nc2,secondz)+func(b,q,nc1,nc2,secondz))
else
it=2**(n-2)
tnm=it
del=(b-a)/tnm
x=a+0.5*del
sum=0.
do j=1,it
sum=sum+func(x,q,nc1,nc2,secondz)
x=x+del
enddo
s=0.5*(s+(b-a)*sum/tnm)
endif
return
END subroutine trapzd_twoparamder
!------------------------------------------------------------------
real function besselstuffder(x,q,nc1,nc2,secondz)
implicit none
integer, intent(in) :: nc1,nc2
real, intent(in) :: x,q,secondz
real :: ker1,ker2,rinter1
real, parameter :: pi = 3.141592653589
if (x .le. tiny(0.)) then
ker1 = 1.
ker2 = 1.
else
ker1 = 2.*bessel_j1(0.5*x)/(0.5*x)
ker2 = 2.*bessel_j1(0.5*secondz*x)/(0.5*secondz*x)
endif
rinter1 = 1./(2.*pi)*ker1**nc1*ker2**nc2
besselstuffder = rinter1*x*bessel_j0(q*x)
end function besselstuffder
!------------------------------------------------------------------
real function dbesselstuffder(x,q,nc1,nc2,secondz)
implicit none
integer, intent(in) :: nc1,nc2
real, intent(in) :: x,q,secondz
real :: ker1,ker2,rinter1
real, parameter :: pi = 3.141592653589
if (x .le. tiny(0.)) then
ker1 = 1.
ker2 = 1.
else
ker1 = 2.*bessel_j1(0.5*x)/(0.5*x)
ker2 = 2.*bessel_j1(0.5*secondz*x)/(0.5*secondz*x)
endif
rinter1 = 1./(2.*pi)*ker1**nc1*ker2**nc2
dbesselstuffder = rinter1*x*x*bessel_j1(q*x)
end function dbesselstuffder
!------------------------------------------------------------------
real function ddbesselstuffder(x,q,nc1,nc2,secondz)
implicit none
integer, intent(in) :: nc1,nc2
real, intent(in) :: x,q,secondz
real :: ker1,ker2,rinter1
real, parameter :: pi = 3.141592653589
if (x .le. tiny(0.)) then
ker1 = 1.
ker2 = 1.
else
ker1 = 2.*bessel_j1(0.5*x)/(0.5*x)
ker2 = 2.*bessel_j1(0.5*secondz*x)/(0.5*secondz*x)
endif
rinter1 = 1./(2.*pi)*ker1**nc1*ker2**nc2
ddbesselstuffder = rinter1*x*x*x*bessel_j0(q*x)
end function ddbesselstuffder
!------------------------------------------------------------------
real function ddzbesselstuffder(x,q,nc1,nc2,secondz)
implicit none
integer, intent(in) :: nc1,nc2
real, intent(in) :: x,q,secondz
real :: ker1,ker2,rinter1
real, parameter :: pi = 3.141592653589
if (x .le. tiny(0.)) then
ker1 = 1.
ker2 = 1.
else
ker1 = 2.*bessel_j1(0.5*x)/(0.5*x)
ker2 = 2.*bessel_j1(0.5*secondz*x)/(0.5*secondz*x)
endif
rinter1 = 1./(2.*pi)*ker1**nc1*ker2**nc2
ddzbesselstuffder = rinter1*x*x*x
end function ddzbesselstuffder