100% found this document useful (3 votes)
2K views53 pages

Fluid Mechanics 101: Fundamentals Course 3

This document introduces unstructured meshes for computational fluid dynamics (CFD) simulations. Unstructured meshes allow for irregular cell shapes and layouts, unlike structured meshes which have a regular grid structure. Unstructured CFD codes treat cells, faces, and nodes as separate objects rather than whole cells. This requires rewriting CFD codes from a structured to an unstructured formulation using object-oriented programming. The document provides an overview of unstructured mesh concepts and finite volume discretization for the heat diffusion equation to demonstrate the unstructured approach.

Uploaded by

Raquel Velasco
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (3 votes)
2K views53 pages

Fluid Mechanics 101: Fundamentals Course 3

This document introduces unstructured meshes for computational fluid dynamics (CFD) simulations. Unstructured meshes allow for irregular cell shapes and layouts, unlike structured meshes which have a regular grid structure. Unstructured CFD codes treat cells, faces, and nodes as separate objects rather than whole cells. This requires rewriting CFD codes from a structured to an unstructured formulation using object-oriented programming. The document provides an overview of unstructured mesh concepts and finite volume discretization for the heat diffusion equation to demonstrate the unstructured approach.

Uploaded by

Raquel Velasco
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 53

Fluid Mechanics 101

Fundamentals Course 3

Dr. Aidan Wimshurst


Table of Contents

Foreward 4

How To Use This Course 5

Unstructured Meshes 6

Mesh Quality 38

2
Disclaimer

The exercises, software, code and equations in this course are for educational and demonstrative
purposes only. They should not be used to analyse, design, test, accredit or validate real
scientific/engineering/mathematical structures and flow systems. For such applications,
appropriate trained, qualified and accredited engineers/scientists should be consulted. Fluid
Mechanics 101 and Dr. Aidan Wimshurst are not accountable or liable in any form for the
use or misuse of the information contained in this course beyond the specific educational and
demonstrative purpose for which it was intended.
Foreward

Welcome to Part 3 of my Computational Fluid Dynamics Fundamentals Course! This course


is an extension of Part 1 and Part 2 and uses the same foundational ideas and approach. In
Part 1 and Part 2, the CFD solution was generated using entirely structured meshes of
quadrilateral cells. However, in the majority of modern CFD codes, the CFD code is
formulated for unstructured meshes, with cells of any shape and layout. The main advantage
of the unstructured approach is that there is no restriction placed on the mesh that can be
generated by the user. The user is then free to generate an appropriate mesh for their problem,
with no restrictions placed on the layout and cell type. However, unstructured CFD codes are
considerably more complicated to assemble and are often written using an object-oriented
programming (OOP) approach.

In Chapter 1 of this course, the unstructured approach to CFD codes is explained in detail.
This requires the nodes, faces and cells in the mesh to be treated as seperate entities and
requires the CFD codes that were created in Part 1 and Part 2 to be completely rewritten.
To finish the Chapter, the example problem from Part 2 of this course is revisited and solved
using an unstructured approach, rather than a structured approach. The solution generated
with the unstructured approach is shown to be identical to the structured approach.

The majority of real CFD codes contain a variety of cell shapes and sizes. Some of these cells
may be of poor quality, which can lead to numerical errors and even divergence of the CFD
solver in some cases. To help the user, CFD codes and mesh generators use a variety of
quality metrics to help identify the bad cells. However it is often not clear what these metrics
actually mean and how they are calculated. In Chapter 2 of this course, the main quality
metrics are introduced and explained in detail. The interactive exercise then allows these
metrics to be observed as the mesh changes.

I am really excited to bring you Part 3 of this course and know that you will find it as useful
as I have. The interactive exercises may even answer some long standing technical questions
that you never found the answer to!

All the best


Aidan

4
How To Use This Course

This course contains a comprehensive set of equations, explantations and diagrams, which are
all contained in this PDF book. As you proceed through Chapter 1 and 2, worked examples
are provided. When instructed, it is advied that you open the example code (either in
Microsoft Excel or in a suitable text editor/graphical user interface (GUI) for Python). Here
you will be able to examine the code, modify the input variables and run the CFD simuations
yourself. This is where the majority of the learning is likely to take place and is highly
encouraged for all readers.

All of the exercises can be completed using either the Excel or Python scripts. Use
whichever approach is more appealing and straightforward for you to follow. Alternatively,
you can use the equations that are provided in the text to write your own code/scripts to
solve the equations! The aim of this course is not to develop knowledge of a specific language
or CFD code, but to learn and observe the overall process. Hence, it is highly encouraged for
you to take your preferred approach.

0 1 1 0 0 1 0 0 0 1 1 0 1 0 0 1 0

0 0 0 1 1 0 0 1 0 0 0 1 1 0 1 0 0 1 0

0 0 1 1 0 1 0 1 0 1 0 1 0 0 1 1 0 0 1

1 1 1 1 1 0 1 1 1 1 1 0 0 0 1 1 1 0 1 1

1 0 0 0 0 1 1 0 0 1 0 0 0 1 1 0 1 0 0 1 0

1 1 0 0 1 1 0 1 0 1 0 1 0 1 0 0 1 1 0 0 1

0 1 1 1 1 1 0 1 1 1 1 1 0 0 0 1 1 1 0 1 1

1 1 0 0 1 1 0 1 0 1 0 1 0 1 0 0 1 1 0 0 1

0 1 1 1 1 1 0 1 1 1 1 1 0 0 0 1 1 1 0 1 1

0 0 0 1 1 0 0 1 0 0 0 1 1 0 1 0 0 1 0

0 0 1 1 0 1 0 1 0 1 0 1 0 0 1 1 0 0 1

1 1 1 0 1 1 1 1 1 0 0 0 1 1 1 0 1 1

5
Chapter 1
Unstructured Meshes

Dr. Aidan Wimshurst


1 Unstructured Meshes
In Part 1 and Part 2 of this course, the geometry was decomposed into a structured mesh of cells
and the CFD solution was computed on this structured mesh. Both 1D and 2D structured meshes
were used for the various example problems. Structured meshes use a regular pattern of cells which all
have the same shape (all quadrilaterals or cuboids for example). As the cells have a regular pattern,
they cant be identified easily with an i, j, k index numbering scheme. Figure 1 shows an example of
a 2D structured mesh, along with the i, j numbering system that can be used to identify a given cell
in the mesh.
Structured meshes are often composed of rectangles and hexahedra. However, it should be
emphasised that it is the regular pattern and numbering system that defines a structured mesh, not
the shape of the elements. It is possible to have a structured mesh composed of triangles or tetrahedra
(even though these meshes are unusual).
The majority of modern CFD codes use an unstructured formulation (rather than a structured
formulation) because it allows the code to handle different cell types. A mixture of triangles, rect-
angles, tetrahedra, prisms and hexahedra can all be used in an unstructured mesh. This means that
no restriction is placed on the mesh that is generated by the user. As long as the mesh has sufficient
quality (this will be examined in Chapter 2), then any type and layout of cells can be used. This
allows the user to generate an appropriate mesh for whatever flow scenario the user is interested in.
The main difference between structured and unstructured meshes is that the CFD code must be
written and constructed in a different way. The construction of the CFD code in an unstructured
format will be the focus of this Chapter. It will be shown that if the mesh and node numbering
scheme are identical, then the code will result in the same set of equations, regardless of whether the
code uses a structured or unstructured formulation.

Cells, Faces and Nodes


Unstructured CFD codes handle cells, faces and nodes (Figure 2) as separate entities. An un-
structured mesh is therefore a collection of cells, faces and nodes that are connected together in an
arbitrary manner. This is different to a structured formulation, where cells are handled as complete
units and are defined by their i, j, k index. For example, the example mesh in Figure 1 could be
treated as a structured mesh or an unstructured mesh, depending on how the CFD code is written.
As cells, faces and nodes are considered as separate entities in an unstructured mesh, the CFD
code is considerably longer and more complicated to assemble than a structured CFD code. However,
the main advantage offered by this approach is that the CFD code can handle any mesh, as long as
the cell quality is sufficiently high.
The most straightforward approach to constructing an unstructured CFD code is to use an object-
oriented programming (OOP) approach. Object-oriented programming is offered by the majority of
modern coding and scripting languages and will be used in the python example later in this Chapter.

Finite Volume Discretisation


In the same manner as the previous course, the finite volume discretisation of the 2D heat diffusion
equation will be used as an example to illustrate how a CFD solution can be constructed on an
unstructured mesh. The 2D heat diffusion equation is the simplest scalar transport equation that is
solved by CFD codes and is sufficient to demonstrate the principles required for unstructured meshes.

Dr. Aidan Wimshurst, Fluid Mechanics 101 7


Figure 1: An example of a structured mesh where the i, j index notation can be used to identify
any cell in the mesh.

(a) Cell (b) Face (c) Node

Figure 2: Cells, faces and nodes are treated as seperate entities in unstructured CFD codes.

For simplicity, the convection term and the temporal derivative will not be considered and the equation
will be solved in 2D rather than 3D.
In the previous course, the cell neighbours were referred to as ’left’, ’right’, ’top’ and ’bottom’, as
the mesh was structured and all cells had 4 neighbours which were aligned with the coordinate axis.
It is not possible to use this approach with an unstructured formulation, as it cannot be specified
explicitly how many neighbours each cell has and which direction they will be in. The approach
needs to be more general. In this Chapter, an alternative formulation of the discretised heat diffusion
equation will be presented, which is applicable to all unstructured meshes.
Start with the vector form of the heat diffusion equation:

0 = ∇ · (k∇T ) + S (1)

where T is the static temperature, k is the thermal conductivity and S is a heat source per unit
volume (with units of [W/m3 ]). Note that ∇· is the divergence operator, which can be written in 2D
Cartesian coordinates (x, y) as: !
∂ ∂
∇= , (2)
∂x ∂y
The first step in the finite volume method is to integrate the equation over a finite-sized control
volume (cell) with volume V . Z
0= [∇ · (k∇T ) + S] dV (3)
V
Separate the integral into two separate integrals: one containing the diffusion term and the other
containing the source term. This is permissible as integration and addition are commutative operations

Dr. Aidan Wimshurst, Fluid Mechanics 101 8


Integral is the same, if we take
the centroid value!

Node Centroid Node

Figure 3: An example of the linear variation of the source term across a control volume.

Accumulation in the volume


Flux out of volume Flux out of volume

Figure 4: A diagram to show the physical significance of the divergence theorem applied to the
vector field B. The accumulation of B in the volume equals the flux of B across the faces of the
volume.

(they can be performed in any order without changing the result).


Z Z
0= [∇ · (k∇T )] dV + [S] dV (4)
V V

In the finite volume method, all quantities vary linearly across the cell. As shown in Figure 3, the
integral of the source term across the cell volume can therefore be replaced by the value at the cell
centroid (Sp ), multiplied by the volume of the cell (Vp ).
Z
[S] dV = Sp Vp (5)
V
Z
0= [∇ · (k∇T )] dV + Sp Vp (6)
V
In this course, the subscript p will be used to refer specifically to the value at the cell centroid, to
distinguish the term from other locations in the mesh (such as the value at the face centre).
The volume integral of the diffusion term can be simplified using Gauss’s divergence theorem.
Recall (from the previous course) that the divergence theorem for a general vector field B is:
Z Z
(∇ · B) dV = (B · n̂) dA (7)
V A

where n̂ is the unit normal vector pointing out of the control volume and A is the surface area of
the control volume. Physically, the divergence theorem states that the rate of accumulation of a
quantity B inside a control volume must be equal to the flux of B across the surface of the volume,
regardless of what the quantity B is. A diagram to illustrate the physical meaning of the divergence
theorem is shown in Figure 4.
To apply the divergence theorem to the heat diffusion equation, let B = k∇T . The diffusion
term can then be simplified using the divergence theorem:
Z
0= (k∇T · n̂) dA + Sp Vp (8)
A

Dr. Aidan Wimshurst, Fluid Mechanics 101 9


Integral is the same, if we take
the face centre value!

Node Face Centre Node

Figure 5: An example of the linear variation across the face of a cell.

