Lecture 22 A

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 32

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:
k N / 2
If  x   
k  N / 2  1
ˆf eik 2 x
k

• Such that:
kN / 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

jN
ˆ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 

• 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:
k  N / 2  1
dIf 1
dx
 x 
2

k  N / 2  1
 ik 2  f k e
ˆ ik 2 x
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
k  N / 2 
3) Then: dIf
 x    gˆ k e ikx

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

• 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
variables.. k N / 2 jN / 2
If  x, y   fˆ eij 2 x eik 2 y  
k  N / 2  1 j  N / 2  1
jk

• The 2D inverse discrete transform and


discrete transform are:
k N / 2 jN / 2
f nm   
k  N / 2  1 j  N / 2  1
fˆjk eij 2 xn eik 2 ym for n, m  1,..., N

n  N m N
ˆf  1 N N
jk
N2
 f
n 1 m1
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

• 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-Schmidt-
Turkel Runge-Kutta Integrator
• Taylor’s theorem tell’s us that
 dt  d  dt 2  d 2 dt  d  
s s

C  t  dt   1      ...     C  t 
 1!  dt  2!  dt  s !  dt  

dt s1  d s1C  *
  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 n1   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
CC  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ˆ

Transforming the derivatives back to  


d x  ifft dˆ x
physical values at the nodes by
inverse fast Fourier transform (ifft). dy  ifft  dˆ  y

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

• 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 Runge-
Kutta 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 

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy