OPAL (Object Oriented Parallel Accelerator Library)  2024.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 //
19 #include "Structure/SDDSWriter.h"
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 
32 SDDSWriter::SDDSWriter(const std::string& fname, bool restart)
33  : fname_m(fname)
34  , mode_m(std::ios::out)
35  , indent_m(" ")
36 {
37  namespace fs = std::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 
48 void 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  fs.open (fname_m.c_str(), std::fstream::out);
67 
68  if (!fs.is_open()) return;
69 
70  while (allLines.size() > numberOfLines) {
71  fs << allLines.front() << "\n";
72  allLines.pop();
73  }
74  fs.close();
75 }
76 
77 
79 
80  if (Ippl::myNode() != 0)
81  return;
82 
83  std::string versionFile;
84  SDDS::SDDSParser parser(fname_m);
85  parser.run();
86  parser.getParameterValue("revision", versionFile);
87 
88  std::string line;
89  std::queue<std::string> allLines;
90  std::fstream fs;
91 
92  fs.open (fname_m.c_str(), std::fstream::in);
93 
94  if (!fs.is_open()) return;
95 
96  while (getline(fs, line)) {
97  allLines.push(line);
98  }
99  fs.close();
100 
101  fs.open (fname_m.c_str(), std::fstream::out);
102 
103  if (!fs.is_open()) return;
104 
105  while (!allLines.empty()) {
106  line = allLines.front();
107 
108  if (line != versionFile) {
109  fs << line << "\n";
110  } else {
111  fs << OPAL_PROJECT_NAME << " "
112  << OPAL_PROJECT_VERSION << " git rev. #"
113  << Util::getGitRevision() << "\n";
114  }
115 
116  allLines.pop();
117  }
118 
119  fs.close();
120 }
121 
122 
123 double SDDSWriter::getLastValue(const std::string& column) {
124  SDDS::SDDSParser parser(fname_m);
125  parser.run();
126  double val = 0.0;
127  parser.getValue(-1, column, val);
128  return val;
129 }
130 
131 
133  if ( Ippl::myNode() != 0 || os_m.is_open() )
134  return;
135 
136  os_m.open(fname_m.c_str(), mode_m);
137  os_m.precision(precision_m);
138  os_m.setf(std::ios::scientific, std::ios::floatfield);
139 }
140 
141 
143  if ( Ippl::myNode() == 0 && os_m.is_open() ) {
144  os_m.close();
145  }
146 }
147 
148 
150  if ( Ippl::myNode() != 0 || mode_m == std::ios::app )
151  return;
152 
153  this->writeDescription();
154 
155  this->writeParameters();
156 
157  this->writeColumns();
158 
159  this->writeInfo();
160 
161  mode_m = std::ios::app;
162 }
163 
164 
166  os_m << "SDDS1" << std::endl;
167  os_m << "&description\n"
168  << indent_m << "text=\"" << desc_m.first << "\",\n"
169  << indent_m << "contents=\"" << desc_m.second << "\"\n"
170  << "&end\n";
171 }
172 
173 
175  while ( !params_m.empty() ) {
176  param_t param = params_m.front();
177 
178  os_m << "&parameter\n"
179  << indent_m << "name=" << std::get<0>(param) << ",\n"
180  << indent_m << "type=" << std::get<1>(param) << ",\n"
181  << indent_m << "description=\"" << std::get<2>(param) << "\"\n"
182  << "&end\n";
183 
184  params_m.pop();
185  }
186 }
187 
188 
191 }
192 
193 
195  os_m << "&data\n"
196  << indent_m << "mode=" << info_m.first << ",\n"
197  << indent_m << "no_row_counts=" << info_m.second << "\n"
198  << "&end";
199 
200  while (!paramValues_m.empty()) {
201  os_m << "\n" << paramValues_m.front();
202 
203  paramValues_m.pop();
204  }
205 
206  os_m << std::endl;
207 }
208 
210  std::stringstream revision;
211  revision << OPAL_PROJECT_NAME << " "
212  << OPAL_PROJECT_VERSION << " "
213  << "git rev. #" << Util::getGitRevision();
214 
215  std::string flavor;
216  if (OpalData::getInstance()->isInOPALTMode()) {
217  flavor = "opal-t";
218  } else if (OpalData::getInstance()->isInOPALCyclMode()) {
219  flavor = "opal-cycl";
220  } else {
221  flavor = "opal-map";
222  }
223 
224  addParameter("processors", "long", "Number of Cores used", Ippl::getNodes());
225 
226  addParameter("revision", "string", "git revision of opal", revision.str());
227 
228  addParameter("flavor", "string", "OPAL flavor that wrote file", flavor);
229 }
static constexpr unsigned int precision_m
Definition: SDDSWriter.h:148
static OpalData * getInstance()
Definition: OpalData.cpp:196
void open()
Definition: SDDSWriter.cpp:132
std::ofstream os_m
Definition: SDDSWriter.h:138
#define OPAL_PROJECT_VERSION
Definition: config.h.in:5
void writeParameters()
Definition: SDDSWriter.cpp:174
void writeInfo()
Definition: SDDSWriter.cpp:194
static int myNode()
Definition: IpplInfo.cpp:691
SDDSColumnSet columns_m
Definition: SDDSWriter.h:122
std::string getGitRevision()
Definition: Util.cpp:33
void addParameter(const std::string &name, const std::string &type, const std::string &desc, const T &value)
Definition: SDDSWriter.h:166
SDDSWriter(const std::string &fname, bool restart)
Definition: SDDSWriter.cpp:32
#define OPAL_PROJECT_NAME
Definition: config.h.in:2
std::string fname_m
Definition: SDDSWriter.h:112
#define INFOMSG(msg)
Definition: IpplInfo.h:348
Inform & endl(Inform &inf)
Definition: Inform.cpp:42
std::tuple< std::string, std::string, std::string > param_t
Definition: SDDSWriter.h:48
void getValue(int t, std::string column_name, T &nval)
Definition: SDDSParser.h:85
std::queue< param_t > params_m
Definition: SDDSWriter.h:143
void writeColumns()
Definition: SDDSWriter.cpp:189
void addDefaultParameters()
Definition: SDDSWriter.cpp:209
static int getNodes()
Definition: IpplInfo.cpp:670
std::queue< std::string > paramValues_m
Definition: SDDSWriter.h:144
FRONT * fs
Definition: hypervolume.cpp:59
void writeDescription()
Definition: SDDSWriter.cpp:165
void rewindLines(size_t numberOfLines)
delete the last &#39;numberOfLines&#39; lines of the file &#39;fileName&#39;
Definition: SDDSWriter.cpp:48
void writeHeader()
Write SDDS header.
Definition: SDDSWriter.cpp:149
void replaceVersionString()
Definition: SDDSWriter.cpp:78
void getParameterValue(std::string parameter_name, T &nval)
Definition: SDDSParser.h:186
void writeHeader(std::ostream &os, const std::string &indent) const
desc_t desc_m
Definition: SDDSWriter.h:142
double getLastValue(const std::string &column)
Definition: SDDSWriter.cpp:123
std::string indent_m
Definition: SDDSWriter.h:140
data_t info_m
Definition: SDDSWriter.h:145
void close()
Definition: SDDSWriter.cpp:142
std::ios_base::openmode mode_m
First write to the statistics output file.
Definition: SDDSWriter.h:120