]> https://gitweb.dealii.org/ - dealii-svn.git/commitdiff
Add optional options to matrix output class.
authorwolf <wolf@0785d39b-7218-0410-832d-ea1e28bc413d>
Wed, 22 Aug 2001 11:52:26 +0000 (11:52 +0000)
committerwolf <wolf@0785d39b-7218-0410-832d-ea1e28bc413d>
Wed, 22 Aug 2001 11:52:26 +0000 (11:52 +0000)
git-svn-id: https://svn.dealii.org/trunk@4903 0785d39b-7218-0410-832d-ea1e28bc413d

deal.II/lac/include/lac/matrix_out.h
deal.II/lac/source/matrix_out.cc
tests/lac/matrix_out.cc
tests/lac/matrix_out.checked

index 9b514fd8e645b287e217b1c886c073d15086edae..910e12289d4685abb3375027c29fd688947fdb27 100644 (file)
  * @ref{FullMatrix}, as long as it satisfies a number of requirements,
  * stated with the member functions of this class.
  *
+ * The generation of patches through the @p{build_patches} function
+ * can be modified by giving it an object holding certain flags. See
+ * the documentation of the members of the @ref{Options} class for a
+ * description of these flags.
+ *
  * @author Wolfgang Bangerth, 2001
  */
 class MatrixOut : public DataOutInterface<2,2>
 {
   public:
+                                    /**
+                                     * Class holding various
+                                     * variables which are used to
+                                     * modify the output of the
+                                     * @ref{MatrixOut} class.
+                                     */
+    class Options 
+    {
+      public:
+                                        /**
+                                         * If @p{true}, only show the
+                                         * absolute values of the
+                                         * matrix entries, rather
+                                         * than their true values
+                                         * including the
+                                         * sign. Default value is
+                                         * @p{false}.
+                                         */
+       bool         show_absolute_values;
+
+                                        /**
+                                         * If larger than one, do not
+                                         * show each element of the
+                                         * matrix, but rather an
+                                         * average over a number of
+                                         * entries. The number of
+                                         * output patches is
+                                         * accordingly smaller. This
+                                         * flag determines how large
+                                         * each shown block shall be
+                                         * (in rows/columns). For
+                                         * example, if it is two,
+                                         * then always four entries
+                                         * are collated into one.
+                                         *
+                                         * Default value is one.
+                                         */
+       unsigned int block_size;
+
+                                        /**
+                                         * Default constructor. Set
+                                         * all elements of this
+                                         * structure to their default
+                                         * values.
+                                         */
+       Options (const bool         show_absolute_values = false,
+                const unsigned int block_size           = 1);
+    };
+    
                                     /**
                                      * Destructor. Declared in order
                                      * to make it virtual.
@@ -71,6 +125,12 @@ class MatrixOut : public DataOutInterface<2,2>
                                      * using one of the supported
                                      * output formats.
                                      *
+                                     * You may give a structure
+                                     * holding various options. See
+                                     * the description of the fields
+                                     * of this structure for more
+                                     * information.
+                                     *
                                      * Note that this function
                                      * requires that we can extract
                                      * elements of the matrix, which
@@ -92,7 +152,8 @@ class MatrixOut : public DataOutInterface<2,2>
                                      */
     template <class Matrix>
     void build_patches (const Matrix      &matrix,
-                       const std::string &name);
+                       const std::string &name,
+                       const Options     &options = Options());
     
   private:
     
@@ -167,7 +228,25 @@ class MatrixOut : public DataOutInterface<2,2>
     static double get_element (const Matrix       &matrix,
                               const unsigned int  i,
                               const unsigned int  j);
-    
+
+                                    /**
+                                     * Get the value of the matrix at
+                                     * gridpoint @p{(i,j)}. Depending
+                                     * on the given flags, this can
+                                     * mean different things, for
+                                     * example if only absolute
+                                     * values shall be shown then the
+                                     * absolute value of the matrix
+                                     * entry is taken. If the block
+                                     * size is larger than one, then
+                                     * an average of several matrix
+                                     * entries is taken.
+                                     */
+    template <class Matrix>
+    static double get_gridpoint_value (const Matrix       &matrix,
+                                      const unsigned int  i,
+                                      const unsigned int  j,
+                                      const Options      &options);    
 };
 
 
