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 572 by mmeineke, Wed Jul 2 21:26:55 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 26 | Line 27 | DumpWriter::DumpWriter( SimInfo* the_entry_plug ){
27   #ifdef IS_MPI
28    if(worldRank == 0 ){
29   #endif // is_mpi
30 <    
31 <    strcpy( outName, entry_plug->sampleName );
32 <    
33 <    outFile.open(outName, ios::out | ios::trunc );
34 <    
35 <    if( !outFile ){
35 <      
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 <    //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 57 | 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, j, which_node, done, which_atom, local_index;
152 >  int i, k;
153 >
154 > #ifdef IS_MPI
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
170 >
171    double q[4];
172    DirectionalAtom* dAtom;
76  int nAtoms = entry_plug->n_atoms;
173    Atom** atoms = entry_plug->atoms;
174 <    
174 >  double pos[3], vel[3];
175  
176   #ifndef IS_MPI
177 <    
178 <  outFile << nAtoms << "\n";
179 <    
84 <  outFile << currentTime << "\t"
85 <          << entry_plug->Hmat[0] << "\t"
86 <          << entry_plug->Hmat[1] << "\t"
87 <          << entry_plug->Hmat[2] << "\t"
177 >  
178 >  for(k = 0; k < outFile.size(); k++){
179 >    *outFile[k] << nAtoms << "\n";
180  
181 <          << entry_plug->Hmat[3] << "\t"
182 <          << entry_plug->Hmat[4] << "\t"
183 <          << entry_plug->Hmat[5] << "\t"
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[6] << "\t"
191 <          << entry_plug->Hmat[7] << "\t"
192 <          << entry_plug->Hmat[8] << "\n";
193 <    
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++ ){
98      
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 126 | Line 229 | void DumpWriter::writeDump( double currentTime ){
229      }
230      else
231        strcat( writeLine, "0.0\t0.0\t0.0\t0.0\t0.0\t0.0\t0.0\n" );
232 <      
233 <    outFile << writeLine;
232 >
233 >    for(k = 0; k < outFile.size(); k++)
234 >      *outFile[k] << writeLine;
235    }
132  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  
139  int myStatus; // 1 = wakeup & success; 0 = error; -1 = AllDone
249    int haveError;
250  
251    MPI_Status istatus;
252    int *AtomToProcMap = mpiSim->getAtomToProcMap();
253 <  
253 >
254    // write out header and node 0's coordinates
255 <  
255 >
256    if( worldRank == 0 ){
257 <    outFile << 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 >      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 << currentTime << "\t"
284 <            << entry_plug->Hmat[0] << "\t"
285 <            << entry_plug->Hmat[1] << "\t"
286 <            << entry_plug->Hmat[2] << "\t"
287 <      
155 <            << entry_plug->Hmat[3] << "\t"
156 <            << entry_plug->Hmat[4] << "\t"
157 <            << entry_plug->Hmat[5] << "\t"
158 <      
159 <            << entry_plug->Hmat[6] << "\t"
160 <            << entry_plug->Hmat[7] << "\t"
161 <            << entry_plug->Hmat[8] << "\n";
162 <    ;
163 <    outFile.flush();
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        
292 <      which_node = AtomToProcMap[i];    
292 >      which_node = AtomToProcMap[i];
293        
294 <      if (which_node == 0 ) {
294 >      if (which_node != 0) {
295 >
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 >        }
304 >
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 <        haveError = 0;
313 <        which_atom = i;
314 <        local_index=-1;        
315 <        for (j=0; (j<mpiSim->getMyNlocal()) && (local_index < 0); j++) {
316 <          if (atoms[j]->getGlobalIndex() == which_atom) local_index = j;
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 <        if (local_index != -1) {
327 <          //format the line
328 <          sprintf( tempBuffer,
329 <                   "%s\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t",
330 <                   atoms[local_index]->getType(),
331 <                   atoms[local_index]->getX(),
332 <                   atoms[local_index]->getY(),
333 <                   atoms[local_index]->getZ(),
334 <                   atoms[local_index]->get_vx(),
335 <                   atoms[local_index]->get_vy(),
336 <                   atoms[local_index]->get_vz()); // check here.
337 <          strcpy( writeLine, tempBuffer );
338 <          
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 );
194            
195            sprintf( tempBuffer,
196                     "%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\n",
197                     q[0],
198                     q[1],
199                     q[2],
200                     q[3],
201                     dAtom->getJx(),
202                     dAtom->getJy(),
203                     dAtom->getJz());
204            strcat( writeLine, tempBuffer );
205            
206          }
207          else
208            strcat( writeLine, "0.0\t0.0\t0.0\t0.0\t0.0\t0.0\t0.0\n" );      
209        }
210        else {
211          sprintf(painCave.errMsg,
212                  "Atom %d not found on processor %d\n",
213                  i, worldRank );
214          haveError= 1;
215          simError();
216        }
217        
218        if(haveError) nodeZeroError();
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 <      else {
387 <        myStatus = 1;
388 <        MPI_Send(&myStatus, 1, MPI_INT, which_node,
224 <                 TAKE_THIS_TAG_INT, MPI_COMM_WORLD);
225 <        MPI_Send(&i, 1, MPI_INT, which_node, TAKE_THIS_TAG_INT,
226 <                 MPI_COMM_WORLD);
227 <        MPI_Recv(writeLine, BUFFERSIZE, MPI_CHAR, which_node,
228 <                 TAKE_THIS_TAG_CHAR, MPI_COMM_WORLD, &istatus);
229 <        MPI_Recv(&myStatus, 1, MPI_INT, which_node,
230 <                 TAKE_THIS_TAG_INT, MPI_COMM_WORLD, &istatus);
386 >      // If we've survived to here, format the line:
387 >      
388 >      if (!isDirectional) {
389          
390 <        if(!myStatus) nodeZeroError();
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 <      outFile << writeLine;
424 <      outFile.flush();
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);
245 <    }
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 {
248    
249    done = 0;
250    while (!done) {
251      
252      MPI_Recv(&myStatus, 1, MPI_INT, 0,
253               TAKE_THIS_TAG_INT, MPI_COMM_WORLD, &istatus);
438  
439 <      if(!myStatus) anonymousNodeDie();
256 <      
257 <      if(myStatus < 0) break;
439 >    // worldRank != 0, so I'm a remote node.  
440  
441 <      MPI_Recv(&which_atom, 1, MPI_INT, 0,
442 <               TAKE_THIS_TAG_INT, MPI_COMM_WORLD, &istatus);
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 <      myStatus = 1;
449 <      local_index=-1;        
450 <      for (j=0; (j<mpiSim->getMyNlocal()) && (local_index < 0); j++) {
265 <        if (atoms[j]->getGlobalIndex() == which_atom) local_index = j;
266 <      }
267 <      if (local_index != -1) {
268 <        //format the line
269 <        sprintf( tempBuffer,
270 <                 "%s\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t",
271 <                 atoms[local_index]->getType(),
272 <                 atoms[local_index]->getX(),
273 <                 atoms[local_index]->getY(),
274 <                 atoms[local_index]->getZ(),
275 <                 atoms[local_index]->get_vx(),
276 <                 atoms[local_index]->get_vy(),
277 <                 atoms[local_index]->get_vz()); // check here.
278 <        strcpy( writeLine, tempBuffer );
279 <        
280 <        if( atoms[local_index]->isDirectional() ){
281 <          
282 <          dAtom = (DirectionalAtom *)atoms[local_index];
283 <          dAtom->getQ( q );
284 <          
285 <          sprintf( tempBuffer,
286 <                   "%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\n",
287 <                   q[0],
288 <                   q[1],
289 <                   q[2],
290 <                   q[3],
291 <                   dAtom->getJx(),
292 <                   dAtom->getJy(),
293 <                   dAtom->getJz());
294 <          strcat( writeLine, tempBuffer );
295 <        }
296 <        else{
297 <          strcat( writeLine, "0.0\t0.0\t0.0\t0.0\t0.0\t0.0\t0.0\n" );
298 <        }
299 <      }
300 <      else {
301 <        sprintf(painCave.errMsg,
302 <                "Atom %d not found on processor %d\n",
303 <                which_atom, worldRank );
304 <        myStatus = 0;
305 <        simError();
448 >      // Am I the node which has this atom?
449 >      
450 >      if (AtomToProcMap[i] == worldRank) {
451  
452 <        strcpy( writeLine, "Hello, I'm an error.\n");
308 <      }
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,
313 <                TAKE_THIS_TAG_INT, MPI_COMM_WORLD);
314 <    }
315 <  }  
316 <  outFile.flush();
317 <  sprintf( checkPointMsg,
318 <           "Sucessfully took a dump.\n");
319 <  MPIcheckPoint();
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 < // last  thing last, enable  fatalities.
459 <  painCave.isEventLoop = 0;
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 < #endif // is_mpi
469 < }
468 >                atoms[local_index]->getPos(pos);
469 >                atoms[local_index]->getVel(vel);
470  
471 < void DumpWriter::writeFinal(double finalTime){
471 >          atomData6[0] = pos[0];
472 >          atomData6[1] = pos[1];
473 >          atomData6[2] = pos[2];
474  
475 <  char finalName[500];
476 <  ofstream finalOut;
475 >          atomData6[3] = vel[0];
476 >          atomData6[4] = vel[1];
477 >          atomData6[5] = vel[2];
478 >          
479 >          isDirectional = 0;
480  
481 <  const int BUFFERSIZE = 2000;
333 <  char tempBuffer[BUFFERSIZE];
334 <  char writeLine[BUFFERSIZE];  
481 >          if( atoms[local_index]->isDirectional() ){
482  
483 <  double q[4];
484 <  DirectionalAtom* dAtom;
485 <  int nAtoms = entry_plug->n_atoms;
486 <  Atom** atoms = entry_plug->atoms;
487 <  int i, j, which_node, done, game_over, which_atom, local_index;
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 <  
497 < #ifdef IS_MPI
498 <  if(worldRank == 0 ){
499 < #endif // is_mpi
346 <    
347 <    strcpy( finalName, entry_plug->finalName );
348 <    
349 <    finalOut.open( finalName, ios::out | ios::trunc );
350 <    if( !finalOut ){
351 <      sprintf( painCave.errMsg,
352 <               "Could not open \"%s\" for final dump output.\n",
353 <               finalName );
354 <      painCave.isFatal = 1;
355 <      simError();
356 <    }
357 <    
358 <    // finalOut.setf( ios::scientific );
359 <    
360 < #ifdef IS_MPI
361 <  }
362 <  
363 <  sprintf(checkPointMsg,"Opened file for final configuration\n");
364 <  MPIcheckPoint();  
365 <  
366 < #endif //is_mpi
496 >            atomData13[10] = dAtom->getJx();
497 >            atomData13[11] = dAtom->getJy();
498 >            atomData13[12] = dAtom->getJz();
499 >          }
500  
501 <  
502 < #ifndef IS_MPI
503 <    
504 <  finalOut << nAtoms << "\n";
505 <    
506 <  finalOut << finalTime << "\t"
507 <           << entry_plug->Hmat[0] << "\t"
375 <           << entry_plug->Hmat[1] << "\t"
376 <           << entry_plug->Hmat[2] << "\t"
377 <    
378 <           << entry_plug->Hmat[3] << "\t"
379 <           << entry_plug->Hmat[4] << "\t"
380 <           << entry_plug->Hmat[5] << "\t"
381 <    
382 <           << entry_plug->Hmat[6] << "\t"
383 <           << entry_plug->Hmat[7] << "\t"
384 <           << entry_plug->Hmat[8] << "\n";
385 <  
386 <  for( i=0; i<nAtoms; i++ ){
387 <      
388 <    sprintf( tempBuffer,
389 <             "%s\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t",
390 <             atoms[i]->getType(),
391 <             atoms[i]->getX(),
392 <             atoms[i]->getY(),
393 <             atoms[i]->getZ(),
394 <             atoms[i]->get_vx(),
395 <             atoms[i]->get_vy(),
396 <             atoms[i]->get_vz());
397 <    strcpy( writeLine, tempBuffer );
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 <    if( atoms[i]->isDirectional() ){
400 <        
401 <      dAtom = (DirectionalAtom *)atoms[i];
402 <      dAtom->getQ( q );
403 <        
404 <      sprintf( tempBuffer,
405 <               "%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\n",
406 <               q[0],
407 <               q[1],
408 <               q[2],
409 <               q[3],
410 <               dAtom->getJx(),
411 <               dAtom->getJy(),
412 <               dAtom->getJz());
413 <      strcat( writeLine, tempBuffer );
414 <    }
415 <    else
416 <      strcat( writeLine, "0.0\t0.0\t0.0\t0.0\t0.0\t0.0\t0.0\n" );
417 <      
418 <    finalOut << writeLine;
419 <  }
420 <  finalOut.flush();
421 <  finalOut.close();
509 >        strncpy(MPIatomTypeString, atomTypeString, MINIBUFFERSIZE);
510  
511 < #else // is_mpi
512 <  
425 <  // first thing first, suspend fatalities.
426 <  painCave.isEventLoop = 1;
511 >        // null terminate the string before sending (just in case):
512 >        MPIatomTypeString[MINIBUFFERSIZE-1] = '\0';
513  
514 <  int myStatus; // 1 = wakeup & success; 0 = error; -1 = AllDone
515 <  int haveError;
514 >        MPI_Send(MPIatomTypeString, MINIBUFFERSIZE, MPI_CHAR, 0,
515 >                             myPotato, MPI_COMM_WORLD);
516 >        
517 >        myPotato++;
518  
519 <  MPI_Status istatus;
520 <  int *AtomToProcMap = mpiSim->getAtomToProcMap();
519 >        MPI_Send(&isDirectional, 1, MPI_INT, 0,
520 >                             myPotato, MPI_COMM_WORLD);
521 >        
522 >        myPotato++;
523 >        
524 >        if (isDirectional) {
525  
526 <  // write out header and node 0's coordinates
527 <  
528 <  haveError = 0;
529 <  if( worldRank == 0 ){
438 <    finalOut << mpiSim->getTotAtoms() << "\n";
439 <    
440 <    finalOut << finalTime << "\t"
441 <             << entry_plug->Hmat[0] << "\t"
442 <             << entry_plug->Hmat[1] << "\t"
443 <             << entry_plug->Hmat[2] << "\t"
444 <      
445 <             << entry_plug->Hmat[3] << "\t"
446 <             << entry_plug->Hmat[4] << "\t"
447 <             << entry_plug->Hmat[5] << "\t"
448 <      
449 <             << entry_plug->Hmat[6] << "\t"
450 <             << entry_plug->Hmat[7] << "\t"
451 <             << entry_plug->Hmat[8] << "\n";
452 <    
453 <    for (i = 0 ; i < mpiSim->getTotAtoms(); i++ ) {
454 <      // Get the Node number which has this molecule:
455 <      
456 <      which_node = AtomToProcMap[i];    
457 <      
458 <      if (which_node == mpiSim->getMyNode()) {
526 >          MPI_Send(atomData13, 13, MPI_DOUBLE, 0,
527 >                   myPotato, MPI_COMM_WORLD);
528 >          
529 >        } else {
530  
531 <        which_atom = i;
532 <        local_index=-1;        
462 <        for (j=0; (j<mpiSim->getMyNlocal()) && (local_index < 0); j++) {
463 <          if (atoms[j]->getGlobalIndex() == which_atom) local_index = j;
531 >          MPI_Send(atomData6, 6, MPI_DOUBLE, 0,
532 >                   myPotato, MPI_COMM_WORLD);
533          }
465        if (local_index != -1) {        
466          sprintf( tempBuffer,
467                   "%s\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t",
468                   atoms[local_index]->getType(),
469                   atoms[local_index]->getX(),
470                   atoms[local_index]->getY(),
471                   atoms[local_index]->getZ(),
472                   atoms[local_index]->get_vx(),
473                   atoms[local_index]->get_vy(),
474                   atoms[local_index]->get_vz());
475          strcpy( writeLine, tempBuffer );
476          
477          if( atoms[local_index]->isDirectional() ){
478            
479            dAtom = (DirectionalAtom *)atoms[local_index];
480            dAtom->getQ( q );
481            
482            sprintf( tempBuffer,
483                     "%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\n",
484                     q[0],
485                     q[1],
486                     q[2],
487                     q[3],
488                     dAtom->getJx(),
489                     dAtom->getJy(),
490                     dAtom->getJz());
491            strcat( writeLine, tempBuffer );
492          }
493          else
494            strcat( writeLine, "0.0\t0.0\t0.0\t0.0\t0.0\t0.0\t0.0\n" );      
495        }
496        else {
497          sprintf(painCave.errMsg,
498                  "Atom %d not found on processor %d\n",
499                  i, worldRank );
500          haveError= 1;
501          simError();
502        }
534  
535 <        if(haveError) nodeZeroError();
536 <    
506 <      }
507 <      else {
508 <        
509 <        myStatus = 1;
510 <        MPI_Send(&myStatus, 1, MPI_INT, which_node,
511 <                 TAKE_THIS_TAG_INT, MPI_COMM_WORLD);
512 <        MPI_Send(&i, 1, MPI_INT, which_node, TAKE_THIS_TAG_INT,
513 <                 MPI_COMM_WORLD);
514 <        MPI_Recv(writeLine, BUFFERSIZE, MPI_CHAR, which_node,
515 <                 TAKE_THIS_TAG_CHAR, MPI_COMM_WORLD, &istatus);
516 <        MPI_Recv(&myStatus, 1, MPI_INT, which_node,
517 <                 TAKE_THIS_TAG_INT, MPI_COMM_WORLD, &istatus);
518 <        
519 <        if(!myStatus) nodeZeroError();
535 >        myPotato++;  
536 >        currentIndex++;    
537        }
521      
522      finalOut << writeLine;
538      }
524    
525    // kill everyone off:
526    myStatus = -1;
527    for (j = 0; j < mpiSim->getNumberProcessors(); j++) {      
528      MPI_Send(&myStatus, 1, MPI_INT, j,
529               TAKE_THIS_TAG_INT, MPI_COMM_WORLD);
530    }
539  
540 <  } else {
540 >    sprintf( checkPointMsg,
541 >             "Sucessfully took a dump.\n");
542 >    MPIcheckPoint();        
543      
544 <    done = 0;
535 <    while (!done) {
536 <
537 <      MPI_Recv(&myStatus, 1, MPI_INT, 0,
538 <               TAKE_THIS_TAG_INT, MPI_COMM_WORLD, &istatus);
539 <      
540 <      if(!myStatus) anonymousNodeDie();
541 <      
542 <      if(myStatus < 0) break;
543 <      
544 <      MPI_Recv(&which_atom, 1, MPI_INT, 0,
545 <               TAKE_THIS_TAG_INT, MPI_COMM_WORLD, &istatus);
546 <      
547 <      myStatus = 1;
548 <      local_index=-1;        
549 <      for (j=0; j < mpiSim->getMyNlocal(); j++) {
550 <        if (atoms[j]->getGlobalIndex() == which_atom) local_index = j;
551 <      }
552 <      if (local_index != -1) {
553 <
554 <        //format the line
555 <        sprintf( tempBuffer,
556 <                 "%s\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t",
557 <                 atoms[local_index]->getType(),
558 <                 atoms[local_index]->getX(),
559 <                 atoms[local_index]->getY(),
560 <                 atoms[local_index]->getZ(),
561 <                 atoms[local_index]->get_vx(),
562 <                 atoms[local_index]->get_vy(),
563 <                 atoms[local_index]->get_vz()); // check here.
564 <        strcpy( writeLine, tempBuffer );
565 <        
566 <        if( atoms[local_index]->isDirectional() ){
567 <          
568 <          dAtom = (DirectionalAtom *)atoms[local_index];
569 <          dAtom->getQ( q );
570 <          
571 <          sprintf( tempBuffer,
572 <                   "%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\n",
573 <                   q[0],
574 <                   q[1],
575 <                   q[2],
576 <                   q[3],
577 <                   dAtom->getJx(),
578 <                   dAtom->getJy(),
579 <                   dAtom->getJz());
580 <          strcat( writeLine, tempBuffer );
581 <        }
582 <        else{
583 <          strcat( writeLine, "0.0\t0.0\t0.0\t0.0\t0.0\t0.0\t0.0\n" );
584 <        }
585 <      }
586 <      else {
587 <        sprintf(painCave.errMsg,
588 <                "Atom %d not found on processor %d\n",
589 <                which_atom, worldRank );
590 <        myStatus = 0;
591 <        simError();
592 <        
593 <        strcpy( writeLine, "Hello, I'm an error.\n");
594 <      }
595 <
596 <      MPI_Send(writeLine, BUFFERSIZE, MPI_CHAR, 0,
597 <               TAKE_THIS_TAG_CHAR, MPI_COMM_WORLD);
598 <      MPI_Send( &myStatus, 1, MPI_INT, 0,
599 <                TAKE_THIS_TAG_INT, MPI_COMM_WORLD);
600 <    }
601 <  }
602 <  finalOut.flush();
603 <  sprintf( checkPointMsg,
604 <           "Sucessfully took a dump.\n");
605 <  MPIcheckPoint();
544 >  }
545    
607  if( worldRank == 0 ) finalOut.close();    
546   #endif // is_mpi
547   }
548  
611
612
549   #ifdef IS_MPI
550  
551   // a couple of functions to let us escape the write loop
552  
553 < void dWrite::nodeZeroError( void ){
618 <  int j, myStatus;
619 <  
620 <  myStatus = 0;
621 <  for (j = 0; j < mpiSim->getNumberProcessors(); j++) {      
622 <    MPI_Send( &myStatus, 1, MPI_INT, j,
623 <              TAKE_THIS_TAG_INT, MPI_COMM_WORLD);
624 <  }  
625 <  
553 > void dWrite::DieDieDie( void ){
554  
555    MPI_Finalize();
556    exit (0);
629  
557   }
558  
632 void dWrite::anonymousNodeDie( void ){
633
634  MPI_Finalize();
635  exit (0);
636 }
637
559   #endif //is_mpi

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines