# Thread: Printing the Output for Lennard Jones Potential Energy

1. ## Printing the Output for Lennard Jones Potential Energy

I am trying to make a program for Metropolis Monte Carlo Simulation. I am still on the first two steps of the program. My problem is on how to output the the number of atoms, distance between two atoms, the Lennard-Jones Energy and total energy correctly in a matrix form. I think this is the part that I am having a problem.
Code:
```float calculate_energy()	// calculates the energies of the atoms
{
// Loop over all pairs of atoms and calculate
// the LJ energy
float Etotal=0.00;
float r2;
float e_lj;
sigma = 0.0;
epsilon = 1.23;
for ( i = 0; i < n_atoms-1; i++)
{
for (j = i+1; j < n_atoms; j++)
{
r2 = sqrt(pow(coords[j][0] - coords[i][0],2)+ (coords[j][1] - coords[i][1],2)+ (coords[j][2] - coords[i][2],2));

//calculate the Lennard-Jones energy
e_lj=4.0*epsilon*(pow(sigma,12)/pow(r2,12)-(pow(sigma,6)/pow(r2,12)));
//calculate the total energy
Etotal = Etotal+ e_lj;
}
}
printf("Output number of atom, distance of the atom and LJE \n");
printf("%d\t %d\t %f\t %f\t \n",i,j,r2,e_lj);
printf("Output the total energy: \t");
printf("%f \n",Etotal);
return 0;
}

int main(){
printf("Output the calculated total energy \n");
calculate_energy();
fclose(fp);
free(coords);
return 0;
}```
Thank you.

2. What exactly is the problem you are having?

Quzah.

3. Thank you for the reply.

I am using Ubuntu 11.10. It compiles correctly but when I do the ./a.out I get segmentation fault. I think it is on the function call made in int main.

4. Then compile in debug mode:

Code:
`gcc -ggdb3 -Wall -pedantic -o myprog myprog.c`
If it compiles with warnings, fix all of the them.

Start the gdb debugger:

Code:
```gdb ./myprog
...
(gdb)```
Run your program in the debugger:

Code:
`(gdb) run`
When the seg fault happens, you will know exactly where it's occurring, and will be able to evaluate the variables at the time of the crash, allowing you to fix the problem.

5. Thank you for the reply . I followed your instructions and I get this:
Code:
` Starting program: /home/lenovo/Desktop/m  Output the calculated total energy   Program received signal SIGSEGV, Segmentation fault. 0x00007ffff7813c9a in __isoc99_fscanf () from /lib/x86_64-linux-gnu/libc.so.6 (gdb)`
I don't know what is the problem. This is my first time to debug code using gdb.

6. You've probably wandered out of bounds of one of your arrays. You don't actually show us any of the arrays (they appear to be global) in the code you provided. But that's most likely what you've done.

Quzah.

7. Here is the first part of the program:
Code:
```#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int n_atoms;    //number of atoms in the file
char space[3];    //for holding blank lines or ignorable entries
float** coords;    //2-d array where coordinates are stored
float sigma;
float epsilon;
FILE*fp;
int i,j;

//open the source coordinates file

fp = fopen("intconfig1.xyz","r");

fscanf(fp,"%i",&n_atoms);

//allocate array
int i;
coords= (float**)malloc(sizeof(float*)*n_atoms);
for(i=0; i<n_atoms; i++)
coords[i] = (float*)malloc(sizeof(float)*3);

for(i=0; i<n_atoms; i++){
fscanf(fp,"%s%f%f%f",space,&coords[i][0],&coords[i][1],&coords[i][2]);
}
//print out coordinates
for(i=0; i<n_atoms; i++){
printf("%f\t%f\t%f\n",coords[i][0],coords[i][1],coords[i][2]);
}

return 0;
}```
where it tries to access this file:
Code:
```21
Disorder Model
Ar            0.303895     4.327322     8.279128
Ar            9.145204     2.342695     4.070601
Ar            8.182916     0.717535     1.654591
Ar            7.572462     6.656014     5.097832
Ar            5.738435     0.753646     9.701556
Ar            0.544729     8.344379     0.667417
Ar            4.999589     0.738486     6.580141
Ar            4.890845     3.251438     7.711195
Ar            1.967732     1.611031     4.274032
Ar            9.068198     7.436556     7.508925
Ar            5.754537     6.052998     1.575422
Ar            2.424211     6.041687     9.465409
Ar            7.808051     4.058478     6.402670
Ar            1.617716     4.503045     4.084561
Ar            9.114211     0.470067     8.364241
Ar            1.318087     1.643199     1.284136
Ar            3.026772     8.354662     5.635452
Ar            4.534749     1.072564     2.679395
Ar            7.152873     9.594938     4.546791
Ar            4.228917     5.481427     5.809191
Ar            7.462603     3.352339     9.202824```

8. It looks like you forgot to read the second line, the one that contains "Disorder Model", as a separate case.

A few other things:
• Avoid global variables.
• If each "Ar x y z" line has a name to it, create a struct with this name and deal with an array of these struct objects instead.
• Just in case, check that the file has been opened, then close the file when done.

9. Originally Posted by benner
Thank you for the reply . I followed your instructions and I get this:
Code:
` Starting program: /home/lenovo/Desktop/m  Output the calculated total energy   Program received signal SIGSEGV, Segmentation fault. 0x00007ffff7813c9a in __isoc99_fscanf () from /lib/x86_64-linux-gnu/libc.so.6 (gdb)`
I don't know what is the problem. This is my first time to debug code using gdb.
Do yourself a *big* favor and get to know GDB. It is your friend.

Debugging with GDB