ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/OpenMD/branches/development/src/io/RestReader.cpp
(Generate patch)

Comparing trunk/src/io/RestReader.cpp (file contents):
Revision 423 by chrisfen, Thu Mar 10 19:11:02 2005 UTC vs.
Revision 1360 by cli2, Mon Sep 7 16:31:51 2009 UTC

# Line 1 | Line 1
1 < /*
2 < * Copyright (c) 2005 The University of Notre Dame. All Rights Reserved.
3 < *
4 < * The University of Notre Dame grants you ("Licensee") a
5 < * non-exclusive, royalty free, license to use, modify and
6 < * redistribute this software in source and binary code form, provided
7 < * that the following conditions are met:
8 < *
9 < * 1. Acknowledgement of the program authors must be made in any
10 < *    publication of scientific results based in part on use of the
11 < *    program.  An acceptable form of acknowledgement is citation of
12 < *    the article in which the program was described (Matthew
13 < *    A. Meineke, Charles F. Vardeman II, Teng Lin, Christopher
14 < *    J. Fennell and J. Daniel Gezelter, "OOPSE: An Object-Oriented
15 < *    Parallel Simulation Engine for Molecular Dynamics,"
16 < *    J. Comput. Chem. 26, pp. 252-271 (2005))
17 < *
18 < * 2. Redistributions of source code must retain the above copyright
19 < *    notice, this list of conditions and the following disclaimer.
20 < *
21 < * 3. Redistributions in binary form must reproduce the above copyright
22 < *    notice, this list of conditions and the following disclaimer in the
23 < *    documentation and/or other materials provided with the
24 < *    distribution.
25 < *
26 < * This software is provided "AS IS," without a warranty of any
27 < * kind. All express or implied conditions, representations and
28 < * warranties, including any implied warranty of merchantability,
29 < * fitness for a particular purpose or non-infringement, are hereby
30 < * excluded.  The University of Notre Dame and its licensors shall not
31 < * be liable for any damages suffered by licensee as a result of
32 < * using, modifying or distributing the software or its
33 < * derivatives. In no event will the University of Notre Dame or its
34 < * licensors be liable for any lost revenue, profit or data, or for
35 < * direct, indirect, special, consequential, incidental or punitive
36 < * damages, however caused and regardless of the theory of liability,
37 < * arising out of the use of or inability to use software, even if the
38 < * University of Notre Dame has been advised of the possibility of
39 < * such damages.
40 < */
1 > /*
2 > * Copyright (c) 2005 The University of Notre Dame. All Rights Reserved.
3 > *
4 > * The University of Notre Dame grants you ("Licensee") a
5 > * non-exclusive, royalty free, license to use, modify and
6 > * redistribute this software in source and binary code form, provided
7 > * that the following conditions are met:
8 > *
9 > * 1. Acknowledgement of the program authors must be made in any
10 > *    publication of scientific results based in part on use of the
11 > *    program.  An acceptable form of acknowledgement is citation of
12 > *    the article in which the program was described (Matthew
13 > *    A. Meineke, Charles F. Vardeman II, Teng Lin, Christopher
14 > *    J. Fennell and J. Daniel Gezelter, "OOPSE: An Object-Oriented
15 > *    Parallel Simulation Engine for Molecular Dynamics,"
16 > *    J. Comput. Chem. 26, pp. 252-271 (2005))
17 > *
18 > * 2. Redistributions of source code must retain the above copyright
19 > *    notice, this list of conditions and the following disclaimer.
20 > *
21 > * 3. Redistributions in binary form must reproduce the above copyright
22 > *    notice, this list of conditions and the following disclaimer in the
23 > *    documentation and/or other materials provided with the
24 > *    distribution.
25 > *
26 > * This software is provided "AS IS," without a warranty of any
27 > * kind. All express or implied conditions, representations and
28 > * warranties, including any implied warranty of merchantability,
29 > * fitness for a particular purpose or non-infringement, are hereby
30 > * excluded.  The University of Notre Dame and its licensors shall not
31 > * be liable for any damages suffered by licensee as a result of
32 > * using, modifying or distributing the software or its
33 > * derivatives. In no event will the University of Notre Dame or its
34 > * licensors be liable for any lost revenue, profit or data, or for
35 > * direct, indirect, special, consequential, incidental or punitive
36 > * damages, however caused and regardless of the theory of liability,
37 > * arising out of the use of or inability to use software, even if the
38 > * University of Notre Dame has been advised of the possibility of
39 > * such damages.
40 > */
41 >  
42 > #include "io/DumpReader.hpp"
43 > #include "io/RestReader.hpp"
44 > #include "primitives/Molecule.hpp"
45 > #include "restraints/ObjectRestraint.hpp"
46 > #include "restraints/MolecularRestraint.hpp"
47  
48 < #define _LARGEFILE_SOURCE64
49 < #define _FILE_OFFSET_BITS 64
48 > namespace oopse {
49 >      
50 >  void RestReader::readReferenceStructure() {
51  
52 < #include <sys/types.h>
46 < #include <sys/stat.h>
52 >    // some of this comes directly from DumpReader.
53  
54 < #include <iostream>
55 < #include <math.h>
54 >    if (!isScanned_)
55 >      scanFile();
56 >        
57 >    int storageLayout = info_->getSnapshotManager()->getStorageLayout();
58 >    
59 >    if (storageLayout & DataStorage::dslPosition) {
60 >      needPos_ = true;
61 >    } else {
62 >      needPos_ = false;
63 >    }
64 >    
65 >    needVel_ = false;
66 >    
67 >    if (storageLayout & DataStorage::dslAmat || storageLayout & DataStorage::dslElectroFrame) {
68 >      needQuaternion_ = true;
69 >    } else {
70 >      needQuaternion_ = false;
71 >    }
72  
73 < #include <stdio.h>
52 < #include <stdlib.h>
53 < #include <string.h>
73 >    needAngMom_ = false;
74  
75 < #include "primitives/Molecule.hpp"
76 < #include "utils/MemoryUtils.hpp"
77 < #include "utils/StringTokenizer.hpp"
58 < #include "io/RestReader.hpp"
59 < #include "utils/simError.h"
75 >    // We need temporary storage to keep track of all StuntDouble positions
76 >    // in case some of the restraints are molecular (i.e. if they use
77 >    // multiple SD positions to determine restrained orientations or positions:
78  
79 < #ifdef IS_MPI
80 < #include <mpi.h>
63 < #define TAKE_THIS_TAG_CHAR 0
64 < #define TAKE_THIS_TAG_INT 1
65 < #define TAKE_THIS_TAG_DOUBLE 2
66 < #endif // is_mpi
79 >    all_pos_.clear();
80 >    all_pos_.resize(info_->getNGlobalIntegrableObjects() );
81  
82 < namespace oopse {
83 <  
84 <  RestReader::RestReader( SimInfo* info ) : info_(info){
85 <        
86 <    idealName = "idealCrystal.in";
82 >
83 >    // Restraint files are just standard dump files, but with the reference
84 >    // structure stored in the first frame (frame 0).
85 >    // RestReader overloads readSet and explicitly handles all of the
86 >    // ObjectRestraints in that method:
87 >
88 >    readSet(0);
89      
90 <    isScanned = false;
91 <    
92 < #ifdef IS_MPI
93 <    if (worldRank == 0) {
94 < #endif
79 <      
80 <      inIdealFile = fopen(idealName, "r");
81 <      if(inIdealFile == NULL){
82 <        sprintf(painCave.errMsg,
83 <                "RestReader: Cannot open file: %s\n", idealName);
84 <        painCave.isFatal = 1;
85 <        simError();
86 <      }
87 <      
88 <      inIdealFileName = idealName;
89 < #ifdef IS_MPI
90 <    }
91 <    strcpy( checkPointMsg,
92 <            "File \"idealCrystal.in\" opened successfully for reading." );
93 <    MPIcheckPoint();
94 < #endif
95 <    return;  
96 <  }
97 <  
98 <  RestReader :: ~RestReader( ){
99 < #ifdef IS_MPI
100 <    if (worldRank == 0) {
101 < #endif
102 <      int error;
103 <      error = fclose( inIdealFile );
104 <      
105 <      if( error ){
106 <        sprintf( painCave.errMsg,
107 <                 "Error closing %s\n", inIdealFileName.c_str());
108 <        simError();
109 <      }
110 <      
111 <      MemoryUtils::deletePointers(framePos);
112 <      
113 < #ifdef IS_MPI
114 <    }
115 <    strcpy( checkPointMsg, "Restraint file closed successfully." );
116 <    MPIcheckPoint();
117 < #endif
118 <    
119 <    return;
120 <  }
121 <  
122 <  
123 <  void RestReader :: readIdealCrystal(){
124 <    
125 <    int i;
126 <    unsigned int j;
127 <    
128 < #ifdef IS_MPI
129 <    int done, which_node, which_atom; // loop counter
130 < #endif //is_mpi
131 <    
132 <    const int BUFFERSIZE = 2000; // size of the read buffer
133 <    int nTotObjs; // the number of atoms
134 <    char read_buffer[BUFFERSIZE]; //the line buffer for reading
135 <    
136 <    char *eof_test; // ptr to see when we reach the end of the file
137 <    char *parseErr;
138 <    
139 <    std::vector<StuntDouble*> integrableObjects;
140 <    
90 >    // all ObjectRestraints have been handled, now we have to worry about
91 >    // molecular restraints:
92 >
93 >    SimInfo::MoleculeIterator i;
94 >    Molecule::IntegrableObjectIterator j;
95      Molecule* mol;
96 <    StuntDouble* integrableObject;
97 <    SimInfo::MoleculeIterator mi;
98 <    Molecule::IntegrableObjectIterator ii;
99 <    
100 < #ifndef IS_MPI
101 <    
102 <    eof_test = fgets(read_buffer, sizeof(read_buffer), inIdealFile);
103 <    if( eof_test == NULL ){
104 <      sprintf( painCave.errMsg,
105 <               "RestraintReader error: error reading 1st line of \"%s\"\n",
106 <               inIdealFileName.c_str() );
153 <      painCave.isFatal = 1;
154 <      simError();
155 <    }
156 <    
157 <    nTotObjs = atoi( read_buffer );
158 <    
159 <    if( nTotObjs != info_->getNGlobalIntegrableObjects() ){
160 <      sprintf( painCave.errMsg,
161 <               "RestraintReader error. %s nIntegrable, %d, "
162 <               "does not match the meta-data file's nIntegrable, %d.\n",
163 <               inIdealFileName.c_str(), nTotObjs,
164 <               info_->getNGlobalIntegrableObjects());
165 <      painCave.isFatal = 1;
166 <      simError();
167 <    }
168 <    
169 <    // skip over the comment line
170 <    eof_test = fgets(read_buffer, sizeof(read_buffer), inIdealFile);
171 <    if(eof_test == NULL){
172 <      sprintf( painCave.errMsg,
173 <               "error in reading commment in %s\n", inIdealFileName.c_str());
174 <      painCave.isFatal = 1;
175 <      simError();
176 <    }
177 <    
178 <    // parse the ideal crystal lines
179 <    /*
180 <     * Note: we assume that there is a one-to-one correspondence between
181 <     * integrable objects and lines in the idealCrystal.in file.  Thermodynamic
182 <     * integration is only supported for simple rigid bodies.
183 <     */
184 <    for (mol = info_->beginMolecule(mi); mol != NULL;
185 <         mol = info_->nextMolecule(mi)) {
96 >    StuntDouble* sd;
97 >
98 >    // no need to worry about parallel molecules, as molecules are not
99 >    // split across processor boundaries.  Just loop over all molecules
100 >    // we know about:
101 >
102 >    for (mol = info_->beginMolecule(i); mol != NULL;
103 >         mol = info_->nextMolecule(i)) {          
104 >
105 >      // is this molecule restrained?    
106 >      GenericData* data = mol->getPropertyByName("Restraint");
107        
108 <      for (integrableObject = mol->beginIntegrableObject(ii);
188 <           integrableObject != NULL;
189 <           integrableObject = mol->nextIntegrableObject(ii)) {    
190 <        
191 <        eof_test = fgets(read_buffer, sizeof(read_buffer), inIdealFile);
192 <        if(eof_test == NULL){
193 <          sprintf(painCave.errMsg,
194 <                  "RestReader Error: error in reading file %s\n"
195 <                  "natoms  = %d; index = %d\n"
196 <                  "error reading the line from the file.\n",
197 <                  inIdealFileName.c_str(), nTotObjs,
198 <                  integrableObject->getGlobalIndex() );
199 <          painCave.isFatal = 1;
200 <          simError();
201 <        }
202 <        
203 <        parseErr = parseIdealLine( read_buffer, integrableObject);
204 <        if( parseErr != NULL ){
205 <          strcpy( painCave.errMsg, parseErr );
206 <          painCave.isFatal = 1;
207 <          simError();
208 <        }
209 <      }
210 <    }
211 <    
212 <    // MPI Section of code..........
213 < #else //IS_MPI
214 <    
215 <    // first thing first, suspend fatalities.
216 <    painCave.isEventLoop = 1;
217 <    
218 <    int masterNode = 0;
219 <    int myStatus; // 1 = wakeup & success; 0 = error; -1 = AllDone
220 <    
221 <    MPI_Status istatus;
222 <    int nCurObj;
223 <    int nitems;
224 <    int haveError;
108 >      if (data != NULL) {
109  
110 <    nTotObjs = info_->getNGlobalIntegrableObjects();
111 <    haveError = 0;
112 <    
113 <    if (worldRank == masterNode) {
114 <      eof_test = fgets(read_buffer, sizeof(read_buffer), inIdealFile);
115 <      if( eof_test == NULL ){
116 <        sprintf( painCave.errMsg,
117 <                 "Error reading 1st line of %s \n ",inIdealFileName.c_str());
118 <        painCave.isFatal = 1;
119 <        simError();
120 <      }
121 <      
122 <      nitems = atoi( read_buffer );
123 <      
124 <      // Check to see that the number of integrable objects in the
125 <      // intial configuration file is the same as derived from the
126 <      // meta-data file.
127 <      if( nTotObjs != nitems){
128 <        sprintf( painCave.errMsg,
129 <                 "RestraintReader Error. %s nIntegrable, %d, "
246 <                 "does not match the meta-data file's nIntegrable, %d.\n",
247 <                 inIdealFileName.c_str(), nTotObjs,
248 <                 info_->getNGlobalIntegrableObjects());
249 <        painCave.isFatal = 1;
250 <        simError();
251 <      }
252 <      
253 <      // skip over the comment line
254 <      eof_test = fgets(read_buffer, sizeof(read_buffer), inIdealFile);
255 <      if(eof_test == NULL){
256 <        sprintf( painCave.errMsg,
257 <                 "error in reading commment in %s\n", inIdealFileName.c_str());
258 <        painCave.isFatal = 1;
259 <        simError();
260 <      }
261 <      
262 <      for (i=0 ; i < info_->getNGlobalMolecules(); i++) {
263 <        int which_node = info_->getMolToProc(i);
264 <        
265 <        if(which_node == masterNode){
266 <          //molecules belong to master node
267 <          
268 <          mol = info_->getMoleculeByGlobalIndex(i);
269 <          
270 <          if(mol == NULL) {
271 <            sprintf(painCave.errMsg,
272 <                   "RestReader Error: Molecule not found on node %d!\n",
273 <                   worldRank);
274 <            painCave.isFatal = 1;
275 <            simError();
276 <          }
277 <          
278 <          for (integrableObject = mol->beginIntegrableObject(ii);
279 <               integrableObject != NULL;
280 <               integrableObject = mol->nextIntegrableObject(ii)){
110 >        // make sure we can reinterpret the generic data as restraint data:
111 >
112 >        RestraintData* restData= dynamic_cast<RestraintData*>(data);        
113 >
114 >        if (restData != NULL) {
115 >
116 >          // make sure we can reinterpet the restraint data as a
117 >          // pointer to a MolecularRestraint:
118 >
119 >          MolecularRestraint* mRest = dynamic_cast<MolecularRestraint*>(restData->getData());
120 >
121 >          if (mRest != NULL) {          
122 >
123 >            // now we need to pack the stunt doubles for the reference
124 >            // structure:
125 >
126 >            std::vector<Vector3d> ref;
127 >            int count = 0;
128 >            RealType mass, mTot;
129 >            Vector3d COM(0.0);
130              
131 <            eof_test = fgets(read_buffer, sizeof(read_buffer), inIdealFile);
131 >            mTot = 0.0;
132              
133 <            if(eof_test == NULL){
134 <              sprintf(painCave.errMsg,
286 <                      "RestReader Error: error in reading file %s\n"
287 <                      "natoms  = %d; index = %d\n"
288 <                      "error reading the line from the file.\n",
289 <                      inIdealFileName.c_str(), nTotObjs, i );
290 <              painCave.isFatal = 1;
291 <              simError();
292 <            }
133 >            // loop over the stunt doubles in this molecule in the order we
134 >            // will be looping them in the restraint code:
135              
136 <            parseIdealLine(read_buffer, integrableObjects[j]);
137 <          }
138 <        } else {
139 <          //molecule belongs to slave nodes
140 <          
141 <          MPI_Recv(&nCurObj, 1, MPI_INT, which_node,
142 <                   TAKE_THIS_TAG_INT, MPI_COMM_WORLD, &istatus);
143 <          
144 <          for(j=0; j < nCurObj; j++){
145 <            
146 <            eof_test = fgets(read_buffer, sizeof(read_buffer), inIdealFile);
147 <            if(eof_test == NULL){
148 <              sprintf(painCave.errMsg,
149 <                      "RestReader Error: error in reading file %s\n"
308 <                      "natoms  = %d; index = %d\n"
309 <                      "error reading the line from the file.\n",
310 <                      inIdealFileName.c_str(), nTotObjs, i );
311 <              painCave.isFatal = 1;
312 <              simError();
136 >            for (sd = mol->beginIntegrableObject(j); sd != NULL;
137 >                 sd = mol->nextIntegrableObject(j)) {
138 >              
139 >              // push back the reference positions of the stunt
140 >              // doubles from the *globally* sorted array of
141 >              // positions:
142 >              
143 >              ref.push_back( all_pos_[sd->getGlobalIndex()] );
144 >              
145 >              mass = sd->getMass();
146 >              
147 >              COM = COM + mass * ref[count];
148 >              mTot = mTot + mass;
149 >              count = count + 1;
150              }
151              
152 <            MPI_Send(read_buffer, BUFFERSIZE, MPI_CHAR, which_node,
153 <                     TAKE_THIS_TAG_CHAR, MPI_COMM_WORLD);
152 >            COM /= mTot;
153 >
154 >            mRest->setReferenceStructure(ref, COM);        
155 >
156            }
157          }
158        }
159 <    } else {
160 <      //actions taken at slave nodes
161 <      for (i=0 ; i < info_->getNGlobalMolecules(); i++) {
162 <        int which_node = info_->getMolToProc(i);
163 <        
164 <        if(which_node == worldRank){
165 <          //molecule with global index i belongs to this processor
159 >    }
160 >  }
161 >
162 >  
163 >  
164 >  void RestReader::parseDumpLine(const std::string& line) {        
165 >    StringTokenizer tokenizer(line);
166 >    int nTokens;
167 >    
168 >    nTokens = tokenizer.countTokens();
169 >    
170 >    if (nTokens < 2) {
171 >      sprintf(painCave.errMsg,
172 >              "DumpReader Error: Not enough Tokens.\n%s\n", line.c_str());
173 >      painCave.isFatal = 1;
174 >      simError();
175 >    }
176 >
177 >    int index = tokenizer.nextTokenAsInt();
178 >
179 >    StuntDouble* integrableObject = info_->getIOIndexToIntegrableObject(index);
180 >
181 >    if (integrableObject == NULL) {
182 >      return;
183 >    }  
184 >
185 >    std::string type = tokenizer.nextToken();
186 >    int size = type.size();
187 >    Vector3d pos;
188 >    Vector3d vel;
189 >    Quat4d q;
190 >    Vector3d ji;
191 >    Vector3d force;
192 >    Vector3d torque;
193            
194 <          mol = info_->getMoleculeByGlobalIndex(i);
195 <          
196 <          if(mol == NULL) {
197 <            sprintf(painCave.errMsg,
198 <                    "RestReader Error: molecule not found on node %d\n",
199 <                    worldRank);
200 <            painCave.isFatal = 1;
201 <            simError();
194 >    for(int i = 0; i < size; ++i) {
195 >      switch(type[i]) {
196 >        
197 >        case 'p': {
198 >            pos[0] = tokenizer.nextTokenAsDouble();
199 >            pos[1] = tokenizer.nextTokenAsDouble();
200 >            pos[2] = tokenizer.nextTokenAsDouble();
201 >            break;
202 >        }
203 >        case 'v' : {
204 >            vel[0] = tokenizer.nextTokenAsDouble();
205 >            vel[1] = tokenizer.nextTokenAsDouble();
206 >            vel[2] = tokenizer.nextTokenAsDouble();
207 >            break;
208 >        }
209 >
210 >        case 'q' : {
211 >           if (integrableObject->isDirectional()) {
212 >              
213 >             q[0] = tokenizer.nextTokenAsDouble();
214 >             q[1] = tokenizer.nextTokenAsDouble();
215 >             q[2] = tokenizer.nextTokenAsDouble();
216 >             q[3] = tokenizer.nextTokenAsDouble();
217 >              
218 >             RealType qlen = q.length();
219 >             if (qlen < oopse::epsilon) { //check quaternion is not equal to 0
220 >                
221 >               sprintf(painCave.errMsg,
222 >                       "DumpReader Error: initial quaternion error (q0^2 + q1^2 + q2^2 + q3^2) ~ 0\n");
223 >               painCave.isFatal = 1;
224 >               simError();
225 >                
226 >             }  
227 >              
228 >             q.normalize();
229 >           }            
230 >           break;
231 >        }  
232 >        case 'j' : {
233 >          if (integrableObject->isDirectional()) {
234 >             ji[0] = tokenizer.nextTokenAsDouble();
235 >             ji[1] = tokenizer.nextTokenAsDouble();
236 >             ji[2] = tokenizer.nextTokenAsDouble();
237            }
238 <          
239 <          nCurObj = mol->getNIntegrableObjects();
240 <          
241 <          MPI_Send(&nCurObj, 1, MPI_INT, masterNode,
242 <                   TAKE_THIS_TAG_INT, MPI_COMM_WORLD);
243 <          
244 <          for (integrableObject = mol->beginIntegrableObject(ii);
245 <               integrableObject != NULL;
246 <               integrableObject = mol->nextIntegrableObject(ii)){
247 <            
248 <            MPI_Recv(read_buffer, BUFFERSIZE, MPI_CHAR, masterNode,
249 <                     TAKE_THIS_TAG_CHAR, MPI_COMM_WORLD, &istatus);
250 <            
251 <            parseErr = parseIdealLine(read_buffer, integrableObject);
252 <            
253 <            if( parseErr != NULL ){
254 <              strcpy( painCave.errMsg, parseErr );
255 <              simError();
256 <            }
238 >          break;
239 >        }  
240 >        case 'f': {
241 >          force[0] = tokenizer.nextTokenAsDouble();
242 >          force[1] = tokenizer.nextTokenAsDouble();
243 >          force[2] = tokenizer.nextTokenAsDouble();          
244 >
245 >          break;
246 >        }
247 >        case 't' : {
248 >           torque[0] = tokenizer.nextTokenAsDouble();
249 >           torque[1] = tokenizer.nextTokenAsDouble();
250 >           torque[2] = tokenizer.nextTokenAsDouble();          
251 >
252 >           break;
253 >        }
254 >        default: {
255 >               sprintf(painCave.errMsg,
256 >                       "DumpReader Error: %s is an unrecognized type\n", type.c_str());
257 >               painCave.isFatal = 1;
258 >               simError();
259 >          break;  
260 >        }
261 >          
262 >      }
263 >    }
264 >    
265 >    // keep the position in case we need it for a molecular restraint:
266 >    
267 >    all_pos_[index] = pos;
268 >
269 >    // is this io restrained?    
270 >    GenericData* data = integrableObject->getPropertyByName("Restraint");
271 >    ObjectRestraint* oRest;
272 >
273 >    if (data != NULL) {
274 >      // make sure we can reinterpret the generic data as restraint data:
275 >      RestraintData* restData= dynamic_cast<RestraintData*>(data);        
276 >      if (restData != NULL) {
277 >        // make sure we can reinterpet the restraint data as a pointer to
278 >        // an ObjectRestraint:
279 >        oRest = dynamic_cast<ObjectRestraint*>(restData->getData());
280 >        if (oRest != NULL) {          
281 >          if (integrableObject->isDirectional()) {
282 >            oRest->setReferenceStructure(pos, q.toRotationMatrix3());
283 >          } else {                          
284 >            oRest->setReferenceStructure(pos);
285            }
286          }
287        }
288      }
289 < #endif
289 >
290    }
291 <  
292 <  char* RestReader::parseIdealLine(char* readLine, StuntDouble* sd){
293 <    
294 <    char *foo; // the pointer to the current string token
295 <    
296 <    double pos[3];        // position place holders
297 <    double q[4];          // the quaternions
298 <    double RfromQ[3][3];  // the rotation matrix
299 <    double normalize;     // to normalize the reference unit vector
300 <    double uX, uY, uZ;    // reference unit vector place holders
301 <    double uselessToken;
302 <    StringTokenizer tokenizer(readLine);
374 <    int nTokens;
375 <    
376 <    nTokens = tokenizer.countTokens();
377 <    
378 <    if (nTokens < 14) {
379 <      sprintf(painCave.errMsg,
380 <              "RestReader Error: Not enough Tokens.\n");
381 <      painCave.isFatal = 1;
382 <      simError();
291 >  
292 >
293 >
294 >  void RestReader::readFrameProperties(std::istream& inputStream) {
295 >    inputStream.getline(buffer, bufferSize);
296 >    std::string line(buffer);
297 >
298 >    if (line.find("<FrameData>") == std::string::npos) {
299 >      sprintf(painCave.errMsg,
300 >              "DumpReader Error: Missing <FrameData>\n");
301 >      painCave.isFatal = 1;
302 >      simError();
303      }
304 <    
305 <    std::string name = tokenizer.nextToken();
306 <    
307 <    if (name != sd->getType()) {
304 >
305 >    // restraints don't care about frame data (unless we need to wrap
306 >    // coordinates, but we'll worry about that later), so
307 >    // we'll just scan ahead until the end of the frame data:
308 >
309 >    while(inputStream.getline(buffer, bufferSize)) {
310 >      line = buffer;
311        
312 <      sprintf(painCave.errMsg,
313 <              "RestReader Error: Atom type [%s] in %s does not "
391 <              "match Atom Type [%s] in .md file.\n",
392 <              name.c_str(), inIdealFileName.c_str(),
393 <              sd->getType().c_str());
394 <      painCave.isFatal = 1;
395 <      simError();        
396 <    }
397 <    
398 <    pos[0] = tokenizer.nextTokenAsDouble();
399 <    pos[1] = tokenizer.nextTokenAsDouble();
400 <    pos[2] = tokenizer.nextTokenAsDouble();
401 <    
402 <    // store the positions in the stuntdouble as generic data doubles
403 <    DoubleGenericData* refPosX = new DoubleGenericData();
404 <    refPosX->setID("refPosX");
405 <    refPosX->setData(pos[0]);
406 <    sd->addProperty(refPosX);
407 <    
408 <    DoubleGenericData* refPosY = new DoubleGenericData();
409 <    refPosY->setID("refPosY");
410 <    refPosY->setData(pos[1]);
411 <    sd->addProperty(refPosY);
412 <    
413 <    DoubleGenericData* refPosZ = new DoubleGenericData();
414 <    refPosZ->setID("refPosZ");
415 <    refPosZ->setData(pos[2]);
416 <    sd->addProperty(refPosZ);
417 <    
418 <    // we don't need the velocities
419 <    uselessToken = tokenizer.nextTokenAsDouble();
420 <    uselessToken = tokenizer.nextTokenAsDouble();
421 <    uselessToken = tokenizer.nextTokenAsDouble();
422 <    
423 <    if (sd->isDirectional()) {
424 <      
425 <      q[0] = tokenizer.nextTokenAsDouble();
426 <      q[1] = tokenizer.nextTokenAsDouble();
427 <      q[2] = tokenizer.nextTokenAsDouble();
428 <      q[3] = tokenizer.nextTokenAsDouble();
429 <      
430 <      // now build the rotation matrix and find the unit vectors
431 <      RfromQ[0][0] = q[0]*q[0] + q[1]*q[1] - q[2]*q[2] - q[3]*q[3];
432 <      RfromQ[0][1] = 2*(q[1]*q[2] + q[0]*q[3]);
433 <      RfromQ[0][2] = 2*(q[1]*q[3] - q[0]*q[2]);
434 <      RfromQ[1][0] = 2*(q[1]*q[2] - q[0]*q[3]);
435 <      RfromQ[1][1] = q[0]*q[0] - q[1]*q[1] + q[2]*q[2] - q[3]*q[3];
436 <      RfromQ[1][2] = 2*(q[2]*q[3] + q[0]*q[1]);
437 <      RfromQ[2][0] = 2*(q[1]*q[3] + q[0]*q[2]);
438 <      RfromQ[2][1] = 2*(q[2]*q[3] - q[0]*q[1]);
439 <      RfromQ[2][2] = q[0]*q[0] - q[1]*q[1] - q[2]*q[2] + q[3]*q[3];
440 <      
441 <      normalize = sqrt(RfromQ[2][0]*RfromQ[2][0] + RfromQ[2][1]*RfromQ[2][1]
442 <                       + RfromQ[2][2]*RfromQ[2][2]);
443 <      uX = RfromQ[2][0]/normalize;
444 <      uY = RfromQ[2][1]/normalize;
445 <      uZ = RfromQ[2][2]/normalize;
446 <      
447 <      // store reference unit vectors as generic data in the stuntdouble
448 <      DoubleGenericData* refVectorX = new DoubleGenericData();
449 <      refVectorX->setID("refVectorX");
450 <      refVectorX->setData(uX);
451 <      sd->addProperty(refVectorX);
452 <      
453 <      DoubleGenericData* refVectorY = new DoubleGenericData();
454 <      refVectorY->setID("refVectorY");
455 <      refVectorY->setData(uY);
456 <      sd->addProperty(refVectorY);
457 <      
458 <      DoubleGenericData* refVectorZ = new DoubleGenericData();
459 <      refVectorZ->setID("refVectorZ");
460 <      refVectorZ->setData(uZ);
461 <      sd->addProperty(refVectorZ);
462 <    }
463 <    
464 <    // we don't need the angular velocities, so let's exit the line
465 <    return NULL;
466 <  }
467 <  
468 <  void RestReader::readZangle(){
469 <    
470 <    int i;
471 <    unsigned int j;
472 <    int isPresent;
473 <    
474 <    Molecule* mol;
475 <    StuntDouble* integrableObject;
476 <    SimInfo::MoleculeIterator mi;
477 <    Molecule::IntegrableObjectIterator ii;
478 <    
479 < #ifdef IS_MPI
480 <    int done, which_node, which_atom; // loop counter
481 <    int nProc;
482 <    MPI_Status istatus;
483 < #endif //is_mpi
484 <    
485 <    const int BUFFERSIZE = 2000; // size of the read buffer
486 <    int nTotObjs; // the number of atoms
487 <    char read_buffer[BUFFERSIZE]; //the line buffer for reading
488 <    
489 <    char *eof_test; // ptr to see when we reach the end of the file
490 <    char *parseErr;
491 <    
492 <    std::vector<StuntDouble*> vecParticles;
493 <    std::vector<double> tempZangs;
494 <      
495 <    inAngFileName = info_->getRestFileName();
496 <    
497 <    inAngFileName += "0";
498 <    
499 <    // open the omega value file for reading
500 < #ifdef IS_MPI
501 <    if (worldRank == 0) {
502 < #endif
503 <      isPresent = 1;
504 <      inAngFile = fopen(inAngFileName.c_str(), "r");
505 <      if(!inAngFile){
506 <        sprintf(painCave.errMsg,
507 <                "Restraints Warning: %s file is not present\n"
508 <                "\tAll omega values will be initialized to zero. If the\n"
509 <                "\tsimulation is starting from the idealCrystal.in reference\n"
510 <                "\tconfiguration, this is the desired action. If this is not\n"
511 <                "\tthe case, the energy calculations will be incorrect.\n",
512 <                inAngFileName.c_str());
513 <        painCave.severity = OOPSE_WARNING;
514 <        painCave.isFatal = 0;
515 <        simError();  
516 <        isPresent = 0;
312 >      if(line.find("</FrameData>") != std::string::npos) {
313 >        break;
314        }
518      
519 #ifdef IS_MPI
520      // let the other nodes know the status of the file search
521      MPI_Bcast(&isPresent, 1, MPI_INT, 0, MPI_COMM_WORLD);
522 #endif // is_mpi
523      
524      if (!isPresent) {
525        zeroZangle();
526        return;
527      }
528      
529 #ifdef IS_MPI
530    }
531    
532    // listen to node 0 to see if we should exit this function
533    if (worldRank != 0) {
534      MPI_Bcast(&isPresent, 1, MPI_INT, 0, MPI_COMM_WORLD);
535      if (!isPresent) {
536        zeroZangle();
537        return;
538      }
539    }
540    
541    strcpy( checkPointMsg, "zAngle file opened successfully for reading." );
542    MPIcheckPoint();
543 #endif
544    
545 #ifndef IS_MPI
546    
547    eof_test = fgets(read_buffer, sizeof(read_buffer), inAngFile);
548    if( eof_test == NULL ){
549      sprintf( painCave.errMsg,
550               "RestraintReader error: error reading 1st line of \"%s\"\n",
551               inAngFileName.c_str() );
552      painCave.isFatal = 1;
553      simError();
554    }
555    
556    eof_test = fgets(read_buffer, sizeof(read_buffer), inAngFile);
557    while ( eof_test != NULL ) {
558      // check for and ignore blank lines
559      if ( read_buffer != NULL )
560        tempZangs.push_back( atof(read_buffer) );
561      eof_test = fgets(read_buffer, sizeof(read_buffer), inAngFile);
562    }
563    
564    nTotObjs = info_->getNGlobalIntegrableObjects();
565    
566    if( nTotObjs != tempZangs.size() ){
567      sprintf( painCave.errMsg,
568               "RestraintReader zAngle reading error. %s nIntegrable, %d, "
569               "does not match the meta-data file's nIntegrable, %d.\n",
570               inAngFileName.c_str(), tempZangs.size(), nTotObjs );
571      painCave.isFatal = 1;
572      simError();
573    }
574    
575    // load the zAngles into the integrable objects
576    i = 0;
577    for (mol = info_->beginMolecule(mi); mol != NULL;
578         mol = info_->nextMolecule(mi)) {
315        
580      for (integrableObject = mol->beginIntegrableObject(ii);
581           integrableObject != NULL;
582           integrableObject = mol->nextIntegrableObject(ii)) {    
583        
584        integrableObject->setZangle(tempZangs[i]);
585        i++;
586      }
316      }
588    
589    // MPI Section of code..........
590 #else //IS_MPI
591    
592    // first thing first, suspend fatalities.
593    painCave.isEventLoop = 1;
317  
595    int masterNode = 0;
596    int myStatus; // 1 = wakeup & success; 0 = error; -1 = AllDone
597    int haveError;
598    int index;    
599
600    int nCurObj;
601    double angleTranfer;
602    
603    nTotObjs = info_->getNGlobalIntegrableObjects();
604    haveError = 0;
605
606    if (worldRank == masterNode) {
607      
608      eof_test = fgets(read_buffer, sizeof(read_buffer), inAngFile);
609      if( eof_test == NULL ){
610        sprintf( painCave.errMsg,
611                 "Error reading 1st line of %s \n ",inAngFileName.c_str());
612        haveError = 1;
613        simError();
614      }
615      
616      // let node 0 load the temporary angle vector
617      eof_test = fgets(read_buffer, sizeof(read_buffer), inAngFile);
618      while ( eof_test != NULL ) {
619        // check for and ignore blank lines
620        if ( read_buffer != NULL )
621          tempZangs.push_back( atof(read_buffer) );
622        eof_test = fgets(read_buffer, sizeof(read_buffer), inAngFile);
623      }
624      
625      // Check to see that the number of integrable objects in the
626      // intial configuration file is the same as derived from the
627      // meta-data file.
628      if( nTotObjs != tempZangs.size() ){
629        sprintf( painCave.errMsg,
630                 "RestraintReader zAngle reading Error. %s nIntegrable, %d, "
631                 "does not match the meta-data file's nIntegrable, %d.\n",
632                 inAngFileName.c_str(), tempZangs.size(), nTotObjs);
633        haveError= 1;
634        simError();
635      }
636      
637      // At this point, node 0 has a tempZangs vector completed, and
638      // everyone else has nada
639      index = 0;
640      
641      for (i=0 ; i < info_->getNGlobalMolecules(); i++) {
642        // Get the Node number which has this atom
643        which_node = info_->getMolToProc(i);
644        
645        if (worldRank == masterNode) {
646          mol = info_->getMoleculeByGlobalIndex(i);
647          
648          if(mol == NULL) {
649            strcpy(painCave.errMsg, "Molecule not found on node 0!");
650            haveError = 1;
651            simError();
652          }
653          
654          for (integrableObject = mol->beginIntegrableObject(ii);
655               integrableObject != NULL;
656               integrableObject = mol->nextIntegrableObject(ii)){
657            
658            integrableObject->setZangle(tempZangs[index]);
659            index++;
660          }    
661          
662        } else {
663          // I am MASTER OF THE UNIVERSE, but I don't own this molecule
664          
665          MPI_Recv(&nCurObj, 1, MPI_INT, which_node,
666                   TAKE_THIS_TAG_INT, MPI_COMM_WORLD, &istatus);
667          
668          for(j=0; j < nCurObj; j++){            
669            angleTransfer = tempZangs[index];
670            MPI_Send(&angleTransfer, 1, MPI_DOUBLE, which_node,
671                     TAKE_THIS_TAG_DOUBLE, MPI_COMM_WORLD);              
672            index++;
673          }
674          
675        }
676      }
677    } else {
678      // I am SLAVE TO THE MASTER
679      for (i=0 ; i < info_->getNGlobalMolecules(); i++) {
680        int which_node = info_->getMolToProc(i);
681
682        if (which_node == worldRank) {
683          
684          // BUT I OWN THIS MOLECULE!!!
685          
686          mol = info_->getMoleculeByGlobalIndex(i);
687
688          if(mol == NULL) {
689            sprintf(painCave.errMsg,
690                    "RestReader Error: molecule not found on node %d\n",
691                    worldRank);
692            painCave.isFatal = 1;
693            simError();
694          }
695
696          nCurObj = mol->getNIntegrableObjects();
697        
698          MPI_Send(&nCurObj, 1, MPI_INT, 0,
699                   TAKE_THIS_TAG_INT, MPI_COMM_WORLD);
700          
701          for (integrableObject = mol->beginIntegrableObject(ii);
702               integrableObject != NULL;
703               integrableObject = mol->nextIntegrableObject(ii)){
704            
705            MPI_Recv(&angleTransfer, 1, MPI_DOUBLE, 0,
706                     TAKE_THIS_TAG_DOUBLE, MPI_COMM_WORLD, &istatus);
707
708            integrableObject->setZangle(angleTransfer);
709          }    
710        }
711      }
712    }
713 #endif
318    }
715  
716  void RestReader :: zeroZangle(){
717    
718    int i;
719    unsigned int j;
720    int nTotObjs; // the number of atoms
721    
722    Molecule* mol;
723    StuntDouble* integrableObject;
724    SimInfo::MoleculeIterator mi;
725    Molecule::IntegrableObjectIterator ii;
726    
727    std::vector<StuntDouble*> vecParticles;
728    
729 #ifndef IS_MPI
730    // set all zAngles to 0.0
731    for (mol = info_->beginMolecule(mi); mol != NULL;
732         mol = info_->nextMolecule(mi))
733      
734      for (integrableObject = mol->beginIntegrableObject(ii);
735           integrableObject != NULL;
736           integrableObject = mol->nextIntegrableObject(ii))    
737        integrableObject->setZangle( 0.0 );
738    
739    
740    // MPI Section of code..........
741 #else //IS_MPI
742    
743    // first thing first, suspend fatalities.
744    painCave.isEventLoop = 1;
745    
746    int masterNode = 0;
747    int myStatus; // 1 = wakeup & success; 0 = error; -1 = AllDone
748    int haveError;
749    int which_node;
750    
751    MPI_Status istatus;
752    
753    int nCurObj;
754    double angleTranfer;
755    
756    nTotObjs = info_->getNGlobalIntegrableObjects();
757    haveError = 0;
758    if (worldRank == masterNode) {
319  
320 <      for (i=0 ; i < info_->getNGlobalMolecules(); i++) {
321 <        // Get the Node number which has this atom
762 <        which_node = info_->getMolToProc(i);
763 <        
764 <        // let's let node 0 pass out constant values to all the processors
765 <        if (worldRank == masterNode) {
766 <          mol = info_->getMoleculeByGlobalIndex(i);
767 <          
768 <          if(mol == NULL) {
769 <            strcpy(painCave.errMsg, "Molecule not found on node 0!");
770 <            haveError = 1;
771 <            simError();
772 <          }
773 <          
774 <          for (integrableObject = mol->beginIntegrableObject(ii);
775 <               integrableObject != NULL;
776 <               integrableObject = mol->nextIntegrableObject(ii)){
777 <            
778 <            integrableObject->setZangle( 0.0 );
779 <            
780 <          }
781 <          
782 <        } else {
783 <          // I am MASTER OF THE UNIVERSE, but I don't own this molecule
784 <          
785 <          MPI_Recv(&nCurObj, 1, MPI_INT, which_node,
786 <                   TAKE_THIS_TAG_INT, MPI_COMM_WORLD, &istatus);
787 <          
788 <          for(j=0; j < nCurObj; j++){            
789 <            angleTransfer = 0.0;
790 <            MPI_Send(&angleTransfer, 1, MPI_DOUBLE, which_node,
791 <                     TAKE_THIS_TAG_DOUBLE, MPI_COMM_WORLD);              
792 <            
793 <          }
794 <        }
795 <      }
796 <    } else {
797 <      // I am SLAVE TO THE MASTER
798 <      for (i=0 ; i < info_->getNGlobalMolecules(); i++) {
799 <        int which_node = info_->getMolToProc(i);
800 <        
801 <        if (which_node == worldRank) {
802 <          
803 <          // BUT I OWN THIS MOLECULE!!!
804 <          mol = info_->getMoleculeByGlobalIndex(i);
805 <          
806 <          if(mol == NULL) {
807 <            sprintf(painCave.errMsg,
808 <                    "RestReader Error: molecule not found on node %d\n",
809 <                    worldRank);
810 <            painCave.isFatal = 1;
811 <            simError();
812 <          }
813 <          
814 <          nCurObj = mol->getNIntegrableObjects();
815 <          
816 <          MPI_Send(&nCurObj, 1, MPI_INT, 0,
817 <                   TAKE_THIS_TAG_INT, MPI_COMM_WORLD);
818 <          
819 <          for (integrableObject = mol->beginIntegrableObject(ii);
820 <               integrableObject != NULL;
821 <               integrableObject = mol->nextIntegrableObject(ii)){
822 <            
823 <            MPI_Recv(&angleTransfer, 1, MPI_DOUBLE, 0,
824 <                     TAKE_THIS_TAG_DOUBLE, MPI_COMM_WORLD, &istatus);
825 <            vecParticles[j]->setZangle(angleTransfer);
826 <          }    
827 <        }
828 <      }
829 <    }
830 < #endif
831 <  }
832 <  
833 < } // end namespace oopse
320 >  
321 > }//end namespace oopse

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines