| 1 |
#include <stdio.h> |
| 2 |
#include <stdlib.h> |
| 3 |
#include <string.h> |
| 4 |
#include <math.h> |
| 5 |
#include <unistd.h> |
| 6 |
#include <sys/types.h> |
| 7 |
#include <sys/stat.h> |
| 8 |
|
| 9 |
struct coords{ |
| 10 |
double pos[3]; // cartesian coords |
| 11 |
double q[4]; // the quanternions |
| 12 |
char name[30]; |
| 13 |
}; |
| 14 |
|
| 15 |
struct linked_xyz{ |
| 16 |
struct coords *r; |
| 17 |
double time; |
| 18 |
double Hmat[3][3]; |
| 19 |
double HmatI[3][3]; |
| 20 |
struct linked_xyz *next; |
| 21 |
}; |
| 22 |
|
| 23 |
char *program_name; /*the name of the program */ |
| 24 |
int printWater = 0; |
| 25 |
int printDipole = 0; |
| 26 |
|
| 27 |
void usage(void); |
| 28 |
|
| 29 |
void rotWrite( FILE* out_file, struct coords* theSSD ); |
| 30 |
|
| 31 |
void setRot( double A[3][3], double q[4] ); |
| 32 |
|
| 33 |
void rotMe( double A[3][3], double r[3] ); |
| 34 |
void wrapVector( double thePos[3], double Hmat[3][3], double HmatI[3][3]); |
| 35 |
double matDet3(double a[3][3]); |
| 36 |
void invertMat3(double a[3][3], double b[3][3]); |
| 37 |
void matVecMul3(double m[3][3], double inVec[3], double outVec[3]); |
| 38 |
double roundMe(double x); |
| 39 |
|
| 40 |
int main(argc, argv) |
| 41 |
int argc; |
| 42 |
char *argv[]; |
| 43 |
{ |
| 44 |
|
| 45 |
|
| 46 |
struct coords *out_coords; |
| 47 |
|
| 48 |
int i, j, k, l, m; /* loop counters */ |
| 49 |
mode_t dir_mode = S_IRWXU; |
| 50 |
|
| 51 |
int lineCount = 0; //the line number |
| 52 |
int newN; // the new number of atoms |
| 53 |
int nSSD=0; // the number of SSD per frame |
| 54 |
|
| 55 |
unsigned int nAtoms; /*the number of atoms in each time step */ |
| 56 |
char read_buffer[1000]; /*the line buffer for reading */ |
| 57 |
char *eof_test; /*ptr to see when we reach the end of the file */ |
| 58 |
char *foo; /*the pointer to the current string token */ |
| 59 |
FILE *in_file; /* the input file */ |
| 60 |
FILE *out_file; /*the output file */ |
| 61 |
char *out_prefix = NULL; /*the prefix of the output file */ |
| 62 |
char out_name[500]; /*the output name */ |
| 63 |
int have_outName =0; |
| 64 |
char in_name[500]; // the input file name |
| 65 |
char temp_name[500]; |
| 66 |
char *root_name = NULL; /*the root name of the input file */ |
| 67 |
unsigned int n_out = 0; /*keeps track of which output file is being written*/ |
| 68 |
|
| 69 |
|
| 70 |
int skipFrames = 1; |
| 71 |
|
| 72 |
struct linked_xyz *current_frame; |
| 73 |
struct linked_xyz *temp_frame; |
| 74 |
|
| 75 |
int nframes =0; |
| 76 |
|
| 77 |
int wrap = 0; |
| 78 |
|
| 79 |
double cX = 0.0; |
| 80 |
double cY = 0.0; |
| 81 |
double cZ = 0.0; |
| 82 |
|
| 83 |
double mcX, mcY, mcZ; |
| 84 |
double newCX, newCY, newCZ; |
| 85 |
double dx, dy, dz; |
| 86 |
int mcount; |
| 87 |
|
| 88 |
int repeatX = 0; |
| 89 |
int repeatY = 0; |
| 90 |
int repeatZ = 0; |
| 91 |
|
| 92 |
int nRepeatX = 0; |
| 93 |
int nRepeatY = 0; |
| 94 |
int nRepeatZ = 0; |
| 95 |
|
| 96 |
struct coords* rCopy; |
| 97 |
|
| 98 |
int done; |
| 99 |
char current_flag; |
| 100 |
|
| 101 |
program_name = argv[0]; /*save the program name in case we need it*/ |
| 102 |
|
| 103 |
for( i = 1; i < argc; i++){ |
| 104 |
|
| 105 |
if(argv[i][0] =='-'){ |
| 106 |
|
| 107 |
if(argv[i][1] == '-' ){ |
| 108 |
|
| 109 |
// parse long word options |
| 110 |
|
| 111 |
if( !strcmp( argv[i], "--repeatX" ) ){ |
| 112 |
repeatX = 1; |
| 113 |
i++; |
| 114 |
nRepeatX = atoi( argv[i] ); |
| 115 |
} |
| 116 |
|
| 117 |
else if( !strcmp( argv[i], "--repeatY" ) ){ |
| 118 |
repeatY = 1; |
| 119 |
i++; |
| 120 |
nRepeatY = atoi( argv[i] ); |
| 121 |
} |
| 122 |
|
| 123 |
else if( !strcmp( argv[i], "--repeatZ" ) ){ |
| 124 |
repeatZ = 1; |
| 125 |
i++; |
| 126 |
nRepeatZ = atoi( argv[i] ); |
| 127 |
} |
| 128 |
|
| 129 |
else{ |
| 130 |
fprintf( stderr, |
| 131 |
"Invalid option \"%s\"\n", argv[i] ); |
| 132 |
usage(); |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
else{ |
| 137 |
|
| 138 |
// parse single character options |
| 139 |
|
| 140 |
done =0; |
| 141 |
j = 1; |
| 142 |
current_flag = argv[i][j]; |
| 143 |
while( (current_flag != '\0') && (!done) ){ |
| 144 |
|
| 145 |
switch(current_flag){ |
| 146 |
|
| 147 |
case 'o': |
| 148 |
// -o <prefix> => the output |
| 149 |
|
| 150 |
i++; |
| 151 |
strcpy( out_name, argv[i] ); |
| 152 |
have_outName = 1; |
| 153 |
done = 1; |
| 154 |
break; |
| 155 |
|
| 156 |
case 'n': |
| 157 |
// -n <#> => print every <#> frames |
| 158 |
|
| 159 |
i++; |
| 160 |
skipFrames = atoi(argv[i]); |
| 161 |
done = 1; |
| 162 |
break; |
| 163 |
|
| 164 |
case 'h': |
| 165 |
// -h => give the usage |
| 166 |
|
| 167 |
usage(); |
| 168 |
break; |
| 169 |
|
| 170 |
case 'd': |
| 171 |
// print the dipoles |
| 172 |
|
| 173 |
printDipole = 1; |
| 174 |
break; |
| 175 |
|
| 176 |
case 'w': |
| 177 |
// print the waters |
| 178 |
|
| 179 |
printWater = 1; |
| 180 |
break; |
| 181 |
|
| 182 |
case 'm': |
| 183 |
// map to a periodic box |
| 184 |
|
| 185 |
wrap = 1; |
| 186 |
break; |
| 187 |
|
| 188 |
default: |
| 189 |
|
| 190 |
fprintf(stderr, "Bad option \"-%c\"\n", current_flag); |
| 191 |
usage(); |
| 192 |
} |
| 193 |
j++; |
| 194 |
current_flag = argv[i][j]; |
| 195 |
} |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
else{ |
| 200 |
|
| 201 |
if( root_name != NULL ){ |
| 202 |
fprintf( stderr, |
| 203 |
"Error at \"%s\", program does not currently support\n" |
| 204 |
"more than one input file.\n" |
| 205 |
"\n", |
| 206 |
argv[i]); |
| 207 |
usage(); |
| 208 |
} |
| 209 |
|
| 210 |
root_name = argv[i]; |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
if(root_name == NULL){ |
| 215 |
usage(); |
| 216 |
} |
| 217 |
|
| 218 |
sprintf( in_name, "%s", root_name ); |
| 219 |
if( !have_outName ) sprintf( out_name, "%s.xyz", root_name ); |
| 220 |
|
| 221 |
in_file = fopen(in_name, "r"); |
| 222 |
if(in_file == NULL){ |
| 223 |
printf("Cannot open file \"%s\" for reading.\n", in_name); |
| 224 |
exit(8); |
| 225 |
} |
| 226 |
|
| 227 |
out_file = fopen( out_name, "w" ); |
| 228 |
if( out_file == NULL ){ |
| 229 |
printf("Cannot open file \"%s\" for writing.\n", out_name); |
| 230 |
exit(8); |
| 231 |
} |
| 232 |
|
| 233 |
// start reading the first frame |
| 234 |
|
| 235 |
eof_test = fgets(read_buffer, sizeof(read_buffer), in_file); |
| 236 |
lineCount++; |
| 237 |
|
| 238 |
current_frame = (struct linked_xyz *)malloc(sizeof(struct linked_xyz)); |
| 239 |
current_frame->next = NULL; |
| 240 |
|
| 241 |
|
| 242 |
while(eof_test != NULL){ |
| 243 |
|
| 244 |
(void)sscanf(read_buffer, "%d", &nAtoms); |
| 245 |
current_frame->r = |
| 246 |
(struct coords *)calloc(nAtoms, sizeof(struct coords)); |
| 247 |
|
| 248 |
// read and the comment line and grab the time and box dimensions |
| 249 |
|
| 250 |
eof_test = fgets(read_buffer, sizeof(read_buffer), in_file); |
| 251 |
lineCount++; |
| 252 |
if(eof_test == NULL){ |
| 253 |
printf("error in reading file at line: %d\n", lineCount); |
| 254 |
exit(8); |
| 255 |
} |
| 256 |
|
| 257 |
foo = strtok( read_buffer, " ,;\t" ); |
| 258 |
sscanf( read_buffer, "%lf", ¤t_frame->time ); |
| 259 |
|
| 260 |
foo = strtok(NULL, " ,;\t"); |
| 261 |
if(foo == NULL){ |
| 262 |
printf("error in reading file at line: %d\n", lineCount); |
| 263 |
exit(8); |
| 264 |
} |
| 265 |
(void)sscanf(foo, "%lf",¤t_frame->Hmat[0][0]); |
| 266 |
|
| 267 |
foo = strtok(NULL, " ,;\t"); |
| 268 |
if(foo == NULL){ |
| 269 |
printf("error in reading file at line: %d\n", lineCount); |
| 270 |
exit(8); |
| 271 |
} |
| 272 |
(void)sscanf(foo, "%lf",¤t_frame->Hmat[1][0]); |
| 273 |
|
| 274 |
foo = strtok(NULL, " ,;\t"); |
| 275 |
if(foo == NULL){ |
| 276 |
printf("error in reading file at line: %d\n", lineCount); |
| 277 |
exit(8); |
| 278 |
} |
| 279 |
(void)sscanf(foo, "%lf",¤t_frame->Hmat[2][0]); |
| 280 |
|
| 281 |
foo = strtok(NULL, " ,;\t"); |
| 282 |
if(foo == NULL){ |
| 283 |
printf("error in reading file at line: %d\n", lineCount); |
| 284 |
exit(8); |
| 285 |
} |
| 286 |
(void)sscanf(foo, "%lf",¤t_frame->Hmat[0][1]); |
| 287 |
|
| 288 |
foo = strtok(NULL, " ,;\t"); |
| 289 |
if(foo == NULL){ |
| 290 |
printf("error in reading file at line: %d\n", lineCount); |
| 291 |
exit(8); |
| 292 |
} |
| 293 |
(void)sscanf(foo, "%lf",¤t_frame->Hmat[1][1]); |
| 294 |
|
| 295 |
foo = strtok(NULL, " ,;\t"); |
| 296 |
if(foo == NULL){ |
| 297 |
printf("error in reading file at line: %d\n", lineCount); |
| 298 |
exit(8); |
| 299 |
} |
| 300 |
(void)sscanf(foo, "%lf",¤t_frame->Hmat[2][1]); |
| 301 |
|
| 302 |
foo = strtok(NULL, " ,;\t"); |
| 303 |
if(foo == NULL){ |
| 304 |
printf("error in reading file at line: %d\n", lineCount); |
| 305 |
exit(8); |
| 306 |
} |
| 307 |
(void)sscanf(foo, "%lf",¤t_frame->Hmat[0][2]); |
| 308 |
|
| 309 |
foo = strtok(NULL, " ,;\t"); |
| 310 |
if(foo == NULL){ |
| 311 |
printf("error in reading file at line: %d\n", lineCount); |
| 312 |
exit(8); |
| 313 |
} |
| 314 |
(void)sscanf(foo, "%lf",¤t_frame->Hmat[1][2]); |
| 315 |
|
| 316 |
foo = strtok(NULL, " ,;\t"); |
| 317 |
if(foo == NULL){ |
| 318 |
printf("error in reading file at line: %d\n", lineCount); |
| 319 |
exit(8); |
| 320 |
} |
| 321 |
(void)sscanf(foo, "%lf",¤t_frame->Hmat[2][2]); |
| 322 |
|
| 323 |
|
| 324 |
// Find HmatI: |
| 325 |
|
| 326 |
invertMat3(current_frame->Hmat, current_frame->HmatI); |
| 327 |
|
| 328 |
|
| 329 |
for( i=0; i < nAtoms; i++){ |
| 330 |
|
| 331 |
eof_test = fgets(read_buffer, sizeof(read_buffer), in_file); |
| 332 |
lineCount++; |
| 333 |
if(eof_test == NULL){ |
| 334 |
printf("error in reading file at line: %d\n", lineCount); |
| 335 |
exit(8); |
| 336 |
} |
| 337 |
|
| 338 |
foo = strtok(read_buffer, " ,;\t"); |
| 339 |
if ( !strcmp( "SSD", foo ) ) nSSD++; |
| 340 |
(void)strcpy(current_frame->r[i].name, foo); //copy the atom name |
| 341 |
|
| 342 |
// next we grab the positions |
| 343 |
|
| 344 |
foo = strtok(NULL, " ,;\t"); |
| 345 |
if(foo == NULL){ |
| 346 |
printf("error in reading postition x from %s\n" |
| 347 |
"natoms = %d, line = %d\n", |
| 348 |
in_name, nAtoms, lineCount ); |
| 349 |
exit(8); |
| 350 |
} |
| 351 |
(void)sscanf( foo, "%lf", ¤t_frame->r[i].pos[0] ); |
| 352 |
|
| 353 |
foo = strtok(NULL, " ,;\t"); |
| 354 |
if(foo == NULL){ |
| 355 |
printf("error in reading postition y from %s\n" |
| 356 |
"natoms = %d, line = %d\n", |
| 357 |
in_name, nAtoms, lineCount ); |
| 358 |
exit(8); |
| 359 |
} |
| 360 |
(void)sscanf( foo, "%lf", ¤t_frame->r[i].pos[1] ); |
| 361 |
|
| 362 |
foo = strtok(NULL, " ,;\t"); |
| 363 |
if(foo == NULL){ |
| 364 |
printf("error in reading postition z from %s\n" |
| 365 |
"natoms = %d, line = %d\n", |
| 366 |
in_name, nAtoms, lineCount ); |
| 367 |
exit(8); |
| 368 |
} |
| 369 |
(void)sscanf( foo, "%lf", ¤t_frame->r[i].pos[2] ); |
| 370 |
|
| 371 |
// get the velocities |
| 372 |
|
| 373 |
foo = strtok(NULL, " ,;\t"); |
| 374 |
if(foo == NULL){ |
| 375 |
printf("error in reading velocity x from %s\n" |
| 376 |
"natoms = %d, line = %d\n", |
| 377 |
in_name, nAtoms, lineCount ); |
| 378 |
exit(8); |
| 379 |
} |
| 380 |
|
| 381 |
|
| 382 |
foo = strtok(NULL, " ,;\t"); |
| 383 |
if(foo == NULL){ |
| 384 |
printf("error in reading velocity y from %s\n" |
| 385 |
"natoms = %d, line = %d\n", |
| 386 |
in_name, nAtoms, lineCount ); |
| 387 |
exit(8); |
| 388 |
} |
| 389 |
|
| 390 |
|
| 391 |
foo = strtok(NULL, " ,;\t"); |
| 392 |
if(foo == NULL){ |
| 393 |
printf("error in reading velocity z from %s\n" |
| 394 |
"natoms = %d, line = %d\n", |
| 395 |
in_name, nAtoms, lineCount ); |
| 396 |
exit(8); |
| 397 |
} |
| 398 |
|
| 399 |
// get the quaternions |
| 400 |
|
| 401 |
foo = strtok(NULL, " ,;\t"); |
| 402 |
if(foo == NULL){ |
| 403 |
printf("error in reading quaternion 0 from %s\n" |
| 404 |
"natoms = %d, line = %d\n", |
| 405 |
in_name, nAtoms, lineCount ); |
| 406 |
exit(8); |
| 407 |
} |
| 408 |
(void)sscanf( foo, "%lf", ¤t_frame->r[i].q[0] ); |
| 409 |
|
| 410 |
foo = strtok(NULL, " ,;\t"); |
| 411 |
if(foo == NULL){ |
| 412 |
printf("error in reading quaternion 1 from %s\n" |
| 413 |
"natoms = %d, line = %d\n", |
| 414 |
in_name, nAtoms, lineCount ); |
| 415 |
exit(8); |
| 416 |
} |
| 417 |
(void)sscanf( foo, "%lf", ¤t_frame->r[i].q[1] ); |
| 418 |
|
| 419 |
foo = strtok(NULL, " ,;\t"); |
| 420 |
if(foo == NULL){ |
| 421 |
printf("error in reading quaternion 2 from %s\n" |
| 422 |
"natoms = %d, line = %d\n", |
| 423 |
in_name, nAtoms, lineCount ); |
| 424 |
exit(8); |
| 425 |
} |
| 426 |
(void)sscanf( foo, "%lf", ¤t_frame->r[i].q[2] ); |
| 427 |
|
| 428 |
foo = strtok(NULL, " ,;\t"); |
| 429 |
if(foo == NULL){ |
| 430 |
printf("error in reading quaternion 3 from %s\n" |
| 431 |
"natoms = %d, line = %d\n", |
| 432 |
in_name, nAtoms, lineCount ); |
| 433 |
exit(8); |
| 434 |
} |
| 435 |
(void)sscanf( foo, "%lf", ¤t_frame->r[i].q[3] ); |
| 436 |
|
| 437 |
// get the angular velocities |
| 438 |
|
| 439 |
foo = strtok(NULL, " ,;\t"); |
| 440 |
if(foo == NULL){ |
| 441 |
printf("error in reading angular momentum jx from %s\n" |
| 442 |
"natoms = %d, line = %d\n", |
| 443 |
in_name, nAtoms, lineCount ); |
| 444 |
exit(8); |
| 445 |
} |
| 446 |
|
| 447 |
foo = strtok(NULL, " ,;\t"); |
| 448 |
if(foo == NULL){ |
| 449 |
printf("error in reading angular momentum jy from %s\n" |
| 450 |
"natoms = %d, line = %d\n", |
| 451 |
in_name, nAtoms, lineCount ); |
| 452 |
exit(8); |
| 453 |
} |
| 454 |
|
| 455 |
foo = strtok(NULL, " ,;\t"); |
| 456 |
if(foo == NULL){ |
| 457 |
printf("error in reading angular momentum jz from %s\n" |
| 458 |
"natoms = %d, line = %d\n", |
| 459 |
in_name, nAtoms, lineCount ); |
| 460 |
exit(8); |
| 461 |
} |
| 462 |
} |
| 463 |
|
| 464 |
// if necassary, wrap into the periodic image |
| 465 |
|
| 466 |
if(wrap){ |
| 467 |
|
| 468 |
for( i=0; i<nAtoms; i++ ){ |
| 469 |
|
| 470 |
/* |
| 471 |
if( !strcmp( current_frame->r[i].name, "HEAD" ) ){ |
| 472 |
|
| 473 |
// find the center of the lipid |
| 474 |
|
| 475 |
mcX = 0.0; |
| 476 |
mcY = 0.0; |
| 477 |
mcZ = 0.0; |
| 478 |
mcount = 0; |
| 479 |
|
| 480 |
mcX += current_frame->r[i].x; |
| 481 |
mcY += current_frame->r[i].y; |
| 482 |
mcZ += current_frame->r[i].z; |
| 483 |
mcount++; |
| 484 |
i++; |
| 485 |
|
| 486 |
while( strcmp( current_frame->r[i].name, "HEAD" ) && |
| 487 |
strcmp( current_frame->r[i].name, "SSD" ) ){ |
| 488 |
|
| 489 |
mcX += current_frame->r[i].x; |
| 490 |
mcY += current_frame->r[i].y; |
| 491 |
mcZ += current_frame->r[i].z; |
| 492 |
mcount++; |
| 493 |
i++; |
| 494 |
} |
| 495 |
|
| 496 |
mcX /= mcount; |
| 497 |
mcY /= mcount; |
| 498 |
mcZ /= mcount; |
| 499 |
|
| 500 |
newCX = mcX; |
| 501 |
newCY = mcY; |
| 502 |
newCZ = mcZ; |
| 503 |
|
| 504 |
map( &newCX, &newCY, &newCZ, |
| 505 |
cX, cY, cZ, |
| 506 |
current_frame->boxX, |
| 507 |
current_frame->boxY, |
| 508 |
current_frame->boxZ ); |
| 509 |
|
| 510 |
dx = mcX - newCX; |
| 511 |
dy = mcY - newCY; |
| 512 |
dz = mcZ - newCZ; |
| 513 |
|
| 514 |
for(j=(i-mcount); j<mcount; j++ ){ |
| 515 |
|
| 516 |
current_frame->r[j].x -= dx; |
| 517 |
current_frame->r[j].y -= dy; |
| 518 |
current_frame->r[j].z -= dz; |
| 519 |
} |
| 520 |
|
| 521 |
i--; // so we don't skip the next atom |
| 522 |
} |
| 523 |
*/ |
| 524 |
// else |
| 525 |
|
| 526 |
wrapVector(current_frame->r[i].pos, |
| 527 |
current_frame->Hmat, |
| 528 |
current_frame->HmatI); |
| 529 |
|
| 530 |
} |
| 531 |
} |
| 532 |
|
| 533 |
nframes++; |
| 534 |
|
| 535 |
// write out here |
| 536 |
|
| 537 |
if(printWater) newN = nAtoms + ( nSSD * 3 ); |
| 538 |
else newN = nAtoms - nSSD; |
| 539 |
|
| 540 |
newN *= (nRepeatX+1); |
| 541 |
newN *= (nRepeatY+1); |
| 542 |
newN *= (nRepeatZ+1); |
| 543 |
|
| 544 |
if( !(nframes%skipFrames) ){ |
| 545 |
fprintf( out_file, |
| 546 |
"%d\n" |
| 547 |
"%lf;\t%lf\t%lf\t%lf;\t%lf\t%lf\t%lf;\t%lf\t%lf\t%lf;\n", |
| 548 |
newN, current_frame->time, |
| 549 |
current_frame->Hmat[0][0], current_frame->Hmat[1][0], |
| 550 |
current_frame->Hmat[2][0], |
| 551 |
current_frame->Hmat[0][1], current_frame->Hmat[1][1], |
| 552 |
current_frame->Hmat[2][1], |
| 553 |
current_frame->Hmat[0][2], current_frame->Hmat[1][2], |
| 554 |
current_frame->Hmat[2][2] ); |
| 555 |
|
| 556 |
rCopy = (struct coords*) |
| 557 |
calloc( nAtoms, sizeof( struct coords )); |
| 558 |
|
| 559 |
for(i=0; i<nAtoms; i++){ |
| 560 |
rCopy[i].q[0] = current_frame->r[i].q[0]; |
| 561 |
rCopy[i].q[1] = current_frame->r[i].q[1]; |
| 562 |
rCopy[i].q[2] = current_frame->r[i].q[2]; |
| 563 |
rCopy[i].q[3] = current_frame->r[i].q[3]; |
| 564 |
|
| 565 |
strcpy(rCopy[i].name, current_frame->r[i].name); |
| 566 |
} |
| 567 |
|
| 568 |
for(j=0; j<(nRepeatX+1); j++){ |
| 569 |
for(k=0; k<(nRepeatY+1); k++){ |
| 570 |
for(l=0; l<(nRepeatZ+1); l++){ |
| 571 |
|
| 572 |
for(i=0; i<nAtoms; i++){ |
| 573 |
|
| 574 |
for(m = 0; m < 3; m++) { |
| 575 |
rCopy[i].pos[m] = current_frame->r[i].pos[m] + |
| 576 |
j * current_frame->Hmat[m][0] + |
| 577 |
k * current_frame->Hmat[m][1] + |
| 578 |
l * current_frame->Hmat[m][2]; |
| 579 |
} |
| 580 |
|
| 581 |
if( !strcmp( "SSD", rCopy[i].name ) ){ |
| 582 |
|
| 583 |
rotWrite( out_file, &rCopy[i] ); |
| 584 |
} |
| 585 |
|
| 586 |
else if( !strcmp( "HEAD", rCopy[i].name ) ){ |
| 587 |
|
| 588 |
rotWrite( out_file, &rCopy[i] ); |
| 589 |
} |
| 590 |
else{ |
| 591 |
//modify |
| 592 |
fprintf( out_file, |
| 593 |
"%s\t%lf\t%lf\t%lf\t0.0\t0.0\t0.0\n", |
| 594 |
rCopy[i].name, |
| 595 |
rCopy[i].pos[0], |
| 596 |
rCopy[i].pos[1], |
| 597 |
rCopy[i].pos[2] ); |
| 598 |
} |
| 599 |
} |
| 600 |
} |
| 601 |
} |
| 602 |
} |
| 603 |
|
| 604 |
free( rCopy ); |
| 605 |
} |
| 606 |
|
| 607 |
/*free up memory */ |
| 608 |
|
| 609 |
temp_frame = current_frame->next; |
| 610 |
current_frame->next = NULL; |
| 611 |
|
| 612 |
if(temp_frame != NULL){ |
| 613 |
|
| 614 |
free(temp_frame->r); |
| 615 |
free(temp_frame); |
| 616 |
} |
| 617 |
|
| 618 |
/* make a new frame */ |
| 619 |
|
| 620 |
temp_frame = (struct linked_xyz *)malloc(sizeof(struct linked_xyz)); |
| 621 |
temp_frame->next = current_frame; |
| 622 |
current_frame = temp_frame; |
| 623 |
|
| 624 |
nSSD = 0; |
| 625 |
|
| 626 |
eof_test = fgets(read_buffer, sizeof(read_buffer), in_file); |
| 627 |
lineCount++; |
| 628 |
} |
| 629 |
|
| 630 |
(void)fclose(in_file); |
| 631 |
|
| 632 |
return 0; |
| 633 |
|
| 634 |
} |
| 635 |
|
| 636 |
|
| 637 |
|
| 638 |
void rotWrite( FILE* out_file, struct coords* theDipole ){ |
| 639 |
|
| 640 |
double u[3]; |
| 641 |
double h1[3]; |
| 642 |
double h2[3]; |
| 643 |
double ox[3]; |
| 644 |
|
| 645 |
double A[3][3]; |
| 646 |
|
| 647 |
|
| 648 |
u[0] = 0.0; u[1] = 0.0; u[2] = 1.0; |
| 649 |
|
| 650 |
h1[0] = 0.0; h1[1] = -0.75695; h1[2] = 0.5206; |
| 651 |
|
| 652 |
h2[0] = 0.0; h2[1] = 0.75695; h2[2] = 0.5206; |
| 653 |
|
| 654 |
ox[0] = 0.0; ox[1] = 0.0; ox[2] = -0.0654; |
| 655 |
|
| 656 |
|
| 657 |
setRot( A, theDipole->q ); |
| 658 |
rotMe( A, u ); |
| 659 |
|
| 660 |
if( !strcmp( "SSD", theDipole->name )){ |
| 661 |
|
| 662 |
rotMe( A, h1 ); |
| 663 |
rotMe( A, h2 ); |
| 664 |
rotMe( A, ox ); |
| 665 |
|
| 666 |
if( printWater ){ |
| 667 |
|
| 668 |
if(printDipole) { |
| 669 |
|
| 670 |
fprintf( out_file, |
| 671 |
"X\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\n", |
| 672 |
theDipole->pos[0], |
| 673 |
theDipole->pos[1], |
| 674 |
theDipole->pos[2], |
| 675 |
u[0], u[1], u[2] ); |
| 676 |
|
| 677 |
fprintf( out_file, |
| 678 |
"O\t%lf\t%lf\t%lf\t0.0\t0.0\t0.0\n", |
| 679 |
theDipole->pos[0] + ox[0], |
| 680 |
theDipole->pos[1] + ox[1], |
| 681 |
theDipole->pos[2] + ox[2] ); |
| 682 |
|
| 683 |
fprintf( out_file, |
| 684 |
"H\t%lf\t%lf\t%lf\t0.0\t0.0\t0.0\n", |
| 685 |
theDipole->pos[0] + h1[0], |
| 686 |
theDipole->pos[1] + h1[1], |
| 687 |
theDipole->pos[2] + h1[2] ); |
| 688 |
|
| 689 |
fprintf( out_file, |
| 690 |
"H\t%lf\t%lf\t%lf\t0.0\t0.0\t0.0\n", |
| 691 |
theDipole->pos[0] + h2[0], |
| 692 |
theDipole->pos[1] + h2[1], |
| 693 |
theDipole->pos[2] + h2[2] ); |
| 694 |
} |
| 695 |
else{ |
| 696 |
|
| 697 |
fprintf( out_file, |
| 698 |
"X\t%lf\t%lf\t%lf\n", |
| 699 |
theDipole->pos[0], |
| 700 |
theDipole->pos[1], |
| 701 |
theDipole->pos[2] ); |
| 702 |
|
| 703 |
fprintf( out_file, |
| 704 |
"O\t%lf\t%lf\t%lf\n", |
| 705 |
theDipole->pos[0] + ox[0], |
| 706 |
theDipole->pos[1] + ox[1], |
| 707 |
theDipole->pos[2] + ox[2] ); |
| 708 |
|
| 709 |
fprintf( out_file, |
| 710 |
"H\t%lf\t%lf\t%lf\n", |
| 711 |
theDipole->pos[0] + h1[0], |
| 712 |
theDipole->pos[1] + h1[1], |
| 713 |
theDipole->pos[2] + h1[2] ); |
| 714 |
|
| 715 |
fprintf( out_file, |
| 716 |
"H\t%lf\t%lf\t%lf\n", |
| 717 |
theDipole->pos[0] + h2[0], |
| 718 |
theDipole->pos[1] + h2[1], |
| 719 |
theDipole->pos[2] + h2[2] ); |
| 720 |
} |
| 721 |
} |
| 722 |
} |
| 723 |
|
| 724 |
else if( !strcmp( "HEAD", theDipole->name )) { |
| 725 |
|
| 726 |
if( printDipole ){ |
| 727 |
|
| 728 |
fprintf( out_file, |
| 729 |
"HEAD\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\n", |
| 730 |
theDipole->pos[0], |
| 731 |
theDipole->pos[1], |
| 732 |
theDipole->pos[2], |
| 733 |
u[0], u[1], u[2] ); |
| 734 |
} |
| 735 |
else{ |
| 736 |
|
| 737 |
fprintf( out_file, |
| 738 |
"HEAD\t%lf\t%lf\t%lf\n", |
| 739 |
theDipole->pos[0], |
| 740 |
theDipole->pos[1], |
| 741 |
theDipole->pos[2] ); |
| 742 |
} |
| 743 |
} |
| 744 |
} |
| 745 |
|
| 746 |
|
| 747 |
void setRot( double A[3][3], double the_q[4] ){ |
| 748 |
|
| 749 |
double q0Sqr, q1Sqr, q2Sqr, q3Sqr; |
| 750 |
|
| 751 |
q0Sqr = the_q[0] * the_q[0]; |
| 752 |
q1Sqr = the_q[1] * the_q[1]; |
| 753 |
q2Sqr = the_q[2] * the_q[2]; |
| 754 |
q3Sqr = the_q[3] * the_q[3]; |
| 755 |
|
| 756 |
A[0][0] = q0Sqr + q1Sqr - q2Sqr - q3Sqr; |
| 757 |
A[0][1] = 2.0 * ( the_q[1] * the_q[2] + the_q[0] * the_q[3] ); |
| 758 |
A[0][2] = 2.0 * ( the_q[1] * the_q[3] - the_q[0] * the_q[2] ); |
| 759 |
|
| 760 |
A[1][0] = 2.0 * ( the_q[1] * the_q[2] - the_q[0] * the_q[3] ); |
| 761 |
A[1][1] = q0Sqr - q1Sqr + q2Sqr - q3Sqr; |
| 762 |
A[1][2] = 2.0 * ( the_q[2] * the_q[3] + the_q[0] * the_q[1] ); |
| 763 |
|
| 764 |
A[2][0] = 2.0 * ( the_q[1] * the_q[3] + the_q[0] * the_q[2] ); |
| 765 |
A[2][1] = 2.0 * ( the_q[2] * the_q[3] - the_q[0] * the_q[1] ); |
| 766 |
A[2][2] = q0Sqr - q1Sqr -q2Sqr +q3Sqr; |
| 767 |
} |
| 768 |
|
| 769 |
|
| 770 |
|
| 771 |
void rotMe( double A[3][3], double r[3] ){ |
| 772 |
|
| 773 |
int i,j; |
| 774 |
double rb[3]; // the body frame vector |
| 775 |
|
| 776 |
for(i=0; i<3; i++ ){ |
| 777 |
rb[i] = r[i]; |
| 778 |
} |
| 779 |
|
| 780 |
for( i=0; i<3; i++ ){ |
| 781 |
r[i] = 0.0; |
| 782 |
|
| 783 |
for( j=0; j<3; j++ ){ |
| 784 |
r[i] += A[j][i] * rb[j]; |
| 785 |
} |
| 786 |
} |
| 787 |
} |
| 788 |
|
| 789 |
|
| 790 |
|
| 791 |
/*************************************************************************** |
| 792 |
* prints out the usage for the command line arguments, then exits. |
| 793 |
***************************************************************************/ |
| 794 |
|
| 795 |
void usage(){ |
| 796 |
(void)fprintf(stderr, |
| 797 |
"The proper usage is: %s [options] <dumpfile>\n" |
| 798 |
"\n" |
| 799 |
"Options:\n" |
| 800 |
"\n" |
| 801 |
" -h Display this message\n" |
| 802 |
" -o <out_name> The output file (Defaults to <dumpfile>.xyz)\n" |
| 803 |
" -d print the dipole moments\n" |
| 804 |
" -w print the waters\n" |
| 805 |
" -m map to the periodic box\n" |
| 806 |
" -n <#> print every <#> frames\n" |
| 807 |
"\n" |
| 808 |
" --repeatX <#> The number of images to repeat in the x direction\n" |
| 809 |
" --repeatY <#> The number of images to repeat in the y direction\n" |
| 810 |
" --repeatZ <#> The number of images to repeat in the z direction\n" |
| 811 |
|
| 812 |
"\n", |
| 813 |
|
| 814 |
program_name); |
| 815 |
exit(8); |
| 816 |
} |
| 817 |
|
| 818 |
void wrapVector( double thePos[3], double Hmat[3][3], double HmatInv[3][3]){ |
| 819 |
|
| 820 |
int i; |
| 821 |
double scaled[3]; |
| 822 |
|
| 823 |
// calc the scaled coordinates. |
| 824 |
|
| 825 |
matVecMul3(HmatInv, thePos, scaled); |
| 826 |
|
| 827 |
for(i=0; i<3; i++) |
| 828 |
scaled[i] -= roundMe(scaled[i]); |
| 829 |
|
| 830 |
// calc the wrapped real coordinates from the wrapped scaled coordinates |
| 831 |
|
| 832 |
matVecMul3(Hmat, scaled, thePos); |
| 833 |
|
| 834 |
} |
| 835 |
|
| 836 |
double matDet3(double a[3][3]) { |
| 837 |
int i, j, k; |
| 838 |
double determinant; |
| 839 |
|
| 840 |
determinant = 0.0; |
| 841 |
|
| 842 |
for(i = 0; i < 3; i++) { |
| 843 |
j = (i+1)%3; |
| 844 |
k = (i+2)%3; |
| 845 |
|
| 846 |
determinant += a[0][i] * (a[1][j]*a[2][k] - a[1][k]*a[2][j]); |
| 847 |
} |
| 848 |
return determinant; |
| 849 |
} |
| 850 |
|
| 851 |
void invertMat3(double a[3][3], double b[3][3]) { |
| 852 |
|
| 853 |
int i, j, k, l, m, n; |
| 854 |
double determinant; |
| 855 |
|
| 856 |
determinant = matDet3( a ); |
| 857 |
|
| 858 |
if (determinant == 0.0) { |
| 859 |
printf("Can't invert a matrix with a zero determinant!\n"); |
| 860 |
} |
| 861 |
|
| 862 |
for (i=0; i < 3; i++) { |
| 863 |
j = (i+1)%3; |
| 864 |
k = (i+2)%3; |
| 865 |
for(l = 0; l < 3; l++) { |
| 866 |
m = (l+1)%3; |
| 867 |
n = (l+2)%3; |
| 868 |
|
| 869 |
b[l][i] = (a[j][m]*a[k][n] - a[j][n]*a[k][m]) / determinant; |
| 870 |
} |
| 871 |
} |
| 872 |
} |
| 873 |
|
| 874 |
|
| 875 |
void matVecMul3(double m[3][3], double inVec[3], double outVec[3]) { |
| 876 |
double a0, a1, a2; |
| 877 |
|
| 878 |
a0 = inVec[0]; a1 = inVec[1]; a2 = inVec[2]; |
| 879 |
|
| 880 |
outVec[0] = m[0][0]*a0 + m[0][1]*a1 + m[0][2]*a2; |
| 881 |
outVec[1] = m[1][0]*a0 + m[1][1]*a1 + m[1][2]*a2; |
| 882 |
outVec[2] = m[2][0]*a0 + m[2][1]*a1 + m[2][2]*a2; |
| 883 |
} |
| 884 |
|
| 885 |
double roundMe( double x ){ |
| 886 |
return ( x >= 0 ) ? floor( x + 0.5 ) : ceil( x - 0.5 ); |
| 887 |
} |