15#ifndef dealii_affine_constraints_h
16#define dealii_affine_constraints_h
31#include <boost/range/iterator_range.hpp>
49template <
typename number>
51template <
typename number>
82 Distributing(
const Distributing &in);
85 operator=(
const Distributing &in);
90 return global_row < in.global_row;
95 mutable size_type constraint_position;
111 template <
typename number>
120 insert_new_index(
const std::pair<size_type, number> &pair);
123 append_index(
const size_type index,
124 const std::pair<size_type, number> &pair);
127 get_size(
const size_type index)
const;
129 const std::pair<size_type, number> *
130 get_entry(
const size_type index)
const;
132 size_type row_length;
134 std::vector<std::pair<size_type, number>> data;
136 std::vector<size_type> individual_size;
169 template <
typename number>
170 class GlobalRowsFromLocal
176 GlobalRowsFromLocal();
179 reinit(
const size_type n_local_rows);
182 insert_index(
const size_type global_row,
183 const size_type local_row,
184 const number constraint_value);
189 print(std::ostream &os);
202 size(
const size_type counter_index)
const;
208 global_row(
const size_type counter_index)
const;
214 global_row(
const size_type counter_index);
222 local_row(
const size_type counter_index)
const;
228 local_row(
const size_type counter_index);
236 local_row(
const size_type counter_index,
237 const size_type index_in_constraint)
const;
244 constraint_value(
const size_type counter_index,
245 const size_type index_in_constraint)
const;
254 have_indirect_rows()
const;
261 insert_constraint(
const size_type constrained_local_dof);
269 n_constraints()
const;
276 n_inhomogeneities()
const;
285 set_ith_constraint_inhomogeneous(
const size_type i);
292 constraint_origin(size_type i)
const;
299 std::vector<Distributing> total_row_indices;
305 DataCache<number> data_cache;
311 size_type n_active_rows;
317 size_type n_inhomogeneous_rows;
339 template <
typename number>
352 ScratchData(
const ScratchData &)
364 std::vector<std::pair<size_type, size_type>> new_entries;
369 std::vector<size_type> rows;
374 std::vector<size_type> columns;
379 std::vector<number> values;
384 std::vector<size_type> block_starts;
389 std::vector<size_type> vector_indices;
394 std::vector<number> vector_values;
399 GlobalRowsFromLocal<number> global_rows;
404 GlobalRowsFromLocal<number> global_columns;
411 namespace AffineConstraintsImplementation
413 template <
typename VectorType>
415 set_zero_all(
const std::vector<types::global_dof_index> &cm,
420 set_zero_all(
const std::vector<types::global_dof_index> &cm,
425 set_zero_all(
const std::vector<types::global_dof_index> &cm,
505template <
typename number =
double>
583 DEAL_II_DEPRECATED_EARLY_WITH_COMMENT(
584 "Use the constructor with two index set arguments.")
619 const
IndexSet &locally_stored_constraints);
657 template <typename other_number>
680 DEAL_II_DEPRECATED_EARLY_WITH_COMMENT(
681 "Use the
reinit() function with two index set arguments.")
695 const
IndexSet &locally_stored_constraints);
756 DEAL_II_DEPRECATED_EARLY_WITH_COMMENT("Use
get_view() and
merge() instead.")
815 const number inhomogeneity = 0);
907 const number weight);
997 template <typename other_number>
1002 const
bool allow_different_local_lines = false);
1317 template <typename VectorType>
1327 template <typename VectorType>
1329 condense(const VectorType &vec_ghosted, VectorType &output) const;
1343 template <typename VectorType>
1351 template <typename BlockVectorType>
1361 template <typename VectorType>
1420 template <class InVector, class OutVector>
1424 OutVector &global_vector) const;
1473 template <typename VectorType>
1477 VectorType &global_vector,
1478 const
FullMatrix<number> &local_matrix) const;
1499 template <typename VectorType>
1502 const
Vector<number> &local_vector,
1505 VectorType &global_vector,
1507 bool diagonal = false) const;
1512 template <typename VectorType>
1516 VectorType &global_vector) const;
1550 template <typename ForwardIteratorVec,
1551 typename ForwardIteratorInd,
1552 typename VectorType>
1555 ForwardIteratorVec local_vector_end,
1556 ForwardIteratorInd local_indices_begin,
1557 VectorType &global_vector) const;
1610 template <typename MatrixType>
1614 MatrixType &global_matrix) const;
1643 template <typename MatrixType>
1648 MatrixType &global_matrix) const;
1666 template <typename MatrixType>
1672 MatrixType &global_matrix) const;
1694 template <typename MatrixType, typename VectorType>
1697 const
Vector<number> &local_vector,
1699 MatrixType &global_matrix,
1700 VectorType &global_vector,
1701 bool use_inhomogeneities_for_rhs = false) const;
1760 const
bool keep_constrained_entries = true,
1761 const
Table<2,
bool> &dof_mask =
Table<2,
bool>()) const;
1771 const
bool keep_constrained_entries = true,
1772 const
Table<2,
bool> &dof_mask =
Table<2,
bool>()) const;
1784 const
bool keep_constrained_entries = true,
1785 const
Table<2,
bool> &dof_mask =
Table<2,
bool>()) const;
1806 template <typename ForwardIteratorVec,
1807 typename ForwardIteratorInd,
1808 typename VectorType>
1811 ForwardIteratorInd local_indices_begin,
1812 ForwardIteratorVec local_vector_begin,
1813 ForwardIteratorVec local_vector_end) const;
1836 template <typename VectorType>
1853 using Entries = std::vector<std::pair<size_type, number>>;
1882 const number inhomogeneity = 0.0);
1918 template <
class Archive>
1922 ar &index &entries &inhomogeneity;
1931 std::swap(l1.index, l2.index);
1932 std::swap(l1.entries, l2.entries);
1933 std::swap(l1.inhomogeneity, l2.inhomogeneity);
1988 const IndexSet &locally_active_dofs,
1990 const bool verbose =
false)
const;
2024 const IndexSet &constraints_to_make_consistent,
2034 "You are attempting an operation on an AffineConstraints object "
2035 "that requires the object to not be 'closed', i.e., for which you "
2036 "must not already have called the close() member function. But the "
2037 "object is already closed, and so the operation can not be "
2046 "You are attempting an operation on an AffineConstraints object "
2047 "that requires the object to be 'closed', i.e., for which you "
2048 "needed to call the close() member function. But the object "
2049 "is not currently closed, and so the operation can not be "
2058 <<
"The specified line " << arg1 <<
" does not exist.");
2069 <<
"The entry for the indices " << arg1 <<
" and " << arg2
2070 <<
" already exists, but the values " << arg3 <<
" (old) and "
2071 << arg4 <<
" (new) differ "
2072 <<
"by " << (arg4 - arg3) <<
'.');
2081 <<
"You tried to constrain DoF " << arg1 <<
" to DoF " << arg2
2082 <<
", but that one is also constrained. This is not allowed!");
2090 <<
"Degree of freedom " << arg1
2091 <<
" is constrained from both object in a merge operation.");
2099 <<
"In the given argument a degree of freedom is constrained "
2100 <<
"to another DoF with number " << arg1
2101 <<
", which however is constrained by this object. This is not"
2110 <<
"The index set given to this constraints object indicates "
2111 <<
"constraints for degree of freedom " << arg1
2112 <<
" should not be stored by this object, but a constraint "
2113 <<
"is being added.");
2123 <<
"The index set given to this constraints object indicates "
2124 <<
"constraints using degree of freedom " << arg2
2125 <<
" should not be stored by this object, but a constraint "
2126 <<
"for degree of freedom " << arg1 <<
" uses it.");
2136 <<
"While distributing the constraint for DoF " << arg1
2137 <<
", it turns out that one of the processors "
2138 <<
"who own the " << arg2 <<
" degrees of freedom that x_"
2139 << arg1 <<
" is constrained against does not know about "
2140 <<
"the constraint on x_" << arg1
2141 <<
". Did you not initialize the AffineConstraints container "
2142 <<
"with the appropriate locally_relevant set so "
2143 <<
"that every processor who owns a DoF that constrains "
2144 <<
"another DoF also knows about this constraint?");
2228 internal::AffineConstraints::ScratchData<number>>
2242 template <
typename MatrixType,
typename VectorType>
2246 const std::vector<size_type> &local_dof_indices,
2247 MatrixType &global_matrix,
2248 VectorType &global_vector,
2249 const bool use_inhomogeneities_for_rhs,
2250 const std::bool_constant<false>)
const;
2256 template <
typename MatrixType,
typename VectorType>
2260 const std::vector<size_type> &local_dof_indices,
2261 MatrixType &global_matrix,
2262 VectorType &global_vector,
2263 const bool use_inhomogeneities_for_rhs,
2264 const std::bool_constant<true>)
const;
2275 internal::AffineConstraints::GlobalRowsFromLocal<number>
2276 &global_rows)
const;
2287 std::vector<size_type> &active_dofs)
const;
2292 template <
typename MatrixScalar,
typename VectorScalar>
2296 const internal::AffineConstraints::GlobalRowsFromLocal<number> &global_rows,
2298 const std::vector<size_type> &local_dof_indices,
2304template <
typename number>
2311template <
typename number>
2313 const IndexSet &locally_stored_constraints)
2315 locally_stored_constraints)
2320template <
typename number>
2323 const IndexSet &locally_stored_constraints)
2330 ExcMessage(
"The set of locally stored constraints needs to be a "
2331 "superset of the locally owned DoFs."));
2341template <
typename number>
2356template <
typename number>
2380 lines.emplace_back();
2381 lines.back().index = line_n;
2382 lines.back().inhomogeneity = 0.;
2388template <
typename number>
2392 const number weight)
2395 Assert(constrained_dof_index != column,
2396 ExcMessage(
"Can't constrain a degree of freedom to itself"));
2401 ExcMessage(
"The current AffineConstraints does not contain the line "
2402 "for the current entry. Call AffineConstraints::add_line "
2403 "before calling this function."));
2416 for (
const auto &p : line_ptr->
entries)
2417 if (p.first == column)
2421 constrained_dof_index, column, p.second, weight));
2425 line_ptr->
entries.emplace_back(column, weight);
2430template <
typename number>
2439 ExcMessage(
"call add_line() before calling set_inhomogeneity()"));
2447template <
typename number>
2448template <
typename VectorType>
2454 std::vector<size_type> constrained_lines(
lines.size());
2455 for (
unsigned int i = 0; i <
lines.size(); ++i)
2456 constrained_lines[i] =
lines[i].index;
2457 internal::AffineConstraintsImplementation::set_zero_all(constrained_lines,
2461template <
typename number>
2465 return lines.size();
2468template <
typename number>
2472 return std::count_if(
lines.begin(),
2475 return (line.entries.size() == 1) &&
2476 (line.entries[0].second == number(1.));
2480template <
typename number>
2484 return std::count_if(
lines.begin(),
2487 return (line.inhomogeneity != number(0.));
2491template <
typename number>
2503template <
typename number>
2521template <
typename number>
2522inline const std::vector<std::pair<types::global_dof_index, number>> *
2538template <
typename number>
2552template <
typename number>
2567template <
typename number>
2576template <
typename number>
2585template <
typename number>
2594template <
typename number>
2595template <
typename VectorType>
2600 VectorType &global_vector)
const
2605 global_vector(index) += value;
2611 global_vector(position.
entries[j].first) +=
2612 value * position.
entries[j].second;
2616template <
typename number>
2617template <
typename ForwardIteratorVec,
2618 typename ForwardIteratorInd,
2619 typename VectorType>
2622 ForwardIteratorVec local_vector_begin,
2623 ForwardIteratorVec local_vector_end,
2624 ForwardIteratorInd local_indices_begin,
2625 VectorType &global_vector)
const
2628 for (; local_vector_begin != local_vector_end;
2629 ++local_vector_begin, ++local_indices_begin)
2633 *local_indices_begin,
2641 (*local_vector_begin) * position.
entries[j].second,
2648template <
typename number>
2649template <
class InVector,
class OutVector>
2652 const InVector &local_vector,
2653 const std::vector<size_type> &local_dof_indices,
2654 OutVector &global_vector)
const
2657 Assert(local_vector.size() == local_dof_indices.size(),
2661 local_dof_indices.begin(),
2665template <
typename number>
2666template <
typename ForwardIteratorVec,
2667 typename ForwardIteratorInd,
2668 typename VectorType>
2671 const VectorType &global_vector,
2672 ForwardIteratorInd local_indices_begin,
2673 ForwardIteratorVec local_vector_begin,
2674 ForwardIteratorVec local_vector_end)
const
2677 for (; local_vector_begin != local_vector_end;
2678 ++local_vector_begin, ++local_indices_begin)
2681 *local_vector_begin = global_vector(*local_indices_begin);
2686 typename VectorType::value_type value = position.
inhomogeneity;
2688 value += (global_vector(position.
entries[j].first) *
2690 *local_vector_begin = value;
2697template <
typename MatrixType>
2699template <
typename SparsityPatternType>
2701template <
typename number>
2725 template <
typename MatrixType>
2726 struct IsBlockMatrix
2734 template <
typename T>
2735 static std::true_type
2742 template <
typename T>
2743 static std::true_type
2750 static std::false_type
2760 static const bool value =
2761 std::is_same_v<decltype(check(std::declval<MatrixType *>())),
2766 template <
typename MatrixType>
2767 const bool IsBlockMatrix<MatrixType>::value;
2775template <
typename number>
2776template <
typename other_number>
2782 lines.reserve(other.
lines.size());
2784 for (
const auto &l : other.
lines)
2785 lines.emplace_back(l.index,
2799template <
typename number>
2800template <
typename other_number>
2805 const bool allow_different_local_lines)
2807 (void)allow_different_local_lines;
2808 Assert(allow_different_local_lines ||
2811 "local_lines for this and the other objects are not the same "
2812 "although allow_different_local_lines is false."));
2815 const bool object_was_sorted = sorted;
2830 for (
const std::pair<size_type, number> &entry : line.entries)
2839 ((merge_conflict_behavior != right_object_wins) &&
2841 this->is_constrained(entry.first)))
2842 tmp.push_back(entry);
2848 const auto *other_entries =
2852 const number weight = entry.second;
2854 for (
const auto &other_entry : *other_entries)
2855 tmp.emplace_back(other_entry.first,
2856 other_entry.second * weight);
2858 line.inhomogeneity +=
2863 line.entries.swap(tmp);
2866 if (local_lines.size() != 0)
2867 local_lines.add_indices(other_constraints.
local_lines);
2872 std::fill(lines_cache.begin(),
2880 const size_type local_line_no = calculate_line_index(line.index);
2881 if (local_line_no >= lines_cache.size())
2883 lines_cache[local_line_no] = index++;
2887 for (
const auto &line : other_constraints.
lines)
2889 const size_type local_line_no = calculate_line_index(line.index);
2890 if (local_line_no >= lines_cache.size())
2893 lines.emplace_back(line.index,
2895 line.entries.begin(), line.entries.end()),
2896 line.inhomogeneity);
2897 lines_cache[local_line_no] = index++;
2902 lines.emplace_back(line.index,
2904 line.entries.begin(), line.entries.end()),
2905 line.inhomogeneity);
2907 lines_cache[local_line_no] = index++;
2912 switch (merge_conflict_behavior)
2914 case no_conflicts_allowed:
2916 ExcDoFIsConstrainedFromBothObjects(line.index));
2919 case left_object_wins:
2923 case right_object_wins:
2925 lines[lines_cache[local_line_no]] = {
2928 line.entries.end()),
2929 static_cast<number
>(line.inhomogeneity)};
2939 for (
size_type i = 0; i < lines_cache.size(); ++i)
2941 Assert(i == calculate_line_index(lines[lines_cache[i]].index),
2947 if (object_was_sorted ==
true)
2953template <
typename number>
2954template <
typename MatrixType>
2958 const std::vector<size_type> &local_dof_indices,
2959 MatrixType &global_matrix)
const
2964 distribute_local_to_global(
2971 std::integral_constant<
2973 internal::AffineConstraints::IsBlockMatrix<MatrixType>::value>());
2978template <
typename number>
2979template <
typename MatrixType,
typename VectorType>
2984 const std::vector<size_type> &local_dof_indices,
2985 MatrixType &global_matrix,
2986 VectorType &global_vector,
2987 bool use_inhomogeneities_for_rhs)
const
2991 distribute_local_to_global(
2997 use_inhomogeneities_for_rhs,
2998 std::integral_constant<
3000 internal::AffineConstraints::IsBlockMatrix<MatrixType>::value>());
3005template <
typename number>
3009 const number inhomogeneity)
3012 , inhomogeneity(inhomogeneity)
IndexSet locally_owned_dofs
void add_entries(const size_type constrained_dof_index, const std::vector< std::pair< size_type, number > > &col_weight_pairs)
size_type n_inhomogeneities() const
bool has_inhomogeneities() const
LineRange get_lines() const
void add_line(const size_type line_n)
const IndexSet & get_locally_owned_indices() const
void add_selected_constraints(const AffineConstraints &constraints_in, const IndexSet &filter)
bool can_store_line(const size_type line_n) const
void add_constraint(const size_type constrained_dof, const ArrayView< const std::pair< size_type, number > > &dependencies, const number inhomogeneity=0)
Threads::ThreadLocalStorage< internal::AffineConstraints::ScratchData< number > > scratch_data
friend class AffineConstraints
void merge(const AffineConstraints< other_number > &other_constraints, const MergeConflictBehavior merge_conflict_behavior=no_conflicts_allowed, const bool allow_different_local_lines=false)
void add_entry(const size_type constrained_dof_index, const size_type column, const number weight)
number get_inhomogeneity(const size_type line_n) const
void make_sorted_row_list(const std::vector< size_type > &local_dof_indices, std::vector< size_type > &active_dofs) const
void get_dof_values(const VectorType &global_vector, ForwardIteratorInd local_indices_begin, ForwardIteratorVec local_vector_begin, ForwardIteratorVec local_vector_end) const
void distribute_local_to_global(const InVector &local_vector, const std::vector< size_type > &local_dof_indices, OutVector &global_vector) const
void make_sorted_row_list(const std::vector< size_type > &local_dof_indices, internal::AffineConstraints::GlobalRowsFromLocal< number > &global_rows) const
const IndexSet & get_local_lines() const
void add_entries_local_to_global(const std::vector< size_type > &local_dof_indices, SparsityPatternBase &sparsity_pattern, const bool keep_constrained_entries=true, const Table< 2, bool > &dof_mask=Table< 2, bool >()) const
void shift(const size_type offset)
size_type n_identities() const
std::size_t memory_consumption() const
typename std::vector< ConstraintLine >::const_iterator const_iterator
void set_inhomogeneity(const size_type constrained_dof_index, const number value)
void condense(SparsityPattern &sparsity) const
size_type max_constraint_indirections() const
ProductType< VectorScalar, MatrixScalar >::type resolve_vector_entry(const size_type i, const internal::AffineConstraints::GlobalRowsFromLocal< number > &global_rows, const Vector< VectorScalar > &local_vector, const std::vector< size_type > &local_dof_indices, const FullMatrix< MatrixScalar > &local_matrix) const
void distribute(VectorType &vec) const
void resolve_indices(std::vector< types::global_dof_index > &indices) const
std::vector< ConstraintLine > lines
bool is_constrained(const size_type line_n) const
AffineConstraints get_view(const IndexSet &mask) const
IndexSet needed_elements_for_distribute
void make_consistent_in_parallel(const IndexSet &locally_owned_dofs, const IndexSet &constraints_to_make_consistent, const MPI_Comm mpi_communicator)
const std::vector< std::pair< size_type, number > > * get_constraint_entries(const size_type line_n) const
size_type calculate_line_index(const size_type line_n) const
void distribute_local_to_global(const FullMatrix< number > &local_matrix, const Vector< number > &local_vector, const std::vector< size_type > &local_dof_indices, MatrixType &global_matrix, VectorType &global_vector, const bool use_inhomogeneities_for_rhs, const std::bool_constant< true >) const
bool is_inhomogeneously_constrained(const size_type index) const
void copy_from(const AffineConstraints< other_number > &other)
std::vector< size_type > lines_cache
void constrain_dof_to_zero(const size_type constrained_dof)
void distribute_local_to_global(const FullMatrix< number > &local_matrix, const Vector< number > &local_vector, const std::vector< size_type > &local_dof_indices, MatrixType &global_matrix, VectorType &global_vector, const bool use_inhomogeneities_for_rhs, const std::bool_constant< false >) const
bool is_consistent_in_parallel(const std::vector< IndexSet > &locally_owned_dofs, const IndexSet &locally_active_dofs, const MPI_Comm mpi_communicator, const bool verbose=false) const
bool is_identity_constrained(const size_type line_n) const
size_type n_constraints() const
void print(std::ostream &out) const
void write_dot(std::ostream &) const
void set_zero(VectorType &vec) const
void add_lines(const std::vector< bool > &lines)
boost::iterator_range< const_iterator > LineRange
bool are_identity_constrained(const size_type line_n_1, const size_type line_n_2) const
bool is_subset_of(const IndexSet &other) const
size_type index_within_set(const size_type global_index) const
bool is_element(const size_type index) const
A class that provides a separate storage location on each thread that accesses the object.
#define DEAL_II_NAMESPACE_OPEN
#define DEAL_II_NAMESPACE_CLOSE
static ::ExceptionBase & ExcMatrixNotClosed()
static ::ExceptionBase & ExcGhostsPresent()
#define DeclException4(Exception4, type1, type2, type3, type4, outsequence)
static ::ExceptionBase & ExcDoFIsConstrainedFromBothObjects(size_type arg1)
static ::ExceptionBase & ExcDoFConstrainedToConstrainedDoF(int arg1, int arg2)
#define Assert(cond, exc)
static ::ExceptionBase & ExcMatrixIsClosed()
#define DeclException2(Exception2, type1, type2, outsequence)
static ::ExceptionBase & ExcDoFIsConstrainedToConstrainedDoF(size_type arg1)
static ::ExceptionBase & ExcColumnNotStoredHere(size_type arg1, size_type arg2)
static ::ExceptionBase & ExcRowNotStoredHere(size_type arg1)
#define AssertIndexRange(index, range)
#define DeclExceptionMsg(Exception, defaulttext)
static ::ExceptionBase & ExcInternalError()
static ::ExceptionBase & ExcIncorrectConstraint(int arg1, int arg2)
static ::ExceptionBase & ExcDimensionMismatch(std::size_t arg1, std::size_t arg2)
#define DeclException1(Exception1, type1, outsequence)
static ::ExceptionBase & ExcEntryAlreadyExists(size_type arg1, size_type arg2, number arg3, number arg4)
static ::ExceptionBase & ExcMessage(std::string arg1)
static ::ExceptionBase & ExcLineInexistent(size_type arg1)
#define AssertThrow(cond, exc)
#define DEAL_II_NOT_IMPLEMENTED()
void reinit(MatrixBlock< MatrixType > &v, const BlockSparsityPattern &p)
const types::global_dof_index invalid_dof_index
const types::global_dof_index invalid_size_type
::VectorizedArray< Number, width > max(const ::VectorizedArray< Number, width > &, const ::VectorizedArray< Number, width > &)
::VectorizedArray< Number, width > abs(const ::VectorizedArray< Number, width > &)
unsigned int global_dof_index
*braid_SplitCommworld & comm
std::vector< std::pair< size_type, number > > Entries
void serialize(Archive &ar, const unsigned int)
ConstraintLine & operator=(const ConstraintLine &other)=default
ConstraintLine(const ConstraintLine &other)=default
ConstraintLine(const size_type &index=numbers::invalid_dof_index, const typename AffineConstraints< number >::ConstraintLine::Entries &entries={}, const number inhomogeneity=0.0)
ConstraintLine(ConstraintLine &&other) noexcept=default
ConstraintLine & operator=(ConstraintLine &&other) noexcept=default
std::size_t memory_consumption() const
friend void swap(ConstraintLine &l1, ConstraintLine &l2) noexcept
typename internal::ProductTypeImpl< std::decay_t< T >, std::decay_t< U > >::type type
static void add(const typename VectorType::value_type value, const types::global_dof_index i, VectorType &V)
bool operator<(const SynchronousIterators< Iterators > &a, const SynchronousIterators< Iterators > &b)