13#ifndef dealii_affine_constraints_h
14#define dealii_affine_constraints_h
45template <
typename number>
47template <
typename number>
78 Distributing(
const Distributing &in);
81 operator=(
const Distributing &in);
86 return global_row < in.global_row;
107 template <
typename number>
116 insert_new_index(
const std::pair<size_type, number> &pair);
119 append_index(
const size_type index,
120 const std::pair<size_type, number> &pair);
123 get_size(
const size_type index)
const;
125 const std::pair<size_type, number> *
126 get_entry(
const size_type index)
const;
130 std::vector<std::pair<size_type, number>>
data;
132 std::vector<size_type> individual_size;
165 template <
typename number>
166 class GlobalRowsFromLocal
172 GlobalRowsFromLocal();
175 reinit(
const size_type n_local_rows);
178 insert_index(
const size_type global_row,
179 const size_type local_row,
180 const number constraint_value);
185 print(std::ostream &os);
198 size(
const size_type counter_index)
const;
204 global_row(
const size_type counter_index)
const;
210 global_row(
const size_type counter_index);
218 local_row(
const size_type counter_index)
const;
224 local_row(
const size_type counter_index);
232 local_row(
const size_type counter_index,
233 const size_type index_in_constraint)
const;
240 constraint_value(
const size_type counter_index,
241 const size_type index_in_constraint)
const;
250 have_indirect_rows()
const;
257 insert_constraint(
const size_type constrained_local_dof);
267 n_constraints()
const;
274 n_inhomogeneities()
const;
283 set_ith_constraint_inhomogeneous(
const size_type i);
290 constraint_origin(size_type i)
const;
297 std::vector<Distributing> total_row_indices;
303 DataCache<number> data_cache;
337 template <
typename number>
350 ScratchData(
const ScratchData &)
362 std::vector<std::pair<size_type, size_type>> new_entries;
367 std::vector<size_type> rows;
372 std::vector<size_type> columns;
377 std::vector<number>
values;
382 std::vector<size_type> block_starts;
387 std::vector<size_type> vector_indices;
392 std::vector<number> vector_values;
397 GlobalRowsFromLocal<number> global_rows;
402 GlobalRowsFromLocal<number> global_columns;
409 namespace AffineConstraintsImplementation
411 template <
typename VectorType>
413 set_zero_all(
const std::vector<types::global_dof_index> &cm,
418 set_zero_all(
const std::vector<types::global_dof_index> &cm,
423 set_zero_all(
const std::vector<types::global_dof_index> &cm,
541template <
typename number =
double>
620 "Use the constructor with two index set arguments.")
655 const
IndexSet &locally_stored_constraints);
693 template <typename other_number>
717 "Use the
reinit() function with two index
set arguments.")
731 const
IndexSet &locally_stored_constraints);
851 const number inhomogeneity = 0);
943 const number weight);
1033 template <typename other_number>
1038 const
bool allow_different_local_lines = false);
1353 template <typename VectorType>
1363 template <typename VectorType>
1365 condense(const VectorType &vec_ghosted, VectorType &output) const;
1379 template <typename VectorType>
1387 template <typename BlockVectorType>
1397 template <typename VectorType>
1456 template <class InVector, class OutVector>
1460 OutVector &global_vector) const;
1509 template <typename VectorType>
1513 VectorType &global_vector,
1514 const
FullMatrix<number> &local_matrix) const;
1535 template <typename VectorType>
1538 const
Vector<number> &local_vector,
1541 VectorType &global_vector,
1543 bool diagonal = false) const;
1548 template <typename VectorType>
1552 VectorType &global_vector) const;
1586 template <typename ForwardIteratorVec,
1587 typename ForwardIteratorInd,
1588 typename VectorType>
1591 ForwardIteratorVec local_vector_end,
1592 ForwardIteratorInd local_indices_begin,
1593 VectorType &global_vector) const;
1646 template <typename MatrixType>
1650 MatrixType &global_matrix) const;
1679 template <typename MatrixType>
1684 MatrixType &global_matrix) const;
1702 template <typename MatrixType>
1708 MatrixType &global_matrix) const;
1730 template <typename MatrixType, typename VectorType>
1733 const
Vector<number> &local_vector,
1735 MatrixType &global_matrix,
1736 VectorType &global_vector,
1737 bool use_inhomogeneities_for_rhs = false) const;
1796 const
bool keep_constrained_entries = true,
1797 const
Table<2,
bool> &dof_mask =
Table<2,
bool>()) const;
1807 const
bool keep_constrained_entries = true,
1808 const
Table<2,
bool> &dof_mask =
Table<2,
bool>()) const;
1820 const
bool keep_constrained_entries = true,
1821 const
Table<2,
bool> &dof_mask =
Table<2,
bool>()) const;
1842 template <typename ForwardIteratorVec,
1843 typename ForwardIteratorInd,
1844 typename VectorType>
1847 ForwardIteratorInd local_indices_begin,
1848 ForwardIteratorVec local_vector_begin,
1849 ForwardIteratorVec local_vector_end) const;
1872 template <typename VectorType>
1889 using Entries = std::vector<std::pair<size_type, number>>;
1918 const number inhomogeneity = 0.0);
1954 template <
class Archive>
1958 ar &index &entries &inhomogeneity;
1967 std::swap(l1.index, l2.index);
1968 std::swap(l1.entries, l2.entries);
1969 std::swap(l1.inhomogeneity, l2.inhomogeneity);
2024 const IndexSet &locally_active_dofs,
2026 const bool verbose =
false)
const;
2089 const IndexSet &constraints_to_make_consistent,
2099 "You are attempting an operation on an AffineConstraints object "
2100 "that requires the object to not be 'closed', i.e., for which you "
2101 "must not already have called the close() member function. But the "
2102 "object is already closed, and so the operation can not be "
2111 "You are attempting an operation on an AffineConstraints object "
2112 "that requires the object to be 'closed', i.e., for which you "
2113 "needed to call the close() member function. But the object "
2114 "is not currently closed, and so the operation can not be "
2123 <<
"The specified line " << arg1 <<
" does not exist.");
2134 <<
"The entry for the indices " << arg1 <<
" and " << arg2
2135 <<
" already exists, but the values " << arg3 <<
" (old) and "
2136 << arg4 <<
" (new) differ "
2137 <<
"by " << (arg4 - arg3) <<
'.');
2146 <<
"You tried to constrain DoF " << arg1 <<
" to DoF " << arg2
2147 <<
", but that one is also constrained. This is not allowed!");
2155 <<
"Degree of freedom " << arg1
2156 <<
" is constrained from both object in a merge operation.");
2164 <<
"In the given argument a degree of freedom is constrained "
2165 <<
"to another DoF with number " << arg1
2166 <<
", which however is constrained by this object. This is not"
2175 <<
"The index set given to this constraints object indicates "
2176 <<
"constraints for degree of freedom " << arg1
2177 <<
" should not be stored by this object, but a constraint "
2178 <<
"is being added.");
2188 <<
"The index set given to this constraints object indicates "
2189 <<
"constraints using degree of freedom " << arg2
2190 <<
" should not be stored by this object, but a constraint "
2191 <<
"for degree of freedom " << arg1 <<
" uses it.");
2201 <<
"While distributing the constraint for DoF " << arg1
2202 <<
", it turns out that one of the processors "
2203 <<
"who own the " << arg2 <<
" degrees of freedom that x_"
2204 << arg1 <<
" is constrained against does not know about "
2205 <<
"the constraint on x_" << arg1
2206 <<
". Did you not initialize the AffineConstraints container "
2207 <<
"with the appropriate locally_relevant set so "
2208 <<
"that every processor who owns a DoF that constrains "
2209 <<
"another DoF also knows about this constraint?");
2293 internal::AffineConstraints::ScratchData<number>>
2307 template <
typename MatrixType,
typename VectorType>
2311 const std::vector<size_type> &local_dof_indices,
2312 MatrixType &global_matrix,
2313 VectorType &global_vector,
2314 const bool use_inhomogeneities_for_rhs,
2315 const std::bool_constant<false>)
const;
2321 template <
typename MatrixType,
typename VectorType>
2325 const std::vector<size_type> &local_dof_indices,
2326 MatrixType &global_matrix,
2327 VectorType &global_vector,
2328 const bool use_inhomogeneities_for_rhs,
2329 const std::bool_constant<true>)
const;
2340 internal::AffineConstraints::GlobalRowsFromLocal<number>
2341 &global_rows)
const;
2352 std::vector<size_type> &active_dofs)
const;
2357 template <
typename MatrixScalar,
typename VectorScalar>
2361 const internal::AffineConstraints::GlobalRowsFromLocal<number> &global_rows,
2363 const std::vector<size_type> &local_dof_indices,
2369template <
typename number>
2376template <
typename number>
2378 const IndexSet &locally_stored_constraints)
2380 locally_stored_constraints)
2385template <
typename number>
2388 const IndexSet &locally_stored_constraints)
2395 ExcMessage(
"The set of locally stored constraints needs to be a "
2396 "superset of the locally owned DoFs."));
2406template <
typename number>
2421template <
typename number>
2445 lines.emplace_back();
2446 lines.back().index = line_n;
2447 lines.back().inhomogeneity = 0.;
2453template <
typename number>
2457 const number weight)
2460 Assert(constrained_dof_index != column,
2461 ExcMessage(
"Can't constrain a degree of freedom to itself"));
2466 ExcMessage(
"The current AffineConstraints does not contain the line "
2467 "for the current entry. Call AffineConstraints::add_line "
2468 "before calling this function."));
2481 for (
const auto &p : line_ptr->
entries)
2482 if (p.first == column)
2486 constrained_dof_index, column, p.second, weight));
2490 line_ptr->
entries.emplace_back(column, weight);
2495template <
typename number>
2504 ExcMessage(
"call add_line() before calling set_inhomogeneity()"));
2512template <
typename number>
2513template <
typename VectorType>
2519 std::vector<size_type> constrained_lines(
lines.size());
2520 for (
unsigned int i = 0; i <
lines.size(); ++i)
2521 constrained_lines[i] =
lines[i].index;
2522 internal::AffineConstraintsImplementation::set_zero_all(constrained_lines,
2526template <
typename number>
2530 return lines.size();
2533template <
typename number>
2537 return std::count_if(
lines.begin(),
2540 return (line.entries.size() == 1) &&
2541 (line.entries[0].second == number(1.));
2545template <
typename number>
2549 return std::count_if(
lines.begin(),
2552 return (line.inhomogeneity != number(0.));
2556template <
typename number>
2568template <
typename number>
2586template <
typename number>
2587inline const std::vector<std::pair<types::global_dof_index, number>> *
2603template <
typename number>
2617template <
typename number>
2632template <
typename number>
2641template <
typename number>
2650template <
typename number>
2659template <
typename number>
2660template <
typename VectorType>
2665 VectorType &global_vector)
const
2670 global_vector(index) += value;
2676 global_vector(position.
entries[j].first) +=
2677 value * position.
entries[j].second;
2681template <
typename number>
2682template <
typename ForwardIteratorVec,
2683 typename ForwardIteratorInd,
2684 typename VectorType>
2687 ForwardIteratorVec local_vector_begin,
2688 ForwardIteratorVec local_vector_end,
2689 ForwardIteratorInd local_indices_begin,
2690 VectorType &global_vector)
const
2693 for (; local_vector_begin != local_vector_end;
2694 ++local_vector_begin, ++local_indices_begin)
2698 *local_indices_begin,
2706 (*local_vector_begin) * position.
entries[j].second,
2713template <
typename number>
2714template <
class InVector,
class OutVector>
2717 const InVector &local_vector,
2718 const std::vector<size_type> &local_dof_indices,
2719 OutVector &global_vector)
const
2722 Assert(local_vector.size() == local_dof_indices.size(),
2726 local_dof_indices.begin(),
2730template <
typename number>
2731template <
typename ForwardIteratorVec,
2732 typename ForwardIteratorInd,
2733 typename VectorType>
2736 const VectorType &global_vector,
2737 ForwardIteratorInd local_indices_begin,
2738 ForwardIteratorVec local_vector_begin,
2739 ForwardIteratorVec local_vector_end)
const
2742 for (; local_vector_begin != local_vector_end;
2743 ++local_vector_begin, ++local_indices_begin)
2746 *local_vector_begin = global_vector(*local_indices_begin);
2751 typename VectorType::value_type value = position.
inhomogeneity;
2753 value += (global_vector(position.
entries[j].first) *
2755 *local_vector_begin = value;
2762template <
typename MatrixType>
2764template <
typename SparsityPatternType>
2766template <
typename number>
2790 template <
typename MatrixType>
2791 struct IsBlockMatrix
2797 template <
typename T>
2798 static std::true_type
2805 static std::false_type
2815 static const bool value =
2816 std::is_same_v<decltype(check(std::declval<MatrixType *>())),
2821 template <
typename MatrixType>
2822 const bool IsBlockMatrix<MatrixType>::value;
2830template <
typename number>
2831template <
typename other_number>
2837 lines.reserve(other.
lines.size());
2839 for (
const auto &l : other.
lines)
2840 lines.emplace_back(l.index,
2854template <
typename number>
2855template <
typename other_number>
2860 const bool allow_different_local_lines)
2862 Assert(allow_different_local_lines ||
2865 "local_lines for this and the other objects are not the same "
2866 "although allow_different_local_lines is false."));
2869 const bool object_was_sorted = sorted;
2884 for (
const std::pair<size_type, number> &entry : line.entries)
2893 ((merge_conflict_behavior != right_object_wins) &&
2895 this->is_constrained(entry.first)))
2896 tmp.push_back(entry);
2902 const auto *other_entries =
2906 const number weight = entry.second;
2908 for (
const auto &other_entry : *other_entries)
2909 tmp.emplace_back(other_entry.first,
2910 other_entry.second * weight);
2912 line.inhomogeneity +=
2917 line.entries.swap(tmp);
2920 if (local_lines.size() != 0)
2921 local_lines.add_indices(other_constraints.
local_lines);
2926 std::fill(lines_cache.begin(),
2934 const size_type local_line_no = calculate_line_index(line.index);
2935 if (local_line_no >= lines_cache.size())
2937 lines_cache[local_line_no] = index++;
2941 for (
const auto &line : other_constraints.
lines)
2943 const size_type local_line_no = calculate_line_index(line.index);
2944 if (local_line_no >= lines_cache.size())
2947 lines.emplace_back(line.index,
2949 line.entries.begin(), line.entries.end()),
2950 line.inhomogeneity);
2951 lines_cache[local_line_no] = index++;
2956 lines.emplace_back(line.index,
2958 line.entries.begin(), line.entries.end()),
2959 line.inhomogeneity);
2961 lines_cache[local_line_no] = index++;
2966 switch (merge_conflict_behavior)
2968 case no_conflicts_allowed:
2970 ExcDoFIsConstrainedFromBothObjects(line.index));
2973 case left_object_wins:
2977 case right_object_wins:
2979 lines[lines_cache[local_line_no]] = {
2982 line.entries.end()),
2983 static_cast<number
>(line.inhomogeneity)};
2993 for (
size_type i = 0; i < lines_cache.size(); ++i)
2995 Assert(i == calculate_line_index(lines[lines_cache[i]].index),
3001 if (object_was_sorted ==
true)
3007template <
typename number>
3008template <
typename MatrixType>
3012 const std::vector<size_type> &local_dof_indices,
3013 MatrixType &global_matrix)
const
3018 distribute_local_to_global(
3026 internal::AffineConstraints::IsBlockMatrix<MatrixType>::value>());
3031template <
typename number>
3032template <
typename MatrixType,
typename VectorType>
3037 const std::vector<size_type> &local_dof_indices,
3038 MatrixType &global_matrix,
3039 VectorType &global_vector,
3040 bool use_inhomogeneities_for_rhs)
const
3044 distribute_local_to_global(
3050 use_inhomogeneities_for_rhs,
3052 internal::AffineConstraints::IsBlockMatrix<MatrixType>::value>());
3057template <
typename number>
3061 const number inhomogeneity)
3064 , inhomogeneity(inhomogeneity)
* x_component_mask set(0, true)
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_DEPRECATED_WITH_COMMENT(comment)
#define DEAL_II_NAMESPACE_CLOSE
#define DEAL_II_NOT_IMPLEMENTED()
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)
std::vector< index_type > data
types::global_dof_index size_type
void reinit(MatrixBlock< MatrixType > &v, const BlockSparsityPattern &p)
constexpr types::global_dof_index invalid_dof_index
constexpr 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
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)
std::vector< std::pair< size_type, number > > Entries
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)