]> https://gitweb.dealii.org/ - dealii.git/commitdiff
extend IndexSet::index_within_set() 3700/head
authorDenis Davydov <davydden@gmail.com>
Fri, 23 Dec 2016 10:59:11 +0000 (11:59 +0100)
committerDenis Davydov <davydden@gmail.com>
Mon, 26 Dec 2016 21:19:34 +0000 (22:19 +0100)
to return numbers::invalid_dof_index
if the global index is not a member of the index set

doc/news/changes/minor/20161223DenisDavydov [new file with mode: 0644]
include/deal.II/base/index_set.h
tests/base/index_set_28.cc [new file with mode: 0644]
tests/base/index_set_28.output [new file with mode: 0644]
tests/base/index_set_29.cc [new file with mode: 0644]
tests/base/index_set_29.output [new file with mode: 0644]

diff --git a/doc/news/changes/minor/20161223DenisDavydov b/doc/news/changes/minor/20161223DenisDavydov
new file mode 100644 (file)
index 0000000..197f9b8
--- /dev/null
@@ -0,0 +1,4 @@
+New: Modify IndexSet::index_within_set() to return numbers::invalid_dof_index
+if the global index is not a member of this index set.
+<br>
+(Denis Davydov, 2016/12/23)
index c1e3472c336457782e288f0a9cdd2ebf4a31f31f..79f66cac53f9b7e0c2a4d608dae36f64f68d9bec 100644 (file)
@@ -250,7 +250,7 @@ public:
   /**
    * Return the how-manyth element of this set (counted in ascending order) @p
    * global_index is. @p global_index needs to be less than the size(). This
-   * function throws an exception if the index @p global_index is not actually
+   * function returns numbers::invalid_dof_index if the index @p global_index is not actually
    * a member of this index set, i.e. if is_element(global_index) is false.
    */
   size_type index_within_set (const size_type global_index) const;
@@ -1648,7 +1648,6 @@ IndexSet::nth_index_in_set (const unsigned int n) const
 }
 
 
-
 inline
 IndexSet::size_type
 IndexSet::index_within_set (const size_type n) const
@@ -1656,7 +1655,6 @@ IndexSet::index_within_set (const size_type n) const
   // to make this call thread-safe, compress() must not be called through this
   // function
   Assert (is_compressed == true, ExcMessage ("IndexSet must be compressed."));
-  Assert (is_element(n) == true, ExcIndexNotPresent (n));
   Assert (n < size(), ExcIndexRangeType<size_type> (n, 0, size()));
 
   // check whether the index is in the largest range. use the result to
@@ -1683,6 +1681,10 @@ IndexSet::index_within_set (const size_type n) const
   p = Utilities::lower_bound(range_begin, range_end, r,
                              Range::end_compare);
 
+  // if n is not in this set
+  if (p==range_end || p->end == n || p->begin > n)
+    return numbers::invalid_dof_index;
+
   Assert(p!=ranges.end(), ExcInternalError());
   Assert(p->begin<=n, ExcInternalError());
   Assert(n<p->end, ExcInternalError());
