]> https://gitweb.dealii.org/ - dealii.git/commitdiff
Enable AffineConstraints::copy_from for mixed number (double/float) 11678/head
authorPeter Munch <peterrmuench@gmail.com>
Thu, 4 Feb 2021 10:17:34 +0000 (11:17 +0100)
committerPeter Munch <peterrmuench@gmail.com>
Fri, 5 Feb 2021 22:23:36 +0000 (23:23 +0100)
doc/news/changes/minor/20210205Munch [new file with mode: 0644]
include/deal.II/lac/affine_constraints.h
include/deal.II/lac/affine_constraints.templates.h
tests/lac/constraints_copy_from_mixed.cc [new file with mode: 0644]
tests/lac/constraints_copy_from_mixed.output [new file with mode: 0644]

diff --git a/doc/news/changes/minor/20210205Munch b/doc/news/changes/minor/20210205Munch
new file mode 100644 (file)
index 0000000..8f82d8d
--- /dev/null
@@ -0,0 +1,5 @@
+Improved: AffineConstraints::copy_from() now also works for differing
+number template types.
+<br>
+(Peter Munch, Maximilian Bergbauer, 2021/02/05)
+
index eede1cc32bbe502a791fa843c447edbc59c39e99..93f242cdb57e31bbbd037afe531fc3abe3c219fb 100644 (file)
@@ -594,8 +594,9 @@ public:
    * This function exists because @p operator=() is explicitly
    * disabled.
    */
+  template <typename other_number>
   void
-  copy_from(const AffineConstraints &other);
+  copy_from(const AffineConstraints<other_number> &other);
 
   /**
    * clear() the AffineConstraints object and supply an IndexSet with lines
@@ -1597,6 +1598,26 @@ public:
      */
     number inhomogeneity;
 
