ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/OOPSE/libmdtools/DumpWriter.cpp
Revision: 450
Committed: Thu Apr 3 21:12:51 2003 UTC (21 years, 3 months ago) by mmeineke
File size: 14207 byte(s)
Log Message:
just little things like deleteing unused variables and such.

File Contents

# User Rev Content
1 mmeineke 377 #include <cstring>
2     #include <iostream>
3     #include <fstream>
4    
5     #ifdef IS_MPI
6     #include <mpi.h>
7     #include "mpiSimulation.hpp"
8 chuckv 436 #define TAKE_THIS_TAG_CHAR 1
9     #define TAKE_THIS_TAG_INT 2
10 mmeineke 440
11     namespace dWrite{
12     void nodeZeroError( void );
13     void anonymousNodeDie( void );
14     }
15    
16     using namespace dWrite;
17 mmeineke 377 #endif //is_mpi
18    
19     #include "ReadWrite.hpp"
20     #include "simError.h"
21    
22     DumpWriter::DumpWriter( SimInfo* the_entry_plug ){
23    
24     entry_plug = the_entry_plug;
25    
26     #ifdef IS_MPI
27     if(worldRank == 0 ){
28     #endif // is_mpi
29    
30    
31    
32     strcpy( outName, entry_plug->sampleName );
33    
34     outFile.open(outName, ios::out | ios::trunc );
35    
36     if( !outFile ){
37    
38     sprintf( painCave.errMsg,
39     "Could not open \"%s\" for dump output.\n",
40     outName);
41     painCave.isFatal = 1;
42     simError();
43     }
44    
45     //outFile.setf( ios::scientific );
46    
47     #ifdef IS_MPI
48     }
49    
50     sprintf( checkPointMsg,
51     "Sucessfully opened output file for dumping.\n");
52     MPIcheckPoint();
53     #endif // is_mpi
54     }
55    
56     DumpWriter::~DumpWriter( ){
57    
58     #ifdef IS_MPI
59     if(worldRank == 0 ){
60     #endif // is_mpi
61    
62     outFile.close();
63    
64     #ifdef IS_MPI
65     }
66     #endif // is_mpi
67     }
68    
69     void DumpWriter::writeDump( double currentTime ){
70    
71     const int BUFFERSIZE = 2000;
72     char tempBuffer[BUFFERSIZE];
73     char writeLine[BUFFERSIZE];
74    
75 mmeineke 450 int i, j, which_node, done, which_atom, local_index;
76 mmeineke 377 double q[4];
77     DirectionalAtom* dAtom;
78     int nAtoms = entry_plug->n_atoms;
79     Atom** atoms = entry_plug->atoms;
80    
81    
82     #ifndef IS_MPI
83    
84     outFile << nAtoms << "\n";
85    
86     outFile << currentTime << "\t"
87     << entry_plug->box_x << "\t"
88     << entry_plug->box_y << "\t"
89     << entry_plug->box_z << "\n";
90    
91     for( i=0; i<nAtoms; i++ ){
92    
93    
94     sprintf( tempBuffer,
95     "%s\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t",
96     atoms[i]->getType(),
97     atoms[i]->getX(),
98     atoms[i]->getY(),
99     atoms[i]->getZ(),
100     atoms[i]->get_vx(),
101     atoms[i]->get_vy(),
102     atoms[i]->get_vz());
103     strcpy( writeLine, tempBuffer );
104    
105     if( atoms[i]->isDirectional() ){
106    
107     dAtom = (DirectionalAtom *)atoms[i];
108     dAtom->getQ( q );
109    
110     sprintf( tempBuffer,
111     "%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\n",
112     q[0],
113     q[1],
114     q[2],
115     q[3],
116     dAtom->getJx(),
117     dAtom->getJy(),
118     dAtom->getJz());
119     strcat( writeLine, tempBuffer );
120     }
121     else
122     strcat( writeLine, "0.0\t0.0\t0.0\t0.0\t0.0\t0.0\t0.0\n" );
123    
124     outFile << writeLine;
125     }
126     outFile.flush();
127    
128     #else // is_mpi
129 gezelter 416
130 mmeineke 440 // first thing first, suspend fatalities.
131     painCave.isEventLoop = 1;
132    
133     int myStatus; // 1 = wakeup & success; 0 = error; -1 = AllDone
134     int haveError;
135    
136 mmeineke 447 MPI_Status istatus;
137 gezelter 416 int *AtomToProcMap = mpiSim->getAtomToProcMap();
138 gezelter 415
139 mmeineke 377 // write out header and node 0's coordinates
140 gezelter 415
141 mmeineke 377 if( worldRank == 0 ){
142     outFile << mpiSim->getTotAtoms() << "\n";
143 gezelter 415
144 mmeineke 377 outFile << currentTime << "\t"
145     << entry_plug->box_x << "\t"
146     << entry_plug->box_y << "\t"
147     << entry_plug->box_z << "\n";
148 chuckv 434 outFile.flush();
149 gezelter 416 for (i = 0 ; i < mpiSim->getTotAtoms(); i++ ) {
150 gezelter 417 // Get the Node number which has this atom;
151 mmeineke 377
152 gezelter 415 which_node = AtomToProcMap[i];
153    
154 chuckv 436 if (which_node == 0 ) {
155 mmeineke 377
156 mmeineke 440 haveError = 0;
157 chuckv 436 which_atom = i;
158     local_index=-1;
159     for (j=0; (j<mpiSim->getMyNlocal()) && (local_index < 0); j++) {
160     if (atoms[j]->getGlobalIndex() == which_atom) local_index = j;
161     }
162     if (local_index != -1) {
163     //format the line
164     sprintf( tempBuffer,
165     "%s\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t",
166     atoms[local_index]->getType(),
167     atoms[local_index]->getX(),
168     atoms[local_index]->getY(),
169     atoms[local_index]->getZ(),
170     atoms[local_index]->get_vx(),
171     atoms[local_index]->get_vy(),
172     atoms[local_index]->get_vz()); // check here.
173     strcpy( writeLine, tempBuffer );
174 mmeineke 377
175 chuckv 436 if( atoms[local_index]->isDirectional() ){
176    
177     dAtom = (DirectionalAtom *)atoms[local_index];
178     dAtom->getQ( q );
179    
180     sprintf( tempBuffer,
181     "%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\n",
182     q[0],
183     q[1],
184     q[2],
185     q[3],
186     dAtom->getJx(),
187     dAtom->getJy(),
188     dAtom->getJz());
189     strcat( writeLine, tempBuffer );
190    
191     }
192     else
193     strcat( writeLine, "0.0\t0.0\t0.0\t0.0\t0.0\t0.0\t0.0\n" );
194     }
195     else {
196 mmeineke 440 sprintf(painCave.errMsg,
197     "Atom %d not found on processor %d\n",
198     i, worldRank );
199     haveError= 1;
200     simError();
201 chuckv 436 }
202 mmeineke 440
203     if(haveError) nodeZeroError();
204    
205     }
206 chuckv 436 else {
207 mmeineke 440 myStatus = 1;
208 mmeineke 447 MPI_Send(&myStatus, 1, MPI_INT, which_node,
209     TAKE_THIS_TAG_INT, MPI_COMM_WORLD);
210     MPI_Send(&i, 1, MPI_INT, which_node, TAKE_THIS_TAG_INT,
211     MPI_COMM_WORLD);
212     MPI_Recv(writeLine, BUFFERSIZE, MPI_CHAR, which_node,
213     TAKE_THIS_TAG_CHAR, MPI_COMM_WORLD, &istatus);
214     MPI_Recv(&myStatus, 1, MPI_INT, which_node,
215     TAKE_THIS_TAG_INT, MPI_COMM_WORLD, &istatus);
216 mmeineke 440
217     if(!myStatus) nodeZeroError();
218    
219 mmeineke 377 }
220 gezelter 415
221 mmeineke 377 outFile << writeLine;
222 chuckv 434 outFile.flush();
223 mmeineke 377 }
224    
225 gezelter 415 // kill everyone off:
226 mmeineke 440 myStatus = -1;
227 gezelter 416 for (j = 0; j < mpiSim->getNumberProcessors(); j++) {
228 mmeineke 447 MPI_Send(&myStatus, 1, MPI_INT, j,
229     TAKE_THIS_TAG_INT, MPI_COMM_WORLD);
230 mmeineke 377 }
231    
232 gezelter 415 } else {
233    
234     done = 0;
235     while (!done) {
236 mmeineke 440
237 mmeineke 447 MPI_Recv(&myStatus, 1, MPI_INT, 0,
238     TAKE_THIS_TAG_INT, MPI_COMM_WORLD, &istatus);
239 mmeineke 440
240     if(!myStatus) anonymousNodeDie();
241    
242     if(myStatus < 0) break;
243    
244 mmeineke 447 MPI_Recv(&which_atom, 1, MPI_INT, 0,
245     TAKE_THIS_TAG_INT, MPI_COMM_WORLD, &istatus);
246 mmeineke 440
247     myStatus = 1;
248     local_index=-1;
249     for (j=0; (j<mpiSim->getMyNlocal()) && (local_index < 0); j++) {
250     if (atoms[j]->getGlobalIndex() == which_atom) local_index = j;
251     }
252     if (local_index != -1) {
253     //format the line
254     sprintf( tempBuffer,
255     "%s\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t",
256     atoms[local_index]->getType(),
257     atoms[local_index]->getX(),
258     atoms[local_index]->getY(),
259     atoms[local_index]->getZ(),
260     atoms[local_index]->get_vx(),
261     atoms[local_index]->get_vy(),
262     atoms[local_index]->get_vz()); // check here.
263     strcpy( writeLine, tempBuffer );
264    
265     if( atoms[local_index]->isDirectional() ){
266 mmeineke 377
267 mmeineke 440 dAtom = (DirectionalAtom *)atoms[local_index];
268     dAtom->getQ( q );
269    
270     sprintf( tempBuffer,
271     "%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\n",
272     q[0],
273     q[1],
274     q[2],
275     q[3],
276     dAtom->getJx(),
277     dAtom->getJy(),
278     dAtom->getJz());
279     strcat( writeLine, tempBuffer );
280     }
281     else{
282     strcat( writeLine, "0.0\t0.0\t0.0\t0.0\t0.0\t0.0\t0.0\n" );
283     }
284     }
285     else {
286     sprintf(painCave.errMsg,
287     "Atom %d not found on processor %d\n",
288     which_atom, worldRank );
289     myStatus = 0;
290     simError();
291    
292     strcpy( writeLine, "Hello, I'm an error.\n");
293 mmeineke 377 }
294 mmeineke 440
295 mmeineke 447 MPI_Send(writeLine, BUFFERSIZE, MPI_CHAR, 0,
296     TAKE_THIS_TAG_CHAR, MPI_COMM_WORLD);
297     MPI_Send( &myStatus, 1, MPI_INT, 0,
298     TAKE_THIS_TAG_INT, MPI_COMM_WORLD);
299 mmeineke 377 }
300 gezelter 415 }
301     outFile.flush();
302     sprintf( checkPointMsg,
303     "Sucessfully took a dump.\n");
304     MPIcheckPoint();
305 mmeineke 440
306     // last thing last, enable fatalities.
307     painCave.isEventLoop = 0;
308    
309 mmeineke 377 #endif // is_mpi
310     }
311    
312     void DumpWriter::writeFinal(){
313 gezelter 416
314 mmeineke 377 char finalName[500];
315     ofstream finalOut;
316 gezelter 416
317     const int BUFFERSIZE = 2000;
318     char tempBuffer[BUFFERSIZE];
319     char writeLine[BUFFERSIZE];
320    
321     double q[4];
322     DirectionalAtom* dAtom;
323     int nAtoms = entry_plug->n_atoms;
324     Atom** atoms = entry_plug->atoms;
325 gezelter 419 int i, j, which_node, done, game_over, which_atom, local_index;
326 mmeineke 377
327 gezelter 416
328 mmeineke 377 #ifdef IS_MPI
329     if(worldRank == 0 ){
330     #endif // is_mpi
331    
332     strcpy( finalName, entry_plug->finalName );
333    
334     finalOut.open( finalName, ios::out | ios::trunc );
335     if( !finalOut ){
336     sprintf( painCave.errMsg,
337     "Could not open \"%s\" for final dump output.\n",
338     finalName );
339     painCave.isFatal = 1;
340     simError();
341     }
342    
343     // finalOut.setf( ios::scientific );
344    
345     #ifdef IS_MPI
346     }
347    
348     sprintf(checkPointMsg,"Opened file for final configuration\n");
349     MPIcheckPoint();
350    
351     #endif //is_mpi
352    
353 gezelter 415
354 mmeineke 377 #ifndef IS_MPI
355    
356     finalOut << nAtoms << "\n";
357    
358     finalOut << entry_plug->box_x << "\t"
359     << entry_plug->box_y << "\t"
360     << entry_plug->box_z << "\n";
361 gezelter 416
362 mmeineke 377 for( i=0; i<nAtoms; i++ ){
363    
364     sprintf( tempBuffer,
365     "%s\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t",
366     atoms[i]->getType(),
367     atoms[i]->getX(),
368     atoms[i]->getY(),
369     atoms[i]->getZ(),
370     atoms[i]->get_vx(),
371     atoms[i]->get_vy(),
372     atoms[i]->get_vz());
373     strcpy( writeLine, tempBuffer );
374    
375     if( atoms[i]->isDirectional() ){
376    
377     dAtom = (DirectionalAtom *)atoms[i];
378     dAtom->getQ( q );
379    
380     sprintf( tempBuffer,
381     "%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\n",
382     q[0],
383     q[1],
384     q[2],
385     q[3],
386     dAtom->getJx(),
387     dAtom->getJy(),
388     dAtom->getJz());
389     strcat( writeLine, tempBuffer );
390     }
391     else
392     strcat( writeLine, "0.0\t0.0\t0.0\t0.0\t0.0\t0.0\t0.0\n" );
393    
394     finalOut << writeLine;
395     }
396     finalOut.flush();
397 gezelter 415 finalOut.close();
398 mmeineke 377
399     #else // is_mpi
400 gezelter 415
401 mmeineke 440 // first thing first, suspend fatalities.
402     painCave.isEventLoop = 1;
403    
404     int myStatus; // 1 = wakeup & success; 0 = error; -1 = AllDone
405     int haveError;
406    
407 mmeineke 447 MPI_Status istatus;
408 gezelter 416 int *AtomToProcMap = mpiSim->getAtomToProcMap();
409    
410 mmeineke 377 // write out header and node 0's coordinates
411 gezelter 415
412 mmeineke 440 haveError = 0;
413 mmeineke 377 if( worldRank == 0 ){
414     finalOut << mpiSim->getTotAtoms() << "\n";
415 gezelter 415
416 mmeineke 377 finalOut << entry_plug->box_x << "\t"
417 gezelter 415 << entry_plug->box_y << "\t"
418     << entry_plug->box_z << "\n";
419 mmeineke 377
420 gezelter 416 for (i = 0 ; i < mpiSim->getTotAtoms(); i++ ) {
421 gezelter 415 // Get the Node number which has this molecule:
422 mmeineke 377
423 gezelter 415 which_node = AtomToProcMap[i];
424    
425 gezelter 416 if (which_node == mpiSim->getMyNode()) {
426 chuckv 437
427     which_atom = i;
428     local_index=-1;
429     for (j=0; (j<mpiSim->getMyNlocal()) && (local_index < 0); j++) {
430     if (atoms[j]->getGlobalIndex() == which_atom) local_index = j;
431     }
432     if (local_index != -1) {
433     sprintf( tempBuffer,
434     "%s\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t",
435     atoms[local_index]->getType(),
436     atoms[local_index]->getX(),
437     atoms[local_index]->getY(),
438     atoms[local_index]->getZ(),
439     atoms[local_index]->get_vx(),
440     atoms[local_index]->get_vy(),
441     atoms[local_index]->get_vz());
442     strcpy( writeLine, tempBuffer );
443 mmeineke 377
444 chuckv 437 if( atoms[local_index]->isDirectional() ){
445    
446     dAtom = (DirectionalAtom *)atoms[local_index];
447     dAtom->getQ( q );
448    
449     sprintf( tempBuffer,
450     "%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\n",
451     q[0],
452     q[1],
453     q[2],
454     q[3],
455     dAtom->getJx(),
456     dAtom->getJy(),
457     dAtom->getJz());
458     strcat( writeLine, tempBuffer );
459     }
460     else
461     strcat( writeLine, "0.0\t0.0\t0.0\t0.0\t0.0\t0.0\t0.0\n" );
462     }
463     else {
464 mmeineke 440 sprintf(painCave.errMsg,
465     "Atom %d not found on processor %d\n",
466     i, worldRank );
467     haveError= 1;
468     simError();
469 chuckv 437 }
470 mmeineke 440
471     if(haveError) nodeZeroError();
472 chuckv 437
473 mmeineke 440 }
474     else {
475 gezelter 415
476 mmeineke 440 myStatus = 1;
477 mmeineke 447 MPI_Send(&myStatus, 1, MPI_INT, which_node,
478     TAKE_THIS_TAG_INT, MPI_COMM_WORLD);
479     MPI_Send(&i, 1, MPI_INT, which_node, TAKE_THIS_TAG_INT,
480     MPI_COMM_WORLD);
481     MPI_Recv(writeLine, BUFFERSIZE, MPI_CHAR, which_node,
482     TAKE_THIS_TAG_CHAR, MPI_COMM_WORLD, &istatus);
483     MPI_Recv(&myStatus, 1, MPI_INT, which_node,
484     TAKE_THIS_TAG_INT, MPI_COMM_WORLD, &istatus);
485 mmeineke 440
486     if(!myStatus) nodeZeroError();
487 mmeineke 377 }
488 gezelter 415
489 mmeineke 377 finalOut << writeLine;
490     }
491    
492 gezelter 415 // kill everyone off:
493 mmeineke 440 myStatus = -1;
494     for (j = 0; j < mpiSim->getNumberProcessors(); j++) {
495 mmeineke 447 MPI_Send(&myStatus, 1, MPI_INT, j,
496     TAKE_THIS_TAG_INT, MPI_COMM_WORLD);
497 mmeineke 377 }
498    
499 gezelter 415 } else {
500    
501     done = 0;
502     while (!done) {
503 mmeineke 440
504 mmeineke 447 MPI_Recv(&myStatus, 1, MPI_INT, 0,
505     TAKE_THIS_TAG_INT, MPI_COMM_WORLD, &istatus);
506 mmeineke 440
507     if(!myStatus) anonymousNodeDie();
508    
509     if(myStatus < 0) break;
510    
511 mmeineke 447 MPI_Recv(&which_atom, 1, MPI_INT, 0,
512     TAKE_THIS_TAG_INT, MPI_COMM_WORLD, &istatus);
513 mmeineke 440
514     myStatus = 1;
515     local_index=-1;
516     for (j=0; j < mpiSim->getMyNlocal(); j++) {
517     if (atoms[j]->getGlobalIndex() == which_atom) local_index = j;
518     }
519     if (local_index != -1) {
520 mmeineke 377
521 mmeineke 440 //format the line
522     sprintf( tempBuffer,
523     "%s\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t",
524     atoms[local_index]->getType(),
525     atoms[local_index]->getX(),
526     atoms[local_index]->getY(),
527     atoms[local_index]->getZ(),
528     atoms[local_index]->get_vx(),
529     atoms[local_index]->get_vy(),
530     atoms[local_index]->get_vz()); // check here.
531     strcpy( writeLine, tempBuffer );
532    
533     if( atoms[local_index]->isDirectional() ){
534 mmeineke 377
535 mmeineke 440 dAtom = (DirectionalAtom *)atoms[local_index];
536     dAtom->getQ( q );
537    
538     sprintf( tempBuffer,
539     "%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\n",
540     q[0],
541     q[1],
542     q[2],
543     q[3],
544     dAtom->getJx(),
545     dAtom->getJy(),
546     dAtom->getJz());
547     strcat( writeLine, tempBuffer );
548     }
549     else{
550     strcat( writeLine, "0.0\t0.0\t0.0\t0.0\t0.0\t0.0\t0.0\n" );
551     }
552     }
553     else {
554     sprintf(painCave.errMsg,
555     "Atom %d not found on processor %d\n",
556     which_atom, worldRank );
557     myStatus = 0;
558     simError();
559    
560     strcpy( writeLine, "Hello, I'm an error.\n");
561 mmeineke 377 }
562 mmeineke 440
563 mmeineke 447 MPI_Send(writeLine, BUFFERSIZE, MPI_CHAR, 0,
564     TAKE_THIS_TAG_CHAR, MPI_COMM_WORLD);
565     MPI_Send( &myStatus, 1, MPI_INT, 0,
566     TAKE_THIS_TAG_INT, MPI_COMM_WORLD);
567 mmeineke 377 }
568 gezelter 419 }
569 gezelter 415 finalOut.flush();
570     sprintf( checkPointMsg,
571     "Sucessfully took a dump.\n");
572     MPIcheckPoint();
573 mmeineke 440
574 gezelter 415 if( worldRank == 0 ) finalOut.close();
575 mmeineke 377 #endif // is_mpi
576     }
577 mmeineke 440
578    
579    
580     #ifdef IS_MPI
581    
582     // a couple of functions to let us escape the write loop
583    
584     void dWrite::nodeZeroError( void ){
585     int j, myStatus;
586    
587     myStatus = 0;
588     for (j = 0; j < mpiSim->getNumberProcessors(); j++) {
589 mmeineke 447 MPI_Send( &myStatus, 1, MPI_INT, j,
590     TAKE_THIS_TAG_INT, MPI_COMM_WORLD);
591 mmeineke 440 }
592    
593    
594     MPI_Finalize();
595     exit (0);
596    
597     }
598    
599     void dWrite::anonymousNodeDie( void ){
600    
601     MPI_Finalize();
602     exit (0);
603     }
604    
605     #endif //is_mpi