1 |
#include <stdio.h> |
2 |
#include <stdlib.h> |
3 |
#include <string.h> |
4 |
#include <math.h> |
5 |
|
6 |
#include "frameCount.h" |
7 |
|
8 |
struct coords{ |
9 |
double x; |
10 |
double y; |
11 |
double z; |
12 |
char name[30]; |
13 |
}; |
14 |
|
15 |
|
16 |
struct xyz_frame{ |
17 |
int nAtoms; |
18 |
double time; |
19 |
double boxX, boxY, boxZ; |
20 |
struct coords *r; |
21 |
}; |
22 |
|
23 |
char *program_name; /*the name of the program */ |
24 |
|
25 |
void usage(void); |
26 |
|
27 |
int main(argc, argv) |
28 |
int argc; |
29 |
char *argv[]; |
30 |
{ |
31 |
|
32 |
|
33 |
struct xyz_frame *dumpArray; |
34 |
int nFrames; // the nu8mvber of frames |
35 |
int lineNum = 0; // keeps track of the line number |
36 |
int n_atoms; // the number of atoms |
37 |
int i,j; // loop counters |
38 |
|
39 |
char read_buffer[2000]; /*the line buffer for reading */ |
40 |
char *foo; /*the pointer to the current string token */ |
41 |
FILE *in_file; /* the input file */ |
42 |
char *in_name = NULL; /*the name of the input file */ |
43 |
|
44 |
|
45 |
program_name = argv[0]; /*save the program name in case we need it*/ |
46 |
|
47 |
for( i = 0; i < argc; i++){ |
48 |
|
49 |
if(argv[i][0] =='-'){ |
50 |
|
51 |
/* argv[i][1] is the actual option character */ |
52 |
|
53 |
switch(argv[i][1]){ |
54 |
|
55 |
/* -f <name> => the xyz input file |
56 |
* [i+1] actually starts the name |
57 |
*/ |
58 |
|
59 |
case 'f': |
60 |
in_name = argv[i+1]; |
61 |
break; |
62 |
|
63 |
default: |
64 |
(void)fprintf(stderr, "Bad option %s\n", argv[i]); |
65 |
usage(); |
66 |
} |
67 |
} |
68 |
} |
69 |
|
70 |
if(in_name == NULL){ |
71 |
usage(); |
72 |
} |
73 |
|
74 |
printf( "Counting number of frames..." ); |
75 |
fflush( stdout ); |
76 |
|
77 |
nFrames = frameCount( in_name ); |
78 |
|
79 |
printf( "done.\n" |
80 |
"nframes = %d\n" |
81 |
"\n", |
82 |
nFrames ); |
83 |
fflush( stdout ); |
84 |
|
85 |
in_file = fopen(in_name, "r"); |
86 |
if(in_file == NULL){ |
87 |
printf("Cannot open file: %s\n", in_name); |
88 |
exit(8); |
89 |
} |
90 |
|
91 |
// create the array of frames |
92 |
|
93 |
dumpArray = (struct xyz_frame*)calloc( nFrames, |
94 |
sizeof( struct xyz_frame ) ); |
95 |
|
96 |
// read the frames |
97 |
|
98 |
printf( "Reading the frames into the coordinate arrays..." ); |
99 |
fflush( stdout ); |
100 |
|
101 |
for(j =0; j<nFrames; j++ ){ |
102 |
|
103 |
// read the number of atoms |
104 |
|
105 |
fgets(read_buffer, sizeof(read_buffer), in_file); |
106 |
lineNum++; |
107 |
|
108 |
n_atoms = atoi( read_buffer ); |
109 |
dumpArray[j].nAtoms = n_atoms; |
110 |
|
111 |
dumpArray[j].r = |
112 |
(struct coords *)calloc(n_atoms, sizeof(struct coords)); |
113 |
|
114 |
//read the time and the box sizes |
115 |
|
116 |
fgets(read_buffer, sizeof(read_buffer), in_file); |
117 |
lineNum++; |
118 |
|
119 |
foo = strtok(read_buffer, " ,;\t"); |
120 |
if(foo == NULL){ |
121 |
printf("error in reading time at line %d\n", lineNum); |
122 |
exit(8); |
123 |
} |
124 |
|
125 |
dumpArray[j].time = atof( foo ); |
126 |
|
127 |
foo = strtok(NULL, " ,;\t"); |
128 |
if(foo == NULL){ |
129 |
printf("error in reading boxX at line %d\n", lineNum); |
130 |
exit(8); |
131 |
} |
132 |
|
133 |
dumpArray[j].boxX = atof( foo ); |
134 |
|
135 |
foo = strtok(NULL, " ,;\t"); |
136 |
if(foo == NULL){ |
137 |
printf("error in reading boxY at line %d\n", lineNum); |
138 |
exit(8); |
139 |
} |
140 |
|
141 |
dumpArray[j].boxY = atof( foo ); |
142 |
|
143 |
foo = strtok(NULL, " ,;\t"); |
144 |
if(foo == NULL){ |
145 |
printf("error in reading boxZ at line %d\n", lineNum); |
146 |
exit(8); |
147 |
} |
148 |
|
149 |
dumpArray[j].boxZ = atof( foo ); |
150 |
|
151 |
|
152 |
for( i=0; i < n_atoms; i++){ |
153 |
|
154 |
fgets(read_buffer, sizeof(read_buffer), in_file); |
155 |
lineNum++; |
156 |
|
157 |
// get the name and positions |
158 |
|
159 |
foo = strtok(read_buffer, " ,;\t"); |
160 |
if(foo == NULL){ |
161 |
printf("error in reading atom name at line %d\n", lineNum ); |
162 |
exit(8); |
163 |
} |
164 |
|
165 |
strcpy(dumpArray[j].r[i].name, foo); /*copy the atom name */ |
166 |
|
167 |
foo = strtok(NULL, " ,;\t"); |
168 |
if(foo == NULL){ |
169 |
printf("error in reading position x at line %d\n", lineNum); |
170 |
exit(8); |
171 |
} |
172 |
|
173 |
dumpArray[j].r[i].x = atof( foo ); |
174 |
|
175 |
foo = strtok(NULL, " ,;\t"); |
176 |
if(foo == NULL){ |
177 |
printf("error in reading position y at line %d\n", lineNum); |
178 |
exit(8); |
179 |
} |
180 |
|
181 |
dumpArray[j].r[i].y = atof( foo ); |
182 |
|
183 |
foo = strtok(NULL, " ,;\t"); |
184 |
if(foo == NULL){ |
185 |
printf("error in reading position z at line %d\n", lineNum); |
186 |
exit(8); |
187 |
} |
188 |
|
189 |
dumpArray[j].r[i].z = atof( foo ); |
190 |
|
191 |
} |
192 |
} |
193 |
|
194 |
(void)fclose(in_file); |
195 |
|
196 |
printf( "done\n" |
197 |
"\n" ); |
198 |
fflush( stdout ); |
199 |
|
200 |
|
201 |
|
202 |
// do calculations here. |
203 |
|
204 |
|
205 |
|
206 |
|
207 |
|
208 |
|
209 |
|
210 |
return 0; |
211 |
|
212 |
} |
213 |
|
214 |
|
215 |
|
216 |
/*************************************************************************** |
217 |
* prints out the usage for the command line arguments, then exits. |
218 |
***************************************************************************/ |
219 |
|
220 |
void usage(){ |
221 |
(void)fprintf(stderr, |
222 |
"The proper usage is: %s [options] -f <xyz_file>\n\n" |
223 |
"Options:\n", |
224 |
program_name); |
225 |
exit(8); |
226 |
} |