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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines