15#include "istlexception.hh"
18#include <dune/common/hybridutilities.hh>
51 int& counter,
int columns,
int width)
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)
133 for (
int j=0; j<m; j++)
150 typename FieldMatrix<K,1,1>::size_type I,
151 typename FieldMatrix<K,1,1>::size_type J,
152 typename FieldMatrix<K,1,1>::size_type therow,
153 int width,
int 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,
179 typename M::size_type i0=I;
180 for (
typename M::size_type i=0; i<A.
N(); i++)
182 if (therow>=i0 && therow<i0+MatrixDimension<M>::rowdim(A,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);
195 fill_row(s,MatrixDimension<M>::coldim(A,j),width,precision);
198 j0 += MatrixDimension<M>::coldim(A,j);
202 i0 += MatrixDimension<M>::rowdim(A,i);
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);
231 <<
",rowdim=" << MatrixDimension<M>::rowdim(A)
232 <<
",coldim=" << MatrixDimension<M>::coldim(A)
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);
289 <<
",rowdim=" << MatrixDimension<Matrix>::rowdim(mat)
290 <<
",coldim=" << MatrixDimension<Matrix>::coldim(mat)
295 for(Row row=mat.
begin(); row != mat.
end(); ++row) {
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,
387 s << rowOffset + 1 <<
" " << colOffset + 1 <<
" ";
388 MatlabPODWriter<FieldType>::write(value, s)<< std::endl;
398 template <
class MatrixType>
400 int externalRowOffset,
int externalColOffset,
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] +
411 MatrixDimension<MatrixType>::coldim(matrix,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()],
428 rowOffset += MatrixDimension<MatrixType>::rowdim(matrix, rowIdx);
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);
466 void writeVectorToMatlabHelper (
const V& v, std::ostream& stream)
470 stream << id(v) << std::endl;
473 for (
const auto& entry :
id(v))
474 writeVectorToMatlabHelper(entry, stream);
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);
503 writeVectorToMatlabHelper(vector, outStream);
504 outStream.precision(oldPrecision);
Implementation of the BCRSMatrix class.
A sparse block matrix with compressed row storage.
Definition: bcrsmatrix.hh:426
A dense n x m matrix.
Definition: fmatrix.hh:69
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
Implements a matrix constructed from a given type representing a field and compile-time given number ...
Implements a vector constructed from a given type representing a field and a compile-time given size.
#define DUNE_UNUSED_PARAMETER(parm)
A macro to mark intentionally unused function parameters with.
Definition: unused.hh:25
decltype(auto) ifElse(const Condition &condition, IfFunc &&ifFunc, ElseFunc &&elseFunc)
A conditional expression.
Definition: hybridutilities.hh:355
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 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
Some handy generic functions for ISTL matrices.
Dune namespace.
Definition: alignedallocator.hh:14
Whether this type acts as a scalar in the context of (hierarchically blocked) containers.
Definition: typetraits.hh:163
Definition of the DUNE_UNUSED macro for the case that config.h is not available.