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

Comparing trunk/OOPSE-4/src/io/RestReader.cpp (file contents):
Revision 2868 by chrisfen, Mon Jun 19 01:36:06 2006 UTC vs.
Revision 2903 by chrisfen, Wed Jun 28 14:35:14 2006 UTC

# Line 70 | Line 70 | namespace oopse {
70    RestReader::RestReader( SimInfo* info ) : info_(info){
71      
72      idealName = "idealCrystal.in";
73 <    
74 <    isScanned = false;
75 <    
73 >    
74   #ifdef IS_MPI
75      if (worldRank == 0) {
76   #endif
77 <      
78 <      inIdealFile = fopen(idealName, "r");
79 <      if(inIdealFile == NULL){
77 >
78 >      inIdealFile = new std::ifstream(idealName.c_str());
79 >
80 >      if(inIdealFile->fail()){
81          sprintf(painCave.errMsg,
82 <                "RestReader: Cannot open file: %s\n", idealName);
82 >                "RestReader: Cannot open file: %s\n",
83 >                idealName.c_str());
84          painCave.isFatal = 1;
85          simError();
86        }
87        
88      inIdealFileName = idealName;
88   #ifdef IS_MPI
89      }
90      strcpy( checkPointMsg,
# Line 100 | Line 99 | namespace oopse {
99   #ifdef IS_MPI
100      if (worldRank == 0) {
101   #endif
102 <      int error;
103 <      error = fclose( inIdealFile );
104 <      
105 <      if( error ){
107 <        sprintf( painCave.errMsg,
108 <                 "Error closing %s\n", inIdealFileName.c_str());
109 <        simError();
110 <      }
111 <      
112 <      MemoryUtils::deletePointers(framePos);
113 <      
102 >
103 >      delete inIdealFile;
104 >      delete inAngFile;
105 >
106   #ifdef IS_MPI
107      }
108 <    strcpy( checkPointMsg, "Restraint file closed successfully." );
108 >    strcpy( checkPointMsg,
109 >            "File idealCrystal.in (and .zang0 if present) closed successfully." );
110      MPIcheckPoint();
111   #endif
112      
# Line 123 | Line 116 | namespace oopse {
116    
117    void RestReader :: readIdealCrystal(){
118          
126    int i;
127    unsigned int j;
128
119   #ifdef IS_MPI
120 <    int done, which_node, which_atom; // loop counter
120 >    int which_node;
121 >    int i, j;
122   #endif //is_mpi
123      
124      const int BUFFERSIZE = 2000; // size of the read buffer
125      int nTotObjs; // the number of atoms
126      char read_buffer[BUFFERSIZE]; //the line buffer for reading
127      
137    char *eof_test; // ptr to see when we reach the end of the file
128      char *parseErr;
129      
130      std::vector<StuntDouble*> integrableObjects;
# Line 146 | Line 136 | namespace oopse {
136      
137   #ifndef IS_MPI
138      
139 <    eof_test = fgets(read_buffer, sizeof(read_buffer), inIdealFile);
140 <    if( eof_test == NULL ){
139 >    inIdealFile->getline(read_buffer, sizeof(read_buffer));
140 >
141 >    if( inIdealFile->eof() ){
142        sprintf( painCave.errMsg,
143                 "RestraintReader error: error reading 1st line of \"%s\"\n",
144 <               inIdealFileName.c_str() );
144 >               idealName.c_str() );
145        painCave.isFatal = 1;
146        simError();
147      }
# Line 161 | Line 152 | namespace oopse {
152        sprintf( painCave.errMsg,
153                 "RestraintReader error. %s nIntegrable, %d, "
154                 "does not match the meta-data file's nIntegrable, %d.\n",
155 <               inIdealFileName.c_str(), nTotObjs,
155 >               idealName.c_str(),
156 >               nTotObjs,
157                 info_->getNGlobalIntegrableObjects());
158        painCave.isFatal = 1;
159        simError();
160      }
161      
162      // skip over the comment line
163 <    eof_test = fgets(read_buffer, sizeof(read_buffer), inIdealFile);
164 <    if(eof_test == NULL){
163 >    inIdealFile->getline(read_buffer, sizeof(read_buffer));
164 >
165 >    if( inIdealFile->eof() ){
166        sprintf( painCave.errMsg,
167 <               "error in reading commment in %s\n", inIdealFileName.c_str());
167 >               "error in reading commment in %s\n",
168 >               idealName.c_str());
169        painCave.isFatal = 1;
170        simError();
171      }
# Line 188 | Line 182 | namespace oopse {
182        for (integrableObject = mol->beginIntegrableObject(ii);
183             integrableObject != NULL;
184             integrableObject = mol->nextIntegrableObject(ii)) {    
185 <        
186 <        eof_test = fgets(read_buffer, sizeof(read_buffer), inIdealFile);
187 <        if(eof_test == NULL){
185 >
186 >        inIdealFile->getline(read_buffer, sizeof(read_buffer));
187 >
188 >        if( inIdealFile->eof() ){
189            sprintf(painCave.errMsg,
190                    "RestReader Error: error in reading file %s\n"
191                    "natoms  = %d; index = %d\n"
192                    "error reading the line from the file.\n",
193 <                  inIdealFileName.c_str(), nTotObjs,
193 >                  idealName.c_str(), nTotObjs,
194                    integrableObject->getGlobalIndex() );
195            painCave.isFatal = 1;
196            simError();
# Line 217 | Line 212 | namespace oopse {
212      painCave.isEventLoop = 1;
213      
214      int masterNode = 0;
220    int myStatus; // 1 = wakeup & success; 0 = error; -1 = AllDone
215      
216      MPI_Status istatus;
217      int nCurObj;
# Line 228 | Line 222 | namespace oopse {
222      haveError = 0;
223  
224      if (worldRank == masterNode) {
225 <      eof_test = fgets(read_buffer, sizeof(read_buffer), inIdealFile);
226 <      if( eof_test == NULL ){
225 >      inIdealFile->getline(read_buffer, sizeof(read_buffer));
226 >
227 >      if( inIdealFile->eof() ){
228          sprintf( painCave.errMsg,
229 <                 "Error reading 1st line of %s \n ",inIdealFileName.c_str());
229 >                 "Error reading 1st line of %s \n ",idealName.c_str());
230          painCave.isFatal = 1;
231          simError();
232        }
# Line 245 | Line 240 | namespace oopse {
240          sprintf( painCave.errMsg,
241                   "RestraintReader Error. %s nIntegrable, %d, "
242                   "does not match the meta-data file's nIntegrable, %d.\n",
243 <                 inIdealFileName.c_str(), nTotObjs,
243 >                 idealName.c_str(), nTotObjs,
244                   info_->getNGlobalIntegrableObjects());
245          painCave.isFatal = 1;
246          simError();
247        }
248        
249        // skip over the comment line
250 <      eof_test = fgets(read_buffer, sizeof(read_buffer), inIdealFile);
251 <      if(eof_test == NULL){
250 >      inIdealFile->getline(read_buffer, sizeof(read_buffer));
251 >
252 >      if( inIdealFile->eof() ){
253          sprintf( painCave.errMsg,
254 <                 "error in reading commment in %s\n", inIdealFileName.c_str());
254 >                 "error in reading commment in %s\n", idealName.c_str());
255          painCave.isFatal = 1;
256          simError();
257        }
# Line 263 | Line 259 | namespace oopse {
259        MPI_Bcast(read_buffer, BUFFERSIZE, MPI_CHAR, masterNode, MPI_COMM_WORLD);
260  
261        for (i=0 ; i < info_->getNGlobalMolecules(); i++) {
262 <        int which_node = info_->getMolToProc(i);
262 >        which_node = info_->getMolToProc(i);
263          
264          if(which_node == masterNode){
265            //molecules belong to master node
# Line 282 | Line 278 | namespace oopse {
278                 integrableObject != NULL;
279                 integrableObject = mol->nextIntegrableObject(ii)){
280              
281 <            eof_test = fgets(read_buffer, sizeof(read_buffer), inIdealFile);
281 >            inIdealFile->getline(read_buffer, sizeof(read_buffer));
282              
283 <            if(eof_test == NULL){
283 >            if( inIdealFile->eof() ){
284                sprintf(painCave.errMsg,
285                        "RestReader Error: error in reading file %s\n"
286                        "natoms  = %d; index = %d\n"
287                        "error reading the line from the file.\n",
288 <                      inIdealFileName.c_str(), nTotObjs, i );
288 >                      idealName.c_str(), nTotObjs, i );
289                painCave.isFatal = 1;
290                simError();
291              }
# Line 305 | Line 301 | namespace oopse {
301                     TAKE_THIS_TAG_INT, MPI_COMM_WORLD, &istatus);
302            
303            for(j = 0; j < nCurObj; j++){
304 <            
305 <            eof_test = fgets(read_buffer, sizeof(read_buffer), inIdealFile);
306 <            if(eof_test == NULL){
304 >            inIdealFile->getline(read_buffer, sizeof(read_buffer));
305 >
306 >            if( inIdealFile->eof() ){
307                sprintf(painCave.errMsg,
308                        "RestReader Error: error in reading file %s\n"
309                        "natoms  = %d; index = %d\n"
310                        "error reading the line from the file.\n",
311 <                      inIdealFileName.c_str(), nTotObjs, i );
311 >                      idealName.c_str(), nTotObjs, i );
312                painCave.isFatal = 1;
313                simError();
314              }
# Line 394 | Line 390 | namespace oopse {
390        sprintf(painCave.errMsg,
391                "RestReader Error: Atom type [%s] in %s does not "
392                "match Atom Type [%s] in .md file.\n",
393 <              name.c_str(), inIdealFileName.c_str(),
393 >              name.c_str(), idealName.c_str(),
394                sd->getType().c_str());
395        painCave.isFatal = 1;
396        simError();        
# Line 473 | Line 469 | namespace oopse {
469    void RestReader::readZangle(){
470      
471      int i;
476    unsigned int j;
472      int isPresent;
473      
474      Molecule* mol;
# Line 482 | Line 477 | namespace oopse {
477      Molecule::IntegrableObjectIterator ii;
478      
479   #ifdef IS_MPI
480 <    int done, which_node, which_atom; // loop counter
486 <    int nProc;
480 >    int which_node;
481      MPI_Status istatus;
482   #endif //is_mpi
483      
484      const int BUFFERSIZE = 2000; // size of the read buffer
485 <    int nTotObjs; // the number of atoms
485 >    unsigned int nTotObjs; // the number of atoms
486      char read_buffer[BUFFERSIZE]; //the line buffer for reading
487      
494    char *eof_test; // ptr to see when we reach the end of the file
495    char *parseErr;
496    
488      std::vector<StuntDouble*> vecParticles;
489      std::vector<RealType> tempZangs;
490        
491 <    inAngFileName = info_->getRestFileName();
491 >    angFile = info_->getRestFileName();
492      
493 <    inAngFileName += "0";
493 >    angFile += "0";
494      
495      // open the omega value file for reading
496   #ifdef IS_MPI
497      if (worldRank == 0) {
498   #endif
499        isPresent = 1;
500 <      inAngFile = fopen(inAngFileName.c_str(), "r");
501 <      if(!inAngFile){
500 >      
501 >      inAngFile = new std::ifstream(angFile.c_str());
502 >      
503 >      if(inAngFile->fail()){
504          sprintf(painCave.errMsg,
505                  "Restraints Warning: %s file is not present\n"
506                  "\tAll omega values will be initialized to zero. If the\n"
507                  "\tsimulation is starting from the idealCrystal.in reference\n"
508                  "\tconfiguration, this is the desired action. If this is not\n"
509                  "\tthe case, the energy calculations will be incorrect.\n",
510 <                inAngFileName.c_str());
510 >                angFile.c_str());
511          painCave.severity = OOPSE_WARNING;
512          painCave.isFatal = 0;
513          simError();  
# Line 532 | Line 525 | namespace oopse {
525        }
526        
527   #ifdef IS_MPI
528 +      if (!isPresent) {
529 +        // master node zeroes out its zAngles if .zang0 isn't present
530 +        zeroZangle();
531 +        return;
532 +      }
533 +
534      }
535      
536      // listen to node 0 to see if we should exit this function
# Line 549 | Line 548 | namespace oopse {
548      
549   #ifndef IS_MPI
550      
551 <    eof_test = fgets(read_buffer, sizeof(read_buffer), inAngFile);
552 <    if( eof_test == NULL ){
551 >    // read the first line and die if there is a failure
552 >    inAngFile->getline(read_buffer, sizeof(read_buffer));
553 >
554 >    if( inAngFile->eof() ){
555        sprintf( painCave.errMsg,
556                 "RestraintReader error: error reading 1st line of \"%s\"\n",
557 <               inAngFileName.c_str() );
557 >               angFile.c_str() );
558        painCave.isFatal = 1;
559        simError();
560      }
561 +
562 +    // read the file and load the values into a vector
563 +    inAngFile->getline(read_buffer, sizeof(read_buffer));
564      
565 <    eof_test = fgets(read_buffer, sizeof(read_buffer), inAngFile);
562 <    while ( eof_test != NULL ) {
565 >    while ( !inAngFile->eof() ) {
566        // check for and ignore blank lines
567        if ( read_buffer != NULL )
568          tempZangs.push_back( atof(read_buffer) );
569 <      eof_test = fgets(read_buffer, sizeof(read_buffer), inAngFile);
569 >
570 >      inAngFile->getline(read_buffer, sizeof(read_buffer));
571      }
572      
573      nTotObjs = info_->getNGlobalIntegrableObjects();
# Line 571 | Line 575 | namespace oopse {
575      if( nTotObjs != tempZangs.size() ){
576        sprintf( painCave.errMsg,
577                 "RestraintReader zAngle reading error. %s nIntegrable, %d, "
578 <               "does not match the meta-data file's nIntegrable, %d.\n",
579 <               inAngFileName.c_str(), tempZangs.size(), nTotObjs );
578 >               "does not match the meta-data file's nIntegrable, %i.\n",
579 >               angFile.c_str(),
580 >               tempZangs.size(),
581 >               nTotObjs );
582        painCave.isFatal = 1;
583        simError();
584      }
# Line 598 | Line 604 | namespace oopse {
604      painCave.isEventLoop = 1;
605  
606      int masterNode = 0;
607 <    int myStatus; // 1 = wakeup & success; 0 = error; -1 = AllDone
607 >
608      int haveError;
609      int intObjIndex;    
610      int intObjIndexTransfer;    
611  
612 +    int j;
613      int nCurObj;
614 <    RealType angleTranfer;
614 >    RealType angleTransfer;
615      
616      nTotObjs = info_->getNGlobalIntegrableObjects();
617      haveError = 0;
618  
619      if (worldRank == masterNode) {
620        
621 <      eof_test = fgets(read_buffer, sizeof(read_buffer), inAngFile);
622 <      if( eof_test == NULL ){
621 >      inAngFile->getline(read_buffer, sizeof(read_buffer));
622 >
623 >      if( inAngFile->eof() ){
624          sprintf( painCave.errMsg,
625 <                 "Error reading 1st line of %s \n ",inAngFileName.c_str());
625 >                 "Error reading 1st line of %s \n ",angFile.c_str());
626          haveError = 1;
627          simError();
628        }
629        
630 <      // let node 0 load the temporary angle vector
631 <      eof_test = fgets(read_buffer, sizeof(read_buffer), inAngFile);
632 <      while ( eof_test != NULL ) {
630 >      // let the master node read the file and load the temporary angle vector
631 >      inAngFile->getline(read_buffer, sizeof(read_buffer));
632 >
633 >      while ( !inAngFile->eof() ) {
634          // check for and ignore blank lines
635          if ( read_buffer != NULL )
636            tempZangs.push_back( atof(read_buffer) );
637 <        eof_test = fgets(read_buffer, sizeof(read_buffer), inAngFile);
637 >
638 >        inAngFile->getline(read_buffer, sizeof(read_buffer));
639        }
640  
641        // Check to see that the number of integrable objects in the
# Line 635 | Line 645 | namespace oopse {
645          sprintf( painCave.errMsg,
646                   "RestraintReader zAngle reading Error. %s nIntegrable, %d, "
647                   "does not match the meta-data file's nIntegrable, %d.\n",
648 <                 inAngFileName.c_str(), tempZangs.size(), nTotObjs);
648 >                 angFile.c_str(),
649 >                 tempZangs.size(),
650 >                 nTotObjs);
651          haveError= 1;
652          simError();
653        }
# Line 683 | Line 695 | namespace oopse {
695      } else {
696        // I am SLAVE TO THE MASTER
697        for (i=0 ; i < info_->getNGlobalMolecules(); i++) {
698 <        int which_node = info_->getMolToProc(i);
698 >        which_node = info_->getMolToProc(i);
699  
700          if (which_node == worldRank) {
701            
# Line 725 | Line 737 | namespace oopse {
737    
738    void RestReader :: zeroZangle(){
739      
728    int i;
729    unsigned int j;
730    int nTotObjs; // the number of atoms
731    
740      Molecule* mol;
741      StuntDouble* integrableObject;
742      SimInfo::MoleculeIterator mi;
743      Molecule::IntegrableObjectIterator ii;
744 <    
737 <    std::vector<StuntDouble*> vecParticles;
738 <    
744 >    
745   #ifndef IS_MPI
746      // set all zAngles to 0.0
747      for (mol = info_->beginMolecule(mi); mol != NULL;
# Line 753 | Line 759 | namespace oopse {
759      // first thing first, suspend fatalities.
760      painCave.isEventLoop = 1;
761      
756    int masterNode = 0;
762      int myStatus; // 1 = wakeup & success; 0 = error; -1 = AllDone
763      int haveError;
764      int which_node;
765      
761    MPI_Status istatus;
762    
763    int nCurObj;
764    RealType angleTranfer;
765    
766    nTotObjs = info_->getNGlobalIntegrableObjects();
766      haveError = 0;
768    if (worldRank == masterNode) {
767  
768 <      for (i=0 ; i < info_->getNGlobalMolecules(); i++) {
769 <        // Get the Node number which has this atom
770 <        which_node = info_->getMolToProc(i);
768 >    for (int i=0 ; i < info_->getNGlobalMolecules(); i++) {
769 >
770 >      // Get the Node number which has this atom
771 >      which_node = info_->getMolToProc(i);
772          
773 <        // let's let node 0 pass out constant values to all the processors
774 <        if (worldRank == masterNode) {
775 <          mol = info_->getMoleculeByGlobalIndex(i);
776 <          
777 <          if(mol == NULL) {
778 <            strcpy(painCave.errMsg, "Molecule not found on node 0!");
779 <            haveError = 1;
780 <            simError();
781 <          }
782 <          
784 <          for (integrableObject = mol->beginIntegrableObject(ii);
785 <               integrableObject != NULL;
786 <               integrableObject = mol->nextIntegrableObject(ii)){
787 <            
788 <            integrableObject->setZangle( 0.0 );
789 <            
790 <          }
791 <          
792 <        } else {
793 <          // I am MASTER OF THE UNIVERSE, but I don't own this molecule
794 <          
795 <          MPI_Recv(&nCurObj, 1, MPI_INT, which_node,
796 <                   TAKE_THIS_TAG_INT, MPI_COMM_WORLD, &istatus);
797 <          
798 <          for(j=0; j < nCurObj; j++){            
799 <            angleTransfer = 0.0;
800 <            MPI_Send(&angleTransfer, 1, MPI_REALTYPE, which_node,
801 <                     TAKE_THIS_TAG_DOUBLE, MPI_COMM_WORLD);              
802 <            
803 <          }
773 >      // each processor zeroes its own integrable objects
774 >      if (which_node == worldRank) {
775 >        mol = info_->getMoleculeByGlobalIndex(i);
776 >        
777 >        if(mol == NULL) {
778 >          sprintf( painCave.errMsg,
779 >                  "Molecule not found on node %i!",
780 >                  which_node );
781 >          haveError = 1;
782 >          simError();
783          }
805      }
806    } else {
807      // I am SLAVE TO THE MASTER
808      for (i=0 ; i < info_->getNGlobalMolecules(); i++) {
809        int which_node = info_->getMolToProc(i);
784          
785 <        if (which_node == worldRank) {
785 >        for (integrableObject = mol->beginIntegrableObject(ii);
786 >             integrableObject != NULL;
787 >             integrableObject = mol->nextIntegrableObject(ii)){
788            
789 <          // BUT I OWN THIS MOLECULE!!!
814 <          mol = info_->getMoleculeByGlobalIndex(i);
789 >          integrableObject->setZangle( 0.0 );
790            
791 <          if(mol == NULL) {
817 <            sprintf(painCave.errMsg,
818 <                    "RestReader Error: molecule not found on node %d\n",
819 <                    worldRank);
820 <            painCave.isFatal = 1;
821 <            simError();
822 <          }
823 <          
824 <          nCurObj = mol->getNIntegrableObjects();
825 <          
826 <          MPI_Send(&nCurObj, 1, MPI_INT, 0,
827 <                   TAKE_THIS_TAG_INT, MPI_COMM_WORLD);
828 <          
829 <          for (integrableObject = mol->beginIntegrableObject(ii);
830 <               integrableObject != NULL;
831 <               integrableObject = mol->nextIntegrableObject(ii)){
832 <            
833 <            MPI_Recv(&angleTransfer, 1, MPI_REALTYPE, 0,
834 <                     TAKE_THIS_TAG_DOUBLE, MPI_COMM_WORLD, &istatus);
835 <            vecParticles[j]->setZangle(angleTransfer);
836 <          }    
837 <        }
791 >        }
792        }
793      }
794 +
795   #endif
796    }
797    

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines