You are on page 1of 64

A Lid-driven Cavity Flow

December 19, 2006


Navier Stokes
s021893 Peter Bk & s021936 Jakob Borbye
41319 - Computational Fluid Dynamics E06
Contents
1. Background and Problem Description 4
2. Governing equations 5
2.1. Deriving an Appropriate Governing Equation . . . . . . . . . . . . . . . . 5
2.2. Boundary Conditions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
3. Discretization and Implementation 9
3.0.1. Cases for Approximation of Spatial Derivatives . . . . . . . . . . . 10
3.1. Determination of H
x
and H
y
. . . . . . . . . . . . . . . . . . . . . . . . . 14
3.2. Solving the Poisson Equation . . . . . . . . . . . . . . . . . . . . . . . . . 14
3.3. Derivation of Schemes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
3.3.1. The inner Scheme . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
3.3.2. The Boundary Scheme . . . . . . . . . . . . . . . . . . . . . . . . . 15
3.4. Solving algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
3.5. Time Stepping Procedure . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
3.6. Obtaining a Steady-State Solution . . . . . . . . . . . . . . . . . . . . . . 18
4. Validation 18
5. Results 24
5.1. Checking for Conservation . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
6. Conclusion 32
A. Matlab Scripts and Functions 33
A.1. Main Script for the Creeping Flow Problem . . . . . . . . . . . . . . . . . 33
A.2. Main Script for Re = 1000. . . . . . . . . . . . . . . . . . . . . . . . . . . 34
A.3. Main Script for Re = 1000, benchmarking. . . . . . . . . . . . . . . . . . . 35
A.4. Grid Generation Function . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
A.5. Boundary Condition Imposing Function . . . . . . . . . . . . . . . . . . . 40
A.6. Determine Partial Velocity Derivatives . . . . . . . . . . . . . . . . . . . . 41
A.7. General Midpoint Rule Function for Full-grid Spacing . . . . . . . . . . . 44
A.8. General Midpoint Rule Function for Half-grid Spacing . . . . . . . . . . . 45
A.9. Function to Determine Boundary Values by 3-point one-sided scheme on
an uneven Grid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
A.10.Function to Determine Boundary Values by 3-point one-sided scheme on
an even Grid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
A.11.Determines H
x
and H
y
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
A.12.Preprocessor for Poisson solver (Setting up and LU-factorizing Coecient
Matrix). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
A.13.Poisson Solver for Constant Coecient Matrix . . . . . . . . . . . . . . . . 53
A.14.Validation Function for Velocity Derivatives . . . . . . . . . . . . . . . . . 54
A.15.Validation Function for Pressure . . . . . . . . . . . . . . . . . . . . . . . 58
2
A.16.Plotting Function for Solution . . . . . . . . . . . . . . . . . . . . . . . . . 60
A.17.Compare With Benchmark Results . . . . . . . . . . . . . . . . . . . . . . 62
3
Figure 1: The considered ow. The lid is moving with a constant velocity U.
1. Background and Problem Description
We consider a two-dimensional lid-driven cavity ow in a rectangular domain. The ow
is sketched on gure 1. The lid is moving in the positive xdirection at a positive,
constant velocity while the other walls of the cavity are at rest. Since the ow is viscous,
the moving lid will cause a motion in the uid.
The objective in the present work is to develop and document a numerical tool to deter-
mine the velocity eld in the cavity.
4
2. Governing equations
In this section the physical assumptions for the ow will be stated and the governing
equations and boundary conditions derived.
2.1. Deriving an Appropriate Governing Equation
The well-known dimensional NS equation is
( u
i
)

t
+
( u
i
u
j
)
x
j
=


T
ij
x
j
+

S
i
(1)
where overbars denote dimensional quantities. For a newtonian uid in 3D
1
:

T
ij
=
_
p +
2
3

u
k
x
k
_

ij
+
_
u
i
x
j
+
u
j
x
i
_
We assume that there only body force is the gravity, so that S
i
= g
i
. In that case we
have
( u
i
)

t
+
( u
i
u
j
)
x
j
= g
i
+

x
j
_

_
p +
2
3

u
k
x
k
_

ij
+
_
u
i
x
j
+
u
j
x
i
__
Expanding the left hand side yields
( u
i
)

t
+
( u
i
u
j
)
x
j
= g
i


x
j
_
p +
2
3

u
k
x
k
_

ij
+

x
j
_
u
i
x
j
+
u
j
x
i
_
(2)
By rewriting g
i
=

x
j
(g
i
x
j
)
ij
and introducing the hydrostatic pressure

P = p +
g
i
x
j

ij
the above can be simplied to
( u
i
)

t
+
( u
i
u
j
)
x
j
=


P
x
j

ij

2
3


x
j
_
u
k
x
k
_

ij
+
_

2
u
i
x
2
j
+

2
u
j
x
j
x
i
_
(3)
Let us see what continuity can do for (3). The continuity equation for an incompressible
uid is
u
k
x
k
= 0 (4)
By use of this we see that the second term in the left hand side of (3) disappears.
Moreover we can rewrite the second term in the stress tensor

2
u
j
x
j
x
i
=

2
u
j
x
i
x
j
=

x
i
_
u
j
x
j
_
= 0 (5)
1
The term
2
3

u
j
x
j
is the dierence between the thermodynamic and the mechanical pressure, as discussed
in [3] p. 67. When derived in 2 dimensions the coecient to the term
u
j
x
j
can be shown to be
1
2
instead of
2
3
which is a three-dimensional result. As will be shown it does not matter here because
the whole term will drop due to the assumption of incompressibility.
5
By inserting (4) and (5) into (3) we get
( u
i
)

t
+
( u
i
u
j
)
x
j
=


P
x
j

ij
+
_

2
u
i
x
2
j
_
Using the nondimensionalization x
j
=
x
j
L
, u
j
=
u
j
U
and t =
U
L

t, the above yields


( [u
i
U])

_
t
L
U
+
( [u
i
U] [u
j
U])
[x
j
L]
=


P
[x
j
L]

ij
+
_

2
[u
i
U]
[x
j
L]
2
_
which is
U
2
L
u
i
t
+
U
2
L
u
i
u
j
x
j
=
1
L


P
x
j

ij
+
U
L
2
_

2
u
i

2
_
or
u
i
t
+
u
i
u
j
x
j
=
1
U
2


P
x
j

ij
+

LU
_

2
u
i
x
2
j
_
(6)
where
LU

is the Reynolds number, Re, such that the above is


u
i
t
+
u
i
u
j
x
j
=
1
U
2


P
x
j

ij
+
1
Re

2
u
i
x
2
j
(7)
Based on this we dene the nondimensional pressure as
P =

P
U
2
=
p + g
i
x
j
U
2
=
p
U
2
+
g
i
L
U
2
x
j
= p +
1
Fr
2
x
j
where Fr is the Froude number Fr =
U

gL
. Now we can write the nondimensional form
of the incompressible Navier-Stokes equations:
u
i
t
+
u
i
u
j
x
j
=
P
x
i
+
1
Re

2
u
i
x
2
j
(8)
which can be written
u
i
t
= H
i

P
x
i
, where H
i
=
1
Re

2
u
i
x
2
j

u
i
u
j
x
j
(9)
This it our most compact form of the momemntum equation, and it relates H to the
velocity eld. Assuming a twodimesional ow, H
i
can be written out for x and y:
6
H
x
=
1
Re
_

2
u
x
2
+

2
u
y
2
_

_
(uu)
x
+
(uv)
y
_
H
y
=
1
Re
_

2
v
x
2
+

2
v
y
2
_

_
(uv)
x
+
(vv)
y
_
(10)
We would like a direct equation for the pressure, so we take the divergence of (9):

x
i
_
u
i
t
_
=

x
i
_
H
i

P
x
i
_
and see that by reversing the order of dierentiation and applying the continuity equation
(4) on the left hand side, the time dependent term disappears:

t
_
u
i
x
i
_
= 0 =
H
i
x
i


2
P
x
2
i
(11)
This is the last equation we need. We now have three coupled dierential equations,
namely the two momentum equations (9), and the pressure equation (11) , which written
out are:
u
t
= H
x

P
x
v
t
= H
y

P
y

2
P
x
2
+

2
P
y
2
=
H
x
x
+
H
y
x
y
(12)
The two rst equations establish the time-dependence of the problem, while the latter is
a Poisson equation relating the pressure eld to the velocity eld at each instant.
2.2. Boundary Conditions
Since we are considering a viscous ow, we have the no-slip condition at all walls. Con-
sidering the problem geometry from gure 1, we can state the boundary conditions for
the uid velocity and its derivatives:
The velocities are given from the no-slip condition. All velocities have to follow the
movement of the wall, which means that all velocity components are zero at the
domain boundaries except that u = U for the lid (The northern domain boundary).
7
Figure 2: The boundary conditions for the uid velocity and its spatial derivatives.
Since the velocity components along the wall are constant, their derivatives along
the walls must be zero. E.g. since u = U , i.e. constant, along the whole lid,
u
x
and all higher order derivatives with respect to x must be zero here.
The rst spatial derivative normal to the wall is always zero because of continuity.
For example, for the lid where
u
x
= 0 we know from continuity that
u
x
+
v
y
= 0,
and therefore that
v
y
= 0. However we do not have information on higher order
spatial derivatives normal to the walls.
These BCs are summarized in gure 2.
The BCs for the pressure equation (12) are, since u and v are invariant in time on the
walls
P
x
= H
x
(on a vertical wall)
P
y
= H
y
(on a horizontal wall) (13)
8
3. Discretization and Implementation
To solve (12) numerically we need to discretize the domain. We chose a rectangular
grid because of the problem geometry. It turns out that it is desirable to introduce four
staggered grids, as presented in gure 3. The idea is to optimize the grid organization
for approximating the needed derivatives with the mid-point rule (the centered dierence
scheme), by making the grid such that the derivatives automatically are evaluated where
they are needed, in order to use as little interpolation as possible. This gives an ecient
implementation with pleasant numerical properties.
x
y
(a) U Grid
x
y
(b) V Grid
x
y
(c) P Grid
x
y
(d) UV Grid
Figure 3: The four staggered grids. The velocities are known in the Uand V grid. The
pressure is solved for in the P grid, while the UV grid is used for some
temporary results. For more details see table 2.
The grids as presented in gure 3 are the following: A Uand a V grid where the u
and v components of the velocity is known respectively. There is a P-grid in which
9
the pressure will be computed, and the last grid, the nite dierence or UV -grid. The
boundaries of the domain have to be represented on all grids (though not necessarily in
the same points). This implies that all grids can not be even in all directions since the
spacing between control points is halved at the boundary, as is also easily seen at gure
3. The approximation of the derivatives within and on the boundaries of the grid are
naturally aected by this fact. We will in this section categorize the dierent principal
methods for getting the derivatives. By principal methods we mean, that we derive it for
one direction, and afterwards utilize that u and v grids have 90

rotational symmetry.
Appreciate the fact that in combination the four grids cover all points in a grid with half
the grid spacing. A quantity known in one of the grids, derived in any direction by the
mid-point rule will always be known in one of the other grids.
Table 2 is the most important table in this report. It gives an overview of the whole
algorithm for our program. It show the cronological order of which things has to be done,
from approximating derivatives to performing the Euler integration. The steps outlined
in this table is described in detail further on in this chapter.
Firstly the dierent cases of approximating the spatial derivatives in the problem are
addressed. As mentioned, we use CDS, but there are dierent cases of how to obtain the
values at the boundaries of the grid and which grid spacing to use, which we will adress
now.
3.0.1. Cases for Approximation of Spatial Derivatives
a) Midpoint rule, half grid spacing. Boundary values known from
BCs or previous results.
An example of this is when
u
x
is found. The uvelocity is known in the U-
grid, and the midpoint rule on half grid spacing in the xdirection supplies
u
x
in the P-grid except for the west and east domain boundaries. In this case
we already know
u
x
at the domain boundaries from the BCs. The situation
is graphically sketched on gure 4a. For the inner points the scheme:
u
xi+1

