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