From aac4b05f59e2b4d4d690cac0bab1aef09b4b2dff Mon Sep 17 00:00:00 2001 From: Denis Davydov Date: Wed, 20 Mar 2019 11:52:01 +0100 Subject: [PATCH] add DynamicSparsityPattern::nonempty_cols() and nonempty_rows() --- doc/news/changes/minor/20190320DenisDavydov | 4 ++ .../deal.II/lac/dynamic_sparsity_pattern.h | 18 ++++++ source/lac/dynamic_sparsity_pattern.cc | 40 +++++++++++++ tests/sparsity/dynamic_sparsity_pattern_20.cc | 60 +++++++++++++++++++ .../dynamic_sparsity_pattern_20.output | 5 ++ 5 files changed, 127 insertions(+) create mode 100644 doc/news/changes/minor/20190320DenisDavydov create mode 100644 tests/sparsity/dynamic_sparsity_pattern_20.cc create mode 100644 tests/sparsity/dynamic_sparsity_pattern_20.output diff --git a/doc/news/changes/minor/20190320DenisDavydov b/doc/news/changes/minor/20190320DenisDavydov new file mode 100644 index 0000000000..fa9905b7e4 --- /dev/null +++ b/doc/news/changes/minor/20190320DenisDavydov @@ -0,0 +1,4 @@ +New: Add DynamicSparsityPattern::nonempty_cols()/DynamicSparsityPattern::nonempty_rows() to return columns/rows +stored in the sparsity pattern. +
+(Denis Davydov, 2019/03/20) diff --git a/include/deal.II/lac/dynamic_sparsity_pattern.h b/include/deal.II/lac/dynamic_sparsity_pattern.h index ed2a200179..1cb5b8f706 100644 --- a/include/deal.II/lac/dynamic_sparsity_pattern.h +++ b/include/deal.II/lac/dynamic_sparsity_pattern.h @@ -615,6 +615,24 @@ public: const IndexSet & row_index_set() const; + /** + * Return the IndexSet that contains entries for all columns in which at least + * one element exists in this sparsity pattern. + * + * @note In a parallel context, this only considers the locally stored rows. + */ + IndexSet + nonempty_cols() const; + + /** + * Return the IndexSet that contains entries for all rows in which at least + * one element exists in this sparsity pattern. + * + * @note In a parallel context, this only considers the locally stored rows. + */ + IndexSet + nonempty_rows() const; + /** * return whether this object stores only those entries that have been added * explicitly, or if the sparsity pattern contains elements that have been diff --git a/source/lac/dynamic_sparsity_pattern.cc b/source/lac/dynamic_sparsity_pattern.cc index dcf53e1bbc..c12ef6141f 100644 --- a/source/lac/dynamic_sparsity_pattern.cc +++ b/source/lac/dynamic_sparsity_pattern.cc @@ -23,6 +23,7 @@ #include #include #include +#include DEAL_II_NAMESPACE_OPEN @@ -554,6 +555,45 @@ DynamicSparsityPattern::n_nonzero_elements() const } + +IndexSet +DynamicSparsityPattern::nonempty_cols() const +{ + std::set cols; + for (const auto &line : lines) + cols.insert(line.entries.begin(), line.entries.end()); + + IndexSet res(this->n_cols()); + res.add_indices(cols.begin(), cols.end()); + return res; +} + + + +IndexSet +DynamicSparsityPattern::nonempty_rows() const +{ + const IndexSet all_rows = complete_index_set(this->n_rows()); + const IndexSet &locally_stored_rows = rowset.size() == 0 ? all_rows : rowset; + + std::vector rows; + auto line = lines.begin(); + AssertDimension(locally_stored_rows.n_elements(), lines.size()); + for (const auto &row : locally_stored_rows) + { + if (line->entries.size() > 0) + rows.push_back(row); + + ++line; + } + + IndexSet res(this->n_rows()); + res.add_indices(rows.begin(), rows.end()); + return res; +} + + + DynamicSparsityPattern::size_type DynamicSparsityPattern::memory_consumption() const { diff --git a/tests/sparsity/dynamic_sparsity_pattern_20.cc b/tests/sparsity/dynamic_sparsity_pattern_20.cc new file mode 100644 index 0000000000..2f1836836d --- /dev/null +++ b/tests/sparsity/dynamic_sparsity_pattern_20.cc @@ -0,0 +1,60 @@ +// --------------------------------------------------------------------- +// +// Copyright (C) 2004 - 2017 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.md at +// the top level directory of deal.II. +// +// --------------------------------------------------------------------- + + + +// check DynamicSparsityPattern::nonempty_columns() +// and nonempty_rows() + +#include + +#include "../tests.h" + + +void +test() +{ + const unsigned int N = 100; + DynamicSparsityPattern dsp(N, 2 * N); + + for (unsigned int i = 0; i < 5; ++i) + for (unsigned int j = (i == 0 ? 0 : i - 1); j < i + 1; ++j) + dsp.add(i, j); + + dsp.add(50, 50); + dsp.add(51, 51); + dsp.add(90, 52); + + dsp.add(N - 1, 2 * N - 1); + + const IndexSet cols = dsp.nonempty_cols(); + deallog << "Columns:" << std::endl; + cols.print(deallog.get_file_stream()); + + const IndexSet rows = dsp.nonempty_rows(); + deallog << "Rows:" << std::endl; + rows.print(deallog.get_file_stream()); +} + + + +int +main() +{ + initlog(); + + test(); + return 0; +} diff --git a/tests/sparsity/dynamic_sparsity_pattern_20.output b/tests/sparsity/dynamic_sparsity_pattern_20.output new file mode 100644 index 0000000000..ee0ac1f04c --- /dev/null +++ b/tests/sparsity/dynamic_sparsity_pattern_20.output @@ -0,0 +1,5 @@ + +DEAL::Columns: +{[0,4], [50,52], 199} +DEAL::Rows: +{[0,4], [50,51], 90, 99} -- 2.39.5