Printing the Output for Lennard Jones Potential Energy

This is a discussion on Printing the Output for Lennard Jones Potential Energy within the C Programming forums, part of the General Programming Boards category; I am trying to make a program for Metropolis Monte Carlo Simulation. I am still on the first two steps ...

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    6

    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");
    	read_intconfig();
    	calculate_energy();
    	fclose(fp);
    	free(coords);
    	return 0;
    }
    Thank you.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    What exactly is the problem you are having?


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jan 2012
    Posts
    6
    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. #4
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,655
    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. #5
    Registered User
    Join Date
    Jan 2012
    Posts
    6
    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. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Jan 2012
    Posts
    6
    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;
    
    
    int read_intconfig(){
        //open the source coordinates file
        
        fp = fopen("intconfig1.xyz","r");
    
    
        //read the first readable 
        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);
    
    
        //read in coordinates
        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. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,272
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,655
    Quote Originally Posted by benner View Post
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Nuclear Energy... pwned?
    By Cheeze-It in forum General Discussions
    Replies: 53
    Last Post: 06-14-2010, 10:55 PM
  2. 1D-oscillationg w. energy-loss
    By joerg in forum C Programming
    Replies: 1
    Last Post: 06-06-2007, 02:38 AM
  3. Endiana Jones and The Last Crash-ade
    By SMurf in forum C Programming
    Replies: 7
    Last Post: 03-09-2005, 05:12 PM
  4. Alternate energy sources
    By Govtcheez in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 02-02-2005, 07:07 PM
  5. energy and life on earth
    By Silvercord in forum A Brief History of Cprogramming.com
    Replies: 40
    Last Post: 01-20-2003, 11:39 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21