]> https://gitweb.dealii.org/ - dealii-svn.git/commitdiff
Add new class MatrixOut and corresponding testcase.
authorwolf <wolf@0785d39b-7218-0410-832d-ea1e28bc413d>
Tue, 21 Aug 2001 16:05:35 +0000 (16:05 +0000)
committerwolf <wolf@0785d39b-7218-0410-832d-ea1e28bc413d>
Tue, 21 Aug 2001 16:05:35 +0000 (16:05 +0000)
git-svn-id: https://svn.dealii.org/trunk@4899 0785d39b-7218-0410-832d-ea1e28bc413d

deal.II/doc/news/2001/c-3-1.html
deal.II/lac/include/lac/matrix_out.h [new file with mode: 0644]
deal.II/lac/source/matrix_out.cc [new file with mode: 0644]
tests/lac/Makefile.in
tests/lac/matrix_out.cc [new file with mode: 0644]
tests/lac/matrix_out.checked [new file with mode: 0644]

index eee8321b95547fde4809082c056467519980b9de..99ee2b3e84f7be9527d03ddb50f3aa06ebd46c03 100644 (file)
@@ -331,6 +331,15 @@ documentation, etc</a>.
 <h3>lac</h3>
 
 <ol>
+  <li> <p>
+       New: There is now a class <code class="class">MatrixOut</code>
+       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.
+       <br>
+       (WB 2001/08/21)
+       </p>
+
   <li> <p>
        Changed: Base class <code class="class">Solver</code> and all
        <code class="class">Preconditioner</code> 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 (file)
index 0000000..9b514fd
--- /dev/null
@@ -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 <base/data_out_base.h>
+#include <lac/sparse_matrix.h>
+#include <lac/block_sparse_matrix.h>
+
+
+/**
+ * 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<double> 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 <class Matrix>
+    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<Patch> 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<Patch> &
+    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<std::string> get_dataset_names () const;
+
+                                    /**
+                                     * Return the element with given
+                                     * indices of a sparse matrix.
+                                     */
+    template <typename number>
+    static double get_element (const SparseMatrix<number> &matrix,
+                              const unsigned int          i,
+                              const unsigned int          j);
+    
+                                    /**
+                                     * Return the element with given
+                                     * indices of a block sparse
+                                     * matrix.
+                                     */
+    template <typename number>
+    static double get_element (const BlockSparseMatrix<number> &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 <class Matrix>
+    static double get_element (const Matrix       &matrix,
+                              const unsigned int  i,
+                              const unsigned int  j);
+    
+};
+
+
+
+
+/* ---------------------- Template and inline functions ------------- */
+
+
+template <typename number>
+inline
+double
+MatrixOut::get_element (const SparseMatrix<number> &matrix,
+                       const unsigned int          i,
+                       const unsigned int          j)
+{
+  return matrix.el(i,j);
+};
+
+
+
+
+template <typename number>
+inline
+double
+MatrixOut::get_element (const BlockSparseMatrix<number> &matrix,
+                       const unsigned int               i,
+                       const unsigned int               j)
+{
+  return matrix.el(i,j);
+};
+
+
+
+
+template <class Matrix>
+inline
+double
+MatrixOut::get_element (const Matrix       &matrix,
+                       const unsigned int  i,
+                       const unsigned int  j)
+{
+  return matrix(i,j);
+};
+
+
+
+template <class Matrix>
+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<matrix.m()-1; ++i)
+    for (unsigned int j=0; j<matrix.n()-1; ++j, ++index)
+      {
+                                        // within each patch, order
+                                        // the points in such a way
+                                        // that if some graphical
+                                        // output program (such as
+                                        // gnuplot) plots the
+                                        // quadrilaterals as two
+                                        // triangles, then the
+                                        // diagonal of the
+                                        // quadrilateral which cuts
+                                        // it into the two printed
+                                        // triangles is parallel to
+                                        // the diagonal of the
+                                        // matrix, rather than
+                                        // perpendicular to it. this
+                                        // has the advantage that,
+                                        // for example, the unit
+                                        // matrix is plotted as a
+                                        // straight rim, rather than
+                                        // as a series of bumps and
+                                        // valleys along the diagonal
+       patches[index].vertices[0](0) = j;
+       patches[index].vertices[0](1) = static_cast<signed int>(-i);
+       patches[index].vertices[1](0) = j;
+       patches[index].vertices[1](1) = static_cast<signed int>(-i-1);
+       patches[index].vertices[2](0) = j+1;
+       patches[index].vertices[2](1) = static_cast<signed int>(-i-1);
+       patches[index].vertices[3](0) = j+1;
+       patches[index].vertices[3](1) = static_cast<signed int>(-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 (file)
index 0000000..5e316c0
--- /dev/null
@@ -0,0 +1,23 @@
+/* $Id$ */
+
+#include <lac/matrix_out.h>
+
+
+MatrixOut::~MatrixOut () 
+{};
+
+
+
+const std::vector<MatrixOut::Patch> &
+MatrixOut::get_patches () const
+{
+  return patches;
+};
+
+
+
+std::vector<std::string> 
+MatrixOut::get_dataset_names () const
+{
+  return std::vector<std::string>(1,name);
+};
index 0b24e9655ba446159f74c378e19622a1ebe8f703..fb34f7c203fb4514105c458a442c3f2b37cb1b92 100644 (file)
@@ -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 (file)
index 0000000..8b0e88f
--- /dev/null
@@ -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 <base/logstream.h>
+#include <lac/matrix_out.h>
+#include <lac/full_matrix.h>
+#include <fstream>
+
+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<double> 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<double> 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<signed int>(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 (file)
index 0000000..acc1493
--- /dev/null
@@ -0,0 +1,128 @@
+
+# This file was generated by the deal.II library.
+
+
+#
+# For a description of the GNUPLOT format see the GNUPLOT manual.
+#
+# <x> <y> <full_matrix> 
+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

In the beginning the Universe was created. This has made a lot of people very angry and has been widely regarded as a bad move.

Douglas Adams


Typeset in Trocchi and Trocchi Bold Sans Serif.