votca 2024-dev
Loading...
Searching...
No Matches
eigenio_matrixmarket.cc
Go to the documentation of this file.
1/*
2 * Copyright 2009-2020 The VOTCA Development Team
3 * (http://www.votca.org)
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License")
6 *
7 * You may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 */
19
20// Local VOTCA includes
21#include "votca/tools/eigen.h"
23#include "votca/tools/types.h"
24
25namespace votca {
26namespace tools {
27
28namespace EigenIO_MatrixMarket {
29
30Eigen::VectorXd ReadVector(const std::string& filename) {
31
32 Eigen::VectorXd output;
33
34 bool success = Eigen::loadMarketVector(output, filename);
35 if (!success) {
36 throw std::runtime_error("Loading Vector from " + filename + " failed");
37 }
38 return output;
39}
40
41void WriteVector(const std::string& filename, const Eigen::VectorXd& output) {
42 bool success = Eigen::saveMarketVector(output, filename);
43 if (!success) {
44 throw std::runtime_error("Writing Vector to " + filename + " failed");
45 }
46}
47
48void WriteMatrix(const std::string& filename, const Eigen::MatrixXd& output) {
49
50 std::ofstream ofs;
51 ofs.open(filename, std::ofstream::out);
52 if (!ofs.is_open()) {
53 throw std::runtime_error("Could not create " + filename);
54 }
55
56 ofs << "%%MatrixMarket matrix array real general\n";
57 ofs << output.rows() << " " << output.cols() << "\n";
58 const Eigen::Map<const Eigen::VectorXd> temp(output.data(), output.size());
59 ofs << temp;
60 ofs.close();
61}
62
63Eigen::MatrixXd ReadMatrix(const std::string& filename) {
64 int sym = 0;
65 bool iscomplex = false; // has to be false because getMarketHeader only sets
66 // it to true but not to false
67 bool isvector = false;
68 bool success = Eigen::getMarketHeader(filename, sym, iscomplex, isvector);
69 if (!success) {
70 throw std::runtime_error("Could not read " + filename);
71 }
72 if (sym != 0) {
73 throw std::runtime_error("Only supports reading in general matrices");
74 }
75 if (iscomplex) {
76 throw std::runtime_error(
77 "Only supports reading in matrices with real numbers");
78 }
79
80 if (!isvector) {
81 throw std::runtime_error(
82 "Use the eigen method `loadMarket` for sparse data");
83 }
84
85 std::ifstream in(filename, std::ios::in);
86 if (!in) {
87 throw std::runtime_error("Could not open " + filename);
88 }
89
90 std::string line;
91 Index cols = 0;
92 Index rows = 0;
93 do { // Skip comments
94 std::getline(in, line);
95 assert(in.good());
96 } while (line[0] == '%');
97 std::istringstream newline(line);
98 newline >> rows >> cols;
99 assert(rows > 0 && cols > 0);
100
101 Index i = 0;
102 Index n = rows * cols;
103 std::vector<double> entries;
104 entries.reserve(n);
105 double value;
106 while (std::getline(in, line) && (i < n)) {
107 std::istringstream newline2(line);
108 newline2 >> value;
109 entries.push_back(value);
110 i++;
111 }
112 in.close();
113
114 return Eigen::Map<Eigen::MatrixXd>(entries.data(), rows, cols);
115}
116
117} // namespace EigenIO_MatrixMarket
118
119} // namespace tools
120} // namespace votca
Eigen::MatrixXd ReadMatrix(const std::string &filename)
void WriteMatrix(const std::string &filename, const Eigen::MatrixXd &output)
Eigen::VectorXd ReadVector(const std::string &filename)
void WriteVector(const std::string &filename, const Eigen::VectorXd &output)
base class for all analysis tools
Definition basebead.h:33
Eigen::Index Index
Definition types.h:26