0% found this document useful (0 votes)
45 views12 pages

Code and Plots

The document describes a C++ program that solves the Navier-Stokes equations for the lid-driven cavity flow problem using a finite difference approach. The program implements functions for boundary conditions, velocity and pressure calculations using an iterative approach, and outputs the results. Key aspects include: 1) Discretizing the cavity domain with grids, initializing variables, and applying boundary conditions for the lid-driven and inlet/outlet cases. 2) Iteratively solving for the stream function, velocity, vorticity, and pressure fields using explicit finite difference schemes until convergence is reached based on predefined criteria. 3) Outputting the results to files and plotting profiles to analyze the velocity variations and compare the lid-

Uploaded by

Abdul Ahmed
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
0% found this document useful (0 votes)
45 views12 pages

Code and Plots

The document describes a C++ program that solves the Navier-Stokes equations for the lid-driven cavity flow problem using a finite difference approach. The program implements functions for boundary conditions, velocity and pressure calculations using an iterative approach, and outputs the results. Key aspects include: 1) Discretizing the cavity domain with grids, initializing variables, and applying boundary conditions for the lid-driven and inlet/outlet cases. 2) Iteratively solving for the stream function, velocity, vorticity, and pressure fields using explicit finite difference schemes until convergence is reached based on predefined criteria. 3) Outputting the results to files and plotting profiles to analyze the velocity variations and compare the lid-

Uploaded by

Abdul Ahmed
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/ 12

Problem statement

Where,
im=jm=30
j1=20
j2=25
j3=10
j4=15
u0=5 m/s
nu=.0025 m2/s
rho=1000 kg/m3
dt=.001 sec
no. of grids=30
cavity size=30cm x 30cm
convergence criteria=.001
PROGRAM

#include<iostream>
#include<cmath>
#include<fstream>
#include<iomanip>
#include<vector>

using namespace std;

typedef vector <double > vect ;


typedef vector <vect > mat2d ;

//function for convergence

double norm(mat2d& a,mat2d& b,int n)


{
double sum=0;
for(int i=1;i<n;i++)
{
for(int j=1;j<n;j++)
{
if(abs(a[i][j]-b[i][j])>sum)
{
sum=abs(a[i][j]-b[i][j]);
}
}
}
return sum;
}

//function for print

void printmat(mat2d& a,int m,int n)


{
for (int i = 0; i<m ;i++)
{
for (int j = 0; j<n; j++)
{
cout << setw(15)<<a[j][i] ;
}
cout << endl;
}
return;
}
//Boundary conditions

void boundry(mat2d& u,mat2d& v,mat2d& phi,mat2d& w,double dx,int n)


{
double u0=5.0,s=.1;
for(int i=0;i<n+1;i++)
{
u[i][n]=5.0;u[i][0]=0.0;
v[i][n]=0;v[i][0]=0;v[0][i]=0;v[n][i]=0;
}
for(int i=1;i<=n;i++)
{
phi[i][n]=s;
}
for(int j=26;j<=n;j++)
{
phi[0][j]=s;
}
for(int j=11;j<=n;j++)
{
phi[n][j]=s;
}
for(int j=1;j<n;j++)
{
u[0][j]=0;u[n][j]=0;
}
for(int i=0;i<n+1;i++)
{
w[0][i]=2.0*(phi[0][i]-phi[1][i])/(dx*dx); //left Boundry condition
w[i][0]=2.0*(phi[i][0]-phi[i][1])/(dx*dx); //bottom boundry condition
w[n][i]=2.0*(phi[n][i]-phi[n-1][i])/(dx*dx);//Right boundry condition
w[i][n]=(2.0*(phi[i][n]-phi[i][n-1])/(dx*dx))-(2.0*u0/dx);//Top boundry condition
}
w[0][0]=w[n][0]=0.0;
w[n][n]=w[n-1][n];w[0][n]=w[1][n];
//at inlet side
for(int j=20;j<=25;j++)
{
v[0][j]=0;
phi[0][j]=(4*phi[1][j]-phi[2][j])/3;
}
for(int j=20;j<=25;j++)
{
u[0][j]=(phi[0][j+1]-phi[0][j-1])/(2*dx);
}
for(int j=20;j<=25;j++)
{
w[0][j]=2*(phi[0][j]-phi[1][j])/(dx*dx)-(u[0][j+1]-u[0][j-1])/(2*dx);
}
//at outlet side
for(int j=5;j<=10;j++)
{
v[n][j]=0;
phi[n][j]=(4*phi[n-1][j]-phi[n-2][j])/3;
}
for(int j=5;j<=10;j++)
{
u[n][j]=(phi[n][j+1]-phi[n][j-1])/(2*dx);
}
for(int j=5;j<=10;j++)
{
w[n][j]=2*(phi[n][j]-phi[n][j])/(dx*dx)-(u[n][j+1]-u[n][j-1])/(2*dx);
}
return ;
}

