From d504765329bc94117f97874622fe3c19b26daa9a Mon Sep 17 00:00:00 2001 From: Wolfgang Bangerth Date: Mon, 23 Oct 2023 11:03:42 -0600 Subject: [PATCH] Make it not an error to call constrain_dof_to_zero() more than once. --- include/deal.II/lac/affine_constraints.h | 6 ++++ .../lac/affine_constraints.templates.h | 36 +++++++++++++------ 2 files changed, 31 insertions(+), 11 deletions(-) diff --git a/include/deal.II/lac/affine_constraints.h b/include/deal.II/lac/affine_constraints.h index f61b6d1897..bd0cc60391 100644 --- a/include/deal.II/lac/affine_constraints.h +++ b/include/deal.II/lac/affine_constraints.h @@ -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); diff --git a/include/deal.II/lac/affine_constraints.templates.h b/include/deal.II/lac/affine_constraints.templates.h index 047dcd1b12..63da4a9e9b 100644 --- a/include/deal.II/lac/affine_constraints.templates.h +++ b/include/deal.II/lac/affine_constraints.templates.h @@ -138,6 +138,8 @@ AffineConstraints::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::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::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; + } } -- 2.39.5