--- trunk/mdtools/md_code/InitializeFromFile.cpp 2002/10/31 21:20:49 162 +++ trunk/mdtools/md_code/InitializeFromFile.cpp 2002/12/12 21:21:59 206 @@ -13,6 +13,9 @@ InitializeFromFile :: InitializeFromFile( char *in_nam InitializeFromFile :: InitializeFromFile( char *in_name ){ +#ifdef IS_MPI + if (worldRank == 0) { +#endif c_in_file = fopen(in_name, "r"); if(c_in_file == NULL){ @@ -23,11 +26,18 @@ InitializeFromFile :: InitializeFromFile( char *in_nam } strcpy( c_in_name, in_name); +#ifdef IS_MPI + } + strcpy( checkPointMsg, "Infile opened for reading successfully." ); + MPIcheckPoint(); +#endif return; } InitializeFromFile :: ~InitializeFromFile( ){ - +#ifdef IS_MPI + if (worldRank == 0) { +#endif int error; error = fclose( c_in_file ); if( error ){ @@ -35,16 +45,26 @@ InitializeFromFile :: ~InitializeFromFile( ){ "Error closing %s\n", c_in_name ); simError(); } +#ifdef IS_MPI + } + strcpy( checkPointMsg, "Infile closed successfully." ); + MPIcheckPoint(); +#endif + return; } -void InitializeFromFile :: read_xyz( SimInfo* entry_plug ){ +void InitializeFromFile :: read_xyz( SimInfo* the_entry_plug ){ int i; // loop counter int n_atoms; // the number of atoms char read_buffer[2000]; //the line buffer for reading +#ifdef IS_MPI + char send_buffer[2000]; +#endif + char *eof_test; // ptr to see when we reach the end of the file char *foo; // the pointer to the current string token @@ -54,6 +74,10 @@ void InitializeFromFile :: read_xyz( SimInfo* entry_pl double jx, jy, jz; // angular velocity placeholders; double qSqr, qLength; // needed to normalize the quaternion vector. + entry_plug = the_entry_plug + + +#ifndef IS_MPI eof_test = fgets(read_buffer, sizeof(read_buffer), c_in_file); if( eof_test == NULL ){ std::cerr << "error reading 1st line of" << c_in_name << "\n"; @@ -296,4 +320,261 @@ void InitializeFromFile :: read_xyz( SimInfo* entry_pl atoms[i]->set_vz( vz ); } + + + // MPI Section of code.......... +#else //IS_MPI + + + + + if (worldRank == 0) { + eof_test = fgets(read_buffer, sizeof(read_buffer), c_in_file); + if( eof_test == NULL ){ + sprintf( painCave.errMsg, + "Error reading 1st line of %d \n ",c_in_name); + painCave.isFatal = 1; + simError(); + } + + (void)sscanf(read_buffer, "%d", &n_atoms); + + Atom **atoms = entry_plug->atoms; + DirectionalAtom* dAtom; + + if( n_atoms != entry_plug->n_atoms ){ + sprintf( painCave.errMsg, + "Initialize from File error. %s n_atoms, %d, " + "does not match the BASS file's n_atoms, %d.\n", + c_in_name, n_atoms, entry_plug->n_atoms ); + painCave.isFatal = 1; + simError(); + } + + //read and toss the comment line + + eof_test = fgets(read_buffer, sizeof(read_buffer), c_in_file); + if(eof_test == NULL){ + sprintf( painCave.errMsg, + "error in reading commment in %s\n", c_in_name); + painCave.isFatal = 1; + simError(); + } + } + for( i=0; i < n_atoms; i++){ + + eof_test = fgets(read_buffer, sizeof(read_buffer), c_in_file); + if(eof_test == NULL){ + sprintf(painCave.errMsg, + "error in reading file %s\n" + "natoms = %d; index = %d\n" + "error reading the line from the file.\n", + c_in_name, n_atoms, i ); + painCave.isFatal = 1; + simError(); + } + + foo = strtok(read_buffer, " ,;\t"); + + // check the atom name to the current atom + + if( strcmp( foo, atoms[i]->getType() ) ){ + sprintf( painCave.errMsg, + "Initialize from file error. Atom %s at index %d " + "in file %s does not" + " match the BASS atom %s.\n", + foo, i, c_in_name, atoms[i]->getType() ); + painCave.isFatal = 1; + simError(); + } + + // get the positions + + foo = strtok(NULL, " ,;\t"); + if(foo == NULL){ + sprintf( painCave.errMsg, + "error in reading postition x from %s\n" + "natoms = %d, index = %d\n", + c_in_name, n_atoms, i ); + painCave.isFatal = 1; + simError(); + } + (void)sscanf( foo, "%lf", &rx ); + + foo = strtok(NULL, " ,;\t"); + if(foo == NULL){ + sprintf( painCave.errMsg, + "error in reading postition y from %s\n" + "natoms = %d, index = %d\n", + c_in_name, n_atoms, i ); + painCave.isFatal = 1; + simError(); + } + (void)sscanf( foo, "%lf", &ry ); + + foo = strtok(NULL, " ,;\t"); + if(foo == NULL){ + sprintf( painCave.errMsg, + "error in reading postition z from %s\n" + "natoms = %d, index = %d\n", + c_in_name, n_atoms, i ); + painCave.isFatal = 1; + simError(); + } + (void)sscanf( foo, "%lf", &rz ); + + // get the velocities + + foo = strtok(NULL, " ,;\t"); + if(foo == NULL){ + sprintf( painCave.errMsg, + "error in reading velocity x from %s\n" + "natoms = %d, index = %d\n", + c_in_name, n_atoms, i ); + painCave.isFatal = 1; + simError(); + } + (void)sscanf( foo, "%lf", &vx ); + + foo = strtok(NULL, " ,;\t"); + if(foo == NULL){ + sprintf( painCave.errMsg, + "error in reading velocity y from %s\n" + "natoms = %d, index = %d\n", + c_in_name, n_atoms, i ); + painCave.isFatal = 1; + simError(); + } + (void)sscanf( foo, "%lf", &vy ); + + foo = strtok(NULL, " ,;\t"); + if(foo == NULL){ + sprintf( painCave.errMsg, + "error in reading velocity z from %s\n" + "natoms = %d, index = %d\n", + c_in_name, n_atoms, i ); + painCave.isFatal = 1; + simError(); + } + (void)sscanf( foo, "%lf", &vz ); + + + // get the quaternions + + if( atoms[i]->isDirectional() ){ + + foo = strtok(NULL, " ,;\t"); + if(foo == NULL){ + sprintf(painCave.errMsg, + "error in reading quaternion 0 from %s\n" + "natoms = %d, index = %d\n", + c_in_name, n_atoms, i ); + painCave.isFatal = 1; + simError(); + } + (void)sscanf( foo, "%lf", &q[0] ); + + foo = strtok(NULL, " ,;\t"); + if(foo == NULL){ + sprintf( painCave.errMsg, + "error in reading quaternion 1 from %s\n" + "natoms = %d, index = %d\n", + c_in_name, n_atoms, i ); + painCave.isFatal = 1; + simError(); + } + (void)sscanf( foo, "%lf", &q[1] ); + + foo = strtok(NULL, " ,;\t"); + if(foo == NULL){ + sprintf( painCave.errMsg, + "error in reading quaternion 2 from %s\n" + "natoms = %d, index = %d\n", + c_in_name, n_atoms, i ); + painCave.isFatal = 1; + simError(); + } + (void)sscanf( foo, "%lf", &q[2] ); + + foo = strtok(NULL, " ,;\t"); + if(foo == NULL){ + sprintf( painCave.errMsg, + "error in reading quaternion 3 from %s\n" + "natoms = %d, index = %d\n", + c_in_name, n_atoms, i ); + painCave.isFatal = 1; + simError(); + } + (void)sscanf( foo, "%lf", &q[3] ); + + // get the angular velocities + + foo = strtok(NULL, " ,;\t"); + if(foo == NULL){ + sprintf( painCave.errMsg, + "error in reading angular momentum jx from %s\n" + "natoms = %d, index = %d\n", + c_in_name, n_atoms, i ); + painCave.isFatal = 1; + simError(); + } + (void)sscanf( foo, "%lf", &jx ); + + foo = strtok(NULL, " ,;\t"); + if(foo == NULL){ + sprintf( painCave.errMsg, + "error in reading angular momentum jy from %s\n" + "natoms = %d, index = %d\n", + c_in_name, n_atoms, i ); + painCave.isFatal = 1; + simError(); + } + (void)sscanf( foo, "%lf", &jy ); + + foo = strtok(NULL, " ,;\t"); + if(foo == NULL){ + sprintf( painCave.errMsg, + "error in reading angular momentum jz from %s\n" + "natoms = %d, index = %d\n", + c_in_name, n_atoms, i ); + painCave.isFatal = 1; + simError(); + } + (void)sscanf( foo, "%lf", &jz ); + + dAtom = ( DirectionalAtom* )atoms[i]; + + // check that the quaternion vector is normalized + + qSqr = (q[0] * q[0]) + (q[1] * q[1]) + (q[2] * q[2]) + (q[3] * q[3]); + + qLength = sqrt( qSqr ); + q[0] = q[0] / qLength; + q[1] = q[1] / qLength; + q[2] = q[2] / qLength; + q[3] = q[3] / qLength; + + dAtom->setQ( q ); + + // add the angular velocities + + dAtom->setJx( jx ); + dAtom->setJy( jy ); + dAtom->setJz( jz ); + } + + // add the positions and velocities to the atom + + atoms[i]->setX( rx ); + atoms[i]->setY( ry ); + atoms[i]->setZ( rz ); + + atoms[i]->set_vx( vx ); + atoms[i]->set_vy( vy ); + atoms[i]->set_vz( vz ); + + } +#endif } + +char* IntitializeFromFile::parseDumpLine(char* readLine);