ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/OOPSE/libmdtools/DumpWriter.cpp
(Generate patch)

Comparing trunk/OOPSE/libmdtools/DumpWriter.cpp (file contents):
Revision 837 by tim, Wed Oct 29 00:19:10 2003 UTC vs.
Revision 934 by tim, Tue Jan 13 20:04:28 2004 UTC

# Line 3 | Line 3
3   #include <string.h>
4   #include <iostream>
5   #include <fstream>
6 + #include <algorithm>
7 + #include <utility>
8  
9   #ifdef IS_MPI
10   #include <mpi.h>
11   #include "mpiSimulation.hpp"
10 #define TAKE_THIS_TAG_CHAR 1
11 #define TAKE_THIS_TAG_INT 2
12  
13   namespace dWrite{
14 <  void nodeZeroError( void );
15 <  void anonymousNodeDie( void );
14 >  void DieDieDie( void );
15   }
16  
17   using namespace dWrite;
# Line 29 | Line 28 | DumpWriter::DumpWriter( SimInfo* the_entry_plug ){
28    if(worldRank == 0 ){
29   #endif // is_mpi
30  
32    strcpy( outName, entry_plug->sampleName );
31  
32 <    outFile.open(outName, ios::out | ios::trunc );
32 >    dumpFile.open(entry_plug->sampleName, ios::out | ios::trunc );
33  
34 <    if( !outFile ){
34 >    if( !dumpFile ){
35  
36        sprintf( painCave.errMsg,
37                 "Could not open \"%s\" for dump output.\n",
38 <               outName);
38 >               entry_plug->sampleName);
39        painCave.isFatal = 1;
40        simError();
41      }
42  
43 <    //outFile.setf( ios::scientific );
43 >    finalOut.open( entry_plug->finalName, ios::out | ios::trunc );
44 >    if( !finalOut ){
45 >      sprintf( painCave.errMsg,
46 >               "Could not open \"%s\" for final dump output.\n",
47 >               entry_plug->finalName );
48 >      painCave.isFatal = 1;
49 >      simError();
50 >    }
51  
52   #ifdef IS_MPI
53    }
54  
55 +  //sort the local atoms by global index
56 +  sortByGlobalIndex();
57 +  
58    sprintf( checkPointMsg,
59             "Sucessfully opened output file for dumping.\n");
60    MPIcheckPoint();
# Line 59 | Line 67 | DumpWriter::~DumpWriter( ){
67    if(worldRank == 0 ){
68   #endif // is_mpi
69  
70 <    outFile.close();
70 >    dumpFile.close();
71 >    finalOut.close();
72  
73   #ifdef IS_MPI
74    }
75   #endif // is_mpi
76   }
77  
78 < void DumpWriter::writeDump( double currentTime ){
78 > #ifdef IS_MPI
79  
80 + /**
81 + * A hook function to load balancing
82 + */
83 +
84 + void DumpWriter::update(){
85 +  sortByGlobalIndex();          
86 + }
87 +  
88 + /**
89 + * Auxiliary sorting function
90 + */
91 +
92 + bool indexSortingCriterion(const pair<int, int>& p1, const pair<int, int>& p2){
93 +  return p1.second < p2.second;
94 + }
95 +
96 + /**
97 + * Sorting the local index by global index
98 + */
99 +
100 + void DumpWriter::sortByGlobalIndex(){
101 +  Atom** atoms = entry_plug->atoms;
102 +  
103 +  indexArray.clear();
104 +  
105 +  for(int i = 0; i < mpiSim->getMyNlocal();i++)
106 +    indexArray.push_back(make_pair(i, atoms[i]->getGlobalIndex()));
107 +  
108 +  sort(indexArray.begin(), indexArray.end(), indexSortingCriterion);    
109 + }
110 + #endif
111 +
112 + void DumpWriter::writeDump(double currentTime){
113 +
114 +  vector<ofstream*> fileStreams;
115 +
116 + #ifdef IS_MPI
117 +  if(worldRank == 0 ){
118 +    finalOut.seekp(0);
119 +  }
120 + #endif // is_mpi
121 +
122 +  fileStreams.push_back(&finalOut);
123 +  fileStreams.push_back(&dumpFile);
124 +
125 +  writeFrame(fileStreams, currentTime);
126 +        
127 + }
128 +
129 + void DumpWriter::writeFinal(double currentTime){
130 +
131 +  vector<ofstream*> fileStreams;
132 +
133 + #ifdef IS_MPI
134 +  if(worldRank == 0 ){
135 +    finalOut.seekp(0);
136 +  }
137 + #endif // is_mpi
138 +  
139 +  fileStreams.push_back(&finalOut);  
140 +  writeFrame(fileStreams, currentTime);
141 +  
142 + }
143 +
144 + void DumpWriter::writeFrame( vector<ofstream*>& outFile, double currentTime ){
145 +
146    const int BUFFERSIZE = 2000;
147 +  const int MINIBUFFERSIZE = 100;
148 +
149    char tempBuffer[BUFFERSIZE];
150    char writeLine[BUFFERSIZE];
151  
152 <  int i;
152 >  int i, k;
153 >
154   #ifdef IS_MPI
155 <  int j, which_node, done, which_atom, local_index;
155 >  
156 >  int *potatoes;
157 >  int myPotato;
158 >
159 >  int nProc;
160 >  int j, which_node, done, which_atom, local_index, currentIndex;
161 >  double atomData6[6];
162 >  double atomData13[13];
163 >  int isDirectional;
164 >  char* atomTypeString;
165 >  char MPIatomTypeString[MINIBUFFERSIZE];
166 >
167   #else //is_mpi
168    int nAtoms = entry_plug->n_atoms;
169   #endif //is_mpi
# Line 84 | Line 173 | void DumpWriter::writeDump( double currentTime ){
173    Atom** atoms = entry_plug->atoms;
174    double pos[3], vel[3];
175  
87
88  // write current frame to the eor file
89
90  this->writeFinal( currentTime );
91
176   #ifndef IS_MPI
177 +  
178 +  for(k = 0; k < outFile.size(); k++){
179 +    *outFile[k] << nAtoms << "\n";
180  
181 <  outFile << nAtoms << "\n";
181 >    *outFile[k] << currentTime << ";\t"
182 >               << entry_plug->Hmat[0][0] << "\t"
183 >                     << entry_plug->Hmat[1][0] << "\t"
184 >                     << entry_plug->Hmat[2][0] << ";\t"
185 >              
186 >               << entry_plug->Hmat[0][1] << "\t"
187 >                     << entry_plug->Hmat[1][1] << "\t"
188 >                     << entry_plug->Hmat[2][1] << ";\t"
189  
190 <  outFile << currentTime << ";\t"
191 <          << entry_plug->Hmat[0][0] << "\t"
192 <          << entry_plug->Hmat[1][0] << "\t"
99 <          << entry_plug->Hmat[2][0] << ";\t"
190 >                     << entry_plug->Hmat[0][2] << "\t"
191 >                     << entry_plug->Hmat[1][2] << "\t"
192 >                     << entry_plug->Hmat[2][2] << ";";
193  
194 <          << entry_plug->Hmat[0][1] << "\t"
195 <          << entry_plug->Hmat[1][1] << "\t"
196 <          << entry_plug->Hmat[2][1] << ";\t"
197 <
105 <          << entry_plug->Hmat[0][2] << "\t"
106 <          << entry_plug->Hmat[1][2] << "\t"
107 <          << entry_plug->Hmat[2][2] << ";";
108 <  //write out additional parameters, such as chi and eta
109 <  outFile << entry_plug->the_integrator->getAdditionalParameters();
110 <  outFile << endl;
111 <
194 >    //write out additional parameters, such as chi and eta
195 >    *outFile[k] << entry_plug->the_integrator->getAdditionalParameters() << endl;
196 >  }
197 >  
198    for( i=0; i<nAtoms; i++ ){
199  
200      atoms[i]->getPos(pos);
# Line 144 | Line 230 | void DumpWriter::writeDump( double currentTime ){
230      else
231        strcat( writeLine, "0.0\t0.0\t0.0\t0.0\t0.0\t0.0\t0.0\n" );
232  
233 <    outFile << writeLine;
233 >    for(k = 0; k < outFile.size(); k++)
234 >      *outFile[k] << writeLine;
235    }
149  outFile.flush();
236  
237   #else // is_mpi
238  
239 <  // first thing first, suspend fatalities.
240 <  painCave.isEventLoop = 1;
239 >  /* code to find maximum tag value */
240 >  
241 >  int *tagub, flag, MAXTAG;
242 >  MPI_Attr_get(MPI_COMM_WORLD, MPI_TAG_UB, &tagub, &flag);
243 >  if (flag) {
244 >    MAXTAG = *tagub;
245 >  } else {
246 >    MAXTAG = 32767;
247 >  }  
248  
156  int myStatus; // 1 = wakeup & success; 0 = error; -1 = AllDone
249    int haveError;
250  
251    MPI_Status istatus;
# Line 162 | Line 254 | void DumpWriter::writeDump( double currentTime ){
254    // write out header and node 0's coordinates
255  
256    if( worldRank == 0 ){
165    outFile << mpiSim->getTotAtoms() << "\n";
257  
258 <    outFile << currentTime << ";\t"
168 <            << entry_plug->Hmat[0][0] << "\t"
169 <            << entry_plug->Hmat[1][0] << "\t"
170 <            << entry_plug->Hmat[2][0] << ";\t"
258 >    // Node 0 needs a list of the magic potatoes for each processor;
259  
260 <            << entry_plug->Hmat[0][1] << "\t"
261 <            << entry_plug->Hmat[1][1] << "\t"
174 <            << entry_plug->Hmat[2][1] << ";\t"
260 >    nProc = mpiSim->getNumberProcessors();
261 >    potatoes = new int[nProc];
262  
263 <            << entry_plug->Hmat[0][2] << "\t"
264 <            << entry_plug->Hmat[1][2] << "\t"
265 <            << entry_plug->Hmat[2][2] << ";";
263 >    //write out the comment lines
264 >    for (i = 0; i < nProc; i++)
265 >      potatoes[i] = 0;
266 >    
267 >      for(k = 0; k < outFile.size(); k++){
268 >        *outFile[k] << mpiSim->getTotAtoms() << "\n";
269  
270 <    outFile << entry_plug->the_integrator->getAdditionalParameters();
271 <    outFile << endl;
272 <    outFile.flush();
270 >        *outFile[k] << currentTime << ";\t"
271 >                         << entry_plug->Hmat[0][0] << "\t"
272 >                         << entry_plug->Hmat[1][0] << "\t"
273 >                         << entry_plug->Hmat[2][0] << ";\t"
274 >
275 >                         << entry_plug->Hmat[0][1] << "\t"
276 >                         << entry_plug->Hmat[1][1] << "\t"
277 >                         << entry_plug->Hmat[2][1] << ";\t"
278 >
279 >                         << entry_plug->Hmat[0][2] << "\t"
280 >                         << entry_plug->Hmat[1][2] << "\t"
281 >                         << entry_plug->Hmat[2][2] << ";";
282 >  
283 >        *outFile[k] << entry_plug->the_integrator->getAdditionalParameters() << endl;
284 >    }
285 >
286 >    currentIndex = 0;
287 >
288      for (i = 0 ; i < mpiSim->getTotAtoms(); i++ ) {
289 +      
290        // Get the Node number which has this atom;
291 <
291 >      
292        which_node = AtomToProcMap[i];
293 +      
294 +      if (which_node != 0) {
295  
296 <      if (which_node == 0 ) {
296 >        if (potatoes[which_node] + 3 >= MAXTAG) {
297 >          // The potato was going to exceed the maximum value,
298 >          // so wrap this processor potato back to 0:        
299  
300 <        haveError = 0;
301 <        which_atom = i;
302 <        local_index=-1;
193 <        for (j=0; (j<mpiSim->getMyNlocal()) && (local_index < 0); j++) {
194 <          if (atoms[j]->getGlobalIndex() == which_atom) local_index = j;
300 >          potatoes[which_node] = 0;          
301 >          MPI_Send(0, 1, MPI_INT, which_node, 0, MPI_COMM_WORLD);
302 >          
303          }
196        if (local_index != -1) {
197          //format the line
304  
305 <          atoms[local_index]->getPos(pos);
306 <          atoms[local_index]->getVel(vel);
305 >        myPotato = potatoes[which_node];        
306 >        
307 >        MPI_Recv(MPIatomTypeString, MINIBUFFERSIZE, MPI_CHAR, which_node,
308 >                 myPotato, MPI_COMM_WORLD, &istatus);
309 >        
310 >        atomTypeString = MPIatomTypeString;
311 >        
312 >        myPotato++;
313  
314 <          sprintf( tempBuffer,
315 <                   "%s\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t",
316 <                   atoms[local_index]->getType(),
317 <                   pos[0],
206 <                   pos[1],
207 <                   pos[2],
208 <                   vel[0],
209 <                   vel[1],
210 <                   vel[2]); // check here.
211 <          strcpy( writeLine, tempBuffer );
314 >        MPI_Recv(&isDirectional, 1, MPI_INT, which_node,
315 >                 myPotato, MPI_COMM_WORLD, &istatus);
316 >              
317 >        myPotato++;
318  
319 <          if( atoms[local_index]->isDirectional() ){
319 >        if (isDirectional) {          
320 >          MPI_Recv(atomData13, 13, MPI_DOUBLE, which_node,
321 >                   myPotato, MPI_COMM_WORLD, &istatus);
322 >        } else {
323 >          MPI_Recv(atomData6, 6, MPI_DOUBLE, which_node,
324 >                   myPotato, MPI_COMM_WORLD, &istatus);          
325 >        }
326 >        
327 >        myPotato++;
328 >        potatoes[which_node] = myPotato;
329  
330 <            dAtom = (DirectionalAtom *)atoms[local_index];
331 <            dAtom->getQ( q );
330 >      } else {
331 >        
332 >        haveError = 0;
333 >              which_atom = i;
334 >        
335 >        local_index = indexArray[currentIndex].first;        
336 >                
337 >        if (which_atom == indexArray[currentIndex].second) {
338 >          
339 >          atomTypeString = atoms[local_index]->getType();
340  
341 <            sprintf( tempBuffer,
342 <                     "%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\n",
220 <                     q[0],
221 <                     q[1],
222 <                     q[2],
223 <                     q[3],
224 <                     dAtom->getJx(),
225 <                     dAtom->getJy(),
226 <                     dAtom->getJz());
227 <            strcat( writeLine, tempBuffer );
341 >                atoms[local_index]->getPos(pos);
342 >                atoms[local_index]->getVel(vel);          
343  
344 <          }
345 <          else
346 <            strcat( writeLine, "0.0\t0.0\t0.0\t0.0\t0.0\t0.0\t0.0\n" );
232 <        }
233 <        else {
234 <          sprintf(painCave.errMsg,
235 <                  "Atom %d not found on processor %d\n",
236 <                  i, worldRank );
237 <          haveError= 1;
238 <          simError();
239 <        }
344 >          atomData6[0] = pos[0];
345 >          atomData6[1] = pos[1];
346 >          atomData6[2] = pos[2];
347  
348 <        if(haveError) nodeZeroError();
348 >          atomData6[3] = vel[0];
349 >          atomData6[4] = vel[1];
350 >          atomData6[5] = vel[2];
351 >          
352 >          isDirectional = 0;
353  
354 <      }
244 <      else {
245 <        myStatus = 1;
246 <        MPI_Send(&myStatus, 1, MPI_INT, which_node,
247 <                 TAKE_THIS_TAG_INT, MPI_COMM_WORLD);
248 <        MPI_Send(&i, 1, MPI_INT, which_node, TAKE_THIS_TAG_INT,
249 <                 MPI_COMM_WORLD);
250 <        MPI_Recv(writeLine, BUFFERSIZE, MPI_CHAR, which_node,
251 <                 TAKE_THIS_TAG_CHAR, MPI_COMM_WORLD, &istatus);
252 <        MPI_Recv(&myStatus, 1, MPI_INT, which_node,
253 <                 TAKE_THIS_TAG_INT, MPI_COMM_WORLD, &istatus);
354 >          if( atoms[local_index]->isDirectional() ){
355  
356 <        if(!myStatus) nodeZeroError();
356 >            isDirectional = 1;
357 >            
358 >            dAtom = (DirectionalAtom *)atoms[local_index];
359 >            dAtom->getQ( q );
360  
361 +            for (int j = 0; j < 6 ; j++)
362 +              atomData13[j] = atomData6[j];            
363 +            
364 +            atomData13[6] = q[0];
365 +            atomData13[7] = q[1];
366 +            atomData13[8] = q[2];
367 +            atomData13[9] = q[3];
368 +            
369 +            atomData13[10] = dAtom->getJx();
370 +            atomData13[11] = dAtom->getJy();
371 +            atomData13[12] = dAtom->getJz();
372 +          }
373 +          
374 +        } else {
375 +          sprintf(painCave.errMsg,
376 +                              "Atom %d not found on processor %d\n",
377 +                              i, worldRank );
378 +                haveError= 1;
379 +                simError();
380 +              }
381 +        
382 +        if(haveError) DieDieDie();
383 +        
384 +        currentIndex ++;
385        }
386 +      // If we've survived to here, format the line:
387 +      
388 +      if (!isDirectional) {
389 +        
390 +        sprintf( writeLine,
391 +                             "%s\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t",
392 +                             atomTypeString,
393 +                             atomData6[0],
394 +                             atomData6[1],
395 +                             atomData6[2],
396 +                             atomData6[3],
397 +                             atomData6[4],
398 +                             atomData6[5]);
399  
400 <      outFile << writeLine;
401 <      outFile.flush();
400 >              strcat( writeLine, "0.0\t0.0\t0.0\t0.0\t0.0\t0.0\t0.0\n" );
401 >        
402 >      } else {
403 >        
404 >              sprintf( writeLine,
405 >                             "%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",
406 >                             atomTypeString,
407 >                             atomData13[0],
408 >                             atomData13[1],
409 >                             atomData13[2],
410 >                             atomData13[3],
411 >                             atomData13[4],
412 >                             atomData13[5],
413 >                             atomData13[6],
414 >                             atomData13[7],
415 >                             atomData13[8],
416 >                             atomData13[9],
417 >                             atomData13[10],
418 >                             atomData13[11],
419 >                             atomData13[12]);
420 >        
421 >      }
422 >      
423 >      for(k = 0; k < outFile.size(); k++)
424 >        *outFile[k] << writeLine;
425      }
426 +    
427 +    for(k = 0; k < outFile.size(); k++)
428 +      outFile[k]->flush();
429 +    
430 +    sprintf( checkPointMsg,
431 +             "Sucessfully took a dump.\n");
432  
433 <    // kill everyone off:
264 <    myStatus = -1;
265 <    for (j = 0; j < mpiSim->getNumberProcessors(); j++) {
266 <      MPI_Send(&myStatus, 1, MPI_INT, j,
267 <               TAKE_THIS_TAG_INT, MPI_COMM_WORLD);
268 <    }
433 >    MPIcheckPoint();        
434  
435 +    delete[] potatoes;
436 +
437    } else {
438  
439 <    done = 0;
273 <    while (!done) {
439 >    // worldRank != 0, so I'm a remote node.  
440  
441 <      MPI_Recv(&myStatus, 1, MPI_INT, 0,
276 <               TAKE_THIS_TAG_INT, MPI_COMM_WORLD, &istatus);
441 >    // Set my magic potato to 0:
442  
443 <      if(!myStatus) anonymousNodeDie();
443 >    myPotato = 0;
444 >    currentIndex = 0;
445 >    
446 >    for (i = 0 ; i < mpiSim->getTotAtoms(); i++ ) {
447 >      
448 >      // Am I the node which has this atom?
449 >      
450 >      if (AtomToProcMap[i] == worldRank) {
451  
452 <      if(myStatus < 0) break;
452 >        if (myPotato + 3 >= MAXTAG) {
453  
454 <      MPI_Recv(&which_atom, 1, MPI_INT, 0,
455 <               TAKE_THIS_TAG_INT, MPI_COMM_WORLD, &istatus);
454 >          // The potato was going to exceed the maximum value,
455 >          // so wrap this processor potato back to 0 (and block until
456 >          // node 0 says we can go:
457  
458 <      myStatus = 1;
459 <      local_index=-1;
460 <      for (j=0; (j<mpiSim->getMyNlocal()) && (local_index < 0); j++) {
461 <        if (atoms[j]->getGlobalIndex() == which_atom) local_index = j;
462 <      }
463 <      if (local_index != -1) {
464 <        //format the line
458 >          MPI_Recv(&myPotato, 1, MPI_INT, 0, 0, MPI_COMM_WORLD, &istatus);
459 >          
460 >        }
461 >        which_atom = i;
462 >        local_index = indexArray[currentIndex].first;        
463 >                
464 >        if (which_atom == indexArray[currentIndex].second) {
465 >        
466 >          atomTypeString = atoms[local_index]->getType();
467  
468 <        atoms[local_index]->getPos(pos);
469 <        atoms[local_index]->getVel(vel);
468 >                atoms[local_index]->getPos(pos);
469 >                atoms[local_index]->getVel(vel);
470  
471 <        sprintf( tempBuffer,
472 <                 "%s\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t",
473 <                 atoms[local_index]->getType(),
299 <                 pos[0],
300 <                 pos[1],
301 <                 pos[2],
302 <                 vel[0],
303 <                 vel[1],
304 <                 vel[2]); // check here.
305 <        strcpy( writeLine, tempBuffer );
471 >          atomData6[0] = pos[0];
472 >          atomData6[1] = pos[1];
473 >          atomData6[2] = pos[2];
474  
475 <        if( atoms[local_index]->isDirectional() ){
475 >          atomData6[3] = vel[0];
476 >          atomData6[4] = vel[1];
477 >          atomData6[5] = vel[2];
478 >          
479 >          isDirectional = 0;
480  
481 <          dAtom = (DirectionalAtom *)atoms[local_index];
310 <          dAtom->getQ( q );
481 >          if( atoms[local_index]->isDirectional() ){
482  
483 <          sprintf( tempBuffer,
484 <                   "%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\n",
485 <                   q[0],
486 <                   q[1],
487 <                   q[2],
488 <                   q[3],
489 <                   dAtom->getJx(),
490 <                   dAtom->getJy(),
491 <                   dAtom->getJz());
492 <          strcat( writeLine, tempBuffer );
493 <        }
494 <        else{
495 <          strcat( writeLine, "0.0\t0.0\t0.0\t0.0\t0.0\t0.0\t0.0\n" );
496 <        }
497 <      }
498 <      else {
499 <        sprintf(painCave.errMsg,
329 <                "Atom %d not found on processor %d\n",
330 <                which_atom, worldRank );
331 <        myStatus = 0;
332 <        simError();
483 >            isDirectional = 1;
484 >            
485 >            dAtom = (DirectionalAtom *)atoms[local_index];
486 >            dAtom->getQ( q );
487 >            
488 >            for (int j = 0; j < 6 ; j++)
489 >              atomData13[j] = atomData6[j];
490 >            
491 >            atomData13[6] = q[0];
492 >            atomData13[7] = q[1];
493 >            atomData13[8] = q[2];
494 >            atomData13[9] = q[3];
495 >  
496 >            atomData13[10] = dAtom->getJx();
497 >            atomData13[11] = dAtom->getJy();
498 >            atomData13[12] = dAtom->getJz();
499 >          }
500  
501 <        strcpy( writeLine, "Hello, I'm an error.\n");
502 <      }
501 >        } else {
502 >                sprintf(painCave.errMsg,
503 >                              "Atom %d not found on processor %d\n",
504 >                              i, worldRank );
505 >                haveError= 1;
506 >                simError();
507 >              }
508  
509 <      MPI_Send(writeLine, BUFFERSIZE, MPI_CHAR, 0,
338 <               TAKE_THIS_TAG_CHAR, MPI_COMM_WORLD);
339 <      MPI_Send( &myStatus, 1, MPI_INT, 0,
340 <                TAKE_THIS_TAG_INT, MPI_COMM_WORLD);
341 <    }
342 <  }
343 <  outFile.flush();
344 <  sprintf( checkPointMsg,
345 <           "Sucessfully took a dump.\n");
346 <  MPIcheckPoint();
509 >        strncpy(MPIatomTypeString, atomTypeString, MINIBUFFERSIZE);
510  
511 < // last  thing last, enable  fatalities.
512 <  painCave.isEventLoop = 0;
511 >        // null terminate the string before sending (just in case):
512 >        MPIatomTypeString[MINIBUFFERSIZE-1] = '\0';
513  
514 < #endif // is_mpi
515 < }
514 >        MPI_Send(MPIatomTypeString, MINIBUFFERSIZE, MPI_CHAR, 0,
515 >                             myPotato, MPI_COMM_WORLD);
516 >        
517 >        myPotato++;
518  
519 < void DumpWriter::writeFinal(double finalTime){
519 >        MPI_Send(&isDirectional, 1, MPI_INT, 0,
520 >                             myPotato, MPI_COMM_WORLD);
521 >        
522 >        myPotato++;
523 >        
524 >        if (isDirectional) {
525  
526 <  char finalName[500];
527 <  ofstream finalOut;
526 >          MPI_Send(atomData13, 13, MPI_DOUBLE, 0,
527 >                   myPotato, MPI_COMM_WORLD);
528 >          
529 >        } else {
530  
531 <  const int BUFFERSIZE = 2000;
532 <  char tempBuffer[BUFFERSIZE];
361 <  char writeLine[BUFFERSIZE];
362 <
363 <  double q[4];
364 <  DirectionalAtom* dAtom;
365 <  Atom** atoms = entry_plug->atoms;
366 <  int i;
367 < #ifdef IS_MPI
368 <  int j, which_node, done, which_atom, local_index;
369 < #else //is_mpi
370 <  int nAtoms = entry_plug->n_atoms;
371 < #endif //is_mpi
372 <
373 <  double pos[3], vel[3];
374 <
375 < #ifdef IS_MPI
376 <  if(worldRank == 0 ){
377 < #endif // is_mpi
378 <
379 <    strcpy( finalName, entry_plug->finalName );
380 <
381 <    finalOut.open( finalName, ios::out | ios::trunc );
382 <    if( !finalOut ){
383 <      sprintf( painCave.errMsg,
384 <               "Could not open \"%s\" for final dump output.\n",
385 <               finalName );
386 <      painCave.isFatal = 1;
387 <      simError();
388 <    }
389 <
390 <    // finalOut.setf( ios::scientific );
391 <
392 < #ifdef IS_MPI
393 <  }
394 <
395 <  sprintf(checkPointMsg,"Opened file for final configuration\n");
396 <  MPIcheckPoint();
397 <
398 < #endif //is_mpi
399 <
400 <
401 < #ifndef IS_MPI
402 <
403 <  finalOut << nAtoms << "\n";
404 <
405 <  finalOut << finalTime << ";\t"
406 <           << entry_plug->Hmat[0][0] << "\t"
407 <           << entry_plug->Hmat[1][0] << "\t"
408 <           << entry_plug->Hmat[2][0] << ";\t"
409 <
410 <           << entry_plug->Hmat[0][1] << "\t"
411 <           << entry_plug->Hmat[1][1] << "\t"
412 <           << entry_plug->Hmat[2][1] << ";\t"
413 <
414 <           << entry_plug->Hmat[0][2] << "\t"
415 <           << entry_plug->Hmat[1][2] << "\t"
416 <           << entry_plug->Hmat[2][2] << ";";
417 <
418 <  //write out additional parameters, such as chi and eta
419 <  finalOut << entry_plug->the_integrator->getAdditionalParameters();
420 <  finalOut << endl;
421 <
422 <  for( i=0; i<nAtoms; i++ ){
423 <
424 <    atoms[i]->getPos(pos);
425 <    atoms[i]->getVel(vel);
426 <
427 <    sprintf( tempBuffer,
428 <             "%s\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t",
429 <             atoms[i]->getType(),
430 <             pos[0],
431 <             pos[1],
432 <             pos[2],
433 <             vel[0],
434 <             vel[1],
435 <             vel[2]);
436 <    strcpy( writeLine, tempBuffer );
437 <
438 <    if( atoms[i]->isDirectional() ){
439 <
440 <      dAtom = (DirectionalAtom *)atoms[i];
441 <      dAtom->getQ( q );
442 <
443 <      sprintf( tempBuffer,
444 <               "%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\n",
445 <               q[0],
446 <               q[1],
447 <               q[2],
448 <               q[3],
449 <               dAtom->getJx(),
450 <               dAtom->getJy(),
451 <               dAtom->getJz());
452 <      strcat( writeLine, tempBuffer );
453 <    }
454 <    else
455 <      strcat( writeLine, "0.0\t0.0\t0.0\t0.0\t0.0\t0.0\t0.0\n" );
456 <
457 <    finalOut << writeLine;
458 <  }
459 <  finalOut.flush();
460 <  finalOut.close();
461 <
462 < #else // is_mpi
463 <
464 <  // first thing first, suspend fatalities.
465 <  painCave.isEventLoop = 1;
466 <
467 <  int myStatus; // 1 = wakeup & success; 0 = error; -1 = AllDone
468 <  int haveError;
469 <
470 <  MPI_Status istatus;
471 <  int *AtomToProcMap = mpiSim->getAtomToProcMap();
472 <
473 <  // write out header and node 0's coordinates
474 <
475 <  haveError = 0;
476 <  if( worldRank == 0 ){
477 <    finalOut << mpiSim->getTotAtoms() << "\n";
478 <
479 <    finalOut << finalTime << ";\t"
480 <             << entry_plug->Hmat[0][0] << "\t"
481 <             << entry_plug->Hmat[1][0] << "\t"
482 <             << entry_plug->Hmat[2][0] << ";\t"
483 <
484 <             << entry_plug->Hmat[0][1] << "\t"
485 <             << entry_plug->Hmat[1][1] << "\t"
486 <             << entry_plug->Hmat[2][1] << ";\t"
487 <
488 <             << entry_plug->Hmat[0][2] << "\t"
489 <             << entry_plug->Hmat[1][2] << "\t"
490 <             << entry_plug->Hmat[2][2] << ";";
491 <
492 <    //write out additional parameters, such as chi and eta
493 <    finalOut << entry_plug->the_integrator->getAdditionalParameters();
494 <    finalOut << endl;
495 <
496 <    for (i = 0 ; i < mpiSim->getTotAtoms(); i++ ) {
497 <      // Get the Node number which has this molecule:
498 <
499 <      which_node = AtomToProcMap[i];
500 <
501 <      if (which_node == mpiSim->getMyNode()) {
502 <
503 <        which_atom = i;
504 <        local_index=-1;
505 <        for (j=0; (j<mpiSim->getMyNlocal()) && (local_index < 0); j++) {
506 <          if (atoms[j]->getGlobalIndex() == which_atom) local_index = j;
531 >          MPI_Send(atomData6, 6, MPI_DOUBLE, 0,
532 >                   myPotato, MPI_COMM_WORLD);
533          }
508        if (local_index != -1) {
534  
535 <          atoms[local_index]->getPos(pos);
536 <          atoms[local_index]->getVel(vel);
512 <
513 <          sprintf( tempBuffer,
514 <                   "%s\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t",
515 <                   atoms[local_index]->getType(),
516 <                   pos[0],
517 <                   pos[1],
518 <                   pos[2],
519 <                   vel[0],
520 <                   vel[1],
521 <                   vel[2]);
522 <          strcpy( writeLine, tempBuffer );
523 <
524 <          if( atoms[local_index]->isDirectional() ){
525 <
526 <            dAtom = (DirectionalAtom *)atoms[local_index];
527 <            dAtom->getQ( q );
528 <
529 <            sprintf( tempBuffer,
530 <                     "%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\n",
531 <                     q[0],
532 <                     q[1],
533 <                     q[2],
534 <                     q[3],
535 <                     dAtom->getJx(),
536 <                     dAtom->getJy(),
537 <                     dAtom->getJz());
538 <            strcat( writeLine, tempBuffer );
539 <          }
540 <          else
541 <            strcat( writeLine, "0.0\t0.0\t0.0\t0.0\t0.0\t0.0\t0.0\n" );
542 <        }
543 <        else {
544 <          sprintf(painCave.errMsg,
545 <                  "Atom %d not found on processor %d\n",
546 <                  i, worldRank );
547 <          haveError= 1;
548 <          simError();
549 <        }
550 <
551 <        if(haveError) nodeZeroError();
552 <
535 >        myPotato++;  
536 >        currentIndex++;    
537        }
554      else {
555
556        myStatus = 1;
557        MPI_Send(&myStatus, 1, MPI_INT, which_node,
558                 TAKE_THIS_TAG_INT, MPI_COMM_WORLD);
559        MPI_Send(&i, 1, MPI_INT, which_node, TAKE_THIS_TAG_INT,
560                 MPI_COMM_WORLD);
561        MPI_Recv(writeLine, BUFFERSIZE, MPI_CHAR, which_node,
562                 TAKE_THIS_TAG_CHAR, MPI_COMM_WORLD, &istatus);
563        MPI_Recv(&myStatus, 1, MPI_INT, which_node,
564                 TAKE_THIS_TAG_INT, MPI_COMM_WORLD, &istatus);
565
566        if(!myStatus) nodeZeroError();
567      }
568
569      finalOut << writeLine;
538      }
539  
540 <    // kill everyone off:
541 <    myStatus = -1;
542 <    for (j = 0; j < mpiSim->getNumberProcessors(); j++) {
543 <      MPI_Send(&myStatus, 1, MPI_INT, j,
544 <               TAKE_THIS_TAG_INT, MPI_COMM_WORLD);
545 <    }
578 <
579 <  } else {
580 <
581 <    done = 0;
582 <    while (!done) {
583 <
584 <      MPI_Recv(&myStatus, 1, MPI_INT, 0,
585 <               TAKE_THIS_TAG_INT, MPI_COMM_WORLD, &istatus);
586 <
587 <      if(!myStatus) anonymousNodeDie();
588 <
589 <      if(myStatus < 0) break;
590 <
591 <      MPI_Recv(&which_atom, 1, MPI_INT, 0,
592 <               TAKE_THIS_TAG_INT, MPI_COMM_WORLD, &istatus);
593 <
594 <      myStatus = 1;
595 <      local_index=-1;
596 <      for (j=0; j < mpiSim->getMyNlocal(); j++) {
597 <        if (atoms[j]->getGlobalIndex() == which_atom) local_index = j;
598 <      }
599 <      if (local_index != -1) {
600 <
601 <        atoms[local_index]->getPos(pos);
602 <        atoms[local_index]->getVel(vel);
603 <
604 <        //format the line
605 <        sprintf( tempBuffer,
606 <                 "%s\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t",
607 <                 atoms[local_index]->getType(),
608 <                 pos[0],
609 <                 pos[1],
610 <                 pos[2],
611 <                 vel[0],
612 <                 vel[1],
613 <                 vel[2]); // check here.
614 <        strcpy( writeLine, tempBuffer );
615 <
616 <        if( atoms[local_index]->isDirectional() ){
617 <
618 <          dAtom = (DirectionalAtom *)atoms[local_index];
619 <          dAtom->getQ( q );
620 <
621 <          sprintf( tempBuffer,
622 <                   "%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\n",
623 <                   q[0],
624 <                   q[1],
625 <                   q[2],
626 <                   q[3],
627 <                   dAtom->getJx(),
628 <                   dAtom->getJy(),
629 <                   dAtom->getJz());
630 <          strcat( writeLine, tempBuffer );
631 <        }
632 <        else{
633 <          strcat( writeLine, "0.0\t0.0\t0.0\t0.0\t0.0\t0.0\t0.0\n" );
634 <        }
635 <      }
636 <      else {
637 <        sprintf(painCave.errMsg,
638 <                "Atom %d not found on processor %d\n",
639 <                which_atom, worldRank );
640 <        myStatus = 0;
641 <        simError();
642 <
643 <        strcpy( writeLine, "Hello, I'm an error.\n");
644 <      }
645 <
646 <      MPI_Send(writeLine, BUFFERSIZE, MPI_CHAR, 0,
647 <               TAKE_THIS_TAG_CHAR, MPI_COMM_WORLD);
648 <      MPI_Send( &myStatus, 1, MPI_INT, 0,
649 <                TAKE_THIS_TAG_INT, MPI_COMM_WORLD);
650 <    }
651 <  }
652 <  finalOut.flush();
653 <  sprintf( checkPointMsg,
654 <           "Sucessfully took a dump.\n");
655 <  MPIcheckPoint();
656 <
657 <  if( worldRank == 0 ) finalOut.close();
540 >    sprintf( checkPointMsg,
541 >             "Sucessfully took a dump.\n");
542 >    MPIcheckPoint();        
543 >    
544 >  }
545 >  
546   #endif // is_mpi
547   }
548  
661
662
549   #ifdef IS_MPI
550  
551   // a couple of functions to let us escape the write loop
552  
553 < void dWrite::nodeZeroError( void ){
668 <  int j, myStatus;
553 > void dWrite::DieDieDie( void ){
554  
670  myStatus = 0;
671  for (j = 0; j < mpiSim->getNumberProcessors(); j++) {
672    MPI_Send( &myStatus, 1, MPI_INT, j,
673              TAKE_THIS_TAG_INT, MPI_COMM_WORLD);
674  }
675
676
555    MPI_Finalize();
556    exit (0);
679
557   }
558  
682 void dWrite::anonymousNodeDie( void ){
683
684  MPI_Finalize();
685  exit (0);
686 }
687
559   #endif //is_mpi

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines