15#include "istlexception.hh"
18#include <dune/common/hybridutilities.hh>
50 int& counter,
int columns,
int width)
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++)
149 [[maybe_unused]]
typename FieldMatrix<K,1,1>::size_type I,
150 [[maybe_unused]]
typename FieldMatrix<K,1,1>::size_type J,
151 [[maybe_unused]]
typename FieldMatrix<K,1,1>::size_type therow,
153 [[maybe_unused]]
int precision,
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,
174 typename M::size_type i0=I;
175 for (
typename M::size_type i=0; i<A.
N(); i++)
177 if (therow>=i0 && therow<i0+MatrixDimension<M>::rowdim(A,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);
190 fill_row(s,MatrixDimension<M>::coldim(A,j),width,precision);
193 j0 += MatrixDimension<M>::coldim(A,j);
197 i0 += MatrixDimension<M>::rowdim(A,i);
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);
226 <<
",rowdim=" << MatrixDimension<M>::rowdim(A)
227 <<
",coldim=" << MatrixDimension<M>::coldim(A)
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);
284 <<
",rowdim=" << MatrixDimension<Matrix>::rowdim(mat)
285 <<
",coldim=" << MatrixDimension<Matrix>::coldim(mat)
290 for(Row row=mat.
begin(); row != mat.
end(); ++row) {
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,
382 s << rowOffset + 1 <<
" " << colOffset + 1 <<
" ";
383 MatlabPODWriter<FieldType>::write(value, s)<< std::endl;
393 template <
class MatrixType>
395 int externalRowOffset,
int externalColOffset,
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] +
406 MatrixDimension<MatrixType>::coldim(matrix,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()],
423 rowOffset += MatrixDimension<MatrixType>::rowdim(matrix, rowIdx);
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);
461 void writeVectorToMatlabHelper (
const V& v, std::ostream& stream)
463 if constexpr (IsNumber<V>()) {
464 stream << v << std::endl;
466 for (
const auto& entry : v)
467 writeVectorToMatlabHelper(entry, stream);
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);
496 writeVectorToMatlabHelper(vector, outStream);
497 outStream.precision(oldPrecision);
Implementation of the BCRSMatrix class.
A sparse block matrix with compressed row storage.
Definition: bcrsmatrix.hh:464
A dense n x m matrix.
Definition: fmatrix.hh:69
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
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.
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 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
Some handy generic functions for ISTL matrices.
Dune namespace.
Definition: alignedallocator.hh:11
Whether this type acts as a scalar in the context of (hierarchically blocked) containers.
Definition: typetraits.hh:194