--- trunk/mdtools/md_code/InitializeFromFile.cpp 2002/12/13 17:59:48 208 +++ trunk/mdtools/md_code/InitializeFromFile.cpp 2002/12/13 17:59:52 209 @@ -59,20 +59,16 @@ void InitializeFromFile :: read_xyz( SimInfo* the_entr int i; // loop counter + const int BUFFERSIZE = 2000; // size of the read buffer int n_atoms; // the number of atoms - char read_buffer[2000]; //the line buffer for reading + char read_buffer[BUFFERSIZE]; //the line buffer for reading #ifdef IS_MPI - char send_buffer[2000]; + char send_buffer[BUFFERSIZE]; #endif char *eof_test; // ptr to see when we reach the end of the file - char *foo; // the pointer to the current string token + char *parseErr; - double rx, ry, rz; // position place holders - double vx, vy, vz; // velocity placeholders - double q[4]; // the quaternions - double jx, jy, jz; // angular velocity placeholders; - double qSqr, qLength; // needed to normalize the quaternion vector. entry_plug = the_entry_plug @@ -80,10 +76,14 @@ void InitializeFromFile :: read_xyz( SimInfo* the_entr #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"; + sprintf( painCave.errMsg, + "InitializeFromFile error: error reading 1st line of \"%s\"\n", + c_in_name ); + painCave.isFatal = 1; + simError(); } - (void)sscanf(read_buffer, "%d", &n_atoms); + n_atoms = atoi( read_buffer ); Atom **atoms = entry_plug->atoms; DirectionalAtom* dAtom; @@ -120,205 +120,13 @@ void InitializeFromFile :: read_xyz( SimInfo* the_entr 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() ); + parseErr = parseDumpLine( read_buffer, i ); + if( parseErr != NULL ){ + strcpy( painCave.errMsg, parseErr ); 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 ); - + } } @@ -337,12 +145,12 @@ void InitializeFromFile :: read_xyz( SimInfo* the_entr simError(); } - (void)sscanf(read_buffer, "%d", &n_atoms); + n_atoms = atoi( read_buffer ); Atom **atoms = entry_plug->atoms; DirectionalAtom* dAtom; - if( n_atoms != entry_plug->n_atoms ){ + if( n_atoms != entry_plug->mpiSim->getTotAtoms() ){ sprintf( painCave.errMsg, "Initialize from File error. %s n_atoms, %d, " "does not match the BASS file's n_atoms, %d.\n", @@ -361,6 +169,7 @@ void InitializeFromFile :: read_xyz( SimInfo* the_entr simError(); } } + for( i=0; i < n_atoms; i++){ eof_test = fgets(read_buffer, sizeof(read_buffer), c_in_file); @@ -373,208 +182,227 @@ void InitializeFromFile :: read_xyz( SimInfo* the_entr 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() ); + parseErr = parseDumpLine( read_buffer, i ); + if( parseErr != NULL ){ + strcpy( painCave.errMsg, parseErr ); painCave.isFatal = 1; simError(); - } + } + } +#endif +} + + +char* IntitializeFromFile::parseDumpLine(char* readLine, int atomIndex){ + + char *foo; // the pointer to the current string token + + double rx, ry, rz; // position place holders + double vx, vy, vz; // velocity placeholders + double q[4]; // the quaternions + double jx, jy, jz; // angular velocity placeholders; + double qSqr, qLength; // needed to normalize the quaternion vector. + + Atom **atoms = entry_plug->atoms; + DirectionalAtom* dAtom; + + int n_atoms; + +#ifdef IS_MPI + n_atoms = entry_plug->mpiSim->getTotAtoms(); +#else + n_atoms = entry_plug->n_atoms; +#endi // is_mpi + + + // set the string tokenizer + + foo = strtok(readLine, " ,;\t"); + + // check the atom name to the current atom + + if( strcmp( foo, atoms[atomIndex]->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, atomIndex, c_in_name, atoms[atomIndex]->getType() ); + return strdup( painCave.errMsg ); + } - // get the positions + // 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, atomIndex ); + return strdup( painCave.errMsg ); + } + rx = atof( foo ); + + 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, atomIndex ); + return strdup( painCave.errMsg ); + } + ry = atof( foo ); + + 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, atomIndex ); + return strdup( painCave.errMsg ); + } + rz = atof( foo ); + + // 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, atomIndex ); + return strdup( painCave.errMsg ); + } + vx = atof( foo ); + + 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, atomIndex ); + return strdup( painCave.errMsg ); + } + vy = atof( foo ); + + 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, atomIndex ); + return strdup( painCave.errMsg ); + } + vz = atof( foo ); + + + // get the quaternions + + if( atoms[atomIndex]->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, atomIndex ); + return strdup( painCave.errMsg ); + } + q[0] = atof( foo ); + + foo = strtok(NULL, " ,;\t"); + if(foo == NULL){ sprintf( painCave.errMsg, - "error in reading postition x from %s\n" + "error in reading quaternion 1 from %s\n" "natoms = %d, index = %d\n", - c_in_name, n_atoms, i ); - painCave.isFatal = 1; - simError(); + c_in_name, n_atoms, atomIndex ); + return strdup( painCave.errMsg ); } - (void)sscanf( foo, "%lf", &rx ); - + q[1] = atof( foo ); + foo = strtok(NULL, " ,;\t"); if(foo == NULL){ sprintf( painCave.errMsg, - "error in reading postition y from %s\n" + "error in reading quaternion 2 from %s\n" "natoms = %d, index = %d\n", - c_in_name, n_atoms, i ); - painCave.isFatal = 1; - simError(); + c_in_name, n_atoms, atomIndex ); + return strdup( painCave.errMsg ); } - (void)sscanf( foo, "%lf", &ry ); - + q[2] = atof( foo ); + foo = strtok(NULL, " ,;\t"); if(foo == NULL){ sprintf( painCave.errMsg, - "error in reading postition z from %s\n" + "error in reading quaternion 3 from %s\n" "natoms = %d, index = %d\n", - c_in_name, n_atoms, i ); - painCave.isFatal = 1; - simError(); + c_in_name, n_atoms, atomIndex ); + return strdup( painCave.errMsg ); } - (void)sscanf( foo, "%lf", &rz ); - - // get the velocities - + q[3] = atof( foo ); + + // get the angular velocities + foo = strtok(NULL, " ,;\t"); if(foo == NULL){ sprintf( painCave.errMsg, - "error in reading velocity x from %s\n" + "error in reading angular momentum jx from %s\n" "natoms = %d, index = %d\n", - c_in_name, n_atoms, i ); - painCave.isFatal = 1; - simError(); + c_in_name, n_atoms, atomIndex ); + return strdup( painCave.errMsg ); } - (void)sscanf( foo, "%lf", &vx ); - + jx = atof( foo ); + foo = strtok(NULL, " ,;\t"); if(foo == NULL){ sprintf( painCave.errMsg, - "error in reading velocity y from %s\n" + "error in reading angular momentum jy from %s\n" "natoms = %d, index = %d\n", - c_in_name, n_atoms, i ); - painCave.isFatal = 1; - simError(); + c_in_name, n_atoms, atomIndex ); + return strdup( painCave.errMsg ); } - (void)sscanf( foo, "%lf", &vy ); - + jy = atof(foo ); + foo = strtok(NULL, " ,;\t"); if(foo == NULL){ sprintf( painCave.errMsg, - "error in reading velocity z from %s\n" + "error in reading angular momentum jz from %s\n" "natoms = %d, index = %d\n", - c_in_name, n_atoms, i ); - painCave.isFatal = 1; - simError(); + c_in_name, n_atoms, atomIndex ); + return strdup( painCave.errMsg ); } - (void)sscanf( foo, "%lf", &vz ); - - - // get the quaternions - - if( atoms[i]->isDirectional() ){ + jz = atof( foo ); - 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]; + dAtom = ( DirectionalAtom* )atoms[atomIndex]; - // check that the quaternion vector is normalized + // check that the quaternion vector is normalized - qSqr = (q[0] * q[0]) + (q[1] * q[1]) + (q[2] * q[2]) + (q[3] * q[3]); + 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; + 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 ); + dAtom->setQ( q ); - // add the angular velocities + // add the angular velocities - dAtom->setJx( jx ); - dAtom->setJy( jy ); - dAtom->setJz( jz ); - } + dAtom->setJx( jx ); + dAtom->setJy( jy ); + dAtom->setJz( jz ); + } - // add the positions and velocities to the atom + // add the positions and velocities to the atom - atoms[i]->setX( rx ); - atoms[i]->setY( ry ); - atoms[i]->setZ( rz ); + atoms[atomIndex]->setX( rx ); + atoms[atomIndex]->setY( ry ); + atoms[atomIndex]->setZ( rz ); - atoms[i]->set_vx( vx ); - atoms[i]->set_vy( vy ); - atoms[i]->set_vz( vz ); - - } -#endif -} + atoms[atomIndex]->set_vx( vx ); + atoms[atomIndex]->set_vy( vy ); + atoms[atomIndex]->set_vz( vz ); -char* IntitializeFromFile::parseDumpLine(char* readLine); + return NULL; +}