Lecture22a

Lecture 22
MA471 Fall 2003
Advection Equation
• Recall the 2D advection equation:
C
x C
y C
a
a
0
t
x
y
• We will use a Runge-Kutta time integrator
and spectral representation in space.
Periodic Data
• Let’s assume we are given N values of a
function f at N data points on the unit
interval.
• For instance N=8 on a unit interval:
0
1/8
2/8
3/8
4/8
5/8
6/8
7/8
8/8
Discrete Fourier Transform
• We can seek a trigonometric interpolation
of a function f as a linear combination of N
(even) trigonometric functions:
If  x  
• Such that:
f  x j   If  x j  
j
where x j 
N
k N / 2

k  N / 2 1
kN / 2

k  N / 2 1
ˆf eik 2 x
k
ˆf eik 2 x j for j  1,..., N
k
Transform
• The interpolation formula defines a linear
system for the unknown fhat coefficients:
fj 
k N / 2

k  N / 2 1
ˆf  1
k
N
jN
ˆf eik 2 x j for j  1,..., N
k
fe
j
j 1
 ik 2 x j
N
N
for k      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:
If  x  
k N / 2

k  N / 2 1
k N / 2
ˆf ei 2 kx
k
dIf
ik 2 x
ˆ
 x     ik 2  f k e
dx
k  N / 2 1
Detail
• We note that the derivative of the k=(N/2) mode
 iN2 2 x ˆ 
fN 
e
2 

• is technically:
iN
2 x


iN
2
ˆ
2  e
fN 
2
2 

• However, as we show on the next slide – this
mode has turning points at all the data points.
Real Component of N/2 Mode
i.e. the slope of the k=(N/2) mode is zero
at all the 8 points..
Modified Derivative Formula
• So we can create an alternative symmetric
derivative formula:
dIf
1
 x 
dx
2
k  N / 2 1

k  N / 2 1
ik 2 x
ˆ
 ik 2  f k e
Spectral Differentiation Scheme
1) Use an fft to compute:
ˆf for k    N   1,   N   2,..., 1,0,1,...,  N 
k
 
 
 
2
2
2
2) Differentiate in spectral space. i.e.
compute:

N
N
ˆ
 i 2 k  f k for k    2   1,...,  2   1
 
 
gˆ k  
N

0 for k 

2
cont
3) Then:
k  N / 2 
dIf
ikx
 x    gˆ k e
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ˆ
2
1
2

N
1
2
, fˆ

N
2
2
,..., fˆ1
• The derivative matrix will now be a matrix
with diagonal entries:
N
N
N


i 2  0,1,..,  1, 0,   1,   2,..., 1 
2
2
2


Spectral
Differentiation
Code
Typo:
See corrected
Code on webpage
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
jN / 2
k N / 2
variables..
ˆ eij 2 x eik 2 y
If  x, y   
f
 jk
k  N / 2 1 j  N / 2 1
• The 2D inverse discrete transform and
discrete transform are:
f nm 
k N / 2

jN / 2

k  N / 2 1 j  N / 2 1
ˆf  1
jk
N2
n N m N
 f
n 1 m 1
nm
e
fˆjk eij 2 xn eik 2 ym for n, m  1,..., N
 ij 2 xn  ik 2 ym
e
N
N
for j,k      1,...,
2
2
Advection Equation
• Recall the 2D advection equation:
C
x C
y C
a
a
0
t
x
y
• We will use a Runge-Kutta time integrator
and spectral representation in space.
Runge-Kutta Time Integrator
• We will now discuss a particularly simple
Runge-Kutta time integrator introduced by
Jameson-Schmidt-Turkel
• The idea is each time step is divided into s
substeps, which taken together
approximate the update to s’th order.
Side note: Jameson-SchmidtTurkel Runge-Kutta Integrator
• Taylor’s theorem tell’s us that
 dt  d  dt 2  d 2
dt s
C  t  dt   1    
 ... 
 1!  dt  2!  dt 
s!

d 
 
 dt 
s

 C  t 

dt s1  d s1C  *
*

t
for
some
t
 t , t  dt 
 s1   
 s  1!  dt 
• We will compute an approximate update
as:
s
s
 dt  d  dt 2  d 2
dt  d  
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 s
C  t  dt   1    
 ... 
 1!  dt  2!  dt 
s!

2
2
s

dt
d
dt
d
dt




 C n1  1    
 ... 
 1!  dt  2!  dt 
s!

d 
 
 dt 
d 
 
 dt 
• We then factorize the polynomial
derivative term:
s
s

 C  t 

 n
 C

Factorized Scheme
2
2
s

dt
d
dt
d
dt




C n1  1    
 ... 


 1!  dt  2!  dt 
s!

d 
 
 dt 
s
 n
 C

n 



dt
d
dt
d
dt
d
dt
dC


n+1
n
n
n
n
C C 
 ..  C 
  
C 
C 
s dt 
s  1 dt 
s  2 dt  
1 dt   

Set C=C n
for k  s : 1:1
dt dC
CC 
k dt
end
n
C n+1 =C
JST + Advection Equation
• We now use the PDE definition
Set C=C n
for k  s : 1:1
C  Cn 
+
dt dC
k dt
C
C
C
 ax
 ay
0
t
x
y
end
C n+1 =C
Set C=Cn
for k  s : 1:1
dt  x C
y C 
C  C  a
a
k  x
y 
end
n
Cn+1 =C
Now Use Spectral Representation
A time step now consists of s substages.
Each stage involves:
a) Fourier transforming the ctilde
using a fast Fourier transform (fft)
b) Scaling the coefficients to
differentiate in Fourier space
c) Transforming the derivatives back to
physical values at the nodes by
inverse fast Fourier transform (ifft).
d) Finally updating ctilde according to
the advection equation.
At the end we update the concentration.
Set c  c
for k  s : 1:1
cˆ = fft  c 
dˆ x  D x cˆ
dˆ y  D y cˆ
 
 ifft  dˆ 
d x  ifft dˆ x
dy
y
dt x x
c j  c j   a j d j  a yj d yj  for j=1,..,N
k
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 RungeKutta time step
35) fft ctilde
37) x- and ydifferentiation in
Fourier space
40-41) transform back
to physical values
of derivatives
43) Update ctilde
C
x C
y C
a
a
0
t
x
y
C  t  0, x, y   e

a x   sin  2 x  cos  2 x 
200  x .75   y .6 
2
2

a y  cos  2 x  sin  2 x 
FFT Resources
• Check out: http://www.fftw.org/ for the
fastest Fourier transform in the West
• Great list of links:
– http://www.fftw.org/links.html
• FFT for irregularly spaced data:
– http://www.math.mu-luebeck.de/potts/nfft/
Lab Exercise
1) Download example DFT code from website.
2) Benchmark codes as a function of N
3) Write your own version using an efficient FFT
(say fftw) which:
1) FFT’s data
2) Scales coefficients
3) IFFT’s scaled coeffients
4) Write an advection code using the JST RungeKutta time integrator.
This is the scalar version of Project 4
Upshot: 2D Advection/DFT
Using MPI_Alltoall
Note: using fft the compute time will drop by approx.
A factor of 30 (for N=256)
Upshot: 2D Advection/DFT
Using Non-Blocked Sends
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 