From: wolf Date: Wed, 22 Aug 2001 11:52:26 +0000 (+0000) Subject: Add optional options to matrix output class. X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=808e313555d934a61184ec833c671b7ccc3453b7;p=dealii-svn.git Add optional options to matrix output class. git-svn-id: https://svn.dealii.org/trunk@4903 0785d39b-7218-0410-832d-ea1e28bc413d --- diff --git a/deal.II/lac/include/lac/matrix_out.h b/deal.II/lac/include/lac/matrix_out.h index 9b514fd8e6..910e12289d 100644 --- a/deal.II/lac/include/lac/matrix_out.h +++ b/deal.II/lac/include/lac/matrix_out.h @@ -48,11 +48,65 @@ * @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 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 + 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 +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 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(-i-1); patches[index].vertices[3](0) = j+1; patches[index].vertices[3](1) = static_cast(-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 diff --git a/deal.II/lac/source/matrix_out.cc b/deal.II/lac/source/matrix_out.cc index 5e316c0be4..d798407359 100644 --- a/deal.II/lac/source/matrix_out.cc +++ b/deal.II/lac/source/matrix_out.cc @@ -3,6 +3,15 @@ #include +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 () {}; diff --git a/tests/lac/matrix_out.cc b/tests/lac/matrix_out.cc index 8b0e88f26c..c3d556f755 100644 --- a/tests/lac/matrix_out.cc +++ b/tests/lac/matrix_out.cc @@ -51,10 +51,25 @@ int main () 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))); + sparse_matrix.set(i,j, static_cast(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 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); }; }; diff --git a/tests/lac/matrix_out.checked b/tests/lac/matrix_out.checked index acc14936f0..5bdc305017 100644 --- a/tests/lac/matrix_out.checked +++ b/tests/lac/matrix_out.checked @@ -126,3 +126,50 @@ 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