/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * This program calculates the 1. Center of mass for a given system of particles. All particles need not have the same mass. 2. Distance to each particle from the center of mass. INPUT : Input file must have the name 'center-of-mass-input.xyz' and the data must be in the following format (space or tab separated) First Line : Number of particles, N Next N Lines : x, y, z and M of each particle DESCRIPTION OF VARIABLES : Nbuf First line of the input file, stored as string xbuf x co-ordinate of the particle, stored as string ybuf y co-ordinate of the particle, stored as string zbuf z co-ordinate of the particle, stored as string Mbuf Mass of the particle, stored as string N Nbuf converted to integer, using atoi() Number of particles x[i] xbuf converted to double precision number, using atof() One dimensional array for storing x co-ordinate of N particles y[i] ybuf converted to double precision number, using atof() One dimensional array for storing y co-ordinate of N particles z[i] zbuf converted to double precision number, using atof() One dimensional array for storing z co-ordinate of N particles M[i] Mbuf converted to integer, using atoi() One dimensional array for storing mass of N particles COM_X Sum of all x co-ordinates COM_Y Sum of all y co-ordinates COM_Z Sum of all z co-ordinates M_TOT Total mass DIST Distance to each particle from the center of mass COMPILATION/EXECUTION PROCEDURE : gcc -o center-of-mass.x center-of-mass.c -lm ./center-of-mass.x OUTPUT : If the compilation and execution is successful, the program writes an output file called 'center-of-mass-output.xyz' containing the 1. Center of mass, and 2. Distance to each particle from the center of mass. Writtten by Gowtham, with valuable tips from H. A. Sawant (hasawant@mtu.edu) in using atoi(). Question and/or comments should be sent to mail@sgowtham.net * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ #include #include #include #include main() { /* Defines input and output files */ FILE *input; FILE *output; /* Declaration and initialization of variables */ char Nbuf[100]={'\0'}; char xbuf[100]={'\0'}; char ybuf[100]={'\0'}; char zbuf[100]={'\0'}; char Mbuf[100]={'\0'}; double x[100]={0}; double y[100]={0}; double z[100]={0}; int M[100]={0}; int i, N, M_TOT; double COM_X, COM_Y, COM_Z, DIST; M_TOT=0; COM_X=0.0; COM_Y=0.0; COM_Z=0.0; DIST=0.0; /* Opens the input file for reading */ input = fopen("center-of-mass-input.xyz", "r"); if (input == NULL) { printf("\nInput File empty or corrupted! Exiting...\n"); exit (1); } /* Creates the output file and opens it for writing */ output = fopen("center-of-mass-output.xyz", "w"); /* Reads the first line of the input file and stores it as the number of particles */ fscanf(input, "%s", Nbuf); /* reads one line */ N = atoi(Nbuf); /* Reads and stores the x, y, z and M for each particle, first as a string (buffer) and later stores it as an array element using appropriate conversion mechanism */ for(i=0; i