--- trunk/OOPSE/libmdtools/DumpReader.cpp 2003/07/22 20:05:30 646 +++ trunk/OOPSE/libmdtools/DumpReader.cpp 2003/07/29 16:32:37 656 @@ -19,42 +19,62 @@ #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: + public: + FilePos(){ myPos = NULL; } + FilePos( fpos_t* thePos ) { myPos = thePos; } + ~FilePos(){ if( myPos != NULL ) delete myPos; } + FilePos &operator=(fpos_t *thePos){ myPos = thePos; return *this; } + + void setPos( fpos_t *thePos ){ myPos = thePos; } + fpos_t *getPos( void ){ return myPos; } + private: + fpos_t *myPos }; + bool operator<(FilePos a, FilePos b){ + return (a.getPos())->__pos < (b.getPos())->__pos; } + + bool operator==(FilePos a, FilePos b){ + return (a.getPos())->__pos == (b.getPos())->__pos; } + vector frameStart; } using namespace dumpRead; -#endif // is_mpi DumpReader :: DumpReader( char *in_name ){ + + isScanned = false; + #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 +88,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 +103,109 @@ 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; + +#ifdef IS_MPI + if( worldRank == 0 ){ +#endif // is_mpi + + rewind( inFile ); + + currPos = new fpos_t; + fgetpos( inFile, currPos ); + fgets( readBuffer, sizeof( readBuffer ), in_file ); + lineNum++; + if( feof( in_file ) ){ + sprintf( painCave.errMsg, + "File \"%s\" ended unexpectedly at line %d\n", + inName, + lineNum ); + painCave.isFatal = 1; + simError(); + } + + while( !feof( in_file ) ){ + + frameStart.push_back( FilePos( currPos ) ); + i = atoi(readBuffer); + + fgets( readBuffer, sizeof( readBuffer ), in_file ); + lineNum++; + if( feof( in_file ) ){ + 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; jreadSet( 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 +220,21 @@ void DumpReader :: getFrame( SimInfo* the_simnfo, int int procIndex; double boxMat[9]; double theBoxMat3[3][3]; + + fpos_t *framePos; + +#ifndef IS_MPI + + framePos = startFrame[whichFrame].getPos(); - simnfo = the_simnfo; + -#ifndef IS_MPI - 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, "DumpReader error: error reading 1st line of \"%s\"\n", - c_in_name ); + inName ); painCave.isFatal = 1; simError(); } @@ -123,17 +248,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 +281,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 +318,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 +338,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(); } @@ -243,13 +368,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(); } @@ -362,7 +487,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 +506,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,7 +517,7 @@ 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 ); @@ -402,7 +527,7 @@ char* DumpReader::parseDumpLine(char* readLine, int gl 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 ); @@ -412,7 +537,7 @@ char* DumpReader::parseDumpLine(char* readLine, int gl 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 ); @@ -425,7 +550,7 @@ 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 ); @@ -435,7 +560,7 @@ char* DumpReader::parseDumpLine(char* readLine, int gl 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 ); @@ -445,7 +570,7 @@ char* DumpReader::parseDumpLine(char* readLine, int gl 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 ); @@ -460,7 +585,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 +595,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 +605,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 +615,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 +627,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 +637,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 +647,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 ); @@ -574,7 +699,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 +710,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 +719,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 +728,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 +739,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 +748,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 +757,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 +768,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 +777,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 +786,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 );