--- trunk/src/io/DumpReader.cpp	2004/09/24 04:16:43	2
+++ branches/development/src/io/DumpReader.cpp	2010/07/09 23:08:25	1465
@@ -1,766 +1,580 @@
-#define _LARGEFILE_SOURCE64
-#define _FILE_OFFSET_BITS 64
-
-#include <sys/types.h>
-#include <sys/stat.h>
-
-#include <iostream>
-#include <math.h>
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-
-
-#include "ReadWrite.hpp"
-#include "simError.h"
-
-#ifdef IS_MPI
-#include <mpi.h>
-#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();
-  }
+/*
+ * 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]  Vardeman & Gezelter, in progress (2009).                        
+ */
   
-  inFileName = in_name;
-#ifdef IS_MPI
-  }
-  strcpy( checkPointMsg, "Dump file opened for reading successfully." );
-  MPIcheckPoint();
-#endif
-  return;  
-}
-
-DumpReader :: ~DumpReader( ){
-#ifdef IS_MPI
-  if (worldRank == 0) {
-#endif
-  vector<fpos_t*>::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();
+#define _LARGEFILE_SOURCE64 
+#define _FILE_OFFSET_BITS 64 
+ 
+#include <sys/types.h> 
+#include <sys/stat.h> 
+ 
+#include <iostream> 
+#include <math.h> 
+ 
+#include <stdio.h> 
+#include <stdlib.h> 
+#include <string.h> 
+ 
+#include "io/DumpReader.hpp" 
+#include "primitives/Molecule.hpp" 
+#include "utils/simError.h" 
+#include "utils/MemoryUtils.hpp" 
+#include "utils/StringTokenizer.hpp" 
+ 
+#ifdef IS_MPI 
+ 
+#include <mpi.h> 
+#define TAKE_THIS_TAG_CHAR 0 
+#define TAKE_THIS_TAG_INT 1 
+ 
+#endif // is_mpi 
+ 
+ 
+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()); 
+      
+      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; j<i; j++){
-	
-	fgets( readBuffer, sizeof( readBuffer ), inFile );
-	lineNum++;    
-	if( feof( inFile ) ){
-	  sprintf( painCave.errMsg,
-		   "File \"%s\" ended unexpectedly at line %d,"
-		   " with atom %d\n", 
-		   inFileName.c_str(),
-		   lineNum, 
-		   j );
-	  painCave.isFatal = 1;
-	  simError();
-	}
-	
-      }
+#ifdef IS_MPI 
       
-      currPos = new fpos_t;
-      fgetpos( inFile, currPos );
-      fgets( readBuffer, sizeof( readBuffer ), inFile );
-      lineNum++;
-    }
+    } 
     
