Top Banner
Numerical Solutions of Advection Diffusion Equations Using Finite Element Method MSc. Thesis By: Kassahun Getnet Mekonen Hawassa University Hawassa, Ethiopia June 2016
95

Numerical Solutions of Advection Diffusion Equations Using ...

May 08, 2023

Download

Documents

Khang Minh
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Numerical Solutions of Advection Diffusion Equations Using ...

Numerical Solutions of Advection Diffusion Equations

Using Finite Element Method

MSc. Thesis

By: Kassahun Getnet Mekonen

Hawassa University

Hawassa, Ethiopia

June 2016

Page 2: Numerical Solutions of Advection Diffusion Equations Using ...

Numerical Solutions of Advection Diffusion Equations

Using Finite Element Method

MSc. Thesis

By: Kassahun Getnet Mekonen

Submitted to:

School of Mathematical and Statistical sciences

In the Partial Fulfillment of the Requirements of Degree of Master of

Science by Mathematical and Statistical Modeling

Hawassa University

Hawassa, Ethiopia

June 2016

Page 3: Numerical Solutions of Advection Diffusion Equations Using ...

Approval Sheet 1

This is to certify that the thesis titled "Numerical Solutions of Advection Diffusion Equa-

tions Using Finite Element Method" submitted in partial fulfillment of the requirement for

the degree of Master of science in Mathematical and Statistical Modelling (MASTMO) to the

School of Mathematical and Statistical science, Hawassa University, and is record of original

research carried out by Kassahun Getnet Mekonen ID.No MASTMO 005/07 under my super-

vision and no part of the thesis has been submitted for another degree or diploma. The assis-

tance and the help received during the course of this investigation have been duly acknowl-

edged. Therefore I recommended that it may be accepted as fulfilling the thesis requirement.

Name of Advisor Signature Date

Page 4: Numerical Solutions of Advection Diffusion Equations Using ...

Approval Sheet 2

We, the undersigned, members of the Board of Examiners of the final open defense by

Kassahun Getnet Mekonen have read and evaluated his thesis in titled "Numerical Solu-

tions of Advection Diffusion Equations Using Finite Element Method" and Examined the

candidate. This is therefore to certify that the thesis has been accepted in partial fulfillment

of the requirement of the degree of Master of Science in Mathematical and Statistical Model-

ing (MASTMO).

Name of Chairman Signature Date

Name of Advisor Signature Date

Name of External Examiner Signature Date

Name of internal Examiner Signature Date

Page 5: Numerical Solutions of Advection Diffusion Equations Using ...

Declaration

I declare that this thesis is my original work and that all source materials used for this

thesis have been properly cited and acknowledged. This thesis has been submitted in partial

fulfillment of the requirements for MSc. degree in Mathematical and Statistical Modelling at

Hawassa University. I earnestly declare that this thesis is not submitted to any other institu-

tion any where for the award of any academic degree, diploma, or certificate.

Name Signature Date

Hawassa University

Hawassa, Ethiopia

Page 6: Numerical Solutions of Advection Diffusion Equations Using ...

Abstract

In this thesis we would implement the finite element method as a tool for the numerical so-

lution of a boundary and initial value problems. We consider the 1D Poisson and heat prob-

lems with two point boundary values , and additionally initial condition for heat equation to

demonstrate the fundamental concepts and details in FEM. We demonstrate in a situation

where we know the correct answer, so that, we know where our approximation is good and

where it is poor. In doing so, the basic idea is to first rewrite the problem as a variational

equation, and then seek a solution approximation from the space of continuous piece wise

linear’s. This discretization procedure results in a linear system that can be solved by using a

MATLAB algorithms for systems of these equations. The dissertation aims to develop numer-

ical techniques for solving the one and two dimensional advection-diffusion equation with

constant parameters. These techniques are based on the finite element approximations us-

ing Galerkin’s method in space resulting system of first order ODE’s in time and then solving

this first order ODE’s by MATLAB using back ward Euler descritization in time. For the two

dimensional problems we use the ode solver ode15i to descritize time. The validity of the

numerical model are verified using different test examples. The computed results showed

that the use of the current method in the simulation is very applicable for the solution of the

advection-diffusion equation.

Key Words: Finite Element Method, ADE, Varational Formulation, Assembly, Numerical in-

tegration.

i

Page 7: Numerical Solutions of Advection Diffusion Equations Using ...

Acknowledgment

First of all, I would like to thank almighty God and his Gracious Mother St. Mary for caring my

life and blessing my activities in advance of the completion of my research work.

My special and heart full gratitude and appreciation goes to my advisor Dr. Zerihun Kinfe

(Assistant Professor) for his great assistance, contribution, useful suggestion and constructive

criticism on this excellence advising and limitless effort in encouraging me in my work, cor-

recting and giving comments by devoting his time from the beginning of the title selection,

the proposal and to the concluding level of the thesis. I greatly appreciate him not only with

his keen and unlimited constructive advises in every aspect of my thesis work, but also with

his professional and personal ethic, that should be an icon for others.

In sponsoring, I would like to express my greatest thanks to HU-PhD-Math-Stat-Science

project, Mathematical and the Statistical Modelling (MASTMO) program and to Dr. Ayele

Taye who is the coordinator of the project for sponsored me by covering all educational and

research expenses financially. My thanks also goes to Mr. Tadele Tesfa (PhD candidate), Mr.

Silesh Gone, Mr. Mamo Teketel, Mr. Kumlachew Wubale and all Hawassa University School

of Mathematical and Statistical science school committees for their contribution and support

during the time of proposing me for the project to study in the program. I would also like to

recognize all who contributed towards the foundation of the project in one or other way.

I would like to express my gratitude to all staffs of School of Mathematical and Statistical

Science at Hawassa University for their knowledge sharing and cooperation, and all class-

mates for their cooperative work through our study time and doing the thesis.

Finally, special thanks goes to my family who pray with me all the time, in all circum-

stances by struggling my educational career and building up my life foundations for the best

achievements. Lastly, but certainly not least, I offer my regards, friends and blessings to all

those who supported me in one way or another during my studies and the time of this thesis

work.

ii

Page 8: Numerical Solutions of Advection Diffusion Equations Using ...

List of Abbreviations

FEM Finite element method

FDM Finite difference method

AD Advection diffusion

ADE Advection diffusion equation

Pe Peclet number

ODE Ordinary differential equations

PDE Partial differential equations

1D One dimensional

2D Two dimensional

3D Three dimensional

BVP Boundary value problem

IVP Initial value problem

MWR Method of weighted residuals

iii

Page 9: Numerical Solutions of Advection Diffusion Equations Using ...

Table of Contents

Abstract i

Acknowledgment ii

List of Abbreviations iii

List of Figures viii

Definition of basic terms ix

1 Introduction 1

1.1 Background of the Study . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1

1.2 Statement of the problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2

1.3 Objective . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2

1.4 Significance of the Study . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3

1.5 Thesis Outline . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3

2 Literature Review 4

2.1 Formal Developments of the Numerical Methods . . . . . . . . . . . . . . . . . . . 4

2.2 The past related works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5

3 The Mathematical Model formulation 8

3.1 Derivation of the Advection Diffusion Equation . . . . . . . . . . . . . . . . . . . . 8

3.2 Boundary Conditions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13

3.2.1 Dirichlet Boundary Conditions . . . . . . . . . . . . . . . . . . . . . . . . . . 13

3.2.2 Neumann Boundary Conditions . . . . . . . . . . . . . . . . . . . . . . . . . 13

3.2.3 Robin Boundary Conditions . . . . . . . . . . . . . . . . . . . . . . . . . . . 13

iv

Page 10: Numerical Solutions of Advection Diffusion Equations Using ...

4 Numerical Method 15

4.1 The finite element method(FEM) approach . . . . . . . . . . . . . . . . . . . . . . 15

4.1.1 The Galerkin method of the Poisson equation . . . . . . . . . . . . . . . . . 16

4.1.2 The Galerkin approximation for time dependent equation . . . . . . . . . 29

4.1.3 Galerkin approximation for 2 D equations . . . . . . . . . . . . . . . . . . . 34

4.2 Finite Element Implementation of the 1D Governing Equation . . . . . . . . . . . 39

4.2.1 Implementation of Advection Equation, 1D . . . . . . . . . . . . . . . . . . 41

4.2.2 Implementation of Diffusion Equation, 1D . . . . . . . . . . . . . . . . . . . 42

4.2.3 Implementation of One Dimensional Advection Diffusion equation . . . . 43

4.3 FEM implementation of the Two Dimensional AD equation . . . . . . . . . . . . . 44

5 Numerical Results and Discussion 48

6 Conclusion and Recommendation 58

6.1 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58

6.2 Recommendation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58

6.3 Limitation of the study . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59

References 60

7 Appendix 63

8 Biography 81

v

Page 11: Numerical Solutions of Advection Diffusion Equations Using ...

List of Figures

1 Mass balance in a volume element of a porous medium. . . . . . . . . . . . . . . . 9

2 The basis (hat) function φi associated to node x j , in this figure ϕi on a mesh.

Also shown is the half hat φ0. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18

3 The plot of Poisson equation for f (x) = 2 solved by finite element solution and

exact solution using total number of nodes (N = 10) Fig. (a), and the error of the

exact solution and the finite element method solution Fig. (b). . . . . . . . . . . . 27

4 Illustration of the hat functions φi−1 and φi , in this figure ϕ, and their support. . 31

5 Fig (a) is solution of time dependent heat equation using FEM and fig (b) is the

exact solution of the given equation with specified boundary values and initial

conditions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33

6 The error of the exact solution and the finite element solution for time depen-

dent heat equation with a space descritization width of 0.01, which is an order

of 10−5. If we decrease a space descritization width to 0.001, then the error de-

creases to an order of 10−7. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33

7 A simple mesh of a rectangleΩ= [0,2]× [0,1]. . . . . . . . . . . . . . . . . . . . . . 37

8 A simple triangulation of the domain with N = 5 total number of nodes . . . . . . 40

9 Implementation of 2D poison equation with trigonometric solution and homo-

geneous Dirichlet boundary conditions . . . . . . . . . . . . . . . . . . . . . . . . . 40

10 The error plot of the trigonometric solution which is an order of 10−4, with N =10 total number of nodes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41

11 The Matlab implementation of the advection dominated equation with f (x, t ) =cos(πx)−atπsin(πx)(x −x2)+ t cos(πx)(1−2x). . . . . . . . . . . . . . . . . . . . 48

12 The error of the FEM solution with the exact solution for the advection equation

with f (x, t ) = cos(πx)−atπsin(πx)(x−x2)+t cos(πx)(1−2x) and exact solution,

u(x, t ) = t cos(πx)(x −x2). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49

13 Solution of the diffusion equation using f = e−t sin(πx)(1− te−t +Dtπ2e−t

). . . 49

vi

Page 12: Numerical Solutions of Advection Diffusion Equations Using ...

14 Solution of the diffusion equation using D = 0.1 and D = 1 for a constant final

time of t f = 0.5. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50

15 Solution of the diffusion equation using D = 10 with t f = 0.5 and D = 0.1 for

final time of t f = 3. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50

16 Solution of the diffusion equation using D = 0.1, D = 1 and D = 10 for a constant

final time of t f = 0.5. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51

17 The solution of 1D AD equation with the source function f (x, t ) = si n(πx)(1+Dπ2t

)+aπt cos(πx) and homogeneous Dirichlet boundary conditions at every point of

time from 0 ≤ t ≤ 1 with a step size for time of ∆t = 0.1. . . . . . . . . . . . . . . . . 51

18 Implementation of 1D AD equation with the source function f (x, t ) = si n(πx)(1+Dπ2t

)+aπt cos(πx) and homogeneous Dirichlet boundary conditions and a = 0.03, D =10, in which the peclet’s number Pe = al

D = 0.003 ¿ 1 which is diffusion domi-

nated. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52

19 Implementation of 1D AD equation with the source function f (x, t ) = si n(πx)(1+Dπ2t

)+aπt cos(πx) and homogeneous Dirichlet boundary conditions and a = 10, D =0.03, in which the peclet’s number Pe = 333.33 À 1 which is advection dominated. 52

20 Numerical solution using FEM for the advection diffusion equation with f (x, t ) =a(2x + 1)− 2D − 1 and robin boundary conditions, with a diffusion coefficient

D = 20 and velocity term a = 0.1 with time t f = 1 for (a) and t f = 3 for (b). . . . . . 54

21 Numerical solution using FEM for the advection diffusion equation with f (x, t ) =a(2x + 1)− 2D − 1 and robin boundary conditions, with a diffusion coefficient

D = 0.02 and velocity term a = 1 with time t f = 1 for (a) and t f = 3 for (b). . . . . . 54

22 Numerical solution using FEM for the advection diffusion equation with f (x, t ) =a(2x + 1)− 2D − 1 and robin boundary conditions, with a diffusion coefficient

D = 0.02 and velocity term a = 1 with time t f = 1 for (a) and t f = 3 for (b) in the

descritization of time. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55

23 The numerical simulation using FEM for the steady 2D diffusion equation with

f (x, y) = Dπ2(sin(πx)+ sin(πy)), with a diffusion coefficient D = 0.5. . . . . . . . 56

vii

Page 13: Numerical Solutions of Advection Diffusion Equations Using ...

24 The numerical simulation using FEM for the steady 2D diffusion equation with

f (x, y) = Dπ2(sin(πx)sin(πy)), and a diffusion coefficient D = 0.5. . . . . . . . . . 56

25 The surface of the time dependet 2D diffusion equation with D = 100 with its

color bar plot for its source term f , f (t , x, y) = (2Dπ2 −1)e−t sin(πx)sin(πy). . . . 57

26 The surface of the time dependet 2D diffusion equation with D = 1 with its color

bar plot for its source term f , f (t , x, y) = 1. . . . . . . . . . . . . . . . . . . . . . . . 57

viii

Page 14: Numerical Solutions of Advection Diffusion Equations Using ...

Definition of basic terms

• Concentration: The concentration of a substance, such as dissolved oxygen in the water,

is defined as the amount of the substance (usually taken as its mass) per unit volume of

the fluid containing it.

• Flux: The flux of a substance in a particular direction is defined as the quantity of that

substance passing through a section perpendicular to that direction per unit area and

per unit time.

• Advection: It is the transport of pollutants or silt in a river by bulk water flow down-

stream.

• Diffusion: The process by which a substance is moved from one place to another under

the action of random fluctuations. Diffusion differs from advection in that it is random

in nature (i.e., it does not necessarily follow a fluid particle). A well known example is

the diffusion of perfume in an empty room.

• Mass Balance: A mass balance, also called a material balance, is a conservation of mass

by accounting for material entering and leaving a system, the mass flows can be iden-

tified which might have been unknown, or difficult to measure, but all revolve around

mass conservation, i.e. that matter cannot disappear or be created spontaneously.

• Matlab: It is an integrated numerical analysis package that makes it very easy to imple-

ment computational modeling codes.

ix

Page 15: Numerical Solutions of Advection Diffusion Equations Using ...

1 Introduction

1.1 Background of the Study

The purpose of this thesis is to study the numerical solutions of Advection-Diffusion equa-

tions using the method of finite element approximations.

An advection-diffusion equation (ADE) is a mathematical model that has been used to

model the concentration of pollutants. It gives the amount of pollutant concentration fields

after input of the velocity data from the hydrodynamic model which are derived from mass

balances. Formally the ADE equation is given by

ut +a∇u =∇(D∇u)+ f , (1)

where u is the concentration of the pollutant, a is the velocity of the considered particle and D

the diffusion coefficient. The term f defines the sources and sinks due to different processes

such as impact ionization, attachment, photoionization, recombination, etc.

For the vast majority of geometries and problems, Eq. 1 cannot be solved with analyti-

cal methods. Instead, an approximation of the equations can be constructed, typically based

upon different types of discretizations. These discretization methods approximate the PDEs

with numerical model equations, which can be solved using numerical methods. The solu-

tion to the numerical model equations are, in turn, an approximation of the real solution to

the PDEs.

Many numerical schemes have been implemented to approximately solve the ADE [26,

15, 31, 21]. In numerical method, a discrete approximations for the solution is computed by

descritizing the given domain in to a set of sub domains. It has two categories: finite differ-

ence method (FDM) and finite element method (FEM).

In this research we were focus on finite element method to solve the PDE given in Eq.

1. In this method, first we develop a weak formulation, from which we derive the discretiza-

tion by multiplying both sides of the ADE equation by a test function and integration by parts

(Green-Gauss Theorem) to reduce second order derivatives to first order terms, i.e., weak for-

mulation. Then we represent the approximate (FE) solution by the linear combination of

basis functions, by constructing a set of basis functions based on the mesh of our domain.

That is, the function u can be approximated by a function uh using linear combinations of

1

Page 16: Numerical Solutions of Advection Diffusion Equations Using ...

basis functions φi according to the following expressions:

u ≈ uh =∑uiφi . (2)

Here, φi denotes the basis functions and ui denotes the coefficients of the functions that

approximate u with uh . After this we get a system of linear equations and we solve the linear

system of equations for the coefficients and hence obtain the approximate solution.

1.2 Statement of the problem

Advection Diffusion equation is used for mathematical models for pollution problems and the

solution for this model is difficult analytically. So it requires numerical simulation like finite

element methods in which we can find an approximate solution for the PDE. In the study of

this research we had to answer the following questions:

• How the advection diffusion equations are derived from mass balances?

• How the Advection Diffusion equations relate with pollution models?

• How finite element method is implemented for the solution of PDE’s?

• How we can find the numerical solutions with FEM for the advection diffusion equa-

tions?

1.3 Objective

General Objective: The general objective of these study is to find the numerical solution of

the advection diffusion equations using finite element method.

