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 417 by gezelter, Thu Mar 27 01:49:45 2003 UTC vs.
Revision 586 by mmeineke, Wed Jul 9 22:14:06 2003 UTC

# Line 13 | Line 13
13  
14   #ifdef IS_MPI
15   #include <mpi.h>
16 #include <mpi++.h>
16   #include "mpiSimulation.hpp"
17 < #define TAKE_THIS_TAG 0
17 > #define TAKE_THIS_TAG_CHAR 0
18 > #define TAKE_THIS_TAG_INT 1
19 >
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 61 | Line 69 | void InitializeFromFile :: read_xyz( SimInfo* the_entr
69   }
70  
71  
72 < void InitializeFromFile :: read_xyz( SimInfo* the_entry_plug ){
72 > void InitializeFromFile :: read_xyz( SimInfo* the_simnfo ){
73  
74    int i, j, done, which_node, which_atom; // loop counter
75  
# Line 75 | 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 boxMat[9];
87 +  
88  
89 <  entry_plug = the_entry_plug;
89 >  simnfo = the_simnfo;
90  
91  
92   #ifndef IS_MPI
# Line 91 | Line 101 | void InitializeFromFile :: read_xyz( SimInfo* the_entr
101  
102    n_atoms = atoi( read_buffer );
103  
104 <  Atom **atoms = entry_plug->atoms;
104 >  Atom **atoms = simnfo->atoms;
105    DirectionalAtom* dAtom;
106  
107 <  if( n_atoms != entry_plug->n_atoms ){
107 >  if( n_atoms != simnfo->n_atoms ){
108      sprintf( painCave.errMsg,
109               "Initialize from File error. %s n_atoms, %d, "
110               "does not match the BASS file's n_atoms, %d.\n",
111 <             c_in_name, n_atoms, entry_plug->n_atoms );
111 >             c_in_name, n_atoms, simnfo->n_atoms );
112      painCave.isFatal = 1;
113      simError();
114    }
115    
116 <  //read and toss the comment line
116 >  //read the box mat from the comment line
117    
118    eof_test = fgets(read_buffer, sizeof(read_buffer), c_in_file);
119    if(eof_test == NULL){
# Line 113 | Line 123 | void InitializeFromFile :: read_xyz( SimInfo* the_entr
123      simError();
124    }
125  
126 +  parseErr = parseBoxLine( read_buffer, boxMat );
127 +  if( parseErr != NULL ){
128 +    strcpy( painCave.errMsg, parseErr );
129 +    painCave.isFatal = 1;
130 +    simError();
131 +  }
132 +
133 +  simnfo->setBoxM( boxMat );
134 +
135 +
136    for( i=0; i < n_atoms; i++){
137      
138      eof_test = fgets(read_buffer, sizeof(read_buffer), c_in_file);
# Line 132 | Line 152 | void InitializeFromFile :: read_xyz( SimInfo* the_entr
152        strcpy( painCave.errMsg, parseErr );
153        painCave.isFatal = 1;
154        simError();
155 <    }    
155 >    }
156    }
157  
158  
159    // MPI Section of code..........
160   #else //IS_MPI
161  
162 <  MPI::Status istatus;
162 >  // first thing first, suspend fatalities.
163 >  painCave.isEventLoop = 1;
164 >
165 >  int myStatus; // 1 = wakeup & success; 0 = error; -1 = AllDone
166 >  int haveError;
167 >  
168 >  MPI_Status istatus;
169    int *AtomToProcMap = mpiSim->getAtomToProcMap();
170  
171 +  
172 +  haveError = 0;
173    if (worldRank == 0) {
174 +
175      eof_test = fgets(read_buffer, sizeof(read_buffer), c_in_file);
176      if( eof_test == NULL ){
177        sprintf( painCave.errMsg,
178                 "Error reading 1st line of %d \n ",c_in_name);
179 <      painCave.isFatal = 1;
179 >      haveError = 1;
180        simError();
181      }
182      
183      n_atoms = atoi( read_buffer );
184      
185 <    Atom **atoms = entry_plug->atoms;
185 >    Atom **atoms = simnfo->atoms;
186      DirectionalAtom* dAtom;
187  
188      // Check to see that the number of atoms in the intial configuration file is the
# Line 163 | Line 192 | void InitializeFromFile :: read_xyz( SimInfo* the_entr
192        sprintf( painCave.errMsg,
193                 "Initialize from File error. %s n_atoms, %d, "
194                 "does not match the BASS file's n_atoms, %d.\n",
195 <               c_in_name, n_atoms, entry_plug->n_atoms );
196 <      painCave.isFatal = 1;
195 >               c_in_name, n_atoms, simnfo->n_atoms );
196 >      haveError= 1;
197        simError();
198      }
199      
200 <    //read and toss the comment line
200 >    //read the boxMat from the comment line
201      
202      eof_test = fgets(read_buffer, sizeof(read_buffer), c_in_file);
203      if(eof_test == NULL){
204        sprintf( painCave.errMsg,
205                 "error in reading commment in %s\n", c_in_name);
206 <      painCave.isFatal = 1;
206 >      haveError = 1;
207        simError();
208      }
209 +    
210 +    parseErr = parseBoxLine( read_buffer, boxMat );
211 +    if( parseErr != NULL ){
212 +      strcpy( painCave.errMsg, parseErr );
213 +      haveError = 1;
214 +      simError();
215 +    }
216 +
217 +    MPI_Bcast(boxMat, 9, MPI_DOUBLE, 0, MPI_COMM_WORLD );
218    
219 +    if(haveError) nodeZeroError();
220  
221      for (i=0 ; i < mpiSim->getTotAtoms(); i++) {
222        
# Line 188 | Line 227 | void InitializeFromFile :: read_xyz( SimInfo* the_entr
227                  "natoms  = %d; index = %d\n"
228                  "error reading the line from the file.\n",
229                  c_in_name, n_atoms, i );
230 <        painCave.isFatal = 1;
230 >        haveError= 1;
231          simError();
232        }
233      
234 +      if(haveError) nodeZeroError();
235 +
236        // Get the Node number which wants this atom:
237        which_node = AtomToProcMap[i];    
238 <      if (which_node == mpiSim->getMyNode()) {
238 >      if (which_node == 0) {
239          parseErr = parseDumpLine( read_buffer, i );
240          if( parseErr != NULL ){
241            strcpy( painCave.errMsg, parseErr );
242 <          painCave.isFatal = 1;
242 >          haveError = 1;
243            simError();
244          }    
245 <      } else {
246 <        MPI::COMM_WORLD.Send(read_buffer, BUFFERSIZE, MPI_CHAR, which_node,
247 <                             TAKE_THIS_TAG);
248 <        MPI::COMM_WORLD.Send(&i, 1, MPI_INT, which_node, TAKE_THIS_TAG);
245 >        if(haveError) nodeZeroError();
246 >      }
247 >      
248 >      else {
249 >      
250 >        myStatus = 1;
251 >        MPI_Send(&myStatus, 1, MPI_INT, which_node,
252 >                 TAKE_THIS_TAG_INT, MPI_COMM_WORLD);
253 >        MPI_Send(read_buffer, BUFFERSIZE, MPI_CHAR, which_node,
254 >                 TAKE_THIS_TAG_CHAR, MPI_COMM_WORLD);
255 >        MPI_Send(&i, 1, MPI_INT, which_node, TAKE_THIS_TAG_INT,
256 >                 MPI_COMM_WORLD);
257 >        MPI_Recv(&myStatus, 1, MPI_INT, which_node, TAKE_THIS_TAG_INT,
258 >                 MPI_COMM_WORLD, &istatus);
259 >        
260 >        if(!myStatus) nodeZeroError();
261        }
262      }
263 <    sprintf(read_buffer, "GAMEOVER");
263 >    myStatus = -1;
264      for (j = 0; j < mpiSim->getNumberProcessors(); j++) {      
265 <      MPI::COMM_WORLD.Send(read_buffer, BUFFERSIZE, MPI_CHAR, j,
266 <                           TAKE_THIS_TAG);
265 >      MPI_Send( &myStatus, 1, MPI_INT, j,
266 >                TAKE_THIS_TAG_INT, MPI_COMM_WORLD);
267      }
268 <
268 >    
269    } else {
270 +    
271 +    MPI_Bcast(boxMat, 9, MPI_DOUBLE, 0, MPI_COMM_WORLD);
272  
273      done = 0;
274      while (!done) {
275 <      MPI::COMM_WORLD.Recv(read_buffer, BUFFERSIZE, MPI_CHAR, 0,
276 <                           TAKE_THIS_TAG, istatus);
277 <      if (strcmp(read_buffer, "GAMEOVER")) {
278 <        done = 1;
279 <        continue;
280 <      } else {
281 <        MPI::COMM_WORLD.Recv(&which_atom, 1, MPI_INT, 0,
282 <                             TAKE_THIS_TAG, istatus);
283 <        parseErr = parseDumpLine( read_buffer, which_atom );
284 <        if( parseErr != NULL ){
285 <          strcpy( painCave.errMsg, parseErr );
286 <          painCave.isFatal = 1;
287 <          simError();
288 <        }
275 >
276 >      MPI_Recv(&myStatus, 1, MPI_INT, 0,
277 >               TAKE_THIS_TAG_INT, MPI_COMM_WORLD, &istatus);
278 >      
279 >      if(!myStatus) anonymousNodeDie();
280 >      
281 >      if(myStatus < 0) break;
282 >
283 >      MPI_Recv(read_buffer, BUFFERSIZE, MPI_CHAR, 0,
284 >               TAKE_THIS_TAG_CHAR, MPI_COMM_WORLD, &istatus);
285 >      MPI_Recv(&which_atom, 1, MPI_INT, 0,
286 >               TAKE_THIS_TAG_INT, MPI_COMM_WORLD, &istatus);
287 >      
288 >      myStatus = 1;
289 >      parseErr = parseDumpLine( read_buffer, which_atom );
290 >      if( parseErr != NULL ){
291 >        strcpy( painCave.errMsg, parseErr );
292 >        myStatus = 0;;
293 >        simError();
294        }
295 +      
296 +      MPI_Send( &myStatus, 1, MPI_INT, 0,
297 +                TAKE_THIS_TAG_INT, MPI_COMM_WORLD);
298 +      
299      }
300    }
301 <        
301 >    
302 > // last  thing last, enable  fatalities.
303 >  painCave.isEventLoop = 0;
304 >  simnfo->setBoxM( boxMat );
305 >
306 >  
307   #endif
308   }
309  
310 < char* InitializeFromFile::parseDumpLine(char* readLine, int atomIndex){
310 > char* InitializeFromFile::parseDumpLine(char* readLine, int globalIndex){
311  
312    char *foo; // the pointer to the current string token
313    
# Line 248 | Line 317 | char* InitializeFromFile::parseDumpLine(char* readLine
317    double jx, jy, jz; // angular velocity placeholders;
318    double qSqr, qLength; // needed to normalize the quaternion vector.
319    
320 <  Atom **atoms = entry_plug->atoms;
320 >  Atom **atoms = simnfo->atoms;
321    DirectionalAtom* dAtom;
322    
323 <  int n_atoms;
323 >  int j, n_atoms, atomIndex;
324  
325   #ifdef IS_MPI
326    n_atoms = mpiSim->getTotAtoms();
327 +  atomIndex=-1;        
328 +  for (j=0; j < mpiSim->getMyNlocal(); j++) {
329 +    if (atoms[j]->getGlobalIndex() == globalIndex) atomIndex = j;
330 +  }
331 +  if (atomIndex == -1) {
332 +    sprintf( painCave.errMsg,
333 +             "Initialize from file error. Atom at index %d "
334 +             "in file %s does not exist on processor %d .\n",
335 +             globalIndex, c_in_name, mpiSim->getMyNode() );
336 +    return strdup( painCave.errMsg );
337 +  }  
338   #else
339 <  n_atoms = entry_plug->n_atoms;
339 >  n_atoms = simnfo->n_atoms;
340 >  atomIndex = globalIndex;
341   #endif // is_mpi
342  
262
343    // set the string tokenizer
344    
345    foo = strtok(readLine, " ,;\t");
# Line 450 | Line 530 | char* InitializeFromFile::parseDumpLine(char* readLine
530  
531    return NULL;
532   }
533 +
534 +
535 + char* InitializeFromFile::parseBoxLine(char* readLine, double boxMat[9]){
536 +
537 +  char *foo; // the pointer to the current string token
538 +  int j;
539 +
540 +  // set the string tokenizer
541 +  
542 +  foo = strtok(readLine, " ,;\t");
543 +  // ignore the first token which is the time stamp.
544 +
545 +
546 +  // get the Hx vector
547 +
548 +  foo = strtok(NULL, " ,;\t");
549 +  if(foo == NULL){
550 +    sprintf( painCave.errMsg,
551 +             "error in reading Hx[0] from %s\n",
552 +             c_in_name );
553 +    return strdup( painCave.errMsg );
554 +  }
555 +  boxMat[0] = atof( foo );
556 +  
557 +  foo = strtok(NULL, " ,;\t");
558 +  if(foo == NULL){
559 +    sprintf( painCave.errMsg,
560 +             "error in reading Hx[1] from %s\n",
561 +             c_in_name );
562 +    return strdup( painCave.errMsg );
563 +  }
564 +  boxMat[1] = atof( foo );
565 +    
566 +  foo = strtok(NULL, " ,;\t");
567 +  if(foo == NULL){
568 +    sprintf( painCave.errMsg,
569 +             "error in reading Hx[2] from %s\n",
570 +             c_in_name );
571 +    return strdup( painCave.errMsg );
572 +  }
573 +  boxMat[2] = atof( foo );    
574 +
575 +  // get the Hy vector
576 +
577 +  foo = strtok(NULL, " ,;\t");
578 +  if(foo == NULL){
579 +    sprintf( painCave.errMsg,
580 +             "error in reading Hy[0] from %s\n",
581 +             c_in_name );
582 +    return strdup( painCave.errMsg );
583 +  }
584 +  boxMat[3] = atof( foo );
585 +  
586 +  foo = strtok(NULL, " ,;\t");
587 +  if(foo == NULL){
588 +    sprintf( painCave.errMsg,
589 +             "error in reading Hy[1] from %s\n",
590 +             c_in_name );
591 +    return strdup( painCave.errMsg );
592 +  }
593 +  boxMat[4] = atof( foo );
594 +    
595 +  foo = strtok(NULL, " ,;\t");
596 +  if(foo == NULL){
597 +    sprintf( painCave.errMsg,
598 +             "error in reading Hy[2] from %s\n",
599 +             c_in_name );
600 +    return strdup( painCave.errMsg );
601 +  }
602 +  boxMat[5] = atof( foo );    
603 +
604 +  // get the Hz vector
605 +
606 +  foo = strtok(NULL, " ,;\t");
607 +  if(foo == NULL){
608 +    sprintf( painCave.errMsg,
609 +             "error in reading Hz[0] from %s\n",
610 +             c_in_name );
611 +    return strdup( painCave.errMsg );
612 +  }
613 +  boxMat[6] = atof( foo );
614 +  
615 +  foo = strtok(NULL, " ,;\t");
616 +  if(foo == NULL){
617 +    sprintf( painCave.errMsg,
618 +             "error in reading Hz[1] from %s\n",
619 +             c_in_name );
620 +    return strdup( painCave.errMsg );
621 +  }
622 +  boxMat[7] = atof( foo );
623 +    
624 +  foo = strtok(NULL, " ,;\t");
625 +  if(foo == NULL){
626 +    sprintf( painCave.errMsg,
627 +             "error in reading Hz[2] from %s\n",
628 +             c_in_name );
629 +    return strdup( painCave.errMsg );
630 +  }
631 +  boxMat[8] = atof( foo );    
632 +
633 +  return NULL;
634 + }
635 +
636 +
637 + #ifdef IS_MPI
638 +
639 + // a couple of functions to let us escape the read loop
640 +
641 + void initFile::nodeZeroError( void ){
642 +  int j, myStatus;
643 +  
644 +  myStatus = 0;
645 +  for (j = 0; j < mpiSim->getNumberProcessors(); j++) {      
646 +    MPI_Send( &myStatus, 1, MPI_INT, j,
647 +              TAKE_THIS_TAG_INT, MPI_COMM_WORLD);
648 +  }  
649 +  
650 +
651 +  MPI_Finalize();
652 +  exit (0);
653 +  
654 + }
655 +
656 + void initFile::anonymousNodeDie( void ){
657 +
658 +  MPI_Finalize();
659 +  exit (0);
660 + }
661 +
662 + #endif //is_mpi

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines