25
15. Spectral numerical differentiation

15. Spectral numerical differentiation - 國立臺灣大學homepage.ntu.edu.tw/~wttsai/fortran/ppt/15.Spectral...•This justifies the use of spectral numerical differentiation. •Write

  • Upload
    others

  • View
    5

  • Download
    0

Embed Size (px)

Citation preview

Page 1: 15. Spectral numerical differentiation - 國立臺灣大學homepage.ntu.edu.tw/~wttsai/fortran/ppt/15.Spectral...•This justifies the use of spectral numerical differentiation. •Write

15. Spectral numerical differentiation

Page 2: 15. Spectral numerical differentiation - 國立臺灣大學homepage.ntu.edu.tw/~wttsai/fortran/ppt/15.Spectral...•This justifies the use of spectral numerical differentiation. •Write

Spectral numerical differentiation

For continuous periodic function � � , � � + 2� = � � , represented by a Fourier series:

� � = � ��� ����

�� ��

The differentiation of �(�)can then be evaluated by:

��(�)

��= � (�����) �

���

�� ��

Fourier coefficient of �′(�)

∴ Once the coefficients of the Fourier series ��� is obtained, the differentiation can be

evaluated by summing the Fourier series with new coefficients (�����)

Page 3: 15. Spectral numerical differentiation - 國立臺灣大學homepage.ntu.edu.tw/~wttsai/fortran/ppt/15.Spectral...•This justifies the use of spectral numerical differentiation. •Write

• Now, for discrete periodic function �� defined at ��, � = 0,1,… ,� − 1:

�� = � ��� �����

����

�����

= � ��� �����

���

�� �

• Can the differentiation of � defined at ��, i.e., ��

����

be evaluated by:

��

����

= � (��)��� �����

����

�����

���

�����

= � �� ���� ����� = − � ����� �

����

����

�����

����

�����

Similarly,

� � � = 2�

��

�� = �(��)

�� �� ��

⋯ ⋯ ⋯ ⋯

��� =1

�� � �� ���������

�� �

where ��� is the discrete Fourier transform:

Page 4: 15. Spectral numerical differentiation - 國立臺灣大學homepage.ntu.edu.tw/~wttsai/fortran/ppt/15.Spectral...•This justifies the use of spectral numerical differentiation. •Write

��

����

= � (�����) �����

����

�� ���

= � −�

2����� �� �

��

����+ � (�����) �

����

��

�� �����

+ 0 + � ����� �����

����

�� �

= − ��

2����� − 1 � + � − �� ���� ���� ��

� �����

+ � ����� �����

����

�� �

= − ��

2����� − 1 � + � �� − ���

∗ ������ + ��� �����

����

�� �

= − ��

2����� − 1 � + � ��

����

�� �

(− ���� cos��� + ���

� sin��� + ���� cos��� − ���

� sin���)

+ �(���� cos��� + ���

� sin��� + ���� cos��� + ���

� sin���)

realcomplex

Since the derivative is real value, this complex term must be set to zero.

• First derivative:

�����

is real∵

�� =2�

��

0 1 2� =

0 2�

�� − 1

�� = ��

Page 5: 15. Spectral numerical differentiation - 國立臺灣大學homepage.ntu.edu.tw/~wttsai/fortran/ppt/15.Spectral...•This justifies the use of spectral numerical differentiation. •Write

��

����

= � (�����) �����

����

�� �����

�����

������� …… ���� ���� ��� ��� ��� …… ���

���

������

�����

�(����1)×

�������

……�(− 2)×����

�(− 1)×����

0 × ���� 1 ×���

� 2 ���

……�(�

��2)×

������

�(���1)×

������

�� = � ��� �����

����

�����

• Given the discrete periodic function �� defined at ��, � = 0,1,… ,� − 1:

• The 1st derivative of the discrete periodic function at ��, � = 0,1,… ,� − 1 can be evaluated by:

��

��

�� �� ��

⋯ ⋯ ⋯ ⋯

�����

������� …… ���� ���� ��� ��� ��� …… ���

���

������

����

������ …… ����� �����

complex conjugate

equal

only these coefficients are stored

• But, since ��� = ����� and ���� = ���∗, only store ���, � = 0,1,… ,�/2:

Page 6: 15. Spectral numerical differentiation - 國立臺灣大學homepage.ntu.edu.tw/~wttsai/fortran/ppt/15.Spectral...•This justifies the use of spectral numerical differentiation. •Write

