Sie sind auf Seite 1von 26

Aarhus University

Advance Numerical Methods in Fluid Dynamics


Steady Diffusion Problem (1D/2D)
(Pseudo-Spectral Methods)

Author:
Javier I. Camacho Hernandez
Contents

1 Spectral Methods 3
1.1 Method of Weighted Residual(MWR) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.2 Basis Function Selection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.3 Spectral Schemes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.4 Pseudo-Spectral Approximation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.5 Lagrangian Polynomial Basis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.6 Differentiation Matrix DN . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.7 Multi-dimension . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5

2 1D Diffusion Problem Definition 6

3 1D Pseudo-Spectral Collocation Method 7


3.1 Discretization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
3.2 Boundary Conditions Implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
3.3 Matlab Implementation(1D Collocation Method) . . . . . . . . . . . . . . . . . . . . . . . 8
3.3.1 Set-up Grid and Differentiation Matrix . . . . . . . . . . . . . . . . . . . . . . . . 8
3.3.2 Dirichlet Boundary Condition Implementation . . . . . . . . . . . . . . . . . . . . 8
3.3.3 Direct Solver . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
3.4 Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
3.4.1 Collocation Points Representation . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
3.4.2 A Matrix Inspection(Before/After BCs Implementation) . . . . . . . . . . . . . . 9
3.4.3 Numerical vs Analytical . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
3.4.4 Convergence Analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10

4 1D Pseudo-Spectral Galerkin Method 11


4.1 Discretization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
4.2 Matlab Implementation(1D Galerkin Method) . . . . . . . . . . . . . . . . . . . . . . . . . 13
4.2.1 Set-up Grid and Differentiation Matrix . . . . . . . . . . . . . . . . . . . . . . . . 13
4.2.2 Dirichlet Boundary Condition Implementation . . . . . . . . . . . . . . . . . . . . 13
4.2.3 Direct Solver . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
4.3 Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
4.3.1 Sampling Points Representation . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
4.3.2 A Matrix Inspection(Before/After BCs Implementation) . . . . . . . . . . . . . . 14
4.3.3 Numerical vs Analytical . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
4.3.4 Convergence Analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15

5 2D Diffusion Problem Definition 16

6 2D Pseudo-Spectral Collocation Method 17


6.1 Discretization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
6.2 Matlab Implementation(2D Collocation Method) . . . . . . . . . . . . . . . . . . . . . . . 17
6.2.1 Set-up Grid and Differentiation Matrix . . . . . . . . . . . . . . . . . . . . . . . . 17
6.2.2 Dirichlet Boundary Condition Implementation . . . . . . . . . . . . . . . . . . . . 17
6.2.3 Direct Solver . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
6.3 Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
6.3.1 Collocation Points Representation . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
6.3.2 A Matrix Inspection(Before/After BCs Implementation) . . . . . . . . . . . . . . 18
6.3.3 Numerical vs Analytical . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
6.3.4 Error Distribution Analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
6.3.5 Convergence Analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20

1
7 2D Pseudo-Spectral Galerkin Method 21
7.1 Discretization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
7.2 Matlab Implementation (2D Galerkin Method) . . . . . . . . . . . . . . . . . . . . . . . . 22
7.2.1 Set-up Grid and Differentiation Matrix . . . . . . . . . . . . . . . . . . . . . . . . 22
7.2.2 Dirichlet Boundary Condition Implementation . . . . . . . . . . . . . . . . . . . . 22
7.2.3 Direct Solver . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
7.3 Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
7.3.1 Collocation Points Representation . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
7.3.2 A Matrix Inspection(Before/After BCs Implementation) . . . . . . . . . . . . . . 23
7.3.3 Numerical vs Analytical . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
7.3.4 Error Distribution Analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
7.3.5 Convergence Analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25

2
1. Spectral Methods

In this section it is describe the discretization in general terms, in order to explain the properties that both
method,pseudo-spectral collocation and Galerkin Method, has in common and the specific differences will
be explained at the corresponding sections.

1.1 Method of Weighted Residual(MWR)


The Spectral Methods can be viewed as a part of the discretization schemes called Method of Weighted
Residuals(MWR).


L (x) = f (x) x (1.1)

n
(x)
X
= (x) = ai i (x) i = 0, 1, 2, , n (1.2)
i=0


L (x) f (x) = R(x) 6= 0 (1.3)

Z
R(x)Wi (x) dx = 0 (1.4)

Where:
L:Differential Operator
i (x):Basis functions/Trial Functions/Expansions Functions

(x):Linear combinations of the basis functions.


ai :unknown constants.
R(x):Residual.

Wi (x):Weight functions/Test Functions.

