Dune Core Modules (2.6.0)

io.hh
Go to the documentation of this file.
1// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2// vi: set et ts=4 sw=2 sts=2:
3#ifndef DUNE_ISTL_IO_HH
4#define DUNE_ISTL_IO_HH
5
6#include <cmath>
7#include <complex>
8#include <limits>
9#include <ios>
10#include <iomanip>
11#include <fstream>
12#include <string>
13
14#include "matrixutils.hh"
15#include "istlexception.hh"
20#include <dune/common/unused.hh>
21
22#include <dune/istl/matrix.hh>
24#include "bcrsmatrix.hh"
25
26
27namespace Dune {
28
41 //
42 // pretty printing of vectors
43 //
44
52 template<class V>
53 void recursive_printvector (std::ostream& s, const V& v, std::string rowtext,
54 int& counter, int columns, int width,
55 int precision)
56 {
57 for (typename V::ConstIterator i=v.begin(); i!=v.end(); ++i)
58 recursive_printvector(s,*i,rowtext,counter,columns,width,precision);
59 }
60
68 template<class K, int n>
69 void recursive_printvector (std::ostream& s, const FieldVector<K,n>& v,
70 std::string rowtext, int& counter, int columns,
71 int width, int precision)
72 {
73 DUNE_UNUSED_PARAMETER(precision);
74 // we now can print n numbers
75 for (int i=0; i<n; i++)
76 {
77 if (counter%columns==0)
78 {
79 s << rowtext; // start a new row
80 s << " "; // space in front of each entry
81 s.width(4); // set width for counter
82 s << counter; // number of first entry in a line
83 }
84 s << " "; // space in front of each entry
85 s.width(width); // set width for each entry anew
86 s << v[i]; // yeah, the number !
87 counter++; // increment the counter
88 if (counter%columns==0)
89 s << std::endl; // start a new line
90 }
91 }
92
93
101 template<class V>
102 void printvector (std::ostream& s, const V& v, std::string title,
103 std::string rowtext, int columns=1, int width=10,
104 int precision=2)
105 {
106 // count the numbers printed to make columns
107 int counter=0;
108
109 // remember old flags
110 std::ios_base::fmtflags oldflags = s.flags();
111
112 // set the output format
113 s.setf(std::ios_base::scientific, std::ios_base::floatfield);
114 int oldprec = s.precision();
115 s.precision(precision);
116
117 // print title
118 s << title << " [blocks=" << v.N() << ",dimension=" << v.dim() << "]"
119 << std::endl;
120
121 // print data from all blocks
122 recursive_printvector(s,v,rowtext,counter,columns,width,precision);
123
124 // check if new line is required
125 if (counter%columns!=0)
126 s << std::endl;
127
128 // reset the output format
129 s.flags(oldflags);
130 s.precision(oldprec);
131 }
132
133
135 //
136 // pretty printing of matrices
137 //
138
146 inline void fill_row (std::ostream& s, int m, int width, int precision)
147 {
148 DUNE_UNUSED_PARAMETER(precision);
149 for (int j=0; j<m; j++)
150 {
151 s << " "; // space in front of each entry
152 s.width(width); // set width for each entry anew
153 s << "."; // yeah, the number !
154 }
155 }
156
164 template<class M>
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)
168 {
169 typename M::size_type i0=I;
170 for (typename M::size_type i=0; i<A.N(); i++)
171 {
172 if (therow>=i0 && therow<i0+MatrixDimension<M>::rowdim(A,i))
173 {
174 // the row is in this block row !
175 typename M::size_type j0=J;
176 for (typename M::size_type j=0; j<A.M(); j++)
177 {
178 // find this block
179 typename M::ConstColIterator it = A[i].find(j);
180
181 // print row or filler
182 if (it!=A[i].end())
183 print_row(s,*it,i0,j0,therow,width,precision);
184 else
185 fill_row(s,MatrixDimension<M>::coldim(A,j),width,precision);
186
187 // advance columns
188 j0 += MatrixDimension<M>::coldim(A,j);
189 }
190 }
191 // advance rows
192 i0 += MatrixDimension<M>::rowdim(A,i);
193 }
194 }
195
203 template<class K, int n, int m>
204 void print_row (std::ostream& s, const FieldMatrix<K,n,m>& A,
205 typename FieldMatrix<K,n,m>::size_type I,
206 typename FieldMatrix<K,n,m>::size_type J,
207 typename FieldMatrix<K,n,m>::size_type therow, int width,
208 int precision)
209 {
211 DUNE_UNUSED_PARAMETER(precision);
212
213 typedef typename FieldMatrix<K,n,m>::size_type size_type;
214
215 for (size_type i=0; i<n; i++)
216 if (I+i==therow)
217 for (int j=0; j<m; j++)
218 {
219 s << " "; // space in front of each entry
220 s.width(width); // set width for each entry anew
221 s << A[i][j]; // yeah, the number !
222 }
223 }
224
232 template<class K>
233 void print_row (std::ostream& s, const FieldMatrix<K,1,1>& A,
234 typename FieldMatrix<K,1,1>::size_type I,
235 typename FieldMatrix<K,1,1>::size_type J,
236 typename FieldMatrix<K,1,1>::size_type therow,
237 int width, int precision)
238 {
240 DUNE_UNUSED_PARAMETER(precision);
241
242 if (I==therow)
243 {
244 s << " "; // space in front of each entry
245 s.width(width); // set width for each entry anew
246 s << static_cast<K>(A); // yeah, the number !
247 }
248 }
249
258 template<class M>
259 void printmatrix (std::ostream& s, const M& A, std::string title,
260 std::string rowtext, int width=10, int precision=2)
261 {
262
263 // remember old flags
264 std::ios_base::fmtflags oldflags = s.flags();
265
266 // set the output format
267 s.setf(std::ios_base::scientific, std::ios_base::floatfield);
268 int oldprec = s.precision();
269 s.precision(precision);
270
271 // print title
272 s << title
273 << " [n=" << A.N()
274 << ",m=" << A.M()
275 << ",rowdim=" << MatrixDimension<M>::rowdim(A)
276 << ",coldim=" << MatrixDimension<M>::coldim(A)
277 << "]" << std::endl;
278
279 // print all rows
280 for (typename M::size_type i=0; i<MatrixDimension<M>::rowdim(A); i++)
281 {
282 s << rowtext; // start a new row
283 s << " "; // space in front of each entry
284 s.width(4); // set width for counter
285 s << i; // number of first entry in a line
286 print_row(s,A,0,0,i,width,precision); // generic print
287 s << std::endl; // start a new line
288 }
289
290 // reset the output format
291 s.flags(oldflags);
292 s.precision(oldprec);
293 }
294
316 template<class B, int n, int m, class A>
317 void printSparseMatrix(std::ostream& s,
318 const BCRSMatrix<FieldMatrix<B,n,m>,A>& mat,
319 std::string title, std::string rowtext,
320 int width=3, int precision=2)
321 {
323 // remember old flags
324 std::ios_base::fmtflags oldflags = s.flags();
325 // set the output format
326 s.setf(std::ios_base::scientific, std::ios_base::floatfield);
327 int oldprec = s.precision();
328 s.precision(precision);
329 // print title
330 s << title
331 << " [n=" << mat.N()
332 << ",m=" << mat.M()
333 << ",rowdim=" << MatrixDimension<Matrix>::rowdim(mat)
334 << ",coldim=" << MatrixDimension<Matrix>::coldim(mat)
335 << "]" << std::endl;
336
337 typedef typename Matrix::ConstRowIterator Row;
338
339 for(Row row=mat.begin(); row != mat.end(); ++row) {
340 int skipcols=0;
341 bool reachedEnd=false;
342
343 while(!reachedEnd) {
344 for(int innerrow=0; innerrow<n; ++innerrow) {
345 int count=0;
346 typedef typename Matrix::ConstColIterator Col;
347 Col col=row->begin();
348 for(; col != row->end(); ++col,++count) {
349 if(count<skipcols)
350 continue;
351 if(count>=skipcols+width)
352 break;
353 if(innerrow==0) {
354 if(count==skipcols) {
355 s << rowtext; // start a new row
356 s << " "; // space in front of each entry
357 s.width(4); // set width for counter
358 s << row.index()<<": "; // number of first entry in a line
359 }
360 s.width(4);
361 s<<col.index()<<": |";
362 } else {
363 if(count==skipcols) {
364 for(typename std::string::size_type i=0; i < rowtext.length(); i++)
365 s<<" ";
366 s<<" ";
367 }
368 s<<" |";
369 }
370 for(int innercol=0; innercol < m; ++innercol) {
371 s.width(9);
372 s<<(*col)[innerrow][innercol]<<" ";
373 }
374
375 s<<"|";
376 }
377 if(innerrow==n-1 && col==row->end())
378 reachedEnd = true;
379 else
380 s << std::endl;
381 }
382 skipcols += width;
383 s << std::endl;
384 }
385 s << std::endl;
386 }
387
388 // reset the output format
389 s.flags(oldflags);
390 s.precision(oldprec);
391 }
392
393 namespace
394 {
395 template<typename T>
396 struct MatlabPODWriter
397 {
398 static std::ostream& write(const T& t, std::ostream& s)
399 {
400 s << t;
401 return s;
402 }
403 };
404 template<typename T>
405 struct MatlabPODWriter<std::complex<T> >
406 {
407 static std::ostream& write(const std::complex<T>& t, std::ostream& s)
408 {
409 s << t.real() << " " << t.imag();
410 return s;
411 }
412 };
413 } // anonymous namespace
414
424 template <class FieldType, int dim>
425 void writeMatrixToMatlabHelper(const ScaledIdentityMatrix<FieldType,dim>& matrix, int rowOffset, int colOffset, std::ostream& s)
426 {
427 for (int i=0; i<dim; i++)
428 {
429 //+1 for Matlab numbering
430 s << rowOffset + i + 1 << " " << colOffset + i + 1 << " ";
431 MatlabPODWriter<FieldType>::write(matrix.scalar(), s)<< std::endl;
432 }
433 }
434
444 template <class FieldType, int dim>
445 void writeMatrixToMatlabHelper(const DiagonalMatrix<FieldType,dim>& matrix, int rowOffset, int colOffset, std::ostream& s)
446 {
447 for (int i=0; i<dim; i++)
448 {
449 //+1 for Matlab numbering
450 s << rowOffset + i + 1 << " " << colOffset + i + 1 << " ";
451 MatlabPODWriter<FieldType>::write(matrix.diagonal(i), s)<< std::endl;
452 }
453 }
454
464 template <class FieldType, int rows, int cols>
466 ( const FieldMatrix<FieldType,rows,cols>& matrix, int rowOffset,
467 int colOffset, std::ostream& s)
468 {
469 for (int i=0; i<rows; i++)
470 for (int j=0; j<cols; j++) {
471 //+1 for Matlab numbering
472 s << rowOffset + i + 1 << " " << colOffset + j + 1 << " ";
473 MatlabPODWriter<FieldType>::write(matrix[i][j], s)<< std::endl;
474 }
475 }
476
486 template <class FieldType>
487 void writeMatrixToMatlabHelper(const DynamicMatrix<FieldType>& matrix, int rowOffset,
488 int colOffset, std::ostream& s)
489 {
490 for (int i=0; i<matrix.N(); i++)
491 for (int j=0; j<matrix.M(); j++) {
492 //+1 for Matlab numbering
493 s << rowOffset + i + 1 << " " << colOffset + j + 1 << " ";
494 MatlabPODWriter<FieldType>::write(matrix[i][j], s)<< std::endl;
495 }
496 }
497
505 template <class MatrixType>
506 void writeMatrixToMatlabHelper(const MatrixType& matrix,
507 int externalRowOffset, int externalColOffset,
508 std::ostream& s)
509 {
510 // Precompute the accumulated sizes of the columns
511 std::vector<typename MatrixType::size_type> colOffset(matrix.M());
512 if (colOffset.size() > 0)
513 colOffset[0] = 0;
514
515 for (typename MatrixType::size_type i=0; i<matrix.M()-1; i++)
516 colOffset[i+1] = colOffset[i] +
517 MatrixDimension<MatrixType>::coldim(matrix,i);
518
519 typename MatrixType::size_type rowOffset = 0;
520
521 // Loop over all matrix rows
522 for (typename MatrixType::size_type rowIdx=0; rowIdx<matrix.N(); rowIdx++)
523 {
524
525 const typename MatrixType::row_type& row = matrix[rowIdx];
526
527 typename MatrixType::row_type::ConstIterator cIt = row.begin();
528 typename MatrixType::row_type::ConstIterator cEndIt = row.end();
529
530 // Loop over all columns in this row
531 for (; cIt!=cEndIt; ++cIt)
533 externalRowOffset+rowOffset,
534 externalColOffset + colOffset[cIt.index()],
535 s);
536
537 rowOffset += MatrixDimension<MatrixType>::rowdim(matrix, rowIdx);
538 }
539
540 }
541
561 template <class MatrixType>
562 void writeMatrixToMatlab(const MatrixType& matrix,
563 const std::string& filename, int outputPrecision = 18)
564 {
565 std::ofstream outStream(filename.c_str());
566 int oldPrecision = outStream.precision();
567 outStream.precision(outputPrecision);
568
569 writeMatrixToMatlabHelper(matrix, 0, 0, outStream);
570 outStream.precision(oldPrecision);
571 }
572
573 // Recursively print all the blocks
574 template<class V>
575 void writeVectorToMatlabHelper (const V& v, std::ostream& stream)
576 {
577 for (const auto& entry : v)
578 writeVectorToMatlabHelper(entry, stream);
579 }
580
581 // Recursively print all the blocks -- specialization for FieldVector
582 template<class K, int n>
583 void writeVectorToMatlabHelper (const FieldVector<K,n>& v, std::ostream& s)
584 {
585 for (const auto& entry : v)
586 {
587 s << entry << std::endl;
588 }
589 }
590
591 // Recursively print all the blocks -- specialization for std::vector
592 template<class K>
593 void writeVectorToMatlabHelper (const std::vector<K>& v, std::ostream& s)
594 {
595 for (const auto& entry : v)
596 {
597 s << entry << std::endl;
598 }
599 }
600
601 // Recursively print all the blocks -- specialization for std::array
602 template<class K, std::size_t n>
603 void writeVectorToMatlabHelper (const std::array<K,n>& v, std::ostream& s)
604 {
605 for (const auto& entry : v)
606 {
607 s << entry << std::endl;
608 }
609 }
610
628 template <class VectorType>
629 void writeVectorToMatlab(const VectorType& vector,
630 const std::string& filename, int outputPrecision = 18)
631 {
632 std::ofstream outStream(filename.c_str());
633 int oldPrecision = outStream.precision();
634 outStream.precision(outputPrecision);
635
636 writeVectorToMatlabHelper(vector, outStream);
637 outStream.precision(oldPrecision);
638 }
639
642} // namespace Dune
643
644#endif
Implementation of the BCRSMatrix class.
A sparse block matrix with compressed row storage.
Definition: bcrsmatrix.hh:423
size_type M() const
number of columns
Definition: densematrix.hh:692
size_type N() const
number of rows
Definition: densematrix.hh:686
A diagonal matrix of static size.
Definition: diagonalmatrix.hh:52
Construct a matrix with a dynamic size.
Definition: dynmatrix.hh:59
A dense n x m matrix.
Definition: fmatrix.hh:68
ConstIterator class for sequential access.
Definition: matrix.hh:398
A generic dynamic dense matrix.
Definition: matrix.hh:555
RowIterator end()
Get iterator to one beyond last row.
Definition: matrix.hh:615
RowIterator begin()
Get iterator to first row.
Definition: matrix.hh:609
row_type::const_iterator ConstColIterator
Const iterator for the entries of each row.
Definition: matrix.hh:583
size_type M() const
Return the number of columns.
Definition: matrix.hh:695
size_type N() const
Return the number of rows.
Definition: matrix.hh:690
A multiple of the identity matrix of static size.
Definition: scaledidmatrix.hh:28
const K & scalar() const
Get const reference to the scalar diagonal value.
Definition: scaledidmatrix.hh:459
This file implements a quadratic diagonal matrix of fixed size.
This file implements a dense matrix with dynamic numbers of rows and columns.
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.
#define DUNE_UNUSED_PARAMETER(parm)
A macro to mark intentionally unused function parameters with.
Definition: unused.hh:25
const K & diagonal(size_type i) const
Get const reference to diagonal entry.
Definition: diagonalmatrix.hh:513
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
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
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
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
void writeVectorToMatlab(const VectorType &vector, const std::string &filename, int outputPrecision=18)
Writes vectors in a Matlab-readable format.
Definition: io.hh:629
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
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 writeMatrixToMatlabHelper(const ScaledIdentityMatrix< FieldType, dim > &matrix, int rowOffset, int colOffset, std::ostream &s)
Helper method for the writeMatrixToMatlab routine.
Definition: io.hh:425
A dynamic dense block matrix class.
Some handy generic functions for ISTL matrices.
Dune namespace.
Definition: alignedallocator.hh:10
STL namespace.
This file implements a quadratic matrix of fixed size which is a multiple of the identity.
Definition of the DUNE_UNUSED macro for the case that config.h is not available.
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Jul 15, 22:36, 2024)