Pgrid

u
i+1
|
U
u
i
|
U
x
is valid (the boundary points are included in the indexing). The derivatives in
the domain boundary points
u
x1

Pgrid
and
u
xN

Pgrid
are known a priori.
b) Midpoint rule, half grid spacing. Boundary values undened, are
not used.
An example of this is when we nd
(uv)
x
, where uv is known in the UV -grid.
The quantity
(uv)
x
has to be known in the V grid for computation of H
y
.
The inner points are treated as in a). When we use the midpoint rule in
the xdirection we obtain nothing on the east and west domain boundaries.
10
Quantity Determined from Procedure Direction Obtained in grid
u previous time step - - UG
v previous time step - - V G
u
x
u a) x PG
v
y
v a) y PG
v
x
v c) x UV G
u
y
u c) y UV G

2
u
x
2
u
x
c) x UG

2
v
y
2
v
y
c) y V G

2
v
x
2
v
x
b) x V G

2
u
y
2
u
y
b) y UG
u
2
u = UG
v
2
v = V G
u
UV G
u a) y UV G
v
UV G
v a) x UV G
uv u
UV G
, v
UV G
= UV G
(u
2
)
x
u
2
d) x UG
(v
2
)
y
v
2
d) y V G
(uv)
x
uv b) x V G
(uv)
y
uv b) y UG
H
x

2
u
x
2
,

2
u
y
2
,
(u
2
)
x
,
(uv)
y
= - UG
H
y

2
v
x
2
,

2
v
y
2
,
(v
2
)
y
,
(uv)
x
= - V G
P H
x
, H
y
PG
P
x
P a) x UG
P
y
P a) y V G
u
(m+1)
H
x
,
P
x
- UG
v
(m+1)
H
y
,
P
y
- V G
Symbol Algorithm
a) Midpoint rule, half grid spacing. Boundary values known from BCs or previous
results.
b) Midpoint rule, half grid spacing. Boundary values undened, are not used.
c) Midpoint rule, half grid spacing. Boundary values approximated by 3-point one-
sided scheme on uneven grid.
d) Midpoint rule, full grid spacing. Boundary values known from BCs or previous
results.
= Evaluation without shift of grid.
Solving system of equations.
Time stepping (Forward Euler) .
Table 2: Algorithm for determining the quantities in the problem.
11
But this is ne, since we do not need the value of H
y
at these boundaries.
As will be seen from section 3.3 H
y
only has to be known at northern and
southern cell faces in the P-grid, which does not include the eastern and
western domain boundary. Therefore we simply do not care to approximate
(uv)
x
here. (gure 4b).
c) Midpoint rule, half grid spacing. Boundary values approximated
by 3-point onesided scheme on uneven grid.
An example of this is when
v
x
is found. The vvelocity is known in the
V -grid, and this grid is not even in the xdirection. The inner points are
treated as in a) which supplies
u
x
in the P-grid except for the west and
east domain boundaries. The scheme for determining the
u
x
on the western
boundary is the 3-point onesided scheme
v
x 1

Pgrid

8 v
1
|
V
+ 9 v
2
|
V
v
3
|
V
3x
For a derivation of this scheme we refer to [1] and [2] .
d) Midpoint rule, full grid spacing. Boundary values known from BCs
or previous results.
The example is here the case where we want
(u
2
)
x
in the U-grid. Since we
in this case want the derivative in the same grid as the quantity we want to
derive, we have to use full grid space for the midpoint rule. The inner scheme
is thus
(u
2
)
x i

u
2
i+1

U
u
2
i1

U
2x
For the domain boundary points, the values are already known since
u
x
is
known to be zero. Since
(u
2
)
x
= 2u
u
x
= 0 in that case, we already know the
values at the boundary (gure 4d).
12
(a) Midpoint rule, half grid spacing. Boundary values known from BCs or previous
results.
(b) Midpoint rule, half grid spacing. Boundary values undened, are not used
(c) Midpoint rule, half grid spacing. Boundary values approximated by 3-point
onesided scheme on uneven grid.
(d) Midpoint rule, full grid spacing. Boundary values approximated by 3-point
onesided scheme on even grid.
Figure 4: The dierent cases for derivative estimation cases.
13
3.1. Determination of H
x
and H
y
Having determined all the relevant velocity derivatives as outlined in table 2, we are now
ready to use (10) to determine H
x
and H
y
. They are evaluated in the U and V -grid
respectively. The implementation is found in appendix A.11.
3.2. Solving the Poisson Equation
3.3. Derivation of Schemes
The discrete version of the governing equation (12) is
P
x

e
w
y
P
x

n
s
x = H
x
|
e
w
y H
y
|
n
s
x
We now use the nomenclature introduced in [2], with the sole dierence that D
j
now is
dened positive, i.e. D
j
=

j

j
. Doing this we can express the governing equation:
Recapitulation of the nomenclature from [2]

j
=
_
1 for j = e j = n
1 for , j = w j = s

j
=
_
x for j = e j = w
y for j = n j = s

j
=
_
y for j = e j = w
x for j = n j = s
In this way the governing equation (12) is changed in the following way:

j
P

j
=

j
H

j
(14)
3.3.1. The inner Scheme
We apply the CDS scheme for the pressure. The approximation for the spatial derivative
in the
j
-direction is then
P

j
=
j
P
i
P
P

j
(15)
14
By inserting this into (14) we get

(i.j)

j
_

j
P
i
P
P

j
_

j
=

(i.j)

j
H

(i.j)
(P
i
P
P
) D
j
=

(i.j)

j
H

j
D
j

(i.j)
D
j
P
i

_
_

j
D
j
_
_
P
P
=

(i.j)

j
H

j
D
j

j
Which is the much wanted inner cell equation from which everything can be set up in
the system of equations, as meticoulisly described in the previous report. We recognize
the form

(i,j)
a
i
P
i
+ a
P
P
P
= S
i
and by identifying the terms we can write the algorithm for the inner scheme:
Set a
i
= D
j
Set a
P
=
_

(i.j)
D
j
_
Set S
i
=

(i.j)

j
H

j
D
j

j
3.3.2. The Boundary Scheme
The pressure equation is a pure Neumann problem with the boundary conditions given
in (13). This boundary condition can be expressed
P

q
= H
q
(16)
We consider a cell with boundary condition on one face, q, and inner conditions on the
others, j. By inserting (15) and (16) into the governing equation (14) we get:

(i,j)=(p,q)

j
_

j
P
j
P
P

j
_

j
+
q
_
H
q

q
=

(i,j)

j
H

(i,j)=(p,q)
(P
j
P
P
) D
j
+
q
H
q
D
q

q
=

(i,j)

j
H

j
D
j

(i,j)=(p,q)
D
j
P
j
+
_
_

j=q
D
j
_
_
P
P
=

(i,j)

j
H

j
D
j

j

q
H
q
D
q

(i,j)=(p,q)
a
i
P
j
+ (a
P
+ a
p
) P
P
=

(i,j)

j
H

j
a
i

j

q
H
q
a
p

q
15
In other words the boundary scheme correction algorithm can be written
Set a
P
= a
P
+ a
p
Set S
i
= S
i

q
H
q
a
p

q
Set a
p
= 0
3.4. Solving algorithm
The identication of the a- and S-coecients makes it possible to set up the system of
equations as described in detail in [2]. This system is
S = AP
In principle everything can now be done as in [2]. However, since the equation is a bit
simpler, eciency can be improved substantially. If we consider A and S of we see that
while the latter depends on H
x
and H
y
and thus is time-dependent, the rst only depends
on the grid and is thus invariant in time. Therefore we only need to construct A one time.
We can even LU-factorize it once and for all. Since it is exactly these two operations
whose computation time has the highest order in N, taking them out of the time-loop is
of great benet for the solving eciency. When the solving the Poisson equation at each
time-step, we now just evaluate S, and solve the system
LP = G where LG = S
for the pressure P, where L and U are the lower and upper triangular matrices from the
LU-factorization of A respectively, and G is an intermediate result. In the implementation
we have made two functions, a preprocessor and a solver:
Preprocessor
Input: Pressure grid geometry
Computes a
i
and a
P
, builds A
LU-factorizes A: [L,U]=lu(A);
Output: L,U and grid information
16
Solver
Input: U, V and output from the preprocessor
Computes H
x
and H
y
and corrects S at the
boundaries
Solves the system: P=U\(L\S);
Output: P
The code for the preprocessor and the solver is seen in appendices A.12 and A.13.
3.5. Time Stepping Procedure
Having solved for the pressure eld we are ready to step the solution forward in time. For
simplicity and clarity we use the simplest time stepping procedure, namely the forward
Euler scheme. Of course any time-stepping procedure will do, e.g. Runge-Kutta schemes.
The two rst equations in (12) describes the time-dependence of the system. Using the
Euler scheme we get
u
m+1
= u
m
+ t
_
H
x

P
x
_
v
m+1
= v
m
+ t
_
H
y

P
y
_
And when we insert the CDS approximations for the derivatives of P:
u
m+1
i+1,j
= u
m
i+1,j
+ t
_
H
x

P
i+1,j
P
i,j
x
_
v
m+1
i,j+1
= v
m
i,j+1
+ t
_
H
y

P
i,j+1
P
i,j
y
_
where u
i,j
and v
i,j
where i = 1 i = m j = 1 j = n as before is given from
the boundary conditions. The linear stability criterion for Euler integration is that the
eigenvalues for A in the equation
u
t
= Au must not have a modulus larger than 1. One
can sense by considering
u
m+1
u
m
t
= H
x

