ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/OOPSE/libmdtools/DumpWriter.cpp
Revision: 434
Committed: Fri Mar 28 19:30:59 2003 UTC (21 years, 3 months ago) by chuckv
File size: 12795 byte(s)
Log Message:
mpi fixes and debugging mpi read write from file.

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 chuckv 434 #define TAKE_THIS_TAG 1
10 mmeineke 377 #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 419 int i, j, which_node, done, game_over, which_atom, local_index;
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 chuckv 434 outFile.flush();
136 gezelter 416 for (i = 0 ; i < mpiSim->getTotAtoms(); i++ ) {
137 gezelter 417 // Get the Node number which has this atom;
138 mmeineke 377
139 gezelter 415 which_node = AtomToProcMap[i];
140    
141 gezelter 416 if (which_node == mpiSim->getMyNode()) {
142 chuckv 434 sprintf( tempBuffer,
143 gezelter 415 "%s\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t",
144     atoms[i]->getType(),
145     atoms[i]->getX(),
146     atoms[i]->getY(),
147     atoms[i]->getZ(),
148     atoms[i]->get_vx(),
149     atoms[i]->get_vy(),
150     atoms[i]->get_vz());
151     strcpy( writeLine, tempBuffer );
152 mmeineke 377
153 gezelter 415 if( atoms[i]->isDirectional() ){
154 mmeineke 377
155 gezelter 415 dAtom = (DirectionalAtom *)atoms[i];
156     dAtom->getQ( q );
157 mmeineke 377
158 gezelter 415 sprintf( tempBuffer,
159     "%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\n",
160     q[0],
161     q[1],
162     q[2],
163     q[3],
164     dAtom->getJx(),
165     dAtom->getJy(),
166     dAtom->getJz());
167     strcat( writeLine, tempBuffer );
168     }
169     else
170     strcat( writeLine, "0.0\t0.0\t0.0\t0.0\t0.0\t0.0\t0.0\n" );
171    
172     } else {
173    
174 chuckv 434 std::cerr << "node 0: sending node " << which_node << " request for atom " << i << "\n";
175 gezelter 415 MPI::COMM_WORLD.Send(&i, 1, MPI_INT, which_node, TAKE_THIS_TAG);
176 chuckv 434 std::cerr << "node 0: sent!\n";
177 gezelter 416 MPI::COMM_WORLD.Recv(writeLine, BUFFERSIZE, MPI_CHAR, which_node,
178 gezelter 415 TAKE_THIS_TAG, istatus);
179 chuckv 434 std::cerr << "node 0: got this line: " << writeLine;
180 mmeineke 377 }
181 gezelter 415
182 mmeineke 377 outFile << writeLine;
183 chuckv 434 outFile.flush();
184 mmeineke 377 }
185    
186 gezelter 415 // kill everyone off:
187 gezelter 416 game_over = -1;
188     for (j = 0; j < mpiSim->getNumberProcessors(); j++) {
189     MPI::COMM_WORLD.Send(&game_over, 1, MPI_INT, j, TAKE_THIS_TAG);
190 mmeineke 377 }
191    
192 gezelter 415 } else {
193    
194     done = 0;
195     while (!done) {
196 chuckv 434 std::cerr << "node: " << mpiSim->getMyNode() << " Waiting for receive \n";
197 gezelter 416 MPI::COMM_WORLD.Recv(&which_atom, 1, MPI_INT, 0,
198 gezelter 419 TAKE_THIS_TAG, istatus);
199 chuckv 434 std::cerr << "node: " << mpiSim->getMyNode() << " got request for atom " << which_atom << "\n";
200 gezelter 416 if (which_atom == -1) {
201 gezelter 415 done=1;
202     continue;
203     } else {
204 gezelter 419 local_index=-1;
205     for (j=0; j < mpiSim->getMyNlocal(); j++) {
206     if (atoms[j]->getGlobalIndex() == which_atom) local_index = j;
207     }
208     if (local_index != -1) {
209     //format the line
210     sprintf( tempBuffer,
211     "%s\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t",
212     atoms[local_index]->getType(),
213     atoms[local_index]->getX(),
214     atoms[local_index]->getY(),
215     atoms[local_index]->getZ(),
216     atoms[local_index]->get_vx(),
217     atoms[local_index]->get_vy(),
218     atoms[local_index]->get_vz()); // check here.
219     strcpy( writeLine, tempBuffer );
220 mmeineke 377
221 gezelter 419 if( atoms[local_index]->isDirectional() ){
222    
223     dAtom = (DirectionalAtom *)atoms[local_index];
224     dAtom->getQ( q );
225    
226     sprintf( tempBuffer,
227     "%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\n",
228     q[0],
229     q[1],
230     q[2],
231     q[3],
232     dAtom->getJx(),
233     dAtom->getJy(),
234     dAtom->getJz());
235     strcat( writeLine, tempBuffer );
236     }
237     else
238     strcat( writeLine, "0.0\t0.0\t0.0\t0.0\t0.0\t0.0\t0.0\n" );
239 chuckv 434 std::cerr << "node: " << mpiSim->getMyNode() << " sending this line" << writeLine;
240 gezelter 419 MPI::COMM_WORLD.Send(writeLine, BUFFERSIZE, MPI_CHAR, 0,
241     TAKE_THIS_TAG);
242     } else {
243     strcpy( writeLine, "ATOM NOT FOUND ON THIS PROCESSOR");
244     MPI::COMM_WORLD.Send(writeLine, BUFFERSIZE, MPI_CHAR, 0,
245     TAKE_THIS_TAG);
246     }
247 mmeineke 377 }
248     }
249 gezelter 415 }
250     outFile.flush();
251     sprintf( checkPointMsg,
252     "Sucessfully took a dump.\n");
253     MPIcheckPoint();
254 mmeineke 377 #endif // is_mpi
255     }
256    
257     void DumpWriter::writeFinal(){
258 gezelter 416
259 mmeineke 377 char finalName[500];
260     ofstream finalOut;
261 gezelter 416
262     const int BUFFERSIZE = 2000;
263     char tempBuffer[BUFFERSIZE];
264     char writeLine[BUFFERSIZE];
265    
266     double q[4];
267     DirectionalAtom* dAtom;
268     int nAtoms = entry_plug->n_atoms;
269     Atom** atoms = entry_plug->atoms;
270 gezelter 419 int i, j, which_node, done, game_over, which_atom, local_index;
271 mmeineke 377
272 gezelter 416
273 mmeineke 377 #ifdef IS_MPI
274     if(worldRank == 0 ){
275     #endif // is_mpi
276    
277     strcpy( finalName, entry_plug->finalName );
278    
279     finalOut.open( finalName, ios::out | ios::trunc );
280     if( !finalOut ){
281     sprintf( painCave.errMsg,
282     "Could not open \"%s\" for final dump output.\n",
283     finalName );
284     painCave.isFatal = 1;
285     simError();
286     }
287    
288     // finalOut.setf( ios::scientific );
289    
290     #ifdef IS_MPI
291     }
292    
293     sprintf(checkPointMsg,"Opened file for final configuration\n");
294     MPIcheckPoint();
295    
296     #endif //is_mpi
297    
298 gezelter 415
299 mmeineke 377 #ifndef IS_MPI
300    
301     finalOut << nAtoms << "\n";
302    
303     finalOut << entry_plug->box_x << "\t"
304     << entry_plug->box_y << "\t"
305     << entry_plug->box_z << "\n";
306 gezelter 416
307 mmeineke 377 for( i=0; i<nAtoms; i++ ){
308    
309     sprintf( tempBuffer,
310     "%s\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t",
311     atoms[i]->getType(),
312     atoms[i]->getX(),
313     atoms[i]->getY(),
314     atoms[i]->getZ(),
315     atoms[i]->get_vx(),
316     atoms[i]->get_vy(),
317     atoms[i]->get_vz());
318     strcpy( writeLine, tempBuffer );
319    
320     if( atoms[i]->isDirectional() ){
321    
322     dAtom = (DirectionalAtom *)atoms[i];
323     dAtom->getQ( q );
324    
325     sprintf( tempBuffer,
326     "%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\n",
327     q[0],
328     q[1],
329     q[2],
330     q[3],
331     dAtom->getJx(),
332     dAtom->getJy(),
333     dAtom->getJz());
334     strcat( writeLine, tempBuffer );
335     }
336     else
337     strcat( writeLine, "0.0\t0.0\t0.0\t0.0\t0.0\t0.0\t0.0\n" );
338    
339     finalOut << writeLine;
340     }
341     finalOut.flush();
342 gezelter 415 finalOut.close();
343 mmeineke 377
344     #else // is_mpi
345 gezelter 415
346 gezelter 416 MPI::Status istatus;
347     int *AtomToProcMap = mpiSim->getAtomToProcMap();
348    
349 mmeineke 377 // write out header and node 0's coordinates
350 gezelter 415
351 mmeineke 377 if( worldRank == 0 ){
352     finalOut << mpiSim->getTotAtoms() << "\n";
353 gezelter 415
354 mmeineke 377 finalOut << entry_plug->box_x << "\t"
355 gezelter 415 << entry_plug->box_y << "\t"
356     << entry_plug->box_z << "\n";
357 mmeineke 377
358 gezelter 416 for (i = 0 ; i < mpiSim->getTotAtoms(); i++ ) {
359 gezelter 415 // Get the Node number which has this molecule:
360 mmeineke 377
361 gezelter 415 which_node = AtomToProcMap[i];
362    
363 gezelter 416 if (which_node == mpiSim->getMyNode()) {
364 gezelter 415
365     sprintf( tempBuffer,
366     "%s\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t",
367     atoms[i]->getType(),
368     atoms[i]->getX(),
369     atoms[i]->getY(),
370     atoms[i]->getZ(),
371     atoms[i]->get_vx(),
372     atoms[i]->get_vy(),
373     atoms[i]->get_vz());
374     strcpy( writeLine, tempBuffer );
375 mmeineke 377
376 gezelter 415 if( atoms[i]->isDirectional() ){
377 mmeineke 377
378 gezelter 415 dAtom = (DirectionalAtom *)atoms[i];
379     dAtom->getQ( q );
380 mmeineke 377
381 gezelter 415 sprintf( tempBuffer,
382     "%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\n",
383     q[0],
384     q[1],
385     q[2],
386     q[3],
387     dAtom->getJx(),
388     dAtom->getJy(),
389     dAtom->getJz());
390     strcat( writeLine, tempBuffer );
391     }
392     else
393     strcat( writeLine, "0.0\t0.0\t0.0\t0.0\t0.0\t0.0\t0.0\n" );
394    
395     } else {
396    
397     MPI::COMM_WORLD.Send(&i, 1, MPI_INT, which_node, TAKE_THIS_TAG);
398 gezelter 416 MPI::COMM_WORLD.Recv(writeLine, BUFFERSIZE, MPI_CHAR, which_node,
399 gezelter 415 TAKE_THIS_TAG, istatus);
400 mmeineke 377 }
401 gezelter 415
402 mmeineke 377 finalOut << writeLine;
403     }
404    
405 gezelter 415 // kill everyone off:
406 gezelter 416 game_over = -1;
407     for (j = 0; j < mpiSim->getNumberProcessors(); j++) {
408     MPI::COMM_WORLD.Send(&game_over, 1, MPI_INT, j, TAKE_THIS_TAG);
409 mmeineke 377 }
410    
411 gezelter 415 } else {
412    
413     done = 0;
414     while (!done) {
415 gezelter 416 MPI::COMM_WORLD.Recv(&which_atom, 1, MPI_INT, 0,
416     TAKE_THIS_TAG, istatus);
417 mmeineke 377
418 gezelter 416 if (which_atom == -1) {
419 gezelter 415 done=1;
420     continue;
421     } else {
422 mmeineke 377
423 gezelter 419 local_index=-1;
424     for (j=0; j < mpiSim->getMyNlocal(); j++) {
425     if (atoms[j]->getGlobalIndex() == which_atom) local_index = j;
426     }
427     if (local_index != -1) {
428    
429     //format the line
430     sprintf( tempBuffer,
431     "%s\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t",
432     atoms[local_index]->getType(),
433     atoms[local_index]->getX(),
434     atoms[local_index]->getY(),
435     atoms[local_index]->getZ(),
436     atoms[local_index]->get_vx(),
437     atoms[local_index]->get_vy(),
438     atoms[local_index]->get_vz()); // check here.
439     strcpy( writeLine, tempBuffer );
440 mmeineke 377
441 gezelter 419 if( atoms[local_index]->isDirectional() ){
442    
443     dAtom = (DirectionalAtom *)atoms[local_index];
444     dAtom->getQ( q );
445 mmeineke 377
446 gezelter 419 sprintf( tempBuffer,
447     "%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\n",
448     q[0],
449     q[1],
450     q[2],
451     q[3],
452     dAtom->getJx(),
453     dAtom->getJy(),
454     dAtom->getJz());
455     strcat( writeLine, tempBuffer );
456     }
457     else
458     strcat( writeLine, "0.0\t0.0\t0.0\t0.0\t0.0\t0.0\t0.0\n" );
459    
460     MPI::COMM_WORLD.Send(writeLine, BUFFERSIZE, MPI_CHAR, 0,
461     TAKE_THIS_TAG);
462     } else {
463     strcpy( writeLine, "ATOM NOT FOUND ON THIS PROCESSOR");
464     MPI::COMM_WORLD.Send(writeLine, BUFFERSIZE, MPI_CHAR, 0,
465     TAKE_THIS_TAG);
466     }
467 mmeineke 377 }
468     }
469 gezelter 419 }
470 gezelter 415 finalOut.flush();
471     sprintf( checkPointMsg,
472     "Sucessfully took a dump.\n");
473     MPIcheckPoint();
474 mmeineke 377
475 gezelter 415 if( worldRank == 0 ) finalOut.close();
476 mmeineke 377 #endif // is_mpi
477     }