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 2 by gezelter, Fri Sep 24 04:16:43 2004 UTC vs.
branches/development/src/io/DumpWriter.cpp (file contents), Revision 1746 by gezelter, Wed Jun 6 02:18:54 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 "mpiSimulation.hpp"
13
14 namespace dWrite{
15  void DieDieDie( void );
16 }
17
18 using namespace dWrite;
53   #endif //is_mpi
54  
55 < #include "ReadWrite.hpp"
56 < #include "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;
259 <
260 <  int nProc;
261 <  int j, which_node, done, which_atom, local_index, currentIndex;
262 <  double atomData[13];
263 <  int isDirectional;
264 <  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
227 <
228 <  double q[4], ji[3];
229 <  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";
241 <
242 <    *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"
250 <
251 <                     << entry_plug->Hmat[0][2] << "\t"
252 <                     << entry_plug->Hmat[1][2] << "\t"
253 <                     << entry_plug->Hmat[2][2] << ";";
254 <
255 <    //write out additional parameters, such as chi and eta
256 <    *outFile[k] << entry_plug->the_integrator->getAdditionalParameters() << endl;
257 <  }
258 <  
259 <  for( i=0; i< entry_plug->n_mol; i++ ){
260 <
261 <    integrableObjects = entry_plug->molecules[i].getIntegrableObjects();
262 <    molName = (entry_plug->compStamps[entry_plug->molecules[i].getStampID()])->getID();
263 <    
264 <    for( iter = integrableObjects.begin();iter !=  integrableObjects.end(); ++iter){
265 <      sd = *iter;
266 <      sd->getPos(pos);
267 <      sd->getVel(vel);
268 <
269 <      sprintf( tempBuffer,
270 <             "%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 );
279 <
280 <      if( sd->isDirectional() ){
281 <
282 <        sd->getQ( q );
283 <        sd->getJ( ji );
284 <
285 <        sprintf( tempBuffer,
286 <               "%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\n",
287 <               q[0],
288 <               q[1],
289 <               q[2],
290 <               q[3],
291 <                 ji[0],
292 <                 ji[1],
293 <                 ji[2]);
294 <        strcat( writeLine, tempBuffer );
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        }
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;      
266      }
267  
268 < }
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 < #else // is_mpi
274 >    os << "    </FrameData>\n";
275 >  }
276  
277 <  /* code to find maximum tag value */
308 <  
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 <  }  
277 >  void DumpWriter::writeFrame(std::ostream& os) {
278  
279 <  int haveError;
279 > #ifdef IS_MPI
280 >    MPI_Status istatus;
281 > #endif
282  
283 <  MPI_Status istatus;
284 <  int nCurObj;
285 <  int *MolToProcMap = mpiSim->getMolToProcMap();
283 >    Molecule* mol;
284 >    StuntDouble* integrableObject;
285 >    SimInfo::MoleculeIterator mi;
286 >    Molecule::IntegrableObjectIterator ii;
287  
288 <  // write out header and node 0's coordinates
288 > #ifndef IS_MPI
289 >    os << "  <Snapshot>\n";
290 >
291 >    writeFrameProperties(os, info_->getSnapshotManager()->getCurrentSnapshot());
292  
293 <  if( worldRank == 0 ){
293 >    os << "    <StuntDoubles>\n";
294 >    for (mol = info_->beginMolecule(mi); mol != NULL; mol = info_->nextMolecule(mi)) {
295  
296 <    // Node 0 needs a list of the magic potatoes for each processor;
297 <
298 <    nProc = mpiSim->getNProcessors();
299 <    potatoes = new int[nProc];
300 <
301 <    //write out the comment lines
302 <    for (i = 0; i < nProc; i++)
303 <      potatoes[i] = 0;
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 <      for(k = 0; k < outFile.size(); k++){
337 <        *outFile[k] << nTotObjects << "\n";
305 >    os << "  </Snapshot>\n";
306  
307 <        *outFile[k] << currentTime << ";\t"
308 <                         << entry_plug->Hmat[0][0] << "\t"
309 <                         << entry_plug->Hmat[1][0] << "\t"
310 <                         << entry_plug->Hmat[2][0] << ";\t"
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  
344                         << entry_plug->Hmat[0][1] << "\t"
345                         << entry_plug->Hmat[1][1] << "\t"
346                         << entry_plug->Hmat[2][1] << ";\t"
313  
314 <                         << entry_plug->Hmat[0][2] << "\t"
315 <                         << entry_plug->Hmat[1][2] << "\t"
316 <                         << entry_plug->Hmat[2][2] << ";";
317 <  
352 <        *outFile[k] << entry_plug->the_integrator->getAdditionalParameters() << endl;
314 >      for (integrableObject = mol->beginIntegrableObject(ii); integrableObject != NULL;
315 >           integrableObject = mol->nextIntegrableObject(ii)) {  
316 >          buffer += prepareDumpLine(integrableObject);
317 >      }
318      }
319 +    
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 <    currentIndex = 0;
330 >      for (int i = 1; i < nProc; ++i) {
331  
332 <    for (i = 0 ; i < mpiSim->getNMolGlobal(); i++ ) {
333 <      
359 <      // Get the Node number which has this atom;
360 <      
361 <      which_node = MolToProcMap[i];
362 <      
363 <      if (which_node != 0) {
364 <        
365 <        if (potatoes[which_node] + 1 >= MAXTAG) {
366 <          // The potato was going to exceed the maximum value,
367 <          // so wrap this processor potato back to 0:        
332 >        // receive the length of the string buffer that was
333 >        // prepared by processor i
334  
335 <          potatoes[which_node] = 0;          
336 <          MPI_Send(&potatoes[which_node], 1, MPI_INT, which_node, 0, MPI_COMM_WORLD);
337 <          
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 <        myPotato = potatoes[which_node];        
362 > #endif // is_mpi
363  
364 <        //recieve the number of integrableObject in current molecule
377 <        MPI_Recv(&nCurObj, 1, MPI_INT, which_node,
378 <                 myPotato, MPI_COMM_WORLD, &istatus);
379 <        myPotato++;
380 <        
381 <        for(int l = 0; l < nCurObj; l++){
364 >  }
365  
366 <          if (potatoes[which_node] + 2 >= MAXTAG) {
367 <            // The potato was going to exceed the maximum value,
368 <            // so wrap this processor potato back to 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 <            potatoes[which_node] = 0;          
374 <            MPI_Send(&potatoes[which_node], 1, MPI_INT, which_node, 0, MPI_COMM_WORLD);
375 <            
390 <          }
373 >    Vector3d pos;
374 >    Vector3d vel;
375 >    pos = integrableObject->getPos();
376  
377 <          MPI_Recv(MPIatomTypeString, MINIBUFFERSIZE, MPI_CHAR, which_node,
378 <          myPotato, MPI_COMM_WORLD, &istatus);
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 <          atomTypeString = MPIatomTypeString;
387 >    vel = integrableObject->getVel();          
388  
389 <          myPotato++;
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 <          MPI_Recv(atomData, 13, MPI_DOUBLE, which_node, myPotato, MPI_COMM_WORLD, &istatus);
400 <          myPotato++;
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 <          MPI_Get_count(&istatus, MPI_DOUBLE, &msgLen);
404 >    if (integrableObject->isDirectional()) {
405 >      type += "qj";
406 >      Quat4d q;
407 >      Vector3d ji;
408 >      q = integrableObject->getQ();
409  
410 <          if(msgLen  == 13)
411 <            isDirectional = 1;
412 <          else
413 <            isDirectional = 0;
414 <          
415 <          // If we've survived to here, format the line:
416 <            
417 <          if (!isDirectional) {
418 <        
413 <            sprintf( writeLine,
414 <                 "%s\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t",
415 <                 atomTypeString,
416 <                 atomData[0],
417 <                 atomData[1],
418 <                 atomData[2],
419 <                 atomData[3],
420 <                 atomData[4],
421 <                 atomData[5]);
422 <        
423 <           strcat( writeLine, "0.0\t0.0\t0.0\t0.0\t0.0\t0.0\t0.0\n" );
424 <        
425 <          }
426 <          else {
427 <        
428 <                sprintf( writeLine,
429 <                         "%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",
430 <                         atomTypeString,
431 <                         atomData[0],
432 <                         atomData[1],
433 <                         atomData[2],
434 <                         atomData[3],
435 <                         atomData[4],
436 <                         atomData[5],
437 <                         atomData[6],
438 <                         atomData[7],
439 <                         atomData[8],
440 <                         atomData[9],
441 <                         atomData[10],
442 <                         atomData[11],
443 <                         atomData[12]);
444 <            
445 <          }
446 <          
447 <          for(k = 0; k < outFile.size(); k++)
448 <            *outFile[k] << writeLine;            
449 <
450 <        }// end for(int l =0)
451 <        potatoes[which_node] = myPotato;
452 <
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        }
454      else {
455        
456        haveError = 0;
457        
458            local_index = indexArray[currentIndex].first;        
420  
421 <        integrableObjects = (entry_plug->molecules[local_index]).getIntegrableObjects();
421 >      ji = integrableObject->getJ();
422  
423 <        for(iter= integrableObjects.begin(); iter != integrableObjects.end(); ++iter){    
424 <                sd = *iter;
425 <            atomTypeString = sd->getType();
426 <            
427 <            sd->getPos(pos);
428 <            sd->getVel(vel);          
429 <          
430 <            atomData[0] = pos[0];
470 <            atomData[1] = pos[1];
471 <            atomData[2] = pos[2];
472 <
473 <            atomData[3] = vel[0];
474 <            atomData[4] = vel[1];
475 <            atomData[5] = vel[2];
476 <              
477 <            isDirectional = 0;
478 <
479 <            if( sd->isDirectional() ){
480 <
481 <              isDirectional = 1;
482 <                
483 <              sd->getQ( q );
484 <              sd->getJ( ji );
485 <
486 <              for (int j = 0; j < 6 ; j++)
487 <                atomData[j] = atomData[j];            
488 <              
489 <              atomData[6] = q[0];
490 <              atomData[7] = q[1];
491 <              atomData[8] = q[2];
492 <              atomData[9] = q[3];
493 <              
494 <              atomData[10] = ji[0];
495 <              atomData[11] = ji[1];
496 <              atomData[12] = ji[2];
497 <            }
498 <            
499 <            // If we've survived to here, format the line:
500 <            
501 <            if (!isDirectional) {
502 <        
503 <              sprintf( writeLine,
504 <                 "%s\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t",
505 <                 atomTypeString,
506 <                 atomData[0],
507 <                 atomData[1],
508 <                 atomData[2],
509 <                 atomData[3],
510 <                 atomData[4],
511 <                 atomData[5]);
512 <        
513 <             strcat( writeLine, "0.0\t0.0\t0.0\t0.0\t0.0\t0.0\t0.0\n" );
514 <        
515 <            }
516 <            else {
517 <        
518 <                sprintf( writeLine,
519 <                         "%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",
520 <                         atomTypeString,
521 <                         atomData[0],
522 <                         atomData[1],
523 <                         atomData[2],
524 <                         atomData[3],
525 <                         atomData[4],
526 <                         atomData[5],
527 <                         atomData[6],
528 <                         atomData[7],
529 <                         atomData[8],
530 <                         atomData[9],
531 <                         atomData[10],
532 <                         atomData[11],
533 <                         atomData[12]);
534 <              
535 <            }
536 <            
537 <            for(k = 0; k < outFile.size(); k++)
538 <              *outFile[k] << writeLine;
539 <            
540 <            
541 <        }//end for(iter = integrableObject.begin())
542 <        
543 <      currentIndex++;
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 <    }//end for(i = 0; i < mpiSim->getNmol())
434 <    
435 <    for(k = 0; k < outFile.size(); k++)
436 <      outFile[k]->flush();
437 <    
551 <    sprintf( checkPointMsg,
552 <             "Sucessfully took a dump.\n");
553 <    
554 <    MPIcheckPoint();        
555 <    
556 <    delete[] potatoes;
557 <    
558 <  } else {
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 <    // worldRank != 0, so I'm a remote node.  
440 <
441 <    // Set my magic potato to 0:
442 <
443 <    myPotato = 0;
444 <    currentIndex = 0;
445 <    
446 <    for (i = 0 ; i < mpiSim->getNMolGlobal(); i++ ) {
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 <      // Am I the node which has this integrableObject?
456 <      
457 <      if (MolToProcMap[i] == worldRank) {
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 +    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 +      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 <        if (myPotato + 1 >= MAXTAG) {
512 <          
513 <          // The potato was going to exceed the maximum value,
514 <          // so wrap this processor potato back to 0 (and block until
515 <          // node 0 says we can go:
516 <          
517 <          MPI_Recv(&myPotato, 1, MPI_INT, 0, 0, MPI_COMM_WORLD, &istatus);
518 <          
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 <          local_index = indexArray[currentIndex].first;        
527 <          integrableObjects = entry_plug->molecules[local_index].getIntegrableObjects();
528 <          
529 <          nCurObj = integrableObjects.size();
530 <                      
531 <          MPI_Send(&nCurObj, 1, MPI_INT, 0,
532 <                             myPotato, MPI_COMM_WORLD);
533 <          myPotato++;
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 <          for( iter = integrableObjects.begin(); iter  != integrableObjects.end(); iter++){
543 >    sprintf(tempBuffer, "%10d %7s %s\n", index, type.c_str(), line.c_str());
544 >    return std::string(tempBuffer);
545 >  }
546  
547 <            if (myPotato + 2 >= MAXTAG) {
548 <          
549 <              // The potato was going to exceed the maximum value,
598 <              // so wrap this processor potato back to 0 (and block until
599 <              // node 0 says we can go:
600 <          
601 <              MPI_Recv(&myPotato, 1, MPI_INT, 0, 0, MPI_COMM_WORLD, &istatus);
602 <              
603 <            }
604 <            
605 <            sd = *iter;
606 <            
607 <            atomTypeString = sd->getType();
547 >  void DumpWriter::writeDump() {
548 >    writeFrame(*dumpFile_);
549 >  }
550  
551 <            sd->getPos(pos);
552 <            sd->getVel(vel);
551 >  void DumpWriter::writeEor() {
552 >    std::ostream* eorStream;
553 >    
554 > #ifdef IS_MPI
555 >    if (worldRank == 0) {
556 > #endif // is_mpi
557  
558 <            atomData[0] = pos[0];
613 <            atomData[1] = pos[1];
614 <            atomData[2] = pos[2];
558 >      eorStream = createOStream(eorFilename_);
559  
560 <            atomData[3] = vel[0];
561 <            atomData[4] = vel[1];
562 <            atomData[5] = vel[2];
619 <              
620 <            isDirectional = 0;
560 > #ifdef IS_MPI
561 >    }
562 > #endif // is_mpi    
563  
564 <            if( sd->isDirectional() ){
564 >    writeFrame(*eorStream);
565  
566 <                isDirectional = 1;
567 <                
568 <                sd->getQ( q );
569 <                sd->getJ( ji );
570 <                
571 <                
572 <                atomData[6] = q[0];
573 <                atomData[7] = q[1];
632 <                atomData[8] = q[2];
633 <                atomData[9] = q[3];
634 <      
635 <                atomData[10] = ji[0];
636 <                atomData[11] = ji[1];
637 <                atomData[12] = ji[2];
638 <              }
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 <            
641 <            strncpy(MPIatomTypeString, atomTypeString, MINIBUFFERSIZE);
575 >  }
576  
643            // null terminate the string before sending (just in case):
644            MPIatomTypeString[MINIBUFFERSIZE-1] = '\0';
577  
578 <            MPI_Send(MPIatomTypeString, MINIBUFFERSIZE, MPI_CHAR, 0,
579 <                             myPotato, MPI_COMM_WORLD);
580 <            
581 <            myPotato++;
582 <            
583 <            if (isDirectional) {
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 <              MPI_Send(atomData, 13, MPI_DOUBLE, 0,
654 <                       myPotato, MPI_COMM_WORLD);
655 <              
656 <            } else {
585 >      buffers.push_back(dumpFile_->rdbuf());
586  
587 <              MPI_Send(atomData, 6, MPI_DOUBLE, 0,
659 <                       myPotato, MPI_COMM_WORLD);
660 <            }
587 >      eorStream = createOStream(eorFilename_);
588  
589 <            myPotato++;  
590 <
591 <          }
665 <
666 <          currentIndex++;    
667 <          
668 <        }
669 <      
670 <      }
671 <
672 <    sprintf( checkPointMsg,
673 <             "Sucessfully took a dump.\n");
674 <    MPIcheckPoint();                
675 <    
589 >      buffers.push_back(eorStream->rdbuf());
590 >        
591 > #ifdef IS_MPI
592      }
593 + #endif // is_mpi    
594  
595 +    TeeBuf tbuf(buffers.begin(), buffers.end());
596 +    std::ostream os(&tbuf);
597  
598 <  
680 < #endif // is_mpi
681 < }
598 >    writeFrame(os);
599  
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 +  }
610  
611 < // a couple of functions to let us escape the write loop
611 >  std::ostream* DumpWriter::createOStream(const std::string& filename) {
612  
613 < void dWrite::DieDieDie( void ){
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=2>" << std::endl;
625 >    (*newOStream) << "  <MetaData>" << std::endl;
626 >    (*newOStream) << info_->getRawMetaData();
627 >    (*newOStream) << "  </MetaData>" << std::endl;
628 >    return newOStream;
629 >  }
630  
631 <  MPI_Finalize();
690 <  exit (0);
691 < }
631 >  void DumpWriter::writeClosing(std::ostream& os) {
632  
633 < #endif //is_mpi
633 >    os << "</OpenMD>\n";
634 >    os.flush();
635 >  }
636 >
637 > }//end namespace OpenMD

Comparing:
trunk/src/io/DumpWriter.cpp (property svn:keywords), Revision 2 by gezelter, Fri Sep 24 04:16:43 2004 UTC vs.
branches/development/src/io/DumpWriter.cpp (property svn:keywords), Revision 1746 by gezelter, Wed Jun 6 02:18:54 2012 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines