Anda di halaman 1dari 16

MSM 732 Assignment 1b

Multigrid V-Cycle Assignment


Alexander R. Mauchle #23382572

09
MSM 732 Assignment 1b 2009

Table of Contents
Introduction .................................................................................................................................................. 3
Inputs ............................................................................................................................................................ 3
Source Function ........................................................................................................................................ 4
Gauss-Seidel Mesh ........................................................................................................................................ 5
In Short ...................................................................................................................................................... 5
V-Cycle Multi-grid Mesh ............................................................................................................................... 6
Gauss-Seidel Function ............................................................................................................................... 6
Step 1 Fine Mesh .................................................................................................................................... 7
Step 2 Restriction ................................................................................................................................... 7
Step 3 Prolongation................................................................................................................................ 7
Step 4 Correction ................................................................................................................................... 7
Results ........................................................................................................................................................... 8
The error ................................................................................................................................................... 9
Conclusion ................................................................................................................................................... 10
Appendix Code ......................................................................................................................................... 11
Appendix Function ................................................................................................................................... 15

2 Alexander Mauchle
#23382572
MSM 732 Assignment 1b 2009

Introduction
In this assignment a Gauss-Seidel solver diffusion solver with be compare to a Multi-grid V-cycle solver.
This question has been set up in the thermal domain with some fixed input variables.

The author originally used this as a opportunity to learn VBA code and did the initial Gauss-Seidel and
Multi-Grid solver in Excel VBA. Though do to time constraints and requirement s that was put on hold to
complete the project in Matlab. Thus informally there is an Excel incomplete version of the program in
the software.

When comparing the code for the Multi-Grid versus the standard Gauss. It was clear that the set of
equations are generally similar. Thus a single Gaussian function was used to calculate the Gauss-Seidel,
Multi-grid fine mesh and the Multi-grid coarse error meshes.

Inputs
The fix inputs for the project are defined below and taken from the Matlab code:

% ================================ %
% --> Initial Varibles %
% ================================ %
kth = 1; % --> Material Diffusivity: Thermal
lg = 1; % --> Length
nn = 129; % --> Number of nodes
ini = 0; % --> Initial Condition
tol = 1e-4; % --> Convergence Tolerance
it_g = 100; % --> No. Iterations Gauss
it_m = 6; % --> No. Iterations Multigrid
v1 = 2; % --> Multigrid relaxation (v1)
v2 = 3; % --> Multigrid relaxation (v2)
v3 = v1+v2; % --> Multigrid relaxation (v3)
lvl = 6; % --> V-Cycle Steps
srcf = inline('(x.^2 + 3*x).*exp(x)'); % --> Source Function
del_x = inline('lg/(nn - 1)','lg','nn');% --> Element length(Delta x)

3 Alexander Mauchle
#23382572
MSM 732 Assignment 1b 2009
Source Function
The Source function was given in the following format:

A plot of the function shows that it is exponential in nature.

4 Alexander Mauchle
#23382572
MSM 732 Assignment 1b 2009

Gauss-Seidel Mesh
The Gauss-Seidel solver for a steady-state is solved through a set of linear equations.

The discretised fundamental equation solution is as follows.

Though this was not necessary for the simulation to converge, though still required a residual was
calculated using the following method:

% --> Build A * phi_a matrix


A_phi = kth*(phi(i+1) - 2*phi(i) + phi(i-1))/delx^2;

% --> r = f - A * phi_a
res(i,1) = src(i,1) + A_phi;

In Short
The Gauss-Seidel process takes the fundamental equation and iterates through body then iterates the
process again in order to get convergence.

5 Alexander Mauchle
#23382572
MSM 732 Assignment 1b 2009

V-Cycle Multi-grid Mesh


The V-cycle method here was complete as it was described in the notes. In this case the properties of
the cycle were fixed.

Gauss-Seidel Function
The Gauss function use in the process for both the Unknown and the error is well documented in the
code and is shown below:

for k = 1:iter
% Zero the Residual Error
res_err = 0;

% --> High frequency relaxation procedure for Gauss iterations


for j = 1 : relax

% --> Gauss-Seidel Method


for i = 2:(nodes-1)
phi(i) = 0.5*(phi(i+1) + phi(i-1)+(delx^2/kth)*src(i));
end

end

for i = 2:(nodes-1)
% --> Build A * phi_a matrix
A_phi = kth*(phi(i+1) - 2*phi(i) + phi(i-1))/delx^2;

% --> r = f - A * phi_a
res(i,1) = src(i,1) + A_phi;

% Build Residual Error

res_err = res_err + (res(i) - res_old(i))^2;


end
res(nodes,1) = 0;
res_old = res;

% Write error to vector


r_err(k) = sqrt(res_err);
end

6 Alexander Mauchle
#23382572
MSM 732 Assignment 1b 2009
The multi-grid method was divided into four steps. These steps are described in detail:

Step 1 Fine Mesh


Initially the V-cycle follows the Gauss-Seidel method at least for the first step, but instead of getting
iterated a 100 times the cycle is only relaxed a few times. This is done to remove some of the high
frequency errors.

A residual is then calculated out of the process and that is passed on to the coarser mesh. This process is
called the Restriction process.

Step 2 Restriction
The restriction process only works with the errors and the residuals. Once the residual is passed down
from the fine mesh it has to be broken down. A linear process is used to reduce the residual to the new
mesh size.

The next step is to calculate the error. This formula follows the same principles as the initial set of
equations, hence the reason why only one simple function is required.

Standard Gauss Error term

Again a residual is calculated and passed down to the next mesh.

Step 3 Prolongation
In this process instead of the residual being carried to the next mesh, the error is carried to the next
process.

Using the same methods and the same Gauss-Seidel Function the error term is interpolated onto the
finer meshes. This corrects the errors in the low frequency.

Errors were from the previous step down cycle were included in this process as described in the
textbook.

Step 4 Correction
In this step the prolonged errors are added to the correct the Unknown in this case heat.

And that is one iteration in the V-cycle process

7 Alexander Mauchle
#23382572
MSM 732 Assignment 1b 2009

Results
The results for the simulations are compared below to each other and also the analytical form:

It is clear that the V-cycle far surpasses the standard Gauss-Seidel methods. In addition the error
between the real and the simulated is tiny.

8 Alexander Mauchle
#23382572
MSM 732 Assignment 1b 2009

The error

9 Alexander Mauchle
#23382572
MSM 732 Assignment 1b 2009

Conclusion
The results look good and it is impressive to see the convergence of the V-cycle process. A concern was
the values calculated in the errors. Though after much manual calculation it makes sense that they are
not linear either, considering that the initial error is take from zero.

Very interesting information to study, the author has thoroughly enjoyed this work.

10 Alexander Mauchle
#23382572
MSM 732 Assignment 1b 2009

Appendix Code
clc; clear all; %close all;format long;
% ================================ %
% 1D Heat: Gaus-Seidel and V-Cycle multigrid Finite Element Approximation
%
% MSM 732 - Assignment 2(b)
% ================================ %
% Written by: Alexander R. Mauchle
% Tel: 083 273 2926
% Date: 15 September 2009
%
% REQUIRES FUNCTONS:
% --> Gauss_Seidel.m
%
% ================================ %
disp('1D Heat: Gauss-Seidel and V-Cycle');
disp('MSM 732 - Assignment 2(b) ');
disp('Written by: Alexander R. Mauchle');
% ================================ %
% --> Initial Varibles %
% ================================ %
kth = 1; % --> Material Diffusivity: Thermal
lg = 1; % --> Length
nn = 129; % --> Number of nodes
ini = 0; % --> Initial Condition
tol = 1e-4; % --> Convergence Tolerance
it_g = 100; % --> No. Iterations Gauss
it_m = 6; % --> No. Iterations Multigrid
v1 = 2; % --> Multigrid relaxation (v1)
v2 = 3; % --> Multigrid relaxation (v2)
v3 = v1+v2; % --> Multigrid relaxation (v3)
lvl = 6; % --> V-Cycle Steps
srcf = inline('(x.^2 + 3*x).*exp(x)'); % --> Source Function
del_x = inline('lg/(nn - 1)','lg','nn');% --> Element length(Delta x)

