Output help

Hi there, I'm writing a program that should calculate the energy of a system. There are three subroutines written, which one is used depends on a random number that is generated. Basically I want the energy of the system to be updated each time one of these subroutines is carried out. What I'm having trouble with is figuring out a way that the variable "energy_box" is updated and printed on screen. Here is one of these subroutines:

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
void MCmove(const double *box_size, double **coords, int n_atoms, CRandomMersenne* pRanGen)

{
		bool accept = false;
		
		
		double random;
		random = pRanGen->Random();
		int atom = int(random*n_atoms);

        int old_coords[3] =  { coords[atom][0], coords[atom][1],
							   coords[atom][2] };

		double total_energy_move = 0;
		double energy_box = 0;

		for (int i=0; i < n_atoms-1; i++)
		{
			
	    double delta_x = coords[atom][0] - coords[i][0];
            double delta_y = coords[atom][1] - coords[i][1];
            double delta_z = coords[atom][2] - coords[i][2];


            delta_x = make_periodic(delta_x, box_size[0]);
            delta_y = make_periodic(delta_y, box_size[1]);
            delta_z = make_periodic(delta_z, box_size[2]);
			
	    double r = pow((delta_x*delta_x) + (delta_y*delta_y) +
                          (delta_z*delta_z),0.5);

	    double e_lj = ((4.0/pow(r,12.0))-(4.0/pow(r,6.0)));

            total_energy_move = (total_energy_move + e_lj);	
		}

 
		double acceptance_move = exp(-beta*(total_energy_move));
		
		if (random > acceptance_move)
		{
			accept = true;
			naccept = naccept + 1;
			coords[atom][0] = coords[n_atoms-1][0];
			coords[atom][1] = coords[n_atoms-1][1];
			coords[atom][2] = coords[n_atoms-1][2];
			coords[n_atoms-1][0] = coords[n_atoms-1][1] = coords[n_atoms-1][2] = 0;

			energy_box = energy_box - total_energy_move;
		}

		else
		{
			accept = false;
			nreject = nreject + 1;
			coords[atom][0] = old_coords[0];
            coords[atom][1] = old_coords[1];
            coords[atom][2] = old_coords[2];
		}

		return energy_box;

}


So the general idea is that this "energy_box" is updated each time one of these subroutines runs. I have "return energy_box" in each of them. At the moment when I print to screen I simply get 0's. I know the way I'm attempting to do it at the moment is seriously flawed so any suggestions of a better way to do it would be much appreciated.

Cheers
Your function is void, change that to returning whatever type energy box is. A double.

voiddouble MCmove(const double *box_size, double **coords, int n_atoms, CRandomMersenne* pRanGen)

That should have been an error from the compiler - having a return in a void function.
Last edited on
Hi there, yes I noticed the function was void soon after I posted this message. I've changed them to return a double.

After all of these subroutines I mentioned, I now return energy_box. However at the end of the program I have

printf("%d: %f %d %d\n", move, energy_box, naccept, nreject);

Everything here works apart from energy_box (which returns only 0's). I've checked what values of energy_box are being produced in the subroutines and they seem fine...
I've checked what values of energy_box are being produced in the subroutines and they seem fine...


This seem to imply that you work out the value of energy_box in several functions - is it being returned by these functions or otherwise changed via a reference or pointer?

The variable energy_box is local to the function posted above, so it is not the same as energy_box declared elsewhere, because it goes out of scope at the end of the function.
I think the problem is that I want the value of energy_box to be updated each time one of the subroutines runs, but I have only declared it locally in each subroutine.

Would a better way to do it be to declare three separate variables (e.g energy_box1, energy_box2 etc) - so one for each subroutine, return them, and then sum them outside of the functions?

You could declare energy_box in main(), then send it to the functions as a reference. That way when you modify the value of it in the function, you are changing it in main as well, because it is actually the same variable. Technically the variable occupies the same memory address. That is the whole idea of references and pointers. References are safer than pointers, so are preferred.

Read up about it all in the articles & reference section on this page at the top left.
If you have several of these and want to sum them, just return them from the functions as you are now, but sum them using a variable in main.

1
2

SumEnergyBox += MCmove() // I didn't show the args 


You could store the separate energies in a container like vector or whatever one suits best: stack, list. map, set etc. Then you have potential do do other calcs if needed.
Thanks a lot, worked a treat.
Topic archived. No new replies allowed.