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

Comparing branches/new_design/OOPSE-4/src/io/DumpReader.cpp (file contents):
Revision 1683, Thu Oct 28 22:34:02 2004 UTC vs.
Revision 1808 by tim, Tue Nov 30 23:14:29 2004 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines