ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/OOPSE/libmdtools/DumpWriter.cpp
Revision: 417
Committed: Thu Mar 27 01:49:45 2003 UTC (21 years, 3 months ago) by gezelter
File size: 10739 byte(s)
Log Message:
Fixes to fileio for MPI

File Contents

# Content
1 #include <cstring>
2 #include <iostream>
3 #include <fstream>
4
5 #ifdef IS_MPI
6 #include <mpi.h>
7 #include <mpi++.h>
8 #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 int i, j, which_node, done, game_over, which_atom;
69 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
123 MPI::Status istatus;
124 int *AtomToProcMap = mpiSim->getAtomToProcMap();
125
126 // write out header and node 0's coordinates
127
128 if( worldRank == 0 ){
129 outFile << mpiSim->getTotAtoms() << "\n";
130
131 outFile << currentTime << "\t"
132 << entry_plug->box_x << "\t"
133 << entry_plug->box_y << "\t"
134 << entry_plug->box_z << "\n";
135
136 for (i = 0 ; i < mpiSim->getTotAtoms(); i++ ) {
137 // Get the Node number which has this atom;
138
139 which_node = AtomToProcMap[i];
140
141 if (which_node == mpiSim->getMyNode()) {
142
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
154 if( atoms[i]->isDirectional() ){
155
156 dAtom = (DirectionalAtom *)atoms[i];
157 dAtom->getQ( q );
158
159 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 MPI::COMM_WORLD.Recv(writeLine, BUFFERSIZE, MPI_CHAR, which_node,
177 TAKE_THIS_TAG, istatus);
178 }
179
180 outFile << writeLine;
181 }
182
183 // kill everyone off:
184 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 }
188
189 } else {
190
191 done = 0;
192 while (!done) {
193 MPI::COMM_WORLD.Recv(&which_atom, 1, MPI_INT, 0,
194 TAKE_THIS_TAG, istatus);
195
196 if (which_atom == -1) {
197 done=1;
198 continue;
199 } else {
200
201 //format the line
202 sprintf( tempBuffer,
203 "%s\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t",
204 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 strcpy( writeLine, tempBuffer );
212
213 if( atoms[which_atom]->isDirectional() ){
214
215 dAtom = (DirectionalAtom *)atoms[which_atom];
216 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
232 MPI::COMM_WORLD.Send(writeLine, BUFFERSIZE, MPI_CHAR, 0,
233 TAKE_THIS_TAG);
234 }
235 }
236 }
237 outFile.flush();
238 sprintf( checkPointMsg,
239 "Sucessfully took a dump.\n");
240 MPIcheckPoint();
241 #endif // is_mpi
242 }
243
244 void DumpWriter::writeFinal(){
245
246 char finalName[500];
247 ofstream finalOut;
248
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
259
260 #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
286 #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
294 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 finalOut.close();
330
331 #else // is_mpi
332
333 MPI::Status istatus;
334 int *AtomToProcMap = mpiSim->getAtomToProcMap();
335
336 // write out header and node 0's coordinates
337
338 if( worldRank == 0 ){
339 finalOut << mpiSim->getTotAtoms() << "\n";
340
341 finalOut << entry_plug->box_x << "\t"
342 << entry_plug->box_y << "\t"
343 << entry_plug->box_z << "\n";
344
345 for (i = 0 ; i < mpiSim->getTotAtoms(); i++ ) {
346 // Get the Node number which has this molecule:
347
348 which_node = AtomToProcMap[i];
349
350 if (which_node == mpiSim->getMyNode()) {
351
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
363 if( atoms[i]->isDirectional() ){
364
365 dAtom = (DirectionalAtom *)atoms[i];
366 dAtom->getQ( q );
367
368 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 MPI::COMM_WORLD.Recv(writeLine, BUFFERSIZE, MPI_CHAR, which_node,
386 TAKE_THIS_TAG, istatus);
387 }
388
389 finalOut << writeLine;
390 }
391
392 // kill everyone off:
393 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 }
397
398 } else {
399
400 done = 0;
401 while (!done) {
402 MPI::COMM_WORLD.Recv(&which_atom, 1, MPI_INT, 0,
403 TAKE_THIS_TAG, istatus);
404
405 if (which_atom == -1) {
406 done=1;
407 continue;
408 } else {
409
410 //format the line
411 sprintf( tempBuffer,
412 "%s\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t",
413 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 strcpy( writeLine, tempBuffer );
421
422 if( atoms[which_atom]->isDirectional() ){
423
424 dAtom = (DirectionalAtom *)atoms[which_atom];
425 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
441 MPI::COMM_WORLD.Send(writeLine, BUFFERSIZE, MPI_CHAR, 0,
442 TAKE_THIS_TAG);
443 }
444 }
445 }
446 finalOut.flush();
447 sprintf( checkPointMsg,
448 "Sucessfully took a dump.\n");
449 MPIcheckPoint();
450
451 if( worldRank == 0 ) finalOut.close();
452 #endif // is_mpi
453 }