votca 2024.2-dev
Loading...
Searching...
No Matches
imcio.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 <iomanip>
21#include <iostream>
22
23// Third party includes
24#include <boost/algorithm/string/trim.hpp>
25#include <boost/lexical_cast.hpp>
26
27// VOTCA includes
28#include <votca/tools/getline.h>
30#include <votca/tools/table.h>
32
33// Local VOTCA includes
34#include "votca/csg/imcio.h"
35
36namespace votca {
37namespace csg {
38
39using group_matrix = Eigen::MatrixXd;
40using namespace std;
41
42void imcio_write_dS(const string &file, const tools::Table &dS,
43 const std::list<Index> *list) {
44 // write the dS
45 ofstream out_dS;
46 out_dS.open(file);
47 out_dS << setprecision(8);
48 if (!out_dS) {
49 throw runtime_error(string("error, cannot open file ") + file);
50 }
51
52 if (list == nullptr) {
53 for (Index i = 0; i < dS.size(); ++i) {
54 out_dS << dS.x(i) << " " << dS.y(i) << endl;
55 }
56 } else {
57 for (Index i : *list) {
58 out_dS << dS.x(i) << " " << dS.y(i) << endl;
59 }
60 }
61
62 out_dS.close();
63 cout << "written " << file << endl;
64}
65
66void imcio_write_matrix(const string &file, const Eigen::MatrixXd &gmc,
67 const std::list<Index> *list) {
68 ofstream out_A;
69 out_A.open(file);
70 out_A << setprecision(8);
71
72 if (!out_A) {
73 throw runtime_error(string("error, cannot open file ") + file);
74 }
75
76 if (list == nullptr) {
77 for (Index i = 0; i < gmc.rows(); ++i) {
78 for (Index j = 0; j < gmc.cols(); ++j) {
79 out_A << gmc(i, j) << " ";
80 }
81 out_A << endl;
82 }
83 } else {
84 for (Index i : *list) {
85 for (Index j : *list) {
86 out_A << gmc(i, j) << " ";
87 }
88 out_A << endl;
89 }
90 }
91 out_A.close();
92 cout << "written " << file << endl;
93}
94
96 const string &file,
97 const std::vector<std::pair<std::string, tools::RangeParser> > &ranges) {
98 // write the index
99 ofstream out_idx;
100 out_idx.open(file);
101
102 if (!out_idx) {
103 throw runtime_error(string("error, cannot open file ") + file);
104 }
105
106 for (const auto &range : ranges) {
107 out_idx << range.first << " " << range.second << endl;
108 }
109 out_idx.close();
110 cout << "written " << file << endl;
111}
112
113Eigen::MatrixXd imcio_read_matrix(const std::string &filename) {
114 std::ifstream intt;
115 intt.open(filename);
116 if (!intt) {
117 throw std::runtime_error(std::string("error, cannot open file ") +
118 filename);
119 }
120
121 std::string line;
122 std::vector<double> result;
123 Index numrows = 0;
124 size_t numcols = 0;
125 while (tools::getline(intt, line)) {
126 if (line[0] == '#') {
127 continue;
128 }
129 votca::tools::Tokenizer tok(line, " \t");
130 std::vector<std::string> tokens = tok.ToVector();
131 if (numrows == 0) {
132 numcols = tokens.size();
133 } else if (numcols != tokens.size()) {
134 throw std::runtime_error(
135 "Matrix has not the same number of entries in each row.");
136 }
137 for (const std::string &s : tokens) {
138 result.push_back(std::stod(s));
139 }
140 numrows++;
141 }
142 intt.close();
143
144 return Eigen::Map<Eigen::MatrixXd>(result.data(), numrows, numcols);
145}
146
147std::vector<std::pair<std::string, tools::RangeParser> > imcio_read_index(
148 const string &filename) {
149 ifstream in;
150 in.open(filename);
151 if (!in) {
152 throw runtime_error(string("error, cannot open file ") + filename);
153 }
154
155 std::vector<std::pair<std::string, tools::RangeParser> > indeces;
156 string line;
157 // read till the first data line
158 while (tools::getline(in, line)) {
159 // remove comments and xmgrace stuff
160 line = line.substr(0, line.find("#"));
161 line = line.substr(0, line.find("@"));
162
163 boost::trim(line);
164 size_t found = line.find(" ");
165 if (found == string::npos) {
166 throw runtime_error(string("wrong format in ") + filename);
167 }
168
169 string name = line.substr(0, found);
170 string range = line.substr(found);
172 rp.Parse(range);
173 indeces.push_back(std::pair<std::string, tools::RangeParser>(name, rp));
174 }
175 in.close();
176 return indeces;
177}
178
179} // namespace csg
180} // namespace votca
void Parse(std::string str)
class to store tables like rdfs, tabulated potentials, etc
Definition table.h:36
double & x(Index i)
Definition table.h:44
Index size() const
Definition table.h:42
double & y(Index i)
Definition table.h:45
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::vector< std::pair< std::string, tools::RangeParser > > imcio_read_index(const std::string &filename)
Definition imcio.cc:147
void imcio_write_dS(const std::string &file, const tools::Table &dS, const std::list< Index > *list=nullptr)
Definition imcio.cc:42
Eigen::MatrixXd imcio_read_matrix(const std::string &filename)
Definition imcio.cc:113
void imcio_write_index(const std::string &file, const std::vector< std::pair< std::string, tools::RangeParser > > &ranges)
Definition imcio.cc:95
void imcio_write_matrix(const std::string &file, const Eigen::MatrixXd &gmc, const std::list< Index > *list=nullptr)
Definition imcio.cc:66
Eigen::MatrixXd group_matrix
Definition imcio.cc:39
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