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 990 by chrisfen, Mon Jun 19 01:36:06 2006 UTC vs.
branches/development/src/io/RestReader.cpp (file contents), Revision 1665 by gezelter, Tue Nov 22 20:38:56 2011 UTC

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

Comparing:
trunk/src/io/RestReader.cpp (property svn:keywords), Revision 990 by chrisfen, Mon Jun 19 01:36:06 2006 UTC vs.
branches/development/src/io/RestReader.cpp (property svn:keywords), Revision 1665 by gezelter, Tue Nov 22 20:38:56 2011 UTC

# Line 0 | Line 1
1 + Author Id Revision Date

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines