KASKADE 7 development version
matlab.hh
Go to the documentation of this file.
1/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2/* */
3/* This file is part of the library KASKADE 7 */
4/* https://www.zib.de/research/projects/kaskade7-finite-element-toolbox */
5/* */
6/* Copyright (C) 2002-2018 Zuse Institute Berlin */
7/* */
8/* KASKADE 7 is distributed under the terms of the ZIB Academic License. */
9/* see $KASKADE/academic.txt */
10/* */
11/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
12
13#ifndef MATLAB_HH
14#define MATLAB_HH
15
16#include <fstream>
17#include <string>
18
19#include "linalg/triplet.hh"
20
21namespace Kaskade
22{
23
24 struct vec {
25 size_t N;
26 std::vector<double> data;
27
28 vec(size_t N) : N(N), data(N) {};
29
30 vec(std::ifstream &in) {
31 in.read(reinterpret_cast<char*>(&N), sizeof(size_t));
32 data.resize(N);
33 in.read(reinterpret_cast<char*>(data.data()), N * sizeof(double));
34 }
35
36 void write(std::ofstream &out) {
37 out.write(reinterpret_cast<char*>(&N), sizeof(size_t));
38 out.write(reinterpret_cast<char*>(data.data()), N * sizeof(double));
39 }
40 };
41
42 struct csr_matrix {
43 size_t N, nnz;
44 std::vector<size_t> row_ptrs;
45 std::vector<size_t> col_idxs;
46 std::vector<double> values;
47
48 csr_matrix(size_t N, size_t nnz) : N(N), nnz(nnz), row_ptrs(N + 1, 0), col_idxs(nnz), values(nnz)
49 {}
50
51 csr_matrix(std::ifstream &in) {
52 in.read(reinterpret_cast<char*>(&N), sizeof(size_t));
53 in.read(reinterpret_cast<char*>(&nnz), sizeof(size_t));
54 row_ptrs.resize(N + 1);
55 col_idxs.resize(nnz);
56 values.resize(nnz);
57 in.read(reinterpret_cast<char*>(row_ptrs.data()), (N + 1) * sizeof(size_t));
58 in.read(reinterpret_cast<char*>(col_idxs.data()), nnz * sizeof(size_t));
59 in.read(reinterpret_cast<char*>(values.data()), nnz * sizeof(double));
60 }
61
62 void write(std::ofstream &out) {
63 out.write(reinterpret_cast<char*>(&N), sizeof(size_t));
64 out.write(reinterpret_cast<char*>(&nnz), sizeof(size_t));
65 out.write(reinterpret_cast<char*>(row_ptrs.data()), (N + 1) * sizeof(size_t));
66 out.write(reinterpret_cast<char*>(col_idxs.data()), nnz * sizeof(size_t));
67 out.write(reinterpret_cast<char*>(values.data()), nnz * sizeof(double));
68 }
69 };
70
75 template <class Entry, class Index, class VEntry>
76 void writeToMatlabBinary(NumaBCRSMatrix<Entry,Index> const& A, Dune::BlockVector<VEntry> const& b, std::string const& basename, std::string const& path, int precision=16)
77 {
78 vec b_binary(b.N());
79 for (size_t i = 0; i < b.N(); i++) {
80 b_binary.data[i] = b[i];
81 }
82
83 int n = Entry::rows;
84 int m = Entry::cols;
85
86 csr_matrix A_binary(b.N(), 0);
87 int count = 0;
88 for (auto row = A.begin(); row!=A.end(); ++row)
89 {
90 for (int i=0; i<n; ++i)
91 {
92 auto ridx = row.index()*n + i;
93 for (auto col=row.begin(); col!=row.end(); ++col)
94 {
95 for (int j=0; j<m; ++j)
96 {
97 auto val = (*col)[i][j];
98 if (val != 0.0) {
99 auto cidx = col.index()*m + j;
100 A_binary.col_idxs.emplace_back(cidx);
101 A_binary.values.emplace_back(val);
102 count++;
103 }
104 }
105 }
106 A_binary.row_ptrs[ridx+1] = count;
107 }
108 }
109 A_binary.nnz = count;
110
111 // write the vector and matrix to disk
112 std::string fname_vec = path+"/"+basename+"_vec.bin";
113 std::string fname_A = path+"/"+basename+"_A.bin";
114 std::ofstream out(fname_vec.c_str(), std::ios::binary);
115 b_binary.write(out);
116 out.close();
117
118 out.open(fname_A.c_str(), std::ios::binary);
119 A_binary.write(out);
120 out.close();
121 }
122
127 template <class Entry, class Index>
128 void writeToMatlab(NumaBCRSMatrix<Entry,Index> const& A, std::string const& basename, std::string const& outPath,int precision=16)
129 {
130 std::string fname = outPath + basename + ".m";
131 std::ofstream f(fname.c_str());
132 f.precision(precision);
133
134 // Write header.
135 f << "function A = " << basename << '\n';
136
137 // write matrix in triplet format
138 f << "data = [\n";
139 int n = Entry::rows;
140 int m = Entry::cols;
141
142 for (auto row = A.begin(); row!=A.end(); ++row)
143 {
144 for (int i=0; i<n; ++i)
145 {
146 auto ridx = row.index()*n + i;
147 for (auto col=row.begin(); col!=row.end(); ++col)
148 {
149 for (int j=0; j<m; ++j)
150 {
151 auto cidx = col.index()*m + j;
152 f << ridx+1 << ' ' << cidx+1 << ' ' << (*col)[i][j] << std::endl;
153 }
154 }
155 }
156 }
157 f << "];\nA = sparse(data(:,1),data(:,2),data(:,3)," << n*A.N() << "," << m*A.M() << ");\n";
158 }
159
164 template <class Entry, class Index, class VEntry>
165 void writeToMatlab(NumaBCRSMatrix<Entry,Index> const& A, Dune::BlockVector<VEntry> const& b, std::string const& basename, int precision=16)
166 {
167 std::string fname = basename + ".m";
168 std::ofstream f(fname.c_str());
169 f.precision(precision);
170
171 // Write vector.
172 f << "function [A,b] = " << basename << '\n'
173 << " b = [\n";
174 for (size_t i=0; i<b.N(); ++i)
175 f << b[i] << std::endl;
176 f << "];\n";
177
178 // write matrix in triplet format
179 f << "data = [\n";
180 int n = Entry::rows;
181 int m = Entry::cols;
182
183 for (auto row = A.begin(); row!=A.end(); ++row)
184 {
185 for (int i=0; i<n; ++i)
186 {
187 auto ridx = row.index()*n + i;
188 for (auto col=row.begin(); col!=row.end(); ++col)
189 {
190 for (int j=0; j<m; ++j)
191 {
192 auto cidx = col.index()*m + j;
193 f << ridx+1 << ' ' << cidx+1 << ' ' << (*col)[i][j] << std::endl;
194 }
195 }
196 }
197 }
198 f << "];\nA = sparse(data(:,1),data(:,2),data(:,3)," << n*A.N() << "," << m*A.M() << ");\n";
199 }
200
213 template <class Assembler>
214 void writeToMatlab(Assembler const& assembler, std::string const& basename, int precision=16)
215 {
216 std::string fname = basename + ".m";
217 std::ofstream f(fname.c_str());
218 f.precision(precision);
219
220 f << "function [A,b] = " << basename << '\n'
221 << " b = [\n";
222
223 assembler.toSequence(0,Assembler::TestVariableSetDescription::noOfVariables,
224 std::ostream_iterator<typename Assembler::Scalar>(f,"\n"));
225 f << "\n];\ndata = [\n";
226
228
229 Matrix A = assembler.template get<Matrix>(false);
230
231 for (size_t i=0; i<A.ridx.size(); ++i)
232 f << A.ridx[i]+1 << ' ' << A.cidx[i]+1 << ' ' << A.data[i] << '\n';
233 f << "];\nA = sparse(data(:,1),data(:,2),data(:,3)," << A.N() << "," << A.M() << ");\n";
234 }
235
236 template <class Assembler>
237 void writeToMatlab(Assembler const& assembler, std::string const& path, std::string const& basename, int precision=16)
238 {
239 std::string fname = path + ".m";
240 std::ofstream f(fname.c_str());
241 f.precision(precision);
242
243 f << "function [A,b] = " << basename << '\n'
244 << " b = [\n";
245
246 assembler.toSequence(0,Assembler::TestVariableSetDescription::noOfVariables,
247 std::ostream_iterator<typename Assembler::Scalar>(f,"\n"));
248 f << "\n];\ndata = [\n";
249
251
252 Matrix A = assembler.template get<Matrix>(false);
253
254 for (size_t i=0; i<A.ridx.size(); ++i)
255 f << A.ridx[i]+1 << ' ' << A.cidx[i]+1 << ' ' << A.data[i] << '\n';
256 f << "];\nA = sparse(data(:,1),data(:,2),data(:,3)," << A.N() << "," << A.M() << ");\n";
257 }
258}
259
260
261#endif
iterator end()
returns an iterator to the first row
iterator begin()
returns an iterator to the first row
Index M() const
The number of columns.
Index N() const
The number of rows.
int count(Cell const &cell, int codim)
Computes the number of subentities of codimension c of a given entity.
Definition: fixdune.hh:716
void writeToMatlab(NumaBCRSMatrix< Entry, Index > const &A, std::string const &basename, std::string const &outPath, int precision=16)
Writes the given matrix to an executable matlab file.
Definition: matlab.hh:128
void writeToMatlabBinary(NumaBCRSMatrix< Entry, Index > const &A, Dune::BlockVector< VEntry > const &b, std::string const &basename, std::string const &path, int precision=16)
Writes the given matrix and vector to an binary file based zero.
Definition: matlab.hh:76
std::vector< size_t > row_ptrs
Definition: matlab.hh:44
void write(std::ofstream &out)
Definition: matlab.hh:62
csr_matrix(std::ifstream &in)
Definition: matlab.hh:51
std::vector< size_t > col_idxs
Definition: matlab.hh:45
std::vector< double > values
Definition: matlab.hh:46
csr_matrix(size_t N, size_t nnz)
Definition: matlab.hh:48
void write(std::ofstream &out)
Definition: matlab.hh:36
std::vector< double > data
Definition: matlab.hh:26
vec(size_t N)
Definition: matlab.hh:28
size_t N
Definition: matlab.hh:25
vec(std::ifstream &in)
Definition: matlab.hh:30