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/dynmatrix.hh> 19 #include <dune/common/diagonalmatrix.hh> 20 #include <dune/common/unused.hh> 54 int& counter,
int columns,
int width,
57 for (
typename V::ConstIterator i=v.begin(); i!=v.end(); ++i)
68 template<
class K,
int n>
70 std::string rowtext,
int& counter,
int columns,
71 int width,
int precision)
73 DUNE_UNUSED_PARAMETER(precision);
75 for (
int i=0; i<n; i++)
77 if (counter%columns==0)
88 if (counter%columns==0)
103 std::string rowtext,
int columns=1,
int width=10,
110 std::ios_base::fmtflags oldflags = s.flags();
113 s.setf(std::ios_base::scientific, std::ios_base::floatfield);
114 int oldprec = s.precision();
115 s.precision(precision);
118 s << title <<
" [blocks=" << v.N() <<
",dimension=" << v.dim() <<
"]" 125 if (counter%columns!=0)
130 s.precision(oldprec);
146 inline void fill_row (std::ostream& s,
int m,
int width,
int precision)
148 DUNE_UNUSED_PARAMETER(precision);
149 for (
int j=0; j<m; j++)
165 void print_row (std::ostream& s,
const M& A,
typename M::size_type I,
166 typename M::size_type J,
typename M::size_type therow,
167 int width,
int precision)
169 typename M::size_type i0=I;
170 for (
typename M::size_type i=0; i<A.N(); i++)
175 typename M::size_type j0=J;
176 for (
typename M::size_type j=0; j<A.M(); j++)
179 typename M::ConstColIterator it = A[i].find(j);
183 print_row(s,*it,i0,j0,therow,width,precision);
203 template<
class K,
int n,
int m>
210 DUNE_UNUSED_PARAMETER(J);
211 DUNE_UNUSED_PARAMETER(precision);
215 for (size_type i=0; i<n; i++)
217 for (
int j=0; j<m; j++)
237 int width,
int precision)
239 DUNE_UNUSED_PARAMETER(J);
240 DUNE_UNUSED_PARAMETER(precision);
246 s << static_cast<K>(A);
260 std::string rowtext,
int width=10,
int precision=2)
264 std::ios_base::fmtflags oldflags = s.flags();
267 s.setf(std::ios_base::scientific, std::ios_base::floatfield);
268 int oldprec = s.precision();
269 s.precision(precision);
280 for (
typename M::size_type i=0; i<MatrixDimension<M>::rowdim(A); i++)
292 s.precision(oldprec);
316 template<
class B,
int n,
int m,
class A>
319 std::string title, std::string rowtext,
320 int width=3,
int precision=2)
324 std::ios_base::fmtflags oldflags = s.flags();
326 s.setf(std::ios_base::scientific, std::ios_base::floatfield);
327 int oldprec = s.precision();
328 s.precision(precision);
341 bool reachedEnd=
false;
344 for(
int innerrow=0; innerrow<n; ++innerrow) {
347 Col
col=row->begin();
348 for(; col != row->end(); ++
col,++count) {
351 if(count>=skipcols+width)
354 if(count==skipcols) {
358 s << row.index()<<
": ";
361 s<<col.index()<<
": |";
363 if(count==skipcols) {
364 for(
typename std::string::size_type i=0; i < rowtext.length(); i++)
370 for(
int innercol=0; innercol < m; ++innercol) {
372 s<<(*col)[innerrow][innercol]<<
" ";
377 if(innerrow==n-1 && col==row->end())
390 s.precision(oldprec);
396 struct MatlabPODWriter
398 static std::ostream& write(
const T& t, std::ostream& s)
405 struct MatlabPODWriter<std::complex<T> >
407 static std::ostream& write(
const std::complex<T>& t, std::ostream& s)
409 s << t.real() <<
" " << t.imag();
424 template <
class FieldType,
int dim>
427 for (
int i=0; i<dim; i++)
430 s << rowOffset + i + 1 <<
" " << colOffset + i + 1 <<
" ";
431 MatlabPODWriter<FieldType>::write(matrix.
scalar(), s)<< std::endl;
444 template <
class FieldType,
int dim>
447 for (
int i=0; i<dim; i++)
450 s << rowOffset + i + 1 <<
" " << colOffset + i + 1 <<
" ";
451 MatlabPODWriter<FieldType>::write(matrix.diagonal(i), s)<< std::endl;
464 template <
class FieldType,
int rows,
int cols>
467 int colOffset, std::ostream& s)
469 for (
int i=0; i<rows; i++)
470 for (
int j=0; j<cols; j++) {
472 s << rowOffset + i + 1 <<
" " << colOffset + j + 1 <<
" ";
473 MatlabPODWriter<FieldType>::write(matrix[i][j], s)<< std::endl;
486 template <
class FieldType>
488 int colOffset, std::ostream& s)
490 for (
int i=0; i<matrix.N(); i++)
491 for (
int j=0; j<matrix.M(); j++) {
493 s << rowOffset + i + 1 <<
" " << colOffset + j + 1 <<
" ";
494 MatlabPODWriter<FieldType>::write(matrix[i][j], s)<< std::endl;
505 template <
class MatrixType>
507 int externalRowOffset,
int externalColOffset,
511 std::vector<typename MatrixType::size_type> colOffset(matrix.M());
512 if (colOffset.size() > 0)
515 for (
typename MatrixType::size_type i=0; i<matrix.M()-1; i++)
516 colOffset[i+1] = colOffset[i] +
519 typename MatrixType::size_type rowOffset = 0;
522 for (
typename MatrixType::size_type rowIdx=0; rowIdx<matrix.N(); rowIdx++)
525 const typename MatrixType::row_type& row = matrix[rowIdx];
527 typename MatrixType::row_type::ConstIterator cIt = row.begin();
528 typename MatrixType::row_type::ConstIterator cEndIt = row.end();
531 for (; cIt!=cEndIt; ++cIt)
533 externalRowOffset+rowOffset,
534 externalColOffset + colOffset[cIt.index()],
561 template <
class MatrixType>
563 const std::string& filename,
int outputPrecision = 18)
565 std::ofstream outStream(filename.c_str());
566 int oldPrecision = outStream.precision();
567 outStream.precision(outputPrecision);
570 outStream.precision(oldPrecision);
size_type N() const
Return the number of rows.
Definition: matrix.hh:160
Matrix & mat
Definition: matrixmatrix.hh:343
size_type M() const
Return the number of columns.
Definition: matrix.hh:165
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:259
row_type::const_iterator ConstColIterator
Const iterator for the entries of each row.
Definition: matrix.hh:53
Definition: matrixutils.hh:25
A dynamic dense block matrix class.
void writeMatrixToMatlab(const MatrixType &matrix, const std::string &filename, int outputPrecision=18)
Writes sparse matrix in a Matlab-readable format.
Definition: io.hh:562
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:53
This file implements a quadratic matrix of fixed size which is a multiple of the identity.
A generic dynamic dense matrix.
Definition: matrix.hh:24
void writeMatrixToMatlabHelper(const ScaledIdentityMatrix< FieldType, dim > &matrix, int rowOffset, int colOffset, std::ostream &s)
Helper method for the writeMatrixToMatlab routine.
Definition: io.hh:425
A sparse block matrix with compressed row storage.
Definition: bcrsmatrix.hh:412
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:102
const K & scalar() const
Get const reference to the scalar diagonal value.
Definition: scaledidmatrix.hh:459
RowIterator end()
Get iterator to one beyond last row.
Definition: matrix.hh:85
VariableBlockVector< T, A >::ConstIterator ConstRowIterator
Const iterator over the matrix rows.
Definition: matrix.hh:50
Col col
Definition: matrixmatrix.hh:347
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:317
Implementation of the BCRSMatrix class.
Some handy generic functions for ISTL matrices.
A multiple of the identity matrix of static size.
Definition: scaledidmatrix.hh:27
Definition: bcrsmatrix.hh:71
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:146
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:165
Definition: basearray.hh:19
RowIterator begin()
Get iterator to first row.
Definition: matrix.hh:79