ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/OOPSE/libmdtools/DumpWriter.cpp
Revision: 415
Committed: Wed Mar 26 22:24:49 2003 UTC (21 years, 3 months ago) by gezelter
File size: 10340 byte(s)
Log Message:
Making DumpWriter less dependent on sequence of atoms on the other
processors.  Node 0 now fires potatoes at other processors to get them
to send french fries back.

File Contents

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