16#include <dune/common/fvector.hh>
17#include <dune/common/fmatrix.hh>
18#include <dune/common/hybridutilities.hh>
50 int& counter,
int columns,
int width)
52 if constexpr (IsNumber<V>())
55 if (counter%columns==0)
66 if (counter%columns==0)
72 for (
const auto& entry : 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, [[maybe_unused]]
int precision)
132 for (
int j=0; j<m; j++)
153 [[maybe_unused]]
int precision,
154 typename std::enable_if_t<Dune::IsNumber<K>::value>* sfinae =
nullptr)
169 void print_row (std::ostream& s,
const M& A,
typename M::size_type I,
170 typename M::size_type J,
typename M::size_type therow,
171 int width,
int precision,
172 typename std::enable_if_t<!Dune::IsNumber<M>::value>* sfinae =
nullptr)
174 typename M::size_type i0=I;
175 for (
typename M::size_type i=0; i<A.
N(); i++)
180 typename M::size_type j0=J;
181 for (
typename M::size_type j=0; j<A.
M(); j++)
184 typename M::ConstColIterator it = A[i].find(j);
188 print_row(s,*it,i0,j0,therow,width,precision);
211 std::string rowtext,
int width=10,
int precision=2)
215 std::ios_base::fmtflags oldflags = s.flags();
218 s.setf(std::ios_base::scientific, std::ios_base::floatfield);
219 int oldprec = s.precision();
220 s.precision(precision);
231 for (
typename M::size_type i=0; i<MatrixDimension<M>::rowdim(A); i++)
243 s.precision(oldprec);
267 template<
class B,
int n,
int m,
class A>
270 std::string title, std::string rowtext,
271 int width=3,
int precision=2)
275 std::ios_base::fmtflags oldflags = s.flags();
277 s.setf(std::ios_base::scientific, std::ios_base::floatfield);
278 int oldprec = s.precision();
279 s.precision(precision);
292 bool reachedEnd=
false;
295 for(
int innerrow=0; innerrow<n; ++innerrow) {
298 Col
col=row->begin();
299 for(;
col != row->end(); ++
col,++count) {
302 if(count>=skipcols+width)
305 if(count==skipcols) {
309 s << row.index()<<
": ";
312 s<<
col.index()<<
": |";
314 if(count==skipcols) {
315 for(
typename std::string::size_type i=0; i < rowtext.length(); i++)
321 for(
int innercol=0; innercol < m; ++innercol) {
323 s<<(*col)[innerrow][innercol]<<
" ";
328 if(innerrow==n-1 &&
col==row->end())
341 s.precision(oldprec);
347 struct MatlabPODWriter
349 static std::ostream& write(
const T& t, std::ostream& s)
356 struct MatlabPODWriter<std::complex<T> >
358 static std::ostream& write(
const std::complex<T>& t, std::ostream& s)
360 s << t.real() <<
" " << t.imag();
375 template <
class FieldType>
377 int rowOffset,
int colOffset,
379 typename std::enable_if_t<Dune::IsNumber<FieldType>::value>* sfinae =
nullptr)
382 s << rowOffset + 1 <<
" " << colOffset + 1 <<
" ";
383 MatlabPODWriter<FieldType>::write(value, s)<< std::endl;
393 template <
class MatrixType>
395 int externalRowOffset,
int externalColOffset,
397 typename std::enable_if_t<!Dune::IsNumber<MatrixType>::value>* sfinae =
nullptr)
400 std::vector<typename MatrixType::size_type> colOffset(matrix.M());
401 if (colOffset.size() > 0)
404 for (
typename MatrixType::size_type i=0; i<matrix.M()-1; i++)
405 colOffset[i+1] = colOffset[i] +
408 typename MatrixType::size_type rowOffset = 0;
411 for (
typename MatrixType::size_type rowIdx=0; rowIdx<matrix.N(); rowIdx++)
413 auto cIt = matrix[rowIdx].begin();
414 auto cEndIt = matrix[rowIdx].end();
417 for (; cIt!=cEndIt; ++cIt)
419 externalRowOffset+rowOffset,
420 externalColOffset + colOffset[cIt.index()],
447 template <
class MatrixType>
449 const std::string& filename,
int outputPrecision = 18)
451 std::ofstream outStream(filename.c_str());
452 int oldPrecision = outStream.precision();
453 outStream.precision(outputPrecision);
456 outStream.precision(oldPrecision);
463 if constexpr (IsNumber<V>()) {
464 stream << v << std::endl;
466 for (
const auto& entry : v)
488 template <
class VectorType>
490 const std::string& filename,
int outputPrecision = 18)
492 std::ofstream outStream(filename.c_str());
493 int oldPrecision = outStream.precision();
494 outStream.precision(outputPrecision);
497 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:448
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:148
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:210
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:376
void writeVectorToMatlabHelper(const V &v, std::ostream &stream)
Definition: io.hh:461
void writeVectorToMatlab(const VectorType &vector, const std::string &filename, int outputPrecision=18)
Writes vectors in a Matlab-readable format.
Definition: io.hh:489
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:49
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:268
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:9
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:464
ConstIterator class for sequential access.
Definition: matrix.hh:402
A generic dynamic dense matrix.
Definition: matrix.hh:559
RowIterator end()
Get iterator to one beyond last row.
Definition: matrix.hh:618
RowIterator begin()
Get iterator to first row.
Definition: matrix.hh:612
row_type::const_iterator ConstColIterator
Const iterator for the entries of each row.
Definition: matrix.hh:587
size_type M() const
Return the number of columns.
Definition: matrix.hh:698
size_type N() const
Return the number of rows.
Definition: matrix.hh:693
Definition: matrixutils.hh:25