votca 2024.2-dev
Loading...
Searching...
No Matches
table.cc
Go to the documentation of this file.
1/*
2 * Copyright 2009-2020 The VOTCA Development Team (http://www.votca.org)
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 */
17
18// Standard includes
19#include <fstream>
20#include <stdexcept>
21#include <vector>
22
23// Third party includes
24#include <boost/algorithm/string/replace.hpp>
25#include <boost/range/algorithm.hpp>
26
27// Local VOTCA includes
29#include "votca/tools/table.h"
31
32namespace votca {
33namespace tools {
34
35using namespace boost;
36using namespace std;
37
39 x_.conservativeResize(N);
40 y_.conservativeResize(N);
41 flags_.resize(N);
42 if (has_yerr_) {
43 yerr_.conservativeResize(N);
44 }
45}
46
47void Table::Load(string filename) {
48 ifstream in;
49 in.open(filename);
50 if (!in) {
51 throw runtime_error(string("error, cannot open file ") + filename);
52 }
53
54 setErrorDetails("file " + filename);
55 in >> *this;
56 in.close();
57}
58
59void Table::Save(string filename) const {
60 ofstream out;
61 out.open(filename);
62 if (!out) {
63 throw runtime_error(string("error, cannot open file ") + filename);
64 }
65
66 if (has_comment_) {
67 string str = "# " + comment_line_;
68 boost::replace_all(str, "\n", "\n# ");
69 boost::replace_all(str, "\\n", "\n# ");
70 out << str << endl;
71 }
72 out << (*this);
73
74 out.close();
75}
76
77void Table::clear(void) {
78 x_.resize(0);
79 y_.resize(0);
80 flags_.resize(0);
81 yerr_.resize(0);
82}
83
84double Table::getMaxY() const { return y_.maxCoeff(); }
85
86double Table::getMinY() const { return y_.minCoeff(); }
87
88double Table::getMaxX() const { return x_.maxCoeff(); }
89
90double Table::getMinX() const { return x_.minCoeff(); }
91
92// TODO: this functon is weired, reading occours twice, cleanup!!
93// TODO: modify function to work properly, when has_yerr_ is true
94istream &operator>>(istream &in, Table &t) {
95 size_t N = 0;
96 bool bHasN = false;
97 string line;
98 Index line_number = 0;
99 t.clear();
100
101 // read till the first data line
102 while (tools::getline(in, line)) {
103 line_number++;
104 string conversion_error = t.getErrorDetails() + ", line " +
105 boost::lexical_cast<string>(line_number);
106
107 // remove comments and xmgrace stuff
108 line = line.substr(0, line.find("#"));
109 line = line.substr(0, line.find("@"));
110
111 // tokenize string and put it to vector
112 vector<string> tokens = Tokenizer(line, " \t").ToVector();
113
114 // skip empty lines
115 if (tokens.size() == 0) {
116 continue;
117 }
118
119 // if first line is only 1 token, it's the size
120 if (tokens.size() == 1) {
121 N = lexical_cast<Index>(tokens[0], conversion_error);
122 bHasN = true;
123 } else if (tokens.size() == 2) {
124 // it's the first data line with 2 or 3 entries
125 t.push_back(std::stod(tokens[0]), std::stod(tokens[1]), 'i');
126 } else if (tokens.size() > 2) {
127 char flag = 'i';
128 string sflag = tokens.back();
129 if (sflag == "i" || sflag == "o" || sflag == "u") {
130 flag = sflag[0];
131 }
132 t.push_back(std::stod(tokens[0]), std::stod(tokens[1]), flag);
133 } else {
134 throw runtime_error("error, wrong table format");
135 }
136 }
137
138 // read the rest
139 while (tools::getline(in, line)) {
140 line_number++;
141 string conversion_error = t.getErrorDetails() + ", line " +
142 boost::lexical_cast<string>(line_number);
143
144 // remove comments and xmgrace stuff
145 line = line.substr(0, line.find("#"));
146 line = line.substr(0, line.find("@"));
147
148 // tokenize string and put it to vector
149 vector<string> tokens = Tokenizer(line, " \t").ToVector();
150
151 // skip empty lines
152 if (tokens.size() == 0) {
153 continue;
154 }
155
156 // it's a data line
157 if (tokens.size() == 2) {
158 t.push_back(std::stod(tokens[0]), std::stod(tokens[1]), 'i');
159 } else if (tokens.size() > 2) {
160 char flag = 'i';
161 if (tokens[2] == "i" || tokens[2] == "o" || tokens[2] == "u") {
162 flag = tokens[2][0];
163 }
164 t.push_back(std::stod(tokens[0]), std::stod(tokens[1]), flag);
165 } else {
166 // otherwise error
167 throw runtime_error("error, wrong table format");
168 }
169 // was size given and did we read N values?
170 if (bHasN) {
171 if (--N == 0) {
172 break;
173 }
174 }
175 }
176
177 return in;
178}
179
180void Table::GenerateGridSpacing(double min, double max, double spacing) {
181 Index vec_size = (Index)((max - min) / spacing + 1.00000001);
182 resize(vec_size);
183 // adjust spacing to generate evenly distributed bins till max
184 double spacing_scaled = (max - min) / (double(vec_size) - 1.0);
185
186 double r_init;
187 int i;
188 for (r_init = min, i = 0; i < vec_size - 1; r_init += spacing_scaled) {
189 x_[i++] = r_init;
190 }
191 x_[i] = max;
192}
193
194void Table::Smooth(Index Nsmooth) {
195
196 Index n_2 = size() - 2;
197 if (n_2 < 0) {
198 throw std::runtime_error(
199 "Smoothing only works for arrays of size 3 and larger");
200 }
201 for (Index i = 0; i < Nsmooth; i++) {
202 y_.segment(1, n_2) =
203 (0.25 * (y_.head(n_2) + 2 * y_.segment(1, n_2) + y_.tail(n_2))).eval();
204 }
205}
206
207std::ostream &operator<<(std::ostream &out, const Table &t) {
208 // TODO: use a smarter precision guess, XXX.YYYYY=8, so 10 should be enough
209 out.precision(10);
210
211 if (t.has_yerr_) {
212 for (Index i = 0; i < t.x_.size(); ++i) {
213 out << t.x_[i] << " " << t.y_[i] << " " << t.yerr_[i];
214 if (t.flags_[i] == '\0' || t.flags_[i] == ' ') {
215 out << "\n";
216 } else {
217 out << " " << t.flags_[i] << "\n";
218 }
219 }
220 } else {
221 for (Index i = 0; i < t.x_.size(); ++i) {
222 out << t.x_[i] << " " << t.y_[i];
223 if (t.flags_[i] == '\0' || t.flags_[i] == ' ') {
224 out << "\n";
225 } else {
226 out << " " << t.flags_[i] << "\n";
227 }
228 }
229 }
230 out << std::flush;
231 return out;
232}
233
234// TODO: modify this function to be able to treat has_yerr_ == true
235void Table::push_back(double x, double y, char flags) {
236 Index n = size();
237 resize(n + 1);
238 x_[n] = x;
239 y_[n] = y;
240 flags_[n] = flags;
241}
242} // namespace tools
243} // namespace votca
class to store tables like rdfs, tabulated potentials, etc
Definition table.h:36
double getMinY() const
Gets the minimum value in the y column.
Definition table.cc:86
Index size() const
Definition table.h:42
double getMaxX() const
Gets the maximum value in the x column.
Definition table.cc:88
Eigen::VectorXd & y()
Definition table.h:105
void GenerateGridSpacing(double min, double max, double spacing)
Definition table.cc:180
std::vector< char > & flags()
Definition table.h:106
Eigen::VectorXd yerr_
Definition table.h:119
std::string comment_line_
Definition table.h:128
double getMaxY() const
Gets the maximum value in the y column.
Definition table.cc:84
void Load(std::string filename)
Definition table.cc:47
Eigen::VectorXd y_
Definition table.h:117
void resize(Index N)
Definition table.cc:38
Eigen::VectorXd & x()
Definition table.h:104
const std::string & getErrorDetails()
Definition table.h:111
void Save(std::string filename) const
Definition table.cc:59
void Smooth(Index Nsmooth)
Definition table.cc:194
std::vector< char > flags_
Definition table.h:118
double getMinX() const
Gets the minimum value in the x column.
Definition table.cc:90
void setErrorDetails(std::string str)
Definition table.h:113
void push_back(double x, double y, char flags=' ')
Definition table.cc:235
Eigen::VectorXd x_
Definition table.h:116
break string into words
Definition tokenizer.h:72
std::vector< T > ToVector()
store all words in a vector of type T, does type conversion.
Definition tokenizer.h:109
STL namespace.
std::ostream & operator<<(std::ostream &out, const Correlate &c)
Definition correlate.h:53
Target lexical_cast(const Source &arg, const std::string &error)
istream & operator>>(istream &in, Table &t)
Definition table.cc:94
std::istream & getline(std::istream &is, std::string &str)
Wrapper for a getline function.
Definition getline.h:35
base class for all analysis tools
Definition basebead.h:33
Eigen::Index Index
Definition types.h:26