-    delete currPos;
-    rewind( inFile );
+    strcpy(checkPointMsg, "Dump file closed successfully."); 
+    errorCheckPoint(); 
+    
+#endif 
+    
+    return; 
+  } 
+  
+  int DumpReader::getNFrames(void) { 
      
-    isScanned = true;
-
-#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 );
-}
+    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;
+      while(inFile_->getline(buffer, bufferSize)) {
+        ++lineNo;
+        
+        std::string line = buffer;
+        currPos = inFile_->tellg(); 
+        if (line.find("<Snapshot>")!= std::string::npos) {
+          if (foundOpenSnapshotTag) {
+            sprintf(painCave.errMsg, 
+                    "DumpReader:<Snapshot> 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("</Snapshot>") != std::string::npos){
+          if (!foundOpenSnapshotTag) {
+            sprintf(painCave.errMsg, 
+                    "DumpReader:</Snapshot> appears before <Snapshot> at line %d in %s \n", lineNo, 
+                    filename_.c_str()); 
+            painCave.isFatal = 1; 
+            simError(); 
+          }
+          
+          if (foundClosedSnapshotTag) {
+            sprintf(painCave.errMsg, 
+                    "DumpReader:</Snapshot> 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 <Snapshot> 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_Bcast(&nframes_, 1, MPI_INT, 0, MPI_COMM_WORLD); 
+    
+#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::dslElectroFrame) { 
+      needQuaternion_ = true; 
+    } else { 
+      needQuaternion_ = false; 
+    } 
+     
+    if (storageLayout & DataStorage::dslAngularMomentum) { 
+      needAngMom_ = true; 
+    } else { 
+      needAngMom_ = false;     
+    } 
+     
+    readSet(whichFrame); 
 
+    if (needCOMprops_) {
+      Snapshot* s = info_->getSnapshotManager()->getCurrentSnapshot();
+      Vector3d com;
+      Vector3d comvel;
+      Vector3d comw;
+      if (needPos_ && needVel_){
+	info_->getComAll(com, comvel);
+	comw = info_->getAngularMomentum();
+      }else{
+	com = info_->getCom();
+	comvel = 0.0;
+	comw   = 0.0;
+      }
+      s->setCOMprops(com, comvel, comw);      
+    }
 
+  } 
+   
+  void DumpReader::readSet(int whichFrame) {     
+    std::string line;
 
-void DumpReader :: readSet( int whichFrame ){
+#ifndef IS_MPI 
+    inFile_->clear();  
+    inFile_->seekg(framePos_[whichFrame]); 
 
-  int i;
-  unsigned int j;
+    std::istream& inputStream = *inFile_;     
 
-#ifdef IS_MPI
-  int done, which_node, which_atom; // loop counter
-#endif //is_mpi
+#else 
+    int masterNode = 0;
+    std::stringstream sstream;
+    if (worldRank == masterNode) {
+      std::string sendBuffer;
 
-  const int BUFFERSIZE = 2000; // size of the read buffer
-  int nTotObjs; // the number of atoms
-  char read_buffer[BUFFERSIZE]; //the line buffer for reading
+      inFile_->clear();  
+      inFile_->seekg(framePos_[whichFrame]); 
+      
+      while (inFile_->getline(buffer, bufferSize)) {
 
-  char *eof_test; // ptr to see when we reach the end of the file
-  char *parseErr;
+        line = buffer;
+        sendBuffer += line;
+        sendBuffer += '\n';
+        if (line.find("</Snapshot>") != std::string::npos) {
+          break;
+        }        
+      }
 
-  vector<StuntDouble*> integrableObjects;
+      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);     
+      
+      sstream.str(sendBuffer);
+    } else {
+      int sendBufferSize;
+      MPI_Bcast(&sendBufferSize, 1, MPI_INT, masterNode, MPI_COMM_WORLD);     
+      char * recvBuffer = new char[sendBufferSize+1];
+      assert(recvBuffer);
+      recvBuffer[sendBufferSize] = '\0';
+      MPI_Bcast(recvBuffer, sendBufferSize, MPI_CHAR, masterNode, MPI_COMM_WORLD);     
+      sstream.str(recvBuffer);
+      delete [] recvBuffer;
+    }      
 
+    std::istream& inputStream = sstream;  
+#endif
 
-#ifndef IS_MPI
+    inputStream.getline(buffer, bufferSize);
 
-  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();
-  }
+    line = buffer;
+    if (line.find("<Snapshot>") == std::string::npos) {
+      sprintf(painCave.errMsg, 
+              "DumpReader Error: can not find <Snapshot>\n"); 
+      painCave.isFatal = 1; 
+      simError(); 
+    } 
+    
+    //read frameData
+    readFrameProperties(inputStream);
 
-  nTotObjs = atoi( read_buffer );
+    //read StuntDoubles
+    readStuntDoubles(inputStream);     
 
-  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();
-  }
+    inputStream.getline(buffer, bufferSize);
+    line = buffer;
+    if (line.find("</Snapshot>") == std::string::npos) {
+      sprintf(painCave.errMsg, 
+              "DumpReader Error: can not find </Snapshot>\n"); 
+      painCave.isFatal = 1; 
+      simError(); 
+    }        
+   
+  } 
+   
+  void DumpReader::parseDumpLine(const std::string& line) { 
 
-  //read the box mat from the comment 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(); 
+    } 
 
-  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();
-  }
+    int index = tokenizer.nextTokenAsInt();
+ 
+    StuntDouble* integrableObject = info_->getIOIndexToIntegrableObject(index);
 
-  parseErr = parseCommentLine( read_buffer, simnfo);
-  if( parseErr != NULL ){
-    strcpy( painCave.errMsg, parseErr );
-    painCave.isFatal = 1;
-    simError();
-  }
+    if (integrableObject == NULL) {
+      return;
+    }
+    std::string type = tokenizer.nextToken(); 
+    int size = type.size();
 
-  //parse dump lines
+    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 (integrableObject->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( i=0; i < simnfo->n_mol; i++){
+    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_) { 
+              integrableObject->setPos(pos); 
+            }             
+            break;
+        }
+        case 'v' : {
+            Vector3d vel;
+            vel[0] = tokenizer.nextTokenAsDouble(); 
+            vel[1] = tokenizer.nextTokenAsDouble(); 
+            vel[2] = tokenizer.nextTokenAsDouble(); 
+            if (needVel_) { 
+              integrableObject->setVel(vel); 
+            } 
+            break;
+        }
+
+        case 'q' : {
+           Quat4d q;
+           if (integrableObject->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_) {            
+               integrableObject->setQ(q); 
+             }               
+           }            
+           break;
+        }  
+        case 'j' : {
+          Vector3d ji;
+          if (integrableObject->isDirectional()) {
+             ji[0] = tokenizer.nextTokenAsDouble(); 
+             ji[1] = tokenizer.nextTokenAsDouble(); 
+             ji[2] = tokenizer.nextTokenAsDouble(); 
+             if (needAngMom_) { 
+               integrableObject->setJ(ji); 
+             } 
+          }
+          break;
+        }  
+        case 'f': {
+
+          Vector3d force;
+          force[0] = tokenizer.nextTokenAsDouble(); 
+          force[1] = tokenizer.nextTokenAsDouble(); 
+          force[2] = tokenizer.nextTokenAsDouble();           
+          integrableObject->setFrc(force); 
+          break;
+        }
+        case 't' : {
 
-    integrableObjects = (simnfo->molecules[i]).getIntegrableObjects();
+           Vector3d torque;
+           torque[0] = tokenizer.nextTokenAsDouble(); 
+           torque[1] = tokenizer.nextTokenAsDouble(); 
+           torque[2] = tokenizer.nextTokenAsDouble();           
+           integrableObject->setTrq(torque);          
+           break;
+        }
+        default: {
+               sprintf(painCave.errMsg, 
+                       "DumpReader Error: %s is an unrecognized type\n", type.c_str()); 
+               painCave.isFatal = 1; 
+               simError(); 
+          break;   
+        }
 
