--- branches/mmeineke/mdtools/md_code/InitializeFromFile.cpp 2002/07/09 18:40:59 10 +++ trunk/mdtools/md_code/InitializeFromFile.cpp 2003/01/30 20:03:37 254 @@ -9,254 +9,504 @@ #include #include "ReadWrite.hpp" +#include "simError.h" +#ifdef IS_MPI +#include "mpiSimulation.hpp" +#endif // is_mpi 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; + int procIndex; - 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); - } - - // 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); - } - (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); - } - (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); - } - (void)sscanf( foo, "%lf", &rz ); - - // get the velocities + // MPI Section of code.......... +#else //IS_MPI - 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); + int masterIndex; + int nodeAtomsStart; + int nodeAtomsEnd; + int mpiErr; + int sendError; + + MPI_Status istatus[MPI_STATUS_SIZE]; + + 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( foo, "%lf", &vx ); - foo = strtok(NULL, " ,;\t"); - if(foo == NULL){ - printf("error in reading velocity y from %s\n" - "natoms = %d, index = %d\n", - c_in_name, n_atoms, i ); - exit(8); - } - (void)sscanf( foo, "%lf", &vy ); + n_atoms = atoi( read_buffer ); - foo = strtok(NULL, " ,;\t"); - if(foo == NULL){ - printf("error in reading velocity z from %s\n" - "natoms = %d, index = %d\n", - c_in_name, n_atoms, i ); - exit(8); + Atom **atoms = entry_plug->atoms; + DirectionalAtom* dAtom; + + // Check to see that the number of atoms in the intial configuration file is the + // same as declared in simBass. + + if( n_atoms != 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", &vz ); + //read and toss the comment line - // get the quaternions + 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(); + } + + // Read Proc 0 share of the xyz file... + masterIndex = 0; + for( i=0; i <= mpiSim->getMyAtomEnd(); i++){ - if( atoms[i]->isDirectional() ){ - - foo = strtok(NULL, " ,;\t"); - if(foo == NULL){ - printf("error in reading quaternion 0 from %s\n" - "natoms = %d, index = %d\n", - c_in_name, n_atoms, i ); - exit(8); + 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", &q[0] ); - - foo = strtok(NULL, " ,;\t"); - if(foo == NULL){ - printf("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[1] ); - - foo = strtok(NULL, " ,;\t"); - if(foo == NULL){ - printf("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[2] ); + + parseErr = parseDumpLine( read_buffer, i ); + if( parseErr != NULL ){ + strcpy( painCave.errMsg, parseErr ); + painCave.isFatal = 1; + simError(); + } + masterIndex++; + } + } - 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] ); - - // get the angular velocities - - foo = strtok(NULL, " ,;\t"); - if(foo == NULL){ - printf("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 ); - - foo = strtok(NULL, " ,;\t"); - if(foo == NULL){ - printf("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 ); - - foo = strtok(NULL, " ,;\t"); - if(foo == NULL){ - printf("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 ); - - dAtom = ( DirectionalAtom* )atoms[i]; + sprintf(checkPointMsg, + "Node 0 has successfully read positions from input file."); + MPIcheckPoint(); - // check that the quaternion vector is normalized + for (procIndex = 1; procIndex < mpiSim->getNumberProcessors(); + procIndex++){ + if (worldRank == 0) { - 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 + mpiErr = MPI_Recv(&nodeAtomsStart,1,MPI_INT,procIndex,MPI_ANY_TAG,MPI_COMM_WORLD, + istatus); - dAtom->setJx( jx ); - dAtom->setJy( jy ); - dAtom->setJz( jz ); + mpiErr = MPI_Recv(&nodeAtomsEnd,1,MPI_INT,procIndex,MPI_ANY_TAG,MPI_COMM_WORLD, + istatus); + // Make sure where node 0 is reading from, matches where the receiving node + // expects it to be. + + if (masterIndex != nodeAtomsStart){ + sendError = 1; + mpiErr = MPI_Send(&sendError,1,MPI_INT,procIndex,MPI_ANY_TAG,MPI_COMM_WORLD); + sprintf(painCave.errMsg, + "Initialize from file error: atoms start index (%d) for " + "node %d not equal to master index (%d)",nodeAtomsStart,procIndex,masterIndex ); + painCave.isFatal = 1; + simError(); + } + sendError = 0; + mpiErr = MPI_Send(&sendError,1,MPI_INT,procIndex,MPI_ANY_TAG,MPI_COMM_WORLD); + + for ( i = nodeAtomsStart; i <= nodeAtomsEnd; i++){ + eof_test = fgets(read_buffer, sizeof(read_buffer), c_in_file); + if(eof_test == NULL){ + + sprintf(read_buffer,"ERROR"); + mpiErr = MPI_Send(read_buffer,BUFFERSIZE,MPI_CHAR,procIndex,MPI_ANY_TAG,MPI_COMM_WORLD); + + 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(); + } + + mpiErr = MPI_Send(read_buffer,BUFFERSIZE,MPI_CHAR,procIndex,MPI_ANY_TAG,MPI_COMM_WORLD); + mpiErr = MPI_Recv(&sendError,1,MPI_INT,procIndex,MPI_ANY_TAG,MPI_COMM_WORLD, + istatus); + if (sendError) MPIcheckPoint(); + + masterIndex++; + } } + + + else if(worldRank == procIndex){ + nodeAtomsStart = mpiSim->getMyAtomStart(); + nodeAtomsEnd = mpiSim->getMyAtomEnd(); + mpiErr = MPI_Send(&nodeAtomsStart,1,MPI_INT,0,MPI_ANY_TAG,MPI_COMM_WORLD); + mpiErr = MPI_Send(&nodeAtomsEnd,1,MPI_INT,0,MPI_ANY_TAG,MPI_COMM_WORLD); + + mpiErr = MPI_Recv(&sendError,1,MPI_INT,0,MPI_ANY_TAG,MPI_COMM_WORLD, + istatus); + if (sendError) MPIcheckPoint(); + + for ( i = 0; i < entry_plug->n_atoms; i++){ + + mpiErr = MPI_Recv(&read_buffer,BUFFERSIZE,MPI_CHAR,0,MPI_ANY_TAG,MPI_COMM_WORLD, + istatus); + + if(!strcmp(read_buffer, "ERROR")) MPIcheckPoint(); + + parseErr = parseDumpLine( read_buffer, i ); + if( parseErr != NULL ){ + sendError = 1; + mpiErr = MPI_Send(&sendError,1,MPI_INT,0,MPI_ANY_TAG,MPI_COMM_WORLD); + + + strcpy( painCave.errMsg, parseErr ); + painCave.isFatal = 1; + simError(); + } + sendError = 0; + mpiErr = MPI_Send(&sendError,1,MPI_INT,0,MPI_ANY_TAG,MPI_COMM_WORLD); + } + } + sprintf(checkPointMsg,"Node %d received initial configuration.",procIndex); + MPIcheckPoint(); + } + +#endif +} + + +char* InitializeFromFile::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 = mpiSim->getTotAtoms(); +#else + n_atoms = entry_plug->n_atoms; +#endif // 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 ); + } - // add the positions and velocities to the atom + // 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 ); - atoms[i]->setX( rx ); - atoms[i]->setY( ry ); - atoms[i]->setZ( rz ); + 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 ); - atoms[i]->set_vx( vx ); - atoms[i]->set_vy( vy ); - atoms[i]->set_vz( vz ); + 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 quaternion 1 from %s\n" + "natoms = %d, index = %d\n", + c_in_name, n_atoms, atomIndex ); + return strdup( painCave.errMsg ); + } + q[1] = atof( foo ); + + 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, atomIndex ); + return strdup( painCave.errMsg ); + } + q[2] = atof( foo ); + + 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, atomIndex ); + return strdup( painCave.errMsg ); + } + q[3] = atof( foo ); + + // 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, atomIndex ); + return strdup( painCave.errMsg ); + } + jx = atof( foo ); + + 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, atomIndex ); + return strdup( painCave.errMsg ); + } + jy = atof(foo ); + + 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, atomIndex ); + return strdup( painCave.errMsg ); + } + jz = atof( foo ); + + dAtom = ( DirectionalAtom* )atoms[atomIndex]; + + // 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[atomIndex]->setX( rx ); + atoms[atomIndex]->setY( ry ); + atoms[atomIndex]->setZ( rz ); + + atoms[atomIndex]->set_vx( vx ); + atoms[atomIndex]->set_vy( vy ); + atoms[atomIndex]->set_vz( vz ); + + return NULL; }