ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/OOPSE/libmdtools/DumpWriter.cpp
Revision: 416
Committed: Wed Mar 26 23:14:02 2003 UTC (21 years, 3 months ago) by gezelter
File size: 10743 byte(s)
Log Message:
bug fixes   many bug fixes

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 gezelter 416 #include <mpi++.h>
8 mmeineke 377 #include "mpiSimulation.hpp"
9     #define TAKE_THIS_TAG 0
10     #endif //is_mpi
11    
12     #include "ReadWrite.hpp"
13     #include "simError.h"
14    
15     DumpWriter::DumpWriter( SimInfo* the_entry_plug ){
16    
17     entry_plug = the_entry_plug;
18    
19     #ifdef IS_MPI
20     if(worldRank == 0 ){
21     #endif // is_mpi
22    
23    
24    
25     strcpy( outName, entry_plug->sampleName );
26    
27     outFile.open(outName, ios::out | ios::trunc );
28    
29     if( !outFile ){
30    
31     sprintf( painCave.errMsg,
32     "Could not open \"%s\" for dump output.\n",
33     outName);
34     painCave.isFatal = 1;
35     simError();
36     }
37    
38     //outFile.setf( ios::scientific );
39    
40     #ifdef IS_MPI
41     }
42    
43     sprintf( checkPointMsg,
44     "Sucessfully opened output file for dumping.\n");
45     MPIcheckPoint();
46     #endif // is_mpi
47     }
48    
49     DumpWriter::~DumpWriter( ){
50    
51     #ifdef IS_MPI
52     if(worldRank == 0 ){
53     #endif // is_mpi
54    
55     outFile.close();
56    
57     #ifdef IS_MPI
58     }
59     #endif // is_mpi
60     }
61    
62     void DumpWriter::writeDump( double currentTime ){
63    
64     const int BUFFERSIZE = 2000;
65     char tempBuffer[BUFFERSIZE];
66     char writeLine[BUFFERSIZE];
67    
68 gezelter 416 int i, j, which_node, done, game_over, which_atom;
69 mmeineke 377 double q[4];
70     DirectionalAtom* dAtom;
71     int nAtoms = entry_plug->n_atoms;
72     Atom** atoms = entry_plug->atoms;
73    
74    
75     #ifndef IS_MPI
76    
77     outFile << nAtoms << "\n";
78    
79     outFile << currentTime << "\t"
80     << entry_plug->box_x << "\t"
81     << entry_plug->box_y << "\t"
82     << entry_plug->box_z << "\n";
83    
84     for( i=0; i<nAtoms; i++ ){
85    
86    
87     sprintf( tempBuffer,
88     "%s\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t",
89     atoms[i]->getType(),
90     atoms[i]->getX(),
91     atoms[i]->getY(),
92     atoms[i]->getZ(),
93     atoms[i]->get_vx(),
94     atoms[i]->get_vy(),
95     atoms[i]->get_vz());
96     strcpy( writeLine, tempBuffer );
97    
98     if( atoms[i]->isDirectional() ){
99    
100     dAtom = (DirectionalAtom *)atoms[i];
101     dAtom->getQ( q );
102    
103     sprintf( tempBuffer,
104     "%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\n",
105     q[0],
106     q[1],
107     q[2],
108     q[3],
109     dAtom->getJx(),
110     dAtom->getJy(),
111     dAtom->getJz());
112     strcat( writeLine, tempBuffer );
113     }
114     else
115     strcat( writeLine, "0.0\t0.0\t0.0\t0.0\t0.0\t0.0\t0.0\n" );
116    
117     outFile << writeLine;
118     }
119     outFile.flush();
120    
121     #else // is_mpi
122 gezelter 416
123     MPI::Status istatus;
124     int *AtomToProcMap = mpiSim->getAtomToProcMap();
125 gezelter 415
126 mmeineke 377 // write out header and node 0's coordinates
127 gezelter 415
128 mmeineke 377 if( worldRank == 0 ){
129     outFile << mpiSim->getTotAtoms() << "\n";
130 gezelter 415
131 mmeineke 377 outFile << currentTime << "\t"
132     << entry_plug->box_x << "\t"
133     << entry_plug->box_y << "\t"
134     << entry_plug->box_z << "\n";
135 gezelter 415
136 gezelter 416 for (i = 0 ; i < mpiSim->getTotAtoms(); i++ ) {
137 gezelter 415 // Get the Node number which has this molecule:
138 mmeineke 377
139 gezelter 415 which_node = AtomToProcMap[i];
140    
141 gezelter 416 if (which_node == mpiSim->getMyNode()) {
142 gezelter 415
143     sprintf( tempBuffer,
144     "%s\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t",
145     atoms[i]->getType(),
146     atoms[i]->getX(),
147     atoms[i]->getY(),
148     atoms[i]->getZ(),
149     atoms[i]->get_vx(),
150     atoms[i]->get_vy(),
151     atoms[i]->get_vz());
152     strcpy( writeLine, tempBuffer );
153 mmeineke 377
154 gezelter 415 if( atoms[i]->isDirectional() ){
155 mmeineke 377
156 gezelter 415 dAtom = (DirectionalAtom *)atoms[i];
157     dAtom->getQ( q );
158 mmeineke 377
159 gezelter 415 sprintf( tempBuffer,
160     "%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\n",
161     q[0],
162     q[1],
163     q[2],
164     q[3],
165     dAtom->getJx(),
166     dAtom->getJy(),
167     dAtom->getJz());
168     strcat( writeLine, tempBuffer );
169     }
170     else
171     strcat( writeLine, "0.0\t0.0\t0.0\t0.0\t0.0\t0.0\t0.0\n" );
172    
173     } else {
174    
175     MPI::COMM_WORLD.Send(&i, 1, MPI_INT, which_node, TAKE_THIS_TAG);
176 gezelter 416 MPI::COMM_WORLD.Recv(writeLine, BUFFERSIZE, MPI_CHAR, which_node,
177 gezelter 415 TAKE_THIS_TAG, istatus);
178 mmeineke 377 }
179 gezelter 415
180 mmeineke 377 outFile << writeLine;
181     }
182    
183 gezelter 415 // kill everyone off:
184 gezelter 416 game_over = -1;
185     for (j = 0; j < mpiSim->getNumberProcessors(); j++) {
186     MPI::COMM_WORLD.Send(&game_over, 1, MPI_INT, j, TAKE_THIS_TAG);
187 mmeineke 377 }
188    
189 gezelter 415 } else {
190    
191     done = 0;
192     while (!done) {
193 gezelter 416 MPI::COMM_WORLD.Recv(&which_atom, 1, MPI_INT, 0,
194 gezelter 415 TAKE_THIS_TAG, istatus);
195 mmeineke 377
196 gezelter 416 if (which_atom == -1) {
197 gezelter 415 done=1;
198     continue;
199     } else {
200 mmeineke 377
201 gezelter 415 //format the line
202 mmeineke 377 sprintf( tempBuffer,
203     "%s\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t",
204 gezelter 415 atoms[which_atom]->getType(),
205     atoms[which_atom]->getX(),
206     atoms[which_atom]->getY(),
207     atoms[which_atom]->getZ(),
208     atoms[which_atom]->get_vx(),
209     atoms[which_atom]->get_vy(),
210     atoms[which_atom]->get_vz()); // check here.
211 mmeineke 377 strcpy( writeLine, tempBuffer );
212    
213 gezelter 415 if( atoms[which_atom]->isDirectional() ){
214 mmeineke 377
215 gezelter 415 dAtom = (DirectionalAtom *)atoms[which_atom];
216 mmeineke 377 dAtom->getQ( q );
217    
218     sprintf( tempBuffer,
219     "%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\n",
220     q[0],
221     q[1],
222     q[2],
223     q[3],
224     dAtom->getJx(),
225     dAtom->getJy(),
226     dAtom->getJz());
227     strcat( writeLine, tempBuffer );
228     }
229     else
230     strcat( writeLine, "0.0\t0.0\t0.0\t0.0\t0.0\t0.0\t0.0\n" );
231 gezelter 415
232     MPI::COMM_WORLD.Send(writeLine, BUFFERSIZE, MPI_CHAR, 0,
233     TAKE_THIS_TAG);
234 mmeineke 377 }
235     }
236 gezelter 415 }
237     outFile.flush();
238     sprintf( checkPointMsg,
239     "Sucessfully took a dump.\n");
240     MPIcheckPoint();
241 mmeineke 377 #endif // is_mpi
242     }
243    
244     void DumpWriter::writeFinal(){
245 gezelter 416
246 mmeineke 377 char finalName[500];
247     ofstream finalOut;
248 gezelter 416
249     const int BUFFERSIZE = 2000;
250     char tempBuffer[BUFFERSIZE];
251     char writeLine[BUFFERSIZE];
252    
253     double q[4];
254     DirectionalAtom* dAtom;
255     int nAtoms = entry_plug->n_atoms;
256     Atom** atoms = entry_plug->atoms;
257     int i, j, which_node, done, game_over, which_atom;
258 mmeineke 377
259 gezelter 416
260 mmeineke 377 #ifdef IS_MPI
261     if(worldRank == 0 ){
262     #endif // is_mpi
263    
264     strcpy( finalName, entry_plug->finalName );
265    
266     finalOut.open( finalName, ios::out | ios::trunc );
267     if( !finalOut ){
268     sprintf( painCave.errMsg,
269     "Could not open \"%s\" for final dump output.\n",
270     finalName );
271     painCave.isFatal = 1;
272     simError();
273     }
274    
275     // finalOut.setf( ios::scientific );
276    
277     #ifdef IS_MPI
278     }
279    
280     sprintf(checkPointMsg,"Opened file for final configuration\n");
281     MPIcheckPoint();
282    
283     #endif //is_mpi
284    
285 gezelter 415
286 mmeineke 377 #ifndef IS_MPI
287    
288     finalOut << nAtoms << "\n";
289    
290     finalOut << entry_plug->box_x << "\t"
291     << entry_plug->box_y << "\t"
292     << entry_plug->box_z << "\n";
293 gezelter 416
294 mmeineke 377 for( i=0; i<nAtoms; i++ ){
295    
296     sprintf( tempBuffer,
297     "%s\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t",
298     atoms[i]->getType(),
299     atoms[i]->getX(),
300     atoms[i]->getY(),
301     atoms[i]->getZ(),
302     atoms[i]->get_vx(),
303     atoms[i]->get_vy(),
304     atoms[i]->get_vz());
305     strcpy( writeLine, tempBuffer );
306    
307     if( atoms[i]->isDirectional() ){
308    
309     dAtom = (DirectionalAtom *)atoms[i];
310     dAtom->getQ( q );
311    
312     sprintf( tempBuffer,
313     "%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\n",
314     q[0],
315     q[1],
316     q[2],
317     q[3],
318     dAtom->getJx(),
319     dAtom->getJy(),
320     dAtom->getJz());
321     strcat( writeLine, tempBuffer );
322     }
323     else
324     strcat( writeLine, "0.0\t0.0\t0.0\t0.0\t0.0\t0.0\t0.0\n" );
325    
326     finalOut << writeLine;
327     }
328     finalOut.flush();
329 gezelter 415 finalOut.close();
330 mmeineke 377
331     #else // is_mpi
332 gezelter 415
333 gezelter 416 MPI::Status istatus;
334     int *AtomToProcMap = mpiSim->getAtomToProcMap();
335    
336 mmeineke 377 // write out header and node 0's coordinates
337 gezelter 415
338 mmeineke 377 if( worldRank == 0 ){
339     finalOut << mpiSim->getTotAtoms() << "\n";
340 gezelter 415
341 mmeineke 377 finalOut << entry_plug->box_x << "\t"
342 gezelter 415 << entry_plug->box_y << "\t"
343     << entry_plug->box_z << "\n";
344 mmeineke 377
345 gezelter 416 for (i = 0 ; i < mpiSim->getTotAtoms(); i++ ) {
346 gezelter 415 // Get the Node number which has this molecule:
347 mmeineke 377
348 gezelter 415 which_node = AtomToProcMap[i];
349    
350 gezelter 416 if (which_node == mpiSim->getMyNode()) {
351 gezelter 415
352     sprintf( tempBuffer,
353     "%s\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t",
354     atoms[i]->getType(),
355     atoms[i]->getX(),
356     atoms[i]->getY(),
357     atoms[i]->getZ(),
358     atoms[i]->get_vx(),
359     atoms[i]->get_vy(),
360     atoms[i]->get_vz());
361     strcpy( writeLine, tempBuffer );
362 mmeineke 377
363 gezelter 415 if( atoms[i]->isDirectional() ){
364 mmeineke 377
365 gezelter 415 dAtom = (DirectionalAtom *)atoms[i];
366     dAtom->getQ( q );
367 mmeineke 377
368 gezelter 415 sprintf( tempBuffer,
369     "%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\n",
370     q[0],
371     q[1],
372     q[2],
373     q[3],
374     dAtom->getJx(),
375     dAtom->getJy(),
376     dAtom->getJz());
377     strcat( writeLine, tempBuffer );
378     }
379     else
380     strcat( writeLine, "0.0\t0.0\t0.0\t0.0\t0.0\t0.0\t0.0\n" );
381    
382     } else {
383    
384     MPI::COMM_WORLD.Send(&i, 1, MPI_INT, which_node, TAKE_THIS_TAG);
385 gezelter 416 MPI::COMM_WORLD.Recv(writeLine, BUFFERSIZE, MPI_CHAR, which_node,
386 gezelter 415 TAKE_THIS_TAG, istatus);
387 mmeineke 377 }
388 gezelter 415
389 mmeineke 377 finalOut << writeLine;
390     }
391    
392 gezelter 415 // kill everyone off:
393 gezelter 416 game_over = -1;
394     for (j = 0; j < mpiSim->getNumberProcessors(); j++) {
395     MPI::COMM_WORLD.Send(&game_over, 1, MPI_INT, j, TAKE_THIS_TAG);
396 mmeineke 377 }
397    
398 gezelter 415 } else {
399    
400     done = 0;
401     while (!done) {
402 gezelter 416 MPI::COMM_WORLD.Recv(&which_atom, 1, MPI_INT, 0,
403     TAKE_THIS_TAG, istatus);
404 mmeineke 377
405 gezelter 416 if (which_atom == -1) {
406 gezelter 415 done=1;
407     continue;
408     } else {
409 mmeineke 377
410 gezelter 415 //format the line
411 mmeineke 377 sprintf( tempBuffer,
412     "%s\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t",
413 gezelter 415 atoms[which_atom]->getType(),
414     atoms[which_atom]->getX(),
415     atoms[which_atom]->getY(),
416     atoms[which_atom]->getZ(),
417     atoms[which_atom]->get_vx(),
418     atoms[which_atom]->get_vy(),
419     atoms[which_atom]->get_vz()); // check here.
420 mmeineke 377 strcpy( writeLine, tempBuffer );
421    
422 gezelter 415 if( atoms[which_atom]->isDirectional() ){
423 mmeineke 377
424 gezelter 415 dAtom = (DirectionalAtom *)atoms[which_atom];
425 mmeineke 377 dAtom->getQ( q );
426    
427     sprintf( tempBuffer,
428     "%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\n",
429     q[0],
430     q[1],
431     q[2],
432     q[3],
433     dAtom->getJx(),
434     dAtom->getJy(),
435     dAtom->getJz());
436     strcat( writeLine, tempBuffer );
437     }
438     else
439     strcat( writeLine, "0.0\t0.0\t0.0\t0.0\t0.0\t0.0\t0.0\n" );
440 gezelter 415
441     MPI::COMM_WORLD.Send(writeLine, BUFFERSIZE, MPI_CHAR, 0,
442     TAKE_THIS_TAG);
443 mmeineke 377 }
444     }
445 gezelter 415 }
446     finalOut.flush();
447     sprintf( checkPointMsg,
448     "Sucessfully took a dump.\n");
449     MPIcheckPoint();
450 mmeineke 377
451 gezelter 415 if( worldRank == 0 ) finalOut.close();
452 mmeineke 377 #endif // is_mpi
453     }