+    /**
+     * Default constructor.
+     */
+    ConstraintLine(const size_type &index         = numbers::invalid_dof_index,
+                   const Entries &  entries       = {},
+                   const number &   inhomogeneity = 0.0);
+
+    /**
+     * Copy constructor.
+     */
+    template <typename ConstraintLineType>
+    ConstraintLine(const ConstraintLineType &other);
+
+    /**
+     * Copy assignment.
+     */
+    template <typename ConstraintLineType>
+    ConstraintLine &
+    operator=(const ConstraintLineType &other);
+
     /**
      * This operator is a bit weird and unintuitive: it compares the line
      * numbers of two lines. We need this to sort the lines; in fact we could
@@ -1797,6 +1818,9 @@ public:
                  << "that every processor who owns a DoF that constrains "
                  << "another DoF also knows about this constraint?");
 
+  template <typename>
+  friend class AffineConstraints;
+
 private:
   /**
    * Store the lines of the matrix.  Entries are usually appended in an
@@ -2394,6 +2418,22 @@ namespace internal
 } // namespace internal
 
 
+
+template <typename number>
+template <typename other_number>
+inline void
+AffineConstraints<number>::copy_from(
+  const AffineConstraints<other_number> &other)
+{
+  lines.clear();
+  lines.insert(lines.begin(), other.lines.begin(), other.lines.end());
+  lines_cache = other.lines_cache;
+  local_lines = other.local_lines;
+  sorted      = other.sorted;
+}
+
+
+
 template <typename number>
 template <typename MatrixType>
 inline void
@@ -2467,6 +2507,51 @@ AffineConstraints<number>::add_entries_local_to_global(
                              SparsityPatternType>::value>());
 }
 
+
+
+template <typename number>
+inline AffineConstraints<number>::ConstraintLine::ConstraintLine(
+  const size_type &                                                  index,
+  const typename AffineConstraints<number>::ConstraintLine::Entries &entries,
+  const number &inhomogeneity)
+  : index(index)
+  , entries(entries)
+  , inhomogeneity(inhomogeneity)
+{}
+
+
+
+template <typename number>
+template <typename ConstraintLineType>
+inline AffineConstraints<number>::ConstraintLine::ConstraintLine(
+  const ConstraintLineType &other)
+{
+  this->index = other.index;
+
+  entries.clear();
+  entries.insert(entries.begin(), other.entries.begin(), other.entries.end());
+
+  this->inhomogeneity = other.inhomogeneity;
+}
+
+
+
+template <typename number>
+template <typename ConstraintLineType>
+inline typename AffineConstraints<number>::ConstraintLine &
+AffineConstraints<number>::ConstraintLine::
+operator=(const ConstraintLineType &other)
+{
+  this->index = other.index;
+
+  entries.clear();
+  entries.insert(entries.begin(), other.entries.begin(), other.entries.end());
+
+  this->inhomogeneity = other.inhomogeneity;
+
+  return *this;
+}
+
 DEAL_II_NAMESPACE_CLOSE
 
 #endif
index 4d0c728c8cd26d0a9d8942e6baadf37140a0a29a..28d4eca74567030efc858eb130005c7028ba1d35 100644 (file)
@@ -62,18 +62,6 @@ DEAL_II_NAMESPACE_OPEN
 
 
 
-template <typename number>
-void
-AffineConstraints<number>::copy_from(const AffineConstraints<number> &other)
-{
-  lines       = other.lines;
-  lines_cache = other.lines_cache;
-  local_lines = other.local_lines;
-  sorted      = other.sorted;
-}
-
-
-
 template <typename number>
 bool
 AffineConstraints<number>::ConstraintLine::
diff --git a/tests/lac/constraints_copy_from_mixed.cc b/tests/lac/constraints_copy_from_mixed.cc
new file mode 100644 (file)
index 0000000..921de11
--- /dev/null
@@ -0,0 +1,63 @@
+// ---------------------------------------------------------------------
+//
+// Copyright (C) 2021 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.
+//
+// ---------------------------------------------------------------------
+
+// Test AffineConstraints::copy_from() for different types (float, double).
+
+#include <deal.II/lac/affine_constraints.h>
+
+#include <vector>
+
+#include "../tests.h"
+
+template <typename T1, typename T2>
+void
+test()
+{
+  AffineConstraints<T1> constraints;
+
+  constraints.add_line(1);
+  constraints.add_entry(1, 2, 1.);
+  constraints.add_entry(1, 3, 1.);
+
+  constraints.add_line(3);
+  constraints.add_entry(3, 4, 1.);
+  constraints.add_entry(3, 5, 1.);
+
+  constraints.add_line(5);
+  constraints.add_entry(5, 0, 1.);
+
+  constraints.close();
+
+  AffineConstraints<T2> constraints_float;
+
+  constraints_float.copy_from(constraints);
+
+  constraints.print(deallog.get_file_stream());
+  deallog << std::endl;
+  constraints_float.print(deallog.get_file_stream());
+  deallog << std::endl;
+}
+
+int
+main()
+{
+  initlog();
+  deallog << std::setprecision(2);
+
+  test<double, double>();
+  test<float, double>();
+  test<double, float>();
+  test<float, float>();
+}
diff --git a/tests/lac/constraints_copy_from_mixed.output b/tests/lac/constraints_copy_from_mixed.output
new file mode 100644 (file)
index 0000000..cf81ab1
--- /dev/null
@@ -0,0 +1,57 @@
+
+    1 0:  1.00000
+    1 2:  1.00000
+    1 4:  1.00000
+    3 0:  1.00000
+    3 4:  1.00000
+    5 0:  1.00000
+DEAL::
+    1 0:  1.00000
+    1 2:  1.00000
+    1 4:  1.00000
+    3 0:  1.00000
+    3 4:  1.00000
+    5 0:  1.00000
+DEAL::
+    1 0:  1.00000
+    1 2:  1.00000
+    1 4:  1.00000
+    3 0:  1.00000
+    3 4:  1.00000
+    5 0:  1.00000
+DEAL::
+    1 0:  1.00000
+    1 2:  1.00000
+    1 4:  1.00000
+    3 0:  1.00000
+    3 4:  1.00000
+    5 0:  1.00000
+DEAL::
+    1 0:  1.00000
+    1 2:  1.00000
+    1 4:  1.00000
+    3 0:  1.00000
+    3 4:  1.00000
+    5 0:  1.00000
+DEAL::
+    1 0:  1.00000
+    1 2:  1.00000
+    1 4:  1.00000
+    3 0:  1.00000
+    3 4:  1.00000
+    5 0:  1.00000
+DEAL::
+    1 0:  1.00000
+    1 2:  1.00000
+    1 4:  1.00000
+    3 0:  1.00000
+    3 4:  1.00000
+    5 0:  1.00000
+DEAL::
+    1 0:  1.00000
+    1 2:  1.00000
+    1 4:  1.00000
+    3 0:  1.00000
+    3 4:  1.00000
+    5 0:  1.00000
+DEAL::

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.