/
fft.f90
433 lines (349 loc) · 10.7 KB
/
fft.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
module fft
!
! Copyright (c) 2013 Alexei Matveev
! Copyright (c) 2013 Bo Li
!
use kinds, only: rk
implicit none
private
real (rk), parameter, public :: pi = 4 * atan (1.0_rk)
!
! These should obey: ab = (2π)³, a²b = (2π)³. The first equality
! guarantees that the forward- and backward FT are mutually inverse,
! whereas the second equation will make the convolution theorem
! factor-less as in FT(f * g) = FT(f) FT(g):
!
real (rk), parameter, public :: FT_FW = 1 ! == a
real (rk), parameter, public :: FT_BW = (2 * pi)**3 ! == b
!
! Functions operated upon by fourier(), integrate() and their fellow
! siblings depend on radius "r" only and are represented by an array
! such as f(1:n) where
!
! f = f(dr * [2 * i - 1] / 2), 1 <= i <= n
! i
!
! and dr = 1 for the purposes of the current module. The
! corresponding Fourier transform, g = FT(f), is represented on a
! similarly spaced k-grid,
!
! g = g(dk * [2 * k - 1] / 2), 1 <= k <= n
! k
!
! albeit with dk related to dr by the following expression:
!
! dr * dk = 2π / 2n
!
public :: mkgrid
public :: fourier ! f(1:n) -> g(1:n)
public :: fourier_rows ! f(*, 1:n) -> g(*, 1:n)
! public :: fourier_cols ! f(1:n, *) -> g(1:n, *)
public :: integrate ! f(1:n) -> scalar
public :: integral ! f(1:n) -> g(1:n)
!
! *** END OF INTERFACE ***
!
!
! This is a concrete function, implemented in C:
!
interface
subroutine rism_dst (n, out, in) bind (c)
!
! Performs DST. In FFTW terms this is RODFT11 (or DST-IV) which
! is self inverse up to a normalization factor.
!
! void rism_dst (size_t n, double out[n], const double in[n])
!
! See ./rism-dst.c
!
use iso_c_binding, only: c_size_t, c_double
implicit none
integer (c_size_t), intent (in), value :: n
real (c_double), intent (out) :: out(n)
real (c_double), intent (in) :: in(n)
end subroutine rism_dst
subroutine rism_dst_columns (m, n, buf) bind (c)
!
! Performs DST of size n in-place for each of the m columns. In
! FFTW terms this is RODFT11 (or DST-IV) which is self inverse
! up to a normalization factor.
!
! See ./rism-dst.c
!
use iso_c_binding, only: c_int, c_double
implicit none
integer (c_int), intent (in), value :: m, n
real (c_double), intent (inout) :: buf(n, m)
end subroutine rism_dst_columns
subroutine rism_dst_rows (n, m, buf) bind (c)
!
! Performs DST of size n in-place for each of the m rows. In
! FFTW terms this is RODFT11 (or DST-IV) which is self inverse
! up to a normalization factor.
!
! See ./rism-dst.c
!
use iso_c_binding, only: c_int, c_double
implicit none
integer (c_int), intent (in), value :: m, n
real (c_double), intent (inout) :: buf(m, n)
end subroutine rism_dst_rows
end interface
contains
pure subroutine mkgrid (rmax, r, dr, k, dk)
implicit none
real (rk), intent (in) :: rmax
real (rk), intent (out) :: r(:), dr, k(:), dk ! (nrad)
! *** end of interface ***
integer :: i, nrad
nrad = size (r)
! dr * dk = 2π/2n:
dr = rmax / nrad
dk = pi / rmax
forall (i = 1:nrad)
r(i) = (2 * i - 1) * dr / 2
k(i) = (2 * i - 1) * dk / 2
end forall
end subroutine mkgrid
function fourier_rows (f) result (g)
implicit none
real (rk), intent (in) :: f(:, :, :)
real (rk) :: g(size (f, 1), size (f, 2), size (f, 3))
! *** end of interface ***
integer :: m, n
m = size (f, 1) * size (f, 2)
n = size (f, 3)
call do_fourier_rows (m, n, f, g)
end function fourier_rows
subroutine do_fourier_rows (m, n, f, g)
implicit none
integer, intent (in) :: m, n
real (rk), intent (in) :: f(m, n)
real (rk), intent (out) :: g(m, n)
! *** end of interface ***
integer :: p, i
real (rk) :: fac
real (rk), parameter :: one = 1
!
! We use RODFT11 (DST-IV) that is "odd around j = -0.5 and even
! around j = n - 0.5". Here we use integer arithmetics and the
! identity (2 * j - 1) / 2 == j - 0.5.
!
!$omp parallel do private(fac, p, i)
do p = 1, n
fac = 2 * n * (2 * p - 1)
do i = 1, m
g(i, p) = f(i, p) * fac
enddo
enddo
!$omp end parallel do
call dst_rows (g)
!$omp parallel do private(fac, p, i)
do p = 1, n
fac = one / (2 * p - 1)
do i = 1, m
g(i, p) = g(i, p) * fac
enddo
enddo
!$omp end parallel do
end subroutine do_fourier_rows
function fourier_cols (f) result (g)
implicit none
real (rk), intent (in) :: f(:, :, :)
real (rk) :: g(size (f, 1), size (f, 2), size (f, 3))
! *** end of interface ***
integer :: m, n
n = size (f, 1)
m = size (f, 2) * size (f, 3)
call do_fourier_cols (n, m, f, g)
end function fourier_cols
subroutine do_fourier_cols (n, m, f, g)
implicit none
integer, intent (in) :: n, m
real (rk), intent (in) :: f(n, m)
real (rk), intent (out) :: g(n, m)
! *** end of interface ***
integer :: p, i
!
! We use RODFT11 (DST-IV) that is "odd around j = -0.5 and even
! around j = n - 0.5". Here we use integer arithmetics and the
! identity (2 * j - 1) / 2 == j - 0.5.
!
!$omp parallel workshare private(p, i)
forall (p = 1:n, i = 1:m)
g(p, i) = f(p, i) * (2 * n * (2 * p - 1))
end forall
!$omp end parallel workshare
call dst_columns (g)
!$omp parallel workshare private(p, i, j)
forall (p = 1:n, i = 1:m)
g(p, i) = g(p, i) / (2 * p - 1)
end forall
!$omp end parallel workshare
end subroutine do_fourier_cols
function fourier (f) result (g)
!
! We could call eithr do_fourier_rows() or do_fourier_cols() here.
!
implicit none
real (rk), intent (in) :: f(:)
real (rk) :: g(size (f))
! *** end of interface ***
integer :: i, n
n = size (f)
!
! We use RODFT11 (DST-IV) that is "odd around j = -0.5 and even
! around j = n - 0.5". Here we use integer arithmetics and the
! identity (2 * j - 1) / 2 == j - 0.5.
!
forall (i = 1:n)
g(i) = f(i) * (2 * i - 1)
end forall
g = 2 * n * dst (g)
forall (i = 1:n)
g(i) = g(i) / (2 * i - 1)
end forall
end function fourier
pure function integrate (f) result (g)
!
! Approximates 4π ∫f(r)r²dr. Should be "related" to the k = 0
! component of the FT. This is also the only reason the function
! is put into this module.
!
implicit none
real (rk), intent (in) :: f(:)
real (rk) :: g
! *** end of interface ***
integer :: i, n
n = size (f)
! Integrating backwards because we assume that f(r) -> 0 for large
! r:
g = 0.0
do i = n, 1, -1
g = g + f(i) * (2 * i - 1)**2 / 4
enddo
g = 4 * pi * g
end function integrate
pure function integral (f) result (g)
!
! Approximate indefinite integral using the same integration
! algorithm as integrate():
!
! R
! g(R) = 4π ∫ f(r)r²dr
! 0
!
! integral(f) returns an array whereas integrate(f) returns a
! scalar.
!
implicit none
real (rk), intent (in) :: f(:)
real (rk) :: g(size(f))
! *** end of interface ***
integer :: i, n
real (rk) :: acc
n = size (f)
! Integrating forward for the lack of better ideas:
acc = 0.0
do i = 1, n
acc = acc + 4 * pi * f(i) * (2 * i - 1)**2 / 4
g(i) = acc
enddo
end function integral
subroutine dst_columns (f)
use iso_c_binding, only: c_int
implicit none
real (rk), intent (inout) :: f(:, :) ! (n, m)
! *** end of interface ***
integer (c_int) :: m, n
! cast to c_int
n = size (f, 1)
m = size (f, 2)
call rism_dst_columns (m, n, f)
end subroutine dst_columns
subroutine dst_rows (f)
use iso_c_binding, only: c_int
implicit none
real (rk), intent (inout) :: f(:, :) ! (m, n)
! *** end of interface ***
integer (c_int) :: m, n
! cast to c_int
n = size (f, 2)
m = size (f, 1)
call rism_dst_rows (n, m, f)
end subroutine dst_rows
function dst (a) result (b)
use iso_c_binding, only: c_size_t
implicit none
real (rk), intent (in) :: a(:)
real (rk) :: b(size (a))
! *** end of interface ***
integer (c_size_t) :: n
! cast to size_t
n = size (a)
call rism_dst (n, b, a)
end function dst
subroutine test_dst (n)
implicit none
integer, intent (in) :: n
! *** end of interface ***
real (rk) :: a(n)
call random_number (a)
! RODFT11 (DST-IV) is self inverse up to a normalization factor:
if (maxval (abs (a - dst (dst (a)) / (2 * n))) > 1.0e-10) then
print *, "diff=", maxval (abs (a - dst (dst (a)) / (2 * n)))
stop "unnormalized DST does not match"
endif
end subroutine test_dst
subroutine test_ft (rmax, n)
implicit none
integer, intent (in) :: n
real (rk), intent (in) :: rmax
! *** end of interface ***
real (rk) :: r(n), k(n), f(n), g(n), h(n)
real (rk) :: dr, dk
integer :: i
!
! The 3d analytical unitary FT of the function
!
! f = exp(-r²/2)
!
! is the function
!
! g = exp(-k²/2), FIXME!
!
dr = rmax / n
dk = pi / rmax
forall (i = 1:n)
r(i) = (i - 0.5) * dr
k(i) = (i - 0.5) * dk
end forall
! Gaussian:
f = exp (-r**2 / 2)
! Unit "charge", a "fat" delta function:
f = f / (integrate (f) * dr**3)
! Forward transform:
g = fourier (f) * (dr**3 / FT_FW)
! Backward transform:
h = fourier (g) * (dk**3 / FT_BW)
print *, "# norm (f )^2 =", integrate (f**2) * dr**3
print *, "# norm (g )^2 =", integrate (g**2) * dk**3
print *, "# norm (f')^2 =", integrate (h**2) * dr**3
print *, "# |f - f'| =", maxval (abs (f - h))
print *, "# int (f') =", integrate (h) * dr**3
print *, "# int (f ) =", integrate (f) * dr**3
! This should correspond to the convolution (f * f) which should
! be again a gaussian, twice as "fat":
h = g * g
h = fourier (h) * (dk**3 / FT_BW)
print *, "# int (h ) =", integrate (h) * dr**3
! Compare width as <r^2>:
print *, "# sigma (f) =", integrate (r**2 * f) * dr**3
print *, "# sigma (h) =", integrate (r**2 * h) * dr**3
! print *, "# n=", n
! print *, "# r, f, k, g, h = (f*f)"
! do i = 1, n
! print *, r(i), f(i), k(i), g(i), h(i)
! enddo
end subroutine test_ft
end module fft