This form of the heat diffusion equation is general for any volume, regardless of its shape. However,
CFD meshes are constructed from cells which have a finite number of flat faces (they are not smooth
continuous surfaces). The surface integral can therefore be split up into the surface integral over
each of the cell faces (regardless of how many faces the cell has).
X Z
0= (k∇T · n̂) dA + Sp Vp (9)
Faces A

This formulation is still general and can be applied to any type of unstructured mesh, as long as the
cells in the mesh have a finite number of flat faces.
In the finite volume method, the flow quantities all vary linearly across the cell faces, as shown
in Figure 5. The integral across the face can therefore be reduced to the value at the centre of the
face (a constant value). Z
X
0= [(kf (∇T )f · n̂f )] dA + Sp Vp (10)
Faces A

The subscript f has been used here to emphasise that these quantities are evaluated at the face
centre. This should avoid confusion with the quantities at the cell centroid, which are given the
subscript p. As all of the quantities have now been moved outside the surface integral, the integral
just evaluates to the area of the face, Af .
X
0= [kf Af ((∇T )f · n̂f )] + Sp Vp (11)
Faces

Equation 11 is the final form of the discretised heat diffusion equation. Unlike the formulation adopted
in the previous course, this formulation is not limited to a particular cell geometry (squares, triangles
or polygons) and it can therefore be applied to unstructured and structured meshes. Note that for
a structured mesh of 2D quadrilaterals, the equation reduces back to the equation adopted in the
previous course:
! ! ! !
∂T ∂T ∂T ∂T
0 = kA − kA + kA − kA + Sp Vp (12)
∂x Right
∂x Left
∂y Top
∂y Bottom

Hence, equation 11 is actually the more generalised form of the discretised form of the heat diffusion
equation considered in the previous course.
Equation 11 may appear confusing at first, due to the number of terms and the subscript notation.
To help understand this equation better, recall that the heat flux (Q) is given by Fourier’s Law:

Q = −kA∇T (13)

where Q is the vector heat flux. The negative sign is required because heat flows in the direction
opposite to the temperature gradient ∇T (from hot to cold). Substituting Fourier’s Law into the

Dr. Aidan Wimshurst, Fluid Mechanics 101 10


Cell Centroid
Face Centre

Figure 6: An example 2D cell with 5 faces (a pentagon). The temperature gradient (∇T )f ,
thermal conductivity (kf ) and the unit normal vector (n̂f ) are required at the face centre to
evaluate the heat flux across each face. Note that the temperature gradient is not necessarily
parallel with the unit normal vector.

discretised heat diffusion equation leads to:


X h i
0= −Qf · n̂f + SP VP (14)
Faces

X h i
Qf · n̂f = SP VP (15)
Faces

Physically the discretised form of the heat diffusion equation therefore means that the sum of the
heat fluxes across all of the faces of the cell must be equal to the sources of heat inside the cell.
Hence, the discretised form of the heat diffusion equation is actually an expression of conservation
of energy. The dot product is required to capture the component of the heat flux (Q) that passes
out of the cell across each face, as the temperature gradient does not necessarily align with the
unit normal vector (which always points out of the cell). Figure 6 shows a diagram to illustrate the
dot product of the temperature gradient and the unit normal vector. Before a CFD solution can
be constructed for the discretised heat diffusion equation (equation 11), the temperature gradient
(∇T )f at the face centre must be expressed in terms of the temperatures at the centroids of the
surrounding cells (TP ). Slightly different methods are used on the interior faces and boundary faces
in the mesh. These methods will now be presented, before illustrating the entire process with an
example problem at the end of the Chapter.

Interior Faces
At this stage, it is useful to think about cell faces specifically. Cell faces can either be interior faces
or boundary faces. Interior faces are connected to a cell on both sides, while boundary faces are
connected to a cell on a single side. Figure 7 shows an example interior face that is connected to a
triangular cell on each side. One of these cells shall be referred to as the owner (P ) and the other
cell as the neighbour (N ). It does not matter which cell is selected as the owner and which one as
the neighbour, as long as each interior face is assigned an owner cell and a neighbour cell. As shown
in Figure 8, there is a vector dP N that connects the owner cell centroid (P ) and the neighbour cell
centroid (N ) across interior faces in the mesh. This vector may not necessarily be parallel with the
unit normal vector (n̂f ) and is essential in the analysis that follows.
As a reminder, we currently have the discretised form of the heat diffusion equation (equation
11): X
0= [kf Af ((∇T )f · n̂f )] + Sp Vp (16)
Faces

Dr. Aidan Wimshurst, Fluid Mechanics 101 11


Neighbour Cell

Owner Cell Centroid


Face Centre

Interior Face

Figure 7: A example interior face that is connected to an owner cell and a neighbour cell.

Neighbour Cell
Owner Cell
Centroid
Face Centre

Shared Face

Figure 8: An example triangular cell which is connected to a rectangular cell across an interior
face. The cell centroids are connected by a vector dP N that is not necessarily parallel with the unit
normal vector n̂f .

and need to express the dot product of the temperature gradient and the unit normal vector ((∇T )f ·
n̂f ) in terms of the temperatures at the owner and neighbour cell centroids (TP and TN ). Start by
decomposing the unit normal vector into a component n̂1 which is parallel with dP N and a residual
component which is left over (n̂2 ).
n̂f = n̂1 + n̂2 (17)
A schematic diagram of this decomposition is shown in Figure 9. Returning to the dot product of
the temperature gradient and the unit normal vector we can write:
(∇T )f · n̂f = (∇T )f · n̂1 + (∇T )f · n̂2 (18)
The vector n̂1 is parallel with dP N . Therefore, the first term can be simplified:
TN − TP
(∇T )f · n̂1 = |n̂1 | (19)
|dP N |
This simplification is valid because the temperature gradient along a straight line is:
∆T Change in Temperature
∇T ∼ = (20)
∆x Distance
and this gradient is parallel with n̂1 , so we can just multiply by the magnitude of n̂1 . Returning to
the dot product of the temperature gradient and n̂f :
TN − TP
(∇T )f · n̂f = |n̂1 | + (∇T )f · n̂2 (21)
|dP N |
Substitute this equation into the finite volume discretisation of the heat diffusion equation (equation
11): " !#
X TN − TP X
0= kf Af |n̂1 | + [kf Af ((∇T )f · n̂2 )] +Sp Vp (22)
Faces |dP N | Faces
| {z }
Non Orthogonal Term

Dr. Aidan Wimshurst, Fluid Mechanics 101 12


Figure 9: An example triangular cell which is connected to a rectangular cell across an interior
face. The unit normal vector (n̂f ) is decomposed into a component that is parallel with dP N (n̂1 )
and a residual component (n̂2 ).

Owner Cell Neighbour Cell

Centroid
Face Centre

Figure 10: An example of two rectangular cells which are connected across an interior face. All of
the internal angles are 90◦ . The cell centroids are connected by a vector dP N that is parallel with
the unit normal vector n̂f .

The second summation is referred to as the non-orthogonal term and is required on meshes where
the vector dP N is not parallel with the unit normal vector n̂f (n̂2 6= 0). In this Chapter, we will limit
our consideration to unstructured meshes made up of perfect squares and rectangles only. For these
meshes n̂2 = 0. Hence:
:0
" !#
X TN − TP X 

0= kf Af |n̂1 | + [kf Af((∇T
) f · n̂2 + Sp Vp
)] (23)
|dP N |  
Faces Faces


" !#
X TN − TP
0= kf Af |n̂1 | + Sp Vp (24)
Faces |dP N |
Split up the fraction (so that TP and TN are separate):
" # " #
X TN X TP
0= kf Af |n̂1 | − kf Af |n̂2 | + Sp Vp (25)
Faces |dP N | Faces |dP N |

The temperature at the centroid of the owner cell (Tp ) is the same for all of the faces. Hence, it can
be brought outside the fraction:
" # " #
X kf Af X kf Af
Tp |n̂1 | = TN |n̂1 | + Sp Vp (26)
Faces |dP N | Faces |dP N |

This form may seem complicated at first glance. However, if we consider the example cell in Figure
11 with three faces, then we can easily write the expression out in its full form as an example:
" ! ! !#
k1 A1 k2 A2 k3 A3
Tp |n̂1,1 | + |n̂1,2 | + |n̂1,3 | =
|dP N,1 | |dP N,2 | |dP N,3 |

Dr. Aidan Wimshurst, Fluid Mechanics 101 13


Face 2 Face 3

Face 1

Figure 11: An example triangular cell with three faces.

! ! !
k1 A1 k2 A2 k3 A3
|n̂1,1 | TN,1 + |n̂1,2 | TN,2 + |n̂1,3 | TN,3 + Sp Vp (27)
|dP N,1 | |dP N,2 | |dP N,3 |
All of the terms in equation 26 are known, with the exception of the temperature at the owner
cell centroid (Tp ) and the neighbouring cell centroids (TN,1 , TN,2 , TN,3 ). It is common in the CFD
literature to write equation 26 in concise form as:
X
ap Tp = (aN TN ) + Sp Vp (28)
Faces

or: X
ap Tp − (aN TN ) = Sp Vp (29)
Faces

AT = B (30)
with the following coefficients:
X kf Af kf Af
ap = |n̂1 | aN = |n̂1 | (31)
Faces |dP N | |dP N |

When writing an unstructured CFD code, the equations above show that the process of calculating
the matrix coefficients (ap and aN ) now involves looping over the faces in the mesh. This is different
to how structured CFD codes are written, where specific cell neighbours are referred to (e.g top,
bottom, left and right). There is no restriction on the shape of the cell or the number of faces it has.
Hence, this form of the discretised heat equation can be applied to any unstructured mesh, along as
the cells have a finite number of faces.
Later in this chapter, an example problem will be used to fully demonstrate the process of calcu-
lating the matrix coefficients and assembling the matrices for unstructured meshes. However, before
this example problem can be considered, the boundary faces in the mesh need to be considered, as
they do not contain a vector dP N and need different treatment.

Boundary Faces
Boundary faces require a different formulation of the discretised heat diffusion equation to interior
faces because boundary faces are only connected to a single cell (the owner cell) on one side. They
are not connected to a neighbour cell and therefore do not have a distance dP N associated with
them. Boundary faces will either have a Dirichlet (fixed value) or Neumann (fixed gradient) condition
applied to them. These conditions were introduced in Part 2 of this course. The treatment of Dirichlet
and Neumann conditions will be revisited again here, because their treatment is slightly different for
unstructured meshes.

Dr. Aidan Wimshurst, Fluid Mechanics 101 14


Cell Centroid
Face Centre
Boundary Face

Figure 12: An example quadrilateral cell with one boundary face. dP F is the vector from the cell
centroid to the face centre, y is the perpendicular distance from the boundary face to the cell
centroid and n̂f is the unit normal vector pointing out of the domain.

Dirichlet and Neumann Boundary Conditions


For Dirichlet boundary conditions, the entire face (including the nodes and the face centre) is at a
fixed temperature of Twall . This results in a heat flux of Qwall across the boundary face and out of
the cell: !
Tp − Twall
Qwall = −kwall Awall [W] (32)
|y|
Following the standard sign convention, the negative sign is required to ensure that heat flows in the
opposite direction to the temperature gradient (from hot to cold) and that positive heat flux is out
of the cell (and out of the domain). Rearranging slightly:
!
Twall − Tp
Qwall = kwall Awall [W] (33)
|y|

Notice that the distance |dP N | has been replaced with |y|, which is the magnitude of the perpendicular
distance to the wall, as shown in Figure 12. The reason for this change is that the boundary
temperature is constant across the face, unlike interior faces where the temperature variation is
linear.
The perpendicular distance (y) can be calculated by first calculating the vector that connects the
cell centre to the face centre dP F .
dP F = xf − xP (34)
Now the perpendicular component can be calculated by taking the dot product with the unit normal
vector (n̂f ):
y = n̂f (dP F · n̂f ) (35)
For Neumann boundary conditions, the user specifies a heat flux per unit area qwall on the boundary
face, rather than a fixed temperature Twall . Note that qwall has units of W/m2 . For these boundary
faces, the heat flux out of the boundary cell is:

Qwall = qwall Awall [W] (36)

Returning to the finite volume discretisation of the heat equation for interior cells (equation 26):
" #
X TN − TP
0= kf Af |n̂1 | + Sp Vp (37)
Interior |dP N |
Faces

