OPALX (Object Oriented Parallel Accelerator Library for Exascal) master (dc2a29eed580)
OPALX
Loading...
Searching...
No Matches
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 "BuildInfo.h"
23#include "PartBunch/PartBunch.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 = std::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()) return;
57
58 while (getline(fs, line)) {
59 allLines.push(line);
60 }
61 fs.close();
62
63 fs.open(fname_m.c_str(), std::fstream::out);
64
65 if (!fs.is_open()) return;
66
67 while (allLines.size() > numberOfLines) {
68 fs << allLines.front() << "\n";
69 allLines.pop();
70 }
71 fs.close();
72}
73
75 if (ippl::Comm->rank() != 0) return;
76
77 std::string versionFile;
79 parser.run();
80 parser.getParameterValue("revision", versionFile);
81
82 std::string line;
83 std::queue<std::string> allLines;
84 std::fstream fs;
85
86 fs.open(fname_m.c_str(), std::fstream::in);
87
88 if (!fs.is_open()) return;
89
90 while (getline(fs, line)) {
91 allLines.push(line);
92 }
93 fs.close();
94
95 fs.open(fname_m.c_str(), std::fstream::out);
96
97 if (!fs.is_open()) return;
98
99 while (!allLines.empty()) {
100 line = allLines.front();
101
102 if (line != versionFile) {
103 fs << line << "\n";
104 } else {
105 fs << buildinfo::project_name << " " << buildinfo::project_version << " git rev. #"
106 << Util::getGitRevision() << "\n";
107 }
108
109 allLines.pop();
110 }
111
112 fs.close();
113}
114
115double SDDSWriter::getLastValue(const std::string& column) {
117 parser.run();
118 double val = 0.0;
119 parser.getValue(-1, column, val);
120 return val;
121}
122
124 if (ippl::Comm->rank() != 0 || os_m.is_open()) return;
125
126 os_m.open(fname_m.c_str(), mode_m);
127 os_m.precision(precision_m);
128 os_m.setf(std::ios::scientific, std::ios::floatfield);
129}
130
132 if (ippl::Comm->rank() == 0 && os_m.is_open()) {
133 os_m.close();
134 }
135}
136
138 if (ippl::Comm->rank() != 0 || mode_m == std::ios::app) return;
139
140 this->writeDescription();
141
142 this->writeParameters();
143
144 this->writeColumns();
145
146 this->writeInfo();
147
148 mode_m = std::ios::app;
149}
150
152 os_m << "SDDS1" << std::endl;
153 os_m << "&description\n"
154 << indent_m << "text=\"" << desc_m.first << "\",\n"
155 << indent_m << "contents=\"" << desc_m.second << "\"\n"
156 << "&end\n";
157}
158
160 while (!params_m.empty()) {
161 param_t param = params_m.front();
162
163 os_m << "&parameter\n"
164 << indent_m << "name=" << std::get<0>(param) << ",\n"
165 << indent_m << "type=" << std::get<1>(param) << ",\n"
166 << indent_m << "description=\"" << std::get<2>(param) << "\"\n"
167 << "&end\n";
168
169 params_m.pop();
170 }
171}
172
174
176 os_m << "&data\n"
177 << indent_m << "mode=" << info_m.first << ",\n"
178 << indent_m << "no_row_counts=" << info_m.second << "\n"
179 << "&end";
180
181 while (!paramValues_m.empty()) {
182 os_m << "\n" << paramValues_m.front();
183
184 paramValues_m.pop();
185 }
186
187 os_m << std::endl;
188}
189
191 std::stringstream revision;
192 revision << buildinfo::project_name << " " << buildinfo::project_version << " "
193 << "git rev. #" << Util::getGitRevision();
194
195 std::string flavor;
196 if (OpalData::getInstance()->isInOPALTMode()) {
197 flavor = "opal-t";
198 } else if (OpalData::getInstance()->isInOPALCyclMode()) {
199 flavor = "opal-cycl";
200 } else {
201 flavor = "opal-map";
202 }
203
204 addParameter("processors", "long", "Number of Cores used", ippl::Comm->size());
205
206 addParameter("revision", "string", "git revision of opal", revision.str());
207
208 addParameter("flavor", "string", "OPAL flavor that wrote file", flavor);
209}
Inform * gmsg
Definition changes.cpp:7
Template PIC bunch: IPPL PicManager, shared field mesh/solver, and multiple particle containers.
Inform * gmsg
Definition changes.cpp:7
static OpalData * getInstance()
Definition OpalData.cpp:193
void writeHeader(std::ostream &os, const std::string &indent) const
void replaceVersionString()
SDDSWriter(const std::string &fname, bool restart)
desc_t desc_m
Definition SDDSWriter.h:131
void rewindLines(size_t numberOfLines)
delete the last 'numberOfLines' lines of the file 'fileName'
SDDSColumnSet columns_m
Definition SDDSWriter.h:111
void writeInfo()
data_t info_m
Definition SDDSWriter.h:134
static constexpr unsigned int precision_m
Definition SDDSWriter.h:136
double getLastValue(const std::string &column)
void writeColumns()
void addDefaultParameters()
void writeParameters()
std::string indent_m
Definition SDDSWriter.h:129
std::queue< std::string > paramValues_m
Definition SDDSWriter.h:133
void writeHeader()
Write SDDS header.
std::ios_base::openmode mode_m
First write to the statistics output file.
Definition SDDSWriter.h:109
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:132
std::string fname_m
Definition SDDSWriter.h:101
std::ofstream os_m
Definition SDDSWriter.h:127
void addParameter(const std::string &name, const std::string &type, const std::string &desc, const T &value)
Definition SDDSWriter.h:146
void getParameterValue(std::string parameter_name, T &nval)
Definition SDDSParser.h:184
void getValue(int t, std::string column_name, T &nval)
Definition SDDSParser.h:83
std::string getGitRevision()
Definition Util.cpp:32
STL namespace.