3 #ifndef DUNE_ISTL_IO_HH
4 #define DUNE_ISTL_IO_HH
16 #include <dune/common/fvector.hh>
17 #include <dune/common/fmatrix.hh>
18 #include <dune/common/hybridutilities.hh>
19 #include <dune/common/unused.hh>
51 int& counter,
int columns,
int width)
53 Hybrid::ifElse(IsNumber<V>(),
56 if (counter%columns==0)
67 if (counter%columns==0)
72 for (
const auto& entry :
id(v))
86 void printvector (std::ostream& s,
const V& v, std::string title,
87 std::string rowtext,
int columns=1,
int width=10,
94 std::ios_base::fmtflags oldflags = s.flags();
97 s.setf(std::ios_base::scientific, std::ios_base::floatfield);
98 int oldprec = s.precision();
99 s.precision(precision);
102 s << title <<
" [blocks=" << v.N() <<
",dimension=" << v.dim() <<
"]"
109 if (counter%columns!=0)
114 s.precision(oldprec);
130 inline void fill_row (std::ostream& s,
int m,
int width,
int precision)
132 DUNE_UNUSED_PARAMETER(precision);
133 for (
int j=0; j<m; j++)
153 int width,
int precision,
154 typename std::enable_if_t<Dune::IsNumber<K>::value>* sfinae =
nullptr)
156 DUNE_UNUSED_PARAMETER(I);
157 DUNE_UNUSED_PARAMETER(J);
158 DUNE_UNUSED_PARAMETER(therow);
159 DUNE_UNUSED_PARAMETER(precision);
174 void print_row (std::ostream& s,
const M& A,
typename M::size_type I,
175 typename M::size_type J,
typename M::size_type therow,
176 int width,
int precision,
177 typename std::enable_if_t<!Dune::IsNumber<M>::value>* sfinae =
nullptr)
179 typename M::size_type i0=I;
180 for (
typename M::size_type i=0; i<A.
N(); i++)
185 typename M::size_type j0=J;
186 for (
typename M::size_type j=0; j<A.
M(); j++)
189 typename M::ConstColIterator it = A[i].find(j);
193 print_row(s,*it,i0,j0,therow,width,precision);
216 std::string rowtext,
int width=10,
int precision=2)
220 std::ios_base::fmtflags oldflags = s.flags();
223 s.setf(std::ios_base::scientific, std::ios_base::floatfield);
224 int oldprec = s.precision();
225 s.precision(precision);
236 for (
typename M::size_type i=0; i<MatrixDimension<M>::rowdim(A); i++)
248 s.precision(oldprec);
272 template<
class B,
int n,
int m,
class A>
275 std::string title, std::string rowtext,
276 int width=3,
int precision=2)
280 std::ios_base::fmtflags oldflags = s.flags();
282 s.setf(std::ios_base::scientific, std::ios_base::floatfield);
283 int oldprec = s.precision();
284 s.precision(precision);
297 bool reachedEnd=
false;
300 for(
int innerrow=0; innerrow<n; ++innerrow) {
303 Col
col=row->begin();
304 for(;
col != row->end(); ++
col,++count) {
307 if(count>=skipcols+width)
310 if(count==skipcols) {
314 s << row.index()<<
": ";
317 s<<
col.index()<<
": |";
319 if(count==skipcols) {
320 for(
typename std::string::size_type i=0; i < rowtext.length(); i++)
326 for(
int innercol=0; innercol < m; ++innercol) {
328 s<<(*col)[innerrow][innercol]<<
" ";
333 if(innerrow==n-1 &&
col==row->end())
346 s.precision(oldprec);
352 struct MatlabPODWriter
354 static std::ostream& write(
const T& t, std::ostream& s)
361 struct MatlabPODWriter<std::complex<T> >
363 static std::ostream& write(
const std::complex<T>& t, std::ostream& s)
365 s << t.real() <<
" " << t.imag();
380 template <
class FieldType>
382 int rowOffset,
int colOffset,
384 typename std::enable_if_t<Dune::IsNumber<FieldType>::value>* sfinae =
nullptr)
387 s << rowOffset + 1 <<
" " << colOffset + 1 <<
" ";
388 MatlabPODWriter<FieldType>::write(value, s)<< std::endl;
398 template <
class MatrixType>
400 int externalRowOffset,
int externalColOffset,
402 typename std::enable_if_t<!Dune::IsNumber<MatrixType>::value>* sfinae =
nullptr)
405 std::vector<typename MatrixType::size_type> colOffset(matrix.M());
406 if (colOffset.size() > 0)
409 for (
typename MatrixType::size_type i=0; i<matrix.M()-1; i++)
410 colOffset[i+1] = colOffset[i] +
413 typename MatrixType::size_type rowOffset = 0;
416 for (
typename MatrixType::size_type rowIdx=0; rowIdx<matrix.N(); rowIdx++)
418 auto cIt = matrix[rowIdx].begin();
419 auto cEndIt = matrix[rowIdx].end();
422 for (; cIt!=cEndIt; ++cIt)
424 externalRowOffset+rowOffset,
425 externalColOffset + colOffset[cIt.index()],
452 template <
class MatrixType>
454 const std::string& filename,
int outputPrecision = 18)
456 std::ofstream outStream(filename.c_str());
457 int oldPrecision = outStream.precision();
458 outStream.precision(outputPrecision);
461 outStream.precision(oldPrecision);
468 Hybrid::ifElse(IsNumber<V>(),
470 stream << id(v) << std::endl;
473 for (
const auto& entry :
id(v))
495 template <
class VectorType>
497 const std::string& filename,
int outputPrecision = 18)
499 std::ofstream outStream(filename.c_str());
500 int oldPrecision = outStream.precision();
501 outStream.precision(outputPrecision);
504 outStream.precision(oldPrecision);
Implementation of the BCRSMatrix class.
Some handy generic functions for ISTL matrices.
Col col
Definition: matrixmatrix.hh:349
Matrix & mat
Definition: matrixmatrix.hh:345
void writeMatrixToMatlab(const MatrixType &matrix, const std::string &filename, int outputPrecision=18)
Writes sparse matrix in a Matlab-readable format.
Definition: io.hh:453
void print_row(std::ostream &s, const K &value, typename FieldMatrix< K, 1, 1 >::size_type I, typename FieldMatrix< K, 1, 1 >::size_type J, typename FieldMatrix< K, 1, 1 >::size_type therow, int width, int precision, typename std::enable_if_t< Dune::IsNumber< K >::value > *sfinae=nullptr)
Print one row of a matrix, specialization for number types.
Definition: io.hh:149
void printmatrix(std::ostream &s, const M &A, std::string title, std::string rowtext, int width=10, int precision=2)
Print a generic block matrix.
Definition: io.hh:215
void printvector(std::ostream &s, const V &v, std::string title, std::string rowtext, int columns=1, int width=10, int precision=2)
Print an ISTL vector.
Definition: io.hh:86
void writeMatrixToMatlabHelper(const FieldType &value, int rowOffset, int colOffset, std::ostream &s, typename std::enable_if_t< Dune::IsNumber< FieldType >::value > *sfinae=nullptr)
Helper method for the writeMatrixToMatlab routine.
Definition: io.hh:381
void writeVectorToMatlabHelper(const V &v, std::ostream &stream)
Definition: io.hh:466
void writeVectorToMatlab(const VectorType &vector, const std::string &filename, int outputPrecision=18)
Writes vectors in a Matlab-readable format.
Definition: io.hh:496
void recursive_printvector(std::ostream &s, const V &v, std::string rowtext, int &counter, int columns, int width)
Recursively print a vector.
Definition: io.hh:50
void printSparseMatrix(std::ostream &s, const BCRSMatrix< FieldMatrix< B, n, m >, A > &mat, std::string title, std::string rowtext, int width=3, int precision=2)
Prints a BCRSMatrix with fixed sized blocks.
Definition: io.hh:273
void fill_row(std::ostream &s, int m, int width, int precision)
Print a row of zeros for a non-existing block.
Definition: io.hh:130
Definition: allocator.hh:7
Definition: matrixutils.hh:209
static auto coldim(const M &A)
Definition: matrixutils.hh:217
static auto rowdim(const M &A)
Definition: matrixutils.hh:212
A sparse block matrix with compressed row storage.
Definition: bcrsmatrix.hh:426
ConstIterator class for sequential access.
Definition: matrix.hh:401
A generic dynamic dense matrix.
Definition: matrix.hh:558
RowIterator end()
Get iterator to one beyond last row.
Definition: matrix.hh:616
RowIterator begin()
Get iterator to first row.
Definition: matrix.hh:610
row_type::const_iterator ConstColIterator
Const iterator for the entries of each row.
Definition: matrix.hh:586
size_type M() const
Return the number of columns.
Definition: matrix.hh:696
size_type N() const
Return the number of rows.
Definition: matrix.hh:691
Definition: matrixutils.hh:26