% ============ CALL ============== %


% --> Gauss-Seidel Function %
% ================================ %

% --> Build function variblesinitial phi & res vector


nodes = nn;
phi = ones(nodes,1)*ini;
res = zeros(nodes,1);
src = srcf(linspace(0,1,nodes))';
delx = del_x(lg,nn);
iter = it_g;
relax = 1;

[phi_g,res_g,It_err_g] = Gauss_Seidel(phi,res,src,kth,delx,iter,nodes,relax);

% ============ CALL ============== %


% --> Multi-Grid: V-cyc Func. %
% ================================ %

11 Alexander Mauchle
#23382572
MSM 732 Assignment 1b 2009
% ============ CALL ============== %
% --> Multi-Grid: V-cyc Func. %
% ================================ %

% --> Build node vector


cnt = nn;
for i = 1:lvl
node(i,1) = cnt;
cnt = (cnt - 1) / 2 + 1;
end

% ========== Step 1 - Fine grid Iterations =========== %


for j = 1:it_m
% --> Build varibles for Gauss Function
nodes = nn;
%phi = ones(nodes,1)*ini;
res = zeros(nodes,1);
%src = srcf(linspace(0,1,nodes))';
delx = del_x(lg,nn);
iter = 1;
relax = v1;

[phi,res,r_err] = Gauss_Seidel(phi,res,src,kth,delx,iter,nodes,relax);

% ========== Step 2 - Restriction ==================== %


err = zeros(node(1),1);
res_n = res(:,1);
for i = 2:lvl
% --> Correct nodes size and element length
nodes = node(i);
delx = del_x(lg,nodes);

% --> Zero error


err_o = zeros(nodes,1);

% --> Resize the Residual


res_o = downsample(res_n,2);

% --> Write Residual to Matrix


res(1:nodes,i) = res_o;
% --> Do final v3 relaxation
if i == lvl
relax = v3;
end

[err_n,res_n,r_err] = Gauss_Seidel...
(err_o,[] ,res_o,kth,delx,1 ,nodes,relax);
% --> (phi(**),res,f ,kth,delx,iter,nodes,relax);

% --> Write error to matrix


err(1:nodes,i) = err_n;

12endAlexander Mauchle
#23382572
MSM 732 Assignment 1b 2009

% ========== Step 3 - Prolongation =================== %


for i = lvl:-1:2
% --> Call correct no. nodes & call error
n = node(i);
n2= node(i-1);
err_o = err(1:n,i);

% --> Interpol error to new vector


x = 1:n;
x_n = 1:0.5:n;
err_n = interp1(x,err_o,x_n)';

% --> Add the errors (See. Versteeg)


err_o = err(1:n2,i-1);
err_c = err_o + err_n;

% --> Relax the new errors


res_e = res(1:n2,i-1);
delx = del_x(lg,n2);
relax = v2;

%[phi ,res,r_err] = Gauss_Seidel...


[err_n,dum,r_err] = Gauss_Seidel...
(err_c, [],res_e,kth,delx, 1, n2,relax);
% (phi ,res,src ,kth,delx,iter,nodes,relax)

err(1:n2,i-1) = err_n;
end

% ========== Step 4 - Correction and final iter. ===== %

phi = phi + err(:,1);


It_err_m(j) = sqrt(sum(err(:,1).^2));
phi_m = phi;
end
% ========== Step # - Analytical Output ============== %
x = linspace(0,1,129);
phi_a = (1/-kth)*(exp(x).*x.^2-exp(x).*x);

13 Alexander Mauchle
#23382572
MSM 732 Assignment 1b 2009

