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/hybridutilities.hh>
19 #include <dune/common/unused.hh>
51 int& counter,
int columns,
int width)
53 Hybrid::ifElse(IsNumber<V>(),
56 if (counter%columns==0)
67 if (counter%columns==0)
72 for (
const auto& entry :
id(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,
int precision)
132 DUNE_UNUSED_PARAMETER(precision);
133 for (
int j=0; j<m; j++)
153 int width,
int precision,
154 typename std::enable_if_t<Dune::IsNumber<K>::value>* sfinae =
nullptr)
156 DUNE_UNUSED_PARAMETER(I);
157 DUNE_UNUSED_PARAMETER(J);
158 DUNE_UNUSED_PARAMETER(therow);
159 DUNE_UNUSED_PARAMETER(precision);
174 void print_row (std::ostream& s,
const M& A,
typename M::size_type I,
175 typename M::size_type J,
typename M::size_type therow,
176 int width,
int precision,
177 typename std::enable_if_t<!Dune::IsNumber<M>::value>* sfinae =
nullptr)
179 typename M::size_type i0=I;
180 for (
typename M::size_type i=0; i<A.
N(); i++)
185 typename M::size_type j0=J;
186 for (
typename M::size_type j=0; j<A.
M(); j++)
189 typename M::ConstColIterator it = A[i].find(j);
193 print_row(s,*it,i0,j0,therow,width,precision);
216 std::string rowtext,
int width=10,
int precision=2)
220 std::ios_base::fmtflags oldflags = s.flags();
223 s.setf(std::ios_base::scientific, std::ios_base::floatfield);
224 int oldprec = s.precision();
225 s.precision(precision);
236 for (
typename M::size_type i=0; i<MatrixDimension<M>::rowdim(A); i++)
248 s.precision(oldprec);
272 template<
class B,
int n,
int m,
class A>
275 std::string title, std::string rowtext,
276 int width=3,
int precision=2)
280 std::ios_base::fmtflags oldflags = s.flags();
282 s.setf(std::ios_base::scientific, std::ios_base::floatfield);
283 int oldprec = s.precision();
284 s.precision(precision);
297 bool reachedEnd=
false;
300 for(
int innerrow=0; innerrow<n; ++innerrow) {
303 Col
col=row->begin();
304 for(;
col != row->end(); ++
col,++count) {
307 if(count>=skipcols+width)
310 if(count==skipcols) {
314 s << row.index()<<
": ";
317 s<<
col.index()<<
": |";
319 if(count==skipcols) {
320 for(
typename std::string::size_type i=0; i < rowtext.length(); i++)
326 for(
int innercol=0; innercol < m; ++innercol) {
328 s<<(*col)[innerrow][innercol]<<
" ";
333 if(innerrow==n-1 &&
col==row->end())
346 s.precision(oldprec);
352 struct MatlabPODWriter
354 static std::ostream& write(
const T& t, std::ostream& s)
361 struct MatlabPODWriter<std::complex<T> >
363 static std::ostream& write(
const std::complex<T>& t, std::ostream& s)
365 s << t.real() <<
" " << t.imag();
380 template <
class FieldType>
382 int rowOffset,
int colOffset,
384 typename std::enable_if_t<Dune::IsNumber<FieldType>::value>* sfinae =
nullptr)
387 s << rowOffset + 1 <<
" " << colOffset + 1 <<
" ";
388 MatlabPODWriter<FieldType>::write(value, s)<< std::endl;
398 template <
class MatrixType>
400 int externalRowOffset,
int externalColOffset,
402 typename std::enable_if_t<!Dune::IsNumber<MatrixType>::value>* sfinae =
nullptr)
405 std::vector<typename MatrixType::size_type> colOffset(matrix.M());
406 if (colOffset.size() > 0)
409 for (
typename MatrixType::size_type i=0; i<matrix.M()-1; i++)
410 colOffset[i+1] = colOffset[i] +
413 typename MatrixType::size_type rowOffset = 0;
416 for (
typename MatrixType::size_type rowIdx=0; rowIdx<matrix.N(); rowIdx++)
418 auto cIt = matrix[rowIdx].begin();
419 auto cEndIt = matrix[rowIdx].end();
422 for (; cIt!=cEndIt; ++cIt)
424 externalRowOffset+rowOffset,
425 externalColOffset + colOffset[cIt.index()],
452 template <
class MatrixType>
454 const std::string& filename,
int outputPrecision = 18)
456 std::ofstream outStream(filename.c_str());
457 int oldPrecision = outStream.precision();
458 outStream.precision(outputPrecision);
461 outStream.precision(oldPrecision);
468 Hybrid::ifElse(IsNumber<V>(),
470 stream << id(v) << std::endl;
473 for (
const auto& entry :
id(v))
495 template <
class VectorType>
497 const std::string& filename,
int outputPrecision = 18)
499 std::ofstream outStream(filename.c_str());
500 int oldPrecision = outStream.precision();
501 outStream.precision(outputPrecision);
504 outStream.precision(oldPrecision);