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

Comparing branches/new_design/OOPSE-4/src/io/DumpWriter.cpp (file contents):
Revision 1727 by tim, Thu Nov 11 16:41:58 2004 UTC vs.
Revision 1819 by tim, Wed Dec 1 22:45:49 2004 UTC

# Line 1 | Line 1
1 < #define _LARGEFILE_SOURCE64
2 < #define _FILE_OFFSET_BITS 64
1 > #include "io/DumpWriter.hpp"
2 > #include "primitives/Molecule.hpp"
3 > #include "utils/simError.h"
4  
4 #include <string.h>
5 #include <iostream>
6 #include <fstream>
7 #include <algorithm>
8 #include <utility>
9
5   #ifdef IS_MPI
11
6   #include <mpi.h>
13 #include "brains/mpiSimulation.hpp"
14
15 namespace dWrite {
16    void DieDieDie(void);
17
18 }
19
20 using namespace dWrite;
21
7   #endif //is_mpi
8  
9 < #include "io/ReadWrite.hpp"
25 < #include "utils/simError.h"
9 > namespace oopse {
10  
11 < DumpWriter::DumpWriter(SimInfo *the_entry_plug) {
12 <    entry_plug = the_entry_plug;
29 <
11 > DumpWriter::DumpWriter(SimInfo* info, const std::string& filename)
12 >                   : info_(info), filename_(filename){
13   #ifdef IS_MPI
14  
15      if (worldRank == 0) {
16   #endif // is_mpi
17  
18 <        dumpFile.open(entry_plug->sampleName.c_str(), ios::out | ios::trunc);
18 >        dumpFile_.open(filename_.c_str(), ios::out | ios::trunc);
19  
20 <        if (!dumpFile) {
20 >        if (!dumpFile_) {
21              sprintf(painCave.errMsg, "Could not open \"%s\" for dump output.\n",
22 <                    entry_plug->sampleName.c_str());
22 >                    filename_.c_str());
23              painCave.isFatal = 1;
24              simError();
25          }
# Line 45 | Line 28 | DumpWriter::DumpWriter(SimInfo *the_entry_plug) {
28  
29      }
30  
48    //sort the local atoms by global index
49    sortByGlobalIndex();
50
31      sprintf(checkPointMsg, "Sucessfully opened output file for dumping.\n");
32      MPIcheckPoint();
33  
# Line 62 | Line 42 | DumpWriter::~DumpWriter() {
42      if (worldRank == 0) {
43   #endif // is_mpi
44  
45 <        dumpFile.close();
45 >        dumpFile_.close();
46  
47   #ifdef IS_MPI
48  
# Line 72 | Line 52 | DumpWriter::~DumpWriter() {
52  
53   }
54  
55 < #ifdef IS_MPI
55 > void DumpWriter::writeCommentLine(std::ostream& os, Snapshot* s) {
56  
57 < /**
58 < * A hook function to load balancing
59 < */
57 >    double currentTime;
58 >    Mat3x3d hmat;
59 >    double chi;
60 >    double integralOfChiDt;
61 >    Mat3x3d eta;
62 >    
63 >    currentTime = s->getTime();
64 >    hmat = s->getHmat();
65 >    chi = s->getChi();
66 >    integralOfChiDt = s->getIntegralOfChiDt();
67 >    eta = s->getEta();
68 >    
69 >    os << currentTime << ";\t"
70 >         << hmat(0, 0) << "\t" << hmat(1, 0) << "\t" << hmat(2, 0) << ";\t"
71 >         << hmat(0, 1) << "\t" << hmat(1, 1) << "\t" << hmat(2, 1) << ";\t"
72 >         << hmat(0, 2) << "\t" << hmat(1, 2) << "\t" << hmat(2, 2) << ";";
73  
74 < void DumpWriter::update() {
82 <    sortByGlobalIndex();
83 < }
74 >    //write out additional parameters, such as chi and eta
75  
76 < /**
86 < * Auxiliary sorting function
87 < */
76 >    os << chi << "\t" << integralOfChiDt << "\t;";
77  
78 < bool indexSortingCriterion(const pair < int, int > &p1, const pair < int,
79 <                           int > &p2) {
80 <    return p1.second < p2.second;
78 >    os << eta(0, 0) << "\t" << eta(1, 0) << "\t" << eta(2, 0) << ";\t"
79 >         << eta(0, 1) << "\t" << eta(1, 1) << "\t" << eta(2, 1) << ";\t"
80 >         << eta(0, 2) << "\t" << eta(1, 2) << "\t" << eta(2, 2) << ";";
81 >        
82 >    os << endl;
83   }
84  
85 < /**
86 < * Sorting the local index by global index
87 < */
85 > void DumpWriter::writeFrame(std::ostream& os) {
86 >    const int BUFFERSIZE = 2000;
87 >    const int MINIBUFFERSIZE = 100;
88  
89 < void DumpWriter::sortByGlobalIndex() {
90 <    Molecule * mols = entry_plug->molecules;
100 <    indexArray.clear();
89 >    char tempBuffer[BUFFERSIZE];
90 >    char writeLine[BUFFERSIZE];
91  
92 <    for(int i = 0; i < entry_plug->n_mol; i++) {
93 <        indexArray.push_back(make_pair(i, mols[i].getGlobalIndex()));
94 <    }
92 >    Quat4d q;
93 >    Vector3d ji;
94 >    Vector3d pos;
95 >    Vector3d vel;
96  
97 <    sort(indexArray.begin(), indexArray.end(), indexSortingCriterion);
98 < }
97 >    Molecule* mol;
98 >    StuntDouble* integrableObject;
99 >    SimInfo::MoleculeIterator mi;
100 >    Molecule::IntegrableObjectIterator ii;
101 >  
102 >    int nTotObjects;    
103 >    nTotObjects = info_->getNGlobalIntegrableObjects();
104  
105 < #endif
105 > #ifndef IS_MPI
106  
111 void DumpWriter::writeDump(double currentTime) {
112    ofstream finalOut;
113    vector<ofstream *>fileStreams;
107  
108 < #ifdef IS_MPI
108 >    os << nTotObjects << "\n";
109 >        
110 >    writeCommentLine(os, info_->getSnapshotManager()->getCurrentSnapshot());
111  
112 <    if (worldRank == 0) {
118 < #endif
112 >    for (mol = info_->beginMolecule(mi); mol != NULL; mol = info_->nextMolecule(mi)) {
113  
114 <        finalOut.open(entry_plug->finalName.c_str(), ios::out | ios::trunc);
114 >        for (integrableObject = mol->beginIntegrableObject(ii); integrableObject != NULL;
115 >            integrableObject = mol->nextIntegrableObject(ii)) {
116 >                
117  
118 <        if (!finalOut) {
119 <            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 <        }
118 >            pos = integrableObject->getPos();
119 >            vel = integrableObject->getVel();
120  
121 < #ifdef IS_MPI
121 >            sprintf(tempBuffer, "%s\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t",
122 >                    integrableObject->getType().c_str(),
123 >                    pos[0], pos[1], pos[2],
124 >                    vel[0], vel[1], vel[2]);
125  
126 <    }
126 >            strcpy(writeLine, tempBuffer);
127  
128 < #endif // is_mpi
128 >            if (integrableObject->isDirectional()) {
129 >                q = integrableObject->getQ();
130 >                ji = integrableObject->getJ();
131  
132 <    fileStreams.push_back(&finalOut);
133 <    fileStreams.push_back(&dumpFile);
132 >                sprintf(tempBuffer, "%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\n",
133 >                        q[0], q[1], q[2], q[3],
134 >                        ji[0], ji[1], ji[2]);
135 >                strcat(writeLine, tempBuffer);
136 >            } else {
137 >                strcat(writeLine, "0.0\t0.0\t0.0\t0.0\t0.0\t0.0\t0.0\n");
138 >            }
139  
140 <    writeFrame(fileStreams, currentTime);
140 >            os << writeLine;
141  
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();
142          }
167
168 #ifdef IS_MPI
169
143      }
144  
145 < #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 <
145 > #else // is_mpi
146      /*********************************************************************
147       * Documentation?  You want DOCUMENTATION?
148       *
# Line 230 | Line 179 | void DumpWriter::writeFrame(vector<ofstream *>&outFile
179       * How's THAT for documentation?
180       *
181       *********************************************************************/
182 +    const int masterNode = 0;
183  
184      int * potatoes;
185      int myPotato;
236
186      int nProc;
238    int j;
187      int which_node;
240    int done;
241    int which_atom;
242    int local_index;
243    int currentIndex;
188      double atomData[13];
189      int isDirectional;
190 <    char * atomTypeString;
190 >    const char * atomTypeString;
191      char MPIatomTypeString[MINIBUFFERSIZE];
248    int nObjects;
192      int msgLen; // the length of message actually recieved at master nodes
193 <
194 < #endif          //is_mpi
195 <
196 <    Quat4d q;
197 <    Vector3d ji;
198 <    DirectionalAtom * dAtom;
199 <    Vector3d pos;
200 <    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;
193 >    int haveError;
194 >    MPI_Status istatus;
195 >    int nCurObj;
196 >    
197 >    // code to find maximum tag value
198 >    int * tagub;
199 >    int flag;
200 >    int MAXTAG;
201      MPI_Attr_get(MPI_COMM_WORLD, MPI_TAG_UB, &tagub, &flag);
202  
203      if (flag) {
# Line 331 | Line 206 | void DumpWriter::writeFrame(vector<ofstream *>&outFile
206          MAXTAG = 32767;
207      }
208  
209 <    int haveError;
209 >    if (worldRank == masterNode) { //master node (node 0) is responsible for writing the dump file
210  
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
211          // Node 0 needs a list of the magic potatoes for each processor;
212  
213 <        nProc = mpiSim->getNProcessors();
213 >        MPI_Comm_size(MPI_COMM_WORLD, &nProc);
214          potatoes = new int[nProc];
215  
216          //write out the comment lines
# Line 351 | Line 218 | void DumpWriter::writeFrame(vector<ofstream *>&outFile
218              potatoes[i] = 0;
219          }
220  
354        for(k = 0; k < outFile.size(); k++) {
355            *outFile[k] << nTotObjects << "\n";
221  
222 <            *outFile[k] << currentTime << ";\t" << entry_plug->Hmat[0][0]
223 <                 << "\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] << ";";
222 >        os << nTotObjects << "\n";
223 >        writeCommentLine(info_->getSnapshotManager()->getCurrentSnapshot());
224  
225 <            *outFile[k] << entry_plug->the_integrator->getAdditionalParameters()
366 <                 << endl;
367 <        }
225 >        for(i = 0; i < info_->getNGlobalMolecules(); i++) {
226  
369        currentIndex = 0;
370
371        for(i = 0; i < mpiSim->getNMolGlobal(); i++) {
372
227              // Get the Node number which has this atom;
228  
229 <            which_node = MolToProcMap[i];
229 >            which_node = info_->getMolToProc(i);
230  
231 <            if (which_node != 0) {
231 >            if (which_node != masterNode) { //current molecule is in slave node
232                  if (potatoes[which_node] + 1 >= MAXTAG) {
233                      // The potato was going to exceed the maximum value,
234                      // so wrap this processor potato back to 0:        
# Line 450 | Line 304 | void DumpWriter::writeFrame(vector<ofstream *>&outFile
304                                  atomData[12]);
305                      }
306  
307 <                    for(k = 0; k < outFile.size(); k++) {
308 <                        *outFile[k] << writeLine;
455 <                    }
307 >                    os << writeLine;
308 >
309                  } // end for(int l =0)
310  
311                  potatoes[which_node] = myPotato;
312 <            } else {
460 <                haveError = 0;
312 >            } else { //master node has current molecule
313  
314 <                local_index = indexArray[currentIndex].first;
314 >                mol = info_->getMoleculeByGlobalIndex(i);
315  
316 <                integrableObjects
317 <                    = (entry_plug->molecules[local_index]).getIntegrableObjects();
316 >                if (mol == NULL) {
317 >                    strcpy(painCave.errMsg, "Molecule not found on node %d!", worldRank);
318 >                    painCave.isFatal = 1;
319 >                    simError();
320 >                }
321 >                
322 >                for (integrableObject = mol->beginIntegrableObject(ii); integrableObject != NULL;
323 >                    integrableObject = mol->nextIntegrableObject(ii)) {
324 >                        
325 >                    atomTypeString = integrableObject->getType().c_str();
326  
327 <                for(iter = integrableObjects.begin();
328 <                    iter != integrableObjects.end(); ++iter) {
469 <                    sd = *iter;
470 <                    atomTypeString = sd->getType();
327 >                    pos = integrableObject->getPos();
328 >                    vel = integrableObject->getVel();
329  
472                    pos = sd->getPos();
473                    vel = sd->getVel();
474
330                      atomData[0] = pos[0];
331                      atomData[1] = pos[1];
332                      atomData[2] = pos[2];
# Line 482 | Line 337 | void DumpWriter::writeFrame(vector<ofstream *>&outFile
337  
338                      isDirectional = 0;
339  
340 <                    if (sd->isDirectional()) {
340 >                    if (integrableObject->isDirectional()) {
341                          isDirectional = 1;
342  
343 <                        q = sd->getQ();
344 <                        ji = sd->getJ();
343 >                        q = integrableObject->getQ();
344 >                        ji = integrableObject->getJ();
345  
346                          for(int j = 0; j < 6; j++) {
347                              atomData[j] = atomData[j];
# Line 532 | Line 387 | void DumpWriter::writeFrame(vector<ofstream *>&outFile
387                                  atomData[12]);
388                      }
389  
535                    for(k = 0; k < outFile.size(); k++) {
536                        *outFile[k] << writeLine;
537                    }
538                } //end for(iter = integrableObject.begin())
390  
391 <                currentIndex++;
391 >                    os << writeLine;
392 >
393 >                } //end for(iter = integrableObject.begin())
394              }
395          } //end for(i = 0; i < mpiSim->getNmol())
396  
397 <        for(k = 0; k < outFile.size(); k++) {
545 <            outFile[k]->flush();
546 <        }
397 >        os.flush();
398  
399          sprintf(checkPointMsg, "Sucessfully took a dump.\n");
400  
# Line 557 | Line 408 | void DumpWriter::writeFrame(vector<ofstream *>&outFile
408          // Set my magic potato to 0:
409  
410          myPotato = 0;
560        currentIndex = 0;
411  
412 <        for(i = 0; i < mpiSim->getNMolGlobal(); i++) {
412 >        for(i = 0; i < info_->getNGlobalMolecules(); i++) {
413  
414              // Am I the node which has this integrableObject?
415 <
416 <            if (MolToProcMap[i] == worldRank) {
415 >            whichNode = info_->getMolToProc(i);
416 >            if (whichNode == worldRank) {
417                  if (myPotato + 1 >= MAXTAG) {
418  
419                      // The potato was going to exceed the maximum value,
# Line 574 | Line 424 | void DumpWriter::writeFrame(vector<ofstream *>&outFile
424                               &istatus);
425                  }
426  
427 <                local_index = indexArray[currentIndex].first;
578 <                integrableObjects =
579 <                    entry_plug->molecules[local_index].getIntegrableObjects();
427 >                mol = info_->getMoleculeByGlobalIndex(i);
428  
429 <                nCurObj = integrableObjects.size();
429 >                
430 >                nCurObj = mol->getNIntegrableObjects();
431  
432                  MPI_Send(&nCurObj, 1, MPI_INT, 0, myPotato, MPI_COMM_WORLD);
433                  myPotato++;
434  
435 <                for(iter = integrableObjects.begin();
436 <                    iter != integrableObjects.end(); iter++) {
435 >                for (integrableObject = mol->beginIntegrableObject(ii); integrableObject != NULL;
436 >                    integrableObject = mol->nextIntegrableObject(ii)) {
437 >
438                      if (myPotato + 2 >= MAXTAG) {
439  
440                          // The potato was going to exceed the maximum value,
# Line 595 | Line 445 | void DumpWriter::writeFrame(vector<ofstream *>&outFile
445                                   &istatus);
446                      }
447  
448 <                    sd = *iter;
448 >                    atomTypeString = integrableObject->getType().c_str();
449  
450 <                    atomTypeString = sd->getType();
450 >                    pos = integrableObject->getPos();
451 >                    vel = integrableObject->getVel();
452  
602                    pos = sd->getPos();
603                    vel = sd->getVel();
604
453                      atomData[0] = pos[0];
454                      atomData[1] = pos[1];
455                      atomData[2] = pos[2];
# Line 612 | Line 460 | void DumpWriter::writeFrame(vector<ofstream *>&outFile
460  
461                      isDirectional = 0;
462  
463 <                    if (sd->isDirectional()) {
463 >                    if (integrableObject->isDirectional()) {
464                          isDirectional = 1;
465  
466 <                        q = sd->getQ();
467 <                        ji = sd->getJ();
466 >                        q = integrableObject->getQ();
467 >                        ji = integrableObject->getJ();
468  
469                          atomData[6] = q[0];
470                          atomData[7] = q[1];
# Line 648 | Line 496 | void DumpWriter::writeFrame(vector<ofstream *>&outFile
496  
497                      myPotato++;
498                  }
499 <
652 <                currentIndex++;
499 >                    
500              }
501 +            
502          }
503  
504          sprintf(checkPointMsg, "Sucessfully took a dump.\n");
# Line 661 | Line 509 | void DumpWriter::writeFrame(vector<ofstream *>&outFile
509  
510   }
511  
512 < #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
512 > }//end namespace oopse

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines