Download as pdf or txt
Download as pdf or txt
You are on page 1of 30

Lecture 11:

Useful Data Structures


Convection and Diffusion
Last Time …

Last time, we looked at two different ways of computing


cell gradients
z Gradient theorem
z Linear least squares
This Time …

We will
z Discuss implementation issues and data structures
z Start looking at adding convection terms to our
transport equation
z Start looking at two different schemes for discretizing
the convection terms
» Central difference scheme
» Upwind difference scheme
Data Structures

z In general, it is wise to carry both face and cell based data


structures
z Flux-based operations are best done using face-based data
structures.
z Source term, unsteady term, Gauss-Seidel iteration typically need
cell-based data structures.
z A typical code would loop over faces, finding the anb coefficients
of the two cells sharing it.
z Then a loop over cells completes the calculation of source
contributions, unsteady terms, under-relaxation, and finally, aP.
z After this, Gauss-Seidel iteration would loop over cells.
Face-Based Data Structures

z All faces would be numbered.


z Each face stores the indices of its C0 and C1 cells.
z Each face would also store face geometry information, such its
area vector and a normal (outward with respect to C0)
z The idea is to loop over the faces, find the coefficient
contributions for the face flux, and add the appropriate
contributions to the neighbor coefficients for the C0 and C1 cells
z Would like to access the coefficients anb of the C0 and C1 cells
directly if possible.
Cell-Based Data Structures

z Each cell would store its aP , anb , and b.


z Each cell would have a list of indices of the face
neighbors for use in the iterative solver.
Structured Mesh Storage Strategies

z For structured meshes, face indices would be related to cell


indices (see next slide)
» If you know the face indices, you automatically know the
indices of the two cells on either side.
z Similarly, the cell loop is also easy. For cell (I,J) – neighbors are
(I-1,J), (I+1,J), (I,J+1),(I,J-1)
» Sometimes continuous numbering using single index is
preferred due to long vector length for vector machines
z Each cell would store AE(I, J), AW(I,J), … AP(I,J), B(I,J).
Face Numbering

NI*NJ Left justified


indexing of
J=1,2,…,NJ face:

Cell
I,J Face (I,J) I,J

1 2 NI

I =1,2,…NI
Continuous Cell Numbering

NI*NJ
Finding L from I,J:

J=1,2,…,NJ L = I+ (J-1)*NI

Reverse:
I,J
J=L/NI +1
I = L-(J-1)*NI
1 2 NI

I =1,2,…NI
Typical Loop for Structured Meshes

/* loop over interior I faces */


for (i=2;i<NI+1;i++)
for(j=1;j<NJ+1;j++)
{
gamma_face = harmonic mean of (gamma[i-1][j],
gamma[i][j]
coeff = gamma_face*area_face/delta_x
ae[i-1][j] =coeff;
aw[i][j] =coeff;
}
Structured Mesh (Cont’d)

/* now loop over cells */


for(i=1;i<NI+1; i++)
for (j=1;j<NJ+1;j++)
{
sp=XXX
sc=XXX
ap[i][j]=ae[i][j]+aw[i][j]… -sp*delta_V
b[i][j] = sc*delta_V
}
Structured Mesh (Cont’d)

/* Gauss-Seidel loop over cells */


for(iter=1;iter<itermax;iter++)
for(i=1;i<NI+1; i++)
for (j=1;j<NJ+1;j++)
{

phi[i][j] = ( ae[i][j]*phi[i+1][j] +aw[i][j]*phi[i-1][j]+


..+b[i][j] )/ap[i][j]
}
Unstructured Mesh Storage

z Many different ways to do this.


z Main difficulties
» Connectivity not known unlike structured meshes
– Cannot tell neighbor cell’s index by looking at
current cell
» Number of neighbors is variable
– Cannot therefore use AE, AW, AP etc
» Must take care to arrange that face “knows” cell
indices and how to access cell storage for
coefficients
Compressed Row Storage

z Let the mesh have N cells


z Let B be the total number of cell neighbors in the
domain

B = 8 for this case


Compressed Row Storage (Cont’d)

z Allocate one integer array NBINDEX(B) and one real


array COEFF(B)
z Also allocate another integer array CINDEX(N+1)
z Idea is that we will store all neighbor indices for all
cells in NBINDEX and all neighbor coefficients in
COEFF
» Will use CINDEX to tell which chunk of NBINDEX
and COEFF belong to which cell
Defining CINDEX

z CINDEX (N+1) is defined as:


Neighbor Index Storage

z The indices of the neighbors of cell i are stored in


locations j in NBINDEX :
CINDEX(I) <= j < CINDEX(i+1) For cell 3, the
neighbor cell
indices are to
be found in
locations 5-6 of
NBINDEX
The indices of
the neighbor
cells are 2, 4
Coefficient Storage

z Same type of idea for coefficients


z In COEFF store the coefficients to the neighbor cells in
the locations corresponding to NBINDEX
z In addition, allocate another real array of length N to
store center coefficient AP(N)
Contains coeff
values

COEFF
Face-Based Storage for Unstructured
Meshes
z Each face would know its two cells.
z Say you’re at face 50 – the two cells are 2 and 3.
z Go to CINDEX [2] – read off “3”
z The neighbor indices of cell 2 lie in locations 3 and 4 of
NBINDEX. The cell neighbor indices are 1 and 3.
z COEFF [3] and COEFF[4] store coeffs for cells 1 and 3. Find
value of the coeff for face 50 (connecting cells 2 and 3) and put it
in COEFF[4]. Also put it in COEFF[5].

50
Unstructured Gauss-Seidel Pseudo Code

+
Solution Loop
Solution Loop (Cont’d)
Steady 2D Convection-Diffusion Equation

z Governing equation:

Assume flow field


known
Assume Cartesian
structured mesh
Discretization

z As usual, integrate over control volume

z Apply divergence theorem, linearize source term:

Nothing different
so far !
Discretization (Cont’d)

z Area vectors:

z Flux on east face:

Units?

z Flow rate on east face:


Diffusion Term

z Write as usual assuming linear profile between (E,P)


etc:
Grid Peclet Number

z Face flux*area written in terms of two coefficients


Multiplies
face gradient

Multiplies face
value φe: How to
evaluate?

z Define grid Peclet number


Central Difference Scheme (CDS)

z Find face value of φ using cell average:

Assuming
uniform mesh

z Convection through face e:

Same sign! Trouble


ahead…
CDS: Discrete Equation

z Note possibility of
negative
coefficients
z Note extra flow
rate term in aP
Closure

In this lecture:
z We considered useful data structures for structured
and unstructured meshes
z We considered the steady 2D convection-diffusion
equation
z Convection term requires evaluation of φ on the faces
z Developed CDS scheme

You might also like