Sei sulla pagina 1di 9

ASSIGNMENT

CFD

By
SHOAIB JAVED
(MSME-24)
Submitted To
DR. AJMAL SHAH

Department of Mechanical Engineering,


Pakistan Institute of Engineering & Applied Sciences,
Nilore, Islamabad, Pakistan.
ASSIGNMENT TASK

Write a computer program in C++/Fortran to solve the


system of linear equations generated in the previous
assignment for solution of 1D convection-diffusion equation.
Use the TDMA m (Tridiagonal matrix algorithm) method.

Background:
The system of linear equations were generated for solving 1D convection-diffusion equation:

Solution:
In the previous assignment we have written our code for finding coefficient matrix A and matrix
B but calculated values of ɸ with the help of MATLAB but in this assignment we will write code
for solving system of linear equations in C++ using tridiagonal matrix algorithm. The result will
be a solution matrix ɸ giving all the values of variable at all nodes as well as boundary points. This
matrix can be plotted in MATLAB against the geometric coordinates.
Detail of Code:
In our already generated coefficient matrix A we had only non -zero entries on principal diagonal
and on two other diagonals which are adjacent to principal diagonal. We will first do forward
elimination and then back substitution to get our desired matrix ɸ. Code used for this purpose is
given below:

for(i=2;i<=Nu;i++)
{
A[i][i]=A[i][i]-A[i-1][i]*A[i][i-1]/A[i-1][i-1];
B[i]=B[i]-B[i-1]*A[i][i-1]/A[i-1][i-1];
}
fi[Nu]=B[Nu]/A[Nu][Nu];
for(i=Nu-1;i>0;i--)
fi[i]=(B[i]-A[i][i+1]*fi[i+1])/A[i][i];
printf("\n solution matrix \n");
for(i=0;i<=Nu+1;i++)
printf("%f\n",fi[i]); //(Will also display boundary nodes values)

PROGRAM OUTPUT:
For conditions used in book:
GRAPH:
COMPLETE CFD PROGRAM FOR SOLVING 1D CONVECTION/DIFFUSION
EQUATION

// Program for solving steady 1D convection/diffusion equation including solver for solving
linear system of equations in C++

#include<stdio.h>
#include<conio.h>
#include<math.h>
int main( )
{
// Inputs section
int Nu=110;
float Ln,D,ve,G, fi[110]={0};
printf("\n Please enter the length of Geometric Domain= ");
scanf("%f",&Ln);
printf("\n Please enter the value of the Density= ");
scanf("%f",&D);
printf("\n Please enter the value of Velocity= ");
scanf("%f",&ve);
printf("\n Please enter the value of Diffusion Coefficient= ");
scanf("%f",&G);

//Discretizing Section (Number of nodes are found from geometric series summation
formula)
int i,j;
float a,y,n,x[110]={0};
for(;Nu>110;)
{
printf("\n Please enter the spacing between zeroeth and 1st node: ");
scanf("%f",&a);
printf("\n Please enter the reduction factor in mesh spacing: ");
scanf("%f",&y);

if (y<1)
{
if(Ln<a/(1-y))
{
n=log(1-Ln*(1-y)/a)/(log(y));
Nu=(int)n;
if(Nu>110)
printf("\n\t Please re enter values as num of nodes have exceeded 110 \n");
else
printf("\n\t The Domain is divided into total of %d nodes \n",Nu);
}
else
printf("\nThe given matrix does not approach geometrically to Domain Length \n");
}

else
{
n=log(1-Ln*(1-y)/a)/(log (y));
Nu=(int)n;
if(Nu>110)
printf("\n The Total Number of Nodes exceed 110, please re-enter the values so that N<110 \n");
else
printf("\n The Domain will be divided into Total Number of %d Nodes \n", Nu);
}

}
for (i=1;i<=Nu;i++)
x[i]=x[i-1]+pow(y,i-1)*a;
x[Nu+1]=Ln;
// Boundary Condition input
float Pe, A[110][110]={{0}}, B[110]={0};
float Acw,Ace,Adw,Ade,Acp,Adp;
printf("\n Please enter the value of variable FI at x=0 : ");
scanf("%f",&fi[0]);
printf("\n Please enter the value of variable FI at x=L : ");
scanf("%f",&fi[Nu+1]);
Pe=D*ve*Ln/G;
printf("\n Note: \n\tPeclet Number for this flow is %f \n", Pe);
if (ve>0)
printf("\n For positive velocity, backward difference scheme will be used for approximating
convective term \n");
else if (ve>0)
printf("\n For negative velocity, forward difference scheme will be used for approximating
convective term \n");
//Generation of Sparse(Co-efficient) Matrix A

for(i=1;i<=Nu;i++)
{
if(ve>0)
{
Acw=-D*ve/(x[i]-x[i-1]);
Ace=0;
}

else if(ve<0)
{
Acw=0;
Ace=-D*ve/(x[i]-x[i-1])
}
Adw=-2*G/((x[i]-x[i-1])*(x[i+1]-x[i-1]));
Ade=-2*G/((x[i+1]-x[i])*(x[i+1]-x[i-1]));
Adp=-(Adw+Ade);
Acp=-(Acw+Ace);
A[i][i]=Adp+Acp;
A[i][i-1]=Adw+Acw;
A[i][i+1]=Ade+Ace;
}

//Generation of Contstant Matrix B

B[1]=-A[1][0]*fi[0];
B[Nu]=-A[Nu][Nu+1]*fi[Nu+1];

//Display of Constant Matrix B

printf("\n CONSTANT MATRIX B \n");


for(i=1;i<=Nu;i++)
printf("%f\n",B[i]);

//Display of Sparse Coefficient Matrix A

printf("\n SPARSE MATRIX A \n");


for(i=1;i<=Nu;i++)
{
for(j=1;j<=Nu;j++)
printf("%f\t",A[i][j]);
printf("\n");
}
//Solution Part

for(i=2;i<=Nu;i++)
{
A[i][i]=A[i][i]-A[i-1][i]*A[i][i-1]/A[i-1][i-1];
B[i]=B[i]-B[i-1]*A[i][i-1]/A[i-1][i-1];
}
fi[Nu]=B[Nu]/A[Nu][Nu];
for(i=Nu-1;i>0;i--)
fi[i]=(B[i]-A[i][i+1]*fi[i+1])/A[i][i];
printf("\n solution matrix \n");
for(i=0;i<=Nu+1;i++)
printf("%f\n",fi[i]); //(Will also display boundary nodes values)
getch();
return 0;
}
getch();
return 0;}

Potrebbero piacerti anche