P
x
that the lesser the right hand side becomes, the larger a t can be used without violating
the stability criterion. As H
x
and H
y
are reversely proportional to Re, we can to some
extent let the time step increase with Re. It is possible to carry out a linearized stability
analysis, but here we will just qualitatively account for the time step suggestions provided
in the assignment text.
17
3.6. Obtaining a Steady-State Solution
In the two test cases we want to obtain a steady state solution for the ow. Explicit Euler
timestepping technique and solve the Poisson equation at each timestep. At t = 0 the
u and v velocities are both zero, except on the northern boundary where the u velocity
is equal to 1. This ow eld satises the continuity equation, and is thus valid a initial
guess. We timestep until the solution has converged to a steady-state. Our critirium for
a steady state is that the change in the pressure P between two timesteps is suciently
low. However evaluating this change is timeconsuming, so we only evaluate the error at
a certain interval of timesteps (the check_interval). The stop criterium (steady state)
for our solver is therefore
error=max(

P
(k)
P
(kcheck interval)

)/(dtcheck_interval)
stop if error<tol
This approach will work nicely given that a steady solution actually exists. However
this is not by any means certain, since the ow can easily have a periodic or chaotic
behaviour. In [4] one of the main results is that the rst Hopf bifurcation of the ow
occurs at Re 8000. A Hopf bifurcation is a bifurcation of a xed point to a limit cycle
(Tabor 1989), which in the context of the velocity eld means that it goes from a steady
state to a periodic state. In [4] this is examplied with a simulation at Re = 10000,
where periodic behaviour is clearly seen. As long as we are well below Re = 8000,
a physical steady state exist, and we should be able to converge to it by the above
sketched criterion.
4. Validation
We validate the function that nd the derivatives of the velocities by using two test
functions for the velocity elds.
u(x, y) =
_
32x 96x
2
+ 64x
3
_ _
y + 5y
2
6y
3
_
v(x, y) =
_
x + 5x
2
6x
3
_ _
32y 96y
2
+ 64y
3
_
These functions fulll the same boundary conditions as the physical problem we are going
to model and can be dierenciated in any point in the domain. The functions are shown
in gure 5.
First Order Derivatives
The rst order derivative have generally very small errors. Examples are the derivatives
u
x
,
v
y
,
u
y
and
v
x
for which the errors are seen in gure 6 and 7. For the derivatives of the
18
PSfrag replacemen
x
y
0
0.5
1
0
0.5
1
0
0
1
2
x
y
0
0.5
1
0
0.5
1
0
0
1
2
Figure 5: Velocity functions used to test the code. u(x,y) (left) and v(x,y) (right).
squared velocities uu and vv the error becomes a bit larger because they are found using
approximations on double grid-spacing. This is seen on gure 8. For the uv derivatives
a 3-point one-sided nite dierence approximation is used on full grid spacing on the
boundaries. Therefore the values at the boundary on gure 9 are quite high. However
since these derivatives as seen from table 2, are not used for any computations, this error
is without signicance. It is just plotted here for completeness.
x
y
0
0.5
1
0
0.5
1
-0.05
0
0.05
x
y
0
0.5
1
0
0.5
1
-0.05
0
0.05
Figure 6: Errors of
u
x
(left) and
v
y
(right). Normalized with maximum value of analytical
solution.
19
PSfrag replacemen
x
y
0
0.5
1
0
0.5
1
-0.01
0
0.01
x
y
0
0.5
1
0
0.5
1
-0.01
0
0.01
Figure 7: Errors of
u
y
(left) and
v
x
(right). Normalized with maximum value of analytical
solution.
x
y
0
0.5
1
0
0.5
1
-0.2
0
0.2
x
y
0
0.5
1
0
0.5
1
-0.2
0
0.2
Figure 8: Errors of
u
2
x
(left) and
v
2
y
(right). Normalized with maximum value of ana-
lytical solution.
20
PSfrag replacemen
x
y
0
0.5
1
0
0.5
1
-0.2
0
0.2
x
y
0
0.5
1
0
0.5
1
-0.2
0
0.2
Figure 9: Errors of
uv
x
(left) and
uv
y
(right). Normalized with maximum value of ana-
lytical solution.
Second Order Derivatives
The second order derivatives become a bit larger because the approximations are based
on the rst order derivatives, and thus carrying the error from gure 6 and 7 with them.
The error for

2
u
x
2
and

2
v
y
2
is seen in gure 10, while it for

2
u
y
2
and

2
v
x
2
is shown in gure
11. In general it can be said for all the derivatives that the error is at an acceptable level.
Only at the boundary does the (normalized) error become as high as 0.1. This is not
surprising when one considers that the grid is coarse, and it is at the domain boundaries
we make the crudest approximations. It has been checked that error indeed goes to zero
for higher spatial resolution. The code for this validation is found in appendix A.14.
x
y
0
0.5
1
0
0.5
1
-0.1
0
0.1
x
y
0
0.5
1
0
0.5
1
-0.1
0
0.1
Figure 10: Errors of

2
u
x
2
(left) and

2
v
y
2
(right). Normalized with maximum value of ana-
lytical solution.
21
PSfrag replacemen
x
y
0
0.5
1
0
0.5
1
-0.05
0
0.05
x
y
0
0.5
1
0
0.5
1
-0.05
0
0.05
Figure 11: Errors of

2
u
y
2
(left) and

2
v
x
2
(right). Normalized with maximum value of ana-
lytical solution.
Pressure Solver Verication
We test the pressure solver using an analytical function that is a solution to the Poisson
equation.

2
P = 2k
2
cos(kx)cos(ky) (17)
which can be rewritten to the form of (12):

2
P =

x
H
x
+