Specific Objective: We have the following specific objectives in the study of these thesis:

• To derive the advection diffusion equations and relate with the mathematical model of

pollution problems.

• To implement the finite element method for 1D and 2D PDE’s.

• To use finite element method for the numerical solution of the AD equation.

• To develop an efficient MATLAB algorithm for the simulation of the numerical tech-

nique.

2

Page 17: Numerical Solutions of Advection Diffusion Equations Using ...

1.4 Significance of the Study

The study is significant in which we used the finite element methods to solve ADE’s numer-

ically. The versatility of the FEM, along with its rich of mathematical formulation and ro-

bustness makes it an ideal numerical method for a wide range of problems. The ability to

model complex geometries using unstructured meshes and employing elements that can be

individually tagged makes the method unique. The ease of implementing boundary condi-

tions as well as being able to use a wide family of element types is a definite advantage of the

scheme over other methods.

1.5 Thesis Outline

This thesis is arranged in the following ways. In section one, we were given the introduction

parts by considering the background of the study, the statement of the problem, the objectives

and significance of the study. In section two, some of the literature’s that are done before this

work and it can use as a guide and reference to do our work are given. In section three, we

were seen the mathematical model formulation of the advection diffusion equations and we

discuss on the boundary conditions of the PDE’s. Then we had to do numerical simulation

of the model by using finite element method and simulate the out puts by using Matlab soft

ware. Lastly the conclusion and recommendation parts of our work by putting necessary and

used references at the end are given.

3

Page 18: Numerical Solutions of Advection Diffusion Equations Using ...

2 Literature Review

Advection diffusion equation is a description of the transport of pollutants in water bodies

and other environments. Advection causes translation of the solute field by moving the so-

lute with the flow velocity and diffusion causes spreading of the solute plume in such envi-

ronments.

2.1 Formal Developments of the Numerical Methods

An analytical integration in two and three dimensional and time dependent case for advection

diffusion equations are mostly not easy, and hence mathematical manipulations are needed.

But in one dimensional case where the terms in y and z directions are constant and depend

only on the longitudinal co-ordinate x are much simpler and are universally considered. The

analytical integration of a one dimensional model leads to a well known formulation [26],

which is currently applied and is very useful to focus on the role of the basic terms involved in

the process of pollution transport, like the velocity of the fluid and the diffusion coefficient.

During the last decades, the progress in numerical calculus has promoted the develop-

ment of numerical procedures, which have been intensively applied for the integration of the

fundamental equations. The process starts from the transformation of the equation in to a

discrete expression. In the numerical approach we should clearly emphasis that the expres-

sion contains two terms that behave in a different way according to the basic mathemati-

cal outlines, namely, a hyperbolic term, determined by the descritization of the first order

derivative of the equation, and a parabolic term, which come from the second order deriva-

tive. These two terms follow a different way of proceeding that normally should be done one

another independently.

In the descritization process, the general differential equation is represented by an ex-

pression of finite terms, both in space with intervals of specified size, ∆x,∆y and ∆z along

the co-ordinate directions, and in time, with a ∆t interval. Normally the integration of fun-

damental equation is brought to the solution of linear equations, or linear equation systems,

with many steps and variables, and involves the concepts of linear systems of mathematics.

At the present time the numerical methods can be grouped in to two broad categories, namely

the finite difference method (FDM) and the finite element method (FEM). An extension of the

FEM is the finite volume method (FVM).

The finite difference method is the simplest and most original way of solving a differential

4

Page 19: Numerical Solutions of Advection Diffusion Equations Using ...

equation [24]. The method is essential for the application by decomposing the domain in to

finite steps with respect to space and time. There are different procedures for the achievement

of final expressions, like the Forward Euler, Backward Euler, Crank-Nicolson methods. When

we use smaller space and time intervals the descritisations should vanish and the final result

would be closer to that of the analytical solution, but it increases a computational burden.

The development of finite element method has favored by the progress of computer tech-

nology and numerical calculus, and originally applied for mechanical structures [8] intro-

duced in late 1950’s for aircraft industry. Several procedures have been tried to interpret

separately the advection and diffusion pollutant transport. The FEM can help to face more

complex problems and the privilege importance of the method is that it can be adapted to

complex geometry domains, but the element wise intervals can assume any form of size, and

obviously there is an expense of more burdensome calculations.

Compared with the finite difference method, the finite element method appears more

difficult to handle, but the positive aspects of the FEM is it can be appreciated in the most

complex problems, when an application of finite difference could request several repeated

equations, difficult to handle even with the most advanced computing facilities.

2.2 The past related works

Advection-Diffusion Equation is a description of contaminant transport in pollution models.

This equation reflects physical phenomena where in the advection process particles are mov-

ing with certain velocity form higher concentration to lower concentration. For the present

study following published literature’s, we have been reviewed via the numerical methods for

solving the advection-diffusion equations.

Numerical Solution of the 1D Advection-Diffusion Equation using standard and non stan-

dard finite difference (NSFD) schemes are done by using constant coefficients [2]. He dis-

cusses thee numerical methods for the equation with constant coefficients. He consider the

Lax-Wendrof scheme which is explicit, the Crank-Nicolson scheme which is implicit, and

a nonstandard finite difference scheme with specified initial and boundary conditions, for

which the exact solution is known to test all the three methods. He concludes that the Lax-

Wendrof and NSFD are quite good methods to approximate the 1D advection diffsion equa-

tions.

5

Page 20: Numerical Solutions of Advection Diffusion Equations Using ...

Nopparat Pochai in 2011 discuss a finite difference scheme for solving the uniform flow

model in one dimensional advection-dispersion-reaction equations (ADRE) is focused, and

the effect of nonuniform water flows in a stream is considered [19]. He use two mathemati-

cal models to simulate pollution due to sewage effluent. The first is a hydrodynamic model

that provides the velocity field and elevation of the water flow. The second is an advection-

dispersion-reaction model that gives the pollutant concentration fields after input of the ve-

locity data from the hydrodynamic model. For numerical techniques, he used the Crank-

Nicolson method for system of a hydrodynamic model and the explicit schemes to the dis-

persion model. The revised explicit schemes are modified from two computation techniques

of uniform flow stream problems: forward time central space (FTCS) and Saulyev schemes for

dispersion model. A comparison of both schemes regarding stability aspect is provided so as

to illustrate their applicability to the real-world problem.

In finite difference numerical methods of partial differential equations in finance with

matlab [22], the finite difference method for diffusion equation (ut = Duxx) is done by using

central difference for the space descritization. Here he uses both forward Euler and back ward

(implicit) Euler methods in time and evaluates the spacial derivatives at the right hand sides

at time steps j and j +1 respectively. The fully descritized schemes are given by

un, j+1 −un, j

∆t= un+1, j −2un, j +un−1, j

(∆x)2, for forward Euler method and,

un, j+1 −un, j

∆t= un+1, j+1 −2un, j+1 +un−1, j+1

(∆x)2, for backward Euler method.

He also discusses the Crank Nicolson method for the descritization by using the average value

of the right hand sides of the above two equations.

Guzman in 2014 addresses Numerical methods for advection-diffusion-reaction equa-

tions and medical applications by using finite difference methods. His thesis is reviewed

as, firstly, the study of a relaxation procedure for numerically solving advection-diffusion-

reaction equations, and secondly, a medical application [11].

Mahm Ud Ahsan in 2016 investigated and developed a numerical method to solve the

one-dimensional advection-diffusion equation for the prediction of the quality of water in

rivers [15]. In this study, he uses the procedure that time variable is eliminated first by the

Laplace transformation, and then a finite analytical method is applied in space. Since the

Laplace transformation has been used for temporal approximation, an efficient and accurate

6

Page 21: Numerical Solutions of Advection Diffusion Equations Using ...

inverse Laplace transform method is employed to obtain the solution in real time. The pro-

posed method is compared against analytical solutions and two finite-difference methods.

The results of his proposed method agree with analytical solutions without numerical oscil-

lation or diffusion. The present method is applied to steady and unsteady flows and it also

provides flexibility for uniform and non-uniform grid spacing and for a wide range of Peclet

numbers. It takes less computational effort than finite-difference methods.

Now we are going to do the numerical solutions and simulations of the advection diffu-

sion equations by using the Galerkin’s method of finite element methods. We had to do the

descritization process of the finite element method for the 1D and 2D Poisson equations in

which it is an auxiliary step in solving the advection diffusion equation with the FEM. Usually

the numerical solutions of PDE’ including our equation (Eq. 1) are done with the finite dif-

ference method, in our thesis, we had to do some of the basic steps and procedures in doing

with the FEM.

7

Page 22: Numerical Solutions of Advection Diffusion Equations Using ...

3 The Mathematical Model formulation

Let we derive the advection diffusion equations for the application of pollution models, in

which we are applied it in water pollution. Consider an elementary water body. The qual-

ity of water within this elementary water body depends on the mass of a polluting substance

present there. Water quality models then should describe the change of the mass of a pollut-

ing substance within this water body. The change of the mass of this substance is calculated

as the difference between mass-flows (mass fluxes) entering and leaving this water body, con-

sidering also the effects of internal sources and sinks of the substance, if any. The mechanism

of mass transfer into and out of this water body includes the following processes [10]:

• Mass is transported by the flow, a, of the velocity vector. This process is termed as the

advective mass transfer. The transfer of mass, that is the mass flux can be calculated as

u ∗a, where u is the concentration of the substance in the water.

• The dispersive mass transfer is usually expressed by the law of Fix which states that the

transport of the substance in a space direction is proportional to the gradient of the

concentration of this substance in that direction and the proportionality factor being

the coefficient of dispersion, D∇u.

3.1 Derivation of the Advection Diffusion Equation

Advection-Diffusion equation uses the mass balance approach by equating the difference be-

tween the mass of material entering a volume element and that leaving the element (i.e., net

influx of mass) to the rate of accumulation of mass inside the volume. The net influx is com-

posed of terms involving diffusion and advection. The diffusion coefficient that appears in

the diffusion component is assumed to be independent of concentration. In addition, it is

assumed that the densities of viscosity of all the fluids in the system are the same and that no

loss or addition of matter occurs within the system. For case of exposition, the development

will be in terms of Cartesian coordinates. Consider a volume element of porous mediums in

three - dimensional Cartesian coordinates (see Fig. 1, [3]). Since we are considering advec-

tion and diffusion as the two modes of transport of a fluid within the porous medium, we can

mathematically represent these two modes of transport (for instance in the x-direction) as

illustrated above as:

transport by advection = aud A ,

8

Page 23: Numerical Solutions of Advection Diffusion Equations Using ...

transport by diffusion = Dx∂u∂x ,

where d A is an elemental cross-sectional area of the cubic element, and Dx is the diffusion

coefficient in the x-direction. The total amount of fluid entering the volume element is:

Figure 1: Mass balance in a volume element of a porous medium.

In flow = Fxdy dz +Fy dxdz +Fzdxdy ,

where Fx , Fy and Fz represent the total amount of mass per unit cross-sectional area trans-

ported in the x, y , and z directions, respectively.

Assuming that the two components (advection and diffusion) may be superposed, the

total amount of material transported parallel to any given direction is obtained by summing

the advective and diffusive transports. Thus,

Fx = axu −nDx(∂u

∂x), (3a)

Fy = ay u −nD y (∂u

∂y), (3b)

Fz = azu −nDz(∂u

∂z), (3c)

where ax , ay , az are velocities in the x, y , and z directions, respectively, Dx , D y , and Dz

are the diffusion coefficients in the x, y , z directions, respectively, u is concentration of the

9

Page 24: Numerical Solutions of Advection Diffusion Equations Using ...

material in the volume elements, n is porosity of the medium. The negative sign indicates

that the contaminant moves forward the zone of lower fluid concentration.

The total amount of solute leaving the volume element is:

Out flow = (Fx − ∂Fx

∂xd x)d yd z + (Fy −

∂Fy

∂yd x)d xd z + (Fz − ∂Fz

∂zd x)d yd y,

where the partial terms indicate the spatial change of the fluid mass in the specified direction.

Therefore,

Outflow-Inflow =−(∂Fx

∂x+ ∂Fy

∂y+ ∂Fz

∂z

)d xd yd z.

By continuity, that is, no loss in the mass of the liquid, the total difference between the out-

flow and the inflow of the volume element must be equal to the total change in time in the

concentration of the material in the volume element. That is,

Outflow-Inflow = n∂u

∂td xd yd z,

which yields,

n∂u

∂t=−

(∂Fx

∂x+ ∂Fy

∂y+ ∂Fz

∂z

). (4)

Equation 4 is a mathematical statement of the law of conservation of mass. Substituting Eq.

3 into Eq. 4 gives:

∂x

(nDx

∂u

∂x−axu

)+ ∂

∂y

(nD y

∂u

∂y−ay u

)+ ∂

∂z

(nDz

∂u

∂z−azu

)= n

∂u

∂t.

If the flux per unit area is constant (i.e., ax , ay , and az are constants):

∂u

∂t+ Ax

∂u

∂x+ Ay

∂u

∂y+ Az

∂u

∂z= ∂

∂x

(Dx

∂u

∂x

)+ ∂

∂y

(D y

∂u

∂y

)+ ∂

∂z

(Dz

∂u

∂z

), (5)

where Ax , Ay , and Az represent average velocities (i.e., Ax = axn , Ay = ay

n , and Az = azn ).

There may be a change in u due to sources, sinks and chemical reactions, which is de-

scribed by∂u

∂t= f ,

where f = f (t , x, y, z) is the source term. The overall change in concentration is described by

combining these three effects, leading to the advection-diffusion equation

∂u

∂t+ Ax

∂u

∂x+ Ay

∂u

∂y+ Az

∂u

∂z= ∂

∂x

(Dx

∂u

∂x

)+ ∂

∂y

(D y

∂u

∂y

)+ ∂

∂z

(Dz

∂u

∂z

)+ f . (6)

10

Page 25: Numerical Solutions of Advection Diffusion Equations Using ...

The multidimensional advection-diffusion equation is used for analysing mixing problems in

rivers. One practical difficulty is that the full equation requires some information about wa-

ter depths, velocities, and diffusion coefficients, which could not conveniently be gathered

in field experiments. In some particular mixing problems, however, some of the terms in the

multidimensional advection-diffusion equation are negligibly small, so that the problem can

be simplified by reducing the model to one dimension [31] and the one dimensional advec-

tion diffusion equation is given in Eq. 7.

∂tu(x, t )+ ∂

∂x(a(x, t )u(x, t )) = ∂

∂x

(d(x, t )

∂xu(x, t )

)+ f (t , x). (7)

The time-derivative term expresses accumulation of mass at a point in space, the advec-

tion term a∇u transport of mass with the flow, and the diffusion term D∇2u reflects transport

of mass due to molecular diffusion [13]. We shall consider the equation in a spatial interval

Ω⊂ R with time t ≥ 0. An initial condition u(x,0) will be given and we also assume that suit-

able boundary conditions are provided, and for our work we consider the velocity field and

the diffusion term as constants.

Here both advection and diffusion move the pollutant from one place to another, but

each accomplishes this differently. The essential difference of the advection and diffusion is

that advection moves the pollutants in one way (downstream) but diffusion goes in both ways

(regardless of a stream direction). This is seen in the respective mathematical expressions:

• Advection a ∂u∂x has a first-order derivative, which means that if x is replaced by −x the

term changes signs (anti-symmetry);

• Diffusion D ∂2u∂x2 has a second-order derivative, which means that if x is replaced by −x

the term does not change sign (symmetry).

Questions are arise like, can we have cases of fast advection and relatively weak diffusion

and other cases of fast diffusion and negligible advection? To answer this question, we must

compare the sizes of the a ∂u∂x and D ∂2u

∂x2 terms to each other, and this is accomplishes by intro-

ducing "scales". A scale is a quantity of dimension identical to the variable to which it refers

and the value of which gives a practical estimate of the magnitude of that variable. To make

matters easy, scales are usually taken as pure constants (independent of space and time), and

their values are rounded to just one or a few digits.

11

Page 26: Numerical Solutions of Advection Diffusion Equations Using ...

Variable scale choice of value

u U Typical concentration value such as average, initial or boundary value

a V Typical velocity value, such as maximum value

x X Approximate domain length or size of release location

Using these scales, we can derive estimates of the sizes of the different terms. Since the

derivative ∂u∂x is expressing, after all, a difference in concentration over a distance (in the in-

finitesimal limit), we can estimate it to be approximately UX , and the advection term scales

as:

a∂u

∂x∼V

U

X.

Similarly, the second derivative ∂2u∂x2 represents a difference of the gradient over a distance and

is estimated at( U

X )X = U

X 2 , and the diffusion term scales as:

D∂2u

∂x2∼ D

U

X 2.

Equipped with these estimates, we can then compare the two processes by forming the ratio

of their scales:Advecton

Diffusion= V U

X

D UX 2

= V X

D

This ratio is dimensionless and Traditionally, it is called the Peclet number and is denoted by

Pe:

Pe = V X

D.

If Pe ¿ 1 (in practice, if Pe < 0.1): the advection term is significantly smaller than the diffusion

term. Physically, diffusion dominates and advection is negligible. Spreading occurs almost

symmetrically despite the directional bias of the flow. If we wish to simplify the problem, we

may drop the a ∂u∂x term, as if a were nil (no amount at all). The relative error committed in

the solution by so doing is expected to be on the order of the Peclet number, and the smaller

Pe, the smaller the error. The solutions established with diffusion only were based on such

simplification and are thus valid as long as Pe ¿ 1.

If Pe À 1 (in practice, if Pe > 10): the advection term is significantly bigger than the

diffusion term. Physically, advection dominates and diffusion is negligible, and spreading is

almost in existent, with the patch (small area) of pollutant being simply moved along by the

