Lecture 22 A
Lecture 22 A
Lecture 22 A
• Such that:
kN / 2
f x j If x j ˆf eik 2 x j for j 1,..., N
k
k N / 2 1
j
where x j
N
Transform
• The interpolation formula defines a linear
system for the unknown fhat coefficients:
k N / 2
fj
k N / 2 1
ˆf eik 2 x j for j 1,..., N
k
jN
ˆf 1 N N
fe
ik 2 x j
k j for k 1,...,
N j 1 2 2
Or:
f ifft fˆ
fˆ fft f
Code for
the DFT
Code for
Inverse
DFT
Fast Fourier Transform
• See handout
Spectral Derivative
• We can differentiate the interpolant by:
k N / 2
If x
k N / 2 1
ˆf ei 2 kx
k
k N / 2
dIf
x ik 2 f k e
ˆ ik 2 x
dx k N / 2 1
Detail
• We note that the derivative of the k=(N/2) mode
iN2 2 x ˆ
e fN
2
iN iN
2 x
ˆ
• is technically: 2 e 2
fN
2 2
dx k N / 2 1
4) Summary:
a) fft transform data to compute coefficients
b) scale Fourier coefficients
c) inverse fft (ifft) scaled coefficients
Final Twist
• Matlab stores the coefficients from the fast
Fourier transform in a slightly odd order:
fˆ0 , fˆ1 ,.., fˆN , fˆN , fˆ N , fˆ N ,..., fˆ1
1 1 2
2 2 2 2
1) DFT data
2) Scale Fourier
coefficients
3) IFT scaled coefficients
Two-Dimensional Fourier
Transform
• We can now construct a Fourier expansion
in two variables for a function of two
variables.. k N / 2 jN / 2
If x, y fˆ eij 2 x eik 2 y
k N / 2 1 j N / 2 1
jk
n N m N
ˆf 1 N N
jk
N2
f
n 1 m1
nm e ij 2 xn ik 2 ym
e for j,k 1,...,
2 2
Advection Equation
• Recall the 2D advection equation:
C x C y C
a a 0
t x y
C t dt 1 ... C t
1! dt 2! dt s ! dt
dt s1 d s1C *
s 1 t , t dt
*
t for some t
s 1 ! dt
• We will compute an approximate update
as: dt d dt 2 d 2 dt d
s s
C t dt 1 ... C t
1! dt 2! dt s ! dt
JST Runge-Kutta
• The numerical scheme will look like
dt d dt 2 d 2 dt d
s s
C t dt 1 ... C t
1! dt 2! dt s ! dt
n 1
2
dt d dt d
2
dt d n
s s
C 1 ... C
1! dt 2! dt s ! dt
• We then factorize the polynomial
derivative term:
Factorized Scheme
dt d dt 2
d
2
dts
d
s
n
C n1 1 ... C
1! dt 2! dt s! dt
n
dt d dt d dt d dt dC
C C C C .. C
n+1 n n n n
s dt s 1 dt s 2 dt 1 dt
Set C=C n
for k s : 1:1
dt dC
CC n
k dt
end
Cn+1 =C
JST + Advection Equation
• We now use the PDE definition
Set C=C n
for k s : 1:1 C C C
ax ay 0
C Cn
dt dC + t x y
k dt
end
Cn+1 =C
Set C=C n
for k s : 1:1
dt x C y C
C C an
a
k x y
end
Cn+1 =C
Now Use Spectral Representation
Set c c
time step now consists of s substages.
for k s : 1:1
ach stage involves:
cˆ = fft c
Fourier transforming the ctilde
using a fast Fourier transform (fft) dˆ x D x cˆ
Scaling the coefficients to
differentiate in Fourier space dˆ y D y cˆ
dt x x
c j c j a j d j a yj d yj for j=1,..,N
Finally updating ctilde according to
the advection equation.
k
t the end we update the concentration. end
c=c
Matlab
Implementation
• First set up the
nodes and the
differentiation
scalings.
24) Compute dt
26) Initiate
concentration
28-29) compute
advection vector
32-45) full Runge-
Kutta time step
35) fft ctilde
37) x- and y-
differentiation in
Fourier space
40-41) transform back
to physical values
of derivatives
43) Update ctilde
C x C y C
a a 0 a x sin 2 x cos 2 x
t x y
a y cos 2 x sin 2 x
C t 0, x, y e
200 x .75 y .6
2 2
FFT Resources
• Check out: http://www.fftw.org/ for the
fastest Fourier transform in the West
Note: I isend and irecv, then compute the x-derivatives, then waitall, then compute
the y-derivatives, then isend/recv and waitall (not the best option perhaps).
With FFT
• Replacing the DFT calls with FFT calls will
radically reduce the computation time.
• It will be much harder to hide the
communication time behind the compute
time.
• Good luck