@@ -214,20 +293,66 @@ MatrixOut::get_element (const Matrix       &matrix,
 
 
 
+template <class Matrix>
+inline
+double
+MatrixOut::get_gridpoint_value (const Matrix       &matrix,
+                               const unsigned int  i,
+                               const unsigned int  j,
+                               const Options      &options)
+{
+                                  // special case if block size is
+                                  // one since we then don't need all
+                                  // that loop overhead
+  if (options.block_size == 1)
+    {
+      if (options.show_absolute_values == true)
+       return std::fabs(get_element (matrix, i, j));
+      else
+       return get_element (matrix, i, j);
+    };
+
+                                  // if blocksize greater than one,
+                                  // then compute average of elements
+  double average = 0;
+  unsigned int n_elements = 0;
+  for (unsigned int row=i*options.block_size;
+       row < std::min(matrix.m(), (i+1)*options.block_size); ++row)
+    for (unsigned int col=j*options.block_size;
+        col < std::min(matrix.m(), (j+1)*options.block_size); ++col, ++n_elements)
+      if (options.show_absolute_values == true)
+       average += std::fabs(get_element (matrix, row, col));
+      else
+       average += get_element (matrix, row, col);
+  average /= n_elements;
+  return average;
+};
+
+
+
 template <class Matrix>
 void
 MatrixOut::build_patches (const Matrix      &matrix,
-                         const std::string &name)
+                         const std::string &name,
+                         const Options     &options)
 {
+  const unsigned int
+    gridpoints_x = (matrix.n() / options.block_size
+                   +
+                   (matrix.n() % options.block_size != 0 ? 1 : 0)),
+    gridpoints_y = (matrix.m() / options.block_size
+                   +
+                   (matrix.m() % options.block_size != 0 ? 1 : 0));
+               
                                   // first clear old data and set it
                                   // to virgin state
   patches.clear ();
-  patches.resize ((matrix.n()-1) * (matrix.m()-1));
+  patches.resize ((gridpoints_x-1) * (gridpoints_y-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)
+  for (unsigned int i=0; i<gridpoints_y-1; ++i)
+    for (unsigned int j=0; j<gridpoints_x-1; ++j, ++index)
       {
                                         // within each patch, order
                                         // the points in such a way
@@ -257,14 +382,20 @@ MatrixOut::build_patches (const Matrix      &matrix,
        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);
-
+                                        // next scale all the patch
+                                        // coordinates by the block
+                                        // size, to get original
+                                        // coordinates
+       for (unsigned int v=0; v<4; ++v)
+         patches[index].vertices[v] *= options.block_size;
+       
        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);
+       patches[index].data(0,0) = get_gridpoint_value(matrix, i,   j,   options);
+       patches[index].data(0,1) = get_gridpoint_value(matrix, i,   j+1, options);
+       patches[index].data(0,2) = get_gridpoint_value(matrix, i+1, j,   options);
+       patches[index].data(0,3) = get_gridpoint_value(matrix, i+1, j+1, options);
       };
 
                                   // finally set the name
index 5e316c0be4d60e808e99a754df355ea686349061..d79840735927c4efc8070620b1107ba8de053924 100644 (file)
@@ -3,6 +3,15 @@
 #include <lac/matrix_out.h>
 
 
+MatrixOut::Options::Options (const bool         show_absolute_values,
+                            const unsigned int block_size)
+               :
+               show_absolute_values (show_absolute_values),
+               block_size (block_size)
+{};
+
+
+
 MatrixOut::~MatrixOut () 
 {};
 
index 8b0e88f26c57520d173e9bb5d0d198b2f22058f0..c3d556f7559d69905e4549c5e7a40460c08e60d1 100644 (file)
@@ -51,10 +51,25 @@ int main ()
       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)));
+         sparse_matrix.set(i,j, static_cast<signed int>(i-j));
   
       MatrixOut matrix_out;
-      matrix_out.build_patches (sparse_matrix, "sparse_matrix");
+      matrix_out.build_patches (sparse_matrix, "sparse_matrix",
+                               MatrixOut::Options (true));
       matrix_out.write_eps (logfile);
