--- trunk/src/io/DumpReader.cpp 2005/01/25 21:59:18 274 +++ branches/development/src/io/DumpReader.cpp 2013/02/20 15:39:39 1850 @@ -1,24 +1,15 @@ - /* - * Copyright (c) 2005 The University of Notre Dame. All Rights Reserved. +/* + * 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. 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 + * 1. 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 + * 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. @@ -37,571 +28,716 @@ * 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). */ + +#define _LARGEFILE_SOURCE64 +#define _FILE_OFFSET_BITS 64 -#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 + + +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; + } + + DumpReader::~DumpReader() { + +#ifdef IS_MPI + + if (worldRank == 0) { +#endif + + delete inFile_; + +#ifdef IS_MPI + + } + + strcpy(checkPointMsg, "Dump file closed successfully."); + errorCheckPoint(); + +#endif + + return; + } + + int DumpReader::getNFrames(void) { + + if (!isScanned_) + scanFile(); + + return nframes_; + } + + void DumpReader::scanFile(void) { + int lineNo = 0; + std::streampos prevPos; + std::streampos currPos; + +#ifdef IS_MPI + + if (worldRank == 0) { +#endif // is_mpi + + currPos = inFile_->tellg(); + prevPos = currPos; + bool foundOpenSnapshotTag = false; + bool foundClosedSnapshotTag = false; -#include -#include + 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); -#include -#include + if (needCOMprops_) { + Snapshot* s = info_->getSnapshotManager()->getCurrentSnapshot(); + Thermo thermo(info_); + Vector3d com; -#include -#include -#include + 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; -#include "io/DumpReader.hpp" -#include "primitives/Molecule.hpp" -#include "utils/simError.h" -#include "utils/MemoryUtils.hpp" -#include "utils/StringTokenizer.hpp" +#ifndef IS_MPI + inFile_->clear(); + inFile_->seekg(framePos_[whichFrame]); -#ifdef IS_MPI + std::istream& inputStream = *inFile_; -#include -#define TAKE_THIS_TAG_CHAR 0 -#define TAKE_THIS_TAG_INT 1 +#else + int masterNode = 0; + std::stringstream sstream; + if (worldRank == masterNode) { + std::string sendBuffer; -#endif // is_mpi + inFile_->clear(); + inFile_->seekg(framePos_[whichFrame]); + + while (inFile_->getline(buffer, bufferSize)) { + line = buffer; + sendBuffer += line; + sendBuffer += '\n'; + if (line.find("") != std::string::npos) { + break; + } + } -namespace oopse { + 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); + + 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; + } -DumpReader::DumpReader(SimInfo* info, const std::string& filename) - : info_(info), filename_(filename), isScanned_(false), nframes_(0) { - -#ifdef IS_MPI - - if (worldRank == 0) { + std::istream& inputStream = sstream; #endif - inFile_ = fopen(filename_.c_str(), "r"); + inputStream.getline(buffer, bufferSize); - if (inFile_ == NULL) { - sprintf(painCave.errMsg, "DumpReader: Cannot open file: %s\n", filename_.c_str()); - painCave.isFatal = 1; - simError(); - } - -#ifdef IS_MPI + 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); - strcpy(checkPointMsg, "Dump file opened for reading successfully."); - MPIcheckPoint(); + inputStream.getline(buffer, bufferSize); + line = buffer; -#endif + 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) { - return; -} + + 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(); + } -DumpReader::~DumpReader() { + int index = tokenizer.nextTokenAsInt(); + + StuntDouble* sd = info_->getIOIndexToIntegrableObject(index); -#ifdef IS_MPI + if (sd == NULL) { + return; + } + std::string type = tokenizer.nextToken(); + int size = type.size(); - if (worldRank == 0) { -#endif - - int error; - error = fclose(inFile_); - - if (error) { - sprintf(painCave.errMsg, "DumpReader Error: Error closing %s\n", filename_.c_str()); - painCave.isFatal = 1; - simError(); + 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(); } - - MemoryUtils::deleteVectorOfPointer(framePos_); - -#ifdef IS_MPI - + } } - strcpy(checkPointMsg, "Dump file closed successfully."); - MPIcheckPoint(); + 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; + } -#endif + 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': { - return; -} + Vector3d force; + force[0] = tokenizer.nextTokenAsDouble(); + force[1] = tokenizer.nextTokenAsDouble(); + force[2] = tokenizer.nextTokenAsDouble(); + sd->setFrc(force); + break; + } + case 't' : { -int DumpReader::getNFrames(void) { + Vector3d torque; + torque[0] = tokenizer.nextTokenAsDouble(); + torque[1] = tokenizer.nextTokenAsDouble(); + torque[2] = tokenizer.nextTokenAsDouble(); + sd->setTrq(torque); + break; + } + case 'u' : { - if (!isScanned_) - scanFile(); + RealType particlePot; + particlePot = tokenizer.nextTokenAsDouble(); + sd->setParticlePot(particlePot); + break; + } + case 'c' : { - return nframes_; -} + RealType flucQPos; + flucQPos = tokenizer.nextTokenAsDouble(); + sd->setFlucQPos(flucQPos); + break; + } + case 'w' : { -void DumpReader::scanFile(void) { - int i, j; - int lineNum = 0; - char readBuffer[maxBufferSize]; - fpos_t * currPos; + RealType flucQVel; + flucQVel = tokenizer.nextTokenAsDouble(); + sd->setFlucQVel(flucQVel); + break; + } + case 'g' : { -#ifdef IS_MPI + RealType flucQFrc; + flucQFrc = tokenizer.nextTokenAsDouble(); + sd->setFlucQFrc(flucQFrc); + break; + } + case 'e' : { - 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, - "DumpReader Error: File \"%s\" ended unexpectedly at line %d\n", - filename_.c_str(), - lineNum); - painCave.isFatal = 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; + } - while (!feof(inFile_)) { - framePos_.push_back(currPos); + } + } + + } + - i = atoi(readBuffer); + void DumpReader::parseSiteLine(const std::string& line) { - 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 - } + 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(); + } - MPI_Bcast(&nframes_, 1, MPI_INT, 0, MPI_COMM_WORLD); + /** + * The first token is the global integrable object index. + */ - strcpy(checkPointMsg, "Successfully scanned DumpFile\n"); - MPIcheckPoint(); + int index = tokenizer.nextTokenAsInt(); + StuntDouble* sd = info_->getIOIndexToIntegrableObject(index); + if (sd == NULL) { + return; + } -#endif // is_mpi + /** + * 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. + */ - isScanned_ = true; -} + 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]; + } -void DumpReader::readFrame(int whichFrame) { - readSet(whichFrame); -} + /** + * 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; + } + case 'c' : { + + RealType flucQPos; + flucQPos = tokenizer.nextTokenAsDouble(); + sd->setFlucQPos(flucQPos); + break; + } + case 'w' : { + + 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) { + + 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(); + } -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 + while(inputStream.getline(buffer, bufferSize)) { + line = buffer; + + if(line.find("") != std::string::npos) { + break; + } - Molecule* mol; - StuntDouble* integrableObject; - SimInfo::MoleculeIterator mi; - Molecule::IntegrableObjectIterator ii; + parseDumpLine(line); + } + + } -#ifndef IS_MPI + void DumpReader::readSiteData(std::istream& inputStream) { - 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(); + inputStream.getline(buffer, bufferSize); + std::string line(buffer); + + if (line.find("") == std::string::npos) { + // site data isn't required for a simulation, so skip + return; } - nTotObjs = atoi(read_buffer); + while(inputStream.getline(buffer, bufferSize)) { + line = buffer; + + if(line.find("") != std::string::npos) { + break; + } - 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(); + parseSiteLine(line); } + + } - //read the box mat from the comment line + void DumpReader::readFrameProperties(std::istream& inputStream) { - eof_test = fgets(read_buffer, sizeof(read_buffer), inFile_); + Snapshot* s = info_->getSnapshotManager()->getCurrentSnapshot(); + inputStream.getline(buffer, bufferSize); + std::string line(buffer); - if (eof_test == NULL) { - sprintf(painCave.errMsg, "DumpReader Error: error in reading commment in %s\n", - filename_.c_str()); - painCave.isFatal = 1; - simError(); + if (line.find("") == std::string::npos) { + sprintf(painCave.errMsg, + "DumpReader Error: Missing \n"); + painCave.isFatal = 1; + simError(); } - parseCommentLine(read_buffer, info_->getSnapshotManager()->getCurrentSnapshot()); + while(inputStream.getline(buffer, bufferSize)) { + line = buffer; + + if(line.find("") != std::string::npos) { + break; + } + + 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(); + } - //parse dump lines - - 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); - - } + 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(); + } + } - // 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(); - } - - 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()); - - 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, "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); - - 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(); - } - - 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 - -} - -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"); - painCave.isFatal = 1; - simError(); - } - - std::string name = tokenizer.nextToken(); - - if (name != integrableObject->getType()) { - - 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(); - integrableObject->setPos(pos); - - vel[0] = tokenizer.nextTokenAsDouble(); - vel[1] = tokenizer.nextTokenAsDouble(); - vel[2] = tokenizer.nextTokenAsDouble(); - integrableObject->setVel(vel); - - if (integrableObject->isDirectional()) { - - 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 - - sprintf(painCave.errMsg, - "DumpReader Error: initial quaternion error (q0^2 + q1^2 + q2^2 + q3^2 ~ 0).\n"); - painCave.isFatal = 1; - simError(); - - } - - q.normalize(); - - integrableObject->setQ(q); - - ji[0] = tokenizer.nextTokenAsDouble(); - ji[1] = tokenizer.nextTokenAsDouble(); - ji[2] = tokenizer.nextTokenAsDouble(); - integrableObject->setJ(ji); - } - -} - - -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, - "DumpReader Error: Not enough tokens in comment line: %s", line); - painCave.isFatal = 1; - simError(); - } - - //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 integrablOfChidt, they should apprear in pair - if (tokenizer.countTokens() >= 2) { - chi = tokenizer.nextTokenAsDouble(); - integralOfChiDt = tokenizer.nextTokenAsDouble(); - - s->setChi(chi); - s->setIntegralOfChiDt(integralOfChiDt); - } - - //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); - } - - -} - -}//end namespace oopse + +}//end namespace OpenMD