1 |
mmeineke |
38 |
#include <stdio.h> |
2 |
|
|
#include <stdlib.h> |
3 |
|
|
#include <string.h> |
4 |
|
|
#include <math.h> |
5 |
|
|
|
6 |
mmeineke |
43 |
|
7 |
|
|
#include "madProps.h" |
8 |
mmeineke |
38 |
#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 |
mmeineke |
45 |
int isFirst; |
26 |
mmeineke |
38 |
|
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 |
mmeineke |
43 |
char *out_prefix; // the output prefix |
32 |
|
|
char current_flag; // used in parseing the flags |
33 |
mmeineke |
38 |
|
34 |
|
|
|
35 |
mmeineke |
43 |
|
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 |
mmeineke |
38 |
program_name = argv[0]; /*save the program name in case we need it*/ |
52 |
|
|
|
53 |
mmeineke |
43 |
for( i = 1; i < argc; i++){ |
54 |
mmeineke |
38 |
|
55 |
|
|
if(argv[i][0] =='-'){ |
56 |
mmeineke |
43 |
|
57 |
|
|
// parse the option |
58 |
mmeineke |
38 |
|
59 |
mmeineke |
43 |
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 |
mmeineke |
38 |
|
92 |
mmeineke |
43 |
else{ |
93 |
mmeineke |
38 |
|
94 |
mmeineke |
43 |
// 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 |
mmeineke |
38 |
|
103 |
mmeineke |
43 |
case 'o': |
104 |
|
|
// -o <prefix> => the output prefix. |
105 |
mmeineke |
38 |
|
106 |
mmeineke |
43 |
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 |
mmeineke |
38 |
usage(); |
154 |
|
|
} |
155 |
mmeineke |
43 |
|
156 |
|
|
in_name = argv[i]; |
157 |
mmeineke |
38 |
} |
158 |
|
|
} |
159 |
|
|
|
160 |
|
|
if(in_name == NULL){ |
161 |
|
|
usage(); |
162 |
|
|
} |
163 |
mmeineke |
43 |
|
164 |
|
|
if( !have_prefix ) out_prefix = in_name; |
165 |
mmeineke |
38 |
|
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 |
mmeineke |
45 |
// create and initialize the array of frames |
184 |
mmeineke |
38 |
|
185 |
|
|
dumpArray = (struct xyz_frame*)calloc( nFrames, |
186 |
|
|
sizeof( struct xyz_frame ) ); |
187 |
mmeineke |
45 |
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 |
mmeineke |
38 |
|
198 |
|
|
// read the frames |
199 |
|
|
|
200 |
|
|
printf( "Reading the frames into the coordinate arrays..." ); |
201 |
|
|
fflush( stdout ); |
202 |
|
|
|
203 |
mmeineke |
45 |
isFirst = 1; |
204 |
mmeineke |
38 |
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 |
mmeineke |
45 |
|
217 |
|
|
if( isFirst ) { |
218 |
|
|
dumpArray[0].names = |
219 |
|
|
(atomID *)calloc( n_atoms, sizeof(atomID) ); |
220 |
|
|
isFirst = 0; |
221 |
|
|
} |
222 |
mmeineke |
38 |
|
223 |
mmeineke |
43 |
if( calcMuCorr || calcCosCorr ){ |
224 |
|
|
dumpArray[j].v = |
225 |
|
|
(struct vect *)calloc(n_atoms, sizeof(struct vect)); |
226 |
|
|
} |
227 |
|
|
|
228 |
mmeineke |
38 |
//read the time and the box sizes |
229 |
|
|
|
230 |
|
|
fgets(read_buffer, sizeof(read_buffer), in_file); |
231 |
|
|
lineNum++; |
232 |
|
|
|
233 |
|
|
foo = strtok(read_buffer, " ,;\t"); |
234 |
|
|
if(foo == NULL){ |
235 |
|
|
printf("error in reading time at line %d\n", lineNum); |
236 |
|
|
exit(8); |
237 |
|
|
} |
238 |
|
|
|
239 |
|
|
dumpArray[j].time = atof( foo ); |
240 |
|
|
|
241 |
|
|
foo = strtok(NULL, " ,;\t"); |
242 |
|
|
if(foo == NULL){ |
243 |
|
|
printf("error in reading boxX at line %d\n", lineNum); |
244 |
|
|
exit(8); |
245 |
|
|
} |
246 |
|
|
|
247 |
|
|
dumpArray[j].boxX = atof( foo ); |
248 |
|
|
|
249 |
|
|
foo = strtok(NULL, " ,;\t"); |
250 |
|
|
if(foo == NULL){ |
251 |
|
|
printf("error in reading boxY at line %d\n", lineNum); |
252 |
|
|
exit(8); |
253 |
|
|
} |
254 |
|
|
|
255 |
|
|
dumpArray[j].boxY = atof( foo ); |
256 |
|
|
|
257 |
|
|
foo = strtok(NULL, " ,;\t"); |
258 |
|
|
if(foo == NULL){ |
259 |
|
|
printf("error in reading boxZ at line %d\n", lineNum); |
260 |
|
|
exit(8); |
261 |
|
|
} |
262 |
|
|
|
263 |
|
|
dumpArray[j].boxZ = atof( foo ); |
264 |
|
|
|
265 |
|
|
|
266 |
|
|
for( i=0; i < n_atoms; i++){ |
267 |
|
|
|
268 |
|
|
fgets(read_buffer, sizeof(read_buffer), in_file); |
269 |
|
|
lineNum++; |
270 |
|
|
|
271 |
|
|
// get the name and positions |
272 |
|
|
|
273 |
|
|
foo = strtok(read_buffer, " ,;\t"); |
274 |
|
|
if(foo == NULL){ |
275 |
|
|
printf("error in reading atom name at line %d\n", lineNum ); |
276 |
|
|
exit(8); |
277 |
|
|
} |
278 |
|
|
|
279 |
mmeineke |
45 |
strcpy(dumpArray[0].names[i], foo); /*copy the atom name */ |
280 |
mmeineke |
38 |
|
281 |
|
|
foo = strtok(NULL, " ,;\t"); |
282 |
|
|
if(foo == NULL){ |
283 |
|
|
printf("error in reading position x at line %d\n", lineNum); |
284 |
|
|
exit(8); |
285 |
|
|
} |
286 |
|
|
|
287 |
|
|
dumpArray[j].r[i].x = atof( foo ); |
288 |
|
|
|
289 |
|
|
foo = strtok(NULL, " ,;\t"); |
290 |
|
|
if(foo == NULL){ |
291 |
|
|
printf("error in reading position y at line %d\n", lineNum); |
292 |
|
|
exit(8); |
293 |
|
|
} |
294 |
|
|
|
295 |
|
|
dumpArray[j].r[i].y = atof( foo ); |
296 |
|
|
|
297 |
|
|
foo = strtok(NULL, " ,;\t"); |
298 |
|
|
if(foo == NULL){ |
299 |
|
|
printf("error in reading position z at line %d\n", lineNum); |
300 |
|
|
exit(8); |
301 |
|
|
} |
302 |
|
|
|
303 |
|
|
dumpArray[j].r[i].z = atof( foo ); |
304 |
mmeineke |
43 |
|
305 |
|
|
if( calcCosCorr || calcMuCorr ){ |
306 |
|
|
|
307 |
|
|
foo = strtok(NULL, " ,;\t"); |
308 |
|
|
if(foo == NULL){ |
309 |
|
|
|
310 |
|
|
dumpArray[j].v[i].x = 0.0; |
311 |
|
|
dumpArray[j].v[i].y = 0.0; |
312 |
|
|
dumpArray[j].v[i].z = 0.0; |
313 |
|
|
} |
314 |
|
|
else{ |
315 |
|
|
|
316 |
|
|
dumpArray[j].v[i].x = atof( foo ); |
317 |
|
|
|
318 |
|
|
foo = strtok(NULL, " ,;\t"); |
319 |
|
|
if(foo == NULL){ |
320 |
|
|
printf("error in reading vector y at line %d\n", lineNum); |
321 |
|
|
exit(8); |
322 |
|
|
} |
323 |
|
|
|
324 |
|
|
dumpArray[j].v[i].y = atof( foo ); |
325 |
|
|
|
326 |
|
|
foo = strtok(NULL, " ,;\t"); |
327 |
|
|
if(foo == NULL){ |
328 |
|
|
printf("error in reading vector z at line %d\n", lineNum); |
329 |
|
|
exit(8); |
330 |
|
|
} |
331 |
|
|
|
332 |
|
|
dumpArray[j].v[i].z = atof( foo ); |
333 |
|
|
} |
334 |
|
|
} |
335 |
|
|
|
336 |
mmeineke |
38 |
} |
337 |
|
|
} |
338 |
|
|
|
339 |
|
|
(void)fclose(in_file); |
340 |
|
|
|
341 |
|
|
printf( "done\n" |
342 |
|
|
"\n" ); |
343 |
|
|
fflush( stdout ); |
344 |
|
|
|
345 |
|
|
|
346 |
|
|
|
347 |
|
|
// do calculations here. |
348 |
|
|
|
349 |
mmeineke |
43 |
if( calcGofR ){ |
350 |
|
|
|
351 |
|
|
fprintf( stdout, |
352 |
|
|
"Calculating the g(r) between atoms \"%s\" and \"%s\"...", |
353 |
|
|
gofR1, gofR2 ); |
354 |
|
|
fflush( stdout ); |
355 |
|
|
|
356 |
|
|
// gofr call |
357 |
|
|
GofR( out_prefix, gofR1, gofR2, dumpArray, nFrames ); |
358 |
|
|
|
359 |
|
|
fprintf( stdout, |
360 |
|
|
" done.\n" |
361 |
|
|
"\n"); |
362 |
|
|
fflush(stdout); |
363 |
|
|
} |
364 |
mmeineke |
38 |
|
365 |
mmeineke |
43 |
if( calcRMSD ){ |
366 |
|
|
|
367 |
|
|
fprintf( stdout, |
368 |
|
|
"Calculating the RMSD..." ); |
369 |
|
|
fflush( stdout ); |
370 |
|
|
|
371 |
|
|
// RMSD call |
372 |
mmeineke |
38 |
|
373 |
mmeineke |
43 |
|
374 |
|
|
fprintf( stdout, |
375 |
|
|
" done.\n" |
376 |
|
|
"\n"); |
377 |
|
|
fflush(stdout); |
378 |
|
|
} |
379 |
mmeineke |
38 |
|
380 |
mmeineke |
43 |
if( calcMuCorr ){ |
381 |
|
|
|
382 |
|
|
fprintf( stdout, |
383 |
|
|
"Calculating the mu correlation for \"%s\"...", |
384 |
|
|
muCorr); |
385 |
|
|
fflush( stdout ); |
386 |
|
|
|
387 |
|
|
// muCorr call |
388 |
mmeineke |
38 |
|
389 |
mmeineke |
43 |
|
390 |
|
|
fprintf( stdout, |
391 |
|
|
" done.\n" |
392 |
|
|
"\n"); |
393 |
|
|
fflush(stdout); |
394 |
|
|
} |
395 |
|
|
|
396 |
|
|
if( calcCosCorr ){ |
397 |
|
|
|
398 |
|
|
fprintf( stdout, |
399 |
|
|
"Calculating the cos correlation between \"%s\" and \"%s\"...", |
400 |
|
|
cosCorr1, cosCorr2 ); |
401 |
|
|
fflush( stdout ); |
402 |
|
|
|
403 |
mmeineke |
46 |
cosCorr( out_prefix, cosCorr1, cosCorr2, dumpArray, nFrames ); |
404 |
mmeineke |
43 |
|
405 |
|
|
fprintf( stdout, |
406 |
|
|
" done.\n" |
407 |
|
|
"\n"); |
408 |
|
|
fflush(stdout); |
409 |
|
|
} |
410 |
mmeineke |
38 |
|
411 |
|
|
return 0; |
412 |
|
|
|
413 |
|
|
} |
414 |
|
|
|
415 |
|
|
|
416 |
mmeineke |
46 |
void map( double *x, double *y, double *z, |
417 |
|
|
double boxX, double boxY, double boxZ ){ |
418 |
|
|
|
419 |
|
|
*x -= boxX * copysign(1.0,*x) * floor( fabs( *x/boxX ) + 0.5 ); |
420 |
|
|
*y -= boxY * copysign(1.0,*y) * floor( fabs( *y/boxY ) + 0.5 ); |
421 |
|
|
*z -= boxZ * copysign(1.0,*z) * floor( fabs( *z/boxZ ) + 0.5 ); |
422 |
mmeineke |
38 |
|
423 |
mmeineke |
46 |
} |
424 |
|
|
|
425 |
|
|
|
426 |
mmeineke |
38 |
/*************************************************************************** |
427 |
|
|
* prints out the usage for the command line arguments, then exits. |
428 |
|
|
***************************************************************************/ |
429 |
|
|
|
430 |
|
|
void usage(){ |
431 |
mmeineke |
43 |
(void)fprintf(stdout, |
432 |
|
|
"The proper usage is: %s [options] <xyz_file>\n" |
433 |
|
|
"\n" |
434 |
|
|
"Options:\n" |
435 |
|
|
"\n" |
436 |
|
|
" short:\n" |
437 |
|
|
" ------\n" |
438 |
|
|
" -h Display this message\n" |
439 |
|
|
" -o <prefix> The output prefix\n" |
440 |
|
|
" -r Calculate the RMSD\n" |
441 |
|
|
" -g Calculate all to all g(r)\n" |
442 |
|
|
|
443 |
|
|
"\n" |
444 |
|
|
" long:\n" |
445 |
|
|
" -----\n" |
446 |
|
|
" --GofR <atom1> <atom2> Calculates g(r) between atom1 and atom 2\n" |
447 |
|
|
" -note: \"all\" will do all atoms\n" |
448 |
|
|
" --MuCorr <atom> Calculate mu correlation of atom\n" |
449 |
|
|
" --CosCorr <atom1> <atom2> Calculate the cos correlation between atom1 and atom2\n" |
450 |
|
|
|
451 |
|
|
|
452 |
|
|
"\n" |
453 |
|
|
"\n", |
454 |
mmeineke |
38 |
program_name); |
455 |
|
|
exit(8); |
456 |
|
|
} |