Global Variables

Hi,

I have a program that has t declared as a global variable with double t; //the time variable . Well, That is initialized to zero by the method, init(); and then is incremented by a variable tau which is also defined globally. So the program chugs along until it gets to the timestep method (included below). It appears that both t0 and t become NaN after the 0 step in the for-loop. I do not understand this part because I am not varying t. By the time that I reach the end of the code where I add tau to t, t is already NaN and so I get a time that is NaN. As you can imagine, for time evolution, this doesn't work too well.

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
void timeStep()
{
	double t0 = t;
	do {
	//	int x = 31;
	//	int y = 61;
		cout << "t = " << t << endl;
		if(debug)
			cout << t0 << endl;
		for(int y = 0; y<sizeY; y++)
		{
			cout << t << endl;
			for(int x = 0; x<sizeX; x++)
			{
				if(debug)
					cout << "c" << endl;
				grid [x][y] = grid[x][y] * pie(x,y); //so P(t2) = P(t1) * somejunk
			}
		}
		t+=tau;
		cout << t << endl;
	} while (abs(c0 * (t - t)) > sizeX / T / framesPerSec); 
    glutPostRedisplay();
    glFlush();
}


Additionally, I am getting some more NaN errors where I wasn't getting them anymore with some of my other variables when they are defined globally.

Is this just an issue with globally defining variables in c++ or possibly something else?

Thanks in advance,

Thomas
Last edited on
I dont understand your problem, but it problably has to do with one of those two things:
- The scope of variables; the variables are bounded to the functions there declared in: global variables (declared outside any function) are accesible from all functions. This means that variable V inside main() is another variable as variable V inside somefunction() and chances on one of them doesnt have any effect on the other. Also, unless you declare a variable as static it's value will be lost when the end of its scope is reached.
- You don't initialize the variables when using them as local variables. This will give problems when using var+=value.
The top of my function is

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
using namespace std;
//using namespace itpp;

//////////////////////////////////VARIABLE DECLARATION////////////////////////////////////////////
double T = 5;
double k = 2; //determines the speed of motion, used in e^ikx
bool debug = false; //True for debugging, used mainly in glut but also gives nice console output every step of the way.
double t; //the time variable
double m = 1; //mass
double c0 = 1; //beginning speed
double xcenter = 32; //x coordinate for the center of the pressure wave
double ycenter = 64; //y coordinate for the center of the pressure wave
double sigmaXY = 5; //determines the width of the pressure wave in the x
double sigma0 = 6; //determines the sigma for the daf
double tau = 0; //time step as determined by the nyquist condition, meant to be less than nyquist
double temp; //Temp double for various calculations, resets as needed
double speed; //velocity profile
double delX = 1; //Grid spacing
double tempP[upperM]; //The values of the array for the Pk 
double bar = 0.1; //This is the difference between xi and xj, yi and yj, etc. Can be tuned for more accuracy.
double ** grid;
double tMax = 10000; //This is tMax, set to something needlessly big so it runs infinitely
double framesPerSec = 60;       // animation rate for screen redraws
bool running; //controls animation
double expDelT;
double * dafX;
double * dafY;


and none of those are within a method. From what I can gather, the scope of the variables is such that they can be used in any method without having to be passed. That's what I wanted to happen. Then, most of those, the ones that aren't already given a value, are initilized in the init() method which is included below.

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
void init()
{	if(debug)
	cout << "a" << endl;
	t = 0; //resets time
	double upperBound = 20; //This is the upper Range for the daf calculations
	tau = (c0*c0)/(k*k*100);
	grid = new double*[sizeX];

	for (int asdf = 0; asdf < sizeX; ++asdf)
		grid[asdf] = new double[sizeY];
	
	//Generates your gaussian
	
	for(int y = 0; y<sizeY; y++)
	{
		for(int x = 0; x<sizeX; x++)
		{
			grid[x][y] = 0;
		}
	}
	
	expDelT = exp(-(tau*tau)); //this is the e^-deltaT^2

	//Generates your DAF Kernel and stores it as an array for the X and an array for the Y
	dafX = new double[sizeX];
	dafY = new double[sizeY];
	
	for(int iterDaf = 0; iterDaf<sizeX; iterDaf++)
	{
		dafX[iterDaf] = deltaX(upperBound, iterDaf);
	}
	
	for(int iterDaf = 0; iterDaf<sizeY; iterDaf++)
	{
		dafX[iterDaf] = deltaY(upperBound, iterDaf);
	}
	
	//Does all the clearing that is needed
	cout << "done finally" << endl;
    glClearColor(1.0, 1.0, 1.0, 0.0);//Define our background color
    gluOrtho2D(0,sizeY,0,sizeX);//(NEW)  Define our viewing area
	glDisable(GL_DEPTH_TEST);
	glEnable(GL_POINT_SMOOTH);
	glPointSize(5);
	//Generates your gaussian
	
	t = 0;
	
	for(int y = 0; y<sizeY; y++)
	{
		for(int x = 0; x<sizeX; x++)
		{
			grid[x][y] = exp(-(((x-xcenter)*(x-xcenter))/(2*sigmaXY*sigmaXY)+((y-ycenter)*(y-ycenter))/(2*sigmaXY*sigmaXY)));
		}
	}
	
}


After this, I believe that the global tau is defined as (c0*c0)/(k*k*100) and the variable t is set to zero. I did this with the intention that when you press enter in the GLUT window, it resets the whole thing.

The problem is, that in the method timestep(); which I included above, time for some reason turns into NaN. I am not sure why.

I hope that helps for clarification.

I also have a question, if I have a variable defined globally, then would I need to declare it in every method that I use it anyways?
I think all those variables should be declared inside a class/struct.

Globals only need to be declared in every file that uses them:
1
2
3
4
//Definition:
T a=0;
//Declaration (will cause errors if in the same file as the definition):
extern T a;
What do you mean by using a class?

All the variables that I have declared are being used in the file they're being used in. Is that a problem?

Sorry for the stupid questions, I'm learning C++ on the fly as I'm taking a computational physics class.
Topic archived. No new replies allowed.