flow. If we wish to simplify the problem, we may drop the D ∂2u∂x2 term, as if D were nil. The rel-

ative error committed in the solution by so doing is expected to be on the order of the inverse

12

Page 27: Numerical Solutions of Advection Diffusion Equations Using ...

of the Peclet number (1/Pe), and the larger Pe, the smaller the error.

Note in this case: that the neglect of the term with the highest-order derivative reduces the

need of boundary conditions by one. No boundary condition may be imposed at the down-

stream end of the domain, and what happens there is whatever the flow brings.

If Pe ∼ 1 (in practice, if 0.1 < Pe < 10): the advection and diffusion terms are not sig-

nificantly different, and neither process dominates over the other. No approximation to the

equation can be justified, and the full equation must be utilized.

3.2 Boundary Conditions

Generally there are many functions u which satisfies the advection diffusion equation, Eq. 6

for a given source function f . Thus, to obtain a unique solution it is necessary to impose some

auxiliary constraints on the equation. These are called boundary conditions and specifies u

at the end-points. There are essentially three types of boundary conditions, namely, Dirichlet,

Neumann, and Robin boundary conditions, named after famous mathematicians.

3.2.1 Dirichlet Boundary Conditions

Dirichlet, or strong, boundary conditions prescribe the value of the solution at the boundary.

If Ω is the domain on which the given equation is to be solved and ∂Ω denotes its boundary,

the Dirichlet boundary condition represents the dependent variable being specified on the

boundary, and is expressed as u = g on ∂Ω.

3.2.2 Neumann Boundary Conditions

Neumann, or natural, boundary conditions prescribe the value of the solution derivative at

the boundary. The Neumann boundary condition is expressed as ∂u∂n = ∇u.n = g on ∂Ω.

Here the boundary condition is being specified by the normal derivative at each point of the

boundary. Neumann conditions require the boundary to be such that one can calculate the

normal derivative ∂u∂n at each point of the boundary of the given region Ω. This requires that

the unit exterior normal vector n be known at each point of the boundary.

3.2.3 Robin Boundary Conditions

Robin boundary conditions is a mixture of Dirichlet and Neumann boundary conditions.

This results to mixed boundary conditions, which are boundary conditions of different types

specified on different subsets of the boundary. Robin boundary conditions are also called

13

Page 28: Numerical Solutions of Advection Diffusion Equations Using ...

impedance boundary conditions, due to their application in electromagnetic problems. The

Robin boundary condition is expressed as:

αu +β∂u

∂n= g , on ∂Ω,

for some non-zero constants α and β and a given function g defined on ∂Ω. Here, u is the

unknown solution defined onΩ and ∂u∂n denotes the normal derivative at the boundary. Robin

boundary conditions can be used to approximate boundary conditions of either Dirichlet or

Neumann type.

14

Page 29: Numerical Solutions of Advection Diffusion Equations Using ...

4 Numerical Method

In this research we use the finite element method (FEM) to approximate the solution of the

advection diffusion equation. FEM can be examined as a powerful tool for the approximate

solution of differential equations describing different physical processes. It is based largely on

the basic finite element procedures used, those are: the formulation of the problem in varia-

tional form, the finite element discretization of this formulation and the effective solution of

the resulting finite element equations. FEM cuts a given domain into several elements (pieces

of the domain) and connected in a finite number of nodal points. Then it is considered that

the nodal displacements determine the field of displacements of each finite element. These

basic steps are the same whichever problem is considered and together with the use of the

digital computer present a quite natural approach for analysis.

4.1 The finite element method(FEM) approach

The FEM is a novel numerical method used to solve ordinary and partial differential equa-

tions. It can handle irregular boundaries in the same way as regular boundaries, [32]. It

provides a formalism for generating discrete (finite) algorithms for approximating the solu-

tions of differential equations, [28, 7]. The method is based on the integration of the terms

in the equation to be solved, in form of point discretization schemes like the finite difference

method. The FEM utilizes the method of weighted residuals and integration by parts (Green-

Gauss Theorem) to reduce second order derivatives to first order terms. The method has been

used to solve a wide range of problems, and permits physical domains to be modeled directly

using unstructured meshes typically based upon triangles or quadrilaterals in 2-D and tetra-

hedrons or hexahedrals in 3-D. The solution domain is discretized into individual elements

and these elements are operated upon individually and then solved globally using matrix so-

lution techniques. It should be thought of as a black box into which one puts the differential

equation (boundary value problem) and out of which develops an algorithm for approximat-

ing the corresponding solutions. Such a task could be done automatically by a computer, but

it necessitates an amount of mathematical skill that to day still requires human involvement,

[28].

15

Page 30: Numerical Solutions of Advection Diffusion Equations Using ...

Galerkin method for boundary value problem (BVP)

The theories of finite element methods provided the reasons why the finite element method

worked well for the class of problems in which variational statements could be obtained [17].

Extension of the mathematical basis to non-linear and non-structural problems was achieved

through the method of weighted residuals (MWR), originally conceived by Galerkin in the

early 20th century. Basically, the method requires the governing differential equation to be

multiplied by a set of predetermined weights and the resulting product integrated over space.

Technically, Galerkin’s method is a subset of the general MWR procedure, since various types

of weights can be utilized; in the case of Galerkin’s method, the weights are chosen to be

the same as the functions used to define the unknown variables. Most practitioners of the

finite element method now employ Galerkin’s method to establish the approximations to the

governing equations, [32, 28].

The Galerkin approximation method allows us to convert a continuous problem, such as

the weak formulation for the partial differential equation into a discrete problem that may be

solved numerically.

4.1.1 The Galerkin method of the Poisson equation

Let us consider the following two-point boundary value problem: find u such that

−u′′(x) = f (x),0 < x < 1,u(0) = 0,u(1) = 0, (8)

where f is a given function. Sometimes this problem is easy to solve analytically. For instance,

if f = 4 then we readily find u = −2x2 + 2x by integrating f twice and using the boundary

conditions u(0) = u(1) = 0. However, for a general f it may be difficult or even impossible to

find u with analytical techniques. Thus, we see that even a very simple differential equation

like this may be difficult to solve analytically. We take this as a good motivation for studying

numerical techniques and, in particular, for introducing the finite element method, [17].

We illustrate the Galerkin FE method for the 1D two-point BVP (Eq. 8) in the Galerkin

approach in which, if u is the solution and v is any (sufficiently regular) function such that it

satisfies the boundary conditions as follows.

The first step is constructing a variational or weak formulation, by multiplying both sides

of the differential equation by a test function v(x) satisfying the boundary conditions (BC)

16

Page 31: Numerical Solutions of Advection Diffusion Equations Using ...

v(0) = 0, v(1) = 0 and v ∈ H 10 (0,1), where H 1

0 (0,1) is the Sobolev space,

H 10 (0,1) = v ∈ L2(0,1); v ′ ∈ L2(0,1),

and it is a function space where all the functions are bounded. Let us now define a sub-space

of H where we can find our solution u. We call this V and

V = v ∈ H(X ) : v |∂Ω = 0, whereΩ is our domain,

−u′′v = f v, (9)

and then integrating from 0 to 1 (using integration by parts). First we will find the integration

by parts for −u′′v = f v from 0 to 1. Thus we have that,∫ 1

0 (−u′′v)d x can be integrated by parts

as; let v = w and −u′′d x = d s, here we get that d w = d v = v ′ and s = −u′. The integration

is now given by∫ 1

0 wd s = w s|10 −∫ 1

0 sd w . Then after substitution all the terms we get the

following ∫ 1

0(−u′′v)d x =−u′v |10 +

∫ 1

0u′v ′d x =

∫ 1

0u′v ′d x. (10)

Then the strong form of the equation −u′′(x) = f (x) becomes,∫ 1

0u′v ′d x =

∫ 1

0f vd x, (11)

which is called the variational or weak formulation of the problem. The relationship is called

variational because the function v is allowed to vary arbitrarily.

Advantages of weak form compared to strong form

Equation 11 is the final weak formulation. It is equivalent to the strong form, since we can re-

verse all the steps, and get back to the original equation. Firstly, if we look at the strong form,

we have two separate partial derivatives of u, so the strong form requires that u be continu-

ously differentiable until at least second partial derivative. Our new formulations has lowered

this requirement to only first partial derivatives by transforming one of the partial derivatives

onto the weight-function v(x, y). This is the first big advantage of a weak formulation. The

subspace V is not difficult to understand; it is a subspace of H because our weak form re-

quires that the functions are in H ; our strong form requires that u be 0 along the boundary, so

V is the subspace of all function which are zero on the boundary.

The next step is to generate a mesh, let be a uniform Cartesian mesh xi = i h, i = 0,1, ...,n,

where h = 1n , and we define the intervals as [xi−1, xi ], i = 1,2, ...,n.

17

Page 32: Numerical Solutions of Advection Diffusion Equations Using ...

After generating a mesh we construct a set of basis functions based on the mesh for each

intervals, such as the piece wise linear functions for i = 1,2, ...,n −1. The characteristic basis

functions are characterized by the following property, [1]

φi (x j ) = δi j , i , j = 1, ...,n −1, (12)

where δi j being the Kronecker delta. The function φi is therefore piece wise linear and are

fix with one node (vertex) and associate the value one to this node and zero at the remaining

nodes of the partition (see Fig. 2, [17]).

Figure 2: The basis (hat) function φi associated to node x j , in this figure ϕi on a mesh. Also

shown is the half hat φ0.

Its expression is given by

φi (x) =

x−xi−1xi−xi−1

, if xi−1 ≤ x ≤ xi

xi+1−xxi+1−xi

, if xi ≤ x ≤ xi+1

0, other wise

, for i = 1,2, . . . ,n −1. (13)

That is,

φi (x j ) = δi j =

1, if i = j

0, if i 6= j. (14)

We use this hat (basis) functions through out the 1D space of this research using equal spaced

step size (xi+1 −xi = h, for all i ). We say that the functions are basis for the following reasons.

If we want to approximate our continuous function u with a piece wise continuous linear

function u′, these functions are what we need. These functions are linearly independent of

each other; it is not possible to make one out of a combination of others. For example, only

one of these functions, φi , is non-zero (equal to 1) at node i .

18

Page 33: Numerical Solutions of Advection Diffusion Equations Using ...

The next step in approximating a PDE with FEM is represent the approximate (FE) solu-

tion by the linear combination of such basis functions, [1] as

uh(x) =n−1∑j=1

c jφi (x), (15)

where the coefficients c j are the unknowns to be determined. since the hat (basis) functions

are piece wise linear, uh(x) is also a piece wise linear function, although this is not usually the

case for the true solution u(x), and here we have,

uh(x j ) =n−1∑i=1

c jφi (x j ) = c j .

We then derive a linear system of equations for the coefficients by substituting the approxi-

mate solution uh(x) for the exact solution u(x) in the weak form

∫ 10 u′v ′d x = ∫ 1

0 f vd x, i.e.,

∫ 1

0u′

h v ′d x =∫ 1

0f vd x. (16)

Here errors are introduced since uh is an approximate solution.∫ 1

0(

n−1∑j=1

c jφi (x j ))′v ′d x =∫ 1

0f vd x.

By mathematical properties of summations, which then becomes

n−1∑j=1

c j

∫ 1

0(φi (x j ))′v ′d x =

∫ 1

0f vd x.

Now the test function v(x) is chosen to be φ1,φ2, ...,φn−1 successively, to get the system of

linear equations as follows:

If v =φ1, we have thatn−1∑j=1

c j

∫ 1

0(φi )′φ′

1d x =∫ 1

0f φ1d x.

Which is

c1

∫ 1

0φ′

1φ′1d x + c2

∫ 1

0φ′

2φ′1d x + ...+ cn−1

∫ 1

0φ′

n−1φ′1d x =

∫ 1

0f φ1d x.

If v =φ2, we have thatn−1∑j=1

c j

∫ 1

0(φi )′φ′

2d x =∫ 1

0f φ2d x.

Then it gives

c1

∫ 1

0φ′

1φ′2d x + c2

∫ 1

0φ′

2φ′2d x + ...+ cn−1

∫ 1

0φ′

n−1φ′2d x =

∫ 1

0f φ2d x.

19

Page 34: Numerical Solutions of Advection Diffusion Equations Using ...

...

If v =φn−1, we have that

n−1∑j=1

c j

∫ 1

0(φi )′φ′

n−1d x =∫ 1

0f φn−1d x.

Which becomes

c1

∫ 1

0φ′

1φ′n−1d x + c2

∫ 1

0φ′

2φ′n−1d x + ...+ cn−1

∫ 1

0φ′

n−1φ′1d x =

∫ 1

0f φn−1d x.

In a matrix form it becomes

AU = F, (17)

where

A =

a(φ1,φ1) a(φ1,φ2) . . . a(φ1,φn−1)

a(φ2,φ1) a(φ2,φ2) . . . a(φ2,φn−1)...

a(φn−1,φ1) a(φn−1,φ2) . . . a(φn−1,φn−1)

, F =

( f ,φ1)

( f ,φ2)...

( f ,φn−1)

and U =

c1

c2

...

cn−1

, where a(φi ,φ j ) = ∫ 10 φ

′iφ

′j d x and ( f ,φi ) = ∫ 1

0 f φi d x. Here the matrix A is

often referred to as the stiffness matrix, a name coming from corresponding matrices in the

context of structural problems.

Assembly of the stiffness matrix in 1D

The stiffness matrix A is symmetric for this simple problem, which makes the computation of

the matrix faster since we don’t have to compute all of the elements, symmetric matrices are

also much faster to invert. Here φi ’s are the hat functions given in Eq. 13, the entries of each

element of the stiffness matrix A is given by

Ai , j =∫ 1

0φ′

iφ′j d x,

=N−1∑e=1

∫Ωe

φ′iφ

′j d x,

=N−1∑e=1

Aei j .

20

Page 35: Numerical Solutions of Advection Diffusion Equations Using ...

similarly the load vector

Fi =∫ 1

0f φi d x =

N−1∑e=1

∫Ωe

f φi d x =N−1∑e=1

F ei .

On the interval I = (a,b) Simpson’s formula is of the form, [17]∫ b

af (x)d x = f (a)+4 f ( a+b

2 )+ f (b)

6(b −a). (18)

Then we can be illustrate by the hat functions and Simpsons formula as follows:

φi (x) =

x−xi−1hi

, if xi−1 ≤ x ≤ xi

xi+1−xhi+1

, if xi ≤ x ≤ xi+1

0, other wise

, φi−1(x) =

x−xi−2hi−1

, if xi−2 ≤ x ≤ xi−1

xi−xhi

, if xi−1 ≤ x ≤ xi

0, other wise

,

and φi+1(x) =

x−xihi+1

, if xi ≤ x ≤ xi+1

xi+2−xhi+2

, if xi+1 ≤ x ≤ xi+2

0, other wise

.

Now

Ai ,i = a(φi ,φi )e =∫Ωe

φ′iφ

′i d x,

=∫ xi

xi−1

φ′iφ

′i d x +

∫ xi+1

xi

φ′iφ

′i d x,

=∫ xi

xi−1

1

hi

1

hid x +

∫ xi+1

xi

−1

hi+1

−1

hi+1d x,

= hi

6

(1

h2i

+ 4

h2i

+ 1

h2i

)+ hi+1

6

(1

h2i+1

+ 4

h2i+1

+ 1

h2i+1

),

= 1

hi+ 1

hi+1.

,

Ai−1,i = a(φi−1,φi )e =∫Ωe

φ′i−1φ

′i d x,

=∫ xi

xi−1

φ′i−1φ

′i d x +

∫ xi+1

xi

φ′i−1φ

′i d x,

=∫ xi

xi−1

−1

hi

1

hid x,

= hi

6

(−1

h2i

+ −4

h2i

+ −1

h2i

)= −1

hi.

21

Page 36: Numerical Solutions of Advection Diffusion Equations Using ...

and

Ai ,i+1 = a(φi+1,φi )e =∫Ωe

φ′i+1φ

′i d x,

=∫ xi

xi−1

φ′i+1φ

′i d x +

∫ xi+1

xi

φ′i+1φ

′i d x,

=∫ xi+1

xi

−1

hi+1

1

hi+1d x,

= hi+1

6

(−1

h2i+1

+ −4

h2i+1

+ −1

h2i+1

)= −1

hi+1.

Each generic interior element contributes to the stiffness matrix of a 2×2 sub matrix.

A =∫ 1

0φ′

iφ′j d x =

N−1∑e=1

Ae =

1h1

−1h1

−1h1

1h1

+ 1h2

−1h2

−−1h2

1h2

+ 1h3

−1h3

. . . . . . . . .

−1hn−1

1hn−1

+ 1hn

−1hn

−1hn

1hn

. (19)

The global stiffness matrix A can be written as a sum of n simpler elemental matrices as:

A = 1h1

1 −1

−1 1

+ 1

h2

1 −1

−1 1

+ . . .+ 1

hn

1 −1

−1 1

.

i.e., A = AΩ1 + AΩ2 + . . .+ AΩn .

Each matrix AΩe ,e = 1,2, . . . ,n, is obtained by restricting the integration to one sub interval or

element Ωe and is therefore called an element stiffness matrix. From the sum we see that on

each element e this small block takes the form: Ae = 1h

1 −1

−1 1

, where h is the length of

e. We refer to Ae as the local element stiffness matrix. The summation of the element stiffness

matrices into the global stiffness matrix is called assembling. We summarize the algorithm on

how to assemble the stiffness matrix M as follows:

Algorithm to assemble the Stiffness Matrix

step 1: Allocate memory for the (n + 1)× (n + 1) matrix A and initialize all matrix entries to

zero.

step 2: For i = 1,2, . . . ,n, compute the 2×2 local element mass matrix Ae given by:

