Creating output file

Hi there, I'm currently writing a program that calculates the energy of atoms inside a simulation space (I'll spare the other details). Basically I want to create an output file (.txt) that shows the value of 'r' and 'total_energy' for each iteration of the loop. The relevant part of the code 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
28
29
// Subroutine that calculates the energies of the atoms
double calculate_energy(double **coords, const int n_atoms, const double *box_size,const double sigma, const double epsilon)
{
    // Loop over all pairs of atoms and calculate the LJ energy
    double total_energy = 0;

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

            // Apply periodic boundaries
            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]);

			// Calculate the LJ potential

            const double r = (pow(((delta_x*delta_x) + (delta_y*delta_y) +
                              (delta_z*delta_z)),0.5))/sigma;
		
            const double A = 4;

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

            total_energy = total_energy + e_lj;


I tried creating the following:

1
2
ofstream outputFile;
	outputFile.open("LJ_plot.txt");


And then assigning a value to the output file inside the loop, but all I got out was the final values of both 'r' and 'total_energy', not the values after each iteration of the loop. Any suggestions of a better way to do this?

Cheers

For each iteration:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
ofstream outputFile;
outputFile.open("LJ_plot.txt", ios::app); //ios::app opens the file for appending
// data and not truncating or deleting previous data
int entry = 0; //to keep track of # of total iterations
for (int i = 0; i < n_atoms-1; i++)
    {
        entry += 1;
        for (int j = i+1; j < n_atoms; j++)
        {
             entry += 1;
//...your code and then...
             total_energy = total_energy + e_lj;
             outputFile << "This is the " << entry << "th entry that is output to file" << endl;
       }
}
outputFile.close();
Last edited on
Thanks Soranz, works correctly now.

Just to add to this, if I wanted to create two output files at the same time, say instead of "LJ_plot.txt", I had instead "LJ_radius" and "LJ_total_energy", how would I go about doing that?
2 options:

1 - You can open and close file 1 and then open and close file 2 for each iteration. Sound good ? Haha I hope not !

2 - Since each file would have data output at essentially the same time it'll be much better to create 2 ofstream objects dedicated to handling each file separately. Declare the 2nd one with the same format as the first. It'll just be a matter of copy pasting mostly with just a change in name of the object and the file to open.
Topic archived. No new replies allowed.