This equation is valid for interior cells only (cells which only contain interior faces). If the cell also
contains boundary faces, then the heat flux out of the cell across these boundary faces is also required.

Dr. Aidan Wimshurst, Fluid Mechanics 101 15


A cell may have multiple boundary faces, therefore the heat flux out of all of these faces is required:
" # " #
X TN − TP X Twall − TP
0= kf A f |n̂1 | + kwall Awall + (38)
Interior |dP N | Dirichlet |y|
Faces Faces
X
[qwall Awall ] + Sp Vp (39)
Neumann
Faces

Following the same procedure as before, split up the fractions so that TN , TP and Twall are treated
as separate terms.
" # " #
X TN X TP
0= kf Af |n̂1 | − kf Af |n̂1 | + (40)
Interior |dP N | Interior |dP N |
Faces Faces
" # " #
X Twall X TP X
kwall Awall − kwall Awall + [qwall Awall ] + Sp Vp (41)
Dirichlet |y| Dirichlet |y| Neumann
Faces Faces Faces

Now rearrange and collect the terms in TP , TN and Twall .


 
" # " # " #
 X kf Af X kwall Awall  X kf Af
TP  |n̂1 | + = TN |n̂1 | + (42)
|dP N | |y| |dP N |

Interior Dirichlet Interior
Faces Faces Faces
" #
X kwall Awall X
Twall + [qwall Awall ] + Sp Vp (43)
Dirichlet |y| Neumann
Faces Faces

While this equation may seem complicated, it can still be written in concise form:
X
ap Tp = (aN TN ) + S (44)
Interior
Faces

with the following coefficients:


 
" # " #
 X kf Af X kwall Awall 
ap = |n̂1 | + (45)
|dP N | |y|
 
Interior Dirichlet
Faces Faces
" #
kf Af X kwall Awall X
aN = |n̂1 | S = Sp Vp + + [qwall Awall ] (46)
|dP N | Dirichlet |y| Neumann
Faces Faces

Equation 42 is identical to the equation 26 (which was derived for interior faces only), with the
exception of the additional terms for the Dirichlet and Neumann boundary faces. The procedure to
assemble the equations is also identical: loop over the faces in the mesh and then add contributions
to the terms in the above equation, depending on whether the face is an interior face or a boundary
face.
Regardless of the complexity of the above formulation, it should always be remembered that the
equation represents conservation of energy in discrete form. Each of the terms represents either a
heat flux out the cell or a volumetric heat source within the cell. At all times, the units of the terms
can be checked and should evaluate to [W].
In the next section, an example problem will be considered to illustrate how the above formulation
is applied to solve the 2D heat equation on an unstructured mesh.

Dr. Aidan Wimshurst, Fluid Mechanics 101 16


1. Create the nodes
2. Create the facess from the nodes
Meshing

3. Create the cells from the faces


4. Establish which faces are boundary faces and interior faces

5. Loop over the boundary faces and interior faces


CFD Solver

6. Caculate the matrix coefficients for each face in the mesh


7. Assign the matrix coefficients to the correct location in the matrices
8. Solve the matrices

Figure 13: An overview of the algorithm used to solve transport equations with an unstructured
finite volume mesh.

General Algorithm
It is now possible to put together a general algorithm for how the 2D heat diffusion equation can be
solved on an unstructured mesh. An overview of the key stages in the general algorithm is shown
in Figure 13. Each of these stages will be demonstrated in the example problem and expanded on
in more detail in the following sections. Stages 1, 2, 3 and 4 (in Figure 13) are usually carried out
by the meshing software. The mesh is then passed to the CFD solver and stages 5, 6, 7 and 8 are
calculated by the CFD solver itself.

Meshing - Nodes, Faces and Cells


Unstructured CFD meshes are constructed from nodes, faces and cells. By treating the nodes, faces
and cells as separate entities and linking them together, the mesh can take any form desired by the
user, allowing complex geometries to be meshed, with any type of cell. Each entity requires several
key quantities to be calculated so that it is properly defined. The table below shows a brief summary
of the key quantities that are calculated when the mesh is created.

Node Face and Cell Properties

Node Face Cell


Coordinates Face Centre Centroid
Face Area Volume
Unit Normal Vector Number of Faces
Number of Nodes

The nodes are straightforward, as they only require x, y and z coordinates to fully define them.
However, the faces and cells require many more quantities (face centre, face area, unit normal vector,
volume etc.) to be calculated in order to fully define them. The methods used to calculate these
quantities will be presented in the following sections.

Dr. Aidan Wimshurst, Fluid Mechanics 101 17


5
4 Face Centre
1

2 3

Figure 14: An example of the face centre calculation for a polyhedral cell

Figure 15: An example of the face area calculation for a 2D cell

Face Centre
The face centre is the most straightforward quantity to calculate. It is calculated as the arithmetic
average of the nodes that make up the face.
1 X
xf = xNode (47)
N Nodes
This calculation is valid for all polyhedral cells, as long as the nodes that make up the face lie in on
a flat plane. Figure 14 shows an example cell where the face centre is calculated on one of the faces.

Face Area
For 2D cells, the face area (Af ) can be calculated from the magnitude of the vector that connects
the two nodes that make up the face.
Af = |x2 − x1 | (48)
This calculation is illustrated in Figure 15. For 3D cells, it is more complicated to compute the
face area, as the face may not be a regular polygon. It could have N faces and may be highly
skewed. Therefore, simple formulas for calculating the areas of regular polygons are not appropriate.
An alternative approach to computing the face area will be presented later, when the cell volume is
computed.

Unit Normal Vector


The unit normal vector for a face can be calculated by taking the cross-product of any two vectors
(that are not parallel) on the face of the cell. The reason that any two vectors can be taken is that
the faces of the cells are flat and hence the cross-product of any two vectors will be perpendicular to
the face. For vectors a and b on the face of the cell, the unit normal vector (n̂f ) is:
nf
nf = a × b n̂f = (49)
|nf |

Dr. Aidan Wimshurst, Fluid Mechanics 101 18


(a) 3D Cell (b) 2D Cell

Figure 16: Normal vector generation on the face of (a) a 3D cell and (b) a 2D cell.

Centroid
Face Centre

(a) Outwards Normal (b) Inwards Normal

Figure 17: Examples of (a) an outwards facing unit normal vector and (b) an inwards facing unit
normal vector. The vector c connects the cell centroid with the face centre.

Figure 16 (a) shows an example of the normal vector generation for a face on a 3D cell. For 2D
cells, the calculation of the unit normal vector is more straightforward, as the face is defined by a
line between 2 nodes. As shown in Figure 16 (b), this 2D vector a can be rotated by 90 degrees
(becoming the normal vector) with the following formula:

a = (ax , ay ) (50)
nf
nf = (−ay , ax ) n̂f = (51)
|nf |
When constructing the mesh, the normal vectors should always point out of the cell that they belong
to (the owner cell P ). So when constructing the mesh, it should be checked that the normal vectors
are pointing out of the owner cells. As shown in Figure 17, this checking procedure can be carried
out by taking the dot product of the vector xf − xc with the unit normal vector. The vector xf − xc
represents the vector from the cell centroid to the face centre. If the dot product is positive, the
unit normal vector is pointing out of the cell. Conversely, if the dot product is negative then the unit
normal vector is pointing into the cell and must be reversed.

n̂f · (xf − xc ) > 0 Outwards Pointing Normal (52)


n̂f · (xf − xc ) < 0 Inwards Pointing Normal (53)

Recall that vectors can be reversed (or rotated by 180◦ ) by multiplying all components by -1.

Dr. Aidan Wimshurst, Fluid Mechanics 101 19


(a) Regular Polyhedra (b) Irregular Polyhedra

Figure 18: Examples of (a) Regular Polyhedra and (b) Irregular Polyhedra

Cell Volume
The cell volume is not straightforward to calculate, as the cells may be skewed, have a large number
of faces or have faces with different areas. Therefore, (for unstructured meshes) a general approach
is needed to compute the cell volume. This approach needs to be appropriate for irregular polyhedra
(see Figure 18) and skewed cells.
To ensure that the approach is general and works for any unstructured mesh, an equation for
the cell volume will be derived for an N sided irregular polyhedra (Figure 18 (b)). As the formula is
appropriate for an N sided irregular polyhedra, it can be applied to all cells in the unstructured mesh,
regardless of their shape. To derive this formula, start with the divergence theorem in its general
form (equation 7 from earlier in this Chapter):
Z Z
(∇ · B) dV = (B · n̂) dA (54)
V A

Recall that this formula is valid for any vector field B. The trick to calculate the volume of the cell
with this formula is to let:
1
B = (x, y, z) (55)
3
We can evaluate the divergence of this field directly:
∂Bx ∂By ∂Bz 1
∇·B = + + = (1 + 1 + 1) = 1 (56)
∂x ∂y ∂z 3
By substituting this expression into the left-hand side of the divergence theorem, the left hand side
becomes the volume of the cell.
Z Z
(∇ · B) dV = (B · n̂) dA (57)
V A
Z Z
(1) dV = (B · n̂) dA (58)
V A
Z
Vp = (B · n̂) dA (59)
A
The divergence theorem has now become an equation for the volume of the cell! However, the vector
field B has now been specified. Hence, B also has to be substituted into the right hand side of this
equation. Z 
1

Vp = (x, y, z) · n̂ dA (60)
A 3

The right hand side of this equation can be best understood using Figure 19. At every location on
the surface, the coordinate of the point on the surface (x, y, z) is multiplied by the local unit normal
vector (n̂). This process is repeated (integrated) over the entire surface of the cell to calculate the
volume. This integral is computationally expensive to carry out. Hence, it will be simplified to reduce
the computational cost.

Dr. Aidan Wimshurst, Fluid Mechanics 101 20


Integrate over the
entire surface

Figure 19: The product of the surface coordinate (x, y, z) and the unit normal vector (n̂) are
integrated over the surface of the cell.

Integral is the same, if we take


the face centre value!

Node Face Centre Node

Figure 20: The integral across the face is equivalent to taking the face centre value multiplied by
the face area.

To simplify the integral, first note that the cell has a finite number of faces N . Hence, the integral
over the surface of the entire cell can be split into the surface integral over each of the faces of the
cell individually:
X Z 1 
Vp = (x, y, z) · n̂ dA (61)
Faces A 3
Recall that the flow quantities all vary linearly across the faces of the cell. Hence, the integral over
the face is equivalent to the value at the face centre multiplied by the area of the face. This can
be best understood visually, using Figure 20. Hence, the integral over the face of the cell can be
replaced with the value at the centre of the face. The subscript f will be used to denote the value
at the centre of the face:
X 1 
Vp = ((xf , yf , zf ) · n̂f ) Af (62)
Faces 3

Notice that the general coordinates x, y and z are now evaluated locally at the centre of the face
only (xf , yf and zf ). Likewise, the unit normal vector (n̂f ) is now only evaluated at the centre of the
face, rather than continuously along the face. Hence, the dot product is now only evaluated locally
at the centre of the face. With this simplification, the formula for the volume of the cell can be
written concisely as the dot product of the face centre coordinate and the unit normal vector at the
centre of the face:
X 1 
Vp = (xf · n̂f )Af (63)
Faces 3

This formula is valid for any 3D polyhedral cell, regardless of the number of faces and whether the
cell is skewed or not.
When evaluating the formula it should be emphasised that the coordinates of the centre of the
face (xf = (xf , yf , zf )) are evaluated relative to the centroid of the cell and are not global
coordinates. This is to ensure that the dot product actually represents the flux out of the cell, as is

Dr. Aidan Wimshurst, Fluid Mechanics 101 21


Cell Centroid

(a) Nodes (b) Face Centres

Figure 21: The cell centroid can be computed from either the nodal coordinates or the face centre
coordinates

required by the divergence theorem. Hence, the formula should actually be written (more correctly)
as:
X 1 
Vp = ((xf − xc ) · n̂f )Af (64)
Faces 3

where xc is the centroid of the cell.


In 2D, the volume of the cell can be calculated using the same approach. However, as the cell is
now in x, y coordinates (rather than x, y, z coordinates), the vector field B has to be set as 12 (x, y)
rather than 31 (x, y, z). This is to ensure that:
!
∂ ∂ 1 1
∇·B = , · (x, y) = (1 + 1) = 1 (65)
∂x ∂y 2 2

