--- trunk/src/io/DumpReader.cpp 2006/09/08 16:13:21 1037 +++ branches/development/src/io/DumpReader.cpp 2012/09/13 14:10:11 1798 @@ -1,43 +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. - */ +/* + * 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, 24107 (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 @@ -57,27 +58,25 @@ #include "utils/simError.h" #include "utils/MemoryUtils.hpp" #include "utils/StringTokenizer.hpp" +#include "brains/Thermo.hpp" #ifdef IS_MPI - #include -#define TAKE_THIS_TAG_CHAR 0 -#define TAKE_THIS_TAG_INT 1 +#endif -#endif // is_mpi - -namespace oopse { +namespace OpenMD { DumpReader::DumpReader(SimInfo* info, const std::string& filename) - : info_(info), filename_(filename), isScanned_(false), nframes_(0) { + : info_(info), filename_(filename), isScanned_(false), nframes_(0), needCOMprops_(false) { #ifdef IS_MPI if (worldRank == 0) { #endif - inFile_ = new std::ifstream(filename_.c_str()); + inFile_ = new std::ifstream(filename_.c_str(), + ifstream::in | ifstream::binary); if (inFile_->fail()) { sprintf(painCave.errMsg, @@ -92,7 +91,7 @@ namespace oopse { } strcpy(checkPointMsg, "Dump file opened for reading successfully."); - MPIcheckPoint(); + errorCheckPoint(); #endif @@ -113,7 +112,7 @@ namespace oopse { } strcpy(checkPointMsg, "Dump file closed successfully."); - MPIcheckPoint(); + errorCheckPoint(); #endif @@ -142,6 +141,7 @@ namespace oopse { prevPos = currPos; bool foundOpenSnapshotTag = false; bool foundClosedSnapshotTag = false; + bool foundOpenSiteDataTag = false; while(inFile_->getline(buffer, bufferSize)) { ++lineNo; @@ -202,7 +202,7 @@ namespace oopse { #ifdef IS_MPI } - MPI_Bcast(&nframes_, 1, MPI_INT, 0, MPI_COMM_WORLD); + MPI::COMM_WORLD.Bcast(&nframes_, 1, MPI::INT, 0); #endif // is_mpi @@ -227,7 +227,9 @@ namespace oopse { needVel_ = false; } - if (storageLayout & DataStorage::dslAmat || storageLayout & DataStorage::dslElectroFrame) { + if (storageLayout & DataStorage::dslAmat || + storageLayout & DataStorage::dslDipole || + storageLayout & DataStorage::dslQuadrupole) { needQuaternion_ = true; } else { needQuaternion_ = false; @@ -240,13 +242,27 @@ namespace oopse { } readSet(whichFrame); + + 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]); @@ -272,16 +288,20 @@ namespace oopse { } int sendBufferSize = sendBuffer.size(); - MPI_Bcast(&sendBufferSize, 1, MPI_INT, masterNode, MPI_COMM_WORLD); - MPI_Bcast((void *)sendBuffer.c_str(), sendBufferSize, MPI_CHAR, masterNode, MPI_COMM_WORLD); + 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_Bcast(&sendBufferSize, 1, MPI_INT, masterNode, MPI_COMM_WORLD); + MPI::COMM_WORLD.Bcast(&sendBufferSize, 1, MPI::INT, masterNode); char * recvBuffer = new char[sendBufferSize+1]; - MPI_Bcast(recvBuffer, sendBufferSize, MPI_CHAR, masterNode, MPI_COMM_WORLD); + assert(recvBuffer); + recvBuffer[sendBufferSize] = '\0'; + MPI::COMM_WORLD.Bcast(recvBuffer, sendBufferSize, MPI::CHAR, masterNode); sstream.str(recvBuffer); + delete [] recvBuffer; } std::istream& inputStream = sstream; @@ -305,13 +325,18 @@ namespace oopse { 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(); - } - + + 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) { @@ -322,7 +347,7 @@ namespace oopse { nTokens = tokenizer.countTokens(); - if (nTokens < 2) { + if (nTokens < 2) { sprintf(painCave.errMsg, "DumpReader Error: Not enough Tokens.\n%s\n", line.c_str()); painCave.isFatal = 1; @@ -331,14 +356,42 @@ namespace oopse { int index = tokenizer.nextTokenAsInt(); - StuntDouble* integrableObject = info_->getIOIndexToIntegrableObject(index); + StuntDouble* sd = info_->getIOIndexToIntegrableObject(index); - if (integrableObject == NULL) { + 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(); + } + } + } + for(int i = 0; i < size; ++i) { switch(type[i]) { @@ -348,7 +401,7 @@ namespace oopse { pos[1] = tokenizer.nextTokenAsDouble(); pos[2] = tokenizer.nextTokenAsDouble(); if (needPos_) { - integrableObject->setPos(pos); + sd->setPos(pos); } break; } @@ -358,14 +411,14 @@ namespace oopse { vel[1] = tokenizer.nextTokenAsDouble(); vel[2] = tokenizer.nextTokenAsDouble(); if (needVel_) { - integrableObject->setVel(vel); + sd->setVel(vel); } break; } case 'q' : { Quat4d q; - if (integrableObject->isDirectional()) { + if (sd->isDirectional()) { q[0] = tokenizer.nextTokenAsDouble(); q[1] = tokenizer.nextTokenAsDouble(); @@ -373,7 +426,7 @@ namespace oopse { q[3] = tokenizer.nextTokenAsDouble(); RealType qlen = q.length(); - if (qlen < oopse::epsilon) { //check quaternion is not equal to 0 + 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"); @@ -384,19 +437,19 @@ namespace oopse { q.normalize(); if (needQuaternion_) { - integrableObject->setQ(q); + sd->setQ(q); } } break; } case 'j' : { Vector3d ji; - if (integrableObject->isDirectional()) { + if (sd->isDirectional()) { ji[0] = tokenizer.nextTokenAsDouble(); ji[1] = tokenizer.nextTokenAsDouble(); ji[2] = tokenizer.nextTokenAsDouble(); if (needAngMom_) { - integrableObject->setJ(ji); + sd->setJ(ji); } } break; @@ -407,7 +460,7 @@ namespace oopse { force[0] = tokenizer.nextTokenAsDouble(); force[1] = tokenizer.nextTokenAsDouble(); force[2] = tokenizer.nextTokenAsDouble(); - integrableObject->setFrc(force); + sd->setFrc(force); break; } case 't' : { @@ -416,9 +469,46 @@ namespace oopse { torque[0] = tokenizer.nextTokenAsDouble(); torque[1] = tokenizer.nextTokenAsDouble(); torque[2] = tokenizer.nextTokenAsDouble(); - integrableObject->setTrq(torque); + sd->setTrq(torque); break; } + 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()); @@ -433,8 +523,106 @@ namespace oopse { } - void DumpReader::readStuntDoubles(std::istream& inputStream) { + void DumpReader::parseSiteLine(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(); + } + + /** + * The first token is the global integrable object index. + */ + + int index = tokenizer.nextTokenAsInt(); + StuntDouble* sd = info_->getIOIndexToIntegrableObject(index); + if (sd == NULL) { + return; + } + + /** + * 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. + */ + + 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]; + } + + /** + * 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); @@ -457,6 +645,28 @@ namespace oopse { } + void DumpReader::readSiteData(std::istream& inputStream) { + + 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; + } + + while(inputStream.getline(buffer, bufferSize)) { + line = buffer; + + if(line.find("") != std::string::npos) { + break; + } + + parseSiteLine(line); + } + + } + void DumpReader::readFrameProperties(std::istream& inputStream) { Snapshot* s = info_->getSnapshotManager()->getCurrentSnapshot(); @@ -502,10 +712,10 @@ namespace oopse { hmat(2, 2) = tokenizer.nextTokenAsDouble(); s->setHmat(hmat); } else if (propertyName == "Thermostat") { - RealType chi = tokenizer.nextTokenAsDouble(); - RealType integralOfChiDt = tokenizer.nextTokenAsDouble(); - s->setChi(chi); - s->setIntegralOfChiDt(integralOfChiDt); + pair thermostat; + thermostat.first = tokenizer.nextTokenAsDouble(); + thermostat.second = tokenizer.nextTokenAsDouble(); + s->setThermostat(thermostat); } else if (propertyName == "Barostat") { Mat3x3d eta; eta(0, 0) = tokenizer.nextTokenAsDouble(); @@ -517,7 +727,7 @@ namespace oopse { eta(2, 0) = tokenizer.nextTokenAsDouble(); eta(2, 1) = tokenizer.nextTokenAsDouble(); eta(2, 2) = tokenizer.nextTokenAsDouble(); - s->setEta(eta); + s->setBarostat(eta); } else { sprintf(painCave.errMsg, "DumpReader Error: %s is an invalid property in \n", propertyName.c_str()); @@ -530,4 +740,4 @@ namespace oopse { } -}//end namespace oopse +}//end namespace OpenMD