22

Page 37: Numerical Solutions of Advection Diffusion Equations Using ...

Ae = 1h

1 −1

−1 1

, where h is the length of e.

step 3:

Add Ae11 to Ai i ,

Add Ae12 to Ai i+1,

Add Ae21 to Ai+1i ,

Add Ae22 to Ai+1i+1.

step 4: End the for loop.

The whole MATLAB algorithm to assemble the stiffness matrix is found in appendix 2.

The stiffness matrix A using a uniform mesh xi+1 −xi = h, ∀i is given by the following.

A =∫ 1

0φ′

iφ′j d x =

N−1∑e=1

Ae = 1

h

1 −1

−1 2 −1

−1 2 −1. . . . . . . . .

−1 2 −1

−1 1

.

Assembly of the Load Vector in 1D

The right-hand-side, load vector of Eq. 17 contains an integral over a function f (x). In gen-

eral, exactly computing this integral is very difficult, so another numerical approximation is

required. We can use a well known integration rule composite simpson rule to approximate

these integration whose formula (for more information you can see, [25]) is given in Eq. 20,

by selecting a set of distinct N nodes in the interval [a,b] with h = b−aN , xi = a + i h for each

i = 0,1, . . . , N :

∫ b

af (x)d x = h

3

f (x0)+2

N2 −1∑j=1

f (x2 j )+4

N2∑

j=1f (x2 j−1)+ f (xN )

, j = 1,2, ..., (N

2)−1. (20)

Using the another quadrature rule for simplicity, for instance, using the Trapezoidal rule, [17]∫ b

af (x)d x = f (a)+ f (b)

2(b −a), (21)

23

Page 38: Numerical Solutions of Advection Diffusion Equations Using ...

we have

fi =∫Ω

f φi d x,

=∫ xi+1

xi−1

f φi d x,

=∫ xi

xi−1

f φi d x +∫ xi+1

xi

f φi d x,

≈ f (xi−1)φi (xi−1)+ f (xi )φi (xi )

2hi + f (xi+1)φi (xi+1)+ f (xi )φi (xi )

2hi+1,

= 0+ f (xi )

2hi + f (xi )+0

2hi+1,

= f (xi )

(hi

2+ hi+1

2

).

Now using this trapezoidal method, the approximate load vector takes the form

F =

f (x0) h12

f (x1)(

h1+h22

)f (x2)

(h2+h3

2

)...

f (xn−1)(

hn−1+hn2

)f (xn) hn

2

. (22)

Splitting F into a sum over the elements yields the n global element load vectors FΩe :

F =

f (x0)

f (x1)

h12 +

f (x1)

f (x2)

h22 +

f (x2)

f (x3)

h32 + . . .+

f (xn−1)

f (xn)

hn2

i.e., F = FΩ1 +FΩ2 + . . .+FΩn . Each vector FΩe ,e = 1,2, . . . ,n, is formally derived by restricting

the integration to element Ωe . The assembly of the load vector is summarized in the next al-

gorithm.

Algorithm to assemble the Load Vector

step 1: Allocate memory for the (n +1)×1 vector F and initialize all vector entries to zero.

step 2: For i = 1,2, . . . ,n, compute the local element load vector F e given by:

24

Page 39: Numerical Solutions of Advection Diffusion Equations Using ...

F e = h2

f (xi−1)

f (xi )

, where h is the length of e.

step 3:Add F e

1 to Fi−1,

Add F e2 to Fi ,

step 4: End the for loop.

The whole MATLAB algorithm to assemble the load vector is found in appendix 2.

Note that ci = uh(xi ),1 ≤ i ≤ N , that is the finite element unknowns are the nodal values

of the finite element solution uh, [1]. To find the numerical solution uh it is now sufficient to

solve the linear system of equation, Eq.17.

Basic Error Estimates

Since uh only approximates u, estimates of the error e = u −uh are necessary to judge the

quality and, consequently, the usability of uh . The following theorem [17] gives a key result

for deriving such error estimates.

Theorem 1 (Galerkin orthogonality). The finite element approximation uh , defined by Eq. 16,

satisfies the orthogonality ∫ 1

0(u −uh)′v ′d x = 0, ∀v ∈V

Proof. From the variational formulation we have∫ 1

0u′v ′d x =

∫ 1

0f vd x, ∀v ∈V ,

and from the finite element method∫ 1

0u′

h v ′d x =∫ 1

0f vd x, ∀v ∈V.

Subtracting these, we have that,∫ 1

0u′v ′d x −

∫ 1

0u′

h v ′d x =∫ 1

0f vd x −

∫ 1

0f vd x, ∀v ∈V ,

which gives ∫ 1

0(u −uh)′v ′d x = 0, ∀v ∈V.

25

Page 40: Numerical Solutions of Advection Diffusion Equations Using ...

Theorem 2. The finite element approximation uh , defined by Eq. 16, satisfies

‖(u −uh)′‖ ≤ ‖(u − v)′‖;∀v ∈V.

Proof. Writing u −uh = u − v + v −uh , for any v ∈V , we have

‖(u −uh)′‖2 =∫

I(u −uh)′(u − v + v −uh)′d x,

=∫

I(u −uh)′(u − v)′d x +

∫I(u −uh)′(v −uh)′d x,

=∫

I(u −uh)′(u − v)′d x,

≤ ‖(u −uh)′‖‖(u − v)′‖,

which gives ‖(u −uh)′‖ ≤ ‖(u − v)′‖.

where we used the Galerkin orthogonality to conclude that∫

I (u −uh)′(v −uh)′d x = 0, since

v −uh ∈V . Dividing by (v −uh)′ concludes the proof.

We shall now state and prove a basic a priori error estimate.

Theorem 3. The finite element approximation uh , defined by Eq. 16, satisfies

‖(u −uh)′‖2 ≤Cn∑

i=1h2

i ‖u′′‖2L2(Ii )

where C is a constant.

Proof. Starting from the best approximation result Thm. 2 and choosing v = πu, and using

the Cauchy-Schwartz inequality, the a prior error estimate immediately follows.

Defining h = max1≤i≤nhi we conclude that ‖(u −uh)′‖ ≤C h‖u′′‖ and thus the derivative

of the error tends to zero as the maximum mesh size h tend to zero.

Numerical experiment for steady state

Let us now consider a test example to solve it using matlab for −uxx = f (x),u(0) = u(1) = 0,

by considering f (x) = 2 as its exact solution is u(x) = x − x2. The matlab code for solving

this equation is located in appendix 1. The plot of the exact solution and the finite element

solution are given in fig. 3(a) and the error is an order of 10−16 which is accurate as located in

fig. 3(b).

26

Page 41: Numerical Solutions of Advection Diffusion Equations Using ...

(a) FEM and exact solution (b) Error of the solution

Figure 3: The plot of Poisson equation for f (x) = 2 solved by finite element solution and exact

solution using total number of nodes (N = 10) Fig. (a), and the error of the exact solution and

the finite element method solution Fig. (b).

We have Considered still now the two point boundary value problems with homogeneous

and Dirichlet boundary conditions and constant coefficients. Now we realize those concepts

to treat equations with variable coefficients and different types of boundary conditions. Let

us consider a more general two point boundary value problem in a general case given as:

−(a(x)u′(x))′ = f (x), x ∈ (0,1), (23a)

b0u′(0) = k0(u(0)− g0), (23b)

−b1u′(1) = k1(u(1)− g1), (23c)

where a and f are given functions, and k0,k1, g0, and g1 are given parameters. From Eq. 23, if

the parameter b0 and b1 is zero, then the given boundary condition is Dirichlet and if k0 and

k1 are zero, it is a Neumann boundary condition. If either of k0 and b0 or k1 and b1 is non zero,

then the boundary condition becomes Robin. Consider b0 = b1 = a with all other parameters

as it is for simplicity of our work. Multiplying Eq. (23a) by a test function v and integrating by

27

Page 42: Numerical Solutions of Advection Diffusion Equations Using ...

parts we have: ∫ 1

0f vd x =

∫ 1

0−(au′)′vd x,

=∫ 1

0au′v ′d x − (au′v)|10,

=∫ 1

0au′v ′d x +k1(u(1)− g1)v(1)+k0(u(0)− g0)v(0).

Note that is v only necessary to satisfy boundaries for problems with Dirichlet boundary con-

ditions. Consequently, the appropriate test and trial space is given by [17]

V = v : ‖v ′‖ <∞, ‖v‖ <∞.

Collecting terms involving u on the left hand side, and terms involving given functions on the

right hand side we obtain the following variational formulation of Eq. 23 as:

Find u ∈V such that∫ 1

0au′v ′d x +k1u(1)v(1)+k0u(0)v(0) =

∫ 1

0f vd x +k0g1v(1)+k1g1v(0), ∀v ∈V. (24)

Replacing the space V by the space of all continuous piecewise polynomials Vh in the varia-

tional formulation (Eq. 24) we obtain the finite element method as follows: find uh ∈ Vh such

that∫ 1

0au′

h v ′d x +k1uh(1)v(1)+k0uh(0)v(0) =∫ 1

0f vd x +k0g1v(1)+k1g1v(0), ∀v ∈Vh . (25)

Now by approximating the finite element solution for uh as a linear combination of basis

functions as given in Eq. 15, we have get the linear system as:

(A+R)U = F + r, (26)

where the entries of the (n +1)× (n +1) matrices A and R, and the (n +1)×1 vectors F and r

are given by:

Ai , j =∫ 1

0aφ′

iφ′j d x,

Ri , j = k0φi (0)φ j (0)+k1φi (1)φ j (1),

Fi =∫ 1

0f φi d x,

ri = k0g0φi (0)+k1g1φi (1).

28

Page 43: Numerical Solutions of Advection Diffusion Equations Using ...

4.1.2 The Galerkin approximation for time dependent equation

We then extend the concepts in order to consider a time-varying problem, and finally imple-

ment a finite element solution to a simple time-varying analogue of the original spatial partial

differential equation, given as

ut −uxx = f (x), 0 ≤ x ≤ 1, u(0, t ) = u(1, t ) = 0 andu(x,0) = g (x), (27)

with the same boundary conditions as before. The variational formulation of this equation

is obtained by multiplying it with a function v ∈ H 10 satisfying v(0, t ) = v(1, t ) = 0 and then

integrating by parts, we obtain:∫ 1

0ut .v − (uxx .v)d x =

∫ 1

0f .vd x,

∫ 1

0ut .v −

∫ 1

0(uxx .v)d x =

∫ 1

0f .vd x,∫ 1

0ut .v −

(ux .v |10 −

∫ 1

0(ux .vx)d x

)=

∫ 1

0f .vd x.

Hence the variational or weak formulation of the Heat equation becomes∫ 1

0ut .v +

∫ 1

0(ux .vx)d x =

∫ 1

0f .vd x. (28)

Now we find a finite element solution of the discrete problem by using the hat functionsφi (x)

defined in Eq. 13. For the given basis function the approximation of u and v can be written as

:

u(t , x) =N−1∑i=1

uiφi (x), (29)

v(t , x) =N−1∑j=1

v jφ j (x). (30)

Now replacing Eq.29, and Eq.30, in Eq.28 We obtain:∫ 1

0

∂t(

N−1∑i=1

uiφi (x)).N−1∑j=1

v jφ j (x)+∫ 1

0

∂x(

N−1∑i=1

uiφi (x)).∂

∂x(

N−1∑j=1

v jφ j (x))d x

=∫ 1

0f .

N−1∑j=1

v jφ j (x)d x,

N−1∑j=1

v j

(N−1∑i=1

∂tui

∫ 1

0φi (x)φ j (x)d x +

N−1∑i=1

ui

∫ 1

0φ′

i (x).φ′j (x)d x

)=

N−1∑j=1

v j

(∫ 1

0f .φ j (x)d x

),

which then becomes

N−1∑i=1

∂tui

∫ 1

0φi (x)φ j (x)d x +

N−1∑i=1

ui

∫ 1

0φ′

i (x).φ′j (x)d x =

∫ 1

0f .φ j (x)d x.

29

Page 44: Numerical Solutions of Advection Diffusion Equations Using ...

In matrix form it can be written as:

MU + AU = F,where (31)

• M is the mass matrix with entries:

Mi , j =∫ 1

0φi (x)φ j (x)d x.

• A is the stiffness matrix with entries:

Ai , j =∫ 1

0φ′

i (x)φ′j (x)d x.

• F is a column load vector with entries:

Fi ,1 =∫ 1

0f .φi (x)d x.

Here M and A are tridiagonal matrices and Eq.31 is a simple system of ODE.

The next step is to descritize the system Eq.31 in time. Here we were consider finite dif-

ference approximations specially the implicit Euler (Back ward Euler) method. By using the

back ward Euler scheme, the system Eq.31 results the following system of algebraic equations:

M

(U n+1 −U n

∆t

)+ AU n+1 = F n+1.

Here U n denotes U at time t = t n = ∆tn, and ∆t is the time step. Rearranging the terms we

obtain the system: (M

∆t+ A

)U n+1 = 1

∆tMU n +F n+1, n = 0,1,2, . . . ,

to be solved for U n+1 by using initial condition for U 0 =U (x, t = 0).

Euler Back ward represents an implicit scheme which is stable for all choices of ∆t [5].

Since the scheme is implicit, we have to solve a system of algebraic equations at each time

step.

Assembly of the mass matrix M in 1D

Let us now go through the details of how to assemble the mass matrix M . We begin by cal-

culating the entries Mi , j of the mass matrix, which involve products of hat functions given in

Eq. 13. Since each hat is a linear polynomial, the product of two hats is a quadratic polyno-

mials. Thus, Simpson’s formula (Eq. 18) can be used to integrate Mi , j =∫Ωφiφ j d x exactly.

30

Page 45: Numerical Solutions of Advection Diffusion Equations Using ...

Moreover, since the hats φi and φ j lack common support for |i − j | > 1 only Mi ,i , Mi ,i+1 ,

and Mi+1,i need to be calculated. All other matrix entries are zero by default. This is clearly

seen from Figure 4, [17] showing two neighboring hat functions and their support. As a con-

sequence, the mass matrix M is tridiagonal. Let we start on the diagonal entries of M , Mi ,i

Figure 4: Illustration of the hat functions φi−1 and φi , in this figure ϕ, and their support.

using Simpson’s formula (Eq. 18)

Mi ,i =∫Ωφ2

i d x,

=∫ xi

xi−1

φ2i d x +

∫ xi+1

xi

φ2i d x,

= 0+4( 12 )2 +1

6hi +

1+4( 12 )2 +0

6hi+1,

= hi

3+ hi+1

3, for i = 1,2, . . . ,n −1,

where hi+1 = xi+1 − xi and hi = xi − xi−1, but in our case we use a uniform mesh length h =xi+1−xi = xi −xi−1 and Mi i = 2h

3 . The first and last diagonal entry are M00 = h13 and Mnn = hn

3 ,

respectively, since the hat functions φ0 and φn are only half.

We now continue with the sub diagonal entries Mi+1,i still using Simpson’s formula we

have

Mi+1,i =∫Ωφi+1φi d x,

=∫ xi+1

xi

φi+1φi d x,

= 1.0+4( 12 )2 +0.1

6hi+1

= hi+1

6, for i = 0,1,2, . . . ,n −1.

By using a similar calculation the super diagonal entries Mi ,i+1 have the values

Mi ,i+1 = hi+1

6, for i = 0,1,2, . . . ,n −1.

31

Page 46: Numerical Solutions of Advection Diffusion Equations Using ...

Hence, the mass matrix M takes the form

M =

h13

h16

h16

h13 + h2

3h26

h26

h23 + h3

3h36

. . . . . . . . .

hn−16

hn−13 + hn

3hn6

hn6

hn3

. (32)

The global mass matrix M can be written as a sum of n simpler elemental matrices as:

M =

h13

h16

h16

h13

+

h23

h26

h26

h23

+ . . .+

hn3

hn6

hn6

hn3

.

i.e., M = MΩ1 +MΩ2 + . . .+MΩn .

Each matrix MΩe , i = 1,2, . . . ,n, is obtained by restricting the integration to one sub interval

or element Ωe and is therefore called an element mass matrix. From the sum we see that on

each element e this small block takes the form: M e = h6

2 1

1 2

, where h is the length of e.

We refer to M e as the local element mass matrix. The summation of the element mass matri-

ces into the global mass matrix is called assembling. We summarize the algorithm on how to

assemble the mass matrix M as follows:

Algorithm to assemble the Mass Matrix

step 1: Allocate memory for the (n +1)× (n +1) matrix M and initialize all matrix entries to

zero.

step 2: For i = 1,2, . . . ,n, compute the 2×2 local element mass matrix M e given by:

M e = h6

2 1

1 2

, where h is the length of e.

step 3:Add M e

11 to Mi i ,

Add M e12 to Mi i+1,

Add M e21 to Mi+1i ,

Add M e22 to Mi+1i+1.

32

Page 47: Numerical Solutions of Advection Diffusion Equations Using ...

step 4: End the for loop.

The whole MATLAB algorithm to assemble the mass matrix is found in appendix 2.

Example: Let we test the FEM Matlab code for time dependent equations for the above

calculations using a test function f = sin(πx)(1+ tπ2) and the back ward Euler method for

time descritization, the exact solution is u(x, t ) = t sin(πx). The Matlab code for this equation

is found in Appendix 3 and the Matlab implementation of these functions with finite element

method and the exact solution are shown in fig.5. The error of this equation with the finite

element method is shown in Fig. 6.

(a) FEM solution (b) Exact solution

Figure 5: Fig (a) is solution of time dependent heat equation using FEM and fig (b) is the exact

solution of the given equation with specified boundary values and initial conditions.

Figure 6: The error of the exact solution and the finite element solution for time dependent

