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 417 by chrisfen, Thu Mar 10 15:10:24 2005 UTC vs.
branches/development/src/io/RestReader.cpp (file contents), Revision 1769 by gezelter, Mon Jul 9 14:15:52 2012 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 < #include "brains/mpiSimulation.hpp"
64 < #define TAKE_THIS_TAG_CHAR 0
65 < #define TAKE_THIS_TAG_INT 1
66 < #define TAKE_THIS_TAG_DOUBLE 2
67 < #endif // is_mpi
68 > namespace OpenMD {
69  
70 < namespace oopse {
71 <  
72 <  RestReader::RestReader( SimInfo* info ) : info_(info){
73 <        
73 <    idealName = "idealCrystal.in";
70 >  void RestReader::scanFile(){
71 >    int lineNo = 0;
72 >    std::streampos prevPos;
73 >    std::streampos  currPos;
74      
75 <    isScanned = false;
75 > #ifdef IS_MPI
76      
77 < #ifdef IS_MPI
78 <    if (worldRank == 0) {
79 < #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;
90 < #ifdef IS_MPI
98 > #ifdef IS_MPI
99      }
100 <    strcpy( checkPointMsg,
101 <            "File \"idealCrystal.in\" opened successfully for reading." );
102 <    MPIcheckPoint();
103 < #endif
104 <    return;  
105 <  }
106 <  
107 <  RestReader :: ~RestReader( ){
108 < #ifdef IS_MPI
109 <    if (worldRank == 0) {
110 < #endif
111 <      int error;
112 <      error = fclose( inIdealFile );
100 >    MPI_Bcast(&framePos_, 1, MPI_INT, 0, MPI_COMM_WORLD);
101 > #endif // is_mpi
102 >  }
103 >
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;
154 <  }
155 <  
156 <  
157 <  void RestReader :: readIdealCrystal(){
153 >    inputStream.getline(buffer, bufferSize);
154 >
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 <    int i;
164 <    unsigned int j;
163 >    //read frameData
164 >    readFrameProperties(inputStream);
165      
166 < #ifdef IS_MPI
167 <    int done, which_node, which_atom; // loop counter
131 < #endif //is_mpi
166 >    //read StuntDoubles
167 >    readStuntDoubles(inputStream);
168      
169 <    const int BUFFERSIZE = 2000; // size of the read buffer
170 <    int nTotObjs; // the number of atoms
171 <    char read_buffer[BUFFERSIZE]; //the line buffer for reading
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 <    char *eof_test; // ptr to see when we reach the end of the file
180 <    char *parseErr;
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 <    std::vector<StuntDouble*> integrableObjects;
189 <    
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 localIndex;
224 <    int nCurObj;
225 <    int nitems;
226 <    
227 <    nTotObjs = info_->getTotIntegrableObjects();
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 <      for (i=0 ; i < info_->getNGlobalMolecules(); i++) {
264 <        int which_node = info_->getMolToProc(i);
220 >        RestraintData* restData= dynamic_cast<RestraintData*>(data);        
221          
222 <        if(which_node == masterNode){
267 <          //molecules belong to master node
222 >        if (restData != NULL) {
223            
224 <          localIndex = info_->getMoleculeByGlobalIndex(i);
224 >          // make sure we can reinterpet the restraint data as a
225 >          // pointer to a MolecularRestraint:
226            
227 <          if(localIndex == NULL) {
272 <            strcpy(painCave.errMsg,
273 <                   "RestReader Error: Molecule not found on node %d!",
274 <                   worldRank);
275 <            painCave.isFatal = 1;
276 <            simError();
277 <          }
227 >          MolecularRestraint* mRest = dynamic_cast<MolecularRestraint*>(restData->getData());
228            
229 <          for (integrableObject = mol->beginIntegrableObject(ii);
280 <               integrableObject != NULL;
281 <               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"
289 <                      "error reading the line from the file.\n",
290 <                      inIdealFileName.c_str(), nTotObjs, i );
291 <              painCave.isFatal = 1;
292 <              simError();
293 <            }
234 >            std::vector<Vector3d> ref;
235 >            int count = 0;
236 >            RealType mass, mTot;
237 >            Vector3d COM(0.0);
238              
239 <            parseIdealLine(read_buffer, integrableObjects[j]);
296 <          }
297 <        } else {
298 <          //molecule belongs to slave nodes
299 <          
300 <          MPI_Recv(&nCurObj, 1, MPI_INT, which_node,
301 <                   TAKE_THIS_TAG_INT, MPI_COMM_WORLD, &istatus);
302 <          
303 <          for(j=0; j < nCurObj; j++){
239 >            mTot = 0.0;
240              
241 <            eof_test = fgets(read_buffer, sizeof(read_buffer), inIdealFile);
242 <            if(eof_test == NULL){
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 );
312 <              painCave.isFatal = 1;
313 <              simError();
314 <            }
241 >            // loop over the stunt doubles in this molecule in the order we
242 >            // will be looping them in the restraint code:
243              
244 <            if(haveError) nodeZeroError();
245 <            
246 <            MPI_Send(read_buffer, BUFFERSIZE, MPI_CHAR, which_node,
247 <                     TAKE_THIS_TAG_CHAR, MPI_COMM_WORLD);
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
264 <      for (i=0 ; i < info_->getNGlobalMolecules(); i++) {
265 <        int which_node = info_->getMolToProc(i);
266 <        
267 <        if(which_node == worldRank){
268 <          //molecule with global index i belongs to this processor
262 >    }
263 >  }
264 >
265 >  
266 >  
267 >  void RestReader::parseDumpLine(const std::string& line) {        
268 >
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* sd = info_->getIOIndexToIntegrableObject(index);
284 >
285 >    if (sd == 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 (sd->isDirectional()) {
314            
315 <          localIndex = 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(localIndex == NULL) {
321 <            sprintf(painCave.errMsg,
335 <                    "RestReader Error: molecule not found on node %d\n",
336 <                    worldRank);
337 <            painCave.isFatal = 1;
338 <            simError();
339 <          }
340 <          
341 <          nCurObj = localIndex->getNIntegrableObjects();
342 <          
343 <          MPI_Send(&nCurObj, 1, MPI_INT, masterNode,
344 <                   TAKE_THIS_TAG_INT, MPI_COMM_WORLD);
345 <          
346 <          for (integrableObject = mol->beginIntegrableObject(ii);
347 <               integrableObject != NULL;
348 <               integrableObject = mol->nextIntegrableObject(ii)){
349 <            
350 <            MPI_Recv(read_buffer, BUFFERSIZE, MPI_CHAR, masterNode,
351 <                     TAKE_THIS_TAG_CHAR, MPI_COMM_WORLD, &istatus);
352 <            
353 <            parseErr = parseIdealLine(read_buffer, integrableObject);
320 >          RealType qlen = q.length();
321 >          if (qlen < OpenMD::epsilon) { //check quaternion is not equal to 0
322              
323 <            if( parseErr != NULL ){
324 <              strcpy( painCave.errMsg, parseErr );
325 <              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 (sd->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 = sd->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 (sd->isDirectional()) {
381 >              oRest->setReferenceStructure(pos, q.toRotationMatrix3());
382 >            } else {                          
383 >              oRest->setReferenceStructure(pos);
384              }
385            }
386          }
387        }
388      }
363 #endif
389    }
390    
391 <  char* RestReader::parseIdealLine(char* readLine, StuntDouble* sd){
391 >  void  RestReader::readStuntDoubles(std::istream& inputStream) {
392      
393 <    char *foo; // the pointer to the current string token
393 >    inputStream.getline(buffer, bufferSize);
394 >    std::string line(buffer);
395      
396 <    double pos[3];        // position place holders
397 <    double q[4];          // the quaternions
398 <    double RfromQ[3][3];  // the rotation matrix
399 <    double normalize;     // to normalize the reference unit vector
400 <    double uX, uY, uZ;    // reference unit vector place holders
375 <    double uselessToken;
376 <    StringTokenizer tokenizer(readLine);
377 <    int nTokens;
378 <    
379 <    nTokens = tokenizer.countTokens();
380 <    
381 <    if (nTokens < 14) {
382 <      sprintf(painCave.errMsg,
383 <              "RestReader Error: Not enough Tokens.\n");
384 <      painCave.isFatal = 1;
385 <      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 <    
390 <    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",
395 <              name.c_str(), inIdealFileName.c_str(),
396 <              sd->getType().c_str());
397 <      painCave.isFatal = 1;
398 <      simError();        
399 <    }
400 <    
401 <    pos[0] = tokenizer.nextTokenAsDouble();
402 <    pos[1] = tokenizer.nextTokenAsDouble();
403 <    pos[2] = tokenizer.nextTokenAsDouble();
404 <    
405 <    // store the positions in the stuntdouble as generic data doubles
406 <    DoubleGenericData* refPosX = new DoubleGenericData();
407 <    refPosX->setID("refPosX");
408 <    refPosX->setData(pos[0]);
409 <    sd->addProperty(refPosX);
410 <    
411 <    DoubleGenericData* refPosY = new DoubleGenericData();
412 <    refPosY->setID("refPosY");
413 <    refPosY->setData(pos[1]);
414 <    sd->addProperty(refPosY);
415 <    
416 <    DoubleGenericData* refPosZ = new DoubleGenericData();
417 <    refPosZ->setID("refPosZ");
418 <    refPosZ->setData(pos[2]);
419 <    sd->addProperty(refPosZ);
420 <    
421 <    // we don't need the velocities
422 <    uselessToken = tokenizer.nextTokenAsDouble();
423 <    uselessToken = tokenizer.nextTokenAsDouble();
424 <    uselessToken = tokenizer.nextTokenAsDouble();
425 <    
426 <    if (sd->isDirectional()) {
406 >      if(line.find("</StuntDoubles>") != std::string::npos) {
407 >        break;
408 >      }
409        
410 <      q[0] = tokenizer.nextTokenAsDouble();
429 <      q[1] = tokenizer.nextTokenAsDouble();
430 <      q[2] = tokenizer.nextTokenAsDouble();
431 <      q[3] = tokenizer.nextTokenAsDouble();
432 <      
433 <      // now build the rotation matrix and find the unit vectors
434 <      RfromQ[0][0] = q[0]*q[0] + q[1]*q[1] - q[2]*q[2] - q[3]*q[3];
435 <      RfromQ[0][1] = 2*(q[1]*q[2] + q[0]*q[3]);
436 <      RfromQ[0][2] = 2*(q[1]*q[3] - q[0]*q[2]);
437 <      RfromQ[1][0] = 2*(q[1]*q[2] - q[0]*q[3]);
438 <      RfromQ[1][1] = q[0]*q[0] - q[1]*q[1] + q[2]*q[2] - q[3]*q[3];
439 <      RfromQ[1][2] = 2*(q[2]*q[3] + q[0]*q[1]);
440 <      RfromQ[2][0] = 2*(q[1]*q[3] + q[0]*q[2]);
441 <      RfromQ[2][1] = 2*(q[2]*q[3] - q[0]*q[1]);
442 <      RfromQ[2][2] = q[0]*q[0] - q[1]*q[1] - q[2]*q[2] + q[3]*q[3];
443 <      
444 <      normalize = sqrt(RfromQ[2][0]*RfromQ[2][0] + RfromQ[2][1]*RfromQ[2][1]
445 <                       + RfromQ[2][2]*RfromQ[2][2]);
446 <      uX = RfromQ[2][0]/normalize;
447 <      uY = RfromQ[2][1]/normalize;
448 <      uZ = RfromQ[2][2]/normalize;
449 <      
450 <      // store reference unit vectors as generic data in the stuntdouble
451 <      DoubleGenericData* refVectorX = new DoubleGenericData();
452 <      refVectorX->setID("refVectorX");
453 <      refVectorX->setData(uX);
454 <      sd->addProperty(refVectorX);
455 <      
456 <      DoubleGenericData* refVectorY = new DoubleGenericData();
457 <      refVectorY->setID("refVectorY");
458 <      refVectorY->setData(uY);
459 <      sd->addProperty(refVectorY);
460 <      
461 <      DoubleGenericData* refVectorZ = new DoubleGenericData();
462 <      refVectorZ->setID("refVectorZ");
463 <      refVectorZ->setData(uZ);
464 <      sd->addProperty(refVectorZ);
410 >      parseDumpLine(line);
411      }
412      
467    // we don't need the angular velocities, so let's exit the line
468    return NULL;
413    }
414 +
415    
416 <  void RestReader::readZangle(){
417 <    
418 <    int i;
419 <    unsigned int j;
420 <    int isPresent;
421 <    
422 <    Molecule* mol;
423 <    StuntDouble* integrableObject;
424 <    SimInfo::MoleculeIterator mi;
480 <    Molecule::IntegrableObjectIterator ii;
481 <    
482 < #ifdef IS_MPI
483 <    int done, which_node, which_atom; // loop counter
484 <    int nProc;
485 <    MPI_Status istatus;
486 < #endif //is_mpi
487 <    
488 <    const int BUFFERSIZE = 2000; // size of the read buffer
489 <    int nTotObjs; // the number of atoms
490 <    char read_buffer[BUFFERSIZE]; //the line buffer for reading
491 <    
492 <    char *eof_test; // ptr to see when we reach the end of the file
493 <    char *parseErr;
494 <    
495 <    std::vector<StuntDouble*> vecParticles;
496 <    std::vector<double> tempZangs;
497 <      
498 <    inAngFileName = info_->getRestFileName();
499 <    
500 <    inAngFileName += "0";
501 <    
502 <    // open the omega value file for reading
503 < #ifdef IS_MPI
504 <    if (worldRank == 0) {
505 < #endif
506 <      isPresent = 1;
507 <      inAngFile = fopen(inAngFileName.c_str(), "r");
508 <      if(!inAngFile){
509 <        sprintf(painCave.errMsg,
510 <                "Restraints Warning: %s file is not present\n"
511 <                "\tAll omega values will be initialized to zero. If the\n"
512 <                "\tsimulation is starting from the idealCrystal.in reference\n"
513 <                "\tconfiguration, this is the desired action. If this is not\n"
514 <                "\tthe case, the energy calculations will be incorrect.\n",
515 <                inAngFileName.c_str());
516 <        painCave.severity = OOPSE_WARNING;
517 <        painCave.isFatal = 0;
518 <        simError();  
519 <        isPresent = 0;
520 <      }
521 <      
522 < #ifdef IS_MPI
523 <      // let the other nodes know the status of the file search
524 <      MPI_Bcast(&isPresent, 1, MPI_INT, 0, MPI_COMM_WORLD);
525 < #endif // is_mpi
526 <      
527 <      if (!isPresent) {
528 <        zeroZangle();
529 <        return;
530 <      }
531 <      
532 < #ifdef IS_MPI
416 >  void RestReader::readFrameProperties(std::istream& inputStream) {
417 >    inputStream.getline(buffer, bufferSize);
418 >    std::string line(buffer);
419 >
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 <    // listen to node 0 to see if we should exit this function
428 <    if (worldRank != 0) {
429 <      MPI_Bcast(&isPresent, 1, MPI_INT, 0, MPI_COMM_WORLD);
430 <      if (!isPresent) {
431 <        zeroZangle();
432 <        return;
433 <      }
434 <    }
435 <    
544 <    strcpy( checkPointMsg, "zAngle file opened successfully for reading." );
545 <    MPIcheckPoint();
546 < #endif
547 <    
548 < #ifndef IS_MPI
549 <    
550 <    eof_test = fgets(read_buffer, sizeof(read_buffer), inAngFile);
551 <    if( eof_test == NULL ){
552 <      sprintf( painCave.errMsg,
553 <               "RestraintReader error: error reading 1st line of \"%s\"\n",
554 <               inAngFileName.c_str() );
555 <      painCave.isFatal = 1;
556 <      simError();
557 <    }
558 <    
559 <    eof_test = fgets(read_buffer, sizeof(read_buffer), inAngFile);
560 <    while ( eof_test != NULL ) {
561 <      // check for and ignore blank lines
562 <      if ( read_buffer != NULL )
563 <        tempZangs.push_back( atof(read_buffer) );
564 <      eof_test = fgets(read_buffer, sizeof(read_buffer), inAngFile);
565 <    }
566 <    
567 <    nTotObjs = info_->getNGlobalIntegrableObjects();
568 <    
569 <    if( nTotObjs != tempZangs.size() ){
570 <      sprintf( painCave.errMsg,
571 <               "RestraintReader zAngle reading error. %s nIntegrable, %d, "
572 <               "does not match the meta-data file's nIntegrable, %d.\n",
573 <               inAngFileName.c_str(), tempZangs.size(), nTotObjs );
574 <      painCave.isFatal = 1;
575 <      simError();
576 <    }
577 <    
578 <    // load the zAngles into the integrable objects
579 <    i = 0;
580 <    for (mol = info_->beginMolecule(mi); mol != NULL;
581 <         mol = info_->nextMolecule(mi)) {
582 <      
583 <      for (integrableObject = mol->beginIntegrableObject(ii);
584 <           integrableObject != NULL;
585 <           integrableObject = mol->nextIntegrableObject(ii)) {    
586 <        
587 <        integrableObject->setZangle(tempZangs[i]);
588 <        i++;
426 >
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 >      if(line.find("</FrameData>") != std::string::npos) {
435 >        break;
436        }
590    }
591    
592    // MPI Section of code..........
593 #else //IS_MPI
594    
595    // first thing first, suspend fatalities.
596    painCave.isEventLoop = 1;
597    
598    int myStatus; // 1 = wakeup & success; 0 = error; -1 = AllDone
599    int haveError, index;
600    
601    int *MolToProcMap = mpiSim->getMolToProcMap();
602    int localIndex;
603    int nCurObj;
604    double angleTranfer;
605    
606    nTotObjs = info_->getTotIntegrableObjects();
607    haveError = 0;
608    if (worldRank == 0) {
437        
610      eof_test = fgets(read_buffer, sizeof(read_buffer), inAngFile);
611      if( eof_test == NULL ){
612        sprintf( painCave.errMsg,
613                 "Error reading 1st line of %s \n ",inAngFileName.c_str());
614        haveError = 1;
615        simError();
616      }
617      
618      // let node 0 load the temporary angle vector
619      eof_test = fgets(read_buffer, sizeof(read_buffer), inAngFile);
620      while ( eof_test != NULL ) {
621        // check for and ignore blank lines
622        if ( read_buffer != NULL )
623          tempZangs.push_back( atof(read_buffer) );
624        eof_test = fgets(read_buffer, sizeof(read_buffer), inAngFile);
625      }
626      
627      // Check to see that the number of integrable objects in the
628      // intial configuration file is the same as derived from the
629      // meta-data file.
630      if( nTotObjs != tempZangs.size() ){
631        sprintf( painCave.errMsg,
632                 "RestraintReader zAngle reading Error. %s nIntegrable, %d, "
633                 "does not match the meta-data file's nIntegrable, %d.\n",
634                 inAngFileName.c_str(), tempZangs.size(), nTotObjs);
635        haveError= 1;
636        simError();
637      }
638      
438      }
640    // At this point, node 0 has a tempZangs vector completed, and
641    // everyone else has nada
642    index = 0;
439      
644    for (i=0 ; i < mpiSim->getNMolGlobal(); i++) {
645      // Get the Node number which has this atom
646      which_node = MolToProcMap[i];
647      
648      if (worldRank == 0) {
649        if (which_node == 0) {
650          localIndex = mpiSim->getGlobalToLocalMol(i);
651          
652          if(localIndex == -1) {
653            strcpy(painCave.errMsg, "Molecule not found on node 0!");
654            haveError = 1;
655            simError();
656          }
657          
658          vecParticles = (info_->molecules[localIndex]).getIntegrableObjects();
659          for(j = 0; j < vecParticles.size(); j++){      
660            vecParticles[j]->setZangle(tempZangs[index]);
661            index++;
662          }    
663          
664        } else {
665          // I am MASTER OF THE UNIVERSE, but I don't own this molecule
666          
667          MPI_Recv(&nCurObj, 1, MPI_INT, which_node,
668                   TAKE_THIS_TAG_INT, MPI_COMM_WORLD, &istatus);
669          
670          for(j=0; j < nCurObj; j++){            
671            angleTransfer = tempZangs[index];
672            MPI_Send(&angleTransfer, 1, MPI_DOUBLE, which_node,
673                     TAKE_THIS_TAG_DOUBLE, MPI_COMM_WORLD);              
674            index++;
675          }
676          
677        }
678        
679      } else {
680        // I am SLAVE TO THE MASTER
681        
682        if (which_node == worldRank) {
683          
684          // BUT I OWN THIS MOLECULE!!!
685          
686          localIndex = mpiSim->getGlobalToLocalMol(i);
687          vecParticles = (info_->molecules[localIndex]).getIntegrableObjects();
688          nCurObj = vecParticles.size();
689          
690          MPI_Send(&nCurObj, 1, MPI_INT, 0,
691                   TAKE_THIS_TAG_INT, MPI_COMM_WORLD);
692          
693          for(j = 0; j < vecParticles.size(); j++){
694            
695            MPI_Recv(&angleTransfer, 1, MPI_DOUBLE, 0,
696                     TAKE_THIS_TAG_DOUBLE, MPI_COMM_WORLD, &istatus);
697            vecParticles[j]->setZangle(angleTransfer);
698          }    
699        }
700      }
701    }
702 #endif
440    }
441 <  
442 <  void RestReader :: zeroZangle(){
443 <    
707 <    int i;
708 <    unsigned int j;
709 <    int nTotObjs; // the number of atoms
710 <    
711 <    Molecule* mol;
712 <    StuntDouble* integrableObject;
713 <    SimInfo::MoleculeIterator mi;
714 <    Molecule::IntegrableObjectIterator ii;
715 <    
716 <    std::vector<StuntDouble*> vecParticles;
717 <    
718 < #ifndef IS_MPI
719 <    // set all zAngles to 0.0
720 <    for (mol = info_->beginMolecule(mi); mol != NULL;
721 <         mol = info_->nextMolecule(mi))
722 <      
723 <      for (integrableObject = mol->beginIntegrableObject(ii);
724 <           integrableObject != NULL;
725 <           integrableObject = mol->nextIntegrableObject(ii))    
726 <        integrableObject->setZangle( 0.0 );
727 <    
728 <    
729 <    // MPI Section of code..........
730 < #else //IS_MPI
731 <    
732 <    // first thing first, suspend fatalities.
733 <    painCave.isEventLoop = 1;
734 <    
735 <    int myStatus; // 1 = wakeup & success; 0 = error; -1 = AllDone
736 <    int haveError, index;
737 <    int which_node;
738 <    
739 <    MPI_Status istatus;
740 <    int *MolToProcMap = mpiSim->getMolToProcMap();
741 <    int localIndex;
742 <    int nCurObj;
743 <    double angleTranfer;
744 <    
745 <    nTotObjs = info_->getTotIntegrableObjects();
746 <    haveError = 0;
747 <    
748 <    for (i=0 ; i < mpiSim->getNMolGlobal(); i++) {
749 <      // Get the Node number which has this atom
750 <      which_node = MolToProcMap[i];
751 <      
752 <      // let's let node 0 pass out constant values to all the processors
753 <      if (worldRank == 0) {
754 <        if (which_node == 0) {
755 <          localIndex = mpiSim->getGlobalToLocalMol(i);
756 <          
757 <          if(localIndex == -1) {
758 <            strcpy(painCave.errMsg, "Molecule not found on node 0!");
759 <            haveError = 1;
760 <            simError();
761 <          }
762 <          
763 <          vecParticles = (info_->molecules[localIndex]).getIntegrableObjects();
764 <          for(j = 0; j < vecParticles.size(); j++){      
765 <            vecParticles[j]->setZangle( 0.0 );
766 <          }    
767 <          
768 <        } else {
769 <          // I am MASTER OF THE UNIVERSE, but I don't own this molecule
770 <          
771 <          MPI_Recv(&nCurObj, 1, MPI_INT, which_node,
772 <                   TAKE_THIS_TAG_INT, MPI_COMM_WORLD, &istatus);
773 <          
774 <          for(j=0; j < nCurObj; j++){            
775 <            angleTransfer = 0.0;
776 <            MPI_Send(&angleTransfer, 1, MPI_DOUBLE, which_node,
777 <                     TAKE_THIS_TAG_DOUBLE, MPI_COMM_WORLD);              
778 <            index++;
779 <          }
780 <        }
781 <      } else {
782 <        // I am SLAVE TO THE MASTER
783 <        
784 <        if (which_node == worldRank) {
785 <          
786 <          // BUT I OWN THIS MOLECULE!!!
787 <          
788 <          localIndex = mpiSim->getGlobalToLocalMol(i);
789 <          vecParticles = (info_->molecules[localIndex]).getIntegrableObjects();
790 <          nCurObj = vecParticles.size();
791 <          
792 <          MPI_Send(&nCurObj, 1, MPI_INT, 0,
793 <                   TAKE_THIS_TAG_INT, MPI_COMM_WORLD);
794 <          
795 <          for(j = 0; j < vecParticles.size(); j++){
796 <            
797 <            MPI_Recv(&angleTransfer, 1, MPI_DOUBLE, 0,
798 <                     TAKE_THIS_TAG_DOUBLE, MPI_COMM_WORLD, &istatus);
799 <            vecParticles[j]->setZangle(angleTransfer);
800 <          }    
801 <        }
802 <      }
803 <    }
804 < #endif
805 <  }
806 <  
807 < } // end namespace oopse
441 >
442 >  
443 > }//end namespace OpenMD

Comparing:
trunk/src/io/RestReader.cpp (property svn:keywords), Revision 417 by chrisfen, Thu Mar 10 15:10:24 2005 UTC vs.
branches/development/src/io/RestReader.cpp (property svn:keywords), Revision 1769 by gezelter, Mon Jul 9 14:15:52 2012 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines