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

Comparing:
trunk/src/io/DumpWriter.cpp (file contents), Revision 221 by chrisfen, Tue Nov 23 22:48:31 2004 UTC vs.
branches/development/src/io/DumpWriter.cpp (file contents), Revision 1714 by gezelter, Sat May 19 18:12:46 2012 UTC

# Line 1 | Line 1
1 < #define _LARGEFILE_SOURCE64
2 < #define _FILE_OFFSET_BITS 64
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. Redistributions of source code must retain the above copyright
10 > *    notice, this list of conditions and the following disclaimer.
11 > *
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.
16 > *
17 > * This software is provided "AS IS," without a warranty of any
18 > * kind. All express or implied conditions, representations and
19 > * warranties, including any implied warranty of merchantability,
20 > * fitness for a particular purpose or non-infringement, are hereby
21 > * excluded.  The University of Notre Dame and its licensors shall not
22 > * be liable for any damages suffered by licensee as a result of
23 > * using, modifying or distributing the software or its
24 > * derivatives. In no event will the University of Notre Dame or its
25 > * licensors be liable for any lost revenue, profit or data, or for
26 > * direct, indirect, special, consequential, incidental or punitive
27 > * damages, however caused and regardless of the theory of liability,
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 > *
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 > #include "io/DumpWriter.hpp"
44 > #include "primitives/Molecule.hpp"
45 > #include "utils/simError.h"
46 > #include "io/basic_teebuf.hpp"
47 > #include "io/gzstream.hpp"
48 > #include "io/Globals.hpp"
49  
4 #include <string.h>
5 #include <iostream>
6 #include <fstream>
7 #include <algorithm>
8 #include <utility>
50  
51   #ifdef IS_MPI
52   #include <mpi.h>
12 #include "brains/mpiSimulation.hpp"
13
14 namespace dWrite{
15  void DieDieDie( void );
16 }
17
18 using namespace dWrite;
53   #endif //is_mpi
54  
55 < #include "io/ReadWrite.hpp"
56 < #include "utils/simError.h"
55 > using namespace std;
56 > namespace OpenMD {
57  
58 < DumpWriter::DumpWriter( SimInfo* the_entry_plug ){
58 >  DumpWriter::DumpWriter(SimInfo* info)
59 >    : info_(info), filename_(info->getDumpFileName()), eorFilename_(info->getFinalConfigFileName()){
60  
61 <  entry_plug = the_entry_plug;
61 >    Globals* simParams = info->getSimParams();
62 >    needCompression_   = simParams->getCompressDumpFile();
63 >    needForceVector_   = simParams->getOutputForceVector();
64 >    needParticlePot_   = simParams->getOutputParticlePotential();
65 >    needFlucQ_         = simParams->getOutputFluctuatingCharges();
66 >    needElectricField_ = simParams->getOutputElectricField();
67  
68 +    createDumpFile_ = true;
69 + #ifdef HAVE_LIBZ
70 +    if (needCompression_) {
71 +      filename_ += ".gz";
72 +      eorFilename_ += ".gz";
73 +    }
74 + #endif
75 +    
76   #ifdef IS_MPI
77 <  if(worldRank == 0 ){
77 >
78 >    if (worldRank == 0) {
79   #endif // is_mpi
80 +        
81 +      dumpFile_ = createOStream(filename_);
82  
83 <    dumpFile.open(entry_plug->sampleName.c_str(), ios::out | ios::trunc );
83 >      if (!dumpFile_) {
84 >        sprintf(painCave.errMsg, "Could not open \"%s\" for dump output.\n",
85 >                filename_.c_str());
86 >        painCave.isFatal = 1;
87 >        simError();
88 >      }
89  
90 <    if( !dumpFile ){
90 > #ifdef IS_MPI
91  
36      sprintf( painCave.errMsg,
37               "Could not open \"%s\" for dump output.\n",
38               entry_plug->sampleName.c_str());
39      painCave.isFatal = 1;
40      simError();
92      }
93  
94 < #ifdef IS_MPI
94 > #endif // is_mpi
95 >
96    }
97  
46  //sort the local atoms by global index
47  sortByGlobalIndex();
48  
49  sprintf( checkPointMsg,
50           "Sucessfully opened output file for dumping.\n");
51  MPIcheckPoint();
52 #endif // is_mpi
53 }
98  
99 < DumpWriter::~DumpWriter( ){
99 >  DumpWriter::DumpWriter(SimInfo* info, const std::string& filename)
100 >    : info_(info), filename_(filename){
101  
102 < #ifdef IS_MPI
103 <  if(worldRank == 0 ){
59 < #endif // is_mpi
102 >    Globals* simParams = info->getSimParams();
103 >    eorFilename_ = filename_.substr(0, filename_.rfind(".")) + ".eor";    
104  
105 <    dumpFile.close();
105 >    needCompression_   = simParams->getCompressDumpFile();
106 >    needForceVector_   = simParams->getOutputForceVector();
107 >    needParticlePot_   = simParams->getOutputParticlePotential();
108 >    needFlucQ_         = simParams->getOutputFluctuatingCharges();
109 >    needElectricField_ = simParams->getOutputElectricField();
110  
111 +    createDumpFile_ = true;
112 + #ifdef HAVE_LIBZ
113 +    if (needCompression_) {
114 +      filename_ += ".gz";
115 +      eorFilename_ += ".gz";
116 +    }
117 + #endif
118 +    
119   #ifdef IS_MPI
120 <  }
120 >
121 >    if (worldRank == 0) {
122   #endif // is_mpi
66 }
123  
124 +      
125 +      dumpFile_ = createOStream(filename_);
126 +
127 +      if (!dumpFile_) {
128 +        sprintf(painCave.errMsg, "Could not open \"%s\" for dump output.\n",
129 +                filename_.c_str());
130 +        painCave.isFatal = 1;
131 +        simError();
132 +      }
133 +
134   #ifdef IS_MPI
135  
136 < /**
71 < * A hook function to load balancing
72 < */
136 >    }
137  
138 < void DumpWriter::update(){
75 <  sortByGlobalIndex();          
76 < }
77 <  
78 < /**
79 < * Auxiliary sorting function
80 < */
81 <
82 < bool indexSortingCriterion(const pair<int, int>& p1, const pair<int, int>& p2){
83 <  return p1.second < p2.second;
84 < }
138 > #endif // is_mpi
139  
140 < /**
87 < * Sorting the local index by global index
88 < */
89 <
90 < void DumpWriter::sortByGlobalIndex(){
91 <  Molecule* mols = entry_plug->molecules;  
92 <  indexArray.clear();
140 >  }
141    
142 <  for(int i = 0; i < entry_plug->n_mol;i++)
143 <    indexArray.push_back(make_pair(i, mols[i].getGlobalIndex()));
144 <  
145 <  sort(indexArray.begin(), indexArray.end(), indexSortingCriterion);    
146 < }
142 >  DumpWriter::DumpWriter(SimInfo* info, const std::string& filename, bool writeDumpFile)
143 >    : info_(info), filename_(filename){
144 >    
145 >    Globals* simParams = info->getSimParams();
146 >    eorFilename_ = filename_.substr(0, filename_.rfind(".")) + ".eor";    
147 >    
148 >    needCompression_   = simParams->getCompressDumpFile();
149 >    needForceVector_   = simParams->getOutputForceVector();
150 >    needParticlePot_   = simParams->getOutputParticlePotential();
151 >    needFlucQ_         = simParams->getOutputFluctuatingCharges();
152 >    needElectricField_ = simParams->getOutputElectricField();
153  
154 + #ifdef HAVE_LIBZ
155 +    if (needCompression_) {
156 +      filename_ += ".gz";
157 +      eorFilename_ += ".gz";
158 +    }
159   #endif
160 +    
161 + #ifdef IS_MPI
162 +    
163 +    if (worldRank == 0) {
164 + #endif // is_mpi
165 +      
166 +      createDumpFile_ = writeDumpFile;
167 +      if (createDumpFile_) {
168 +        dumpFile_ = createOStream(filename_);
169 +      
170 +        if (!dumpFile_) {
171 +          sprintf(painCave.errMsg, "Could not open \"%s\" for dump output.\n",
172 +                  filename_.c_str());
173 +          painCave.isFatal = 1;
174 +          simError();
175 +        }
176 +      }
177 + #ifdef IS_MPI
178 +      
179 +    }
180  
181 < void DumpWriter::writeDump(double currentTime){
181 >    
182 > #endif // is_mpi
183 >    
184 >  }
185  
186 <  ofstream finalOut;
105 <  vector<ofstream*> fileStreams;
186 >  DumpWriter::~DumpWriter() {
187  
188   #ifdef IS_MPI
189 <  if(worldRank == 0 ){
190 < #endif    
110 <    finalOut.open( entry_plug->finalName.c_str(), ios::out | ios::trunc );
111 <    if( !finalOut ){
112 <      sprintf( painCave.errMsg,
113 <               "Could not open \"%s\" for final dump output.\n",
114 <               entry_plug->finalName.c_str() );
115 <      painCave.isFatal = 1;
116 <      simError();
117 <    }
118 < #ifdef IS_MPI
119 <  }
189 >
190 >    if (worldRank == 0) {
191   #endif // is_mpi
192 +      if (createDumpFile_){
193 +        writeClosing(*dumpFile_);
194 +        delete dumpFile_;
195 +      }
196 + #ifdef IS_MPI
197  
198 <  fileStreams.push_back(&finalOut);
123 <  fileStreams.push_back(&dumpFile);
198 >    }
199  
200 <  writeFrame(fileStreams, currentTime);
200 > #endif // is_mpi
201  
202 < #ifdef IS_MPI
128 <  finalOut.close();
129 < #endif
130 <        
131 < }
202 >  }
203  
204 < void DumpWriter::writeFinal(double currentTime){
204 >  void DumpWriter::writeFrameProperties(std::ostream& os, Snapshot* s) {
205  
206 <  ofstream finalOut;
136 <  vector<ofstream*> fileStreams;
206 >    char buffer[1024];
207  
208 < #ifdef IS_MPI
139 <  if(worldRank == 0 ){
140 < #endif // is_mpi
208 >    os << "    <FrameData>\n";
209  
210 <    finalOut.open( entry_plug->finalName.c_str(), ios::out | ios::trunc );
210 >    RealType currentTime = s->getTime();
211  
212 <    if( !finalOut ){
212 >    if (isinf(currentTime) || isnan(currentTime)) {      
213        sprintf( painCave.errMsg,
214 <               "Could not open \"%s\" for final dump output.\n",
147 <               entry_plug->finalName.c_str() );
214 >               "DumpWriter detected a numerical error writing the time");      
215        painCave.isFatal = 1;
216        simError();
217      }
218 +    
219 +    sprintf(buffer, "        Time: %.10g\n", currentTime);
220 +    os << buffer;
221  
222 < #ifdef IS_MPI
223 <  }
154 < #endif // is_mpi
155 <  
156 <  fileStreams.push_back(&finalOut);  
157 <  writeFrame(fileStreams, currentTime);
222 >    Mat3x3d hmat;
223 >    hmat = s->getHmat();
224  
225 < #ifdef IS_MPI
226 <  finalOut.close();
227 < #endif
228 <  
229 < }
225 >    for (unsigned int i = 0; i < 3; i++) {
226 >      for (unsigned int j = 0; j < 3; j++) {
227 >        if (isinf(hmat(i,j)) || isnan(hmat(i,j))) {      
228 >          sprintf( painCave.errMsg,
229 >                   "DumpWriter detected a numerical error writing the box");
230 >          painCave.isFatal = 1;
231 >          simError();
232 >        }        
233 >      }
234 >    }
235 >    
236 >    sprintf(buffer, "        Hmat: {{ %.10g, %.10g, %.10g }, { %.10g, %.10g, %.10g }, { %.10g, %.10g, %.10g }}\n",
237 >            hmat(0, 0), hmat(1, 0), hmat(2, 0),
238 >            hmat(0, 1), hmat(1, 1), hmat(2, 1),
239 >            hmat(0, 2), hmat(1, 2), hmat(2, 2));
240 >    os << buffer;
241  
242 < void DumpWriter::writeFrame( vector<ofstream*>& outFile, double currentTime ){
243 <
244 <  const int BUFFERSIZE = 2000;
245 <  const int MINIBUFFERSIZE = 100;
246 <
247 <  char tempBuffer[BUFFERSIZE];  
248 <  char writeLine[BUFFERSIZE];
249 <
250 <  int i;
251 <  unsigned int k;
242 >    RealType chi = s->getChi();
243 >    RealType integralOfChiDt = s->getIntegralOfChiDt();
244 >    if (isinf(chi) || isnan(chi) ||
245 >        isinf(integralOfChiDt) || isnan(integralOfChiDt)) {      
246 >      sprintf( painCave.errMsg,
247 >               "DumpWriter detected a numerical error writing the thermostat");
248 >      painCave.isFatal = 1;
249 >      simError();
250 >    }
251 >    sprintf(buffer, "  Thermostat: %.10g , %.10g\n", chi, integralOfChiDt);
252 >    os << buffer;
253  
254 < #ifdef IS_MPI
255 <  
178 <  /*********************************************************************
179 <   * Documentation?  You want DOCUMENTATION?
180 <   *
181 <   * Why all the potatoes below?  
182 <   *
183 <   * To make a long story short, the original version of DumpWriter
184 <   * worked in the most inefficient way possible.  Node 0 would
185 <   * poke each of the node for an individual atom's formatted data
186 <   * as node 0 worked its way down the global index. This was particularly
187 <   * inefficient since the method blocked all processors at every atom
188 <   * (and did it twice!).
189 <   *
190 <   * An intermediate version of DumpWriter could be described from Node
191 <   * zero's perspective as follows:
192 <   *
193 <   *  1) Have 100 of your friends stand in a circle.
194 <   *  2) When you say go, have all of them start tossing potatoes at
195 <   *     you (one at a time).
196 <   *  3) Catch the potatoes.
197 <   *
198 <   * It was an improvement, but MPI has buffers and caches that could
199 <   * best be described in this analogy as "potato nets", so there's no
200 <   * need to block the processors atom-by-atom.
201 <   *
202 <   * This new and improved DumpWriter works in an even more efficient
203 <   * way:
204 <   *
205 <   *  1) Have 100 of your friend stand in a circle.
206 <   *  2) When you say go, have them start tossing 5-pound bags of
207 <   *     potatoes at you.
208 <   *  3) Once you've caught a friend's bag of potatoes,
209 <   *     toss them a spud to let them know they can toss another bag.
210 <   *
211 <   * How's THAT for documentation?
212 <   *
213 <   *********************************************************************/
254 >    Mat3x3d eta;
255 >    eta = s->getEta();
256  
257 <  int *potatoes;
258 <  int myPotato;
257 >    for (unsigned int i = 0; i < 3; i++) {
258 >      for (unsigned int j = 0; j < 3; j++) {
259 >        if (isinf(eta(i,j)) || isnan(eta(i,j))) {      
260 >          sprintf( painCave.errMsg,
261 >                   "DumpWriter detected a numerical error writing the barostat");
262 >          painCave.isFatal = 1;
263 >          simError();
264 >        }        
265 >      }
266 >    }
267  
268 <  int nProc;
269 <  int j, which_node, done, which_atom, local_index, currentIndex;
270 <  double atomData[13];
271 <  int isDirectional;
272 <  char* atomTypeString;
223 <  char MPIatomTypeString[MINIBUFFERSIZE];
224 <  int nObjects;
225 <  int msgLen; // the length of message actually recieved at master nodes
226 < #endif //is_mpi
268 >    sprintf(buffer, "    Barostat: {{ %.10g, %.10g, %.10g }, { %.10g, %.10g, %.10g }, { %.10g, %.10g, %.10g }}\n",
269 >            eta(0, 0), eta(1, 0), eta(2, 0),
270 >            eta(0, 1), eta(1, 1), eta(2, 1),
271 >            eta(0, 2), eta(1, 2), eta(2, 2));
272 >    os << buffer;
273  
274 <  double q[4], ji[3];
275 <  DirectionalAtom* dAtom;
230 <  double pos[3], vel[3];
231 <  int nTotObjects;
232 <  StuntDouble* sd;
233 <  char* molName;
234 <  vector<StuntDouble*> integrableObjects;
235 <  vector<StuntDouble*>::iterator iter;
236 <  nTotObjects = entry_plug->getTotIntegrableObjects();
237 < #ifndef IS_MPI
238 <  
239 <  for(k = 0; k < outFile.size(); k++){
240 <    *outFile[k] << nTotObjects << "\n";
274 >    os << "    </FrameData>\n";
275 >  }
276  
277 <    *outFile[k] << currentTime << ";\t"
243 <               << entry_plug->Hmat[0][0] << "\t"
244 <                     << entry_plug->Hmat[1][0] << "\t"
245 <                     << entry_plug->Hmat[2][0] << ";\t"
246 <              
247 <               << entry_plug->Hmat[0][1] << "\t"
248 <                     << entry_plug->Hmat[1][1] << "\t"
249 <                     << entry_plug->Hmat[2][1] << ";\t"
277 >  void DumpWriter::writeFrame(std::ostream& os) {
278  
279 <                     << entry_plug->Hmat[0][2] << "\t"
280 <                     << entry_plug->Hmat[1][2] << "\t"
281 <                     << entry_plug->Hmat[2][2] << ";";
279 > #ifdef IS_MPI
280 >    MPI_Status istatus;
281 > #endif
282  
283 <    //write out additional parameters, such as chi and eta
284 <    *outFile[k] << entry_plug->the_integrator->getAdditionalParameters() << endl;
285 <  }
286 <  
259 <  for( i=0; i< entry_plug->n_mol; i++ ){
283 >    Molecule* mol;
284 >    StuntDouble* integrableObject;
285 >    SimInfo::MoleculeIterator mi;
286 >    Molecule::IntegrableObjectIterator ii;
287  
288 <    integrableObjects = entry_plug->molecules[i].getIntegrableObjects();
289 <    molName = (entry_plug->compStamps[entry_plug->molecules[i].getStampID()])->getID();
290 <    
291 <    for( iter = integrableObjects.begin();iter !=  integrableObjects.end(); ++iter){
265 <      sd = *iter;
266 <      sd->getPos(pos);
267 <      sd->getVel(vel);
288 > #ifndef IS_MPI
289 >    os << "  <Snapshot>\n";
290 >
291 >    writeFrameProperties(os, info_->getSnapshotManager()->getCurrentSnapshot());
292  
293 <      sprintf( tempBuffer,
294 <             "%s\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t",
271 <             sd->getType(),
272 <             pos[0],
273 <             pos[1],
274 <             pos[2],
275 <             vel[0],
276 <             vel[1],
277 <             vel[2]);
278 <      strcpy( writeLine, tempBuffer );
293 >    os << "    <StuntDoubles>\n";
294 >    for (mol = info_->beginMolecule(mi); mol != NULL; mol = info_->nextMolecule(mi)) {
295  
296 <      if( sd->isDirectional() ){
296 >      
297 >      for (integrableObject = mol->beginIntegrableObject(ii); integrableObject != NULL;  
298 >           integrableObject = mol->nextIntegrableObject(ii)) {  
299 >          os << prepareDumpLine(integrableObject);
300 >          
301 >      }
302 >    }    
303 >    os << "    </StuntDoubles>\n";
304 >    
305 >    os << "  </Snapshot>\n";
306  
307 <        sd->getQ( q );
308 <        sd->getJ( ji );
307 >    os.flush();
308 > #else
309 >    //every node prepares the dump lines for integrable objects belong to itself
310 >    std::string buffer;
311 >    for (mol = info_->beginMolecule(mi); mol != NULL; mol = info_->nextMolecule(mi)) {
312  
313 <        sprintf( tempBuffer,
314 <               "%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\n",
315 <               q[0],
316 <               q[1],
289 <               q[2],
290 <               q[3],
291 <                 ji[0],
292 <                 ji[1],
293 <                 ji[2]);
294 <        strcat( writeLine, tempBuffer );
313 >
314 >      for (integrableObject = mol->beginIntegrableObject(ii); integrableObject != NULL;
315 >           integrableObject = mol->nextIntegrableObject(ii)) {  
316 >          buffer += prepareDumpLine(integrableObject);
317        }
296      else
297        strcat( writeLine, "0.0\t0.0\t0.0\t0.0\t0.0\t0.0\t0.0\n" );
298    
299      for(k = 0; k < outFile.size(); k++)
300        *outFile[k] << writeLine;      
318      }
319      
320 <  }
320 >    const int masterNode = 0;
321 >    int nProc;
322 >    MPI_Comm_size(MPI_COMM_WORLD, &nProc);
323 >    if (worldRank == masterNode) {      
324 >      os << "  <Snapshot>\n";  
325 >      writeFrameProperties(os, info_->getSnapshotManager()->getCurrentSnapshot());
326 >      os << "    <StuntDoubles>\n";
327 >        
328 >      os << buffer;
329  
330 < #else // is_mpi
330 >      for (int i = 1; i < nProc; ++i) {
331  
332 <  /* code to find maximum tag value */
333 <  
309 <  int *tagub, flag, MAXTAG;
310 <  MPI_Attr_get(MPI_COMM_WORLD, MPI_TAG_UB, &tagub, &flag);
311 <  if (flag) {
312 <    MAXTAG = *tagub;
313 <  } else {
314 <    MAXTAG = 32767;
315 <  }  
332 >        // receive the length of the string buffer that was
333 >        // prepared by processor i
334  
335 <  int haveError;
335 >        MPI_Bcast(&i, 1, MPI_INT,masterNode,MPI_COMM_WORLD);
336 >        int recvLength;
337 >        MPI_Recv(&recvLength, 1, MPI_INT, i, 0, MPI_COMM_WORLD, &istatus);
338 >        char* recvBuffer = new char[recvLength];
339 >        if (recvBuffer == NULL) {
340 >        } else {
341 >          MPI_Recv(recvBuffer, recvLength, MPI_CHAR, i, 0, MPI_COMM_WORLD, &istatus);
342 >          os << recvBuffer;
343 >          delete [] recvBuffer;
344 >        }
345 >      }
346 >      os << "    </StuntDoubles>\n";
347 >      
348 >      os << "  </Snapshot>\n";
349 >      os.flush();
350 >    } else {
351 >      int sendBufferLength = buffer.size() + 1;
352 >      int myturn = 0;
353 >      for (int i = 1; i < nProc; ++i){
354 >        MPI_Bcast(&myturn,1, MPI_INT,masterNode,MPI_COMM_WORLD);
355 >        if (myturn == worldRank){
356 >          MPI_Send(&sendBufferLength, 1, MPI_INT, masterNode, 0, MPI_COMM_WORLD);
357 >          MPI_Send((void *)buffer.c_str(), sendBufferLength, MPI_CHAR, masterNode, 0, MPI_COMM_WORLD);
358 >        }
359 >      }
360 >    }
361  
362 <  MPI_Status istatus;
320 <  int nCurObj;
321 <  int *MolToProcMap = mpiSim->getMolToProcMap();
362 > #endif // is_mpi
363  
364 <  // write out header and node 0's coordinates
364 >  }
365  
366 <  if( worldRank == 0 ){
366 >  std::string DumpWriter::prepareDumpLine(StuntDouble* integrableObject) {
367 >        
368 >    int index = integrableObject->getGlobalIntegrableObjectIndex();
369 >    std::string type("pv");
370 >    std::string line;
371 >    char tempBuffer[4096];
372  
373 <    // Node 0 needs a list of the magic potatoes for each processor;
373 >    Vector3d pos;
374 >    Vector3d vel;
375 >    pos = integrableObject->getPos();
376  
377 <    nProc = mpiSim->getNProcessors();
378 <    potatoes = new int[nProc];
377 >    if (isinf(pos[0]) || isnan(pos[0]) ||
378 >        isinf(pos[1]) || isnan(pos[1]) ||
379 >        isinf(pos[2]) || isnan(pos[2]) ) {      
380 >      sprintf( painCave.errMsg,
381 >               "DumpWriter detected a numerical error writing the position"
382 >               " for object %d", index);      
383 >      painCave.isFatal = 1;
384 >      simError();
385 >    }
386  
387 <    //write out the comment lines
388 <    for (i = 0; i < nProc; i++)
389 <      potatoes[i] = 0;
390 <    
391 <    for(k = 0; k < outFile.size(); k++){
392 <      *outFile[k] << nTotObjects << "\n";
387 >    vel = integrableObject->getVel();          
388 >
389 >    if (isinf(vel[0]) || isnan(vel[0]) ||
390 >        isinf(vel[1]) || isnan(vel[1]) ||
391 >        isinf(vel[2]) || isnan(vel[2]) ) {      
392 >      sprintf( painCave.errMsg,
393 >               "DumpWriter detected a numerical error writing the velocity"
394 >               " for object %d", index);      
395 >      painCave.isFatal = 1;
396 >      simError();
397 >    }
398 >
399 >    sprintf(tempBuffer, "%18.10g %18.10g %18.10g %13e %13e %13e",
400 >            pos[0], pos[1], pos[2],
401 >            vel[0], vel[1], vel[2]);                    
402 >    line += tempBuffer;
403 >
404 >    if (integrableObject->isDirectional()) {
405 >      type += "qj";
406 >      Quat4d q;
407 >      Vector3d ji;
408 >      q = integrableObject->getQ();
409 >
410 >      if (isinf(q[0]) || isnan(q[0]) ||
411 >          isinf(q[1]) || isnan(q[1]) ||
412 >          isinf(q[2]) || isnan(q[2]) ||
413 >          isinf(q[3]) || isnan(q[3]) ) {      
414 >        sprintf( painCave.errMsg,
415 >                 "DumpWriter detected a numerical error writing the quaternion"
416 >                 " for object %d", index);      
417 >        painCave.isFatal = 1;
418 >        simError();
419 >      }
420 >
421 >      ji = integrableObject->getJ();
422 >
423 >      if (isinf(ji[0]) || isnan(ji[0]) ||
424 >          isinf(ji[1]) || isnan(ji[1]) ||
425 >          isinf(ji[2]) || isnan(ji[2]) ) {      
426 >        sprintf( painCave.errMsg,
427 >                 "DumpWriter detected a numerical error writing the angular"
428 >                 " momentum for object %d", index);      
429 >        painCave.isFatal = 1;
430 >        simError();
431 >      }
432 >
433 >      sprintf(tempBuffer, " %13e %13e %13e %13e %13e %13e %13e",
434 >              q[0], q[1], q[2], q[3],
435 >              ji[0], ji[1], ji[2]);
436 >      line += tempBuffer;
437 >    }
438 >
439 >    if (needForceVector_) {
440 >      type += "f";
441 >      Vector3d frc = integrableObject->getFrc();
442 >      if (isinf(frc[0]) || isnan(frc[0]) ||
443 >          isinf(frc[1]) || isnan(frc[1]) ||
444 >          isinf(frc[2]) || isnan(frc[2]) ) {      
445 >        sprintf( painCave.errMsg,
446 >                 "DumpWriter detected a numerical error writing the force"
447 >                 " for object %d", index);      
448 >        painCave.isFatal = 1;
449 >        simError();
450 >      }
451 >      sprintf(tempBuffer, " %13e %13e %13e",
452 >              frc[0], frc[1], frc[2]);
453 >      line += tempBuffer;
454        
455 <      *outFile[k] << currentTime << ";\t"
456 <                  << entry_plug->Hmat[0][0] << "\t"
457 <                  << entry_plug->Hmat[1][0] << "\t"
458 <                  << entry_plug->Hmat[2][0] << ";\t"
459 <        
460 <                  << entry_plug->Hmat[0][1] << "\t"
461 <                  << entry_plug->Hmat[1][1] << "\t"
462 <                  << entry_plug->Hmat[2][1] << ";\t"
463 <        
464 <                  << entry_plug->Hmat[0][2] << "\t"
465 <                  << entry_plug->Hmat[1][2] << "\t"
466 <                  << entry_plug->Hmat[2][2] << ";";
467 <      
468 <      *outFile[k] << entry_plug->the_integrator->getAdditionalParameters()
469 <                  << endl;
455 >      if (integrableObject->isDirectional()) {
456 >        type += "t";
457 >        Vector3d trq = integrableObject->getTrq();        
458 >        if (isinf(trq[0]) || isnan(trq[0]) ||
459 >            isinf(trq[1]) || isnan(trq[1]) ||
460 >            isinf(trq[2]) || isnan(trq[2]) ) {      
461 >          sprintf( painCave.errMsg,
462 >                   "DumpWriter detected a numerical error writing the torque"
463 >                   " for object %d", index);      
464 >          painCave.isFatal = 1;
465 >          simError();
466 >        }        
467 >        sprintf(tempBuffer, " %13e %13e %13e",
468 >                trq[0], trq[1], trq[2]);
469 >        line += tempBuffer;
470 >      }      
471      }
472 +
473 +    if (needParticlePot_) {
474 +      type += "u";
475 +      RealType particlePot = integrableObject->getParticlePot();
476 +      if (isinf(particlePot) || isnan(particlePot)) {      
477 +        sprintf( painCave.errMsg,
478 +                 "DumpWriter detected a numerical error writing the particle "
479 +                 " potential for object %d", index);      
480 +        painCave.isFatal = 1;
481 +        simError();
482 +      }
483 +      sprintf(tempBuffer, " %13e", particlePot);
484 +      line += tempBuffer;
485 +    }
486      
487 <    currentIndex = 0;
487 >    if (needFlucQ_) {
488 >      type += "cw";
489 >      RealType fqPos = integrableObject->getFlucQPos();
490 >      if (isinf(fqPos) || isnan(fqPos) ) {      
491 >        sprintf( painCave.errMsg,
492 >                 "DumpWriter detected a numerical error writing the"
493 >                 " fluctuating charge for object %d", index);      
494 >        painCave.isFatal = 1;
495 >        simError();
496 >      }
497 >      sprintf(tempBuffer, " %13e ", fqPos);
498 >      line += tempBuffer;
499      
500 <    for (i = 0 ; i < mpiSim->getNMolGlobal(); i++ ) {
501 <      
502 <      // Get the Node number which has this atom;
503 <      
504 <      which_node = MolToProcMap[i];
505 <      
506 <      if (which_node != 0) {
507 <        
508 <        if (potatoes[which_node] + 1 >= MAXTAG) {
509 <          // The potato was going to exceed the maximum value,
368 <          // so wrap this processor potato back to 0:        
500 >      RealType fqVel = integrableObject->getFlucQVel();
501 >      if (isinf(fqVel) || isnan(fqVel) ) {      
502 >        sprintf( painCave.errMsg,
503 >                 "DumpWriter detected a numerical error writing the"
504 >                 " fluctuating charge velocity for object %d", index);      
505 >        painCave.isFatal = 1;
506 >        simError();
507 >      }
508 >      sprintf(tempBuffer, " %13e ", fqVel);
509 >      line += tempBuffer;
510  
511 <          potatoes[which_node] = 0;          
512 <          MPI_Send(&potatoes[which_node], 1, MPI_INT, which_node, 0,
513 <                   MPI_COMM_WORLD);
514 <          
511 >      if (needForceVector_) {
512 >        type += "g";
513 >        RealType fqFrc = integrableObject->getFlucQFrc();        
514 >        if (isinf(fqFrc) || isnan(fqFrc) ) {      
515 >          sprintf( painCave.errMsg,
516 >                   "DumpWriter detected a numerical error writing the"
517 >                   " fluctuating charge force for object %d", index);      
518 >          painCave.isFatal = 1;
519 >          simError();
520          }
521 +        sprintf(tempBuffer, " %13e ", fqFrc);        
522 +        line += tempBuffer;
523 +      }
524 +    }
525  
526 <        myPotato = potatoes[which_node];        
526 >    if (needElectricField_) {
527 >      type += "e";
528 >      Vector3d eField= integrableObject->getElectricField();
529 >      if (isinf(eField[0]) || isnan(eField[0]) ||
530 >          isinf(eField[1]) || isnan(eField[1]) ||
531 >          isinf(eField[2]) || isnan(eField[2]) ) {      
532 >        sprintf( painCave.errMsg,
533 >                 "DumpWriter detected a numerical error writing the electric"
534 >                 " field for object %d", index);      
535 >        painCave.isFatal = 1;
536 >        simError();
537 >      }
538 >      sprintf(tempBuffer, " %13e %13e %13e",
539 >              eField[0], eField[1], eField[2]);
540 >      line += tempBuffer;
541 >    }
542  
543 <        //recieve the number of integrableObject in current molecule
544 <        MPI_Recv(&nCurObj, 1, MPI_INT, which_node,
545 <                 myPotato, MPI_COMM_WORLD, &istatus);
381 <        myPotato++;
382 <        
383 <        for(int l = 0; l < nCurObj; l++){
543 >    sprintf(tempBuffer, "%10d %7s %s\n", index, type.c_str(), line.c_str());
544 >    return std::string(tempBuffer);
545 >  }
546  
547 <          if (potatoes[which_node] + 2 >= MAXTAG) {
548 <            // The potato was going to exceed the maximum value,
549 <            // so wrap this processor potato back to 0:        
547 >  void DumpWriter::writeDump() {
548 >    writeFrame(*dumpFile_);
549 >  }
550  
551 <            potatoes[which_node] = 0;          
552 <            MPI_Send(&potatoes[which_node], 1, MPI_INT, which_node, 0, MPI_COMM_WORLD);
553 <            
554 <          }
551 >  void DumpWriter::writeEor() {
552 >    std::ostream* eorStream;
553 >    
554 > #ifdef IS_MPI
555 >    if (worldRank == 0) {
556 > #endif // is_mpi
557  
558 <          MPI_Recv(MPIatomTypeString, MINIBUFFERSIZE, MPI_CHAR, which_node,
395 <          myPotato, MPI_COMM_WORLD, &istatus);
558 >      eorStream = createOStream(eorFilename_);
559  
560 <          atomTypeString = MPIatomTypeString;
560 > #ifdef IS_MPI
561 >    }
562 > #endif // is_mpi    
563  
564 <          myPotato++;
564 >    writeFrame(*eorStream);
565  
566 <          MPI_Recv(atomData, 13, MPI_DOUBLE, which_node, myPotato, MPI_COMM_WORLD, &istatus);
567 <          myPotato++;
566 > #ifdef IS_MPI
567 >    if (worldRank == 0) {
568 > #endif // is_mpi
569 >      writeClosing(*eorStream);
570 >      delete eorStream;
571 > #ifdef IS_MPI
572 >    }
573 > #endif // is_mpi  
574  
575 <          MPI_Get_count(&istatus, MPI_DOUBLE, &msgLen);
575 >  }
576  
406          if(msgLen  == 13)
407            isDirectional = 1;
408          else
409            isDirectional = 0;
410          
411          // If we've survived to here, format the line:
412            
413          if (!isDirectional) {
414        
415            sprintf( writeLine,
416                 "%s\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t",
417                 atomTypeString,
418                 atomData[0],
419                 atomData[1],
420                 atomData[2],
421                 atomData[3],
422                 atomData[4],
423                 atomData[5]);
424        
425           strcat( writeLine, "0.0\t0.0\t0.0\t0.0\t0.0\t0.0\t0.0\n" );
426        
427          }
428          else {
429        
430                sprintf( writeLine,
431                         "%s\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\n",
432                         atomTypeString,
433                         atomData[0],
434                         atomData[1],
435                         atomData[2],
436                         atomData[3],
437                         atomData[4],
438                         atomData[5],
439                         atomData[6],
440                         atomData[7],
441                         atomData[8],
442                         atomData[9],
443                         atomData[10],
444                         atomData[11],
445                         atomData[12]);
446            
447          }
448          
449          for(k = 0; k < outFile.size(); k++)
450            *outFile[k] << writeLine;            
577  
578 <        }// end for(int l =0)
579 <        potatoes[which_node] = myPotato;
580 <        
581 <      }
582 <      else {
583 <        
584 <        haveError = 0;
578 >  void DumpWriter::writeDumpAndEor() {
579 >    std::vector<std::streambuf*> buffers;
580 >    std::ostream* eorStream;
581 > #ifdef IS_MPI
582 >    if (worldRank == 0) {
583 > #endif // is_mpi
584 >
585 >      buffers.push_back(dumpFile_->rdbuf());
586 >
587 >      eorStream = createOStream(eorFilename_);
588 >
589 >      buffers.push_back(eorStream->rdbuf());
590          
591 <            local_index = indexArray[currentIndex].first;        
592 <            
593 <            integrableObjects = (entry_plug->molecules[local_index]).getIntegrableObjects();
591 > #ifdef IS_MPI
592 >    }
593 > #endif // is_mpi    
594  
595 <        for(iter= integrableObjects.begin(); iter != integrableObjects.end(); ++iter){    
596 <                sd = *iter;
466 <            atomTypeString = sd->getType();
467 <            
468 <            sd->getPos(pos);
469 <            sd->getVel(vel);          
470 <          
471 <            atomData[0] = pos[0];
472 <            atomData[1] = pos[1];
473 <            atomData[2] = pos[2];
595 >    TeeBuf tbuf(buffers.begin(), buffers.end());
596 >    std::ostream os(&tbuf);
597  
598 <            atomData[3] = vel[0];
476 <            atomData[4] = vel[1];
477 <            atomData[5] = vel[2];
478 <              
479 <            isDirectional = 0;
598 >    writeFrame(os);
599  
600 <            if( sd->isDirectional() ){
601 <
602 <              isDirectional = 1;
603 <                
604 <              sd->getQ( q );
605 <              sd->getJ( ji );
606 <
607 <              for (int j = 0; j < 6 ; j++)
489 <                atomData[j] = atomData[j];            
490 <              
491 <              atomData[6] = q[0];
492 <              atomData[7] = q[1];
493 <              atomData[8] = q[2];
494 <              atomData[9] = q[3];
495 <              
496 <              atomData[10] = ji[0];
497 <              atomData[11] = ji[1];
498 <              atomData[12] = ji[2];
499 <            }
500 <            
501 <            // If we've survived to here, format the line:
502 <            
503 <            if (!isDirectional) {
504 <        
505 <              sprintf( writeLine,
506 <                 "%s\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t",
507 <                 atomTypeString,
508 <                 atomData[0],
509 <                 atomData[1],
510 <                 atomData[2],
511 <                 atomData[3],
512 <                 atomData[4],
513 <                 atomData[5]);
514 <        
515 <             strcat( writeLine, "0.0\t0.0\t0.0\t0.0\t0.0\t0.0\t0.0\n" );
516 <        
517 <            }
518 <            else {
519 <        
520 <                sprintf( writeLine,
521 <                         "%s\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\n",
522 <                         atomTypeString,
523 <                         atomData[0],
524 <                         atomData[1],
525 <                         atomData[2],
526 <                         atomData[3],
527 <                         atomData[4],
528 <                         atomData[5],
529 <                         atomData[6],
530 <                         atomData[7],
531 <                         atomData[8],
532 <                         atomData[9],
533 <                         atomData[10],
534 <                         atomData[11],
535 <                         atomData[12]);
536 <              
537 <            }
538 <            
539 <            for(k = 0; k < outFile.size(); k++)
540 <              *outFile[k] << writeLine;
541 <            
542 <            
543 <        }//end for(iter = integrableObject.begin())
544 <        
545 <      currentIndex++;
546 <      }
547 <
548 <    }//end for(i = 0; i < mpiSim->getNmol())
600 > #ifdef IS_MPI
601 >    if (worldRank == 0) {
602 > #endif // is_mpi
603 >      writeClosing(*eorStream);
604 >      delete eorStream;
605 > #ifdef IS_MPI
606 >    }
607 > #endif // is_mpi  
608      
609 <    for(k = 0; k < outFile.size(); k++)
551 <      outFile[k]->flush();
552 <    
553 <    sprintf( checkPointMsg,
554 <             "Sucessfully took a dump.\n");
555 <    
556 <    MPIcheckPoint();        
557 <    
558 <    delete[] potatoes;
559 <    
560 <  } else {
609 >  }
610  
611 <    // worldRank != 0, so I'm a remote node.  
611 >  std::ostream* DumpWriter::createOStream(const std::string& filename) {
612  
613 <    // Set my magic potato to 0:
613 >    std::ostream* newOStream;
614 > #ifdef HAVE_LIBZ
615 >    if (needCompression_) {
616 >      newOStream = new ogzstream(filename.c_str());
617 >    } else {
618 >      newOStream = new std::ofstream(filename.c_str());
619 >    }
620 > #else
621 >    newOStream = new std::ofstream(filename.c_str());
622 > #endif
623 >    //write out MetaData first
624 >    (*newOStream) << "<OpenMD version=1>" << std::endl;
625 >    (*newOStream) << "  <MetaData>" << std::endl;
626 >    (*newOStream) << info_->getRawMetaData();
627 >    (*newOStream) << "  </MetaData>" << std::endl;
628 >    return newOStream;
629 >  }
630  
631 <    myPotato = 0;
567 <    currentIndex = 0;
568 <    
569 <    for (i = 0 ; i < mpiSim->getNMolGlobal(); i++ ) {
570 <      
571 <      // Am I the node which has this integrableObject?
572 <      
573 <      if (MolToProcMap[i] == worldRank) {
631 >  void DumpWriter::writeClosing(std::ostream& os) {
632  
633 <
634 <        if (myPotato + 1 >= MAXTAG) {
577 <          
578 <          // The potato was going to exceed the maximum value,
579 <          // so wrap this processor potato back to 0 (and block until
580 <          // node 0 says we can go:
581 <          
582 <          MPI_Recv(&myPotato, 1, MPI_INT, 0, 0, MPI_COMM_WORLD, &istatus);
583 <          
584 <        }
585 <
586 <          local_index = indexArray[currentIndex].first;        
587 <          integrableObjects = entry_plug->molecules[local_index].getIntegrableObjects();
588 <          
589 <          nCurObj = integrableObjects.size();
590 <                      
591 <          MPI_Send(&nCurObj, 1, MPI_INT, 0,
592 <                   myPotato, MPI_COMM_WORLD);
593 <          myPotato++;
594 <
595 <          for( iter = integrableObjects.begin(); iter  != integrableObjects.end(); iter++){
596 <
597 <            if (myPotato + 2 >= MAXTAG) {
598 <          
599 <              // The potato was going to exceed the maximum value,
600 <              // so wrap this processor potato back to 0 (and block until
601 <              // node 0 says we can go:
602 <          
603 <              MPI_Recv(&myPotato, 1, MPI_INT, 0, 0, MPI_COMM_WORLD, &istatus);
604 <              
605 <            }
606 <            
607 <            sd = *iter;
608 <            
609 <            atomTypeString = sd->getType();
610 <
611 <            sd->getPos(pos);
612 <            sd->getVel(vel);
613 <
614 <            atomData[0] = pos[0];
615 <            atomData[1] = pos[1];
616 <            atomData[2] = pos[2];
617 <
618 <            atomData[3] = vel[0];
619 <            atomData[4] = vel[1];
620 <            atomData[5] = vel[2];
621 <              
622 <            isDirectional = 0;
623 <
624 <            if( sd->isDirectional() ){
625 <
626 <                isDirectional = 1;
627 <                
628 <                sd->getQ( q );
629 <                sd->getJ( ji );
630 <                
631 <                
632 <                atomData[6] = q[0];
633 <                atomData[7] = q[1];
634 <                atomData[8] = q[2];
635 <                atomData[9] = q[3];
636 <      
637 <                atomData[10] = ji[0];
638 <                atomData[11] = ji[1];
639 <                atomData[12] = ji[2];
640 <              }
641 <
642 <            
643 <            strncpy(MPIatomTypeString, atomTypeString, MINIBUFFERSIZE);
644 <
645 <            // null terminate the string before sending (just in case):
646 <            MPIatomTypeString[MINIBUFFERSIZE-1] = '\0';
647 <
648 <            MPI_Send(MPIatomTypeString, MINIBUFFERSIZE, MPI_CHAR, 0,
649 <                             myPotato, MPI_COMM_WORLD);
650 <            
651 <            myPotato++;
652 <            
653 <            if (isDirectional) {
654 <
655 <              MPI_Send(atomData, 13, MPI_DOUBLE, 0,
656 <                       myPotato, MPI_COMM_WORLD);
657 <              
658 <            } else {
659 <
660 <              MPI_Send(atomData, 6, MPI_DOUBLE, 0,
661 <                       myPotato, MPI_COMM_WORLD);
662 <            }
663 <
664 <            myPotato++;  
665 <
666 <          }
667 <
668 <          currentIndex++;    
669 <          
670 <        }
671 <      
672 <      }
673 <
674 <    sprintf( checkPointMsg,
675 <             "Successfully took a dump.\n");
676 <    MPIcheckPoint();                
677 <    
633 >    os << "</OpenMD>\n";
634 >    os.flush();
635    }
679  
680 #endif // is_mpi
681 }
636  
637 < #ifdef IS_MPI
684 <
685 < // a couple of functions to let us escape the write loop
686 <
687 < void dWrite::DieDieDie( void ){
688 <
689 <  MPI_Finalize();
690 <  exit (0);
691 < }
692 <
693 < #endif //is_mpi
637 > }//end namespace OpenMD

Comparing:
trunk/src/io/DumpWriter.cpp (property svn:keywords), Revision 221 by chrisfen, Tue Nov 23 22:48:31 2004 UTC vs.
branches/development/src/io/DumpWriter.cpp (property svn:keywords), Revision 1714 by gezelter, Sat May 19 18:12:46 2012 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines