OPALX (Object Oriented Parallel Accelerator Library for Exascal) master (dc2a29eed580)
OPALX
Loading...
Searching...
No Matches
BinConfigWriter.cpp
Go to the documentation of this file.
1//
2// Class BinConfigWriter
3// Writes binning configuration snapshots to a JSON file.
4//
5// See the corresponding header for copyright and license details.
6//
7
9
10#include "Ippl.h"
12
13#include <iomanip>
14
15BinConfigWriter::BinConfigWriter(const std::string& fileName)
16 : os_(fileName.c_str(), std::ios::out | std::ios::trunc), first_(true) {
17 Inform m("BinConfigWriter::BinConfigWriter");
18
19 if (!os_) {
20 m << level4 << "Failed to open bin configuration file \"" << fileName << "\"." << endl;
21 throw OpalException(
22 "BinConfigWriter::BinConfigWriter",
23 "Failed to open bin configuration file \"" + fileName + "\"");
24 }
25
26 m << level4 << "Opened bin configuration JSON file \"" << fileName << "\" for writing." << endl;
27 os_ << "[\n";
28}
29
31 Inform m("BinConfigWriter::~BinConfigWriter");
32
33 if (os_) {
34 m << level5 << "Closing bin configuration JSON file." << endl;
35 os_.close();
36 }
37}
38
40 long long step, double time, bool preMerge, const std::vector<std::size_t>& binCounts,
41 const std::vector<double>& binWidths, double xMin) {
42 if (!os_) {
43 return;
44 }
45
46 Inform m("BinConfigWriter::writeEntry");
47 m << level5 << "Writing bin configuration entry: step=" << step
48 << ", time=" << std::setprecision(17) << time << ", preMerge=" << (preMerge ? 1 : 0)
49 << ", nBins=" << binCounts.size() << ", xMin=" << std::setprecision(17) << xMin << endl;
50
51 // We maintain a *closed* top-level JSON array on disk after each call:
52 // [ <obj1>,\n <obj2>,\n ... <objN>\n]
53 //
54 // For the first entry we just append after "[\n".
55 // For subsequent entries we seek back over the trailing "\n]\n" and
56 // insert ",\n" before appending the next object and the new "\n]\n".
57
58 // Move write position to the end of the file.
59 os_.seekp(0, std::ios::end);
60
61 if (!first_) {
62 // File currently ends with "\n]\n" (3 chars). Step back before that
63 // closing sequence and append a comma+newline to separate entries.
64 os_.seekp(-3, std::ios::end);
65 os_ << ",\n";
66 } else {
67 first_ = false;
68 }
69
70 os_ << " {\n";
71 os_ << " \"step\": " << step << ",\n";
72 os_ << " \"time\": " << std::setprecision(17) << time << ",\n";
73 os_ << " \"preMerge\": " << (preMerge ? "true" : "false") << ",\n";
74 os_ << " \"xMin\": " << std::setprecision(17) << xMin << ",\n";
75
76 // binCounts array (pretty-printed, fixed number of entries per line).
77 os_ << " \"binCounts\": [";
78 for (std::size_t i = 0; i < binCounts.size(); ++i) {
79 if (i % 16 == 0) {
80 os_ << "\n ";
81 }
82 os_ << binCounts[i];
83 if (i + 1 < binCounts.size()) {
84 os_ << ", ";
85 }
86 }
87 if (!binCounts.empty()) {
88 os_ << "\n";
89 }
90 os_ << " ],\n";
91
92 // binWidths array (pretty-printed as well).
93 os_ << " \"binWidths\": [";
94 for (std::size_t i = 0; i < binWidths.size(); ++i) {
95 if (i % 16 == 0) {
96 os_ << "\n ";
97 }
98 os_ << std::setprecision(17) << binWidths[i];
99 if (i + 1 < binWidths.size()) {
100 os_ << ", ";
101 }
102 }
103 if (!binWidths.empty()) {
104 os_ << "\n";
105 }
106 os_ << " ]\n";
107
108 os_ << " ";
109 os_ << "}";
110
111 // Close the top-level array so the file is valid JSON after each write.
112 os_ << "\n]\n";
113
114 // Flush each entry so that the JSON stays well-formed on disk even if the
115 // simulation terminates unexpectedly between steps (between calls, the
116 // file always ends with a complete, closed JSON array).
117 os_.flush();
118}
BinConfigWriter(const std::string &fileName)
void writeEntry(long long step, double time, bool preMerge, const std::vector< std::size_t > &binCounts, const std::vector< double > &binWidths, double xMin)
std::ofstream os_
STL namespace.