| 1 |
#include <iostream> |
| 2 |
#include <cmath> |
| 3 |
|
| 4 |
#include <stdio.h> |
| 5 |
#include <stdlib.h> |
| 6 |
#include <string.h> |
| 7 |
#include <unistd.h> |
| 8 |
#include <sys/types.h> |
| 9 |
#include <sys/stat.h> |
| 10 |
|
| 11 |
#include "ReadWrite.hpp" |
| 12 |
#include "simError.h" |
| 13 |
|
| 14 |
#ifdef IS_MPI |
| 15 |
#include "mpiSimulation.hpp" |
| 16 |
|
| 17 |
#define TAKE_THIS_TAG 0 |
| 18 |
#endif // is_mpi |
| 19 |
|
| 20 |
InitializeFromFile :: InitializeFromFile( char *in_name ){ |
| 21 |
#ifdef IS_MPI |
| 22 |
if (worldRank == 0) { |
| 23 |
#endif |
| 24 |
|
| 25 |
c_in_file = fopen(in_name, "r"); |
| 26 |
if(c_in_file == NULL){ |
| 27 |
sprintf(painCave.errMsg, |
| 28 |
"Cannot open file: %s\n", in_name); |
| 29 |
painCave.isFatal = 1; |
| 30 |
simError(); |
| 31 |
} |
| 32 |
|
| 33 |
strcpy( c_in_name, in_name); |
| 34 |
#ifdef IS_MPI |
| 35 |
} |
| 36 |
strcpy( checkPointMsg, "Infile opened for reading successfully." ); |
| 37 |
MPIcheckPoint(); |
| 38 |
#endif |
| 39 |
return; |
| 40 |
} |
| 41 |
|
| 42 |
InitializeFromFile :: ~InitializeFromFile( ){ |
| 43 |
#ifdef IS_MPI |
| 44 |
if (worldRank == 0) { |
| 45 |
#endif |
| 46 |
int error; |
| 47 |
error = fclose( c_in_file ); |
| 48 |
if( error ){ |
| 49 |
sprintf( painCave.errMsg, |
| 50 |
"Error closing %s\n", c_in_name ); |
| 51 |
simError(); |
| 52 |
} |
| 53 |
#ifdef IS_MPI |
| 54 |
} |
| 55 |
strcpy( checkPointMsg, "Infile closed successfully." ); |
| 56 |
MPIcheckPoint(); |
| 57 |
#endif |
| 58 |
|
| 59 |
return; |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
void InitializeFromFile :: read_xyz( SimInfo* the_entry_plug ){ |
| 64 |
|
| 65 |
int i; // loop counter |
| 66 |
|
| 67 |
const int BUFFERSIZE = 2000; // size of the read buffer |
| 68 |
int n_atoms; // the number of atoms |
| 69 |
char read_buffer[BUFFERSIZE]; //the line buffer for reading |
| 70 |
#ifdef IS_MPI |
| 71 |
char send_buffer[BUFFERSIZE]; |
| 72 |
#endif |
| 73 |
|
| 74 |
char *eof_test; // ptr to see when we reach the end of the file |
| 75 |
char *parseErr; |
| 76 |
int procIndex; |
| 77 |
|
| 78 |
entry_plug = the_entry_plug; |
| 79 |
|
| 80 |
|
| 81 |
#ifndef IS_MPI |
| 82 |
eof_test = fgets(read_buffer, sizeof(read_buffer), c_in_file); |
| 83 |
if( eof_test == NULL ){ |
| 84 |
sprintf( painCave.errMsg, |
| 85 |
"InitializeFromFile error: error reading 1st line of \"%s\"\n", |
| 86 |
c_in_name ); |
| 87 |
painCave.isFatal = 1; |
| 88 |
simError(); |
| 89 |
} |
| 90 |
|
| 91 |
n_atoms = atoi( read_buffer ); |
| 92 |
|
| 93 |
Atom **atoms = entry_plug->atoms; |
| 94 |
DirectionalAtom* dAtom; |
| 95 |
|
| 96 |
if( n_atoms != entry_plug->n_atoms ){ |
| 97 |
sprintf( painCave.errMsg, |
| 98 |
"Initialize from File error. %s n_atoms, %d, " |
| 99 |
"does not match the BASS file's n_atoms, %d.\n", |
| 100 |
c_in_name, n_atoms, entry_plug->n_atoms ); |
| 101 |
painCave.isFatal = 1; |
| 102 |
simError(); |
| 103 |
} |
| 104 |
|
| 105 |
//read and toss the comment line |
| 106 |
|
| 107 |
eof_test = fgets(read_buffer, sizeof(read_buffer), c_in_file); |
| 108 |
if(eof_test == NULL){ |
| 109 |
sprintf( painCave.errMsg, |
| 110 |
"error in reading commment in %s\n", c_in_name); |
| 111 |
painCave.isFatal = 1; |
| 112 |
simError(); |
| 113 |
} |
| 114 |
|
| 115 |
for( i=0; i < n_atoms; i++){ |
| 116 |
|
| 117 |
eof_test = fgets(read_buffer, sizeof(read_buffer), c_in_file); |
| 118 |
if(eof_test == NULL){ |
| 119 |
sprintf(painCave.errMsg, |
| 120 |
"error in reading file %s\n" |
| 121 |
"natoms = %d; index = %d\n" |
| 122 |
"error reading the line from the file.\n", |
| 123 |
c_in_name, n_atoms, i ); |
| 124 |
painCave.isFatal = 1; |
| 125 |
simError(); |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
parseErr = parseDumpLine( read_buffer, i ); |
| 130 |
if( parseErr != NULL ){ |
| 131 |
strcpy( painCave.errMsg, parseErr ); |
| 132 |
painCave.isFatal = 1; |
| 133 |
simError(); |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
// MPI Section of code.......... |
| 139 |
#else //IS_MPI |
| 140 |
|
| 141 |
int masterIndex; |
| 142 |
int nodeAtomsStart; |
| 143 |
int nodeAtomsEnd; |
| 144 |
int mpiErr; |
| 145 |
int sendError; |
| 146 |
|
| 147 |
MPI_Status istatus[MPI_STATUS_SIZE]; |
| 148 |
|
| 149 |
if (worldRank == 0) { |
| 150 |
eof_test = fgets(read_buffer, sizeof(read_buffer), c_in_file); |
| 151 |
if( eof_test == NULL ){ |
| 152 |
sprintf( painCave.errMsg, |
| 153 |
"Error reading 1st line of %d \n ",c_in_name); |
| 154 |
painCave.isFatal = 1; |
| 155 |
simError(); |
| 156 |
} |
| 157 |
|
| 158 |
n_atoms = atoi( read_buffer ); |
| 159 |
|
| 160 |
Atom **atoms = entry_plug->atoms; |
| 161 |
DirectionalAtom* dAtom; |
| 162 |
|
| 163 |
// Check to see that the number of atoms in the intial configuration file is the |
| 164 |
// same as declared in simBass. |
| 165 |
|
| 166 |
if( n_atoms != mpiSim->getTotAtoms() ){ |
| 167 |
sprintf( painCave.errMsg, |
| 168 |
"Initialize from File error. %s n_atoms, %d, " |
| 169 |
"does not match the BASS file's n_atoms, %d.\n", |
| 170 |
c_in_name, n_atoms, entry_plug->n_atoms ); |
| 171 |
painCave.isFatal = 1; |
| 172 |
simError(); |
| 173 |
} |
| 174 |
|
| 175 |
//read and toss the comment line |
| 176 |
|
| 177 |
eof_test = fgets(read_buffer, sizeof(read_buffer), c_in_file); |
| 178 |
if(eof_test == NULL){ |
| 179 |
sprintf( painCave.errMsg, |
| 180 |
"error in reading commment in %s\n", c_in_name); |
| 181 |
painCave.isFatal = 1; |
| 182 |
simError(); |
| 183 |
} |
| 184 |
|
| 185 |
// Read Proc 0 share of the xyz file... |
| 186 |
masterIndex = 0; |
| 187 |
for( i=0; i <= mpiSim->getMyAtomEnd(); i++){ |
| 188 |
|
| 189 |
eof_test = fgets(read_buffer, sizeof(read_buffer), c_in_file); |
| 190 |
if(eof_test == NULL){ |
| 191 |
sprintf(painCave.errMsg, |
| 192 |
"error in reading file %s\n" |
| 193 |
"natoms = %d; index = %d\n" |
| 194 |
"error reading the line from the file.\n", |
| 195 |
c_in_name, n_atoms, i ); |
| 196 |
painCave.isFatal = 1; |
| 197 |
simError(); |
| 198 |
} |
| 199 |
|
| 200 |
parseErr = parseDumpLine( read_buffer, i ); |
| 201 |
if( parseErr != NULL ){ |
| 202 |
strcpy( painCave.errMsg, parseErr ); |
| 203 |
painCave.isFatal = 1; |
| 204 |
simError(); |
| 205 |
} |
| 206 |
masterIndex++; |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
sprintf(checkPointMsg, |
| 211 |
"Node 0 has successfully read positions from input file."); |
| 212 |
MPIcheckPoint(); |
| 213 |
|
| 214 |
for (procIndex = 1; procIndex < mpiSim->getNumberProcessors(); |
| 215 |
procIndex++){ |
| 216 |
if (worldRank == 0) { |
| 217 |
|
| 218 |
mpiErr = MPI_Recv(&nodeAtomsStart,1,MPI_INT,procIndex,TAKE_THIS_TAG,MPI_COMM_WORLD, |
| 219 |
istatus); |
| 220 |
|
| 221 |
mpiErr = MPI_Recv(&nodeAtomsEnd,1,MPI_INT,procIndex,TAKE_THIS_TAG,MPI_COMM_WORLD, |
| 222 |
istatus); |
| 223 |
// Make sure where node 0 is reading from, matches where the receiving node |
| 224 |
// expects it to be. |
| 225 |
|
| 226 |
if (masterIndex != nodeAtomsStart){ |
| 227 |
sendError = 1; |
| 228 |
mpiErr = MPI_Send(&sendError,1,MPI_INT,procIndex,TAKE_THIS_TAG,MPI_COMM_WORLD); |
| 229 |
sprintf(painCave.errMsg, |
| 230 |
"Initialize from file error: atoms start index (%d) for " |
| 231 |
"node %d not equal to master index (%d)",nodeAtomsStart,procIndex,masterIndex ); |
| 232 |
painCave.isFatal = 1; |
| 233 |
simError(); |
| 234 |
} |
| 235 |
sendError = 0; |
| 236 |
mpiErr = MPI_Send(&sendError,1,MPI_INT,procIndex,TAKE_THIS_TAG,MPI_COMM_WORLD); |
| 237 |
|
| 238 |
for ( i = nodeAtomsStart; i <= nodeAtomsEnd; i++){ |
| 239 |
eof_test = fgets(read_buffer, sizeof(read_buffer), c_in_file); |
| 240 |
if(eof_test == NULL){ |
| 241 |
|
| 242 |
sprintf(read_buffer,"ERROR"); |
| 243 |
mpiErr = MPI_Send(read_buffer,BUFFERSIZE,MPI_CHAR,procIndex,TAKE_THIS_TAG,MPI_COMM_WORLD); |
| 244 |
|
| 245 |
sprintf(painCave.errMsg, |
| 246 |
"error in reading file %s\n" |
| 247 |
"natoms = %d; index = %d\n" |
| 248 |
"error reading the line from the file.\n", |
| 249 |
c_in_name, n_atoms, i ); |
| 250 |
painCave.isFatal = 1; |
| 251 |
simError(); |
| 252 |
} |
| 253 |
|
| 254 |
mpiErr = MPI_Send(read_buffer,BUFFERSIZE,MPI_CHAR,procIndex,TAKE_THIS_TAG,MPI_COMM_WORLD); |
| 255 |
mpiErr = MPI_Recv(&sendError,1,MPI_INT,procIndex,TAKE_THIS_TAG,MPI_COMM_WORLD, |
| 256 |
istatus); |
| 257 |
if (sendError) MPIcheckPoint(); |
| 258 |
|
| 259 |
masterIndex++; |
| 260 |
} |
| 261 |
} |
| 262 |
|
| 263 |
|
| 264 |
else if(worldRank == procIndex){ |
| 265 |
nodeAtomsStart = mpiSim->getMyAtomStart(); |
| 266 |
nodeAtomsEnd = mpiSim->getMyAtomEnd(); |
| 267 |
mpiErr = MPI_Send(&nodeAtomsStart,1,MPI_INT,0,TAKE_THIS_TAG,MPI_COMM_WORLD); |
| 268 |
mpiErr = MPI_Send(&nodeAtomsEnd,1,MPI_INT,0,TAKE_THIS_TAG,MPI_COMM_WORLD); |
| 269 |
|
| 270 |
mpiErr = MPI_Recv(&sendError,1,MPI_INT,0,TAKE_THIS_TAG,MPI_COMM_WORLD, |
| 271 |
istatus); |
| 272 |
if (sendError) MPIcheckPoint(); |
| 273 |
|
| 274 |
for ( i = 0; i < entry_plug->n_atoms; i++){ |
| 275 |
|
| 276 |
mpiErr = MPI_Recv(&read_buffer,BUFFERSIZE,MPI_CHAR,0,TAKE_THIS_TAG,MPI_COMM_WORLD, |
| 277 |
istatus); |
| 278 |
|
| 279 |
if(!strcmp(read_buffer, "ERROR")) MPIcheckPoint(); |
| 280 |
|
| 281 |
parseErr = parseDumpLine( read_buffer, i ); |
| 282 |
if( parseErr != NULL ){ |
| 283 |
sendError = 1; |
| 284 |
mpiErr = MPI_Send(&sendError,1,MPI_INT,0,TAKE_THIS_TAG,MPI_COMM_WORLD); |
| 285 |
|
| 286 |
|
| 287 |
strcpy( painCave.errMsg, parseErr ); |
| 288 |
painCave.isFatal = 1; |
| 289 |
simError(); |
| 290 |
} |
| 291 |
sendError = 0; |
| 292 |
mpiErr = MPI_Send(&sendError,1,MPI_INT,0,TAKE_THIS_TAG,MPI_COMM_WORLD); |
| 293 |
} |
| 294 |
} |
| 295 |
sprintf(checkPointMsg,"Node %d received initial configuration.",procIndex); |
| 296 |
MPIcheckPoint(); |
| 297 |
} |
| 298 |
|
| 299 |
#endif |
| 300 |
} |
| 301 |
|
| 302 |
|
| 303 |
char* InitializeFromFile::parseDumpLine(char* readLine, int atomIndex){ |
| 304 |
|
| 305 |
char *foo; // the pointer to the current string token |
| 306 |
|
| 307 |
double rx, ry, rz; // position place holders |
| 308 |
double vx, vy, vz; // velocity placeholders |
| 309 |
double q[4]; // the quaternions |
| 310 |
double jx, jy, jz; // angular velocity placeholders; |
| 311 |
double qSqr, qLength; // needed to normalize the quaternion vector. |
| 312 |
|
| 313 |
Atom **atoms = entry_plug->atoms; |
| 314 |
DirectionalAtom* dAtom; |
| 315 |
|
| 316 |
int n_atoms; |
| 317 |
|
| 318 |
#ifdef IS_MPI |
| 319 |
n_atoms = mpiSim->getTotAtoms(); |
| 320 |
#else |
| 321 |
n_atoms = entry_plug->n_atoms; |
| 322 |
#endif // is_mpi |
| 323 |
|
| 324 |
|
| 325 |
// set the string tokenizer |
| 326 |
|
| 327 |
foo = strtok(readLine, " ,;\t"); |
| 328 |
|
| 329 |
// check the atom name to the current atom |
| 330 |
|
| 331 |
if( strcmp( foo, atoms[atomIndex]->getType() ) ){ |
| 332 |
sprintf( painCave.errMsg, |
| 333 |
"Initialize from file error. Atom %s at index %d " |
| 334 |
"in file %s does not" |
| 335 |
" match the BASS atom %s.\n", |
| 336 |
foo, atomIndex, c_in_name, atoms[atomIndex]->getType() ); |
| 337 |
return strdup( painCave.errMsg ); |
| 338 |
} |
| 339 |
|
| 340 |
// get the positions |
| 341 |
|
| 342 |
foo = strtok(NULL, " ,;\t"); |
| 343 |
if(foo == NULL){ |
| 344 |
sprintf( painCave.errMsg, |
| 345 |
"error in reading postition x from %s\n" |
| 346 |
"natoms = %d, index = %d\n", |
| 347 |
c_in_name, n_atoms, atomIndex ); |
| 348 |
return strdup( painCave.errMsg ); |
| 349 |
} |
| 350 |
rx = atof( foo ); |
| 351 |
|
| 352 |
foo = strtok(NULL, " ,;\t"); |
| 353 |
if(foo == NULL){ |
| 354 |
sprintf( painCave.errMsg, |
| 355 |
"error in reading postition y from %s\n" |
| 356 |
"natoms = %d, index = %d\n", |
| 357 |
c_in_name, n_atoms, atomIndex ); |
| 358 |
return strdup( painCave.errMsg ); |
| 359 |
} |
| 360 |
ry = atof( foo ); |
| 361 |
|
| 362 |
foo = strtok(NULL, " ,;\t"); |
| 363 |
if(foo == NULL){ |
| 364 |
sprintf( painCave.errMsg, |
| 365 |
"error in reading postition z from %s\n" |
| 366 |
"natoms = %d, index = %d\n", |
| 367 |
c_in_name, n_atoms, atomIndex ); |
| 368 |
return strdup( painCave.errMsg ); |
| 369 |
} |
| 370 |
rz = atof( foo ); |
| 371 |
|
| 372 |
|
| 373 |
// get the velocities |
| 374 |
|
| 375 |
foo = strtok(NULL, " ,;\t"); |
| 376 |
if(foo == NULL){ |
| 377 |
sprintf( painCave.errMsg, |
| 378 |
"error in reading velocity x from %s\n" |
| 379 |
"natoms = %d, index = %d\n", |
| 380 |
c_in_name, n_atoms, atomIndex ); |
| 381 |
return strdup( painCave.errMsg ); |
| 382 |
} |
| 383 |
vx = atof( foo ); |
| 384 |
|
| 385 |
foo = strtok(NULL, " ,;\t"); |
| 386 |
if(foo == NULL){ |
| 387 |
sprintf( painCave.errMsg, |
| 388 |
"error in reading velocity y from %s\n" |
| 389 |
"natoms = %d, index = %d\n", |
| 390 |
c_in_name, n_atoms, atomIndex ); |
| 391 |
return strdup( painCave.errMsg ); |
| 392 |
} |
| 393 |
vy = atof( foo ); |
| 394 |
|
| 395 |
foo = strtok(NULL, " ,;\t"); |
| 396 |
if(foo == NULL){ |
| 397 |
sprintf( painCave.errMsg, |
| 398 |
"error in reading velocity z from %s\n" |
| 399 |
"natoms = %d, index = %d\n", |
| 400 |
c_in_name, n_atoms, atomIndex ); |
| 401 |
return strdup( painCave.errMsg ); |
| 402 |
} |
| 403 |
vz = atof( foo ); |
| 404 |
|
| 405 |
|
| 406 |
// get the quaternions |
| 407 |
|
| 408 |
if( atoms[atomIndex]->isDirectional() ){ |
| 409 |
|
| 410 |
foo = strtok(NULL, " ,;\t"); |
| 411 |
if(foo == NULL){ |
| 412 |
sprintf(painCave.errMsg, |
| 413 |
"error in reading quaternion 0 from %s\n" |
| 414 |
"natoms = %d, index = %d\n", |
| 415 |
c_in_name, n_atoms, atomIndex ); |
| 416 |
return strdup( painCave.errMsg ); |
| 417 |
} |
| 418 |
q[0] = atof( foo ); |
| 419 |
|
| 420 |
foo = strtok(NULL, " ,;\t"); |
| 421 |
if(foo == NULL){ |
| 422 |
sprintf( painCave.errMsg, |
| 423 |
"error in reading quaternion 1 from %s\n" |
| 424 |
"natoms = %d, index = %d\n", |
| 425 |
c_in_name, n_atoms, atomIndex ); |
| 426 |
return strdup( painCave.errMsg ); |
| 427 |
} |
| 428 |
q[1] = atof( foo ); |
| 429 |
|
| 430 |
foo = strtok(NULL, " ,;\t"); |
| 431 |
if(foo == NULL){ |
| 432 |
sprintf( painCave.errMsg, |
| 433 |
"error in reading quaternion 2 from %s\n" |
| 434 |
"natoms = %d, index = %d\n", |
| 435 |
c_in_name, n_atoms, atomIndex ); |
| 436 |
return strdup( painCave.errMsg ); |
| 437 |
} |
| 438 |
q[2] = atof( foo ); |
| 439 |
|
| 440 |
foo = strtok(NULL, " ,;\t"); |
| 441 |
if(foo == NULL){ |
| 442 |
sprintf( painCave.errMsg, |
| 443 |
"error in reading quaternion 3 from %s\n" |
| 444 |
"natoms = %d, index = %d\n", |
| 445 |
c_in_name, n_atoms, atomIndex ); |
| 446 |
return strdup( painCave.errMsg ); |
| 447 |
} |
| 448 |
q[3] = atof( foo ); |
| 449 |
|
| 450 |
// get the angular velocities |
| 451 |
|
| 452 |
foo = strtok(NULL, " ,;\t"); |
| 453 |
if(foo == NULL){ |
| 454 |
sprintf( painCave.errMsg, |
| 455 |
"error in reading angular momentum jx from %s\n" |
| 456 |
"natoms = %d, index = %d\n", |
| 457 |
c_in_name, n_atoms, atomIndex ); |
| 458 |
return strdup( painCave.errMsg ); |
| 459 |
} |
| 460 |
jx = atof( foo ); |
| 461 |
|
| 462 |
foo = strtok(NULL, " ,;\t"); |
| 463 |
if(foo == NULL){ |
| 464 |
sprintf( painCave.errMsg, |
| 465 |
"error in reading angular momentum jy from %s\n" |
| 466 |
"natoms = %d, index = %d\n", |
| 467 |
c_in_name, n_atoms, atomIndex ); |
| 468 |
return strdup( painCave.errMsg ); |
| 469 |
} |
| 470 |
jy = atof(foo ); |
| 471 |
|
| 472 |
foo = strtok(NULL, " ,;\t"); |
| 473 |
if(foo == NULL){ |
| 474 |
sprintf( painCave.errMsg, |
| 475 |
"error in reading angular momentum jz from %s\n" |
| 476 |
"natoms = %d, index = %d\n", |
| 477 |
c_in_name, n_atoms, atomIndex ); |
| 478 |
return strdup( painCave.errMsg ); |
| 479 |
} |
| 480 |
jz = atof( foo ); |
| 481 |
|
| 482 |
dAtom = ( DirectionalAtom* )atoms[atomIndex]; |
| 483 |
|
| 484 |
// check that the quaternion vector is normalized |
| 485 |
|
| 486 |
qSqr = (q[0] * q[0]) + (q[1] * q[1]) + (q[2] * q[2]) + (q[3] * q[3]); |
| 487 |
|
| 488 |
qLength = sqrt( qSqr ); |
| 489 |
q[0] = q[0] / qLength; |
| 490 |
q[1] = q[1] / qLength; |
| 491 |
q[2] = q[2] / qLength; |
| 492 |
q[3] = q[3] / qLength; |
| 493 |
|
| 494 |
dAtom->setQ( q ); |
| 495 |
|
| 496 |
// add the angular velocities |
| 497 |
|
| 498 |
dAtom->setJx( jx ); |
| 499 |
dAtom->setJy( jy ); |
| 500 |
dAtom->setJz( jz ); |
| 501 |
} |
| 502 |
|
| 503 |
// add the positions and velocities to the atom |
| 504 |
|
| 505 |
atoms[atomIndex]->setX( rx ); |
| 506 |
atoms[atomIndex]->setY( ry ); |
| 507 |
atoms[atomIndex]->setZ( rz ); |
| 508 |
|
| 509 |
atoms[atomIndex]->set_vx( vx ); |
| 510 |
atoms[atomIndex]->set_vy( vy ); |
| 511 |
atoms[atomIndex]->set_vz( vz ); |
| 512 |
|
| 513 |
return NULL; |
| 514 |
} |