--- trunk/mdtools/md_code/InitializeFromFile.cpp 2002/07/09 18:40:59 11 +++ trunk/mdtools/md_code/InitializeFromFile.cpp 2002/12/13 17:59:52 209 @@ -9,254 +9,400 @@ #include #include "ReadWrite.hpp" +#include "simError.h" InitializeFromFile :: InitializeFromFile( char *in_name ){ +#ifdef IS_MPI + if (worldRank == 0) { +#endif c_in_file = fopen(in_name, "r"); if(c_in_file == NULL){ - printf("Cannot open file: %s\n", in_name); - exit(8); + sprintf(painCave.errMsg, + "Cannot open file: %s\n", in_name); + painCave.isFatal = 1; + simError(); } 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 ){ - fprintf( stderr, "Error closing %s\n", c_in_name ); + sprintf( painCave.errMsg, + "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 + 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[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 + + +#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; if( n_atoms != entry_plug->n_atoms ){ - fprintf( stderr, + 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 ); - exit(8); + 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){ - printf("error in reading commment in %s\n", c_in_name); - exit(8); + 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){ - printf("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 ); - exit(8); + 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 + parseErr = parseDumpLine( read_buffer, i ); + if( parseErr != NULL ){ + strcpy( painCave.errMsg, parseErr ); + painCave.isFatal = 1; + simError(); + } + } - if( strcmp( foo, atoms[i]->getType() ) ){ - fprintf( stderr, - "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() ); - exit(8); + + // 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(); } - // get the positions - - foo = strtok(NULL, " ,;\t"); - if(foo == NULL){ - printf("error in reading postition x from %s\n" - "natoms = %d, index = %d\n", - c_in_name, n_atoms, i ); - exit(8); + n_atoms = atoi( read_buffer ); + + Atom **atoms = entry_plug->atoms; + DirectionalAtom* dAtom; + + 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", + c_in_name, n_atoms, entry_plug->n_atoms ); + painCave.isFatal = 1; + simError(); } - (void)sscanf( foo, "%lf", &rx ); - - foo = strtok(NULL, " ,;\t"); - if(foo == NULL){ - printf("error in reading postition y from %s\n" - "natoms = %d, index = %d\n", - c_in_name, n_atoms, i ); - exit(8); + + //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(); } - (void)sscanf( foo, "%lf", &ry ); - - foo = strtok(NULL, " ,;\t"); - if(foo == NULL){ - printf("error in reading postition z from %s\n" - "natoms = %d, index = %d\n", - c_in_name, n_atoms, i ); - exit(8); + } + + 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(); } - (void)sscanf( foo, "%lf", &rz ); - // get the velocities + parseErr = parseDumpLine( read_buffer, i ); + if( parseErr != NULL ){ + strcpy( painCave.errMsg, parseErr ); + painCave.isFatal = 1; + simError(); + } + } +#endif +} - foo = strtok(NULL, " ,;\t"); - if(foo == NULL){ - printf("error in reading velocity x from %s\n" - "natoms = %d, index = %d\n", - c_in_name, n_atoms, i ); - exit(8); - } - (void)sscanf( foo, "%lf", &vx ); + +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 ); + } - foo = strtok(NULL, " ,;\t"); - if(foo == NULL){ - printf("error in reading velocity y from %s\n" + // 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 ); - exit(8); - } - (void)sscanf( foo, "%lf", &vy ); - - foo = strtok(NULL, " ,;\t"); - if(foo == NULL){ - printf("error in reading velocity z from %s\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, i ); - exit(8); - } - (void)sscanf( foo, "%lf", &vz ); + 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 ); - // get the quaternions + 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 ); - if( atoms[i]->isDirectional() ){ + + // get the quaternions + + if( atoms[atomIndex]->isDirectional() ){ - foo = strtok(NULL, " ,;\t"); - if(foo == NULL){ - printf("error in reading quaternion 0 from %s\n" + 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 quaternion 1 from %s\n" "natoms = %d, index = %d\n", - c_in_name, n_atoms, i ); - exit(8); - } - (void)sscanf( foo, "%lf", &q[0] ); + c_in_name, n_atoms, atomIndex ); + return strdup( painCave.errMsg ); + } + q[1] = atof( foo ); - foo = strtok(NULL, " ,;\t"); - if(foo == NULL){ - printf("error in reading quaternion 1 from %s\n" + 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 ); - exit(8); - } - (void)sscanf( foo, "%lf", &q[1] ); + c_in_name, n_atoms, atomIndex ); + return strdup( painCave.errMsg ); + } + q[2] = atof( foo ); - foo = strtok(NULL, " ,;\t"); - if(foo == NULL){ - printf("error in reading quaternion 2 from %s\n" + 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 ); - exit(8); - } - (void)sscanf( foo, "%lf", &q[2] ); - - foo = strtok(NULL, " ,;\t"); - if(foo == NULL){ - printf("error in reading quaternion 3 from %s\n" - "natoms = %d, index = %d\n", - c_in_name, n_atoms, i ); - exit(8); - } - (void)sscanf( foo, "%lf", &q[3] ); + c_in_name, n_atoms, atomIndex ); + return strdup( painCave.errMsg ); + } + q[3] = atof( foo ); - // get the angular velocities + // get the angular velocities - foo = strtok(NULL, " ,;\t"); - if(foo == NULL){ - printf("error in reading angular momentum jx from %s\n" + 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 ); - exit(8); - } - (void)sscanf( foo, "%lf", &jx ); + c_in_name, n_atoms, atomIndex ); + return strdup( painCave.errMsg ); + } + jx = atof( foo ); - foo = strtok(NULL, " ,;\t"); - if(foo == NULL){ - printf("error in reading angular momentum jy from %s\n" + 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 ); - exit(8); - } - (void)sscanf( foo, "%lf", &jy ); + c_in_name, n_atoms, atomIndex ); + return strdup( painCave.errMsg ); + } + jy = atof(foo ); - foo = strtok(NULL, " ,;\t"); - if(foo == NULL){ - printf("error in reading angular momentum jz from %s\n" + 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 ); - exit(8); - } - (void)sscanf( foo, "%lf", &jz ); + c_in_name, n_atoms, atomIndex ); + return strdup( painCave.errMsg ); + } + jz = atof( foo ); - 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 ); - - } + atoms[atomIndex]->set_vx( vx ); + atoms[atomIndex]->set_vy( vy ); + atoms[atomIndex]->set_vz( vz ); + + return NULL; }