OpenMD 3.1
Molecular Dynamics in the Open
Loading...
Searching...
No Matches
StatWriter.cpp
1/*
2 * Copyright (c) 2004-present, The University of Notre Dame. All rights
3 * reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 *
15 * 3. Neither the name of the copyright holder nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 *
31 * SUPPORT OPEN SCIENCE! If you use OpenMD or its source code in your
32 * research, please cite the appropriate papers when you publish your
33 * work. Good starting points are:
34 *
35 * [1] Meineke, et al., J. Comp. Chem. 26, 252-271 (2005).
36 * [2] Fennell & Gezelter, J. Chem. Phys. 124, 234104 (2006).
37 * [3] Sun, Lin & Gezelter, J. Chem. Phys. 128, 234107 (2008).
38 * [4] Vardeman, Stocker & Gezelter, J. Chem. Theory Comput. 7, 834 (2011).
39 * [5] Kuang & Gezelter, Mol. Phys., 110, 691-701 (2012).
40 * [6] Lamichhane, Gezelter & Newman, J. Chem. Phys. 141, 134109 (2014).
41 * [7] Lamichhane, Newman & Gezelter, J. Chem. Phys. 141, 134110 (2014).
42 * [8] Bhattarai, Newman & Gezelter, Phys. Rev. B 99, 094106 (2019).
43 */
44
45#include <config.h>
46
47#define _LARGEFILE_SOURCE64
48#define _FILE_OFFSET_BITS 64
49
50#include "brains/Stats.hpp"
51#include "io/StatWriter.hpp"
52#include "utils/Revision.hpp"
53#include "utils/simError.h"
54
55using namespace std;
56
57namespace OpenMD {
58
59 StatWriter::StatWriter(const std::string& filename, Stats* stats) :
60 stats_(stats) {
61#ifdef IS_MPI
62 if (worldRank == 0) {
63#endif
64
65 statfile_.open(filename.c_str(), std::ios::out | std::ios::trunc);
66
67 if (!statfile_) {
68 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
69 "Could not open \"%s\" for stat output.\n", filename.c_str());
70 painCave.isFatal = 1;
71 simError();
72 }
73
74 // Create the string for embedding the version information in the MetaData
75 version.assign("## Last run using OpenMD version: ");
76 version.append(OPENMD_VERSION_MAJOR);
77 version.append(".");
78 version.append(OPENMD_VERSION_MINOR);
79 version.append(",");
80
81 std::string rev(revision, strnlen(revision, 40));
82 rev.append(40 - rev.length(), ' ');
83 version.append(" revision: ");
84 // If there's no GIT revision, just call this the RELEASE revision.
85 if (!rev.empty()) {
86 version.append(rev);
87 } else {
88 version.append("RELEASE");
89 }
90
91 writeTitle();
92
93#ifdef IS_MPI
94 }
95
96 snprintf(checkPointMsg, MAX_SIM_ERROR_MSG_LENGTH,
97 "Sucessfully opened output file for stating.\n");
98 errorCheckPoint();
99#endif
100 }
101
102 StatWriter::~StatWriter() {
103#ifdef IS_MPI
104 if (worldRank == 0) {
105#endif
106
107 statfile_.close();
108
109#ifdef IS_MPI
110 }
111#endif
112 }
113
114 void StatWriter::writeTitle() {
115 Stats::StatsBitSet mask = stats_->getStatsMask();
116
117#ifdef IS_MPI
118 if (worldRank == 0) {
119#endif
120
121 // write revision
122 statfile_ << version << std::endl;
123 // write title
124 statfile_ << "#";
125 for (unsigned int i = 0; i < mask.size(); ++i) {
126 if (mask[i]) {
127 statfile_ << "\t" << stats_->getTitle(i) << "(" << stats_->getUnits(i)
128 << ")";
129 }
130 }
131 statfile_ << std::endl;
132
133#ifdef IS_MPI
134 }
135#endif
136 }
137
138 void StatWriter::writeStat() {
139#ifdef IS_MPI
140 if (worldRank == 0) {
141#endif
142
143 Stats::StatsBitSet mask = stats_->getStatsMask();
144 statfile_.precision(stats_->getPrecision());
145
146 for (unsigned int i = 0; i < mask.size(); ++i) {
147 if (mask[i]) {
148 if (stats_->getDataType(i) == "RealType")
149 writeReal(i);
150 else if (stats_->getDataType(i) == "Vector3d")
151 writeVector(i);
152 else if (stats_->getDataType(i) == "potVec")
153 writePotVec(i);
154 else if (stats_->getDataType(i) == "Mat3x3d")
155 writeMatrix(i);
156 else if (stats_->getDataType(i) == "Array2d")
157 writeArray(i);
158 else {
159 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
160 "StatWriter found an unknown data type for: %s ",
161 stats_->getTitle(i).c_str());
162 painCave.isFatal = 1;
163 simError();
164 }
165 }
166 }
167
168 statfile_ << std::endl;
169 statfile_.rdbuf()->pubsync();
170
171#ifdef IS_MPI
172 }
173 errorCheckPoint();
174#endif
175 }
176
177 void StatWriter::writeReal(int i) {
178 RealType s = stats_->getRealData(i);
179
180 if (!std::isinf(s) && !std::isnan(s)) {
181 statfile_ << "\t" << s;
182 } else {
183 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
184 "StatWriter detected a numerical error writing: %s ",
185 stats_->getTitle(i).c_str());
186 painCave.isFatal = 1;
187 simError();
188 }
189 }
190
191 void StatWriter::writeVector(int i) {
192 Vector3d s = stats_->getVectorData(i);
193 if (std::isinf(s[0]) || std::isnan(s[0]) || std::isinf(s[1]) ||
194 std::isnan(s[1]) || std::isinf(s[2]) || std::isnan(s[2])) {
195 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
196 "StatWriter detected a numerical error writing: %s",
197 stats_->getTitle(i).c_str());
198 painCave.isFatal = 1;
199 simError();
200 } else {
201 statfile_ << "\t" << s[0] << "\t" << s[1] << "\t" << s[2];
202 }
203 }
204
205 void StatWriter::writePotVec(int i) {
206 potVec s = stats_->getPotVecData(i);
207
208 bool foundError = false;
209
210 for (unsigned int j = 0; j < N_INTERACTION_FAMILIES; j++) {
211 if (std::isinf(s[j]) || std::isnan(s[j])) foundError = true;
212 }
213 if (foundError) {
214 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
215 "StatWriter detected a numerical error writing: %s",
216 stats_->getTitle(i).c_str());
217 painCave.isFatal = 1;
218 simError();
219 } else {
220 for (unsigned int j = 0; j < N_INTERACTION_FAMILIES; j++) {
221 statfile_ << "\t" << s[j];
222 }
223 }
224 }
225
226 void StatWriter::writeMatrix(int i) {
227 Mat3x3d s = stats_->getMatrixData(i);
228
229 for (unsigned int i1 = 0; i1 < 3; i1++) {
230 for (unsigned int j1 = 0; j1 < 3; j1++) {
231 if (std::isinf(s(i1, j1)) || std::isnan(s(i1, j1))) {
232 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
233 "StatWriter detected a numerical error writing: %s",
234 stats_->getTitle(i).c_str());
235 painCave.isFatal = 1;
236 simError();
237 } else {
238 statfile_ << "\t" << s(i1, j1);
239 }
240 }
241 }
242 }
243
244 void StatWriter::writeArray(int i) {
245 std::vector<RealType> s = stats_->getArrayData(i);
246
247 for (unsigned int j = 0; j < s.size(); ++j) {
248 if (std::isinf(s[j]) || std::isnan(s[j])) {
249 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
250 "StatWriter detected a numerical error writing: %s",
251 stats_->getTitle(i).c_str());
252 painCave.isFatal = 1;
253 simError();
254 } else {
255 statfile_ << "\t" << s[j];
256 }
257 }
258 }
259
260 void StatWriter::writeStatReport() {
261#ifdef IS_MPI
262 if (worldRank == 0) {
263#endif
264 reportfile_.open(reportFileName_.c_str(),
265 std::ios::out | std::ios::trunc);
266
267 if (!reportfile_) {
268 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
269 "Could not open \"%s\" for report output.\n",
270 reportFileName_.c_str());
271 painCave.isFatal = 1;
272 simError();
273 }
274
275 reportfile_ << stats_->getStatsReport();
276 std::cout << stats_->getStatsReport();
277 reportfile_.close();
278
279#ifdef IS_MPI
280 }
281#endif
282 }
283} // namespace OpenMD
This basic Periodic Table class was originally taken from the data.cpp file in OpenBabel.