]> https://gitweb.dealii.org/ - dealii.git/commitdiff
add DynamicSparsityPattern::nonempty_cols() and nonempty_rows() 7843/head
authorDenis Davydov <davydden@gmail.com>
Wed, 20 Mar 2019 10:52:01 +0000 (11:52 +0100)
committerDenis Davydov <davydden@gmail.com>
Mon, 25 Mar 2019 07:40:11 +0000 (08:40 +0100)
doc/news/changes/minor/20190320DenisDavydov [new file with mode: 0644]
include/deal.II/lac/dynamic_sparsity_pattern.h
source/lac/dynamic_sparsity_pattern.cc
tests/sparsity/dynamic_sparsity_pattern_20.cc [new file with mode: 0644]
tests/sparsity/dynamic_sparsity_pattern_20.output [new file with mode: 0644]

diff --git a/doc/news/changes/minor/20190320DenisDavydov b/doc/news/changes/minor/20190320DenisDavydov
new file mode 100644 (file)
index 0000000..fa9905b
--- /dev/null
@@ -0,0 +1,4 @@
+New: Add DynamicSparsityPattern::nonempty_cols()/DynamicSparsityPattern::nonempty_rows() to return columns/rows
+stored in the sparsity pattern.
+<br>
+(Denis Davydov, 2019/03/20)
index ed2a200179cc02cb1517ee7e683808b9843e4536..1cb5b8f706f3de45a62cecf115d167f505c05623 100644 (file)
@@ -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
index dcf53e1bbcef60a9194ea5a725c3289562fa5ed6..c12ef6141f5b458d6c4d76fbe2a078471d24ef94 100644 (file)
@@ -23,6 +23,7 @@
 #include <cmath>
 #include <functional>
 #include <numeric>
+#include <set>
 
 DEAL_II_NAMESPACE_OPEN
 
@@ -554,6 +555,45 @@ DynamicSparsityPattern::n_nonzero_elements() const
 }
 
 
+
+IndexSet
+DynamicSparsityPattern::nonempty_cols() const
+{
+  std::set<types::global_dof_index> 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<types::global_dof_index> 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 (file)
index 0000000..2f18368
--- /dev/null
@@ -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 <deal.II/lac/dynamic_sparsity_pattern.h>
+
+#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 (file)
index 0000000..ee0ac1f
--- /dev/null
@@ -0,0 +1,5 @@
+
+DEAL::Columns:
+{[0,4], [50,52], 199}
+DEAL::Rows:
+{[0,4], [50,51], 90, 99}

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.