�����

������� …… ���� ���� ��� ��� ��� …… ���

���

������

����

������ …… ����� �����

complex conjugate

equal

only these coefficients are stored

�����

�(����1)×

�������

……�(− 2)×����

�(− 1)×����

0 × ���� 1 ×���

� 2 ���

……�(�

��2)×

������

�(���1)×

������

����

− �(���1)×

�� ����

∗ ……− �(2)×

���∗

− �(1)×���∗

0 × ���� 1 ×���

� 2 ���

……�(�

��2)×

������

�(���1)×

������

����

− �(���1)×

�� ����

∗ ……− �(2)×

���∗

− �(1)×���∗

complex conjugate

equal

���� = ���∗��� = real, ��

���= real,• The discrete Fourier transform ��� has the properties: ��� = �����,

• Since �����∗= − �����

∗ , the un-stored DFFT of 1st derivatives are − �����∗:

• Apply the property ���� = ���∗, we have �(− �)���� = − �����

∗:

Page 7: 15. Spectral numerical differentiation - 國立臺灣大學homepage.ntu.edu.tw/~wttsai/fortran/ppt/15.Spectral...•This justifies the use of spectral numerical differentiation. •Write

� = 0 1 � − 2 � − 1

�� �� �� �� …… …… ���� ����

• The algorithm to compute first-order differentiation of discrete real �� using

spectral approximation is:

2. Compute ���� = ����� , � = 1,… ,

�− 1, and ��

���

� = ����

� ≡ 0

��

����

= � ���� �����

����

�� ���

3. Call backward FFT to compute

1. Given �� at ��, call forward FFT to compute ���: �� = � ��� �����

����

�� ���

= � ��� �����

���

�� �

� = 0 1 1 2 2����

����

��

���� ���

� ���� ���

� ���� …… …… ���

���

� ������

� ����

� = 0 1 1 2 2����

����

��

� − ���� ���

� − 2���� 2���

� …… …… − ���� ���

���

� ��− 1 ���

���

��

Page 8: 15. Spectral numerical differentiation - 國立臺灣大學homepage.ntu.edu.tw/~wttsai/fortran/ppt/15.Spectral...•This justifies the use of spectral numerical differentiation. •Write

���

�����

= � �� ���� �����

����

�� ���

= − −�

2

����� �� �

��

����+ � �� ���� �

����

��

�� �����

+ 0 + � �� ���� �����

����

�� �

= −��

4����� − 1 � + � − �� ����� �

��� ��

� �����

+ � �� ���� �����

����

�� �

= −��

4����� − 1 � + � − �� ���

∗ ������ + ��� �����

����

�� �

= −��

4����� − 1 � + � − ��

(���� cos��� − ���

� sin��� + ���� cos��� − ���

� sin���)

+ �(− ���� cos��� − ���

� sin��� + ���� cos��� + ���

� sin���)

����

�� �

= real

�� =2�

��

0 1 2� =

0 2�

�� − 1

�� = ��• Second derivative:

Page 9: 15. Spectral numerical differentiation - 國立臺灣大學homepage.ntu.edu.tw/~wttsai/fortran/ppt/15.Spectral...•This justifies the use of spectral numerical differentiation. •Write

• The algorithm to compute second-order differentiation of discrete real �� using

spectral approximation is:

2. Compute ����� = �� ���� = − �����, � = 1,… ,

�− 1

���

�����

= � ����� �����

����

�� ���

3. Call backward FFT to compute

1. Given �� at ��, call forward FFT to compute ���: �� = � ��� �����

����

�� ���� = 0 1 � − 2 � − 1

�� �� �� �� …… …… ���� ����

� = 0 1 1 2 2����

����

��

���� ���

� ���� ���

� ���� …… ���

� ���� …… ���

���

� ������

� ����

� = 0 1 1 2 2����

����

��

���� − ���

� − ���� − 2����

� − 2����� …… − �����

� − ������ …… −

2

����

Page 10: 15. Spectral numerical differentiation - 國立臺灣大學homepage.ntu.edu.tw/~wttsai/fortran/ppt/15.Spectral...•This justifies the use of spectral numerical differentiation. •Write

• The spectral derivative is much more accurate than any finite-difference schemes for periodic functions.

• The major cost involved is the use of fast Fourier transform.