The main purpose it is to approximate the (x) by (x) which is a linear combination of the basis
functions selected from a linearly independent set (Orthogonal). The MWR force the residual to zero in
some average sense over the domain .The number of Wi (x) is exactly equal of the number of unknown
constants ai in (x).

3
1.2 Basis Function Selection
The expansion basis functions i (x) in the Spectral Method are infinitely global functions and mutually
orthogonal with respect the positive weight functions Wi (x). The selection procedure of the basis
functions is differ between the multiple spectral schemes,the Galerkin,Collocation and Tau Methods.

Zb (
1, if m = n
n (x)m (x)w(x) dx (1.5)
0, otherwise
a

1.3 Spectral Schemes


(
1, if x = xi
Collocation : (x xi ) .The test functions are the translated Dirac delta
0, otherwise
functions centred at the special collocations points(the points depends on the roots of the specific
polynomial(Legendre,Chebychev,et)).This spectral scheme requires the differential equation to be
satisfied exactly at the collocation points.
Galerkin : Wi (x) = i (x) i = 0, 1, 2, , n. The expansion functions and thereby the test
functions individually satisfy the BCs.
Tau : Wi (x) = i (x) i = 0, 1, 2, , n 1.The test functions do not need to satisfy the BCs.It is
requires to implement a supplementary seets of equations to apply the BCs.

1.4 Pseudo-Spectral Approximation


In the pseudo-spectral approximation the solution to the elliptic PDE problem takes the form of an
interpolation polynomial, however with a new set of basis functions hi (x) and interpolation coefficients
i (x):
N
X
N (x) = i hi (x) i = 1, 2, , N (1.6)
i=1

Where:
N ):Approximation solution of (x) (x)
= N
hi (x):Basis functions
i :Interpolation coefficients.
xi :Sampling points (Collocation Points in the case of the collocation method).

The unknowns i are the approximated values of the function (x) at the sampling points xi .In the
pseudo-spectral approximation the basis functions hi has to satisfy the requirements hi = ij N = i .
In a similar manner as the Spectral Methods, the approximations of the unknown coefficients i in the
Pseudo-Spectral approximations can be calculated by using Collocation,Galerkin or Tau schemes.

The advantage of the pseudo-spectral with respect the spectral method is that,the procedure enables
to solve directly for the function values i at the sampling points xi , which is often preferred over the
indirect approach of spectral methods, where one has to first calculate the interpolation coefficients ai .

4
1.5 Lagrangian Polynomial Basis
As it mentioned before the basis functionshi = ij N = i ,so that the solution directly gives the
function values at the sampling points. The implemented basis functions are the Lagrangian Polynomials
p(x) and they are defined by the following expression:
N N
1 Y Y
pj (x) = (x xk ) , aj = (xi xk ) (1.7)
aj
k=0,k6=j k=0,k6=j

Inserting the Lagrange basis polynomial in Eq.(2.6)


N
X
p(x) = j pj (x) (1.8)
j=0

Differentiating the polynomial in Eq.(1.8)


N
0 X 0
p (x) = pj (x) j pj (x) (1.9)
j=0

N
0 X
pj (x) = pj (x) (x xk )1 (1.10)
k=0,k6=j

1.6 Differentiation Matrix DN


The collocation pseud-spectral scheme has to satisfy the differential equation at the collocation points.

L (xj ) = f (xj ) xj (1.11)

It is denoted the function values vi = p(xi ) at the grid point xi ,and the corresponding derivative wi =
0
p (xi ). This is a linear operation from v to w,further more this operation can be represented by a
differentiation matrix.

w = DN v (1.12)

0
The Eq.(1.9) can be compared with the Eq.(1.12),it is clear that the polynomial pj (x) correspond to the
Dij term. The differentiation matrix Dij it is calculated by substituting the sampling or collocations
points in Eq.(1.10).This procedure it is performed in this report, by implementing the provided Matlab
files (MimeticpolyVal.m,LegendreVal.m,GLLnodes.m) (this files are not include in the report).

1.7 Multi-dimension
This section is a brief explanation on how to extend the 1D problem to a 2D.The dimension is extended
by using the tensor product rule to obtain a tensor product grid from the 1D grid. The discrete Laplace
operator in a 2D = [1, 1] can be formulated by using the tensor product:
2 2
LN = I DN + DN I (1.13)

5
2. 1D Diffusion Problem Definition

The main purpose of this problem is to use pseudo-spectral collocation method(CM) and Spectral Finite
Element Method (Galerkin Method(GM))to solve a 1-D steady diffusion problem (Elliptic PDE).In both
cases the collocations points (for CM) and the nodes for the quadrature rule (for GM)are the roots of a
Gauss-Lobatto Legendre(GLL) polynomial,which are defined in the domain [1, 1].

The problem is defined in a 1D domain = [1, 1] with an homogeneous Dirichlet boundary


condition(DBCs),and it is required to perform a discretization,and MATLAB implementation in order
to solve the governing equation.

Governing Equation
2 (x) = sin(x) = f (x) (2.1)

Domain
= [1, 1] (2.2)

Dirichlet Boundary Condition


(1) = (1) = 0 (2.3)

Analytical Solution
1
(x) = sin(x) (2.4)
2


L (x) = f (x) x (2.5)

N
X
N (x) = i hi (x) = p(x) i = 0, 1, , 2 , N (2.6)
i=0

6
3. 1D Pseudo-Spectral Collocation Method

3.1 Discretization
d2 (x)
= f (x) x : [1, 1] (3.1)
dx2

BC 0 s : 0 = (1) = N = (1) = 0 (3.2)

N
X
2
Dij j (x) = f (xi ) i = 0, 2, , N (3.3)
j=0

3.2 Boundary Conditions Implementation


N
X 1
2 2 2
Dij j (x) = f (xi ) (Di0 0 + DiN N ) i = 1, 2, , N (3.4)
j=1

This is a linear system of equations, and the matrix coefficients is a full matrix.The solution it is
implemented by a direct solver method (Gaussian elimination in Matlab.)

7
3.3 Matlab Implementation(1D Collocation Method)
3.3.1 Set-up Grid and Differentiation Matrix

1 %% 1D Steady Diffusion Problem (Elliptic Problem)


2 %% Numerical Solution by Spectral Collocation GaussLobattoLegendre Method
3 % Domain on [1,1]x[1,1] with u=0 on boundary
4 % Set up grids and tensor product Laplacian and solve for u:
5 clc;clear;close all
6 %% Set up grid
7 N=5;
8 % Collocation Points
9 [x,w]=GLLnodes(N);k=1;
10 % Create the LGL differentiation Matrix
11 [,dhdx] = MimeticpolyVal(x,N,k);
12 D=dhdx';D2 = D2;
13 % Compute the Coefficients Matrix
14 A=D2;

3.3.2 Dirichlet Boundary Condition Implementation

1 %% Create a column matrix to hold the RHS


2 f =sin(pi.*x)';rhs=f;
3 %% Apply the Boundary Conditions
4 % Find the bounday points of the coordinate vectors:
5 bp = find(abs(x)==1); % boundary pts
6 % Impose the boundary conditions on the right side vector.
7 % rhs(bp) = 0;
8 rhs(bp) = rhs(bp)((x(bp)'==0));
9 % Apply the boundary conditions to the coefficient matrix
10 % Zero the rows out
11 A(bp,:) = zeros(length(bp),length(rhs));%L(bp,:) = zeros(4*N,(N+1)2);
12 % put in 1 for known points
13 A(bp,bp) = eye(length(bp));%L(bp,bp) = eye(4*N);

3.3.3 Direct Solver

1 %% Numerical Solution (Direct Solver)


2 u = A\rhs;
3 %% Analytical Solution
4 u analytic= (1/(pi2))*sin(pi.*x);
5 %% Error Analysis (Analytical vs Numerical)
6 % Error
7 error=(uu analytic');
8 % Norm 2 Error
9 error norm=norm(error);

8
3.4 Results
3.4.1 Collocation Points Representation

Gauss-Lobatto-Legendre Nodes Plot


0.5

0.4
Nodes = 16

0.3

0.2

0.1

-0.1

-0.2

-0.3

-0.4

-0.5
-1 -0.8 -0.6 -0.4 -0.2 0 0.2 0.4 0.6 0.8 1
x

3.4.2 A Matrix Inspection(Before/After BCs Implementation)

1-D Steady Diffusion Problem


Coeff. Matrix Before Implement BCS
0

10

12

14

16

0 2 4 6 8 10 12 14 16
nz = 256

1-D Steady Diffusion Problem


Coeff. Matrix After Implement BCS
0

10

12

14

16

0 2 4 6 8 10 12 14 16
nz = 226

9
3.4.3 Numerical vs Analytical

1-D Steady Diffusion Problem


Numerical Solution by Spectral Collocation GLL Method
Polynomial Order = 15
0.15
Numerical Solution
0.1 Analytical Solution

0.05
||Error||2 = 1.1753e-12
(x)

0 Nodes = 16

-0.05

-0.1

-0.15
-1 -0.8 -0.6 -0.4 -0.2 0 0.2 0.4 0.6 0.8 1
x

1-D Steady Diffusion Problem


10 -13 Error Distribution Analysis
5
Error(x)

-5
-1 -0.8 -0.6 -0.4 -0.2 0 0.2 0.4 0.6 0.8 1
x

3.4.4 Convergence Analysis

Spectral Convergence Analysis


Norm Error in Function of the Polynomial Order

100

10-5
||Error|| 2

10-10

10-15

10 20 30 40 50 60 70 80 90 100
N

10
4. 1D Pseudo-Spectral Galerkin Method

4.1 Discretization
Strong Formulation
uxx (x) = f (x) x : [1, 1] (4.1)
Insert Test Function v and integrate on both sides
Z Z
uxx v dx = f v dx (4.2)

Weak Formulation (IBP in LHS)


 Z  Z
ux v| ux vx dx = f (x)v dx (4.3)

Implement BCs(first term vanish)


Z Z
ux vx dx) = f v dx (4.4)

As it mentioned before in the pseudo-spectral Galerkin scheme the selected basis and test functions are
the same. h = v
N
X
u = up hp (x) (4.5)
p=0

N
X
v= vp hp (x) (4.6)
p=0

Following the definition of the pseudo-spectral methods,polynomial basis function has to satisfy the
function values in the sampling points,by fulfilling the Kronecker delta property.

hp (xq ) = pq (4.7)

Derivatives of the approximations (u(x)) functions and the test (v)
N N
X hq (xq ) X
ux (xq ) = up = Dpq uq (4.8)
i=0
x p=0

N N
X hq (xq ) X
vx (xq ) = vp = Dpq vq (4.9)
p=0
x p=0

Numerical Integration by by Gauss-Lobatto quadrature(w: weight from quadrature rule)


N
X Z
wn ux (xn )vx (xn ) = f v dx (4.10)
n=0

(Using Kronecker delta property)


N
X N
X N
X N
X
wn ux (xn )vx (xn ) = wn f (xn )vx (xn ) = wn f (xn )vq hq (xn ) = wn f (xn )vq qn vq (4.11)
n=0 n=0 n=0 n=0

11
Substitution of the derivatives,vq cancel out and Kronecker delta changes indexes.
N
X N
X
wn Dnp up Dnq vq = wn f (xn )vq qn vq (4.12)
n=0 n=0

N
X N
X
wn Dnp up Dnq = wq fq (4.13)
n=0 n=0

Matrix Form
DT D~u = f~ (4.14)
Where:
is the diagonal matrix created with the weights from the quadrature.
D is the differentiation matrix.
f~ is the source term evaluated at the sampling points.

~u is the unknowns vector.


Re-formulated in the Standard Form

DT D~u = A (4.15)

f~ = b (4.16)
A~u = ~b (4.17)
Boundary Conditions Implementations
The Galerkin method produce a symmetric A matrix.The boundary conditions are implemented by
imposed the entries to zero in the ith row in the A matrix and in the symmetry is achieved by imposed
the entries to zero in the ith column too.

12
4.2 Matlab Implementation(1D Galerkin Method)
4.2.1 Set-up Grid and Differentiation Matrix

1 %% 1D Steady Diffusion Problem (Elliptic Problem)


2 %% Numerical Solution by Spectral Galerking Method
3 % Domain on [1,1]x[1,1] with u=0 on boundary
4 % Set up grids and tensor product Laplacian and solve for u:
5 clc;clear;close all
6 %% Set up grid
7 N=5;
8 % Collocation Points
9 [x,w]=GLLnodes(N);
10 k=1;
11 % Create the LGL differentiation Matrix
12 [,dhdx] = MimeticpolyVal(x,N,k);
13 D=dhdx';
14 % Compute the Coefficients Matrix
15 %Weight Matrix
16 lambda 1=diag(w);
17 lambda 2=diag(repmat(w,[1,N+1]));
18 %Coefficients Matrix
19 A=D'*lambda 1*D;

4.2.2 Dirichlet Boundary Condition Implementation

1 %% Create a column matrix to hold the RHS


2 f =sin(pi.*x)';
3 rhs=lambda 1*f;%Weighted RHS
4 %% Apply the Boundary Conditions
5 % Find the bounday points of the coordinate vectors:
6 bp = find(abs(x)==1); % boundary pts
7 % Impose the boundary conditions on the right side vector.
8 rhs(bp) = 0;
9 % Apply the boundary conditions to the coefficient matrix
10 % Zero the rows out
11 A(bp,:) = zeros(length(bp),length(rhs));%L(bp,:) = zeros(4*N,(N+1)2);
12 A(:,bp) = zeros(length(bp),length(rhs))';%keep symmetry
13 % put in 1 for known points
14 A(bp,bp) = eye(length(bp));%A(bp,bp) = eye(4*N);

4.2.3 Direct Solver

1 %% Numerical Solution (Direct Solver)


2 u = A\rhs;
3 %% Analytical Solution
4 u analytic= (1/(pi2))*sin(pi.*x);
5 %% Error Analysis (Analytical vs Numerical)
6 % Error
7 error=(uu analytic');
8 % Norm Error
9 error norm=norm(error);

13
4.3 Results
4.3.1 Sampling Points Representation

Gauss-Lobatto-Legendre Nodes Plot


0.5

0.4
Nodes = 16

0.3

0.2

0.1

-0.1

-0.2

-0.3

-0.4

-0.5
-1 -0.8 -0.6 -0.4 -0.2 0 0.2 0.4 0.6 0.8 1
x

4.3.2 A Matrix Inspection(Before/After BCs Implementation)

1-D Steady Diffusion Problem


Coeff. Matrix Before Implement BCS
0

10

12

14

16

0 2 4 6 8 10 12 14 16
nz = 256

1-D Steady Diffusion Problem


Coeff. Matrix After Implement BCS
0

10

12

14

16

0 2 4 6 8 10 12 14 16
nz = 198

14
4.3.3 Numerical vs Analytical

1-D Steady Diffusion Problem


Numerical Solution by Spectral Galerking Method
Polynomial Order = 15
0.15
Numerical Solution
0.1 Analytical Solution

0.05
||Error||2 = 1.1753e-12
(x)

0 Nodes = 16

-0.05

-0.1

-0.15
-1 -0.8 -0.6 -0.4 -0.2 0 0.2 0.4 0.6 0.8 1
x

1-D Steady Diffusion Problem


10 -13 Error Distribution Analysis
5
Error(x)

-5
-1 -0.8 -0.6 -0.4 -0.2 0 0.2 0.4 0.6 0.8 1
x

4.3.4 Convergence Analysis

Spectral Convergence Analysis


Norm Error in Function of the Polynomial Order

100

10-5
||Error|| 2

10-10

10-15

10 20 30 40 50 60 70 80 90 100
N

15
5. 2D Diffusion Problem Definition

The main purpose of this problem is to use pseudo-spectral collocation method(CM) and Spectral Finite
Element Method (Galerkin Method(GM))to solve a 2-D steady diffusion problem (Elliptic PDE).In both
cases the collocations points (for CM) and the nodes for the quadrature rule (for GM)are the roots of a
Gauss-Lobatto Legendre(GLL) polynomial,which are defined in the domain [1, 1]2 .

The problem is defined in a 2D domain = [1, 1]2 with an homogeneous Dirichlet boundary
condition(DBCs),and it is required to perform a discretization,and MATLAB implementation in order
to solve the governing equation.

Governing Equation
2 (x, y) = sin(x)sin(x) = f (x, y) (5.1)

Domain
= [1, 1]2 (5.2)

Dirichlet Boundary Condition

(1, y) = (1, y) = (x, 1) = (x, 1) = 0 (5.3)

Analytical Solution
1
(x, y) = sin(x)sin(y) (5.4)
2 2

16
6. 2D Pseudo-Spectral Collocation Method

6.1 Discretization

L (x, y) = f (x, y) x and y (6.1)
As it mentioned in the 1D case, the extension of the problem from 1D to 2D imply that a tensor product
is performed.
2 2
LN = I DN + DN I (6.2)

6.2 Matlab Implementation(2D Collocation Method)


6.2.1 Set-up Grid and Differentiation Matrix

1 %% 2D Steady Diffusion Problem


2 %% Numerical Solution by Spectral Collocation LGL Method
3 % Domain on [1,1]x[1,1] with u=0 on boundary
4 % Set up grids and tensor product Laplacian and solve for u:
5 % format shortEng
6 %% Set up grid
7 N=15;
8 % Collocation Points
9 [x,w]=GLLnodes(N);y=x;k=1;[xx,yy] = meshgrid(x,y);xx = xx(:); yy = yy(:);
10 % Create the LGL differentiation Matrix
11 [,dhdx] = MimeticpolyVal(x,N,k);D=dhdx';
12 % Set small values to zeros
13 D(abs(D)<1e12) = 0;D2 = D2;
14 % Compute the tensor product spectral grid.
15 I = eye(N+1);
16 L = kron(I,D2) + kron(D2,I); % Laplacian

6.2.2 Dirichlet Boundary Condition Implementation

1 rhs =sin(pi.*xx).*sin(pi.*yy);%Create a column matrix to hold the RHS


2 %% Apply the Boundary Conditions
3 % Find the bounday points of the coordinate vectors:
4 bp = find(abs(xx)==1 | abs(yy)==1); % boundary pts
5 % Impose the boundary conditions on the right side vector.
6 rhs(bp) = 0;% rhs(b) = rhs(b)((yy(b)==0) +(xx(b)==0));
7 % Apply the boundary conditions to the laplacian
8 L(bp,:) = zeros(length(bp),length(rhs)); % Zero the rows out
9 L(bp,bp) = eye(length(bp));%L(bp,bp) = eye(4*N); % put in 1 for known points

6.2.3 Direct Solver

1 u = L\rhs; % Numerical Solution (Direct Solver)


2 uu = reshape(u,N+1,N+1);% Reshape long 1D results onto 2D grid:
3 u analytic= (1/(2*pi2))*sin(pi.*xx).*sin(pi.*yy);%Analytical Solution
4 uu analytic= reshape(u analytic,N+1,N+1);
5 %% Error Analysis (Analytical vs Numerical)
6 error=(uuuu analytic');% Error
7 % Norm Error
8 error norm=norm(error);

17
6.3 Results
6.3.1 Collocation Points Representation

Gauss-Lobatto-Legendre Nodes Plot

1
Total Nodes = 256
0.8

0.6

0.4

0.2

-0.2

-0.4

-0.6

-0.8

-1

-1.5 -1 -0.5 0 0.5 1 1.5


x

6.3.2 A Matrix Inspection(Before/After BCs Implementation)

2-D Steady Diffusion Problem


L Matrix Before Implement BCS
0

50

100

150

200

250

0 50 100 150 200 250


nz = 7936

2-D Steady Diffusion Problem


L Matrix After Implement BCS
0

50

100

150

200

250

0 50 100 150 200 250


nz = 6136

18
6.3.3 Numerical vs Analytical

2-D Steady Diffusion Problem


Numerical Solution by Collocation Legendre Gauss-Lobatto Method
Polynomial Order = 15
1 0 0 0 0 0
0.01 0.01 -0.01
-0.01
0.02 -0.02 -0.02
0.02

0
0.8 0.03 -0.03

0
-0.0
1
0.0
0.04 -0.04

1
0.03

3
0.6

-0.0
0.01

-0.01

-0.03
0.03

-0.02
0.02
4 -0.
0.0 04

0.02
0.4
0.04 4
-0.0

0.02 0.03 -0.03 2


0.2 -0.0

0
0.0 01
1 0.02 -0.02 -0.

0
0.01 0 -0.01
0.01 -0.01
0
y

0 0 0 0
-0.01 0 0.01
-0.01 0.01
-0.02 0.02
-0.2 -0. 2
-0.03 -0
02 0.0 0.03
0

-0.0
.0

0.01

03
4 3 0.04
-0.0

0.
1
2

-0.4

0.02
-0.0

0.01
-0.01

0.
.0

0
-0

4
-0.6 -0
.0

03
0
3 -0.04 0.04

0.
0.
02
-0.03 0.03
0

-0.8 0.0
-0.02 -0.02 0 1 0.02
-0. 1 0.02
-0.01 0.01 0.01
-0.01
-1 0 0 0 0 0 0
-1 -0.8 -0.6 -0.4 -0.2 0 0.2 0.4 0.6 0.8 1
x

-0.05 -0.04 -0.03 -0.02 -0.01 0 0.01 0.02 0.03 0.04 0.05

19
6.3.4 Error Distribution Analysis

2-D Steady Diffusion Problem


Error Analysis
||Error|| 2 = 1.3906e-12
1 0 0 0 0 0
0 0
0 0 0 0

0 0 1e-13 0 0
-1e-13
0.8

1e-13

-1e-13
0 -1e-

0
0 13 0
-1e-13 1 -
33 1e2e-13 2e

0
0 -2e-1 0 -1 0

0
0

0
3
0.6 0 -1 0 1
213e e3- -1e e-13

0
1e-1 3 -1
3 1e-1
2e-1
3e 3 -1e-13 1e- -13 -2 e-1 3
1
1e-13 -2e-13 3
3
-1e-1 0
0 -13
0- 0
0 0 1e 0 0

0
0.4 3 -1 -13 -1e-1
-1e e-1 3 e 3
-1 1 -2e-13 0 1 2e-13 3
-2e-13 3 1e-12e-13
-1e-1 0 3

0
-1e-13 0 3 1e-1 3
0 3 -1e 1e-1
0.2 1e-13
1e-1
-13 -1e-13 0 0

1e-13

-1e-13
1e-13 0 -2e-13 -1e-13

0
2e-13 1e-13 0 -1e-13
1e-13 -1e-13
0

y
0 0 0 0 0 0
-1e-13 -1e-13 -1e-13 0 1e-13 1e-13
-2e-13 1e-13
2e-13 1e-13
-1e-13 0 0 1e-13
-0.2 0 -1e-13 -1e 3 0 -1e 0
3 0 3 -13 -1
1e-1 -2e-13e-13 -13
1e-1
2e-13 1e-12e-13
-2e-13

0
3 -1e
-0.4 0 1e-10 -1e-1 -1
3
3 2e -1e
-1 3 0 -13 0
1e-13
-1e-13 -2e-13 3 1e 1 -13 0 3 1e3-1
2e-13

0
e- e-1
0 -2 0 3
0

0
-1e

3
-1e 3
-13

1e-1
-1e-1
-13 13 -13 -1 -13

0
-1
-0.6 1e 2e-13 - 1e -1e
2e -2e-13

e
0 -13 0 1e 0

-1
0

3
3
0 1e

0
-13 0
-1e-13 1e-13
0
1e-13
0 -1e-13 0
0

0 0
-0.8 0 0 0
0
0
0 0 0 0 0
0 0 0
-1 0 0 0 0
-1 -0.8 -0.6 -0.4 -0.2 0 0.2 0.4 0.6 0.8 1
x

-3.6 -2.88 -2.16 -1.44 -0.72 0 0.72 1.44 2.16 2.88 3.6
10-13

6.3.5 Convergence Analysis

Spectral Convergence Analysis


Norm Error in Function of the Polynomial Order

100

10-5
||Error|| 2

10-10

10-15

5 10 15 20 25 30 35 40 45 50
N

20
7. 2D Pseudo-Spectral Galerkin Method

7.1 Discretization

L (x, y) = f (x, y) x and y (7.1)
Matrix Form  
DT 1 D~u = 2 f~ (7.2)

W = diag(w) (7.3)

1 = W (7.4)

2 = 1 1 (7.5)
Where:
is the diagonal matrix created with the weights from the quadrature.
D is the differentiation matrix.

f~ is the source term evaluated at the sampling points.


~u is the unknowns vector.
It necessary to mention the differences between the 1D case and 2D.In the 1D case the 1 and 2 are
equal except i terms of dimension inorder to be able to perform the operations.In the 2D case the 1 and
2 require a pre-process in order to extend the solution to a 2D problem.
Re-formulated in the Standard Form

DT 1 D~u = A (7.6)

2 f~ = b (7.7)
As it mentioned in the 1D case, the extension of the problem from 1D to 2D imply that a tensor product
is performed.
L2 = 1 A + A 1 (7.8)

L2 ~u = ~b (7.9)

21
7.2 Matlab Implementation (2D Galerkin Method)
7.2.1 Set-up Grid and Differentiation Matrix

1 %% 2D Steady Diffusion Problem


2 %% Numerical Solution by Spectral Galerking Method
3 % Domain on [1,1]x[1,1] with u=0 on boundary
4 % Set up grids and tensor product Laplacian and solve for u:
5 clc;clear;close all
6 %% Set up grid
7 N=5;%Polynomial Order
8 % Collocation Points
9 [x,w]=GLLnodes(N);
10 y=x;k=1;
11 [xx,yy] = meshgrid(x,y);
12 xx = xx(:); yy = yy(:); % stretch 2D grids to 1D vectors
13 % Create the LGL differentiation Matrix
14 [,dhdx] = MimeticpolyVal(x,N,k);D=dhdx';%
15 % Set small values to zeros
16 D(abs(D)<1e12) = 0;
17 %Weight Matrix
18 lambda 1=diag(w);lambda 2=kron(lambda 1,lambda 1);
19 %Coefficients Matrix
20 A=(D'*lambda 1*D);
21 L = kron(lambda 1,A) + kron(A,lambda 1); % Laplacian

7.2.2 Dirichlet Boundary Condition Implementation

1 %% Create a column matrix to hold the RHS


2 f = sin(pi.*xx).*sin(pi.*yy);rhs=lambda 2*f;%Weighted RHS
3 %% Apply the Boundary Conditions
4 % Find the bounday points of the coordinate vectors:
5 bp = find(abs(xx)==1 | abs(yy)==1); % boundary pts
6 % Impose the boundary conditions on the right side vector.
7 rhs(bp) = 0;% rhs(bp) = rhs(bp)+(yy(bp)==0) +(xx(bp)==0);
8 % Apply the boundary conditions to the coefficient matrix
9 % Zero the rows out
10 L(bp,:) = zeros(length(bp),length(rhs));%L(bp,:) = zeros(4*N,(N+1)2);
11 L(:,bp) = zeros(length(bp),length(rhs))';%keep symmetry
12 % put in 1 for known points
13 L(bp,bp) = eye(length(bp));%L(bp,bp) = eye(4*N);

7.2.3 Direct Solver

1 %% Numerical Solution (Direct Solver)


2 u = L\rhs;
3 % Reshape long 1D results onto 2D grid:
4 uu = reshape(u,N+1,N+1);
5 %% Analytical Solution
6 u analytic= (1/(2*pi2))*sin(pi.*xx).*sin(pi.*yy);
7 % Reshape long 1D results onto 2D grid:
8 uu analytic= reshape(u analytic,N+1,N+1);
9 %% Error Analysis (Analytical vs Numerical)
10 % Error
11 error=(uuuu analytic');
12 % Norm Error
13 error norm=norm(error);
14 %% Graphical Representation Preparation
15 [xx,yy] = meshgrid(x,y);
16 % Interpolate to finer grid and plot:
17 %Numerical
18 [xxx,yyy] = meshgrid(1:.01:1,1:.01:1);
19 %Analitical
20 uuu analytic = (1/(2*pi2))*sin(pi.*xxx).*sin(pi.*yyy);

22
7.3 Results
7.3.1 Collocation Points Representation

Gauss-Lobatto-Legendre Nodes Plot

1
Total Nodes = 256
0.8

0.6

0.4

0.2

-0.2

-0.4

-0.6

-0.8

-1

-1.5 -1 -0.5 0 0.5 1 1.5


x

7.3.2 A Matrix Inspection(Before/After BCs Implementation)

2-D Steady Diffusion Problem


L Matrix Before Implement BCS
0

50

100

150

200

250

0 50 100 150 200 250


nz = 7936

2-D Steady Diffusion Problem


L Matrix After Implement BCS
0

50

100

150

200

250

0 50 100 150 200 250


nz = 5352

23
7.3.3 Numerical vs Analytical

2-D Steady Diffusion Problem


Numerical Solution by Spectral Galerking Method Method
Polynomial Order = 15
1 0 0
0.01 -0.01 -0.01
0.01 0.02 -0.02
0.8 0.03 -0.03

0
0. 4
0.6 04 .0
-0

-0.03
-0.02
0.03
0.02

0.02

-0.0
0.4 0.0 .04
4 -0

0
-0
.0

01
0.0 0.03 1 -0.03 1
0.2 .0

0.
1 -0
0.02 -0.02
0.01 -0.01
0
y
0 0 0 0
-0 .01 -0.0 0.01
-0.02 1 0.01 0.0 2
-0.2 -0.03 0.03
0

-0.4

0.02
-0.02

-0.0

0.04

0.04
-0.0 3

02
-0.6

3
-0

0.0

0.0
.0

-0.

2
3 0.03

1
-0.0
-0

0.0
0
01
.0

-0.8 0.0
-0.
1

1
-0.02 0.02
-0.01 0.01
-1 0 0
-1 -0.5 0 0.5 1
x

-0.05 -0.04 -0.03 -0.02 -0.01 0 0.01 0.02 0.03 0.04 0.05

24
7.3.4 Error Distribution Analysis

2-D Steady Diffusion Problem


Error Analysis
||Error|| 2 = 1.3891e-12
1 0 0 0 0
0 0
0 0

-1e-1
0 1e-13 30

1e-13
0.8 -1e-1
13 1e-1

0
1
-1 30 -1e- 3 0 e-13

13

3
e

2e
-1

e-

-1
0.6 0 0

0
0 3

-2

0
3
10e- 1e-13
-1e-1

-13
0 -13

2e
13 0 2e-13 -13
-2e -1e

-1

-2e
0 0

3
3 1e-13
0.4 -1e-13 -1e-13 0 1e-13
-13 1 0 2e-13 2e-
e -2e- 13

0
-2
0

1e-13
-1e0

-1e-
0.2 0 3 0
1e-13 2e-13 1e-1 -13 13
-2e-13 -1e-

13
1e-13 -1e-13
0
y

0 0 0 0
-13 -2e-13 -1e-13 1e-13 2e-13 1e-13
-1e
1 3 1e-1
-0.2 0 -1e-01e-13 0 3 0

0
21ee--13 -1e-13 3
2e-1 -13 -1e-1 13
0

13 3 e-
-2e - 2
-0.4 0 -13 1e-103

3
3 e 1e-

2e-
-1 -1 -13 2e-1

1e-13
-2e-1
13

-1e-1

-1e-13
3
-1e -2e

1e-13
13
1e-13 e-13
0 1e-1 2e 0 0-1 -13 130

3
-0.6 0
0 3 -1
3 e -1e-
0 -2
0 -1e-1 3 01e-13
0

0
-0.8 0 0

0
0

0 0 0 0
-1 0 0 0 0
-1 -0.5 0 0.5 1
x

-1.95 -1.56 -1.17 -0.78 -0.39 0 0.39 0.78 1.17 1.56 1.95
10-12

7.3.5 Convergence Analysis

Spectral Convergence Analysis


Norm Error in Function of the Polynomial Order

100

10-5
||Error|| 2

10-10

10-15

5 10 15 20 25 30 35 40 45 50
N

25

Das könnte Ihnen auch gefallen