ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/branches/new_design/OOPSE-3.0/src/io/DumpReader.cpp
(Generate patch)

Comparing:
trunk/OOPSE-3.0/src/io/DumpReader.cpp (file contents), Revision 1490 by gezelter, Fri Sep 24 04:16:43 2004 UTC vs.
branches/new_design/OOPSE-3.0/src/io/DumpReader.cpp (file contents), Revision 1739 by tim, Mon Nov 15 18:02:15 2004 UTC

# Line 11 | Line 11
11   #include <stdlib.h>
12   #include <string.h>
13  
14 <
15 < #include "ReadWrite.hpp"
16 < #include "simError.h"
17 <
14 > #include "io/DumpReader.hpp"
15 > #include "utils/simError.h"
16 > #include "utils/MemoryUtils"
17   #ifdef IS_MPI
18 +
19   #include <mpi.h>
20 #include "mpiSimulation.hpp"
20   #define TAKE_THIS_TAG_CHAR 0
21   #define TAKE_THIS_TAG_INT 1
22 +
23   #endif // is_mpi
24  
25  
26 < DumpReader :: DumpReader(const char *in_name ){
26 > namespace oopse {
27  
28 <  isScanned = false;
28 > DumpReader::DumpReader(SimInfo* info, const std::string& filename)
29 >                     : info_(info), filename_(filename), isScanned_(false){
30  
31   #ifdef IS_MPI
32 <  if (worldRank == 0) {
32 >
33 >    if (worldRank == 0) {
34   #endif
35  
36 <  inFile = fopen(in_name, "r");
37 <  if(inFile == NULL){
38 <    sprintf(painCave.errMsg,
39 <            "Cannot open file: %s\n", in_name);
40 <    painCave.isFatal = 1;
41 <    simError();
42 <  }
43 <  
42 <  inFileName = in_name;
36 >        inFile_ = fopen(filename_.c_str(), "r");
37 >
38 >        if (inFile_ == NULL) {
39 >            sprintf(painCave.errMsg, "Cannot open file: %s\n", in_name);
40 >            painCave.isFatal = 1;
41 >            simError();
42 >        }
43 >
44   #ifdef IS_MPI
45 <  }
46 <  strcpy( checkPointMsg, "Dump file opened for reading successfully." );
47 <  MPIcheckPoint();
45 >
46 >    }
47 >
48 >    strcpy(checkPointMsg, "Dump file opened for reading successfully.");
49 >    MPIcheckPoint();
50 >
51   #endif
52 <  return;  
52 >
53 >    return;
54   }
55  
56 < DumpReader :: ~DumpReader( ){
56 > DumpReader::~DumpReader() {
57 >
58   #ifdef IS_MPI
59 <  if (worldRank == 0) {
59 >
60 >    if (worldRank == 0) {
61   #endif
55  vector<fpos_t*>::iterator i;
62  
63 <  int error;
64 <  error = fclose( inFile );
59 <  if( error ){
60 <    sprintf( painCave.errMsg,
61 <             "Error closing %s\n", inFileName.c_str());
62 <    simError();
63 <  }
63 >        int error;
64 >        error = fclose(inFile_);
65  
66 <  for(i = framePos.begin(); i != framePos.end(); ++i)
67 <    delete *i;
68 <  framePos.clear();
69 <  
66 >        if (error) {
67 >            sprintf(painCave.errMsg, "Error closing %s\n", filename_.c_str());
68 >            painCave.isFatal = 1;            
69 >            simError();
70 >        }
71 >
72 >        MemoryUtils::deleteVectorOfPointer(framePos_);
73 >
74   #ifdef IS_MPI
75 <  }
76 <  strcpy( checkPointMsg, "Dump file closed successfully." );
77 <  MPIcheckPoint();
75 >
76 >    }
77 >
78 >    strcpy(checkPointMsg, "Dump file closed successfully.");
79 >    MPIcheckPoint();
80 >
81   #endif
82  
83 <  return;
83 >    return;
84   }
85  
86 < int DumpReader::getNframes( void ){
86 > int DumpReader::getNFrames(void) {
87 >    if (!isScanned_)
88 >        scanFile();
89  
90 <  if( !isScanned )
81 <    scanFile();
82 <  return framePos.size();
90 >    return framePos_.size();
91   }
92  
93 < void DumpReader::scanFile( void ){
86 <
93 > void DumpReader::scanFile(void) {
94    int i, j;
95    int lineNum = 0;
96 <  char readBuffer[2000];
97 <  fpos_t *currPos;
96 >  char readBuffer[maxBufferSize];
97 >  fpos_t * currPos;
98  
99   #ifdef IS_MPI
100 <  if( worldRank == 0 ){
100 >
101 >    if (worldRank == 0) {
102   #endif // is_mpi
95    
96    rewind( inFile );
97    
98    currPos = new fpos_t;
99    fgetpos( inFile, currPos );
100    fgets( readBuffer, sizeof( readBuffer ), inFile );
101    lineNum++;
102    if( feof( inFile ) ){
103      sprintf( painCave.errMsg,
104               "File \"%s\" ended unexpectedly at line %d\n",
105               inFileName.c_str(),
106               lineNum );
107      painCave.isFatal = 1;
108      simError();
109    }
103  
104 <    while( !feof( inFile ) ){
112 <      
113 <      framePos.push_back(currPos);
104 >        rewind(inFile_);
105  
106 <      i = atoi(readBuffer);
107 <      
108 <      fgets( readBuffer, sizeof( readBuffer ), inFile );
109 <      lineNum++;    
119 <      if( feof( inFile ) ){
120 <        sprintf( painCave.errMsg,
121 <                 "File \"%s\" ended unexpectedly at line %d\n",
122 <                 inFileName.c_str(),
123 <                 lineNum );
124 <        painCave.isFatal = 1;
125 <        simError();
126 <      }
127 <            
128 <      for(j=0; j<i; j++){
129 <        
130 <        fgets( readBuffer, sizeof( readBuffer ), inFile );
131 <        lineNum++;    
132 <        if( feof( inFile ) ){
133 <          sprintf( painCave.errMsg,
134 <                   "File \"%s\" ended unexpectedly at line %d,"
135 <                   " with atom %d\n",
136 <                   inFileName.c_str(),
137 <                   lineNum,
138 <                   j );
139 <          painCave.isFatal = 1;
140 <          simError();
141 <        }
142 <        
143 <      }
144 <      
145 <      currPos = new fpos_t;
146 <      fgetpos( inFile, currPos );
147 <      fgets( readBuffer, sizeof( readBuffer ), inFile );
148 <      lineNum++;
149 <    }
150 <    
151 <    delete currPos;
152 <    rewind( inFile );
153 <    
154 <    isScanned = true;
106 >        currPos = new fpos_t;
107 >        fgetpos(inFile_, currPos);
108 >        fgets(readBuffer, sizeof(readBuffer), inFile_);
109 >        lineNum++;
110  
111 < #ifdef IS_MPI
112 <  }
113 <  strcpy( checkPointMsg, "Successfully scanned DumpFile\n" );
114 <  MPIcheckPoint();
115 < #endif // is_mpi
116 < }
111 >        if (feof(inFile_)) {
112 >            sprintf(painCave.errMsg,
113 >                    "File \"%s\" ended unexpectedly at line %d\n",
114 >                    filename_.c_str(),
115 >                    lineNum);
116 >            painCave.isFatal = 1;
117 >            simError();
118 >        }
119  
120 < void DumpReader :: readFrame( SimInfo* the_simnfo, int whichFrame){
120 >        while (!feof(inFile_)) {
121 >            framePos_.push_back(currPos);
122  
123 <  simnfo = the_simnfo;
123 >            i = atoi(readBuffer);
124  
125 <  this->readSet( whichFrame );
126 < }
125 >            fgets(readBuffer, sizeof(readBuffer), inFile_);
126 >            lineNum++;
127  
128 +            if (feof(inFile_)) {
129 +                sprintf(painCave.errMsg,
130 +                        "File \"%s\" ended unexpectedly at line %d\n",
131 +                        filename_.c_str(),
132 +                        lineNum);
133 +                painCave.isFatal = 1;
134 +                simError();
135 +            }
136  
137 +            for(j = 0; j < i; j++) {
138 +                fgets(readBuffer, sizeof(readBuffer), inFile_);
139 +                lineNum++;
140  
141 < void DumpReader :: readSet( int whichFrame ){
141 >                if (feof(inFile_)) {
142 >                    sprintf(painCave.errMsg,
143 >                            "File \"%s\" ended unexpectedly at line %d,"
144 >                                " with atom %d\n", filename_.c_str(),
145 >                            lineNum,
146 >                            j);
147  
148 <  int i;
149 <  unsigned int j;
148 >                    painCave.isFatal = 1;
149 >                    simError();
150 >                }
151 >            }
152  
153 < #ifdef IS_MPI
154 <  int done, which_node, which_atom; // loop counter
155 < #endif //is_mpi
153 >            currPos = new fpos_t;
154 >            fgetpos(inFile_, currPos);
155 >            fgets(readBuffer, sizeof(readBuffer), inFile_);
156 >            lineNum++;
157 >        }
158  
159 <  const int BUFFERSIZE = 2000; // size of the read buffer
160 <  int nTotObjs; // the number of atoms
183 <  char read_buffer[BUFFERSIZE]; //the line buffer for reading
159 >        delete currPos;
160 >        rewind(inFile_);
161  
162 <  char *eof_test; // ptr to see when we reach the end of the file
186 <  char *parseErr;
162 >        isScanned = true;
163  
164 <  vector<StuntDouble*> integrableObjects;
164 > #ifdef IS_MPI
165  
166 +    }
167  
168 < #ifndef IS_MPI
168 >    strcpy(checkPointMsg, "Successfully scanned DumpFile\n");
169 >    MPIcheckPoint();
170  
171 <  fsetpos(inFile, framePos[whichFrame]);
194 <  eof_test = fgets(read_buffer, sizeof(read_buffer), inFile);
195 <  if( eof_test == NULL ){
196 <    sprintf( painCave.errMsg,
197 <             "DumpReader error: error reading 1st line of \"%s\"\n",
198 <             inFileName.c_str() );
199 <    painCave.isFatal = 1;
200 <    simError();
201 <  }
171 > #endif // is_mpi
172  
173 <  nTotObjs = atoi( read_buffer );
173 > }
174  
175 <  if( nTotObjs != simnfo->getTotIntegrableObjects() ){
176 <    sprintf( painCave.errMsg,
177 <             "DumpReader error. %s nIntegrable, %d, "
208 <             "does not match the meta-data file's nIntegrable, %d.\n",
209 <             inFileName.c_str(), nTotObjs, simnfo->getTotIntegrableObjects());
210 <    painCave.isFatal = 1;
211 <    simError();
212 <  }
175 > void DumpReader::readFrame(int whichFrame) {
176 >    readSet(whichFrame);
177 > }
178  
179 <  //read the box mat from the comment line
179 > void DumpReader::readSet(int whichFrame) {
180 >  int i;
181 >  int nTotObjs;                  // the number of atoms
182 >  char read_buffer[maxBufferSize];  //the line buffer for reading
183 >  char * eof_test;               // ptr to see when we reach the end of the file
184  
185 <  eof_test = fgets(read_buffer, sizeof(read_buffer), inFile);
186 <  if(eof_test == NULL){
187 <    sprintf( painCave.errMsg,
188 <             "error in reading commment in %s\n", inFileName.c_str());
220 <    painCave.isFatal = 1;
221 <    simError();
222 <  }
185 >  Molecule* mol;
186 >  StuntDouble* integrableObject;
187 >  typename SimInfo::MoleculeIterator mi;
188 >  typename Molecule::IntegrableObjectIterator ii;
189  
190 <  parseErr = parseCommentLine( read_buffer, simnfo);
225 <  if( parseErr != NULL ){
226 <    strcpy( painCave.errMsg, parseErr );
227 <    painCave.isFatal = 1;
228 <    simError();
229 <  }
190 > #ifndef IS_MPI
191  
192 <  //parse dump lines
192 >    fsetpos(inFile_, framePos_[whichFrame]);
193 >    eof_test = fgets(read_buffer, sizeof(read_buffer), inFile_);
194  
195 <  for( i=0; i < simnfo->n_mol; i++){
195 >    if (eof_test == NULL) {
196 >        sprintf(painCave.errMsg,
197 >                "DumpReader error: error reading 1st line of \"%s\"\n",
198 >                filename_.c_str());
199 >        painCave.isFatal = 1;
200 >        simError();
201 >    }
202  
203 <    integrableObjects = (simnfo->molecules[i]).getIntegrableObjects();
203 >    nTotObjs = atoi(read_buffer);
204  
205 <    for(j = 0; j < integrableObjects.size(); j++){
238 <
239 <      eof_test = fgets(read_buffer, sizeof(read_buffer), inFile);
240 <      if(eof_test == NULL){
205 >    if (nTotObjs != info_->getNGlobalIntegrableObjects()) {
206          sprintf(painCave.errMsg,
207 <              "error in reading file %s\n"
208 <              "natoms  = %d; index = %d\n"
209 <              "error reading the line from the file.\n",
210 <              inFileName.c_str(), nTotObjs, i );
207 >                "DumpReader error. %s nIntegrable, %d, "
208 >                    "does not match the meta-data file's nIntegrable, %d.\n",
209 >                filename_.c_str(),
210 >                nTotObjs,
211 >                info_->getNIntegrableObjects());
212 >
213          painCave.isFatal = 1;
214          simError();
248      }
249      
250      parseErr = parseDumpLine( read_buffer, integrableObjects[j]);
251      if( parseErr != NULL ){
252        strcpy( painCave.errMsg, parseErr );
253        painCave.isFatal = 1;
254        simError();
255      }
215      }
257  }
216  
217 <  // MPI Section of code..........
260 < #else //IS_MPI
217 >    //read the box mat from the comment line
218  
219 <  // first thing first, suspend fatalities.
263 <  painCave.isEventLoop = 1;
219 >    eof_test = fgets(read_buffer, sizeof(read_buffer), inFile_);
220  
221 <  int myStatus; // 1 = wakeup & success; 0 = error; -1 = AllDone
222 <  int haveError;
223 <
224 <  MPI_Status istatus;
225 <  int *MolToProcMap = mpiSim->getMolToProcMap();
270 <  int localIndex;
271 <  int nCurObj;
272 <  int nitems;
273 <
274 <  nTotObjs = simnfo->getTotIntegrableObjects();
275 <  haveError = 0;
276 <  if (worldRank == 0) {
277 <     fsetpos(inFile,  framePos[whichFrame]);
278 <
279 <    eof_test = fgets(read_buffer, sizeof(read_buffer), inFile);
280 <    if( eof_test == NULL ){
281 <      sprintf( painCave.errMsg,
282 <               "Error reading 1st line of %s \n ",inFileName.c_str());
283 <      haveError = 1;
284 <      simError();
221 >    if (eof_test == NULL) {
222 >        sprintf(painCave.errMsg, "error in reading commment in %s\n",
223 >                filename_.c_str());
224 >        painCave.isFatal = 1;
225 >        simError();
226      }
227  
228 <    nitems = atoi( read_buffer );
228 >    parseCommentLine(read_buffer, info_->getSnapshotManager()->getCurrentSnapshot(), info_->useInitState());
229  
230 <    // Check to see that the number of integrable objects in the
290 <    // intial configuration file is the same as derived from the
291 <    // meta-data file.
230 >    //parse dump lines
231  
232 <    if( nTotObjs != nitems){
294 <      sprintf( painCave.errMsg,
295 <               "DumpReader Error. %s nIntegrable, %d, "
296 <               "does not match the meta-data file's nIntegrable, %d.\n",
297 <               inFileName.c_str(), nTotObjs, simnfo->getTotIntegrableObjects());
298 <      haveError= 1;
299 <      simError();
300 <    }
232 >    for (mol = info_->beginMolecule(mi); mol != NULL; mol = info_->nextMolecule(mi)) {
233  
234 <    //read the boxMat from the comment line
234 >        for (integrableObject = mol->beginIntegrableObject(i); integrableObject != NULL;
235 >            integrableObject = mol->nextIntegrableObject(i)) {          
236  
237 <    eof_test = fgets(read_buffer, sizeof(read_buffer), inFile);
305 <    if(eof_test == NULL){
306 <      sprintf( painCave.errMsg,
307 <               "error in reading commment in %s\n", inFileName.c_str());
308 <      haveError = 1;
309 <      simError();
310 <    }
237 >            eof_test = fgets(read_buffer, sizeof(read_buffer), inFile_);
238  
239 <    //Every single processor will parse the comment line by itself
240 <    //By using this way, we might lose some efficiency, but if we want to add
241 <    //more parameters into comment line, we only need to modify function
242 <    //parseCommentLine
239 >            if (eof_test == NULL) {
240 >                sprintf(painCave.errMsg,
241 >                        "error in reading file %s\n"
242 >                            "natoms  = %d; index = %d\n"
243 >                            "error reading the line from the file.\n",
244 >                        filename_.c_str(),
245 >                        nTotObjs,
246 >                        i);
247  
248 <    MPI_Bcast(read_buffer, BUFFERSIZE, MPI_CHAR, 0, MPI_COMM_WORLD);
248 >                painCave.isFatal = 1;
249 >                simError();
250 >            }
251  
252 <    parseErr = parseCommentLine( read_buffer, simnfo);
253 <
254 <    if( parseErr != NULL ){
322 <      strcpy( painCave.errMsg, parseErr );
323 <      haveError = 1;
324 <      simError();
252 >            parseDumpLine(read_buffer, integrableObjects[j]);
253 >            
254 >        }
255      }
256  
257 <    for (i=0 ; i < mpiSim->getNMolGlobal(); i++) {
328 <      which_node = MolToProcMap[i];
329 <      if(which_node == 0){
330 <       //molecules belong to master node
257 >    // MPI Section of code..........
258  
259 <      localIndex = mpiSim->getGlobalToLocalMol(i);
259 > #else //IS_MPI
260  
261 <      if(localIndex == -1) {
262 <        strcpy(painCave.errMsg, "Molecule not found on node 0!");
263 <        haveError = 1;
264 <        simError();
338 <      }
261 >    // first thing first, suspend fatalities.
262 >    int masterNode = 0;
263 >    int nCurObj;
264 >    painCave.isEventLoop = 1;
265  
266 <       integrableObjects = (simnfo->molecules[localIndex]).getIntegrableObjects();
267 <       for(j=0; j < integrableObjects.size(); j++){
342 <        
343 <          eof_test = fgets(read_buffer, sizeof(read_buffer), inFile);
344 <          if(eof_test == NULL){
345 <                sprintf(painCave.errMsg,
346 <                    "error in reading file %s\n"
347 <                    "natoms  = %d; index = %d\n"
348 <                    "error reading the line from the file.\n",
349 <                    inFileName.c_str(), nTotObjs, i );
350 <                haveError= 1;
351 <                simError();
352 <          }
353 <          
354 <          if(haveError) nodeZeroError();
266 >    int myStatus; // 1 = wakeup & success; 0 = error; -1 = AllDone
267 >    int haveError;
268  
269 <          parseDumpLine(read_buffer, integrableObjects[j]);
270 <          
358 <       }
269 >    MPI_Status istatus;
270 >    int nitems;
271  
272 +    nTotObjs = info_->getNGlobalIntegrableObjects();
273 +    haveError = 0;
274  
275 <      }
276 <      else{
363 <      //molecule belongs to slave nodes
275 >    if (worldRank == masterNode) {
276 >        fsetpos(inFile_, framePos_[whichFrame]);
277  
278 <        MPI_Recv(&nCurObj, 1, MPI_INT, which_node,
366 <               TAKE_THIS_TAG_INT, MPI_COMM_WORLD, &istatus);
367 <      
368 <       for(j=0; j < nCurObj; j++){
369 <        
370 <          eof_test = fgets(read_buffer, sizeof(read_buffer), inFile);
371 <          if(eof_test == NULL){
372 <                sprintf(painCave.errMsg,
373 <                    "error in reading file %s\n"
374 <                    "natoms  = %d; index = %d\n"
375 <                    "error reading the line from the file.\n",
376 <                    inFileName.c_str(), nTotObjs, i );
377 <                haveError= 1;
378 <                simError();
379 <          }
380 <          
381 <          if(haveError) nodeZeroError();
278 >        eof_test = fgets(read_buffer, sizeof(read_buffer), inFile_);
279  
280 <            MPI_Send(read_buffer, BUFFERSIZE, MPI_CHAR, which_node,
281 <                      TAKE_THIS_TAG_CHAR, MPI_COMM_WORLD);
282 <          
283 <       }
280 >        if (eof_test == NULL) {
281 >            sprintf(painCave.errMsg, "Error reading 1st line of %s \n ",
282 >                    filename_.c_str());
283 >            painCave.isFatal = 1;
284 >            simError();
285 >        }
286  
287 <      }
389 <      
390 <    }
391 <    
392 <  }
393 <  else{
394 <  //actions taken at slave nodes
395 <    MPI_Bcast(read_buffer, BUFFERSIZE, MPI_CHAR, 0, MPI_COMM_WORLD);
287 >        nitems = atoi(read_buffer);
288  
289 <    parseErr = parseCommentLine( read_buffer, simnfo);
289 >        // Check to see that the number of integrable objects in the
290 >        // intial configuration file is the same as derived from the
291 >        // meta-data file.
292  
293 <    if( parseErr != NULL ){
294 <      strcpy( painCave.errMsg, parseErr );
295 <      haveError = 1;
296 <      simError();
297 <    }
298 <  
299 <    for (i=0 ; i < mpiSim->getNMolGlobal(); i++) {
406 <      which_node = MolToProcMap[i];
407 <      
408 <      if(which_node == worldRank){
409 <      //molecule with global index i belongs to this processor
410 <      
411 <        localIndex = mpiSim->getGlobalToLocalMol(i);
293 >        if (nTotObjs != nitems) {
294 >            sprintf(painCave.errMsg,
295 >                    "DumpReader Error. %s nIntegrable, %d, "
296 >                        "does not match the meta-data file's nIntegrable, %d.\n",
297 >                    filename_.c_str(),
298 >                    nTotObjs,
299 >                    info_->getTotIntegrableObjects());
300  
301 <        if(localIndex == -1) {
302 <          sprintf(painCave.errMsg, "Molecule not found on node %d\n", worldRank);
415 <          haveError = 1;
416 <          simError();
301 >            painCave.isFatal = 1;
302 >            simError();
303          }
304  
305 <        integrableObjects = (simnfo->molecules[localIndex]).getIntegrableObjects();        
305 >        //read the boxMat from the comment line
306  
307 <        nCurObj = integrableObjects.size();
422 <        
423 <        MPI_Send(&nCurObj, 1, MPI_INT, 0,
424 <                        TAKE_THIS_TAG_INT, MPI_COMM_WORLD);
307 >        eof_test = fgets(read_buffer, sizeof(read_buffer), inFile_);
308  
309 <        for(j = 0; j < integrableObjects.size(); j++){
309 >        if (eof_test == NULL) {
310 >            sprintf(painCave.errMsg, "error in reading commment in %s\n",
311 >                    filename_.c_str());
312 >            painCave.isFatal = 1;
313 >            simError();
314 >        }
315  
316 <          MPI_Recv(read_buffer, BUFFERSIZE, MPI_CHAR, 0,
317 <                              TAKE_THIS_TAG_CHAR, MPI_COMM_WORLD, &istatus);
316 >        //Every single processor will parse the comment line by itself
317 >        //By using this way, we might lose some efficiency, but if we want to add
318 >        //more parameters into comment line, we only need to modify function
319 >        //parseCommentLine
320  
321 <          parseErr = parseDumpLine(read_buffer, integrableObjects[j]);
321 >        MPI_Bcast(read_buffer, maxBufferSize, MPI_CHAR, masterNode, MPI_COMM_WORLD);
322 >        parseCommentLine(read_buffer, info_->getSnapshotManager()->getCurrentSnapshot(), info_->useInitState());
323  
324 <          if( parseErr != NULL ){
325 <                strcpy( painCave.errMsg, parseErr );
435 <                simError();
436 <          }
324 >        for(i = 0; i < info_->getNGlobalMolecules(); i++) {
325 >            which_node = info_->getMolToProc(i);
326  
327 <        }
328 <          
329 <      }
330 <      
442 <    }
327 >            if (which_node == masterNode) {
328 >                //molecules belong to master node
329 >
330 >                mol = info_->getMoleculeByGlobalIndex(i);
331  
332 <  }
332 >                if (mol == NULL) {
333 >                    strcpy(painCave.errMsg, "Molecule not found on node %d!", worldRank);
334 >                        painCave.isFatal = 1;
335 >                    simError();
336 >                }
337  
338 < #endif
339 < }
338 >                for (integrableObject = mol->beginIntegrableObject(ii); integrableObject != NULL;
339 >                       integrableObject = mol->nextIntegrableObject(ii)){
340 >                        
341 >                    eof_test = fgets(read_buffer, sizeof(read_buffer), inFile_);
342  
343 < char* DumpReader::parseDumpLine(char* readLine, StuntDouble* sd){
343 >                    if (eof_test == NULL) {
344 >                        sprintf(painCave.errMsg,
345 >                                "error in reading file %s\n"
346 >                                    "natoms  = %d; index = %d\n"
347 >                                    "error reading the line from the file.\n",
348 >                                filename_.c_str(),
349 >                                nTotObjs,
350 >                                i);
351  
352 <  char *foo; // the pointer to the current string token
352 >                        painCave.isFatal = 1;
353 >                        simError();
354 >                    }
355  
356 <  double pos[3]; // position place holders
357 <  double vel[3]; // velocity placeholders
358 <  double q[4]; // the quaternions
359 <  double ji[3]; // angular velocity placeholders;
457 <  double qSqr, qLength; // needed to normalize the quaternion vector.
356 >                    parseDumpLine(read_buffer, integrableObject);
357 >                }
358 >            } else {
359 >                //molecule belongs to slave nodes
360  
361 +                MPI_Recv(&nCurObj, 1, MPI_INT, which_node, TAKE_THIS_TAG_INT,
362 +                         MPI_COMM_WORLD, &istatus);
363  
364 <  // set the string tokenizer
364 >                for(int j = 0; j < nCurObj; j++) {
365 >                    eof_test = fgets(read_buffer, sizeof(read_buffer), inFile_);
366  
367 <  foo = strtok(readLine, " ,;\t");
367 >                    if (eof_test == NULL) {
368 >                        sprintf(painCave.errMsg,
369 >                                "error in reading file %s\n"
370 >                                    "natoms  = %d; index = %d\n"
371 >                                    "error reading the line from the file.\n",
372 >                                filename_.c_str(),
373 >                                nTotObjs,
374 >                                i);
375  
376 <  // check the atom name to the current atom
376 >                        painCave.isFatal = 1;
377 >                        simError();
378 >                    }
379 >                    
380 >                    MPI_Send(read_buffer, maxBufferSize, MPI_CHAR, which_node,
381 >                             TAKE_THIS_TAG_CHAR, MPI_COMM_WORLD);
382 >                }
383 >            }
384 >        }
385 >    } else {
386 >        //actions taken at slave nodes
387 >        MPI_Bcast(read_buffer, maxBufferSize, MPI_CHAR, masterNode, MPI_COMM_WORLD);
388  
389 <  if( strcmp( foo, sd->getType() ) ){
390 <    sprintf( painCave.errMsg,
468 <             "DumpReader error.  Does not"
469 <             " match the meta-data atom %s.\n",
470 <             sd->getType() );
471 <    return strdup( painCave.errMsg );
472 <  }
389 >        /**@todo*/
390 >        parseCommentLine(read_buffer, info_->getSnapshotManager()->getCurrentSnapshot(), info_->useInitState());
391  
392 <  // get the positions
392 >        for(i = 0; i < info_->getNGlobalMolecules(); i++) {
393 >            which_node = info_->getMolToProc(i);
394  
395 <  foo = strtok(NULL, " ,;\t");
396 <  if(foo == NULL){
397 <    sprintf( painCave.errMsg,
398 <             "error in reading postition x from %s\n",
399 <             inFileName.c_str());
400 <    return strdup( painCave.errMsg );
401 <  }
402 <  pos[0] = atof( foo );
395 >            if (which_node == worldRank) {
396 >                //molecule with global index i belongs to this processor
397 >                
398 >                mol = info_->getMoleculeByGlobalIndex(i);
399 >                if (mol == NULL) {
400 >                    strcpy(painCave.errMsg, "Molecule not found on node %d!", worldRank);
401 >                    painCave.isFatal = 1;
402 >                    simError();
403 >                }
404 >                
405 >                nCurObj = mol->getNIntegrableObjects();
406  
407 <  foo = strtok(NULL, " ,;\t");
408 <  if(foo == NULL){
487 <    sprintf( painCave.errMsg,
488 <             "error in reading postition y from %s\n",
489 <             inFileName.c_str());
490 <    return strdup( painCave.errMsg );
491 <  }
492 <  pos[1] = atof( foo );
407 >                MPI_Send(&nCurObj, 1, MPI_INT, masterNode, TAKE_THIS_TAG_INT,
408 >                         MPI_COMM_WORLD);
409  
410 <  foo = strtok(NULL, " ,;\t");
411 <  if(foo == NULL){
412 <    sprintf( painCave.errMsg,
413 <             "error in reading postition z from %s\n",
414 <             inFileName.c_str());
499 <    return strdup( painCave.errMsg );
500 <  }
501 <  pos[2] = atof( foo );
410 >                for (integrableObject = mol->beginIntegrableObject(ii); integrableObject != NULL;
411 >                       integrableObject = mol->nextIntegrableObject(ii)){
412 >                        
413 >                    MPI_Recv(read_buffer, maxBufferSize, MPI_CHAR, masterNode,
414 >                             TAKE_THIS_TAG_CHAR, MPI_COMM_WORLD, &istatus);
415  
416 +                    parseDumpLine(read_buffer, integrableObject);
417 +                }
418 +                      
419 +            }
420 +            
421 +        }
422 +        
423 +    }
424  
425 <  // get the velocities
425 > #endif
426  
427 <  foo = strtok(NULL, " ,;\t");
507 <  if(foo == NULL){
508 <    sprintf( painCave.errMsg,
509 <             "error in reading velocity x from %s\n",
510 <             inFileName.c_str() );
511 <    return strdup( painCave.errMsg );
512 <  }
513 <  vel[0] = atof( foo );
427 > }
428  
429 <  foo = strtok(NULL, " ,;\t");
516 <  if(foo == NULL){
517 <    sprintf( painCave.errMsg,
518 <             "error in reading velocity x from %s\n",
519 <             inFileName.c_str() );
520 <    return strdup( painCave.errMsg );
521 <  }
522 <  vel[1] = atof( foo );
429 > void DumpReader::parseDumpLine(char *line, StuntDouble *integrableObject) {
430  
431 <  foo = strtok(NULL, " ,;\t");
432 <  if(foo == NULL){
433 <    sprintf( painCave.errMsg,
434 <             "error in reading velocity x from %s\n",
435 <             inFileName.c_str() );
436 <    return strdup( painCave.errMsg );
437 <  }
438 <  vel[2] = atof( foo );
431 >    Vector3d pos;  // position place holders
432 >    Vector3d vel;  // velocity placeholders
433 >    Quat4d q;    // the quaternions
434 >    Vector3d ji;   // angular velocity placeholders;
435 >    StringTokenizer tokenizer(line);
436 >    int nTokens;
437 >    
438 >    nTokens = tokenizer.countTokens();
439  
440 +    if (nTokens < ) {
441  
534  // add the positions and velocities to the atom
442  
443 <  sd->setPos( pos );
537 <  sd->setVel( vel );
443 >    }
444  
445 <  if (!sd->isDirectional())
540 <    return NULL;
445 >    std::string name = tokenizer.nextToken();
446  
447 <  // get the quaternions
447 >    if (name != integrableObject->getType()) {
448 >        
449 >    }
450  
451 <  if( sd->isDirectional() ){
451 >    pos[0] = tokenizer.nextTokenAsFloat();
452 >    pos[1] = tokenizer.nextTokenAsFloat();
453 >    pos[2] = tokenizer.nextTokenAsFloat();
454 >    integrableObject->setPos(pos);
455 >    
456 >    vel[0] = tokenizer.nextTokenAsFloat();
457 >    vel[1] = tokenizer.nextTokenAsFloat();
458 >    vel[2] = tokenizer.nextTokenAsFloat();
459 >    integrableObject->setVel(vel);
460  
461 <    foo = strtok(NULL, " ,;\t");
462 <    if(foo == NULL){
463 <      sprintf( painCave.errMsg,
464 <                     "error in reading velocity x from %s\n",
465 <                      inFileName.c_str() );
551 <      return strdup( painCave.errMsg );
552 <    }
553 <    q[0] = atof( foo );
461 >    if (integrableObject->isDirectional()) {
462 >        q[0] = tokenizer.nextTokenAsFloat();
463 >        q[1] = tokenizer.nextTokenAsFloat();
464 >        q[2] = tokenizer.nextTokenAsFloat();
465 >        q[3] = tokenizer.nextTokenAsFloat();
466  
467 <    foo = strtok(NULL, " ,;\t");
468 <    if(foo == NULL){
469 <      sprintf( painCave.errMsg,
470 <                     "error in reading velocity x from %s\n",
471 <                      inFileName.c_str() );
472 <      return strdup( painCave.errMsg );
467 >        double qlen = q.length();
468 >        if (qlen < oopse::epsilon) { //check quaternion is not equal to 0
469 >            
470 >            sprintf(painCave.errMsg,
471 >                    "initial quaternion error (q0^2 + q1^2 + q2^2 + q3^2 ~ 0).\n");
472 >            painCave.isFatal = 1;
473 >            simError();
474 >            
475 >        } else if (fabs(qlen - 1.0) > oopse::epsilon) { // check that the quaternion vector is normalized
476 >            sprintf(painCave.errMsg,
477 >                    "initial quaternion error (q0^2 + q1^2 + q2^2 + q3^2  != 1.0).\n");
478 >            painCave.isFatal = 0;
479 >            simError();        
480 >            
481 >            q.normalize();
482 >        }        
483 >        integrableObject->setQ(q);
484 >        
485 >        ji[0] = tokenizer.nextTokenAsFloat();
486 >        ji[1] = tokenizer.nextTokenAsFloat();
487 >        ji[2] = tokenizer.nextTokenAsFloat();
488 >        integrableObject->setJ(ji);
489      }
562    q[1] = atof( foo );
490  
491 <    foo = strtok(NULL, " ,;\t");
565 <    if(foo == NULL){
566 <      sprintf( painCave.errMsg,
567 <                     "error in reading velocity x from %s\n",
568 <                      inFileName.c_str() );
569 <      return strdup( painCave.errMsg );
570 <    }
571 <    q[2] = atof( foo );
491 > }
492  
493 <    foo = strtok(NULL, " ,;\t");
494 <    if(foo == NULL){
495 <      sprintf( painCave.errMsg,
496 <                     "error in reading velocity x from %s\n",
497 <                      inFileName.c_str() );
498 <      return strdup( painCave.errMsg );
499 <    }
580 <    q[3] = atof( foo );
493 >
494 > char *DumpReader::parseCommentLine(char* line, Snapshot* s, bool useInitXSstate) {
495 >    double currTime;
496 >    Mat3x3d hmat;
497 >    double chi;
498 >    double integralOfChiDt;
499 >    Mat3x3d eta;
500  
501 <    // get the angular velocities
501 >    StringTokenizer tokenizer(line);
502 >    int nTokens;
503  
504 <    foo = strtok(NULL, " ,;\t");
585 <    if(foo == NULL){
586 <      sprintf( painCave.errMsg,
587 <                     "error in reading velocity x from %s\n",
588 <                      inFileName.c_str() );
589 <      return strdup( painCave.errMsg );
590 <    }
591 <    ji[0] = atof( foo );
504 >    nTokens = tokenizer.countTokens();
505  
506 <    foo = strtok(NULL, " ,;\t");
507 <    if(foo == NULL){
508 <      sprintf( painCave.errMsg,
509 <                     "error in reading velocity x from %s\n",
510 <                      inFileName.c_str() );
511 <      return strdup( painCave.errMsg );
506 >    //comment line should at least contain 10 tokens: current time(1 token) and  h-matrix(9 tokens)
507 >    if (nTokens < 10) {
508 >            sprintf(painCave.errMsg,
509 >                    "Not enough tokens in comment line: %s", line);
510 >            painCave.isFatal = 1;
511 >            simError();  
512      }
600    ji[1] = atof(foo );
513  
514 <    foo = strtok(NULL, " ,;\t");
515 <    if(foo == NULL){
516 <      sprintf( painCave.errMsg,
517 <                     "error in reading velocity x from %s\n",
606 <                      inFileName.c_str() );
607 <      return strdup( painCave.errMsg );
514 >    //read current time
515 >    currTime = tokenizer.nextTokenAsFloat();
516 >    if (useInitXSstate) {
517 >        s->setTime(currTime);
518      }
519 <    ji[2] = atof( foo );
519 >    
520 >    //read h-matrix
521 >    hmat(0, 0) = tokenizer.nextTokenAsFloat();
522 >    hmat(0, 1) = tokenizer.nextTokenAsFloat();
523 >    hmat(0, 2) = tokenizer.nextTokenAsFloat();
524 >    hmat(1, 0) = tokenizer.nextTokenAsFloat();
525 >    hmat(1, 1) = tokenizer.nextTokenAsFloat();
526 >    hmat(1, 2) = tokenizer.nextTokenAsFloat();
527 >    hmat(2, 0) = tokenizer.nextTokenAsFloat();
528 >    hmat(2, 1) = tokenizer.nextTokenAsFloat();
529 >    hmat(2, 2) = tokenizer.nextTokenAsFloat();
530 >    s->setHmat(hmat);
531 >    
532 >    if (useInitXSstate) {
533 >        //read chi and integrablOfChidt, they should apprear in pair
534 >        if (tokenizer.countTokens() >= 2) {
535 >            chi = tokenizer.nextTokenAsFloat();
536 >            integralOfChiDt = tokenizer.nextTokenAsFloat();            
537  
538 +            s->setChi(chi);
539 +            s->setIntegralOfChiDt(integralOfChiDt);
540 +        }
541 +        
542 +        //read eta (eta is 3x3 matrix)
543 +        if (tokenizer.countTokens() >= 9) {
544 +            eta(0, 0) = tokenizer.nextTokenAsFloat();
545 +            eta(0, 1) = tokenizer.nextTokenAsFloat();
546 +            eta(0, 2) = tokenizer.nextTokenAsFloat();
547 +            eta(1, 0) = tokenizer.nextTokenAsFloat();
548 +            eta(1, 1) = tokenizer.nextTokenAsFloat();
549 +            eta(1, 2) = tokenizer.nextTokenAsFloat();
550 +            eta(2, 0) = tokenizer.nextTokenAsFloat();
551 +            eta(2, 1) = tokenizer.nextTokenAsFloat();
552 +            eta(2, 2) = tokenizer.nextTokenAsFloat();      
553  
554 <    // check that the quaternion vector is normalized
555 <
614 <    qSqr = (q[0] * q[0]) + (q[1] * q[1]) + (q[2] * q[2]) + (q[3] * q[3]);
615 <
616 <    if (fabs(qSqr) < 1e-6) {
617 <      sprintf(painCave.errMsg,
618 <          "initial quaternion error (q0^2 + q1^2 + q2^2 + q3^2 ~ 0).\n");
619 <       return strdup(painCave.errMsg);
554 >            s->setEta(eta);
555 >        }
556      }
557  
622    qLength = sqrt( qSqr );
623    q[0] = q[0] / qLength;
624    q[1] = q[1] / qLength;
625    q[2] = q[2] / qLength;
626    q[3] = q[3] / qLength;
627
628    // add quaternion and angular velocities
629
630    sd->setQ( q );
631    sd->setJ( ji );
632  }
633
634
635
636  return NULL;
637 }
638
639
640 char* DumpReader::parseCommentLine(char* readLine, SimInfo* entry_plug){
641
642  double currTime;
643  double boxMat[9];
644  double theBoxMat3[3][3];
645  double chi;
646  double integralOfChidt;
647  double eta[9];
648
649  char *foo; // the pointer to the current string token
650
651  // set the string tokenizer
652
653  foo = strtok(readLine, " ,;\t");
654  // set the timeToken.
655
656  if(foo == NULL){
657    sprintf( painCave.errMsg,
658             "error in reading Time from %s\n",
659             inFileName.c_str() );
660    return strdup( painCave.errMsg );
661  }
662
663  currTime = atof( foo );
664  entry_plug->setTime( currTime );
665
666  //get H-Matrix
667
668  for(int i = 0 ; i < 9; i++){
669    foo = strtok(NULL, " ,;\t");
670    if(foo == NULL){
671      sprintf( painCave.errMsg,
672               "error in reading H[%d] from %s\n", i, inFileName.c_str() );
673      return strdup( painCave.errMsg );
674    }
675    boxMat[i] = atof( foo );
676  }
677
678  for(int i=0;i<3;i++)
679    for(int j=0;j<3;j++) theBoxMat3[i][j] = boxMat[3*j+i];
680
681  //set H-Matrix
682  entry_plug->setBoxM( theBoxMat3 );
683
684  //get chi and integralOfChidt, they should appear by pair
685
686  if( entry_plug->useInitXSstate ){
687    foo = strtok(NULL, " ,;\t\n");
688    if(foo != NULL){
689      chi = atof(foo);
690      
691      foo = strtok(NULL, " ,;\t\n");
692      if(foo == NULL){
693        sprintf( painCave.errMsg,
694                 "chi and integralOfChidt should appear by pair in %s\n", inFileName.c_str() );
695        return strdup( painCave.errMsg );
696      }
697      integralOfChidt = atof( foo );
698      
699      //push chi and integralOfChidt into SimInfo::properties which can be
700      //retrieved by integrator later
701      DoubleData* chiValue = new DoubleData();
702      chiValue->setID(CHIVALUE_ID);
703      chiValue->setData(chi);
704      entry_plug->addProperty(chiValue);
705      
706      DoubleData* integralOfChidtValue = new DoubleData();
707      integralOfChidtValue->setID(INTEGRALOFCHIDT_ID);
708      integralOfChidtValue->setData(integralOfChidt);
709      entry_plug->addProperty(integralOfChidtValue);
710      
711    }
712    else
713      return NULL;
558      
715    //get eta
716    foo = strtok(NULL, " ,;\t\n");
717    if(foo != NULL ){
718  
719      for(int i = 0 ; i < 9; i++){
720        
721        if(foo == NULL){
722          sprintf( painCave.errMsg,
723                   "error in reading eta[%d] from %s\n", i, inFileName.c_str() );
724          return strdup( painCave.errMsg );
725        }
726        eta[i] = atof( foo );
727        foo = strtok(NULL, " ,;\t\n");
728      }
729    }
730    else
731      return NULL;
732    
733    //push eta into SimInfo::properties which can be
734    //retrieved by integrator later
735    //entry_plug->setBoxM( theBoxMat3 );
736    DoubleArrayData* etaValue = new DoubleArrayData();
737    etaValue->setID(ETAVALUE_ID);
738    etaValue->setData(eta, 9);
739    entry_plug->addProperty(etaValue);
740  }
741
742  return NULL;
559   }
560  
561 < #ifdef IS_MPI
746 < void DumpReader::nodeZeroError( void ){
747 <  int j, myStatus;
748 <
749 <  myStatus = 0;
750 <  for (j = 0; j < mpiSim->getNProcessors(); j++) {
751 <    MPI_Send( &myStatus, 1, MPI_INT, j,
752 <              TAKE_THIS_TAG_INT, MPI_COMM_WORLD);
753 <  }
754 <
755 <
756 <  MPI_Finalize();
757 <  exit (0);
758 <
759 < }
760 <
761 < void DumpReader::anonymousNodeDie( void ){
762 <
763 <  MPI_Finalize();
764 <  exit (0);
765 < }
766 < #endif
561 > }//end namespace oopse

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines