-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkernel_utils.f90
285 lines (266 loc) · 11 KB
/
kernel_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
!------------------------------------------------------------------------------!
! NDSPMHD: A Smoothed Particle (Magneto)Hydrodynamics code for (astrophysical) !
! fluid dynamics simulations in 1, 2 and 3 spatial dimensions. !
! !
! (c) 2002-2015 Daniel Price !
! !
! http://users.monash.edu.au/~dprice/ndspmhd !
! daniel.price@monash.edu -or- dprice@cantab.net (forwards to current address) !
! !
! NDSPMHD comes with ABSOLUTELY NO WARRANTY. !
! This is free software; and you are welcome to redistribute !
! it under the terms of the GNU General Public License !
! (see LICENSE file for details) and the provision that !
! this notice remains intact. If you modify this file, please !
! note section 2a) of the GPLv2 states that: !
! !
! a) You must cause the modified files to carry prominent notices !
! stating that you changed the files and the date of any change. !
! !
! ChangeLog: !
!------------------------------------------------------------------------------!
!-----------------------------------------------------------------------------
! This module contains utility routines for messing around with the SPH kernel
!-----------------------------------------------------------------------------
module kernel_utils
implicit none
integer, parameter :: dp = kind(0.)
real(dp), parameter :: pi = 3.1415926536
contains
!-----------------------------------------
! routine to normalise the kernel table
! appropriately in 1, 2 and 3D
!-----------------------------------------
subroutine normalise(ikern,wkern,c,radkern2)
integer, intent(in) :: ikern
real(dp), intent(in) :: radkern2
real(dp), intent(inout) :: wkern(0:ikern)
real(dp), intent(out) :: c(3)
real(dp) :: dq2table,q2,q,f(3),qprev,dq
integer :: i
dq2table = radkern2/real(ikern,kind=dp)
! trapezoidal rule to get integral under kernel
f = 0.
qprev = 0.
do i=1,ikern
q2 = i*dq2table
q = sqrt(q2)
dq = q - qprev
f(1) = f(1) + 0.5*(wkern(i) + wkern(i-1))*dq
f(2) = f(2) + 0.5*(q*wkern(i) + qprev*wkern(i-1))*dq
f(3) = f(3) + 0.5*(q2*wkern(i) + qprev**2*wkern(i-1))*dq
qprev = q
enddo
f(1) = 2.*f(1)
f(2) = 2.*pi*f(2)
f(3) = 4.*pi*f(3)
!print*,' integral is ',f(:)
c(:) = 1./f(:)
!print*,' normalisation is ',c(:)
end subroutine normalise
!-----------------------------------------------
! differentiate the kernel table to get
! tables for the derivative and 2nd deriv
! We use 2nd-order accurate finite differencing
! on non-uniform meshes
!-----------------------------------------------
subroutine diff(ikern,wkern,grkern,grgrkern,radkern2)
integer, intent(in) :: ikern
real(dp), intent(in) :: radkern2
real(dp), intent(inout) :: wkern(0:ikern)
real(dp), intent(out) :: grkern(0:ikern),grgrkern(0:ikern)
real(dp) :: dq2table,q2,q,qm1,qm2,qm3,qp1,qp2,qp3
real(dp) :: dq,dqm1,dqm2,dqm3,dqp1,dqp2,a,b,c,d
integer :: i
dq2table = radkern2/real(ikern,kind=dp)
!do i=0,ikern
! wkern(i) = i*dq2table
!enddo
! finite differencing to get kernel derivatives
do i=0,ikern
q2 = i*dq2table
q = sqrt(q2)
if (i==0 .or. i==1) then
! forward diff
qp1 = sqrt((i+1)*dq2table)
qp2 = sqrt((i+2)*dq2table)
qp3 = sqrt((i+3)*dq2table)
dq = qp1 - q
dqp1 = qp2 - qp1
dqp2 = qp3 - qp2
a = -(2.*dq + dqp1)/(dq*(dq + dqp1))
b = (dq + dqp1)/(dq*dqp1)
c = -dq/(dqp1*(dq + dqp1))
grkern(i) = a*wkern(i) + b*wkern(i+1) + c*wkern(i+2)
! second deriv, forward diff
a = (6.d0*dq + 4.d0*dqp1 + 2.d0*dqp2)/(dq*(dq + dqp1)*(dq + dqp1 + dqp2))
b = -(4.d0*(dq + dqp1) + 2.d0*dqp2)/(dq*dqp1*(dqp1 + dqp2))
c = (4.d0*dq + 2.d0*(dqp1 + dqp2))/((dqp1 + dq)*dqp1*dqp2)
d = -(4.d0*dq + 2.d0*dqp1)/((dq + dqp1 + dqp2)*(dqp2 + dqp1)*dqp2)
grgrkern(i) = a*wkern(i) + b*wkern(i+1) + c*wkern(i+2) + d*wkern(i+3)
elseif (i==ikern .or. i==ikern-1) then
!elseif (i > 3) then
! backward diff
qm1 = sqrt((i-1)*dq2table)
qm2 = sqrt((i-2)*dq2table)
qm3 = sqrt((i-3)*dq2table)
dqm1 = q - qm1
dqm2 = qm1 - qm2
dqm3 = qm2 - qm3
a = dqm1/(dqm2*(dqm1+dqm2))
b = -(dqm1 + dqm2)/(dqm1*dqm2)
c = (2.d0*dqm1 + dqm2)/(dqm1*(dqm1 + dqm2))
grkern(i) = a*wkern(i-2) + b*wkern(i-1) + c*wkern(i)
! second deriv, backwards
a = -(4.d0*dqm1 + 2.d0*dqm2)/(dqm3*(dqm3 + dqm2)*(dqm3 + dqm2 + dqm1))
b = (4.d0*dqm1 + 2.d0*(dqm2 + dqm3))/(dqm3*dqm2*(dqm2 + dqm1))
c = -(4.d0*(dqm1 + dqm2) + 2.d0*dqm3)/((dqm2 + dqm3)*dqm2*dqm1)
d = (6.d0*dqm1 + 4.d0*dqm2 + 2.d0*dqm3)/((dqm1 + dqm2 + dqm3)*(dqm1 + dqm2)*dqm1)
grgrkern(i) = a*wkern(i-3) + b*wkern(i-2) + c*wkern(i-1) + d*wkern(i)
else
qp2 = sqrt((i+2)*dq2table)
qp1 = sqrt((i+1)*dq2table)
qm1 = sqrt((i-1)*dq2table)
dqm1 = q - qm1
dq = qp1 - q
dqp1 = qp2 - qp1
! 2nd order unequal grid finite diff
grkern(i) = -dq/(dqm1*(dq+dqm1))*wkern(i-1) &
+ (dq-dqm1)/(dq*dqm1)*wkern(i) &
+ dqm1/(dq*(dq + dqm1))*wkern(i+1)
! 2nd deriv - BUT THIS IS ONLY FIRST ORDER ACCURATE
!a = 2./(dqm1*(dq + dqm1))
!b = -2./(dqm1*dq)
!c = 2./(dq*(dq + dqm1))
!grgrkern(i) = a*wkern(i-1) + b*wkern(i) + c*wkern(i+1)
! two nodes forward, one back 2nd derivative estimate
!a = 2.d0*(2.d0*dq + dqp1)/(dqm1*(dqm1 + dq)*(dqm1 + dq + dqp1))
!b = -2.d0*(2.d0*dq + dqp1 - dqm1)/(dqm1*dq*(dq + dqp1))
!c = 2.d0*(dq + dqp1 - dqm1)/((dqm1 + dq)*dq*dqp1)
!d = -2.d0*(dq - dqm1)/((dqm1 + dq + dqp1)*(dq + dqp1)*dqp1)
!print*,a,b,c,d
a = (2.*dqp1 + 4.*dq)/((q-qm1)*(qp1-qm1)*(qp2-qm1))
b = 2.*((qm1 - q) + 2.*(qp1-q) + qp2-qp1)/((qm1 - q)*(qp1-q)*(qp2 - q))
c = 2.*(-2.*q + qm1 + qp2)/((q - qp1)*(qm1 - qp1)*(qp2 - qp1))
d = 2.*(-2.*q + qm1 + qp1)/((q - qp2)*(qm1 - qp2)*(qp1 - qp2))
!print*,a,b,c,d
grgrkern(i) = a*wkern(i-1) + b*wkern(i) + c*wkern(i+1) + d*wkern(i+2)
endif
enddo
end subroutine diff
!-----------------------------------------------
! differentiate the kernel table to get
! tables for the derivative and 2nd deriv
! We use 2nd-order accurate finite differencing
! on non-uniform meshes
!-----------------------------------------------
subroutine differentiate(ikern,wfunc,grfunc,radkern2)
integer, intent(in) :: ikern
real(dp), intent(in) :: wfunc(0:ikern),radkern2
real(dp), intent(out) :: grfunc(0:ikern)
real(dp) :: dq2table,q2,q,qm1,qm2,qp1,qp2
real(dp) :: dq,dqm1,dqm2,dqp1,a,b,c
integer :: i
dq2table = radkern2/real(ikern,kind=dp)
! finite differencing to get kernel derivatives
do i=0,ikern
q2 = i*dq2table
q = sqrt(q2)
if (i==0 .or. i==1) then
! forward diff
qp1 = sqrt((i+1)*dq2table)
qp2 = sqrt((i+2)*dq2table)
dq = qp1 - q
dqp1 = qp2 - qp1
a = -(2.*dq + dqp1)/(dq*(dq + dqp1))
b = (dq + dqp1)/(dq*dqp1)
c = -dq/(dqp1*(dq + dqp1))
grfunc(i) = a*wfunc(i) + b*wfunc(i+1) + c*wfunc(i+2)
elseif (i==ikern .or. i==ikern-1) then
! backward diff
qm1 = sqrt((i-1)*dq2table)
qm2 = sqrt((i-2)*dq2table)
dqm2 = qm1 - qm2
dqm1 = q - qm1
a = dqm1/(dqm2*(dqm1+dqm2))
b = -(dqm1 + dqm2)/(dqm1*dqm2)
c = (2.*dqm1 + dqm2)/(dqm1*(dqm1 + dqm2))
grfunc(i) = a*wfunc(i-2) + b*wfunc(i-1) + c*wfunc(i)
else
qp1 = sqrt((i+1)*dq2table)
qm1 = sqrt((i-1)*dq2table)
dqm1 = q - qm1
dq = qp1 - q
! 2nd order unequal grid finite diff
grfunc(i) = -dq/(dqm1*(dq+dqm1))*wfunc(i-1) &
+ (dq-dqm1)/(dq*dqm1)*wfunc(i) &
+ dqm1/(dq*(dq + dqm1))*wfunc(i+1)
endif
enddo
end subroutine differentiate
!-----------------------------------------------
! differentiate the kernel table to get
! tables for the derivative and 2nd deriv
! this version does standard finite differencing
! on the q2 mesh and then chain rule to get
! deriv w.r.t. q instead of q2
!-----------------------------------------------
subroutine diffu(ikern,wkern,grkern,grgrkern,radkern2)
integer, intent(in) :: ikern
real(dp), intent(in) :: wkern(0:ikern),radkern2
real(dp), intent(out) :: grkern(0:ikern),grgrkern(0:ikern)
real(dp) :: dq2table,q2,q
real(dp) :: ddq2,ddq22,qp1,qm1
integer :: i
dq2table = radkern2/real(ikern,kind=dp)
ddq2 = 1.d0/dq2table
ddq22 = ddq2*ddq2
! finite differencing to get kernel derivatives
do i=0,ikern
q2 = i*dq2table
q = sqrt(q2)
if (i==0) then
! forward diff
grkern(i) = 0.5*(-3.*wkern(i) + 4.*wkern(i+1) - wkern(i+2))*ddq2
grgrkern(i) = (2.*wkern(i) - 5.*wkern(i+1) + 4.*wkern(i+2) - wkern(i+3))*ddq22*2.*q + grkern(i)*2.
grkern(i) = grkern(i)*2.*q
elseif (i==ikern) then
! backward diff
grkern(i) = 0.5*(wkern(i-2) - 4.*wkern(i-1) + 3.*wkern(i))*ddq2
grgrkern(i) = (-wkern(i-3) + 4.*wkern(i-2) - 5.*wkern(i-1) + 2.*wkern(i))*ddq22*2.*q + grkern(i)*2.
grkern(i) = grkern(i)*2.*q
else
qp1 = sqrt((i+1)*dq2table)
qm1 = sqrt((i-1)*dq2table)
! centred diff
grkern(i) = 0.5*(wkern(i+1) - wkern(i-1))*ddq2
grgrkern(i) = (wkern(i-1) - 2.*wkern(i) + wkern(i+1))*ddq22*2.*q + grkern(i)*2.
grkern(i) = grkern(i)*2.*q
if (i.eq.1 .or. i.eq.2 .or. i.eq.3) print*,i,grkern(i)
endif
enddo
end subroutine diffu
!-----------------------------------------------
! Integrate the kernel table to get
! the kernel from the kernel derivative
! or kernel derivative from 2nd derivative.
! Called twice this does opposite of diff
!-----------------------------------------------
subroutine integrate(ikern,df,f,radkern2)
integer, intent(in) :: ikern
real(dp), intent(in) :: df(0:ikern),radkern2
real(dp), intent(out) :: f(0:ikern)
real(dp) :: dq2table,q,qprev,dq
integer :: i
dq2table = radkern2/real(ikern,kind=dp)
! integrate to get kernel gradient
f(ikern) = 0.
qprev = sqrt(ikern*dq2table)
do i=ikern-1,0,-1
q = sqrt(i*dq2table)
dq = q - qprev
f(i) = f(i+1) + 0.5*(df(i) + df(i+1))*dq
qprev = q
enddo
end subroutine integrate
end module kernel_utils