y
H
y
where
H
x
= k sin(kx) cos(ky) and H
y
= k sin(ky) cos(kx).
The analytical solution to (17) is then
P(x, y) = cos(kx)cos(ky)
We supply H
x
and H
y
to our Poisson solver and the result can be evaluated against the
analytical solution in gure 12. The numerical solution corresponds very well with the
analytical solution and the error is seen in gure 13. For a N = 10 grid a relative error
less than 0.02 is perfectly acceptable. Note that we do not evaluate the pressure on the
boundaries since it is not needed for the further solution of our cavity ow problem. If it
was to be evaluated we would approximate it by a 3-point onesided scheme on an uneven
grid.
The code for this validation is found in appendix A.15.
22
PSfrag replacemen
x
y
0
0.5
1
0
0.5
1
-1
0
1
x
y
0
0.5
1
0
0.5
1
-1
0
1
Figure 12: Pressure calculated by our Poisson-solver (left) exact solution (right).
x
y
0
0.5
1
0
0.5
1
-0.02
-0.01
0
0.01
0.02
Figure 13: Error of pressure solver at N=10. Normalized with maximum value of analyt-
ical solution.
23
5. Results
Case I: Creeping Flow
For this case we use a time step of t = 0.0001, a grid size of n = 20, and a Reynolds-
number of Re = 1 (a creeping ow) . With a tolerance of tol = 10
10
between an
interval of 500 time steps we nd that the solution has converged after 3000 time steps.
The steady state solution is seen in gure 14. If we wish to discover the small rollers in
the lower corners of the domain we nd it necessary to increase the grid size to n = 40.
In gure 15 we have plotted the high-resolution solution, and here the rollers are present.
The streamlines are therefore comparable with the ow seen in the lecture note.
x
y
0 0.2 0.4 0.6 0.8 1
0
0.2
0.4
0.6
0.8
1
Figure 14: Steady state solution of creeping ow problem (Re=1, t=0.0001). Grid size,
n=20.
24
x
y
0 0.2 0.4 0.6 0.8 1
0
0.2
0.4
0.6
0.8
1
Figure 15: Steady state solution of creeping ow problem (Re=1, t=0.0001). Grid size,
n=40. Now the rollers in the lower corners are present.
To verify that the method ensures mass conservation to machine precision, we consider
the continuity equation (4):
u
x
+
v
y
= 0
This has to be valid both locally and globally. Since we have already used the conti-
nuity equation to derive the boundary conditions (see gure 2), global conservation is
automatically ensured. In other words the no-slip condition makes sure that the normal
velocity to all walls (and its derivatives) is set to zero, and thus there can be no ux into
or out of the domain.
Locally it should also be conserved, since the continuity equation is used in the derivation
of 12. To check whether this is actually the case is easy. Both
u
x
and
v
y
is known in the
Pgrid. By element-wise addition of
u
x
and
v
y
in the P-grid, any norm of the resulting
matrix has to be zero to the order of machine precision.
The norm is calculated for each time step and shown in gure 16. We see that the mass
is conserved to a level in the order of machine precision. With increasing time steps
25
however we see that error accumulates due truncation error at each time step, which is
expected.
5.1. Checking for Conservation
Timestep
n
o
r
m
(
u
x
+
v
y
)
Mass Conservation
0 100 200 300 400 500
0
0.5
1
1.5
2
10
12
Figure 16: Conservation of mass at early time steps. Re=1000, t=0.0001, n=20.
Case II: Re=1000
We now consider a more advanced ow in the sense that the Reynolds number is now
Re = 1000. Since the inertial forces are now more dominant than the viscous, the added
momentum from the moving lid does that the ow-pattern is not longer simple and
symmetric, but will evolve eddies. We keep a grid size of n = 20 and set t = 0.01.
With the same stopping criterion as before we nd a steady state after 12000 time steps.
The solution is shown in gure 17 which qualitatively agrees with solution given as
example in the lecture note.
26
x
y
0 0.2 0.4 0.6 0.8 1
0
0.2
0.4
0.6
0.8
1
Figure 17: Steady state solution of ow with Re=1000. Found with t=0.01) and grid
size, n=20.
We now compare our solution with the results of Bruneau and Saad [4], and see how the
solution compares at dierent grid sizes. This should give a picture of the convergence
rate of our solver. The direct comparison is seen in gures 18 and 19, and a plot of
the convergence to the reference values is given in 20. It should be noted that we have
changed the sign of the velocity of the lid so the lid now goes from right to left. This was
done to compare with the reference data. We see that the convergence rate is between
-1.5 and -2. This convergence is governed by the CDS scheme for the pressure solver and
the mid-point-rule schemes used for nding the derivatives. The reason for it not to be
-2 is maybe that the solver uses approximations based on earlier approximation (like the
second order derivatives and mixed velocity derivatives). A reason is also that we do not
consider very ne grids, so the convergence rate is probably not completely constant yet.
A convergence plot computed for considerably higher numbers of grid points is expected
to yield a slope close to 2.
27
y
u
Reference u
u, n=30
u, n=50
u, n=70
0 0.5 1
-0.6
-0.4
-0.2
0
0.2
0.4
x
v
Reference v
v, n=30
v, n=50
v, n=70
0 0.5 1
-0.6
-0.4
-0.2
0
0.2
0.4
Figure 18: Comparison of our results for dierent grid sizes with results of [4] at
Re=1000. Left plot shows the u-velocity along the vertical center line, and
right plot shows the v-velocity along the horizontal center line. The boundary-
values have been ignored in the plot.
28
y
P
Reference P(y)
P(y), n=30
P(y), n=50
P(y), n=70
0 0.5 1
0
0.05
0.1
0.15
0.2
x
P
Reference P(x)
P(x), n=30
P(x), n=50
P(x), n=70
0 0.5 1
0
0.02
0.04
0.06
0.08
0.1
Figure 19: Comparison of our results for dierent grid sizes with results of [4] at
Re=1000. Left plot shows the pressure along the vertical center line, and right
plot shows the pressure along the horizontal center line. The boundary-values
have been ignored in the plot. Pressure has been normalized with regard to the
pressure at the center of the domain.
29
x
norm(|u u
ref
|), slope=-1.9
norm(|v v
ref
|), slope=-1.9
norm(|P(x) P
ref
(x)|), slope=-1.8
norm(|P(y) P(y)
ref
|), slope=-1.6
30 50 70
10
2
10
1
Figure 20: Convergence of velocities and pressure to results of [4] at Re=1000. The error
is estimated by taking the norm of the dierence between the our results and
those of [4].
A time series of the transient ow at Re = 1000 is shown in gure 21. This is to
demonstrate how the ow converges to the steady state. The time it takes to reach this
state is inuenced by the time step, grid size and of course the tolerance in the criterion
for the steady state.
30
Time=5
y
0 0.5 1
0
0.2
0.4
0.6
0.8
1
Time=10
y
0 0.5 1
0
0.2
0.4
0.6
0.8
1
Time=15
y
0 0.5 1
0
0.2
0.4
0.6
0.8
1
Time=40
y
0 0.5 1
0
0.2
0.4
0.6
0.8
1
Figure 21: Time series showing the most interesting events in the transient ow. Upper
left, ow at t=5: a cell is developing in the lower right side. Upper right, at
t=10: lower left cell fully developed. Lower left at t=15: new cell is starting to
show up in lower right corner. Lower right gure at t=40. Flow is considered
fully developed and steady state. Colors show absolute ow velocity. Blue
is rapid (at the moving wall), red is slower (main ow cell) and bordeaux is
slowest (cell centers and stationary walls).
31
6. Conclusion
A numerical tool to solve the Navier-Stokes in 2D for a ow in a rectangular cavity has
been developed. It is based on a CDS nite volume method and a forward Euler time
integration scheme, for which the basic theory has been covered in the present report and
in [2]. The performance of the tool has been validated against an analytical function to
check its computation of H
x
and H
y
, and the velocity eld resulting from the computation
has been compared to values from [4]. Both validations yields ne results. The method
shows furthermore global mass conservation to the order of machine precision.
Literature
1. Harry B. Bingham: Lecture Note VI: Solving the Navier-Stokes Equation. (Lecture
notes in the present course).
2. Peter Baek & Jakob Borbye: Heat Transport In a Cavity Flow. (2
nd
report in the
present course).
3. Frank M. White: Viscous Fluid Flow. 3rd Edition. McGraw-Hill, 2006.
4. Bruneau, C.-H. and Saad, M: The 2D lid-driven cavity problem revisited. Comput-
ers and Fluids, 35:326-348, 2006
32
A. Matlab Scripts and Functions
A.1. Main Script for the Creeping Flow Problem
1 f unct i on Sol ut i on=runner ( )
%Runner f or probl em 5:
%Se t t i ng s f or probl em5
Re=1
6 nx=40
ny=40
Lx=1
Ly=1
dt =0. 0001;
11 t o l =1e10
out put _i nt er val =500;
%Find s t eady s t a t e
Sol ut i on=St eadySt at eSol ver ( nx , ny , Lx , Ly , Re , dt , t ol , out put _i nt er val
) ;
16
Pl ot Sol ut i on ( Sol ut i on ) ;
33
A.2. Main Script for Re = 1000.
f unct i on Sol ut i on=runner ( )
%Runner f or probl em 6 part a ( compare wi t h l e c t ur e not e ) :
3
%Se t t i ng s :
Re=1000
nx=20
ny=20
8 Lx=1
Ly=1
dt =0. 01;
t o l =1e 10;
out put _i nt er val =1000;
13
%Find St eady St at e
Sol ut i on=St eadySt at eSol ver ( nx , ny , Lx , Ly , Re , dt , t ol , out put _i nt er val
) ;
Pl ot Sol ut i on ( Sol ut i on ) ;
34
A.3. Main Script for Re = 1000, benchmarking.
f unct i on Sol vec=runner ( Sol vec )
%Runner f or probl em 6 part 2 ( Compare wi t h Bruneau and Saad) :
c l o s e a l l
5 %Se t t i ng s
n_vec=[30 50 7 0 ] ; %Vect or de f i ni ng d i f f e r e n t g r i d s i z e s t o be
computed and compared t o f i nd convergence .
Re=1000
Lx=1
10 Ly=1
dt =0.01
t o l =1e10
out put _i nt er val =1000
15 %For a l l t he d i f f e r e n t g r i d s i z e s :
f o r k=1: l engt h ( n_vec )
k=k
nx=n_vec( k)
ny=n_vec( k)
20
Sol ut i on=St eadySt at eSol ver ( nx , ny , Lx , Ly , Re , dt , t ol ,
out put _i nt er val ) ;
Sol vec ( k) . Sol ut i on=Sol ut i on ;
end
25 %Compare i t wi t h Bruneau and Saad .
CompareWithBenchmark( Sol vec )
35
A.4. Grid Generation Function
f unct i on [ PG, UG,VG,UVG]=Gri dGenerator ( nx , ny , Lx , Ly)
%Generat es t he 4 s t ag g e r e d g r i d s and de c l ar e s t he v a r i a b l e s t hat
are t o be
%f ound i n t he corres pondi ng g r i d s . Var i abl es are decl ar ed
accordi ng t o t he
4 %al gor i t hms of t a b l e 2 i n t he pr es ent r epor t .
p l o t i t =0;
i f nar gi n==0
nx=4
9 ny=4
Lx=1
Ly=1
p l o t i t =1
end
14
%Generate St andard Grid :
[ StG. X, StG. Y]=meshgri d ( l i ns pa c e ( 0 , Lx , 2 nx+1) , l i ns pa c e ( 0 , Ly , 2 ny
+1) ) ;
%Generate Pg r i d :
19 PG. X=StG.X( [ 1 2 : 2 : end1 end ] , [ 1 2 : 2 : end1 end ] ) ;
PG. Y=StG.Y( [ 1 2 : 2 : end1 end ] , [ 1 2 : 2 : end1 end ] ) ;
PG. ux=zer os ( s i z e (PG. X) ) ;
PG. vy=zer os ( s i z e (PG. X) ) ;
PG. P=zer os ( s i z e (PG. X) ) ;
24 PG. dx=StG. X( 1 , 3)StG. X( 1 , 1) ;
PG. dy=StG. Y( 3 , 1)StG. Y( 1 , 1) ;
PG. nx=nx ;
PG. ny=ny ;
29
%Generate Ug r i d :
UG. X=StG.X( [ 1 2 : 2 : end1 end ] , 1 : 2 : end) ;
UG. Y=StG.Y( [ 1 2 : 2 : end1 end ] , 1 : 2 : end) ;
UG. u=zer os ( s i z e (UG. X) ) ;
34 UG. uxx=zer os ( s i z e (UG. X) ) ;
UG. uyy=zer os ( s i z e (UG. X) ) ;
UG. uux=zer os ( s i z e (UG. X) ) ;
UG. uvy=zer os ( s i z e (UG. X) ) ;
UG. H1=zer os ( s i z e (UG. X) ) ;
39 UG. Px=zer os ( s i z e (UG. X) ) ;
36
%Generate Vg r i d :
VG. X=StG.X( 1 : 2 : end , [ 1 2 : 2 : end1 end ] ) ;
44 VG. Y=StG.Y( 1 : 2 : end , [ 1 2 : 2 : end1 end ] ) ;
VG. v=zer os ( s i z e (VG. X) ) ;
VG. vyy=zer os ( s i z e (VG. X) ) ;
VG. vxx=zer os ( s i z e (VG. X) ) ;
VG. vvy=zer os ( s i z e (VG. X) ) ;
49 VG. uvx=zer os ( s i z e (VG. X) ) ;
VG. H2=zer os ( s i z e (VG. X) ) ;
VG. Py=zer os ( s i z e (VG. X) ) ;
%Generate UVg r i d :
54 UVG. X=StG. X( 1 : 2 : end , 1 : 2 : end) ;
UVG. Y=StG. Y( 1 : 2 : end , 1 : 2 : end) ;
UVG. uy=zer os ( s i z e (UVG. X) ) ;
UVG. vx=zer os ( s i z e (UVG. X) ) ;
UVG. uv=zer os ( s i z e (UVG. X) ) ;
59
i f p l o t i t
64 %Pl ot Gri ds :
xt i c ks=StG. X( 1 , 1 : 2 : end) ;
yt i c ks=StG. Y( 1 : 2 : end , 1 ) ;
[ f i g1 , axes1]=pl ot _cr eat or ( U Grid , $x$ , $y$ )
69 s e t ( gca , Xti ck , xt i cks , Yti ck , yt i cks , XTickLabel , { , ,
, , } , YTickLabel , { , , , , , })
hol d on
pl ot ( reshape (UG. X( 2 : end 1 , 2: end1) , 1 , [ ] ) , reshape (UG. Y( 2 : end
1 , 2: end1) , 1 , [ ] ) , ro )
pl ot ( reshape (UG. X( [ 1 : end ] , 1 ) , 1 , [ ] ) , reshape (UG. Y( [ 1 : end ] , 1 )
, 1 , [ ] ) , bo )
pl ot ( reshape (UG. X( [ 1 : end ] , end) , 1 , [ ] ) , reshape (UG. Y( [ 1 : end ] ,
end) , 1 , [ ] ) , bo )
74 pl ot ( reshape (UG. X( 1 , [ 1 : end ] ) , 1 , [ ] ) , reshape (UG. Y( 1 , [ 1 : end ] )
, 1 , [ ] ) , bo )
pl ot ( reshape (UG. X( end , [ 1 : end ] ) , 1 , [ ] ) , reshape (UG. Y( end , [ 1 : end
] ) , 1 , [ ] ) , bo )
axi s ( [ 0 1 0 1 ] )
gr i d
37
pl ot _l at ex ( f i g1 , ha l f )
79
[ f i g2 , axes2]=pl ot _cr eat or ( V Grid , $x$ , $y$ )
s e t ( gca , Xti ck , xt i cks , Yti ck , yt i cks , XTickLabel , { , ,
, , } , YTickLabel , { , , , , , })
hol d on
pl ot ( reshape (VG. X( 2 : end 1 , 2: end1) , 1 , [ ] ) , reshape (VG. Y( 2 : end
1 , 2: end1) , 1 , [ ] ) , ro )
84 pl ot ( reshape (VG. X( [ 1 : end ] , 1 ) , 1 , [ ] ) , reshape (VG. Y( [ 1 : end ] , 1 )
, 1 , [ ] ) , bo )
pl ot ( reshape (VG. X( [ 1 : end ] , end) , 1 , [ ] ) , reshape (VG. Y( [ 1 : end ] ,
end) , 1 , [ ] ) , bo )
pl ot ( reshape (VG. X( 1 , [ 1 : end ] ) , 1 , [ ] ) , reshape (VG. Y( 1 , [ 1 : end ] )
, 1 , [ ] ) , bo )
pl ot ( reshape (VG. X( end , [ 1 : end ] ) , 1 , [ ] ) , reshape (VG. Y( end , [ 1 : end
] ) , 1 , [ ] ) , bo )
axi s ( [ 0 1 0 1 ] )
89 gr i d
pl ot _l at ex ( f i g2 , ha l f )
[ f i g3 , axes3]=pl ot _cr eat or ( P Grid , $x$ , $y$ )
s e t ( gca , Xti ck , xt i cks , Yti ck , yt i cks , XTickLabel , { , ,
, , } , YTickLabel , { , , , , , })
94 hol d on
pl ot ( reshape (PG. X( 2 : end 1 , 2: end1) , 1 , [ ] ) , reshape (PG. Y( 2 : end
1 , 2: end1) , 1 , [ ] ) , ro )
hol d on
pl ot ( reshape (PG. X( [ 1 : end ] , 1 ) , 1 , [ ] ) , reshape (PG. Y( [ 1 : end ] , 1 )
, 1 , [ ] ) , bo )
pl ot ( reshape (PG. X( [ 1 : end ] , end) , 1 , [ ] ) , reshape (PG. Y( [ 1 : end ] ,
end) , 1 , [ ] ) , bo )
99 pl ot ( reshape (PG. X( 1 , [ 1 : end ] ) , 1 , [ ] ) , reshape (PG. Y( 1 , [ 1 : end ] )
, 1 , [ ] ) , bo )
pl ot ( reshape (PG. X( end , [ 1 : end ] ) , 1 , [ ] ) , reshape (PG. Y( end , [ 1 : end
] ) , 1 , [ ] ) , bo )
axi s ( [ 0 1 0 1 ] )
gr i d
pl ot _l at ex ( f i g3 , ha l f )
104
[ f i g4 , axes4]=pl ot _cr eat or ( UV Grid , $x$ , $y$ )
s e t ( gca , Xti ck , xt i cks , Yti ck , yt i cks , XTickLabel , { , ,
, , } , YTickLabel , { , , , , , })
hol d on
38
pl ot ( reshape (UVG. X( 2 : end 1 , 2: end1) , 1 , [ ] ) , reshape (UVG. Y( 2 :
end 1 , 2: end1) , 1 , [ ] ) , ro )
109 pl ot ( reshape (UVG. X( [ 1 : end ] , 1 ) , 1 , [ ] ) , reshape (UVG. Y( [ 1 : end ] , 1 )
, 1 , [ ] ) , bo )
pl ot ( reshape (UVG. X( [ 1 : end ] , end) , 1 , [ ] ) , reshape (UVG. Y( [ 1 : end ] ,
end) , 1 , [ ] ) , bo )
pl ot ( reshape (UVG. X( 1 , [ 1 : end ] ) , 1 , [ ] ) , reshape (UVG. Y( 1 , [ 1 : end ] )
, 1 , [ ] ) , bo )
pl ot ( reshape (UVG. X( end , [ 1 : end ] ) , 1 , [ ] ) , reshape (UVG. Y( end , [ 1 :
end ] ) , 1 , [ ] ) , bo )
axi s ( [ 0 1 0 1 ] )
114 gr i d
pl ot _l at ex ( f i g4 , ha l f )
end
39
A.5. Boundary Condition Imposing Function
f unct i on DBC=SetDomai nBoundaryCondi ti ons ( )
%The boundary c ondi t i ons of our p h y s i c a l probl em .
%El ements of DBC (Domain Boundary Condi t i ons ) f o l l o w t he order
of nort h ( 1) ,
4 %s out h ( 2) , e as t ( 3) , west ( 4) .
%The nort h wal l i s moving wi t h cons t ant v e l o c i t y .
Wal l Vel oci t y =1;
9 %The v e l o c i t i e s at t he boundari es are gi ven from nos l i p
condi t i on .
DBC( 1) . u=Wal l Vel oci ty ; %( nort h wal l )
DBC( 2) . u=0; %s out h wal l
DBC( 3) . u=0;
DBC( 4) . u=0;
14 DBC( 1) . v=0;
DBC( 2) . v=0;
DBC( 3) . v=0;
DBC( 4) . v=0;
%The d e r i v a t i v e s of t he v e l o c i t i e s p a r a l l e l t o t he wa l l s .
19 DBC( 1) . ux=0;
DBC( 2) . ux=0;
DBC( 3) . vy=0;
DBC( 4) . vy=0;
24 DBC( 1) . vx=0;
DBC( 2) . vx=0;
DBC( 3) . uy=0;
DBC( 4) . uy=0;
%And t he second d e r i v a t i v e s of t he v e l o c i t i e s p a r a l l e l t o t he
wa l l s wi t h
29 %r e s pe c t t o t he d i r e c t i o n p a r r a l l e l t o t he wa l l s .
DBC( 1) . uxx=0;
DBC( 2) . uxx=0;
DBC( 3) . vyy=0;
DBC( 4) . vyy=0;
34 %And from t he c ont i nui t y equat i on we al s o have :
DBC( 1) . vy=0;
DBC( 2) . vy=0;
DBC( 3) . ux=0;
DBC( 4) . ux=0;
40
A.6. Determine Partial Velocity Derivatives
1 f unct i on [ PG, UG,VG,UVG]=Fi ndVel oci t yDer i vat i ves (PG, UG,VG,UVG,DBC
)
%Funct i on t o compute a l l r equi r ed d e r i v a t i v e s of t he gi ven
v e l o c i t i e s u and
%v i n t he U and V g r i d r e s p e c t i v e l y . The d e r i v a t i v e s are f ound
i n t he g r i d s
%where t hey are r equi r ed f or t he f ur t h e r comput at i ons .
6 %ux i s known on e as t and west f ac e s :
PG. ux ( : , 1 )=DBC( 4) . ux ; %West
PG. ux ( : , end)=DBC( 3) . ux ; %East
%On t he r e s t i t i s approxi mat ed us i ng MPR
PG. ux ( : , 2 : end1)=mpr_hgs (UG. u ,UG. X, 2 ) ;
11
%vy i s known on nort h and s out h f ac e s :
PG. vy ( end , : ) =DBC( 1) . vy ; %North
PG. vy ( 1 , : )=DBC( 2) . vy ; %South
%On t he r e s t i t i s approxi mat ed us i ng MPR
16 PG. vy ( 2 : end 1 , : )=mpr_hgs (VG. v ,VG. Y, 1 ) ;
%uy
%Use tp1s_ueg t o f i nd uy on nort h and s out h f ac e s :
[ uy_south , uy_north]=tp1s_ueg (UG. u ,UG. Y, 1 ) ; %South
21 UVG. uy ( 1 , : )=uy_south ;
UVG. uy( end , : ) =uy_north ;
%On t he r e s t i t i s approxi mat ed us i ng MPR
UVG. uy ( 2 : end 1 , : )=mpr_hgs (UG. u ( 2 : end 1 , : ) ,UG. Y( 2 : end 1 , : ) , 1) ;
26 %vx
%Use tp1s_ueg t o f i nd uy on e as t and we s t f ac e s :
[ vx_west , vx_east ]=tp1s_ueg (VG. v ,VG. X, 2 ) ; %South
UVG. vx ( : , 1 )=vx_west ;
UVG. vx ( : , end)=vx_east ;
31 %On t he r e s t i t i s approxi mat ed us i ng MPR
UVG. vx ( : , 2 : end1)=mpr_hgs (VG. v ( : , 2 : end1) ,VG. X( : , 2 : end1) , 2) ;
%uxx
%On e as t and west we approxi mat e i t us i ng TP1S_UEG:
36 [ uxx_west , uxx_east ]=tp1s_ueg (PG. ux ,PG. X, 2 ) ;
UG. uxx ( : , 1 )=uxx_west ;
UG. uxx ( : , end)=uxx_east ;
UG. uxx ( : , 2 : end1)=mpr_hgs (PG. ux ( : , 2 : end1) ,PG. X( : , 2 : end1) , 2) ;
41
41 %vyy
%On nort h and s out h we approxi mat e i t us i ng TP1S_UEG:
[ vyy_south , vyy_north]=tp1s_ueg (PG. vy ,PG. Y, 1 ) ;
VG. vyy ( 1 , : )=vyy_south ;
VG. vyy( end , : ) =vyy_north ;
46 VG. vyy ( 2 : end 1 , : )=mpr_hgs (PG. vy ( 2 : end 1 , : ) ,PG. Y( 2 : end 1 , : ) , 1) ;
%uyy
%On nort h and s out h we approxi mat e i t us i ng TP1S_EG:
[ uyy_south , uyy_north]=tp1s_eg (UVG. uy ,UVG. Y, 1 ) ;
51 UG. uyy ( 1 , : )=uyy_south ;
UG. uyy ( end , : ) =uyy_north ;
UG. uyy ( 2 : end 1 , : )=mpr_hgs (UVG. uy ,UVG. Y, 1 ) ;
%vxx
56 %On e as t and west we approxi mat e i t us i ng TP1S_EG:
[ vxx_west , vxx_east ]=tp1s_eg (UVG. vx ,UVG. X, 2 ) ;
VG. vxx ( : , 1 )=vxx_west ;
VG. vxx ( : , end)=vxx_east ;
VG. vxx ( : , 2 : end1)=mpr_hgs (UVG. vx ,UVG. X, 2 ) ;
61
%uv
%We use t he mean v al ue s bet ween t o g r i d p o i nt s .
UVG. u=[UG. u ( 1 , : ) ; (UG. u ( 2 : end 2 , : )+UG. u ( 3 : end 1 , : ) ) /2; UG. u( end
, : ) ] ;
UVG. v=[VG. v ( : , 1 ) (VG. v ( : , 2 : end2)+VG. v ( : , 3 : end1) ) /2 VG. v ( : , end)
] ;
66 UVG. uv=UVG. u. UVG. v ;
%uux :
%On e as t and west we know t he d e r i v a t i v e from t he boundary
condi t i on .
UG. uux ( : , 1 ) =2DBC( 4) . ux . DBC( 4) . u ; %West
71 UG. uux ( : , end)=2DBC( 3) . ux . DBC( 3) . u ; %East
%on t he r e s t we use doubl e g r i d s paci ng i nt e r p o l a t i o n .
UG. uux ( : , 2 : end1)=mpr_fgs (UG. u. ^2 ,UG. X, 2 ) ;
76 %vvy :
%On nort h and s out h we know t he d e r i v a t i v e due t o boundary
c ondi t i ons .
VG. vvy ( 1 , : ) =2DBC( 2) . vy . DBC( 2) . v ; %South
VG. vvy( end , : ) =2DBC( 1) . vy . DBC( 1) . v ; %nort h
42
%on t he r e s t we use doubl e g r i d s paci ng i nt e r p o l a t i o n .
81 VG. vvy ( 2 : end 1 , : )=mpr_fgs (VG. v . ^2 ,VG. Y, 1 ) ;
%uvx :
%On e as t and west we approxi mat e i t us i ng TP1S_EG:
[ uvx_west , uvx_east ]=tp1s_eg (UVG. uv ,UVG. X, 2 ) ;
86 VG. uvx ( : , 1 )=uvx_west ;
VG. uvx ( : , end)=uvx_east ;
%On t he r e s t we use mpr hgs .
VG. uvx ( : , 2 : end1)=mpr_hgs (UVG. uv ,UVG. X, 2 ) ;
91 %uvy :
%On e as t and west we approxi mat e i t us i ng TP1S_EG:
[ uvy_south , uvy_north]=tp1s_eg (UVG. uv ,UVG. Y, 1 ) ;
UG. uvy ( 1 , : )=uvy_south ;
UG. uvy ( end , : ) =uvy_north ;
96 %On t he r e s t we use mpr hgs .
UG. uvy ( 2 : end 1 , : )=mpr_hgs (UVG. uv ,UVG. Y, 1 ) ;
43
A.7. General Midpoint Rule Function for Full-grid Spacing
f unct i on dmat=mpr_fgs ( mat , pos , dim)
2 %MidPoi ntRul e t o f i nd d e r i v a t i v e s on t he g r i dpoi nt us i ng t he
two nei gbor i ng v al ue s .
%Find t he d e r i v a t i v e i n t he d i r e c t i o n dim (1=rowwi se dQ/dy ,
%2=col oumnwi se dQ/dx i n c a r t i s i a n coor di nat es f or pr oper t y Q) .
dxs i=d i f f ( pos , [ ] , dim) ;
7 s wi t ch dim
cas e 1
dmat=(mat ( 3 : end , : )mat ( 1 : end 2 , : ) ) . / ( dxs i ( 1 : end 1 , : )+
dxs i ( 2 : end , : ) ) ;
cas e 2
dmat=(mat ( : , 3 : end)mat ( : , 1 : end2) ) . / ( dxs i ( : , 1 : end1)+
dxs i ( : , 2 : end) ) ;
12 end
44
A.8. General Midpoint Rule Function for Half-grid Spacing
f unct i on dmat=mpr_hgs ( mat , pos , dim)
%MidPoi ntRul e t o f i nd d e r i v a t i v e s on t he hal f way bet ween t he
3 %mat ri xv al ue s . Find t he d e r i v a t i v e i n t he d i r e c t i o n dim (1=
rowwi se dQ/dy ,
%2=col oumnwi se dQ/dx i n c a r t i s i a n coor di nat es f or pr oper t y Q) .
dxs i=d i f f ( pos , [ ] , dim) ;
s wi t ch dim
8 cas e 1
dmat=(mat ( 2 : end , : )mat ( 1 : end 1 , : ) ) . / dxs i ;
cas e 2
dmat=(mat ( : , 2 : end)mat ( : , 1 : end1) ) . / dxs i ;
end
45
A.9. Function to Determine Boundary Values by 3-point one-sided
scheme on an uneven Grid
f unct i on [ dvec1 , dvec2]=tp1s_ueg ( mat , pos , dim)
%Three poi nt ones i ded sheme on a uneveng r i d ( h a l f g r i d s paci ng
f i r s t bet ween f i r s t and l a s t two col oumns /rows and f u l l
bet ween t he r e s t ) .
3
%Find d e r i v a t i v e of mat v al ue s gi ven on pos p o s i t i o ns on t he
border .
% dim det ermi nes whi ch borders are l ooked up : i f dim=1 dQ/dy
on
%nort h / s out h f ac e s are f ound . I f dim=2 dQ/dx i s f ound on East /
West f ac e s .
8 %Ret urns two v e c t or s :
% dvec1 bei ng s out h ( row vect or s i z e (1 , nx ) ) or west ( coloumn
vect or s i z e ( ny , 1) ) ;
% dvec2 bei ng nort h ( row vect or s i z e (1 , nx ) ) or e as t ( coloumn
vect or s i z e ( ny , 1) )
dxs i=d i f f ( pos , [ ] , dim) ;
13 %Act ual l y we onl y use dx s i ( 2 , 2) because t h i s v al ue s r e pr e s e nt s
t he f u l l
%g r i d s paci ng .
i f min( dxs i )==0
e r r or ( Wrong di mensi on ! dxs i =0 )
end
18 i f dxs i ( 2 , 2)==dxs i ( 1 , 1)
e r r or ( Grid s paci ng not c o r r e c t ! Fi r s t row/ c ol must be ha l f
of second row c ol . Do not use TP1S_UEG )
end
23 s wi t ch dim
cas e 1
dvec1 =1/(3 dxs i ( 2 , 2) ) (8mat ( 1 , : ) +9mat ( 2 , : )mat ( 3 , : ) ) ;
%South
dvec2=1/(3 dxs i ( 2 , 2) ) (8mat ( end , : ) +9mat ( end 1 , : )mat
( end 2 , : ) ) ; %North
28 cas e 2
dvec1 =1/(3 dxs i ( 2 , 2) ) (8mat ( : , 1 ) +9mat ( : , 2 )mat ( : , 3 ) ) ;
%West
46
dvec2=1/(3 dxs i ( 2 , 2) ) (8mat ( : , end)+9mat ( : , end1)mat
( : , end2) ) ; %East
end
47
A.10. Function to Determine Boundary Values by 3-point one-sided
scheme on an even Grid
f unct i on [ dvec1 , dvec2]=tp1s_eg ( mat , pos , dim)
%Three poi nt ones i ded sheme on a eveng r i d s paci ng .
4 %Find d e r i v a t i v e of mat v al ue s gi ven on pos p o s i t i o ns on t he
border .
% dim det ermi nes whi ch borders are l ooked up : i f dim=1 dQ/dy
on
%nort h / s out h f ac e s are f ound . I f dim=2 dQ/dx i s f ound on East /
West f ac e s .
%Ret urns two v e c t or s :
9 % dvec1 bei ng s out h ( row vect or s i z e (1 , nx ) ) or west ( coloumn
vect or s i z e ( ny , 1) ) ;
% dvec2 bei ng nort h ( row vect or s i z e (1 , nx ) ) or e as t ( coloumn
vect or s i z e ( ny , 1) )
dxs i=d i f f ( pos , [ ] , dim) ;
14 %Test f or even g r i d :
i f min( dxs i )==0
e r r or ( Wrong di mensi on ! dxs i =0 )
end
i f dxs i ( 1 , 1)~=dxs i ( 2 , 2)
19 e r r or ( Grid NOT EVEN! Do not use TP1S_EG )
end
s wi t ch dim
cas e 1
24 dvec1 =1/(6 dxs i ( 1 , 1) ) (9mat ( 1 , : ) +12mat ( 2 , : ) 3mat
( 3 , : ) ) ; %South
dvec2=1/(6 dxs i ( 1 , 1) ) (9mat ( end , : ) +12mat ( end 1 , : ) 3
mat ( end 2 , : ) ) ; %North
cas e 2
dvec1 =1/(6 dxs i ( 1 , 1) ) (9mat ( : , 1 ) +12mat ( : , 2 ) 3mat
( : , 3 ) ) ; %West
29 dvec2=1/(6 dxs i ( 1 , 1) ) (9mat ( : , end) +12mat ( : , end1)3
mat ( : , end2) ) ; %East
end
48
A.11. Determines H
x
and H
y
f unct i on [UG,VG]=FindH1H2(UG,VG, Re) ;
%Computes t he Hx and Hy f unc t i ons needed f or t he sourcet erm of
our Poi sson
%Equat i on .
5 %Find Hx :
UG. H1=1/Re(UG. uxx+UG. uyy ) (UG. uux+UG. uvy ) ;
%Find Hy:
VG. H2=1/Re(VG. vxx+VG. vyy ) (VG. vvy+VG. uvx ) ;
49
A.12. Preprocessor for Poisson solver (Setting up and LU-factorizing
Coecient Matrix).
1 f unct i on PS=Poi s s onSol ver Pr epr oces s or (PG)
% A pr epr oces s i ng f unct i on f or t he Poi sson Sol ver whi ch can be
used when
% t he boundary c ondi t i ons of t he probl em does not change wi t h
t i me .
% Main t as k i s t o s e t up t he Amat ri x and carry out t he LU
f a c t o r i z a t i o n .
% Ret urns a s t r uc t PS t hat has t o be passed as a paramet er t o
Poi ssonSol verConst ant BC
6
%Def i ne t he names of t he f ac e s and CVs
f a c e s= nsew ;
CVs=NSEWP ;
11 %Grid s paci ng
dx=PG. dx ;
dy=PG. dy ;
% dxs i de f i ne s t he c e l l l e ng t h s i n t he appr opr i at e coord s ys .
El ement no .
16 %are c o ns i s t e nt wi t h f aces .
dxs i ( 1)=dy ;
dxs i ( 2)=dy ;
dxs i ( 3)=dx ;
dxs i ( 4)=dx ;
21
% phi de f i ne s t he normal v e c t or s . El ement numbers of phi are
c o ns i s t e nt wi t h
%t he order of t he f aces vect or :
phi ( 1) =1;
phi ( 2) =1;
26 phi ( 3) =1;
phi ( 4) =1;
nx=PG. nx ;
ny=PG. ny ;
31 N=nxny ;
%We f i nd i t us e f ul t o save t he i ndi c e s t hat poi nt t o t he domain
boundari es
%i n v a r i a b l e s :
50
36 %nort h
i nx_i {1}=ny ;
i nx_j {1}=1: nx ;
%s out h
i nx_i {2}=1;
41 i nx_j {2}=1: nx ;
%e as t
i nx_i {3}=1: ny ;
i nx_j {3}=nx ;
%west
46 i nx_i {4}=1: ny ;
i nx_j {4}=1;
%Creat e g r i d of c o e f f i c i e n t s :
%el ement numbers are c o ns i s t e nt wi t h CVs vect or so t hat a{1}=
aN
51 %a{2}=aS . . .
a{1}=s par s e ( ny , nx) ;
a{2}=s par s e ( ny , nx) ;
a{3}=s par s e ( ny , nx) ;
56 a{4}=s par s e ( ny , nx) ;
a{5}=s par s e ( ny , nx) ;
%Di f f us i v e f l u x e s i n 2D:
D{1}=dx/dyones ( ny , nx) ;
61 D{2}=dx/dyones ( ny , nx) ;
D{3}=dy/dxones ( ny , nx) ;
D{4}=dy/dxones ( ny , nx) ;
%Fi l l t he i nt e r na l poi nt s :
66 f o r f ac e =1: l engt h ( f a c e s )
a{ f ac e}=D{ f ac e };
%Center coef :
a{5}=a{5}a{ f ac e };
end
71 %Correct t he domain boundary c ondi t i ons :
f o r f ac e =1: l engt h ( f a c e s )
%Find t he rows and col umns t hat poi nt t o t he domain boundary
rows=i nx_i { f ac e };
c o l s=i nx_j { f ac e };
76
%Al ways Neumann BC
51
a {5}( rows , c o l s )=a {5}( rows , c o l s )+a{ f ac e }( rows , c o l s ) ;
a_or i gi nal { f ac e }( rows , c o l s )=a{ f ac e }( rows , c o l s ) ;
a{ f ac e }( rows , c o l s ) =0;
81 end
%Rename t he c o e f f i c i e n t mat ri xes f or t rans parency
aN=a {1};
aS=a {2};
86 aE=a {3};
aW=a {4};
aP=a {5};
%%%%%%%%%%%%%%% CONSTRUCT LHS OF LINEAR SYS OF EQS.
%%%%%%%%%%%%%%%%%%
91 coef mat =[ [aW( ny+1: end) zer os ( 1 , ny) ] [ aS ( 2 : end) 0] aP( : ) [ 0 ; aN
( 1 : end1) ] [ zer os ( ny , 1 ) ; aE( 1 : endny) ] ] ;
di agvec=[ny 1 0 1 ny ] ;
A=s pdi ags ( coef mat , di agvec , N, N) ;
%%%%%%%%%%%%%%%% SOLVE LINEAR SYS OF EQS.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
96 %Find P us i ng Matl ab Sol ver .
P=zer os ( ny , nx) ;
%LUf a c t o r i z a t i o n :
[ L, U]=l u (A) ;
101
% Savi ng v a r i a b l e s i nt o s t r uc t
PS. i nx_i=i nx_i ;
PS. i nx_j=i nx_j ;
PS. L=L;
106 PS. U=U;
PS. f a c e s=f a c e s ;
PS. phi=phi ;
PS. dxs i=dxs i ;
PS. a_or i gi nal=a_or i gi nal ;
111 PS. nx=nx ;
PS. ny=ny ;
PS. dx=dx ;
PS. dy=dy ;
PS. A=A;
52
A.13. Poisson Solver for Constant Coecient Matrix
f unct i on P=Poi ssonSol verConstantBC(UG, VG, PS)
3 % 2D Fi ni t e Volume Sol ver t hat can be used i f t he BCs are
i nv ar i ant i n
% t i me . Takes t he UGg r i d and t he VGgri d , and a s t r uc t cr eat ed
by
% Poi s s onSol verPreproces s or .
%Combine H1 and H2 wi t h domain boundari es :
8 H{1}=VG. H2(PS. i nx_i {1}+1,1+PS. i nx_j {1}) ;
H{2}=VG. H2(PS. i nx_i {2},1+PS. i nx_j {2}) ;
H{3}=UG. H1(PS. i nx_i {3}+1,PS. i nx_j {3}+1) ;
H{4}=UG. H1(PS. i nx_i {4}+1,PS. i nx_j {4}) ;
13 %Source term mat ri x f or a l l CVs :
Source=s par s e (PS. ny , PS. nx) ;
Source =(UG. H1( 2 : end 1 , 2: end)UG. H1( 2 : end 1 , 1: end1) ) PS. dy+(VG.
H2( 2 : end , 2 : end1)VG. H2( 1 : end 1 , 2: end1) ) PS. dx ;
%Correct sourcet erms at boundari es .
18 f o r f ac e =1: l engt h (PS. f a c e s )
rows=PS. i nx_i { f ac e };
c o l s=PS. i nx_j { f ac e };
Source ( rows , c o l s )=Source ( rows , c o l s )PS. a_or i gi nal { f ac e }( rows
, c o l s ) PS. phi ( f ac e ) PS. dxs i ( f ac e ) . H{ f ac e };
end
23
ST=Source ;
%%%% SOLVE LINEAR SYS OF EQS. %%%%
P=zer os (PS. ny , PS. nx) ;
28
% Sol vi ng based on LUf a c t o r i z a t i o n :
P( : ) = PS. U\(PS. L\ST( : ) ) ;
53
A.14. Validation Function for Velocity Derivatives
f unct i on Tes t Der i vat i ves ( )
%Funct i on t o v a l i d a t e our v e l o c i t y d e r i v a t i v e s :
c l o s e a l l
5 %Se t t i ng s :
nx=10
ny=10
Lx=1
Ly=1
10
%Creat e Gri ds
[ PG,UG,VG,UVG]=Gri dGenerator ( nx , ny , Lx , Ly) ;
DBC=SetDomai nBoundaryCondi ti ons ( ) ;
15 %Def i ne v e l o c i t y t e s t f unc t i ons .
Ufunc=@( x , y) (16x.^232x.^3+16x . ^4) . ( y+5y.^26y . ^3)
Vfunc=@( x , y) (16y.^232y.^3+16y . ^4) . ( x+5x.^26x . ^3)
UG. u=Ufunc (UG. X,UG. Y) ;
20 VG. v=Vfunc (VG. X,VG. Y) ;
%Find d e r i v a t i v e s us i ng our approxi mat i ons .
[ PG,UG,VG,UVG]=Fi ndVel oci t yDer i vat i ves (PG,UG,VG,UVG,DBC) ;
25 %Anal y t hi c al Ve l oc i t y d e r i v a t i v e s :
UXfunc=@( x , y) (32x96x.^2+64x . ^3) . ( y+5y.^26y . ^3)
VYfunc=@( x , y) (32y96y.^2+64y . ^3) . ( x+5x.^26x . ^3)
UYfunc=@( x , y) (16x.^232x.^3+16x . ^4) . ( 1+10. y18.y . ^2)
VXfunc=@( x , y) (16y.^232y.^3+16y . ^4) . ( 1+10. x18.x . ^2)
30 UXXfunc=@( x , y) (32192x+192x . ^2) . ( y+5.y. ^2 6. y . ^3)
VYYfunc=@( x , y) (32192y+192y . ^2) . ( x+5.x. ^2 6. x . ^3)
UYYfunc=@( x , y) (16x.^232x.^3+16x . ^4) . (10 36y)
VXXfunc=@( x , y) (16y.^232y.^3+16y . ^4) . (10 36x)
UUXfunc=@( x , y) 2(16x.^232x.^3+16x . ^4) . ( y+5y.^26y . ^3)
. ^2. ( 32 x96x.^2+64x . ^3) ;
35 VVYfunc=@( x , y) 2(16y.^232y.^3+16y . ^4) . ( x+5x.^26x . ^3)
. ^2. ( 32 y96y.^2+64y . ^3) ;
UVXfunc=@( x , y) (16y.^232y.^3+16y . ^4) . (1+10x18x . ^2) . ( 16
x.^232x.^3+16x . ^4) . ( y+5y.^26y . ^3) +(16y.^232y
.^3+16y . ^4) . ( x+5x.^26x . ^3) . ( 32 x96x.^2+64x . ^3) . ( y
+5y.^26y . ^3) ;
54
UVYfunc=@( x , y) (32y96y.^2+64y . ^3) . ( x+5x.^26x . ^3) . ( 16 x
.^232x.^3+16x . ^4) . ( y+5y.^26y . ^3) +(16y.^232y.^3+16
y . ^4) . ( x+5x.^26x . ^3) . ( 16 x.^232x.^3+16x . ^4) . (1+10y
18y . ^2) ;
%Cal cul at e t he er r or s :
40 t t l {1}= u t e s t v e l o c i t y ;
xvar {1}=UG. X;
yvar {1}=UG. Y;
zvar {1}=Ufunc (UG. X,UG. Y) ;
45 t t l {2}= v t e s t v e l o c i t y ;
xvar {2}=VG. X;
yvar {2}=VG. Y;
zvar {2}=Vfunc (VG. X,VG. Y) ;
50 t t l {3}= ux e r r or ;
xvar {3}=PG. X;
yvar {3}=PG. Y;
errorUX=(PG. uxUXfunc (PG. X,PG.Y) ) /max(max( abs ( UXfunc (PG. X,PG. Y) )
) ) ;
zvar {3}=errorUX ;
55
t t l {4}= vy e r r or ;
xvar {4}=PG. X;
yvar {4}=PG. Y;
errorVY=(PG. vyVYfunc (PG. X,PG.Y) ) /max(max( abs ( VYfunc (PG. X,PG. Y) )
) ) ;
60 zvar {4}=errorVY ;
errorUY=(UVG. uyUYfunc (UVG. X,UVG. Y) ) /max(max( abs ( UYfunc (UVG. X,
UVG. Y) ) ) ) ;
t t l {5}= uy e r r or ;
xvar {5}=UVG. X;
65 yvar {5}=UVG. Y;
zvar {5}=errorUY ;
errorVX=(UVG. vxVXfunc (UVG. X,UVG. Y) ) /max(max( abs ( VXfunc (UVG. X,
UVG. Y) ) ) ) ;
t t l {6}= vx e r r or ;
70 xvar {6}=UVG. X;
yvar {6}=UVG. Y;
zvar {6}=errorVX ;
55
errorUXX=(UG. uxxUXXfunc(UG. X,UG. Y) ) /max(max( abs ( UXXfunc(UG. X,UG
. Y) ) ) ) ;
75 t t l {7}= uxx e r r or ;
xvar {7}=UG. X;
yvar {7}=UG. Y;
zvar {7}=errorUXX ;
80 errorVYY=(VG. vyyVYYfunc(VG. X,VG. Y) ) /max(max( abs ( VYYfunc(VG. X,VG
. Y) ) ) ) ;
t t l {8}= vyy e r r or ;
xvar {8}=VG. X;
yvar {8}=VG. Y;
zvar {8}=errorVYY ;
85
errorUYY=(UG. uyyUYYfunc(UG. X,UG. Y) ) /max(max( abs ( UYYfunc(UG. X,UG
. Y) ) ) ) ;
t t l {9}= uyy e r r or ;
xvar {9}=UG. X;
yvar {9}=UG. Y;
90 zvar {9}=errorUYY ;
errorVXX=(VG. vxxVXXfunc(VG. X,VG. Y) ) /max(max( abs ( VXXfunc(VG. X,VG
. Y) ) ) ) ;
t t l {10}= vxx e r r or ;
xvar {10}=VG. X;
95 yvar {10}=VG. Y;
zvar {10}=errorVXX ;
errorUV=(UVG. uv(Ufunc (UVG. X,UVG. Y) . Vfunc (UVG. X,UVG. Y) ) ) /max(
max( abs ( ( Ufunc (UVG. X,UVG. Y) . Vfunc (UVG. X,UVG. Y) ) ) ) ) ;
t t l {11}= uv e r r or ;
100 xvar {11}=UVG. X;
yvar {11}=UVG. Y;
zvar {11}=errorUV ;
errorUUX=(UG. uuxUUXfunc(UG. X,UG. Y) ) /max(max( abs ( UUXfunc(UG. X,UG
. Y) ) ) ) ;
105 t t l {12}= uux e r r or ;
xvar {12}=UG. X;
yvar {12}=UG. Y;
zvar {12}=errorUUX ;
110 errorVVY=(VG. vvyVVYfunc(VG. X,VG. Y) ) /max(max( abs ( VVYfunc(VG. X,VG
. Y) ) ) ) ;
56
t t l {13}= vvy e r r or ;
xvar {13}=VG. X;
yvar {13}=VG. Y;
zvar {13}=errorVVY ;
115
errorUVX=(VG. uvxUVXfunc(VG. X,VG. Y) ) /max(max( abs ( UVXfunc(VG. X,VG
. Y) ) ) ) ;
t t l {14}= uvx e r r or ;
xvar {14}=VG. X;
yvar {14}=VG. Y;
120 zvar {14}=errorUVX ;
errorUVY=(UG. uvyUVYfunc(UG. X,UG. Y) ) /max(max( abs ( UVYfunc(UG. X,UG
. Y) ) ) ) ;
t t l {15}= uvy e r r or ;
xvar {15}=UG. X;
125 yvar {15}=UG. Y;
zvar {15}=errorUVY ;
%And p l o t t he er r or s :
f o r i =1: l engt h ( t t l )
130 x=xvar { i };
y=yvar { i };
z=zvar { i };
[ f i g1 , axes1]=pl ot _cr eat or ( t t l ( i ) , $x$ , $y$ )
135 meshc ( axes1 , x , y , z )
vi ew( axes1 , [ 37. 5 30] ) ;
gr i d ( axes1 , on ) ;
pl ot _l at ex ( f i g1 , ha l f )
end
57
A.15. Validation Function for Pressure
1 f unct i on Tes t Pr es s ur e ( )
%Funct i on f or v a l i d a t i n g our Poi sson s o l v e r :
%Se t t i ng s
nx=10
6 ny=10
Lx=1
Ly=1
%Creat e g r i d s :
11 [ PG,UG,VG,UVG]=Gri dGenerator ( nx , ny , Lx , Ly) ;
DBC=SetDomai nBoundaryCondi ti ons ( ) ;
%Def i ne a n a l y t i c a l t e s t f unct i on .
k=2pi ;
16 UG. H1=k s i n ( kUG. X) . cos ( kUG. Y) ;
VG. H2=k cos ( kVG. X) . s i n ( kVG. Y) ;
%Sol ve f or t he pr es s ur e .
P=Poi s s onSol ver (PG,UG,VG) ;
21 P=f u l l (Pmean(mean(P) ) ) ;
s i z e (P)
%And t he a n a l y t i c a l s o l ut i o n t o t he Poi sson Eq .
Pfunc=@( x , y) ( cos ( kx) . cos ( ky) ) ;
26
%Pl ot t he er r or :
[ f i g1 , axes1]=pl ot _cr eat or ( Test Exact Pr es s ur e , $x$ , $y$ )
meshc ( axes1 ,PG. X,PG. Y, Pfunc (PG. X,PG. Y) )
z l a be l = $P$ ;
31 vi ew( axes1 , [ 37. 5 30] ) ;
gr i d ( axes1 , on ) ;
pl ot _l at ex ( f i g1 , ha l f )
[ f i g2 , axes2]=pl ot _cr eat or ( Test Cal cul at ed Pr es s ur e , $x$ , $y$
)
36 meshc ( axes2 ,PG. X( 2 : end 1 , 2: end1) ,PG. Y( 2 : end 1 , 2: end1) ,P)
z l a be l = $P$ ;
vi ew( axes2 , [ 37. 5 30] ) ;
gr i d ( axes2 , on ) ;
pl ot _l at ex ( f i g2 , ha l f )
41
58
[ f i g3 , axes3]=pl ot _cr eat or ( Test Pr es s ur e Error , $x$ , $y$ )
meshc ( axes3 ,PG. X( 2 : end 1 , 2: end1) ,PG. Y( 2 : end 1 , 2: end1) , (PPfunc
(PG. X( 2 : end 1 , 2: end1) ,PG. Y( 2 : end 1 , 2: end1) ) ) /max(max(P) ) )
z l a be l = $\ var e ps i l on (P) $ ;
vi ew( axes3 , [ 37. 5 30] ) ;
46 gr i d ( axes3 , on ) ;
pl ot _l at ex ( f i g3 , f u l l )
59
A.16. Plotting Function for Solution
f unct i on Pl ot Sol ut i on ( Sol ut i on )
%Pl ot s t he s t r e aml i ne s and pr es s ur e d i s t r i b u t i o n s of a gi ven
s o l ut i o n . I f
3 %s o l ut i o n cont ai ns more t i mes t eps a movie wi l l be pl ayed .
c l o s e a l l
%Se l e c t v a r i a b l e s .
PG=Sol ut i on ( end) .PG
8 UG=Sol ut i on ( end) .UG
VG=Sol ut i on ( end) .VG
UVG=Sol ut i on ( end) .UVG
Cons=cel l 2mat ({ Sol ut i on ( : ) . Cons }) ;
t=cel l 2mat ({ Sol ut i on ( : ) . t }) ;
13 dt=Sol ut i on ( end) . dt ;
%Make p l o t s of f i n a l s i t ua t i o n .
[ f i gur e1 , axes1]=pl ot _cr eat or ( [ St r eaml i nes of s o l ut i o n Re_
num2str ( Sol ut i on ( end) . Re) ] , $x$ , $y$ ) ;
s t r e a ms l i c e ( axes1 ,UVG. X,UVG. Y, [ ] , UVG. u ,UVG. v , [ ] , [ ] , [ ] , [ ] , 2 ,
arrowmode , cubi c )
18 axi s equal
axi s ( [ 0 1 0 1 ] )
[ f i gur e2 , axes2]=pl ot _cr eat or ( [ Pr es s ur e at Re_ num2str ( Sol ut i on
( end) . Re) ] , $x$ , $y$ ) ;
meshc ( axes2 ,PG. X,PG. Y,PG. P)
23
[ f i gur e4 , axes4]=pl ot _cr eat or ( [ Conservati on of Mass Re_ num2str
( Sol ut i on ( end) . Re) ] , Timestep , norm( $u_x$+$v_y$) ) ;
plot_append ( axes4 , t /dt , Cons , [ Mass Conservati on ] , 1 , 0 , 0 , 0 ,
0 , l i ne a r ) ;
%Make movie !
28 [ f i gur e5 , axes5]=pl ot _cr eat or ( [ St r eaml i nes of s o l ut i o n Re_
num2str ( Sol ut i on ( end) . Re) ] , $x$ , $y$ ) ;
f o r i =1: l engt h ( Sol ut i on )
c l a
s e t ( f i gur e5 , name , [ Streaml i nes_evol uti on_i n_ti me_
num2str ( Sol ut i on ( i ) . t ) ] )
33 s ur f ( Sol ut i on ( i ) .UVG. X, Sol ut i on ( i ) .UVG. Y,s qr t ( Sol ut i on (
i ) .UVG. u.^2+Sol ut i on ( i ) .UVG. v . ^2) )
60
shadi ng i nt e r p
%col or bar 1 = c ol or b ar ( peer , axes5 , . . .
% YTick , [ 0. 9 0.8 0.7 0.6 0.5 0.4 0.3 0.2
0. 1] , . . .
% YTi ckLabel
, { 0 . 9 , 0 . 8 , 0 . 7 , 0 . 6 , 0 . 5 , 0 . 4 , 0 . 3 , 0 . 2 , 0 . 1 } )
;
38 hol d on
s t r e a ms l i c e ( axes5 , Sol ut i on ( i ) .UVG. X, Sol ut i on ( i ) .UVG. Y
, [ ] , Sol ut i on ( i ) .UVG. u , Sol ut i on ( i ) .UVG. v
, [ ] , [ ] , [ ] , [ ] , 2 , arrowmode , cubi c )
xl abe l ( [ Time= num2str ( Sol ut i on ( i ) . t ) ] )
axi s equal
43 axi s ( [ 0 1 0 1 ] )
% pl ot _l at e x ( f i gur e5 , hal f )
pause ( 0 . 5 )
end
61
A.17. Compare With Benchmark Results
f unct i on CompareWithBenchmark( Sol vec )
%Take a vect or of Sol ut i on s t r u c t s and compare wi t h benchmark
r e s u l t s ( eg . f or d i f f e r e n t g r i d s i z e s ) .
c l o s e a l l
4
Benchmark =[ 1. 0000 1.0000 0. 052971 0. 0000 0. 00000
0. 077429
0. 9688 0.58031 0. 051493 0. 0391 0.29330 0. 078658
0. 9531 0.47239 0. 050314 0. 0547 0.41018
0. 077128
0. 7344 0.18861 0. 012113 0. 1406 0.42634 0. 049004
9 0. 5000 0. 06205 0. 0000 0. 5000 0. 02580 0. 00000
0. 2813 0. 28040 0. 040381 0. 7734 0. 33398 0. 047259
0. 1016 0. 30029 0. 104416 0. 9062 0. 33290 0. 084369
0. 0625 0. 20227 0. 10916 0. 9297 0. 29627 0. 087625
0. 0000 0. 0000 0. 11056 1. 000 0. 0000
0 . 0 9 0 4 4 8 ] ;
14
%Do not cons i der t he boundari es :
Benchmark ( [ 1 end ] , : ) =[ ] ;
%Save benchmark as v e c t or s
19 y_bench=Benchmark ( : , 1 ) ;
u_bench=Benchmark ( : , 2 ) ;
p_bench_y=Benchmark ( : , 3 ) ;
x_bench=Benchmark ( : , 4 ) ;
v_bench=Benchmark ( : , 5 ) ;
24 p_bench_x=Benchmark ( : , 6 ) ;
%Creat e f i g u r e s
[ f i gur e3a , axes3a ]=pl ot _cr eat or ( [ U v e l o c i t y d i f f e r e n t n ] , $y$ ,
$u$ ) ;
plot_append ( axes3a , y_bench , u_bench , [ Ref er ence $u$ ] , 1 , 0 , 0 ,
0 , 0 , l i ne a r ) ;
29
[ f i gur e3b , axes3b]=pl ot _cr eat or ( [ V v e l o c i t y d i f f e r e n t n ] , $x$ ,
$v$ ) ;
plot_append ( axes3b , x_bench , v_bench , [ Ref er ence $v$ ] , 1 , 0 , 0 ,
0 , 0 , l i ne a r ) ;
[ f i gur e 3c , axes3c ]=pl ot _cr eat or ( [ P al ong x at d i f f e r e n t n ] , $x$
, $P$ ) ;
62
34 plot_append ( axes3c , x_bench , p_bench_x , [ Ref er ence $P( x) $ ] , 1 , 0 ,
0 , 0 , 0 , l i ne a r ) ;
[ f i gur e3d , axes3d]=pl ot _cr eat or ( [ P al ong y at d i f f e r e n t n ] , $y$
, $P$ ) ;
plot_append ( axes3d , y_bench , p_bench_y , [ Ref er ence $P( y) $ ] , 1 , 0 ,
0 , 0 , 0 , l i ne a r ) ;
39 %For a l l s o l ut i o ns i n Sol vec vect or :
f o r k=1: l engt h ( Sol vec )
Sol ut i on=Sol vec ( k) . Sol ut i on ;
%Pi ck out r e s u l t s :
UG=Sol ut i on ( end) .UG;
44 VG=Sol ut i on ( end) .VG;
UVG=Sol ut i on ( end) .UVG;
PG=Sol ut i on ( end) .PG;
n_vec ( k)=Sol ut i on ( end) .PG. nx ;
49 %i n t e r p o l a t e t o benchmarks :
u_cntr ( : , k)=i nt er p2 (UG. X,UG. Y,UG. u , 0 . 5 ones ( s i z e ( Benchmark
, 1 ) , 1) , Benchmark ( : , 1 ) )
plot_append ( axes3a , Benchmark ( : , 1 ) , u_cntr ( : , k) , [ $u$ , n=
num2str ( n_vec ( k) ) ] , 1 , 0 , 0 , 0 , 0 , l i ne a r ) ;
v_cntr ( : , k)=i nt er p2 (VG. X,VG. Y,VG. v , Benchmark ( : , 4 ) , 0. 5 ones (
s i z e ( Benchmark , 1 ) , 1) )
54 plot_append ( axes3b , Benchmark ( : , 4 ) , v_cntr ( : , k) , [ $v$ , n=
num2str ( n_vec ( k) ) ] , 1 , 0 , 0 , 0 , 0 , l i ne a r ) ;
p_cntr_y ( : , k)=i nt er p2 (PG. X,PG. Y,PG. P, 0 . 5 ones ( s i z e ( Benchmark
, 1 ) , 1) , Benchmark ( : , 1 ) ) ;
p_cntr_x ( : , k)=i nt er p2 (PG. X,PG. Y,PG. P, Benchmark ( : , 4 ) , 0. 5 ones
( s i z e ( Benchmark , 1 ) , 1) ) ;
p_cntr_x ( : , k)=p_cntr_x ( : , k)p_cntr_x ( 4 , k) ;
59 p_cntr_y ( : , k)=p_cntr_y ( : , k)p_cntr_y ( 4 , k) ;
%Pl ot our r e s u l t s :
plot_append ( axes3d , Benchmark ( : , 1 ) , p_cntr_y ( : , k)p_cntr_y ( 4 , k
) , [ $P( y) $ , n= num2str ( n_vec( k) ) ] , 1 , 0 , 0 , 0 , 0 ,
l i ne a r ) ;
plot_append ( axes3c , Benchmark ( : , 4 ) , p_cntr_x ( : , k)p_cntr_x ( 4 , k
) , [ $P( x) $ , n= num2str ( n_vec( k) ) ] , 1 , 0 , 0 , 0 , 0 ,
l i ne a r ) ;
64
63
%Compute er r or
error_u ( k)=norm( abs ( u_cntr ( : , k)u_bench) ) ;
error_v ( k)=norm( abs ( v_cntr ( : , k)v_bench) ) ;
error_p_x ( k)=norm( abs ( p_cntr_x ( : , k)p_bench_x) ) ;
69 error_p_y ( k)=norm( abs ( p_cntr_y ( : , k)p_bench_y) ) ;
end
%Make Convergence p l o t
74 sl ope_u=p o l y f i t ( l og ( n_vec ) , l og ( error_u ) , 1)
sl ope_v=p o l y f i t ( l og ( n_vec ) , l og ( error_v ) , 1)
slope_p_x=p o l y f i t ( l og ( n_vec ) , l og ( error_p_x ) , 1)
slope_p_y=p o l y f i t ( l og ( n_vec ) , l og ( error_p_y ) , 1)
79 [ f i gur e4 , axes4]=pl ot _cr eat or ( [ Convergence ] , $x$ , ) ;
plot_append ( axes4 , n_vec , error_u , [ $norm ( | uu_{ r e f } | ) $ , s l ope=
num2str ( sl ope_u ( 1) , 2) ] , 1 , 0 , 0 , 0 , 0 , l ogl og ) ;
plot_append ( axes4 , n_vec , error_v , [ $norm ( | vv_{ r e f } | ) $ , s l ope=
num2str ( sl ope_v ( 1) , 2) ] , 1 , 0 , 0 , 0 , 0 , l ogl og ) ;
plot_append ( axes4 , n_vec , error_p_x , [ $norm ( | P( x)P_{ r e f }( x) | ) $ ,
s l ope= num2str ( slope_p_x ( 1) , 2) ] , 1 , 0 , 0 , 0 , 0 , l ogl og ) ;
plot_append ( axes4 , n_vec , error_p_y , [ $norm ( | P( y)P( y)_{ r e f } | ) $ ,
s l ope= num2str ( slope_p_y ( 1) , 2) ] , 1 , 0 , 0 , 0 , 0 , l ogl og ) ;
84 s e t ( axes4 , XTick , [ 30 50 70] )
axi s ( axes4 , [ 2 9 . 4 9 71. 72 0. 00766 0 . 1 4 0 7 ] ) ;
%Save t he f i g u r e s .
pl ot _l at ex ( f i gur e3a , ha l f )
89 pl ot _l at ex ( f i gur e3b , ha l f )
pl ot _l at ex ( f i gur e 3c , ha l f )
pl ot _l at ex ( f i gur e3d , ha l f )
pl ot _l at ex ( f i gur e4 , f u l l )
64

You might also like