• However, it is inaccurate and does not converge when the derivative is discontinuous.

Notes about spectral numerical differentiation:

Page 11: 15. Spectral numerical differentiation - 國立臺灣大學homepage.ntu.edu.tw/~wttsai/fortran/ppt/15.Spectral...•This justifies the use of spectral numerical differentiation. •Write

program t_specderiv! Example of computing first derivative of a real function ! using discrete spectral transform and calling NCAR FFTPACK

implicit noneinteger,parameter :: nn = 16real :: x(0:nn-1), f(0:nn-1), df(0:nn-1)real :: trig(2*nn+15), tmpreal,parameter :: pi = acos(-1.0)integer :: i, ii

!-- Give values: f(x) = cos2x + sin5xdo i = 0, nn-1

x(i) = 2.0*pi/nn*if(i) = cos(2.0*x(i))+sin(5.0*x(i))df(i) = -2.0*sin(2.0*x(i))+5.0*cos(5.0*x(i))

end do

!-- Forward transform to compute the complex coefficientscall rffti(nn,trig)call rfftf(nn,f,trig)

!-- Set 0 to the coefficient of nn/2 modef(nn-1) = 0.0 (continued)

Example of computing first derivative of a real function using discrete Fourier transform and calling NCAR FFTPACK

> gfortran t_specderiv.f90 NCAR_fft.f

� � = cos 2� + sin(5�)

Page 12: 15. Spectral numerical differentiation - 國立臺灣大學homepage.ntu.edu.tw/~wttsai/fortran/ppt/15.Spectral...•This justifies the use of spectral numerical differentiation. •Write

!-- Multiply and swap the Fourier coefficients for first derivativeii = 1do i = 1, nn-3, 2

tmp = -ii*f(i+1)f(i+1) = ii*f(i)f(i) = tmpii = ii + 1

end dof = f/nn

!-- Backward transformcall rfftb(nn,f,trig)

!–- Output and compare with exact valueswrite(*,*) ' j spectral exact'do i = 0, nn-1

write(*,*) i, f(i), df(i)end dowrite(*,*) ''write(*,*) 'Max error: ', maxval(abs(f-df))

end program

Page 13: 15. Spectral numerical differentiation - 國立臺灣大學homepage.ntu.edu.tw/~wttsai/fortran/ppt/15.Spectral...•This justifies the use of spectral numerical differentiation. •Write

j spectral exact0 4.9999995 5.00000001 -3.3276315 -3.32763122 -5.5355334 -5.53553303 3.2051830 3.20518404 -2.86202066E-08 -1.51398558E-065 -3.2051840 -3.20518336 5.5355349 5.53553397 3.3276296 3.32762658 -5.0000024 -5.00000059 0.49920809 0.4992060110 1.5355268 1.535532711 -6.0336103 -6.033614612 6.30433988E-06 3.45821547E-0713 6.0336065 6.033611814 -1.5355327 -1.535539915 -0.49920332 -0.49920550

Max error: 7.15255737E-06

� � = cos 2� + sin(5�)

��(�)

��= − 2 sin2� + 5 cos(5�)

output:

Page 14: 15. Spectral numerical differentiation - 國立臺灣大學homepage.ntu.edu.tw/~wttsai/fortran/ppt/15.Spectral...•This justifies the use of spectral numerical differentiation. •Write

program t_specderiv_2! Example of computing first derivative of a real function! using discrete spectral transform and calling NCAR FFTPACK!! > gfortran t_specderiv_2.f90 NCAR_fft.f

implicit noneinteger :: nnreal,allocatable,dimension(:) :: x, f, df, trigreal :: tmpreal,parameter :: pi = acos(-1.0)integer :: i, ii

!-- Input number of grids & allocate arraieswrite(*,*) "number of grid n=?"read(*,*) nnallocate (x(0:nn-1), f(0:nn-1), df(0:nn-1), trig(2*nn+15))

!-- Give values: f(x) = 2.*pi*x -x**2do i = 0, nn-1

x(i) = 2.0*pi/nn*if(i) = 2.*pi*x(i) -x(i)**2df(i) = 2.*pi -2.*x(i)

end do

Example of computing first derivative of a real function using discrete Fourier transform

when the derivatives at the periodic boundaries are not continuous

> gfortran t_specderiv_2.f90 NCAR_fft.f

� � = 2�� − ��

�� � = 2� − 2�

