--- trunk/OOPSE-2.0/src/io/DumpReader.cpp 2004/09/24 04:16:43 1490 +++ trunk/OOPSE-2.0/src/io/DumpReader.cpp 2005/03/10 15:10:24 2101 @@ -1,3 +1,44 @@ + /* + * Copyright (c) 2005 The University of Notre Dame. All Rights Reserved. + * + * The University of Notre Dame grants you ("Licensee") a + * non-exclusive, royalty free, license to use, modify and + * redistribute this software in source and binary code form, provided + * that the following conditions are met: + * + * 1. Acknowledgement of the program authors must be made in any + * publication of scientific results based in part on use of the + * program. An acceptable form of acknowledgement is citation of + * the article in which the program was described (Matthew + * A. Meineke, Charles F. Vardeman II, Teng Lin, Christopher + * J. Fennell and J. Daniel Gezelter, "OOPSE: An Object-Oriented + * Parallel Simulation Engine for Molecular Dynamics," + * J. Comput. Chem. 26, pp. 252-271 (2005)) + * + * 2. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 3. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the + * distribution. + * + * This software is provided "AS IS," without a warranty of any + * kind. All express or implied conditions, representations and + * warranties, including any implied warranty of merchantability, + * fitness for a particular purpose or non-infringement, are hereby + * excluded. The University of Notre Dame and its licensors shall not + * be liable for any damages suffered by licensee as a result of + * using, modifying or distributing the software or its + * derivatives. In no event will the University of Notre Dame or its + * licensors be liable for any lost revenue, profit or data, or for + * direct, indirect, special, consequential, incidental or punitive + * damages, however caused and regardless of the theory of liability, + * arising out of the use of or inability to use software, even if the + * University of Notre Dame has been advised of the possibility of + * such damages. + */ + #define _LARGEFILE_SOURCE64 #define _FILE_OFFSET_BITS 64 @@ -11,756 +52,590 @@ #include #include +#include "io/DumpReader.hpp" +#include "primitives/Molecule.hpp" +#include "utils/simError.h" +#include "utils/MemoryUtils.hpp" +#include "utils/StringTokenizer.hpp" -#include "ReadWrite.hpp" -#include "simError.h" - #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 ){ - - isScanned = false; - -#ifdef IS_MPI - 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(); - } +namespace oopse { - inFileName = in_name; + DumpReader::DumpReader(SimInfo* info, const std::string& filename) + : info_(info), filename_(filename), isScanned_(false), nframes_(0) { + #ifdef IS_MPI - } - strcpy( checkPointMsg, "Dump file opened for reading successfully." ); - MPIcheckPoint(); + + if (worldRank == 0) { #endif - return; -} - -DumpReader :: ~DumpReader( ){ + + inFile_ = fopen(filename_.c_str(), "r"); + + if (inFile_ == NULL) { + sprintf(painCave.errMsg, "DumpReader: Cannot open file: %s\n", filename_.c_str()); + painCave.isFatal = 1; + simError(); + } + #ifdef IS_MPI - if (worldRank == 0) { + + } + + strcpy(checkPointMsg, "Dump file opened for reading successfully."); + MPIcheckPoint(); + #endif - vector::iterator i; - - int error; - error = fclose( inFile ); - if( error ){ - sprintf( painCave.errMsg, - "Error closing %s\n", inFileName.c_str()); - simError(); + + return; } - - for(i = framePos.begin(); i != framePos.end(); ++i) - delete *i; - framePos.clear(); + DumpReader::~DumpReader() { + #ifdef IS_MPI - } - strcpy( checkPointMsg, "Dump file closed successfully." ); - MPIcheckPoint(); -#endif - - return; -} - -int DumpReader::getNframes( void ){ - - if( !isScanned ) - scanFile(); - return framePos.size(); -} - -void DumpReader::scanFile( void ){ - - int i, j; - int lineNum = 0; - char readBuffer[2000]; - 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 ), 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 ) ){ + if (worldRank == 0) { +#endif - framePos.push_back(currPos); - - i = atoi(readBuffer); + int error; + error = fclose(inFile_); - 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(); + if (error) { + sprintf(painCave.errMsg, "DumpReader Error: Error closing %s\n", filename_.c_str()); + painCave.isFatal = 1; + simError(); } - - for(j=0; jreadSet( whichFrame ); -} - - - -void DumpReader :: readSet( int whichFrame ){ - - int i; - unsigned int j; - -#ifdef IS_MPI - int done, which_node, which_atom; // loop counter -#endif //is_mpi - - const int BUFFERSIZE = 2000; // size of the read buffer - int nTotObjs; // the number of atoms - char read_buffer[BUFFERSIZE]; //the line buffer for reading - - char *eof_test; // ptr to see when we reach the end of the file - char *parseErr; - - vector integrableObjects; - - -#ifndef IS_MPI - - 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(); + + int DumpReader::getNFrames(void) { + + if (!isScanned_) + scanFile(); + + return nframes_; } - - 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(); - } - - //read the box mat from the comment line - - 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(); - } - - parseErr = parseCommentLine( read_buffer, simnfo); - if( parseErr != NULL ){ - strcpy( painCave.errMsg, parseErr ); - painCave.isFatal = 1; - simError(); - } - - //parse dump lines - - for( i=0; i < simnfo->n_mol; i++){ - - integrableObjects = (simnfo->molecules[i]).getIntegrableObjects(); - - for(j = 0; j < integrableObjects.size(); j++){ - - eof_test = fgets(read_buffer, sizeof(read_buffer), inFile); - if(eof_test == NULL){ + + void DumpReader::scanFile(void) { + int i, j; + int lineNum = 0; + char readBuffer[maxBufferSize]; + 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), inFile_); + lineNum++; + + if (feof(inFile_)) { 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: File \"%s\" ended unexpectedly at line %d\n", + filename_.c_str(), + lineNum); painCave.isFatal = 1; simError(); } - parseErr = parseDumpLine( read_buffer, integrableObjects[j]); - if( parseErr != NULL ){ - strcpy( painCave.errMsg, parseErr ); - painCave.isFatal = 1; - simError(); + while (!feof(inFile_)) { + framePos_.push_back(currPos); + + i = atoi(readBuffer); + + fgets(readBuffer, sizeof(readBuffer), inFile_); + lineNum++; + + if (feof(inFile_)) { + sprintf(painCave.errMsg, + "DumpReader Error: 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++; + + if (feof(inFile_)) { + sprintf(painCave.errMsg, + "DumpReader Error: File \"%s\" ended unexpectedly at line %d," + " with atom %d\n", filename_.c_str(), + lineNum, + j); + + painCave.isFatal = 1; + simError(); + } + } + + currPos = new fpos_t; + fgetpos(inFile_, currPos); + fgets(readBuffer, sizeof(readBuffer), inFile_); + lineNum++; } + + delete currPos; + rewind(inFile_); + + nframes_ = framePos_.size(); +#ifdef IS_MPI } + + MPI_Bcast(&nframes_, 1, MPI_INT, 0, MPI_COMM_WORLD); + + strcpy(checkPointMsg, "Successfully scanned DumpFile\n"); + MPIcheckPoint(); + +#endif // is_mpi + + isScanned_ = true; } - - // MPI Section of code.......... -#else //IS_MPI - - // first thing first, suspend fatalities. - painCave.isEventLoop = 1; - - 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(); + + void DumpReader::readFrame(int whichFrame) { + int storageLayout = info_->getSnapshotManager()->getStorageLayout(); + + if (storageLayout & DataStorage::dslPosition) { + needPos_ = true; + } else { + needPos_ = false; } - - nitems = atoi( read_buffer ); - - // 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( 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; + + if (storageLayout & DataStorage::dslVelocity) { + needVel_ = true; + } else { + needVel_ = false; + } + + if (storageLayout & DataStorage::dslAmat || storageLayout & DataStorage::dslElectroFrame) { + needQuaternion_ = true; + } else { + needQuaternion_ = false; + } + + if (storageLayout & DataStorage::dslAngularMomentum) { + needAngMom_ = true; + } else { + needAngMom_ = false; + } + + readSet(whichFrame); + } + + 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 + + Molecule* mol; + StuntDouble* integrableObject; + SimInfo::MoleculeIterator mi; + Molecule::IntegrableObjectIterator ii; + +#ifndef IS_MPI + + 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", + filename_.c_str()); + painCave.isFatal = 1; simError(); } - - //read the boxMat from the comment line - - 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; + + nTotObjs = atoi(read_buffer); + + if (nTotObjs != info_->getNGlobalIntegrableObjects()) { + sprintf(painCave.errMsg, + "DumpReader error. %s nIntegrable, %d, " + "does not match the meta-data file's nIntegrable, %d.\n", + filename_.c_str(), + nTotObjs, + info_->getNGlobalIntegrableObjects()); + + painCave.isFatal = 1; simError(); } - - //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 - - MPI_Bcast(read_buffer, BUFFERSIZE, MPI_CHAR, 0, MPI_COMM_WORLD); - - parseErr = parseCommentLine( read_buffer, simnfo); - - if( parseErr != NULL ){ - strcpy( painCave.errMsg, parseErr ); - haveError = 1; + + //read the box mat from the comment line + + eof_test = fgets(read_buffer, sizeof(read_buffer), inFile_); + + if (eof_test == NULL) { + sprintf(painCave.errMsg, "DumpReader Error: error in reading commment in %s\n", + filename_.c_str()); + painCave.isFatal = 1; simError(); } - - for (i=0 ; i < mpiSim->getNMolGlobal(); i++) { - which_node = MolToProcMap[i]; - if(which_node == 0){ - //molecules belong to master node - - localIndex = mpiSim->getGlobalToLocalMol(i); - - if(localIndex == -1) { - strcpy(painCave.errMsg, "Molecule not found on node 0!"); - haveError = 1; + + parseCommentLine(read_buffer, info_->getSnapshotManager()->getCurrentSnapshot()); + + //parse dump lines + + i = 0; + for (mol = info_->beginMolecule(mi); mol != NULL; mol = info_->nextMolecule(mi)) { + + for (integrableObject = mol->beginIntegrableObject(ii); integrableObject != NULL; + integrableObject = mol->nextIntegrableObject(ii)) { + + eof_test = fgets(read_buffer, sizeof(read_buffer), inFile_); + + if (eof_test == NULL) { + sprintf(painCave.errMsg, + "DumpReader Error: error in reading file %s\n" + "natoms = %d; index = %d\n" + "error reading the line from the file.\n", + filename_.c_str(), + nTotObjs, + i); + + painCave.isFatal = 1; + simError(); + } + + parseDumpLine(read_buffer, integrableObject); + i++; + } + } + + // MPI Section of code.......... + +#else //IS_MPI + + // first thing first, suspend fatalities. + int masterNode = 0; + int nCurObj; + painCave.isEventLoop = 1; + + int myStatus; // 1 = wakeup & success; 0 = error; -1 = AllDone + int haveError; + + MPI_Status istatus; + int nitems; + + nTotObjs = info_->getNGlobalIntegrableObjects(); + haveError = 0; + + if (worldRank == masterNode) { + 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 ", + filename_.c_str()); + painCave.isFatal = 1; simError(); } - - integrableObjects = (simnfo->molecules[localIndex]).getIntegrableObjects(); - for(j=0; j < integrableObjects.size(); j++){ + + nitems = atoi(read_buffer); + + // 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 (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_->getNGlobalIntegrableObjects()); - 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(); - - parseDumpLine(read_buffer, integrableObjects[j]); - - } - - + painCave.isFatal = 1; + simError(); } - else{ - //molecule belongs to slave nodes - - MPI_Recv(&nCurObj, 1, MPI_INT, which_node, - TAKE_THIS_TAG_INT, MPI_COMM_WORLD, &istatus); - for(j=0; j < nCurObj; j++){ + //read the boxMat from the comment line + + eof_test = fgets(read_buffer, sizeof(read_buffer), inFile_); + + if (eof_test == NULL) { + sprintf(painCave.errMsg, "DumpReader Error: error in reading commment in %s\n", + filename_.c_str()); + painCave.isFatal = 1; + simError(); + } + + //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 + + MPI_Bcast(read_buffer, maxBufferSize, MPI_CHAR, masterNode, MPI_COMM_WORLD); + parseCommentLine(read_buffer, info_->getSnapshotManager()->getCurrentSnapshot()); + + for(i = 0; i < info_->getNGlobalMolecules(); i++) { + int which_node = info_->getMolToProc(i); - 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 (which_node == masterNode) { + //molecules belong to master node + + mol = info_->getMoleculeByGlobalIndex(i); + + if (mol == NULL) { + sprintf(painCave.errMsg, "DumpReader Error: Molecule not found on node %d!", worldRank); + painCave.isFatal = 1; + simError(); } - if(haveError) nodeZeroError(); - - MPI_Send(read_buffer, BUFFERSIZE, MPI_CHAR, which_node, - TAKE_THIS_TAG_CHAR, MPI_COMM_WORLD); - - } - + for (integrableObject = mol->beginIntegrableObject(ii); integrableObject != NULL; + integrableObject = mol->nextIntegrableObject(ii)){ + + eof_test = fgets(read_buffer, sizeof(read_buffer), inFile_); + + if (eof_test == NULL) { + sprintf(painCave.errMsg, + "DumpReader Error: error in reading file %s\n" + "natoms = %d; index = %d\n" + "error reading the line from the file.\n", + filename_.c_str(), + nTotObjs, + i); + + painCave.isFatal = 1; + simError(); + } + + 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); + + for(int j = 0; j < nCurObj; j++) { + eof_test = fgets(read_buffer, sizeof(read_buffer), inFile_); + + if (eof_test == NULL) { + sprintf(painCave.errMsg, + "DumpReader Error: error in reading file %s\n" + "natoms = %d; index = %d\n" + "error reading the line from the file.\n", + filename_.c_str(), + nTotObjs, + i); + + 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); + /**@todo*/ + parseCommentLine(read_buffer, info_->getSnapshotManager()->getCurrentSnapshot()); + + for(i = 0; i < info_->getNGlobalMolecules(); i++) { + int which_node = info_->getMolToProc(i); + + if (which_node == worldRank) { + //molecule with global index i belongs to this processor + + mol = info_->getMoleculeByGlobalIndex(i); + if (mol == NULL) { + sprintf(painCave.errMsg, "DumpReader Error: Molecule not found on node %d!", worldRank); + painCave.isFatal = 1; + simError(); + } + + nCurObj = mol->getNIntegrableObjects(); + + MPI_Send(&nCurObj, 1, MPI_INT, masterNode, TAKE_THIS_TAG_INT, + MPI_COMM_WORLD); + + 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); + } + + } + + } + } +#endif + } - else{ - //actions taken at slave nodes - MPI_Bcast(read_buffer, BUFFERSIZE, MPI_CHAR, 0, MPI_COMM_WORLD); - - parseErr = parseCommentLine( read_buffer, simnfo); - - if( parseErr != NULL ){ - strcpy( painCave.errMsg, parseErr ); - haveError = 1; + + void DumpReader::parseDumpLine(char *line, StuntDouble *integrableObject) { + + 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 < 14) { + sprintf(painCave.errMsg, + "DumpReader Error: Not enough Tokens.\n%s\n", line); + painCave.isFatal = 1; simError(); } - - for (i=0 ; i < mpiSim->getNMolGlobal(); i++) { - which_node = MolToProcMap[i]; + + std::string name = tokenizer.nextToken(); + + if (name != integrableObject->getType()) { - if(which_node == worldRank){ - //molecule with global index i belongs to this processor + sprintf(painCave.errMsg, + "DumpReader Error: Atom type [%s] in %s does not match Atom Type [%s] in .md file.\n", + name.c_str(), filename_.c_str(), integrableObject->getType().c_str()); + painCave.isFatal = 1; + simError(); + } + + pos[0] = tokenizer.nextTokenAsDouble(); + pos[1] = tokenizer.nextTokenAsDouble(); + pos[2] = tokenizer.nextTokenAsDouble(); + if (needPos_) { + integrableObject->setPos(pos); + } + + vel[0] = tokenizer.nextTokenAsDouble(); + vel[1] = tokenizer.nextTokenAsDouble(); + vel[2] = tokenizer.nextTokenAsDouble(); + if (needVel_) { + integrableObject->setVel(vel); + } + + if (integrableObject->isDirectional()) { - localIndex = mpiSim->getGlobalToLocalMol(i); - - if(localIndex == -1) { - sprintf(painCave.errMsg, "Molecule not found on node %d\n", worldRank); - haveError = 1; - simError(); - } - - integrableObjects = (simnfo->molecules[localIndex]).getIntegrableObjects(); - - nCurObj = integrableObjects.size(); + q[0] = tokenizer.nextTokenAsDouble(); + q[1] = tokenizer.nextTokenAsDouble(); + q[2] = tokenizer.nextTokenAsDouble(); + q[3] = tokenizer.nextTokenAsDouble(); + + double qlen = q.length(); + if (qlen < oopse::epsilon) { //check quaternion is not equal to 0 - MPI_Send(&nCurObj, 1, MPI_INT, 0, - TAKE_THIS_TAG_INT, MPI_COMM_WORLD); - - for(j = 0; j < integrableObjects.size(); j++){ - - MPI_Recv(read_buffer, BUFFERSIZE, MPI_CHAR, 0, - TAKE_THIS_TAG_CHAR, MPI_COMM_WORLD, &istatus); - - parseErr = parseDumpLine(read_buffer, integrableObjects[j]); - - if( parseErr != NULL ){ - strcpy( painCave.errMsg, parseErr ); - simError(); - } - - } - + sprintf(painCave.errMsg, + "DumpReader Error: initial quaternion error (q0^2 + q1^2 + q2^2 + q3^2 ~ 0).\n"); + painCave.isFatal = 1; + simError(); + + } + + q.normalize(); + if (needQuaternion_) { + integrableObject->setQ(q); } + ji[0] = tokenizer.nextTokenAsDouble(); + ji[1] = tokenizer.nextTokenAsDouble(); + ji[2] = tokenizer.nextTokenAsDouble(); + if (needAngMom_) { + integrableObject->setJ(ji); + } } - + } - -#endif -} - -char* DumpReader::parseDumpLine(char* readLine, StuntDouble* sd){ - - char *foo; // the pointer to the current string token - - 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. - - - // set the string tokenizer - - foo = strtok(readLine, " ,;\t"); - - // check the atom name to the current atom - - 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 ); - } - - // get the positions - - 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 ); - - 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 ); - - 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 ); - - - // get the velocities - - 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 ); - - 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 ); - - - // add the positions and velocities to the atom - - sd->setPos( pos ); - sd->setVel( vel ); - - if (!sd->isDirectional()) - return NULL; - - // get the quaternions - - if( sd->isDirectional() ){ - - 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 ); - - 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[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 ); - - // get the angular velocities - - 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 ); - - 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[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 ); - } - ji[2] = atof( foo ); - - - // 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) { + + + void DumpReader::parseCommentLine(char* line, Snapshot* s) { + double currTime; + Mat3x3d hmat; + double chi; + double integralOfChiDt; + Mat3x3d eta; + + StringTokenizer tokenizer(line); + int nTokens; + + nTokens = tokenizer.countTokens(); + + //comment line should at least contain 10 tokens: current time(1 token) and h-matrix(9 tokens) + if (nTokens < 10) { sprintf(painCave.errMsg, - "initial quaternion error (q0^2 + q1^2 + q2^2 + q3^2 ~ 0).\n"); - return strdup(painCave.errMsg); + "DumpReader Error: Not enough tokens in comment line: %s", line); + painCave.isFatal = 1; + simError(); } - - 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); + + //read current time + currTime = tokenizer.nextTokenAsDouble(); + s->setTime(currTime); + + //read h-matrix + hmat(0, 0) = tokenizer.nextTokenAsDouble(); + hmat(0, 1) = tokenizer.nextTokenAsDouble(); + hmat(0, 2) = tokenizer.nextTokenAsDouble(); + hmat(1, 0) = tokenizer.nextTokenAsDouble(); + hmat(1, 1) = tokenizer.nextTokenAsDouble(); + hmat(1, 2) = tokenizer.nextTokenAsDouble(); + hmat(2, 0) = tokenizer.nextTokenAsDouble(); + hmat(2, 1) = tokenizer.nextTokenAsDouble(); + hmat(2, 2) = tokenizer.nextTokenAsDouble(); + s->setHmat(hmat); + + //read chi and integralOfChidt, they should apprear in pair + if (tokenizer.countTokens() >= 2) { + chi = tokenizer.nextTokenAsDouble(); + integralOfChiDt = tokenizer.nextTokenAsDouble(); - 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); - + s->setChi(chi); + s->setIntegralOfChiDt(integralOfChiDt); } - 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"); - } + //read eta (eta is 3x3 matrix) + if (tokenizer.countTokens() >= 9) { + eta(0, 0) = tokenizer.nextTokenAsDouble(); + eta(0, 1) = tokenizer.nextTokenAsDouble(); + eta(0, 2) = tokenizer.nextTokenAsDouble(); + eta(1, 0) = tokenizer.nextTokenAsDouble(); + eta(1, 1) = tokenizer.nextTokenAsDouble(); + eta(1, 2) = tokenizer.nextTokenAsDouble(); + eta(2, 0) = tokenizer.nextTokenAsDouble(); + eta(2, 1) = tokenizer.nextTokenAsDouble(); + eta(2, 2) = tokenizer.nextTokenAsDouble(); + + s->setEta(eta); } - 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