ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/branches/mmeineke/commandLine/madProps.c
Revision: 516
Committed: Mon May 5 21:29:11 2003 UTC (21 years, 2 months ago) by mmeineke
Content type: text/plain
File size: 9679 byte(s)
Log Message:
a generic template for parseing command line options.

File Contents

# User Rev Content
1 mmeineke 516 #include <stdio.h>
2     #include <stdlib.h>
3     #include <string.h>
4     #include <math.h>
5    
6    
7     #include "madProps.h"
8     #include "frameCount.h"
9    
10     char *program_name; /*the name of the program */
11    
12     void usage(void);
13    
14     int main(argc, argv)
15     int argc;
16     char *argv[];
17     {
18    
19    
20     struct xyz_frame *dumpArray;
21     int nFrames; // the nu8mvber of frames
22     int lineNum = 0; // keeps track of the line number
23     int n_atoms; // the number of atoms
24     int i,j; // loop counters
25     int isFirst;
26    
27     char read_buffer[2000]; /*the line buffer for reading */
28     char *foo; /*the pointer to the current string token */
29     FILE *in_file; /* the input file */
30     char *in_name = NULL; /*the name of the input file */
31     char *out_prefix; // the output prefix
32     char current_flag; // used in parseing the flags
33    
34    
35    
36     int done = 0; // multipurpose boolean
37     int have_prefix = 0; // prefix boolean
38     int calcRMSD = 0;
39    
40     int calcGofR = 0;
41     char gofR1[30];
42     char gofR2[30];
43    
44     int calcMuCorr = 0;
45     char muCorr[30];
46    
47     int calcCosCorr = 0;
48     char cosCorr1[30];
49     char cosCorr2[30];
50    
51     int startFrame = 0;
52     int haveStartFrame = 0;
53     int endFrame = 0;
54     int haveEndFrame = 0;
55    
56     program_name = argv[0]; /*save the program name in case we need it*/
57    
58     for( i = 1; i < argc; i++){
59    
60     if(argv[i][0] =='-'){
61    
62     // parse the option
63    
64     if(argv[i][1] == '-' ){
65    
66     // parse long word options
67    
68     if( !strcmp( argv[i], "--GofR" ) ){
69     calcGofR = 1;
70     i++;
71     strcpy( gofR1, argv[i] );
72     i++;
73     strcpy( gofR2, argv[i] );
74     }
75    
76     else if( !strcmp( argv[i], "--CosCorr" ) ){
77     calcCosCorr = 1;
78     i++;
79     strcpy( cosCorr1, argv[i] );
80     i++;
81     strcpy( cosCorr2, argv[i] );
82     }
83    
84     else if( !strcmp( argv[i], "--MuCorr") ){
85     calcMuCorr = 1;
86     i++;
87     strcpy( muCorr, argv[i] );
88     }
89    
90     else if( !strcmp( argv[i], "--startFrame" ) ){
91     haveStartFrame = 1;
92     i++;
93     startFrame = atoi(argv[i]);
94     startFrame--;
95     }
96    
97     else if( !strcmp( argv[i], "--endFrame" ) ){
98     haveEndFrame = 1;
99     i++;
100     endFrame = atoi(argv[i]);
101     }
102    
103     else{
104     fprintf( stderr,
105     "Invalid option \"%s\"\n", argv[i] );
106     usage();
107     }
108     }
109    
110     else{
111    
112     // parse single character options
113    
114     done =0;
115     j = 1;
116     current_flag = argv[i][j];
117     while( (current_flag != '\0') && (!done) ){
118    
119     switch(current_flag){
120    
121     case 'o':
122     // -o <prefix> => the output prefix.
123    
124     i++;
125     out_prefix = argv[i];
126     have_prefix = 1;
127     done = 1;
128     break;
129    
130     case 'h':
131     // -h => give the usage
132    
133     usage();
134     break;
135    
136     case 'r':
137     // calculates the rmsd
138    
139     calcRMSD = 1;
140     break;
141    
142     case 'g':
143     // calculate all to all g(r)
144    
145     calcGofR = 1;
146     strcpy( gofR1, "all" );
147     strcpy( gofR2, "all" );
148     break;
149    
150     default:
151    
152     (void)fprintf(stderr, "Bad option \"-%c\"\n", current_flag);
153     usage();
154     }
155     j++;
156     current_flag = argv[i][j];
157     }
158     }
159     }
160    
161     else{
162    
163     if( in_name != NULL ){
164     fprintf( stderr,
165     "Error at \"%s\", program does not currently support\n"
166     "more than one input file.\n"
167     "\n",
168     argv[i]);
169     usage();
170     }
171    
172     in_name = argv[i];
173     }
174     }
175    
176     if(in_name == NULL){
177     usage();
178     }
179    
180     if( !have_prefix ) out_prefix = in_name;
181    
182     printf( "Counting number of frames..." );
183     fflush( stdout );
184    
185     nFrames = frameCount( in_name );
186     if( !haveEndFrame ) endFrame = nFrames;
187    
188     printf( "done.\n"
189     "nframes = %d\n"
190     "\n",
191     nFrames );
192     fflush( stdout );
193    
194     in_file = fopen(in_name, "r");
195     if(in_file == NULL){
196     printf("Cannot open file: %s\n", in_name);
197     exit(8);
198     }
199    
200     // create and initialize the array of frames
201    
202     dumpArray = (struct xyz_frame*)calloc( nFrames,
203     sizeof( struct xyz_frame ) );
204     for( i=0; i<nFrames; i++ ){
205     dumpArray[i].nAtoms = 0;
206     dumpArray[i].time = 0.0;
207     dumpArray[i].boxX = 0.0;
208     dumpArray[i].boxY = 0.0;
209     dumpArray[i].boxZ = 0.0;
210     dumpArray[i].r = NULL;
211     dumpArray[i].v = NULL;
212     dumpArray[i].names = NULL;
213     }
214    
215     // read the frames
216    
217     printf( "Reading the frames into the coordinate arrays..." );
218     fflush( stdout );
219    
220     isFirst = 1;
221     for(j =0; j<nFrames; j++ ){
222    
223     // read the number of atoms
224    
225     fgets(read_buffer, sizeof(read_buffer), in_file);
226     lineNum++;
227    
228     n_atoms = atoi( read_buffer );
229     dumpArray[j].nAtoms = n_atoms;
230    
231     dumpArray[j].r =
232     (struct coords *)calloc(n_atoms, sizeof(struct coords));
233    
234     if( isFirst ) {
235     dumpArray[0].names =
236     (atomID *)calloc( n_atoms, sizeof(atomID) );
237     isFirst = 0;
238     }
239    
240     if( calcMuCorr || calcCosCorr ){
241     dumpArray[j].v =
242     (struct vect *)calloc(n_atoms, sizeof(struct vect));
243     }
244    
245     //read the time and the box sizes
246    
247     fgets(read_buffer, sizeof(read_buffer), in_file);
248     lineNum++;
249    
250     foo = strtok(read_buffer, " ,;\t");
251     if(foo == NULL){
252     printf("error in reading time at line %d\n", lineNum);
253     exit(8);
254     }
255    
256     dumpArray[j].time = atof( foo );
257    
258     foo = strtok(NULL, " ,;\t");
259     if(foo == NULL){
260     printf("error in reading boxX at line %d\n", lineNum);
261     exit(8);
262     }
263    
264     dumpArray[j].boxX = atof( foo );
265    
266     foo = strtok(NULL, " ,;\t");
267     if(foo == NULL){
268     printf("error in reading boxY at line %d\n", lineNum);
269     exit(8);
270     }
271    
272     dumpArray[j].boxY = atof( foo );
273    
274     foo = strtok(NULL, " ,;\t");
275     if(foo == NULL){
276     printf("error in reading boxZ at line %d\n", lineNum);
277     exit(8);
278     }
279    
280     dumpArray[j].boxZ = atof( foo );
281    
282    
283     for( i=0; i < n_atoms; i++){
284    
285     fgets(read_buffer, sizeof(read_buffer), in_file);
286     lineNum++;
287    
288     // get the name and positions
289    
290     foo = strtok(read_buffer, " ,;\t");
291     if(foo == NULL){
292     printf("error in reading atom name at line %d\n", lineNum );
293     exit(8);
294     }
295    
296     strcpy(dumpArray[0].names[i], foo); /*copy the atom name */
297    
298     foo = strtok(NULL, " ,;\t");
299     if(foo == NULL){
300     printf("error in reading position x at line %d\n", lineNum);
301     exit(8);
302     }
303    
304     dumpArray[j].r[i].x = atof( foo );
305    
306     foo = strtok(NULL, " ,;\t");
307     if(foo == NULL){
308     printf("error in reading position y at line %d\n", lineNum);
309     exit(8);
310     }
311    
312     dumpArray[j].r[i].y = atof( foo );
313    
314     foo = strtok(NULL, " ,;\t");
315     if(foo == NULL){
316     printf("error in reading position z at line %d\n", lineNum);
317     exit(8);
318     }
319    
320     dumpArray[j].r[i].z = atof( foo );
321    
322     if( calcCosCorr || calcMuCorr ){
323    
324     foo = strtok(NULL, " ,;\t");
325     if(foo == NULL){
326    
327     dumpArray[j].v[i].x = 0.0;
328     dumpArray[j].v[i].y = 0.0;
329     dumpArray[j].v[i].z = 0.0;
330     }
331     else{
332    
333     dumpArray[j].v[i].x = atof( foo );
334    
335     foo = strtok(NULL, " ,;\t");
336     if(foo == NULL){
337     printf("error in reading vector y at line %d\n", lineNum);
338     exit(8);
339     }
340    
341     dumpArray[j].v[i].y = atof( foo );
342    
343     foo = strtok(NULL, " ,;\t");
344     if(foo == NULL){
345     printf("error in reading vector z at line %d\n", lineNum);
346     exit(8);
347     }
348    
349     dumpArray[j].v[i].z = atof( foo );
350     }
351     }
352    
353     }
354     }
355    
356     (void)fclose(in_file);
357    
358     printf( "done\n"
359     "\n" );
360     fflush( stdout );
361    
362    
363    
364     // do calculations here.
365    
366     if( calcGofR ){
367    
368     fprintf( stdout,
369     "Calculating the g(r) between atoms \"%s\" and \"%s\"...",
370     gofR1, gofR2 );
371     fflush( stdout );
372    
373     // gofr call
374     GofR( out_prefix, gofR1, gofR2, dumpArray, nFrames, startFrame, endFrame );
375    
376     fprintf( stdout,
377     " done.\n"
378     "\n");
379     fflush(stdout);
380     }
381    
382     if( calcRMSD ){
383    
384     fprintf( stdout,
385     "Calculating the RMSD..." );
386     fflush( stdout );
387    
388     // RMSD call
389    
390    
391     fprintf( stdout,
392     " done.\n"
393     "\n");
394     fflush(stdout);
395     }
396    
397     if( calcMuCorr ){
398    
399     fprintf( stdout,
400     "Calculating the mu correlation for \"%s\"...",
401     muCorr);
402     fflush( stdout );
403    
404     // muCorr call
405    
406    
407     fprintf( stdout,
408     " done.\n"
409     "\n");
410     fflush(stdout);
411     }
412    
413     if( calcCosCorr ){
414    
415     fprintf( stdout,
416     "Calculating the cos correlation between \"%s\" and \"%s\"...",
417     cosCorr1, cosCorr2 );
418     fflush( stdout );
419    
420     cosCorr( out_prefix, cosCorr1, cosCorr2, dumpArray, nFrames, startFrame,
421     endFrame );
422    
423     fprintf( stdout,
424     " done.\n"
425     "\n");
426     fflush(stdout);
427     }
428    
429     return 0;
430    
431     }
432    
433    
434     void map( double *x, double *y, double *z,
435     double boxX, double boxY, double boxZ ){
436    
437     *x -= boxX * copysign(1.0,*x) * floor( fabs( *x/boxX ) + 0.5 );
438     *y -= boxY * copysign(1.0,*y) * floor( fabs( *y/boxY ) + 0.5 );
439     *z -= boxZ * copysign(1.0,*z) * floor( fabs( *z/boxZ ) + 0.5 );
440    
441     }
442    
443    
444     /***************************************************************************
445     * prints out the usage for the command line arguments, then exits.
446     ***************************************************************************/
447    
448     void usage(){
449     (void)fprintf(stdout,
450     "The proper usage is: %s [options] <xyz_file>\n"
451     "\n"
452     "Options:\n"
453     "\n"
454     " short:\n"
455     " ------\n"
456     " -h Display this message\n"
457     " -o <prefix> The output prefix\n"
458     " -r Calculate the RMSD\n"
459     " -g Calculate all to all g(r)\n"
460    
461     "\n"
462     " long:\n"
463     " -----\n"
464     " --GofR <atom1> <atom2> Calculates g(r) between atom1 and atom 2\n"
465     " -note: \"all\" will do all atoms\n"
466     " --MuCorr <atom> Calculate mu correlation of atom\n"
467     " --CosCorr <atom1> <atom2> Calculate the cos correlation between atom1 and atom2\n"
468     " --startFrame <frame#> Specifies a frame to start correlating\n"
469     " --endFrame <frame#> Specifies a frame to stop correlating.\n"
470    
471     "\n"
472     "\n",
473     program_name);
474     exit(8);
475     }