�/�

(continued)

Page 15: 15. Spectral numerical differentiation - 國立臺灣大學homepage.ntu.edu.tw/~wttsai/fortran/ppt/15.Spectral...•This justifies the use of spectral numerical differentiation. •Write

!-- Forward transformcall rffti(nn,trig)call rfftf(nn,f,trig)

!-- Set 0 to 0 & nn/2 modesf(0) = 0.0f(nn-1) = 0.0

!-- Multiply and swap the Fourier coefficientsii = 0do i = 1, nn-3, 2

ii = ii + 1tmp = -ii*f(i+1)f(i+1) = ii*f(i)f(i) = tmp

end dof = f/nn

!-- Backward transformcall rfftb(nn,f,trig)

!-- Output the derivatives by spectral method and compare with exact valueswrite(*,*) ' j x spectral exact'do i = 0, nn-1

write(*,*) i, x(i)/pi, f(i), df(i)end do

write(*,*) ''write(*,*) 'Max error: ', maxval(abs(f-df))

end program

Page 16: 15. Spectral numerical differentiation - 國立臺灣大學homepage.ntu.edu.tw/~wttsai/fortran/ppt/15.Spectral...•This justifies the use of spectral numerical differentiation. •Write

�/�

��(�)

� = 16

32

64

exact

� � = 2�� − ��

�� � = 2� − 2�

�/�

• Although �(�) is continuous at the two boundaries � = 0 and 2�, its derivative ��(�) is not.

• The discontinuity of ��(�) at � = 0 and

2� results in highly oscillatory spectral

approximation near the boundaries.

Page 17: 15. Spectral numerical differentiation - 國立臺灣大學homepage.ntu.edu.tw/~wttsai/fortran/ppt/15.Spectral...•This justifies the use of spectral numerical differentiation. •Write

• Discrete Fourier Transform with a period �� :

�� = � ��� �����

����

�� ���

= � ��� �������

����

�� ���

0 1 2� =

0 2�

�� − 1

�� =2�

��

• For � with a general period �: � � = � � + � 0 1 2� =

0 �

�� − 1

�� =�

��

• The discrete Fourier transform pair has the same form regardless the period.

Discrete Fourier Transform with a period �

� � = � � + 2�

��� =1

�� �� �

�����

���

�� �

=1

�� �� �

������

���

�� �

� ≡�

2�� � ≡

2�

��

�� = � ��� �����

����

�� ���

= � ��� ��������

����

�� ���

= � ��� ���������

����

�� ���

= � ��� �������

����

�����

��� =1

�� �� �

�����

���

�� �

=1

�� �� �

��������

���

�� �

=1

�� �� �

���������

���

�� �

=1

�� �� �

������

���

�� �

Page 18: 15. Spectral numerical differentiation - 國立臺灣大學homepage.ntu.edu.tw/~wttsai/fortran/ppt/15.Spectral...•This justifies the use of spectral numerical differentiation. •Write

• But to use spectral approximation to compute the derivative of � with a period �,

��

��=��

��

��

��=2�

��

��

wavenumber coefficient when the period is �

� � = � � + �

� ≡2�

��

� � = � � + 2�

Page 19: 15. Spectral numerical differentiation - 國立臺灣大學homepage.ntu.edu.tw/~wttsai/fortran/ppt/15.Spectral...•This justifies the use of spectral numerical differentiation. •Write

An Alternative Measure for the Accuracy of Finite Difference

• Common measure:

e.g., 2nd order finite-difference scheme mesh refinement by a factor of 2 improves the accuracy by fourfold:

• Alternative measure:

• Modified wavenumber approach.

• Measure how well a finite-difference scheme differentiates sinusoidal functions.

Consider pure harmonic functions of periods �/�:

� � = ���� � =2�

�� � = 0,1,2,⋯ ,

2

The exact derivative:��

��= ���

(� vs. �)

Δ =�

�= �� − ����

��

����

=���� − ����

2Δ+ � Δ� ≡

��

����

+ �(Δ�)

0 1 2� = �� − 1

�� ��������

Page 20: 15. Spectral numerical differentiation - 國立臺灣大學homepage.ntu.edu.tw/~wttsai/fortran/ppt/15.Spectral...•This justifies the use of spectral numerical differentiation. •Write

� � = ���� = ������� � = 0,1,2,⋯ ,

2