-    for(j = 0; j < integrableObjects.size(); j++){
-
-      eof_test = fgets(read_buffer, sizeof(read_buffer), inFile);
-      if(eof_test == NULL){
-        sprintf(painCave.errMsg,
-  	      "error in reading file %s\n"
-  	      "natoms  = %d; index = %d\n"
-  	      "error reading the line from the file.\n",
-  	      inFileName.c_str(), nTotObjs, i );
-        painCave.isFatal = 1;
-        simError();
       }
-      
-      parseErr = parseDumpLine( read_buffer, integrableObjects[j]);
-      if( parseErr != NULL ){
-        strcpy( painCave.errMsg, parseErr );
-        painCave.isFatal = 1;
-        simError();
-      }
     }
-  }
+     
+  } 
+   
 
-  // MPI Section of code..........
-#else //IS_MPI
+  void  DumpReader::readStuntDoubles(std::istream& inputStream) {
 
-  // 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();
+    inputStream.getline(buffer, bufferSize);
+    std::string line(buffer);
+    
+    if (line.find("<StuntDoubles>") == std::string::npos) {
+      sprintf(painCave.errMsg, 
+              "DumpReader Error: Missing <StuntDoubles>\n"); 
+      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",
-	       inFileName.c_str(), nTotObjs, simnfo->getTotIntegrableObjects());
-      haveError= 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;
-      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;
-      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;
-        simError();
-      }
-
-       integrableObjects = (simnfo->molecules[localIndex]).getIntegrableObjects();
-       for(j=0; j < integrableObjects.size(); j++){
-        
-          eof_test = fgets(read_buffer, sizeof(read_buffer), inFile);
-          if(eof_test == NULL){
-        	sprintf(painCave.errMsg,
-		    "error in reading file %s\n"
-		    "natoms  = %d; index = %d\n"
-		    "error reading the line from the file.\n",
-		    inFileName.c_str(), nTotObjs, i );
-	        haveError= 1;
-	        simError();
-          }
-          
-          if(haveError) nodeZeroError();
-
-          parseDumpLine(read_buffer, integrableObjects[j]);
-           
-       }
-
-
-      }
-      else{
-      //molecule belongs to slave nodes
-
-        MPI_Recv(&nCurObj, 1, MPI_INT, which_node,
-	       TAKE_THIS_TAG_INT, MPI_COMM_WORLD, &istatus);
+    while(inputStream.getline(buffer, bufferSize)) {
+      line = buffer;
       
-       for(j=0; j < nCurObj; j++){
-        
-          eof_test = fgets(read_buffer, sizeof(read_buffer), inFile);
-          if(eof_test == NULL){
-        	sprintf(painCave.errMsg,
-		    "error in reading file %s\n"
-		    "natoms  = %d; index = %d\n"
-		    "error reading the line from the file.\n",
-		    inFileName.c_str(), nTotObjs, i );
-	        haveError= 1;
-	        simError();
-          }
-          
-          if(haveError) nodeZeroError();
-
-            MPI_Send(read_buffer, BUFFERSIZE, MPI_CHAR, which_node,
-		      TAKE_THIS_TAG_CHAR, MPI_COMM_WORLD);
-           
-       }
-
+      if(line.find("</StuntDoubles>") != std::string::npos) {
+        break;
       }
-      
-    }
-    
-  }
-  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;
-      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::readFrameProperties(std::istream& inputStream) {
 
-        integrableObjects = (simnfo->molecules[localIndex]).getIntegrableObjects();        
+    Snapshot* s = info_->getSnapshotManager()->getCurrentSnapshot();
+    inputStream.getline(buffer, bufferSize);
+    std::string line(buffer);
 
-        nCurObj = integrableObjects.size();
-        
-        MPI_Send(&nCurObj, 1, MPI_INT, 0,
-                        TAKE_THIS_TAG_INT, MPI_COMM_WORLD);
+    if (line.find("<FrameData>") == std::string::npos) {
+      sprintf(painCave.errMsg, 
+              "DumpReader Error: Missing <FrameData>\n"); 
+      painCave.isFatal = 1; 
+      simError(); 
+    }
 
-        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();
-          }
-
-        }
-          
+    while(inputStream.getline(buffer, bufferSize)) {
+      line = buffer;
+      
+      if(line.find("</FrameData>") != 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();      
+      }
 
-  }
-
-#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) {
-      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);
-      
-      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 );
+      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") {
+        RealType chi = tokenizer.nextTokenAsDouble();
+        RealType integralOfChiDt = tokenizer.nextTokenAsDouble();
+        s->setChi(chi); 
+        s->setIntegralOfChiDt(integralOfChiDt);        
+     } 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->setEta(eta); 
+      } else {
+        sprintf(painCave.errMsg, 
+                "DumpReader Error: %s is an invalid property in <FrameData>\n", propertyName.c_str()); 
+        painCave.isFatal = 0; 
+        simError();         
       }