+    };
+
+                                  // test collation of elements
+  if (true)
+    {
+      FullMatrix<double> full_matrix(20,20);
+      for (unsigned int i=0; i<20; ++i)
+       for (unsigned int j=0; j<20; ++j)
+         full_matrix(i,j) = (1.*i*i/20/20-1.*j*j*j/20/20/20);
+  
+      MatrixOut matrix_out;
+      matrix_out.build_patches (full_matrix, "collated_matrix",
+                               MatrixOut::Options (false, 4));
+      matrix_out.write_gmv (logfile);
     };
 };
index acc14936f03c1e7be85de6d7686656e10040ec3a..5bdc3050174f553e1898810e5444c2d007366a00 100644 (file)
 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
+gmvinput ascii
+
+nodes 64
+0.00000 4.00000 0.00000 4.00000 4.00000 8.00000 4.00000 8.00000 8.00000 12.00000 8.00000 12.00000 12.00000 16.00000 12.00000 16.00000 0.00000 4.00000 0.00000 4.00000 4.00000 8.00000 4.00000 8.00000 8.00000 12.00000 8.00000 12.00000 12.00000 16.00000 12.00000 16.00000 0.00000 4.00000 0.00000 4.00000 4.00000 8.00000 4.00000 8.00000 8.00000 12.00000 8.00000 12.00000 12.00000 16.00000 12.00000 16.00000 0.00000 4.00000 0.00000 4.00000 4.00000 8.00000 4.00000 8.00000 8.00000 12.00000 8.00000 12.00000 12.00000 16.00000 12.00000 16.00000 
+0.00000 0.00000 -4.00000 -4.00000 0.00000 0.00000 -4.00000 -4.00000 0.00000 0.00000 -4.00000 -4.00000 0.00000 0.00000 -4.00000 -4.00000 -4.00000 -4.00000 -8.00000 -8.00000 -4.00000 -4.00000 -8.00000 -8.00000 -4.00000 -4.00000 -8.00000 -8.00000 -4.00000 -4.00000 -8.00000 -8.00000 -8.00000 -8.00000 -12.00000 -12.00000 -8.00000 -8.00000 -12.00000 -12.00000 -8.00000 -8.00000 -12.00000 -12.00000 -8.00000 -8.00000 -12.00000 -12.00000 -12.00000 -12.00000 -16.00000 -16.00000 -12.00000 -12.00000 -16.00000 -16.00000 -12.00000 -12.00000 -16.00000 -16.00000 -12.00000 -12.00000 -16.00000 -16.00000 
+0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
+
+cells 16
+quad 4
+  1 3 4 2
+quad 4
+  5 7 8 6
+quad 4
+  9 11 12 10
+quad 4
+  13 15 16 14
+quad 4
+  17 19 20 18
+quad 4
+  21 23 24 22
+quad 4
+  25 27 28 26
+quad 4
+  29 31 32 30
+quad 4
+  33 35 36 34
+quad 4
+  37 39 40 38
+quad 4
+  41 43 44 42
+quad 4
+  45 47 48 46
+quad 4
+  49 51 52 50
+quad 4
+  53 55 56 54
+quad 4
+  57 59 60 58
+quad 4
+  61 63 64 62
+
+variable
+collated_matrix 1
+0.00762 -0.01462 0.07762 0.05538 -0.01462 -0.10287 0.05538 -0.03287 -0.10287 -0.30513 -0.03287 -0.23513 -0.30513 -0.66938 -0.23513 -0.59938 0.07762 0.05538 0.22762 0.20538 0.05538 -0.03287 0.20538 0.11713 -0.03287 -0.23513 0.11713 -0.08512 -0.23513 -0.59938 -0.08512 -0.44938 0.22762 0.20538 0.45763 0.43537 0.20538 0.11713 0.43537 0.34712 0.11713 -0.08512 0.34712 0.14488 -0.08512 -0.44938 0.14488 -0.21937 0.45763 0.43537 0.76762 0.74538 0.43537 0.34712 0.74538 0.65712 0.34712 0.14488 0.65712 0.45487 0.14488 -0.21937 0.45487 0.09063 
+
+endvars
+endgmv

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.