Code and Plots
Code and Plots
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>
// 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
30
25
distance from bottom
20
15
10
0
-1 0 1 2 3 4 5 6 7
velocity
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
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