Following the same approach as used for the 3D cell, the volume of the cell in 2D is:

X 1 
Vp,2D = ((xf − xc ) · n̂f )Af (66)
Faces 2

This formula is used in the example code later in this chapter to calculate the area of the cells that
make up the 2D unstructured mesh.

Cell Centroid
The cell centroid can be calculated by either taking the arithmetic average of all of the nodal coordi-
nates that make up the cell, or the arithmetic average of the face centre coordinates. This method
is applicable to both 2D cells and 3D cells.
1 X 1 X
xCentroid = xFace xCentroid = xNode (67)
N Faces N Nodes

As the face centre coordinates where calculated from the nodes, either method of calculating the
cell centroid will produce the same result. Figure 21 shows an example 2D cell to illustrate the two
methods that can be used to calculate the cell centroid.

Dr. Aidan Wimshurst, Fluid Mechanics 101 22


Face 3 Face 7

Face 4

Face 2

Face 6
Cell 1 Cell 2

Face 1 Face 5 Centroid


Face Centre
Interior Face
1 Boundary Face

0
0 2 4

Figure 22: Two example cells that will be used to illustrate the cell construction process.

Cell Construction: Worked Example


In this section, two example cells will be constructed using the methods described in the previous sec-
tion. In the next section, the same procedure will then be applied to construct an entire unstructured
mesh (using the python code and the Excel sheets provided).
Consider the 2D rectangular cells in Figure 22. 2D Rectangles have been chosen for this example
as the calculations can be readily checked by inspection. Despite these cells being 2D, the area of
the rectangles will be referred to as the cell volume and the length of the sides will be referred to as
the face areas, to ensure that the naming convention is the same for 2D and 3D cells.
Start by calculating the centroid of each cell using equation 67:
1
xc,1 = [(0, 0) + (2, 0) + (2, 1) + (0, 1)] = (1, 0.5) (68)
4
1
xc,2 = [(2, 0) + (4, 0) + (4, 1) + (2, 1)] = (3, 0.5) (69)
4
Now calculate the centre of each face using equation 47, the face area using equation 48 and the
unit normal vector of each face using equation 51. The table below summarises the face centre (xf ),
face area (Af ) and unit normal vector (n̂f ) for all 7 faces.

Dr. Aidan Wimshurst, Fluid Mechanics 101 23


Properties of Cell Faces in the Example Problem

Face xf xc xf − xc n̂f Af Type Owner Cell


1 (1, 0) (1, 0.5) (0, -0.5) (0, -1) 2 Boundary 1
2 (2, 0.5) (1, 0.5) (1, 0) (1, 0) 1 Interior 1
3 (1, 1) (1, 0.5) (0, 0.5) (0, 1) 2 Boundary 1
4 (0, 0.5) (1, 0.5) (-1, 0) (-1, 0) 1 Boundary 1
5 (3, 0) (3, 0.5) (0, -0.5) (0, -1) 2 Boundary 2
6 (4, 0.5) (3, 0.5) (1, 0) (1, 0) 1 Boundary 2
7 (3, 1) (3, 0.5) (0, 0.5) (0, 1) 2 Boundary 2

Notice that all of the faces are boundary faces (they are only connected to a single cell), except
for face 2, which is an interior face. Face 2 is connected to both cell 1 and cell 2. For face 2, its
owner cell has been selected as cell 1 and its neighbour cell as face 2. This means that (later on when
the matrices are calculated) the unit normal vector points out of cell 1 and into cell 2. As explained
in the previous section, it does not matter which cell is selected as the owner and the neighbour cell,
as long as the vector points out of the owner cell and into the neighbour cell.
Now that the face centres and unit normal vectors have been computed for all the faces, the
volume of each cell can be computed. Once the cell volumes have been computed, the cells are fully
defined and a CFD solution can be computed.
To calculate the volume of the cells in Figure 22, the cells are considered one at a time, in isolation
from the rest of the mesh. This means that the unit normal vectors will all be pointing out of the
cell, when its volume is being calculated. Hence, when Cell 2 is considered, the unit normal vector
for Face 2 will be reversed (as Face 2 belongs to Cell 1). Recall that the volume of a 2D cell can be
computed using equation 66:
X 1 
Vp,2D = ((xf − xc ) · n̂f )Af (70)
Faces 2

Using the values in the table above, the volume of Cell 1 is:
1 1 1 1
       
Vp,2D = (0 + 0.5)2 + (1 + 0)1 + (0 + 0.5)2 + (1 + 0)1 = 2 (71)
2 2 2 2
and the volume of Cell 2 is:
1 1 1 1
       
Vp = (0 + 0.5)2 + (1 + 0)1 + (0 + 0.5)2 + (1 + 0)1 = 2 (72)
2 2 2 2
Each cell has a volume of 2, which can be checked by inspection.
This method may appear unnecessarily complicated for such simple cells. However, it should be
remembered that this method is universal and can also be applied to more complicated cells, like
skewed cells and irregular polyhedra, where simple methods cannot.

Unstructured Mesh: Full Worked Example


For the example problem in this Chapter, heat diffusion in a 2D plate will be considered. Figure 23
shows the geometry and boundary conditions of the example problem. The geometry is identical to

Dr. Aidan Wimshurst, Fluid Mechanics 101 24


4m

4m
(a) Geometry (b) Boundary Conditions
0 1 2 3 4
0 1 2 3
5 6 7 8 9 4 5 8 6 7 0 1 2 3
9
10 11 12
10 11 12 13 14 13 14 15 16 17 4 5 6 7
18 19 20 21
15 16 17 18 19 22 23 24 25 26 8 9 10 11
27 28 29 30
20 21 22 23 24 31 32 33 34 35 12 13 14 15
36 37 38 39
(c) Nodes (d) Faces (e) Cells

Figure 23: A summary of the geometry, mesh and boundary conditions for the example problem.

the geometry that was used in the Part 2 of this course. However, the mesh is now treated as an
unstructured mesh, rather than a structured mesh. This means that the nodes, faces and cells are
treated as independent units.
The plate has a thickness of 0.1m, a width of 4m, a height of 4m, and a thermal conductivity
of 100 W/mK. The walls of the plate are held at fixed temperatures of 100◦ C, 150◦ C, 200◦ C and
250◦ C on the left, bottom, right and top sides respectively. There is a volumetric heat source of 1000
W/m3 in the plate.
As you proceed through the following sections where the solution procedure is described, follow
along with either the python source code or the Excel sheets provided.

unstructuredMesh.py unstructuredMesh.xls

Either the python or Excel approach can be used to solve this problem. However, it should be
noted that real CFD codes use an object-oriented programming approach. Hence, the python code
is preferred in this instance.

Mesh Generation
The first stage in the solution process is to assemble the mesh. Meshes can be generated using a
variety of techniques (block-structured, Delaunay triangulation, Octree etc). In this course, a manual
approach will be adopted for simplicity.
Start by assembling the nodes as shown in Figure 23 (c). The numbering scheme used to identify
the nodes (along with their coordinates) is shown in the table below.

Dr. Aidan Wimshurst, Fluid Mechanics 101 25


0 1 2 3
4 5 6 7 8
9 10 11 12
13 14 15 16 17
Dirichlet Boundary Faces
18 19 20 21 Interior Faces
22 23 24 25 26
27 28 29 30
31 32 33 34 35
36 37 38 39

Figure 24: A diagram to highlight the interior faces and Dirichlet boundary faces in the mesh.

Nodes

Node (x, y) Node (x, y) Node (x, y)


0 (0,4) 8 (3,3) 16 (1,1)
1 (1,4) 9 (4,3) 17 (2,1)
2 (2,4) 10 (0,2) 18 (3,1)
3 (3,4) 11 (1,2) 19 (4,1)
4 (4,4) 12 (2,2) 20 (0,0)
5 (0,3) 13 (3,2) 21 (1,0)
6 (1,3) 14 (4,2) 22 (2,0)
7 (2,3) 15 (0,1) 23 (3,0)
24 (4,0)

For this example problem, the nodes have a regular pattern. This is to make it easier to follow
and recreate but is not essential for unstructured meshes.
Now that the nodes have been created, the faces can be constructed from the nodes. Each face
needs to know which nodes it was constructed from and whether it is an interior face, a Dirichlet
boundary face or a Neumann boundary face. Figure 24 shows a diagram to highlight which faces are
interior faces, which faces are Dirichlet and which faces are Neumann faces in the mesh. The interior
faces have an owner cell and a neighbour cells, while the Dirichlet and Neumann faces only have an
owner cell. Hence, interior faces are actually two-sided faces, while the Dirichlet and Neumann faces
are single-sided faces.

Dr. Aidan Wimshurst, Fluid Mechanics 101 26


Faces
Face Node 1 Node 2 Type Face Node 1 Node 2 Type
0 0 1 Dirichlet 20 12 13 Interior
1 1 2 Dirichlet 21 13 14 Interior
2 2 3 Dirichlet 22 10 15 Dirichlet
3 3 4 Dirichlet 23 11 16 Interior
4 0 5 Dirichlet 24 12 17 Interior
5 1 6 Interior 25 13 18 Interior
6 2 7 Interior 26 14 19 Dirichlet
7 3 8 Interior 27 15 16 Interior
8 4 9 Dirichlet 28 16 17 Interior
9 5 6 Interior 29 17 18 Interior
10 6 7 Interior 30 18 19 Interior
11 7 8 Interior 31 15 20 Dirichlet
12 8 9 Interior 32 16 21 Interior
13 5 10 Dirichlet 33 17 22 Interior
14 6 11 Interior 34 18 23 Interior
15 7 12 Interior 35 19 24 Dirichlet
16 8 13 Interior 36 20 21 Dirichlet
17 9 14 Dirichlet 37 21 22 Dirichlet
18 10 11 Interior 38 22 23 Dirichlet
19 11 12 Interior 39 23 24 Dirichlet

Having constructed the faces from the nodes, the properties of the faces can be calculated. This
includes: the face centre (xf ), the face area (Af ) and the unit normal vector (n̂f ). The face centres
are calculated by arithmetic averaging the nodes that they were constructed from (equation 47). As
the mesh is 2D and each face only has 2 nodes:
1 X 1
xf = xNode −→ xf = (x1 + x2 ) (73)
N Nodes 2

The face area can be computed using equation 48. This is the same method that was used for the
example problem in the previous section (the 2D rectangles).

Af = |x2 − x1 | (74)

As the mesh in this example is 2D, the unit normal vector for each face is calculated using equation
51. More specifically, the vector that connects the two nodes that make up the face (a) is rotated
by 90 degrees.
n
n = (−ay , ax ) n̂f = (75)
|n|
After creating the unit normal vectors, it must be checked that the unit normal vectors are all pointing
out of the cells that they belong to, to ensure that the cell volumes are computed correctly. The check
on the unit normal vector direction is carried out by taking the dot product of the unit normal vector
(n̂f ) and the vector that connects the face centre to the cell centroid (xf − xc ) (using equation 52).

n̂f · (xf − xc ) > 0 (76)

Dr. Aidan Wimshurst, Fluid Mechanics 101 27


(a) Interior Face (b) Boundary Face

Figure 25: Diagrams to show the connecting vector dP N and perpendicular vector y that are
needed for interior and boundary faces respectively.

If the unit normal vector is pointing out of the cell, then the dot product will be positive. Conversely,
if the unit normal vector is pointing into the cell, then the dot product will be negative and the unit
normal vector must be reversed (by multiplying it by -1). This process of checking the unit normal
vector can be examined in the python code and Excel sheets.
Unlike the previous example problem (where a CFD solution was not computed), the vector dP N
that connects the cell centroids across the interior faces in the mesh, also needs to be computed.
As shown in Figure 25, the connecting vector is the vector from the owner cell centroid (xP ) to the
neighbour cell centroid (xN ) across the face of interest.

dP N = xP − xN (77)

Boundary faces do not have a neighbour cell. Hence, a connecting vector is not calculated for these
faces. However, boundary faces need to know the perpendicular distance from the cell centroid to
the wall (y) instead. This vector is also shown in Figure 25. To calculate the perpendicular distance
to the wall, start with the vector between the cell centroid and the boundary face centre (dP F ):

