OPALX (Object Oriented Parallel Accelerator Library for Exascal) MINIorX
OPALX
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
22#include "PartBunch/PartBunch.h"
23#include "OPALconfig.h"
25#include "Utilities/Util.h"
26
27#include "Utility/IpplInfo.h"
28
29#include <queue>
30
31extern Inform* gmsg;
32
33SDDSWriter::SDDSWriter(const std::string& fname, bool restart)
34 : fname_m(fname), mode_m(std::ios::out), indent_m(" ") {
35 namespace fs = boost::filesystem;
36
37 if (fs::exists(fname_m) && restart) {
38 mode_m = std::ios::app;
39 *gmsg<< "* Appending data to existing data file: '" << fname_m << "'" << endl;
40 } else {
41 *gmsg << "* Creating new file for data: '" << fname_m << "'" << endl;
42 }
43}
44
45void SDDSWriter::rewindLines(size_t numberOfLines) {
46 if (numberOfLines == 0 || ippl::Comm->rank() != 0) {
47 return;
48 }
49
50 std::string line;
51 std::queue<std::string> allLines;
52 std::fstream fs;
53
54 fs.open(fname_m.c_str(), std::fstream::in);
55
56 if (!fs.is_open())
57 return;
58
59 while (getline(fs, line)) {
60 allLines.push(line);
61 }
62 fs.close();
63
64 fs.open(fname_m.c_str(), std::fstream::out);
65
66 if (!fs.is_open())
67 return;
68
69 while (allLines.size() > numberOfLines) {
70 fs << allLines.front() << "\n";
71 allLines.pop();
72 }
73 fs.close();
74}
75
77 if (ippl::Comm->rank() != 0)
78 return;
79
80 std::string versionFile;
82 parser.run();
83 parser.getParameterValue("revision", versionFile);
84
85 std::string line;
86 std::queue<std::string> allLines;
87 std::fstream fs;
88
89 fs.open(fname_m.c_str(), std::fstream::in);
90
91 if (!fs.is_open())
92 return;
93
94 while (getline(fs, line)) {
95 allLines.push(line);
96 }
97 fs.close();
98
99 fs.open(fname_m.c_str(), std::fstream::out);
100
101 if (!fs.is_open())
102 return;
103
104 while (!allLines.empty()) {
105 line = allLines.front();
106
107 if (line != versionFile) {
108 fs << line << "\n";
109 } else {
110 fs << OPAL_PROJECT_NAME << " " << OPAL_PROJECT_VERSION << " git rev. #"
111 << Util::getGitRevision() << "\n";
112 }
113
114 allLines.pop();
115 }
116
117 fs.close();
118}
119
120double SDDSWriter::getLastValue(const std::string& column) {
122 parser.run();
123 double val = 0.0;
124 parser.getValue(-1, column, val);
125 return val;
126}
127
129 if (ippl::Comm->rank() != 0 || os_m.is_open())
130 return;
131
132 os_m.open(fname_m.c_str(), mode_m);
133 os_m.precision(precision_m);
134 os_m.setf(std::ios::scientific, std::ios::floatfield);
135}
136
138 if (ippl::Comm->rank() == 0 && os_m.is_open()) {
139 os_m.close();
140 }
141}
142
144 if (ippl::Comm->rank() != 0 || mode_m == std::ios::app)
145 return;
146
147 this->writeDescription();
148
149 this->writeParameters();
150
151 this->writeColumns();
152
153 this->writeInfo();
154
155 mode_m = std::ios::app;
156}
157
159 os_m << "SDDS1" << std::endl;
160 os_m << "&description\n"
161 << indent_m << "text=\"" << desc_m.first << "\",\n"
162 << indent_m << "contents=\"" << desc_m.second << "\"\n"
163 << "&end\n";
164}
165
167 while (!params_m.empty()) {
168 param_t param = params_m.front();
169
170 os_m << "&parameter\n"
171 << indent_m << "name=" << std::get<0>(param) << ",\n"
172 << indent_m << "type=" << std::get<1>(param) << ",\n"
173 << indent_m << "description=\"" << std::get<2>(param) << "\"\n"
174 << "&end\n";
175
176 params_m.pop();
177 }
178}
179
181 columns_m.writeHeader(os_m, indent_m);
182}
183
185 os_m << "&data\n"
186 << indent_m << "mode=" << info_m.first << ",\n"
187 << indent_m << "no_row_counts=" << info_m.second << "\n"
188 << "&end";
189
190 while (!paramValues_m.empty()) {
191 os_m << "\n" << paramValues_m.front();
192
193 paramValues_m.pop();
194 }
195
196 os_m << std::endl;
197}
198
200 std::stringstream revision;
201 revision << OPAL_PROJECT_NAME << " " << OPAL_PROJECT_VERSION << " "
202 << "git rev. #" << Util::getGitRevision();
203
204 std::string flavor;
205 if (OpalData::getInstance()->isInOPALTMode()) {
206 flavor = "opal-t";
207 } else if (OpalData::getInstance()->isInOPALCyclMode()) {
208 flavor = "opal-cycl";
209 } else {
210 flavor = "opal-map";
211 }
212
213 addParameter("processors", "long", "Number of Cores used", ippl::Comm->size());
214
215 addParameter("revision", "string", "git revision of opal", revision.str());
216
217 addParameter("flavor", "string", "OPAL flavor that wrote file", flavor);
218}
#define OPAL_PROJECT_VERSION
Definition OPALconfig.h:5
#define OPAL_PROJECT_NAME
Definition OPALconfig.h:2
Inform * gmsg
Definition changes.cpp:7
STL namespace.
std::string getGitRevision()
Definition Util.cpp:32
static OpalData * getInstance()
Definition OpalData.cpp:195
void replaceVersionString()
SDDSWriter(const std::string &fname, bool restart)
desc_t desc_m
Definition SDDSWriter.h:130
void rewindLines(size_t numberOfLines)
delete the last 'numberOfLines' lines of the file 'fileName'
SDDSColumnSet columns_m
Definition SDDSWriter.h:110
void writeInfo()
data_t info_m
Definition SDDSWriter.h:133
static constexpr unsigned int precision_m
Definition SDDSWriter.h:135
double getLastValue(const std::string &column)
void writeColumns()
void addDefaultParameters()
void writeParameters()
std::string indent_m
Definition SDDSWriter.h:128
std::queue< std::string > paramValues_m
Definition SDDSWriter.h:132
void writeHeader()
Write SDDS header.
std::ios_base::openmode mode_m
First write to the statistics output file.
Definition SDDSWriter.h:108
void writeDescription()
std::tuple< std::string, std::string, std::string > param_t
Definition SDDSWriter.h:44
std::queue< param_t > params_m
Definition SDDSWriter.h:131
std::string fname_m
Definition SDDSWriter.h:100
std::ofstream os_m
Definition SDDSWriter.h:126
void addParameter(const std::string &name, const std::string &type, const std::string &desc, const T &value)
Definition SDDSWriter.h:147
void getParameterValue(std::string parameter_name, T &nval)
Definition SDDSParser.h:187
void getValue(int t, std::string column_name, T &nval)
Definition SDDSParser.h:86