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 469 by mmeineke, Mon Apr 7 20:06:31 2003 UTC vs.
Revision 934 by tim, Tue Jan 13 20:04:28 2004 UTC

# Line 1 | Line 1
1 < #include <cstring>
1 > #define _FILE_OFFSET_BITS 64
2 >
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"
8 #define TAKE_THIS_TAG_CHAR 1
9 #define TAKE_THIS_TAG_INT 2
12  
13   namespace dWrite{
14 <  void nodeZeroError( void );
13 <  void anonymousNodeDie( void );
14 >  void DieDieDie( void );
15   }
16  
17   using namespace dWrite;
# Line 23 | Line 24 | DumpWriter::DumpWriter( SimInfo* the_entry_plug ){
24  
25    entry_plug = the_entry_plug;
26  
26  std::cerr << "Look at me I'm a freebird!\n";
27
27   #ifdef IS_MPI
28    if(worldRank == 0 ){
29   #endif // is_mpi
30 <    
31 <
32 <    
33 <    strcpy( outName, entry_plug->sampleName );
34 <    
35 <    outFile.open(outName, ios::out | ios::trunc );
37 <    
38 <    if( !outFile ){
39 <      
30 >
31 >
32 >    dumpFile.open(entry_plug->sampleName, ios::out | ios::trunc );
33 >
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 <    std::cerr << "me TOO!\n";
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  
49    //outFile.setf( ios::scientific );
50
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 63 | 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 <  const int BUFFERSIZE = 2000;
89 <  char tempBuffer[BUFFERSIZE];
90 <  char writeLine[BUFFERSIZE];
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 <  int i, j, which_node, done, which_atom, local_index;
97 <  double q[4];
98 <  DirectionalAtom* dAtom;
99 <  int nAtoms = entry_plug->n_atoms;
96 > /**
97 > * Sorting the local index by global index
98 > */
99 >
100 > void DumpWriter::sortByGlobalIndex(){
101    Atom** atoms = entry_plug->atoms;
102 <    
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 < #ifndef IS_MPI
87 <    
88 <  outFile << nAtoms << "\n";
89 <    
90 <  outFile << currentTime << "\t"
91 <          << entry_plug->box_x << "\t"
92 <          << entry_plug->box_y << "\t"
93 <          << entry_plug->box_z << "\n";
94 <    
95 <  for( i=0; i<nAtoms; i++ ){
96 <      
112 > void DumpWriter::writeDump(double currentTime){
113  
114 <    sprintf( tempBuffer,
99 <             "%s\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t",
100 <             atoms[i]->getType(),
101 <             atoms[i]->getX(),
102 <             atoms[i]->getY(),
103 <             atoms[i]->getZ(),
104 <             atoms[i]->get_vx(),
105 <             atoms[i]->get_vy(),
106 <             atoms[i]->get_vz());
107 <    strcpy( writeLine, tempBuffer );
114 >  vector<ofstream*> fileStreams;
115  
116 <    if( atoms[i]->isDirectional() ){
117 <        
118 <      dAtom = (DirectionalAtom *)atoms[i];
112 <      dAtom->getQ( q );
113 <        
114 <      sprintf( tempBuffer,
115 <               "%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\n",
116 <               q[0],
117 <               q[1],
118 <               q[2],
119 <               q[3],
120 <               dAtom->getJx(),
121 <               dAtom->getJy(),
122 <               dAtom->getJz());
123 <      strcat( writeLine, tempBuffer );
124 <    }
125 <    else
126 <      strcat( writeLine, "0.0\t0.0\t0.0\t0.0\t0.0\t0.0\t0.0\n" );
127 <      
128 <    outFile << writeLine;
116 > #ifdef IS_MPI
117 >  if(worldRank == 0 ){
118 >    finalOut.seekp(0);
119    }
120 <  outFile.flush();
120 > #endif // is_mpi
121  
122 < #else // is_mpi
122 >  fileStreams.push_back(&finalOut);
123 >  fileStreams.push_back(&dumpFile);
124  
125 <  // first thing first, suspend fatalities.
126 <  painCave.isEventLoop = 1;
125 >  writeFrame(fileStreams, currentTime);
126 >        
127 > }
128  
129 <  int myStatus; // 1 = wakeup & success; 0 = error; -1 = AllDone
138 <  int haveError;
129 > void DumpWriter::writeFinal(double currentTime){
130  
131 <  MPI_Status istatus;
141 <  int *AtomToProcMap = mpiSim->getAtomToProcMap();
142 <  
143 <  // write out header and node 0's coordinates
144 <  
145 <  if( worldRank == 0 ){
146 <    outFile << mpiSim->getTotAtoms() << "\n";
147 <    
148 <    outFile << currentTime << "\t"
149 <            << entry_plug->box_x << "\t"
150 <            << entry_plug->box_y << "\t"
151 <            << entry_plug->box_z << "\n";
152 <    outFile.flush();
153 <    for (i = 0 ; i < mpiSim->getTotAtoms(); i++ ) {
154 <      // Get the Node number which has this atom;
155 <      
156 <      which_node = AtomToProcMap[i];    
157 <      
158 <      if (which_node == 0 ) {
159 <        
160 <        haveError = 0;
161 <        which_atom = i;
162 <        local_index=-1;        
163 <        for (j=0; (j<mpiSim->getMyNlocal()) && (local_index < 0); j++) {
164 <          if (atoms[j]->getGlobalIndex() == which_atom) local_index = j;
165 <        }
166 <        if (local_index != -1) {
167 <          //format the line
168 <          sprintf( tempBuffer,
169 <                   "%s\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t",
170 <                   atoms[local_index]->getType(),
171 <                   atoms[local_index]->getX(),
172 <                   atoms[local_index]->getY(),
173 <                   atoms[local_index]->getZ(),
174 <                   atoms[local_index]->get_vx(),
175 <                   atoms[local_index]->get_vy(),
176 <                   atoms[local_index]->get_vz()); // check here.
177 <          strcpy( writeLine, tempBuffer );
178 <          
179 <          if( atoms[local_index]->isDirectional() ){
180 <            
181 <            dAtom = (DirectionalAtom *)atoms[local_index];
182 <            dAtom->getQ( q );
183 <            
184 <            sprintf( tempBuffer,
185 <                     "%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\n",
186 <                     q[0],
187 <                     q[1],
188 <                     q[2],
189 <                     q[3],
190 <                     dAtom->getJx(),
191 <                     dAtom->getJy(),
192 <                     dAtom->getJz());
193 <            strcat( writeLine, tempBuffer );
194 <            
195 <          }
196 <          else
197 <            strcat( writeLine, "0.0\t0.0\t0.0\t0.0\t0.0\t0.0\t0.0\n" );      
198 <        }
199 <        else {
200 <          sprintf(painCave.errMsg,
201 <                  "Atom %d not found on processor %d\n",
202 <                  i, worldRank );
203 <          haveError= 1;
204 <          simError();
205 <        }
206 <        
207 <        if(haveError) nodeZeroError();
208 <
209 <      }
210 <      else {
211 <        myStatus = 1;
212 <        MPI_Send(&myStatus, 1, MPI_INT, which_node,
213 <                 TAKE_THIS_TAG_INT, MPI_COMM_WORLD);
214 <        MPI_Send(&i, 1, MPI_INT, which_node, TAKE_THIS_TAG_INT,
215 <                 MPI_COMM_WORLD);
216 <        MPI_Recv(writeLine, BUFFERSIZE, MPI_CHAR, which_node,
217 <                 TAKE_THIS_TAG_CHAR, MPI_COMM_WORLD, &istatus);
218 <        MPI_Recv(&myStatus, 1, MPI_INT, which_node,
219 <                 TAKE_THIS_TAG_INT, MPI_COMM_WORLD, &istatus);
220 <        
221 <        if(!myStatus) nodeZeroError();
222 <
223 <      }
224 <      
225 <      outFile << writeLine;
226 <      outFile.flush();
227 <    }
228 <    
229 <    // kill everyone off:
230 <    myStatus = -1;
231 <    for (j = 0; j < mpiSim->getNumberProcessors(); j++) {      
232 <      MPI_Send(&myStatus, 1, MPI_INT, j,
233 <               TAKE_THIS_TAG_INT, MPI_COMM_WORLD);
234 <    }
131 >  vector<ofstream*> fileStreams;
132  
133 <  } else {
134 <    
135 <    done = 0;
136 <    while (!done) {
137 <      
138 <      MPI_Recv(&myStatus, 1, MPI_INT, 0,
139 <               TAKE_THIS_TAG_INT, MPI_COMM_WORLD, &istatus);
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 <      if(!myStatus) anonymousNodeDie();
245 <      
246 <      if(myStatus < 0) break;
144 > void DumpWriter::writeFrame( vector<ofstream*>& outFile, double currentTime ){
145  
146 <      MPI_Recv(&which_atom, 1, MPI_INT, 0,
147 <               TAKE_THIS_TAG_INT, MPI_COMM_WORLD, &istatus);
250 <      
251 <      myStatus = 1;
252 <      local_index=-1;        
253 <      for (j=0; (j<mpiSim->getMyNlocal()) && (local_index < 0); j++) {
254 <        if (atoms[j]->getGlobalIndex() == which_atom) local_index = j;
255 <      }
256 <      if (local_index != -1) {
257 <        //format the line
258 <        sprintf( tempBuffer,
259 <                 "%s\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t",
260 <                 atoms[local_index]->getType(),
261 <                 atoms[local_index]->getX(),
262 <                 atoms[local_index]->getY(),
263 <                 atoms[local_index]->getZ(),
264 <                 atoms[local_index]->get_vx(),
265 <                 atoms[local_index]->get_vy(),
266 <                 atoms[local_index]->get_vz()); // check here.
267 <        strcpy( writeLine, tempBuffer );
268 <        
269 <        if( atoms[local_index]->isDirectional() ){
270 <          
271 <          dAtom = (DirectionalAtom *)atoms[local_index];
272 <          dAtom->getQ( q );
273 <          
274 <          sprintf( tempBuffer,
275 <                   "%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\n",
276 <                   q[0],
277 <                   q[1],
278 <                   q[2],
279 <                   q[3],
280 <                   dAtom->getJx(),
281 <                   dAtom->getJy(),
282 <                   dAtom->getJz());
283 <          strcat( writeLine, tempBuffer );
284 <        }
285 <        else{
286 <          strcat( writeLine, "0.0\t0.0\t0.0\t0.0\t0.0\t0.0\t0.0\n" );
287 <        }
288 <      }
289 <      else {
290 <        sprintf(painCave.errMsg,
291 <                "Atom %d not found on processor %d\n",
292 <                which_atom, worldRank );
293 <        myStatus = 0;
294 <        simError();
146 >  const int BUFFERSIZE = 2000;
147 >  const int MINIBUFFERSIZE = 100;
148  
149 <        strcpy( writeLine, "Hello, I'm an error.\n");
150 <      }
149 >  char tempBuffer[BUFFERSIZE];
150 >  char writeLine[BUFFERSIZE];
151  
152 <      MPI_Send(writeLine, BUFFERSIZE, MPI_CHAR, 0,
300 <               TAKE_THIS_TAG_CHAR, MPI_COMM_WORLD);
301 <      MPI_Send( &myStatus, 1, MPI_INT, 0,
302 <                TAKE_THIS_TAG_INT, MPI_COMM_WORLD);
303 <    }
304 <  }  
305 <  outFile.flush();
306 <  sprintf( checkPointMsg,
307 <           "Sucessfully took a dump.\n");
308 <  MPIcheckPoint();
152 >  int i, k;
153  
154 < // last  thing last, enable  fatalities.
155 <  painCave.isEventLoop = 0;
154 > #ifdef IS_MPI
155 >  
156 >  int *potatoes;
157 >  int myPotato;
158  
159 < #endif // is_mpi
160 < }
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 < void DumpWriter::writeFinal(){
167 > #else //is_mpi
168 >  int nAtoms = entry_plug->n_atoms;
169 > #endif //is_mpi
170  
318  char finalName[500];
319  ofstream finalOut;
320
321  const int BUFFERSIZE = 2000;
322  char tempBuffer[BUFFERSIZE];
323  char writeLine[BUFFERSIZE];  
324
171    double q[4];
172    DirectionalAtom* dAtom;
327  int nAtoms = entry_plug->n_atoms;
173    Atom** atoms = entry_plug->atoms;
174 <  int i, j, which_node, done, game_over, which_atom, local_index;
330 <  
331 <  
332 < #ifdef IS_MPI
333 <  if(worldRank == 0 ){
334 < #endif // is_mpi
335 <    
336 <    strcpy( finalName, entry_plug->finalName );
337 <    
338 <    finalOut.open( finalName, ios::out | ios::trunc );
339 <    if( !finalOut ){
340 <      sprintf( painCave.errMsg,
341 <               "Could not open \"%s\" for final dump output.\n",
342 <               finalName );
343 <      painCave.isFatal = 1;
344 <      simError();
345 <    }
346 <    
347 <    // finalOut.setf( ios::scientific );
348 <    
349 < #ifdef IS_MPI
350 <  }
351 <  
352 <  sprintf(checkPointMsg,"Opened file for final configuration\n");
353 <  MPIcheckPoint();  
354 <  
355 < #endif //is_mpi
174 >  double pos[3], vel[3];
175  
357  
176   #ifndef IS_MPI
359    
360  finalOut << nAtoms << "\n";
361    
362  finalOut << entry_plug->box_x << "\t"
363           << entry_plug->box_y << "\t"
364           << entry_plug->box_z << "\n";
177    
178 +  for(k = 0; k < outFile.size(); k++){
179 +    *outFile[k] << nAtoms << "\n";
180 +
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 +                     << entry_plug->Hmat[0][2] << "\t"
191 +                     << entry_plug->Hmat[1][2] << "\t"
192 +                     << entry_plug->Hmat[2][2] << ";";
193 +
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 <      
199 >
200 >    atoms[i]->getPos(pos);
201 >    atoms[i]->getVel(vel);
202 >
203      sprintf( tempBuffer,
204               "%s\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t",
205               atoms[i]->getType(),
206 <             atoms[i]->getX(),
207 <             atoms[i]->getY(),
208 <             atoms[i]->getZ(),
209 <             atoms[i]->get_vx(),
210 <             atoms[i]->get_vy(),
211 <             atoms[i]->get_vz());
206 >             pos[0],
207 >             pos[1],
208 >             pos[2],
209 >             vel[0],
210 >             vel[1],
211 >             vel[2]);
212      strcpy( writeLine, tempBuffer );
213  
214      if( atoms[i]->isDirectional() ){
215 <        
215 >
216        dAtom = (DirectionalAtom *)atoms[i];
217        dAtom->getQ( q );
218 <        
218 >
219        sprintf( tempBuffer,
220                 "%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\n",
221                 q[0],
# Line 394 | Line 229 | void DumpWriter::writeFinal(){
229      }
230      else
231        strcat( writeLine, "0.0\t0.0\t0.0\t0.0\t0.0\t0.0\t0.0\n" );
232 <      
233 <    finalOut << writeLine;
232 >
233 >    for(k = 0; k < outFile.size(); k++)
234 >      *outFile[k] << writeLine;
235    }
400  finalOut.flush();
401  finalOut.close();
236  
237   #else // is_mpi
238 +
239 +  /* code to find maximum tag value */
240    
241 <  // first thing first, suspend fatalities.
242 <  painCave.isEventLoop = 1;
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  
408  int myStatus; // 1 = wakeup & success; 0 = error; -1 = AllDone
249    int haveError;
250  
251    MPI_Status istatus;
252    int *AtomToProcMap = mpiSim->getAtomToProcMap();
253  
254    // write out header and node 0's coordinates
255 <  
416 <  haveError = 0;
255 >
256    if( worldRank == 0 ){
257 <    finalOut << mpiSim->getTotAtoms() << "\n";
257 >
258 >    // Node 0 needs a list of the magic potatoes for each processor;
259 >
260 >    nProc = mpiSim->getNumberProcessors();
261 >    potatoes = new int[nProc];
262 >
263 >    //write out the comment lines
264 >    for (i = 0; i < nProc; i++)
265 >      potatoes[i] = 0;
266      
267 <    finalOut << entry_plug->box_x << "\t"
268 <            << entry_plug->box_y << "\t"
269 <            << entry_plug->box_z << "\n";
270 <    
267 >      for(k = 0; k < outFile.size(); k++){
268 >        *outFile[k] << mpiSim->getTotAtoms() << "\n";
269 >
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++ ) {
425      // Get the Node number which has this molecule:
289        
290 <      which_node = AtomToProcMap[i];    
290 >      // Get the Node number which has this atom;
291        
292 <      if (which_node == mpiSim->getMyNode()) {
292 >      which_node = AtomToProcMap[i];
293 >      
294 >      if (which_node != 0) {
295  
296 <        which_atom = i;
297 <        local_index=-1;        
298 <        for (j=0; (j<mpiSim->getMyNlocal()) && (local_index < 0); j++) {
299 <          if (atoms[j]->getGlobalIndex() == which_atom) local_index = j;
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 >          potatoes[which_node] = 0;          
301 >          MPI_Send(0, 1, MPI_INT, which_node, 0, MPI_COMM_WORLD);
302 >          
303          }
436        if (local_index != -1) {        
437          sprintf( tempBuffer,
438                   "%s\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t",
439                   atoms[local_index]->getType(),
440                   atoms[local_index]->getX(),
441                   atoms[local_index]->getY(),
442                   atoms[local_index]->getZ(),
443                   atoms[local_index]->get_vx(),
444                   atoms[local_index]->get_vy(),
445                   atoms[local_index]->get_vz());
446          strcpy( writeLine, tempBuffer );
447          
448          if( atoms[local_index]->isDirectional() ){
449            
450            dAtom = (DirectionalAtom *)atoms[local_index];
451            dAtom->getQ( q );
452            
453            sprintf( tempBuffer,
454                     "%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\n",
455                     q[0],
456                     q[1],
457                     q[2],
458                     q[3],
459                     dAtom->getJx(),
460                     dAtom->getJy(),
461                     dAtom->getJz());
462            strcat( writeLine, tempBuffer );
463          }
464          else
465            strcat( writeLine, "0.0\t0.0\t0.0\t0.0\t0.0\t0.0\t0.0\n" );      
466        }
467        else {
468          sprintf(painCave.errMsg,
469                  "Atom %d not found on processor %d\n",
470                  i, worldRank );
471          haveError= 1;
472          simError();
473        }
304  
305 <        if(haveError) nodeZeroError();
476 <    
477 <      }
478 <      else {
305 >        myPotato = potatoes[which_node];        
306          
307 <        myStatus = 1;
308 <        MPI_Send(&myStatus, 1, MPI_INT, which_node,
309 <                 TAKE_THIS_TAG_INT, MPI_COMM_WORLD);
310 <        MPI_Send(&i, 1, MPI_INT, which_node, TAKE_THIS_TAG_INT,
484 <                 MPI_COMM_WORLD);
485 <        MPI_Recv(writeLine, BUFFERSIZE, MPI_CHAR, which_node,
486 <                 TAKE_THIS_TAG_CHAR, MPI_COMM_WORLD, &istatus);
487 <        MPI_Recv(&myStatus, 1, MPI_INT, which_node,
488 <                 TAKE_THIS_TAG_INT, MPI_COMM_WORLD, &istatus);
307 >        MPI_Recv(MPIatomTypeString, MINIBUFFERSIZE, MPI_CHAR, which_node,
308 >                 myPotato, MPI_COMM_WORLD, &istatus);
309 >        
310 >        atomTypeString = MPIatomTypeString;
311          
312 <        if(!myStatus) nodeZeroError();
312 >        myPotato++;
313 >
314 >        MPI_Recv(&isDirectional, 1, MPI_INT, which_node,
315 >                 myPotato, MPI_COMM_WORLD, &istatus);
316 >              
317 >        myPotato++;
318 >
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 >      } 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 >                atoms[local_index]->getPos(pos);
342 >                atoms[local_index]->getVel(vel);          
343 >
344 >          atomData6[0] = pos[0];
345 >          atomData6[1] = pos[1];
346 >          atomData6[2] = pos[2];
347 >
348 >          atomData6[3] = vel[0];
349 >          atomData6[4] = vel[1];
350 >          atomData6[5] = vel[2];
351 >          
352 >          isDirectional = 0;
353 >
354 >          if( atoms[local_index]->isDirectional() ){
355 >
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 <      finalOut << writeLine;
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 >              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 <    // kill everyone off:
428 <    myStatus = -1;
429 <    for (j = 0; j < mpiSim->getNumberProcessors(); j++) {      
430 <      MPI_Send(&myStatus, 1, MPI_INT, j,
431 <               TAKE_THIS_TAG_INT, MPI_COMM_WORLD);
501 <    }
427 >    for(k = 0; k < outFile.size(); k++)
428 >      outFile[k]->flush();
429 >    
430 >    sprintf( checkPointMsg,
431 >             "Sucessfully took a dump.\n");
432  
433 +    MPIcheckPoint();        
434 +
435 +    delete[] potatoes;
436 +
437    } else {
504    
505    done = 0;
506    while (!done) {
438  
439 <      MPI_Recv(&myStatus, 1, MPI_INT, 0,
440 <               TAKE_THIS_TAG_INT, MPI_COMM_WORLD, &istatus);
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->getTotAtoms(); i++ ) {
447        
448 <      if(!myStatus) anonymousNodeDie();
448 >      // Am I the node which has this atom?
449        
450 <      if(myStatus < 0) break;
514 <      
515 <      MPI_Recv(&which_atom, 1, MPI_INT, 0,
516 <               TAKE_THIS_TAG_INT, MPI_COMM_WORLD, &istatus);
517 <      
518 <      myStatus = 1;
519 <      local_index=-1;        
520 <      for (j=0; j < mpiSim->getMyNlocal(); j++) {
521 <        if (atoms[j]->getGlobalIndex() == which_atom) local_index = j;
522 <      }
523 <      if (local_index != -1) {
450 >      if (AtomToProcMap[i] == worldRank) {
451  
452 <        //format the line
526 <        sprintf( tempBuffer,
527 <                 "%s\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t",
528 <                 atoms[local_index]->getType(),
529 <                 atoms[local_index]->getX(),
530 <                 atoms[local_index]->getY(),
531 <                 atoms[local_index]->getZ(),
532 <                 atoms[local_index]->get_vx(),
533 <                 atoms[local_index]->get_vy(),
534 <                 atoms[local_index]->get_vz()); // check here.
535 <        strcpy( writeLine, tempBuffer );
536 <        
537 <        if( atoms[local_index]->isDirectional() ){
538 <          
539 <          dAtom = (DirectionalAtom *)atoms[local_index];
540 <          dAtom->getQ( q );
541 <          
542 <          sprintf( tempBuffer,
543 <                   "%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\n",
544 <                   q[0],
545 <                   q[1],
546 <                   q[2],
547 <                   q[3],
548 <                   dAtom->getJx(),
549 <                   dAtom->getJy(),
550 <                   dAtom->getJz());
551 <          strcat( writeLine, tempBuffer );
552 <        }
553 <        else{
554 <          strcat( writeLine, "0.0\t0.0\t0.0\t0.0\t0.0\t0.0\t0.0\n" );
555 <        }
556 <      }
557 <      else {
558 <        sprintf(painCave.errMsg,
559 <                "Atom %d not found on processor %d\n",
560 <                which_atom, worldRank );
561 <        myStatus = 0;
562 <        simError();
563 <        
564 <        strcpy( writeLine, "Hello, I'm an error.\n");
565 <      }
452 >        if (myPotato + 3 >= MAXTAG) {
453  
454 <      MPI_Send(writeLine, BUFFERSIZE, MPI_CHAR, 0,
455 <               TAKE_THIS_TAG_CHAR, MPI_COMM_WORLD);
456 <      MPI_Send( &myStatus, 1, MPI_INT, 0,
570 <                TAKE_THIS_TAG_INT, MPI_COMM_WORLD);
571 <    }
572 <  }
573 <  finalOut.flush();
574 <  sprintf( checkPointMsg,
575 <           "Sucessfully took a dump.\n");
576 <  MPIcheckPoint();
577 <  
578 <  if( worldRank == 0 ) finalOut.close();    
579 < #endif // is_mpi
580 < }
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 +          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);
470  
471 < #ifdef IS_MPI
471 >          atomData6[0] = pos[0];
472 >          atomData6[1] = pos[1];
473 >          atomData6[2] = pos[2];
474  
475 < // a couple of functions to let us escape the write loop
475 >          atomData6[3] = vel[0];
476 >          atomData6[4] = vel[1];
477 >          atomData6[5] = vel[2];
478 >          
479 >          isDirectional = 0;
480  
481 < void dWrite::nodeZeroError( void ){
482 <  int j, myStatus;
481 >          if( atoms[local_index]->isDirectional() ){
482 >
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 <  myStatus = 0;
497 <  for (j = 0; j < mpiSim->getNumberProcessors(); j++) {      
498 <    MPI_Send( &myStatus, 1, MPI_INT, j,
499 <              TAKE_THIS_TAG_INT, MPI_COMM_WORLD);
595 <  }  
596 <  
496 >            atomData13[10] = dAtom->getJx();
497 >            atomData13[11] = dAtom->getJy();
498 >            atomData13[12] = dAtom->getJz();
499 >          }
500  
501 <  MPI_Finalize();
502 <  exit (0);
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 >        strncpy(MPIatomTypeString, atomTypeString, MINIBUFFERSIZE);
510 >
511 >        // null terminate the string before sending (just in case):
512 >        MPIatomTypeString[MINIBUFFERSIZE-1] = '\0';
513 >
514 >        MPI_Send(MPIatomTypeString, MINIBUFFERSIZE, MPI_CHAR, 0,
515 >                             myPotato, MPI_COMM_WORLD);
516 >        
517 >        myPotato++;
518 >
519 >        MPI_Send(&isDirectional, 1, MPI_INT, 0,
520 >                             myPotato, MPI_COMM_WORLD);
521 >        
522 >        myPotato++;
523 >        
524 >        if (isDirectional) {
525 >
526 >          MPI_Send(atomData13, 13, MPI_DOUBLE, 0,
527 >                   myPotato, MPI_COMM_WORLD);
528 >          
529 >        } else {
530 >
531 >          MPI_Send(atomData6, 6, MPI_DOUBLE, 0,
532 >                   myPotato, MPI_COMM_WORLD);
533 >        }
534 >
535 >        myPotato++;  
536 >        currentIndex++;    
537 >      }
538 >    }
539 >
540 >    sprintf( checkPointMsg,
541 >             "Sucessfully took a dump.\n");
542 >    MPIcheckPoint();        
543 >    
544 >  }
545    
546 + #endif // is_mpi
547   }
548  
549 < void dWrite::anonymousNodeDie( void ){
549 > #ifdef IS_MPI
550  
551 + // a couple of functions to let us escape the write loop
552 +
553 + void dWrite::DieDieDie( void ){
554 +
555    MPI_Finalize();
556    exit (0);
557   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines