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

Comparing trunk/mdtools/md_code/DumpWriter.cpp (file contents):
Revision 189 by mmeineke, Tue Nov 26 21:04:43 2002 UTC vs.
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"
# Line 50 | Line 51 | void DumpWriter::writeDump( double currentTime ){
51   }
52  
53   void DumpWriter::writeDump( double currentTime ){
54 +  
55 +  const int BUFFERSIZE = 2000;
56 +  char tempBuffer[500];
57 +  char writeLine[BUFFERSIZE];
58  
59 < #ifdef IS_MPI
60 <  if(worldRank == 0 ){
61 < #endif // is_mpi
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 <
59 <    int i;
60 <    double q[4];
61 <    DirectionalAtom* dAtom;
62 <    int nAtoms = entry_plug->n_atoms;
63 <    Atom** atoms = entry_plug->atoms;
66 > #ifndef IS_MPI
67      
68 +  outFile << nAtoms << "\n";
69      
70 <    outFile << nAtoms << "\n";
70 >  outFile << currentTime << "\t"
71 >          << entry_plug->box_x << "\t"
72 >          << entry_plug->box_y << "\t"
73 >          << entry_plug->box_z << "\n";
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 +
88 +    if( atoms[i]->isDirectional() ){
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 << 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 <    
132 >
133 >    masterIndex = 0;
134      for( i=0; i<nAtoms; i++ ){
135        
136 <      outFile
137 <        << atoms[i]->getType() << "\t"
138 <        << atoms[i]->getX() << "\t"
139 <        << atoms[i]->getY() << "\t"
140 <        << atoms[i]->getZ() << "\t"
141 <        << atoms[i]->get_vx() << "\t"
142 <        << atoms[i]->get_vy() << "\t"
143 <        << atoms[i]->get_vz() << "\t";
144 <      
145 <      if( atoms[i]->isDirectional() ){
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
167 <          << q[0] << "\t"
168 <          << q[1] << "\t"
169 <          << q[2] << "\t"
170 <          << q[3] << "\t"
171 <          << dAtom->getJx() << "\t"
172 <          << dAtom->getJy() << "\t"
173 <          << dAtom->getJz() << "\n";
166 >      outfile << writeLine;
167 >      masterIndex++;
168 >    }
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 <      else{
198 <        outFile
199 <          << 0.0 << "\t"
200 <          << 0.0 << "\t"
201 <          << 0.0 << "\t"
202 <          << 0.0 << "\t"
203 <          << 0.0 << "\t"
204 <          << 0.0 << "\t"
205 <          << 0.0 << "\n";
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      }
109    outFile.flush();
213  
214 < #ifdef IS_MPI
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 +    
272   #endif // is_mpi
273   }
274  
# Line 117 | Line 276 | void DumpWriter::writeFinal(){
276  
277   void DumpWriter::writeFinal(){
278  
279 +
280 +  const int BUFFERSIZE = 2000;
281 +  char tempBuffer[500];
282 +  char writeLine[BUFFERSIZE];
283 +  
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 <
124 <    char finalName[500];
297 >    
298      strcpy( finalName, entry_plug->finalName );
299 <
300 <    ofstream finalOut( 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",
# Line 135 | Line 308 | void DumpWriter::writeFinal(){
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 <    int i;
321 <    double q[4];
141 <    DirectionalAtom* dAtom;
142 <    int nAtoms = entry_plug->n_atoms;
143 <    Atom** atoms = entry_plug->atoms;
320 >
321 > #ifndef IS_MPI
322      
323 +  finalOut << nAtoms << "\n";
324      
325 <    finalOut << nAtoms << "\n";
325 >  finalOut << currentTime << "\t"
326 >          << entry_plug->box_x << "\t"
327 >          << entry_plug->box_y << "\t"
328 >          << entry_plug->box_z << "\n";
329      
330 <    finalOut << 0.0 << "\t"
331 <             << entry_plug->box_x << "\t"
332 <             << entry_plug->box_y << "\t"
333 <             << entry_plug->box_z << "\n";
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 >
343 >    if( atoms[i]->isDirectional() ){
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 << 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 <      finalOut
392 <        << atoms[i]->getType() << "\t"
393 <        << atoms[i]->getX() << "\t"
394 <        << atoms[i]->getY() << "\t"
395 <        << atoms[i]->getZ() << "\t"
396 <        << atoms[i]->get_vx() << "\t"
397 <        << atoms[i]->get_vy() << "\t"
398 <        << atoms[i]->get_vz() << "\t";
399 <      
400 <      if( atoms[i]->isDirectional() ){
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 <        finalOut
422 <          << q[0] << "\t"
423 <          << q[1] << "\t"
424 <          << q[2] << "\t"
425 <          << q[3] << "\t"
426 <          << dAtom->getJx() << "\t"
427 <          << dAtom->getJy() << "\t"
428 <          << dAtom->getJz() << "\n";
421 >      outfile << writeLine;
422 >      masterIndex++;
423 >    }
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 <      else{
453 <        finalOut
454 <          << 0.0 << "\t"
455 <          << 0.0 << "\t"
456 <          << 0.0 << "\t"
457 <          << 0.0 << "\t"
458 <          << 0.0 << "\t"
459 <          << 0.0 << "\t"
460 <          << 0.0 << "\n";
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 <    finalOut.close();
471 <    
472 < #ifdef IS_MPI
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 +
529 +  if( worldRank == 0 ) finalOut.close();
530 +
531 +    
532   #endif // is_mpi
533   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines