--- trunk/OOPSE-3.0/src/io/DumpReader.cpp 2004/09/24 04:16:43 1490 +++ branches/new_design/OOPSE-3.0/src/io/DumpReader.cpp 2004/11/15 18:02:15 1739 @@ -11,756 +11,551 @@ #include #include - -#include "ReadWrite.hpp" -#include "simError.h" - +#include "io/DumpReader.hpp" +#include "utils/simError.h" +#include "utils/MemoryUtils" #ifdef IS_MPI + #include -#include "mpiSimulation.hpp" #define TAKE_THIS_TAG_CHAR 0 #define TAKE_THIS_TAG_INT 1 + #endif // is_mpi -DumpReader :: DumpReader(const char *in_name ){ +namespace oopse { - isScanned = false; +DumpReader::DumpReader(SimInfo* info, const std::string& filename) + : info_(info), filename_(filename), isScanned_(false){ #ifdef IS_MPI - if (worldRank == 0) { + + if (worldRank == 0) { #endif - inFile = fopen(in_name, "r"); - if(inFile == NULL){ - sprintf(painCave.errMsg, - "Cannot open file: %s\n", in_name); - painCave.isFatal = 1; - simError(); - } - - inFileName = in_name; + inFile_ = fopen(filename_.c_str(), "r"); + + if (inFile_ == NULL) { + sprintf(painCave.errMsg, "Cannot open file: %s\n", in_name); + painCave.isFatal = 1; + simError(); + } + #ifdef IS_MPI - } - strcpy( checkPointMsg, "Dump file opened for reading successfully." ); - MPIcheckPoint(); + + } + + strcpy(checkPointMsg, "Dump file opened for reading successfully."); + MPIcheckPoint(); + #endif - return; + + return; } -DumpReader :: ~DumpReader( ){ +DumpReader::~DumpReader() { + #ifdef IS_MPI - if (worldRank == 0) { + + if (worldRank == 0) { #endif - vector::iterator i; - int error; - error = fclose( inFile ); - if( error ){ - sprintf( painCave.errMsg, - "Error closing %s\n", inFileName.c_str()); - simError(); - } + int error; + error = fclose(inFile_); - for(i = framePos.begin(); i != framePos.end(); ++i) - delete *i; - framePos.clear(); - + if (error) { + sprintf(painCave.errMsg, "Error closing %s\n", filename_.c_str()); + painCave.isFatal = 1; + simError(); + } + + MemoryUtils::deleteVectorOfPointer(framePos_); + #ifdef IS_MPI - } - strcpy( checkPointMsg, "Dump file closed successfully." ); - MPIcheckPoint(); + + } + + strcpy(checkPointMsg, "Dump file closed successfully."); + MPIcheckPoint(); + #endif - return; + return; } -int DumpReader::getNframes( void ){ +int DumpReader::getNFrames(void) { + if (!isScanned_) + scanFile(); - if( !isScanned ) - scanFile(); - return framePos.size(); + return framePos_.size(); } -void DumpReader::scanFile( void ){ - +void DumpReader::scanFile(void) { int i, j; int lineNum = 0; - char readBuffer[2000]; - fpos_t *currPos; + char readBuffer[maxBufferSize]; + fpos_t * currPos; #ifdef IS_MPI - if( worldRank == 0 ){ + + 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", - inFileName.c_str(), - lineNum ); - painCave.isFatal = 1; - simError(); - } - while( !feof( inFile ) ){ - - framePos.push_back(currPos); + rewind(inFile_); - i = atoi(readBuffer); - - fgets( readBuffer, sizeof( readBuffer ), inFile ); - lineNum++; - if( feof( inFile ) ){ - sprintf( painCave.errMsg, - "File \"%s\" ended unexpectedly at line %d\n", - inFileName.c_str(), - lineNum ); - painCave.isFatal = 1; - simError(); - } - - for(j=0; jreadSet( whichFrame ); -} + fgets(readBuffer, sizeof(readBuffer), inFile_); + lineNum++; + if (feof(inFile_)) { + sprintf(painCave.errMsg, + "File \"%s\" ended unexpectedly at line %d\n", + filename_.c_str(), + lineNum); + painCave.isFatal = 1; + simError(); + } + for(j = 0; j < i; j++) { + fgets(readBuffer, sizeof(readBuffer), inFile_); + lineNum++; -void DumpReader :: readSet( int whichFrame ){ + if (feof(inFile_)) { + sprintf(painCave.errMsg, + "File \"%s\" ended unexpectedly at line %d," + " with atom %d\n", filename_.c_str(), + lineNum, + j); - int i; - unsigned int j; + painCave.isFatal = 1; + simError(); + } + } -#ifdef IS_MPI - int done, which_node, which_atom; // loop counter -#endif //is_mpi + currPos = new fpos_t; + fgetpos(inFile_, currPos); + fgets(readBuffer, sizeof(readBuffer), inFile_); + lineNum++; + } - const int BUFFERSIZE = 2000; // size of the read buffer - int nTotObjs; // the number of atoms - char read_buffer[BUFFERSIZE]; //the line buffer for reading + delete currPos; + rewind(inFile_); - char *eof_test; // ptr to see when we reach the end of the file - char *parseErr; + isScanned = true; - vector integrableObjects; +#ifdef IS_MPI + } -#ifndef IS_MPI + strcpy(checkPointMsg, "Successfully scanned DumpFile\n"); + MPIcheckPoint(); - fsetpos(inFile, framePos[whichFrame]); - 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", - inFileName.c_str() ); - painCave.isFatal = 1; - simError(); - } +#endif // is_mpi - nTotObjs = atoi( read_buffer ); +} - if( nTotObjs != simnfo->getTotIntegrableObjects() ){ - sprintf( painCave.errMsg, - "DumpReader error. %s nIntegrable, %d, " - "does not match the meta-data file's nIntegrable, %d.\n", - inFileName.c_str(), nTotObjs, simnfo->getTotIntegrableObjects()); - painCave.isFatal = 1; - simError(); - } +void DumpReader::readFrame(int whichFrame) { + readSet(whichFrame); +} - //read the box mat from the comment line +void DumpReader::readSet(int whichFrame) { + int i; + int nTotObjs; // the number of atoms + char read_buffer[maxBufferSize]; //the line buffer for reading + char * eof_test; // ptr to see when we reach the end of the file - eof_test = fgets(read_buffer, sizeof(read_buffer), inFile); - if(eof_test == NULL){ - sprintf( painCave.errMsg, - "error in reading commment in %s\n", inFileName.c_str()); - painCave.isFatal = 1; - simError(); - } + Molecule* mol; + StuntDouble* integrableObject; + typename SimInfo::MoleculeIterator mi; + typename Molecule::IntegrableObjectIterator ii; - parseErr = parseCommentLine( read_buffer, simnfo); - if( parseErr != NULL ){ - strcpy( painCave.errMsg, parseErr ); - painCave.isFatal = 1; - simError(); - } +#ifndef IS_MPI - //parse dump lines + fsetpos(inFile_, framePos_[whichFrame]); + eof_test = fgets(read_buffer, sizeof(read_buffer), inFile_); - for( i=0; i < simnfo->n_mol; i++){ + if (eof_test == NULL) { + sprintf(painCave.errMsg, + "DumpReader error: error reading 1st line of \"%s\"\n", + filename_.c_str()); + painCave.isFatal = 1; + simError(); + } - integrableObjects = (simnfo->molecules[i]).getIntegrableObjects(); + nTotObjs = atoi(read_buffer); - for(j = 0; j < integrableObjects.size(); j++){ - - eof_test = fgets(read_buffer, sizeof(read_buffer), inFile); - if(eof_test == NULL){ + if (nTotObjs != info_->getNGlobalIntegrableObjects()) { sprintf(painCave.errMsg, - "error in reading file %s\n" - "natoms = %d; index = %d\n" - "error reading the line from the file.\n", - inFileName.c_str(), nTotObjs, i ); + "DumpReader error. %s nIntegrable, %d, " + "does not match the meta-data file's nIntegrable, %d.\n", + filename_.c_str(), + nTotObjs, + info_->getNIntegrableObjects()); + painCave.isFatal = 1; simError(); - } - - parseErr = parseDumpLine( read_buffer, integrableObjects[j]); - if( parseErr != NULL ){ - strcpy( painCave.errMsg, parseErr ); - painCave.isFatal = 1; - simError(); - } } - } - // MPI Section of code.......... -#else //IS_MPI + //read the box mat from the comment line - // first thing first, suspend fatalities. - painCave.isEventLoop = 1; + eof_test = fgets(read_buffer, sizeof(read_buffer), inFile_); - int myStatus; // 1 = wakeup & success; 0 = error; -1 = AllDone - int haveError; - - MPI_Status istatus; - int *MolToProcMap = mpiSim->getMolToProcMap(); - int localIndex; - int nCurObj; - int nitems; - - nTotObjs = simnfo->getTotIntegrableObjects(); - haveError = 0; - if (worldRank == 0) { - fsetpos(inFile, framePos[whichFrame]); - - eof_test = fgets(read_buffer, sizeof(read_buffer), inFile); - if( eof_test == NULL ){ - sprintf( painCave.errMsg, - "Error reading 1st line of %s \n ",inFileName.c_str()); - haveError = 1; - simError(); + if (eof_test == NULL) { + sprintf(painCave.errMsg, "error in reading commment in %s\n", + filename_.c_str()); + painCave.isFatal = 1; + simError(); } - nitems = atoi( read_buffer ); + parseCommentLine(read_buffer, info_->getSnapshotManager()->getCurrentSnapshot(), info_->useInitState()); - // Check to see that the number of integrable objects in the - // intial configuration file is the same as derived from the - // meta-data file. + //parse dump lines - if( nTotObjs != nitems){ - sprintf( painCave.errMsg, - "DumpReader Error. %s nIntegrable, %d, " - "does not match the meta-data file's nIntegrable, %d.\n", - inFileName.c_str(), nTotObjs, simnfo->getTotIntegrableObjects()); - haveError= 1; - simError(); - } + for (mol = info_->beginMolecule(mi); mol != NULL; mol = info_->nextMolecule(mi)) { - //read the boxMat from the comment line + for (integrableObject = mol->beginIntegrableObject(i); integrableObject != NULL; + integrableObject = mol->nextIntegrableObject(i)) { - eof_test = fgets(read_buffer, sizeof(read_buffer), inFile); - if(eof_test == NULL){ - sprintf( painCave.errMsg, - "error in reading commment in %s\n", inFileName.c_str()); - haveError = 1; - simError(); - } + eof_test = fgets(read_buffer, sizeof(read_buffer), inFile_); - //Every single processor will parse the comment line by itself - //By using this way, we might lose some efficiency, but if we want to add - //more parameters into comment line, we only need to modify function - //parseCommentLine + 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", + filename_.c_str(), + nTotObjs, + i); - MPI_Bcast(read_buffer, BUFFERSIZE, MPI_CHAR, 0, MPI_COMM_WORLD); + painCave.isFatal = 1; + simError(); + } - parseErr = parseCommentLine( read_buffer, simnfo); - - if( parseErr != NULL ){ - strcpy( painCave.errMsg, parseErr ); - haveError = 1; - simError(); + parseDumpLine(read_buffer, integrableObjects[j]); + + } } - for (i=0 ; i < mpiSim->getNMolGlobal(); i++) { - which_node = MolToProcMap[i]; - if(which_node == 0){ - //molecules belong to master node + // MPI Section of code.......... - localIndex = mpiSim->getGlobalToLocalMol(i); +#else //IS_MPI - if(localIndex == -1) { - strcpy(painCave.errMsg, "Molecule not found on node 0!"); - haveError = 1; - simError(); - } + // first thing first, suspend fatalities. + int masterNode = 0; + int nCurObj; + painCave.isEventLoop = 1; - integrableObjects = (simnfo->molecules[localIndex]).getIntegrableObjects(); - for(j=0; j < integrableObjects.size(); j++){ - - 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", - inFileName.c_str(), nTotObjs, i ); - haveError= 1; - simError(); - } - - if(haveError) nodeZeroError(); + int myStatus; // 1 = wakeup & success; 0 = error; -1 = AllDone + int haveError; - parseDumpLine(read_buffer, integrableObjects[j]); - - } + MPI_Status istatus; + int nitems; + nTotObjs = info_->getNGlobalIntegrableObjects(); + haveError = 0; - } - else{ - //molecule belongs to slave nodes + if (worldRank == masterNode) { + fsetpos(inFile_, framePos_[whichFrame]); - MPI_Recv(&nCurObj, 1, MPI_INT, which_node, - TAKE_THIS_TAG_INT, MPI_COMM_WORLD, &istatus); - - for(j=0; j < nCurObj; j++){ - - 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", - inFileName.c_str(), nTotObjs, i ); - haveError= 1; - simError(); - } - - if(haveError) nodeZeroError(); + eof_test = fgets(read_buffer, sizeof(read_buffer), inFile_); - MPI_Send(read_buffer, BUFFERSIZE, MPI_CHAR, which_node, - TAKE_THIS_TAG_CHAR, MPI_COMM_WORLD); - - } + if (eof_test == NULL) { + sprintf(painCave.errMsg, "Error reading 1st line of %s \n ", + filename_.c_str()); + painCave.isFatal = 1; + simError(); + } - } - - } - - } - else{ - //actions taken at slave nodes - MPI_Bcast(read_buffer, BUFFERSIZE, MPI_CHAR, 0, MPI_COMM_WORLD); + nitems = atoi(read_buffer); - parseErr = parseCommentLine( read_buffer, simnfo); + // Check to see that the number of integrable objects in the + // intial configuration file is the same as derived from the + // meta-data file. - if( parseErr != NULL ){ - strcpy( painCave.errMsg, parseErr ); - haveError = 1; - simError(); - } - - for (i=0 ; i < mpiSim->getNMolGlobal(); i++) { - which_node = MolToProcMap[i]; - - if(which_node == worldRank){ - //molecule with global index i belongs to this processor - - localIndex = mpiSim->getGlobalToLocalMol(i); + if (nTotObjs != nitems) { + sprintf(painCave.errMsg, + "DumpReader Error. %s nIntegrable, %d, " + "does not match the meta-data file's nIntegrable, %d.\n", + filename_.c_str(), + nTotObjs, + info_->getTotIntegrableObjects()); - if(localIndex == -1) { - sprintf(painCave.errMsg, "Molecule not found on node %d\n", worldRank); - haveError = 1; - simError(); + painCave.isFatal = 1; + simError(); } - integrableObjects = (simnfo->molecules[localIndex]).getIntegrableObjects(); + //read the boxMat from the comment line - nCurObj = integrableObjects.size(); - - MPI_Send(&nCurObj, 1, MPI_INT, 0, - TAKE_THIS_TAG_INT, MPI_COMM_WORLD); + eof_test = fgets(read_buffer, sizeof(read_buffer), inFile_); - for(j = 0; j < integrableObjects.size(); j++){ + if (eof_test == NULL) { + sprintf(painCave.errMsg, "error in reading commment in %s\n", + filename_.c_str()); + painCave.isFatal = 1; + simError(); + } - MPI_Recv(read_buffer, BUFFERSIZE, MPI_CHAR, 0, - TAKE_THIS_TAG_CHAR, MPI_COMM_WORLD, &istatus); + //Every single processor will parse the comment line by itself + //By using this way, we might lose some efficiency, but if we want to add + //more parameters into comment line, we only need to modify function + //parseCommentLine - parseErr = parseDumpLine(read_buffer, integrableObjects[j]); + MPI_Bcast(read_buffer, maxBufferSize, MPI_CHAR, masterNode, MPI_COMM_WORLD); + parseCommentLine(read_buffer, info_->getSnapshotManager()->getCurrentSnapshot(), info_->useInitState()); - if( parseErr != NULL ){ - strcpy( painCave.errMsg, parseErr ); - simError(); - } + for(i = 0; i < info_->getNGlobalMolecules(); i++) { + which_node = info_->getMolToProc(i); - } - - } - - } + if (which_node == masterNode) { + //molecules belong to master node + + mol = info_->getMoleculeByGlobalIndex(i); - } + if (mol == NULL) { + strcpy(painCave.errMsg, "Molecule not found on node %d!", worldRank); + painCave.isFatal = 1; + simError(); + } -#endif -} + for (integrableObject = mol->beginIntegrableObject(ii); integrableObject != NULL; + integrableObject = mol->nextIntegrableObject(ii)){ + + eof_test = fgets(read_buffer, sizeof(read_buffer), inFile_); -char* DumpReader::parseDumpLine(char* readLine, StuntDouble* sd){ + 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", + filename_.c_str(), + nTotObjs, + i); - char *foo; // the pointer to the current string token + painCave.isFatal = 1; + simError(); + } - double pos[3]; // position place holders - double vel[3]; // velocity placeholders - double q[4]; // the quaternions - double ji[3]; // angular velocity placeholders; - double qSqr, qLength; // needed to normalize the quaternion vector. + parseDumpLine(read_buffer, integrableObject); + } + } else { + //molecule belongs to slave nodes + MPI_Recv(&nCurObj, 1, MPI_INT, which_node, TAKE_THIS_TAG_INT, + MPI_COMM_WORLD, &istatus); - // set the string tokenizer + for(int j = 0; j < nCurObj; j++) { + eof_test = fgets(read_buffer, sizeof(read_buffer), inFile_); - foo = strtok(readLine, " ,;\t"); + 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", + filename_.c_str(), + nTotObjs, + i); - // check the atom name to the current atom + painCave.isFatal = 1; + simError(); + } + + MPI_Send(read_buffer, maxBufferSize, MPI_CHAR, which_node, + TAKE_THIS_TAG_CHAR, MPI_COMM_WORLD); + } + } + } + } else { + //actions taken at slave nodes + MPI_Bcast(read_buffer, maxBufferSize, MPI_CHAR, masterNode, MPI_COMM_WORLD); - if( strcmp( foo, sd->getType() ) ){ - sprintf( painCave.errMsg, - "DumpReader error. Does not" - " match the meta-data atom %s.\n", - sd->getType() ); - return strdup( painCave.errMsg ); - } + /**@todo*/ + parseCommentLine(read_buffer, info_->getSnapshotManager()->getCurrentSnapshot(), info_->useInitState()); - // get the positions + for(i = 0; i < info_->getNGlobalMolecules(); i++) { + which_node = info_->getMolToProc(i); - foo = strtok(NULL, " ,;\t"); - if(foo == NULL){ - sprintf( painCave.errMsg, - "error in reading postition x from %s\n", - inFileName.c_str()); - return strdup( painCave.errMsg ); - } - pos[0] = atof( foo ); + if (which_node == worldRank) { + //molecule with global index i belongs to this processor + + mol = info_->getMoleculeByGlobalIndex(i); + if (mol == NULL) { + strcpy(painCave.errMsg, "Molecule not found on node %d!", worldRank); + painCave.isFatal = 1; + simError(); + } + + nCurObj = mol->getNIntegrableObjects(); - foo = strtok(NULL, " ,;\t"); - if(foo == NULL){ - sprintf( painCave.errMsg, - "error in reading postition y from %s\n", - inFileName.c_str()); - return strdup( painCave.errMsg ); - } - pos[1] = atof( foo ); + MPI_Send(&nCurObj, 1, MPI_INT, masterNode, TAKE_THIS_TAG_INT, + MPI_COMM_WORLD); - foo = strtok(NULL, " ,;\t"); - if(foo == NULL){ - sprintf( painCave.errMsg, - "error in reading postition z from %s\n", - inFileName.c_str()); - return strdup( painCave.errMsg ); - } - pos[2] = atof( foo ); + for (integrableObject = mol->beginIntegrableObject(ii); integrableObject != NULL; + integrableObject = mol->nextIntegrableObject(ii)){ + + MPI_Recv(read_buffer, maxBufferSize, MPI_CHAR, masterNode, + TAKE_THIS_TAG_CHAR, MPI_COMM_WORLD, &istatus); + parseDumpLine(read_buffer, integrableObject); + } + + } + + } + + } - // get the velocities +#endif - foo = strtok(NULL, " ,;\t"); - if(foo == NULL){ - sprintf( painCave.errMsg, - "error in reading velocity x from %s\n", - inFileName.c_str() ); - return strdup( painCave.errMsg ); - } - vel[0] = atof( foo ); +} - foo = strtok(NULL, " ,;\t"); - if(foo == NULL){ - sprintf( painCave.errMsg, - "error in reading velocity x from %s\n", - inFileName.c_str() ); - return strdup( painCave.errMsg ); - } - vel[1] = atof( foo ); +void DumpReader::parseDumpLine(char *line, StuntDouble *integrableObject) { - foo = strtok(NULL, " ,;\t"); - if(foo == NULL){ - sprintf( painCave.errMsg, - "error in reading velocity x from %s\n", - inFileName.c_str() ); - return strdup( painCave.errMsg ); - } - vel[2] = atof( foo ); + Vector3d pos; // position place holders + Vector3d vel; // velocity placeholders + Quat4d q; // the quaternions + Vector3d ji; // angular velocity placeholders; + StringTokenizer tokenizer(line); + int nTokens; + + nTokens = tokenizer.countTokens(); + if (nTokens < ) { - // add the positions and velocities to the atom - sd->setPos( pos ); - sd->setVel( vel ); + } - if (!sd->isDirectional()) - return NULL; + std::string name = tokenizer.nextToken(); - // get the quaternions + if (name != integrableObject->getType()) { + + } - if( sd->isDirectional() ){ + pos[0] = tokenizer.nextTokenAsFloat(); + pos[1] = tokenizer.nextTokenAsFloat(); + pos[2] = tokenizer.nextTokenAsFloat(); + integrableObject->setPos(pos); + + vel[0] = tokenizer.nextTokenAsFloat(); + vel[1] = tokenizer.nextTokenAsFloat(); + vel[2] = tokenizer.nextTokenAsFloat(); + integrableObject->setVel(vel); - foo = strtok(NULL, " ,;\t"); - if(foo == NULL){ - sprintf( painCave.errMsg, - "error in reading velocity x from %s\n", - inFileName.c_str() ); - return strdup( painCave.errMsg ); - } - q[0] = atof( foo ); + if (integrableObject->isDirectional()) { + q[0] = tokenizer.nextTokenAsFloat(); + q[1] = tokenizer.nextTokenAsFloat(); + q[2] = tokenizer.nextTokenAsFloat(); + q[3] = tokenizer.nextTokenAsFloat(); - foo = strtok(NULL, " ,;\t"); - if(foo == NULL){ - sprintf( painCave.errMsg, - "error in reading velocity x from %s\n", - inFileName.c_str() ); - return strdup( painCave.errMsg ); + double qlen = q.length(); + if (qlen < oopse::epsilon) { //check quaternion is not equal to 0 + + sprintf(painCave.errMsg, + "initial quaternion error (q0^2 + q1^2 + q2^2 + q3^2 ~ 0).\n"); + painCave.isFatal = 1; + simError(); + + } else if (fabs(qlen - 1.0) > oopse::epsilon) { // check that the quaternion vector is normalized + sprintf(painCave.errMsg, + "initial quaternion error (q0^2 + q1^2 + q2^2 + q3^2 != 1.0).\n"); + painCave.isFatal = 0; + simError(); + + q.normalize(); + } + integrableObject->setQ(q); + + ji[0] = tokenizer.nextTokenAsFloat(); + ji[1] = tokenizer.nextTokenAsFloat(); + ji[2] = tokenizer.nextTokenAsFloat(); + integrableObject->setJ(ji); } - q[1] = atof( foo ); - foo = strtok(NULL, " ,;\t"); - if(foo == NULL){ - sprintf( painCave.errMsg, - "error in reading velocity x from %s\n", - inFileName.c_str() ); - return strdup( painCave.errMsg ); - } - q[2] = atof( foo ); +} - foo = strtok(NULL, " ,;\t"); - if(foo == NULL){ - sprintf( painCave.errMsg, - "error in reading velocity x from %s\n", - inFileName.c_str() ); - return strdup( painCave.errMsg ); - } - q[3] = atof( foo ); + +char *DumpReader::parseCommentLine(char* line, Snapshot* s, bool useInitXSstate) { + double currTime; + Mat3x3d hmat; + double chi; + double integralOfChiDt; + Mat3x3d eta; - // get the angular velocities + StringTokenizer tokenizer(line); + int nTokens; - foo = strtok(NULL, " ,;\t"); - if(foo == NULL){ - sprintf( painCave.errMsg, - "error in reading velocity x from %s\n", - inFileName.c_str() ); - return strdup( painCave.errMsg ); - } - ji[0] = atof( foo ); + nTokens = tokenizer.countTokens(); - foo = strtok(NULL, " ,;\t"); - if(foo == NULL){ - sprintf( painCave.errMsg, - "error in reading velocity x from %s\n", - inFileName.c_str() ); - return strdup( painCave.errMsg ); + //comment line should at least contain 10 tokens: current time(1 token) and h-matrix(9 tokens) + if (nTokens < 10) { + sprintf(painCave.errMsg, + "Not enough tokens in comment line: %s", line); + painCave.isFatal = 1; + simError(); } - ji[1] = atof(foo ); - foo = strtok(NULL, " ,;\t"); - if(foo == NULL){ - sprintf( painCave.errMsg, - "error in reading velocity x from %s\n", - inFileName.c_str() ); - return strdup( painCave.errMsg ); + //read current time + currTime = tokenizer.nextTokenAsFloat(); + if (useInitXSstate) { + s->setTime(currTime); } - ji[2] = atof( foo ); + + //read h-matrix + hmat(0, 0) = tokenizer.nextTokenAsFloat(); + hmat(0, 1) = tokenizer.nextTokenAsFloat(); + hmat(0, 2) = tokenizer.nextTokenAsFloat(); + hmat(1, 0) = tokenizer.nextTokenAsFloat(); + hmat(1, 1) = tokenizer.nextTokenAsFloat(); + hmat(1, 2) = tokenizer.nextTokenAsFloat(); + hmat(2, 0) = tokenizer.nextTokenAsFloat(); + hmat(2, 1) = tokenizer.nextTokenAsFloat(); + hmat(2, 2) = tokenizer.nextTokenAsFloat(); + s->setHmat(hmat); + + if (useInitXSstate) { + //read chi and integrablOfChidt, they should apprear in pair + if (tokenizer.countTokens() >= 2) { + chi = tokenizer.nextTokenAsFloat(); + integralOfChiDt = tokenizer.nextTokenAsFloat(); + s->setChi(chi); + s->setIntegralOfChiDt(integralOfChiDt); + } + + //read eta (eta is 3x3 matrix) + if (tokenizer.countTokens() >= 9) { + eta(0, 0) = tokenizer.nextTokenAsFloat(); + eta(0, 1) = tokenizer.nextTokenAsFloat(); + eta(0, 2) = tokenizer.nextTokenAsFloat(); + eta(1, 0) = tokenizer.nextTokenAsFloat(); + eta(1, 1) = tokenizer.nextTokenAsFloat(); + eta(1, 2) = tokenizer.nextTokenAsFloat(); + eta(2, 0) = tokenizer.nextTokenAsFloat(); + eta(2, 1) = tokenizer.nextTokenAsFloat(); + eta(2, 2) = tokenizer.nextTokenAsFloat(); - // check that the quaternion vector is normalized - - qSqr = (q[0] * q[0]) + (q[1] * q[1]) + (q[2] * q[2]) + (q[3] * q[3]); - - if (fabs(qSqr) < 1e-6) { - sprintf(painCave.errMsg, - "initial quaternion error (q0^2 + q1^2 + q2^2 + q3^2 ~ 0).\n"); - return strdup(painCave.errMsg); + s->setEta(eta); + } } - qLength = sqrt( qSqr ); - q[0] = q[0] / qLength; - q[1] = q[1] / qLength; - q[2] = q[2] / qLength; - q[3] = q[3] / qLength; - - // add quaternion and angular velocities - - sd->setQ( q ); - sd->setJ( ji ); - } - - - - return NULL; -} - - -char* DumpReader::parseCommentLine(char* readLine, SimInfo* entry_plug){ - - double currTime; - double boxMat[9]; - double theBoxMat3[3][3]; - double chi; - double integralOfChidt; - double eta[9]; - - char *foo; // the pointer to the current string token - - // set the string tokenizer - - foo = strtok(readLine, " ,;\t"); - // set the timeToken. - - if(foo == NULL){ - sprintf( painCave.errMsg, - "error in reading Time from %s\n", - inFileName.c_str() ); - return strdup( painCave.errMsg ); - } - - currTime = atof( foo ); - entry_plug->setTime( currTime ); - - //get H-Matrix - - for(int i = 0 ; i < 9; i++){ - foo = strtok(NULL, " ,;\t"); - if(foo == NULL){ - sprintf( painCave.errMsg, - "error in reading H[%d] from %s\n", i, inFileName.c_str() ); - return strdup( painCave.errMsg ); - } - boxMat[i] = atof( foo ); - } - - for(int i=0;i<3;i++) - for(int j=0;j<3;j++) theBoxMat3[i][j] = boxMat[3*j+i]; - - //set H-Matrix - entry_plug->setBoxM( theBoxMat3 ); - - //get chi and integralOfChidt, they should appear by pair - - if( entry_plug->useInitXSstate ){ - foo = strtok(NULL, " ,;\t\n"); - if(foo != NULL){ - chi = atof(foo); - - foo = strtok(NULL, " ,;\t\n"); - if(foo == NULL){ - sprintf( painCave.errMsg, - "chi and integralOfChidt should appear by pair in %s\n", inFileName.c_str() ); - return strdup( painCave.errMsg ); - } - integralOfChidt = atof( foo ); - - //push chi and integralOfChidt into SimInfo::properties which can be - //retrieved by integrator later - DoubleData* chiValue = new DoubleData(); - chiValue->setID(CHIVALUE_ID); - chiValue->setData(chi); - entry_plug->addProperty(chiValue); - - DoubleData* integralOfChidtValue = new DoubleData(); - integralOfChidtValue->setID(INTEGRALOFCHIDT_ID); - integralOfChidtValue->setData(integralOfChidt); - entry_plug->addProperty(integralOfChidtValue); - - } - else - return NULL; - //get eta - foo = strtok(NULL, " ,;\t\n"); - if(foo != NULL ){ - - for(int i = 0 ; i < 9; i++){ - - if(foo == NULL){ - sprintf( painCave.errMsg, - "error in reading eta[%d] from %s\n", i, inFileName.c_str() ); - return strdup( painCave.errMsg ); - } - eta[i] = atof( foo ); - foo = strtok(NULL, " ,;\t\n"); - } - } - else - return NULL; - - //push eta into SimInfo::properties which can be - //retrieved by integrator later - //entry_plug->setBoxM( theBoxMat3 ); - DoubleArrayData* etaValue = new DoubleArrayData(); - etaValue->setID(ETAVALUE_ID); - etaValue->setData(eta, 9); - entry_plug->addProperty(etaValue); - } - - return NULL; } -#ifdef IS_MPI -void DumpReader::nodeZeroError( void ){ - int j, myStatus; - - myStatus = 0; - for (j = 0; j < mpiSim->getNProcessors(); j++) { - MPI_Send( &myStatus, 1, MPI_INT, j, - TAKE_THIS_TAG_INT, MPI_COMM_WORLD); - } - - - MPI_Finalize(); - exit (0); - -} - -void DumpReader::anonymousNodeDie( void ){ - - MPI_Finalize(); - exit (0); -} -#endif +}//end namespace oopse