ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/madProps/madProps.c
Revision: 45
Committed: Tue Jul 23 16:08:35 2002 UTC (21 years, 11 months ago) by mmeineke
Content type: text/plain
File size: 8891 byte(s)
Log Message:
got the GofR code working, and slimmed down the memory usage.

File Contents

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