Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 21

Course number: MEEN 5331

Course Name: CFD with Open FOAM

Project 1
Solving two-dimensional Poisson equation

Submitted to
Dr. Ping He
Assistant Professor
Mechanical Engineering

Submitted by
Saikat Sarkar
Lamar ID L20553231

Department of Mechanical Engineering


Lamar University
Table of contents

1. Introduction
2. Problem Statement
3. Solution Methodology
4. Result and discussion
5. Conclusion
6. References
7. Appendix
1. Introduction:

CFD (Computational fluid dynamics) is the brunch of fluid mechanics which uses numerical
analysis and data structure for analyzing and solving problems related to fluid flows.

Finite difference method is the simplest method to solve CFD problems. It is a numerical
technique for solving differential equations. It converts ordinary differential equations or partial
differential equations into a system of linear equations which is solved by the matrix algebra
technique.

This report will represent the finite difference method technique to solve the 2D Poisson
equation problem using MATLAB software. The results of the problem will be visualized with a
2D color map.
2. Problem Statement:

Solve the Poisson equation of the following domain with the specified boundary conditions.

𝜕!𝑈 𝜕!𝑈 !
+ = 3𝑥 + 4𝑦!
𝜕𝑥 ! 𝜕𝑦 !

Finite difference method will be used to solve 𝑈 = 𝑈 (𝑥, 𝑦). In the report, present the solution
procedures, and include the plot of 2D color distribution of the solution field. Also include the
MATLAB code in the appendix within the report document.
3. Solution Methodology:

According to the problem statement in this report, a Poisson equation needs to be solved using
finite difference method for the below equation with provided boundary conditions:

𝜕!𝑈 𝜕!𝑈 ! !
+ =3 + 4𝑦 𝐸𝑞. (1)
𝜕𝑥 ! 𝜕𝑦 !

According to the Poisson equation:

𝜆∇! 𝑈 = 𝑓 𝐸𝑞. (2)

⇒ ∇ ⋅ (𝜆∇𝑈) = 𝑓 𝐸𝑞. (3)

This above Eq. (2) and Eq. (3) are not same

Eq. (2) is for constant property of 𝜆

And Eq. (3) is for variable property of 𝜆

Poisson equation is a part of the fluid flow equation which came from the governing equations of
fluid flow.

The governing equation of fluid flows are:


>> Conservation of mass equation ⟹ Continuity equation
>> Conservation of momentum ⟹ Navier -Stokes equation

According to the continuity equation:


𝜕𝜌
+ ∇ ⋅ (𝜌𝐔) = 0 𝐸𝑞. (4)
𝜕𝑡
Here,
𝜌 = density
U = velocity
! ! ! ! ! !
And ∇ is a differential operator and the value of ∇ = 𝑥% + 𝑦% + 𝑧̂ , , }
!" !# !# !" ! !$
={ #
Thus, 𝜕𝑈 𝜕𝑈 𝜕𝑈
" # $
∇⋅𝐔= + + 𝐸𝑞. (5)
𝜕𝑥 𝜕𝑦 𝜕𝑧
Now from Eq. (4) we get:

𝜕𝜌 𝜕(𝜌𝑈" ) 𝜕=𝜌𝑈# > 𝜕(𝜌𝑈$ )


+ + + =0 𝐸𝑞. (6)
𝜕𝑡 𝜕𝑥 𝜕𝑦 𝜕𝑧

In case of low Mach# application:


Mach# ≪ 1
Then,

𝜕𝜌
𝜕𝑡 = 0

This situation is called an incompressible fluid flow.


So,
𝜕𝑈 𝜕𝑈 𝜕𝑈
" # $
∇⋅𝐔=0⇒ + +
=0 𝐸𝑞. (7)
𝜕𝑥 𝜕𝑦 𝜕𝑧

Divergence of the velocity vector means that the sum of mass flows in minus the sum of mass
flows out.

According to Navier -Stokes equation for Newtonian fluids,


𝜕(𝜌𝐔)
+ ∇ ⋅ (𝜌𝐔𝐔) = −∇𝑝 + 𝜇∇! 𝐔 + 𝜌𝑔 𝐸𝑞. (8)
∂𝑡

Where,

!(#𝐔)
= change of momentum due to time
&'
∇ ⋅ (𝜌𝐔𝐔) = change of momentum due to flows in and flows out

−∇𝑝 = gradient of static internal stress

𝜇∇! 𝐔 = viscose force of dynamic internal stress

Let’s assume the density is constant, so from Eq. (8) we get


𝜕 (𝐔)
𝜌 + 𝜌∇ ⋅ (𝐔𝐔) = −∇𝑝 + 𝜇∇! 𝐔 + 𝜌𝑔
∂𝑡

Now dividing both sides with 𝜌, we get


𝜕(𝐔) ∇𝑝
+ ∇ ⋅ (𝐔𝐔) = − 𝜇!
+ ∇ 𝐔+𝑔
∂𝑡 𝜌 𝜌

but,
𝜇
𝜈=
𝜌
Thus,
𝜕 (𝐔) ∇𝑝
+ ∇ ⋅ (𝐔𝐔) = − + 𝜈∇! 𝐔 + 𝑔 𝐸𝑞. (9)
∂𝑡 𝜌

As finite difference method needs to be used for calculation, let’s drive the finite difference
model.

Finite difference method directly models the (partial) differential terms using the finite difference
terms:

𝑑𝑈 𝑈(𝑥 + ∆𝑥) − 𝑈(𝑥) 𝑈(𝑥 + ∆𝑥) − 𝑈(𝑥)


𝑑𝑥 = lim
∆"→' ∆𝑥 ≈ ∆𝑥 + 𝑂(∆𝑥) 𝐸𝑞. (10)

We can use the Taylor series to increase the accuracy of finite differential terms:

𝑈(𝑥 + ∆𝑥) = 𝑈 (𝑥) + 𝑈((𝑥) ∆𝑥 + )


!! 𝑈 (( (𝑥) ∆𝑥 ! + 𝑂(∆𝑥 + )
𝑈(𝑥 + ∆𝑥) = 𝑈 (𝑥) + 𝑈((𝑥) ∆𝑥 + )
!! 𝑈 (( (𝑥) ∆𝑥 ! + 𝑂 (∆𝑥 + ) Eq. (11)
𝑈(𝑥 − ∆𝑥) = 𝑈 (𝑥) − 𝑈((𝑥) ∆𝑥 + )
!! 𝑈 (( (𝑥) ∆𝑥 ! + 𝑂 (∆𝑥 + ) Eq. (12)

Eq. (11) - Eq. (12) generates the first order derivatives:

𝑈 (𝑥 + ∆𝑥) − 𝑈(𝑥 − ∆𝑥) = 2 𝑈 ( (𝑥)∆𝑥 + 𝑂(∆𝑥 ! )


Thus,
𝑈(𝑥 + ∆𝑥) − 𝑈 (𝑥 − ∆𝑥)
𝑈 ( (𝑥) = + 𝑂(∆𝑥 ! ) 𝐸𝑞. (13)
2∆𝑥
Again, we can expand with Taylor series:

𝑈(𝑥 + ∆𝑥) = 𝑈 (𝑥) + 𝑈((𝑥) ∆𝑥 + )


!! 𝑈 (( (𝑥) ∆𝑥 ! + )
𝑈 ((( (𝑥) ∆𝑥 + + 𝑂 (∆𝑥 , ) Eq. (14)
+!

𝑈(𝑥 − ∆𝑥) = 𝑈 (𝑥) − 𝑈((𝑥) ∆𝑥 + ) 𝑈 (( (𝑥) ∆𝑥 ! − )


𝑈 ((( (𝑥) ∆𝑥 + + 𝑂 (∆𝑥 , ) Eq. (15)
!! +!

Eq. (14) + Eq. (15) generates the following second order derivatives:

𝑈(𝑥 + ∆𝑥) + 𝑈(𝑥 − ∆𝑥) = 2𝑈(𝑥) + 𝑈 (( (𝑥)∆𝑥 ! + 𝑂(∆𝑥 ! )

𝑈 (( (𝑥)∆𝑥 ! = 𝑈(𝑥 + ∆𝑥) + 𝑈(𝑥 − ∆𝑥) − 2𝑈(𝑥) + +𝑂(∆𝑥 ! )

𝑈(𝑥 + ∆𝑥) + 𝑈(𝑥 − ∆𝑥) − 2𝑈(𝑥)


𝑈 (( (𝑥 ) = ∆𝑥 ! + +𝑂(∆𝑥 ! ) 𝐸𝑞. (16)

The above finite difference schemes are central differences.

Figure 1: Finite difference scheme with central difference

First order derivative with second order accuracy:

𝑈-.) − 𝑈-/)
𝑈( = + 𝑂(∆𝑥 ! ) 𝐸𝑞. (17)
-
2∆𝑥

Second order derivative with second order of accuracy:

𝑈-.) − 2𝑈- + 𝑈-/)


𝑈 (( = + 𝑂(∆𝑥 ! ) 𝐸𝑞. (18)
-
∆𝑥 !
The values of the derivatives can be calculated using either the table of coefficients of central
finite differences or using the table of forward finite difference.
Both the tables are illustrated bellow:

Table 1:Table contains coefficients of central finite differences [1]

Table 2: Table contains coefficients of forward finite differences [1]


The partial differential equation will be decomposed to get the linear system. Finally, the problem
will be solved using matrix algebra method:

[ 𝐴 ] {𝑥} = {𝑏} 𝐸𝑞. (19)

This matrix 𝐴 may be symmetric or non-symmetric, but it must be a square matrix. It means for
this matrix:
𝑁𝑢𝑚𝑏𝑒𝑟 𝑜𝑓 𝐸𝑞𝑢𝑎𝑡𝑖𝑜𝑛𝑠 = 𝑁𝑢𝑚𝑏𝑒𝑟 𝑜𝑓 𝑈𝑛𝑘𝑛𝑜𝑤𝑛𝑠

Let ‘s consider a simple grid of (𝑥 , 𝑦) to solve the given Poisson equation

𝑁# + 1
𝑁#

1
1 𝑖→ 𝑁" 𝑁" + 1

Figure 2: Representation of a simple grid of (x, y) to solve the Poisson equation

Let’s use the nodes (grid points) instead of the cell centers, because in this problem we have only
one unknown field, 𝑈

𝑖 = 1, 2, 3, … … … … … … … , 𝑁" + 1

𝑗 = 1, 2, 3, … … … … … … … , 𝑁# + 1

To describe each grid point, let’s use a pair of indices, (𝑥 , 𝑦).

So, for the bulk points:


𝑖 = 2, 3, 4, … … … 𝑁"

𝑗 = 2, 3, 4, … … … 𝑁#

For calculating the bulk point according to the given problem, second order derivative will be
used:

𝜕!𝑈
𝜕!𝑈
𝜆w ! + ! x= (𝑥, ) 𝐸𝑞. (20)
𝜕𝑥 𝜕𝑦

Using the table 2 (coefficient of forward finite differences method), we get:

𝑈-/),1 − 2𝑈-1 + 𝑈-.),1


⇒ 𝜆 y 𝑈-,1/) − 2𝑈-1 + 𝑈-1.)
∆𝑥 ! + z = 𝑓-1 𝐸𝑞. (21)
∆𝑦 !

Dividing both sides of the Eq. (21) by ∆𝑥 we


get:
∆𝑥 ! 𝑓-1 !
⇒ 𝑈-.),1 − 2𝑈-1 + 𝑈-/),1 + =𝑈-,1/) − 2𝑈-1 + 𝑈-,1/) > = ∆𝑥 𝐸𝑞. (22)
∆𝑦 ! 𝜆
According to the given problem,
𝑓-1 = 3𝑥 ! + 4𝑦 !
And
𝜆 =1

So, from Eq. (22) we get:

∆𝑥 ! ! !) !
𝑈-.),1 − 2𝑈-1 + 𝑈-/),1 + =𝑈-,1/) − 2𝑈-1 + 𝑈-,1/) > = (3𝑥 + 4𝑦 ∆𝑥 𝐸𝑞. (23)
∆𝑦 !
This equation Eq. (23) will be applied for the bulk points.

For the boundary points, all the boundaries will be calculated according to the boundary
conditions.

For left side of the problem domain, the given boundary condition is 34 = 0.
3"

So, for the left side we consider the first order derivative (forward finite differences method)
using the table 2. We get:
𝜕𝑈
= 0 ⇒ 1.5 − 2𝑈 + 0.5 𝑈 = 0 𝐸𝑞. (24)
-5 -/),1 -/!,1
𝜕𝑥

This equation Eq. (24) will be applied for the left side boundary points.

And for left side boundary point calculation:


𝑖 =1

𝑗 = 1, 2, 3, … … … 𝑁# + 1

For the right side of the problem domain, the given boundary condition is 34 = 0.
3"

So, for the right side, we consider the first order derivative (reverse finite differences method).

Using the table 2, we get:


𝜕𝑈
= 0 ⇒ −1.5𝑈
+
𝑈2 − 0.5 𝑈 = 0 𝐸𝑞. (25)
-,1 -/),1 -/!,1
𝜕𝑥

This equation Eq. (25) will be applied for the right-side boundary points.

For right side boundary point calculation:

𝑖 = 𝑁" + 1

𝑗 = 1, 2, 3, … … … 𝑁# + 1

For the lower side of the problem domain, there are two segments 𝑈 = 3 and 𝑈 = 2.

For 𝑈 = 3:
The right side of the equation is 3.
The coefficient of 𝑈-1 means coefficient of 𝑈 is 1.
For 𝑈 = 2:
The right side of the equation is 2.
The coefficient of 𝑈-1 means coefficient of 𝑈 is 1.

For the lower side boundary point calculation:

𝑖 = 2, 3, 4, … … … … … . 𝑁"

𝑗 =1

For the upper side of the problem domain, there are two segments 𝑈 = 0 and 𝑈 =

1. For 𝑈 = 0:
The right side of the equation is 0.
The coefficient of 𝑈-1 means coefficient of 𝑈 is 1.

For 𝑈 = 1:
The right side of the equation is 1.
The coefficient of 𝑈-1 means coefficient of 𝑈 is 1.

For the upper side boundary point calculation:

𝑖 = 2, 3, 4, … … … … … . 𝑁"

𝑗 = 𝑁# + 1
We have considered (𝑖, 𝑗) as the indices for grid points, but for the sparse matrix formation, we
need matrix conversion. Let ‘s consider (𝐼, 𝐽) as matrix points.
From Eq. (19):
[ 𝐴 ] {𝑥} = {𝑏}

Where [ 𝐴 ] is a square matrix.


So, we need to convert the 2D index (𝑖 , 𝑗) into 1D index 𝐼.
The equation of matrix points will be:

𝐼 = (𝑖 − 1) ∗ =𝑁# + 1> + 𝑗 𝐸𝑞. (26)

In our calculation, vertical counting manner will be used to number all the points from 1 to the
total number of points. Each equation will be for the 𝐼-th row. i,e. (𝑖, 𝑗)-th row. the element of
the matrix will be for the coefficient before 𝑈67:

(𝑎, 𝑏) can be (𝑖 − 1, 𝑗), (𝑖, 𝑗), (𝑖 + 1, 𝑗), (𝑖, 𝑗 − 1), (𝑖, 𝑗 + 1)

(𝑎, 𝑏)-th column has the value of the matrix as the coefficient before 𝑈67 .

Using this methodology, the Poisson equation will be solved for the given problem using sparge
matrix system by MATLAB software. Detail codes for this problem are included in the appendix.
4. Result and discussion

The provided partial differential equation has been decomposed to a linear system. The problem
has been solved by matrix algebra method:
[ 𝐴 ] {𝑥} = {𝑏}

In this problem, sparse matrix system has been used.


We have calculated, considering total number 𝑁" = 1000 and 𝑁# = 500
So, total grid points are 501501.
The size of the matrix [ 𝐴 ] = All zero sparse: 501501 X
501501. The non-zero elements of this matrix are 2497509.
So, the percentage of non-zero element is 9.9303e-4 %
The non-zero elements of this sparse matrix are diagonal:

Figure 3: Sparse matrix with non-zero element


After representing the solution using 2D color distribution, it is observed that there is no change
in the left side and right-side boundary of the problem domain, which represent the provided
34
boundary condition: = 0.
3"

The lower side the color map perfectly represents the boundary conditions 𝑈 = 3 and 𝑈 =

2. Also, the upper side the color map satisfies the boundary conditions 𝑈 = 0 and 𝑈 = 1.

Smooth transition also been observed from lower boundary to upper boundary which also reflects
the boundary conations.

There are two abrupt changes to be noted, one in the lower boundary and one in the upper
boundary because no specific boundary condition is defined in that points.

Figure 4: 2D color distribution of the solution field


5. Conclusion

The Poisson equation has been solved using MATLAB software for the provided problem with
defined boundary conditions and the results are represented by a 2D color map.

The given partial differential equation was decomposed to get the linear system. Finally, the
problem has been solved using matrix algebra method.

The 2D color map of the solution field perfectly represents the boundary conditions provided in
the problem.

6. References

1) https://en.wikipedia.org/wiki/Finite_difference_coefficient

7. Appendix

clear

Nx = 1000; % cell # on x
Ny = 500; % cell # on y
Lx = 0.08; % Lx = 0.04+0.04 = 0.08
Ly = 0.04; % Ly = 0.02+0.02 = 0.04
dx = Lx / Nx;
dy = Ly / Ny;
dx2_dy2 = dx*dx / (dy*dy);

Ntot = (Nx + 1) *(Ny +1); % total grid points


A = sparse (Ntot, Ntot);

lambda = 1.0;

[ X, Y] = meshgrid(-0.04:dx:0.04, -0.02:dy:0.02);

[ny, nx] = size(X)


[ny, nx] = size(Y)

% this above equation tells us the (i,j)th row of the bulk equations
% we have a square matrix, Ntot by Ntot, we need to convert the 2D index
(i,j) into 1D index I
% how to convert (i,j) to I can be varied in different application
% but in every individual case, it has to well defined
% in this case, we use a vertical continuing manner to number all the
% Points from 1 to Ntot
% Each equation will be for the I-th row. i, e. (i,j)th row
% the element of the matrix will be the coefficient before Uab
% (a,b) can be (i-1,j),(i,j),(i+1,j), (i,j-1), (i,j+1)
% (a,b)th column has the value of the matrix as the coefficient before Uab

% Bulk points

for i = 2:Nx
for j = 2:Ny

% This iteration goes through all bulk points


% Each time we have point (i, j)
% I represents the I-th row in the matrix

I = (i-1)*(Ny+1)+j;

% I-Ny-1 is the column index for the first nonzero element


% the smallest column is (i-1,j)
% J = (i-1-1)*(Ny+1)+j = (i-1)*(Ny+1)+j-(Ny+1) = I-Ny-
1 A(I,I-Ny-1) = 1;
% the next column is (i,j-1)
% J2 = (i-1)*(Ny+1)+j-1 = I-1
A(I,I-1) = dx2_dy2;
A(I,I) = -2 -2 *dx2_dy2;
A(I,I+1) = dx2_dy2;
A(I,I+Ny+1) = 1;
% (x,y) is the cordinate pair for (i,j)
x = X(j,i);
y = Y(j,i);

fxy = 3*x^2 + 4*y^2;


RHS = fxy / lambda * dx * dx ;
b(I) = RHS;
end
end

% boundary points

i = 1;
for j = 1: Ny+1
I = (i-1)*(Ny+1)+j;
A(I,I) = -1.5;
A(I,I+Ny+1) = 2;
A(I,I+2*Ny+2) = -0.5;

b(I) = 0;
end

i = Nx +1;
for j = 1: Ny+1
I = (i-1)*(Ny+1)+j;
A(I,I) = 1.5;
A(I,I-Ny-1) = -2;
A(I,I-2*Ny-2) = 0.5;
b(I) = 0;
end

j = 1;
for i = 2:Nx
I = (i-1)*(Ny+1)+j;
A(I,I) = 1;

if X(j,i) < 0

b(I) = 3;
else
b(I) = 2;
end
end

j = Ny+1;
for i = 2:Nx
I = (i-1)*(Ny+1)+j;
A(I,I) = 1;

if X(j,i) < 0

b(I) = 0;
else
b(I) = 1;
end
end

A;
spy(A)
b = b';
x = A\b;

% we are going to reshape the solution field to 2d from 1d


for i = 1:(Nx+1)
for j = 1:(Ny+1)
I = (i-1)*(Ny +1) + j;
F(j,i) = x(I);
end
end figure(2);clf;hold on pcolor(X,Y,F); shading interp;
colorbar('northoutside'); colormap('turbo')
axis equal tight; xlabel('x (mm)')
ylabel('y (mm)') box on
set (gca, 'fontsize', 12, 'linewidth', 1)

You might also like