diff --git a/tests/base/index_set_28.cc b/tests/base/index_set_28.cc
new file mode 100644 (file)
index 0000000..09d80dd
--- /dev/null
@@ -0,0 +1,79 @@
+// ---------------------------------------------------------------------
+//
+// Copyright (C) 2016 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.
+//
+// ---------------------------------------------------------------------
+
+
+// test IndexSet::index_within_set () for global indices which are not
+// part of a contiguous index set.
+// This test has exactly the same output as index_set_10
+
+#include "../tests.h"
+#include <iomanip>
+#include <iomanip>
+#include <fstream>
+#include <cmath>
+
+#include <deal.II/base/index_set.h>
+
+
+void test ()
+{
+  IndexSet index_set (20);
+
+  index_set.add_index (2);
+  index_set.add_index (3);
+  index_set.add_index (4);
+
+  index_set.add_index (6);
+  index_set.add_index (7);
+
+  index_set.compress ();
+
+  index_set.add_index (5);
+
+  for (unsigned int i=0; i<index_set.n_elements(); ++i)
+    {
+      deallog << index_set.nth_index_in_set(i)
+              << std::endl;
+
+      AssertThrow (index_set.index_within_set(index_set.nth_index_in_set(i) == i),
+                   ExcInternalError());
+    }
+  deallog << "OK" << std::endl;
+
+  for (unsigned int i=0; i<index_set.size(); ++i)
+    {
+      const unsigned int i_out = index_set.index_within_set(i);
+      const bool within = (i_out != numbers::invalid_dof_index);
+      AssertThrow (within == index_set.is_element(i),
+                   ExcInternalError());
+
+      if (within)
+        deallog << i << ' ' << i_out
+                << std::endl;
+
+    }
+}
+
+
+
+
+int main()
+{
+  std::ofstream logfile("output");
+  deallog.attach(logfile);
+  deallog.threshold_double(1.e-10);
+
+  test ();
+}
diff --git a/tests/base/index_set_28.output b/tests/base/index_set_28.output
new file mode 100644 (file)
index 0000000..a5cb34c
--- /dev/null
@@ -0,0 +1,14 @@
+
+DEAL::2
+DEAL::3
+DEAL::4
+DEAL::5
+DEAL::6
+DEAL::7
+DEAL::OK
+DEAL::2 0
+DEAL::3 1
+DEAL::4 2
+DEAL::5 3
+DEAL::6 4
+DEAL::7 5
diff --git a/tests/base/index_set_29.cc b/tests/base/index_set_29.cc
new file mode 100644 (file)
index 0000000..ae2577a
--- /dev/null
@@ -0,0 +1,79 @@
+// ---------------------------------------------------------------------
+//
+// Copyright (C) 2016 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.
+//
+// ---------------------------------------------------------------------
+
+
+// test IndexSet::index_within_set () for global indices which are not
+// part of a non-contiguous index set.
+// This test has exactly the same output as index_set_12
+
+#include "../tests.h"
+#include <iomanip>
+#include <iomanip>
+#include <fstream>
+#include <cmath>
+
+#include <deal.II/base/index_set.h>
+
+
+void test ()
+{
+  IndexSet index_set (20);
+
+  index_set.add_index (2);
+  index_set.add_index (3);
+  index_set.add_index (4);
+
+  index_set.add_index (6);
+  index_set.add_index (7);
+
+  index_set.compress ();
+
+  index_set.add_index (9);
+
+  for (unsigned int i=0; i<index_set.n_elements(); ++i)
+    {
+      deallog << index_set.nth_index_in_set(i)
+              << std::endl;
+
+      AssertThrow (index_set.index_within_set(index_set.nth_index_in_set(i) == i),
+                   ExcInternalError());
+    }
+  deallog << "OK" << std::endl;
+
+  for (unsigned int i=0; i<index_set.size(); ++i)
+    {
+      const unsigned int i_out = index_set.index_within_set(i);
+      const bool within = (i_out != numbers::invalid_dof_index);
+      AssertThrow (within == index_set.is_element(i),
+                   ExcInternalError());
+
+      if (within)
+        deallog << i << ' ' << i_out
+                << std::endl;
+
+    }
+}
+
+
+
+
+int main()
+{
+  std::ofstream logfile("output");
+  deallog.attach(logfile);
+  deallog.threshold_double(1.e-10);
+
+  test ();
+}
diff --git a/tests/base/index_set_29.output b/tests/base/index_set_29.output
new file mode 100644 (file)
index 0000000..99fa7a6
--- /dev/null
@@ -0,0 +1,14 @@
+
+DEAL::2
+DEAL::3
+DEAL::4
+DEAL::6
+DEAL::7
+DEAL::9
+DEAL::OK
+DEAL::2 0
+DEAL::3 1
+DEAL::4 2
+DEAL::6 3
+DEAL::7 4
+DEAL::9 5

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.