ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/OOPSE/libmdtools/InitializeFromFile.cpp
(Generate patch)

Comparing trunk/OOPSE/libmdtools/InitializeFromFile.cpp (file contents):
Revision 378 by mmeineke, Fri Mar 21 17:42:12 2003 UTC vs.
Revision 644 by mmeineke, Tue Jul 22 16:41:08 2003 UTC

# Line 12 | Line 12
12   #include "simError.h"
13  
14   #ifdef IS_MPI
15 + #include <mpi.h>
16   #include "mpiSimulation.hpp"
17 + #define TAKE_THIS_TAG_CHAR 0
18 + #define TAKE_THIS_TAG_INT 1
19  
20 < #define TAKE_THIS_TAG 0
20 > namespace initFile{
21 >  void nodeZeroError( void );
22 >  void anonymousNodeDie( void );
23 > }
24 >
25 > using namespace initFile;
26 >
27   #endif // is_mpi
28  
29   InitializeFromFile :: InitializeFromFile( char *in_name ){
# Line 60 | Line 69 | void InitializeFromFile :: read_xyz( SimInfo* the_entr
69   }
70  
71  
72 < void InitializeFromFile :: read_xyz( SimInfo* the_entry_plug ){
72 > void InitializeFromFile :: readInit( SimInfo* the_simnfo ){
73  
74 <  int i; // loop counter
74 >  int i, j, done, which_node, which_atom; // loop counter
75  
76    const int BUFFERSIZE = 2000; // size of the read buffer
77    int n_atoms; // the number of atoms
# Line 74 | Line 83 | void InitializeFromFile :: read_xyz( SimInfo* the_entr
83    char *eof_test; // ptr to see when we reach the end of the file
84    char *parseErr;
85    int procIndex;
86 +  double currTime;
87 +  double boxMat[9];
88 +  double theBoxMat3[3][3];
89  
90 <  entry_plug = the_entry_plug;
90 >  simnfo = the_simnfo;
91  
92  
93   #ifndef IS_MPI
# Line 90 | Line 102 | void InitializeFromFile :: read_xyz( SimInfo* the_entr
102  
103    n_atoms = atoi( read_buffer );
104  
105 <  Atom **atoms = entry_plug->atoms;
105 >  Atom **atoms = simnfo->atoms;
106    DirectionalAtom* dAtom;
107  
108 <  if( n_atoms != entry_plug->n_atoms ){
108 >  if( n_atoms != simnfo->n_atoms ){
109      sprintf( painCave.errMsg,
110               "Initialize from File error. %s n_atoms, %d, "
111               "does not match the BASS file's n_atoms, %d.\n",
112 <             c_in_name, n_atoms, entry_plug->n_atoms );
112 >             c_in_name, n_atoms, simnfo->n_atoms );
113      painCave.isFatal = 1;
114      simError();
115    }
116    
117 <  //read and toss the comment line
117 >  //read the box mat from the comment line
118    
119    eof_test = fgets(read_buffer, sizeof(read_buffer), c_in_file);
120    if(eof_test == NULL){
# Line 112 | Line 124 | void InitializeFromFile :: read_xyz( SimInfo* the_entr
124      simError();
125    }
126  
127 +  parseErr = parseBoxLine( read_buffer, boxMat, currTime );
128 +  if( parseErr != NULL ){
129 +    strcpy( painCave.errMsg, parseErr );
130 +    painCave.isFatal = 1;
131 +    simError();
132 +  }
133 +
134 +  for(i=0;i<3;i++)
135 +    for(j=0;j<3;j++) theBoxMat3[i][j] = boxMat[3*j+i];
136 +
137 +  simnfo->setBoxM( theBoxMat3 );
138 +  simnfo->setTime( currTime );
139 +
140 +
141    for( i=0; i < n_atoms; i++){
142      
143      eof_test = fgets(read_buffer, sizeof(read_buffer), c_in_file);
# Line 131 | Line 157 | void InitializeFromFile :: read_xyz( SimInfo* the_entr
157        strcpy( painCave.errMsg, parseErr );
158        painCave.isFatal = 1;
159        simError();
160 <    }    
160 >    }
161    }
162  
163  
164    // MPI Section of code..........
165   #else //IS_MPI
166  
167 <  int masterIndex;
168 <  int nodeAtomsStart;
143 <  int nodeAtomsEnd;
144 <  int mpiErr;
145 <  int sendError;
167 >  // first thing first, suspend fatalities.
168 >  painCave.isEventLoop = 1;
169  
170 <  MPI_Status istatus[MPI_STATUS_SIZE];
170 >  int myStatus; // 1 = wakeup & success; 0 = error; -1 = AllDone
171 >  int haveError;
172 >  
173 >  MPI_Status istatus;
174 >  int *AtomToProcMap = mpiSim->getAtomToProcMap();
175  
176 +  
177 +  haveError = 0;
178    if (worldRank == 0) {
179 +
180      eof_test = fgets(read_buffer, sizeof(read_buffer), c_in_file);
181      if( eof_test == NULL ){
182        sprintf( painCave.errMsg,
183                 "Error reading 1st line of %d \n ",c_in_name);
184 <      painCave.isFatal = 1;
184 >      haveError = 1;
185        simError();
186      }
187      
188      n_atoms = atoi( read_buffer );
189      
190 <    Atom **atoms = entry_plug->atoms;
190 >    Atom **atoms = simnfo->atoms;
191      DirectionalAtom* dAtom;
192  
193      // Check to see that the number of atoms in the intial configuration file is the
# Line 167 | Line 197 | void InitializeFromFile :: read_xyz( SimInfo* the_entr
197        sprintf( painCave.errMsg,
198                 "Initialize from File error. %s n_atoms, %d, "
199                 "does not match the BASS file's n_atoms, %d.\n",
200 <               c_in_name, n_atoms, entry_plug->n_atoms );
201 <      painCave.isFatal = 1;
200 >               c_in_name, n_atoms, simnfo->n_atoms );
201 >      haveError= 1;
202        simError();
203      }
204      
205 <    //read and toss the comment line
205 >    //read the boxMat from the comment line
206      
207      eof_test = fgets(read_buffer, sizeof(read_buffer), c_in_file);
208      if(eof_test == NULL){
209        sprintf( painCave.errMsg,
210                 "error in reading commment in %s\n", c_in_name);
211 <      painCave.isFatal = 1;
211 >      haveError = 1;
212        simError();
213      }
184  
185    // Read Proc 0 share of the xyz file...
186    masterIndex = 0;
187    for( i=0; i <= mpiSim->getMyAtomEnd(); i++){
214      
215 +    parseErr = parseBoxLine( read_buffer, boxMat, currTime );
216 +    if( parseErr != NULL ){
217 +      strcpy( painCave.errMsg, parseErr );
218 +      haveError = 1;
219 +      simError();
220 +    }
221 +
222 +    MPI_Bcast(boxMat, 9, MPI_DOUBLE, 0, MPI_COMM_WORLD );
223 +    MPI_Bcast(&currTime, 1, MPI_DOUBLE, 0, MPI_COMM_WORLD );
224 +  
225 +    if(haveError) nodeZeroError();
226 +
227 +    for (i=0 ; i < mpiSim->getTotAtoms(); i++) {
228 +      
229        eof_test = fgets(read_buffer, sizeof(read_buffer), c_in_file);
230        if(eof_test == NULL){
231          sprintf(painCave.errMsg,
# Line 193 | Line 233 | void InitializeFromFile :: read_xyz( SimInfo* the_entr
233                  "natoms  = %d; index = %d\n"
234                  "error reading the line from the file.\n",
235                  c_in_name, n_atoms, i );
236 <        painCave.isFatal = 1;
236 >        haveError= 1;
237          simError();
238        }
239      
240 <      parseErr = parseDumpLine( read_buffer, i );
201 <      if( parseErr != NULL ){
202 <        strcpy( painCave.errMsg, parseErr );
203 <        painCave.isFatal = 1;
204 <        simError();
205 <      }    
206 <      masterIndex++;
207 <    }
208 <  }
209 <
210 <  sprintf(checkPointMsg,
211 <          "Node 0 has successfully read positions from input file.");
212 <  MPIcheckPoint();
240 >      if(haveError) nodeZeroError();
241  
242 <  for (procIndex = 1; procIndex < mpiSim->getNumberProcessors();
243 <         procIndex++){
244 <    if (worldRank == 0) {
245 <
246 <      mpiErr = MPI_Recv(&nodeAtomsStart,1,MPI_INT,procIndex,TAKE_THIS_TAG,MPI_COMM_WORLD,
247 <               istatus);
248 <
249 <      mpiErr = MPI_Recv(&nodeAtomsEnd,1,MPI_INT,procIndex,TAKE_THIS_TAG,MPI_COMM_WORLD,
250 <               istatus);
251 <      // Make sure where node 0 is reading from, matches where the receiving node
252 <      // expects it to be.
225 <
226 <      if (masterIndex != nodeAtomsStart){
227 <        sendError = 1;
228 <        mpiErr = MPI_Send(&sendError,1,MPI_INT,procIndex,TAKE_THIS_TAG,MPI_COMM_WORLD);
229 <        sprintf(painCave.errMsg,
230 <                "Initialize from file error: atoms start index (%d) for "
231 <                "node %d not equal to master index (%d)",nodeAtomsStart,procIndex,masterIndex );
232 <        painCave.isFatal = 1;
233 <        simError();
234 <      }
235 <      sendError = 0;
236 <      mpiErr = MPI_Send(&sendError,1,MPI_INT,procIndex,TAKE_THIS_TAG,MPI_COMM_WORLD);
242 >      // Get the Node number which wants this atom:
243 >      which_node = AtomToProcMap[i];    
244 >      if (which_node == 0) {
245 >        parseErr = parseDumpLine( read_buffer, i );
246 >        if( parseErr != NULL ){
247 >          strcpy( painCave.errMsg, parseErr );
248 >          haveError = 1;
249 >          simError();
250 >        }    
251 >        if(haveError) nodeZeroError();
252 >      }
253        
254 <      for ( i = nodeAtomsStart; i <= nodeAtomsEnd; i++){
255 <        eof_test = fgets(read_buffer, sizeof(read_buffer), c_in_file);
256 <        if(eof_test == NULL){
257 <          
258 <          sprintf(read_buffer,"ERROR");
259 <          mpiErr = MPI_Send(read_buffer,BUFFERSIZE,MPI_CHAR,procIndex,TAKE_THIS_TAG,MPI_COMM_WORLD);
260 <          
261 <          sprintf(painCave.errMsg,
262 <                  "error in reading file %s\n"
263 <                  "natoms  = %d; index = %d\n"
264 <                  "error reading the line from the file.\n",
249 <                  c_in_name, n_atoms, i );
250 <          painCave.isFatal = 1;
251 <          simError();
252 <        }
254 >      else {
255 >      
256 >        myStatus = 1;
257 >        MPI_Send(&myStatus, 1, MPI_INT, which_node,
258 >                 TAKE_THIS_TAG_INT, MPI_COMM_WORLD);
259 >        MPI_Send(read_buffer, BUFFERSIZE, MPI_CHAR, which_node,
260 >                 TAKE_THIS_TAG_CHAR, MPI_COMM_WORLD);
261 >        MPI_Send(&i, 1, MPI_INT, which_node, TAKE_THIS_TAG_INT,
262 >                 MPI_COMM_WORLD);
263 >        MPI_Recv(&myStatus, 1, MPI_INT, which_node, TAKE_THIS_TAG_INT,
264 >                 MPI_COMM_WORLD, &istatus);
265          
266 <        mpiErr = MPI_Send(read_buffer,BUFFERSIZE,MPI_CHAR,procIndex,TAKE_THIS_TAG,MPI_COMM_WORLD);
255 <        mpiErr = MPI_Recv(&sendError,1,MPI_INT,procIndex,TAKE_THIS_TAG,MPI_COMM_WORLD,
256 <                          istatus);
257 <        if (sendError) MPIcheckPoint();
258 <
259 <        masterIndex++;
266 >        if(!myStatus) nodeZeroError();
267        }
268      }
269 <
270 <
271 <    else if(worldRank == procIndex){
272 <      nodeAtomsStart = mpiSim->getMyAtomStart();
273 <      nodeAtomsEnd = mpiSim->getMyAtomEnd();
274 <      mpiErr = MPI_Send(&nodeAtomsStart,1,MPI_INT,0,TAKE_THIS_TAG,MPI_COMM_WORLD);
275 <      mpiErr = MPI_Send(&nodeAtomsEnd,1,MPI_INT,0,TAKE_THIS_TAG,MPI_COMM_WORLD);
276 <      
277 <      mpiErr = MPI_Recv(&sendError,1,MPI_INT,0,TAKE_THIS_TAG,MPI_COMM_WORLD,
278 <               istatus);
272 <      if (sendError) MPIcheckPoint();
273 <
274 <      for ( i = 0; i < entry_plug->n_atoms; i++){
275 <
276 <        mpiErr = MPI_Recv(&read_buffer,BUFFERSIZE,MPI_CHAR,0,TAKE_THIS_TAG,MPI_COMM_WORLD,
277 <                          istatus);
278 <        
279 <        if(!strcmp(read_buffer, "ERROR")) MPIcheckPoint();
280 <        
281 <        parseErr = parseDumpLine( read_buffer, i );
282 <        if( parseErr != NULL ){
283 <          sendError = 1;
284 <          mpiErr = MPI_Send(&sendError,1,MPI_INT,0,TAKE_THIS_TAG,MPI_COMM_WORLD);
269 >    myStatus = -1;
270 >    for (j = 0; j < mpiSim->getNumberProcessors(); j++) {      
271 >      MPI_Send( &myStatus, 1, MPI_INT, j,
272 >                TAKE_THIS_TAG_INT, MPI_COMM_WORLD);
273 >    }
274 >    
275 >  } else {
276 >    
277 >    MPI_Bcast(boxMat, 9, MPI_DOUBLE, 0, MPI_COMM_WORLD);
278 >    MPI_Bcast(&currTime, 1, MPI_DOUBLE, 0, MPI_COMM_WORLD);
279  
280 +    done = 0;
281 +    while (!done) {
282  
283 <          strcpy( painCave.errMsg, parseErr );
284 <          painCave.isFatal = 1;
285 <          simError();
286 <        }
287 <        sendError = 0;
288 <        mpiErr = MPI_Send(&sendError,1,MPI_INT,0,TAKE_THIS_TAG,MPI_COMM_WORLD);
283 >      MPI_Recv(&myStatus, 1, MPI_INT, 0,
284 >               TAKE_THIS_TAG_INT, MPI_COMM_WORLD, &istatus);
285 >      
286 >      if(!myStatus) anonymousNodeDie();
287 >      
288 >      if(myStatus < 0) break;
289 >
290 >      MPI_Recv(read_buffer, BUFFERSIZE, MPI_CHAR, 0,
291 >               TAKE_THIS_TAG_CHAR, MPI_COMM_WORLD, &istatus);
292 >      MPI_Recv(&which_atom, 1, MPI_INT, 0,
293 >               TAKE_THIS_TAG_INT, MPI_COMM_WORLD, &istatus);
294 >      
295 >      myStatus = 1;
296 >      parseErr = parseDumpLine( read_buffer, which_atom );
297 >      if( parseErr != NULL ){
298 >        strcpy( painCave.errMsg, parseErr );
299 >        myStatus = 0;;
300 >        simError();
301        }
302 +      
303 +      MPI_Send( &myStatus, 1, MPI_INT, 0,
304 +                TAKE_THIS_TAG_INT, MPI_COMM_WORLD);
305 +      
306      }
295    sprintf(checkPointMsg,"Node %d received initial configuration.",procIndex);
296    MPIcheckPoint();
307    }
308 +    
309 + // last  thing last, enable  fatalities.
310 +  painCave.isEventLoop = 0;
311  
312 +  for(i=0;i<3;i++)
313 +    for(j=0;j<3;j++) theBoxMat3[i][j] = boxMat[3*j+i];
314 +
315 +  simnfo->setBoxM( theBoxMat3 );
316 +  simnfo->setTime( currTime );
317 +
318 +  
319   #endif
320   }
321  
322 + char* InitializeFromFile::parseDumpLine(char* readLine, int globalIndex){
323  
303 char* InitializeFromFile::parseDumpLine(char* readLine, int atomIndex){
304
324    char *foo; // the pointer to the current string token
325    
326    double rx, ry, rz; // position place holders
# Line 310 | Line 329 | char* InitializeFromFile::parseDumpLine(char* readLine
329    double jx, jy, jz; // angular velocity placeholders;
330    double qSqr, qLength; // needed to normalize the quaternion vector.
331    
332 <  Atom **atoms = entry_plug->atoms;
332 >  Atom **atoms = simnfo->atoms;
333    DirectionalAtom* dAtom;
334    
335 <  int n_atoms;
335 >  int j, n_atoms, atomIndex;
336  
337   #ifdef IS_MPI
338    n_atoms = mpiSim->getTotAtoms();
339 +  atomIndex=-1;        
340 +  for (j=0; j < mpiSim->getMyNlocal(); j++) {
341 +    if (atoms[j]->getGlobalIndex() == globalIndex) atomIndex = j;
342 +  }
343 +  if (atomIndex == -1) {
344 +    sprintf( painCave.errMsg,
345 +             "Initialize from file error. Atom at index %d "
346 +             "in file %s does not exist on processor %d .\n",
347 +             globalIndex, c_in_name, mpiSim->getMyNode() );
348 +    return strdup( painCave.errMsg );
349 +  }  
350   #else
351 <  n_atoms = entry_plug->n_atoms;
351 >  n_atoms = simnfo->n_atoms;
352 >  atomIndex = globalIndex;
353   #endif // is_mpi
354  
324
355    // set the string tokenizer
356    
357    foo = strtok(readLine, " ,;\t");
# Line 509 | Line 539 | char* InitializeFromFile::parseDumpLine(char* readLine
539    atoms[atomIndex]->set_vx( vx );
540    atoms[atomIndex]->set_vy( vy );
541    atoms[atomIndex]->set_vz( vz );
542 +
543 +  return NULL;
544 + }
545 +
546 +
547 + char* InitializeFromFile::parseBoxLine(char* readLine, double boxMat[9],
548 +                                       double &time ){
549 +
550 +  char *foo; // the pointer to the current string token
551 +  int j;
552 +
553 +  // set the string tokenizer
554 +  
555 +  foo = strtok(readLine, " ,;\t");
556 +  // set the timeToken.
557 +
558 +  if(foo == NULL){
559 +    sprintf( painCave.errMsg,
560 +             "error in reading Time from %s\n",
561 +             c_in_name );
562 +    return strdup( painCave.errMsg );
563 +  }
564 +  time = atof( foo );
565 +
566 +  // get the Hx vector
567 +
568 +  foo = strtok(NULL, " ,;\t");
569 +  if(foo == NULL){
570 +    sprintf( painCave.errMsg,
571 +             "error in reading Hx[0] from %s\n",
572 +             c_in_name );
573 +    return strdup( painCave.errMsg );
574 +  }
575 +  boxMat[0] = atof( foo );
576 +  
577 +  foo = strtok(NULL, " ,;\t");
578 +  if(foo == NULL){
579 +    sprintf( painCave.errMsg,
580 +             "error in reading Hx[1] from %s\n",
581 +             c_in_name );
582 +    return strdup( painCave.errMsg );
583 +  }
584 +  boxMat[1] = atof( foo );
585 +    
586 +  foo = strtok(NULL, " ,;\t");
587 +  if(foo == NULL){
588 +    sprintf( painCave.errMsg,
589 +             "error in reading Hx[2] from %s\n",
590 +             c_in_name );
591 +    return strdup( painCave.errMsg );
592 +  }
593 +  boxMat[2] = atof( foo );    
594  
595 +  // get the Hy vector
596 +
597 +  foo = strtok(NULL, " ,;\t");
598 +  if(foo == NULL){
599 +    sprintf( painCave.errMsg,
600 +             "error in reading Hy[0] from %s\n",
601 +             c_in_name );
602 +    return strdup( painCave.errMsg );
603 +  }
604 +  boxMat[3] = atof( foo );
605 +  
606 +  foo = strtok(NULL, " ,;\t");
607 +  if(foo == NULL){
608 +    sprintf( painCave.errMsg,
609 +             "error in reading Hy[1] from %s\n",
610 +             c_in_name );
611 +    return strdup( painCave.errMsg );
612 +  }
613 +  boxMat[4] = atof( foo );
614 +    
615 +  foo = strtok(NULL, " ,;\t");
616 +  if(foo == NULL){
617 +    sprintf( painCave.errMsg,
618 +             "error in reading Hy[2] from %s\n",
619 +             c_in_name );
620 +    return strdup( painCave.errMsg );
621 +  }
622 +  boxMat[5] = atof( foo );    
623 +
624 +  // get the Hz vector
625 +
626 +  foo = strtok(NULL, " ,;\t");
627 +  if(foo == NULL){
628 +    sprintf( painCave.errMsg,
629 +             "error in reading Hz[0] from %s\n",
630 +             c_in_name );
631 +    return strdup( painCave.errMsg );
632 +  }
633 +  boxMat[6] = atof( foo );
634 +  
635 +  foo = strtok(NULL, " ,;\t");
636 +  if(foo == NULL){
637 +    sprintf( painCave.errMsg,
638 +             "error in reading Hz[1] from %s\n",
639 +             c_in_name );
640 +    return strdup( painCave.errMsg );
641 +  }
642 +  boxMat[7] = atof( foo );
643 +    
644 +  foo = strtok(NULL, " ,;\t");
645 +  if(foo == NULL){
646 +    sprintf( painCave.errMsg,
647 +             "error in reading Hz[2] from %s\n",
648 +             c_in_name );
649 +    return strdup( painCave.errMsg );
650 +  }
651 +  boxMat[8] = atof( foo );    
652 +
653    return NULL;
654   }
655 +
656 +
657 + #ifdef IS_MPI
658 +
659 + // a couple of functions to let us escape the read loop
660 +
661 + void initFile::nodeZeroError( void ){
662 +  int j, myStatus;
663 +  
664 +  myStatus = 0;
665 +  for (j = 0; j < mpiSim->getNumberProcessors(); j++) {      
666 +    MPI_Send( &myStatus, 1, MPI_INT, j,
667 +              TAKE_THIS_TAG_INT, MPI_COMM_WORLD);
668 +  }  
669 +  
670 +
671 +  MPI_Finalize();
672 +  exit (0);
673 +  
674 + }
675 +
676 + void initFile::anonymousNodeDie( void ){
677 +
678 +  MPI_Finalize();
679 +  exit (0);
680 + }
681 +
682 + #endif //is_mpi

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines