47#define _LARGEFILE_SOURCE64
48#define _FILE_OFFSET_BITS 64
50#include "brains/Stats.hpp"
51#include "io/StatWriter.hpp"
52#include "utils/Revision.hpp"
53#include "utils/simError.h"
59 StatWriter::StatWriter(
const std::string& filename, Stats* stats) :
65 statfile_.open(filename.c_str(), std::ios::out | std::ios::trunc);
68 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
69 "Could not open \"%s\" for stat output.\n", filename.c_str());
75 version.assign(
"## Last run using OpenMD version: ");
76 version.append(OPENMD_VERSION_MAJOR);
78 version.append(OPENMD_VERSION_MINOR);
81 std::string rev(revision, strnlen(revision, 40));
82 rev.append(40 - rev.length(),
' ');
83 version.append(
" revision: ");
88 version.append(
"RELEASE");
96 snprintf(checkPointMsg, MAX_SIM_ERROR_MSG_LENGTH,
97 "Sucessfully opened output file for stating.\n");
102 StatWriter::~StatWriter() {
104 if (worldRank == 0) {
114 void StatWriter::writeTitle() {
115 Stats::StatsBitSet mask = stats_->getStatsMask();
118 if (worldRank == 0) {
122 statfile_ << version << std::endl;
125 for (
unsigned int i = 0; i < mask.size(); ++i) {
127 statfile_ <<
"\t" << stats_->getTitle(i) <<
"(" << stats_->getUnits(i)
131 statfile_ << std::endl;
138 void StatWriter::writeStat() {
140 if (worldRank == 0) {
143 Stats::StatsBitSet mask = stats_->getStatsMask();
144 statfile_.precision(stats_->getPrecision());
146 for (
unsigned int i = 0; i < mask.size(); ++i) {
148 if (stats_->getDataType(i) ==
"RealType")
150 else if (stats_->getDataType(i) ==
"Vector3d")
152 else if (stats_->getDataType(i) ==
"potVec")
154 else if (stats_->getDataType(i) ==
"Mat3x3d")
156 else if (stats_->getDataType(i) ==
"Array2d")
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;
168 statfile_ << std::endl;
169 statfile_.rdbuf()->pubsync();
177 void StatWriter::writeReal(
int i) {
178 RealType s = stats_->getRealData(i);
180 if (!std::isinf(s) && !std::isnan(s)) {
181 statfile_ <<
"\t" << s;
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;
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;
201 statfile_ <<
"\t" << s[0] <<
"\t" << s[1] <<
"\t" << s[2];
205 void StatWriter::writePotVec(
int i) {
206 potVec s = stats_->getPotVecData(i);
208 bool foundError =
false;
210 for (
unsigned int j = 0; j < N_INTERACTION_FAMILIES; j++) {
211 if (std::isinf(s[j]) || std::isnan(s[j])) foundError =
true;
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;
220 for (
unsigned int j = 0; j < N_INTERACTION_FAMILIES; j++) {
221 statfile_ <<
"\t" << s[j];
226 void StatWriter::writeMatrix(
int i) {
227 Mat3x3d s = stats_->getMatrixData(i);
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;
238 statfile_ <<
"\t" << s(i1, j1);
244 void StatWriter::writeArray(
int i) {
245 std::vector<RealType> s = stats_->getArrayData(i);
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;
255 statfile_ <<
"\t" << s[j];
260 void StatWriter::writeStatReport() {
262 if (worldRank == 0) {
264 reportfile_.open(reportFileName_.c_str(),
265 std::ios::out | std::ios::trunc);
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;
275 reportfile_ << stats_->getStatsReport();
276 std::cout << stats_->getStatsReport();
This basic Periodic Table class was originally taken from the data.cpp file in OpenBabel.