1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
|
#include<iostream>
#include<fstream>
#include<sstream>
#include<string>
#include<iomanip>
#include<cstdlib>
#include<cmath>
using namespace std;
const int Q=9;
const int NX=400;
const int NY=40;
const double U0=0.1;
int i,j,ip,jp,inext,jnext,k,n;
int Re=200;
double niu=U0*NY/Re,tau=3.0*niu+0.5;
int e[Q][2]={{0,0},{1,0},{0,1},{-1,0},{0,-1},{1,1},{-1,1},{-1,-1},{1,-1}};
double w[Q]={4.0/9,1.0/9,1.0/9,1.0/9,1.0/9,1.0/36,1.0/36,1.0/36,1.0/36};
double rho0=1.0,rho[NX+1][NY+1];
double ux0[NX+1][NY+1],uy0[NX+1][NY+1],ux[NX+1][NY+1],uy[NX+1][NY+1];
double f[NX+1][NY+1][Q],F[NX+1][NY+1][Q];
void init();
void evolution();
void output(int);
double feq(int i,int j,int k);
int main()
{
using namespace std;
init();
for(n=0;n<=20000;n++)
{
evolution();
if(n>0)
{
if(n%100==0) cout<<"The ux and uy of [NX/2][NY/2] is:"<<setiosflags(ios::scientific)<<ux[NX/2][NY/2]<<" , "<<uy[NX/2][NY/2]<<endl;
if(n%100==0) output(n);
}
}
system("pause");
return 0;
}
void init()
{
std::cout<<"tau="<<tau<<endl;
for(i=0;i<=400;i++)
for(j=0;j<=40;j++)
{
rho[i][j]=rho0;
ux[i][j]=0;
uy[i][j]=0;
for(k=0;k<Q;k++)
{
f[i][j][k]=feq(i,j,k);
}
}
for(j=20;j<=40;j++)
{
ux[0][j]=U0;
}
if(i=40 && j<=20)
{
ux[i][j]=0;
uy[i][j]=0;
}
if(j=20 && i<=40)
{
ux[i][j]=0;
uy[i][j]=0;
}
}
double feq(int i,int j, int k)
{
double eu,uu,feq;
eu=e[k][0]*ux[i][j]+e[k][1]*uy[i][j];
uu=ux[i][j]*ux[i][j]+uy[i][j]*uy[i][j];
feq=w[k]*rho[i][j]*(1+3.0*eu+4.5*eu*eu-1.5*uu);
return feq;
}
void evolution()
{
for(i=0;i<=400;i++)
for(j=0;j<=40;j++)
for(k=0;k<Q;k++)
{
ip=i-e[k][0];
jp=j-e[k][1];
inext=i+e[k][0];
jnext=j+e[k][1];
F[i][j][k]=f[ip][jp][k]+(feq(ip,jp,k)-f[ip][jp][k])/tau;
}
for(i=0;i<=400;i++)
for(j=0;j<=40;j++)
{
ux0[i][j]=ux[i][j];
uy0[i][j]=uy[i][j];
rho[i][j]=0;
ux[i][j]=0;
uy[i][j]=0;
for(k=0;k<Q;k++)
{
f[i][j][k]=F[i][j][k];
rho[i][j]+=f[i][j][k];
ux[i][j]+=e[k][0]*f[i][j][k];
uy[i][j]+=e[k][1]*f[i][j][k];
}
ux[i][j]/=rho[i][j];
uy[i][j]/=rho[i][j];
}
for(i=1;i<=400;i++)
for(k=0;k<Q;k++)
{
f[i][NY][4]=f[i][NY-1][2];
f[i][NY][7]=f[ip][NY-1][5];
f[i][NY][8]=f[inext][NY-1][6];
}
for(i=41;i<=400;i++)
for(k=0;k<Q;k++)
{
f[i][0][2]=f[i][1][4];
f[i][0][6]=f[ip][1][8];
f[i][0][5]=f[inext][1][7];
}
for(i=1;i<=40;i++)
for(k=0;k<Q;k++)
{
f[i][20][2]=f[i][21][4];
f[i][20][6]=f[ip][21][8];
f[i][20][5]=f[inext][21][7];
}
for(j=1;j<=20;j++)
for(k=0;k<Q;k++)
{
f[40][j][1]=f[41][j][3];
f[40][j][5]=f[41][jnext][7];
f[40][j][8]=f[41][jnext][6];
}
for(j=21;j<40;j++)
for(k=0;k<Q;k++)
{
rho[0][j]=rho[1][j];
f[0][j][k]=feq(0,j,k)+f[1][j][k]-feq(1,j,k);
ux[0][j]=U0;
}
for(j=0;j<=NY;j++)
for(k=0;k<Q;k++)
{
rho[NX][j]=rho[NX-1][j];
f[NX][j][k]=feq(NX,j,k)+f[NX-1][j][k]-feq(NX-1,j,k);
ux[NX][j]=ux[NX-1][j];
}
}
void output(int)
{
ostringstream name;
name<<"cavity_"<<n<<".dat";
ofstream out(name.str().c_str());
out<<"Title=\"LBM 2D cavity Flow\"\n"<<
"VARIABLES= \"X\",\"Y\",\"U\",\"V\"\n"<<"ZONE T=\"BOX\",I="
<<NX+1<<",J="<<NY+1<<",F=POINT"<<endl;
for(j = 0;j <= NY;j++)
for(i = 0;i <=NX;i++)
{
out<<double(i)<<" "<<double(j)<<" "<<
ux[i][j]<<" "<<uy[i][j]<<endl;
}
}
|