The shortest wave �(�) represents: � � = ����� ��� = ����

���

The shortest wavelength =2�

�= 2Δ

� =�

2

The longest wave �(�) represents: � � = ����� ��

The longest wavelength = �

� = 1

Δ

1 2� = 0 �− 1�− 2 �

1 2� = 0 �− 1�− 2 �

Page 21: 15. Spectral numerical differentiation - 國立臺灣大學homepage.ntu.edu.tw/~wttsai/fortran/ppt/15.Spectral...•This justifies the use of spectral numerical differentiation. •Write

• The second-order finite difference approximation :

��

����

=���� − ����

=����

� ���� − ����

� ����

2Δ =

������� ����

�� − �����

��

2Δ=

������ − �����

��

2�� =

� sin 2���

��

= �����

Δ =�

�� ≡sin

2���

Δ=sin

2���

��

Δ=sin(�∆)

��

��= ���

• �′is called the modified wavenumber for the 2nd-order central difference scheme.

• A measure of accuracy of a finite difference scheme is provided by comparing the modified wavenumber �′with �.

• If � � is defined at discrete grid �� :

�� =�

�� � = 0,1,2,⋯ ,N − 1

� =2�

��

0 1 2� = ��− 1

�� ���������� = � �� = ����� = ��

��� �⋅

��� = ����

���

• Exact differention:

Page 22: 15. Spectral numerical differentiation - 國立臺灣大學homepage.ntu.edu.tw/~wttsai/fortran/ppt/15.Spectral...•This justifies the use of spectral numerical differentiation. •Write

The fourth-order finite approximation :

��

����

=4

3

���� − ����

2Δ−1

3

���� − ����

=4

3

����� ���

� − ����� ���

2Δ−1

3

����� ���

� − ����� ���

=4

3����

���

������ − �����

��

2Δ−1

3����

���

������� − �����

���

=4

3

� sin 2���

Δ�� −

1

3

� sin 2�2��

2��

= �4

3

sin 2���

Δ−1

3

sin 2�2��

2��

= �����

�� =4

3

sin 2���

Δ−1

3

sin 2�2��

2Δ=4

3

sin�∆

Δ−1

3

sin2�∆

0 1 2� = ��− 1

�� �������� ��������

�� = � �� = �����

= �� ��� �⋅

���

= ���� ���

Page 23: 15. Spectral numerical differentiation - 國立臺灣大學homepage.ntu.edu.tw/~wttsai/fortran/ppt/15.Spectral...•This justifies the use of spectral numerical differentiation. •Write

• The modified wavenumber �� is in good agreement with the exact wavenumber � at small value

of �Δ, i.e., when � is slowly varying.

• For higher �Δ, i.e., when � varies rapidly, the finite-difference scheme provides a poor approximation.

�� =4

3

sin�∆

Δ−1

3

sin2�∆

�� =sin(�∆)

�∆=2�

���

�= 2�

�′∆

exact

2nd order

4th order

�Δ < 0.5 ⟹ 2�

�Δ < 0.5 ⟹ Δ <

4� ⟹

Δ> 4�

• For 2nd order finite-difference scheme, to have accurate approximation of the 1st derivative:

• Modified wavenumber of

4th order finite-difference scheme:

2nd order finite-difference scheme:

i.e., the number of grids per wavelength must be more than 4� ≈ 12

Page 24: 15. Spectral numerical differentiation - 國立臺灣大學homepage.ntu.edu.tw/~wttsai/fortran/ppt/15.Spectral...•This justifies the use of spectral numerical differentiation. •Write

• The number of grids needed for simulating such a wavy flow of multiple scales will be enormous when using finite-difference scheme!

• This justifies the use of spectral numerical differentiation.

Page 25: 15. Spectral numerical differentiation - 國立臺灣大學homepage.ntu.edu.tw/~wttsai/fortran/ppt/15.Spectral...•This justifies the use of spectral numerical differentiation. •Write

• Write two subroutines which compute the first and second derivatives of a real discrete function sampled at NUM equal spacing grids.

• Use the 1-D real FFT routine in FFTW.

• Test your subroutines by comparing with the derivatives of known functions,

e.g., � � = sincos � �.

• Note that the array storage arrangement of DFT in FFTW is:

Exercise

� = 0 1 2����

��

���� 2 1

���� ���

� ���� …… …… ���

���

� ����

� ������

�…… …… ���

� ����