--- trunk/OOPSE/libmdtools/DumpReader.cpp 2003/07/22 20:05:30 646 +++ trunk/OOPSE/libmdtools/DumpReader.cpp 2003/10/28 16:03:37 829 @@ -1,16 +1,16 @@ #define _FILE_OFFSET_BITS 64 +#include +#include + #include -#include -#include +#include #include #include #include -#include -#include -#include + #include "ReadWrite.hpp" #include "simError.h" @@ -19,42 +19,38 @@ #include "mpiSimulation.hpp" #define TAKE_THIS_TAG_CHAR 0 #define TAKE_THIS_TAG_INT 1 +#endif // is_mpi namespace dumpRead{ + +#ifdef IS_MPI void nodeZeroError( void ); void anonymousNodeDie( void ); - +#endif // is_mpi - class FilePos{ - public: - - - private: - - - }; - - vector frameStart; } using namespace dumpRead; -#endif // is_mpi DumpReader :: DumpReader( char *in_name ){ + + isScanned = false; + headFP = new FilePos; + #ifdef IS_MPI if (worldRank == 0) { #endif - c_in_file = fopen(in_name, "r"); - if(c_in_file == NULL){ + inFile = fopen(in_name, "r"); + if(inFile == NULL){ sprintf(painCave.errMsg, "Cannot open file: %s\n", in_name); painCave.isFatal = 1; simError(); } - strcpy( c_in_name, in_name); + strcpy( inName, in_name); #ifdef IS_MPI } strcpy( checkPointMsg, "Dump file opened for reading successfully." ); @@ -68,10 +64,10 @@ DumpReader :: ~DumpReader( ){ if (worldRank == 0) { #endif int error; - error = fclose( c_in_file ); + error = fclose( inFile ); if( error ){ sprintf( painCave.errMsg, - "Error closing %s\n", c_in_name ); + "Error closing %s\n", inName ); simError(); } #ifdef IS_MPI @@ -83,10 +79,131 @@ DumpReader :: ~DumpReader( ){ return; } +int DumpReader::getNframes( void ){ + if( !isScanned ) scanFile(); + return nFrames; +} -void DumpReader :: getFrame( SimInfo* the_simnfo, int whichFrame){ +void DumpReader::scanFile( void ){ + int vectorSize; + int i, j, k; + int lineNum = 0; + char readBuffer[2000]; + char* foo; + fpos_t *currPos; + double time; + + FilePos *currFP; + + +#ifdef IS_MPI + if( worldRank == 0 ){ +#endif // is_mpi + + rewind( inFile ); + + currPos = new fpos_t; + fgetpos( inFile, currPos ); + fgets( readBuffer, sizeof( readBuffer ), inFile ); + lineNum++; + if( feof( inFile ) ){ + sprintf( painCave.errMsg, + "File \"%s\" ended unexpectedly at line %d\n", + inName, + lineNum ); + painCave.isFatal = 1; + simError(); + } + + nFrames = 0; + while( !feof( inFile ) ){ + + headFP->add( currPos ); + nFrames++; + + i = atoi(readBuffer); + + fgets( readBuffer, sizeof( readBuffer ), inFile ); + lineNum++; + if( feof( inFile ) ){ + sprintf( painCave.errMsg, + "File \"%s\" ended unexpectedly at line %d\n", + inName, + lineNum ); + painCave.isFatal = 1; + simError(); + } + +// if(outTime){ +// foo = strtok( readBuffer, " ,;\t" ); +// time = atof( foo ); +// } + + for(j=0; jgetNext(); + if( currFP == NULL ){ + sprintf( painCave.errMsg, + "DumpReader error: scanFile FilePos mismatch at " + "nFrames = %d\n", + i ); + painCave.isFatal = 1; + simError(); + } + + frameStart[i] = currFP; + } + + isScanned = true; + +#ifdef IS_MPI + } + strcpy( checkPointMsg, "Successfully scanned DumpFile\n" ); + MPIcheckPoint(); +#endif // is_mpi +} + +void DumpReader :: readFrame( SimInfo* the_simnfo, int whichFrame){ + + simnfo = the_simnfo; + + this->readSet( whichFrame ); +} + + + +void DumpReader :: readSet( int whichFrame ){ + int i, j, done, which_node, which_atom; // loop counter const int BUFFERSIZE = 2000; // size of the read buffer @@ -101,15 +218,22 @@ void DumpReader :: getFrame( SimInfo* the_simnfo, int int procIndex; double boxMat[9]; double theBoxMat3[3][3]; + double time; - simnfo = the_simnfo; - + fpos_t *framePos; + #ifndef IS_MPI - eof_test = fgets(read_buffer, sizeof(read_buffer), c_in_file); + + framePos = frameStart[whichFrame]->getPos(); + + + + + eof_test = fgets(read_buffer, sizeof(read_buffer), inFile); if( eof_test == NULL ){ sprintf( painCave.errMsg, "DumpReader error: error reading 1st line of \"%s\"\n", - c_in_name ); + inName ); painCave.isFatal = 1; simError(); } @@ -123,17 +247,17 @@ void DumpReader :: getFrame( SimInfo* the_simnfo, int sprintf( painCave.errMsg, "DumpReader error. %s n_atoms, %d, " "does not match the BASS file's n_atoms, %d.\n", - c_in_name, n_atoms, simnfo->n_atoms ); + inName, n_atoms, simnfo->n_atoms ); painCave.isFatal = 1; simError(); } //read the box mat from the comment line - eof_test = fgets(read_buffer, sizeof(read_buffer), c_in_file); + eof_test = fgets(read_buffer, sizeof(read_buffer), inFile); if(eof_test == NULL){ sprintf( painCave.errMsg, - "error in reading commment in %s\n", c_in_name); + "error in reading commment in %s\n", inName); painCave.isFatal = 1; simError(); } @@ -156,13 +280,13 @@ void DumpReader :: getFrame( SimInfo* the_simnfo, int for( i=0; i < n_atoms; i++){ - eof_test = fgets(read_buffer, sizeof(read_buffer), c_in_file); + eof_test = fgets(read_buffer, sizeof(read_buffer), inFile); 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 ); + inName, n_atoms, i ); painCave.isFatal = 1; simError(); } @@ -193,10 +317,10 @@ void DumpReader :: getFrame( SimInfo* the_simnfo, int haveError = 0; if (worldRank == 0) { - eof_test = fgets(read_buffer, sizeof(read_buffer), c_in_file); + eof_test = fgets(read_buffer, sizeof(read_buffer), inFile); if( eof_test == NULL ){ sprintf( painCave.errMsg, - "Error reading 1st line of %d \n ",c_in_name); + "Error reading 1st line of %d \n ",inName); haveError = 1; simError(); } @@ -213,17 +337,17 @@ void DumpReader :: getFrame( SimInfo* the_simnfo, int 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, simnfo->n_atoms ); + inName, n_atoms, simnfo->n_atoms ); haveError= 1; simError(); } //read the time and boxMat from the comment line - eof_test = fgets(read_buffer, sizeof(read_buffer), c_in_file); + eof_test = fgets(read_buffer, sizeof(read_buffer), inFile); if(eof_test == NULL){ sprintf( painCave.errMsg, - "error in reading commment in %s\n", c_in_name); + "error in reading commment in %s\n", inName); haveError = 1; simError(); } @@ -235,7 +359,7 @@ void DumpReader :: getFrame( SimInfo* the_simnfo, int simError(); } - MPI_Bcast(time, 1, MPI_DOUBLE, 0, MPI_COMM_WORLD ); + MPI_Bcast(&time, 1, MPI_DOUBLE, 0, MPI_COMM_WORLD ); MPI_Bcast(boxMat, 9, MPI_DOUBLE, 0, MPI_COMM_WORLD ); @@ -243,13 +367,13 @@ void DumpReader :: getFrame( SimInfo* the_simnfo, int for (i=0 ; i < mpiSim->getTotAtoms(); i++) { - eof_test = fgets(read_buffer, sizeof(read_buffer), c_in_file); + eof_test = fgets(read_buffer, sizeof(read_buffer), inFile); 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 ); + inName, n_atoms, i ); haveError= 1; simError(); } @@ -291,7 +415,7 @@ void DumpReader :: getFrame( SimInfo* the_simnfo, int } else { - MPI_Bcast(time, 1, MPI_DOUBLE, 0, MPI_COMM_WORLD); + MPI_Bcast(&time, 1, MPI_DOUBLE, 0, MPI_COMM_WORLD); MPI_Bcast(boxMat, 9, MPI_DOUBLE, 0, MPI_COMM_WORLD); done = 0; @@ -341,8 +465,8 @@ char* DumpReader::parseDumpLine(char* readLine, int gl char *foo; // the pointer to the current string token - double rx, ry, rz; // position place holders - double vx, vy, vz; // velocity placeholders + double pos[3]; // position place holders + double vel[3]; // velocity placeholders double q[4]; // the quaternions double jx, jy, jz; // angular velocity placeholders; double qSqr, qLength; // needed to normalize the quaternion vector. @@ -362,7 +486,7 @@ char* DumpReader::parseDumpLine(char* readLine, int gl sprintf( painCave.errMsg, "Initialize from file error. Atom at index %d " "in file %s does not exist on processor %d .\n", - globalIndex, c_in_name, mpiSim->getMyNode() ); + globalIndex, inName, mpiSim->getMyNode() ); return strdup( painCave.errMsg ); } #else @@ -381,7 +505,7 @@ char* DumpReader::parseDumpLine(char* readLine, int gl "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() ); + foo, atomIndex, inName, atoms[atomIndex]->getType() ); return strdup( painCave.errMsg ); } @@ -392,30 +516,30 @@ char* DumpReader::parseDumpLine(char* readLine, int gl sprintf( painCave.errMsg, "error in reading postition x from %s\n" "natoms = %d, index = %d\n", - c_in_name, n_atoms, atomIndex ); + inName, n_atoms, atomIndex ); return strdup( painCave.errMsg ); } - rx = atof( foo ); + pos[0] = 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 ); + inName, n_atoms, atomIndex ); return strdup( painCave.errMsg ); } - ry = atof( foo ); + pos[1] = 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 ); + inName, n_atoms, atomIndex ); return strdup( painCave.errMsg ); } - rz = atof( foo ); + pos[2] = atof( foo ); // get the velocities @@ -425,30 +549,30 @@ char* DumpReader::parseDumpLine(char* readLine, int gl sprintf( painCave.errMsg, "error in reading velocity x from %s\n" "natoms = %d, index = %d\n", - c_in_name, n_atoms, atomIndex ); + inName, n_atoms, atomIndex ); return strdup( painCave.errMsg ); } - vx = atof( foo ); + vel[0] = 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 ); + inName, n_atoms, atomIndex ); return strdup( painCave.errMsg ); } - vy = atof( foo ); + vel[1] = 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 ); + inName, n_atoms, atomIndex ); return strdup( painCave.errMsg ); } - vz = atof( foo ); + vel[2] = atof( foo ); // get the quaternions @@ -460,7 +584,7 @@ char* DumpReader::parseDumpLine(char* readLine, int gl sprintf(painCave.errMsg, "error in reading quaternion 0 from %s\n" "natoms = %d, index = %d\n", - c_in_name, n_atoms, atomIndex ); + inName, n_atoms, atomIndex ); return strdup( painCave.errMsg ); } q[0] = atof( foo ); @@ -470,7 +594,7 @@ char* DumpReader::parseDumpLine(char* readLine, int gl sprintf( painCave.errMsg, "error in reading quaternion 1 from %s\n" "natoms = %d, index = %d\n", - c_in_name, n_atoms, atomIndex ); + inName, n_atoms, atomIndex ); return strdup( painCave.errMsg ); } q[1] = atof( foo ); @@ -480,7 +604,7 @@ char* DumpReader::parseDumpLine(char* readLine, int gl sprintf( painCave.errMsg, "error in reading quaternion 2 from %s\n" "natoms = %d, index = %d\n", - c_in_name, n_atoms, atomIndex ); + inName, n_atoms, atomIndex ); return strdup( painCave.errMsg ); } q[2] = atof( foo ); @@ -490,7 +614,7 @@ char* DumpReader::parseDumpLine(char* readLine, int gl sprintf( painCave.errMsg, "error in reading quaternion 3 from %s\n" "natoms = %d, index = %d\n", - c_in_name, n_atoms, atomIndex ); + inName, n_atoms, atomIndex ); return strdup( painCave.errMsg ); } q[3] = atof( foo ); @@ -502,7 +626,7 @@ char* DumpReader::parseDumpLine(char* readLine, int gl sprintf( painCave.errMsg, "error in reading angular momentum jx from %s\n" "natoms = %d, index = %d\n", - c_in_name, n_atoms, atomIndex ); + inName, n_atoms, atomIndex ); return strdup( painCave.errMsg ); } jx = atof( foo ); @@ -512,7 +636,7 @@ char* DumpReader::parseDumpLine(char* readLine, int gl sprintf( painCave.errMsg, "error in reading angular momentum jy from %s\n" "natoms = %d, index = %d\n", - c_in_name, n_atoms, atomIndex ); + inName, n_atoms, atomIndex ); return strdup( painCave.errMsg ); } jy = atof(foo ); @@ -522,7 +646,7 @@ char* DumpReader::parseDumpLine(char* readLine, int gl sprintf( painCave.errMsg, "error in reading angular momentum jz from %s\n" "natoms = %d, index = %d\n", - c_in_name, n_atoms, atomIndex ); + inName, n_atoms, atomIndex ); return strdup( painCave.errMsg ); } jz = atof( foo ); @@ -549,20 +673,15 @@ char* DumpReader::parseDumpLine(char* readLine, int gl } // 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 ); + atoms[atomIndex]->setPos( pos ); + atoms[atomIndex]->setVel( vel ); + return NULL; } -char* DumpReader::parseCommentLine(char* readLine, double time, +char* DumpReader::parseCommentLine(char* readLine, double &time, double boxMat[9]){ char *foo; // the pointer to the current string token @@ -574,7 +693,7 @@ char* DumpReader::parseCommentLine(char* readLine, dou if(foo == NULL){ sprintf( painCave.errMsg, "error in reading time from %s\n", - c_in_name ); + inName ); return strdup( painCave.errMsg ); } time = atof( foo ); @@ -585,7 +704,7 @@ char* DumpReader::parseCommentLine(char* readLine, dou if(foo == NULL){ sprintf( painCave.errMsg, "error in reading Hx[0] from %s\n", - c_in_name ); + inName ); return strdup( painCave.errMsg ); } boxMat[0] = atof( foo ); @@ -594,7 +713,7 @@ char* DumpReader::parseCommentLine(char* readLine, dou if(foo == NULL){ sprintf( painCave.errMsg, "error in reading Hx[1] from %s\n", - c_in_name ); + inName ); return strdup( painCave.errMsg ); } boxMat[1] = atof( foo ); @@ -603,7 +722,7 @@ char* DumpReader::parseCommentLine(char* readLine, dou if(foo == NULL){ sprintf( painCave.errMsg, "error in reading Hx[2] from %s\n", - c_in_name ); + inName ); return strdup( painCave.errMsg ); } boxMat[2] = atof( foo ); @@ -614,7 +733,7 @@ char* DumpReader::parseCommentLine(char* readLine, dou if(foo == NULL){ sprintf( painCave.errMsg, "error in reading Hy[0] from %s\n", - c_in_name ); + inName ); return strdup( painCave.errMsg ); } boxMat[3] = atof( foo ); @@ -623,7 +742,7 @@ char* DumpReader::parseCommentLine(char* readLine, dou if(foo == NULL){ sprintf( painCave.errMsg, "error in reading Hy[1] from %s\n", - c_in_name ); + inName ); return strdup( painCave.errMsg ); } boxMat[4] = atof( foo ); @@ -632,7 +751,7 @@ char* DumpReader::parseCommentLine(char* readLine, dou if(foo == NULL){ sprintf( painCave.errMsg, "error in reading Hy[2] from %s\n", - c_in_name ); + inName ); return strdup( painCave.errMsg ); } boxMat[5] = atof( foo ); @@ -643,7 +762,7 @@ char* DumpReader::parseCommentLine(char* readLine, dou if(foo == NULL){ sprintf( painCave.errMsg, "error in reading Hz[0] from %s\n", - c_in_name ); + inName ); return strdup( painCave.errMsg ); } boxMat[6] = atof( foo ); @@ -652,7 +771,7 @@ char* DumpReader::parseCommentLine(char* readLine, dou if(foo == NULL){ sprintf( painCave.errMsg, "error in reading Hz[1] from %s\n", - c_in_name ); + inName ); return strdup( painCave.errMsg ); } boxMat[7] = atof( foo ); @@ -661,7 +780,7 @@ char* DumpReader::parseCommentLine(char* readLine, dou if(foo == NULL){ sprintf( painCave.errMsg, "error in reading Hz[2] from %s\n", - c_in_name ); + inName ); return strdup( painCave.errMsg ); } boxMat[8] = atof( foo ); @@ -674,7 +793,7 @@ char* DumpReader::parseCommentLine(char* readLine, dou // a couple of functions to let us escape the read loop -void initFile::nodeZeroError( void ){ +void dumpRead::nodeZeroError( void ){ int j, myStatus; myStatus = 0; @@ -689,7 +808,7 @@ void initFile::nodeZeroError( void ){ } -void initFile::anonymousNodeDie( void ){ +void dumpRead::anonymousNodeDie( void ){ MPI_Finalize(); exit (0);