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);
577 for (
const auto& entry : v)
582 template<
class K,
int n>
585 for (
const auto& entry : v)
587 s << entry << std::endl;
595 for (
const auto& entry : v)
597 s << entry << std::endl;
602 template<
class K, std::
size_t n>
605 for (
const auto& entry : v)
607 s << entry << std::endl;
628 template <
class VectorType>
630 const std::string& filename,
int outputPrecision = 18)
632 std::ofstream outStream(filename.c_str());
633 int oldPrecision = outStream.precision();
634 outStream.precision(outputPrecision);
637 outStream.precision(oldPrecision);