From: Wolfgang Bangerth Date: Tue, 21 Aug 2001 16:05:35 +0000 (+0000) Subject: Add new class MatrixOut and corresponding testcase. X-Git-Tag: v8.0.0~18899 X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=f545f2e5deedadc2c5779bee57fe0b0fb3efb3f0;p=dealii.git Add new class MatrixOut and corresponding testcase. git-svn-id: https://svn.dealii.org/trunk@4899 0785d39b-7218-0410-832d-ea1e28bc413d --- diff --git a/deal.II/doc/news/2001/c-3-1.html b/deal.II/doc/news/2001/c-3-1.html index eee8321b95..99ee2b3e84 100644 --- a/deal.II/doc/news/2001/c-3-1.html +++ b/deal.II/doc/news/2001/c-3-1.html @@ -331,6 +331,15 @@ documentation, etc.

lac

    +
  1. + New: There is now a class MatrixOut + which can be used to convert a matrix into a graphical output, + for example in order to see visually that a matrix is + diagonally dominant or not. +
    + (WB 2001/08/21) +

    +
  2. Changed: Base class Solver and all Preconditioner classes are now diff --git a/deal.II/lac/include/lac/matrix_out.h b/deal.II/lac/include/lac/matrix_out.h new file mode 100644 index 0000000000..9b514fd8e6 --- /dev/null +++ b/deal.II/lac/include/lac/matrix_out.h @@ -0,0 +1,281 @@ +//---------------------------- matrix_out.h --------------------------- +// $Id$ +// Version: $Name$ +// +// Copyright (C) 2001 by the deal.II authors +// +// This file is subject to QPL and may not be distributed +// without copyright and license information. Please refer +// to the file deal.II/doc/license.html for the text and +// further information on this license. +// +//---------------------------- matrix_out.h --------------------------- +#ifndef __deal2__matrix_out_h +#define __deal2__matrix_out_h + +#include +#include +#include + + +/** + * Output a matrix in graphical form using the generic format + * independent output routines of the base class. The matrix is + * converted into a list of patches on a 2d domain where the height is + * given by the elements of the matrix. The functions of the base + * class can then write this "mountain representation" of the matrix + * in a variety of graphical output formats. The coordinates of the + * matrix output are that the columns run with increasing x-axis, as + * usual, starting from zero, while the rows run into the negative + * y-axis, also starting from zero. Note that due to some internal + * restrictions, this class can only output one matrix at a time, + * i.e. it can not take advantage of the multiple dataset capabilities + * of the base class. + * + * A typical usage of this class would be as follows: + * @begin{verbatim} + * FullMatrix M; + * ... // fill matrix M with some values + * + * // now write out M: + * MatrixOut matrix_out; + * std::ofstream out ("M.gnuplot"); + * matrix_out.build_patches (M, "M"); + * matrix_out.write_gnuplot (out); + * @end{verbatim} + * Of course, you can as well choose a different graphical output + * format. Also, this class supports any matrix, not only of type + * @ref{FullMatrix}, as long as it satisfies a number of requirements, + * stated with the member functions of this class. + * + * @author Wolfgang Bangerth, 2001 + */ +class MatrixOut : public DataOutInterface<2,2> +{ + public: + /** + * Destructor. Declared in order + * to make it virtual. + */ + virtual ~MatrixOut (); + + /** + * Generate a list of patches + * from the given matrix and use + * the given string as the name + * of the data set upon writing + * to a file. Once patches have + * been built, you can use the + * functions of the base class to + * write the data into a files, + * using one of the supported + * output formats. + * + * Note that this function + * requires that we can extract + * elements of the matrix, which + * is done using the + * @p{get_element} function + * declared below. By adding + * specializations, you can + * extend this class to other + * matrix classes which are not + * presently + * supported. Furthermore, we + * need to be able to extract the + * size of the matrix, for which + * we assume that the matrix type + * offers member functions + * @p{m()} and @p{n()}, which + * return the number of rows and + * columns, respectively. + */ + template + void build_patches (const Matrix &matrix, + const std::string &name); + + private: + + /** + * Abbreviate the somewhat + * lengthy name for the @p{Patch} + * class. + */ + typedef DataOutBase::Patch<2,2> Patch; + + /** + * This is a list of patches that + * is created each time + * @p{build_patches} is + * called. These patches are used + * in the output routines of the + * base classes. + */ + std::vector patches; + + /** + * Name of the matrix to be + * written. + */ + std::string name; + + /** + * Function by which the base + * class's functions get to know + * what patches they shall write + * to a file. + */ + virtual const std::vector & + get_patches () const; + + /** + * Virtual function through which + * the names of data sets are + * obtained by the output + * functions of the base class. + */ + virtual std::vector get_dataset_names () const; + + /** + * Return the element with given + * indices of a sparse matrix. + */ + template + static double get_element (const SparseMatrix &matrix, + const unsigned int i, + const unsigned int j); + + /** + * Return the element with given + * indices of a block sparse + * matrix. + */ + template + static double get_element (const BlockSparseMatrix &matrix, + const unsigned int i, + const unsigned int j); + + /** + * Return the element with given + * indices from any matrix type + * for which no specialization of + * this function was declared + * above. This will call + * @p{operator()} on the matrix. + */ + template + static double get_element (const Matrix &matrix, + const unsigned int i, + const unsigned int j); + +}; + + + + +/* ---------------------- Template and inline functions ------------- */ + + +template +inline +double +MatrixOut::get_element (const SparseMatrix &matrix, + const unsigned int i, + const unsigned int j) +{ + return matrix.el(i,j); +}; + + + + +template +inline +double +MatrixOut::get_element (const BlockSparseMatrix &matrix, + const unsigned int i, + const unsigned int j) +{ + return matrix.el(i,j); +}; + + + + +template +inline +double +MatrixOut::get_element (const Matrix &matrix, + const unsigned int i, + const unsigned int j) +{ + return matrix(i,j); +}; + + + +template +void +MatrixOut::build_patches (const Matrix &matrix, + const std::string &name) +{ + // first clear old data and set it + // to virgin state + patches.clear (); + patches.resize ((matrix.n()-1) * (matrix.m()-1)); + + // now build the patches + unsigned int index=0; + for (unsigned int i=0; i(-i); + patches[index].vertices[1](0) = j; + patches[index].vertices[1](1) = static_cast(-i-1); + patches[index].vertices[2](0) = j+1; + patches[index].vertices[2](1) = static_cast(-i-1); + patches[index].vertices[3](0) = j+1; + patches[index].vertices[3](1) = static_cast(-i); + + patches[index].n_subdivisions = 1; + + patches[index].data.reinit (1,4); + patches[index].data(0,0) = get_element(matrix, i, j); + patches[index].data(0,1) = get_element(matrix, i, j+1); + patches[index].data(0,2) = get_element(matrix, i+1, j); + patches[index].data(0,3) = get_element(matrix, i+1, j+1); + }; + + // finally set the name + this->name = name; +}; + + + +/*---------------------------- matrix_out.h ---------------------------*/ + +#endif +/*---------------------------- matrix_out.h ---------------------------*/ + + diff --git a/deal.II/lac/source/matrix_out.cc b/deal.II/lac/source/matrix_out.cc new file mode 100644 index 0000000000..5e316c0be4 --- /dev/null +++ b/deal.II/lac/source/matrix_out.cc @@ -0,0 +1,23 @@ +/* $Id$ */ + +#include + + +MatrixOut::~MatrixOut () +{}; + + + +const std::vector & +MatrixOut::get_patches () const +{ + return patches; +}; + + + +std::vector +MatrixOut::get_dataset_names () const +{ + return std::vector(1,name); +}; diff --git a/tests/lac/Makefile.in b/tests/lac/Makefile.in index 0b24e9655b..fb34f7c203 100644 --- a/tests/lac/Makefile.in +++ b/tests/lac/Makefile.in @@ -22,6 +22,7 @@ default: run-tests block_matrices.exe : block_matrices.go $(libraries) block_vector.exe : block_vector.go abort.go $(libraries) full_matrix.exe : full_matrix.go $(libraries) +matrix_out.exe : matrix_out.go $(libraries) solver.exe : solver.go testmatrix.go $(libraries) sparsity_pattern.exe: sparsity_pattern.go testmatrix.go $(libraries) sparse_ilu.exe : sparse_ilu.go testmatrix.go $(libraries) @@ -30,7 +31,7 @@ vector-vector.exe : vector-vector.go $(libraries) #mg.exe : mg.go testmatrix.go $(libraries) -tests = block_matrices block_vector full_matrix solver \ +tests = block_matrices block_vector full_matrix matrix_out solver \ sparsity_pattern sparse_ilu vector-vector ############################################################ diff --git a/tests/lac/matrix_out.cc b/tests/lac/matrix_out.cc new file mode 100644 index 0000000000..8b0e88f26c --- /dev/null +++ b/tests/lac/matrix_out.cc @@ -0,0 +1,60 @@ +//---------------------------- matrix_out.cc --------------------------- +// $Id$ +// Version: $Name$ +// +// Copyright (C) 2001 by the deal.II authors +// +// This file is subject to QPL and may not be distributed +// without copyright and license information. Please refer +// to the file deal.II/doc/license.html for the text and +// further information on this license. +// +//---------------------------- matrix_out.cc --------------------------- + + +#include +#include +#include +#include + +int main () +{ + std::ofstream logfile("matrix_out.output"); + logfile.setf(std::ios::fixed); + logfile.precision(2); + deallog.attach(logfile); + deallog.depth_console(0); + + // test for a square full matrix + if (true) + { + FullMatrix full_matrix(4,4); + for (unsigned int i=0; i<4; ++i) + full_matrix(i,i) = 1; + + MatrixOut matrix_out; + matrix_out.build_patches (full_matrix, "full_matrix"); + matrix_out.write_gnuplot (logfile); + }; + + // test for a rectangular sparse + // matrix + if (true) + { + SparsityPattern sparsity (4,8,7); + for (unsigned int i=0; i<4; ++i) + for (unsigned int j=0; j<8; ++j) + if (i!=j) + sparsity.add (i,j); + sparsity.compress (); + + SparseMatrix sparse_matrix(sparsity); + for (unsigned int i=0; i<4; ++i) + for (unsigned int j=0; j<8; ++j) + sparse_matrix.set(i,j, fabs(static_cast(i-j))); + + MatrixOut matrix_out; + matrix_out.build_patches (sparse_matrix, "sparse_matrix"); + matrix_out.write_eps (logfile); + }; +}; diff --git a/tests/lac/matrix_out.checked b/tests/lac/matrix_out.checked new file mode 100644 index 0000000000..acc14936f0 --- /dev/null +++ b/tests/lac/matrix_out.checked @@ -0,0 +1,128 @@ + +# This file was generated by the deal.II library. + + +# +# For a description of the GNUPLOT format see the GNUPLOT manual. +# +# +0.00 0.00 1.00 +1.00 0.00 0.00 + +0.00 -1.00 0.00 +1.00 -1.00 1.00 + + +1.00 0.00 0.00 +2.00 0.00 0.00 + +1.00 -1.00 1.00 +2.00 -1.00 0.00 + + +2.00 0.00 0.00 +3.00 0.00 0.00 + +2.00 -1.00 0.00 +3.00 -1.00 0.00 + + +0.00 -1.00 0.00 +1.00 -1.00 1.00 + +0.00 -2.00 0.00 +1.00 -2.00 0.00 + + +1.00 -1.00 1.00 +2.00 -1.00 0.00 + +1.00 -2.00 0.00 +2.00 -2.00 1.00 + + +2.00 -1.00 0.00 +3.00 -1.00 0.00 + +2.00 -2.00 1.00 +3.00 -2.00 0.00 + + +0.00 -2.00 0.00 +1.00 -2.00 0.00 + +0.00 -3.00 0.00 +1.00 -3.00 0.00 + + +1.00 -2.00 0.00 +2.00 -2.00 1.00 + +1.00 -3.00 0.00 +2.00 -3.00 0.00 + + +2.00 -2.00 1.00 +3.00 -2.00 0.00 + +2.00 -3.00 0.00 +3.00 -3.00 1.00 + + +%!PS-Adobe-2.0 EPSF-1.2 +%%Title: deal.II Output +%%Creator: the deal.II library + +%%BoundingBox: 0 0 299 252 +/m {moveto} bind def +/l {lineto} bind def +/s {setrgbcolor} bind def +/sg {setgray} bind def +/lx {lineto closepath stroke} bind def +/lf {lineto closepath fill} bind def +%%EndProlog + +0.50 setlinewidth +0.00000 0.00000 0.00000 s 59.50667 81.28762 m 39.67111 98.46571 l 74.02730 54.19175 l 93.86286 105.72603 lf +0 sg 59.50667 81.28762 m 39.67111 98.46571 l 74.02730 54.19175 l 93.86286 105.72603 lx +0.00000 0.00000 0.36364 s 93.86286 105.72603 m 74.02730 54.19175 l 108.38349 78.63016 l 128.21905 130.16444 lf +0 sg 93.86286 105.72603 m 74.02730 54.19175 l 108.38349 78.63016 l 128.21905 130.16444 lx +0.00000 0.00000 0.36364 s 39.67111 98.46571 m 19.83556 115.64381 l 54.19175 71.36984 l 74.02730 54.19175 lf +0 sg 39.67111 98.46571 m 19.83556 115.64381 l 54.19175 71.36984 l 74.02730 54.19175 lx +0.00000 0.00000 0.00000 s 74.02730 54.19175 m 54.19175 71.36984 l 88.54794 27.09587 l 108.38349 78.63016 lf +0 sg 74.02730 54.19175 m 54.19175 71.36984 l 88.54794 27.09587 l 108.38349 78.63016 lx +0.00000 0.09091 0.90909 s 128.21905 130.16444 m 108.38349 78.63016 l 142.73968 103.06857 l 162.57524 154.60286 lf +0 sg 128.21905 130.16444 m 108.38349 78.63016 l 142.73968 103.06857 l 162.57524 154.60286 lx +0.00000 0.00000 0.36364 s 108.38349 78.63016 m 88.54794 27.09587 l 122.90413 51.53429 l 142.73968 103.06857 lf +0 sg 108.38349 78.63016 m 88.54794 27.09587 l 122.90413 51.53429 l 142.73968 103.06857 lx +0.00000 0.00000 0.36364 s 54.19175 71.36984 m 34.35619 88.54794 l 68.71238 44.27397 l 88.54794 27.09587 lf +0 sg 54.19175 71.36984 m 34.35619 88.54794 l 68.71238 44.27397 l 88.54794 27.09587 lx +0.00000 0.09091 0.90909 s 19.83556 115.64381 m 0.00000 132.82190 l 34.35619 88.54794 l 54.19175 71.36984 lf +0 sg 19.83556 115.64381 m 0.00000 132.82190 l 34.35619 88.54794 l 54.19175 71.36984 lx +0.00000 0.00000 0.00000 s 88.54794 27.09587 m 68.71238 44.27397 l 103.06857 0.00000 l 122.90413 51.53429 lf +0 sg 88.54794 27.09587 m 68.71238 44.27397 l 103.06857 0.00000 l 122.90413 51.53429 lx +0.00000 0.81818 0.18182 s 162.57524 154.60286 m 142.73968 103.06857 l 177.09587 127.50698 l 196.93143 179.04127 lf +0 sg 162.57524 154.60286 m 142.73968 103.06857 l 177.09587 127.50698 l 196.93143 179.04127 lx +0.00000 0.09091 0.90909 s 142.73968 103.06857 m 122.90413 51.53429 l 157.26032 75.97270 l 177.09587 127.50698 lf +0 sg 142.73968 103.06857 m 122.90413 51.53429 l 157.26032 75.97270 l 177.09587 127.50698 lx +0.00000 0.00000 0.36364 s 122.90413 51.53429 m 103.06857 0.00000 l 137.42476 24.43841 l 157.26032 75.97270 lf +0 sg 122.90413 51.53429 m 103.06857 0.00000 l 137.42476 24.43841 l 157.26032 75.97270 lx +0.54545 0.45455 0.00000 s 196.93143 179.04127 m 177.09587 127.50698 l 211.45206 151.94540 l 231.28762 203.47968 lf +0 sg 196.93143 179.04127 m 177.09587 127.50698 l 211.45206 151.94540 l 231.28762 203.47968 lx +0.00000 0.81818 0.18182 s 177.09587 127.50698 m 157.26032 75.97270 l 191.61651 100.41111 l 211.45206 151.94540 lf +0 sg 177.09587 127.50698 m 157.26032 75.97270 l 191.61651 100.41111 l 211.45206 151.94540 lx +0.00000 0.09091 0.90909 s 157.26032 75.97270 m 137.42476 24.43841 l 171.78095 48.87683 l 191.61651 100.41111 lf +0 sg 157.26032 75.97270 m 137.42476 24.43841 l 171.78095 48.87683 l 191.61651 100.41111 lx +1.00000 0.27273 0.27273 s 231.28762 203.47968 m 211.45206 151.94540 l 245.80825 176.38381 l 265.64381 227.91809 lf +0 sg 231.28762 203.47968 m 211.45206 151.94540 l 245.80825 176.38381 l 265.64381 227.91809 lx +0.54545 0.45455 0.00000 s 211.45206 151.94540 m 191.61651 100.41111 l 225.97270 124.84952 l 245.80825 176.38381 lf +0 sg 211.45206 151.94540 m 191.61651 100.41111 l 225.97270 124.84952 l 245.80825 176.38381 lx +0.00000 0.81818 0.18182 s 191.61651 100.41111 m 171.78095 48.87683 l 206.13714 73.31524 l 225.97270 124.84952 lf +0 sg 191.61651 100.41111 m 171.78095 48.87683 l 206.13714 73.31524 l 225.97270 124.84952 lx +1.00000 1.00000 1.00000 s 265.64381 227.91809 m 245.80825 176.38381 l 280.16444 200.82222 l 300.00000 252.35651 lf +0 sg 265.64381 227.91809 m 245.80825 176.38381 l 280.16444 200.82222 l 300.00000 252.35651 lx +1.00000 0.27273 0.27273 s 245.80825 176.38381 m 225.97270 124.84952 l 260.32889 149.28794 l 280.16444 200.82222 lf +0 sg 245.80825 176.38381 m 225.97270 124.84952 l 260.32889 149.28794 l 280.16444 200.82222 lx +0.54545 0.45455 0.00000 s 225.97270 124.84952 m 206.13714 73.31524 l 240.49333 97.75365 l 260.32889 149.28794 lf +0 sg 225.97270 124.84952 m 206.13714 73.31524 l 240.49333 97.75365 l 260.32889 149.28794 lx +showpage