4 |
|
#include <string.h> |
5 |
|
|
6 |
|
#include "GofR.h" |
7 |
+ |
|
8 |
+ |
void map( double *x, double *y, double *z, |
9 |
+ |
double boxX, double boxY, double boxZ ); |
10 |
+ |
|
11 |
+ |
// this algoritm assumes constant number of atoms between frames, and |
12 |
+ |
// constant boxSize |
13 |
+ |
|
14 |
+ |
void GofR( char* out_prefix, char* atom1, char* atom2, |
15 |
+ |
struct xyz_frame* frames, int nFrames ){ |
16 |
+ |
|
17 |
+ |
int i,j,k; |
18 |
+ |
|
19 |
+ |
enum g_types { all_all, atom_all, atom_atom }; |
20 |
+ |
enum g_types the_type; |
21 |
+ |
char* allAtom; |
22 |
+ |
|
23 |
+ |
double atom1Dens; |
24 |
+ |
double atom2Dens; |
25 |
+ |
double allDens; |
26 |
+ |
|
27 |
+ |
double atom1Constant; |
28 |
+ |
double atom2Constant; |
29 |
+ |
double allConstant; |
30 |
+ |
|
31 |
+ |
double nAtom1; |
32 |
+ |
double nAtom2; |
33 |
+ |
double nAtoms; |
34 |
+ |
|
35 |
+ |
double delR; |
36 |
+ |
double boxVol; |
37 |
+ |
double shortBox; |
38 |
+ |
|
39 |
+ |
double rxj, ryj, rzj; |
40 |
+ |
double dx, dy, dz; |
41 |
+ |
double distSqr; |
42 |
+ |
double dist; |
43 |
+ |
|
44 |
+ |
double rLower, rUpper; |
45 |
+ |
double nIdeal; |
46 |
+ |
double volSlice; |
47 |
+ |
|
48 |
+ |
int bin; |
49 |
+ |
int histogram[GofRBins]; |
50 |
+ |
double the_GofR[GofRBins]; |
51 |
+ |
double rValue[GofRBins]; |
52 |
+ |
|
53 |
+ |
char out_name[500]; |
54 |
+ |
char tempString[100]; |
55 |
+ |
FILE *out_file; |
56 |
+ |
|
57 |
+ |
|
58 |
+ |
// figure out the type of GofR we are calculating |
59 |
+ |
|
60 |
+ |
if( !strcmp( atom1, "all" ) ){ |
61 |
+ |
|
62 |
+ |
if( !strcmp( atom2, "all" ) ) the_type = all_all; |
63 |
+ |
else{ |
64 |
+ |
the_type = atom_all; |
65 |
+ |
allAtom = atom2; |
66 |
+ |
} |
67 |
+ |
} |
68 |
+ |
else{ |
69 |
+ |
|
70 |
+ |
if( !strcmp( atom2, "all" ) ){ |
71 |
+ |
the_type = atom_all; |
72 |
+ |
allAtom = atom1; |
73 |
+ |
} |
74 |
+ |
else the_type = atom_atom; |
75 |
+ |
} |
76 |
+ |
|
77 |
+ |
// find the box size and delR; |
78 |
+ |
|
79 |
+ |
shortBox = frames[0].boxX; |
80 |
+ |
if( shortBox > frames[0].boxY ) shortBox = frames[0].boxY; |
81 |
+ |
if( shortBox > frames[0].boxZ ) shortBox = frames[0].boxZ; |
82 |
+ |
|
83 |
+ |
delR = ( shortBox / 2.0 ) / GofRBins; |
84 |
+ |
boxVol = frames[0].boxX * frames[0].boxY * frames[0].boxZ; |
85 |
+ |
|
86 |
+ |
// zero the histograms; |
87 |
+ |
|
88 |
+ |
for(i=0; i<GofRBins; i++ ){ |
89 |
+ |
|
90 |
+ |
rValue[i] = 0.0; |
91 |
+ |
the_GofR[i] = 0.0; |
92 |
+ |
histogram[i] = 0; |
93 |
+ |
} |
94 |
+ |
|
95 |
+ |
switch( the_type ){ |
96 |
+ |
|
97 |
+ |
case atom_atom: |
98 |
+ |
|
99 |
+ |
// find the number of each type; |
100 |
+ |
|
101 |
+ |
nAtom1 = 0; |
102 |
+ |
nAtom2 = 0; |
103 |
+ |
for( i=0; i<frames[0].nAtoms; i++ ){ |
104 |
+ |
|
105 |
+ |
if( !strcmp( frames[0].names[i], atom1 ) ) nAtom1++; |
106 |
+ |
if( !strcmp( frames[0].names[i], atom2 ) ) nAtom2++; |
107 |
+ |
} |
108 |
+ |
|
109 |
+ |
if( !nAtom1 ){ |
110 |
+ |
|
111 |
+ |
fprintf( stderr, |
112 |
+ |
"\n" |
113 |
+ |
"GofR error, \"%s\" was not found in the trajectory.\n", |
114 |
+ |
atom1 ); |
115 |
+ |
exit(8); |
116 |
+ |
} |
117 |
+ |
|
118 |
+ |
if( !nAtom2 ){ |
119 |
+ |
|
120 |
+ |
fprintf( stderr, |
121 |
+ |
"\n" |
122 |
+ |
"GofR error, \"%s\" was not found in the trajectory.\n", |
123 |
+ |
atom2 ); |
124 |
+ |
exit(8); |
125 |
+ |
} |
126 |
+ |
|
127 |
+ |
// calculate some of the constants; |
128 |
+ |
|
129 |
+ |
atom1Dens = nAtom1 / boxVol; |
130 |
+ |
atom2Dens = nAtom2 / boxVol; |
131 |
+ |
|
132 |
+ |
atom1Constant = ( 4.0 * M_PI * atom1Dens ) / 3.0; |
133 |
+ |
atom2Constant = ( 4.0 * M_PI * atom2Dens ) / 3.0; |
134 |
+ |
|
135 |
+ |
// calculate the histogram |
136 |
+ |
|
137 |
+ |
for( i=0; i<nFrames; i++){ |
138 |
+ |
for( j=0; j<(frames[i].nAtoms-1); j++ ){ |
139 |
+ |
|
140 |
+ |
if( !strcmp( frames[0].names[j], atom1 ) ){ |
141 |
+ |
|
142 |
+ |
rxj = frames[i].r[j].x; |
143 |
+ |
ryj = frames[i].r[j].y; |
144 |
+ |
rzj = frames[i].r[j].z; |
145 |
+ |
|
146 |
+ |
for( k=j+1; k< frames[i].nAtoms; k++ ){ |
147 |
+ |
|
148 |
+ |
if( !strcmp( frames[0].names[k], atom2 ) ){ |
149 |
+ |
|
150 |
+ |
dx = rxj - frames[i].r[k].x; |
151 |
+ |
dy = ryj - frames[i].r[k].y; |
152 |
+ |
dz = rzj - frames[i].r[k].z; |
153 |
+ |
|
154 |
+ |
map( &dx, &dy, &dz, |
155 |
+ |
frames[i].boxX, frames[i].boxY, frames[i].boxZ ); |
156 |
+ |
|
157 |
+ |
distSqr = (dx * dx) + (dy * dy) + (dz * dz); |
158 |
+ |
dist = sqrt( distSqr ); |
159 |
+ |
|
160 |
+ |
// add to the appropriate bin |
161 |
+ |
bin = (int)( dist / delR ); |
162 |
+ |
if( bin < GofRBins ) histogram[bin] += 2; |
163 |
+ |
} |
164 |
+ |
} |
165 |
+ |
} |
166 |
+ |
|
167 |
+ |
else if( !strcmp( frames[0].names[j], atom2 ) ){ |
168 |
+ |
|
169 |
+ |
rxj = frames[i].r[j].x; |
170 |
+ |
ryj = frames[i].r[j].y; |
171 |
+ |
rzj = frames[i].r[j].z; |
172 |
+ |
|
173 |
+ |
for( k=j+1; k< frames[i].nAtoms; k++ ){ |
174 |
+ |
|
175 |
+ |
if( !strcmp( frames[0].names[k], atom1 ) ){ |
176 |
+ |
|
177 |
+ |
dx = rxj - frames[i].r[k].x; |
178 |
+ |
dy = ryj - frames[i].r[k].y; |
179 |
+ |
dz = rzj - frames[i].r[k].z; |
180 |
+ |
|
181 |
+ |
map( &dx, &dy, &dz, |
182 |
+ |
frames[i].boxX, frames[i].boxY, frames[i].boxZ ); |
183 |
+ |
|
184 |
+ |
distSqr = (dx * dx) + (dy * dy) + (dz * dz); |
185 |
+ |
dist = sqrt( distSqr ); |
186 |
+ |
|
187 |
+ |
// add to the appropriate bin |
188 |
+ |
bin = (int)( dist / delR ); |
189 |
+ |
if( bin < GofRBins ) histogram[bin] += 2; |
190 |
+ |
} |
191 |
+ |
} |
192 |
+ |
} |
193 |
+ |
} |
194 |
+ |
} |
195 |
+ |
|
196 |
+ |
// calculate the GofR |
197 |
+ |
|
198 |
+ |
for( i=0; i<GofRBins; i++ ){ |
199 |
+ |
|
200 |
+ |
rLower = i * delR; |
201 |
+ |
rUpper = rLower + delR; |
202 |
+ |
|
203 |
+ |
volSlice = pow( rUpper, 3.0 ) - pow( rLower, 3.0 ); |
204 |
+ |
nIdeal = volSlice * ( atom1Constant + atom2Constant ); |
205 |
+ |
|
206 |
+ |
the_GofR[i] = histogram[i] / ( nFrames * ( nAtom1 + nAtom2 ) * nIdeal ); |
207 |
+ |
rValue[i] = rLower + ( delR / 2.0 ); |
208 |
+ |
} |
209 |
+ |
|
210 |
+ |
// make the out_name; |
211 |
+ |
|
212 |
+ |
strcpy( out_name, out_prefix ); |
213 |
+ |
sprintf( tempString, "-%s-%s.GofR", atom1, atom2 ); |
214 |
+ |
strcat( out_name, tempString ); |
215 |
+ |
|
216 |
+ |
out_file = fopen( out_name, "w" ); |
217 |
+ |
if( out_file == NULL ){ |
218 |
+ |
fprintf( stderr, |
219 |
+ |
"\n" |
220 |
+ |
"GofR error, unable to open \"%s\" for writing.\n", |
221 |
+ |
out_name ); |
222 |
+ |
exit(8); |
223 |
+ |
} |
224 |
+ |
break; |
225 |
+ |
|
226 |
+ |
case atom_all: |
227 |
+ |
|
228 |
+ |
// find the number of AllAtoms; |
229 |
+ |
|
230 |
+ |
nAtom1 = 0; |
231 |
+ |
for( i=0; i<frames[0].nAtoms; i++ ){ |
232 |
+ |
|
233 |
+ |
if( !strcmp( frames[0].names[i], allAtom ) ) nAtom1++; |
234 |
+ |
} |
235 |
+ |
|
236 |
+ |
if( !nAtom1 ){ |
237 |
+ |
|
238 |
+ |
fprintf( stderr, |
239 |
+ |
"\n" |
240 |
+ |
"GofR error, \"%s\" was not found in the trajectory.\n", |
241 |
+ |
atom1 ); |
242 |
+ |
exit(8); |
243 |
+ |
} |
244 |
+ |
|
245 |
+ |
// calculate some of the constants; |
246 |
+ |
|
247 |
+ |
atom1Dens = nAtom1 / boxVol; |
248 |
+ |
allDens = frames[0].nAtoms / boxVol; |
249 |
+ |
|
250 |
+ |
atom1Constant = ( 4.0 * M_PI * atom1Dens ) / 3.0; |
251 |
+ |
allConstant = ( 4.0 * M_PI * allDens ) / 3.0; |
252 |
+ |
|
253 |
+ |
// calculate the histogram |
254 |
+ |
|
255 |
+ |
for( i=0; i<nFrames; i++){ |
256 |
+ |
for( j=0; j<(frames[i].nAtoms-1); j++ ){ |
257 |
+ |
|
258 |
+ |
if( !strcmp( frames[0].names[j], allAtom ) ){ |
259 |
+ |
|
260 |
+ |
rxj = frames[i].r[j].x; |
261 |
+ |
ryj = frames[i].r[j].y; |
262 |
+ |
rzj = frames[i].r[j].z; |
263 |
+ |
|
264 |
+ |
for( k=j+1; k< frames[i].nAtoms; k++ ){ |
265 |
+ |
|
266 |
+ |
dx = rxj - frames[i].r[k].x; |
267 |
+ |
dy = ryj - frames[i].r[k].y; |
268 |
+ |
dz = rzj - frames[i].r[k].z; |
269 |
+ |
|
270 |
+ |
map( &dx, &dy, &dz, |
271 |
+ |
frames[i].boxX, frames[i].boxY, frames[i].boxZ ); |
272 |
+ |
|
273 |
+ |
distSqr = (dx * dx) + (dy * dy) + (dz * dz); |
274 |
+ |
dist = sqrt( distSqr ); |
275 |
+ |
|
276 |
+ |
// add to the appropriate bin |
277 |
+ |
bin = (int)( dist / delR ); |
278 |
+ |
if( bin < GofRBins ) histogram[bin] += 2; |
279 |
+ |
} |
280 |
+ |
} |
281 |
+ |
|
282 |
+ |
else{ |
283 |
+ |
|
284 |
+ |
rxj = frames[i].r[j].x; |
285 |
+ |
ryj = frames[i].r[j].y; |
286 |
+ |
rzj = frames[i].r[j].z; |
287 |
+ |
|
288 |
+ |
for( k=j+1; k< frames[i].nAtoms; k++ ){ |
289 |
+ |
|
290 |
+ |
if( !strcmp( frames[0].names[k], allAtom ) ){ |
291 |
+ |
|
292 |
+ |
dx = rxj - frames[i].r[k].x; |
293 |
+ |
dy = ryj - frames[i].r[k].y; |
294 |
+ |
dz = rzj - frames[i].r[k].z; |
295 |
+ |
|
296 |
+ |
map( &dx, &dy, &dz, |
297 |
+ |
frames[i].boxX, frames[i].boxY, frames[i].boxZ ); |
298 |
+ |
|
299 |
+ |
distSqr = (dx * dx) + (dy * dy) + (dz * dz); |
300 |
+ |
dist = sqrt( distSqr ); |
301 |
+ |
|
302 |
+ |
// add to the appropriate bin |
303 |
+ |
bin = (int)( dist / delR ); |
304 |
+ |
if( bin < GofRBins ) histogram[bin] += 2; |
305 |
+ |
} |
306 |
+ |
} |
307 |
+ |
} |
308 |
+ |
} |
309 |
+ |
} |
310 |
+ |
|
311 |
+ |
// calculate the GofR |
312 |
+ |
|
313 |
+ |
for( i=0; i<GofRBins; i++ ){ |
314 |
+ |
|
315 |
+ |
rLower = i * delR; |
316 |
+ |
rUpper = rLower + delR; |
317 |
+ |
|
318 |
+ |
volSlice = pow( rUpper, 3.0 ) - pow( rLower, 3.0 ); |
319 |
+ |
nIdeal = volSlice * ( allConstant ); |
320 |
+ |
|
321 |
+ |
the_GofR[i] = histogram[i] / ( nFrames * frames[0].nAtoms * nIdeal ); |
322 |
+ |
rValue[i] = rLower + ( delR / 2.0 ); |
323 |
+ |
} |
324 |
+ |
|
325 |
+ |
// make the out_name; |
326 |
+ |
|
327 |
+ |
strcpy( out_name, out_prefix ); |
328 |
+ |
sprintf( tempString, "-%s-%s.GofR", allAtom, "all" ); |
329 |
+ |
strcat( out_name, tempString ); |
330 |
+ |
|
331 |
+ |
out_file = fopen( out_name, "w" ); |
332 |
+ |
if( out_file == NULL ){ |
333 |
+ |
fprintf( stderr, |
334 |
+ |
"\n" |
335 |
+ |
"GofR error, unable to open \"%s\" for writing.\n", |
336 |
+ |
out_name ); |
337 |
+ |
exit(8); |
338 |
+ |
} |
339 |
+ |
break; |
340 |
+ |
|
341 |
+ |
case all_all: |
342 |
+ |
|
343 |
+ |
// calculate some of the constants; |
344 |
+ |
|
345 |
+ |
allDens = frames[0].nAtoms / boxVol; |
346 |
+ |
|
347 |
+ |
allConstant = ( 4.0 * M_PI * allDens ) / 3.0; |
348 |
+ |
|
349 |
+ |
// calculate the histogram |
350 |
+ |
|
351 |
+ |
for( i=0; i<nFrames; i++){ |
352 |
+ |
for( j=0; j<(frames[i].nAtoms-1); j++ ){ |
353 |
+ |
|
354 |
+ |
rxj = frames[i].r[j].x; |
355 |
+ |
ryj = frames[i].r[j].y; |
356 |
+ |
rzj = frames[i].r[j].z; |
357 |
+ |
|
358 |
+ |
for( k=j+1; k< frames[i].nAtoms; k++ ){ |
359 |
+ |
|
360 |
+ |
dx = rxj - frames[i].r[k].x; |
361 |
+ |
dy = ryj - frames[i].r[k].y; |
362 |
+ |
dz = rzj - frames[i].r[k].z; |
363 |
+ |
|
364 |
+ |
map( &dx, &dy, &dz, |
365 |
+ |
frames[i].boxX, frames[i].boxY, frames[i].boxZ ); |
366 |
+ |
|
367 |
+ |
distSqr = (dx * dx) + (dy * dy) + (dz * dz); |
368 |
+ |
dist = sqrt( distSqr ); |
369 |
+ |
|
370 |
+ |
// add to the appropriate bin |
371 |
+ |
bin = (int)( dist / delR ); |
372 |
+ |
if( bin < GofRBins ) histogram[bin] += 2; |
373 |
+ |
} |
374 |
+ |
} |
375 |
+ |
} |
376 |
+ |
|
377 |
+ |
// calculate the GofR |
378 |
+ |
|
379 |
+ |
for( i=0; i<GofRBins; i++ ){ |
380 |
+ |
|
381 |
+ |
rLower = i * delR; |
382 |
+ |
rUpper = rLower + delR; |
383 |
+ |
|
384 |
+ |
volSlice = pow( rUpper, 3.0 ) - pow( rLower, 3.0 ); |
385 |
+ |
nIdeal = volSlice * ( allConstant ); |
386 |
+ |
|
387 |
+ |
the_GofR[i] = histogram[i] / ( nFrames * frames[0].nAtoms * nIdeal ); |
388 |
+ |
rValue[i] = rLower + ( delR / 2.0 ); |
389 |
+ |
} |
390 |
+ |
|
391 |
+ |
// make the out_name; |
392 |
+ |
|
393 |
+ |
strcpy( out_name, out_prefix ); |
394 |
+ |
sprintf( tempString, "-%s-%s.GofR", "all", "all" ); |
395 |
+ |
strcat( out_name, tempString ); |
396 |
+ |
|
397 |
+ |
out_file = fopen( out_name, "w" ); |
398 |
+ |
if( out_file == NULL ){ |
399 |
+ |
fprintf( stderr, |
400 |
+ |
"\n" |
401 |
+ |
"GofR error, unable to open \"%s\" for writing.\n", |
402 |
+ |
out_name ); |
403 |
+ |
exit(8); |
404 |
+ |
} |
405 |
+ |
break; |
406 |
+ |
} |
407 |
+ |
|
408 |
+ |
for( i=0; i<GofRBins; i++ ){ |
409 |
+ |
fprintf( out_file, |
410 |
+ |
"%lf\t%lf\n", |
411 |
+ |
rValue[i], the_GofR[i] ); |
412 |
+ |
} |
413 |
+ |
|
414 |
+ |
fclose( out_file ); |
415 |
+ |
} |
416 |
+ |
|