-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfc_test.f08
233 lines (182 loc) · 7.38 KB
/
fc_test.f08
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
! ---------------------------------------------------------------------
MODULE fft_mod
! ---------------------------------------------------------------------
! Simple unoptimized fft borrowed from
! https://rosettacode.org/wiki/Fast_Fourier_transform#Fortran.
!
IMPLICIT NONE
INTEGER, PARAMETER :: wp=selected_real_kind(15,300)
REAL(KIND=wp), PARAMETER :: pi=3.141592653589793238460_wp
CONTAINS
! ---------------------------------------------------------------------
RECURSIVE SUBROUTINE fft(x)
! ---------------------------------------------------------------------
! In place Cooley-Tukey FFT
!
COMPLEX(KIND=wp), DIMENSION(:), INTENT(INOUT) :: x
COMPLEX(KIND=wp) :: t
INTEGER :: N
INTEGER :: i
COMPLEX(KIND=wp), DIMENSION(:), ALLOCATABLE :: even, odd
N = SIZE(x)
IF ( N .le. 1 ) RETURN
ALLOCATE(odd((N+1)/2))
ALLOCATE(even(N/2))
! Divide
odd =x(1:N:2)
even=x(2:N:2)
! Conquer
CALL fft(odd)
CALL fft(even)
! Combine
DO i=1,N/2
t = EXP(CMPLX(0.0_wp,-2.0_wp*pi*REAL(i-1,wp)/REAL(N,wp),KIND=wp))*even(i)
x(i) = odd(i) + t
x(i+N/2) = odd(i) - t
END DO
DEALLOCATE(odd)
DEALLOCATE(even)
END SUBROUTINE fft
END MODULE fft_mod
! =====================================================================
PROGRAM FC_TEST
! =====================================================================
USE fft_mod
REAL (KIND=wp) , DIMENSION(:,:), ALLOCATABLE :: Q,A,Qder
COMPLEX(KIND=wp), DIMENSION(:), ALLOCATABLE :: f
REAL(KIND=wp) , DIMENSION(:), ALLOCATABLE :: fp,x,k
REAL(KIND=wp) :: dxp
INTEGER :: i, n, c, d, tkind
CHARACTER (LEN=100) :: odir ! Dummy variable
CHARACTER (LEN=5) :: Cstr,dstr
! Total number of points, continuation points, and matching points
! Neum is currently unused
NAMELIST / required / odir,d,c,tkind
OPEN(1,file='parameter.inp',status='unknown',form="formatted")
READ(1,NML=required)
CLOSE(1)
CALL get_command_argument(1, Cstr)
IF ( LEN_TRIM(Cstr) == 0 ) THEN
PRINT*, "Usage: fc_test N, with N > c the total grid size.",&
"N must be a power of 2."
STOP
ELSE
READ(Cstr,*) n
IF ( n <= c) THEN
PRINT*, "Usage: fc_test N, with N > c the total grid ",&
"size. N must be a power of 2."
STOP
ENDIF
ENDIF
n = n - c
ALLOCATE ( f(n+c), fp(n), x(n), k(n+c) )
ALLOCATE ( Q(d,d) )
! Grid from x=0 to x=10, function and derivative
x = (/( i*1.0_wp/(n-1), i=0,n-1 )/)
f(1:n) = BESSEL_J0(20*x)*EXP(3*x/x(n))
fp = -20*BESSEL_J1(20*x)*EXP(3*x/x(n)) + &
BESSEL_J0(20*x)*EXP(3*x/x(n))*3/x(n)
! Load Q
WRITE(dstr,'(I5)') d
OPEN(10, FILE=trim(odir) // '/Q' // trim(adjustl(dstr)) // '.dat',&
FORM='unformatted', ACCESS='stream')
READ(10) Q
CLOSE(10)
IF (tkind .eq. 0) THEN
100 FORMAT( "Testing derivative estimation using ", i0, " matching ", &
"points and ", i0, " continuation points.")
WRITE(*,100) d, c
! Load A
ALLOCATE( A(c,d) )
WRITE(Cstr,'(I5)') c
OPEN(10, FILE=trim(odir) // '/A' // trim(adjustl(Cstr)) // '-' // &
trim(adjustl(dstr)) // '.dat', &
FORM='unformatted', ACCESS='stream')
READ(10) A
CLOSE(10)
! Wavenumbers
DO i = 1,(n+c)/2
k(i) = REAL(i-1,KIND=wp)
k(i+(n+c)/2) = REAL(i-(n+c)/2-1,KIND=wp)
END DO
k = 2*pi*k/( x(2)*(n+c) ) ! k = 2*pi*k_ind/L
! Perform continuation. First contribution of last d points,
! then add contribution of first d points.
f(n+1:n+c) = MATMUL(A, MATMUL(TRANSPOSE(Q), f(n-d+1:n)) )
f(n+1:n+c) = f(n+1:n+c) + &
MATMUL(A(c:1:-1,:), MATMUL(TRANSPOSE(Q), f(d:1:-1)) )
! Transform, derivate, antitransform
! ifft can be obtained as conj(fft(conj(f_k))).
! For real signals it is redundant anyway.
CALL fft(f)
f = CMPLX(0.0_wp, 1.0_wp)*k*f
f = CONJG(f)
CALL fft(f)
f = CONJG(f)/(n+c)
! Print error comparison
PRINT*, "Maximum relative error: ", &
MAXVAL(ABS(REAL(f(1:n),kind=wp) - fp)/ABS(fp))
PRINT*, "Average relative error: ", &
SUM(ABS(REAL(f(1:n),kind=wp) - fp)/ABS(fp))/n
! Neumann reconstruction
ELSEIF (tkind .eq. 1) THEN
101 FORMAT( "Testing endpoints reconstruction from the prescription of ", &
"the normal derivative employing ", i0, " matching points.")
WRITE(*,101) d
! Load Q2n
ALLOCATE(Qder(d,d))
OPEN(10, FILE=trim(odir) // '/Qn' // trim(adjustl(dstr)) // '.dat',&
FORM='unformatted', ACCESS='stream')
READ(10) dxp
READ(10) Qder
CLOSE(10)
fp(2:d) = f(2:d)
fp(n-d+1:n-1) = f(n-d+1:n-1)
fp(1) = fp(1)*(-x(2)/dxp)
fp(n) = fp(n)*(x(2)/dxp)
fp(1) = DOT_PRODUCT(Q(d,:), MATMUL(TRANSPOSE(Qder), fp(d:1:-1)))
fp(n) = DOT_PRODUCT(Q(d,:), MATMUL(TRANSPOSE(Qder), fp(n-d+1:n)))
! Print error
PRINT*, "Relative reconstruction error at x=0: ", &
ABS((fp(1)-f(1))/f(1))
PRINT*, "Relative reconstruction error at x=L: ", &
ABS((fp(n)-f(n))/f(n))
! Second normal derivative reconstruction
ELSEIF (tkind .eq. 2) THEN
102 FORMAT( "Testing endpoints reconstruction from the prescription of ", &
"the 2nd normal derivative employing ", i0, " matching points.")
WRITE(*,102) d
! Load Q2n
ALLOCATE(Qder(d,d))
OPEN(10, FILE=trim(odir) // '/Q2n' // trim(adjustl(dstr)) // '.dat',&
FORM='unformatted', ACCESS='stream')
READ(10) dxp
READ(10) Qder
CLOSE(10)
fp(2:d) = f(2:d)
fp(n-d+1:n-1) = f(n-d+1:n-1)
fp(1) = -20*20/2*(BESSEL_J0(20*x(1))-BESSEL_JN(2,20*x(1)))*EXP(3*x(1)/x(n)) &
-2*(20*BESSEL_J1(20*x(1))*EXP(3*x(1)/x(n))*3/x(n)) &
+BESSEL_J0(20*x(1))*EXP(3*x(1)/x(n))*(3/x(n))**2
fp(n) = -20*20/2*(BESSEL_J0(20*x(n))-BESSEL_JN(2,20*x(n)))*EXP(3*x(n)/x(n)) &
-2*(20*BESSEL_J1(20*x(n))*EXP(3*x(n)/x(n))*3/x(n)) &
+BESSEL_J0(20*x(n))*EXP(3*x(n)/x(n))*(3/x(n))**2
fp(1) = fp(1)*(x(2)/dxp)**2
fp(n) = fp(n)*(x(2)/dxp)**2
fp(1) = DOT_PRODUCT(Q(d,:), MATMUL(TRANSPOSE(Qder), fp(d:1:-1)))
fp(n) = DOT_PRODUCT(Q(d,:), MATMUL(TRANSPOSE(Qder), fp(n-d+1:n)))
! Print error
PRINT*, "Relative reconstruction error at x=0: ", &
ABS((fp(1)-f(1))/f(1))
PRINT*, "Relative reconstruction error at x=L: ", &
ABS((fp(n)-f(n))/f(n))
ENDIF
! Clean up
DEALLOCATE( x,f,fp )
DEALLOCATE( Q )
IF (tkind .eq. 0) THEN
DEALLOCATE( A )
ELSEIF ( tkind .ge. 0) THEN
DEALLOCATE( Qder )
ENDIF
END PROGRAM FC_TEST