help!!some probnlem about“unhandled exception”

somebody help me! run the code there comes "unhandled exception in ...:0xC0000005:Access Violation."

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
#include<iostream>
#include<fstream>
#include<sstream>
#include<string>
#include<cmath>
#include<cstdlib>
#include<iomanip>

using namespace std;

const int Q=9;
const int NX=100;
const int NY=50;
const int Re=200;
const double U0=0.1;
const double rho0=1.0;

int k,n,i,j,ip,jp;
double niu=NY*U0/Re;
double 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 rho[NX][NY];
double ux0[NX][NY],uy0[NX][NY],ux[NX][NY],uy[NX][NY];
double f[NX][NY][Q],F[NX][NY][Q];

void init();
void evolution();
void output(int m);
double feq(int k,int i,int j);

int main()
{
	using namespace std;

	init();

	for(n=0;n<=50000;n++)
	{
		evolution();
		if(n%100==0)
			std::cout<<"The ux and uy of [NX/2][NY/2]:"<<setprecision(6)<<ux[NX/2][NY/2]<<" , "<<uy[NX/2][NY/2]<<endl;
		if(n%1000==0)
			output(n);
	}

	system("pause");
	return 0;
}

void init()
{
	for(i=0;i<=NX;i++)
		for(j=0;j<=NY;j++)
		{	
			ux[i][j]=0.0;
			uy[i][j]=0.0;

            rho[i][j]=rho0;
			ux[0][j]=U0;
			
			for(k=0;k<Q;k++)
			{
				f[i][j][k]=feq(k,i,j);
			}
		}
}

double feq(int k,int i,int j)
{
	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.0+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];
				F[i][j][k]=f[ip][jp][k]-(f[ip][jp][k]-feq(k,ip,jp))/tau;
			}
	
	for(i=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.0;
			ux[i][j]=0.0;
			uy[i][j]=0.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]+=f[i][j][k]*e[k][0];
				uy[i][j]+=f[i][j][k]*e[k][1];
			}
			ux[i][j]/=rho[i][j];
			uy[i][j]/=rho[i][j];
		}
	
	for(i=0;i<=NX;i++)
		for(k=0;k<Q;k++)
		{
			rho[i][0]=rho[i][1];
			f[i][0][k]=feq(k,i,0)+f[i][1][k]-feq(k,i,1);

			rho[i][NY]=rho[i][NY-1];
			f[i][NY][k]=feq(k,i,NY)+f[i][NY-1][k]-feq(k,i,NY-1);
		}
	for(j=0;j<=NY;j++)
		for(k=0;k<Q;k++)
		{
			rho[0][j]=rho[1][j];
			ux[0][j]=U0;
			f[0][j][k]=feq(k,0,j)+f[1][j][k]-feq(k,1,j);

			rho[NX][j]=rho[NX-1][j];
			f[NX][j][k]=feq(k,NX,j)+f[NX-1][j][k]-feq(k,NX-1,j);
		}
}

void output(int m)
{
	ostringstream name;
	name<<"2D flow_"<<m<<".dat";
	ofstream out(name.str().c_str());
	
	for(i=0;i<=NX;i++)
		for(j=0;j<=NY;j++)
		{
			out<<double(i)/NX<<" "<<double(j)/NY<<" "<<ux[i][j]<<" "<<uy[i][j]<<endl;
		}
}

Last edited on
The problem is this:
1
2
	for(i=0;i<=NX;i++)
		for(j=0;j<=NY;j++)
Whenever i<=NX or j<=NY appears within the loop your index will be out of bounds at the end.

Write i<NX and j<NY instead. Note: < instead of <=
Thank you very much for coder777! I changed all the wrong you point out ,then the code could run .You are so great ,thank you very much !!
Topic archived. No new replies allowed.