dP F = xf − xP (78)

Now the perpendicular vector y can be calculated from this vector:

y = n̂f (dP F · n̂f ) (79)

The connecting vector (dP N ) and the perpendicular vector (y) are calculated for every face in the
mesh and are shown in the table below. Note that interior faces do not have a perpendicular distance
vector y, while boundary faces do not have a connecting vector dP N .

Dr. Aidan Wimshurst, Fluid Mechanics 101 28


Face Properties

Face xf Af n̂f dP N y Face xf Af n̂f dP N y


0 (0.5, 4) 0.1 (0, 1) - (0, 0.5) 20 (2.5, 2) 0.1 (0, -1) (0, -1) -
1 (1.5, 4) 0.1 (0, 1) - (0, 0.5) 21 (3.5, 2) 0.1 (0, -1) (0, -1) -
2 (2.5, 4) 0.1 (0, 1) - (0, 0.5) 22 (0, 1.5) 0.1 (-1, 0) - (-0.5, 0)
3 (3.5, 4) 0.1 (0, 1) - (0, 0.5) 23 (1, 1.5) 0.1 (1, 0) (1, 0) -
4 (0, 3.5) 0.1 (-1, 0) - (-0.5, 0) 24 (2, 1.5) 0.1 (1, 0) (1, 0) -
5 (1, 3.5) 0.1 (1, 0) (1, 0) - 25 (3, 1.5) 0.1 (1, 0) (1, 0) -
6 (2, 3.5) 0.1 (1, 0) (1, 0) - 26 (4, 1.5) 0.1 (1, 0) - (0.5, 0)
7 (3, 3.5) 0.1 (1, 0) (1, 0) - 27 (0.5, 1) 0.1 (0, -1) (0, -1) -
8 (4, 3.5) 0.1 (1, 0) - (0.5, 0) 28 (1.5, 1) 0.1 (0, -1) (0, -1) -
9 (0.5, 3) 0.1 (0, -1) (0, -1) - 29 (2,5, 1) 0.1 (0, -1) (0, -1) -
10 (1.5, 3) 0.1 (0, -1) (0, -1) - 30 (3.5, 1) 0.1 (0, -1) (0, -1) -
11 (2.5, 3) 0.1 (0, -1) (0, -1) - 31 (0, 0.5) 0.1 (-1, 0) - (-0.5, 0)
12 (3.5, 3) 0.1 (0, -1) (0, -1) - 32 (1, 0.5) 0.1 (1, 0) (1, 0) -
13 (0, 2.5) 0.1 (-1, 0) - (-0.5, 0) 33 (2, 0.5) 0.1 (1, 0) (1, 0) -
14 (1, 2.5) 0.1 (1, 0) (1, 0) - 34 (3, 0.5) 0.1 (1, 0) (1, 0) -
15 (2, 2.5) 0.1 (1, 0) (1, 0) - 35 (4, 0.5) 0.1 (1, 0) - (0.5, 0)
16 (3, 2.5) 0.1 (1, 0) (1, 0) - 36 (0.5, 0) 0.1 (0, -1) - (0, -0.5)
17 (4, 2.5) 0.1 (1, 0) - (0.5, 0) 37 (1.5, 0) 0.1 (0, -1) - (0, -0.5)
18 (0.5, 2) 0.1 (0, -1) (0, -1) - 38 (2.5, 0) 0.1 (0, -1) - (0, -0.5)
19 (1.5, 2) 0.1 (0, -1) (0, -1) - 39 (3.5, 0) 0.1 (0, -1) - (0, -0.5)

The nodes and faces have now been assembled and are fully defined. The cells can now be
constructed from the faces. To start constructing the cells, the cell centroids are calculated by
arithmetic averaging either the face centres or the nodes that make up the cell (equation 67):
1 X 1 X
xP = xf or xP = xNode (80)
N Faces N Nodes

As the mesh is constructed from all quadrilateral cells in this example, the equation becomes:
1 1
xP = (xf 1 + xf 2 + xf 3 + xf 4 ) or xP = (xn1 + xn2 + xn3 + xn4 ) (81)
4 4
In addition to the cell centroid, the cell volume has to be calculated. The cell volume is calculated
using equation 66
X 1 
Vp,2D = ((xf − xc ) · n̂f )Af (82)
Faces 2

The cell volume calculation is carried out by looping over the faces that make up each cell and adding
their contribution to the cell volume. This process can be examined in the python code and the
Excel sheets.
The table below shows the cell centroid (xP ) and volume of the cell (VP ) for all cells in the mesh.

Dr. Aidan Wimshurst, Fluid Mechanics 101 29


Cells

Cell Faces xp Vp Cell Faces xp Vp


0 9, 5, 0, 4 (0.5, 3.5) 0.1 8 27, 23, 18, 22 (0.5, 1.5) 0.1
1 10, 6, 1, 5 (1.5, 3.5) 0.1 9 28, 24, 19, 23 (1.5, 1.5) 0.1
2 11, 7, 2, 6 (2.5, 3.5) 0.1 10 29, 25, 20, 24 (2.5, 1.5) 0.1
3 12, 8, 3, 7 (3.5, 3.5) 0.1 11 30, 26, 21, 25 (3.5, 1.5) 0.1
4 18, 14, 9, 13 (0.5, 2.5) 0.1 12 36, 32, 27, 31 (0.5, 0.5) 0.1
5 19, 15, 10, 14 (1.5, 2.5) 0.1 13 37, 33, 28, 32 (1.5, 0.5) 0.1
6 20, 16, 11, 15 (2.5, 2.5) 0.1 14 38, 34, 29, 33 (2.5, 0.5) 0.1
7 21, 17, 12, 16 (3.5, 2.5) 0.1 15 39, 35, 30, 34 (3.5, 0.5) 0.1

Now that the nodes, faces and cells are all fully defined, the meshing process is complete. The
generated mesh can now be passed to the CFD software (such as ANSYS Fluent, OpenFOAM, Star
CCM and ANSYS CFX), where the matrices are assembled and the equations solved.

Matrix Coefficients
The first stage in the solution process is to compute the matrix coefficients (aP , aN and S), so that
the matrices can be assembled and the CFD equations solved. As a reminder, the finite volume
discretisation of the heat diffusion equation is:
X
ap Tp = (aN TN ) + S (83)
Interior
Faces

with the following coefficients:


 
" # " #
 X kf Af X kwall Awall 
ap = |n̂1 | + (84)
|dP N | |y|
 
Interior Dirichlet
Faces Faces
" #
kf Af X kwall Awall X
aN = |n̂1 | S = Sp Vp + Twall + [qwall Awall ] (85)
|dP N | Dirichlet |y| Neumann
Faces Faces
The coefficients aP , aN and S need to be calculated for every cell in the mesh. The best way
to calculate these coefficients is to loop over all the faces in the mesh, and have each face add a
contribution to the coefficients of the cell that it belongs to. This process might appear confusing.
Hence, the reader is encouraged to examine the python code and Excel sheets, where the process
is carried out.
When looping over the faces in the mesh, a different treatment is adopted, depending on whether
the face is an interior face, a Dirichlet boundary face or a Neumann boundary face. If the face is an
interior face, then add a contribution to ap and calculate aN :
kf Af kf Af
ap = ap + |n̂1 | aN = |n̂1 | (86)
|dP N | |dP N |
If the face is a Dirichlet boundary face, then a contribution is added to ap and the source term S:
kwall Awall kwall Awall
ap = ap + S = S + Twall (87)
|y| |y|

Dr. Aidan Wimshurst, Fluid Mechanics 101 30


If the face is a Neumann boundary face, then a contribution is added to the source term:
S = S + qwall Awall (88)
The table below summarises the contributions that are added for all 40 faces in the mesh of the
example problem.

Face Contributions to Cell Totals


Face ap aN S Face ap aN S
0 20 - 5000 20 10 10 -
1 20 - 5000 21 10 10 -
2 20 - 5000 22 20 - 2000
3 20 - 5000 23 10 10 -
4 20 - 2000 24 10 10 -
5 10 10 - 25 10 10 -
6 10 10 - 26 20 - 4000
7 10 10 - 27 10 10 -
8 20 - 4000 28 10 10 -
9 10 10 - 29 10 10 -
10 10 10 - 30 10 10 -
11 10 10 - 31 20 - 2000
12 10 10 - 32 10 10 -
13 20 - 2000 33 10 10 -
14 10 10 - 34 10 10 -
15 10 10 - 35 20 - 4000
16 10 10 - 36 20 - 3000
17 20 - 4000 37 20 - 3000
18 10 10 - 38 20 - 3000
19 10 10 - 39 20 - 3000

Next loop over the cells in the mesh. For each of the cells, add up the contributions of each of
the faces that makes up the cell. The table below summarises each of the cells and the faces that it
is constructed from:

Cells

Cell Faces ap S Cell Faces ap S


0 9, 5, 0, 4 60 7000 8 27, 23, 18, 22 50 2000
1 10, 6, 1, 5 50 5000 9 28, 24, 19, 23 40 0
2 11, 7, 2, 6 50 5000 10 29, 25, 20, 24 40 0
3 12, 8, 3, 7 60 9000 11 30, 26, 21, 25 50 4000
4 18, 14, 9, 13 50 2000 12 36, 32, 27, 31 60 5000
5 19, 15, 10, 14 40 0 13 37, 33, 28, 32 50 3000
6 20, 16, 11, 15 40 0 14 38, 34, 29, 33 50 3000
7 21, 17, 12, 16 50 4000 15 39, 35, 30, 34 60 7000

Dr. Aidan Wimshurst, Fluid Mechanics 101 31


0 0 1 2 3

4 Cell 0 5 4 5 6 7

9 8 9 10 11

12 13 14 15

Figure 26: A diagram to highlight Cell 0 in the mesh. Cell 0 is constructed from faces 9, 5, 0 and
4.

As an example of this process, consider Cell 0, which is located in the top left hand corner of
the mesh, as shown in Figure 26. Cell 0 is constructed from faces 9, 5, 0 and 4. Faces 9 and 5 are
interior faces, while faces 0 and 4 are Dirichlet boundary faces. Hence, the coefficient ap for Cell 0
has a contribution from faces 9, 5, 0 and 4. By using the table of face contributions, ap for Cell 0 is:

ap = 10 + 10 + 20 + 20 = 60 (89)

which is in agreement with the table above. As a reminder, ap is the diagonal coefficient of the A
matrix and will appear on the diagonal of row 0.
The source term for Cell 0 (S) also has contributions from faces 9, 5, 0 and 4:

S = 0 + 0 + 5000 + 2000 = 7000 (90)

In addition to the face contributions to S, the volumetric heat source contribution (SP Vp ) is also
required. As a reminder:
" #
X kwall Awall X
S = Sp V p + Twall + [qwall Awall ] (91)
| {z } Dirichlet |y| Neumann
Volumetric Faces Faces

Hence, the total source term for Cell 0 is:

S = 100 + 7000 = 7100 (92)

S is the coefficient of the left-hand side (B) vector that is associated with cell 0. This process of
assembling the coefficients is carried out for all cells in the mesh. The resulting coefficients ap , aN
and S are summarised in the table above.
While this method of assembling the coefficients for the matrices may appear unnecessarily com-
plicated, the unstructured approach is completely general and does not restrict the user to a type
of cell or mesh structure. For example, if the cells in the mesh had five faces, then each cell would
receive five contributions towards the coefficients ap , aN and S instead of four (as in this example).

Assembling the Matrices


Now that the matrix coefficients have been calculated for every cell in the mesh, the matrices can be
assembled. In order to assemble the matrices, it is useful to consider the discretised heat diffusion
equation for Cell 0, as an example. The general form of the discretised heat diffusion equation is:
X
ap Tp − (aN TN ) = S (93)
Interior
Faces

Dr. Aidan Wimshurst, Fluid Mechanics 101 32


Cell 0 has two neighbours: Cell 1 and Cell 4 (see Figure 23). Hence, the discretised heat diffusion
equation for Cell 0 is:
ap0 T0 − an1 T1 − an4 T4 = S0 (94)
where the coefficients are given in the tables above. In the same manner, the discretised heat diffusion
equation for Cell 1 is:
−an0 T0 + ap1 T1 − an2 T2 − an5 Tf = S1 (95)
The process can then be repeated for every cell in the mesh. An example of this process is given in
the previous Parts of this course.
The matrix form of the discretised heat diffusion equation is:

