16 #include <dune/common/fvector.hh>
17 #include <dune/common/fmatrix.hh>
18 #include <dune/common/diagonalmatrix.hh>
19 #include <dune/common/unused.hh>
53 int& counter,
int columns,
int width,
56 for (
typename V::ConstIterator i=v.begin(); i!=v.end(); ++i)
67 template<
class K,
int n>
69 std::string rowtext,
int& counter,
int columns,
70 int width,
int precision)
72 DUNE_UNUSED_PARAMETER(precision);
74 for (
int i=0; i<n; i++)
76 if (counter%columns==0)
87 if (counter%columns==0)
102 std::string rowtext,
int columns=1,
int width=10,
109 std::ios_base::fmtflags oldflags = s.flags();
112 s.setf(std::ios_base::scientific, std::ios_base::floatfield);
113 int oldprec = s.precision();
114 s.precision(precision);
117 s << title <<
" [blocks=" << v.N() <<
",dimension=" << v.dim() <<
"]"
124 if (counter%columns!=0)
129 s.precision(oldprec);
145 inline void fill_row (std::ostream& s,
int m,
int width,
int precision)
147 DUNE_UNUSED_PARAMETER(precision);
148 for (
int j=0; j<m; j++)
164 void print_row (std::ostream& s,
const M&
A,
typename M::size_type I,
165 typename M::size_type J,
typename M::size_type therow,
166 int width,
int precision)
168 typename M::size_type i0=I;
169 for (
typename M::size_type i=0; i<A.N(); i++)
174 typename M::size_type j0=J;
175 for (
typename M::size_type j=0; j<A.M(); j++)
178 typename M::ConstColIterator it = A[i].find(j);
182 print_row(s,*it,i0,j0,therow,width,precision);
202 template<
class K,
int n,
int m>
209 DUNE_UNUSED_PARAMETER(J);
210 DUNE_UNUSED_PARAMETER(precision);
214 for (size_type i=0; i<n; i++)
216 for (
int j=0; j<m; j++)
236 int width,
int precision)
238 DUNE_UNUSED_PARAMETER(J);
239 DUNE_UNUSED_PARAMETER(precision);
245 s << static_cast<K>(
A);
259 std::string rowtext,
int width=10,
int precision=2)
263 std::ios_base::fmtflags oldflags = s.flags();
266 s.setf(std::ios_base::scientific, std::ios_base::floatfield);
267 int oldprec = s.precision();
268 s.precision(precision);
279 for (
typename M::size_type i=0; i<MatrixDimension<M>::rowdim(A); i++)
291 s.precision(oldprec);
315 template<
class B,
int n,
int m,
class A>
318 std::string title, std::string rowtext,
319 int width=3,
int precision=2)
323 std::ios_base::fmtflags oldflags = s.flags();
325 s.setf(std::ios_base::scientific, std::ios_base::floatfield);
326 int oldprec = s.precision();
327 s.precision(precision);
340 bool reachedEnd=
false;
343 for(
int innerrow=0; innerrow<n; ++innerrow) {
350 if(count>=skipcols+width)
353 if(count==skipcols) {
357 s <<
row.index()<<
": ";
360 s<<col.index()<<
": |";
362 if(count==skipcols) {
363 for(
typename std::string::size_type i=0; i < rowtext.length(); i++)
369 for(
int innercol=0; innercol < m; ++innercol) {
371 s<<(*col)[innerrow][innercol]<<
" ";
376 if(innerrow==n-1 && col==
row->end())
389 s.precision(oldprec);
395 struct MatlabPODWriter
397 static std::ostream& write(
const T& t, std::ostream& s)
404 struct MatlabPODWriter<std::complex<T> >
406 static std::ostream& write(
const std::complex<T>& t, std::ostream& s)
408 s << t.real() <<
" " << t.imag();
423 template <
class FieldType,
int dim>
426 for (
int i=0; i<dim; i++)
429 s << rowOffset + i + 1 <<
" " << colOffset + i + 1 <<
" ";
430 MatlabPODWriter<FieldType>::write(matrix.
scalar(), s)<< std::endl;
443 template <
class FieldType,
int dim>
446 for (
int i=0; i<dim; i++)
449 s << rowOffset + i + 1 <<
" " << colOffset + i + 1 <<
" ";
450 MatlabPODWriter<FieldType>::write(matrix.diagonal(i), s)<< std::endl;
463 template <
class FieldType,
int rows,
int cols>
466 int colOffset, std::ostream& s)
468 for (
int i=0; i<rows; i++)
469 for (
int j=0; j<cols; j++) {
471 s << rowOffset + i + 1 <<
" " << colOffset + j + 1 <<
" ";
472 MatlabPODWriter<FieldType>::write(matrix[i][j], s)<< std::endl;
483 template <
class MatrixType>
485 int externalRowOffset,
int externalColOffset,
489 std::vector<typename MatrixType::size_type> colOffset(matrix.M());
490 if (colOffset.size() > 0)
493 for (
typename MatrixType::size_type i=0; i<matrix.M()-1; i++)
494 colOffset[i+1] = colOffset[i] +
497 typename MatrixType::size_type rowOffset = 0;
500 for (
typename MatrixType::size_type rowIdx=0; rowIdx<matrix.N(); rowIdx++)
503 const typename MatrixType::row_type&
row = matrix[rowIdx];
505 typename MatrixType::row_type::ConstIterator cIt = row.begin();
506 typename MatrixType::row_type::ConstIterator cEndIt = row.end();
509 for (; cIt!=cEndIt; ++cIt)
511 externalRowOffset+rowOffset,
512 externalColOffset + colOffset[cIt.index()],
539 template <
class MatrixType>
541 const std::string& filename,
int outputPrecision = 18)
543 std::ofstream outStream(filename.c_str());
544 int oldPrecision = outStream.precision();
545 outStream.precision(outputPrecision);
548 outStream.precision(oldPrecision);
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:316
const K & scalar() const
Get const reference to the scalar diagonal value.
Definition: scaledidmatrix.hh:459
row_type::const_iterator ConstColIterator
Const iterator for the entries of each row.
Definition: matrix.hh:53
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:101
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:258
Some handy generic functions for ISTL matrices.
A sparse block matrix with compressed row storage.
Definition: bcrsmatrix.hh:413
void print_row(std::ostream &s, const M &A, typename M::size_type I, typename M::size_type J, typename M::size_type therow, int width, int precision)
Print one row of a matrix.
Definition: io.hh:164
void writeMatrixToMatlabHelper(const ScaledIdentityMatrix< FieldType, dim > &matrix, int rowOffset, int colOffset, std::ostream &s)
Helper method for the writeMatrixToMatlab routine.
Definition: io.hh:424
A generic dynamic dense matrix.
Definition: matrix.hh:24
size_type M() const
Return the number of columns.
Definition: matrix.hh:165
void writeMatrixToMatlab(const MatrixType &matrix, const std::string &filename, int outputPrecision=18)
Writes sparse matrix in a Matlab-readable format.
Definition: io.hh:540
Definition: bcrsmatrix.hh:72
RowIterator end()
Get iterator to one beyond last row.
Definition: matrix.hh:85
Col col
Definition: matrixmatrix.hh:347
Matrix & A
Definition: matrixmatrix.hh:216
Implementation of the BCRSMatrix class.
VariableBlockVector< T, A >::ConstIterator ConstRowIterator
Const iterator over the matrix rows.
Definition: matrix.hh:50
This file implements a quadratic matrix of fixed size which is a multiple of the identity.
A multiple of the identity matrix of static size.
Definition: scaledidmatrix.hh:27
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:145
Row row
Definition: matrixmatrix.hh:345
RowIterator begin()
Get iterator to first row.
Definition: matrix.hh:79
size_type N() const
Return the number of rows.
Definition: matrix.hh:160
std::size_t count
Definition: matrixmatrix.hh:215
A dynamic dense block matrix class.
Matrix & mat
Definition: matrixmatrix.hh:343
void recursive_printvector(std::ostream &s, const V &v, std::string rowtext, int &counter, int columns, int width, int precision)
Recursively print all the blocks.
Definition: io.hh:52
Definition: matrixutils.hh:25