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