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

Comparing:
branches/mmeineke/mdtools/md_code/DumpWriter.cpp (file contents), Revision 10 by mmeineke, Tue Jul 9 18:40:59 2002 UTC vs.
trunk/mdtools/md_code/DumpWriter.cpp (file contents), Revision 214 by mmeineke, Mon Dec 16 21:42:14 2002 UTC

# Line 1 | Line 1
1   #include <cstring>
2   #include <iostream>
3   #include <fstream>
4 + #include <mpi.h>
5  
6   #include "ReadWrite.hpp"
7 + #include "simError.h"
8  
9  
8
10   DumpWriter::DumpWriter( SimInfo* the_entry_plug ){
11  
12 <  entry_plug = the_entry_plug;
13 <  
14 <  strcpy( outName, entry_plug->sampleName );
14 <  outFile.open(outName, ios::out | ios::trunc );
15 <  
16 <  if( !outFile ){
12 > #ifdef IS_MPI
13 >  if(worldRank == 0 ){
14 > #endif // is_mpi
15      
16 <    cerr << "Could not open \"" << outName << "\" for dump output.\n";
17 <    exit(8);
18 <  }
16 >    entry_plug = the_entry_plug;
17 >    
18 >    strcpy( outName, entry_plug->sampleName );
19 >    
20 >    std::cerr << "Opening " << outName << " for dumping.\n";
21 >
22 >    outFile.open(outName, ios::out | ios::trunc );
23 >    
24 >    if( !outFile ){
25 >      
26 >      sprintf( painCave.errMsg,
27 >               "Could not open \"%s\" for dump output.\n",
28 >               outName);
29 >      painCave.isFatal = 1;
30 >      simError();
31 >    }
32    
33 <  //outFile.setf( ios::scientific );
33 >    //outFile.setf( ios::scientific );
34 >
35 > #ifdef IS_MPI
36 >  }
37 > #endif // is_mpi
38   }
39  
40   DumpWriter::~DumpWriter( ){
41  
42 <  outFile.close();
42 > #ifdef IS_MPI
43 >  if(worldRank == 0 ){
44 > #endif // is_mpi
45 >
46 >    outFile.close();
47 >
48 > #ifdef IS_MPI
49 >  }
50 > #endif // is_mpi
51   }
52  
53   void DumpWriter::writeDump( double currentTime ){
54 +  
55 +  const int BUFFERSIZE = 2000;
56 +  char tempBuffer[500];
57 +  char writeLine[BUFFERSIZE];
58  
59    int i;
60    double q[4];
61    DirectionalAtom* dAtom;
62    int nAtoms = entry_plug->n_atoms;
63    Atom** atoms = entry_plug->atoms;
64 +    
65  
66 <
66 > #ifndef IS_MPI
67 >    
68    outFile << nAtoms << "\n";
69 <  
69 >    
70    outFile << currentTime << "\t"
71            << entry_plug->box_x << "\t"
72            << entry_plug->box_y << "\t"
73            << entry_plug->box_z << "\n";
74 <
74 >    
75    for( i=0; i<nAtoms; i++ ){
76 +      
77 +    sprintf( tempBuffer,
78 +             "%s\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t",
79 +             atoms[i]->getType(),
80 +             atoms[i]->getX(),
81 +             atoms[i]->getY(),
82 +             atoms[i]->getZ(),
83 +             atoms[i]->get_vx(),
84 +             atoms[i]->get_vy(),
85 +             atoms[i]->get_vz());
86 +    strcpy( writeLine, tempBuffer );
87  
48    outFile
49      << atoms[i]->getType() << "\t"
50      << atoms[i]->getX() << "\t"
51      << atoms[i]->getY() << "\t"
52      << atoms[i]->getZ() << "\t"
53      << atoms[i]->get_vx() << "\t"
54      << atoms[i]->get_vy() << "\t"
55      << atoms[i]->get_vz() << "\t";
56
88      if( atoms[i]->isDirectional() ){
89 <      
89 >        
90        dAtom = (DirectionalAtom *)atoms[i];
91        dAtom->getQ( q );
92 +        
93 +      sprintf( tempBuffer,
94 +               "%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\n",
95 +               q[0],
96 +               q[1],
97 +               q[2],
98 +               q[3],
99 +               dAtom->getJx(),
100 +               dAtom->getJy(),
101 +               dAtom->getJz());
102 +      strcat( writeLine, tempBuffer );
103 +    }
104 +    else
105 +      strcat( writeLine, "0.0\t0.0\t0.0\t0.0\t0.0\t0.0\t0.0\n" );
106        
107 <      outFile
108 <        << q[0] << "\t"
109 <        << q[1] << "\t"
110 <        << q[2] << "\t"
111 <        << q[3] << "\t"
112 <        << dAtom->getJx() << "\t"
113 <        << dAtom->getJy() << "\t"
114 <        << dAtom->getJz() << "\n";
107 >    outFile << writeLine;
108 >  }
109 >  outFile.flush();
110 >
111 > #else // is_mpi
112 >
113 >  int masterIndex;
114 >  int nodeAtomsStart;
115 >  int nodeAtomsEnd;
116 >  int mpiErr;
117 >  int sendError;
118 >  int procIndex;
119 >    
120 >  MPI_Status istatus[MPI_STATUS_SIZE];
121 >
122 >    
123 >  // write out header and node 0's coordinates
124 >
125 >  if( worldRank == 0 ){
126 >    outFile << entry_plug->mpiSim->getTotAtoms() << "\n";
127 >      
128 >    outFile << currentTime << "\t"
129 >            << entry_plug->box_x << "\t"
130 >            << entry_plug->box_y << "\t"
131 >            << entry_plug->box_z << "\n";
132 >
133 >    masterIndex = 0;
134 >    for( i=0; i<nAtoms; i++ ){
135 >      
136 >      sprintf( tempBuffer,
137 >               "%s\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t",
138 >               atoms[i]->getType(),
139 >               atoms[i]->getX(),
140 >               atoms[i]->getY(),
141 >               atoms[i]->getZ(),
142 >               atoms[i]->get_vx(),
143 >               atoms[i]->get_vy(),
144 >               atoms[i]->get_vz());
145 >      strcpy( writeLine, tempBuffer );
146 >        
147 >      if( atoms[i]->isDirectional() ){
148 >          
149 >        dAtom = (DirectionalAtom *)atoms[i];
150 >        dAtom->getQ( q );
151 >          
152 >        sprintf( tempBuffer,
153 >                 "%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\n",
154 >                 q[0],
155 >                 q[1],
156 >                 q[2],
157 >                 q[3],
158 >                 dAtom->getJx(),
159 >                 dAtom->getJy(),
160 >                 dAtom->getJz());
161 >        strcat( writeLine, tempBuffer );
162 >      }
163 >      else
164 >        strcat( writeLine, "0.0\t0.0\t0.0\t0.0\t0.0\t0.0\t0.0\n" );
165 >        
166 >      outfile << writeLine;
167 >      masterIndex++;
168      }
169 <    else{
170 <      outFile
171 <        << 0.0 << "\t"
172 <        << 0.0 << "\t"
173 <        << 0.0 << "\t"
174 <        << 0.0 << "\t"
175 <        << 0.0 << "\t"
176 <        << 0.0 << "\t"
177 <        << 0.0 << "\n";
169 >    outFile.flush();
170 >  }
171 >    
172 >  for (procIndex = 1; procIndex < entry_plug->mpiSim->getNumberProcessors();
173 >       procIndex++){
174 >
175 >    if( worldRank == 0 ){
176 >        
177 >      mpiErr = MPI_Recv(&nodeAtomsStart,1,MPI_INT,procIndex,
178 >                        MPI_ANY_TAG,MPI_COMM_WORLD,istatus);
179 >
180 >      mpiErr = MPI_Recv(&nodeAtomsEnd,1,MPI_INT,procIndex,
181 >                        MPI_ANY_TAG,MPI_COMM_WORLD, istatus);
182 >        
183 >      // Make sure where node 0 is writing to, matches where the
184 >      // receiving node expects it to be.
185 >        
186 >      if (masterIndex != nodeAtomsStart){
187 >        sendError = 1;
188 >        mpiErr = MPI_Send(&sendError,1,MPI_INT,procIndex,MPI_ANY_TAG,
189 >                          MPI_COMM_WORLD);
190 >        sprintf(painCave.errMsg,
191 >                "DumpWriter error: atoms start index (%d) for "
192 >                "node %d not equal to master index (%d)",
193 >                nodeAtomsStart,procIndex,masterIndex );
194 >        painCave.isFatal = 1;
195 >        simError();
196 >      }
197 >        
198 >      sendError = 0;
199 >      mpiErr = MPI_Send(&sendError,1,MPI_INT,procIndex,MPI_ANY_TAG,
200 >                        MPI_COMM_WORLD);
201 >
202 >      // recieve the nodes writeLines
203 >
204 >      for ( i = nodeAtomStart; i <= nodeAtomEnd, i++){
205 >          
206 >        mpiErr = MPI_Recv(&read_buffer,BUFFERSIZE,MPI_CHAR,procIndex,
207 >                          MPI_ANY_TAG,MPI_COMM_WORLD,istatus );
208 >
209 >        outFile << writeLine;
210 >        masterIndex++;
211 >      }
212      }
213 +
214 +    else if( worldRank == procIndex ){
215 +
216 +      nodeAtomStart = entry_plug->mpiSim->getMyAtomStart();
217 +      nodeAtomEnd = entry_plug->mpiSim->getMyAtomEnd();
218 +        
219 +      mpiErr = MPI_Send(&nodeAtomsStart,1,MPI_INT,0,MPI_ANY_TAG,
220 +                        MPI_COMM_WORLD);
221 +      mpiErr = MPI_Send(&nodeAtomsEnd,1,MPI_INT,0,MPI_ANY_TAG,
222 +                        MPI_COMM_WORLD);
223 +        
224 +      mpiErr = MPI_Recv(&sendError,1,MPI_INT,0,MPI_ANY_TAG,
225 +                        MPI_COMM_WORLD, istatus);
226 +      if (sendError) mpiCheckpoint();
227 +
228 +      // send current node's configuration line by line.
229 +
230 +      for( i=0; i<nAtoms; i++ ){
231 +          
232 +        sprintf( tempBuffer,
233 +                 "%s\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t",
234 +                 atoms[i]->getType(),
235 +                 atoms[i]->getX(),
236 +                 atoms[i]->getY(),
237 +                 atoms[i]->getZ(),
238 +                 atoms[i]->get_vx(),
239 +                 atoms[i]->get_vy(),
240 +                 atoms[i]->get_vz());
241 +        strcpy( writeLine, tempBuffer );
242 +          
243 +        if( atoms[i]->isDirectional() ){
244 +            
245 +          dAtom = (DirectionalAtom *)atoms[i];
246 +          dAtom->getQ( q );
247 +            
248 +          sprintf( tempBuffer,
249 +                   "%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\n",
250 +                   q[0],
251 +                   q[1],
252 +                   q[2],
253 +                   q[3],
254 +                   dAtom->getJx(),
255 +                   dAtom->getJy(),
256 +                   dAtom->getJz());
257 +          strcat( writeLine, tempBuffer );
258 +        }
259 +        else
260 +          strcat( writeLine, "0.0\t0.0\t0.0\t0.0\t0.0\t0.0\t0.0\n" );
261 +          
262 +        mpiErr = MPI_Send(writeLine,BUFFERSIZE,MPI_CHAR,0,MPI_ANY_TAG,
263 +                          MPI_COMM_WORLD);
264 +      }
265 +    }
266 +      
267 +    sprintf(checkPointMsg,"Node %d sent dump configuration.",
268 +            procIndex);
269 +    mpiCheckPoint();
270    }
271 <  outFile.flush();
271 >    
272 > #endif // is_mpi
273   }
274 +
275  
276  
277   void DumpWriter::writeFinal(){
278  
88  char finalName[500];
89  strcpy( finalName, entry_plug->finalName );
279  
280 <  ofstream finalOut( finalName );
281 <  if( !finalOut ){
282 <    cerr << "Could not open \"" << finalName << "\" for final dump output.\n";
94 <    exit(8);
95 <  }
280 >  const int BUFFERSIZE = 2000;
281 >  char tempBuffer[500];
282 >  char writeLine[BUFFERSIZE];
283    
284 < // finalOut.setf( ios::scientific );
98 <    
284 >  char finalName[500];
285  
286    int i;
287    double q[4];
288    DirectionalAtom* dAtom;
289    int nAtoms = entry_plug->n_atoms;
290    Atom** atoms = entry_plug->atoms;
291 +  
292 +  ofstream finalOut;
293 +  
294 + #ifdef IS_MPI
295 +  if(worldRank == 0 ){
296 + #endif // is_mpi
297 +    
298 +    strcpy( finalName, entry_plug->finalName );
299 +    
300 +    finalOut.open( finalName, ios::out | ios::trunc );
301 +    if( !finalOut ){
302 +      sprintf( painCave.errMsg,
303 +               "Could not open \"%s\" for final dump output.\n",
304 +               finalName );
305 +      painCave.isFatal = 1;
306 +      simError();
307 +    }
308 +    
309 +    // finalOut.setf( ios::scientific );
310 +    
311 + #ifdef IS_MPI
312 +  }
313 +  
314 +  sprintf(checkPointMsg,"Opened file for final configuration\n",procIndex);
315 +  mpiCheckPoint();  
316 +  
317 + #endif //is_mpi
318  
319 +    
320  
321 + #ifndef IS_MPI
322 +    
323    finalOut << nAtoms << "\n";
324 <  
325 <  finalOut << 0.0 << "\t"
324 >    
325 >  finalOut << currentTime << "\t"
326            << entry_plug->box_x << "\t"
327            << entry_plug->box_y << "\t"
328            << entry_plug->box_z << "\n";
329 <
329 >    
330    for( i=0; i<nAtoms; i++ ){
331 +      
332 +    sprintf( tempBuffer,
333 +             "%s\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t",
334 +             atoms[i]->getType(),
335 +             atoms[i]->getX(),
336 +             atoms[i]->getY(),
337 +             atoms[i]->getZ(),
338 +             atoms[i]->get_vx(),
339 +             atoms[i]->get_vy(),
340 +             atoms[i]->get_vz());
341 +    strcpy( writeLine, tempBuffer );
342  
116    finalOut
117      << atoms[i]->getType() << "\t"
118      << atoms[i]->getX() << "\t"
119      << atoms[i]->getY() << "\t"
120      << atoms[i]->getZ() << "\t"
121      << atoms[i]->get_vx() << "\t"
122      << atoms[i]->get_vy() << "\t"
123      << atoms[i]->get_vz() << "\t";
124
343      if( atoms[i]->isDirectional() ){
344 <      
344 >        
345        dAtom = (DirectionalAtom *)atoms[i];
346        dAtom->getQ( q );
347 +        
348 +      sprintf( tempBuffer,
349 +               "%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\n",
350 +               q[0],
351 +               q[1],
352 +               q[2],
353 +               q[3],
354 +               dAtom->getJx(),
355 +               dAtom->getJy(),
356 +               dAtom->getJz());
357 +      strcat( writeLine, tempBuffer );
358 +    }
359 +    else
360 +      strcat( writeLine, "0.0\t0.0\t0.0\t0.0\t0.0\t0.0\t0.0\n" );
361        
362 <      finalOut
363 <        << q[0] << "\t"
364 <        << q[1] << "\t"
365 <        << q[2] << "\t"
366 <        << q[3] << "\t"
367 <        << dAtom->getJx() << "\t"
368 <        << dAtom->getJy() << "\t"
369 <        << dAtom->getJz() << "\n";
362 >    finalOut << writeLine;
363 >  }
364 >  finalOut.flush();
365 >
366 > #else // is_mpi
367 >
368 >  int masterIndex;
369 >  int nodeAtomsStart;
370 >  int nodeAtomsEnd;
371 >  int mpiErr;
372 >  int sendError;
373 >  int procIndex;
374 >    
375 >  MPI_Status istatus[MPI_STATUS_SIZE];
376 >
377 >    
378 >  // write out header and node 0's coordinates
379 >
380 >  if( worldRank == 0 ){
381 >    finalOut << entry_plug->mpiSim->getTotAtoms() << "\n";
382 >      
383 >    finalOut << currentTime << "\t"
384 >            << entry_plug->box_x << "\t"
385 >            << entry_plug->box_y << "\t"
386 >            << entry_plug->box_z << "\n";
387 >
388 >    masterIndex = 0;
389 >    for( i=0; i<nAtoms; i++ ){
390 >      
391 >      sprintf( tempBuffer,
392 >               "%s\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t",
393 >               atoms[i]->getType(),
394 >               atoms[i]->getX(),
395 >               atoms[i]->getY(),
396 >               atoms[i]->getZ(),
397 >               atoms[i]->get_vx(),
398 >               atoms[i]->get_vy(),
399 >               atoms[i]->get_vz());
400 >      strcpy( writeLine, tempBuffer );
401 >        
402 >      if( atoms[i]->isDirectional() ){
403 >          
404 >        dAtom = (DirectionalAtom *)atoms[i];
405 >        dAtom->getQ( q );
406 >          
407 >        sprintf( tempBuffer,
408 >                 "%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\n",
409 >                 q[0],
410 >                 q[1],
411 >                 q[2],
412 >                 q[3],
413 >                 dAtom->getJx(),
414 >                 dAtom->getJy(),
415 >                 dAtom->getJz());
416 >        strcat( writeLine, tempBuffer );
417 >      }
418 >      else
419 >        strcat( writeLine, "0.0\t0.0\t0.0\t0.0\t0.0\t0.0\t0.0\n" );
420 >        
421 >      outfile << writeLine;
422 >      masterIndex++;
423      }
424 <    else{
425 <      finalOut
426 <        << 0.0 << "\t"
427 <        << 0.0 << "\t"
428 <        << 0.0 << "\t"
429 <        << 0.0 << "\t"
430 <        << 0.0 << "\t"
431 <        << 0.0 << "\t"
432 <        << 0.0 << "\n";
424 >    finalOut.flush();
425 >  }
426 >    
427 >  for (procIndex = 1; procIndex < entry_plug->mpiSim->getNumberProcessors();
428 >       procIndex++){
429 >
430 >    if( worldRank == 0 ){
431 >        
432 >      mpiErr = MPI_Recv(&nodeAtomsStart,1,MPI_INT,procIndex,
433 >                        MPI_ANY_TAG,MPI_COMM_WORLD,istatus);
434 >
435 >      mpiErr = MPI_Recv(&nodeAtomsEnd,1,MPI_INT,procIndex,
436 >                        MPI_ANY_TAG,MPI_COMM_WORLD, istatus);
437 >        
438 >      // Make sure where node 0 is writing to, matches where the
439 >      // receiving node expects it to be.
440 >        
441 >      if (masterIndex != nodeAtomsStart){
442 >        sendError = 1;
443 >        mpiErr = MPI_Send(&sendError,1,MPI_INT,procIndex,MPI_ANY_TAG,
444 >                          MPI_COMM_WORLD);
445 >        sprintf(painCave.errMsg,
446 >                "DumpWriter error: atoms start index (%d) for "
447 >                "node %d not equal to master index (%d)",
448 >                nodeAtomsStart,procIndex,masterIndex );
449 >        painCave.isFatal = 1;
450 >        simError();
451 >      }
452 >        
453 >      sendError = 0;
454 >      mpiErr = MPI_Send(&sendError,1,MPI_INT,procIndex,MPI_ANY_TAG,
455 >                        MPI_COMM_WORLD);
456 >
457 >      // recieve the nodes writeLines
458 >
459 >      for ( i = nodeAtomStart; i <= nodeAtomEnd, i++){
460 >          
461 >        mpiErr = MPI_Recv(&read_buffer,BUFFERSIZE,MPI_CHAR,procIndex,
462 >                          MPI_ANY_TAG,MPI_COMM_WORLD,istatus );
463 >
464 >        finalOut << writeLine;
465 >        masterIndex++;
466 >      }
467 >
468 >      finalOut.flush();
469      }
470 +
471 +    else if( worldRank == procIndex ){
472 +
473 +      nodeAtomStart = entry_plug->mpiSim->getMyAtomStart();
474 +      nodeAtomEnd = entry_plug->mpiSim->getMyAtomEnd();
475 +        
476 +      mpiErr = MPI_Send(&nodeAtomsStart,1,MPI_INT,0,MPI_ANY_TAG,
477 +                        MPI_COMM_WORLD);
478 +      mpiErr = MPI_Send(&nodeAtomsEnd,1,MPI_INT,0,MPI_ANY_TAG,
479 +                        MPI_COMM_WORLD);
480 +        
481 +      mpiErr = MPI_Recv(&sendError,1,MPI_INT,0,MPI_ANY_TAG,
482 +                        MPI_COMM_WORLD, istatus);
483 +      if (sendError) mpiCheckpoint();
484 +
485 +      // send current node's configuration line by line.
486 +
487 +      for( i=0; i<nAtoms; i++ ){
488 +          
489 +        sprintf( tempBuffer,
490 +                 "%s\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t",
491 +                 atoms[i]->getType(),
492 +                 atoms[i]->getX(),
493 +                 atoms[i]->getY(),
494 +                 atoms[i]->getZ(),
495 +                 atoms[i]->get_vx(),
496 +                 atoms[i]->get_vy(),
497 +                 atoms[i]->get_vz());
498 +        strcpy( writeLine, tempBuffer );
499 +          
500 +        if( atoms[i]->isDirectional() ){
501 +            
502 +          dAtom = (DirectionalAtom *)atoms[i];
503 +          dAtom->getQ( q );
504 +            
505 +          sprintf( tempBuffer,
506 +                   "%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\n",
507 +                   q[0],
508 +                   q[1],
509 +                   q[2],
510 +                   q[3],
511 +                   dAtom->getJx(),
512 +                   dAtom->getJy(),
513 +                   dAtom->getJz());
514 +          strcat( writeLine, tempBuffer );
515 +        }
516 +        else
517 +          strcat( writeLine, "0.0\t0.0\t0.0\t0.0\t0.0\t0.0\t0.0\n" );
518 +          
519 +        mpiErr = MPI_Send(writeLine,BUFFERSIZE,MPI_CHAR,0,MPI_ANY_TAG,
520 +                          MPI_COMM_WORLD);
521 +      }
522 +    }
523 +      
524 +    sprintf(checkPointMsg,"Node %d sent dump configuration.",
525 +            procIndex);
526 +    mpiCheckPoint();
527    }
528 <  finalOut.close();
528 >
529 >  if( worldRank == 0 ) finalOut.close();
530 >
531 >    
532 > #endif // is_mpi
533   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines