--- trunk/src/io/DumpReader.cpp 2004/09/24 16:27:58 3 +++ branches/development/src/io/DumpReader.cpp 2013/05/10 14:59:13 1871 @@ -1,766 +1,741 @@ -#define _LARGEFILE_SOURCE64 -#define _FILE_OFFSET_BITS 64 - -#include -#include - -#include -#include - -#include -#include -#include - - -#include "io/ReadWrite.hpp" -#include "utils/simError.h" - -#ifdef IS_MPI -#include -#include "brains/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(); - } +/* + * Copyright (c) 2009 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. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. 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. + * + * SUPPORT OPEN SCIENCE! If you use OpenMD or its source code in your + * research, please cite the appropriate papers when you publish your + * work. Good starting points are: + * + * [1] Meineke, et al., J. Comp. Chem. 26, 252-271 (2005). + * [2] Fennell & Gezelter, J. Chem. Phys. 124, 234104 (2006). + * [3] Sun, Lin & Gezelter, J. Chem. Phys. 128, 234107 (2008). + * [4] Kuang & Gezelter, J. Chem. Phys. 133, 164101 (2010). + * [5] Vardeman, Stocker & Gezelter, J. Chem. Theory Comput. 7, 834 (2011). + */ - inFileName = in_name; -#ifdef IS_MPI - } - strcpy( checkPointMsg, "Dump file opened for reading successfully." ); - MPIcheckPoint(); +#define _LARGEFILE_SOURCE64 +#define _FILE_OFFSET_BITS 64 + +#include +#include + +#include +#include + +#include +#include +#include + +#include "io/DumpReader.hpp" +#include "primitives/Molecule.hpp" +#include "utils/simError.h" +#include "utils/MemoryUtils.hpp" +#include "utils/StringTokenizer.hpp" +#include "brains/Thermo.hpp" + +#ifdef IS_MPI +#include #endif - return; -} - -DumpReader :: ~DumpReader( ){ -#ifdef IS_MPI - 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(); - } - - for(i = framePos.begin(); i != framePos.end(); ++i) - delete *i; - framePos.clear(); + + +namespace OpenMD { + + DumpReader::DumpReader(SimInfo* info, const std::string& filename) + : info_(info), filename_(filename), isScanned_(false), nframes_(0), needCOMprops_(false) { + +#ifdef IS_MPI + + if (worldRank == 0) { +#endif + + inFile_ = new std::ifstream(filename_.c_str(), + ifstream::in | ifstream::binary); + + if (inFile_->fail()) { + sprintf(painCave.errMsg, + "DumpReader: Cannot open file: %s\n", + filename_.c_str()); + painCave.isFatal = 1; + simError(); + } + +#ifdef IS_MPI + + } + + strcpy(checkPointMsg, "Dump file opened for reading successfully."); + errorCheckPoint(); + +#endif + + return; + } -#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 + DumpReader::~DumpReader() { - rewind( inFile ); +#ifdef IS_MPI - 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); + delete 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(); - } - - for(j=0; jtellg(); + prevPos = currPos; + bool foundOpenSnapshotTag = false; + bool foundClosedSnapshotTag = false; -#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; - 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(); - } - - 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++){ + while(inFile_->getline(buffer, bufferSize)) { + ++lineNo; + + std::string line = buffer; + currPos = inFile_->tellg(); + if (line.find("")!= std::string::npos) { + if (foundOpenSnapshotTag) { + sprintf(painCave.errMsg, + "DumpReader: is multiply nested at line %d in %s \n", lineNo, + filename_.c_str()); + painCave.isFatal = 1; + simError(); + } + foundOpenSnapshotTag = true; + foundClosedSnapshotTag = false; + framePos_.push_back(prevPos); + + } else if (line.find("") != std::string::npos){ + if (!foundOpenSnapshotTag) { + sprintf(painCave.errMsg, + "DumpReader: appears before at line %d in %s \n", lineNo, + filename_.c_str()); + painCave.isFatal = 1; + simError(); + } + + if (foundClosedSnapshotTag) { + sprintf(painCave.errMsg, + "DumpReader: appears multiply nested at line %d in %s \n", lineNo, + filename_.c_str()); + painCave.isFatal = 1; + simError(); + } + foundClosedSnapshotTag = true; + foundOpenSnapshotTag = false; + } + prevPos = currPos; + } + + // only found for the last frame means the file is corrupted, we should discard + // it and give a warning message + if (foundOpenSnapshotTag) { + sprintf(painCave.errMsg, + "DumpReader: last frame in %s is invalid\n", filename_.c_str()); + painCave.isFatal = 0; + simError(); + framePos_.pop_back(); + } + + nframes_ = framePos_.size(); + + if (nframes_ == 0) { + sprintf(painCave.errMsg, + "DumpReader: %s does not contain a valid frame\n", filename_.c_str()); + painCave.isFatal = 1; + simError(); + } +#ifdef IS_MPI + } + + MPI::COMM_WORLD.Bcast(&nframes_, 1, MPI::INT, 0); + +#endif // is_mpi + + isScanned_ = true; + } + + void DumpReader::readFrame(int whichFrame) { + if (!isScanned_) + scanFile(); + + int storageLayout = info_->getSnapshotManager()->getStorageLayout(); + + if (storageLayout & DataStorage::dslPosition) { + needPos_ = true; + } else { + needPos_ = false; + } + + if (storageLayout & DataStorage::dslVelocity) { + needVel_ = true; + } else { + needVel_ = false; + } + + if (storageLayout & DataStorage::dslAmat || + storageLayout & DataStorage::dslDipole || + storageLayout & DataStorage::dslQuadrupole) { + needQuaternion_ = true; + } else { + needQuaternion_ = false; + } + + if (storageLayout & DataStorage::dslAngularMomentum) { + needAngMom_ = true; + } else { + needAngMom_ = false; + } + + readSet(whichFrame); - 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 ); - painCave.isFatal = 1; - simError(); + if (needCOMprops_) { + Snapshot* s = info_->getSnapshotManager()->getCurrentSnapshot(); + Thermo thermo(info_); + Vector3d com; + + if (needPos_ && needVel_) { + Vector3d comvel; + Vector3d comw; + thermo.getComAll(com, comvel); + comw = thermo.getAngularMomentum(); + } else { + com = thermo.getCom(); + } + } + } + + void DumpReader::readSet(int whichFrame) { + std::string line; + +#ifndef IS_MPI + inFile_->clear(); + inFile_->seekg(framePos_[whichFrame]); + + std::istream& inputStream = *inFile_; + +#else + int masterNode = 0; + std::stringstream sstream; + if (worldRank == masterNode) { + std::string sendBuffer; + + inFile_->clear(); + inFile_->seekg(framePos_[whichFrame]); + + while (inFile_->getline(buffer, bufferSize)) { + + line = buffer; + sendBuffer += line; + sendBuffer += '\n'; + if (line.find("") != std::string::npos) { + break; + } } + + int sendBufferSize = sendBuffer.size(); + MPI::COMM_WORLD.Bcast(&sendBufferSize, 1, MPI::INT, masterNode); + MPI::COMM_WORLD.Bcast((void *)sendBuffer.c_str(), sendBufferSize, + MPI::CHAR, masterNode); - parseErr = parseDumpLine( read_buffer, integrableObjects[j]); - if( parseErr != NULL ){ - strcpy( painCave.errMsg, parseErr ); - painCave.isFatal = 1; - simError(); + sstream.str(sendBuffer); + } else { + int sendBufferSize; + MPI::COMM_WORLD.Bcast(&sendBufferSize, 1, MPI::INT, masterNode); + char * recvBuffer = new char[sendBufferSize+1]; + assert(recvBuffer); + recvBuffer[sendBufferSize] = '\0'; + MPI::COMM_WORLD.Bcast(recvBuffer, sendBufferSize, MPI::CHAR, masterNode); + sstream.str(recvBuffer); + delete [] recvBuffer; + } + + std::istream& inputStream = sstream; +#endif + + inputStream.getline(buffer, bufferSize); + + line = buffer; + if (line.find("") == std::string::npos) { + sprintf(painCave.errMsg, + "DumpReader Error: can not find \n"); + painCave.isFatal = 1; + simError(); + } + + //read frameData + readFrameProperties(inputStream); + + //read StuntDoubles + readStuntDoubles(inputStream); + + inputStream.getline(buffer, bufferSize); + line = buffer; + + if (line.find("") != std::string::npos) { + //read SiteData + readSiteData(inputStream); + } else { + if (line.find("") == std::string::npos) { + sprintf(painCave.errMsg, + "DumpReader Error: can not find \n"); + painCave.isFatal = 1; + simError(); + } + } + } + + void DumpReader::parseDumpLine(const std::string& line) { + + + StringTokenizer tokenizer(line); + int nTokens; + + nTokens = tokenizer.countTokens(); + + if (nTokens < 2) { + sprintf(painCave.errMsg, + "DumpReader Error: Not enough Tokens.\n%s\n", line.c_str()); + painCave.isFatal = 1; + simError(); + } + + int index = tokenizer.nextTokenAsInt(); + + StuntDouble* sd = info_->getIOIndexToIntegrableObject(index); + + if (sd == NULL) { + return; + } + std::string type = tokenizer.nextToken(); + int size = type.size(); + + size_t found; + + if (needPos_) { + found = type.find("p"); + if (found == std::string::npos) { + sprintf(painCave.errMsg, + "DumpReader Error: StuntDouble %d has no Position\n" + "\tField (\"p\") specified.\n%s\n", index, + line.c_str()); + painCave.isFatal = 1; + simError(); } } - } + + if (sd->isDirectional()) { + if (needQuaternion_) { + found = type.find("q"); + if (found == std::string::npos) { + sprintf(painCave.errMsg, + "DumpReader Error: Directional StuntDouble %d has no\n" + "\tQuaternion Field (\"q\") specified.\n%s\n", index, + line.c_str()); + painCave.isFatal = 1; + simError(); + } + } + } - // MPI Section of code.......... -#else //IS_MPI + for(int i = 0; i < size; ++i) { + switch(type[i]) { + + case 'p': { + Vector3d pos; + pos[0] = tokenizer.nextTokenAsDouble(); + pos[1] = tokenizer.nextTokenAsDouble(); + pos[2] = tokenizer.nextTokenAsDouble(); + if (needPos_) { + sd->setPos(pos); + } + break; + } + case 'v' : { + Vector3d vel; + vel[0] = tokenizer.nextTokenAsDouble(); + vel[1] = tokenizer.nextTokenAsDouble(); + vel[2] = tokenizer.nextTokenAsDouble(); + if (needVel_) { + sd->setVel(vel); + } + break; + } - // first thing first, suspend fatalities. - painCave.isEventLoop = 1; + case 'q' : { + Quat4d q; + if (sd->isDirectional()) { + + q[0] = tokenizer.nextTokenAsDouble(); + q[1] = tokenizer.nextTokenAsDouble(); + q[2] = tokenizer.nextTokenAsDouble(); + q[3] = tokenizer.nextTokenAsDouble(); + + RealType qlen = q.length(); + if (qlen < OpenMD::epsilon) { //check quaternion is not equal to 0 + + 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_) { + sd->setQ(q); + } + } + break; + } + case 'j' : { + Vector3d ji; + if (sd->isDirectional()) { + ji[0] = tokenizer.nextTokenAsDouble(); + ji[1] = tokenizer.nextTokenAsDouble(); + ji[2] = tokenizer.nextTokenAsDouble(); + if (needAngMom_) { + sd->setJ(ji); + } + } + break; + } + case 'f': { - int myStatus; // 1 = wakeup & success; 0 = error; -1 = AllDone - int haveError; + Vector3d force; + force[0] = tokenizer.nextTokenAsDouble(); + force[1] = tokenizer.nextTokenAsDouble(); + force[2] = tokenizer.nextTokenAsDouble(); + sd->setFrc(force); + break; + } + case 't' : { - MPI_Status istatus; - int *MolToProcMap = mpiSim->getMolToProcMap(); - int localIndex; - int nCurObj; - int nitems; + Vector3d torque; + torque[0] = tokenizer.nextTokenAsDouble(); + torque[1] = tokenizer.nextTokenAsDouble(); + torque[2] = tokenizer.nextTokenAsDouble(); + sd->setTrq(torque); + break; + } + case 'u' : { - nTotObjs = simnfo->getTotIntegrableObjects(); - haveError = 0; - if (worldRank == 0) { - fsetpos(inFile, framePos[whichFrame]); + RealType particlePot; + particlePot = tokenizer.nextTokenAsDouble(); + sd->setParticlePot(particlePot); + break; + } + case 'c' : { - 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(); - } + RealType flucQPos; + flucQPos = tokenizer.nextTokenAsDouble(); + sd->setFlucQPos(flucQPos); + break; + } + case 'w' : { - nitems = atoi( read_buffer ); + RealType flucQVel; + flucQVel = tokenizer.nextTokenAsDouble(); + sd->setFlucQVel(flucQVel); + break; + } + case 'g' : { - // Check to see that the number of integrable objects in the - // intial configuration file is the same as derived from the - // meta-data file. + RealType flucQFrc; + flucQFrc = tokenizer.nextTokenAsDouble(); + sd->setFlucQFrc(flucQFrc); + break; + } + case 'e' : { - 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(); - } + Vector3d eField; + eField[0] = tokenizer.nextTokenAsDouble(); + eField[1] = tokenizer.nextTokenAsDouble(); + eField[2] = tokenizer.nextTokenAsDouble(); + sd->setElectricField(eField); + break; + } + default: { + sprintf(painCave.errMsg, + "DumpReader Error: %s is an unrecognized type\n", type.c_str()); + painCave.isFatal = 1; + simError(); + break; + } - //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; - 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 + void DumpReader::parseSiteLine(const std::string& line) { - MPI_Bcast(read_buffer, BUFFERSIZE, MPI_CHAR, 0, MPI_COMM_WORLD); + StringTokenizer tokenizer(line); + int nTokens; + + nTokens = tokenizer.countTokens(); + + if (nTokens < 2) { + sprintf(painCave.errMsg, + "DumpReader Error: Not enough Tokens.\n%s\n", line.c_str()); + painCave.isFatal = 1; + simError(); + } - parseErr = parseCommentLine( read_buffer, simnfo); + /** + * The first token is the global integrable object index. + */ - if( parseErr != NULL ){ - strcpy( painCave.errMsg, parseErr ); - haveError = 1; - simError(); + int index = tokenizer.nextTokenAsInt(); + StuntDouble* sd = info_->getIOIndexToIntegrableObject(index); + if (sd == NULL) { + return; } - for (i=0 ; i < mpiSim->getNMolGlobal(); i++) { - which_node = MolToProcMap[i]; - if(which_node == 0){ - //molecules belong to master node + /** + * Test to see if the next token is an integer or not. If not, + * we've got data on the integrable object itself. If there is an + * integer, we're parsing data for a site on a rigid body. + */ - localIndex = mpiSim->getGlobalToLocalMol(i); + std::string indexTest = tokenizer.peekNextToken(); + std::istringstream i(indexTest); + int siteIndex; + if (i >> siteIndex) { + // chew up this token and parse as an int: + siteIndex = tokenizer.nextTokenAsInt(); + RigidBody* rb = static_cast(sd); + sd = rb->getAtoms()[siteIndex]; + } - if(localIndex == -1) { - strcpy(painCave.errMsg, "Molecule not found on node 0!"); - haveError = 1; - simError(); + /** + * The next token contains information on what follows. + */ + std::string type = tokenizer.nextToken(); + int size = type.size(); + + for(int i = 0; i < size; ++i) { + switch(type[i]) { + + case 'u' : { + + RealType particlePot; + particlePot = tokenizer.nextTokenAsDouble(); + sd->setParticlePot(particlePot); + break; } - - integrableObjects = (simnfo->molecules[localIndex]).getIntegrableObjects(); - for(j=0; j < integrableObjects.size(); j++){ + case 'c' : { - 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]); - - } - - + RealType flucQPos; + flucQPos = tokenizer.nextTokenAsDouble(); + sd->setFlucQPos(flucQPos); + break; } - 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++){ + case 'w' : { - 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(); - - MPI_Send(read_buffer, BUFFERSIZE, MPI_CHAR, which_node, - TAKE_THIS_TAG_CHAR, MPI_COMM_WORLD); - - } - + RealType flucQVel; + flucQVel = tokenizer.nextTokenAsDouble(); + sd->setFlucQVel(flucQVel); + break; } - - } + case 'g' : { + + RealType flucQFrc; + flucQFrc = tokenizer.nextTokenAsDouble(); + sd->setFlucQFrc(flucQFrc); + break; + } + case 'e' : { + + Vector3d eField; + eField[0] = tokenizer.nextTokenAsDouble(); + eField[1] = tokenizer.nextTokenAsDouble(); + eField[2] = tokenizer.nextTokenAsDouble(); + sd->setElectricField(eField); + break; + } + default: { + sprintf(painCave.errMsg, + "DumpReader Error: %s is an unrecognized type\n", type.c_str()); + painCave.isFatal = 1; + simError(); + break; + } + } + } + } + + + void DumpReader::readStuntDoubles(std::istream& inputStream) { - } - else{ - //actions taken at slave nodes - MPI_Bcast(read_buffer, BUFFERSIZE, MPI_CHAR, 0, MPI_COMM_WORLD); + inputStream.getline(buffer, bufferSize); + std::string line(buffer); + + if (line.find("") == std::string::npos) { + sprintf(painCave.errMsg, + "DumpReader Error: Missing \n"); + painCave.isFatal = 1; + simError(); + } - parseErr = parseCommentLine( read_buffer, simnfo); + while(inputStream.getline(buffer, bufferSize)) { + line = buffer; + + if(line.find("") != std::string::npos) { + break; + } - if( parseErr != NULL ){ - strcpy( painCave.errMsg, parseErr ); - haveError = 1; - simError(); + parseDumpLine(line); } - 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(localIndex == -1) { - sprintf(painCave.errMsg, "Molecule not found on node %d\n", worldRank); - haveError = 1; - simError(); - } + void DumpReader::readSiteData(std::istream& inputStream) { - integrableObjects = (simnfo->molecules[localIndex]).getIntegrableObjects(); + std::string line(buffer); - nCurObj = integrableObjects.size(); - - 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(); - } - - } - - } + // We already found the starting tag or we wouldn't be + // here, so just start parsing until we get to the ending + // tag: + + while(inputStream.getline(buffer, bufferSize)) { + line = buffer; - } + if(line.find("") != std::string::npos) { + break; + } + parseSiteLine(line); + } + } -#endif -} + void DumpReader::readFrameProperties(std::istream& inputStream) { -char* DumpReader::parseDumpLine(char* readLine, StuntDouble* sd){ + Snapshot* s = info_->getSnapshotManager()->getCurrentSnapshot(); + inputStream.getline(buffer, bufferSize); + std::string line(buffer); - char *foo; // the pointer to the current string token + if (line.find("") == std::string::npos) { + sprintf(painCave.errMsg, + "DumpReader Error: Missing \n"); + 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. - - - // 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) { - sprintf(painCave.errMsg, - "initial quaternion error (q0^2 + q1^2 + q2^2 + q3^2 ~ 0).\n"); - return strdup(painCave.errMsg); - } - - 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); + while(inputStream.getline(buffer, bufferSize)) { + line = buffer; + + if(line.find("") != std::string::npos) { + break; + } - 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 ); + StringTokenizer tokenizer(line, " ;\t\n\r{}:,"); + if (!tokenizer.hasMoreTokens()) { + sprintf(painCave.errMsg, + "DumpReader Error: Not enough Tokens.\n%s\n", line.c_str()); + painCave.isFatal = 1; + simError(); } - integralOfChidt = atof( foo ); + + std::string propertyName = tokenizer.nextToken(); + if (propertyName == "Time") { + RealType currTime = tokenizer.nextTokenAsDouble(); + s->setTime(currTime); + } else if (propertyName == "Hmat"){ + Mat3x3d hmat; + 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); + } else if (propertyName == "Thermostat") { + pair thermostat; + thermostat.first = tokenizer.nextTokenAsDouble(); + thermostat.second = tokenizer.nextTokenAsDouble(); + s->setThermostat(thermostat); + } else if (propertyName == "Barostat") { + Mat3x3d eta; + 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->setBarostat(eta); + } else { + sprintf(painCave.errMsg, + "DumpReader Error: %s is an invalid property in \n", propertyName.c_str()); + painCave.isFatal = 0; + simError(); + } - //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 OpenMD