]> https://gitweb.dealii.org/ - dealii.git/commitdiff
Add DynamicSparsityPattern::get_view() 8047/head
authorDenis Davydov <davydden@gmail.com>
Wed, 8 May 2019 18:15:35 +0000 (20:15 +0200)
committerDenis Davydov <davydden@gmail.com>
Thu, 9 May 2019 14:45:02 +0000 (16:45 +0200)
doc/news/changes/minor/20190508DenisDavydov [new file with mode: 0644]
include/deal.II/lac/dynamic_sparsity_pattern.h
source/lac/dynamic_sparsity_pattern.cc
tests/sparsity/dynamic_sparsity_pattern_22.cc [new file with mode: 0644]
tests/sparsity/dynamic_sparsity_pattern_22.output [new file with mode: 0644]

diff --git a/doc/news/changes/minor/20190508DenisDavydov b/doc/news/changes/minor/20190508DenisDavydov
new file mode 100644 (file)
index 0000000..db06a72
--- /dev/null
@@ -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)
index b8ba6bfcedc64648eca0039ee50b28bf3c64a9b8..6cd9e74a6bd9e304877240de5d2482812d8017ed 100644 (file)
@@ -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.
index 5631edc66191eeba3c7e2884b279d76328b4139c..a6a4c054012ea4fd678c852a721b76cbaa1a4fa6 100644 (file)
@@ -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 (file)
index 0000000..dda2796
--- /dev/null
@@ -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 (file)
index 0000000..442c26f
--- /dev/null
@@ -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]

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.