-      integralOfChidt = atof( foo );
       
-      //push chi and integralOfChidt into SimInfo::properties which can be
-      //retrieved by integrator later
-      DoubleData* chiValue = new DoubleData();
-      chiValue->setID(CHIVALUE_ID);
-      chiValue->setData(chi);
-      entry_plug->addProperty(chiValue);
-      
-      DoubleData* integralOfChidtValue = new DoubleData();
-      integralOfChidtValue->setID(INTEGRALOFCHIDT_ID);
-      integralOfChidtValue->setData(integralOfChidt);
-      entry_plug->addProperty(integralOfChidtValue);
-      
     }
-    else
-      return NULL;
-    
-    //get eta
-    foo = strtok(NULL, " ,;\t\n");
-    if(foo != NULL ){
-  
-      for(int i = 0 ; i < 9; i++){
-	
-	if(foo == NULL){
-	  sprintf( painCave.errMsg,
-		   "error in reading eta[%d] from %s\n", i, inFileName.c_str() );
-	  return strdup( painCave.errMsg );
-	}
-	eta[i] = atof( foo );
-	foo = strtok(NULL, " ,;\t\n");
-      }
-    }
-    else
-      return NULL;
-    
-    //push eta into SimInfo::properties which can be
-    //retrieved by integrator later
-    //entry_plug->setBoxM( theBoxMat3 );
-    DoubleArrayData* etaValue = new DoubleArrayData();
-    etaValue->setID(ETAVALUE_ID);
-    etaValue->setData(eta, 9);
-    entry_plug->addProperty(etaValue);
-  }
 
-  return NULL;
-}
-
-#ifdef IS_MPI
-void DumpReader::nodeZeroError( void ){
-  int j, myStatus;
-
-  myStatus = 0;
-  for (j = 0; j < mpiSim->getNProcessors(); j++) {
-    MPI_Send( &myStatus, 1, MPI_INT, j,
-	      TAKE_THIS_TAG_INT, MPI_COMM_WORLD);
   }
 
-
-  MPI_Finalize();
-  exit (0);
-
-}
-
-void DumpReader::anonymousNodeDie( void ){
-
-  MPI_Finalize();
-  exit (0);
-}
-#endif
+   
+}//end namespace OpenMD