AT = B (96)

where A is a square matrix of coefficients, T is the vector of temperatures at the cell centroids and
B is the vector of source terms.
For the example problem considered here, the mesh has 16 cells. Therefore, the A, T and B
matrices are:
ap0 −an1 0 0 −an4 0 0 0 0 0 0 0 0 0 0 0
 
−a
 n0 ap1 −an2 0 0 −an5 0 0 0 0 0 0 0 0 0 0  
 0 −an1 ap2 −an3 0 0 −a 0 0 0 0 0 0 0 0 0
 
 n6 

 0
 0 −an2 ap3 −an4 0 0 −an7 0 0 0 0 0 0 0 0  
−an0 0 0 −an3 ap4 −an5 0 0 −an8 0 0 0 0 0 0 0 
 
 
 0 −an1 0 0 −an4 ap5 −an6 0 0 −an9 0 0 0 0 0 0 
 
 0

0 −an2 0 0 −an5 ap6 −an7 0 0 −an10 0 0 0 0 0  
 0 0 0 −a 0 0 −a a −a 0 0 −a 0 0 0 0
 
n3 n6 p7 n8 n11 
A=
 
 0 0 0 0 −an4 0 0 −an7 ap8 −an9 0 0 −an12 0 0 0  
 0 0 0 0 0 −an5 0 0 −an8 ap9 −an10 0 0 −an13 0 0 
 
 
 0
 0 0 0 0 0 −an6 0 0 −an9 ap10 −an11 0 0 −an14 0  
 0

0 0 0 0 0 0 −an7 0 0 −an10 ap11 −an12 0 0 −an15  
−a −a −a
 
 0 0 0 0 0 0 0 0 n8 0 0 a
n11 p12 n13 0 0 
 
 0
 0 0 0 0 0 0 0 0 −an9 0 0 −an12 ap13 −an14 0  
 0 0 0 0 0 0 0 0 0 0 −an10 0 0 −an13 ap14 −an15 
 

0 0 0 0 0 0 0 0 0 0 0 −an11 0 0 −an14 ap15

T = [T0 T1 T2 T3 ... T13 T14 T15 ]T (97)


B = [S0 S1 S2 S3 ... S13 S14 S15 ]T (98)
where the subscript index for each coefficient represents the cell number. For example ap1 is the
coefficient ap for cell 1.
Notice that the coefficients ap reside on the diagonal of the A matrix and the source coefficient
S resides in the right-hand side (B) vector. The off-diagonal coefficients in the A matrix are given
by −aN , taking care to remember the negative sign!

Dr. Aidan Wimshurst, Fluid Mechanics 101 33


Filling in the coefficients from the table above, the matrices are:
60 −10 0 0 −10 0 0 0 0 0 0 0 0 0 0 0
 
−10 50 −10 0 0 −10 0 0 0 0 0 0 0 0 0 0 
 
 0 −10 50 −10 0 0 −10 0 0 0 0 0 0 0 0 0
 

 
 0
 0 −10 60 0 0 0 −10 0 0 0 0 0 0 0 0  
−10 0 0 0 50 −10 0 0 −10 0 0 0 0 0 0 0
 

 
 0 −10 0 0 −10 40 −10 0 0 −10 0 0 0 0 0 0 
 
 0

0 −10 0 0 −10 40 −10 0 0 −10 0 0 0 0 0  
 0 0 0 −10 0 0 −10 50 0 0 0 −10 0 0 0 0
 

A=
  (99)
 0 0 0 0 −10 0 0 0 50 −10 0 0 −10 0 0 0  
 0 0 0 0 0 −10 0 0 −10 40 −10 0 0 −10 0 0
 

 
 0
 0 0 0 0 0 −10 0 0 −10 40 −10 0 0 −10 0  
 0

0 0 0 0 0 0 −10 0 0 −10 50 0 0 0 −10 
−10 −10
 
 0 0 0 0 0 0 0 0 0 0 0 60 0 0 
 
 0
 0 0 0 0 0 0 0 0 −10 0 0 −10 50 −10 0  
 0 0 0 0 0 0 0 0 0 0 −10 0 0 −10 50 −10
 

0 0 0 0 0 0 0 0 0 0 0 −10 0 0 −10 60

T = [T1 T2 T3 T4 ... T14 T15 T16 ]T (100)

B = [7100 5100 5100 9100 2100 100 100 4100 2100 100 100 ...

... 4100 5100 3100 3100 7100]T (101)


These matrices are identical to the matrices calculated in the previous course and will therefore
yield an identical solution for the temperature field. The matrices are identical because the mesh
and boundary conditions applied in this problem are identical. Using an unstructured approach to
assemble the matrices rather than a structured approach does not change the matrices, as long as
the meshes and boundary conditions are identical. This is why, despite the unstructured approach
being more complicated, an unstructured approach is adopted by the majority of mainstream CFD
codes. It offers greater flexibility and does not limit the user to a given mesh structure or type of cell.

Solving the Equations


Now that the matrices have been assembled, the matrix equation AT = B can solved with an
appropriate iterative method. As with the previous course, different algorithms to solve the matrix
equation AT = B will not be considered and the default algorithms used by Excel and python will
be used instead.

Results
Figure 27 shows the temperature variation across the plate computed with the unstructured mesh
approach. Notice that the computed temperature field is identical to the computed temperature field
in the Part 2 of this course, as the mesh and boundary conditions are identical. The only aspect
that has changed is the method used to assemble the equations, as the mesh has been treated as an
unstructured mesh rather than a structured mesh.
To plot the temperature variation as a contour plot, it is more convenient to plot the temperature
at the mesh nodes (Figure 27 (a)) rather than the cell centroids (Figure 27 (b)). As shown in
Figure 28, this is because the boundary temperatures are specified at the nodes (shown in green) and
these would be missed if a contour plot was generated from the cell centroids (shown in blue).

Dr. Aidan Wimshurst, Fluid Mechanics 101 34


The CFD solution is computed at the cell centroids. Hence, in order to generate a contour plot
from the values at the mesh nodes, the interior nodal values (shown in white in Figure 28) have to
be computed from the centroid values (shown in blue). The boundary nodes (shown in green) are
already known and do not need to be computed. As the variation across the cells and faces in the
CFD mesh is linear, the nodal values can be calculated by averaging the values at the cell centroids
that surround the node.
1 X
TNode = Tp (102)
N Cells

Temperature [C]

Temperature [C]
(a) Nodal Temperatures (b) Centroid Temperatures

Figure 27: Temperature variation across the 2D plate. The centroids are shown as blue circles, the
interior nodes as white circles and the boundary nodes as green circles.

0 1 2 3

4 5 6 7

8 9 10 11

12 13 14 15

Figure 28: A diagram to show the centroid numbering scheme used in the mesh. The centroids
are shown as dark blue circles, the boundary nodes as green circles and the interior nodes as white
circles.

Dr. Aidan Wimshurst, Fluid Mechanics 101 35


0 1

4 5

Figure 29: An example of the calculation of the temperature of the top left node (Node 6, shown
in white) by arithmetic averaging the temperature of the surrounding cell centroids.

Figure 29 shows an example of this calculation for the top left interior node in the mesh (Node 6).
The temperature of this node is the average of the temperatures at the four centroids that surround
it:
1 X 1
TNode = Tp −→ TNode = (T0 + T1 + T4 + T5 ) (103)
N Cells 4
However, for the majority of unstructured meshes, the nodes are not equally distant from the centroids
that surround them. For these meshes, arithmetic averaging would lead to an incorrect calculation
of the nodal temperatures, as the temperatures must vary linearly across the cell. Figure 30 shows
an example of a node in an unstructured mesh that is not equally distant from the centroids that
surround it.
In order to compute the correct nodal temperatures for these meshes, weighted averaging must
be used instead. With weighted averaging, the contribution of each centroid to the nodal value is
weighted (multiplied) by 1/|d|, where d is the vector distance of the node from the centroid (shown
in Figure 30). The cells which are closer to the node have a smaller distance and therefore have a
larger weighting than cells which are further away.
The formula for the nodal temperature using weighted averaging is:
!
P 1
Cells Tp
|d|
TNode = ! (104)
P 1
Cells
|d|

1 2

3
5

4
Figure 30: An example node which is surrounded by 5 cells. Cells 1 and 5 are further away from
the node than cells 2, 3 and 4 and so weighted averaging must be used.

where the summation is carried out on the cell centroids that surround that particular node. This
summation might appear complicated. However, it is actually quite simple. For the example in Figure
30, the node is surrounded by five centroids. Hence the temperature of the node is:
 
T1 T2 T3 T4 T5
d
+ d2
+ d3
+ d4
+ d5
TNode =  1  (105)
1 1 1 1 1
d1
+ d2
+ d3
+ d4
+ d5

Dr. Aidan Wimshurst, Fluid Mechanics 101 36


If all the centroids are equidistant to the node (d1 = d2 = d3 = d4 = d5 ), then the formula reduces
back to the arithmetic average:
1 X 1
TNode = Tp −→ TNode = (T1 + T2 + T3 + T4 + T5 ) (106)
N Cells 5

Weighted averaging is used in both the python code and Excel sheets to calculate the nodal
temperatures that are used in the contour plots.

Closing Remarks
The unstructured mesh used in this Chapter only contains square cells with perfect quality. However,
the majority of real meshes in CFD have skewed cells with poor quality. These cells reduce the
accuracy of the simulation and can sometimes even lead to divergence of the CFD flow solver. In the
next Chapter, a variety of mesh metrics will be introduced that can be used to assess the quality of
real CFD meshes.

Dr. Aidan Wimshurst, Fluid Mechanics 101 37


Chapter 2
Mesh Quality

Dr. Aidan Wimshurst


2 Mesh Quality
The majority of CFD meshes are created with a variety of different cell types, shapes and layouts.
An example of a slice through a typical unstructured CFD mesh is shown in Figure 31. The example
mesh in Figure 31 contains three different types of cell: quadrilaterals (hexahedra in 3D), triangles
(tetrahedra in 3D) and prisms. Most modern CFD codes and mesh generators are able to process all
of these cells types, even when different cell types are used in the same mesh. This is accomplished
by constructing the CFD code in a general manner for N sided (polyhedral) cells. Hence, the user
can create a mesh using cells of any type, as long as the cells have a finite number of sides. Figure
32 shows some example cells that can all be processed by CFD codes, as long as the CFD code is
written in a general manner to handle N sided (polyhedral) cells. The unstructured mesh algorithm
in the previous Chapter is one example of a general algorithm that can be used to handle polyhedral
cells.
In addition to using different types of cell, the cells themselves often have different shapes. This
allows the mesh to conform to the geometry that is being studied. Figure 33 shows an example mesh
that contains only triangular cells (tetrahedra in 3D). Despite using cells of the same type, Figure 33
shows that the shape of the triangles themselves are different. Some of the triangles are closer to
equilateral triangles and some of the cells are highly skewed.
When constructing meshes for CFD, it is generally accepted that the user should try and generate

Quardilaterals
Triangles

Prisms

Figure 31: An example of a mesh that contains cells with a variety of shapes.

Triangle Quardilateral Pentagon Polygon

Tetrahedron Pyramid Prism Polyhedron

Figure 32: Some example cells that can be processed by CFD codes.

Dr. Aidan Wimshurst, Fluid Mechanics 101 39


Lower Quality

Higher Quality

Figure 33: An example of a mesh that contains triangular cells with different shapes.

Longest Side
Aspect Ratio =
Shortest Side

Aspect Ratio =

Figure 34: The aspect ratio of a regular quadrilateral is the ratio of the longest and shortest side
lengths

cells that are as close to perfect squares and perfect (equilateral) triangles as possible. This is because
skewed cells reduce the accuracy of the solution and in some cases can even lead to divergence of
the solver itself. To improve the shape of the cells, it is useful to have quantitative metrics that can
be used to assess how close the cells are to perfect squares/triangles. When generating the mesh,
the user can use these metrics to assess the quality of the cells and seek to improve them as much
as possible.
CFD codes and mesh generators have a variety of metrics that can be used to assess the quality
of cells in the mesh. All of these metrics have their utility. However, it is often not clear which metric
is most important for the user to optimise. In this Chapter, the main cell quality metrics will be
presented and it will be demonstrated how these metrics affect the CFD solution process. The table
below summarises the main quality metrics that will assessed in this Chapter.

