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