From a4706d66612d07bb7c0a713a941d34307281a225 Mon Sep 17 00:00:00 2001 From: Wolfgang Bangerth Date: Mon, 11 May 2015 08:40:26 -0500 Subject: [PATCH] Also make MatrixOut work for PETSc matrices. --- doc/news/changes.h | 2 +- include/deal.II/lac/matrix_out.h | 14 +- tests/lac/matrix_out_03.cc | 61 +++++ .../lac/matrix_out_03.with_petsc=true.output | 232 ++++++++++++++++++ 4 files changed, 303 insertions(+), 6 deletions(-) create mode 100644 tests/lac/matrix_out_03.cc create mode 100644 tests/lac/matrix_out_03.with_petsc=true.output diff --git a/doc/news/changes.h b/doc/news/changes.h index ec481a65f8..8b2e37c95f 100644 --- a/doc/news/changes.h +++ b/doc/news/changes.h @@ -493,7 +493,7 @@ inconvenience this causes.

Specific improvements

    -
  1. Fixed: MatrixOut now also works with Trilinos matrices. +
  2. Fixed: MatrixOut now also works with Trilinos and PETSc matrices.
    (Wolfgang Bangerth, 2015/05/11)
  3. diff --git a/include/deal.II/lac/matrix_out.h b/include/deal.II/lac/matrix_out.h index 7be4cbbbab..261e9c294f 100644 --- a/include/deal.II/lac/matrix_out.h +++ b/include/deal.II/lac/matrix_out.h @@ -26,10 +26,6 @@ # include #endif -#ifdef DEAL_II_WITH_PETSC -# include -# include -#endif DEAL_II_NAMESPACE_OPEN @@ -223,9 +219,10 @@ namespace internal return matrix.el(i,j); } + #ifdef DEAL_II_WITH_TRILINOS /** - * Return the element with given indices of a sparse matrix. + * Return the element with given indices of a Trilinos sparse matrix. */ double get_element (const TrilinosWrappers::SparseMatrix &matrix, const types::global_dof_index i, @@ -249,6 +246,13 @@ namespace internal #endif +#ifdef DEAL_II_WITH_PETSC + // no need to do anything: PETSc matrix objects do not distinguish + // between operator() and el(i,j), so we can safely access elements + // through the generic function below +#endif + + /** * Return the element with given indices from any matrix type for which no * specialization of this function was declared above. This will call diff --git a/tests/lac/matrix_out_03.cc b/tests/lac/matrix_out_03.cc new file mode 100644 index 0000000000..b728538528 --- /dev/null +++ b/tests/lac/matrix_out_03.cc @@ -0,0 +1,61 @@ +// --------------------------------------------------------------------- +// +// Copyright (C) 2001 - 2015 by the deal.II authors +// +// This file is part of the deal.II library. +// +// The deal.II library is free software; you can use it, redistribute +// it, and/or modify it under the terms of the GNU Lesser General +// Public License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// The full text of the license can be found in the file LICENSE at +// the top level of the deal.II distribution. +// +// --------------------------------------------------------------------- + + +// like matrix_out_02.cc, but test for PETSc matrices + + +#include "../tests.h" +#include +#include +#include +#include +#include + +int main (int argc, char **argv) +{ + Utilities::MPI::MPI_InitFinalize mpi_initialization (argc, argv, numbers::invalid_unsigned_int); + + std::ofstream logfile("output"); + deallog << std::fixed; + deallog << std::setprecision(2); + deallog.attach(logfile); + deallog.depth_console(0); + deallog.threshold_double(1.e-10); + + // test for a rectangular sparse + // matrix + if (true) + { + DynamicSparsityPattern sparsity (4,8); + for (unsigned int i=0; i<4; ++i) + for (unsigned int j=0; j<8; ++j) + if (i==j+1) + sparsity.add (i,j); + sparsity.compress (); + + PETScWrappers::SparseMatrix sparse_matrix(sparsity); + for (unsigned int i=0; i<4; ++i) + for (unsigned int j=0; j<8; ++j) + if (i==j+1) + sparse_matrix.set(i,j, i+3*j); + sparse_matrix.compress(VectorOperation::insert); + + MatrixOut matrix_out; + matrix_out.build_patches (sparse_matrix, "sparse_matrix", + MatrixOut::Options (true, 1, true)); + matrix_out.write_gnuplot (logfile); + } +} diff --git a/tests/lac/matrix_out_03.with_petsc=true.output b/tests/lac/matrix_out_03.with_petsc=true.output new file mode 100644 index 0000000000..c0fa8d8c6e --- /dev/null +++ b/tests/lac/matrix_out_03.with_petsc=true.output @@ -0,0 +1,232 @@ + +# This file was generated by the deal.II library. + + +# +# For a description of the GNUPLOT format see the GNUPLOT manual. +# +# +0.00000 0.00000 0.00000 +0.00000 -1.00000 0.00000 + +1.00000 0.00000 0.00000 +1.00000 -1.00000 0.00000 + + +1.00000 0.00000 0.00000 +1.00000 -1.00000 0.00000 + +2.00000 0.00000 0.00000 +2.00000 -1.00000 0.00000 + + +2.00000 0.00000 0.00000 +2.00000 -1.00000 0.00000 + +3.00000 0.00000 0.00000 +3.00000 -1.00000 0.00000 + + +3.00000 0.00000 0.00000 +3.00000 -1.00000 0.00000 + +4.00000 0.00000 0.00000 +4.00000 -1.00000 0.00000 + + +4.00000 0.00000 0.00000 +4.00000 -1.00000 0.00000 + +5.00000 0.00000 0.00000 +5.00000 -1.00000 0.00000 + + +5.00000 0.00000 0.00000 +5.00000 -1.00000 0.00000 + +6.00000 0.00000 0.00000 +6.00000 -1.00000 0.00000 + + +6.00000 0.00000 0.00000 +6.00000 -1.00000 0.00000 + +7.00000 0.00000 0.00000 +7.00000 -1.00000 0.00000 + + +7.00000 0.00000 0.00000 +7.00000 -1.00000 0.00000 + +8.00000 0.00000 0.00000 +8.00000 -1.00000 0.00000 + + +0.00000 -1.00000 1.00000 +0.00000 -2.00000 1.00000 + +1.00000 -1.00000 1.00000 +1.00000 -2.00000 1.00000 + + +1.00000 -1.00000 0.00000 +1.00000 -2.00000 0.00000 + +2.00000 -1.00000 0.00000 +2.00000 -2.00000 0.00000 + + +2.00000 -1.00000 0.00000 +2.00000 -2.00000 0.00000 + +3.00000 -1.00000 0.00000 +3.00000 -2.00000 0.00000 + + +3.00000 -1.00000 0.00000 +3.00000 -2.00000 0.00000 + +4.00000 -1.00000 0.00000 +4.00000 -2.00000 0.00000 + + +4.00000 -1.00000 0.00000 +4.00000 -2.00000 0.00000 + +5.00000 -1.00000 0.00000 +5.00000 -2.00000 0.00000 + + +5.00000 -1.00000 0.00000 +5.00000 -2.00000 0.00000 + +6.00000 -1.00000 0.00000 +6.00000 -2.00000 0.00000 + + +6.00000 -1.00000 0.00000 +6.00000 -2.00000 0.00000 + +7.00000 -1.00000 0.00000 +7.00000 -2.00000 0.00000 + + +7.00000 -1.00000 0.00000 +7.00000 -2.00000 0.00000 + +8.00000 -1.00000 0.00000 +8.00000 -2.00000 0.00000 + + +0.00000 -2.00000 0.00000 +0.00000 -3.00000 0.00000 + +1.00000 -2.00000 0.00000 +1.00000 -3.00000 0.00000 + + +1.00000 -2.00000 5.00000 +1.00000 -3.00000 5.00000 + +2.00000 -2.00000 5.00000 +2.00000 -3.00000 5.00000 + + +2.00000 -2.00000 0.00000 +2.00000 -3.00000 0.00000 + +3.00000 -2.00000 0.00000 +3.00000 -3.00000 0.00000 + + +3.00000 -2.00000 0.00000 +3.00000 -3.00000 0.00000 + +4.00000 -2.00000 0.00000 +4.00000 -3.00000 0.00000 + + +4.00000 -2.00000 0.00000 +4.00000 -3.00000 0.00000 + +5.00000 -2.00000 0.00000 +5.00000 -3.00000 0.00000 + + +5.00000 -2.00000 0.00000 +5.00000 -3.00000 0.00000 + +6.00000 -2.00000 0.00000 +6.00000 -3.00000 0.00000 + + +6.00000 -2.00000 0.00000 +6.00000 -3.00000 0.00000 + +7.00000 -2.00000 0.00000 +7.00000 -3.00000 0.00000 + + +7.00000 -2.00000 0.00000 +7.00000 -3.00000 0.00000 + +8.00000 -2.00000 0.00000 +8.00000 -3.00000 0.00000 + + +0.00000 -3.00000 0.00000 +0.00000 -4.00000 0.00000 + +1.00000 -3.00000 0.00000 +1.00000 -4.00000 0.00000 + + +1.00000 -3.00000 0.00000 +1.00000 -4.00000 0.00000 + +2.00000 -3.00000 0.00000 +2.00000 -4.00000 0.00000 + + +2.00000 -3.00000 9.00000 +2.00000 -4.00000 9.00000 + +3.00000 -3.00000 9.00000 +3.00000 -4.00000 9.00000 + + +3.00000 -3.00000 0.00000 +3.00000 -4.00000 0.00000 + +4.00000 -3.00000 0.00000 +4.00000 -4.00000 0.00000 + + +4.00000 -3.00000 0.00000 +4.00000 -4.00000 0.00000 + +5.00000 -3.00000 0.00000 +5.00000 -4.00000 0.00000 + + +5.00000 -3.00000 0.00000 +5.00000 -4.00000 0.00000 + +6.00000 -3.00000 0.00000 +6.00000 -4.00000 0.00000 + + +6.00000 -3.00000 0.00000 +6.00000 -4.00000 0.00000 + +7.00000 -3.00000 0.00000 +7.00000 -4.00000 0.00000 + + +7.00000 -3.00000 0.00000 +7.00000 -4.00000 0.00000 + +8.00000 -3.00000 0.00000 +8.00000 -4.00000 0.00000 + + -- 2.39.5