Mesh Quality Metrics

Aspect Ratio
Jacobian Determinant
Non-Orthogonality
Equiangle Skewness

Dr. Aidan Wimshurst, Fluid Mechanics 101 40


Longest Distance
Aspect Ratio =
Shortest Distance

Aspect Ratio =

Figure 35: An example of how the cell aspect ratio is calculated for a pentagon using the approach
adopted by ANSYS Fluent.

Aspect Ratio
For regular quadrilaterals (where all the sides are at 90◦ to eachother), the aspect ratio is straight-
forward to calculate. As shown in Figure 34, the aspect ratio is the ratio of the length of the longest
and shortest sides. An aspect ratio of 1 indicates a perfect square.
Longest Side L
Aspect Ratio = = (107)
Shortest Side S
However, for skewed quadrilaterals (where the sides are not all at 90◦ to eachother) and for other types
of cell, it is not clear how the aspect ratio should be defined. Therefore, different mesh generators
and CFD codes use different definitions of aspect ratio. Regardless of the aspect ratio definition
adopted, it is desirable for solver stability to have aspect ratios as close to 1 as possible. This is
because cells with a high aspect ratio have faces with much larger areas than the others. These faces
have a much larger face flux contribution than the other faces. To demonstrate this point, refer to
the heat diffusion equation from Chapter 1.

0 = ∇ · (k∇T ) + S (108)

The finite volume discretisation of this equation is:


" #
X TN − TP
0= kf Af |n̂f | + Sp Vp (109)
Faces |dP N |

The summation occurs over all of the faces of the cell. If one cell has a much larger area (Af ) than
the other faces of the cell, then this face dominates the contribution from the other faces. This can
lead to instability in the system of equations if the aspect ratios are too large.

Aspect Ratio - ANSYS Fluent


In ANSYS Fluent, the distance from the cell centroid to all of the face centres and the nodes are
calculated individually. Figure 35 shows an example cell to illustrate this calculation. The aspect
ratio is then taken as the ratio of the largest of these distances (L) and the smallest (S) of these
distances.
L = max (xf 1 − xp , xf 2 − xp , xf 3 − xp , ... xf N − xp ) (110)
S = min (xf 1 − xp , xf 2 − xp , xf 3 − xp , ... xf N − xp ) (111)
L
Aspect Ratio = (112)
S

Dr. Aidan Wimshurst, Fluid Mechanics 101 41


Longest Distance
Aspect Ratio =
1 Shortest Distance

Aspect Ratio =

Figure 36: An example of the aspect ratio calculation for a perfect square, using the method
adopted by ANSYS Fluent.

Longest Side
Aspect Ratio =
Shortest Side

Aspect Ratio =

Figure 37: An example of how the cell aspect ratio is calculated by OpenFOAM for 2D faces

It should be noted that for a perfect square, this method does not compute an aspect ratio of 1. As
shown in Figure 36, the length of the diagonal (the distance from the centroid to the corner node)
is greater than the side length (the distance from the centroid to the face centre) and therefore the
computed aspect ratio is greater than 1.

0.5 2
Aspect Ratio (Square) = = 1.414 (113)
0.5
Despite this difference, this method is completely general and can be applied to skewed cells and
cells with any number of faces. Therefore, the slight difference in the aspect ratio calculation is not
critically important.

Aspect Ratio - OpenFOAM


OpenFOAM computes the aspect ratio of 2D faces and 3D cells using different approaches. For 2D
faces, the aspect ratio is computed using the ratio of the longest and shortest sides that make up the
face.
Longest Side
Aspect Ratio (2D Face) = (114)
Shortest Side
Figure 37 shows an example of this calculation, for two different example faces. With this approach,
the aspect ratio of polyhedral cells with any number of faces can be computed using the same
approach.
To compute the aspect ratio of 3D cells, a bounding box is constructed around the cell first.
This is different to the approach adopted by ANSYS Fluent. A bounding box is a cuboid that fully
contains the cell. Figure 38 shows the bounding boxes that are constructed around some example
cells. Clearly, a bounding box can always be constructed around the 3D cell, regardless of its shape.
Therefore the aspect ratio calculation is general and can be applied to any polyhedral cell.
A bounding box can be constructed by identifying the maximum and minimum x, y and z coor-
dinates of the nodes that make up the cell. These maximum and minimum coordinates then become
the corners of the bounding box. Figure 38 shows an example of this calculation.

Dr. Aidan Wimshurst, Fluid Mechanics 101 42


Maximum

Minimum

Figure 38: An example of the bounding boxes that are constructed around some 3D cells in order
to calculate the cell aspect ratio.

Once the bounding box has been constructed, the aspect ratio is computed with the following
formula:
Largest Area
Aspect Ratio = (115)
Smallest Area
where the areas are the areas of the faces of the bounding box. With this approach, the aspect ratio
of skewed cells and cells with any number of sides can all be calculated using the same approach. It
is also relatively simple to implement in a CFD code or mesh generator.

Jacobian Determinant
In a CFD mesh, it needs to be ensured that none of the cells are inverted. An inverted cell has one
or more of its nodes collapsed inwards towards the centre of the cell. This results in an internal angle
greater than 180◦ . Figure 39 shows a comparison of a regular pentagon and a pentagon where one
of the faces is inverted.
The Jacobian Determinant (often shortened to ’Determinant’ or ’Determinant 2x2x2 ’) is a check
of whether any of the cell faces are inverted. As the cells have multiple faces, the mesh generator
computes the Jacobian Determinant for each of the faces in turn and then reports the lowest value
(which corresponds with the worst face). Jacobian Determinant values less than 0 indicate that the
cell has 1 or more inverted faces, while a Jacobian Determinant value greater than 0 indicates that
none of the faces are inverted.
It is generally recommended that all of the cells in the mesh have a Jacobian Determinant greater
than 0.2. However, CFD codes can often still solve successfully with Jacobian Determinant values
as low as 0.1. Some mesh generators offer the option of calculating the ’Determinant 2x2x2 ’ or the
’Determinant 3x3x3 ’. These options are included because mesh generators can create finite element
meshes, as well as finite volume meshes. The finite volume meshes used in CFD codes only have
nodes at the corners of the cells. An example of a finite volume CFD cell is shown in Figure 40 (a).
Finite volume cells are linear because the flow quantities vary linearly between the nodes and across
the cells.
Finite element meshes can use higher-order elements, where the faces of the element contain
extra nodes. An example of a higher-order finite element is shown in Figure 40 (b). The extra node

Dr. Aidan Wimshurst, Fluid Mechanics 101 43


Jacobian Determinant 1

-1

(a) Regular Pentagon (b) Skewed Pentagon (c) Inverted Pentagon

Figure 39: A comparison of a regular pentagon, a skewed pentagon and a pentagon with an
inverted face

Flow
Quantity Extra
Nodes

Cell

(a) Linear CFD Cell (b) Higher-Order Finite Element

Figure 40: A comparison of a linear CFD cell and a higher-order finite element.

allows the quantity to vary non-linearly across the element, increasing the accuracy of the solution.
However, because of the additional node, the Jacobian Determinant calculation is slightly different.
The Jacobian Determinant calculation for higher-order elements is referred to as the Determinant
3x3x3 (where the 3 refers to the number of nodes along the face) rather than the Determinant 2x2x2.
Linear finite volume cells that are used in CFD codes do not contain additional nodes along the
faces. Therefore, the Determinant 2x2x2 and Determinant 3x3x3 calculations will produce the same
result. As the Determinant 2x2x2 is quicker to compute, this quantity is usually preferable when
constructing CFD meshes.
Details of how the Jacobian Determinant is computed will not be provided in this course, as
an extensive background of the finite element method is required. A general understanding of the
meaning of the Jacobian Determinant and how it is applied should be sufficient for the user to
generate high quality CFD meshes, without understanding the details of the calculation.

Dr. Aidan Wimshurst, Fluid Mechanics 101 44


Neighbour Cell
Owner Cell
Centroid
Face Centre

Shared Face

Figure 41: An example of a triangular cell which is connected to a rectangular cell across a shared
face. The cell centroids are connected by a vector dP N that is not necessarily parallel with the face
unit normal vector n̂f .

Owner Cell Neighbour Cell

Centroid
Face Centre

Figure 42: An example of two rectangular cells which are connected across a shared face. All of
the internal angles are 90◦ . The cell centroids are connected by a vector dP N that is parallel with
the unit normal vector n̂f .

Non-Orthogonality
The main metric which directly affects the stability of the CFD solution itself is face non-orthogonality.
To help understand how non-orthogonality arises, Figure 41 shows an example of a quadrilateral cell
and a triangular cell which are connected by a shared face. The unit normal vector at the face centre
is n̂f and the vector that connects the centroids of the cells is dP N . Unless the two cells are perfect
quadrilaterals, the unit normal vector (n̂f ) and the centroid-to-centroid vector (dP N ) will not be
parallel with eachother. The angle between these two vectors is the face non-orthogonality angle
θface . It can be calculated by rearranging the formula for the dot product:

dP N · n̂f = |dP N ||n̂f | cos (θface ) (116)


!
−1 dP N · n̂f
θface = cos (117)
|dP N ||n̂f |
An angle (θface ) of 0◦ indicates a perfectly orthogonal face, where n̂f and dP N are parallel with
eachother. Conversely, an angle greater than 90◦ indicates that the face is collapsed and the cell
is inverted. As an example of a perfectly orthogonal face, Figure 42 shows two rectangular cells
which are perfectly orthogonal to eachother (θface = 0◦ .) For stable CFD solutions, the face non-
orthogonality angle should be as close to 0◦ as possible. This ensures that the unit normal vector
and the centroid-to-centroid vector are as close to parallel as possible.
Once the face non-orthogonality has been calculated for all the faces in the mesh, the cell non-
orthogonality (θcell ) is taken as the highest non-orthogonality angle of the faces that make up that
cell.
θcell = max (θface, 1 , θface, 2 , θface, 3 , ...θface, N ) (118)

Dr. Aidan Wimshurst, Fluid Mechanics 101 45


Owner Cell Neighbour Cell

Centroid
Face Centre

Shared Face

Figure 43: An example of a triangular cell which is connected to a rectangular cell across a shared
face. The cell centroid is connected to the face centre by a vector dP F , which is highlighted in
green.

Neighbour Cell

Shared Face
Centroid
Face Centre
Owner Cell

Figure 44: An example of a triangular cell which is connected to a rectangular cell across a shared
face. The vector dP N and the unit normal vector n̂f are parallel, even though the shared face is
highly skewed.

Some CFD codes and mesh generators express the cell non-orthogonality as non-orthogonality
quality (θquality ), by normalising the computed values by 90◦ .

max (θface, 1 , θface, 2 , θface, 3 , ...θface, N )


θquality = 1 − (119)
90◦
With this approach, the non-orthogonality quality will take values between 1 and 0, rather than
between 0◦ and 90◦ . For non-orthogonality quality, it is desirable to have values as close to 1 as
possible, rather than as close to 0 as possible. Regardless of the metric employed, it is desirable to
have the unit normal vector and the centroid-to-centroid vector are as close to parallel as possible,
to ensure a stable CFD solution.
It should be noted that ANSYS Fluent uses a slight variation of equation 117 to compute the
face non-orthogonality. In addition to using the vector that connects the centroids across the face
(dP N ), ANSYS Fluent also uses the vector that connects the face centre and the cell centroid (dP F ).
This vector is shown in Figure 43. The face non-orthgonality is then calculated using an extension
of Equation 117. " ! !#
−1 dP N · n̂f −1 dP F · n̂f
θface = max cos , cos (120)
|dP N ||n̂f | |dP F ||n̂f |
The reason for this extension is that there are some instances where the vector dP N and n̂f appear
to be parallel. However, the shared face is still at a sharp angle and the original definition of face
non-orthogonality would not pick this up. Figure 44, shows an example of two connected cells where
the vectors dP N and n̂f are parallel, despite the face being highly skewed. The extended formula for
face non-orthogonality gives a better indication that this face has poor quality.

