ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/mdtools/md_code/DumpWriter.cpp
Revision: 215
Committed: Thu Dec 19 21:59:51 2002 UTC (21 years, 6 months ago) by chuckv
File size: 11857 byte(s)
Log Message:
+ added lennard-jones force module and corresponding class.
+ created forceFactory directory.

File Contents

# User Rev Content
1 mmeineke 10 #include <cstring>
2     #include <iostream>
3     #include <fstream>
4 chuckv 215
5     #ifdef IS_MPI
6 mmeineke 214 #include <mpi.h>
7 chuckv 215 #include "mpiSimulation.hpp"
8     #endif //is_mpi
9 mmeineke 10
10     #include "ReadWrite.hpp"
11 mmeineke 162 #include "simError.h"
12 mmeineke 10
13    
14 chuckv 215
15    
16    
17 mmeineke 10 DumpWriter::DumpWriter( SimInfo* the_entry_plug ){
18    
19 mmeineke 184 #ifdef IS_MPI
20 mmeineke 189 if(worldRank == 0 ){
21 mmeineke 184 #endif // is_mpi
22    
23     entry_plug = the_entry_plug;
24    
25     strcpy( outName, entry_plug->sampleName );
26    
27     std::cerr << "Opening " << outName << " for dumping.\n";
28    
29     outFile.open(outName, ios::out | ios::trunc );
30    
31     if( !outFile ){
32    
33     sprintf( painCave.errMsg,
34     "Could not open \"%s\" for dump output.\n",
35     outName);
36     painCave.isFatal = 1;
37     simError();
38     }
39 mmeineke 10
40 mmeineke 184 //outFile.setf( ios::scientific );
41    
42     #ifdef IS_MPI
43 mmeineke 10 }
44 mmeineke 184 #endif // is_mpi
45 mmeineke 10 }
46    
47     DumpWriter::~DumpWriter( ){
48    
49 mmeineke 184 #ifdef IS_MPI
50 mmeineke 189 if(worldRank == 0 ){
51 mmeineke 184 #endif // is_mpi
52    
53     outFile.close();
54    
55     #ifdef IS_MPI
56     }
57     #endif // is_mpi
58 mmeineke 10 }
59    
60     void DumpWriter::writeDump( double currentTime ){
61 mmeineke 214
62     const int BUFFERSIZE = 2000;
63     char tempBuffer[500];
64     char writeLine[BUFFERSIZE];
65 mmeineke 10
66 mmeineke 214 int i;
67     double q[4];
68     DirectionalAtom* dAtom;
69     int nAtoms = entry_plug->n_atoms;
70     Atom** atoms = entry_plug->atoms;
71    
72 mmeineke 10
73 mmeineke 214 #ifndef IS_MPI
74    
75     outFile << nAtoms << "\n";
76    
77     outFile << currentTime << "\t"
78     << entry_plug->box_x << "\t"
79     << entry_plug->box_y << "\t"
80     << entry_plug->box_z << "\n";
81    
82     for( i=0; i<nAtoms; i++ ){
83    
84     sprintf( tempBuffer,
85     "%s\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t",
86     atoms[i]->getType(),
87     atoms[i]->getX(),
88     atoms[i]->getY(),
89     atoms[i]->getZ(),
90     atoms[i]->get_vx(),
91     atoms[i]->get_vy(),
92     atoms[i]->get_vz());
93     strcpy( writeLine, tempBuffer );
94 mmeineke 10
95 mmeineke 214 if( atoms[i]->isDirectional() ){
96    
97     dAtom = (DirectionalAtom *)atoms[i];
98     dAtom->getQ( q );
99    
100     sprintf( tempBuffer,
101     "%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\n",
102     q[0],
103     q[1],
104     q[2],
105     q[3],
106     dAtom->getJx(),
107     dAtom->getJy(),
108     dAtom->getJz());
109     strcat( writeLine, tempBuffer );
110     }
111     else
112     strcat( writeLine, "0.0\t0.0\t0.0\t0.0\t0.0\t0.0\t0.0\n" );
113    
114     outFile << writeLine;
115     }
116     outFile.flush();
117    
118     #else // is_mpi
119    
120     int masterIndex;
121     int nodeAtomsStart;
122     int nodeAtomsEnd;
123     int mpiErr;
124     int sendError;
125     int procIndex;
126 mmeineke 184
127 mmeineke 214 MPI_Status istatus[MPI_STATUS_SIZE];
128    
129 mmeineke 184
130 mmeineke 214 // write out header and node 0's coordinates
131    
132     if( worldRank == 0 ){
133     outFile << entry_plug->mpiSim->getTotAtoms() << "\n";
134    
135 mmeineke 184 outFile << currentTime << "\t"
136     << entry_plug->box_x << "\t"
137     << entry_plug->box_y << "\t"
138     << entry_plug->box_z << "\n";
139 mmeineke 214
140     masterIndex = 0;
141 mmeineke 184 for( i=0; i<nAtoms; i++ ){
142 mmeineke 10
143 mmeineke 214 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 mmeineke 184 if( atoms[i]->isDirectional() ){
155 mmeineke 214
156 mmeineke 184 dAtom = (DirectionalAtom *)atoms[i];
157     dAtom->getQ( q );
158 mmeineke 214
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 mmeineke 184
173 mmeineke 214 outfile << writeLine;
174     masterIndex++;
175     }
176     outFile.flush();
177     }
178    
179     for (procIndex = 1; procIndex < entry_plug->mpiSim->getNumberProcessors();
180     procIndex++){
181    
182     if( worldRank == 0 ){
183    
184     mpiErr = MPI_Recv(&nodeAtomsStart,1,MPI_INT,procIndex,
185     MPI_ANY_TAG,MPI_COMM_WORLD,istatus);
186    
187     mpiErr = MPI_Recv(&nodeAtomsEnd,1,MPI_INT,procIndex,
188     MPI_ANY_TAG,MPI_COMM_WORLD, istatus);
189    
190     // Make sure where node 0 is writing to, matches where the
191     // receiving node expects it to be.
192    
193     if (masterIndex != nodeAtomsStart){
194     sendError = 1;
195     mpiErr = MPI_Send(&sendError,1,MPI_INT,procIndex,MPI_ANY_TAG,
196     MPI_COMM_WORLD);
197     sprintf(painCave.errMsg,
198     "DumpWriter error: atoms start index (%d) for "
199     "node %d not equal to master index (%d)",
200     nodeAtomsStart,procIndex,masterIndex );
201     painCave.isFatal = 1;
202     simError();
203 mmeineke 184 }
204 mmeineke 214
205     sendError = 0;
206     mpiErr = MPI_Send(&sendError,1,MPI_INT,procIndex,MPI_ANY_TAG,
207     MPI_COMM_WORLD);
208    
209     // recieve the nodes writeLines
210    
211     for ( i = nodeAtomStart; i <= nodeAtomEnd, i++){
212    
213     mpiErr = MPI_Recv(&read_buffer,BUFFERSIZE,MPI_CHAR,procIndex,
214     MPI_ANY_TAG,MPI_COMM_WORLD,istatus );
215    
216     outFile << writeLine;
217     masterIndex++;
218 mmeineke 184 }
219 mmeineke 10 }
220 mmeineke 184
221 mmeineke 214 else if( worldRank == procIndex ){
222    
223     nodeAtomStart = entry_plug->mpiSim->getMyAtomStart();
224     nodeAtomEnd = entry_plug->mpiSim->getMyAtomEnd();
225    
226     mpiErr = MPI_Send(&nodeAtomsStart,1,MPI_INT,0,MPI_ANY_TAG,
227     MPI_COMM_WORLD);
228     mpiErr = MPI_Send(&nodeAtomsEnd,1,MPI_INT,0,MPI_ANY_TAG,
229     MPI_COMM_WORLD);
230    
231     mpiErr = MPI_Recv(&sendError,1,MPI_INT,0,MPI_ANY_TAG,
232     MPI_COMM_WORLD, istatus);
233     if (sendError) mpiCheckpoint();
234    
235     // send current node's configuration line by line.
236    
237     for( i=0; i<nAtoms; i++ ){
238    
239     sprintf( tempBuffer,
240     "%s\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t",
241     atoms[i]->getType(),
242     atoms[i]->getX(),
243     atoms[i]->getY(),
244     atoms[i]->getZ(),
245     atoms[i]->get_vx(),
246     atoms[i]->get_vy(),
247     atoms[i]->get_vz());
248     strcpy( writeLine, tempBuffer );
249    
250     if( atoms[i]->isDirectional() ){
251    
252     dAtom = (DirectionalAtom *)atoms[i];
253     dAtom->getQ( q );
254    
255     sprintf( tempBuffer,
256     "%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\n",
257     q[0],
258     q[1],
259     q[2],
260     q[3],
261     dAtom->getJx(),
262     dAtom->getJy(),
263     dAtom->getJz());
264     strcat( writeLine, tempBuffer );
265     }
266     else
267     strcat( writeLine, "0.0\t0.0\t0.0\t0.0\t0.0\t0.0\t0.0\n" );
268    
269     mpiErr = MPI_Send(writeLine,BUFFERSIZE,MPI_CHAR,0,MPI_ANY_TAG,
270     MPI_COMM_WORLD);
271     }
272     }
273    
274     sprintf(checkPointMsg,"Node %d sent dump configuration.",
275     procIndex);
276     mpiCheckPoint();
277 mmeineke 10 }
278 mmeineke 214
279 mmeineke 184 #endif // is_mpi
280 mmeineke 10 }
281    
282    
283 mmeineke 184
284 mmeineke 10 void DumpWriter::writeFinal(){
285    
286 mmeineke 214
287     const int BUFFERSIZE = 2000;
288     char tempBuffer[500];
289     char writeLine[BUFFERSIZE];
290    
291     char finalName[500];
292    
293     int i;
294     double q[4];
295     DirectionalAtom* dAtom;
296     int nAtoms = entry_plug->n_atoms;
297     Atom** atoms = entry_plug->atoms;
298    
299     ofstream finalOut;
300    
301 mmeineke 184 #ifdef IS_MPI
302 mmeineke 189 if(worldRank == 0 ){
303 mmeineke 184 #endif // is_mpi
304 mmeineke 214
305 mmeineke 184 strcpy( finalName, entry_plug->finalName );
306 mmeineke 214
307     finalOut.open( finalName, ios::out | ios::trunc );
308 mmeineke 184 if( !finalOut ){
309     sprintf( painCave.errMsg,
310     "Could not open \"%s\" for final dump output.\n",
311     finalName );
312     painCave.isFatal = 1;
313     simError();
314     }
315 mmeineke 10
316 mmeineke 184 // finalOut.setf( ios::scientific );
317    
318 mmeineke 214 #ifdef IS_MPI
319     }
320    
321     sprintf(checkPointMsg,"Opened file for final configuration\n",procIndex);
322     mpiCheckPoint();
323    
324     #endif //is_mpi
325    
326 mmeineke 184
327 mmeineke 214
328     #ifndef IS_MPI
329 mmeineke 184
330 mmeineke 214 finalOut << nAtoms << "\n";
331 mmeineke 184
332 mmeineke 214 finalOut << currentTime << "\t"
333     << entry_plug->box_x << "\t"
334     << entry_plug->box_y << "\t"
335     << entry_plug->box_z << "\n";
336 mmeineke 184
337 mmeineke 214 for( i=0; i<nAtoms; i++ ){
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     finalOut << writeLine;
370     }
371     finalOut.flush();
372    
373     #else // is_mpi
374    
375     int masterIndex;
376     int nodeAtomsStart;
377     int nodeAtomsEnd;
378     int mpiErr;
379     int sendError;
380     int procIndex;
381 mmeineke 184
382 mmeineke 214 MPI_Status istatus[MPI_STATUS_SIZE];
383    
384    
385     // write out header and node 0's coordinates
386    
387     if( worldRank == 0 ){
388     finalOut << entry_plug->mpiSim->getTotAtoms() << "\n";
389    
390     finalOut << currentTime << "\t"
391     << entry_plug->box_x << "\t"
392     << entry_plug->box_y << "\t"
393     << entry_plug->box_z << "\n";
394    
395     masterIndex = 0;
396 mmeineke 184 for( i=0; i<nAtoms; i++ ){
397 mmeineke 10
398 mmeineke 214 sprintf( tempBuffer,
399     "%s\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t",
400     atoms[i]->getType(),
401     atoms[i]->getX(),
402     atoms[i]->getY(),
403     atoms[i]->getZ(),
404     atoms[i]->get_vx(),
405     atoms[i]->get_vy(),
406     atoms[i]->get_vz());
407     strcpy( writeLine, tempBuffer );
408    
409 mmeineke 184 if( atoms[i]->isDirectional() ){
410 mmeineke 214
411 mmeineke 184 dAtom = (DirectionalAtom *)atoms[i];
412     dAtom->getQ( q );
413 mmeineke 214
414     sprintf( tempBuffer,
415     "%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\n",
416     q[0],
417     q[1],
418     q[2],
419     q[3],
420     dAtom->getJx(),
421     dAtom->getJy(),
422     dAtom->getJz());
423     strcat( writeLine, tempBuffer );
424     }
425     else
426     strcat( writeLine, "0.0\t0.0\t0.0\t0.0\t0.0\t0.0\t0.0\n" );
427 mmeineke 184
428 mmeineke 214 outfile << writeLine;
429     masterIndex++;
430     }
431     finalOut.flush();
432     }
433    
434     for (procIndex = 1; procIndex < entry_plug->mpiSim->getNumberProcessors();
435     procIndex++){
436    
437     if( worldRank == 0 ){
438    
439     mpiErr = MPI_Recv(&nodeAtomsStart,1,MPI_INT,procIndex,
440     MPI_ANY_TAG,MPI_COMM_WORLD,istatus);
441    
442     mpiErr = MPI_Recv(&nodeAtomsEnd,1,MPI_INT,procIndex,
443     MPI_ANY_TAG,MPI_COMM_WORLD, istatus);
444    
445     // Make sure where node 0 is writing to, matches where the
446     // receiving node expects it to be.
447    
448     if (masterIndex != nodeAtomsStart){
449     sendError = 1;
450     mpiErr = MPI_Send(&sendError,1,MPI_INT,procIndex,MPI_ANY_TAG,
451     MPI_COMM_WORLD);
452     sprintf(painCave.errMsg,
453     "DumpWriter error: atoms start index (%d) for "
454     "node %d not equal to master index (%d)",
455     nodeAtomsStart,procIndex,masterIndex );
456     painCave.isFatal = 1;
457     simError();
458 mmeineke 184 }
459 mmeineke 214
460     sendError = 0;
461     mpiErr = MPI_Send(&sendError,1,MPI_INT,procIndex,MPI_ANY_TAG,
462     MPI_COMM_WORLD);
463    
464     // recieve the nodes writeLines
465    
466     for ( i = nodeAtomStart; i <= nodeAtomEnd, i++){
467    
468     mpiErr = MPI_Recv(&read_buffer,BUFFERSIZE,MPI_CHAR,procIndex,
469     MPI_ANY_TAG,MPI_COMM_WORLD,istatus );
470    
471     finalOut << writeLine;
472     masterIndex++;
473 mmeineke 184 }
474 mmeineke 214
475     finalOut.flush();
476 mmeineke 10 }
477 mmeineke 214
478     else if( worldRank == procIndex ){
479    
480     nodeAtomStart = entry_plug->mpiSim->getMyAtomStart();
481     nodeAtomEnd = entry_plug->mpiSim->getMyAtomEnd();
482    
483     mpiErr = MPI_Send(&nodeAtomsStart,1,MPI_INT,0,MPI_ANY_TAG,
484     MPI_COMM_WORLD);
485     mpiErr = MPI_Send(&nodeAtomsEnd,1,MPI_INT,0,MPI_ANY_TAG,
486     MPI_COMM_WORLD);
487    
488     mpiErr = MPI_Recv(&sendError,1,MPI_INT,0,MPI_ANY_TAG,
489     MPI_COMM_WORLD, istatus);
490     if (sendError) mpiCheckpoint();
491    
492     // send current node's configuration line by line.
493    
494     for( i=0; i<nAtoms; i++ ){
495    
496     sprintf( tempBuffer,
497     "%s\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t",
498     atoms[i]->getType(),
499     atoms[i]->getX(),
500     atoms[i]->getY(),
501     atoms[i]->getZ(),
502     atoms[i]->get_vx(),
503     atoms[i]->get_vy(),
504     atoms[i]->get_vz());
505     strcpy( writeLine, tempBuffer );
506    
507     if( atoms[i]->isDirectional() ){
508    
509     dAtom = (DirectionalAtom *)atoms[i];
510     dAtom->getQ( q );
511    
512     sprintf( tempBuffer,
513     "%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\n",
514     q[0],
515     q[1],
516     q[2],
517     q[3],
518     dAtom->getJx(),
519     dAtom->getJy(),
520     dAtom->getJz());
521     strcat( writeLine, tempBuffer );
522     }
523     else
524     strcat( writeLine, "0.0\t0.0\t0.0\t0.0\t0.0\t0.0\t0.0\n" );
525    
526     mpiErr = MPI_Send(writeLine,BUFFERSIZE,MPI_CHAR,0,MPI_ANY_TAG,
527     MPI_COMM_WORLD);
528     }
529     }
530    
531     sprintf(checkPointMsg,"Node %d sent dump configuration.",
532     procIndex);
533     mpiCheckPoint();
534     }
535    
536     if( worldRank == 0 ) finalOut.close();
537    
538 mmeineke 184
539     #endif // is_mpi
540 mmeineke 10 }