-
Notifications
You must be signed in to change notification settings - Fork 0
/
fftw_module.F90
246 lines (186 loc) · 10.1 KB
/
fftw_module.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
module fftw_module
use amr_utils
use, intrinsic :: iso_c_binding
implicit none
include 'fftw3.f03'
contains
subroutine DFT_1D(real_field, complex_field, N)
! Transform purely real field into complex field
integer, intent(in) :: N ! Size of grid
integer(kind=C_INT) :: N_c ! As C integer
real(kind=dp), intent(in) :: real_field(0:N-1)
! Scalar field
complex(kind=C_DOUBLE_COMPLEX), intent(out) :: complex_field(0:N-1)
! Complex field output
type(C_PTR) :: plan ! FFTW plan pointer
N_c = N
complex_field = cmplx(real_field, kind=C_DOUBLE_COMPLEX)
plan = fftw_plan_dft_1d(N_c, complex_field, complex_field,&
&FFTW_FORWARD, FFTW_ESTIMATE)
!write (6,*) "Performing FFT..."
call fftw_execute_dft(plan, complex_field, complex_field)
complex_field = complex_field / sqrt(real(N, dp))
call fftw_destroy_plan(plan)
return
end subroutine DFT_1D
subroutine DFT_2D(real_field, complex_field, N)
! Transform purely real field into complex field
integer, intent(in) :: N ! Size of grid
integer(kind=C_INT) :: N_c ! As C integer
real(kind=dp), intent(in) :: real_field(0:N-1, 0:N-1)
! Scalar field
complex(kind=C_DOUBLE_COMPLEX), intent(out) :: complex_field(0:N-1, 0:N-1)
! Complex field output
type(C_PTR) :: plan ! FFTW plan pointer
N_c = N
complex_field = cmplx(real_field, kind=C_DOUBLE_COMPLEX)
plan = fftw_plan_dft_2d(N_c, N_c, complex_field, complex_field,&
&FFTW_FORWARD, FFTW_ESTIMATE)
!write (6,*) "Performing FFT..."
call fftw_execute_dft(plan, complex_field, complex_field)
complex_field = complex_field / sqrt(real(N**2, dp))
call fftw_destroy_plan(plan)
return
end subroutine DFT_2D
subroutine DFT_3D(real_field, complex_field, N)
! Transform purely real field into complex field
integer, intent(in) :: N ! Size of grid
integer(kind=C_INT) :: N_c ! As C integer
real(kind=dp), intent(in) :: real_field(0:N-1, 0:N-1, 0:N-1)
! Scalar field
complex(kind=C_DOUBLE_COMPLEX), intent(out) :: complex_field(0:N-1, 0:N-1, 0:N-1)
! Complex field output
type(C_PTR) :: plan ! FFTW plan pointer
N_c = N
complex_field = cmplx(real_field, kind=C_DOUBLE_COMPLEX)
plan = fftw_plan_dft_3d(N_c, N_c, N_c, complex_field, complex_field,&
&FFTW_FORWARD, FFTW_ESTIMATE)
!write (6,*) "Performing FFT..."
call fftw_execute_dft(plan, complex_field, complex_field)
complex_field = complex_field / sqrt(real(N**3, dp))
call fftw_destroy_plan(plan)
return
end subroutine DFT_3D
subroutine FFT_1D(complex_field_in, complex_field_out, N)
integer, intent(in) :: N ! Size of grid
complex(kind=C_DOUBLE_COMPLEX), intent(inout) :: complex_field_in(0:N-1)
! Complex field input
complex(kind=C_DOUBLE_COMPLEX), intent(out) :: complex_field_out(0:N-1)
! Complex field output
call FFT_1D_int(complex_field_in, complex_field_out, N, .TRUE.)
end subroutine FFT_1D
subroutine IFT_1D(complex_field_in, complex_field_out, N)
integer, intent(in) :: N ! Size of grid
complex(kind=C_DOUBLE_COMPLEX), intent(inout) :: complex_field_in(0:N-1)
! Complex field input
complex(kind=C_DOUBLE_COMPLEX), intent(out) :: complex_field_out(0:N-1)
! Complex field output
call FFT_1D_int(complex_field_in, complex_field_out, N, .FALSE.)
end subroutine IFT_1D
subroutine FFT_1D_int(complex_field_in, complex_field_out, N, forwards)
! Transform complex field into complex field
integer, intent(in) :: N ! Size of grid
complex(kind=C_DOUBLE_COMPLEX), intent(inout) :: complex_field_in(0:N-1)
! Complex field input
complex(kind=C_DOUBLE_COMPLEX), intent(out) :: complex_field_out(0:N-1)
! Complex field output
logical, intent(in) :: forwards ! Forwards transform?
integer(kind=C_INT) :: N_c ! As C integer
type(C_PTR) :: plan ! FFTW plan pointer
N_c = N
if (forwards) then
plan = fftw_plan_dft_1d(N_c, complex_field_in, complex_field_out,&
&FFTW_FORWARD, FFTW_ESTIMATE)
else
plan = fftw_plan_dft_1d(N_c, complex_field_in, complex_field_out,&
&FFTW_BACKWARD, FFTW_ESTIMATE)
end if
!write (6,*) "Performing FFT..."
call fftw_execute_dft(plan, complex_field_in, complex_field_out)
complex_field_out = complex_field_out / sqrt(real(N, dp))
call fftw_destroy_plan(plan)
return
end subroutine FFT_1D_int
subroutine FFT_2D(complex_field_in, complex_field_out, N)
integer, intent(in) :: N ! Size of grid
complex(kind=C_DOUBLE_COMPLEX), intent(inout) :: &
&complex_field_in(0:N-1, 0:N-1) ! Complex field input
complex(kind=C_DOUBLE_COMPLEX), intent(out) :: &
&complex_field_out(0:N-1, 0:N-1) ! Complex field output
call FFT_2D_int(complex_field_in, complex_field_out, N, .TRUE.)
end subroutine FFT_2D
subroutine IFT_2D(complex_field_in, complex_field_out, N)
integer, intent(in) :: N ! Size of grid
complex(kind=C_DOUBLE_COMPLEX), intent(inout) :: &
&complex_field_in(0:N-1, 0:N-1) ! Complex field input
complex(kind=C_DOUBLE_COMPLEX), intent(out) :: &
&complex_field_out(0:N-1, 0:N-1) ! Complex field output
call FFT_2D_int(complex_field_in, complex_field_out, N, .FALSE.)
end subroutine IFT_2D
subroutine FFT_2D_int(complex_field_in, complex_field_out, N, forwards)
! Transform complex field into complex field
integer, intent(in) :: N ! Size of grid
complex(kind=C_DOUBLE_COMPLEX), intent(inout) :: &
&complex_field_in(0:N-1, 0:N-1) ! Complex field input
complex(kind=C_DOUBLE_COMPLEX), intent(out) :: &
&complex_field_out(0:N-1, 0:N-1) ! Complex field output
logical, intent(in) :: forwards ! Forwards transform?
integer(kind=C_INT) :: N_c ! As C integer
type(C_PTR) :: plan ! FFTW plan pointer
N_c = N
if (forwards) then
plan = fftw_plan_dft_2d(N_c, N_c, complex_field_in, complex_field_out,&
&FFTW_FORWARD, FFTW_ESTIMATE)
else
plan = fftw_plan_dft_2d(N_c, N_c, complex_field_in, complex_field_out,&
&FFTW_BACKWARD, FFTW_ESTIMATE)
end if
!write (6,*) "Performing FFT..."
call fftw_execute_dft(plan, complex_field_in, complex_field_out)
complex_field_out = complex_field_out / sqrt(real(N**2, dp))
call fftw_destroy_plan(plan)
return
end subroutine FFT_2D_int
subroutine FFT_3D(complex_field_in, complex_field_out, N)
integer, intent(in) :: N ! Size of grid
complex(kind=C_DOUBLE_COMPLEX), intent(inout) :: &
&complex_field_in(0:N-1, 0:N-1) ! Complex field input
complex(kind=C_DOUBLE_COMPLEX), intent(out) :: &
&complex_field_out(0:N-1, 0:N-1) ! Complex field output
call FFT_3D_int(complex_field_in, complex_field_out, N, .TRUE.)
end subroutine FFT_3D
subroutine IFT_3D(complex_field_in, complex_field_out, N)
integer, intent(in) :: N ! Size of grid
complex(kind=C_DOUBLE_COMPLEX), intent(inout) :: &
&complex_field_in(0:N-1, 0:N-1, 0:N-1) ! Complex field input
complex(kind=C_DOUBLE_COMPLEX), intent(out) :: &
&complex_field_out(0:N-1, 0:N-1, 0:N-1) ! Complex field output
call FFT_3D_int(complex_field_in, complex_field_out, N, .FALSE.)
end subroutine IFT_3D
subroutine FFT_3D_int(complex_field_in, complex_field_out, N, forwards)
! Transform complex field into complex field
integer, intent(in) :: N ! Size of grid
complex(kind=C_DOUBLE_COMPLEX), intent(inout) :: &
&complex_field_in(0:N-1, 0:N-1, 0:N-1) ! Complex field input
complex(kind=C_DOUBLE_COMPLEX), intent(out) :: &
&complex_field_out(0:N-1, 0:N-1, 0:N-1) ! Complex field output
logical, intent(in) :: forwards ! Forwards transform?
integer(kind=C_INT) :: N_c ! As C integer
type(C_PTR) :: plan ! FFTW plan pointer
N_c = N
if (forwards) then
plan = fftw_plan_dft_3d(N_c, N_c, N_c, complex_field_in,&
&complex_field_out, FFTW_FORWARD,&
&FFTW_ESTIMATE)
else
plan = fftw_plan_dft_3d(N_c, N_c, N_c, complex_field_in,&
&complex_field_out, FFTW_BACKWARD,&
&FFTW_ESTIMATE)
end if
!write (6,*) "Performing FFT..."
call fftw_execute_dft(plan, complex_field_in, complex_field_out)
complex_field_out = complex_field_out / sqrt(real(N**3, dp))
call fftw_destroy_plan(plan)
return
end subroutine FFT_3D_int
end module fftw_module