]> https://gitweb.dealii.org/ - dealii.git/commitdiff
Make it not an error to call constrain_dof_to_zero() more than once.
authorWolfgang Bangerth <bangerth@colostate.edu>
Mon, 23 Oct 2023 17:03:42 +0000 (11:03 -0600)
committerWolfgang Bangerth <bangerth@colostate.edu>
Mon, 23 Oct 2023 17:13:16 +0000 (11:13 -0600)
include/deal.II/lac/affine_constraints.h
include/deal.II/lac/affine_constraints.templates.h

index f61b6d1897c327dda2fb7991c5b90ec4c41c43b4..bd0cc60391a3e8bc838e38fbd03ba9e602c18783 100644 (file)
@@ -825,6 +825,12 @@ public:
    * @code
    *   constraints.add_constraint (42, {}, 0.0);
    * @endcode
+   *
+   * It is not an error to call this function more than once on the same
+   * degree of freedom, but it is an error to call this function on a
+   * degree of freedom that has previously been constrained to either
+   * a different value than zero, or to a linear combination of degrees
+   * of freedom via the add_constraint() function.
    */
   void
   constrain_dof_to_zero(const size_type constrained_dof);
index 047dcd1b126b6e9f2233b4529d86d95977015d1e..63da4a9e9b6ce556cbecb9d191d11a12ae294d51 100644 (file)
@@ -138,6 +138,8 @@ AffineConstraints<number>::add_constraint(
   constraint.inhomogeneity = inhomogeneity;
 
   // Record the new constraint in the cache:
+  Assert(lines_cache[line_index] == numbers::invalid_size_type,
+         ExcInternalError());
   lines_cache[line_index] = lines.size() - 1;
 }
 
@@ -149,9 +151,6 @@ AffineConstraints<number>::constrain_dof_to_zero(
   const size_type constrained_dof)
 {
   Assert(sorted == false, ExcMatrixIsClosed());
-  Assert(is_constrained(constrained_dof) == false,
-         ExcMessage("You cannot add a constraint for a degree of freedom "
-                    "that is already constrained."));
 
   // The following can happen when we compute with distributed meshes and dof
   // handlers and we constrain a degree of freedom whose number we don't have
@@ -166,14 +165,29 @@ AffineConstraints<number>::constrain_dof_to_zero(
                                 line_index + 1),
                        numbers::invalid_size_type);
 
-  // Push a new line to the end of the list and fill it with the
-  // provided information:
-  ConstraintLine &constraint = lines.emplace_back();
-  constraint.index           = constrained_dof;
-  constraint.inhomogeneity   = 0.;
-
-  // Record the new constraint in the cache:
-  lines_cache[line_index] = lines.size() - 1;
+  // Let's check whether the DoF is already constrained. This is only allowed
+  // if it had previously been constrained to zero, and only then.
+  if (lines_cache[line_index] != numbers::invalid_size_type)
+    {
+      Assert(lines[lines_cache[line_index]].entries.empty() &&
+               (lines[lines_cache[line_index]].inhomogeneity == number(0.)),
+             ExcMessage("You cannot constrain a degree of freedom "
+                        "to zero that is is already constrained to "
+                        "something else."));
+    }
+  else
+    {
+      // Push a new line to the end of the list and fill it with the
+      // provided information:
+      ConstraintLine &constraint = lines.emplace_back();
+      constraint.index           = constrained_dof;
+      constraint.inhomogeneity   = 0.;
+
+      // Record the new constraint in the cache:
+      Assert(lines_cache[line_index] == numbers::invalid_size_type,
+             ExcInternalError());
+      lines_cache[line_index] = lines.size() - 1;
+    }
 }
 
 

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.