// main function
int main()
{
int n,k;
double dx,c1,c2,c3,c4,mu,nor,nor1,dt,rho;
cout<<"Grid:\n";
cin>>n;
mat2d phi(n+1,vect(n+1)),u(n+1,vect(n+1)),v(n+1,vect(n+1)),w(n+1,vect(n+1)),
w1(n+1,vect(n+1)),phi1(n+1,vect(n+1)),p(n+1,vect(n+1)),p1(n+1,vect(n+1));
// considering cavity of 30*30 cm^2, & other properties of fluid (here water)
// kinematic viscosity = 0.0025 Pa-sec & density = 1000 kg/m3
dx=0.3/(n);dt=0.001;
mu=0.0025;rho=1000;

//initialization of variable
for(int i=0;i<n+1;i++)
{
for(int j=0;j<+1;j++)
{
w[i][j]=phi[i][j]=v[i][j]=u[i][j]=0;
}
}
boundary(u,v,phi,w,dx,n);
phi1=phi;
w1=w;
do
{
//Stream function calculations
// convegence criteria 1e-3.
do
{
for(int i=1;i<n;i++)
{
for(int j=1;j<n;j++)
{
phi[i][j]=(phi1[i+1][j]+phi[i-1][j]+phi1[i][j+1]+phi[i][j-1]+w[i][j]*dx*dx)/4.0;
}
}
nor1=norm(phi,phi1,n);
phi1=phi;
}
while(nor1>1e-3);

//velocity calculation

boundry(u,v,phi,w,dx,n);
for(int i=1;i<n;i++)
{
for(int j=1;j<n;j++)
{
u[i][j]=(phi[i][j+1]-phi[i][j-1])/(2.0*dx);
v[i][j]=(phi[i-1][j]-phi[i+1][j])/(2.0*dx);
}
}
//vorticity calculation
// convergence citeria 1e-3.
for(int i=1;i<n;i++)
{
for(int j=1;j<n;j++)
{
c1=u[i][j]*(w[i+1][j]-w[i-1][j])/(2.0*dx);
c2=v[i][j]*(w[i][j+1]-w[i][j-1])/(2.0*dx);
c3=mu*(w[i+1][j]+w[i-1][j]-2*w[i][j])/(dx*dx);
c4=mu*(w[i][j+1]+w[i][j-1]-2*w[i][j])/(dx*dx);
w[i][j]=w[i][j]+dt*(c3+c4-c1-c2);
}
}

nor=norm(w,w1,n+1);
w1=w;
cout<<"nor"<<nor<<"\n nor1 "<<nor1<<endl;
}
while(nor>1e-3) ;

//Pressure calculation
do
{
for(int i=1;i<n;i++)
{
for(int j=1;j<n;j++)
{
c1=(u[i+1][j]-u[i-1][j])/(2.0);
c1=pow(c1,2);
c2=(v[i][j+1]-v[i][j-1])/(2.0);
c2=pow(c2,2);
c3=2.0*(u[i][j+1]-u[i][j-1]+v[i+1][j]-v[i-1][j])/4.0;
c4=(p1[i+1][j]+p[i-1][j]+p1[i][j+1]+p[i][j-1]);
p[i][j]=(rho*(c1+c2+c3)+c4)/4.0;
}
}
nor=norm(p,p1,n+1);
cout<<"pressure : "<<nor<<endl;
p1=p;
}
while(nor>1e-3) ;
ofstream ab,vel;
ab.open("cavity.dat",ios::trunc|ios::binary|ios::out);
vel.open("velocity.dat",ios::trunc|ios::binary|ios::out);
for(int i=0;i<n+1;i++)
{
for(int j=0;j<n+1;j++)
{
ab<<setw(5)<<i<<setw(5)<<j<<setw(15)<<w[i][j]<<setw(15)<<phi[i][j]<<setw(15
)<<p[i][j]<<endl;
vel<<setw(5)<<i<<setw(5)<<j<<setw(15)<<u[i][j]<<setw(15)<<v[i][j]<<endl;
}
ab<<endl;vel<<endl;
}
cout<<"\n\n\n";

// velocity at y=15
for(int i=0;i<n+1;i++)
{
cout<<i<<"\t\t"<<v[i][14]<<endl;
}
cout<<endl;
// velocity at y=15
for(int j=0;i<n+1;j++)
{
cout<<j<<"\t\t"<<u[14][j]<<endl;
}
return 0;
}
result

velocity profile at x=14


35

30

25
distance from bottom

20

15

10

0
-1 0 1 2 3 4 5 6 7
velocity

velocity profile at y=15


0.5

0
0 5 10 15 20 25 30 35
-0.5
velocity

-1

-1.5

-2

-2.5
distance
velocity profile lid x=15
35

30

25

20
distance

15

10

0
-2 -1 0 1 2 3 4 5 6 7
velocity

velocity y=15 lid


1.5

0.5

0
velocity

0 5 10 15 20 25 30 35
-0.5

-1

-1.5

-2
distance
Pressure variation with inlet and outlet

Pressure variation in lid driven


Stream function inlet and outlet case

Stream function lid driven


velocity vector inlet and outlet case

velocity vector lid driven case


Reference:
Computational fluid dynamics vol 1 by Hoffmann

You might also like