the code can not work continuously

run the code with codeblocks there comes the "nan"
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#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 int wr=40;
const int wu=20;
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],Fc[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++)
	{
		evolution();
		if(n>0)
		{
			if(n%100==0)  cout<<"n="<<n<<endl<<"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%1000==0)  output(n);
			//if(n==20000) output(n);
		}
	}
	system("pause");
	return 0;
}

void init()
{
	std::cout<<"tau="<<tau<<endl;

	for(i=0;i<=NX;i++)
		for(j=0;j<=NY;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=wu+1;j<=NY-1;j++)
	{
		ux[0][j]=U0;
	}
}

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=1;i<NX;i++)
		for(j=1;j<NY;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];
				Fc[i][j][k]=F[i][j][k]+(feq(i,j,k)-f[i][j][k])/tau;
				//F[i][j][k]=f[ip][jp][k]+(feq(ip,jp,k)-f[ip][jp][k])/tau;
			}
	for(i=1;i<=wr+1;i++)
		for(j=wu+1;j<=NY-1;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]=Fc[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=wr+1;i<NX;i++)
		for(j=1;j<NY;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]=Fc[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<NX;i++)
		//for(k=0;k<Q;k++)
		{
			/*f[i][NY][2]=0;
			f[i][NY][5]=0;
			f[i][NY][6]=0;
			f[i][NY][0]=f[i][NY][1]=f[i][NY][3]=0;

			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];*/
			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=wr+1;i<NX;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<=wr;i++)
		//for(k=0;k<Q;k++)
		{
			f[i][wu][2]=F[i][wu+1][4];
			f[i][wu][6]=F[ip][wu+1][8];
			f[i][wu][5]=F[inext][wu+1][7];
		}
	for(j=1;j<=wu;j++)
		//for(k=0;k<Q;k++)
		{
			f[wr][j][1]=F[wr+1][j][3];
			f[wr][j][5]=F[wr+1][jnext][7];
			f[wr][j][8]=F[wr+1][jp][6];
		}
	for(j=wu+1;j<NY;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=1;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_bounce2_"<<NX<<"+"<<NY<<"-"<<U0<<"-"<<Re<<"-"<<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;
		}

}
I can't follow that code, but perhaps it's a 'division by zero' error somewhere?

https://en.wikipedia.org/wiki/NaN
Not a freaking comment, all your variables are global with names of no more than 3 letters and that correspond to a domain that you tell nothing off, inconsistent (and plain wrong) indentation

The values in the `rho' matrix grow too much.
Last edited on
Topic archived. No new replies allowed.