% ========== Step # - Graphical Output =============== %


x = linspace(0,1,129);
figure(20)
plot(x,phi_m,'-o',x,phi_g,'-x',x,phi_a,'xr')
title('Gauss & Multigrid solution for: (x^2 +
3x)exp(x)','fontsize',16)
xlabel('Length [m]','fontsize',16)
ylabel('Source [ ]','fontsize',16)
legend(['V-Cycle-Multigrid Method: Iter = ' num2str(it_m)],['Gauss
Seidel Method: Iter = ' num2str(it_g)],'Analytical
result','Location','NW')
figure(30)
xg = linspace(1,size(It_err_g,2),size(It_err_g,2));
xm = linspace(1,size(It_err_m,2),size(It_err_m,2));
semilogy(xm,It_err_m,'-o',xg,It_err_g,'-x','linewidth',2)
grid on
title('Gauss & Multigrid solution for: (x^2 +
3x)exp(x)','fontsize',16)
xlabel('No. Iterations','fontsize',16)
ylabel('Euclidean Norm','fontsize',16)
legend(['V-Cycle-Multigrid Method: Iter = ' num2str(it_m)],['Gauss
Seidel Method: Iter = ' num2str(it_g)],'Location','S')
figure(40)
xg = linspace(1,size(It_err_g,2),size(It_err_g,2));
xm = linspace(1,size(It_err_m,2),size(It_err_m,2));
It_err_m = It_err_m./It_err_m(1);
It_err_g = It_err_g./It_err_g(1);
semilogy(xm,It_err_m,'-o',xg,It_err_g,'-x','linewidth',2)
grid on
title('Normalised Gauss & Multigrid solution','fontsize',16)
xlabel('No. Iterations','fontsize',16)
ylabel('Euclidean Norm','fontsize',16)
legend(['V-Cycle-Multigrid Method: Iter = ' num2str(it_m)],['Gauss
Seidel Method: Iter = ' num2str(it_g)],'Location','S')

14 Alexander Mauchle
#23382572
MSM 732 Assignment 1b 2009

Appendix Function
% 1D Gauss-Seidel Solver
%
% MSM 732 - Assignment 2(b)
% ================================ %
% Written by: Alexander R. Mauchle
% Tel: 083 273 2926
% Date: 15 September 2009
%
% This function is build on the "Algebraic" Gauss method(discribed below)
%
% Gauss-Seidel method (including Relax):
% ======================================
% A * phi = f
% r = f - A * phi_a
%
% --> it_g iterations
% --> nn Number of Nodes
% --> ini Initial values
% --> delx Element length - Delta X
% --> kth Thermal Conductivity
% --> srcv [] Source Vector
% <-- temp [] Temperature Vector
% <-- res [] Residual Vector
% <-- err [] Error Vector
%
% ================================ %
function [phi,res,r_err] = Gauss_Seidel(phi,res,src,kth,delx,iter,nodes,relax)

% Create the old residual vector


res_old = zeros(nodes,1);

for k = 1:iter
% Zero the Residual Error
res_err = 0;

% --> High frequency relaxation procedure for Gauss iterations


for j = 1 : relax

% --> Gauss-Seidel Method


for i = 2:(nodes-1)
phi(i) = 0.5*(phi(i+1) + phi(i-1)+(delx^2/kth)*src(i));
end

end

for i = 2:(nodes-1)
% --> Build A * phi_a matrix
A_phi = kth*(phi(i+1) - 2*phi(i) + phi(i-1))/delx^2;

15 Alexander Mauchle
#23382572
MSM 732 Assignment 1b 2009

% --> r = f - A * phi_a
res(i,1) = src(i,1) + A_phi;

% Build Residual Error

res_err = res_err + (res(i) - res_old(i))^2;


end
res(nodes,1) = 0;
res_old = res;

% Write error to vector


r_err(k) = sqrt(res_err);
end

16 Alexander Mauchle
#23382572

Anda mungkin juga menyukai