heat equation with a space descritization width of 0.01, which is an order of 10−5. If we de-

crease a space descritization width to 0.001, then the error decreases to an order of 10−7.

33

Page 48: Numerical Solutions of Advection Diffusion Equations Using ...

4.1.3 Galerkin approximation for 2 D equations

Let us write the 2D Poisson Equation with a domain ofΩ= (0,1)2 as:

−∆u =−∇2u =−(∂2u

∂x2+ ∂2u

∂y2

)= f defined onΩ= (0,1)2. (33)

This is called the strong formulation in finite element, and says no more than the original PDE

formulation. Then we were re-formulate the original PDE (Strong form) in to weak formula-

tion , and from this form the final FE approach is established. To establish the weak form

of the PDE, we multiply it with an arbitrary function ( the same as the 1 D form), so-called

weight-function,v(x, y) to obtain

−v∇2u = v f .

Integrating this expression overΩ= (0,1)2 (this is now an integral in two-dimensions) yields,

−∫Ω

(v∇2u)dΩ=∫Ω

v f dΩ.

Since we previously claimed that v(x, y) is arbitrary, this is not completely true, since the ma-

nipulations that v(x, y) should be meaningful. Define a space of functions and call it H 1,

[28, 7],

H 1(Ω) = v :Ω→ R :∫Ω

v2,∫Ω

v2x ,

∫Ω

v2y <∞,

where, H 1 is a function space where all the functions are bounded [quadratic integrable]. We

want our functions to be well-defined, so that we can define operations on them within the

rules of integration. Let us now define a sub-space of H 1 where we can find our solution u.

We call this V and

V = v ∈ H 1(Ω) : v |∂Ω = 0.

Let us now find out the integration a little further. Let u, v ∈V . We know from calculus that

∇(v∇u) =∇v.∇u + v∇2u.

We have then, ∫Ω∇(v∇u)dΩ=

∫Ω∇v.∇udΩ+

∫Ω

v∇2udΩ.

That is ∫Ω

v∇2udΩ=∫Ω∇v.∇udΩ−

∫Ω∇(v∇u)dΩ.

Now using Gauss’s theorem on ∇(v∇u) we get,∫Ω∇(v∇u)dΩ=

∫∂Ω

v∇u.nd s = 0. (34)

34

Page 49: Numerical Solutions of Advection Diffusion Equations Using ...

In Eq. 34, we have transformed a surface integral to a line integral and d s refers to an infinites-

imal line segment. Now the integral reduces to∫Ω

(v∇2u)dΩ=−∫Ω

(∇v∇u)dΩ.

And hence we get the weak formulation of the PDE as,∫Ω

(∇v∇u)dΩ=∫Ω

( f v)dΩ.

Basis functions in 2D

We need to define basis functions for our 2D-domain. As in 1D , we have as many basis

functions as we have nodes. Each node has one basis function. We denote these functions

φi (x, y) for node number i . As in 1D φi (x j , y j ) = δi j . Each φi (x, y) is non-zero only in ele-

ments which share node i . That is the continuous 2D hat functions on a mesh with nodes

(x1, y1), . . . , (xn , yn) of the domainΩ is

φi (x, y) =

1 if (x, y) = (xi , yi )

0 if (x, y) = (x j , y j ), i 6= j, with 1 ≤ i , j ≤ n.

Now let us go back to our weak form. Here we look for a function u(x, y) which is con-

tinuously differentiable. Note that all of our φi functions are continuously differentiable. In

our specific problem, we are in search of an approximate function uh(x, y) which is pieces

wise linear on each element. As in 1D , we know that we can write this function as a linear

combination of our basis functions.

uh(x, y) =N∑

i=1uiφi (x, y).

And again,

uh(x j , y j ) =N∑

i=1uiφi (x j , y j ) = u j .

To make the problem more convenient and easy to read, let us define two forms a and l as

a(u, v) = ∫Ω (∇v∇u)dΩ and l (v) = ∫

Ω ( f v)dΩ.

Note that a(u, v) = a(v,u). Using the integral rules, we also find that a and l are bi linear and

linear forms, respectively. Now we can rewrite our weak form as

a(u, v) = l (v), u, v ∈V.

35

Page 50: Numerical Solutions of Advection Diffusion Equations Using ...

Going towards our final formulation, let us express our approximation uh and our weight

function (which is arbitrary and in V ) v as

uh(x, y) =N∑

i=1uiφi .

v(x, y) =N∑

j=1v jφ j .

The number N is the total number of nodes, including boundary nodes. Note that we are

using v in terms of our basis functions, which is called the Galerkin method. We put these

two expressions in our weak form and complete the expression, we get the following:

a

(N∑

i=1v jφ j ,

N∑i=1

uiφi

)= l

(N∑

i=1v jφ j

),

a

(v1φ1 +

N∑j=2

v jφ j ,N∑

i=1uiφi

)= l

(N∑

i=1v jφ j

),

a

(v1φ1,

N∑i=1

uiφi

)+a

(N∑

j=2v jφ j ,

N∑i=1

uiφi

)= l

(N∑

i=1v jφ j

),

N∑j=1

a

(v jφ j ,

N∑i=1

uiφi

)=

N∑i=1

v j l (φ j ),

N∑j=1

N∑i=1

a(v jφ j ,uiφi

)= N∑i=1

v j l (φ j ),

N∑j=1

v j

N∑i=1

a(φ j ,φi

)ui =

N∑i=1

v j l (φ j ).

The above expressions are done by using the properties of the linear and bi linear forms of a

and l . The last expression can be written in compact form as:

V T AUh =V T F ⇒ AUh = F.

Here V = (v1, v2, . . . , vN )T and Uh = (uh1,uh2, . . . ,uhN )T , and the matrices A and F are given as:

A =

a(φ1,φ1) a(φ1,φ2) . . . a(φ1,φN )

a(φ2,φ1) a(φ2,φ2) . . . a(φ2,φN )...

......

a(φN ,φ1) a(φN ,φ2) . . . a(φN ,φN )

, and F =

l (φ1)

l (φ2)...

l (φN )

.

The system of linear equations is called the Galerkin system, and the function Uh computed

by substituting the solution of these system into the approximate solution is the Galerkin so-

lution. The matrix A is referred to as the stiffness matrix.36

Page 51: Numerical Solutions of Advection Diffusion Equations Using ...

Assembly of the Stiffness Matrix in 2D

We next show how to compute the stiffness matrix A in two dimensions by considering a

simple mesh of the rectangleΩ in Fig. 7, [17].

Figure 7: A simple mesh of a rectangleΩ= [0,2]× [0,1].

On this mesh we wish to compute the stiffness matrix A, given by

Ai , j = a(φi ,φ j ) =∫Ω∇φi∇φ j d xd y.

To do so, we first break the integral over the whole domain Ω into a sum of integrals over the

triangles Ki , i = 1,2,3. We then have

Ai , j =3∑

i=1

∫Ki

∇φi∇φ j d xd y.

There are only three non-zero hat functions on each triangle. For example, the only non-zero

hats on K2 areφ1,φ2 andφ4. Integrating the product of these we see that K2 , or any triangle for

that matter, gives a total of 3.3 = 9 integral contributions to A. Moreover, for a given triangle,

the index on the non-zero hat functions are the same as the node numbers for that triangle.

Thus, inspecting which hats are non-zero on which triangle, we can therefore say which rows

and columns vanish in each matrix AKi . For example, the only non-zero entries of AK1 are

AK11,1, AK1

1,4, AK11,5, AK1

4,1, AK14,4, AK1

4,5, AK15,1, AK1

5,4, and AK15,5.

We will calculate the 9 values aKi j simultaneously. Let ni1 = (xi1 , yi1 ), ni2 = (xi2 , yi2 ) and

ni3 = (xi3 , yi3 ) be the coordinates of the three nodes of triangle Ki . On one hand, the numerical

solution uh on Ki is a linear combination of the three relevant hat functions:

uh(x, y) = ci1φi1 (x, y)+ ci2φi2 (x, y)+ ci3φi3 (x, y).

37

Page 52: Numerical Solutions of Advection Diffusion Equations Using ...

The transposed gradient of uh can be written as a matrix vector product:

∇T uh(x, y) = ci1∇Tφi1 (x, y)+ ci2∇Tφi2 (x, y)+ ci3∇Tφi3 (x, y),

= (∇Tφi1 (x, y),∇Tφi2 (x, y),∇Tφi3 (x, y))

ci1

ci2

ci3

.

On the other hand, the numerical solution uh on Ki is a linear function:

uh(x, y) =α1 +α2x +α3 y.

At the triangle nodes the solution takes the values ci1 ,ci2 and ci3

1 xi1 yi1

1 xi2 yi2

1 xi3 yi3

︸ ︷︷ ︸

Fi

α1

α2

α3

=

ci1

ci2

ci3

.

Since Ki is a triangle, matrix Fi is regular and di = |Fi | 6= 0. Hence, we can use Cramer’s rule to

find the components α2 and α3 explicitly:

∇T uh(x, y) = α2

α3

=1

di

yi2 − yi3 yi3 − yi1 yi1 − yi2

xi3 −xi2 xi1 −xi3 xi2 −xi1

︸ ︷︷ ︸

Ei

ci1

ci2

ci3

.

Now we can compare the two representations of ∇T uh . Since ci1 , ci2 , and ci3 are arbitrary

numbers, we have equality of the two matrices:

(∇Tφi1 (x, y),∇Tφi2 (x, y),∇Tφi3 (x, y)) = Ei .

Here the gradients of the hat functions are row vectors with constant numbers. This implies

Ai , j =

∇Tφi1 (x, y)

∇Tφi2 (x, y)

∇Tφi3 (x, y)

(∇Tφi1 (x, y),∇Tφi2 (x, y),∇Tφi3 (x, y))∫ ∫

Ki

1d xd y,

=1

2|di |E T

i Ei ,

38

Page 53: Numerical Solutions of Advection Diffusion Equations Using ...

using the area 12 |di | of the triangle Ki . Hence the element stiffness matrix Ai , j on a triangle Ki

with vertices (xi1 , yi1 ), (xi2 , yi2 ), (xi3 , yi3 ) can be computed with [16],

Ai , j = 1

2|di |E T

i Ei ,

using the area 12 |di | of the triangle Ki and coordinate matrix Ei :

di =

1 xi1 yi1

1 xi2 yi2

1 xi3 yi3

, Ei = 1di

yi2 − yi3 yi3 − yi1 yi1 − yi2

xi3 −xi2 xi1 −xi3 xi2 −xi1

Assembly of the Load Vector in 2D

The load vector F is assembled using the same technique as the mass matrix A, that is, by

summing element load vectors Fk over the mesh. On each element K we get a local 3× 1

element load vector FK with entries

F ik =

∫K

f φi d x, i = 1,2,3

Using corner quadrature, for instance, to compute these integrals we have that

F ik ' 1

3f (ni ) | K |,

where | K | is the area of the triangle (element) K , andni = ni 1 +ni 2 +ni 3, sum of nodes.

Example: Let us see a test example for 2D Poisson equation −(uxx +uy y ) = f (x, y) onΩ=(0,1)2. We test it by assigning a trigonometrical function f (x, y) =−2+ y sin(x) and the exact

solution is u(x, y) = y2+y sin(x) on the domainΩ= [0,1]2 with Dirichlet boundary conditions.

A triangulation of the domainΩwith total number of nodes N = 5 is found in fig.8.

The Matlab output of this equation for exact and finite element solution is located in fig. 9,

and the out put for the error for this equation is located on fig. 10 which is an order of 10−4.

The Matlab algorithm for this problem is found in appendix 4.

4.2 Finite Element Implementation of the 1D Governing Equation

Let us consider the one dimensional Advection-Diffusion equation given by:

∂u

∂t+a

∂u

∂x= D

∂2u

∂x2+ f , u(0) = u(1) = 0, (35)

39

Page 54: Numerical Solutions of Advection Diffusion Equations Using ...

Figure 8: A simple triangulation of the domain with N = 5 total number of nodes

(a) Exact solution (b) FEM solution

Figure 9: Implementation of 2D poison equation with trigonometric solution and homoge-

neous Dirichlet boundary conditions

40

Page 55: Numerical Solutions of Advection Diffusion Equations Using ...

Figure 10: The error plot of the trigonometric solution which is an order of 10−4, with N = 10

total number of nodes.

where u is the concentration of the pollutant, a is the velocity, f is the source term, and D is

the diffusion coefficient, with all the three variables be constants.

4.2.1 Implementation of Advection Equation, 1D

Let we first consider only an advection equation, that is the diffusion term does not exist;

∂u

∂t+a

∂u

∂x= 0.

By the same procedure as above, we multiply the equation by a test function v(x), which

satisfies the boundary conditions v(0) = v(1) = 0 and then integrating by parts we have that:

∂u

∂t.v +a

∂u

∂x.v = 0,∫ 1

0

(∂u

∂t.v +a

∂u

∂x.v

)d x = 0.

Hence by using integration by parts, we obtain∫ 1

0(∂u

∂t.v)d x +

∫ 1

0(a∂u

∂x.v)d x = 0, (36)

which is the weak formulation of the one dimensional advection equation. Then let the ap-

proximate solution for u be given by a linear combination of basis functionsφi = δi j , as given

in Eq. 29, and also for v as in Eq. 30. Now substituting Eq.29 and Eq.30 in the weak formula-

tion of the equation Eq.36, we have:∫ 1

0(∂

∂t

N−1∑i=1

uiφi .N−1∑j=1

v jφ j )d x +∫ 1

0(a

∂x

(N−1∑i=1

uiφi

).

N−1∑j=1

v jφ j )d x = 0.

41

Page 56: Numerical Solutions of Advection Diffusion Equations Using ...

which then implies,

N−1∑j=1

v j

(∂

∂t

N−1∑i=1

ui

∫ 1

0φi .φ j d x +a

N−1∑i=1

ui

∫ 1

0φ′

i .φ j d x

)= 0.

That is∂

∂t

N−1∑i=1

ui

∫ 1

0φi .φ j d x +a

N−1∑i=1

ui

∫ 1

0φ′

i .φ j d x = 0.

In a matrix form it can be written as:

MU +aBU = 0, (37)

where M is the mass matrix with entries given in Eq. 32, and B is a matrix with entries:

Bi j =∫ 1

0φ′

i (x)φ j (x)d x =

0, if i = j

−12 , if i − j = 1

12 , if j − i = 1

0, other wise

. (38)

Now the system of equation, that is Eq.37 is a simple system of ordinary differential equations.

To solve this system of ode’s, we have to use a back ward Euler method and a MATLAB soft

ware to solve the system at each time steps using an initial condition.

4.2.2 Implementation of Diffusion Equation, 1D

Let we now consider the diffusion equation

∂u

∂t= D

∂2u

∂x2.

We multiply the equation by a test function v(x), which satisfies the boundary conditions

v(0) = v(1) = 0 and then integrating by parts we have that:

∂u

∂t.v = D

∂2u

∂x2.v∫ 1

0

(∂u

∂t.v

)d x =

∫ 1

0

(D∂2u

∂x2.v

)d x

Hence by using integration by parts, we obtain∫ 1

0

∂u

∂t.vd x =−D

∫ 1

0

∂u

∂x.∂v

∂xd x, (39)

which is the weak formulation of the one dimensional Diffusion equation. Now substituting

Eq.29 and Eq.30 in the weak formulation of the equation Eq.39, we have:∫ 1

0(∂

∂t

N−1∑i=1

uiφi .N−1∑j=1

v jφ j )d x =−D∫ 1

0(∂

∂x

N−1∑i=1

uiφi .∂

∂x

N−1∑j=1

v jφ j )d x.

42

Page 57: Numerical Solutions of Advection Diffusion Equations Using ...

which then implies,

N−1∑j=1

v j

(∂

∂t

N−1∑i=1

ui

∫ 1

0φi .φ j d x

)=

N−1∑j=1

v j

(−D

N−1∑i=1

ui

∫ 1

0φ′

i .φ′j d x

).

That is∂

∂t

N−1∑i=1

ui

∫ 1

0φi .φ j d x =−D

N−1∑i=1

ui

∫ 1

0φ′

i .φ′j d x.

In a matrix form it can be written as:

MU +D AU = 0, (40)

where M is the mass matrix with entries given in Eq. 32 and A is the stiffness matrix with

entries given in Eq. 19. Now the system of equation, that is Eq.40 also is a simple system of

ordinary differential equations and we can solve this system of Ode’s to get the solution of the

original PDE.

4.2.3 Implementation of One Dimensional Advection Diffusion equation

Let us now solve the 1D governing (advection diffusion) equation

∂u

∂t+a

∂u

∂x= D

∂2u

∂x2+ f , u(0) = u(1) = 0,

by using FEM. Let we find the weak formulation of the equation by multiplying the equation

with a test function v(x), which satisfies the boundary conditions v(0) = v(1) = 0 and then

integrating by parts as the same procedure above. we have that:

∂u

∂t.v +a

∂u

∂x.v = D

∂2u

∂x2.v + f .v,

∫ 1

0

(∂u

∂t.v +a

∂u

∂x.v

)d x =

∫ 1

0

(D∂2u

∂x2.v + f .v

)d x.

Using integration by parts, we have then∫ 1

0

∂u

∂t.vd x +

∫ 1

0a∂u

∂x.vd x =−D

∫ 1

0

∂u

∂x.∂v

∂xd x +

∫ 1

0f .vd x, (41)

which is the weak formulation of the one dimensional advection-Diffusion equation. Substi-

tuting Eq.29 and Eq.30 in the weak formulation of the equation Eq.41, we have:∫ 1

0(∂

∂t

N−1∑i=1

uiφi .N−1∑j=1

v jφ j +∫ 1

0a∂

∂x

(N−1∑i=1

uiφi

).

N−1∑j=1

v jφ j )d x

