ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/branches/new_design/OOPSE-2.0/src/io/DumpWriter.cpp
(Generate patch)

Comparing branches/new_design/OOPSE-2.0/src/io/DumpWriter.cpp (file contents):
Revision 1738 by tim, Thu Nov 11 16:41:58 2004 UTC vs.
Revision 1739 by tim, Mon Nov 15 18:02:15 2004 UTC

# Line 1 | Line 1
1 < #define _LARGEFILE_SOURCE64
2 < #define _FILE_OFFSET_BITS 64
1 > #include "io/DumpWrite.hpp"
2 > #include "utils/simError.h"
3  
4 #include <string.h>
5 #include <iostream>
6 #include <fstream>
7 #include <algorithm>
8 #include <utility>
9
4   #ifdef IS_MPI
11
5   #include <mpi.h>
13 #include "brains/mpiSimulation.hpp"
14
15 namespace dWrite {
16    void DieDieDie(void);
17
18 }
19
20 using namespace dWrite;
21
6   #endif //is_mpi
7 <
8 < #include "io/ReadWrite.hpp"
9 < #include "utils/simError.h"
26 <
27 < DumpWriter::DumpWriter(SimInfo *the_entry_plug) {
28 <    entry_plug = the_entry_plug;
29 <
7 > namespace oopse {
8 > DumpWriter::DumpWriter(SimInfo* info, const std::string& filename)
9 >                   : info_(info), filename_(filename){
10   #ifdef IS_MPI
11  
12      if (worldRank == 0) {
13   #endif // is_mpi
14  
15 <        dumpFile.open(entry_plug->sampleName.c_str(), ios::out | ios::trunc);
15 >        dumpFile.open(filename_.c_str(), ios::out | ios::trunc);
16  
17          if (!dumpFile) {
18              sprintf(painCave.errMsg, "Could not open \"%s\" for dump output.\n",
19 <                    entry_plug->sampleName.c_str());
19 >                    info_->filename_.c_str());
20              painCave.isFatal = 1;
21              simError();
22          }
# Line 45 | Line 25 | DumpWriter::DumpWriter(SimInfo *the_entry_plug) {
25  
26      }
27  
48    //sort the local atoms by global index
49    sortByGlobalIndex();
50
28      sprintf(checkPointMsg, "Sucessfully opened output file for dumping.\n");
29      MPIcheckPoint();
30  
# Line 72 | Line 49 | DumpWriter::~DumpWriter() {
49  
50   }
51  
52 < #ifdef IS_MPI
52 > void writeCommentLine(Snapshot* s) {
53  
54 < /**
55 < * A hook function to load balancing
56 < */
54 >    double currentTime;
55 >    Mat3x3d hmat;
56 >    double chi;
57 >    double integralOfChiDt;
58 >    Mat3x3d eta;
59 >    
60 >    currentTime = s->getTime();
61 >    hmat = s->getHmat();
62 >    chi = s->getChi();
63 >    integralOfChiDt = s->getIntegralOfChiDt();
64 >    eta = s->getEta();
65 >    
66 >    os << currentTime << ";\t"
67 >         << hmat[0][0] << "\t" << hmat[1][0] << "\t" << hmat[2][0] << ";\t"
68 >         << hmat[0][1] << "\t" << hmat[1][1] << "\t" << hmat[2][1] << ";\t"
69 >         << hmat[0][2] << "\t" << hmat[1][2] << "\t" << hmat[2][2] << ";";
70  
71 < void DumpWriter::update() {
82 <    sortByGlobalIndex();
83 < }
71 >    //write out additional parameters, such as chi and eta
72  
73 < /**
86 < * Auxiliary sorting function
87 < */
73 >    os << chi << "\t" << integralOfChiDt << "\t;"
74  
75 < bool indexSortingCriterion(const pair < int, int > &p1, const pair < int,
76 <                           int > &p2) {
77 <    return p1.second < p2.second;
75 >    os << eta[0][0] << "\t" << eta[1][0] << "\t" << eta[2][0] << ";\t"
76 >         << eta[0][1] << "\t" << eta[1][1] << "\t" << eta[2][1] << ";\t"
77 >         << eta[0][2] << "\t" << eta[1][2] << "\t" << eta[2][2] << ";";
78 >        
79 >    os << endl;
80   }
81  
82 < /**
83 < * Sorting the local index by global index
84 < */
82 > void DumpWriter::writeFrame(std::ostream& os) {
83 >    const int BUFFERSIZE = 2000;
84 >    const int MINIBUFFERSIZE = 100;
85  
86 < void DumpWriter::sortByGlobalIndex() {
87 <    Molecule * mols = entry_plug->molecules;
100 <    indexArray.clear();
86 >    char tempBuffer[BUFFERSIZE];
87 >    char writeLine[BUFFERSIZE];
88  
89 <    for(int i = 0; i < entry_plug->n_mol; i++) {
90 <        indexArray.push_back(make_pair(i, mols[i].getGlobalIndex()));
91 <    }
89 >    Quat4d q;
90 >    Vector3d ji;
91 >    Vector3d pos;
92 >    Vector3d vel;
93  
94 <    sort(indexArray.begin(), indexArray.end(), indexSortingCriterion);
95 < }
94 >    Molecule* mol;
95 >    StuntDouble* integrableObject;
96 >    typename SimInfo::MoleculeIterator mi;
97 >    typename Molecule::IntegrableObjectIterator ii;
98 >  
99 >    int nTotObjects;    
100 >    nTotObjects = info_->getNGlobalIntegrableObjects();
101  
102 < #endif
102 > #ifndef IS_MPI
103  
111 void DumpWriter::writeDump(double currentTime) {
112    ofstream finalOut;
113    vector<ofstream *>fileStreams;
104  
105 < #ifdef IS_MPI
105 >    os << nTotObjects << "\n";
106 >        
107 >    writeCommentLine(info_->getSnapshotManager()->getCurrentSnapshot());
108  
109 <    if (worldRank == 0) {
118 < #endif
109 >    for (mol = info_->beginMolecule(mi); mol != NULL; mol = info_->nextMolecule(mi)) {
110  
111 <        finalOut.open(entry_plug->finalName.c_str(), ios::out | ios::trunc);
111 >        for (integrableObject = mol->beginIntegrableObject(ii); integrableObject != NULL;
112 >            integrableObject = mol->nextIntegrableObject(ii)) {
113 >                
114  
115 <        if (!finalOut) {
116 <            sprintf(painCave.errMsg,
124 <                    "Could not open \"%s\" for final dump output.\n",
125 <                    entry_plug->finalName.c_str());
126 <            painCave.isFatal = 1;
127 <            simError();
128 <        }
115 >            pos = integrableObject->getPos();
116 >            vel = integrableObject->getVel();
117  
118 < #ifdef IS_MPI
118 >            sprintf(tempBuffer, "%s\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t",
119 >                    integrableObject->getType(),
120 >                    pos[0], pos[1], pos[2],
121 >                    vel[0], vel[1], vel[2]);
122  
123 <    }
123 >            strcpy(writeLine, tempBuffer);
124  
125 < #endif // is_mpi
125 >            if (integrableObject->isDirectional()) {
126 >                q = integrableObject->getQ();
127 >                ji = integrableObject->getJ();
128  
129 <    fileStreams.push_back(&finalOut);
130 <    fileStreams.push_back(&dumpFile);
129 >                sprintf(tempBuffer, "%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\n",
130 >                        q[0], q[1], q[2], q[3],
131 >                        ji[0], ji[1], ji[2]);
132 >                strcat(writeLine, tempBuffer);
133 >            } else {
134 >                strcat(writeLine, "0.0\t0.0\t0.0\t0.0\t0.0\t0.0\t0.0\n");
135 >            }
136  
137 <    writeFrame(fileStreams, currentTime);
137 >            os << writeLine;
138  
141 #ifdef IS_MPI
142
143    finalOut.close();
144
145 #endif
146
147 }
148
149 void DumpWriter::writeFinal(double currentTime) {
150    ofstream finalOut;
151    vector<ofstream *>fileStreams;
152
153 #ifdef IS_MPI
154
155    if (worldRank == 0) {
156 #endif // is_mpi
157
158        finalOut.open(entry_plug->finalName.c_str(), ios::out | ios::trunc);
159
160        if (!finalOut) {
161            sprintf(painCave.errMsg,
162                    "Could not open \"%s\" for final dump output.\n",
163                    entry_plug->finalName.c_str());
164            painCave.isFatal = 1;
165            simError();
139          }
167
168 #ifdef IS_MPI
169
140      }
141  
142 < #endif // is_mpi
173 <
174 <    fileStreams.push_back(&finalOut);
175 <    writeFrame(fileStreams, currentTime);
176 <
177 < #ifdef IS_MPI
178 <
179 <    finalOut.close();
180 <
181 < #endif
182 <
183 < }
184 <
185 < void DumpWriter::writeFrame(vector<ofstream *>&outFile, double currentTime) {
186 <    const int BUFFERSIZE = 2000;
187 <    const int MINIBUFFERSIZE = 100;
188 <
189 <    char tempBuffer[BUFFERSIZE];
190 <    char writeLine[BUFFERSIZE];
191 <
192 <    int i;
193 <    unsigned int k;
194 <
195 < #ifdef IS_MPI
196 <
142 > #else // is_mpi
143      /*********************************************************************
144       * Documentation?  You want DOCUMENTATION?
145       *
# Line 230 | Line 176 | void DumpWriter::writeFrame(vector<ofstream *>&outFile
176       * How's THAT for documentation?
177       *
178       *********************************************************************/
179 +    const int masterNode = 0;
180  
181      int * potatoes;
182      int myPotato;
236
183      int nProc;
238    int j;
184      int which_node;
240    int done;
241    int which_atom;
242    int local_index;
243    int currentIndex;
185      double atomData[13];
186      int isDirectional;
187 <    char * atomTypeString;
187 >    const char * atomTypeString;
188      char MPIatomTypeString[MINIBUFFERSIZE];
248    int nObjects;
189      int msgLen; // the length of message actually recieved at master nodes
190 <
191 < #endif          //is_mpi
192 <
193 <    Quat4d q;
194 <    Vector3d ji;
195 <    DirectionalAtom * dAtom;
196 <    Vector3d pos;
197 <    Vector3d vel;
258 <
259 <    int nTotObjects;
260 <    StuntDouble * sd;
261 <    char * molName;
262 <    vector<StuntDouble *>integrableObjects;
263 <    vector<StuntDouble *>::iterator iter;
264 <    nTotObjects = entry_plug->getTotIntegrableObjects();
265 <
266 < #ifndef IS_MPI
267 <
268 <    for(k = 0; k < outFile.size(); k++) {
269 <        *outFile[k] << nTotObjects << "\n";
270 <
271 <        *outFile[k] << currentTime << ";\t" << entry_plug->Hmat[0][0] << "\t"
272 <             << entry_plug->Hmat[1][0] << "\t" << entry_plug->Hmat[2][0]
273 <             << ";\t" << entry_plug->Hmat[0][1] << "\t"
274 <             << entry_plug->Hmat[1][1] << "\t" << entry_plug->Hmat[2][1]
275 <             << ";\t" << entry_plug->Hmat[0][2] << "\t"
276 <             << entry_plug->Hmat[1][2] << "\t" << entry_plug->Hmat[2][2] << ";";
277 <
278 <        //write out additional parameters, such as chi and eta
279 <        //another circular reference nightmare
280 <        *outFile[k] << entry_plug->the_integrator->getAdditionalParameters()
281 <             << endl;
282 <    }
283 <
284 <    for(i = 0; i < entry_plug->n_mol; i++) {
285 <        integrableObjects = entry_plug->molecules[i].getIntegrableObjects();
286 <        molName
287 <            = (entry_plug->compStamps[entry_plug->molecules[i].getStampID()])->getID();
288 <
289 <        for(iter = integrableObjects.begin();
290 <            iter != integrableObjects.end(); ++iter) {
291 <            sd = *iter;
292 <            pos = sd->getPos();
293 <            vel = sd->getVel();
294 <
295 <            sprintf(tempBuffer, "%s\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t",
296 <                    sd->getType(), pos[0],
297 <                    pos[1], pos[2],
298 <                    vel[0], vel[1],
299 <                    vel[2]);
300 <
301 <            strcpy(writeLine, tempBuffer);
302 <
303 <            if (sd->isDirectional()) {
304 <                q = sd->getQ();
305 <                ji = sd->getJ();
306 <
307 <                sprintf(tempBuffer, "%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\n", q[0],
308 <                        q[1], q[2], q[3],
309 <                        ji[0], ji[1], ji[2]);
310 <                strcat(writeLine, tempBuffer);
311 <            } else {
312 <                strcat(writeLine, "0.0\t0.0\t0.0\t0.0\t0.0\t0.0\t0.0\n");
313 <            }
314 <
315 <            for(k = 0; k < outFile.size(); k++) {
316 <                *outFile[k] << writeLine;
317 <            }
318 <        }
319 <    }
320 <
321 < #else // is_mpi
322 <
323 <    /* code to find maximum tag value */
324 <
325 <    int * tagub, flag, MAXTAG;
190 >    int haveError;
191 >    MPI_Status istatus;
192 >    int nCurObj;
193 >    
194 >    // code to find maximum tag value
195 >    int * tagub;
196 >    int flag;
197 >    int MAXTAG;
198      MPI_Attr_get(MPI_COMM_WORLD, MPI_TAG_UB, &tagub, &flag);
199  
200      if (flag) {
# Line 331 | Line 203 | void DumpWriter::writeFrame(vector<ofstream *>&outFile
203          MAXTAG = 32767;
204      }
205  
206 <    int haveError;
206 >    if (worldRank == masterNode) { //master node (node 0) is responsible for writing the dump file
207  
336    MPI_Status istatus;
337    int nCurObj;
338    int * MolToProcMap = mpiSim->getMolToProcMap();
339
340    // write out header and node 0's coordinates
341
342    if (worldRank == 0) {
343
208          // Node 0 needs a list of the magic potatoes for each processor;
209  
210 <        nProc = mpiSim->getNProcessors();
210 >        MPI_Comm_size(MPI_COMM_WORLD, &nProc);
211          potatoes = new int[nProc];
212  
213          //write out the comment lines
# Line 351 | Line 215 | void DumpWriter::writeFrame(vector<ofstream *>&outFile
215              potatoes[i] = 0;
216          }
217  
354        for(k = 0; k < outFile.size(); k++) {
355            *outFile[k] << nTotObjects << "\n";
218  
219 <            *outFile[k] << currentTime << ";\t" << entry_plug->Hmat[0][0]
220 <                 << "\t" << entry_plug->Hmat[1][0] << "\t"
359 <                 << entry_plug->Hmat[2][0] << ";\t" << entry_plug->Hmat[0][1]
360 <                 << "\t" << entry_plug->Hmat[1][1] << "\t"
361 <                 << entry_plug->Hmat[2][1] << ";\t" << entry_plug->Hmat[0][2]
362 <                 << "\t" << entry_plug->Hmat[1][2] << "\t"
363 <                 << entry_plug->Hmat[2][2] << ";";
219 >        os << nTotObjects << "\n";
220 >        writeCommentLine(info_->getSnapshotManager()->getCurrentSnapshot());
221  
222 <            *outFile[k] << entry_plug->the_integrator->getAdditionalParameters()
366 <                 << endl;
367 <        }
222 >        for(i = 0; i < info_->getNGlobalMolecules(); i++) {
223  
369        currentIndex = 0;
370
371        for(i = 0; i < mpiSim->getNMolGlobal(); i++) {
372
224              // Get the Node number which has this atom;
225  
226 <            which_node = MolToProcMap[i];
226 >            which_node = info_->getMolToProc(i);
227  
228 <            if (which_node != 0) {
228 >            if (which_node != masterNode) { //current molecule is in slave node
229                  if (potatoes[which_node] + 1 >= MAXTAG) {
230                      // The potato was going to exceed the maximum value,
231                      // so wrap this processor potato back to 0:        
# Line 450 | Line 301 | void DumpWriter::writeFrame(vector<ofstream *>&outFile
301                                  atomData[12]);
302                      }
303  
304 <                    for(k = 0; k < outFile.size(); k++) {
305 <                        *outFile[k] << writeLine;
455 <                    }
304 >                    os << writeLine;
305 >
306                  } // end for(int l =0)
307  
308                  potatoes[which_node] = myPotato;
309 <            } else {
460 <                haveError = 0;
309 >            } else { //master node has current molecule
310  
311 <                local_index = indexArray[currentIndex].first;
311 >                mol = info_->getMoleculeByGlobalIndex(i);
312  
313 <                integrableObjects
314 <                    = (entry_plug->molecules[local_index]).getIntegrableObjects();
313 >                if (mol == NULL) {
314 >                    strcpy(painCave.errMsg, "Molecule not found on node %d!", worldRank);
315 >                    painCave.isFatal = 1;
316 >                    simError();
317 >                }
318 >                
319 >                for (integrableObject = mol->beginIntegrableObject(ii); integrableObject != NULL;
320 >                    integrableObject = mol->nextIntegrableObject(ii)) {
321 >                        
322 >                    atomTypeString = integrableObject->getType().c_str();
323  
324 <                for(iter = integrableObjects.begin();
325 <                    iter != integrableObjects.end(); ++iter) {
469 <                    sd = *iter;
470 <                    atomTypeString = sd->getType();
324 >                    pos = integrableObject->getPos();
325 >                    vel = integrableObject->getVel();
326  
472                    pos = sd->getPos();
473                    vel = sd->getVel();
474
327                      atomData[0] = pos[0];
328                      atomData[1] = pos[1];
329                      atomData[2] = pos[2];
# Line 482 | Line 334 | void DumpWriter::writeFrame(vector<ofstream *>&outFile
334  
335                      isDirectional = 0;
336  
337 <                    if (sd->isDirectional()) {
337 >                    if (integrableObject->isDirectional()) {
338                          isDirectional = 1;
339  
340 <                        q = sd->getQ();
341 <                        ji = sd->getJ();
340 >                        q = integrableObject->getQ();
341 >                        ji = integrableObject->getJ();
342  
343                          for(int j = 0; j < 6; j++) {
344                              atomData[j] = atomData[j];
# Line 532 | Line 384 | void DumpWriter::writeFrame(vector<ofstream *>&outFile
384                                  atomData[12]);
385                      }
386  
535                    for(k = 0; k < outFile.size(); k++) {
536                        *outFile[k] << writeLine;
537                    }
538                } //end for(iter = integrableObject.begin())
387  
388 <                currentIndex++;
388 >                    os << writeLine;
389 >
390 >                } //end for(iter = integrableObject.begin())
391              }
392          } //end for(i = 0; i < mpiSim->getNmol())
393  
394 <        for(k = 0; k < outFile.size(); k++) {
545 <            outFile[k]->flush();
546 <        }
394 >        os.flush();
395  
396          sprintf(checkPointMsg, "Sucessfully took a dump.\n");
397  
# Line 557 | Line 405 | void DumpWriter::writeFrame(vector<ofstream *>&outFile
405          // Set my magic potato to 0:
406  
407          myPotato = 0;
560        currentIndex = 0;
408  
409 <        for(i = 0; i < mpiSim->getNMolGlobal(); i++) {
409 >        for(i = 0; i < info_->getNGlobalMolecules(); i++) {
410  
411              // Am I the node which has this integrableObject?
412 <
413 <            if (MolToProcMap[i] == worldRank) {
412 >            whichNode = info_->getMolToProc(i);
413 >            if (whichNode == worldRank) {
414                  if (myPotato + 1 >= MAXTAG) {
415  
416                      // The potato was going to exceed the maximum value,
# Line 574 | Line 421 | void DumpWriter::writeFrame(vector<ofstream *>&outFile
421                               &istatus);
422                  }
423  
424 <                local_index = indexArray[currentIndex].first;
578 <                integrableObjects =
579 <                    entry_plug->molecules[local_index].getIntegrableObjects();
424 >                mol = info_->getMoleculeByGlobalIndex(i);
425  
426 <                nCurObj = integrableObjects.size();
426 >                
427 >                nCurObj = mol->getNIntegrableObjects();
428  
429                  MPI_Send(&nCurObj, 1, MPI_INT, 0, myPotato, MPI_COMM_WORLD);
430                  myPotato++;
431  
432 <                for(iter = integrableObjects.begin();
433 <                    iter != integrableObjects.end(); iter++) {
432 >                for (integrableObject = mol->beginIntegrableObject(ii); integrableObject != NULL;
433 >                    integrableObject = mol->nextIntegrableObject(ii)) {
434 >
435                      if (myPotato + 2 >= MAXTAG) {
436  
437                          // The potato was going to exceed the maximum value,
# Line 595 | Line 442 | void DumpWriter::writeFrame(vector<ofstream *>&outFile
442                                   &istatus);
443                      }
444  
445 <                    sd = *iter;
445 >                    atomTypeString = integrableObject->getType().c_str();
446  
447 <                    atomTypeString = sd->getType();
447 >                    pos = integrableObject->getPos();
448 >                    vel = integrableObject->getVel();
449  
602                    pos = sd->getPos();
603                    vel = sd->getVel();
604
450                      atomData[0] = pos[0];
451                      atomData[1] = pos[1];
452                      atomData[2] = pos[2];
# Line 612 | Line 457 | void DumpWriter::writeFrame(vector<ofstream *>&outFile
457  
458                      isDirectional = 0;
459  
460 <                    if (sd->isDirectional()) {
460 >                    if (integrableObject->isDirectional()) {
461                          isDirectional = 1;
462  
463 <                        q = sd->getQ();
464 <                        ji = sd->getJ();
463 >                        q = integrableObject->getQ();
464 >                        ji = integrableObject->getJ();
465  
466                          atomData[6] = q[0];
467                          atomData[7] = q[1];
# Line 648 | Line 493 | void DumpWriter::writeFrame(vector<ofstream *>&outFile
493  
494                      myPotato++;
495                  }
496 <
652 <                currentIndex++;
496 >                    
497              }
498 +            
499          }
500  
501          sprintf(checkPointMsg, "Sucessfully took a dump.\n");
# Line 661 | Line 506 | void DumpWriter::writeFrame(vector<ofstream *>&outFile
506  
507   }
508  
509 < #ifdef IS_MPI
665 <
666 < // a couple of functions to let us escape the write loop
667 <
668 < void dWrite::DieDieDie(void) {
669 <    MPI_Finalize();
670 <    exit(0);
671 < }
672 <
673 < #endif //is_mpi
509 > }//end namespace oopse

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines