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 1 |
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, local_index; |
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 |
outFile.flush(); |
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 |
sprintf( tempBuffer, |
143 |
"%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 |
|
153 |
if( atoms[i]->isDirectional() ){ |
154 |
|
155 |
dAtom = (DirectionalAtom *)atoms[i]; |
156 |
dAtom->getQ( q ); |
157 |
|
158 |
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 |
std::cerr << "node 0: sending node " << which_node << " request for atom " << i << "\n"; |
175 |
MPI::COMM_WORLD.Send(&i, 1, MPI_INT, which_node, TAKE_THIS_TAG); |
176 |
std::cerr << "node 0: sent!\n"; |
177 |
MPI::COMM_WORLD.Recv(writeLine, BUFFERSIZE, MPI_CHAR, which_node, |
178 |
TAKE_THIS_TAG, istatus); |
179 |
std::cerr << "node 0: got this line: " << writeLine; |
180 |
} |
181 |
|
182 |
outFile << writeLine; |
183 |
outFile.flush(); |
184 |
} |
185 |
|
186 |
// kill everyone off: |
187 |
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 |
} |
191 |
|
192 |
} else { |
193 |
|
194 |
done = 0; |
195 |
while (!done) { |
196 |
std::cerr << "node: " << mpiSim->getMyNode() << " Waiting for receive \n"; |
197 |
MPI::COMM_WORLD.Recv(&which_atom, 1, MPI_INT, 0, |
198 |
TAKE_THIS_TAG, istatus); |
199 |
std::cerr << "node: " << mpiSim->getMyNode() << " got request for atom " << which_atom << "\n"; |
200 |
if (which_atom == -1) { |
201 |
done=1; |
202 |
continue; |
203 |
} else { |
204 |
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 |
|
221 |
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 |
std::cerr << "node: " << mpiSim->getMyNode() << " sending this line" << writeLine; |
240 |
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 |
} |
248 |
} |
249 |
} |
250 |
outFile.flush(); |
251 |
sprintf( checkPointMsg, |
252 |
"Sucessfully took a dump.\n"); |
253 |
MPIcheckPoint(); |
254 |
#endif // is_mpi |
255 |
} |
256 |
|
257 |
void DumpWriter::writeFinal(){ |
258 |
|
259 |
char finalName[500]; |
260 |
ofstream finalOut; |
261 |
|
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 |
int i, j, which_node, done, game_over, which_atom, local_index; |
271 |
|
272 |
|
273 |
#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 |
|
299 |
#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 |
|
307 |
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 |
finalOut.close(); |
343 |
|
344 |
#else // is_mpi |
345 |
|
346 |
MPI::Status istatus; |
347 |
int *AtomToProcMap = mpiSim->getAtomToProcMap(); |
348 |
|
349 |
// write out header and node 0's coordinates |
350 |
|
351 |
if( worldRank == 0 ){ |
352 |
finalOut << mpiSim->getTotAtoms() << "\n"; |
353 |
|
354 |
finalOut << entry_plug->box_x << "\t" |
355 |
<< entry_plug->box_y << "\t" |
356 |
<< entry_plug->box_z << "\n"; |
357 |
|
358 |
for (i = 0 ; i < mpiSim->getTotAtoms(); i++ ) { |
359 |
// Get the Node number which has this molecule: |
360 |
|
361 |
which_node = AtomToProcMap[i]; |
362 |
|
363 |
if (which_node == mpiSim->getMyNode()) { |
364 |
|
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 |
|
376 |
if( atoms[i]->isDirectional() ){ |
377 |
|
378 |
dAtom = (DirectionalAtom *)atoms[i]; |
379 |
dAtom->getQ( q ); |
380 |
|
381 |
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 |
MPI::COMM_WORLD.Recv(writeLine, BUFFERSIZE, MPI_CHAR, which_node, |
399 |
TAKE_THIS_TAG, istatus); |
400 |
} |
401 |
|
402 |
finalOut << writeLine; |
403 |
} |
404 |
|
405 |
// kill everyone off: |
406 |
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 |
} |
410 |
|
411 |
} else { |
412 |
|
413 |
done = 0; |
414 |
while (!done) { |
415 |
MPI::COMM_WORLD.Recv(&which_atom, 1, MPI_INT, 0, |
416 |
TAKE_THIS_TAG, istatus); |
417 |
|
418 |
if (which_atom == -1) { |
419 |
done=1; |
420 |
continue; |
421 |
} else { |
422 |
|
423 |
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 |
|
441 |
if( atoms[local_index]->isDirectional() ){ |
442 |
|
443 |
dAtom = (DirectionalAtom *)atoms[local_index]; |
444 |
dAtom->getQ( q ); |
445 |
|
446 |
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 |
} |
468 |
} |
469 |
} |
470 |
finalOut.flush(); |
471 |
sprintf( checkPointMsg, |
472 |
"Sucessfully took a dump.\n"); |
473 |
MPIcheckPoint(); |
474 |
|
475 |
if( worldRank == 0 ) finalOut.close(); |
476 |
#endif // is_mpi |
477 |
} |