=−D∫ 1

0(∂

∂x

N−1∑i=1

uiφi .∂

∂x

N−1∑j=1

v jφ j )d x +∫ 1

0f .

N−1∑j=1

v jφ j d x.

43

Page 58: Numerical Solutions of Advection Diffusion Equations Using ...

which then implies,

N−1∑j=1

v j

(∂

∂t

N−1∑i=1

ui

∫ 1

0φi .φ j d x +a

N−1∑i=1

ui

∫ 1

0φ′

i .φ j d x

)

=N−1∑j=1

v j

(−D

N−1∑i=1

ui

∫ 1

0φ′

i .φ′j d x +

∫ 1

0f .φ j d x

).

That is

∂t

N−1∑i=1

ui

∫ 1

0φi .φ j d x +a

N−1∑i=1

ui

∫ 1

0φ′

i .φ j d x =−DN−1∑i=1

ui

∫ 1

0φ′

i .φ′j d x +

∫ 1

0f .φ j d x.

In a matrix form it can be written as:

MU +aBU +D AU = F, (42)

where M is the mass matrix with entries given in Eq. 32, B is a matrix with entries given in

Eq. 38, A is the stiffness matrix given in Eq. 19 and F is a load vector given in Eq. 22. Now the

system of equation, that is Eq.42 can be written in the form:

MU + (aB +D A)U = F, (43)

which is a simple system of ordinary differential equations. For solving this system of Ode’s,

we have to use a Matlab soft ware in which it has a number of tools for numerically solving

ordinary differential equations. We would focus on the back ward Euler method to descritize

time.

4.3 FEM implementation of the Two Dimensional AD equation

The 2D advection diffusion equation with the same and constant velocity and diffusion term

is given by

ut +a(ux +uy ) = D(uxx +uy y )+ f , (x, y) ∈Ω= [0,1],

with homogeneous boundary conditions. That is

ut +a∇u = D∇2u + f .

Now to find a weak formulation for this 2D equation, we multiply both sides of the equation

with a test function v = v(x, y) ∈V which satisfies the boundary conditions.

ut .v +a∇u.v = D∇2u.v + f v.

44

Page 59: Numerical Solutions of Advection Diffusion Equations Using ...

Here integrating this over the domainΩ yields the following:∫Ω

(ut .v +a∇u.v)dΩ=∫Ω

(D∇2u.v + f v

)dΩ.

We see from the 2D FEM of Poisson equation (using Gauss theorem and the transformation

of a surface integral to a line integral) that∫Ω v∇2udΩ=−∫

Ω∇v∇udΩ, and hence we get:∫Ω

(ut .v +a∇u.v)dΩ=−D∫Ω

(∇v∇u)dΩ+∫Ω

f vdΩ.

∫Ω

ut .vdΩ=−∫Ω

(D∇v∇u +a∇u.v)dΩ+∫Ω

f vdΩ. (44)

Here Eq. 44 is the weak formulation and it can be simplified as

(ut , v) = l (u, v)+ ( f , v) ∀v ∈V , (45)

where (ut , v) = ∫Ωut .vdΩ and l (u, v) =−∫

Ω (D∇v∇u +a∇u.v)dΩ.

Given a FE space V , withφi (x, y), i = 1,2, . . . , N denoting a set of basis functions for V , we

seek the FE solution of form

uh(x, y, t ) =N∑

j=1u j (t )φ j (x, y). (46)

And taking the test function v(x, y) as a linear combination of basis functions

v(x, y) =N∑

j=1v jφ j , with v j are constants. (47)

Substituting this expression (46 and 47) into eq.45, we obtain(N∑

j=1u′

j (t )φ j (x, y),N∑

j=1v jφ j

)= l

(N∑

j=1u j (t )φ j (x, y),

N∑j=1

v jφ j

)+ ( f ,

N∑j=1

v jφ j ). (48)

Then we get the linear system of ordinary differential equations in the u j (t ) as:(u′

1(t )φ1(x, y)+N∑

j=2u′

j (t )φ j (x, y),N∑

j=1v jφ j

)= l

(u1(t )φ1(x, y)+

N∑j=1

u j (t )φ j (x, y),N∑

j=1v jφ j (x, y)

)+

( f ,N∑

j=1v jφ j (x, y)),

(u′

1(t )φ1,N∑

j=1v jφ j

)+

(N∑

j=2u′

j (t )φ j ,N∑

j=1v jφ j

)= l

(u1(t )φ1,

N∑j=1

v jφ j

)+

45

Page 60: Numerical Solutions of Advection Diffusion Equations Using ...

l

(N∑

j=1u j (t )φ j ,

N∑j=1

v jφ j

)+ ( f ,

N∑j=1

v jφ j ),

N∑j=1

(u′

j (t )φ j ,N∑

j=1v jφ j

)=

N∑j=1

l

(u j (t )φ j ,

N∑j=1

v jφ j

)+ ( f ,

N∑j=1

v jφ j ),

N∑j=1

N∑j=1

(u′

j (t )φ j , v jφ j

)=

N∑j=1

N∑j=1

l(u j (t )φ j , v jφ j

)+ N∑j=1

( f , v jφ j ).

Since v j ’s are constants we have also that:

N∑j=1

v j

N∑j=1

(φ j ,φ j

)u′

j (t ) =N∑

j=1v j

N∑j=1

l(φ j ,φ j

)u j (t )+

N∑j=1

v j ( f ,φ j ).

The corresponding problem can therefore be expressed as

V T MU =V T AU +V T F.

That is

MU = AU +F. (49)

Where, M =

(φ1,φ1) (φ1,φ2) . . . (φ1,φN )

(φ2,φ1) (φ2,φ2) . . . (φ2,φN )...

......

(φN ,φ1) (φN ,φ2) . . . (φN ,φN )

,

A =

l (φ1,φ1) l (φ1,φ2) . . . l (φ1,φN )

l (φ2,φ1) l (φ2,φ2) . . . l (φ2,φN )...

......

l (φN ,φ1) l (φN ,φ2) . . . l (φN ,φN )

, and F =

( f ,φ1)

( f ,φ2)...

( f ,φN )

.

There are many methods to solve the above problem involving the system of first order

ODE. We can use FD methods that will descritize in time by using Explicit Euler method, Im-

plicit Euler method or the Crank-Nicolson method, by considering an appropriate initial con-

dition, [18, 14, 12]. But for this thesis in the two dimensional case, we can use the ODE Suite

in Matlab which is the matlab build in system of ode solver, ODE15I.

ODE15I Solves fully implicit differential equations using variable order method. It inte-

grates the system of differential equations f (t ,u,u′) = 0 from initial time t0 to final time t f ,

with initial conditions u(0, x, y) = u0 and ut (0, x, y) = ut0. Its command is [Tout ,U out ] =

46

Page 61: Numerical Solutions of Advection Diffusion Equations Using ...

ODE15I (ODEFU N ,T SPAN ,u0,ut0) with T SPAN = [t0, t f ]. ODEFU N is a function handle.

For a scalar T and vectors U and U t , ODEFU N (T,U ,U t ) must return a column vector corre-

sponding to f (t ,u,ut ′). Each row in the solution array U out corresponds to a time returned

in the column vector Tout .

47

Page 62: Numerical Solutions of Advection Diffusion Equations Using ...

5 Numerical Results and Discussion

In this section, we are compared for the advection-diffusion equations with an exact solu-

tion for the given finite element methods and then we solve the equation with out know-

ing the exact solution. The comparison is carried out by means of computed solutions for

a wide range of characteristic parameters. Linear elements are employed at the discretiza-

tion in case of one-dimensional problem and bilinear elements in case of two-dimensional

problems. Dirichlet and general boundary conditions are considered with different initial

conditions and different size of computational domain.

First, consider the advection equation ut + aux = f with sources and sinks function,

f (x, t ) = cos(πx)− atπsin(πx)(x − x2)+ t cos(πx)(1− 2x) and the velocity parameter a = 3,

with homogeneous boundary conditions. The FM solution using Matlab with a back ward

Euler descritization in time is given in Fig. 11(a), and the Matlab algorithm for this is found

in Appendix 5 with N = 100 nodes. To saw our error the exact solution for this equation is

u(x, t ) = t cos(πx)(x − x2) and its graph is Fig. 11(b). The error of this equation is an order of

10−4 which is found in Fig. 12. If we increase the number of nodes from N = 100 to N = 1000,

then our numerical solution becomes more accurate and we saw that the error is an order of

10−7 by modifying h = 0.001 from the algorithm.

(a) FEM solution (b) Exact solution

Figure 11: The Matlab implementation of the advection dominated equation with f (x, t ) =cos(πx)−atπsin(πx)(x −x2)+ t cos(πx)(1−2x).

Consider a source and sink term, f = e−t sin(πx)(1− te−t +Dtπ2e−t

)for the 1D diffusion

48

Page 63: Numerical Solutions of Advection Diffusion Equations Using ...

Figure 12: The error of the FEM solution with the exact solution for the advection equation

with f (x, t ) = cos(πx) − atπsin(πx)(x − x2) + t cos(πx)(1 − 2x) and exact solution, u(x, t ) =t cos(πx)(x −x2).

equation ut = Duxx + f . The solution of the equation by using back ward Euler method in

time with diffusion coefficient D = 300 and N = 100, total number of descritizations is given

in Fig. 13(a) and the matlab algorithm for this problem is found in appendix 6. The exact

solution for the given source term is u(x, t ) = te−t sin(πx) and the error for this is an order of

10−5 as located in Fig. 13(b) with the specified number of nodes and diffusion coefficient.

(a) FEM solution (b) Error with the exact solution

Figure 13: Solution of the diffusion equation using f = e−t sin(πx)(1− te−t +Dtπ2e−t

).

In [22], there is a diffusion (ut = Duxx) example solved with finite difference methods, and let

we solve that equation with the finite element method. He solves the diffusion (heat) equa-

tion by using initial condition u(x,0) = sin(πx) and with homogeneous Dirichlet boundary

49

Page 64: Numerical Solutions of Advection Diffusion Equations Using ...

conditions in the interval 0 ≤ x ≤ 1. If we consider different values for final time and diffusion

coefficient we get the following simulations (Fig. 14 to Fig. 16) with a similar descritization

of space (x) in to 50 nodes. When we saw the results of those figures (Fig. 14 to Fig. 16) we

(a) FEM sol. with D = 0.1 and t f = 0.5 (b) FEM sol. with D = 1 and t f = 0.5

Figure 14: Solution of the diffusion equation using D = 0.1 and D = 1 for a constant final time

of t f = 0.5.

(a) FEM sol. with D = 10 and t f = 0.5 (b) FEM sol. with D = 0.1 and t f = 3

Figure 15: Solution of the diffusion equation using D = 10 with t f = 0.5 and D = 0.1 for final

time of t f = 3.

can observe the following. If we use a small amount of diffusion coefficient, then it has a mall

diffusion process and if we consider relatively large diffusion coefficient, the diffusion process

is faster. Hence our finite element method is reasonable and accurate.

Let we now consider the ADE ut + aux − Duxx = f with f (x, t ) = sin(πx)(1+Dπ2t

)+aπt cos(πx), u(x,0) = 0, u(0, t ) = u(1, t ) = 0. By using the forward Euler descritization in time

50

Page 65: Numerical Solutions of Advection Diffusion Equations Using ...

(a) FEM sol. with D = 1 and t f = 3 (b) FEM sol. with D = 10 and t f = 3

Figure 16: Solution of the diffusion equation using D = 0.1, D = 1 and D = 10 for a constant

final time of t f = 0.5.

the Matlab implementation with a = 0.03 and D = 10 for this problem is given in Fig. 18. The

peclet’s number Pe = alD = 0.003 ¿ 1 which is diffusion dominated for this choice of a and D .

If we change the parameters to a = 10 and D = 0.03, the peclet’s number Pe = alD = 333.33 À 1

which is advection dominated and located in Fig. 19. The Matlab algorithm for this is given

in appendix 7.

(a) FEM solution for a = 0.03, D = 10 at

each time

(b) FEM solution for a = 10, D = 0.03 at

each time

Figure 17: The solution of 1D AD equation with the source function f (x, t ) =si n(πx)

(1+Dπ2t

)+ aπt cos(πx) and homogeneous Dirichlet boundary conditions at every

point of time from 0 ≤ t ≤ 1 with a step size for time of ∆t = 0.1.

Since we implements zero flux concentrations of the pollutants in the boundaries, the

total mass of the pollution should remain constant. These boundaries physically correspond

to a system where the species is enclosed inside a mesh that it cannot penetrate, however,

51

Page 66: Numerical Solutions of Advection Diffusion Equations Using ...

(a) FEM solution (b) Bar plot of the solution

Figure 18: Implementation of 1D AD equation with the source function f (x, t ) =si n(πx)

(1+Dπ2t

)+ aπt cos(πx) and homogeneous Dirichlet boundary conditions and a =0.03, D = 10, in which the peclet’s number Pe = al

D = 0.003 ¿ 1 which is diffusion dominated.

(a) FEM solution (b) Bar plot of the solution

Figure 19: Implementation of 1D AD equation with the source function f (x, t ) =si n(πx)

(1+Dπ2t

)+ aπt cos(πx) and homogeneous Dirichlet boundary conditions and a =10, D = 0.03, in which the peclet’s number Pe = 333.33 À 1 which is advection dominated.

52

Page 67: Numerical Solutions of Advection Diffusion Equations Using ...

the mesh allows the fluids to diffuse through the flow field. We see that all masses eventu-

ally concentrate along the domain as we have seen in Fig. 18 and Fig. 19. This steady state

corresponds to diffusive and advective fluxes balancing each other. The flow carries addi-

tional mass towards the domain, but the density gradient limits how much more mass can be

deposited.

We see now on an arbitrary input concentration by using Neumann and Robin boundary

conditions. Here, the problem is undetermined because of an unknown input concentration

and hence unknown exit concentration and depends an the parameters. Authors of previous

works on problems of this type have done on a known exit concentration by assuming a con-

tinuous and constant concentration at the flow boundary of Dirichlet type. This yields we to

consider a problem by forcing the flow boundaries using Neuman and Robin boundary con-

ditions. This conditions yields the flow of the concentration to move freely. Here the velocity

term and the diffusion coefficient highly affects the flow of the concentration. We saw it by

giving a source function f (x, t ) = a(2x +1)−2D −1, 0 ≤ x ≤ 1, 0 ≤ t ≤ t f with a robin bound-

