From 69b64b866bd080c97c6e34e25ae9ef39eb83ed20 Mon Sep 17 00:00:00 2001
From: Denis Davydov <davydden@gmail.com>
Date: Wed, 8 May 2019 20:15:35 +0200
Subject: [PATCH] Add DynamicSparsityPattern::get_view()

---
 doc/news/changes/minor/20190508DenisDavydov   |  3 +
 .../deal.II/lac/dynamic_sparsity_pattern.h    |  9 +++
 source/lac/dynamic_sparsity_pattern.cc        | 22 +++++++
 tests/sparsity/dynamic_sparsity_pattern_22.cc | 59 +++++++++++++++++++
 .../dynamic_sparsity_pattern_22.output        | 19 ++++++
 5 files changed, 112 insertions(+)
 create mode 100644 doc/news/changes/minor/20190508DenisDavydov
 create mode 100644 tests/sparsity/dynamic_sparsity_pattern_22.cc
 create mode 100644 tests/sparsity/dynamic_sparsity_pattern_22.output

diff --git a/doc/news/changes/minor/20190508DenisDavydov b/doc/news/changes/minor/20190508DenisDavydov
new file mode 100644
index 0000000000..db06a72b54
--- /dev/null
+++ b/doc/news/changes/minor/20190508DenisDavydov
@@ -0,0 +1,3 @@
+New: Add DynamicSparsityPattern::get_view() to create a subset of a sparsity pattern based on a selection of some rows.
+<br>
+(Denis Davydov, 2019/05/08)
diff --git a/include/deal.II/lac/dynamic_sparsity_pattern.h b/include/deal.II/lac/dynamic_sparsity_pattern.h
index b8ba6bfced..6cd9e74a6b 100644
--- a/include/deal.II/lac/dynamic_sparsity_pattern.h
+++ b/include/deal.II/lac/dynamic_sparsity_pattern.h
@@ -446,6 +446,15 @@ public:
   bool
   exists(const size_type i, const size_type j) const;
 
+  /**
+   * Return a view of this sparsity pattern.
+   * That is, for all rows in @p rows extract non-empty columns.
+   * The resulting sparsity pattern will have number of rows equal
+   * `rows.n_elements()`.
+   */
+  DynamicSparsityPattern
+  get_view(const IndexSet &rows) const;
+
   /**
    * Make the sparsity pattern symmetric by adding the sparsity pattern of the
    * transpose object.
diff --git a/source/lac/dynamic_sparsity_pattern.cc b/source/lac/dynamic_sparsity_pattern.cc
index 5631edc661..a6a4c05401 100644
--- a/source/lac/dynamic_sparsity_pattern.cc
+++ b/source/lac/dynamic_sparsity_pattern.cc
@@ -435,6 +435,28 @@ DynamicSparsityPattern::clear_row(const size_type row)
 
 
 
+DynamicSparsityPattern
+DynamicSparsityPattern::get_view(const IndexSet &rows) const
+{
+  DynamicSparsityPattern view;
+  view.reinit(rows.n_elements(), this->n_cols());
+  AssertDimension(rows.size(), this->n_rows());
+
+  const auto                        end      = rows.end();
+  DynamicSparsityPattern::size_type view_row = 0;
+  for (auto it = rows.begin(); it != end; ++it, ++view_row)
+    {
+      const size_type rowindex =
+        rowset.size() == 0 ? *it : rowset.index_within_set(*it);
+
+      view.lines[view_row].entries = lines[rowindex].entries;
+      view.have_entries |= (lines[rowindex].entries.size() > 0);
+    }
+  return view;
+}
+
+
+
 template <typename SparsityPatternTypeLeft, typename SparsityPatternTypeRight>
 void
 DynamicSparsityPattern::compute_Tmmult_pattern(
diff --git a/tests/sparsity/dynamic_sparsity_pattern_22.cc b/tests/sparsity/dynamic_sparsity_pattern_22.cc
new file mode 100644
index 0000000000..dda2796cfd
--- /dev/null
+++ b/tests/sparsity/dynamic_sparsity_pattern_22.cc
@@ -0,0 +1,59 @@
+// ---------------------------------------------------------------------
+//
+// Copyright (C) 2019 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::get_view()
+
+#include <deal.II/lac/dynamic_sparsity_pattern.h>
+
+#include "../tests.h"
+
+
+void
+test()
+{
+  const unsigned int     N = 10;
+  DynamicSparsityPattern dsp(N, N);
+
+  for (unsigned int i = 0; i < N; ++i)
+    if (i != 2 && i != 4)
+      for (unsigned int j = (i == 0 ? 0 : i - 1); j < (i == N - 1 ? N : i + 1);
+           ++j)
+        dsp.add(i, j);
+
+  IndexSet rows(N);
+  rows.add_range(1, 5);
+
+  DynamicSparsityPattern view = dsp.get_view(rows);
+
+  deallog << "Initial sparsity:" << std::endl;
+  dsp.print(deallog.get_file_stream());
+  deallog << "Rows:" << std::endl;
+  rows.print(deallog.get_file_stream());
+  deallog << "View:" << std::endl;
+  view.print(deallog.get_file_stream());
+}
+
+
+
+int
+main()
+{
+  initlog();
+
+  test();
+  return 0;
+}
diff --git a/tests/sparsity/dynamic_sparsity_pattern_22.output b/tests/sparsity/dynamic_sparsity_pattern_22.output
new file mode 100644
index 0000000000..442c26f9ff
--- /dev/null
+++ b/tests/sparsity/dynamic_sparsity_pattern_22.output
@@ -0,0 +1,19 @@
+
+DEAL::Initial sparsity:
+[0,0]
+[1,0,1]
+[2]
+[3,2,3]
+[4]
+[5,4,5]
+[6,5,6]
+[7,6,7]
+[8,7,8]
+[9,8,9]
+DEAL::Rows:
+{[1,4]}
+DEAL::View:
+[0,0,1]
+[1]
+[2,2,3]
+[3]
-- 
2.39.5