Dr. Aidan Wimshurst, Fluid Mechanics 101 46


Figure 45: An example of the unit normal vector decomposition into parallel (n̂1 ) and residual
(n̂2 ) components

Non-Orthogonal Correctors
Non-orthogonality directly affects the stability of the CFD solution because of the finite volume
discretisation of the diffusion term. To observe how non-orthogonality affects the diffusion term,
refer back to the heat diffusion equation from Chapter 1.

0 = ∇ · (k∇T ) + S (121)

In this equation, k is the thermal conductivity, T is the temperature and S represents any volumetric
sources of heat that may be present. As shown in Chapter 1, the finite volume discretisation of the
heat diffusion equation is: X
0= [kf Af ((∇T )f · n̂f )] + Sp Vp (122)
Faces

where kf is the thermal conductivity at the face centre, (∇T )f is the temperature gradient at the
face centre, Sp is the volumetric heat source at the cell centroid, Vp is the volume of the cell and
n̂f is the unit normal vector at the face centre pointing out of the cell. The summation is carried
out over all of the faces of the cell and represents the flux of heat out of the cell across each of its
faces. It is balanced by any sources of heat inside the cell (Sp Vp ) and is therefore an expression of
conservation of energy.
As shown in Chapter 1, the dot product of the temperature gradient at the face centre and the
unit normal vector can be written as:
TN − TP
(∇T )f · n̂f = |n̂f | (123)
|dP N |

only if the vectors dP N and n̂f are parallel with each other (if the face non-orthogonality θface = 0◦ ).
For the majority of meshes, these vectors are not parallel with each other (θface > 0◦ ). Hence, this
approach cannot be used. For non-orthogonal meshes (where θface > 0◦ ), the unit normal vector is
decomposed into 2 components n̂1 and n̂2 :

n̂f = n̂1 + n̂2 (124)

where n̂1 is parallel with the vector between the cell centroids (dP N ) and n̂2 is the residual component
from the decomposition. A diagram to illustrate this decomposition is shown in Figure 45. Substitute
the decomposition into the finite volume discretisation:
X
0= [kf Af ((∇T )f · n̂f )] + Sp Vp (125)
Faces
X X
0= [kf Af ((∇T )f · n̂1 )] + [kf Af ((∇T )f · n̂2 )] + SP VP (126)
Faces Faces

Dr. Aidan Wimshurst, Fluid Mechanics 101 47


As n̂1 is parallel with the vector between cell centroids (dP N ), the first summation can be simplified:
TN − TP
(∇T )f · n̂1 = |n̂1 | (127)
|dP N |
However, the second summation cannot be simplified in the same manner as n̂2 and dP N are not
parallel. Hence, the second summation is not simplified.
" #
X TN − TP X
0= kf Af |n̂1 | + [kf Af ((∇T )f · n̂2 )] + SP VP (128)
Faces |dP N | Faces

In Chapter 1, the analysis was limited to meshes made up of perfect squares and rectangles where
n̂2 = 0.
:0
" #
X TN − TP X 
0= kf Af |n̂1 | +  )f · n̂2 )] + SP VP
[kf Af((∇T 
(129)
Faces |dP N | Faces

 

However for the majority of real meshes, θface > 0, n̂2 6= 0 and the second summation term cannot
be neglected. Because (∇T )f and n̂2 are not parallel, they cannot be rewritten in terms of TN and
TP . Instead, the second summation is evaluated explicitly (directly) using the temperature gradient
computed in the previous iteration of the CFD solver. This term is called the non-orthogonal
corrector. Physically, the non-orthogonal term is lagged (delayed) one iteration behind the orthogonal
term and can lead to divergence if it is too large.
It should be emphasised that once the CFD simulation has converged, the temperature gradient
in the current iteration will be the same as the previous iteration. Hence, the decomposition of the
unit normal vector into components n̂1 and n̂2 will not affect the accuracy of the final solution.
However, the decomposition does affect the stability of the CFD simulation, as it converges.
Returning to the finite volume discretisation of the heat equation:
" #
X TN − TP X
0= kf Af |n̂1 | + [kf Af ((∇T )f · n̂2 )] + SP VP (130)
Faces |dP N | Faces

As the face non-orthogonality (θface ) increases, n̂1 decreases and n̂2 increases. Hence, the summation
over the faces gets smaller and the non-orthogonal source term gets larger. This reduces the stability
of the equations as large source terms contribute to instability. Hence, it is desirable to have θface as
small as possible, so that n̂1 is large and n̂2 is as small as possible. This reduces the strength of the
source term and increases the stability of the equations.

Equiangle Skewness
Equiangle skewness is a useful metric used by mesh generators to indicate cells which are likely to
have poor non-orthogonality. It is relatively quick and straightforward to evaluate. The equiangle
skewness for a face is the departure of the face shape from a perfect (regular) polygon with the same
number of sides as the face. As shown in Figure 46, the equiangle skewness for a triangular face (with
3 sides) is the departure of the corner angles from a perfect triangle (60◦ ). For a quadrilateral (with
4 sides), the equiangle skewness is the departure of the corner angles from a perfect quadrilateral
(90◦ ).
To quantify this departure from a regular polygon (perfect shape), start by defining the equilateral
angle θe as: 


 60◦ Triangle

 ◦
θe = 90 Quadrialteral (131)


 180 (N − 2)
N-Sided Face



N

Dr. Aidan Wimshurst, Fluid Mechanics 101 48


(a) Regular Triangle (b) Regular Quadrilateral (c) Regular Polygon

Figure 46: An example of regular polygons and their equilateral angle θe .

Evaluate each face Tetrahedral Cell


separately

Figure 47: An example of the equiangle skewness calculation for three triangular faces with
internal angles of 60◦ , 90◦ and 120◦ .

The equilateral angle can be thought of as the ideal angle of each of the face corners required to
create a perfect (regular) polygon. Any depature from this ideal angle will result in a skewed cell.
Now that the equilateral (ideal) angle has been defined, the equiangle skewness eface for a face is:
" #
θmax − θe θe − θmin
eface = max , (132)
180◦ − θe θe
where θmin and θmax are the maximum and minimum corner angles on the face. This equation is
general and can be applied to any N sided face. Figure 47 shows three example triangles (3 sides)
with different internal angles to illustrate this calculation process.
The equiangle skewness is based on the worst angle of the face. A perfect triangular or quadrilat-
eral face will result in an equiangle skewness of 0. A corner angle less than 0◦ or greater than 180◦
indicates that the face is inverted and will result in a equiangle skewness greater than 1.
Once the equiangle skewness has been calculated for all faces in the mesh, the equiangle skewness
for each cell is taken as the highest equiangle skewness for all the faces that make up the cell.
ecell = max (eface, 1 , eface, 2 , eface, 1 , ...eface, N ) (133)
In general, when constructing the mesh, the user should aim for equiangle skewness as low as possible.
ANSYS Fluent recommends that all cells in the mesh should have an equiangle skewness less than
0.95. Any cells with an equiangle skewness greater than 1 have inverted faces and will cause the CFD
solver to fail. Hence, these faces must be corrected before a solution can be attempted.

Dr. Aidan Wimshurst, Fluid Mechanics 101 49


Recommended Values
Now that the key mesh quality metrics have been introduced and described, recommended values
will be provided. These recommended values indicate the range of the quality metrics that should
be aimed for when constructing a mesh. However, it should be remembered that these values are
only indicative and values outside the recommended range can still produce solutions (or may even
diverge depending on the boundary and initial conditions)!
The table below summarises the recommended values for the various mesh quality metrics:

Recommended Mesh Quality Metrics

Metric Stable Solution Accurate Solution Ideal Mesh


Aspect Ratio < 1000 < 10 1
Jacobian Determinant > 0.1 > 0.2 1
Face Non-Orthogonality < 70◦ < 60◦ 0◦
Equiangle Skewness < 0.95 < 0.9 0

In all cases the ideal mesh is generated from perfect squares and equilateral triangles. Therefore,
the user should attempt to construct the mesh so that the cells are as close to perfect squares and
equilateral triangles as possible.

Example Problem
For the example problem in this Chapter, consider the two quadrilateral elements shown in Figure 48.
These elements will be used to demonstrate and investigate the mesh quality metrics introduced in
this Chapter. The quadrilaterals are constructed from 6 nodes and share 1 face. The location of each
of the 6 nodes are variable. By manipulating the location of the nodes, the shape of the elements
will change, allowing the mesh quality metrics to be investigated.
Now open up either the python source code or the Excel spreadsheets provided.

meshQuality.py meshQuality.xls

Try manipulating the coordinates of the various nodes and observe the changes in the mesh quality
metrics. In all cases, perfect rectangles lead to the best mesh quality metrics. In the next section,
three specific example shapes will be used to demonstrate the changes in the mesh quality metrics
that occur when the nodes are moved around.

Dr. Aidan Wimshurst, Fluid Mechanics 101 50


3 2 5

Cell 0 Cell 1

0 1 4

Figure 48: The geometry of the example problem for Chapter 2. Two rectangular cells are
constructed from 6 nodes, which are labelled 0 to 5 in the diagram

Results

Cell 0 Cell 1 Cell 0 Cell 0


Cell 1
Cell 1

(a) Mesh 1 (b) Mesh 2 (c) Mesh 3

Figure 49: A diagram to show the mesh configurations investigated in the example problem by
moving node 2 in the lateral direction

Figure 49 shows the three example meshes that will be analysed using the python code and Excel
spreadsheets in this Chapter. These meshes were generated by moving node 2 (the middle node on
the top row) in the lateral direction towards node 5. The table below shows the results of the quality
metrics for Cell 0 (the left cell) and Cell 1 (the right cell), as the node 2 is moved in the lateral
direction.

Mesh Quality Metrics for Cell 0

Mesh Node 2 Aspect Ratio Aspect Ratio es θface θcell


(x, y) (Fluent) (OpenFOAM) [-] [◦ ] [-]
1 (1.0, 2.0) 2.24 2.00 0.00 0.0◦ 1.00
2 (1.4, 2.0) 2.13 2.04 0.13 11.3◦ 0.87
3 (1.8, 2.0) 2.12 2.15 0.24 21.8◦ 0.76

Dr. Aidan Wimshurst, Fluid Mechanics 101 51


Mesh Quality Metrics for Cell 1

Mesh Node 2 Aspect Ratio Aspect Ratio es θface θcell


(x, y) (Fluent) (OpenFOAM) [-] [◦ ] [-]
1 (1.0, 2.0) 2.24 2.00 0.00 0.0◦ 1.00
2 (1.4, 2.0) 2.92 3.40 0.13 11.31◦ 0.87
3 (1.8, 2.0) 4.07 10.77 0.24 21.8◦ 0.76

As node 2 is moved in the lateral direction, both cells (Cell 0 and Cell 1) become more skewed.
This is reflected in the equiangle skewness (es ), face non-orthogonality angle (θface ) and the cell
non-orthogonality quality (θcell ), which are all worse for mesh 3 than mesh 2 and mesh 1. This simple
investigation is an excellent example to show that low quality (skewed) cells are reflected in all of
the main quality metrics. Therefore, when constructing the mesh as a user, it does not really matter
which metric is used to identify poor quality cells in the mesh, as long as these cells are improved
before they are sent to the CFD solver.
As node 2 is moved in the lateral direction, the aspect ratio of Cell 1 increases significantly
because the length of the top side of the cell reduces. However, the aspect ratio of Cell 0 is relatively
unaffected by the movement of node 2. Therefore, this simple investigation shows that to identify
skewed faces and cells, it is more convenient to use the equiangle skewness and non-orthogonality
metrics, as all of these metrics get worse as the cell is skewed. The aspect ratio metric is more useful
to assess the thin cells that are often used in inflation layers (prism layers) near the walls of CFD
geometries.

Dr. Aidan Wimshurst, Fluid Mechanics 101 52


FluidMechanics101@gmail.com

FluidMechanics101

FluidMechanics101

Aidan Wimshurst

Aidan Wimshurst

1 0 1 0 1 1 0 1 0 0 1 0 0 1 0 0 1 0 1 1 0 1 0 1 1 0 0 0 1 0 1 0 0 1 1 0 1 0 1 1 0

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy