OPAL (Object Oriented Parallel Accelerator Library) 2022.1
OPAL
SDDSWriter.cpp
Go to the documentation of this file.
1//
2// Class SDDSWriter
3// This class is the base class for all SDDS writers.
4//
5// Copyright (c) 2019, Matthias Frey, Paul Scherrer Institut, Villigen PSI, Switzerland
6// Christof Metzger-Kraus, Open Sourcerer
7// All rights reserved
8//
9// This file is part of OPAL.
10//
11// OPAL is free software: you can redistribute it and/or modify
12// it under the terms of the GNU General Public License as published by
13// the Free Software Foundation, either version 3 of the License, or
14// (at your option) any later version.
15//
16// You should have received a copy of the GNU General Public License
17// along with OPAL. If not, see <https://www.gnu.org/licenses/>.
18//
20
23#include "OPALconfig.h"
24#include "Util/SDDSParser.h"
25#include "Utilities/Util.h"
26
27#include "Utility/IpplInfo.h"
28
29#include <queue>
30
31
32SDDSWriter::SDDSWriter(const std::string& fname, bool restart)
33 : fname_m(fname)
34 , mode_m(std::ios::out)
35 , indent_m(" ")
36{
37 namespace fs = boost::filesystem;
38
39 if (fs::exists(fname_m) && restart) {
40 mode_m = std::ios::app;
41 INFOMSG("* Appending data to existing data file: '" << fname_m << "'" << endl);
42 } else {
43 INFOMSG("* Creating new file for data: '" << fname_m << "'" << endl);
44 }
45}
46
47
48void SDDSWriter::rewindLines(size_t numberOfLines) {
49 if (numberOfLines == 0 || Ippl::myNode() != 0) {
50 return;
51 }
52
53 std::string line;
54 std::queue<std::string> allLines;
55 std::fstream fs;
56
57 fs.open (fname_m.c_str(), std::fstream::in);
58
59 if (!fs.is_open()) return;
60
61 while (getline(fs, line)) {
62 allLines.push(line);
63 }
64 fs.close();
65
66
67 fs.open (fname_m.c_str(), std::fstream::out);
68
69 if (!fs.is_open()) return;
70
71 while (allLines.size() > numberOfLines) {
72 fs << allLines.front() << "\n";
73 allLines.pop();
74 }
75 fs.close();
76}
77
78
80
81 if (Ippl::myNode() != 0)
82 return;
83
84 std::string versionFile;
86 parser.run();
87 parser.getParameterValue("revision", versionFile);
88
89 std::string line;
90 std::queue<std::string> allLines;
91 std::fstream fs;
92
93 fs.open (fname_m.c_str(), std::fstream::in);
94
95 if (!fs.is_open()) return;
96
97 while (getline(fs, line)) {
98 allLines.push(line);
99 }
100 fs.close();
101
102
103 fs.open (fname_m.c_str(), std::fstream::out);
104
105 if (!fs.is_open()) return;
106
107 while (!allLines.empty()) {
108 line = allLines.front();
109
110 if (line != versionFile) {
111 fs << line << "\n";
112 } else {
113 fs << OPAL_PROJECT_NAME << " "
114 << OPAL_PROJECT_VERSION << " git rev. #"
115 << Util::getGitRevision() << "\n";
116 }
117
118 allLines.pop();
119 }
120
121 fs.close();
122}
123
124
125double SDDSWriter::getLastValue(const std::string& column) {
127 parser.run();
128 double val = 0.0;
129 parser.getValue(-1, column, val);
130 return val;
131}
132
133
135 if ( Ippl::myNode() != 0 || os_m.is_open() )
136 return;
137
138 os_m.open(fname_m.c_str(), mode_m);
139 os_m.precision(precision_m);
140 os_m.setf(std::ios::scientific, std::ios::floatfield);
141}
142
143
145 if ( Ippl::myNode() == 0 && os_m.is_open() ) {
146 os_m.close();
147 }
148}
149
150
152 if ( Ippl::myNode() != 0 || mode_m == std::ios::app )
153 return;
154
155 this->writeDescription();
156
157 this->writeParameters();
158
159 this->writeColumns();
160
161 this->writeInfo();
162
163 mode_m = std::ios::app;
164}
165
166
168 os_m << "SDDS1" << std::endl;
169 os_m << "&description\n"
170 << indent_m << "text=\"" << desc_m.first << "\",\n"
171 << indent_m << "contents=\"" << desc_m.second << "\"\n"
172 << "&end\n";
173}
174
175
177 while ( !params_m.empty() ) {
178 param_t param = params_m.front();
179
180 os_m << "&parameter\n"
181 << indent_m << "name=" << std::get<0>(param) << ",\n"
182 << indent_m << "type=" << std::get<1>(param) << ",\n"
183 << indent_m << "description=\"" << std::get<2>(param) << "\"\n"
184 << "&end\n";
185
186 params_m.pop();
187 }
188}
189
190
193}
194
195
197 os_m << "&data\n"
198 << indent_m << "mode=" << info_m.first << ",\n"
199 << indent_m << "no_row_counts=" << info_m.second << "\n"
200 << "&end";
201
202 while (!paramValues_m.empty()) {
203 os_m << "\n" << paramValues_m.front();
204
205 paramValues_m.pop();
206 }
207
208 os_m << std::endl;
209}
210
212 std::stringstream revision;
213 revision << OPAL_PROJECT_NAME << " "
214 << OPAL_PROJECT_VERSION << " "
215 << "git rev. #" << Util::getGitRevision();
216
217 std::string flavor;
218 if (OpalData::getInstance()->isInOPALTMode()) {
219 flavor = "opal-t";
220 } else if (OpalData::getInstance()->isInOPALCyclMode()) {
221 flavor = "opal-cycl";
222 } else {
223 flavor = "opal-map";
224 }
225
226 addParameter("processors", "long", "Number of Cores used", Ippl::getNodes());
227
228 addParameter("revision", "string", "git revision of opal", revision.str());
229
230 addParameter("flavor", "string", "OPAL flavor that wrote file", flavor);
231}
#define OPAL_PROJECT_VERSION
Definition: OPALconfig.h:5
#define OPAL_PROJECT_NAME
Definition: OPALconfig.h:2
Inform & endl(Inform &inf)
Definition: Inform.cpp:42
#define INFOMSG(msg)
Definition: IpplInfo.h:348
std::string getGitRevision()
Definition: Util.cpp:32
FRONT * fs
Definition: hypervolume.cpp:59
static OpalData * getInstance()
Definition: OpalData.cpp:196
void writeHeader(std::ostream &os, const std::string &indent) const
void replaceVersionString()
Definition: SDDSWriter.cpp:79
SDDSWriter(const std::string &fname, bool restart)
Definition: SDDSWriter.cpp:32
desc_t desc_m
Definition: SDDSWriter.h:147
void rewindLines(size_t numberOfLines)
delete the last 'numberOfLines' lines of the file 'fileName'
Definition: SDDSWriter.cpp:48
SDDSColumnSet columns_m
Definition: SDDSWriter.h:126
void writeInfo()
Definition: SDDSWriter.cpp:196
data_t info_m
Definition: SDDSWriter.h:150
static constexpr unsigned int precision_m
Definition: SDDSWriter.h:153
double getLastValue(const std::string &column)
Definition: SDDSWriter.cpp:125
void writeColumns()
Definition: SDDSWriter.cpp:191
void addDefaultParameters()
Definition: SDDSWriter.cpp:211
void writeParameters()
Definition: SDDSWriter.cpp:176
std::string indent_m
Definition: SDDSWriter.h:145
void open()
Definition: SDDSWriter.cpp:134
std::queue< std::string > paramValues_m
Definition: SDDSWriter.h:149
void writeHeader()
Write SDDS header.
Definition: SDDSWriter.cpp:151
std::ios_base::openmode mode_m
First write to the statistics output file.
Definition: SDDSWriter.h:124
void writeDescription()
Definition: SDDSWriter.cpp:167
void close()
Definition: SDDSWriter.cpp:144
std::tuple< std::string, std::string, std::string > param_t
Definition: SDDSWriter.h:50
std::queue< param_t > params_m
Definition: SDDSWriter.h:148
std::string fname_m
Definition: SDDSWriter.h:116
std::ofstream os_m
Definition: SDDSWriter.h:143
void addParameter(const std::string &name, const std::string &type, const std::string &desc, const T &value)
Definition: SDDSWriter.h:172
static int getNodes()
Definition: IpplInfo.cpp:670
static int myNode()
Definition: IpplInfo.cpp:691
void getParameterValue(std::string parameter_name, T &nval)
Definition: SDDSParser.h:186
void getValue(int t, std::string column_name, T &nval)
Definition: SDDSParser.h:85