1 |
gezelter |
507 |
/* |
2 |
gezelter |
1390 |
* Copyright (c) 2009 The University of Notre Dame. All Rights Reserved. |
3 |
gezelter |
246 |
* |
4 |
|
|
* The University of Notre Dame grants you ("Licensee") a |
5 |
|
|
* non-exclusive, royalty free, license to use, modify and |
6 |
|
|
* redistribute this software in source and binary code form, provided |
7 |
|
|
* that the following conditions are met: |
8 |
|
|
* |
9 |
gezelter |
1390 |
* 1. Redistributions of source code must retain the above copyright |
10 |
gezelter |
246 |
* notice, this list of conditions and the following disclaimer. |
11 |
|
|
* |
12 |
gezelter |
1390 |
* 2. Redistributions in binary form must reproduce the above copyright |
13 |
gezelter |
246 |
* notice, this list of conditions and the following disclaimer in the |
14 |
|
|
* documentation and/or other materials provided with the |
15 |
|
|
* distribution. |
16 |
|
|
* |
17 |
|
|
* This software is provided "AS IS," without a warranty of any |
18 |
|
|
* kind. All express or implied conditions, representations and |
19 |
|
|
* warranties, including any implied warranty of merchantability, |
20 |
|
|
* fitness for a particular purpose or non-infringement, are hereby |
21 |
|
|
* excluded. The University of Notre Dame and its licensors shall not |
22 |
|
|
* be liable for any damages suffered by licensee as a result of |
23 |
|
|
* using, modifying or distributing the software or its |
24 |
|
|
* derivatives. In no event will the University of Notre Dame or its |
25 |
|
|
* licensors be liable for any lost revenue, profit or data, or for |
26 |
|
|
* direct, indirect, special, consequential, incidental or punitive |
27 |
|
|
* damages, however caused and regardless of the theory of liability, |
28 |
|
|
* arising out of the use of or inability to use software, even if the |
29 |
|
|
* University of Notre Dame has been advised of the possibility of |
30 |
|
|
* such damages. |
31 |
gezelter |
1390 |
* |
32 |
|
|
* SUPPORT OPEN SCIENCE! If you use OpenMD or its source code in your |
33 |
|
|
* research, please cite the appropriate papers when you publish your |
34 |
|
|
* work. Good starting points are: |
35 |
|
|
* |
36 |
|
|
* [1] Meineke, et al., J. Comp. Chem. 26, 252-271 (2005). |
37 |
|
|
* [2] Fennell & Gezelter, J. Chem. Phys. 124, 234104 (2006). |
38 |
gezelter |
1879 |
* [3] Sun, Lin & Gezelter, J. Chem. Phys. 128, 234107 (2008). |
39 |
gezelter |
1782 |
* [4] Kuang & Gezelter, J. Chem. Phys. 133, 164101 (2010). |
40 |
|
|
* [5] Vardeman, Stocker & Gezelter, J. Chem. Theory Comput. 7, 834 (2011). |
41 |
gezelter |
246 |
*/ |
42 |
gezelter |
1938 |
|
43 |
|
|
#include "config.h" |
44 |
|
|
|
45 |
|
|
#ifdef IS_MPI |
46 |
|
|
#include <mpi.h> |
47 |
|
|
#endif |
48 |
gezelter |
246 |
|
49 |
|
|
#include "io/DumpWriter.hpp" |
50 |
|
|
#include "primitives/Molecule.hpp" |
51 |
|
|
#include "utils/simError.h" |
52 |
tim |
376 |
#include "io/basic_teebuf.hpp" |
53 |
gezelter |
1782 |
#ifdef HAVE_ZLIB |
54 |
tim |
615 |
#include "io/gzstream.hpp" |
55 |
gezelter |
1782 |
#endif |
56 |
tim |
615 |
#include "io/Globals.hpp" |
57 |
|
|
|
58 |
gezelter |
1782 |
#ifdef _MSC_VER |
59 |
|
|
#define isnan(x) _isnan((x)) |
60 |
|
|
#define isinf(x) (!_finite(x) && !_isnan(x)) |
61 |
|
|
#endif |
62 |
gezelter |
1437 |
|
63 |
|
|
using namespace std; |
64 |
gezelter |
1390 |
namespace OpenMD { |
65 |
gezelter |
2 |
|
66 |
gezelter |
507 |
DumpWriter::DumpWriter(SimInfo* info) |
67 |
|
|
: info_(info), filename_(info->getDumpFileName()), eorFilename_(info->getFinalConfigFileName()){ |
68 |
tim |
615 |
|
69 |
|
|
Globals* simParams = info->getSimParams(); |
70 |
gezelter |
1782 |
needCompression_ = simParams->getCompressDumpFile(); |
71 |
|
|
needForceVector_ = simParams->getOutputForceVector(); |
72 |
|
|
needParticlePot_ = simParams->getOutputParticlePotential(); |
73 |
|
|
needFlucQ_ = simParams->getOutputFluctuatingCharges(); |
74 |
|
|
needElectricField_ = simParams->getOutputElectricField(); |
75 |
|
|
|
76 |
|
|
if (needParticlePot_ || needFlucQ_ || needElectricField_) { |
77 |
|
|
doSiteData_ = true; |
78 |
|
|
} else { |
79 |
|
|
doSiteData_ = false; |
80 |
|
|
} |
81 |
|
|
|
82 |
chuckv |
791 |
createDumpFile_ = true; |
83 |
tim |
619 |
#ifdef HAVE_LIBZ |
84 |
tim |
615 |
if (needCompression_) { |
85 |
tim |
1024 |
filename_ += ".gz"; |
86 |
|
|
eorFilename_ += ".gz"; |
87 |
tim |
615 |
} |
88 |
tim |
619 |
#endif |
89 |
tim |
615 |
|
90 |
tim |
376 |
#ifdef IS_MPI |
91 |
|
|
|
92 |
tim |
1024 |
if (worldRank == 0) { |
93 |
tim |
376 |
#endif // is_mpi |
94 |
chuckv |
791 |
|
95 |
tim |
1024 |
dumpFile_ = createOStream(filename_); |
96 |
tim |
615 |
|
97 |
tim |
1024 |
if (!dumpFile_) { |
98 |
|
|
sprintf(painCave.errMsg, "Could not open \"%s\" for dump output.\n", |
99 |
|
|
filename_.c_str()); |
100 |
|
|
painCave.isFatal = 1; |
101 |
|
|
simError(); |
102 |
|
|
} |
103 |
tim |
376 |
|
104 |
|
|
#ifdef IS_MPI |
105 |
|
|
|
106 |
tim |
1024 |
} |
107 |
tim |
376 |
|
108 |
|
|
#endif // is_mpi |
109 |
|
|
|
110 |
tim |
1024 |
} |
111 |
tim |
376 |
|
112 |
|
|
|
113 |
gezelter |
507 |
DumpWriter::DumpWriter(SimInfo* info, const std::string& filename) |
114 |
|
|
: info_(info), filename_(filename){ |
115 |
tim |
615 |
|
116 |
|
|
Globals* simParams = info->getSimParams(); |
117 |
|
|
eorFilename_ = filename_.substr(0, filename_.rfind(".")) + ".eor"; |
118 |
|
|
|
119 |
gezelter |
1782 |
needCompression_ = simParams->getCompressDumpFile(); |
120 |
|
|
needForceVector_ = simParams->getOutputForceVector(); |
121 |
|
|
needParticlePot_ = simParams->getOutputParticlePotential(); |
122 |
|
|
needFlucQ_ = simParams->getOutputFluctuatingCharges(); |
123 |
|
|
needElectricField_ = simParams->getOutputElectricField(); |
124 |
|
|
|
125 |
|
|
if (needParticlePot_ || needFlucQ_ || needElectricField_) { |
126 |
|
|
doSiteData_ = true; |
127 |
|
|
} else { |
128 |
|
|
doSiteData_ = false; |
129 |
|
|
} |
130 |
|
|
|
131 |
chuckv |
791 |
createDumpFile_ = true; |
132 |
tim |
619 |
#ifdef HAVE_LIBZ |
133 |
tim |
615 |
if (needCompression_) { |
134 |
tim |
1024 |
filename_ += ".gz"; |
135 |
|
|
eorFilename_ += ".gz"; |
136 |
tim |
615 |
} |
137 |
tim |
619 |
#endif |
138 |
tim |
615 |
|
139 |
gezelter |
246 |
#ifdef IS_MPI |
140 |
gezelter |
2 |
|
141 |
tim |
1024 |
if (worldRank == 0) { |
142 |
gezelter |
246 |
#endif // is_mpi |
143 |
gezelter |
2 |
|
144 |
chuckv |
791 |
|
145 |
tim |
1024 |
dumpFile_ = createOStream(filename_); |
146 |
tim |
615 |
|
147 |
tim |
1024 |
if (!dumpFile_) { |
148 |
|
|
sprintf(painCave.errMsg, "Could not open \"%s\" for dump output.\n", |
149 |
|
|
filename_.c_str()); |
150 |
|
|
painCave.isFatal = 1; |
151 |
|
|
simError(); |
152 |
|
|
} |
153 |
gezelter |
2 |
|
154 |
|
|
#ifdef IS_MPI |
155 |
|
|
|
156 |
tim |
1024 |
} |
157 |
gezelter |
2 |
|
158 |
|
|
#endif // is_mpi |
159 |
gezelter |
246 |
|
160 |
tim |
1024 |
} |
161 |
chuckv |
791 |
|
162 |
|
|
DumpWriter::DumpWriter(SimInfo* info, const std::string& filename, bool writeDumpFile) |
163 |
tim |
1024 |
: info_(info), filename_(filename){ |
164 |
chuckv |
791 |
|
165 |
|
|
Globals* simParams = info->getSimParams(); |
166 |
|
|
eorFilename_ = filename_.substr(0, filename_.rfind(".")) + ".eor"; |
167 |
|
|
|
168 |
gezelter |
1782 |
needCompression_ = simParams->getCompressDumpFile(); |
169 |
|
|
needForceVector_ = simParams->getOutputForceVector(); |
170 |
|
|
needParticlePot_ = simParams->getOutputParticlePotential(); |
171 |
|
|
needFlucQ_ = simParams->getOutputFluctuatingCharges(); |
172 |
|
|
needElectricField_ = simParams->getOutputElectricField(); |
173 |
|
|
|
174 |
|
|
if (needParticlePot_ || needFlucQ_ || needElectricField_) { |
175 |
|
|
doSiteData_ = true; |
176 |
|
|
} else { |
177 |
|
|
doSiteData_ = false; |
178 |
|
|
} |
179 |
|
|
|
180 |
chuckv |
791 |
#ifdef HAVE_LIBZ |
181 |
|
|
if (needCompression_) { |
182 |
|
|
filename_ += ".gz"; |
183 |
|
|
eorFilename_ += ".gz"; |
184 |
|
|
} |
185 |
|
|
#endif |
186 |
|
|
|
187 |
|
|
#ifdef IS_MPI |
188 |
|
|
|
189 |
|
|
if (worldRank == 0) { |
190 |
|
|
#endif // is_mpi |
191 |
|
|
|
192 |
|
|
createDumpFile_ = writeDumpFile; |
193 |
|
|
if (createDumpFile_) { |
194 |
|
|
dumpFile_ = createOStream(filename_); |
195 |
|
|
|
196 |
|
|
if (!dumpFile_) { |
197 |
|
|
sprintf(painCave.errMsg, "Could not open \"%s\" for dump output.\n", |
198 |
|
|
filename_.c_str()); |
199 |
|
|
painCave.isFatal = 1; |
200 |
|
|
simError(); |
201 |
|
|
} |
202 |
|
|
} |
203 |
|
|
#ifdef IS_MPI |
204 |
|
|
|
205 |
|
|
} |
206 |
tim |
1024 |
|
207 |
chuckv |
791 |
|
208 |
|
|
#endif // is_mpi |
209 |
|
|
|
210 |
|
|
} |
211 |
gezelter |
2 |
|
212 |
gezelter |
507 |
DumpWriter::~DumpWriter() { |
213 |
gezelter |
2 |
|
214 |
|
|
#ifdef IS_MPI |
215 |
gezelter |
246 |
|
216 |
|
|
if (worldRank == 0) { |
217 |
gezelter |
2 |
#endif // is_mpi |
218 |
chuckv |
791 |
if (createDumpFile_){ |
219 |
tim |
1024 |
writeClosing(*dumpFile_); |
220 |
chuckv |
791 |
delete dumpFile_; |
221 |
|
|
} |
222 |
gezelter |
2 |
#ifdef IS_MPI |
223 |
gezelter |
246 |
|
224 |
|
|
} |
225 |
|
|
|
226 |
gezelter |
2 |
#endif // is_mpi |
227 |
gezelter |
246 |
|
228 |
gezelter |
507 |
} |
229 |
gezelter |
2 |
|
230 |
tim |
1024 |
void DumpWriter::writeFrameProperties(std::ostream& os, Snapshot* s) { |
231 |
gezelter |
2 |
|
232 |
tim |
1024 |
char buffer[1024]; |
233 |
|
|
|
234 |
|
|
os << " <FrameData>\n"; |
235 |
|
|
|
236 |
|
|
RealType currentTime = s->getTime(); |
237 |
gezelter |
1437 |
|
238 |
|
|
if (isinf(currentTime) || isnan(currentTime)) { |
239 |
|
|
sprintf( painCave.errMsg, |
240 |
|
|
"DumpWriter detected a numerical error writing the time"); |
241 |
|
|
painCave.isFatal = 1; |
242 |
|
|
simError(); |
243 |
|
|
} |
244 |
|
|
|
245 |
gezelter |
1025 |
sprintf(buffer, " Time: %.10g\n", currentTime); |
246 |
tim |
1024 |
os << buffer; |
247 |
|
|
|
248 |
gezelter |
246 |
Mat3x3d hmat; |
249 |
|
|
hmat = s->getHmat(); |
250 |
gezelter |
1437 |
|
251 |
|
|
for (unsigned int i = 0; i < 3; i++) { |
252 |
|
|
for (unsigned int j = 0; j < 3; j++) { |
253 |
|
|
if (isinf(hmat(i,j)) || isnan(hmat(i,j))) { |
254 |
|
|
sprintf( painCave.errMsg, |
255 |
|
|
"DumpWriter detected a numerical error writing the box"); |
256 |
|
|
painCave.isFatal = 1; |
257 |
|
|
simError(); |
258 |
|
|
} |
259 |
|
|
} |
260 |
|
|
} |
261 |
|
|
|
262 |
tim |
1024 |
sprintf(buffer, " Hmat: {{ %.10g, %.10g, %.10g }, { %.10g, %.10g, %.10g }, { %.10g, %.10g, %.10g }}\n", |
263 |
|
|
hmat(0, 0), hmat(1, 0), hmat(2, 0), |
264 |
|
|
hmat(0, 1), hmat(1, 1), hmat(2, 1), |
265 |
|
|
hmat(0, 2), hmat(1, 2), hmat(2, 2)); |
266 |
|
|
os << buffer; |
267 |
gezelter |
2 |
|
268 |
gezelter |
1782 |
pair<RealType, RealType> thermostat = s->getThermostat(); |
269 |
|
|
|
270 |
|
|
if (isinf(thermostat.first) || isnan(thermostat.first) || |
271 |
|
|
isinf(thermostat.second) || isnan(thermostat.second)) { |
272 |
gezelter |
1437 |
sprintf( painCave.errMsg, |
273 |
|
|
"DumpWriter detected a numerical error writing the thermostat"); |
274 |
|
|
painCave.isFatal = 1; |
275 |
|
|
simError(); |
276 |
|
|
} |
277 |
gezelter |
1782 |
sprintf(buffer, " Thermostat: %.10g , %.10g\n", thermostat.first, |
278 |
|
|
thermostat.second); |
279 |
tim |
1024 |
os << buffer; |
280 |
gezelter |
246 |
|
281 |
tim |
1024 |
Mat3x3d eta; |
282 |
gezelter |
1782 |
eta = s->getBarostat(); |
283 |
gezelter |
1437 |
|
284 |
|
|
for (unsigned int i = 0; i < 3; i++) { |
285 |
|
|
for (unsigned int j = 0; j < 3; j++) { |
286 |
|
|
if (isinf(eta(i,j)) || isnan(eta(i,j))) { |
287 |
|
|
sprintf( painCave.errMsg, |
288 |
|
|
"DumpWriter detected a numerical error writing the barostat"); |
289 |
|
|
painCave.isFatal = 1; |
290 |
|
|
simError(); |
291 |
|
|
} |
292 |
|
|
} |
293 |
|
|
} |
294 |
|
|
|
295 |
tim |
1024 |
sprintf(buffer, " Barostat: {{ %.10g, %.10g, %.10g }, { %.10g, %.10g, %.10g }, { %.10g, %.10g, %.10g }}\n", |
296 |
|
|
eta(0, 0), eta(1, 0), eta(2, 0), |
297 |
|
|
eta(0, 1), eta(1, 1), eta(2, 1), |
298 |
|
|
eta(0, 2), eta(1, 2), eta(2, 2)); |
299 |
|
|
os << buffer; |
300 |
gezelter |
246 |
|
301 |
tim |
1024 |
os << " </FrameData>\n"; |
302 |
gezelter |
507 |
} |
303 |
gezelter |
2 |
|
304 |
gezelter |
507 |
void DumpWriter::writeFrame(std::ostream& os) { |
305 |
gezelter |
246 |
|
306 |
tim |
1024 |
#ifdef IS_MPI |
307 |
gezelter |
1796 |
MPI::Status istatus; |
308 |
tim |
1024 |
#endif |
309 |
gezelter |
246 |
|
310 |
|
|
Molecule* mol; |
311 |
gezelter |
1782 |
StuntDouble* sd; |
312 |
gezelter |
246 |
SimInfo::MoleculeIterator mi; |
313 |
|
|
Molecule::IntegrableObjectIterator ii; |
314 |
gezelter |
1782 |
RigidBody::AtomIterator ai; |
315 |
gezelter |
2 |
|
316 |
gezelter |
246 |
#ifndef IS_MPI |
317 |
tim |
1024 |
os << " <Snapshot>\n"; |
318 |
gezelter |
1025 |
|
319 |
tim |
1024 |
writeFrameProperties(os, info_->getSnapshotManager()->getCurrentSnapshot()); |
320 |
gezelter |
2 |
|
321 |
tim |
1024 |
os << " <StuntDoubles>\n"; |
322 |
gezelter |
1879 |
for (mol = info_->beginMolecule(mi); mol != NULL; |
323 |
|
|
mol = info_->nextMolecule(mi)) { |
324 |
xsun |
1217 |
|
325 |
gezelter |
1782 |
for (sd = mol->beginIntegrableObject(ii); sd != NULL; |
326 |
|
|
sd = mol->nextIntegrableObject(ii)) { |
327 |
|
|
os << prepareDumpLine(sd); |
328 |
xsun |
1217 |
|
329 |
tim |
1024 |
} |
330 |
|
|
} |
331 |
|
|
os << " </StuntDoubles>\n"; |
332 |
gezelter |
1782 |
|
333 |
|
|
if (doSiteData_) { |
334 |
|
|
os << " <SiteData>\n"; |
335 |
gezelter |
1796 |
for (mol = info_->beginMolecule(mi); mol != NULL; |
336 |
|
|
mol = info_->nextMolecule(mi)) { |
337 |
gezelter |
1782 |
|
338 |
|
|
for (sd = mol->beginIntegrableObject(ii); sd != NULL; |
339 |
gezelter |
1880 |
sd = mol->nextIntegrableObject(ii)) { |
340 |
|
|
|
341 |
gezelter |
1782 |
int ioIndex = sd->getGlobalIntegrableObjectIndex(); |
342 |
|
|
// do one for the IO itself |
343 |
|
|
os << prepareSiteLine(sd, ioIndex, 0); |
344 |
|
|
|
345 |
|
|
if (sd->isRigidBody()) { |
346 |
|
|
|
347 |
|
|
RigidBody* rb = static_cast<RigidBody*>(sd); |
348 |
|
|
int siteIndex = 0; |
349 |
gezelter |
1879 |
for (Atom* atom = rb->beginAtom(ai); atom != NULL; |
350 |
gezelter |
1782 |
atom = rb->nextAtom(ai)) { |
351 |
|
|
os << prepareSiteLine(atom, ioIndex, siteIndex); |
352 |
|
|
siteIndex++; |
353 |
|
|
} |
354 |
|
|
} |
355 |
|
|
} |
356 |
|
|
} |
357 |
|
|
os << " </SiteData>\n"; |
358 |
|
|
} |
359 |
tim |
1024 |
os << " </Snapshot>\n"; |
360 |
gezelter |
2 |
|
361 |
tim |
1024 |
os.flush(); |
362 |
|
|
#else |
363 |
gezelter |
1796 |
|
364 |
|
|
const int masterNode = 0; |
365 |
|
|
int worldRank = MPI::COMM_WORLD.Get_rank(); |
366 |
|
|
int nProc = MPI::COMM_WORLD.Get_size(); |
367 |
|
|
|
368 |
|
|
if (worldRank == masterNode) { |
369 |
|
|
os << " <Snapshot>\n"; |
370 |
|
|
writeFrameProperties(os, |
371 |
|
|
info_->getSnapshotManager()->getCurrentSnapshot()); |
372 |
|
|
os << " <StuntDoubles>\n"; |
373 |
|
|
} |
374 |
|
|
|
375 |
tim |
1024 |
//every node prepares the dump lines for integrable objects belong to itself |
376 |
|
|
std::string buffer; |
377 |
gezelter |
1796 |
for (mol = info_->beginMolecule(mi); mol != NULL; |
378 |
|
|
mol = info_->nextMolecule(mi)) { |
379 |
gezelter |
1782 |
for (sd = mol->beginIntegrableObject(ii); sd != NULL; |
380 |
|
|
sd = mol->nextIntegrableObject(ii)) { |
381 |
gezelter |
1796 |
buffer += prepareDumpLine(sd); |
382 |
gezelter |
507 |
} |
383 |
gezelter |
2 |
} |
384 |
tim |
1024 |
|
385 |
|
|
if (worldRank == masterNode) { |
386 |
gezelter |
1025 |
os << buffer; |
387 |
gezelter |
1796 |
|
388 |
tim |
1024 |
for (int i = 1; i < nProc; ++i) { |
389 |
gezelter |
1796 |
// tell processor i to start sending us data: |
390 |
|
|
MPI::COMM_WORLD.Bcast(&i, 1, MPI::INT, masterNode); |
391 |
gezelter |
2 |
|
392 |
tim |
1024 |
// receive the length of the string buffer that was |
393 |
gezelter |
1796 |
// prepared by processor i: |
394 |
|
|
int recvLength; |
395 |
|
|
MPI::COMM_WORLD.Recv(&recvLength, 1, MPI::INT, i, MPI::ANY_TAG, |
396 |
|
|
istatus); |
397 |
gezelter |
1782 |
|
398 |
gezelter |
1796 |
// create a buffer to receive the data |
399 |
tim |
1024 |
char* recvBuffer = new char[recvLength]; |
400 |
|
|
if (recvBuffer == NULL) { |
401 |
|
|
} else { |
402 |
gezelter |
1796 |
// receive the data: |
403 |
|
|
MPI::COMM_WORLD.Recv(recvBuffer, recvLength, MPI::CHAR, i, |
404 |
|
|
MPI::ANY_TAG, istatus); |
405 |
|
|
// send it to the file: |
406 |
tim |
1024 |
os << recvBuffer; |
407 |
gezelter |
1796 |
// get rid of the receive buffer: |
408 |
gezelter |
1313 |
delete [] recvBuffer; |
409 |
tim |
1024 |
} |
410 |
|
|
} |
411 |
gezelter |
246 |
} else { |
412 |
gezelter |
1025 |
int sendBufferLength = buffer.size() + 1; |
413 |
chuckv |
1564 |
int myturn = 0; |
414 |
|
|
for (int i = 1; i < nProc; ++i){ |
415 |
gezelter |
1796 |
// wait for the master node to call our number: |
416 |
|
|
MPI::COMM_WORLD.Bcast(&myturn, 1, MPI::INT, masterNode); |
417 |
chuckv |
1564 |
if (myturn == worldRank){ |
418 |
gezelter |
1796 |
// send the length of our buffer: |
419 |
|
|
MPI::COMM_WORLD.Send(&sendBufferLength, 1, MPI::INT, masterNode, 0); |
420 |
|
|
|
421 |
|
|
// send our buffer: |
422 |
|
|
MPI::COMM_WORLD.Send((void *)buffer.c_str(), sendBufferLength, |
423 |
|
|
MPI::CHAR, masterNode, 0); |
424 |
gezelter |
1879 |
|
425 |
chuckv |
1564 |
} |
426 |
|
|
} |
427 |
tim |
1024 |
} |
428 |
gezelter |
1796 |
|
429 |
|
|
if (worldRank == masterNode) { |
430 |
|
|
os << " </StuntDoubles>\n"; |
431 |
|
|
} |
432 |
gezelter |
2 |
|
433 |
gezelter |
1796 |
if (doSiteData_) { |
434 |
|
|
if (worldRank == masterNode) { |
435 |
|
|
os << " <SiteData>\n"; |
436 |
|
|
} |
437 |
|
|
buffer.clear(); |
438 |
|
|
for (mol = info_->beginMolecule(mi); mol != NULL; |
439 |
|
|
mol = info_->nextMolecule(mi)) { |
440 |
|
|
|
441 |
|
|
for (sd = mol->beginIntegrableObject(ii); sd != NULL; |
442 |
|
|
sd = mol->nextIntegrableObject(ii)) { |
443 |
|
|
|
444 |
|
|
int ioIndex = sd->getGlobalIntegrableObjectIndex(); |
445 |
|
|
// do one for the IO itself |
446 |
|
|
buffer += prepareSiteLine(sd, ioIndex, 0); |
447 |
|
|
|
448 |
|
|
if (sd->isRigidBody()) { |
449 |
|
|
|
450 |
|
|
RigidBody* rb = static_cast<RigidBody*>(sd); |
451 |
|
|
int siteIndex = 0; |
452 |
gezelter |
1879 |
for (Atom* atom = rb->beginAtom(ai); atom != NULL; |
453 |
gezelter |
1796 |
atom = rb->nextAtom(ai)) { |
454 |
|
|
buffer += prepareSiteLine(atom, ioIndex, siteIndex); |
455 |
|
|
siteIndex++; |
456 |
|
|
} |
457 |
|
|
} |
458 |
|
|
} |
459 |
|
|
} |
460 |
|
|
|
461 |
|
|
if (worldRank == masterNode) { |
462 |
|
|
os << buffer; |
463 |
|
|
|
464 |
|
|
for (int i = 1; i < nProc; ++i) { |
465 |
|
|
|
466 |
|
|
// tell processor i to start sending us data: |
467 |
|
|
MPI::COMM_WORLD.Bcast(&i, 1, MPI::INT, masterNode); |
468 |
|
|
|
469 |
|
|
// receive the length of the string buffer that was |
470 |
|
|
// prepared by processor i: |
471 |
|
|
int recvLength; |
472 |
|
|
MPI::COMM_WORLD.Recv(&recvLength, 1, MPI::INT, i, MPI::ANY_TAG, |
473 |
|
|
istatus); |
474 |
|
|
|
475 |
|
|
// create a buffer to receive the data |
476 |
|
|
char* recvBuffer = new char[recvLength]; |
477 |
|
|
if (recvBuffer == NULL) { |
478 |
|
|
} else { |
479 |
|
|
// receive the data: |
480 |
|
|
MPI::COMM_WORLD.Recv(recvBuffer, recvLength, MPI::CHAR, i, |
481 |
|
|
MPI::ANY_TAG, istatus); |
482 |
|
|
// send it to the file: |
483 |
|
|
os << recvBuffer; |
484 |
|
|
// get rid of the receive buffer: |
485 |
|
|
delete [] recvBuffer; |
486 |
|
|
} |
487 |
|
|
} |
488 |
|
|
} else { |
489 |
|
|
int sendBufferLength = buffer.size() + 1; |
490 |
|
|
int myturn = 0; |
491 |
|
|
for (int i = 1; i < nProc; ++i){ |
492 |
|
|
// wait for the master node to call our number: |
493 |
|
|
MPI::COMM_WORLD.Bcast(&myturn, 1, MPI::INT, masterNode); |
494 |
|
|
if (myturn == worldRank){ |
495 |
|
|
// send the length of our buffer: |
496 |
|
|
MPI::COMM_WORLD.Send(&sendBufferLength, 1, MPI::INT, masterNode, 0); |
497 |
|
|
// send our buffer: |
498 |
|
|
MPI::COMM_WORLD.Send((void *)buffer.c_str(), sendBufferLength, |
499 |
|
|
MPI::CHAR, masterNode, 0); |
500 |
|
|
} |
501 |
|
|
} |
502 |
|
|
} |
503 |
|
|
|
504 |
|
|
if (worldRank == masterNode) { |
505 |
|
|
os << " </SiteData>\n"; |
506 |
|
|
} |
507 |
|
|
} |
508 |
|
|
|
509 |
|
|
if (worldRank == masterNode) { |
510 |
|
|
os << " </Snapshot>\n"; |
511 |
|
|
os.flush(); |
512 |
|
|
} |
513 |
|
|
|
514 |
tim |
1024 |
#endif // is_mpi |
515 |
gezelter |
1796 |
|
516 |
tim |
1024 |
} |
517 |
gezelter |
2 |
|
518 |
gezelter |
1782 |
std::string DumpWriter::prepareDumpLine(StuntDouble* sd) { |
519 |
tim |
1024 |
|
520 |
gezelter |
1782 |
int index = sd->getGlobalIntegrableObjectIndex(); |
521 |
tim |
1024 |
std::string type("pv"); |
522 |
|
|
std::string line; |
523 |
|
|
char tempBuffer[4096]; |
524 |
gezelter |
2 |
|
525 |
tim |
1024 |
Vector3d pos; |
526 |
|
|
Vector3d vel; |
527 |
gezelter |
1782 |
pos = sd->getPos(); |
528 |
gezelter |
1437 |
|
529 |
|
|
if (isinf(pos[0]) || isnan(pos[0]) || |
530 |
|
|
isinf(pos[1]) || isnan(pos[1]) || |
531 |
|
|
isinf(pos[2]) || isnan(pos[2]) ) { |
532 |
|
|
sprintf( painCave.errMsg, |
533 |
|
|
"DumpWriter detected a numerical error writing the position" |
534 |
|
|
" for object %d", index); |
535 |
|
|
painCave.isFatal = 1; |
536 |
|
|
simError(); |
537 |
|
|
} |
538 |
|
|
|
539 |
gezelter |
1782 |
vel = sd->getVel(); |
540 |
gezelter |
1437 |
|
541 |
|
|
if (isinf(vel[0]) || isnan(vel[0]) || |
542 |
|
|
isinf(vel[1]) || isnan(vel[1]) || |
543 |
|
|
isinf(vel[2]) || isnan(vel[2]) ) { |
544 |
|
|
sprintf( painCave.errMsg, |
545 |
|
|
"DumpWriter detected a numerical error writing the velocity" |
546 |
|
|
" for object %d", index); |
547 |
|
|
painCave.isFatal = 1; |
548 |
|
|
simError(); |
549 |
|
|
} |
550 |
|
|
|
551 |
gezelter |
1025 |
sprintf(tempBuffer, "%18.10g %18.10g %18.10g %13e %13e %13e", |
552 |
tim |
1024 |
pos[0], pos[1], pos[2], |
553 |
|
|
vel[0], vel[1], vel[2]); |
554 |
|
|
line += tempBuffer; |
555 |
gezelter |
2 |
|
556 |
gezelter |
1782 |
if (sd->isDirectional()) { |
557 |
tim |
1024 |
type += "qj"; |
558 |
|
|
Quat4d q; |
559 |
|
|
Vector3d ji; |
560 |
gezelter |
1782 |
q = sd->getQ(); |
561 |
gezelter |
1437 |
|
562 |
|
|
if (isinf(q[0]) || isnan(q[0]) || |
563 |
|
|
isinf(q[1]) || isnan(q[1]) || |
564 |
|
|
isinf(q[2]) || isnan(q[2]) || |
565 |
|
|
isinf(q[3]) || isnan(q[3]) ) { |
566 |
|
|
sprintf( painCave.errMsg, |
567 |
|
|
"DumpWriter detected a numerical error writing the quaternion" |
568 |
|
|
" for object %d", index); |
569 |
|
|
painCave.isFatal = 1; |
570 |
|
|
simError(); |
571 |
|
|
} |
572 |
|
|
|
573 |
gezelter |
1782 |
ji = sd->getJ(); |
574 |
gezelter |
1437 |
|
575 |
|
|
if (isinf(ji[0]) || isnan(ji[0]) || |
576 |
|
|
isinf(ji[1]) || isnan(ji[1]) || |
577 |
|
|
isinf(ji[2]) || isnan(ji[2]) ) { |
578 |
|
|
sprintf( painCave.errMsg, |
579 |
|
|
"DumpWriter detected a numerical error writing the angular" |
580 |
|
|
" momentum for object %d", index); |
581 |
|
|
painCave.isFatal = 1; |
582 |
|
|
simError(); |
583 |
|
|
} |
584 |
|
|
|
585 |
gezelter |
1025 |
sprintf(tempBuffer, " %13e %13e %13e %13e %13e %13e %13e", |
586 |
tim |
1024 |
q[0], q[1], q[2], q[3], |
587 |
|
|
ji[0], ji[1], ji[2]); |
588 |
|
|
line += tempBuffer; |
589 |
|
|
} |
590 |
gezelter |
2 |
|
591 |
tim |
1024 |
if (needForceVector_) { |
592 |
gezelter |
1437 |
type += "f"; |
593 |
gezelter |
1782 |
Vector3d frc = sd->getFrc(); |
594 |
gezelter |
1437 |
if (isinf(frc[0]) || isnan(frc[0]) || |
595 |
|
|
isinf(frc[1]) || isnan(frc[1]) || |
596 |
|
|
isinf(frc[2]) || isnan(frc[2]) ) { |
597 |
|
|
sprintf( painCave.errMsg, |
598 |
|
|
"DumpWriter detected a numerical error writing the force" |
599 |
|
|
" for object %d", index); |
600 |
|
|
painCave.isFatal = 1; |
601 |
|
|
simError(); |
602 |
|
|
} |
603 |
|
|
sprintf(tempBuffer, " %13e %13e %13e", |
604 |
|
|
frc[0], frc[1], frc[2]); |
605 |
tim |
1024 |
line += tempBuffer; |
606 |
gezelter |
1437 |
|
607 |
gezelter |
1782 |
if (sd->isDirectional()) { |
608 |
gezelter |
1437 |
type += "t"; |
609 |
gezelter |
1782 |
Vector3d trq = sd->getTrq(); |
610 |
gezelter |
1437 |
if (isinf(trq[0]) || isnan(trq[0]) || |
611 |
|
|
isinf(trq[1]) || isnan(trq[1]) || |
612 |
|
|
isinf(trq[2]) || isnan(trq[2]) ) { |
613 |
|
|
sprintf( painCave.errMsg, |
614 |
|
|
"DumpWriter detected a numerical error writing the torque" |
615 |
|
|
" for object %d", index); |
616 |
|
|
painCave.isFatal = 1; |
617 |
|
|
simError(); |
618 |
gezelter |
1782 |
} |
619 |
gezelter |
1437 |
sprintf(tempBuffer, " %13e %13e %13e", |
620 |
|
|
trq[0], trq[1], trq[2]); |
621 |
|
|
line += tempBuffer; |
622 |
|
|
} |
623 |
gezelter |
246 |
} |
624 |
gezelter |
1782 |
|
625 |
|
|
sprintf(tempBuffer, "%10d %7s %s\n", index, type.c_str(), line.c_str()); |
626 |
|
|
return std::string(tempBuffer); |
627 |
|
|
} |
628 |
|
|
|
629 |
|
|
std::string DumpWriter::prepareSiteLine(StuntDouble* sd, int ioIndex, int siteIndex) { |
630 |
gezelter |
1921 |
int storageLayout = info_->getSnapshotManager()->getStorageLayout(); |
631 |
gezelter |
1782 |
|
632 |
|
|
std::string id; |
633 |
|
|
std::string type; |
634 |
|
|
std::string line; |
635 |
|
|
char tempBuffer[4096]; |
636 |
|
|
|
637 |
|
|
if (sd->isRigidBody()) { |
638 |
|
|
sprintf(tempBuffer, "%10d ", ioIndex); |
639 |
|
|
id = std::string(tempBuffer); |
640 |
|
|
} else { |
641 |
|
|
sprintf(tempBuffer, "%10d %10d", ioIndex, siteIndex); |
642 |
|
|
id = std::string(tempBuffer); |
643 |
|
|
} |
644 |
|
|
|
645 |
|
|
if (needFlucQ_) { |
646 |
gezelter |
1921 |
if (storageLayout & DataStorage::dslFlucQPosition) { |
647 |
|
|
type += "c"; |
648 |
|
|
RealType fqPos = sd->getFlucQPos(); |
649 |
|
|
if (isinf(fqPos) || isnan(fqPos) ) { |
650 |
|
|
sprintf( painCave.errMsg, |
651 |
|
|
"DumpWriter detected a numerical error writing the" |
652 |
|
|
" fluctuating charge for object %s", id.c_str()); |
653 |
|
|
painCave.isFatal = 1; |
654 |
|
|
simError(); |
655 |
|
|
} |
656 |
|
|
sprintf(tempBuffer, " %13e ", fqPos); |
657 |
|
|
line += tempBuffer; |
658 |
|
|
} |
659 |
gezelter |
1782 |
|
660 |
gezelter |
1921 |
if (storageLayout & DataStorage::dslFlucQVelocity) { |
661 |
|
|
type += "w"; |
662 |
|
|
RealType fqVel = sd->getFlucQVel(); |
663 |
|
|
if (isinf(fqVel) || isnan(fqVel) ) { |
664 |
gezelter |
1782 |
sprintf( painCave.errMsg, |
665 |
|
|
"DumpWriter detected a numerical error writing the" |
666 |
gezelter |
1921 |
" fluctuating charge velocity for object %s", id.c_str()); |
667 |
gezelter |
1782 |
painCave.isFatal = 1; |
668 |
|
|
simError(); |
669 |
|
|
} |
670 |
gezelter |
1921 |
sprintf(tempBuffer, " %13e ", fqVel); |
671 |
gezelter |
1782 |
line += tempBuffer; |
672 |
|
|
} |
673 |
gezelter |
1921 |
|
674 |
|
|
if (needForceVector_) { |
675 |
|
|
if (storageLayout & DataStorage::dslFlucQForce) { |
676 |
|
|
type += "g"; |
677 |
|
|
RealType fqFrc = sd->getFlucQFrc(); |
678 |
|
|
if (isinf(fqFrc) || isnan(fqFrc) ) { |
679 |
|
|
sprintf( painCave.errMsg, |
680 |
|
|
"DumpWriter detected a numerical error writing the" |
681 |
|
|
" fluctuating charge force for object %s", id.c_str()); |
682 |
|
|
painCave.isFatal = 1; |
683 |
|
|
simError(); |
684 |
|
|
} |
685 |
|
|
sprintf(tempBuffer, " %13e ", fqFrc); |
686 |
|
|
line += tempBuffer; |
687 |
|
|
} |
688 |
|
|
} |
689 |
gezelter |
1782 |
} |
690 |
gezelter |
1921 |
|
691 |
gezelter |
1782 |
if (needElectricField_) { |
692 |
gezelter |
1921 |
if (storageLayout & DataStorage::dslElectricField) { |
693 |
|
|
type += "e"; |
694 |
|
|
Vector3d eField= sd->getElectricField(); |
695 |
|
|
if (isinf(eField[0]) || isnan(eField[0]) || |
696 |
|
|
isinf(eField[1]) || isnan(eField[1]) || |
697 |
|
|
isinf(eField[2]) || isnan(eField[2]) ) { |
698 |
|
|
sprintf( painCave.errMsg, |
699 |
|
|
"DumpWriter detected a numerical error writing the electric" |
700 |
|
|
" field for object %s", id.c_str()); |
701 |
|
|
painCave.isFatal = 1; |
702 |
|
|
simError(); |
703 |
|
|
} |
704 |
|
|
sprintf(tempBuffer, " %13e %13e %13e", |
705 |
|
|
eField[0], eField[1], eField[2]); |
706 |
|
|
line += tempBuffer; |
707 |
gezelter |
1782 |
} |
708 |
|
|
} |
709 |
|
|
|
710 |
|
|
|
711 |
gezelter |
1610 |
if (needParticlePot_) { |
712 |
gezelter |
1921 |
if (storageLayout & DataStorage::dslParticlePot) { |
713 |
|
|
type += "u"; |
714 |
|
|
RealType particlePot = sd->getParticlePot(); |
715 |
|
|
if (isinf(particlePot) || isnan(particlePot)) { |
716 |
|
|
sprintf( painCave.errMsg, |
717 |
|
|
"DumpWriter detected a numerical error writing the particle " |
718 |
|
|
" potential for object %s", id.c_str()); |
719 |
|
|
painCave.isFatal = 1; |
720 |
|
|
simError(); |
721 |
|
|
} |
722 |
|
|
sprintf(tempBuffer, " %13e", particlePot); |
723 |
|
|
line += tempBuffer; |
724 |
gezelter |
1610 |
} |
725 |
|
|
} |
726 |
gezelter |
1921 |
|
727 |
gezelter |
1782 |
sprintf(tempBuffer, "%s %7s %s\n", id.c_str(), type.c_str(), line.c_str()); |
728 |
tim |
1024 |
return std::string(tempBuffer); |
729 |
gezelter |
507 |
} |
730 |
gezelter |
2 |
|
731 |
gezelter |
507 |
void DumpWriter::writeDump() { |
732 |
tim |
615 |
writeFrame(*dumpFile_); |
733 |
gezelter |
507 |
} |
734 |
tim |
376 |
|
735 |
gezelter |
507 |
void DumpWriter::writeEor() { |
736 |
tim |
615 |
std::ostream* eorStream; |
737 |
tim |
376 |
|
738 |
|
|
#ifdef IS_MPI |
739 |
|
|
if (worldRank == 0) { |
740 |
|
|
#endif // is_mpi |
741 |
gezelter |
1879 |
|
742 |
tim |
615 |
eorStream = createOStream(eorFilename_); |
743 |
tim |
376 |
|
744 |
|
|
#ifdef IS_MPI |
745 |
|
|
} |
746 |
gezelter |
1879 |
#endif |
747 |
|
|
|
748 |
tim |
615 |
writeFrame(*eorStream); |
749 |
gezelter |
1879 |
|
750 |
tim |
615 |
#ifdef IS_MPI |
751 |
|
|
if (worldRank == 0) { |
752 |
gezelter |
1879 |
#endif |
753 |
|
|
|
754 |
tim |
1024 |
writeClosing(*eorStream); |
755 |
|
|
delete eorStream; |
756 |
gezelter |
1879 |
|
757 |
tim |
615 |
#ifdef IS_MPI |
758 |
|
|
} |
759 |
|
|
#endif // is_mpi |
760 |
|
|
|
761 |
gezelter |
507 |
} |
762 |
tim |
376 |
|
763 |
|
|
|
764 |
gezelter |
507 |
void DumpWriter::writeDumpAndEor() { |
765 |
tim |
376 |
std::vector<std::streambuf*> buffers; |
766 |
tim |
615 |
std::ostream* eorStream; |
767 |
tim |
376 |
#ifdef IS_MPI |
768 |
|
|
if (worldRank == 0) { |
769 |
|
|
#endif // is_mpi |
770 |
|
|
|
771 |
tim |
615 |
buffers.push_back(dumpFile_->rdbuf()); |
772 |
tim |
376 |
|
773 |
tim |
615 |
eorStream = createOStream(eorFilename_); |
774 |
tim |
376 |
|
775 |
tim |
615 |
buffers.push_back(eorStream->rdbuf()); |
776 |
tim |
376 |
|
777 |
|
|
#ifdef IS_MPI |
778 |
|
|
} |
779 |
|
|
#endif // is_mpi |
780 |
|
|
|
781 |
|
|
TeeBuf tbuf(buffers.begin(), buffers.end()); |
782 |
|
|
std::ostream os(&tbuf); |
783 |
|
|
|
784 |
|
|
writeFrame(os); |
785 |
tim |
615 |
|
786 |
|
|
#ifdef IS_MPI |
787 |
|
|
if (worldRank == 0) { |
788 |
|
|
#endif // is_mpi |
789 |
tim |
1024 |
writeClosing(*eorStream); |
790 |
|
|
delete eorStream; |
791 |
tim |
615 |
#ifdef IS_MPI |
792 |
|
|
} |
793 |
|
|
#endif // is_mpi |
794 |
tim |
376 |
|
795 |
gezelter |
507 |
} |
796 |
tim |
376 |
|
797 |
tim |
1024 |
std::ostream* DumpWriter::createOStream(const std::string& filename) { |
798 |
tim |
619 |
|
799 |
tim |
615 |
std::ostream* newOStream; |
800 |
gezelter |
1782 |
#ifdef HAVE_ZLIB |
801 |
tim |
615 |
if (needCompression_) { |
802 |
tim |
1024 |
newOStream = new ogzstream(filename.c_str()); |
803 |
tim |
615 |
} else { |
804 |
tim |
1024 |
newOStream = new std::ofstream(filename.c_str()); |
805 |
tim |
615 |
} |
806 |
tim |
619 |
#else |
807 |
|
|
newOStream = new std::ofstream(filename.c_str()); |
808 |
|
|
#endif |
809 |
tim |
1024 |
//write out MetaData first |
810 |
gezelter |
1782 |
(*newOStream) << "<OpenMD version=2>" << std::endl; |
811 |
tim |
1024 |
(*newOStream) << " <MetaData>" << std::endl; |
812 |
|
|
(*newOStream) << info_->getRawMetaData(); |
813 |
|
|
(*newOStream) << " </MetaData>" << std::endl; |
814 |
tim |
615 |
return newOStream; |
815 |
tim |
1024 |
} |
816 |
tim |
376 |
|
817 |
tim |
1024 |
void DumpWriter::writeClosing(std::ostream& os) { |
818 |
|
|
|
819 |
gezelter |
1390 |
os << "</OpenMD>\n"; |
820 |
tim |
1024 |
os.flush(); |
821 |
|
|
} |
822 |
|
|
|
823 |
gezelter |
1390 |
}//end namespace OpenMD |