ary condition Dux(0, t ) = k0(u(0, t )+ g0 and −Dux(1, t ) = k1(u(1, t )+ g1 and by considering

different robin boundary parameters.

If the k0 and k1 parameters in the robin boundary condition is zero, then the boundary

condition is Neumann and the FE solution for the given source term using D = 20 and a = 0.1,

(g0, g1) = (20,60) is given in Fig. 20(a) using a total time of t f = 1 and 20(b) using a total time

of t f = 3. The matlab algorithm is found in appendix 8.

When we saw from Fig. 20, the concentration of a given substance diffuses to the right

boundary of the given domain and as time increases, the concentration moves collectively to

the right. Let we change the velocity term and the diffusion coefficient for the given source

term using D = 0.02 and a = 1, (g0, g1) = (0.02,0.06). The solution for this conditions is given in

Fig. 21(a) using a total time of t f = 1 and 21(b) using a total time of t f = 3. This is an advection

dominated example in which the movement of the concentration is faster in advection terms

relative to diffusion. If we change the velocity term and the diffusion coefficient for the given

source term using D = 1 and a = 1, (g0, g1) = (1,3). The solution for this conditions is given in

Fig. 22(a) using a total time of t f = 1 and 22(b) using a total time of t f = 3. This conditions

are examples of the balacing advection and diffusion terms in which the concentration of the

substance moves in a balancing advection and diffusion terms.

53

Page 68: Numerical Solutions of Advection Diffusion Equations Using ...

(a) FEM solution with t f = 1 (b) FEM solution with t f = 3

Figure 20: Numerical solution using FEM for the advection diffusion equation with f (x, t ) =a(2x + 1)− 2D − 1 and robin boundary conditions, with a diffusion coefficient D = 20 and

velocity term a = 0.1 with time t f = 1 for (a) and t f = 3 for (b).

(a) FEM solution with t f = 1 (b) FEM solution with t f = 3

Figure 21: Numerical solution using FEM for the advection diffusion equation with f (x, t ) =a(2x +1)−2D −1 and robin boundary conditions, with a diffusion coefficient D = 0.02 and

velocity term a = 1 with time t f = 1 for (a) and t f = 3 for (b).

54

Page 69: Numerical Solutions of Advection Diffusion Equations Using ...

(a) FEM solution with t f = 1 (b) FEM solution with t f = 3

Figure 22: Numerical solution using FEM for the advection diffusion equation with f (x, t ) =a(2x +1)−2D −1 and robin boundary conditions, with a diffusion coefficient D = 0.02 and

velocity term a = 1 with time t f = 1 for (a) and t f = 3 for (b) in the descritization of time.

Let we consider the 2D steady diffusion equation with a source function given as f (x, y) =Dπ2(sin(πx)+ sin(πy)) to implement the 2D problem. The FEM numerical simulation of the

PDE is given in Fig. 23 and the Matlab algorithm is given in appendix 4 with only some modi-

fications to include the diffusion term.

If we consider another example with a source of f (x, y) = Dπ2 sin(πx)sin(πy), then the finite

element solution using Matlab is given in Fig. 24.

In order to consider the finite element method of the time dependent two dimensional

equations, we use the unsteady 2D diffusion equation. let we test it with a known exact func-

tion and use the source terms and boundary conditions from that function. Let u(t , x, y) =e−t sin(πx)sin(πy) be the given exact solution for the diffusion equation ut −D∆u = f , then

our source function f becomes f (t , x, y) = (2Dπ2 − 1)e−t sin(πx)sin(πy). Now by using the

ode15i, ode solver to integrate for time in the final finite element method descritization the

solutions for the equation is given in Fig. 25. The surface plots of those figures are the exact

solutions, the finite element method solutions and the color bar of the finite element solution

to view its properties. The full matlab code is given in appendix 9.

If we consider another linear test example with exact solution u(x, y, t ) = x + y + t , then its

source function becomes f (x, y, t ) = 1. The finite element solution for this 2D unsteady diffu-

55

Page 70: Numerical Solutions of Advection Diffusion Equations Using ...

(a) FEM solution (b) Bar plot of the solution

Figure 23: The numerical simulation using FEM for the steady 2D diffusion equation with

f (x, y) = Dπ2(sin(πx)+ sin(πy)), with a diffusion coefficient D = 0.5.

(a) FEM solution (b) Bar plot of the solution

Figure 24: The numerical simulation using FEM for the steady 2D diffusion equation with

f (x, y) = Dπ2(sin(πx)sin(πy)), and a diffusion coefficient D = 0.5.

56

Page 71: Numerical Solutions of Advection Diffusion Equations Using ...

(a) FEM & exact sol (b) Colour bar of the FEM solution

Figure 25: The surface of the time dependet 2D diffusion equation with D = 100 with its color

bar plot for its source term f , f (t , x, y) = (2Dπ2 −1)e−t sin(πx)sin(πy).

sion equation with its exact solution and color bar is given in Fig. 26 by ode solver ode15i.

(a) FEM & exact sol (b) Colour bar of the FEM solution

Figure 26: The surface of the time dependet 2D diffusion equation with D = 1 with its color

bar plot for its source term f , f (t , x, y) = 1.

The numerical results in all the above mentioned discussions using the finite element method

are almost closed to the analytical solution in the case of the test examples. This shows us that

the finite element method is a best numerical method to solve any differential equations nu-

merically in any type of geometries.

57

Page 72: Numerical Solutions of Advection Diffusion Equations Using ...

6 Conclusion and Recommendation

We would present a summary of our thesis work in this section and some of the recommen-

dations to provide suggested areas for further studies. We also discuss some of the limitations

that we had during the study of the thesis.

6.1 Conclusion

In this thesis, we studied the finite element solution of the advection-diffusion equation. In

finite element analysis we approximated a function defined in a domain, Ω, with a set of or-

thogonal basis functions with coefficients corresponding to the functional values at some

node points. We dead the numerical simulation of advection-diffusion equation using the

FEM scheme in space and solved the system of ode’s using back ward Euler method in time.

The main focus of the thesis has been on the varational formulation techniques for the so-

lution of the discrete Galerkin method and on the other hand on computational analysis of

different one and two dimensional PDE’s. We have done numerical simulations for the above

mentioned equations by considering the technique and using different test examples. The

solution for the values at the nodes for the partial differential equations has been obtained by

solving a linear system of equations involving the inversion of the sparse matrices.

We have solved the advection-diffusion equation for both one dimensional and two di-

mensional cases with the weighted residual (Galerkin) method of finite elements with con-

stant velocity term and diffusion coefficient. These statements are supported by our test nu-

merical investigations for the 1D and 2D Poisson equation, the advection equation, diffusion

equation and the advection-diffusion equation.

We have also seen the simulations of the equations by using the language of technical

computing called Matlab and for time dependent equation using back ward (implicit) Euler

finite difference method for 1D problems and the built in function ode15i to solve the system

of ode’s for two dimensional equations.

6.2 Recommendation

The numerical solution of the one and two dimensional advection-diffusion equations has

been presented in this work. The main directions for the future works is listed here.

58

Page 73: Numerical Solutions of Advection Diffusion Equations Using ...

The generalization of the proposed Galerkin FEM to the three-dimensional advection dif-

fusion is obtained with in the current frame work and an interested body will done on this

approach by including the reaction term. In order to obtain the solutions of this problems,

the method can be extended to least square finite element method, and another interested

body will also done on this approach.

Here we consider using constant parameters, i.e., constant velocity terms and diffusion

coefficients. One can do this method using variable velocities and diffusion coefficients which

varies with time and space of the given dimension. The simulation can also be done exactly if

the exact values of the parameters for the pollutants are obtained from some polluting bodies

by including the reaction term of the pollutant in the AD equation.

The finite element method descritizes only in space and the time descritization is based

on the finite difference methods. Hence the finite element method needs a human power still

now for the descritization process of time. So this is a very interesting area to do researches

on the future to include the time descritization also in the method.

6.3 Limitation of the study

The limited access of the data for a given concentration of a pollution substance with nec-

essary parameters, such as the velocity field, diffusion coefficients and the source and sink

terms, the accuracy of our model did not test via real data and the results are shown only with

artificial data. Hence, the velocity field and diffusion coefficients are in general not very ac-

curate. Therefore the accuracy requirements for the numerical solution are also low. It is the

limitation in order to apply and recommend the model with real life applications.

59

Page 74: Numerical Solutions of Advection Diffusion Equations Using ...

References

[1] Alfio Quorteroni,Numerical Models for Differential Problems,Volume 2, c Springer-Verlag

Italia, Milan, 2009.

[2] A. R. Appadu, Numerical Solution of the 1D Advection-Diffusion Equation Using Standard

and Non Standard Finite Difference Schemes, South Africa, 2013.

[3] Asan Ali Akbar Fatah BAJELLAN, Computation of the convection diffusion equation by the

Fourth order Compact Finite Difference method, January, 2015.

[4] Biyue Liu, Myron B. Allen, Hristo Kojouharov, Benito Chen, Finite-Element Solution of

Reaction-Diffusion Equations with Advection, 2009.

[5] Brynjulf Owren, Lecture Note on TMA4212, Numerical solution of partial differential equa-

tions with finite difference methods, 2013.

[6] Busayamas Pimpunchat, Winston L. Sweatman, Graeme C. Wake, Wannapong Triampo,

Aroon Parshotam, A mathematical model for pollution in a river and its remediation by

aeration, Applied Mathematics Letters, 2009.

[7] Deb, Manas K and Babu, Solution of stochastic partial differential equations using

Galerkin finite element techniques, Computer Methods in Applied Mechanics and Engi-

neering, volume 190, Elsevier, 2001.

[8] Donea, Jean and Huerta, Antonio, Finite element methods for flow problems, 2003.

[9] F. Index, E. G. E. Standard, and F.-i. Tariff., Water pollution, Wikipedia: the free encyclope-

dia, Retrieved on April, 25, 2011.

[10] Geza Jolankai, Basic river water quality models, Technical Documents in Hydrology, UN-

ESCO, Paris, 1997.

[11] Gino Ignacio Montecino Guzman, Numerical methods for advection-diffusion-reaction

equations and medical applications, 2014.

[12] Johnson, Claes, Numerical solution of partial differential equations by the finite element

method, Courier Corporation, 2012.

60

Page 75: Numerical Solutions of Advection Diffusion Equations Using ...

[13] Langtangen, HP, Computational partial differential equations: Numerical methods and

Diffpack Programming, Springer Verlag, Second edition, Volume 1, 1999.

[14] Li, Zhilin and Ito, Kazufumi, The immersed interface method: Numerical solutions of

PDEs involving interfaces and irregular domains, Volume 33, Siam, 2006.

[15] Mahm UD Ahsan, Numerical solution of the advection-diffusion equation using Laplace

transform finite analytical method, Halcrow Group Limited, Birchwood Warrington, UK,

2016.

[16] Martin Stampfle, Mathematical Methods, January 30, 2013.

[17] Mats G. Larson, Fredrik Bengzon, The Finite Element Method: Theory, Implementation,

and Practice, Springer, November 9, 2010.

[18] Morton, Keith W and Mayers, David Francis, Numerical solution of partial differential

equations: an introduction, Cambridge university press, 2005.

[19] Nopparat Pochai, A Numerical Treatment of Non dimensional Form of Water Quality

Model in a Nonuniform Flow Stream Using Saulyev Scheme, Hindawi Publishing Corpora-

tion, 2011.

[20] Nopparat Pocha, Suwon Tangmanee, L.J. Crane, and J.J.H. Miller, A Mathematical model

of pollution water control using finite element method, WILEY-VCH Verlag GmbH , Co.

KGaA, Weinheim, 2006.

[21] Nopparat Pochaia, Rujira Deepan, A Numerical Computation of Water Quality Measure-

ment in a Uniform Channel Using a Finite Difference Method, 2011.

[22] Professor Aitor Begara, Finite Difference Numerical methods of Partial Differential equa-

tions in Finance with Matlab, 2013.

[23] R.L. Knight, V.W.E. Payne Jr., R.E. Borer, R.A. Clarke Jr., J.H. Pries, Constructed wetlands

for livestock waste water treatment, Ecol. Engin, 2000.

[24] Radwan, Mona and Willems, P and El-Sadek, A and Berlamont, Jean, Modelling of dis-

solved oxygen and biochemical oxygen demand in river water using a detailed and a sim-

plified model, International Journal of River Basin Management, volume 1, Taylor & Fran-

cis, 2003.

61

Page 76: Numerical Solutions of Advection Diffusion Equations Using ...

[25] Richard L. Burden and J. Douglas Faires, Numerical Analysis, Ninth Edition, 2011.

[26] Runkel, Robert L, Solution of the advection-dispersion equation: continuous load of finite

duration, Journal of Environmental Engineering, volume 122, American Society of Civil

Engineers, 1996.

[27] S.A Mirbagherie, M.Abaspour, K.H Zamani, Mathematical modeling of water quality in

river system, case study in Jajrood river in Tehran, Iran, E.W publications, 2009.

[28] Susanne C. Brenner L. Ridgway Scott,The Mathematical Theory of Finite Element Meth-

ods,Third Edition, Springer Science& Business Media, LLC, USA, New york, 2008.

[29] United States Environmental Protection Agency (EPA), Washington, DC. The National

Water Quality Inventory: Report to Congress for the 2002 Reporting Cycle - A Profile, Octo-

ber 2007.

[30] Wachman, Richard,Water becomes the new oil as world runs dry, the Guardian (London),

Retrieved 2015.

[31] Willem Hundsdorfer CWI, Numerical Solution of Advection-Diffusion-Reaction Equa-

tions, Amesterdam, 1996.

[32] Yang, Won Y and Cao, Wenwu and Chung, Tae-Sang and Morris, John,Applied numerical

methods using MATLAB, John Wiley & Sons, 2005.

62

Page 77: Numerical Solutions of Advection Diffusion Equations Using ...

7 Appendix

Appendix 1: Matlab code for 1D equation using FEM

%%% Matlab code for the equation -U_xx=2; 0<x<1; U(0)=U(1)=0 %%%%%%

U=@(x)x-x^2;%Exact solution

a=0;%left boundary

b=1;%right boundary

N=10;% Number of nodes

h=(b-a)/N;% mesh width

f=@(x)2;

x=zeros(N+1,1);

x(1)=a;

for i=2:N+1

x(i)=a+(i-1)*h;

end

n=20;%number of intervals for each element

intf=zeros(N-1,1);

%%%%% to find load vector( integration of f for each interval) by

%the build in function int%%%%%

% syms k

% f=12*k.^2;

% for t=1:N-1

% intf(t)=int(f,k,h*(t-1),h*(t));

% end

%%%%% to find load vector( integration of f for each interval) by

%using composite simpsons rule%%%%%

for m=1:N-1

xi0=f((m-1)*h)+f((m)*h);%functional value at nodal points

xi1=0;xi2=0;

t=(m*h-((m-1)*h))/n;%size of the descritization in each element

xi=zeros(n+1,1);

63

Page 78: Numerical Solutions of Advection Diffusion Equations Using ...

xi(1)=(m-1)*h;

for j=2:n+1

xi(j)=(m-1)*h+(j-1)*t;%discritization of each element

end

for j=2:2:n

xi1=xi1+f(xi(j));%sum of odd node functional values

end

for k=3:2:n-1

xi2=xi2+f(xi(k));%sum of even node functional values

end

intf(m)=t/3*(xi0+2*xi2+4*xi1);%composite simpson rule

end

%A=diag(2/h*ones(N-1,1),0)+diag(-1/h*ones(N-2,1),-1)+diag(-1/h*ones(N-2,1),1);

A=(1/h)*full(gallery('tridiag',N-1,-1,2,-1));%load matrix with drichlet BC

Uhi=zeros(N,1);% A vector to store approximate solutions

Uhi(1,1) = 0;

Uhi(N+1,1)= 0;

Uhi(2:N,1)=A\intf;%FE solution

plot(x,U(x),'--g',x,Uhi,'r')%plot of exact solution with FE solution

legend('Exact solution','FEM solution')

Matlab code for Composite simpson rule

%To approximate the integral f (x) dx from a to b using composite simpsons rule%%%

a=0;b=4;N=10;

h=(b-a)/N;

f=@(x)sin(x);

x=zeros(N,1);

xi0=f(a)+f(b);

xi1=0;

xi2=0;

n=1:N+1;

64

Page 79: Numerical Solutions of Advection Diffusion Equations Using ...

x(1)=a;

for i=2:N+1

x(i)=a+(i-1)*h;

end

for i=2:2:N

xi1=xi1+f(x(i));

end

for j=3:2:N-1

xi2=xi2+f(x(j));

end

intf=h/3*(xi0+2*xi2+4*xi1)

Appendix 2: The functions to solve the Mass matrix, Stiffness matrix and

Load vector in 1D

%%%%%%A function to solve mass matrix%%%%%%%

function M = MassMat1D(x)

n = length(x)-1; % number of subintervals

M = zeros(n+1,n+1); % allocate mass matrix

for i = 1:n % loop over subintervals

h = x(i+1) - x(i); % interval length

M(i,i) = M(i,i) + h/3; % add h/3 to M(i,i)

M(i,i+1) = M(i,i+1) + h/6;

M(i+1,i) = M(i+1,i) + h/6;

M(i+1,i+1) = M(i+1,i+1) + h/3;

end

%%%%%%% A function to solve the stiffness matrix%%%%%

function A = StiffMat1D(x)

n = length(x)-1;

A = zeros(n+1,n+1);

for i = 1:n

h = x(i+1) - x(i);

65

Page 80: Numerical Solutions of Advection Diffusion Equations Using ...

A(i,i) = A(i,i) + 1/h; % add 1/h to A(i,i)

A(i,i+1) = A(i,i+1) - 1/h;

A(i+1,i) = A(i+1,i) - 1/h;

A(i+1,i+1) = A(i+1,i+1) + 1/h;

end

end

%%%%%%%% A function to solve the load vector%%%%%%%

function C = LoadVec1Dt(x,f,t)

n = length(x)-1;

C = zeros(n+1,1);

for i = 1:n

h = x(i+1) - x(i);

C(i) = C(i) + f(x(i),t)*h/2;

C(i+1) = C(i+1) + f(x(i+1),t)*h/2;

end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Appendix 3: Matlab function for 1D time dependent equation using FEM

%%%%%% %%%%% A script in which we will set the number of grid points, the

initial values and specify for which values of t we want output.%%%%%%

%%% Matlab code for the equation U_t-U_xx=sin(pi*x)+pi^2*t*sin(pi*x); ...

%0<x<1; U(0,t)=0,U(1,t)=t and U(0,x)=sin(pi x) %%%%%%

%%%exact solution U(x,t)=tsin(pi*x) and using the mass matrix and load

%%%%%% vector in appendex 2%%%%%%

%function Heat1D()

h = 0.01; % mesh size

x = 0:h:1; % mesh

N = length(x);

U = 0*x(2:N-1)';%initial value

L = 10;

TT =0.5;

66

Page 81: Numerical Solutions of Advection Diffusion Equations Using ...

t = linspace(0,TT,L+1); % time grid

k = t(2)-t(1);

[X,T]=meshgrid(x,t);

uexa = @(x,t)t.*sin(pi*x);

f=@(x,t)sin(pi*x)*(1+pi^2*t);

A=(1/h)*full(gallery('tridiag',N-2,-1,2,-1));%stiffness matri

M = MassMat1D(x); % mass matrix

M= M(2:N-1,2:N-1);

uu = zeros(L+1,N);

for i = 1:L % time loop

b = LoadVec1Dt(x,f,t(i+1)); % load vector

b = b(2:N-1,:);

% k = t(i+1) - t(i); % for different time step;

U = (M + k*A)\(M*U + k*b); % backward Euler method

plot(x,[0;U;0]), axis([0 3 0 3]), pause(0.1) % plot

uu(i+1,:) = [0;U;0];

end

figure(1)

%subplot(1,2,1)

surf(X,T,uu)

title('FEM plot')

xlabel('x')% the name of the x axis

ylabel('t')% name of y axis

zlabel('u')% name of z axis

%subplot(1,2,2)

figure(2)

surf(X,T,uex(X,T))

title('Exact solution plot')

xlabel('x')% the name of the x axis

ylabel('t')% name of y axis

zlabel('u')% name of z axis

figure(3)

67

Page 82: Numerical Solutions of Advection Diffusion Equations Using ...

surf(X,T,uu-uex(X,T))

Appendix 4: Matlab code for the 2D poisson equation

%%%% A function to triangulate the domain and know the elemnts%%%%%

function [p t edge] = getSquare(N)

% Grid on a square

% Generate the points.

x = linspace(0,1,N)';

e = ones(N,1);

%coordinates of x and y on the mesh

p = zeros(N2,2);

p(:,1) = kron(x,e); %xmultiply x with e

p(:,2) =kron(e,x);

% Generate the elements.

t = [];

for i=1:N-1

for j=1:N-1

k = N*(j-1)+i;

t = [t; k,k+1,k+N;k+N,k+N+1,k+1];

end

end

% Generate the edges

edge = [];

N0 = N*(N-1);

for i=1:N-1

es = [i,i+1];

en = [N0+i, N0+i+1];

ew = [N*(i-1)+1,N*i+1];

ee = [N*i, N*(i+1)];

edge = [edge;es;en;ew;ee];

end

68

Page 83: Numerical Solutions of Advection Diffusion Equations Using ...

figure(1)%triangulation of the domain

triplot(t,p(:,1),p(:,2))

%%%%%%%%%%%%%%%%%%%%%

uex = @(x,y) y.^2+y.*sin(x);

f= @(x,y) -2+y*sin(x);

N=10;

[p t edge] = getSquare(N);

M=size(p,1);%total number of elements

T=size(t,1); %total number of nodes

%to label number for the elements

for k = 1:T

nodes=t(k,:);

cpt = 1/3*sum(p(nodes,:)); % centroid of the element

text(cpt(1),cpt(2),num2str(k),'color','b')

end

% to lable number for the node

for nodes=1:M

c = p(nodes,1:2);

text(c(1)+0.01,c(2)+0.01,num2str(nodes),'color','r')

end

A=sparse(M,M);%memory for stiffness matrix

F=zeros(M,1);

r=zeros(M,1);

iE = [2:N];%boundaries in east

iW = [(N-1)*N+2:N^2]; % boundaries in west

iS = [1:N:N^2]; %boundaries in south

iN = iS(2:end-1)+N-1;%boundaries in north

Drech = [iE iS iW iN];

for e=1:T

nodes=t(e,:);% row of t = node numbers of the 3 corners of triangle e

Pa=[ones(3,1),p(nodes,:)];% 3 by 3 matrix with rows=[1 xcorner ycorner]

Area=abs(det(Pa))/2;% area of triangle e = half of parallelogram area

69

Page 84: Numerical Solutions of Advection Diffusion Equations Using ...

qpt = 1/3*sum(p(nodes,:));%store the sum of each column, for the column

C=inv(Pa);

grad=C(2:3,:);

Ah=Area*grad'*grad;

g = feval(f,qpt(1),qpt(2));%g=f(qpt(1),qpt(2))

Fe=(Area/3)*g; %corner quadrature formula

A(nodes,nodes)=A(nodes,nodes)+Ah;% add Ah to 9 entries of global A

%A(nodes,nodes)=the values of A at each nodal point

F(nodes)=F(nodes)+Fe;% add Fe to 3 components of load vector F

end

%Implement Dirichlet boundary conditions

for i=1:length(Drech)

enodes=Drech(i);

pp=p(enodes,:);

F(:,1) = F(:,1)-A(:,Drech(i)).*uex(pp(1,1),pp(1,2));

A(:,Drech(i))=0;

A(Drech(i),:)=0;

F(Drech(i))=uex(pp(1,1),pp(1,2));

end

A(Drech,Drech)=speye(length(Drech),length(Drech));

Ab=A;

Fb=F;

U=Ab\Fb;

u=reshape(U,N,N);

x1= reshape(p(:,1),N,N);

y1= reshape(p(:,2),N,N);

figure(2)

surf(x1,y1,u)

title('Numerical solution')

figure(3)

surf(x1,y1,uex(x1,y1))

title('Analytical solution')

70

Page 85: Numerical Solutions of Advection Diffusion Equations Using ...

figure(4)

err = (u-uex(x1,y1));

mesh(x1,y1,err)

title('error')

Appendix 5: Matlab script for 1D advection equation using FEM

%%%%% Solving the advection dominated equation using the mass matrix and

%%%%%% load vector in appendex 2%%%%%%%

function Advection1D()

%U_t+a*U_x=f, U(0,t)=U(1,t)=0,U(x,0)=U0;

h = 0.01; % mesh size

x = 0:h:1; % mesh

N = length(x);

U = 0*x(2:N-1)';%initial value

L = 10;

TT = 1;

t = linspace(0,TT,L+1); % time grid

k = t(2)-t(1);

[X,T]=meshgrid(x,t);

a=3;

uex = @(x,t)t.*cos(pi*x).*(x-x.^2);

f= @(x,t)(cos(pi*x)-a*pi*t.*sin(pi*x)).*...

(x-x.^2)+a*t.*cos(pi*x).*(1-2*x);;

A=full(gallery('tridiag',N-2,-1/2,0,1/2));

%The matrix,A_ij=\int\phi_j'\phi_idx

M = MassMat1D(x); % mass matrix

M= M(2:N-1,2:N-1);

% b = LoadVec1Dt(x,f,t); % load vector

% b = b(2:N-1,:);

uu = zeros(L+1,N);

for i = 1:L % time loop

71

Page 86: Numerical Solutions of Advection Diffusion Equations Using ...

b = LoadVec1Dt(x,f,t(i+1)); % load vector

b = b(2:N-1,:);

% k = t(i+1) - t(i); % for different time step;

U = (M + a*k*A)\(M*U + k*b); % backward Euler method

plot(x,[0;U;0]), axis([0 1 0 1]), pause(0.1) % plot

uu(i+1,:) = [0;U;0];

end

figure(1)

surf(X,T,uu)

title('FEM plot')

xlabel('x')% the name of the x axis

ylabel('t')% name of y axis

zlabel('u')% name of z axis

figure(2)

surf(X,T,uex(X,T))

title('Exact solution plot')

xlabel('x')% the name of the x axis

ylabel('t')% name of y axis

zlabel('u')% name of z axis

figure(3)

surf(X,T,uu-uex(X,T))

Appendix 6: Matlab code for 1D diffusion equation

%%%%% Solving diffusion dominated equation with mass matrix and

%%%% load vector in appendix 2 %%%%%%%%

function Diffusion1D()

%U_t-D*U_xx=f, U(0)=U(1)=0;

h = 0.01; % mesh size

x = 0:h:1; % mesh

N = length(x);

U = 0*x(2:N-1)';%initial value

72

Page 87: Numerical Solutions of Advection Diffusion Equations Using ...

L = 10;

TT = 1;

t = linspace(0,TT,L+1); % time grid

k = t(2)-t(1);

[X,T]=meshgrid(x,t);

D=300;

uex = @(x,t) t.*exp(-t).*sin(pi*x);

f= @(x,t)exp(-t).*sin(pi*x)*(1-t+D*t*pi^2);

A=(1/h)*full(gallery('tridiag',N-2,-1,2,-1));%stiffness matrix

M = MassMat1D(x); % mass matrix

M= M(2:N-1,2:N-1);

uu = zeros(L+1,N);

for i = 1:L % time loop

b = LoadVec1Dt(x,f,t(i+1)); % load vector

b = b(2:N-1,:);

% k = t(i+1) - t(i); % for different time step;

U = (M + D*k*A)\(M*U + k*b); % backward Euler method

plot(x,[0;U;0]), axis([0 1 0 1]), pause(0.1) % plot

uu(i+1,:) = [0;U;0];

end

figure(1)

surf(X,T,uu)

title('FEM plot')

xlabel('x')% the name of the x axis

ylabel('t')% name of y axis

zlabel('u')% name of z axis

figure(2)

surf(X,T,uu-uex(X,T))

73

Page 88: Numerical Solutions of Advection Diffusion Equations Using ...

Appendix 7: Matlab code for 1D AD equation with homogeneous Dirichlet

boundary conditions

function ADR1D()

%U_t+a*U_x-D*U_xx=f, U(0)=U(1)=0;

h = 0.01; % mesh size

x = 0:h:1; % mesh

N = length(x);

U = 0*x(2:N-1)';%initial value

L = 10;

TT = 3;

t = linspace(0,TT,L+1); % time grid

k = t(2)-t(1);

[X,T]=meshgrid(x,t);

D=10;a=0.03;%a=10;D=0.03;

f= @(x,t)sin(pi*x)*(1+D*pi^2*t)+a*pi*t*cos(pi*x);

K=(1/h)*full(gallery('tridiag',N-2,-1,2,-1));%stiffness matrix

A=full(gallery('tridiag',N-2,-1/2,0,1/2));%matrix ,A_ij=\int\phi_j'\phi_i

M = MassMat1D(x); % mass matrix

M= M(2:N-1,2:N-1);

uu = zeros(L+1,N);

for i = 1:L % time loop

b = LoadVec1Dt(x,f,t(i+1)); % load vector

b = b(2:N-1,:);

% k = t(i+1) - t(i); % for different time step;

U = (M +(k*a*A+D*k*K))\(M*U + k*b); % backward Euler method

plot(x,[0;U;0]), axis([0 1 0 1]), pause(0.1) % plot

uu(i+1,:) = [0;U;0];

end

surf(X,T,uu)

title('FEM plot')

xlabel('x')% the name of the x axis

74

Page 89: Numerical Solutions of Advection Diffusion Equations Using ...

ylabel('t')% name of y axis

zlabel('u')% name of z axis

Appendix 8: Matlab code for 1D AD equation with general boundary condi-

tions

%%%%%%function solve for load vector%%%%%%%%%

function b = LoadVec1Drobin(x,f,kappa,g,aa)

n = length(x)-1;

b = zeros(n+1,1);

for i = 1:n

h = x(i+1) - x(i);

b(i) = b(i) + f(x(i)) *h/2;

b(i+1) = b(i+1) + f(x(i+1)) *h/2;

end

b(1) = b(1) + kappa(1) *g(1)/aa(1);

b(n+1) = b(n+1) + kappa(2) *g(2)/aa(2);

%%%%%%function for stiffness matrix%%%%%

function A = StiffMat1Drobin(x,a,kappa,aa)

n = length(x)-1;

A = zeros(n+1,n+1);

for i = 1:n

h = x(i+1) - x(i);

xmid = (x(i+1) + x(i))/2; % interval mid-point

amid = a(xmid); % value of a(x) at mid-point

A(i,i) = A(i,i) + amid/h; % add amid/h to A(i,i)

A(i,i+1) = A(i,i+1) - amid/h;

A(i+1,i) = A(i+1,i) - amid/h;

A(i+1,i+1) = A(i+1,i+1) + amid/h;

end

A(1,1) = A(1,1) + kappa(1)/aa(1);

A(n+1,n+1) = A(n+1,n+1) + kappa(2)/aa(2);

75

Page 90: Numerical Solutions of Advection Diffusion Equations Using ...

%%%%%%%function for mass matrix%%%%%%%%

function M = MassMat1Drobin(x)

n = length(x)-1; % number of subintervals

M = zeros(n+1,n+1); % allocate mass matrix

for i = 1:n % loop over subintervals

h = x(i+1) - x(i); % interval length

M(i,i) = M(i,i) + h/3; % add h/3 to M(i,i)

M(i,i+1) = M(i,i+1) + h/6;

M(i+1,i) = M(i+1,i) + h/6;

M(i+1,i+1) = M(i+1,i+1) + h/3;

end

%%%%%%%then solve the ADE with general boundary condition%%%%%

function AdvectionDiffusion1Drobin()

h = 0.02; % mesh size

x = 0:h:1; % mesh

v=10;%D=1;

N=length(x);

L = 10; % number of time levels

T = 10; % final time

t = linspace(0,T,L+1) % time grid

U = 0.5-abs(0.5-x)'; % inital condition

U0 = 0.5-abs(0.5-x)'; % inital condition

kappa =[1 1];% [1.e+6 1.e+6]; % Robin BC parameters

aa=[0.1 1];%coefficients of derivatives for robin boundary conditions

g = [-1 1];

A = StiffMat1Drobin(x,@diff,kappa,aa); % stiffness matrix

M = MassMat1Drobin(x); % mass matrix

BB=full(gallery('tridiag',N-2,-1/2,0,1/2));%matrix ,A_ij=\int\phi_j'\phi_i

B=sparse(N,N);

B(1,1)=1/h;

B(N,N)=1/h;

B(2:N-1,2:N-1)=BB;

76

Page 91: Numerical Solutions of Advection Diffusion Equations Using ...

b = LoadVec1Drobin(x,@souf,kappa,g,aa); % load vector

uu = zeros(L+1,N);

uu(1,:)=U0;

for l = 1:L % time loop

k = t(l+1) - t(l); % time step;

%U = (M + k*A)\(M*U + k*b); % backward Euler method Diff

U = (M +(k*v*B+k*A))\(M*U + k*b); % backward Euler method AdvDiff

plot(x,U), axis([0 1 0 1]), pause(0.1) % plot

uu(l+1,:) = U;

end

figure(2)

%subplot(1,2,1)

[X,T]=meshgrid(x,t);

surf(X,T,uu)

title('FEM plot')

xlabel('x')% the name of the x axis

ylabel('t')% name of y axis

zlabel('u')% name of z axis

function y = diff(x)%diffusion coefficient

y = 0.1; % coefficient a=1

function y = souf(x)%source function

y = 2*x; % function f=2x

%%%%%%

Appendix 9: Matlab code for time dependent 2D diffusion equation

function out = diffu(tt,u,up)

N=sqrt(length(u));

d=1;

uex = @(x,y,tt) exp(-tt).*sin(pi*x).*sin(pi*y);%x+y+tt;% exact solution

f= @(x,y,tt)(2*d*pi^2-1)*exp(-tt).*sin(pi*x).*sin(pi*y);%1;f=u_t-d(u_xx+u_yy)

[p t edge] = getSquare(N);

77

Page 92: Numerical Solutions of Advection Diffusion Equations Using ...

M=size(p,1);%total number of elements

T=size(t,1); %total number of nodes

A=sparse(M,M);%memory for stiffness matrix

R=sparse(M,M);%memory for mass matrix

F=zeros(M,1);%memory for load vector

r=zeros(M,1);

iE = [2:N];%boundaries in east

iW = [(N-1)*N+2:N^2]; % boundaries in west

iS = [1:N:N^2]; %boundaries in south

iN = iS(2:end-1)+N-1;%boundaries in north

Drech = [iE iS iW iN];

%U= reshape(u0,N^2,1);

for e=1:T

nodes=t(e,:);% row of t = node numbers of the 3 corners of triangle e

Pa=[ones(3,1),p(nodes,:)];% 3 by 3 matrix with rows=[1 xcorner ycorner]

Area=abs(det(Pa))/2;% area of triangle e = half of parallelogram area

qpt = 1/3*sum(p(nodes,:));%store the sum of each column, for the column

C=inv(Pa);

grad=C(2:3,:);

Ah=Area*grad'*grad;

g = feval(f,qpt(1),qpt(2),tt);%g=f(qpt(1),qpt(2))

Fe=(Area/3)*g; %corner quadrature formula

A(nodes,nodes)=A(nodes,nodes)+Ah;% add Ah to 9 entries of global A

Rh = [2 1 1;

1 2 1;

1 1 2]*Area/12;

R(nodes,nodes)=R(nodes,nodes)+Rh;% add Rh to 9 entries of global R

F(nodes)=F(nodes)+Fe;% add Fe to 3 components of load vector F

%Implement Dirichlet boundary conditions

end

78

Page 93: Numerical Solutions of Advection Diffusion Equations Using ...

for i=1:length(Drech)

enodes=Drech(i);

pp=p(enodes,:);

F(:,1) = F(:,1)-A(:,Drech(i)).*uex(pp(1,1),pp(1,2),tt);

A(:,Drech(i))=0;

A(Drech(i),:)=0;

F(Drech(i))=uex(pp(1,1),pp(1,2),tt);

end

A(Drech,Drech)=speye(length(Drech),length(Drech));

R(Drech,:)=0;R(:,Drech)=0;

out=R*up+d*A*u-F;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

N=7;

uex = @(x,y,tt) exp(-tt).*sin(pi*x).*sin(pi*y);%x+y+tt;%

[p t edge] = getSquare(N);

uin = uex(p(:,1),p(:,2),0);

yp0=zeros(N^2,1);%ones(N^2,1);

y0=uin;

tspan=[0 0.5];

[tt,u] = ode15i(@diffu,tspan,y0,yp0);

x1= reshape(p(:,1),N,N);

y1= reshape(p(:,2),N,N);

for i = 1:length(tt)

Un = u(i,:);

un = reshape(Un,N,N);

subplot(1,2,1)

surf(x1,y1,un)

xlabel('x')

ylabel('y')

zlabel('u')

legend('FEM')

subplot(1,2,2)

79

Page 94: Numerical Solutions of Advection Diffusion Equations Using ...

surf(x1,y1,uex(x1,y1,tt(i)))

xlabel('x')

ylabel('y')

zlabel('u')

legend('Exact')

pause(.1)

figure(2)

trisurf(t,p(:,1),p(:,2),0*p(:,1),un,'edgecolor','k','facecolor','interp');

view(2),axis equal,colorbar

end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

80

Page 95: Numerical Solutions of Advection Diffusion Equations Using ...

8 Biography

The author was born in Ethiopia, Amhara region, AWI administrative Zone from his fa-

ther Mr. Getnet Mekonen and his mother Alganesh Alamneh on April 21, 1993. He attend his

primary school from grade 1-6 at Assem primary school starting from September, 1999. He

then finished his primary school from grades 7-8 at Dangia general primary school. He attend

both his secondary and preparatory programs at Enjibara General Secondary and preparatory

school and he taken his University entrance exam in 2011. After he finished the prepara-

tory school successfully he joined Hawassa University by the Faculty of Natural and compu-

tational science, Mathematics department in November, 2011. He received his Bachelor of

Science Degree in Mathematics from the University in July, 2014. After he graduate with his

BSc. degree, he was also joined directly to Mathematical and Statistical Modeling (MASTMO)

program at Hawassa University to proceed his Masters program in October, 2014 with the

sponsor ship of HU- PhD -Math-Stat-Science project, and expected to graduate in July, 2016.

81